1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
33 (eval-when-compile (require 'cl))
36 (autoload 'message-make-message-id "message")
37 (autoload 'gnus-setup-posting-charset "gnus-msg")
38 (autoload 'gnus-make-local-hook "gnus-util")
39 (autoload 'message-fetch-field "message")
40 (autoload 'message-mark-active-p "message")
41 (autoload 'message-info "message")
42 (autoload 'fill-flowed-encode "flow-fill")
43 (autoload 'message-posting-charset "message"))
46 (autoload 'dnd-get-local-file-name "dnd"))
48 (defvar gnus-article-mime-handles)
50 (defvar gnus-newsrc-hashtb)
51 (defvar message-default-charset)
52 (defvar message-deletable-headers)
53 (defvar message-options)
54 (defvar message-posting-charset)
55 (defvar message-required-mail-headers)
56 (defvar message-required-news-headers)
58 (defcustom mml-content-type-parameters
59 '(name access-type expiration size permission format)
60 "*A list of acceptable parameters in MML tag.
61 These parameters are generated in Content-Type header if exists."
63 :type '(repeat (symbol :tag "Parameter"))
66 (defcustom mml-content-disposition-parameters
67 '(filename creation-date modification-date read-date)
68 "*A list of acceptable parameters in MML tag.
69 These parameters are generated in Content-Disposition header if exists."
71 :type '(repeat (symbol :tag "Parameter"))
74 (defcustom mml-insert-mime-headers-always nil
75 "If non-nil, always put Content-Type: text/plain at top of empty parts.
76 It is necessary to work against a bug in certain clients."
81 (defvar mml-tweak-type-alist nil
82 "A list of (TYPE . FUNCTION) for tweaking MML parts.
83 TYPE is a string containing a regexp to match the MIME type. FUNCTION
84 is a Lisp function which is called with the MML handle to tweak the
85 part. This variable is used only when no TWEAK parameter exists in
88 (defvar mml-tweak-function-alist nil
89 "A list of (NAME . FUNCTION) for tweaking MML parts.
90 NAME is a string containing the name of the TWEAK parameter in the MML
91 handle. FUNCTION is a Lisp function which is called with the MML
92 handle to tweak the part.")
94 (defvar mml-tweak-sexp-alist
95 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
96 "A list of (SEXP . FUNCTION) for tweaking MML parts.
97 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
98 is called. FUNCTION is a Lisp function which is called with the MML
99 handle to tweak the part.")
101 (defvar mml-externalize-attachments nil
102 "*If non-nil, local-file attachments are generated as external parts.")
104 (defvar mml-generate-multipart-alist nil
105 "*Alist of multipart generation functions.
106 Each entry has the form (NAME . FUNCTION), where
107 NAME is a string containing the name of the part (without the
108 leading \"/multipart/\"),
109 FUNCTION is a Lisp function which is called to generate the part.
111 The Lisp function has to supply the appropriate MIME headers and the
112 contents of this part.")
114 (defvar mml-syntax-table
115 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
116 (modify-syntax-entry ?\\ "/" table)
117 (modify-syntax-entry ?< "(" table)
118 (modify-syntax-entry ?> ")" table)
119 (modify-syntax-entry ?@ "w" table)
120 (modify-syntax-entry ?/ "w" table)
121 (modify-syntax-entry ?= " " table)
122 (modify-syntax-entry ?* " " table)
123 (modify-syntax-entry ?\; " " table)
124 (modify-syntax-entry ?\' " " table)
127 (defvar mml-boundary-function 'mml-make-boundary
128 "A function called to suggest a boundary.
129 The function may be called several times, and should try to make a new
130 suggestion each time. The function is called with one parameter,
131 which is a number that says how many times the function has been
132 called for this message.")
134 (defvar mml-confirmation-set nil
135 "A list of symbols, each of which disables some warning.
136 `unknown-encoding': always send messages contain characters with
137 unknown encoding; `use-ascii': always use ASCII for those characters
138 with unknown encoding; `multipart': always send messages with more than
141 (defvar mml-generate-default-type "text/plain"
142 "Content type by which the Content-Type header can be omitted.
143 The Content-Type header will not be put in the MIME part if the type
144 equals the value and there's no parameter (e.g. charset, format, etc.)
145 and `mml-insert-mime-headers-always' is nil. The value will be bound
146 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
147 part. This is for the internal use, you should never modify the value.")
149 (defvar mml-buffer-list nil)
151 (defun mml-generate-new-buffer (name)
152 (let ((buf (generate-new-buffer name)))
153 (push buf mml-buffer-list)
156 (defun mml-destroy-buffers ()
157 (let (kill-buffer-hook)
158 (mapc 'kill-buffer mml-buffer-list)
159 (setq mml-buffer-list nil)))
162 "Parse the current buffer as an MML document."
164 (goto-char (point-min))
165 (with-syntax-table mml-syntax-table
168 (defun mml-parse-1 ()
169 "Parse the current buffer as an MML document."
170 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
171 (while (and (not (eobp))
172 (not (looking-at "<#/multipart")))
174 ((looking-at "<#secure")
175 ;; The secure part is essentially a meta-meta tag, which
176 ;; expands to either a part tag if there are no other parts in
177 ;; the document or a multipart tag if there are other parts
178 ;; included in the message
180 (taginfo (mml-read-tag))
181 (keyfile (cdr (assq 'keyfile taginfo)))
182 (certfile (cdr (assq 'certfile taginfo)))
183 (recipients (cdr (assq 'recipients taginfo)))
184 (sender (cdr (assq 'sender taginfo)))
185 (location (cdr (assq 'tag-location taginfo)))
186 (mode (cdr (assq 'mode taginfo)))
187 (method (cdr (assq 'method taginfo)))
190 (if (re-search-forward
191 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
192 (setq secure-mode "multipart")
193 (setq secure-mode "part")))
196 (re-search-forward "<#secure[^\n]*>\n"))
197 (delete-region (match-beginning 0) (match-end 0))
198 (cond ((string= mode "sign")
199 (setq tags (list "sign" method)))
200 ((string= mode "encrypt")
201 (setq tags (list "encrypt" method)))
202 ((string= mode "signencrypt")
203 (setq tags (list "sign" method "encrypt" method))))
204 (eval `(mml-insert-tag ,secure-mode
206 ,(if keyfile "keyfile")
208 ,(if certfile "certfile")
210 ,(if recipients "recipients")
212 ,(if sender "sender")
215 (goto-char location)))
216 ((looking-at "<#multipart")
217 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
218 ((looking-at "<#external")
219 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
222 (if (or (looking-at "<#part") (looking-at "<#mml"))
223 (setq tag (mml-read-tag)
226 (setq tag (list 'part '(type . "text/plain"))
229 (setq raw (cdr (assq 'raw tag))
231 contents (mml-read-part (eq 'mml (car tag)))
236 (intern (downcase (cdr (assq 'charset tag))))))
238 (mm-find-mime-charset-region point (point)
240 (when (and (not raw) (memq nil charsets))
241 (if (or (memq 'unknown-encoding mml-confirmation-set)
242 (message-options-get 'unknown-encoding)
244 Message contains characters with unknown encoding. Really send? ")
245 (message-options-set 'unknown-encoding t)))
247 (or (memq 'use-ascii mml-confirmation-set)
248 (message-options-get 'use-ascii)
249 (and (y-or-n-p "Use ASCII as charset? ")
250 (message-options-set 'use-ascii t))))
251 (setq charsets (delq nil charsets))
253 (error "Edit your message to remove those characters")))
256 (< (length charsets) 2))
257 (if (or (not no-markup-p)
258 (string-match "[^ \t\r\n]" contents))
259 ;; Don't create blank parts.
260 (push (nconc tag (list (cons 'contents contents)))
262 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
263 tag point (point) use-ascii)))
265 (not (memq 'multipart mml-confirmation-set))
266 (not (message-options-get 'multipart))
267 (not (and (y-or-n-p (format "\
268 A message part needs to be split into %d charset parts. Really send? "
270 (message-options-set 'multipart t))))
271 (error "Edit your message to use only one charset"))
272 (setq struct (nconc nstruct struct)))))))
277 (defun mml-parse-singlepart-with-multiple-charsets
278 (orig-tag beg end &optional use-ascii)
281 (narrow-to-region beg end)
282 (goto-char (point-min))
283 (let ((current (or (mm-mime-charset (mm-charset-after))
284 (and use-ascii 'us-ascii)))
285 charset struct space newline paragraph)
287 (setq charset (mm-mime-charset (mm-charset-after)))
289 ;; The charset remains the same.
290 ((eq charset 'us-ascii))
291 ((or (and use-ascii (not charset))
292 (eq charset current))
296 ;; The initial charset was ascii.
297 ((eq current 'us-ascii)
298 (setq current charset
302 ;; We have a change in charsets.
306 (list (cons 'contents
307 (buffer-substring-no-properties
308 beg (or paragraph newline space (point))))))
310 (setq beg (or paragraph newline space (point))
315 ;; Compute places where it might be nice to break the part.
317 ((memq (following-char) '(? ?\t))
318 (setq space (1+ (point))))
319 ((and (eq (following-char) ?\n)
321 (eq (char-after (1- (point))) ?\n))
322 (setq paragraph (point)))
323 ((eq (following-char) ?\n)
324 (setq newline (1+ (point)))))
326 ;; Do the final part.
327 (unless (= beg (point))
328 (push (append orig-tag
329 (list (cons 'contents
330 (buffer-substring-no-properties
335 (defun mml-read-tag ()
336 "Read a tag and return the contents."
337 (let ((orig-point (point))
338 contents name elem val)
340 (setq name (buffer-substring-no-properties
341 (point) (progn (forward-sexp 1) (point))))
342 (skip-chars-forward " \t\n")
343 (while (not (looking-at ">[ \t]*\n?"))
344 (setq elem (buffer-substring-no-properties
345 (point) (progn (forward-sexp 1) (point))))
346 (skip-chars-forward "= \t\n")
347 (setq val (buffer-substring-no-properties
348 (point) (progn (forward-sexp 1) (point))))
349 (when (string-match "^\"\\(.*\\)\"$" val)
350 (setq val (match-string 1 val)))
351 (push (cons (intern elem) val) contents)
352 (skip-chars-forward " \t\n"))
353 (goto-char (match-end 0))
354 ;; Don't skip the leading space.
355 ;;(skip-chars-forward " \t\n")
356 ;; Put the tag location into the returned contents
357 (setq contents (append (list (cons 'tag-location orig-point)) contents))
358 (cons (intern name) (nreverse contents))))
360 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
361 (let ((str (buffer-substring-no-properties start end))
362 (bufstart start) tmp)
363 (while (setq tmp (text-property-any start end 'hard 't))
364 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
366 (setq start (1+ tmp)))
369 (defun mml-read-part (&optional mml)
370 "Return the buffer up till the next part, multipart or closing part or multipart.
371 If MML is non-nil, return the buffer up till the correspondent mml tag."
372 (let ((beg (point)) (count 1))
373 ;; If the tag ended at the end of the line, we go to the next line.
374 (when (looking-at "[ \t]*\n")
378 (while (and (> count 0) (not (eobp)))
379 (if (re-search-forward "<#\\(/\\)?mml." nil t)
380 (setq count (+ count (if (match-beginning 1) -1 1)))
381 (goto-char (point-max))))
382 (mml-buffer-substring-no-properties-except-hard-newlines
385 (match-beginning 0))))
386 (if (re-search-forward
387 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
389 (mml-buffer-substring-no-properties-except-hard-newlines
390 beg (match-beginning 0))
391 (if (or (not (match-beginning 1))
392 (equal (match-string 2) "multipart"))
393 (goto-char (match-beginning 0))
394 (when (looking-at "[ \t]*\n")
396 (mml-buffer-substring-no-properties-except-hard-newlines
397 beg (goto-char (point-max)))))))
399 (defvar mml-boundary nil)
400 (defvar mml-base-boundary "-=-=")
401 (defvar mml-multipart-number 0)
403 (defun mml-generate-mime ()
404 "Generate a MIME message based on the current MML document."
405 (let ((cont (mml-parse))
406 (mml-multipart-number mml-multipart-number))
410 (if (and (consp (car cont))
412 (mml-generate-mime-1 (car cont))
413 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
417 (defun mml-generate-mime-1 (cont)
418 (let ((mm-use-ultra-safe-encoding
419 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
421 (narrow-to-region (point) (point))
422 (mml-tweak-part cont)
424 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
425 (let* ((raw (cdr (assq 'raw cont)))
426 (filename (cdr (assq 'filename cont)))
427 (type (or (cdr (assq 'type cont))
429 (or (mm-default-file-encoding filename)
430 "application/octet-stream")
432 (charset (cdr (assq 'charset cont)))
433 (coding (mm-charset-to-coding-system charset))
434 encoding flowed coded)
435 (cond ((eq coding 'ascii)
439 (setq charset (intern (downcase charset)))))
441 (member (car (split-string type "/")) '("text" "message")))
445 ((cdr (assq 'buffer cont))
446 (insert-buffer-substring (cdr (assq 'buffer cont))))
448 (not (equal (cdr (assq 'nofile cont)) "yes")))
449 (let ((coding-system-for-read coding))
450 (mm-insert-file-contents filename)))
451 ((eq 'mml (car cont))
452 (insert (cdr (assq 'contents cont))))
455 (narrow-to-region (point) (point))
456 (insert (cdr (assq 'contents cont)))
457 ;; Remove quotes from quoted tags.
458 (goto-char (point-min))
459 (while (re-search-forward
460 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
462 (delete-region (+ (match-beginning 0) 2)
463 (+ (match-beginning 0) 3))))))
465 ((eq (car cont) 'mml)
466 (let ((mml-boundary (mml-compute-boundary cont))
467 ;; It is necessary for the case where this
468 ;; function is called recursively since
469 ;; `m-g-d-t' will be bound to "message/rfc822"
470 ;; when encoding an article to be forwarded.
471 (mml-generate-default-type "text/plain"))
473 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
474 ;; ignore 0x1b, it is part of iso-2022-jp
475 (setq encoding (mm-body-7-or-8))))
476 ((string= (car (split-string type "/")) "message")
477 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
478 ;; ignore 0x1b, it is part of iso-2022-jp
479 (setq encoding (mm-body-7-or-8))))
481 ;; Only perform format=flowed filling on text/plain
482 ;; parts where there either isn't a format parameter
483 ;; in the mml tag or it says "flowed" and there
484 ;; actually are hard newlines in the text.
485 (let (use-hard-newlines)
486 (when (and (string= type "text/plain")
487 (not (string= (cdr (assq 'sign cont)) "pgp"))
488 (or (null (assq 'format cont))
489 (string= (cdr (assq 'format cont))
491 (setq use-hard-newlines
493 (point-min) (point-max) 'hard 't)))
495 ;; Indicate that `mml-insert-mime-headers' should
496 ;; insert a "; format=flowed" string unless the
497 ;; user has already specified it.
498 (setq flowed (null (assq 'format cont)))))
499 (setq charset (mm-encode-body charset))
500 (setq encoding (mm-body-encoding
501 charset (cdr (assq 'encoding cont))))))
502 (setq coded (buffer-string)))
503 (mml-insert-mime-headers cont type charset encoding flowed)
506 (mm-with-unibyte-buffer
508 ((cdr (assq 'buffer cont))
509 (insert (with-current-buffer (cdr (assq 'buffer cont))
510 (mm-with-unibyte-current-buffer
513 (not (equal (cdr (assq 'nofile cont)) "yes")))
514 (let ((coding-system-for-read mm-binary-coding-system))
515 (mm-insert-file-contents filename nil nil nil nil t))
517 (setq charset (mm-coding-system-to-mime-charset
518 (mm-find-buffer-file-coding-system
521 (let ((contents (cdr (assq 'contents cont))))
522 (if (if (featurep 'xemacs)
523 (string-match "[^\000-\377]" contents)
524 (mm-multibyte-string-p contents))
526 (mm-enable-multibyte)
528 (setq charset (mm-encode-body charset)))
529 (insert contents)))))
530 (setq encoding (mm-encode-buffer type)
531 coded (mm-string-as-multibyte (buffer-string))))
532 (mml-insert-mime-headers cont type charset encoding nil)
534 (mm-with-unibyte-current-buffer
536 ((eq (car cont) 'external)
537 (insert "Content-Type: message/external-body")
538 (let ((parameters (mml-parameter-string
539 cont '(expiration size permission)))
540 (name (cdr (assq 'name cont)))
541 (url (cdr (assq 'url cont))))
543 (setq name (mml-parse-file-name name))
545 (mml-insert-parameter
546 (mail-header-encode-parameter "name" name)
547 "access-type=local-file")
548 (mml-insert-parameter
549 (mail-header-encode-parameter
550 "name" (file-name-nondirectory (nth 2 name)))
551 (mail-header-encode-parameter "site" (nth 1 name))
552 (mail-header-encode-parameter
553 "directory" (file-name-directory (nth 2 name))))
554 (mml-insert-parameter
555 (concat "access-type="
556 (if (member (nth 0 name) '("ftp@" "anonymous@"))
560 (mml-insert-parameter
561 (mail-header-encode-parameter "url" url)
564 (mml-insert-parameter-string
565 cont '(expiration size permission)))
567 (insert "Content-Type: "
568 (or (cdr (assq 'type cont))
570 (or (mm-default-file-encoding name)
571 "application/octet-stream")
574 (insert "Content-ID: " (message-make-message-id) "\n")
575 (insert "Content-Transfer-Encoding: "
576 (or (cdr (assq 'encoding cont)) "binary"))
578 (insert (or (cdr (assq 'contents cont))))
580 ((eq (car cont) 'multipart)
581 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
582 (mml-generate-default-type (if (equal type "digest")
585 (handler (assoc type mml-generate-multipart-alist)))
587 (funcall (cdr handler) cont)
588 ;; No specific handler. Use default one.
589 (let ((mml-boundary (mml-compute-boundary cont)))
590 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
592 (if (cdr (assq 'start cont))
593 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
595 (let ((cont cont) part)
596 (while (setq part (pop cont))
597 ;; Skip `multipart' and attributes.
598 (when (and (consp part) (consp (cdr part)))
599 (insert "\n--" mml-boundary "\n")
600 (mml-generate-mime-1 part)
601 (goto-char (point-max)))))
602 (insert "\n--" mml-boundary "--\n")))))
604 (error "Invalid element: %S" cont)))
605 ;; handle sign & encrypt tags in a semi-smart way.
606 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
607 (encrypt-item (assoc (cdr (assq 'encrypt cont))
610 (when (or sign-item encrypt-item)
611 (when (setq sender (cdr (assq 'sender cont)))
612 (message-options-set 'mml-sender sender)
613 (message-options-set 'message-sender sender))
614 (if (setq recipients (cdr (assq 'recipients cont)))
615 (message-options-set 'message-recipients recipients))
616 (let ((style (mml-signencrypt-style
617 (first (or sign-item encrypt-item)))))
618 ;; check if: we're both signing & encrypting, both methods
619 ;; are the same (why would they be different?!), and that
620 ;; the signencrypt style allows for combined operation.
621 (if (and sign-item encrypt-item (equal (first sign-item)
622 (first encrypt-item))
623 (equal style 'combined))
624 (funcall (nth 1 encrypt-item) cont t)
625 ;; otherwise, revert to the old behavior.
627 (funcall (nth 1 sign-item) cont))
629 (funcall (nth 1 encrypt-item) cont)))))))))
631 (defun mml-compute-boundary (cont)
632 "Return a unique boundary that does not exist in CONT."
633 (let ((mml-boundary (funcall mml-boundary-function
634 (incf mml-multipart-number))))
635 ;; This function tries again and again until it has found
636 ;; a unique boundary.
637 (while (not (catch 'not-unique
638 (mml-compute-boundary-1 cont))))
641 (defun mml-compute-boundary-1 (cont)
644 ((eq (car cont) 'part)
647 ((cdr (assq 'buffer cont))
648 (insert-buffer-substring (cdr (assq 'buffer cont))))
649 ((and (setq filename (cdr (assq 'filename cont)))
650 (not (equal (cdr (assq 'nofile cont)) "yes")))
651 (mm-insert-file-contents filename nil nil nil nil t))
653 (insert (cdr (assq 'contents cont)))))
654 (goto-char (point-min))
655 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
657 (setq mml-boundary (funcall mml-boundary-function
658 (incf mml-multipart-number)))
659 (throw 'not-unique nil))))
660 ((eq (car cont) 'multipart)
661 (mapc 'mml-compute-boundary-1 (cddr cont))))
664 (defun mml-make-boundary (number)
665 (concat (make-string (% number 60) ?=)
671 (defun mml-insert-mime-headers (cont type charset encoding flowed)
672 (let (parameters id disposition description)
674 (mml-parameter-string
675 cont mml-content-type-parameters))
679 (not (equal type mml-generate-default-type))
680 mml-insert-mime-headers-always)
681 (when (consp charset)
683 "Can't encode a part with several charsets"))
684 (insert "Content-Type: " type)
686 (mml-insert-parameter
687 (mail-header-encode-parameter "charset" (symbol-name charset))))
689 (mml-insert-parameter "format=flowed"))
691 (mml-insert-parameter-string
692 cont mml-content-type-parameters))
694 (when (setq id (cdr (assq 'id cont)))
695 (insert "Content-ID: " id "\n"))
697 (mml-parameter-string
698 cont mml-content-disposition-parameters))
699 (when (or (setq disposition (cdr (assq 'disposition cont)))
701 (insert "Content-Disposition: " (or disposition "inline"))
703 (mml-insert-parameter-string
704 cont mml-content-disposition-parameters))
706 (unless (eq encoding '7bit)
707 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
708 (when (setq description (cdr (assq 'description cont)))
709 (insert "Content-Description: ")
710 (setq description (prog1
712 (insert description "\n")))
713 (mail-encode-encoded-word-region description (point)))))
715 (defun mml-parameter-string (cont types)
718 (while (setq type (pop types))
719 (when (setq value (cdr (assq type cont)))
720 ;; Strip directory component from the filename parameter.
721 (when (eq type 'filename)
722 (setq value (file-name-nondirectory value)))
723 (setq string (concat string "; "
724 (mail-header-encode-parameter
725 (symbol-name type) value)))))
726 (when (not (zerop (length string)))
729 (defun mml-insert-parameter-string (cont types)
731 (while (setq type (pop types))
732 (when (setq value (cdr (assq type cont)))
733 ;; Strip directory component from the filename parameter.
734 (when (eq type 'filename)
735 (setq value (file-name-nondirectory value)))
736 (mml-insert-parameter
737 (mail-header-encode-parameter
738 (symbol-name type) value))))))
741 (defvar ange-ftp-name-format)
742 (defvar efs-path-regexp))
743 (defun mml-parse-file-name (path)
744 (if (if (boundp 'efs-path-regexp)
745 (string-match efs-path-regexp path)
746 (if (boundp 'ange-ftp-name-format)
747 (string-match (car ange-ftp-name-format) path)))
748 (list (match-string 1 path) (match-string 2 path)
749 (substring path (1+ (match-end 2))))
752 (defun mml-insert-buffer (buffer)
753 "Insert BUFFER at point and quote any MML markup."
755 (narrow-to-region (point) (point))
756 (insert-buffer-substring buffer)
757 (mml-quote-region (point-min) (point-max))
758 (goto-char (point-max))))
761 ;;; Transforming MIME to MML
764 (defun mime-to-mml (&optional handles)
765 "Translate the current buffer (which should be a message) into MML.
766 If HANDLES is non-nil, use it instead reparsing the buffer."
767 ;; First decode the head.
769 (message-narrow-to-head)
770 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
771 (mail-decode-encoded-word-region (point-min) (point-max))))
773 (setq handles (mm-dissect-buffer t)))
774 (goto-char (point-min))
775 (search-forward "\n\n" nil t)
776 (delete-region (point) (point-max))
777 (if (stringp (car handles))
778 (mml-insert-mime handles)
779 (mml-insert-mime handles t))
780 (mm-destroy-parts handles)
782 (message-narrow-to-head)
783 ;; Remove them, they are confusing.
784 (message-remove-header "Content-Type")
785 (message-remove-header "MIME-Version")
786 (message-remove-header "Content-Disposition")
787 (message-remove-header "Content-Transfer-Encoding")))
789 (defun mml-to-mime ()
790 "Translate the current buffer from MML to MIME."
791 (message-encode-message-body)
793 (message-narrow-to-headers-or-head)
794 ;; Skip past any From_ headers.
795 (while (looking-at "From ")
797 (let ((mail-parse-charset message-default-charset))
798 (mail-encode-encoded-word-buffer))))
800 (defun mml-insert-mime (handle &optional no-markup)
801 (let (textp buffer mmlp)
802 ;; Determine type and stuff.
803 (unless (stringp (car handle))
804 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
806 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
807 (mm-insert-part handle 'no-cache)
808 (if (setq mmlp (equal (mm-handle-media-type handle)
812 (mml-insert-mml-markup handle nil t t)
813 (unless (and no-markup
814 (equal (mm-handle-media-type handle) "text/plain"))
815 (mml-insert-mml-markup handle buffer textp)))
818 (insert-buffer-substring buffer)
819 (goto-char (point-max))
820 (insert "<#/mml>\n"))
821 ((stringp (car handle))
822 (mapcar 'mml-insert-mime (cdr handle))
823 (insert "<#/multipart>\n"))
825 (let ((charset (mail-content-type-get
826 (mm-handle-type handle) 'charset))
828 (if (eq charset 'gnus-decoded)
829 (mm-insert-part handle)
830 (insert (mm-decode-string (mm-get-part handle) charset)))
831 (mml-quote-region start (point)))
832 (goto-char (point-max)))
834 (insert "<#/part>\n")))))
836 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
837 "Take a MIME handle and insert an MML tag."
838 (if (stringp (car handle))
840 (insert "<#multipart type=" (mm-handle-media-subtype handle))
841 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
843 (insert " start=\"" start "\"")))
846 (insert "<#mml type=" (mm-handle-media-type handle))
847 (insert "<#part type=" (mm-handle-media-type handle)))
848 (dolist (elem (append (cdr (mm-handle-type handle))
849 (cdr (mm-handle-disposition handle))))
850 (unless (symbolp (cdr elem))
851 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
852 (when (mm-handle-id handle)
853 (insert " id=\"" (mm-handle-id handle) "\""))
854 (when (mm-handle-disposition handle)
855 (insert " disposition=" (car (mm-handle-disposition handle))))
857 (insert " buffer=\"" (buffer-name buffer) "\""))
859 (insert " nofile=yes"))
860 (when (mm-handle-description handle)
861 (insert " description=\"" (mm-handle-description handle) "\""))
864 (defun mml-insert-parameter (&rest parameters)
865 "Insert PARAMETERS in a nice way."
867 (dolist (param parameters)
874 (if (> (current-column) 76)
878 (goto-char (1+ end)))
882 ;;; Mode for inserting and editing MML forms
886 (let ((sign (make-sparse-keymap))
887 (encrypt (make-sparse-keymap))
888 (signpart (make-sparse-keymap))
889 (encryptpart (make-sparse-keymap))
890 (map (make-sparse-keymap))
891 (main (make-sparse-keymap)))
892 (define-key map "\C-s" 'mml-secure-message-sign)
893 (define-key map "\C-c" 'mml-secure-message-encrypt)
894 (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
895 (define-key map "\C-p\C-s" 'mml-secure-sign)
896 (define-key map "\C-p\C-c" 'mml-secure-encrypt)
897 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
898 (define-key sign "o" 'mml-secure-message-sign-pgp)
899 (define-key sign "s" 'mml-secure-message-sign-smime)
900 (define-key signpart "p" 'mml-secure-sign-pgpmime)
901 (define-key signpart "o" 'mml-secure-sign-pgp)
902 (define-key signpart "s" 'mml-secure-sign-smime)
903 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
904 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
905 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
906 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
907 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
908 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
909 (define-key map "\C-n" 'mml-unsecure-message)
910 (define-key map "f" 'mml-attach-file)
911 (define-key map "b" 'mml-attach-buffer)
912 (define-key map "e" 'mml-attach-external)
913 (define-key map "q" 'mml-quote-region)
914 (define-key map "m" 'mml-insert-multipart)
915 (define-key map "p" 'mml-insert-part)
916 (define-key map "v" 'mml-validate)
917 (define-key map "P" 'mml-preview)
918 (define-key map "s" sign)
919 (define-key map "S" signpart)
920 (define-key map "c" encrypt)
921 (define-key map "C" encryptpart)
922 ;;(define-key map "n" 'mml-narrow-to-part)
923 ;; `M-m' conflicts with `back-to-indentation'.
924 ;; (define-key main "\M-m" map)
925 (define-key main "\C-c\C-m" map)
929 mml-menu mml-mode-map ""
931 ["Attach File..." mml-attach-file
932 ,@(if (featurep 'xemacs) '(t)
933 '(:help "Attach a file at point"))]
934 ["Attach Buffer..." mml-attach-buffer
935 ,@(if (featurep 'xemacs) '(t)
936 '(:help "Attach a buffer to the outgoing MIME message"))]
937 ["Attach External..." mml-attach-external
938 ,@(if (featurep 'xemacs) '(t)
939 '(:help "Attach reference to file"))]
941 ("Change Security Method"
943 (lambda () (interactive) (setq mml-secure-method "pgpmime"))
944 ,@(if (featurep 'xemacs) nil
945 '(:help "Set Security Method to PGP/MIME"))
947 :selected (equal mml-secure-method "pgpmime") ]
949 (lambda () (interactive) (setq mml-secure-method "smime"))
950 ,@(if (featurep 'xemacs) nil
951 '(:help "Set Security Method to S/MIME"))
953 :selected (equal mml-secure-method "smime") ]
955 (lambda () (interactive) (setq mml-secure-method "pgp"))
956 ,@(if (featurep 'xemacs) nil
957 '(:help "Set Security Method to inline PGP"))
959 :selected (equal mml-secure-method "pgp") ] )
961 ["Sign Message" mml-secure-message-sign t]
962 ["Encrypt Message" mml-secure-message-encrypt t]
963 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
964 ["Encrypt/Sign off" mml-unsecure-message
965 ,@(if (featurep 'xemacs) '(t)
966 '(:help "Don't Encrypt/Sign Message"))]
967 ;; Maybe we could remove these, because people who write MML most probably
968 ;; don't use the menu:
969 ["Insert Part..." mml-insert-part
970 :active (message-in-body-p)]
971 ["Insert Multipart..." mml-insert-multipart
972 :active (message-in-body-p)]
974 ;; Do we have separate encrypt and encrypt/sign commands for parts?
975 ["Sign Part" mml-secure-sign t]
976 ["Encrypt Part" mml-secure-encrypt t]
977 ;;["Narrow" mml-narrow-to-part t]
978 ["Quote MML in region" mml-quote-region
979 :active (message-mark-active-p)
980 ,@(if (featurep 'xemacs) nil
981 '(:help "Quote MML tags in region"))]
982 ["Validate MML" mml-validate t]
983 ["Preview" mml-preview t]
985 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
986 ,@(if (featurep 'xemacs) '(t)
987 '(:help "Display the Emacs MIME manual"))]
988 ["PGG manual" (lambda () (interactive) (message-info 16))
989 ,@(if (featurep 'xemacs) '(t)
990 '(:help "Display the PGG manual"))]))
993 "Minor mode for editing MML.")
995 (defun mml-mode (&optional arg)
996 "Minor mode for editing MML.
997 MML is the MIME Meta Language, a minor mode for composing MIME articles.
998 See Info node `(emacs-mime)Composing'.
1002 (when (set (make-local-variable 'mml-mode)
1003 (if (null arg) (not mml-mode)
1004 (> (prefix-numeric-value arg) 0)))
1005 (add-minor-mode 'mml-mode " MML" mml-mode-map)
1006 (easy-menu-add mml-menu mml-mode-map)
1007 (when (boundp 'dnd-protocol-alist)
1008 (set (make-local-variable 'dnd-protocol-alist)
1009 (append mml-dnd-protocol-alist
1010 (symbol-value 'dnd-protocol-alist))))
1011 (run-hooks 'mml-mode-hook)))
1014 ;;; Helper functions for reading MIME stuff from the minibuffer and
1015 ;;; inserting stuff to the buffer.
1018 (defun mml-minibuffer-read-file (prompt)
1019 (let* ((completion-ignored-extensions nil)
1020 (file (read-file-name prompt nil nil t)))
1021 ;; Prevent some common errors. This is inspired by similar code in
1023 (when (file-directory-p file)
1024 (error "%s is a directory, cannot attach" file))
1025 (unless (file-exists-p file)
1026 (error "No such file: %s" file))
1027 (unless (file-readable-p file)
1028 (error "Permission denied: %s" file))
1031 (defun mml-minibuffer-read-type (name &optional default)
1032 (mailcap-parse-mimetypes)
1033 (let* ((default (or default
1034 (mm-default-file-encoding name)
1035 ;; Perhaps here we should check what the file
1036 ;; looks like, and offer text/plain if it looks
1038 "application/octet-stream"))
1039 (string (completing-read
1040 (format "Content type (default %s): " default)
1041 (mapcar 'list (mailcap-mime-types)))))
1042 (if (not (equal string ""))
1046 (defun mml-minibuffer-read-description ()
1047 (let ((description (read-string "One line description: ")))
1048 (when (string-match "\\`[ \t]*\\'" description)
1049 (setq description nil))
1052 (defun mml-minibuffer-read-disposition (type &optional default)
1053 (unless default (setq default
1054 (if (and (string-match "\\`text/" type)
1055 (not (string-match "\\`text/rtf\\'" type)))
1058 (let ((disposition (completing-read
1059 (format "Disposition (default %s): " default)
1060 '(("attachment") ("inline") (""))
1061 nil t nil nil default)))
1062 (if (not (equal disposition ""))
1066 (defun mml-quote-region (beg end)
1067 "Quote the MML tags in the region."
1071 ;; Temporarily narrow the region to defend from changes
1072 ;; invalidating END.
1073 (narrow-to-region beg end)
1074 (goto-char (point-min))
1076 (while (re-search-forward
1077 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
1078 ;; Insert ! after the #.
1079 (goto-char (+ (match-beginning 0) 2))
1082 (defun mml-insert-tag (name &rest plist)
1083 "Insert an MML tag described by NAME and PLIST."
1084 (when (symbolp name)
1085 (setq name (symbol-name name)))
1088 (let ((key (pop plist))
1089 (value (pop plist)))
1091 ;; Quote VALUE if it contains suspicious characters.
1092 (when (string-match "[\"'\\~/*;() \t\n]" value)
1093 (setq value (with-output-to-string
1094 (let (print-escape-nonascii)
1096 (insert (format " %s=%s" key value)))))
1099 (defun mml-insert-empty-tag (name &rest plist)
1100 "Insert an empty MML tag described by NAME and PLIST."
1101 (when (symbolp name)
1102 (setq name (symbol-name name)))
1103 (apply #'mml-insert-tag name plist)
1104 (insert "<#/" name ">\n"))
1106 ;;; Attachment functions.
1108 (defcustom mml-dnd-protocol-alist
1109 '(("^file:///" . mml-dnd-attach-file)
1110 ("^file://" . dnd-open-file)
1111 ("^file:" . mml-dnd-attach-file))
1112 "The functions to call when a drop in `mml-mode' is made.
1113 See `dnd-protocol-alist' for more information. When nil, behave
1114 as in other buffers."
1115 :type '(choice (repeat (cons (regexp) (function)))
1116 (const :tag "Behave as in other buffers" nil))
1117 :version "23.0" ;; No Gnus
1120 (defcustom mml-dnd-attach-options nil
1121 "Which options should be queried when attaching a file via drag and drop.
1123 If it is a list, valid members are `type', `description' and
1124 `disposition'. `disposition' implies `type'. If it is nil,
1125 don't ask for options. If it is t, ask the user whether or not
1126 to specify options."
1128 (const :tag "Non" nil)
1129 (const :tag "Query" t)
1130 (list :value (type description disposition)
1134 (const disposition))))
1135 :version "23.0" ;; No Gnus
1138 (defun mml-attach-file (file &optional type description disposition)
1139 "Attach a file to the outgoing MIME message.
1140 The file is not inserted or encoded until you send the message with
1141 `\\[message-send-and-exit]' or `\\[message-send]'.
1143 FILE is the name of the file to attach. TYPE is its
1144 content-type, a string of the form \"type/subtype\". DESCRIPTION
1145 is a one-line description of the attachment. The DISPOSITION
1146 specifies how the attachment is intended to be displayed. It can
1147 be either \"inline\" (displayed automatically within the message
1148 body) or \"attachment\" (separate from the body)."
1150 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1151 (type (mml-minibuffer-read-type file))
1152 (description (mml-minibuffer-read-description))
1153 (disposition (mml-minibuffer-read-disposition type)))
1154 (list file type description disposition)))
1156 (unless (message-in-body-p) (goto-char (point-max)))
1157 (mml-insert-empty-tag 'part
1160 'disposition (or disposition "attachment")
1161 'description description)))
1163 (defun mml-dnd-attach-file (uri action)
1164 "Attach a drag and drop file.
1166 Ask for type, description or disposition according to
1167 `mml-dnd-attach-options'."
1168 (let ((file (dnd-get-local-file-name uri t)))
1169 (when (and file (file-regular-p file))
1170 (let ((mml-dnd-attach-options mml-dnd-attach-options)
1171 type description disposition)
1172 (setq mml-dnd-attach-options
1173 (when (and (eq mml-dnd-attach-options t)
1176 "Use default type, disposition and description? ")))
1177 '(type description disposition)))
1178 (when (or (memq 'type mml-dnd-attach-options)
1179 (memq 'disposition mml-dnd-attach-options))
1180 (setq type (mml-minibuffer-read-type file)))
1181 (when (memq 'description mml-dnd-attach-options)
1182 (setq description (mml-minibuffer-read-description)))
1183 (when (memq 'disposition mml-dnd-attach-options)
1184 (setq disposition (mml-minibuffer-read-disposition type)))
1185 (mml-attach-file file type description disposition)))))
1187 (defun mml-attach-buffer (buffer &optional type description)
1188 "Attach a buffer to the outgoing MIME message.
1189 See `mml-attach-file' for details of operation."
1191 (let* ((buffer (read-buffer "Attach buffer: "))
1192 (type (mml-minibuffer-read-type buffer "text/plain"))
1193 (description (mml-minibuffer-read-description)))
1194 (list buffer type description)))
1196 (unless (message-in-body-p) (goto-char (point-max)))
1197 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1198 'disposition "attachment"
1199 'description description)))
1201 (defun mml-attach-external (file &optional type description)
1202 "Attach an external file into the buffer.
1203 FILE is an ange-ftp/efs specification of the part location.
1204 TYPE is the MIME type to use."
1206 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1207 (type (mml-minibuffer-read-type file))
1208 (description (mml-minibuffer-read-description)))
1209 (list file type description)))
1211 (unless (message-in-body-p) (goto-char (point-max)))
1212 (mml-insert-empty-tag 'external 'type type 'name file
1213 'disposition "attachment" 'description description)))
1215 (defun mml-insert-multipart (&optional type)
1216 (interactive (list (completing-read "Multipart type (default mixed): "
1217 '(("mixed") ("alternative") ("digest") ("parallel")
1218 ("signed") ("encrypted"))
1221 (setq type "mixed"))
1222 (mml-insert-empty-tag "multipart" 'type type)
1225 (defun mml-insert-part (&optional type)
1227 (list (mml-minibuffer-read-type "")))
1228 (mml-insert-tag 'part 'type type 'disposition "inline")
1231 (defun mml-preview-insert-mail-followup-to ()
1232 "Insert a Mail-Followup-To header before previewing an article.
1233 Should be adopted if code in `message-send-mail' is changed."
1234 (when (and (message-mail-p)
1235 (message-subscribed-p)
1236 (not (mail-fetch-field "mail-followup-to"))
1237 (message-make-mail-followup-to))
1238 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1239 (insert (message-make-mail-followup-to))))
1241 (defvar mml-preview-buffer nil)
1243 (defun mml-preview (&optional raw)
1244 "Display current buffer with Gnus, in a new buffer.
1245 If RAW, display a raw encoded MIME message.
1247 The window layout for the preview buffer is controled by the variables
1248 `special-display-buffer-names', `special-display-regexps', or
1249 `gnus-buffer-configuration' (the first match made will be used),
1250 or the `pop-to-buffer' function."
1252 (setq mml-preview-buffer (generate-new-buffer
1253 (concat (if raw "*Raw MIME preview of "
1254 "*MIME preview of ") (buffer-name))))
1256 (let* ((buf (current-buffer))
1257 (message-options message-options)
1258 (message-this-is-mail (message-mail-p))
1259 (message-this-is-news (message-news-p))
1260 (message-posting-charset (or (gnus-setup-posting-charset
1262 (message-narrow-to-headers-or-head)
1263 (message-fetch-field "Newsgroups")))
1264 message-posting-charset)))
1265 (message-options-set-recipient)
1266 (when (boundp 'gnus-buffers)
1267 (push mml-preview-buffer gnus-buffers))
1270 (set-buffer mml-preview-buffer)
1272 (insert-buffer-substring buf))
1273 (mml-preview-insert-mail-followup-to)
1274 (let ((message-deletable-headers (if (message-news-p)
1276 message-deletable-headers)))
1277 (message-generate-headers
1278 (copy-sequence (if (message-news-p)
1279 message-required-news-headers
1280 message-required-mail-headers))))
1281 (if (re-search-forward
1282 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1283 (replace-match "\n"))
1284 (let ((mail-header-separator ""));; mail-header-separator is removed.
1285 (message-sort-headers)
1288 (when (fboundp 'set-buffer-multibyte)
1289 (let ((s (buffer-string)))
1290 ;; Insert the content into unibyte buffer.
1292 (mm-disable-multibyte)
1294 (let ((gnus-newsgroup-charset (car message-posting-charset))
1295 gnus-article-prepare-hook gnus-original-article-buffer)
1296 (run-hooks 'gnus-article-decode-hook)
1297 (let ((gnus-newsgroup-name "dummy")
1298 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1299 (gnus-make-hashtable 5))))
1300 (gnus-article-prepare-display))))
1301 ;; Disable article-mode-map.
1303 (gnus-make-local-hook 'kill-buffer-hook)
1304 (add-hook 'kill-buffer-hook
1306 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1307 (setq buffer-read-only t)
1308 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1309 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1313 (widget-button-press (point))))
1314 (local-set-key gnus-mouse-2
1317 (widget-button-press (widget-event-point event) event)))
1318 ;; FIXME: Buffer is in article mode, but most tool bar commands won't
1319 ;; work. Maybe only keep the following icons: search, print, quit
1320 (goto-char (point-min))))
1321 (if (and (not (mm-special-display-p (buffer-name mml-preview-buffer)))
1322 (boundp 'gnus-buffer-configuration)
1323 (assq 'mml-preview gnus-buffer-configuration))
1324 (let ((gnus-message-buffer (current-buffer)))
1325 (gnus-configure-windows 'mml-preview))
1326 (pop-to-buffer mml-preview-buffer)))
1328 (defun mml-validate ()
1329 "Validate the current MML document."
1333 (defun mml-tweak-part (cont)
1335 (let ((tweak (cdr (assq 'tweak cont)))
1340 (or (cdr (assoc tweak mml-tweak-function-alist))
1342 (mml-tweak-type-alist
1343 (let ((alist mml-tweak-type-alist)
1344 (type (or (cdr (assq 'type cont)) "text/plain")))
1346 (if (string-match (caar alist) type)
1347 (setq func (cdar alist)
1349 (setq alist (cdr alist)))))))
1353 (let ((alist mml-tweak-sexp-alist))
1355 (if (eval (caar alist))
1356 (funcall (cdar alist) cont))
1357 (setq alist (cdr alist)))))
1360 (defun mml-tweak-externalize-attachments (cont)
1361 "Tweak attached files as external parts."
1362 (let (filename-cons)
1363 (when (and (eq (car cont) 'part)
1364 (not (cdr (assq 'buffer cont)))
1365 (and (setq filename-cons (assq 'filename cont))
1366 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1367 (setcar cont 'external)
1368 (setcar filename-cons 'name))))
1372 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1373 ;;; mml.el ends here