Importing pgnus-0.79
[elisp/gnus.git-] / lisp / mml.el
1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'mm-util)
27 (require 'mm-bodies)
28 (require 'mm-encode)
29
30 (eval-and-compile
31   (autoload 'message-make-message-id "message"))
32
33 (defvar mml-syntax-table
34   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
35     (modify-syntax-entry ?\\ "/" table)
36     (modify-syntax-entry ?< "(" table)
37     (modify-syntax-entry ?> ")" table)
38     (modify-syntax-entry ?@ "w" table)
39     (modify-syntax-entry ?/ "w" table)
40     (modify-syntax-entry ?= " " table)
41     (modify-syntax-entry ?* " " table)
42     (modify-syntax-entry ?\; " " table)
43     (modify-syntax-entry ?\' " " table)
44     table))
45
46 (defun mml-parse ()
47   "Parse the current buffer as an MML document."
48   (goto-char (point-min))
49   (let ((table (syntax-table)))
50     (unwind-protect
51         (progn
52           (set-syntax-table mml-syntax-table)
53           (mml-parse-1))
54       (set-syntax-table table))))
55
56 (defun mml-parse-1 ()
57   "Parse the current buffer as an MML document."
58   (let (struct tag point contents charsets warn)
59     (while (and (not (eobp))
60                 (not (looking-at "<#/multipart")))
61       (cond
62        ((looking-at "<#multipart")
63         (push (nconc (mml-read-tag) (mml-parse-1)) struct))
64        ((looking-at "<#external")
65         (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
66               struct))
67        (t
68         (if (looking-at "<#part")
69             (setq tag (mml-read-tag))
70           (setq tag (list 'part '(type . "text/plain"))
71                 warn t))
72         (setq point (point)
73               contents (mml-read-part)
74               charsets (mm-find-mime-charset-region point (point)))
75         (if (< (length charsets) 2)
76             (push (nconc tag (list (cons 'contents contents)))
77                   struct)
78           (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
79                           tag point (point))))
80             (when (and warn
81                        (not
82                         (y-or-n-p
83                          (format
84                           "Warning: Your message contains %d parts.  Really send? "
85                           (length nstruct)))))
86               (error "Edit your message to use only one charset"))
87             (setq struct (nconc nstruct struct)))))))
88     (unless (eobp)
89       (forward-line 1))
90     (nreverse struct)))
91
92 (defun mml-parse-singlepart-with-multiple-charsets (orig-tag beg end)
93   (save-excursion
94     (narrow-to-region beg end)
95     (goto-char (point-min))
96     (let ((current (mm-mime-charset (char-charset (following-char))))
97           charset struct space newline paragraph)
98       (while (not (eobp))
99         (cond
100          ;; The charset remains the same.
101          ((or (eq (setq charset (mm-mime-charset
102                                  (char-charset (following-char)))) 'us-ascii)
103               (eq charset current)))
104          ;; The initial charset was ascii.
105          ((eq current 'us-ascii)
106           (setq current charset
107                 space nil
108                 newline nil
109                 paragraph nil))
110          ;; We have a change in charsets.
111          (t
112           (push (append
113                  orig-tag
114                  (list (cons 'contents
115                              (buffer-substring-no-properties
116                               beg (or paragraph newline space (point))))))
117                 struct)
118           (setq beg (or paragraph newline space (point))
119                 current charset
120                 space nil
121                 newline nil
122                 paragraph nil)))
123         ;; Compute places where it might be nice to break the part.
124         (cond
125          ((memq (following-char) '(?  ?\t))
126           (setq space (1+ (point))))
127          ((eq (following-char) ?\n)
128           (setq newline (1+ (point))))
129          ((and (eq (following-char) ?\n)
130                (not (bobp))
131                (eq (char-after (1- (point))) ?\n))
132           (setq paragraph (point))))
133         (forward-char 1))
134       ;; Do the final part.
135       (unless (= beg (point))
136         (push (append orig-tag
137                       (list (cons 'contents
138                                   (buffer-substring-no-properties
139                                    beg (point)))))
140               struct))
141       struct)))
142
143 (defun mml-read-tag ()
144   "Read a tag and return the contents."
145   (let (contents name elem val)
146     (forward-char 2)
147     (setq name (buffer-substring-no-properties
148                 (point) (progn (forward-sexp 1) (point))))
149     (skip-chars-forward " \t\n")
150     (while (not (looking-at ">"))
151       (setq elem (buffer-substring-no-properties
152                   (point) (progn (forward-sexp 1) (point))))
153       (skip-chars-forward "= \t\n")
154       (setq val (buffer-substring-no-properties
155                  (point) (progn (forward-sexp 1) (point))))
156       (when (string-match "^\"\\(.*\\)\"$" val)
157         (setq val (match-string 1 val)))
158       (push (cons (intern elem) val) contents)
159       (skip-chars-forward " \t\n"))
160     (forward-char 1)
161     (skip-chars-forward " \t\n")
162     (cons (intern name) (nreverse contents))))
163
164 (defun mml-read-part ()
165   "Return the buffer up till the next part, multipart or closing part or multipart."
166   (let ((beg (point)))
167     ;; If the tag ended at the end of the line, we go to the next line.
168     (when (looking-at "[ \t]*\n")
169       (forward-line 1))
170     (if (re-search-forward
171          "<#\\(/\\)?\\(multipart\\|part\\|external\\)." nil t)
172         (prog1
173             (buffer-substring-no-properties beg (match-beginning 0))
174           (if (or (not (match-beginning 1))
175                   (equal (match-string 2) "multipart"))
176               (goto-char (match-beginning 0))
177             (when (looking-at "[ \t]*\n")
178               (forward-line 1))))
179       (buffer-substring-no-properties beg (goto-char (point-max))))))
180
181 (defvar mml-boundary nil)
182 (defvar mml-base-boundary "-=-=")
183 (defvar mml-multipart-number 0)
184
185 (defun mml-generate-mime ()
186   "Generate a MIME message based on the current MML document."
187   (let ((cont (mml-parse))
188         (mml-multipart-number 0))
189     (if (not cont)
190         nil
191       (with-temp-buffer
192         (if (and (consp (car cont))
193                  (= (length cont) 1))
194             (mml-generate-mime-1 (car cont))
195           (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
196                                       cont)))
197         (buffer-string)))))
198
199 (defun mml-generate-mime-1 (cont)
200   (cond
201    ((eq (car cont) 'part)
202     (let (coded encoding charset filename type)
203       (setq type (or (cdr (assq 'type cont)) "text/plain"))
204       (if (equal (car (split-string type "/")) "text")
205           (with-temp-buffer
206             (cond
207              ((cdr (assq 'buffer cont))
208               (insert-buffer-substring (cdr (assq 'buffer cont))))
209              ((setq filename (cdr (assq 'filename cont)))
210               (insert-file-contents-literally filename))
211              (t
212               (save-restriction
213                 (narrow-to-region (point) (point))
214                 (insert (cdr (assq 'contents cont)))
215                 ;; Remove quotes from quoted tags.
216                 (goto-char (point-min))
217                 (while (re-search-forward
218                         "<#!+/?\\(part\\|multipart\\|external\\)" nil t)
219                   (delete-region (+ (match-beginning 0) 2)
220                                  (+ (match-beginning 0) 3))))))
221             (setq charset (mm-encode-body))
222             (setq encoding (mm-body-encoding charset))
223             (setq coded (buffer-string)))
224         (mm-with-unibyte-buffer
225           (cond
226            ((cdr (assq 'buffer cont))
227             (insert-buffer-substring (cdr (assq 'buffer cont))))
228            ((setq filename (cdr (assq 'filename cont)))
229             (insert-file-contents-literally filename))
230            (t
231             (insert (cdr (assq 'contents cont)))))
232           (setq encoding (mm-encode-buffer type)
233                 coded (buffer-string))))
234       (mml-insert-mime-headers cont type charset encoding)
235       (insert "\n")
236       (insert coded)))
237    ((eq (car cont) 'external)
238     (insert "Content-Type: message/external-body")
239     (let ((parameters (mml-parameter-string
240                        cont '(expiration size permission)))
241           (name (cdr (assq 'name cont))))
242       (when name
243         (setq name (mml-parse-file-name name))
244         (if (stringp name)
245             (insert ";\n " (mail-header-encode-parameter "name" name)
246                     "\";\n access-type=local-file")
247           (insert
248            (format ";\n "
249                    (mail-header-encode-parameter
250                     "name" (file-name-nondirectory (nth 2 name)))
251                    (mail-header-encode-parameter "site" (nth 1 name))
252                    (mail-header-encode-parameter
253                     "directory" (file-name-directory (nth 2 name)))))
254           (insert ";\n access-type="
255                   (if (member (nth 0 name) '("ftp@" "anonymous@"))
256                       "anon-ftp"
257                     "ftp"))))
258       (when parameters
259         (insert parameters)))
260     (insert "\n\n")
261     (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
262     (insert "Content-ID: " (message-make-message-id) "\n")
263     (insert "Content-Transfer-Encoding: "
264             (or (cdr (assq 'encoding cont)) "binary"))
265     (insert "\n\n")
266     (insert (or (cdr (assq 'contents cont))))
267     (insert "\n"))
268    ((eq (car cont) 'multipart)
269     (let ((mml-boundary (mml-compute-boundary cont)))
270       (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
271                       (or (cdr (assq 'type cont)) "mixed")
272                       mml-boundary))
273       (insert "\n")
274       (setq cont (cddr cont))
275       (while cont
276         (insert "\n--" mml-boundary "\n")
277         (mml-generate-mime-1 (pop cont)))
278       (insert "\n--" mml-boundary "--\n")))
279    (t
280     (error "Invalid element: %S" cont))))
281
282 (defun mml-compute-boundary (cont)
283   "Return a unique boundary that does not exist in CONT."
284   (let ((mml-boundary (mml-make-boundary)))
285     ;; This function tries again and again until it has found
286     ;; a unique boundary.
287     (while (not (catch 'not-unique
288                   (mml-compute-boundary-1 cont))))
289     mml-boundary))
290
291 (defun mml-compute-boundary-1 (cont)
292   (let (filename)
293     (cond
294      ((eq (car cont) 'part)
295       (with-temp-buffer
296         (cond
297          ((cdr (assq 'buffer cont))
298           (insert-buffer-substring (cdr (assq 'buffer cont))))
299          ((setq filename (cdr (assq 'filename cont)))
300           (insert-file-contents-literally filename))
301          (t
302           (insert (cdr (assq 'contents cont)))))
303         (goto-char (point-min))
304         (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
305                                  nil t)
306           (setq mml-boundary (mml-make-boundary))
307           (throw 'not-unique nil))))
308      ((eq (car cont) 'multipart)
309       (mapcar 'mml-compute-boundary-1 (cddr cont))))
310     t))
311
312 (defun mml-make-boundary ()
313   (concat (make-string (% (incf mml-multipart-number) 60) ?=)
314           (if (> mml-multipart-number 17)
315               (format "%x" mml-multipart-number)
316             "")
317           mml-base-boundary))
318
319 (defun mml-make-string (num string)
320   (let ((out ""))
321     (while (not (zerop (decf num)))
322       (setq out (concat out string)))
323     out))
324
325 (defun mml-insert-mime-headers (cont type charset encoding)
326   (let (parameters disposition description)
327     (setq parameters
328           (mml-parameter-string
329            cont '(name access-type expiration size permission)))
330     (when (or charset
331               parameters
332               (not (equal type "text/plain")))
333       (when (consp charset)
334         (error
335          "Can't encode a part with several charsets."))
336       (insert "Content-Type: " type)
337       (when charset
338         (insert "; " (mail-header-encode-parameter
339                       "charset" (symbol-name charset))))
340       (when parameters
341         (insert parameters))
342       (insert "\n"))
343     (setq parameters
344           (mml-parameter-string
345            cont '(filename creation-date modification-date read-date)))
346     (when (or (setq disposition (cdr (assq 'disposition cont)))
347               parameters)
348       (insert "Content-Disposition: " (or disposition "inline"))
349       (when parameters
350         (insert parameters))
351       (insert "\n"))
352     (unless (eq encoding '7bit)
353       (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
354     (when (setq description (cdr (assq 'description cont)))
355       (insert "Content-Description: "
356               (mail-encode-encoded-word-string description) "\n"))))
357
358 (defun mml-parameter-string (cont types)
359   (let ((string "")
360         value type)
361     (while (setq type (pop types))
362       (when (setq value (cdr (assq type cont)))
363         ;; Strip directory component from the filename parameter.
364         (when (eq type 'filename)
365           (setq value (file-name-nondirectory value)))
366         (setq string (concat string ";\n "
367                              (mail-header-encode-parameter
368                               (symbol-name type) value)))))
369     (when (not (zerop (length string)))
370       string)))
371
372 (defvar ange-ftp-path-format)
373 (defvar efs-path-regexp)
374 (defun mml-parse-file-name (path)
375   (if (if (boundp 'efs-path-regexp)
376           (string-match efs-path-regexp path)
377         (if (boundp 'ange-ftp-path-format)
378             (string-match (car ange-ftp-path-format))))
379       (list (match-string 1 path) (match-string 2 path)
380             (substring path (1+ (match-end 2))))
381     path))
382
383 (defun mml-insert-buffer (buffer)
384   "Insert BUFFER at point and quote any MML markup."
385   (save-restriction
386     (narrow-to-region (point) (point))
387     (insert-buffer-substring buffer)
388     (mml-quote-region (point-min) (point-max))
389     (goto-char (point-max))))
390
391 ;;;
392 ;;; Transforming MIME to MML
393 ;;;
394
395 (defun mime-to-mml ()
396   "Translate the current buffer (which should be a message) into MML."
397   ;; First decode the head.
398   (save-restriction
399     (message-narrow-to-head)
400     (mail-decode-encoded-word-region (point-min) (point-max)))
401   (let ((handles (mm-dissect-buffer t)))
402     (goto-char (point-min))
403     (search-forward "\n\n" nil t)
404     (delete-region (point) (point-max))
405     (if (stringp (car handles))
406         (mml-insert-mime handles)
407       (mml-insert-mime handles t))
408     (mm-destroy-parts handles)))
409
410 (defun mml-to-mime ()
411   "Translate the current buffer from MML to MIME."
412   (message-encode-message-body)
413   (save-restriction
414     (message-narrow-to-headers)
415     (mail-encode-encoded-word-buffer)))
416
417 (defun mml-insert-mime (handle &optional no-markup)
418   (let (textp buffer)
419     ;; Determine type and stuff.
420     (unless (stringp (car handle))
421       (unless (setq textp (equal
422                            (car (split-string
423                                  (car (mm-handle-type handle)) "/"))
424                            "text"))
425         (save-excursion
426           (set-buffer (setq buffer (generate-new-buffer " *mml*")))
427           (mm-insert-part handle))))
428     (unless no-markup
429       (mml-insert-mml-markup handle buffer))
430     (cond
431      ((stringp (car handle))
432       (mapcar 'mml-insert-mime (cdr handle))
433       (insert "<#/multipart>\n"))
434      (textp
435       (mm-insert-part handle)
436       (goto-char (point-max)))
437      (t
438       (insert "<#/part>\n")))))
439
440 (defun mml-insert-mml-markup (handle &optional buffer)
441   "Take a MIME handle and insert an MML tag."
442   (if (stringp (car handle))
443       (insert "<#multipart type=" (cadr (split-string (car handle) "/"))
444               ">\n")
445     (insert "<#part type=" (car (mm-handle-type handle)))
446     (dolist (elem (append (cdr (mm-handle-type handle))
447                           (cdr (mm-handle-disposition handle))))
448       (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
449     (when buffer
450       (insert " buffer=\"" (buffer-name buffer) "\""))
451     (when (mm-handle-description handle)
452       (insert " description=\"" (mm-handle-description handle) "\""))
453     (equal (split-string (car (mm-handle-type handle)) "/") "text")
454     (insert ">\n")))
455
456 ;;;
457 ;;; Mode for inserting and editing MML forms
458 ;;;
459
460 (defvar mml-mode-map
461   (let ((map (make-sparse-keymap))
462         (main (make-sparse-keymap)))
463     (define-key map "f" 'mml-attach-file)
464     (define-key map "b" 'mml-attach-buffer)
465     (define-key map "q" 'mml-quote-region)
466     (define-key map "m" 'mml-insert-multipart)
467     (define-key map "q" 'mml-insert-part)
468     (define-key map "v" 'mml-validate)
469     (define-key main "\M-m" map)
470     main))
471
472 (easy-menu-define
473  mml-menu mml-mode-map ""
474  '("MML"
475    ("Attach"
476     ["File" mml-attach-file t]
477     ["Buffer" mml-attach-buffer t])
478    ("Insert"
479     ["Multipart" mml-insert-multipart t]
480     ["Part" mml-insert-part t])
481    ["Quote" mml-quote-region t]
482    ["Validate" mml-validate t]))
483
484 (defvar mml-mode nil
485   "Minor mode for editing MML.")
486
487 (defun mml-mode (&optional arg)
488   "Minor mode for editing MML.
489
490 \\{mml-mode-map}"
491   (interactive "P")
492   (if (not (set (make-local-variable 'mml-mode)
493                 (if (null arg) (not mml-mode)
494                   (> (prefix-numeric-value arg) 0))))
495       nil
496     (set (make-local-variable 'mml-mode) t)
497     (unless (assq 'mml-mode minor-mode-alist)
498       (push `(mml-mode " MML") minor-mode-alist))
499     (unless (assq 'mml-mode minor-mode-map-alist)
500       (push (cons 'mml-mode mml-mode-map)
501             minor-mode-map-alist)))
502   (run-hooks 'mml-mode-hook))
503
504 (defun mml-read-file (prompt)
505   (let ((file (read-file-name prompt nil nil t)))
506     ;; Prevent some common errors.  This is inspired by similar code in
507     ;; VM.
508     (when (file-directory-p file)
509       (error "%s is a directory, cannot attach" file))
510     (unless (file-exists-p file)
511       (error "No such file: %s" file))
512     (unless (file-readable-p file)
513       (error "Permission denied: %s" file))
514     file))
515
516 (defun mml-read-type (file)
517   (let* ((default (or (mm-default-file-encoding file)
518                       ;; Perhaps here we should check what the file
519                       ;; looks like, and offer text/plain if it looks
520                       ;; like text/plain.
521                       "application/octet-stream"))
522          (string (completing-read
523                   (format "Content type (default %s): " default)
524                   (delete-duplicates
525                    (mapcar (lambda (m) (list (cdr m))) mailcap-mime-extensions)
526                    :test 'equal))))
527     (if (not (equal string ""))
528         string
529       default)))
530
531 (defun mml-read-description ()
532   (let ((description (read-string "One line description: ")))
533     (when (string-match "\\`[ \t]*\\'" description)
534       (setq description nil))
535     description))
536
537 (defun mml-quote-region (beg end)
538   "Quote the MML tags in the region."
539   (interactive "r")
540   (save-excursion
541     (goto-char beg)
542     ;; Quote parts.
543     (while (re-search-forward
544             "<#/?!*\\(multipart\\|part\\|external\\)" end t)
545       (goto-char (match-beginning 1))
546       (insert "!"))))
547
548 (defun mml-attach-file (file &optional type description)
549   "Attach a file to the outgoing MIME message.
550 The file is not inserted or encoded until you send the message with
551 `\\[message-send-and-exit]' or `\\[message-send]'.
552
553 FILE is the name of the file to attach.  TYPE is its content-type, a
554 string of the form \"type/subtype\".  DESCRIPTION is a one-line
555 description of the attachment."
556   (interactive
557    (let* ((file (mml-read-file "Attach file: "))
558           (type (mml-read-type file))
559           (description (mml-read-description)))
560      (list file type description)))
561   (insert
562    (format
563     "<#part type=%s name=%s filename=%s%s disposition=attachment><#/part>\n"
564     type (prin1-to-string (file-name-nondirectory file))
565     (prin1-to-string file)
566     (if description
567         (format " description=%s" (prin1-to-string description))
568       ""))))
569
570 (defun mml-attach-external (file &optional type description)
571   "Attach an external file into the buffer.
572 FILE is an ange-ftp/efs specification of the part location.
573 TYPE is the MIME type to use."
574   (interactive
575    (let* ((file (mml-read-file "Attach external file: "))
576           (type (mml-read-type file))
577           (description (mml-read-description)))
578      (list file type description)))
579   (insert (format
580            "<#external type=%s name=%s disposition=attachment><#/external>\n"
581            type (prin1-to-string file))))
582
583
584 (provide 'mml)
585
586 ;;; mml.el ends here