1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
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)
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.
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.
32 (autoload 'message-make-message-id "message"))
34 (defvar mml-generate-multipart-alist
35 '(("signed" . rfc2015-generate-signed-multipart)
36 ("encrypted" . rfc2015-generate-encrypted-multipart))
37 "*Alist of multipart generation functions.
39 Each entry has the form (NAME . FUNCTION), where
40 NAME: is a string containing the name of the part (without the
41 leading \"/multipart/\"),
42 FUNCTION: is a Lisp function which is called to generate the part.
44 The Lisp function has to supply the appropriate MIME headers and the
45 contents of this part.")
47 (defvar mml-syntax-table
48 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
49 (modify-syntax-entry ?\\ "/" table)
50 (modify-syntax-entry ?< "(" table)
51 (modify-syntax-entry ?> ")" table)
52 (modify-syntax-entry ?@ "w" table)
53 (modify-syntax-entry ?/ "w" table)
54 (modify-syntax-entry ?= " " table)
55 (modify-syntax-entry ?* " " table)
56 (modify-syntax-entry ?\; " " table)
57 (modify-syntax-entry ?\' " " table)
61 "Parse the current buffer as an MML document."
62 (goto-char (point-min))
63 (let ((table (syntax-table)))
66 (set-syntax-table mml-syntax-table)
68 (set-syntax-table table))))
71 "Parse the current buffer as an MML document."
72 (let (struct tag point contents charsets warn)
73 (while (and (not (eobp))
74 (not (looking-at "<#/multipart")))
76 ((looking-at "<#multipart")
77 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
78 ((looking-at "<#external")
79 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
82 (if (looking-at "<#part")
83 (setq tag (mml-read-tag))
84 (setq tag (list 'part '(type . "text/plain"))
87 contents (mml-read-part)
88 charsets (mm-find-mime-charset-region point (point)))
89 (if (< (length charsets) 2)
90 (push (nconc tag (list (cons 'contents contents)))
92 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
98 "Warning: Your message contains %d parts. Really send? "
100 (error "Edit your message to use only one charset"))
101 (setq struct (nconc nstruct struct)))))))
106 (defun mml-parse-singlepart-with-multiple-charsets (orig-tag beg end)
108 (narrow-to-region beg end)
109 (goto-char (point-min))
110 (let ((current (mm-mime-charset (char-charset (following-char))))
111 charset struct space newline paragraph)
114 ;; The charset remains the same.
115 ((or (eq (setq charset (mm-mime-charset
116 (char-charset (following-char)))) 'us-ascii)
117 (eq charset current)))
118 ;; The initial charset was ascii.
119 ((eq current 'us-ascii)
120 (setq current charset
124 ;; We have a change in charsets.
128 (list (cons 'contents
129 (buffer-substring-no-properties
130 beg (or paragraph newline space (point))))))
132 (setq beg (or paragraph newline space (point))
137 ;; Compute places where it might be nice to break the part.
139 ((memq (following-char) '(? ?\t))
140 (setq space (1+ (point))))
141 ((eq (following-char) ?\n)
142 (setq newline (1+ (point))))
143 ((and (eq (following-char) ?\n)
145 (eq (char-after (1- (point))) ?\n))
146 (setq paragraph (point))))
148 ;; Do the final part.
149 (unless (= beg (point))
150 (push (append orig-tag
151 (list (cons 'contents
152 (buffer-substring-no-properties
157 (defun mml-read-tag ()
158 "Read a tag and return the contents."
159 (let (contents name elem val)
161 (setq name (buffer-substring-no-properties
162 (point) (progn (forward-sexp 1) (point))))
163 (skip-chars-forward " \t\n")
164 (while (not (looking-at ">"))
165 (setq elem (buffer-substring-no-properties
166 (point) (progn (forward-sexp 1) (point))))
167 (skip-chars-forward "= \t\n")
168 (setq val (buffer-substring-no-properties
169 (point) (progn (forward-sexp 1) (point))))
170 (when (string-match "^\"\\(.*\\)\"$" val)
171 (setq val (match-string 1 val)))
172 (push (cons (intern elem) val) contents)
173 (skip-chars-forward " \t\n"))
175 (skip-chars-forward " \t\n")
176 (cons (intern name) (nreverse contents))))
178 (defun mml-read-part ()
179 "Return the buffer up till the next part, multipart or closing part or multipart."
181 ;; If the tag ended at the end of the line, we go to the next line.
182 (when (looking-at "[ \t]*\n")
184 (if (re-search-forward
185 "<#\\(/\\)?\\(multipart\\|part\\|external\\)." nil t)
187 (buffer-substring-no-properties beg (match-beginning 0))
188 (if (or (not (match-beginning 1))
189 (equal (match-string 2) "multipart"))
190 (goto-char (match-beginning 0))
191 (when (looking-at "[ \t]*\n")
193 (buffer-substring-no-properties beg (goto-char (point-max))))))
195 (defvar mml-boundary nil)
196 (defvar mml-base-boundary "-=-=")
197 (defvar mml-multipart-number 0)
199 (defun mml-generate-mime ()
200 "Generate a MIME message based on the current MML document."
201 (let ((cont (mml-parse))
202 (mml-multipart-number 0))
206 (if (and (consp (car cont))
208 (mml-generate-mime-1 (car cont))
209 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
213 (defun mml-generate-mime-1 (cont)
215 ((eq (car cont) 'part)
216 (let (coded encoding charset filename type)
217 (setq type (or (cdr (assq 'type cont)) "text/plain"))
218 (if (member (car (split-string type "/")) '("text" "message"))
221 ((cdr (assq 'buffer cont))
222 (insert-buffer-substring (cdr (assq 'buffer cont))))
223 ((and (setq filename (cdr (assq 'filename cont)))
224 (not (equal (cdr (assq 'nofile cont)) "yes")))
225 (mm-insert-file-contents filename))
228 (narrow-to-region (point) (point))
229 (insert (cdr (assq 'contents cont)))
230 ;; Remove quotes from quoted tags.
231 (goto-char (point-min))
232 (while (re-search-forward
233 "<#!+/?\\(part\\|multipart\\|external\\)" nil t)
234 (delete-region (+ (match-beginning 0) 2)
235 (+ (match-beginning 0) 3))))))
236 (setq charset (mm-encode-body))
237 (setq encoding (mm-body-encoding charset))
238 (setq coded (buffer-string)))
239 (mm-with-unibyte-buffer
241 ((cdr (assq 'buffer cont))
242 (insert-buffer-substring (cdr (assq 'buffer cont))))
243 ((and (setq filename (cdr (assq 'filename cont)))
244 (not (equal (cdr (assq 'nofile cont)) "yes")))
245 (let ((coding-system-for-read mm-binary-coding-system))
246 (mm-insert-file-contents filename nil nil nil nil t)))
248 (insert (cdr (assq 'contents cont)))))
249 (setq encoding (mm-encode-buffer type)
250 coded (buffer-string))))
251 (mml-insert-mime-headers cont type charset encoding)
254 ((eq (car cont) 'external)
255 (insert "Content-Type: message/external-body")
256 (let ((parameters (mml-parameter-string
257 cont '(expiration size permission)))
258 (name (cdr (assq 'name cont))))
260 (setq name (mml-parse-file-name name))
262 (mml-insert-parameter
263 (mail-header-encode-parameter "name" name)
264 "access-type=local-file")
265 (mml-insert-parameter
266 (mail-header-encode-parameter
267 "name" (file-name-nondirectory (nth 2 name)))
268 (mail-header-encode-parameter "site" (nth 1 name))
269 (mail-header-encode-parameter
270 "directory" (file-name-directory (nth 2 name))))
271 (mml-insert-parameter
272 (concat "access-type="
273 (if (member (nth 0 name) '("ftp@" "anonymous@"))
277 (mml-insert-parameter-string
278 cont '(expiration size permission))))
280 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
281 (insert "Content-ID: " (message-make-message-id) "\n")
282 (insert "Content-Transfer-Encoding: "
283 (or (cdr (assq 'encoding cont)) "binary"))
285 (insert (or (cdr (assq 'contents cont))))
287 ((eq (car cont) 'multipart)
288 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
289 (handler (assoc type mml-generate-multipart-alist)))
291 (funcall (cdr handler) cont)
292 ;; No specific handler. Use default one.
293 (let ((mml-boundary (mml-compute-boundary cont)))
294 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
297 (setq cont (cddr cont))
299 (insert "\n--" mml-boundary "\n")
300 (mml-generate-mime-1 (pop cont)))
301 (insert "\n--" mml-boundary "--\n")))))
303 (error "Invalid element: %S" cont))))
305 (defun mml-compute-boundary (cont)
306 "Return a unique boundary that does not exist in CONT."
307 (let ((mml-boundary (mml-make-boundary)))
308 ;; This function tries again and again until it has found
309 ;; a unique boundary.
310 (while (not (catch 'not-unique
311 (mml-compute-boundary-1 cont))))
314 (defun mml-compute-boundary-1 (cont)
317 ((eq (car cont) 'part)
320 ((cdr (assq 'buffer cont))
321 (insert-buffer-substring (cdr (assq 'buffer cont))))
322 ((and (setq filename (cdr (assq 'filename cont)))
323 (not (equal (cdr (assq 'nofile cont)) "yes")))
324 (mm-insert-file-contents filename))
326 (insert (cdr (assq 'contents cont)))))
327 (goto-char (point-min))
328 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
330 (setq mml-boundary (mml-make-boundary))
331 (throw 'not-unique nil))))
332 ((eq (car cont) 'multipart)
333 (mapcar 'mml-compute-boundary-1 (cddr cont))))
336 (defun mml-make-boundary ()
337 (concat (make-string (% (incf mml-multipart-number) 60) ?=)
338 (if (> mml-multipart-number 17)
339 (format "%x" mml-multipart-number)
343 (defun mml-make-string (num string)
345 (while (not (zerop (decf num)))
346 (setq out (concat out string)))
349 (defun mml-insert-mime-headers (cont type charset encoding)
350 (let (parameters disposition description)
352 (mml-parameter-string
353 cont '(name access-type expiration size permission)))
356 (not (equal type "text/plain")))
357 (when (consp charset)
359 "Can't encode a part with several charsets."))
360 (insert "Content-Type: " type)
362 (insert "; " (mail-header-encode-parameter
363 "charset" (symbol-name charset))))
365 (mml-insert-parameter-string
366 cont '(name access-type expiration size permission)))
369 (mml-parameter-string
370 cont '(filename creation-date modification-date read-date)))
371 (when (or (setq disposition (cdr (assq 'disposition cont)))
373 (insert "Content-Disposition: " (or disposition "inline"))
375 (mml-insert-parameter-string
376 cont '(filename creation-date modification-date read-date)))
378 (unless (eq encoding '7bit)
379 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
380 (when (setq description (cdr (assq 'description cont)))
381 (insert "Content-Description: "
382 (mail-encode-encoded-word-string description) "\n"))))
384 (defun mml-parameter-string (cont types)
387 (while (setq type (pop types))
388 (when (setq value (cdr (assq type cont)))
389 ;; Strip directory component from the filename parameter.
390 (when (eq type 'filename)
391 (setq value (file-name-nondirectory value)))
392 (setq string (concat string "; "
393 (mail-header-encode-parameter
394 (symbol-name type) value)))))
395 (when (not (zerop (length string)))
398 (defun mml-insert-parameter-string (cont types)
400 (while (setq type (pop types))
401 (when (setq value (cdr (assq type cont)))
402 ;; Strip directory component from the filename parameter.
403 (when (eq type 'filename)
404 (setq value (file-name-nondirectory value)))
405 (mml-insert-parameter
406 (mail-header-encode-parameter
407 (symbol-name type) value))))))
409 (defvar ange-ftp-path-format)
410 (defvar efs-path-regexp)
411 (defun mml-parse-file-name (path)
412 (if (if (boundp 'efs-path-regexp)
413 (string-match efs-path-regexp path)
414 (if (boundp 'ange-ftp-path-format)
415 (string-match (car ange-ftp-path-format))))
416 (list (match-string 1 path) (match-string 2 path)
417 (substring path (1+ (match-end 2))))
420 (defun mml-insert-buffer (buffer)
421 "Insert BUFFER at point and quote any MML markup."
423 (narrow-to-region (point) (point))
424 (insert-buffer-substring buffer)
425 (mml-quote-region (point-min) (point-max))
426 (goto-char (point-max))))
429 ;;; Transforming MIME to MML
432 (defun mime-to-mml ()
433 "Translate the current buffer (which should be a message) into MML."
434 ;; First decode the head.
436 (message-narrow-to-head)
437 (mail-decode-encoded-word-region (point-min) (point-max)))
438 (let ((handles (mm-dissect-buffer t)))
439 (goto-char (point-min))
440 (search-forward "\n\n" nil t)
441 (delete-region (point) (point-max))
442 (if (stringp (car handles))
443 (mml-insert-mime handles)
444 (mml-insert-mime handles t))
445 (mm-destroy-parts handles)))
447 (defun mml-to-mime ()
448 "Translate the current buffer from MML to MIME."
449 (message-encode-message-body)
451 (message-narrow-to-headers-or-head)
452 (mail-encode-encoded-word-buffer)))
454 (defun mml-insert-mime (handle &optional no-markup)
456 ;; Determine type and stuff.
457 (unless (stringp (car handle))
458 (unless (setq textp (equal (mm-handle-media-supertype handle)
461 (set-buffer (setq buffer (generate-new-buffer " *mml*")))
462 (mm-insert-part handle))))
464 (mml-insert-mml-markup handle buffer textp))
466 ((stringp (car handle))
467 (mapcar 'mml-insert-mime (cdr handle))
468 (insert "<#/multipart>\n"))
470 (let ((text (mm-get-part handle))
471 (charset (mail-content-type-get
472 (mm-handle-type handle) 'charset)))
473 (insert (mm-decode-string text charset)))
474 (goto-char (point-max)))
476 (insert "<#/part>\n")))))
478 (defun mml-insert-mml-markup (handle &optional buffer nofile)
479 "Take a MIME handle and insert an MML tag."
480 (if (stringp (car handle))
481 (insert "<#multipart type=" (mm-handle-media-subtype handle)
483 (insert "<#part type=" (mm-handle-media-type handle))
484 (dolist (elem (append (cdr (mm-handle-type handle))
485 (cdr (mm-handle-disposition handle))))
486 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
487 (when (mm-handle-disposition handle)
488 (insert " disposition=" (car (mm-handle-disposition handle))))
490 (insert " buffer=\"" (buffer-name buffer) "\""))
492 (insert " nofile=yes"))
493 (when (mm-handle-description handle)
494 (insert " description=\"" (mm-handle-description handle) "\""))
497 (defun mml-insert-parameter (&rest parameters)
498 "Insert PARAMETERS in a nice way."
499 (dolist (param parameters)
501 (let ((point (point)))
503 (when (> (current-column) 71)
509 ;;; Mode for inserting and editing MML forms
513 (let ((map (make-sparse-keymap))
514 (main (make-sparse-keymap)))
515 (define-key map "f" 'mml-attach-file)
516 (define-key map "b" 'mml-attach-buffer)
517 (define-key map "e" 'mml-attach-external)
518 (define-key map "q" 'mml-quote-region)
519 (define-key map "m" 'mml-insert-multipart)
520 (define-key map "p" 'mml-insert-part)
521 (define-key map "v" 'mml-validate)
522 (define-key map "P" 'mml-preview)
523 (define-key map "n" 'mml-narrow-to-part)
524 (define-key main "\M-m" map)
528 mml-menu mml-mode-map ""
531 ["File" mml-attach-file t]
532 ["Buffer" mml-attach-buffer t]
533 ["External" mml-attach-external t])
535 ["Multipart" mml-insert-multipart t]
536 ["Part" mml-insert-part t])
537 ["Narrow" mml-narrow-to-part t]
538 ["Quote" mml-quote-region t]
539 ["Validate" mml-validate t]
540 ["Preview" mml-preview t]))
543 "Minor mode for editing MML.")
545 (defun mml-mode (&optional arg)
546 "Minor mode for editing MML.
550 (if (not (set (make-local-variable 'mml-mode)
551 (if (null arg) (not mml-mode)
552 (> (prefix-numeric-value arg) 0))))
554 (set (make-local-variable 'mml-mode) t)
555 (unless (assq 'mml-mode minor-mode-alist)
556 (push `(mml-mode " MML") minor-mode-alist))
557 (unless (assq 'mml-mode minor-mode-map-alist)
558 (push (cons 'mml-mode mml-mode-map)
559 minor-mode-map-alist)))
560 (run-hooks 'mml-mode-hook))
563 ;;; Helper functions for reading MIME stuff from the minibuffer and
564 ;;; inserting stuff to the buffer.
567 (defun mml-minibuffer-read-file (prompt)
568 (let ((file (read-file-name prompt nil nil t)))
569 ;; Prevent some common errors. This is inspired by similar code in
571 (when (file-directory-p file)
572 (error "%s is a directory, cannot attach" file))
573 (unless (file-exists-p file)
574 (error "No such file: %s" file))
575 (unless (file-readable-p file)
576 (error "Permission denied: %s" file))
579 (defun mml-minibuffer-read-type (name &optional default)
580 (let* ((default (or default
581 (mm-default-file-encoding name)
582 ;; Perhaps here we should check what the file
583 ;; looks like, and offer text/plain if it looks
585 "application/octet-stream"))
586 (string (completing-read
587 (format "Content type (default %s): " default)
592 (mapcar (lambda (m) (cdr m))
593 mailcap-mime-extensions)
601 (let ((type (cdr (assq 'type (cdr m)))))
602 (if (equal (cadr (split-string type "/"))
609 (if (not (equal string ""))
613 (defun mml-minibuffer-read-description ()
614 (let ((description (read-string "One line description: ")))
615 (when (string-match "\\`[ \t]*\\'" description)
616 (setq description nil))
619 (defun mml-quote-region (beg end)
620 "Quote the MML tags in the region."
624 ;; Temporarily narrow the region to defend from changes
626 (narrow-to-region beg end)
627 (goto-char (point-min))
629 (while (re-search-forward
630 "<#/?!*\\(multipart\\|part\\|external\\)" nil t)
631 (goto-char (match-beginning 1))
634 (defun mml-insert-tag (name &rest plist)
635 "Insert an MML tag described by NAME and PLIST."
637 (setq name (symbol-name name)))
640 (let ((key (pop plist))
643 ;; Quote VALUE if it contains suspicious characters.
644 (when (string-match "[\"\\~/* \t\n]" value)
645 (setq value (prin1-to-string value)))
646 (insert (format " %s=%s" key value)))))
647 (insert ">\n<#/" name ">\n"))
649 ;;; Attachment functions.
651 (defun mml-attach-file (file &optional type description)
652 "Attach a file to the outgoing MIME message.
653 The file is not inserted or encoded until you send the message with
654 `\\[message-send-and-exit]' or `\\[message-send]'.
656 FILE is the name of the file to attach. TYPE is its content-type, a
657 string of the form \"type/subtype\". DESCRIPTION is a one-line
658 description of the attachment."
660 (let* ((file (mml-minibuffer-read-file "Attach file: "))
661 (type (mml-minibuffer-read-type file))
662 (description (mml-minibuffer-read-description)))
663 (list file type description)))
664 (mml-insert-tag 'part 'type type 'filename file 'disposition "attachment"
665 'description description))
667 (defun mml-attach-buffer (buffer &optional type description)
668 "Attach a buffer to the outgoing MIME message.
669 See `mml-attach-file' for details of operation."
671 (let* ((buffer (read-buffer "Attach buffer: "))
672 (type (mml-minibuffer-read-type buffer "text/plain"))
673 (description (mml-minibuffer-read-description)))
674 (list buffer type description)))
675 (mml-insert-tag 'part 'type type 'buffer buffer 'disposition "attachment"
676 'description description))
678 (defun mml-attach-external (file &optional type description)
679 "Attach an external file into the buffer.
680 FILE is an ange-ftp/efs specification of the part location.
681 TYPE is the MIME type to use."
683 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
684 (type (mml-minibuffer-read-type file))
685 (description (mml-minibuffer-read-description)))
686 (list file type description)))
687 (mml-insert-tag 'external 'type type 'name file 'disposition "attachment"
688 'description description))
690 (defun mml-insert-multipart (&optional type)
691 (interactive (list (completing-read "Multipart type (default mixed): "
692 '(("mixed") ("alternative") ("digest") ("parallel")
693 ("signed") ("encrypted"))
697 (mml-insert-tag "multipart" 'type type)
700 (defun mml-preview (&optional raw)
701 "Display current buffer with Gnus, in a new buffer.
702 If RAW, don't highlight the article."
704 (let ((buf (current-buffer)))
705 (switch-to-buffer (get-buffer-create
706 (concat (if raw "*Raw MIME preview of "
707 "*MIME preview of ") (buffer-name))))
712 (run-hooks 'gnus-article-decode-hook)
713 (let ((gnus-newsgroup-name "dummy"))
714 (gnus-article-prepare-display)))
716 (setq buffer-read-only t)
717 (goto-char (point-min))))
719 (defun mml-validate ()
720 "Validate the current MML document."