1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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-add-minor-mode "gnus-ems")
39 (autoload 'gnus-make-local-hook "gnus-util")
40 (autoload 'message-fetch-field "message")
41 (autoload 'message-info "message")
42 (autoload 'fill-flowed-encode "flow-fill")
43 (autoload 'message-posting-charset "message")
44 (autoload 'dnd-get-local-file-name "dnd"))
46 (defvar gnus-article-mime-handles)
48 (defvar gnus-newsrc-hashtb)
49 (defvar message-default-charset)
50 (defvar message-deletable-headers)
51 (defvar message-options)
52 (defvar message-posting-charset)
53 (defvar message-required-mail-headers)
54 (defvar message-required-news-headers)
55 (defvar dnd-protocol-alist)
57 (defcustom mml-content-type-parameters
58 '(name access-type expiration size permission format)
59 "*A list of acceptable parameters in MML tag.
60 These parameters are generated in Content-Type header if exists."
62 :type '(repeat (symbol :tag "Parameter"))
65 (defcustom mml-content-disposition-parameters
66 '(filename creation-date modification-date read-date)
67 "*A list of acceptable parameters in MML tag.
68 These parameters are generated in Content-Disposition header if exists."
70 :type '(repeat (symbol :tag "Parameter"))
73 (defcustom mml-insert-mime-headers-always nil
74 "If non-nil, always put Content-Type: text/plain at top of empty parts.
75 It is necessary to work against a bug in certain clients."
80 (defvar mml-tweak-type-alist nil
81 "A list of (TYPE . FUNCTION) for tweaking MML parts.
82 TYPE is a string containing a regexp to match the MIME type. FUNCTION
83 is a Lisp function which is called with the MML handle to tweak the
84 part. This variable is used only when no TWEAK parameter exists in
87 (defvar mml-tweak-function-alist nil
88 "A list of (NAME . FUNCTION) for tweaking MML parts.
89 NAME is a string containing the name of the TWEAK parameter in the MML
90 handle. FUNCTION is a Lisp function which is called with the MML
91 handle to tweak the part.")
93 (defvar mml-tweak-sexp-alist
94 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
95 "A list of (SEXP . FUNCTION) for tweaking MML parts.
96 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
97 is called. FUNCTION is a Lisp function which is called with the MML
98 handle to tweak the part.")
100 (defvar mml-externalize-attachments nil
101 "*If non-nil, local-file attachments are generated as external parts.")
103 (defvar mml-generate-multipart-alist nil
104 "*Alist of multipart generation functions.
105 Each entry has the form (NAME . FUNCTION), where
106 NAME is a string containing the name of the part (without the
107 leading \"/multipart/\"),
108 FUNCTION is a Lisp function which is called to generate the part.
110 The Lisp function has to supply the appropriate MIME headers and the
111 contents of this part.")
113 (defvar mml-syntax-table
114 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
115 (modify-syntax-entry ?\\ "/" table)
116 (modify-syntax-entry ?< "(" table)
117 (modify-syntax-entry ?> ")" table)
118 (modify-syntax-entry ?@ "w" table)
119 (modify-syntax-entry ?/ "w" table)
120 (modify-syntax-entry ?= " " table)
121 (modify-syntax-entry ?* " " table)
122 (modify-syntax-entry ?\; " " table)
123 (modify-syntax-entry ?\' " " table)
126 (defvar mml-boundary-function 'mml-make-boundary
127 "A function called to suggest a boundary.
128 The function may be called several times, and should try to make a new
129 suggestion each time. The function is called with one parameter,
130 which is a number that says how many times the function has been
131 called for this message.")
133 (defvar mml-confirmation-set nil
134 "A list of symbols, each of which disables some warning.
135 `unknown-encoding': always send messages contain characters with
136 unknown encoding; `use-ascii': always use ASCII for those characters
137 with unknown encoding; `multipart': always send messages with more than
140 (defvar mml-generate-default-type "text/plain"
141 "Content type by which the Content-Type header can be omitted.
142 The Content-Type header will not be put in the MIME part if the type
143 equals the value and there's no parameter (e.g. charset, format, etc.)
144 and `mml-insert-mime-headers-always' is nil. The value will be bound
145 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
146 part. This is for the internal use, you should never modify the value.")
148 (defvar mml-buffer-list nil)
150 (defun mml-generate-new-buffer (name)
151 (let ((buf (generate-new-buffer name)))
152 (push buf mml-buffer-list)
155 (defun mml-destroy-buffers ()
156 (let (kill-buffer-hook)
157 (mapcar 'kill-buffer mml-buffer-list)
158 (setq mml-buffer-list nil)))
161 "Parse the current buffer as an MML document."
163 (goto-char (point-min))
164 (let ((table (syntax-table)))
167 (set-syntax-table mml-syntax-table)
169 (set-syntax-table table)))))
171 (defun mml-parse-1 ()
172 "Parse the current buffer as an MML document."
173 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
174 (while (and (not (eobp))
175 (not (looking-at "<#/multipart")))
177 ((looking-at "<#secure")
178 ;; The secure part is essentially a meta-meta tag, which
179 ;; expands to either a part tag if there are no other parts in
180 ;; the document or a multipart tag if there are other parts
181 ;; included in the message
183 (taginfo (mml-read-tag))
184 (recipients (cdr (assq 'recipients taginfo)))
185 (sender (cdr (assq 'sender taginfo)))
186 (location (cdr (assq 'tag-location taginfo)))
187 (mode (cdr (assq 'mode taginfo)))
188 (method (cdr (assq 'method taginfo)))
193 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
194 (setq secure-mode "multipart")
195 (setq secure-mode "part")))
198 (re-search-forward "<#secure[^\n]*>\n"))
199 (delete-region (match-beginning 0) (match-end 0))
200 (cond ((string= mode "sign")
201 (setq tags (list "sign" method)))
202 ((string= mode "encrypt")
203 (setq tags (list "encrypt" method)))
204 ((string= mode "signencrypt")
205 (setq tags (list "sign" method "encrypt" method))))
206 (eval `(mml-insert-tag ,secure-mode
208 ,(if recipients "recipients")
210 ,(if sender "sender")
213 (goto-char location)))
214 ((looking-at "<#multipart")
215 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
216 ((looking-at "<#external")
217 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
220 (if (or (looking-at "<#part") (looking-at "<#mml"))
221 (setq tag (mml-read-tag)
224 (setq tag (list 'part '(type . "text/plain"))
227 (setq raw (cdr (assq 'raw tag))
229 contents (mml-read-part (eq 'mml (car tag)))
234 (intern (downcase (cdr (assq 'charset tag))))))
236 (mm-find-mime-charset-region point (point)))))
237 (when (and (not raw) (memq nil charsets))
238 (if (or (memq 'unknown-encoding mml-confirmation-set)
239 (message-options-get 'unknown-encoding)
241 Message contains characters with unknown encoding. Really send? ")
242 (message-options-set 'unknown-encoding t)))
244 (or (memq 'use-ascii mml-confirmation-set)
245 (message-options-get 'use-ascii)
246 (and (y-or-n-p "Use ASCII as charset? ")
247 (message-options-set 'use-ascii t))))
248 (setq charsets (delq nil charsets))
250 (error "Edit your message to remove those characters")))
253 (< (length charsets) 2))
254 (if (or (not no-markup-p)
255 (string-match "[^ \t\r\n]" contents))
256 ;; Don't create blank parts.
257 (push (nconc tag (list (cons 'contents contents)))
259 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
260 tag point (point) use-ascii)))
262 (not (memq 'multipart mml-confirmation-set))
263 (not (message-options-get 'multipart))
264 (not (and (y-or-n-p (format "\
265 A message part needs to be split into %d charset parts. Really send? "
267 (message-options-set 'multipart t))))
268 (error "Edit your message to use only one charset"))
269 (setq struct (nconc nstruct struct)))))))
274 (defun mml-parse-singlepart-with-multiple-charsets
275 (orig-tag beg end &optional use-ascii)
278 (narrow-to-region beg end)
279 (goto-char (point-min))
280 (let ((current (or (mm-mime-charset (mm-charset-after))
281 (and use-ascii 'us-ascii)))
282 charset struct space newline paragraph)
284 (setq charset (mm-mime-charset (mm-charset-after)))
286 ;; The charset remains the same.
287 ((eq charset 'us-ascii))
288 ((or (and use-ascii (not charset))
289 (eq charset current))
293 ;; The initial charset was ascii.
294 ((eq current 'us-ascii)
295 (setq current charset
299 ;; We have a change in charsets.
303 (list (cons 'contents
304 (buffer-substring-no-properties
305 beg (or paragraph newline space (point))))))
307 (setq beg (or paragraph newline space (point))
312 ;; Compute places where it might be nice to break the part.
314 ((memq (following-char) '(? ?\t))
315 (setq space (1+ (point))))
316 ((and (eq (following-char) ?\n)
318 (eq (char-after (1- (point))) ?\n))
319 (setq paragraph (point)))
320 ((eq (following-char) ?\n)
321 (setq newline (1+ (point)))))
323 ;; Do the final part.
324 (unless (= beg (point))
325 (push (append orig-tag
326 (list (cons 'contents
327 (buffer-substring-no-properties
332 (defun mml-read-tag ()
333 "Read a tag and return the contents."
334 (let ((orig-point (point))
335 contents name elem val)
337 (setq name (buffer-substring-no-properties
338 (point) (progn (forward-sexp 1) (point))))
339 (skip-chars-forward " \t\n")
340 (while (not (looking-at ">[ \t]*\n?"))
341 (setq elem (buffer-substring-no-properties
342 (point) (progn (forward-sexp 1) (point))))
343 (skip-chars-forward "= \t\n")
344 (setq val (buffer-substring-no-properties
345 (point) (progn (forward-sexp 1) (point))))
346 (when (string-match "^\"\\(.*\\)\"$" val)
347 (setq val (match-string 1 val)))
348 (push (cons (intern elem) val) contents)
349 (skip-chars-forward " \t\n"))
350 (goto-char (match-end 0))
351 ;; Don't skip the leading space.
352 ;;(skip-chars-forward " \t\n")
353 ;; Put the tag location into the returned contents
354 (setq contents (append (list (cons 'tag-location orig-point)) contents))
355 (cons (intern name) (nreverse contents))))
357 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
358 (let ((str (buffer-substring-no-properties start end))
359 (bufstart start) tmp)
360 (while (setq tmp (text-property-any start end 'hard 't))
361 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
363 (setq start (1+ tmp)))
366 (defun mml-read-part (&optional mml)
367 "Return the buffer up till the next part, multipart or closing part or multipart.
368 If MML is non-nil, return the buffer up till the correspondent mml tag."
369 (let ((beg (point)) (count 1))
370 ;; If the tag ended at the end of the line, we go to the next line.
371 (when (looking-at "[ \t]*\n")
375 (while (and (> count 0) (not (eobp)))
376 (if (re-search-forward "<#\\(/\\)?mml." nil t)
377 (setq count (+ count (if (match-beginning 1) -1 1)))
378 (goto-char (point-max))))
379 (mml-buffer-substring-no-properties-except-hard-newlines
382 (match-beginning 0))))
383 (if (re-search-forward
384 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
386 (mml-buffer-substring-no-properties-except-hard-newlines
387 beg (match-beginning 0))
388 (if (or (not (match-beginning 1))
389 (equal (match-string 2) "multipart"))
390 (goto-char (match-beginning 0))
391 (when (looking-at "[ \t]*\n")
393 (mml-buffer-substring-no-properties-except-hard-newlines
394 beg (goto-char (point-max)))))))
396 (defvar mml-boundary nil)
397 (defvar mml-base-boundary "-=-=")
398 (defvar mml-multipart-number 0)
400 (defun mml-generate-mime ()
401 "Generate a MIME message based on the current MML document."
402 (let ((cont (mml-parse))
403 (mml-multipart-number mml-multipart-number))
406 (mm-with-multibyte-buffer
407 (if (and (consp (car cont))
409 (mml-generate-mime-1 (car cont))
410 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
414 (defun mml-generate-mime-1 (cont)
415 (let ((mm-use-ultra-safe-encoding
416 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
418 (narrow-to-region (point) (point))
419 (mml-tweak-part cont)
421 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
422 (let* ((raw (cdr (assq 'raw cont)))
423 (filename (cdr (assq 'filename cont)))
424 (type (or (cdr (assq 'type cont))
426 (or (mm-default-file-encoding filename)
427 "application/octet-stream")
429 coded encoding charset flowed)
431 (member (car (split-string type "/")) '("text" "message")))
434 (setq charset (mm-charset-to-coding-system
435 (cdr (assq 'charset cont))))
436 (when (eq charset 'ascii)
439 ((cdr (assq 'buffer cont))
440 (insert-buffer-substring (cdr (assq 'buffer cont))))
442 (not (equal (cdr (assq 'nofile cont)) "yes")))
443 (let ((coding-system-for-read charset))
444 (mm-insert-file-contents filename)))
445 ((eq 'mml (car cont))
446 (insert (cdr (assq 'contents cont))))
449 (narrow-to-region (point) (point))
450 (insert (cdr (assq 'contents cont)))
451 ;; Remove quotes from quoted tags.
452 (goto-char (point-min))
453 (while (re-search-forward
454 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
456 (delete-region (+ (match-beginning 0) 2)
457 (+ (match-beginning 0) 3))))))
459 ((eq (car cont) 'mml)
460 (let ((mml-boundary (mml-compute-boundary cont))
461 ;; It is necessary for the case where this
462 ;; function is called recursively since
463 ;; `m-g-d-t' will be bound to "message/rfc822"
464 ;; when encoding an article to be forwarded.
465 (mml-generate-default-type "text/plain"))
467 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
468 ;; ignore 0x1b, it is part of iso-2022-jp
469 (setq encoding (mm-body-7-or-8))))
470 ((string= (car (split-string type "/")) "message")
471 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
472 ;; ignore 0x1b, it is part of iso-2022-jp
473 (setq encoding (mm-body-7-or-8))))
475 ;; Only perform format=flowed filling on text/plain
476 ;; parts where there either isn't a format parameter
477 ;; in the mml tag or it says "flowed" and there
478 ;; actually are hard newlines in the text.
479 (let (use-hard-newlines)
480 (when (and (string= type "text/plain")
481 (not (string= (cdr (assq 'sign cont)) "pgp"))
482 (or (null (assq 'format cont))
483 (string= (cdr (assq 'format cont))
485 (setq use-hard-newlines
487 (point-min) (point-max) 'hard 't)))
489 ;; Indicate that `mml-insert-mime-headers' should
490 ;; insert a "; format=flowed" string unless the
491 ;; user has already specified it.
492 (setq flowed (null (assq 'format cont)))))
493 (setq charset (mm-encode-body charset))
494 (setq encoding (mm-body-encoding
495 charset (cdr (assq 'encoding cont))))))
496 (setq coded (buffer-string)))
497 (mml-insert-mime-headers cont type charset encoding flowed)
500 (mm-with-unibyte-buffer
502 ((cdr (assq 'buffer cont))
503 (insert (mm-string-as-unibyte
504 (with-current-buffer (cdr (assq 'buffer cont))
507 (not (equal (cdr (assq 'nofile cont)) "yes")))
508 (let ((coding-system-for-read mm-binary-coding-system))
509 (mm-insert-file-contents filename nil nil nil nil t)))
511 (let ((contents (cdr (assq 'contents cont))))
512 (if (if (featurep 'xemacs)
513 (string-match "[^\000-\377]" contents)
514 (mm-multibyte-string-p contents))
516 (mm-enable-multibyte)
519 (setq charset (mm-encode-body))))
520 (insert contents)))))
521 (setq encoding (mm-encode-buffer type)
522 coded (mm-string-as-multibyte (buffer-string))))
523 (mml-insert-mime-headers cont type charset encoding nil)
524 (insert "\n" coded))))
525 ((eq (car cont) 'external)
526 (insert "Content-Type: message/external-body")
527 (let ((parameters (mml-parameter-string
528 cont '(expiration size permission)))
529 (name (cdr (assq 'name cont)))
530 (url (cdr (assq 'url cont))))
532 (setq name (mml-parse-file-name name))
534 (mml-insert-parameter
535 (mail-header-encode-parameter "name" name)
536 "access-type=local-file")
537 (mml-insert-parameter
538 (mail-header-encode-parameter
539 "name" (file-name-nondirectory (nth 2 name)))
540 (mail-header-encode-parameter "site" (nth 1 name))
541 (mail-header-encode-parameter
542 "directory" (file-name-directory (nth 2 name))))
543 (mml-insert-parameter
544 (concat "access-type="
545 (if (member (nth 0 name) '("ftp@" "anonymous@"))
549 (mml-insert-parameter
550 (mail-header-encode-parameter "url" url)
553 (mml-insert-parameter-string
554 cont '(expiration size permission)))
556 (insert "Content-Type: "
557 (or (cdr (assq 'type cont))
559 (or (mm-default-file-encoding name)
560 "application/octet-stream")
563 (insert "Content-ID: " (message-make-message-id) "\n")
564 (insert "Content-Transfer-Encoding: "
565 (or (cdr (assq 'encoding cont)) "binary"))
567 (insert (or (cdr (assq 'contents cont))))
569 ((eq (car cont) 'multipart)
570 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
571 (mml-generate-default-type (if (equal type "digest")
574 (handler (assoc type mml-generate-multipart-alist)))
576 (funcall (cdr handler) cont)
577 ;; No specific handler. Use default one.
578 (let ((mml-boundary (mml-compute-boundary cont)))
579 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
581 (if (cdr (assq 'start cont))
582 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
584 (let ((cont cont) part)
585 (while (setq part (pop cont))
586 ;; Skip `multipart' and attributes.
587 (when (and (consp part) (consp (cdr part)))
588 (insert "\n--" mml-boundary "\n")
589 (mml-generate-mime-1 part)
590 (goto-char (point-max)))))
591 (insert "\n--" mml-boundary "--\n")))))
593 (error "Invalid element: %S" cont)))
594 ;; handle sign & encrypt tags in a semi-smart way.
595 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
596 (encrypt-item (assoc (cdr (assq 'encrypt cont))
599 (when (or sign-item encrypt-item)
600 (when (setq sender (cdr (assq 'sender cont)))
601 (message-options-set 'mml-sender sender)
602 (message-options-set 'message-sender sender))
603 (if (setq recipients (cdr (assq 'recipients cont)))
604 (message-options-set 'message-recipients recipients))
605 (let ((style (mml-signencrypt-style
606 (first (or sign-item encrypt-item)))))
607 ;; check if: we're both signing & encrypting, both methods
608 ;; are the same (why would they be different?!), and that
609 ;; the signencrypt style allows for combined operation.
610 (if (and sign-item encrypt-item (equal (first sign-item)
611 (first encrypt-item))
612 (equal style 'combined))
613 (funcall (nth 1 encrypt-item) cont t)
614 ;; otherwise, revert to the old behavior.
616 (funcall (nth 1 sign-item) cont))
618 (funcall (nth 1 encrypt-item) cont)))))))))
620 (defun mml-compute-boundary (cont)
621 "Return a unique boundary that does not exist in CONT."
622 (let ((mml-boundary (funcall mml-boundary-function
623 (incf mml-multipart-number))))
624 ;; This function tries again and again until it has found
625 ;; a unique boundary.
626 (while (not (catch 'not-unique
627 (mml-compute-boundary-1 cont))))
630 (defun mml-compute-boundary-1 (cont)
633 ((eq (car cont) 'part)
636 ((cdr (assq 'buffer cont))
637 (insert-buffer-substring (cdr (assq 'buffer cont))))
638 ((and (setq filename (cdr (assq 'filename cont)))
639 (not (equal (cdr (assq 'nofile cont)) "yes")))
640 (mm-insert-file-contents filename nil nil nil nil t))
642 (insert (cdr (assq 'contents cont)))))
643 (goto-char (point-min))
644 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
646 (setq mml-boundary (funcall mml-boundary-function
647 (incf mml-multipart-number)))
648 (throw 'not-unique nil))))
649 ((eq (car cont) 'multipart)
650 (mapcar 'mml-compute-boundary-1 (cddr cont))))
653 (defun mml-make-boundary (number)
654 (concat (make-string (% number 60) ?=)
660 (defun mml-insert-mime-headers (cont type charset encoding flowed)
661 (let (parameters id disposition description)
663 (mml-parameter-string
664 cont mml-content-type-parameters))
668 (not (equal type mml-generate-default-type))
669 mml-insert-mime-headers-always)
670 (when (consp charset)
672 "Can't encode a part with several charsets"))
673 (insert "Content-Type: " type)
675 (mml-insert-parameter
676 (mail-header-encode-parameter "charset" (symbol-name charset))))
678 (mml-insert-parameter "format=flowed"))
680 (mml-insert-parameter-string
681 cont mml-content-type-parameters))
683 (when (setq id (cdr (assq 'id cont)))
684 (insert "Content-ID: " id "\n"))
686 (mml-parameter-string
687 cont mml-content-disposition-parameters))
688 (when (or (setq disposition (cdr (assq 'disposition cont)))
690 (insert "Content-Disposition: " (or disposition "inline"))
692 (mml-insert-parameter-string
693 cont mml-content-disposition-parameters))
695 (unless (eq encoding '7bit)
696 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
697 (when (setq description (cdr (assq 'description cont)))
698 (insert "Content-Description: ")
699 (setq description (prog1
701 (insert description "\n")))
702 (mail-encode-encoded-word-region description (point)))))
704 (defun mml-parameter-string (cont types)
707 (while (setq type (pop types))
708 (when (setq value (cdr (assq type cont)))
709 ;; Strip directory component from the filename parameter.
710 (when (eq type 'filename)
711 (setq value (file-name-nondirectory value)))
712 (setq string (concat string "; "
713 (mail-header-encode-parameter
714 (symbol-name type) value)))))
715 (when (not (zerop (length string)))
718 (defun mml-insert-parameter-string (cont types)
720 (while (setq type (pop types))
721 (when (setq value (cdr (assq type cont)))
722 ;; Strip directory component from the filename parameter.
723 (when (eq type 'filename)
724 (setq value (file-name-nondirectory value)))
725 (mml-insert-parameter
726 (mail-header-encode-parameter
727 (symbol-name type) value))))))
730 (defvar ange-ftp-name-format)
731 (defvar efs-path-regexp))
732 (defun mml-parse-file-name (path)
733 (if (if (boundp 'efs-path-regexp)
734 (string-match efs-path-regexp path)
735 (if (boundp 'ange-ftp-name-format)
736 (string-match (car ange-ftp-name-format) path)))
737 (list (match-string 1 path) (match-string 2 path)
738 (substring path (1+ (match-end 2))))
741 (defun mml-insert-buffer (buffer)
742 "Insert BUFFER at point and quote any MML markup."
744 (narrow-to-region (point) (point))
745 (insert-buffer-substring buffer)
746 (mml-quote-region (point-min) (point-max))
747 (goto-char (point-max))))
750 ;;; Transforming MIME to MML
753 (defun mime-to-mml (&optional handles)
754 "Translate the current buffer (which should be a message) into MML.
755 If HANDLES is non-nil, use it instead reparsing the buffer."
756 ;; First decode the head.
758 (message-narrow-to-head)
759 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
760 (mail-decode-encoded-word-region (point-min) (point-max))))
762 (setq handles (mm-dissect-buffer t)))
763 (goto-char (point-min))
764 (search-forward "\n\n" nil t)
765 (delete-region (point) (point-max))
766 (if (stringp (car handles))
767 (mml-insert-mime handles)
768 (mml-insert-mime handles t))
769 (mm-destroy-parts handles)
771 (message-narrow-to-head)
772 ;; Remove them, they are confusing.
773 (message-remove-header "Content-Type")
774 (message-remove-header "MIME-Version")
775 (message-remove-header "Content-Disposition")
776 (message-remove-header "Content-Transfer-Encoding")))
778 (defun mml-to-mime ()
779 "Translate the current buffer from MML to MIME."
780 (message-encode-message-body)
782 (message-narrow-to-headers-or-head)
783 ;; Skip past any From_ headers.
784 (while (looking-at "From ")
786 (let ((mail-parse-charset message-default-charset))
787 (mail-encode-encoded-word-buffer))))
789 (defun mml-insert-mime (handle &optional no-markup)
790 (let (textp buffer mmlp)
791 ;; Determine type and stuff.
792 (unless (stringp (car handle))
793 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
795 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
796 (mm-insert-part handle 'no-cache)
797 (if (setq mmlp (equal (mm-handle-media-type handle)
801 (mml-insert-mml-markup handle nil t t)
802 (unless (and no-markup
803 (equal (mm-handle-media-type handle) "text/plain"))
804 (mml-insert-mml-markup handle buffer textp)))
807 (insert-buffer-substring buffer)
808 (goto-char (point-max))
809 (insert "<#/mml>\n"))
810 ((stringp (car handle))
811 (mapcar 'mml-insert-mime (cdr handle))
812 (insert "<#/multipart>\n"))
814 (let ((charset (mail-content-type-get
815 (mm-handle-type handle) 'charset))
817 (if (eq charset 'gnus-decoded)
818 (mm-insert-part handle)
819 (insert (mm-decode-string (mm-get-part handle) charset)))
820 (mml-quote-region start (point)))
821 (goto-char (point-max)))
823 (insert "<#/part>\n")))))
825 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
826 "Take a MIME handle and insert an MML tag."
827 (if (stringp (car handle))
829 (insert "<#multipart type=" (mm-handle-media-subtype handle))
830 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
832 (insert " start=\"" start "\"")))
835 (insert "<#mml type=" (mm-handle-media-type handle))
836 (insert "<#part type=" (mm-handle-media-type handle)))
837 (dolist (elem (append (cdr (mm-handle-type handle))
838 (cdr (mm-handle-disposition handle))))
839 (unless (symbolp (cdr elem))
840 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
841 (when (mm-handle-id handle)
842 (insert " id=\"" (mm-handle-id handle) "\""))
843 (when (mm-handle-disposition handle)
844 (insert " disposition=" (car (mm-handle-disposition handle))))
846 (insert " buffer=\"" (buffer-name buffer) "\""))
848 (insert " nofile=yes"))
849 (when (mm-handle-description handle)
850 (insert " description=\"" (mm-handle-description handle) "\""))
853 (defun mml-insert-parameter (&rest parameters)
854 "Insert PARAMETERS in a nice way."
856 (dolist (param parameters)
863 (if (> (current-column) 76)
867 (goto-char (1+ end)))
871 ;;; Mode for inserting and editing MML forms
875 (let ((sign (make-sparse-keymap))
876 (encrypt (make-sparse-keymap))
877 (signpart (make-sparse-keymap))
878 (encryptpart (make-sparse-keymap))
879 (map (make-sparse-keymap))
880 (main (make-sparse-keymap)))
881 (define-key map "\C-s" 'mml-secure-message-sign)
882 (define-key map "\C-c" 'mml-secure-message-encrypt)
883 (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
884 (define-key map "\C-p\C-s" 'mml-secure-sign)
885 (define-key map "\C-p\C-c" 'mml-secure-encrypt)
886 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
887 (define-key sign "o" 'mml-secure-message-sign-pgp)
888 (define-key sign "s" 'mml-secure-message-sign-smime)
889 (define-key signpart "p" 'mml-secure-sign-pgpmime)
890 (define-key signpart "o" 'mml-secure-sign-pgp)
891 (define-key signpart "s" 'mml-secure-sign-smime)
892 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
893 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
894 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
895 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
896 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
897 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
898 (define-key map "\C-n" 'mml-unsecure-message)
899 (define-key map "f" 'mml-attach-file)
900 (define-key map "b" 'mml-attach-buffer)
901 (define-key map "e" 'mml-attach-external)
902 (define-key map "q" 'mml-quote-region)
903 (define-key map "m" 'mml-insert-multipart)
904 (define-key map "p" 'mml-insert-part)
905 (define-key map "v" 'mml-validate)
906 (define-key map "P" 'mml-preview)
907 (define-key map "s" sign)
908 (define-key map "S" signpart)
909 (define-key map "c" encrypt)
910 (define-key map "C" encryptpart)
911 ;;(define-key map "n" 'mml-narrow-to-part)
912 ;; `M-m' conflicts with `back-to-indentation'.
913 ;; (define-key main "\M-m" map)
914 (define-key main "\C-c\C-m" map)
918 mml-menu mml-mode-map ""
920 ["Attach File..." mml-attach-file
921 ,@(if (featurep 'xemacs) '(t)
922 '(:help "Attach a file at point"))]
923 ["Attach Buffer..." mml-attach-buffer
924 ,@(if (featurep 'xemacs) '(t)
925 '(:help "Attach a buffer to the outgoing MIME message"))]
926 ["Attach External..." mml-attach-external
927 ,@(if (featurep 'xemacs) '(t)
928 '(:help "Attach reference to file"))]
930 ("Change Security Method"
932 (lambda () (interactive) (setq mml-secure-method "pgpmime"))
933 ,@(if (featurep 'xemacs) nil
934 '(:help "Set Security Method to PGP/MIME"))
936 :selected (equal mml-secure-method "pgpmime") ]
938 (lambda () (interactive) (setq mml-secure-method "smime"))
939 ,@(if (featurep 'xemacs) nil
940 '(:help "Set Security Method to S/MIME"))
942 :selected (equal mml-secure-method "smime") ]
944 (lambda () (interactive) (setq mml-secure-method "pgp"))
945 ,@(if (featurep 'xemacs) nil
946 '(:help "Set Security Method to inline PGP"))
948 :selected (equal mml-secure-method "pgp") ] )
950 ["Sign Message" mml-secure-message-sign t]
951 ["Encrypt Message" mml-secure-message-encrypt t]
952 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
953 ["Encrypt/Sign off" mml-unsecure-message
954 ,@(if (featurep 'xemacs) '(t)
955 '(:help "Don't Encrypt/Sign Message"))]
956 ;; Maybe we could remove these, because people who write MML most probably
957 ;; don't use the menu:
958 ["Insert Part..." mml-insert-part
959 :active (message-in-body-p)]
960 ["Insert Multipart..." mml-insert-multipart
961 :active (message-in-body-p)]
963 ;; Do we have separate encrypt and encrypt/sign commands for parts?
964 ["Sign Part" mml-secure-sign t]
965 ["Encrypt Part" mml-secure-encrypt t]
966 ;;["Narrow" mml-narrow-to-part t]
967 ["Quote MML in region" mml-quote-region
968 :active (message-mark-active-p)
969 ,@(if (featurep 'xemacs) nil
970 '(:help "Quote MML tags in region"))]
971 ["Validate MML" mml-validate t]
972 ["Preview" mml-preview t]
974 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
975 ,@(if (featurep 'xemacs) '(t)
976 '(:help "Display the Emacs MIME manual"))]
977 ["PGG manual" (lambda () (interactive) (message-info 16))
978 ,@(if (featurep 'xemacs) '(t)
979 '(:help "Display the PGG manual"))]))
982 "Minor mode for editing MML.")
984 (defun mml-mode (&optional arg)
985 "Minor mode for editing MML.
986 MML is the MIME Meta Language, a minor mode for composing MIME articles.
987 See Info node `(emacs-mime)Composing'.
991 (when (set (make-local-variable 'mml-mode)
992 (if (null arg) (not mml-mode)
993 (> (prefix-numeric-value arg) 0)))
994 (add-minor-mode 'mml-mode " MML" mml-mode-map)
995 (easy-menu-add mml-menu mml-mode-map)
996 (when (boundp 'dnd-protocol-alist)
997 (set (make-local-variable 'dnd-protocol-alist)
998 (append mml-dnd-protocol-alist dnd-protocol-alist)))
999 (run-hooks 'mml-mode-hook)))
1002 ;;; Helper functions for reading MIME stuff from the minibuffer and
1003 ;;; inserting stuff to the buffer.
1006 (defun mml-minibuffer-read-file (prompt)
1007 (let* ((completion-ignored-extensions nil)
1008 (file (read-file-name prompt nil nil t)))
1009 ;; Prevent some common errors. This is inspired by similar code in
1011 (when (file-directory-p file)
1012 (error "%s is a directory, cannot attach" file))
1013 (unless (file-exists-p file)
1014 (error "No such file: %s" file))
1015 (unless (file-readable-p file)
1016 (error "Permission denied: %s" file))
1019 (defun mml-minibuffer-read-type (name &optional default)
1020 (mailcap-parse-mimetypes)
1021 (let* ((default (or default
1022 (mm-default-file-encoding name)
1023 ;; Perhaps here we should check what the file
1024 ;; looks like, and offer text/plain if it looks
1026 "application/octet-stream"))
1027 (string (completing-read
1028 (format "Content type (default %s): " default)
1029 (mapcar 'list (mailcap-mime-types)))))
1030 (if (not (equal string ""))
1034 (defun mml-minibuffer-read-description ()
1035 (let ((description (read-string "One line description: ")))
1036 (when (string-match "\\`[ \t]*\\'" description)
1037 (setq description nil))
1040 (defun mml-minibuffer-read-disposition (type &optional default)
1041 (unless default (setq default
1042 (if (and (string-match "\\`text/" type)
1043 (not (string-match "\\`text/rtf\\'" type)))
1046 (let ((disposition (completing-read
1047 (format "Disposition (default %s): " default)
1048 '(("attachment") ("inline") (""))
1049 nil t nil nil default)))
1050 (if (not (equal disposition ""))
1054 (defun mml-quote-region (beg end)
1055 "Quote the MML tags in the region."
1059 ;; Temporarily narrow the region to defend from changes
1060 ;; invalidating END.
1061 (narrow-to-region beg end)
1062 (goto-char (point-min))
1064 (while (re-search-forward
1065 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
1066 ;; Insert ! after the #.
1067 (goto-char (+ (match-beginning 0) 2))
1070 (defun mml-insert-tag (name &rest plist)
1071 "Insert an MML tag described by NAME and PLIST."
1072 (when (symbolp name)
1073 (setq name (symbol-name name)))
1076 (let ((key (pop plist))
1077 (value (pop plist)))
1079 ;; Quote VALUE if it contains suspicious characters.
1080 (when (string-match "[\"'\\~/*;() \t\n]" value)
1081 (setq value (with-output-to-string
1082 (let (print-escape-nonascii)
1084 (insert (format " %s=%s" key value)))))
1087 (defun mml-insert-empty-tag (name &rest plist)
1088 "Insert an empty MML tag described by NAME and PLIST."
1089 (when (symbolp name)
1090 (setq name (symbol-name name)))
1091 (apply #'mml-insert-tag name plist)
1092 (insert "<#/" name ">\n"))
1094 ;;; Attachment functions.
1096 (defcustom mml-dnd-protocol-alist
1097 '(("^file:///" . mml-dnd-attach-file)
1098 ("^file://" . dnd-open-file)
1099 ("^file:" . mml-dnd-attach-file))
1100 "The functions to call when a drop in `mml-mode' is made.
1101 See `dnd-protocol-alist' for more information. When nil, behave
1102 as in other buffers."
1103 :type '(choice (repeat (cons (regexp) (function)))
1104 (const :tag "Behave as in other buffers" nil))
1105 :version "22.1" ;; Gnus 5.10.9
1108 (defcustom mml-dnd-attach-options nil
1109 "Which options should be queried when attaching a file via drag and drop.
1111 If it is a list, valid members are `type', `description' and
1112 `disposition'. `disposition' implies `type'. If it is nil,
1113 don't ask for options. If it is t, ask the user whether or not
1114 to specify options."
1116 (const :tag "Non" nil)
1117 (const :tag "Query" t)
1118 (list :value (type description disposition)
1122 (const disposition))))
1123 :version "22.1" ;; Gnus 5.10.9
1126 (defun mml-attach-file (file &optional type description disposition)
1127 "Attach a file to the outgoing MIME message.
1128 The file is not inserted or encoded until you send the message with
1129 `\\[message-send-and-exit]' or `\\[message-send]'.
1131 FILE is the name of the file to attach. TYPE is its
1132 content-type, a string of the form \"type/subtype\". DESCRIPTION
1133 is a one-line description of the attachment. The DISPOSITION
1134 specifies how the attachment is intended to be displayed. It can
1135 be either \"inline\" (displayed automatically within the message
1136 body) or \"attachment\" (separate from the body)."
1138 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1139 (type (mml-minibuffer-read-type file))
1140 (description (mml-minibuffer-read-description))
1141 (disposition (mml-minibuffer-read-disposition type)))
1142 (list file type description disposition)))
1144 (unless (message-in-body-p) (goto-char (point-max)))
1145 (mml-insert-empty-tag 'part
1148 'disposition (or disposition "attachment")
1149 'description description)))
1151 (defun mml-dnd-attach-file (uri action)
1152 "Attach a drag and drop file.
1154 Ask for type, description or disposition according to
1155 `mml-dnd-attach-options'."
1156 (let ((file (dnd-get-local-file-name uri t)))
1157 (when (and file (file-regular-p file))
1158 (let ((mml-dnd-attach-options mml-dnd-attach-options)
1159 type description disposition)
1160 (setq mml-dnd-attach-options
1161 (when (and (eq mml-dnd-attach-options t)
1164 "Use default type, disposition and description? ")))
1165 '(type description disposition)))
1166 (when (or (memq 'type mml-dnd-attach-options)
1167 (memq 'disposition mml-dnd-attach-options))
1168 (setq type (mml-minibuffer-read-type file)))
1169 (when (memq 'description mml-dnd-attach-options)
1170 (setq description (mml-minibuffer-read-description)))
1171 (when (memq 'disposition mml-dnd-attach-options)
1172 (setq disposition (mml-minibuffer-read-disposition type)))
1173 (mml-attach-file file type description disposition)))))
1175 (defun mml-attach-buffer (buffer &optional type description)
1176 "Attach a buffer to the outgoing MIME message.
1177 See `mml-attach-file' for details of operation."
1179 (let* ((buffer (read-buffer "Attach buffer: "))
1180 (type (mml-minibuffer-read-type buffer "text/plain"))
1181 (description (mml-minibuffer-read-description)))
1182 (list buffer type description)))
1184 (unless (message-in-body-p) (goto-char (point-max)))
1185 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1186 'disposition "attachment"
1187 'description description)))
1189 (defun mml-attach-external (file &optional type description)
1190 "Attach an external file into the buffer.
1191 FILE is an ange-ftp/efs specification of the part location.
1192 TYPE is the MIME type to use."
1194 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1195 (type (mml-minibuffer-read-type file))
1196 (description (mml-minibuffer-read-description)))
1197 (list file type description)))
1199 (unless (message-in-body-p) (goto-char (point-max)))
1200 (mml-insert-empty-tag 'external 'type type 'name file
1201 'disposition "attachment" 'description description)))
1203 (defun mml-insert-multipart (&optional type)
1204 (interactive (list (completing-read "Multipart type (default mixed): "
1205 '(("mixed") ("alternative") ("digest") ("parallel")
1206 ("signed") ("encrypted"))
1209 (setq type "mixed"))
1210 (mml-insert-empty-tag "multipart" 'type type)
1213 (defun mml-insert-part (&optional type)
1215 (list (mml-minibuffer-read-type "")))
1216 (mml-insert-tag 'part 'type type 'disposition "inline")
1219 (defun mml-preview-insert-mail-followup-to ()
1220 "Insert a Mail-Followup-To header before previewing an article.
1221 Should be adopted if code in `message-send-mail' is changed."
1222 (when (and (message-mail-p)
1223 (message-subscribed-p)
1224 (not (mail-fetch-field "mail-followup-to"))
1225 (message-make-mail-followup-to))
1226 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1227 (insert (message-make-mail-followup-to))))
1229 (defun mml-preview (&optional raw)
1230 "Display current buffer with Gnus, in a new buffer.
1231 If RAW, display a raw encoded MIME message."
1234 (let* ((buf (current-buffer))
1235 (message-options message-options)
1236 (message-this-is-mail (message-mail-p))
1237 (message-this-is-news (message-news-p))
1238 (message-posting-charset (or (gnus-setup-posting-charset
1240 (message-narrow-to-headers-or-head)
1241 (message-fetch-field "Newsgroups")))
1242 message-posting-charset)))
1243 (message-options-set-recipient)
1244 (pop-to-buffer (generate-new-buffer
1245 (concat (if raw "*Raw MIME preview of "
1246 "*MIME preview of ") (buffer-name))))
1247 (when (boundp 'gnus-buffers)
1248 (push (current-buffer) gnus-buffers))
1250 (insert-buffer-substring buf)
1251 (mml-preview-insert-mail-followup-to)
1252 (let ((message-deletable-headers (if (message-news-p)
1254 message-deletable-headers)))
1255 (message-generate-headers
1256 (copy-sequence (if (message-news-p)
1257 message-required-news-headers
1258 message-required-mail-headers))))
1259 (if (re-search-forward
1260 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1261 (replace-match "\n"))
1262 (let ((mail-header-separator ""));; mail-header-separator is removed.
1265 (when (fboundp 'set-buffer-multibyte)
1266 (let ((s (buffer-string)))
1267 ;; Insert the content into unibyte buffer.
1269 (mm-disable-multibyte)
1271 (let ((gnus-newsgroup-charset (car message-posting-charset))
1272 gnus-article-prepare-hook gnus-original-article-buffer)
1273 (run-hooks 'gnus-article-decode-hook)
1274 (let ((gnus-newsgroup-name "dummy")
1275 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1276 (gnus-make-hashtable 5))))
1277 (gnus-article-prepare-display))))
1278 ;; Disable article-mode-map.
1280 (gnus-make-local-hook 'kill-buffer-hook)
1281 (add-hook 'kill-buffer-hook
1283 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1284 (setq buffer-read-only t)
1285 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1286 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1290 (widget-button-press (point))))
1291 (local-set-key gnus-mouse-2
1294 (widget-button-press (widget-event-point event) event)))
1295 (goto-char (point-min)))))
1297 (defun mml-validate ()
1298 "Validate the current MML document."
1302 (defun mml-tweak-part (cont)
1304 (let ((tweak (cdr (assq 'tweak cont)))
1309 (or (cdr (assoc tweak mml-tweak-function-alist))
1311 (mml-tweak-type-alist
1312 (let ((alist mml-tweak-type-alist)
1313 (type (or (cdr (assq 'type cont)) "text/plain")))
1315 (if (string-match (caar alist) type)
1316 (setq func (cdar alist)
1318 (setq alist (cdr alist)))))))
1322 (let ((alist mml-tweak-sexp-alist))
1324 (if (eval (caar alist))
1325 (funcall (cdar alist) cont))
1326 (setq alist (cdr alist)))))
1329 (defun mml-tweak-externalize-attachments (cont)
1330 "Tweak attached files as external parts."
1331 (let (filename-cons)
1332 (when (and (eq (car cont) 'part)
1333 (not (cdr (assq 'buffer cont)))
1334 (and (setq filename-cons (assq 'filename cont))
1335 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1336 (setcar cont 'external)
1337 (setcar filename-cons 'name))))
1341 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1342 ;;; mml.el ends here