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 (mm-insert-file-contents filename nil nil nil nil t))
247 (insert (cdr (assq 'contents cont)))))
248 (setq encoding (mm-encode-buffer type)
249 coded (buffer-string))))
250 (mml-insert-mime-headers cont type charset encoding)
253 ((eq (car cont) 'external)
254 (insert "Content-Type: message/external-body")
255 (let ((parameters (mml-parameter-string
256 cont '(expiration size permission)))
257 (name (cdr (assq 'name cont))))
259 (setq name (mml-parse-file-name name))
261 (mml-insert-parameter
262 (mail-header-encode-parameter "name" name)
263 "access-type=local-file")
264 (mml-insert-parameter
265 (mail-header-encode-parameter
266 "name" (file-name-nondirectory (nth 2 name)))
267 (mail-header-encode-parameter "site" (nth 1 name))
268 (mail-header-encode-parameter
269 "directory" (file-name-directory (nth 2 name))))
270 (mml-insert-parameter
271 (concat "access-type="
272 (if (member (nth 0 name) '("ftp@" "anonymous@"))
276 (mml-insert-parameter-string
277 cont '(expiration size permission))))
279 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
280 (insert "Content-ID: " (message-make-message-id) "\n")
281 (insert "Content-Transfer-Encoding: "
282 (or (cdr (assq 'encoding cont)) "binary"))
284 (insert (or (cdr (assq 'contents cont))))
286 ((eq (car cont) 'multipart)
287 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
288 (handler (assoc type mml-generate-multipart-alist)))
290 (funcall (cdr handler) cont)
291 ;; No specific handler. Use default one.
292 (let ((mml-boundary (mml-compute-boundary cont)))
293 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
296 (setq cont (cddr cont))
298 (insert "\n--" mml-boundary "\n")
299 (mml-generate-mime-1 (pop cont)))
300 (insert "\n--" mml-boundary "--\n")))))
302 (error "Invalid element: %S" cont))))
304 (defun mml-compute-boundary (cont)
305 "Return a unique boundary that does not exist in CONT."
306 (let ((mml-boundary (mml-make-boundary)))
307 ;; This function tries again and again until it has found
308 ;; a unique boundary.
309 (while (not (catch 'not-unique
310 (mml-compute-boundary-1 cont))))
313 (defun mml-compute-boundary-1 (cont)
316 ((eq (car cont) 'part)
319 ((cdr (assq 'buffer cont))
320 (insert-buffer-substring (cdr (assq 'buffer cont))))
321 ((and (setq filename (cdr (assq 'filename cont)))
322 (not (equal (cdr (assq 'nofile cont)) "yes")))
323 (mm-insert-file-contents filename))
325 (insert (cdr (assq 'contents cont)))))
326 (goto-char (point-min))
327 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
329 (setq mml-boundary (mml-make-boundary))
330 (throw 'not-unique nil))))
331 ((eq (car cont) 'multipart)
332 (mapcar 'mml-compute-boundary-1 (cddr cont))))
335 (defun mml-make-boundary ()
336 (concat (make-string (% (incf mml-multipart-number) 60) ?=)
337 (if (> mml-multipart-number 17)
338 (format "%x" mml-multipart-number)
342 (defun mml-make-string (num string)
344 (while (not (zerop (decf num)))
345 (setq out (concat out string)))
348 (defun mml-insert-mime-headers (cont type charset encoding)
349 (let (parameters disposition description)
351 (mml-parameter-string
352 cont '(name access-type expiration size permission)))
355 (not (equal type "text/plain")))
356 (when (consp charset)
358 "Can't encode a part with several charsets."))
359 (insert "Content-Type: " type)
361 (insert "; " (mail-header-encode-parameter
362 "charset" (symbol-name charset))))
364 (mml-insert-parameter-string
365 cont '(name access-type expiration size permission)))
368 (mml-parameter-string
369 cont '(filename creation-date modification-date read-date)))
370 (when (or (setq disposition (cdr (assq 'disposition cont)))
372 (insert "Content-Disposition: " (or disposition "inline"))
374 (mml-insert-parameter-string
375 cont '(filename creation-date modification-date read-date)))
377 (unless (eq encoding '7bit)
378 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
379 (when (setq description (cdr (assq 'description cont)))
380 (insert "Content-Description: "
381 (mail-encode-encoded-word-string description) "\n"))))
383 (defun mml-parameter-string (cont types)
386 (while (setq type (pop types))
387 (when (setq value (cdr (assq type cont)))
388 ;; Strip directory component from the filename parameter.
389 (when (eq type 'filename)
390 (setq value (file-name-nondirectory value)))
391 (setq string (concat string "; "
392 (mail-header-encode-parameter
393 (symbol-name type) value)))))
394 (when (not (zerop (length string)))
397 (defun mml-insert-parameter-string (cont types)
399 (while (setq type (pop types))
400 (when (setq value (cdr (assq type cont)))
401 ;; Strip directory component from the filename parameter.
402 (when (eq type 'filename)
403 (setq value (file-name-nondirectory value)))
404 (mml-insert-parameter
405 (mail-header-encode-parameter
406 (symbol-name type) value))))))
408 (defvar ange-ftp-path-format)
409 (defvar efs-path-regexp)
410 (defun mml-parse-file-name (path)
411 (if (if (boundp 'efs-path-regexp)
412 (string-match efs-path-regexp path)
413 (if (boundp 'ange-ftp-path-format)
414 (string-match (car ange-ftp-path-format))))
415 (list (match-string 1 path) (match-string 2 path)
416 (substring path (1+ (match-end 2))))
419 (defun mml-insert-buffer (buffer)
420 "Insert BUFFER at point and quote any MML markup."
422 (narrow-to-region (point) (point))
423 (insert-buffer-substring buffer)
424 (mml-quote-region (point-min) (point-max))
425 (goto-char (point-max))))
428 ;;; Transforming MIME to MML
431 (defun mime-to-mml ()
432 "Translate the current buffer (which should be a message) into MML."
433 ;; First decode the head.
435 (message-narrow-to-head)
436 (mail-decode-encoded-word-region (point-min) (point-max)))
437 (let ((handles (mm-dissect-buffer t)))
438 (goto-char (point-min))
439 (search-forward "\n\n" nil t)
440 (delete-region (point) (point-max))
441 (if (stringp (car handles))
442 (mml-insert-mime handles)
443 (mml-insert-mime handles t))
444 (mm-destroy-parts handles)))
446 (defun mml-to-mime ()
447 "Translate the current buffer from MML to MIME."
448 (message-encode-message-body)
450 (message-narrow-to-headers-or-head)
451 (mail-encode-encoded-word-buffer)))
453 (defun mml-insert-mime (handle &optional no-markup)
455 ;; Determine type and stuff.
456 (unless (stringp (car handle))
457 (unless (setq textp (equal (mm-handle-media-supertype handle)
460 (set-buffer (setq buffer (generate-new-buffer " *mml*")))
461 (mm-insert-part handle))))
463 (mml-insert-mml-markup handle buffer textp))
465 ((stringp (car handle))
466 (mapcar 'mml-insert-mime (cdr handle))
467 (insert "<#/multipart>\n"))
469 (let ((text (mm-get-part handle))
470 (charset (mail-content-type-get
471 (mm-handle-type handle) 'charset)))
472 (insert (mm-decode-string text charset)))
473 (goto-char (point-max)))
475 (insert "<#/part>\n")))))
477 (defun mml-insert-mml-markup (handle &optional buffer nofile)
478 "Take a MIME handle and insert an MML tag."
479 (if (stringp (car handle))
480 (insert "<#multipart type=" (mm-handle-media-subtype handle)
482 (insert "<#part type=" (mm-handle-media-type handle))
483 (dolist (elem (append (cdr (mm-handle-type handle))
484 (cdr (mm-handle-disposition handle))))
485 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
486 (when (mm-handle-disposition handle)
487 (insert " disposition=" (car (mm-handle-disposition handle))))
489 (insert " buffer=\"" (buffer-name buffer) "\""))
491 (insert " nofile=yes"))
492 (when (mm-handle-description handle)
493 (insert " description=\"" (mm-handle-description handle) "\""))
496 (defun mml-insert-parameter (&rest parameters)
497 "Insert PARAMETERS in a nice way."
498 (dolist (param parameters)
500 (let ((point (point)))
502 (when (> (current-column) 71)
508 ;;; Mode for inserting and editing MML forms
512 (let ((map (make-sparse-keymap))
513 (main (make-sparse-keymap)))
514 (define-key map "f" 'mml-attach-file)
515 (define-key map "b" 'mml-attach-buffer)
516 (define-key map "e" 'mml-attach-external)
517 (define-key map "q" 'mml-quote-region)
518 (define-key map "m" 'mml-insert-multipart)
519 (define-key map "p" 'mml-insert-part)
520 (define-key map "v" 'mml-validate)
521 (define-key map "P" 'mml-preview)
522 (define-key map "n" 'mml-narrow-to-part)
523 (define-key main "\M-m" map)
527 mml-menu mml-mode-map ""
530 ["File" mml-attach-file t]
531 ["Buffer" mml-attach-buffer t]
532 ["External" mml-attach-external t])
534 ["Multipart" mml-insert-multipart t]
535 ["Part" mml-insert-part t])
536 ["Narrow" mml-narrow-to-part t]
537 ["Quote" mml-quote-region t]
538 ["Validate" mml-validate t]
539 ["Preview" mml-preview t]))
542 "Minor mode for editing MML.")
544 (defun mml-mode (&optional arg)
545 "Minor mode for editing MML.
549 (if (not (set (make-local-variable 'mml-mode)
550 (if (null arg) (not mml-mode)
551 (> (prefix-numeric-value arg) 0))))
553 (set (make-local-variable 'mml-mode) t)
554 (unless (assq 'mml-mode minor-mode-alist)
555 (push `(mml-mode " MML") minor-mode-alist))
556 (unless (assq 'mml-mode minor-mode-map-alist)
557 (push (cons 'mml-mode mml-mode-map)
558 minor-mode-map-alist)))
559 (run-hooks 'mml-mode-hook))
562 ;;; Helper functions for reading MIME stuff from the minibuffer and
563 ;;; inserting stuff to the buffer.
566 (defun mml-minibuffer-read-file (prompt)
567 (let ((file (read-file-name prompt nil nil t)))
568 ;; Prevent some common errors. This is inspired by similar code in
570 (when (file-directory-p file)
571 (error "%s is a directory, cannot attach" file))
572 (unless (file-exists-p file)
573 (error "No such file: %s" file))
574 (unless (file-readable-p file)
575 (error "Permission denied: %s" file))
578 (defun mml-minibuffer-read-type (name &optional default)
579 (let* ((default (or default
580 (mm-default-file-encoding name)
581 ;; Perhaps here we should check what the file
582 ;; looks like, and offer text/plain if it looks
584 "application/octet-stream"))
585 (string (completing-read
586 (format "Content type (default %s): " default)
591 (mapcar (lambda (m) (cdr m))
592 mailcap-mime-extensions)
600 (let ((type (cdr (assq 'type (cdr m)))))
601 (if (equal (cadr (split-string type "/"))
608 (if (not (equal string ""))
612 (defun mml-minibuffer-read-description ()
613 (let ((description (read-string "One line description: ")))
614 (when (string-match "\\`[ \t]*\\'" description)
615 (setq description nil))
618 (defun mml-quote-region (beg end)
619 "Quote the MML tags in the region."
623 ;; Temporarily narrow the region to defend from changes
625 (narrow-to-region beg end)
626 (goto-char (point-min))
628 (while (re-search-forward
629 "<#/?!*\\(multipart\\|part\\|external\\)" nil t)
630 (goto-char (match-beginning 1))
633 (defun mml-insert-tag (name &rest plist)
634 "Insert an MML tag described by NAME and PLIST."
636 (setq name (symbol-name name)))
639 (let ((key (pop plist))
642 ;; Quote VALUE if it contains suspicious characters.
643 (when (string-match "[\"\\~/* \t\n]" value)
644 (setq value (prin1-to-string value)))
645 (insert (format " %s=%s" key value)))))
646 (insert ">\n<#/" name ">\n"))
648 ;;; Attachment functions.
650 (defun mml-attach-file (file &optional type description)
651 "Attach a file to the outgoing MIME message.
652 The file is not inserted or encoded until you send the message with
653 `\\[message-send-and-exit]' or `\\[message-send]'.
655 FILE is the name of the file to attach. TYPE is its content-type, a
656 string of the form \"type/subtype\". DESCRIPTION is a one-line
657 description of the attachment."
659 (let* ((file (mml-minibuffer-read-file "Attach file: "))
660 (type (mml-minibuffer-read-type file))
661 (description (mml-minibuffer-read-description)))
662 (list file type description)))
663 (mml-insert-tag 'part 'type type 'filename file 'disposition "attachment"
664 'description description))
666 (defun mml-attach-buffer (buffer &optional type description)
667 "Attach a buffer to the outgoing MIME message.
668 See `mml-attach-file' for details of operation."
670 (let* ((buffer (read-buffer "Attach buffer: "))
671 (type (mml-minibuffer-read-type buffer "text/plain"))
672 (description (mml-minibuffer-read-description)))
673 (list buffer type description)))
674 (mml-insert-tag 'part 'type type 'buffer buffer 'disposition "attachment"
675 'description description))
677 (defun mml-attach-external (file &optional type description)
678 "Attach an external file into the buffer.
679 FILE is an ange-ftp/efs specification of the part location.
680 TYPE is the MIME type to use."
682 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
683 (type (mml-minibuffer-read-type file))
684 (description (mml-minibuffer-read-description)))
685 (list file type description)))
686 (mml-insert-tag 'external 'type type 'name file 'disposition "attachment"
687 'description description))
689 (defun mml-insert-multipart (&optional type)
690 (interactive (list (completing-read "Multipart type (default mixed): "
691 '(("mixed") ("alternative") ("digest") ("parallel")
692 ("signed") ("encrypted"))
696 (mml-insert-tag "multipart" 'type type)
699 (defun mml-preview (&optional raw)
700 "Display current buffer with Gnus, in a new buffer.
701 If RAW, don't highlight the article."
703 (let ((buf (current-buffer)))
704 (switch-to-buffer (get-buffer-create
705 (concat (if raw "*Raw MIME preview of "
706 "*MIME preview of ") (buffer-name))))
711 (run-hooks 'gnus-article-decode-hook)
712 (let ((gnus-newsgroup-name "dummy"))
713 (gnus-article-prepare-display)))
715 (setq buffer-read-only t)
716 (goto-char (point-min))))
718 (defun mml-validate ()
719 "Validate the current MML document."