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)
60 (defvar mml-boundary-function 'mml-make-boundary
61 "A function called to suggest a boundary.
62 The function may be called several times, and should try to make a new
63 suggestion each time. The function is called with one parameter,
64 which is a number that says how many times the function has been
65 called for this message.")
68 "Parse the current buffer as an MML document."
69 (goto-char (point-min))
70 (let ((table (syntax-table)))
73 (set-syntax-table mml-syntax-table)
75 (set-syntax-table table))))
78 "Parse the current buffer as an MML document."
79 (let (struct tag point contents charsets warn)
80 (while (and (not (eobp))
81 (not (looking-at "<#/multipart")))
83 ((looking-at "<#multipart")
84 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
85 ((looking-at "<#external")
86 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
89 (if (looking-at "<#part")
90 (setq tag (mml-read-tag))
91 (setq tag (list 'part '(type . "text/plain"))
94 contents (mml-read-part)
95 charsets (mm-find-mime-charset-region point (point)))
96 (if (< (length charsets) 2)
97 (push (nconc tag (list (cons 'contents contents)))
99 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
105 "Warning: Your message contains %d parts. Really send? "
107 (error "Edit your message to use only one charset"))
108 (setq struct (nconc nstruct struct)))))))
113 (defun mml-parse-singlepart-with-multiple-charsets (orig-tag beg end)
115 (narrow-to-region beg end)
116 (goto-char (point-min))
117 (let ((current (mm-mime-charset (char-charset (following-char))))
118 charset struct space newline paragraph)
121 ;; The charset remains the same.
122 ((or (eq (setq charset (mm-mime-charset
123 (char-charset (following-char)))) 'us-ascii)
124 (eq charset current)))
125 ;; The initial charset was ascii.
126 ((eq current 'us-ascii)
127 (setq current charset
131 ;; We have a change in charsets.
135 (list (cons 'contents
136 (buffer-substring-no-properties
137 beg (or paragraph newline space (point))))))
139 (setq beg (or paragraph newline space (point))
144 ;; Compute places where it might be nice to break the part.
146 ((memq (following-char) '(? ?\t))
147 (setq space (1+ (point))))
148 ((eq (following-char) ?\n)
149 (setq newline (1+ (point))))
150 ((and (eq (following-char) ?\n)
152 (eq (char-after (1- (point))) ?\n))
153 (setq paragraph (point))))
155 ;; Do the final part.
156 (unless (= beg (point))
157 (push (append orig-tag
158 (list (cons 'contents
159 (buffer-substring-no-properties
164 (defun mml-read-tag ()
165 "Read a tag and return the contents."
166 (let (contents name elem val)
168 (setq name (buffer-substring-no-properties
169 (point) (progn (forward-sexp 1) (point))))
170 (skip-chars-forward " \t\n")
171 (while (not (looking-at ">"))
172 (setq elem (buffer-substring-no-properties
173 (point) (progn (forward-sexp 1) (point))))
174 (skip-chars-forward "= \t\n")
175 (setq val (buffer-substring-no-properties
176 (point) (progn (forward-sexp 1) (point))))
177 (when (string-match "^\"\\(.*\\)\"$" val)
178 (setq val (match-string 1 val)))
179 (push (cons (intern elem) val) contents)
180 (skip-chars-forward " \t\n"))
182 (skip-chars-forward " \t\n")
183 (cons (intern name) (nreverse contents))))
185 (defun mml-read-part ()
186 "Return the buffer up till the next part, multipart or closing part or multipart."
188 ;; If the tag ended at the end of the line, we go to the next line.
189 (when (looking-at "[ \t]*\n")
191 (if (re-search-forward
192 "<#\\(/\\)?\\(multipart\\|part\\|external\\)." nil t)
194 (buffer-substring-no-properties beg (match-beginning 0))
195 (if (or (not (match-beginning 1))
196 (equal (match-string 2) "multipart"))
197 (goto-char (match-beginning 0))
198 (when (looking-at "[ \t]*\n")
200 (buffer-substring-no-properties beg (goto-char (point-max))))))
202 (defvar mml-boundary nil)
203 (defvar mml-base-boundary "-=-=")
204 (defvar mml-multipart-number 0)
206 (defun mml-generate-mime ()
207 "Generate a MIME message based on the current MML document."
208 (let ((cont (mml-parse))
209 (mml-multipart-number 0))
213 (if (and (consp (car cont))
215 (mml-generate-mime-1 (car cont))
216 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
220 (defun mml-generate-mime-1 (cont)
222 ((eq (car cont) 'part)
223 (let (coded encoding charset filename type)
224 (setq type (or (cdr (assq 'type cont)) "text/plain"))
225 (if (member (car (split-string type "/")) '("text" "message"))
228 ((cdr (assq 'buffer cont))
229 (insert-buffer-substring (cdr (assq 'buffer cont))))
230 ((and (setq filename (cdr (assq 'filename cont)))
231 (not (equal (cdr (assq 'nofile cont)) "yes")))
232 (mm-insert-file-contents filename))
235 (narrow-to-region (point) (point))
236 (insert (cdr (assq 'contents cont)))
237 ;; Remove quotes from quoted tags.
238 (goto-char (point-min))
239 (while (re-search-forward
240 "<#!+/?\\(part\\|multipart\\|external\\)" nil t)
241 (delete-region (+ (match-beginning 0) 2)
242 (+ (match-beginning 0) 3))))))
243 (setq charset (mm-encode-body))
244 (setq encoding (mm-body-encoding charset))
245 (setq coded (buffer-string)))
246 (mm-with-unibyte-buffer
248 ((cdr (assq 'buffer cont))
249 (insert-buffer-substring (cdr (assq 'buffer cont))))
250 ((and (setq filename (cdr (assq 'filename cont)))
251 (not (equal (cdr (assq 'nofile cont)) "yes")))
252 (let ((coding-system-for-read mm-binary-coding-system))
253 (mm-insert-file-contents filename nil nil nil nil t)))
255 (insert (cdr (assq 'contents cont)))))
256 (setq encoding (mm-encode-buffer type)
257 coded (buffer-string))))
258 (mml-insert-mime-headers cont type charset encoding)
261 ((eq (car cont) 'external)
262 (insert "Content-Type: message/external-body")
263 (let ((parameters (mml-parameter-string
264 cont '(expiration size permission)))
265 (name (cdr (assq 'name cont))))
267 (setq name (mml-parse-file-name name))
269 (mml-insert-parameter
270 (mail-header-encode-parameter "name" name)
271 "access-type=local-file")
272 (mml-insert-parameter
273 (mail-header-encode-parameter
274 "name" (file-name-nondirectory (nth 2 name)))
275 (mail-header-encode-parameter "site" (nth 1 name))
276 (mail-header-encode-parameter
277 "directory" (file-name-directory (nth 2 name))))
278 (mml-insert-parameter
279 (concat "access-type="
280 (if (member (nth 0 name) '("ftp@" "anonymous@"))
284 (mml-insert-parameter-string
285 cont '(expiration size permission))))
287 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
288 (insert "Content-ID: " (message-make-message-id) "\n")
289 (insert "Content-Transfer-Encoding: "
290 (or (cdr (assq 'encoding cont)) "binary"))
292 (insert (or (cdr (assq 'contents cont))))
294 ((eq (car cont) 'multipart)
295 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
296 (handler (assoc type mml-generate-multipart-alist)))
298 (funcall (cdr handler) cont)
299 ;; No specific handler. Use default one.
300 (let ((mml-boundary (mml-compute-boundary cont)))
301 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
304 (setq cont (cddr cont))
306 (insert "\n--" mml-boundary "\n")
307 (mml-generate-mime-1 (pop cont)))
308 (insert "\n--" mml-boundary "--\n")))))
310 (error "Invalid element: %S" cont))))
312 (defun mml-compute-boundary (cont)
313 "Return a unique boundary that does not exist in CONT."
314 (let ((mml-boundary (funcall mml-boundary-function
315 (incf mml-multipart-number))))
316 ;; This function tries again and again until it has found
317 ;; a unique boundary.
318 (while (not (catch 'not-unique
319 (mml-compute-boundary-1 cont))))
322 (defun mml-compute-boundary-1 (cont)
325 ((eq (car cont) 'part)
328 ((cdr (assq 'buffer cont))
329 (insert-buffer-substring (cdr (assq 'buffer cont))))
330 ((and (setq filename (cdr (assq 'filename cont)))
331 (not (equal (cdr (assq 'nofile cont)) "yes")))
332 (mm-insert-file-contents filename))
334 (insert (cdr (assq 'contents cont)))))
335 (goto-char (point-min))
336 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
338 (setq mml-boundary (funcall mml-boundary-function
339 (incf mml-multipart-number)))
340 (throw 'not-unique nil))))
341 ((eq (car cont) 'multipart)
342 (mapcar 'mml-compute-boundary-1 (cddr cont))))
345 (defun mml-make-boundary (number)
346 (concat (make-string (% number 60) ?=)
352 (defun mml-make-string (num string)
354 (while (not (zerop (decf num)))
355 (setq out (concat out string)))
358 (defun mml-insert-mime-headers (cont type charset encoding)
359 (let (parameters disposition description)
361 (mml-parameter-string
362 cont '(name access-type expiration size permission)))
365 (not (equal type "text/plain")))
366 (when (consp charset)
368 "Can't encode a part with several charsets."))
369 (insert "Content-Type: " type)
371 (insert "; " (mail-header-encode-parameter
372 "charset" (symbol-name charset))))
374 (mml-insert-parameter-string
375 cont '(name access-type expiration size permission)))
378 (mml-parameter-string
379 cont '(filename creation-date modification-date read-date)))
380 (when (or (setq disposition (cdr (assq 'disposition cont)))
382 (insert "Content-Disposition: " (or disposition "inline"))
384 (mml-insert-parameter-string
385 cont '(filename creation-date modification-date read-date)))
387 (unless (eq encoding '7bit)
388 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
389 (when (setq description (cdr (assq 'description cont)))
390 (insert "Content-Description: "
391 (mail-encode-encoded-word-string description) "\n"))))
393 (defun mml-parameter-string (cont types)
396 (while (setq type (pop types))
397 (when (setq value (cdr (assq type cont)))
398 ;; Strip directory component from the filename parameter.
399 (when (eq type 'filename)
400 (setq value (file-name-nondirectory value)))
401 (setq string (concat string "; "
402 (mail-header-encode-parameter
403 (symbol-name type) value)))))
404 (when (not (zerop (length string)))
407 (defun mml-insert-parameter-string (cont types)
409 (while (setq type (pop types))
410 (when (setq value (cdr (assq type cont)))
411 ;; Strip directory component from the filename parameter.
412 (when (eq type 'filename)
413 (setq value (file-name-nondirectory value)))
414 (mml-insert-parameter
415 (mail-header-encode-parameter
416 (symbol-name type) value))))))
418 (defvar ange-ftp-path-format)
419 (defvar efs-path-regexp)
420 (defun mml-parse-file-name (path)
421 (if (if (boundp 'efs-path-regexp)
422 (string-match efs-path-regexp path)
423 (if (boundp 'ange-ftp-path-format)
424 (string-match (car ange-ftp-path-format))))
425 (list (match-string 1 path) (match-string 2 path)
426 (substring path (1+ (match-end 2))))
429 (defun mml-insert-buffer (buffer)
430 "Insert BUFFER at point and quote any MML markup."
432 (narrow-to-region (point) (point))
433 (insert-buffer-substring buffer)
434 (mml-quote-region (point-min) (point-max))
435 (goto-char (point-max))))
438 ;;; Transforming MIME to MML
441 (defun mime-to-mml ()
442 "Translate the current buffer (which should be a message) into MML."
443 ;; First decode the head.
445 (message-narrow-to-head)
446 (mail-decode-encoded-word-region (point-min) (point-max)))
447 (let ((handles (mm-dissect-buffer t)))
448 (goto-char (point-min))
449 (search-forward "\n\n" nil t)
450 (delete-region (point) (point-max))
451 (if (stringp (car handles))
452 (mml-insert-mime handles)
453 (mml-insert-mime handles t))
454 (mm-destroy-parts handles)))
456 (defun mml-to-mime ()
457 "Translate the current buffer from MML to MIME."
458 (message-encode-message-body)
460 (message-narrow-to-headers-or-head)
461 (mail-encode-encoded-word-buffer)))
463 (defun mml-insert-mime (handle &optional no-markup)
465 ;; Determine type and stuff.
466 (unless (stringp (car handle))
467 (unless (setq textp (equal (mm-handle-media-supertype handle)
470 (set-buffer (setq buffer (generate-new-buffer " *mml*")))
471 (mm-insert-part handle))))
473 (mml-insert-mml-markup handle buffer textp))
475 ((stringp (car handle))
476 (mapcar 'mml-insert-mime (cdr handle))
477 (insert "<#/multipart>\n"))
479 (let ((text (mm-get-part handle))
480 (charset (mail-content-type-get
481 (mm-handle-type handle) 'charset)))
482 (insert (mm-decode-string text charset)))
483 (goto-char (point-max)))
485 (insert "<#/part>\n")))))
487 (defun mml-insert-mml-markup (handle &optional buffer nofile)
488 "Take a MIME handle and insert an MML tag."
489 (if (stringp (car handle))
490 (insert "<#multipart type=" (mm-handle-media-subtype handle)
492 (insert "<#part type=" (mm-handle-media-type handle))
493 (dolist (elem (append (cdr (mm-handle-type handle))
494 (cdr (mm-handle-disposition handle))))
495 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))
496 (when (mm-handle-disposition handle)
497 (insert " disposition=" (car (mm-handle-disposition handle))))
499 (insert " buffer=\"" (buffer-name buffer) "\""))
501 (insert " nofile=yes"))
502 (when (mm-handle-description handle)
503 (insert " description=\"" (mm-handle-description handle) "\""))
506 (defun mml-insert-parameter (&rest parameters)
507 "Insert PARAMETERS in a nice way."
508 (dolist (param parameters)
510 (let ((point (point)))
512 (when (> (current-column) 71)
518 ;;; Mode for inserting and editing MML forms
522 (let ((map (make-sparse-keymap))
523 (main (make-sparse-keymap)))
524 (define-key map "f" 'mml-attach-file)
525 (define-key map "b" 'mml-attach-buffer)
526 (define-key map "e" 'mml-attach-external)
527 (define-key map "q" 'mml-quote-region)
528 (define-key map "m" 'mml-insert-multipart)
529 (define-key map "p" 'mml-insert-part)
530 (define-key map "v" 'mml-validate)
531 (define-key map "P" 'mml-preview)
532 (define-key map "n" 'mml-narrow-to-part)
533 (define-key main "\M-m" map)
537 mml-menu mml-mode-map ""
540 ["File" mml-attach-file t]
541 ["Buffer" mml-attach-buffer t]
542 ["External" mml-attach-external t])
544 ["Multipart" mml-insert-multipart t]
545 ["Part" mml-insert-part t])
546 ["Narrow" mml-narrow-to-part t]
547 ["Quote" mml-quote-region t]
548 ["Validate" mml-validate t]
549 ["Preview" mml-preview t]))
552 "Minor mode for editing MML.")
554 (defun mml-mode (&optional arg)
555 "Minor mode for editing MML.
559 (if (not (set (make-local-variable 'mml-mode)
560 (if (null arg) (not mml-mode)
561 (> (prefix-numeric-value arg) 0))))
563 (set (make-local-variable 'mml-mode) t)
564 (unless (assq 'mml-mode minor-mode-alist)
565 (push `(mml-mode " MML") minor-mode-alist))
566 (unless (assq 'mml-mode minor-mode-map-alist)
567 (push (cons 'mml-mode mml-mode-map)
568 minor-mode-map-alist)))
569 (run-hooks 'mml-mode-hook))
572 ;;; Helper functions for reading MIME stuff from the minibuffer and
573 ;;; inserting stuff to the buffer.
576 (defun mml-minibuffer-read-file (prompt)
577 (let ((file (read-file-name prompt nil nil t)))
578 ;; Prevent some common errors. This is inspired by similar code in
580 (when (file-directory-p file)
581 (error "%s is a directory, cannot attach" file))
582 (unless (file-exists-p file)
583 (error "No such file: %s" file))
584 (unless (file-readable-p file)
585 (error "Permission denied: %s" file))
588 (defun mml-minibuffer-read-type (name &optional default)
589 (let* ((default (or default
590 (mm-default-file-encoding name)
591 ;; Perhaps here we should check what the file
592 ;; looks like, and offer text/plain if it looks
594 "application/octet-stream"))
595 (string (completing-read
596 (format "Content type (default %s): " default)
601 (mapcar (lambda (m) (cdr m))
602 mailcap-mime-extensions)
610 (let ((type (cdr (assq 'type (cdr m)))))
611 (if (equal (cadr (split-string type "/"))
618 (if (not (equal string ""))
622 (defun mml-minibuffer-read-description ()
623 (let ((description (read-string "One line description: ")))
624 (when (string-match "\\`[ \t]*\\'" description)
625 (setq description nil))
628 (defun mml-quote-region (beg end)
629 "Quote the MML tags in the region."
633 ;; Temporarily narrow the region to defend from changes
635 (narrow-to-region beg end)
636 (goto-char (point-min))
638 (while (re-search-forward
639 "<#/?!*\\(multipart\\|part\\|external\\)" nil t)
640 ;; Insert ! after the #.
641 (goto-char (+ (match-beginning 0) 2))
644 (defun mml-insert-tag (name &rest plist)
645 "Insert an MML tag described by NAME and PLIST."
647 (setq name (symbol-name name)))
650 (let ((key (pop plist))
653 ;; Quote VALUE if it contains suspicious characters.
654 (when (string-match "[\"\\~/* \t\n]" value)
655 (setq value (prin1-to-string value)))
656 (insert (format " %s=%s" key value)))))
657 (insert ">\n<#/" name ">\n"))
659 ;;; Attachment functions.
661 (defun mml-attach-file (file &optional type description)
662 "Attach a file to the outgoing MIME message.
663 The file is not inserted or encoded until you send the message with
664 `\\[message-send-and-exit]' or `\\[message-send]'.
666 FILE is the name of the file to attach. TYPE is its content-type, a
667 string of the form \"type/subtype\". DESCRIPTION is a one-line
668 description of the attachment."
670 (let* ((file (mml-minibuffer-read-file "Attach file: "))
671 (type (mml-minibuffer-read-type file))
672 (description (mml-minibuffer-read-description)))
673 (list file type description)))
674 (mml-insert-tag 'part 'type type 'filename file 'disposition "attachment"
675 'description description))
677 (defun mml-attach-buffer (buffer &optional type description)
678 "Attach a buffer to the outgoing MIME message.
679 See `mml-attach-file' for details of operation."
681 (let* ((buffer (read-buffer "Attach buffer: "))
682 (type (mml-minibuffer-read-type buffer "text/plain"))
683 (description (mml-minibuffer-read-description)))
684 (list buffer type description)))
685 (mml-insert-tag 'part 'type type 'buffer buffer 'disposition "attachment"
686 'description description))
688 (defun mml-attach-external (file &optional type description)
689 "Attach an external file into the buffer.
690 FILE is an ange-ftp/efs specification of the part location.
691 TYPE is the MIME type to use."
693 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
694 (type (mml-minibuffer-read-type file))
695 (description (mml-minibuffer-read-description)))
696 (list file type description)))
697 (mml-insert-tag 'external 'type type 'name file 'disposition "attachment"
698 'description description))
700 (defun mml-insert-multipart (&optional type)
701 (interactive (list (completing-read "Multipart type (default mixed): "
702 '(("mixed") ("alternative") ("digest") ("parallel")
703 ("signed") ("encrypted"))
707 (mml-insert-tag "multipart" 'type type)
710 (defun mml-preview (&optional raw)
711 "Display current buffer with Gnus, in a new buffer.
712 If RAW, don't highlight the article."
714 (let ((buf (current-buffer)))
715 (switch-to-buffer (get-buffer-create
716 (concat (if raw "*Raw MIME preview of "
717 "*MIME preview of ") (buffer-name))))
720 (if (re-search-forward
721 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
722 (replace-match "\n"))
725 (run-hooks 'gnus-article-decode-hook)
726 (let ((gnus-newsgroup-name "dummy"))
727 (gnus-article-prepare-display)))
729 (setq buffer-read-only t)
730 (goto-char (point-min))))
732 (defun mml-validate ()
733 "Validate the current MML document."