* gnus-msg.el (gnus-copy-article-buffer): Quote decoded words containing
[gnus] / lisp / mml.el
1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'mm-util)
28 (require 'mm-bodies)
29 (require 'mm-encode)
30 (require 'mm-decode)
31 (require 'mml-sec)
32 (eval-when-compile (require 'cl))
33
34 (eval-and-compile
35   (autoload 'message-make-message-id "message")
36   (autoload 'gnus-setup-posting-charset "gnus-msg")
37   (autoload 'gnus-make-local-hook "gnus-util")
38   (autoload 'message-fetch-field "message")
39   (autoload 'message-mark-active-p "message")
40   (autoload 'fill-flowed-encode "flow-fill")
41   (autoload 'message-posting-charset "message")
42   (autoload 'x-dnd-get-local-file-name "x-dnd"))
43
44 (defcustom mml-content-type-parameters
45   '(name access-type expiration size permission format)
46   "*A list of acceptable parameters in MML tag.
47 These parameters are generated in Content-Type header if exists."
48   :version "22.1"
49   :type '(repeat (symbol :tag "Parameter"))
50   :group 'message)
51
52 (defcustom mml-content-disposition-parameters
53   '(filename creation-date modification-date read-date)
54   "*A list of acceptable parameters in MML tag.
55 These parameters are generated in Content-Disposition header if exists."
56   :version "22.1"
57   :type '(repeat (symbol :tag "Parameter"))
58   :group 'message)
59
60 (defcustom mml-insert-mime-headers-always nil
61   "If non-nil, always put Content-Type: text/plain at top of empty parts.
62 It is necessary to work against a bug in certain clients."
63   :version "22.1"
64   :type 'boolean
65   :group 'message)
66
67 (defvar mml-tweak-type-alist nil
68   "A list of (TYPE . FUNCTION) for tweaking MML parts.
69 TYPE is a string containing a regexp to match the MIME type.  FUNCTION
70 is a Lisp function which is called with the MML handle to tweak the
71 part.  This variable is used only when no TWEAK parameter exists in
72 the MML handle.")
73
74 (defvar mml-tweak-function-alist nil
75   "A list of (NAME . FUNCTION) for tweaking MML parts.
76 NAME is a string containing the name of the TWEAK parameter in the MML
77 handle.  FUNCTION is a Lisp function which is called with the MML
78 handle to tweak the part.")
79
80 (defvar mml-tweak-sexp-alist
81   '((mml-externalize-attachments . mml-tweak-externalize-attachments))
82   "A list of (SEXP . FUNCTION) for tweaking MML parts.
83 SEXP is an s-expression.  If the evaluation of SEXP is non-nil, FUNCTION
84 is called.  FUNCTION is a Lisp function which is called with the MML
85 handle to tweak the part.")
86
87 (defvar mml-externalize-attachments nil
88   "*If non-nil, local-file attachments are generated as external parts.")
89
90 (defvar mml-generate-multipart-alist nil
91   "*Alist of multipart generation functions.
92 Each entry has the form (NAME . FUNCTION), where
93 NAME is a string containing the name of the part (without the
94 leading \"/multipart/\"),
95 FUNCTION is a Lisp function which is called to generate the part.
96
97 The Lisp function has to supply the appropriate MIME headers and the
98 contents of this part.")
99
100 (defvar mml-syntax-table
101   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
102     (modify-syntax-entry ?\\ "/" table)
103     (modify-syntax-entry ?< "(" table)
104     (modify-syntax-entry ?> ")" table)
105     (modify-syntax-entry ?@ "w" table)
106     (modify-syntax-entry ?/ "w" table)
107     (modify-syntax-entry ?= " " table)
108     (modify-syntax-entry ?* " " table)
109     (modify-syntax-entry ?\; " " table)
110     (modify-syntax-entry ?\' " " table)
111     table))
112
113 (defvar mml-boundary-function 'mml-make-boundary
114   "A function called to suggest a boundary.
115 The function may be called several times, and should try to make a new
116 suggestion each time.  The function is called with one parameter,
117 which is a number that says how many times the function has been
118 called for this message.")
119
120 (defvar mml-confirmation-set nil
121   "A list of symbols, each of which disables some warning.
122 `unknown-encoding': always send messages contain characters with
123 unknown encoding; `use-ascii': always use ASCII for those characters
124 with unknown encoding; `multipart': always send messages with more than
125 one charsets.")
126
127 (defvar mml-generate-default-type "text/plain")
128
129 (defvar mml-buffer-list nil)
130
131 (defun mml-generate-new-buffer (name)
132   (let ((buf (generate-new-buffer name)))
133     (push buf mml-buffer-list)
134     buf))
135
136 (defun mml-destroy-buffers ()
137   (let (kill-buffer-hook)
138     (mapc 'kill-buffer mml-buffer-list)
139     (setq mml-buffer-list nil)))
140
141 (defun mml-parse ()
142   "Parse the current buffer as an MML document."
143   (save-excursion
144     (goto-char (point-min))
145     (with-syntax-table mml-syntax-table
146       (mml-parse-1))))
147
148 (defun mml-parse-1 ()
149   "Parse the current buffer as an MML document."
150   (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
151     (while (and (not (eobp))
152                 (not (looking-at "<#/multipart")))
153       (cond
154        ((looking-at "<#secure")
155         ;; The secure part is essentially a meta-meta tag, which
156         ;; expands to either a part tag if there are no other parts in
157         ;; the document or a multipart tag if there are other parts
158         ;; included in the message
159         (let* (secure-mode
160                (taginfo (mml-read-tag))
161                (recipients (cdr (assq 'recipients taginfo)))
162                (sender (cdr (assq 'sender taginfo)))
163                (location (cdr (assq 'tag-location taginfo)))
164                (mode (cdr (assq 'mode taginfo)))
165                (method (cdr (assq 'method taginfo)))
166                tags)
167           (save-excursion
168             (if (re-search-forward
169                  "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
170                 (setq secure-mode "multipart")
171               (setq secure-mode "part")))
172           (save-excursion
173             (goto-char location)
174             (re-search-forward "<#secure[^\n]*>\n"))
175           (delete-region (match-beginning 0) (match-end 0))
176           (cond ((string= mode "sign")
177                  (setq tags (list "sign" method)))
178                 ((string= mode "encrypt")
179                  (setq tags (list "encrypt" method)))
180                 ((string= mode "signencrypt")
181                  (setq tags (list "sign" method "encrypt" method))))
182           (eval `(mml-insert-tag ,secure-mode
183                                  ,@tags
184                                  ,(if recipients "recipients")
185                                  ,recipients
186                                  ,(if sender "sender")
187                                  ,sender))
188           ;; restart the parse
189           (goto-char location)))
190        ((looking-at "<#multipart")
191         (push (nconc (mml-read-tag) (mml-parse-1)) struct))
192        ((looking-at "<#external")
193         (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
194               struct))
195        (t
196         (if (or (looking-at "<#part") (looking-at "<#mml"))
197             (setq tag (mml-read-tag)
198                   no-markup-p nil
199                   warn nil)
200           (setq tag (list 'part '(type . "text/plain"))
201                 no-markup-p t
202                 warn t))
203         (setq raw (cdr (assq 'raw tag))
204               point (point)
205               contents (mml-read-part (eq 'mml (car tag)))
206               charsets (cond
207                         (raw nil)
208                         ((assq 'charset tag)
209                          (list
210                           (intern (downcase (cdr (assq 'charset tag))))))
211                         (t
212                          (mm-find-mime-charset-region point (point)
213                                                       mm-hack-charsets))))
214         (when (and (not raw) (memq nil charsets))
215           (if (or (memq 'unknown-encoding mml-confirmation-set)
216                   (message-options-get 'unknown-encoding)
217                   (and (y-or-n-p "\
218 Message contains characters with unknown encoding.  Really send? ")
219                        (message-options-set 'unknown-encoding t)))
220               (if (setq use-ascii
221                         (or (memq 'use-ascii mml-confirmation-set)
222                             (message-options-get 'use-ascii)
223                             (and (y-or-n-p "Use ASCII as charset? ")
224                                  (message-options-set 'use-ascii t))))
225                   (setq charsets (delq nil charsets))
226                 (setq warn nil))
227             (error "Edit your message to remove those characters")))
228         (if (or raw
229                 (eq 'mml (car tag))
230                 (< (length charsets) 2))
231             (if (or (not no-markup-p)
232                     (string-match "[^ \t\r\n]" contents))
233                 ;; Don't create blank parts.
234                 (push (nconc tag (list (cons 'contents contents)))
235                       struct))
236           (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
237                           tag point (point) use-ascii)))
238             (when (and warn
239                        (not (memq 'multipart mml-confirmation-set))
240                        (not (message-options-get 'multipart))
241                        (not (and (y-or-n-p (format "\
242 A message part needs to be split into %d charset parts.  Really send? "
243                                                    (length nstruct)))
244                                  (message-options-set 'multipart t))))
245               (error "Edit your message to use only one charset"))
246             (setq struct (nconc nstruct struct)))))))
247     (unless (eobp)
248       (forward-line 1))
249     (nreverse struct)))
250
251 (defun mml-parse-singlepart-with-multiple-charsets
252   (orig-tag beg end &optional use-ascii)
253   (save-excursion
254     (save-restriction
255       (narrow-to-region beg end)
256       (goto-char (point-min))
257       (let ((current (or (mm-mime-charset (mm-charset-after))
258                          (and use-ascii 'us-ascii)))
259             charset struct space newline paragraph)
260         (while (not (eobp))
261           (setq charset (mm-mime-charset (mm-charset-after)))
262           (cond
263            ;; The charset remains the same.
264            ((eq charset 'us-ascii))
265            ((or (and use-ascii (not charset))
266                 (eq charset current))
267             (setq space nil
268                   newline nil
269                   paragraph nil))
270            ;; The initial charset was ascii.
271            ((eq current 'us-ascii)
272             (setq current charset
273                   space nil
274                   newline nil
275                   paragraph nil))
276            ;; We have a change in charsets.
277            (t
278             (push (append
279                    orig-tag
280                    (list (cons 'contents
281                                (buffer-substring-no-properties
282                                 beg (or paragraph newline space (point))))))
283                   struct)
284             (setq beg (or paragraph newline space (point))
285                   current charset
286                   space nil
287                   newline nil
288                   paragraph nil)))
289           ;; Compute places where it might be nice to break the part.
290           (cond
291            ((memq (following-char) '(?  ?\t))
292             (setq space (1+ (point))))
293            ((and (eq (following-char) ?\n)
294                  (not (bobp))
295                  (eq (char-after (1- (point))) ?\n))
296             (setq paragraph (point)))
297            ((eq (following-char) ?\n)
298             (setq newline (1+ (point)))))
299           (forward-char 1))
300         ;; Do the final part.
301         (unless (= beg (point))
302           (push (append orig-tag
303                         (list (cons 'contents
304                                     (buffer-substring-no-properties
305                                      beg (point)))))
306                 struct))
307         struct))))
308
309 (defun mml-read-tag ()
310   "Read a tag and return the contents."
311   (let ((orig-point (point))
312         contents name elem val)
313     (forward-char 2)
314     (setq name (buffer-substring-no-properties
315                 (point) (progn (forward-sexp 1) (point))))
316     (skip-chars-forward " \t\n")
317     (while (not (looking-at ">[ \t]*\n?"))
318       (setq elem (buffer-substring-no-properties
319                   (point) (progn (forward-sexp 1) (point))))
320       (skip-chars-forward "= \t\n")
321       (setq val (buffer-substring-no-properties
322                  (point) (progn (forward-sexp 1) (point))))
323       (when (string-match "^\"\\(.*\\)\"$" val)
324         (setq val (match-string 1 val)))
325       (push (cons (intern elem) val) contents)
326       (skip-chars-forward " \t\n"))
327     (goto-char (match-end 0))
328     ;; Don't skip the leading space.
329     ;;(skip-chars-forward " \t\n")
330     ;; Put the tag location into the returned contents
331     (setq contents (append (list (cons 'tag-location orig-point)) contents))
332     (cons (intern name) (nreverse contents))))
333
334 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
335   (let ((str (buffer-substring-no-properties start end))
336         (bufstart start) tmp)
337     (while (setq tmp (text-property-any start end 'hard 't))
338       (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
339                            '(hard t) str)
340       (setq start (1+ tmp)))
341     str))
342
343 (defun mml-read-part (&optional mml)
344   "Return the buffer up till the next part, multipart or closing part or multipart.
345 If MML is non-nil, return the buffer up till the correspondent mml tag."
346   (let ((beg (point)) (count 1))
347     ;; If the tag ended at the end of the line, we go to the next line.
348     (when (looking-at "[ \t]*\n")
349       (forward-line 1))
350     (if mml
351         (progn
352           (while (and (> count 0) (not (eobp)))
353             (if (re-search-forward "<#\\(/\\)?mml." nil t)
354                 (setq count (+ count (if (match-beginning 1) -1 1)))
355               (goto-char (point-max))))
356           (mml-buffer-substring-no-properties-except-hard-newlines
357            beg (if (> count 0)
358                    (point)
359                  (match-beginning 0))))
360       (if (re-search-forward
361            "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
362           (prog1
363               (mml-buffer-substring-no-properties-except-hard-newlines
364                beg (match-beginning 0))
365             (if (or (not (match-beginning 1))
366                     (equal (match-string 2) "multipart"))
367                 (goto-char (match-beginning 0))
368               (when (looking-at "[ \t]*\n")
369                 (forward-line 1))))
370         (mml-buffer-substring-no-properties-except-hard-newlines
371          beg (goto-char (point-max)))))))
372
373 (defvar mml-boundary nil)
374 (defvar mml-base-boundary "-=-=")
375 (defvar mml-multipart-number 0)
376
377 (defun mml-generate-mime ()
378   "Generate a MIME message based on the current MML document."
379   (let ((cont (mml-parse))
380         (mml-multipart-number mml-multipart-number))
381     (if (not cont)
382         nil
383       (with-temp-buffer
384         (if (and (consp (car cont))
385                  (= (length cont) 1))
386             (mml-generate-mime-1 (car cont))
387           (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
388                                       cont)))
389         (buffer-string)))))
390
391 (defun mml-generate-mime-1 (cont)
392   (let ((mm-use-ultra-safe-encoding
393          (or mm-use-ultra-safe-encoding (assq 'sign cont))))
394     (save-restriction
395       (narrow-to-region (point) (point))
396       (mml-tweak-part cont)
397       (cond
398        ((or (eq (car cont) 'part) (eq (car cont) 'mml))
399         (let ((raw (cdr (assq 'raw cont)))
400               type charset coding filename encoding flowed coded)
401           (setq type (or (cdr (assq 'type cont)) "text/plain")
402                 charset (cdr (assq 'charset cont))
403                 coding (mm-charset-to-coding-system charset))
404           (cond ((eq coding 'ascii)
405                  (setq charset nil
406                        coding nil))
407                 (charset
408                  (setq charset (intern (downcase charset)))))
409           (if (and (not raw)
410                    (member (car (split-string type "/")) '("text" "message")))
411               (progn
412                 (with-temp-buffer
413                   (cond
414                    ((cdr (assq 'buffer cont))
415                     (insert-buffer-substring (cdr (assq 'buffer cont))))
416                    ((and (setq filename (cdr (assq 'filename cont)))
417                          (not (equal (cdr (assq 'nofile cont)) "yes")))
418                     (let ((coding-system-for-read coding))
419                       (mm-insert-file-contents filename)))
420                    ((eq 'mml (car cont))
421                     (insert (cdr (assq 'contents cont))))
422                    (t
423                     (save-restriction
424                       (narrow-to-region (point) (point))
425                       (insert (cdr (assq 'contents cont)))
426                       ;; Remove quotes from quoted tags.
427                       (goto-char (point-min))
428                       (while (re-search-forward
429                               "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
430                               nil t)
431                         (delete-region (+ (match-beginning 0) 2)
432                                        (+ (match-beginning 0) 3))))))
433                   (cond
434                    ((eq (car cont) 'mml)
435                     (let ((mml-boundary (mml-compute-boundary cont))
436                           (mml-generate-default-type "text/plain"))
437                       (mml-to-mime))
438                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
439                       ;; ignore 0x1b, it is part of iso-2022-jp
440                       (setq encoding (mm-body-7-or-8))))
441                    ((string= (car (split-string type "/")) "message")
442                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
443                       ;; ignore 0x1b, it is part of iso-2022-jp
444                       (setq encoding (mm-body-7-or-8))))
445                    (t
446                     ;; Only perform format=flowed filling on text/plain
447                     ;; parts where there either isn't a format parameter
448                     ;; in the mml tag or it says "flowed" and there
449                     ;; actually are hard newlines in the text.
450                     (let (use-hard-newlines)
451                       (when (and (string= type "text/plain")
452                                  (not (string= (cdr (assq 'sign cont)) "pgp"))
453                                  (or (null (assq 'format cont))
454                                      (string= (cdr (assq 'format cont))
455                                               "flowed"))
456                                  (setq use-hard-newlines
457                                        (text-property-any
458                                         (point-min) (point-max) 'hard 't)))
459                         (fill-flowed-encode)
460                         ;; Indicate that `mml-insert-mime-headers' should
461                         ;; insert a "; format=flowed" string unless the
462                         ;; user has already specified it.
463                         (setq flowed (null (assq 'format cont)))))
464                     (setq charset (mm-encode-body charset))
465                     (setq encoding (mm-body-encoding
466                                     charset (cdr (assq 'encoding cont))))))
467                   (setq coded (buffer-string)))
468                 (mml-insert-mime-headers cont type charset encoding flowed)
469                 (insert "\n")
470                 (insert coded))
471             (mm-with-unibyte-buffer
472               (cond
473                ((cdr (assq 'buffer cont))
474                 (insert (with-current-buffer (cdr (assq 'buffer cont))
475                           (mm-with-unibyte-current-buffer
476                             (buffer-string)))))
477                ((and (setq filename (cdr (assq 'filename cont)))
478                      (not (equal (cdr (assq 'nofile cont)) "yes")))
479                 (let ((coding-system-for-read mm-binary-coding-system))
480                   (mm-insert-file-contents filename nil nil nil nil t))
481                 (unless charset
482                   (setq charset (mm-coding-system-to-mime-charset
483                                  (mm-find-buffer-file-coding-system
484                                   filename)))))
485                (t
486                 (insert (cdr (assq 'contents cont)))))
487               (setq encoding (mm-encode-buffer type)
488                     coded (mm-string-as-multibyte (buffer-string))))
489             (mml-insert-mime-headers cont type charset encoding nil)
490             (insert "\n")
491             (mm-with-unibyte-current-buffer
492               (insert coded)))))
493        ((eq (car cont) 'external)
494         (insert "Content-Type: message/external-body")
495         (let ((parameters (mml-parameter-string
496                            cont '(expiration size permission)))
497               (name (cdr (assq 'name cont)))
498               (url (cdr (assq 'url cont))))
499           (when name
500             (setq name (mml-parse-file-name name))
501             (if (stringp name)
502                 (mml-insert-parameter
503                  (mail-header-encode-parameter "name" name)
504                  "access-type=local-file")
505               (mml-insert-parameter
506                (mail-header-encode-parameter
507                 "name" (file-name-nondirectory (nth 2 name)))
508                (mail-header-encode-parameter "site" (nth 1 name))
509                (mail-header-encode-parameter
510                 "directory" (file-name-directory (nth 2 name))))
511               (mml-insert-parameter
512                (concat "access-type="
513                        (if (member (nth 0 name) '("ftp@" "anonymous@"))
514                            "anon-ftp"
515                          "ftp")))))
516           (when url
517             (mml-insert-parameter
518              (mail-header-encode-parameter "url" url)
519              "access-type=url"))
520           (when parameters
521             (mml-insert-parameter-string
522              cont '(expiration size permission))))
523         (insert "\n\n")
524         (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
525         (insert "Content-ID: " (message-make-message-id) "\n")
526         (insert "Content-Transfer-Encoding: "
527                 (or (cdr (assq 'encoding cont)) "binary"))
528         (insert "\n\n")
529         (insert (or (cdr (assq 'contents cont))))
530         (insert "\n"))
531        ((eq (car cont) 'multipart)
532         (let* ((type (or (cdr (assq 'type cont)) "mixed"))
533                (mml-generate-default-type (if (equal type "digest")
534                                               "message/rfc822"
535                                             "text/plain"))
536                (handler (assoc type mml-generate-multipart-alist)))
537           (if handler
538               (funcall (cdr handler) cont)
539             ;; No specific handler.  Use default one.
540             (let ((mml-boundary (mml-compute-boundary cont)))
541               (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
542                               type mml-boundary)
543                       (if (cdr (assq 'start cont))
544                           (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
545                         "\n"))
546               (let ((cont cont) part)
547                 (while (setq part (pop cont))
548                   ;; Skip `multipart' and attributes.
549                   (when (and (consp part) (consp (cdr part)))
550                     (insert "\n--" mml-boundary "\n")
551                     (mml-generate-mime-1 part))))
552               (insert "\n--" mml-boundary "--\n")))))
553        (t
554         (error "Invalid element: %S" cont)))
555       ;; handle sign & encrypt tags in a semi-smart way.
556       (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
557             (encrypt-item (assoc (cdr (assq 'encrypt cont))
558                                  mml-encrypt-alist))
559             sender recipients)
560         (when (or sign-item encrypt-item)
561           (when (setq sender (cdr (assq 'sender cont)))
562             (message-options-set 'mml-sender sender)
563             (message-options-set 'message-sender sender))
564           (if (setq recipients (cdr (assq 'recipients cont)))
565               (message-options-set 'message-recipients recipients))
566           (let ((style (mml-signencrypt-style
567                         (first (or sign-item encrypt-item)))))
568             ;; check if: we're both signing & encrypting, both methods
569             ;; are the same (why would they be different?!), and that
570             ;; the signencrypt style allows for combined operation.
571             (if (and sign-item encrypt-item (equal (first sign-item)
572                                                    (first encrypt-item))
573                      (equal style 'combined))
574                 (funcall (nth 1 encrypt-item) cont t)
575               ;; otherwise, revert to the old behavior.
576               (when sign-item
577                 (funcall (nth 1 sign-item) cont))
578               (when encrypt-item
579                 (funcall (nth 1 encrypt-item) cont)))))))))
580
581 (defun mml-compute-boundary (cont)
582   "Return a unique boundary that does not exist in CONT."
583   (let ((mml-boundary (funcall mml-boundary-function
584                                (incf mml-multipart-number))))
585     ;; This function tries again and again until it has found
586     ;; a unique boundary.
587     (while (not (catch 'not-unique
588                   (mml-compute-boundary-1 cont))))
589     mml-boundary))
590
591 (defun mml-compute-boundary-1 (cont)
592   (let (filename)
593     (cond
594      ((eq (car cont) 'part)
595       (with-temp-buffer
596         (cond
597          ((cdr (assq 'buffer cont))
598           (insert-buffer-substring (cdr (assq 'buffer cont))))
599          ((and (setq filename (cdr (assq 'filename cont)))
600                (not (equal (cdr (assq 'nofile cont)) "yes")))
601           (mm-insert-file-contents filename nil nil nil nil t))
602          (t
603           (insert (cdr (assq 'contents cont)))))
604         (goto-char (point-min))
605         (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
606                                  nil t)
607           (setq mml-boundary (funcall mml-boundary-function
608                                       (incf mml-multipart-number)))
609           (throw 'not-unique nil))))
610      ((eq (car cont) 'multipart)
611       (mapc 'mml-compute-boundary-1 (cddr cont))))
612     t))
613
614 (defun mml-make-boundary (number)
615   (concat (make-string (% number 60) ?=)
616           (if (> number 17)
617               (format "%x" number)
618             "")
619           mml-base-boundary))
620
621 (defun mml-insert-mime-headers (cont type charset encoding flowed)
622   (let (parameters id disposition description)
623     (setq parameters
624           (mml-parameter-string
625            cont mml-content-type-parameters))
626     (when (or charset
627               parameters
628               flowed
629               (not (equal type mml-generate-default-type))
630               mml-insert-mime-headers-always)
631       (when (consp charset)
632         (error
633          "Can't encode a part with several charsets"))
634       (insert "Content-Type: " type)
635       (when charset
636         (insert "; " (mail-header-encode-parameter
637                       "charset" (symbol-name charset))))
638       (when flowed
639         (insert "; format=flowed"))
640       (when parameters
641         (mml-insert-parameter-string
642          cont mml-content-type-parameters))
643       (insert "\n"))
644     (when (setq id (cdr (assq 'id cont)))
645       (insert "Content-ID: " id "\n"))
646     (setq parameters
647           (mml-parameter-string
648            cont mml-content-disposition-parameters))
649     (when (or (setq disposition (cdr (assq 'disposition cont)))
650               parameters)
651       (insert "Content-Disposition: " (or disposition "inline"))
652       (when parameters
653         (mml-insert-parameter-string
654          cont mml-content-disposition-parameters))
655       (insert "\n"))
656     (unless (eq encoding '7bit)
657       (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
658     (when (setq description (cdr (assq 'description cont)))
659       (insert "Content-Description: "
660               (mail-encode-encoded-word-string description) "\n"))))
661
662 (defun mml-parameter-string (cont types)
663   (let ((string "")
664         value type)
665     (while (setq type (pop types))
666       (when (setq value (cdr (assq type cont)))
667         ;; Strip directory component from the filename parameter.
668         (when (eq type 'filename)
669           (setq value (file-name-nondirectory value)))
670         (setq string (concat string "; "
671                              (mail-header-encode-parameter
672                               (symbol-name type) value)))))
673     (when (not (zerop (length string)))
674       string)))
675
676 (defun mml-insert-parameter-string (cont types)
677   (let (value type)
678     (while (setq type (pop types))
679       (when (setq value (cdr (assq type cont)))
680         ;; Strip directory component from the filename parameter.
681         (when (eq type 'filename)
682           (setq value (file-name-nondirectory value)))
683         (mml-insert-parameter
684          (mail-header-encode-parameter
685           (symbol-name type) value))))))
686
687 (eval-when-compile
688   (defvar ange-ftp-name-format)
689   (defvar efs-path-regexp))
690 (defun mml-parse-file-name (path)
691   (if (if (boundp 'efs-path-regexp)
692           (string-match efs-path-regexp path)
693         (if (boundp 'ange-ftp-name-format)
694             (string-match (car ange-ftp-name-format) path)))
695       (list (match-string 1 path) (match-string 2 path)
696             (substring path (1+ (match-end 2))))
697     path))
698
699 (defun mml-insert-buffer (buffer)
700   "Insert BUFFER at point and quote any MML markup."
701   (save-restriction
702     (narrow-to-region (point) (point))
703     (insert-buffer-substring buffer)
704     (mml-quote-region (point-min) (point-max))
705     (goto-char (point-max))))
706
707 ;;;
708 ;;; Transforming MIME to MML
709 ;;;
710
711 (defun mime-to-mml (&optional handles)
712   "Translate the current buffer (which should be a message) into MML.
713 If HANDLES is non-nil, use it instead reparsing the buffer."
714   ;; First decode the head.
715   (save-restriction
716     (message-narrow-to-head)
717     (let ((rfc2047-quote-decoded-words-containing-tspecials t))
718       (mail-decode-encoded-word-region (point-min) (point-max))))
719   (unless handles
720     (setq handles (mm-dissect-buffer t)))
721   (goto-char (point-min))
722   (search-forward "\n\n" nil t)
723   (delete-region (point) (point-max))
724   (if (stringp (car handles))
725       (mml-insert-mime handles)
726     (mml-insert-mime handles t))
727   (mm-destroy-parts handles)
728   (save-restriction
729     (message-narrow-to-head)
730     ;; Remove them, they are confusing.
731     (message-remove-header "Content-Type")
732     (message-remove-header "MIME-Version")
733     (message-remove-header "Content-Disposition")
734     (message-remove-header "Content-Transfer-Encoding")))
735
736 (defun mml-to-mime ()
737   "Translate the current buffer from MML to MIME."
738   (message-encode-message-body)
739   (save-restriction
740     (message-narrow-to-headers-or-head)
741     ;; Skip past any From_ headers.
742     (while (looking-at "From ")
743       (forward-line 1))
744     (let ((mail-parse-charset message-default-charset))
745       (mail-encode-encoded-word-buffer))))
746
747 (defun mml-insert-mime (handle &optional no-markup)
748   (let (textp buffer mmlp)
749     ;; Determine type and stuff.
750     (unless (stringp (car handle))
751       (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
752         (save-excursion
753           (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
754           (mm-insert-part handle)
755           (if (setq mmlp (equal (mm-handle-media-type handle)
756                                 "message/rfc822"))
757               (mime-to-mml)))))
758     (if mmlp
759         (mml-insert-mml-markup handle nil t t)
760       (unless (and no-markup
761                    (equal (mm-handle-media-type handle) "text/plain"))
762         (mml-insert-mml-markup handle buffer textp)))
763     (cond
764      (mmlp
765       (insert-buffer-substring buffer)
766       (goto-char (point-max))
767       (insert "<#/mml>\n"))
768      ((stringp (car handle))
769       (mapcar 'mml-insert-mime (cdr handle))
770       (insert "<#/multipart>\n"))
771      (textp
772       (let ((charset (mail-content-type-get
773                       (mm-handle-type handle) 'charset))
774             (start (point)))
775         (if (eq charset 'gnus-decoded)
776             (mm-insert-part handle)
777           (insert (mm-decode-string (mm-get-part handle) charset)))
778         (mml-quote-region start (point)))
779       (goto-char (point-max)))
780      (t
781       (insert "<#/part>\n")))))
782
783 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
784   "Take a MIME handle and insert an MML tag."
785   (if (stringp (car handle))
786       (progn
787         (insert "<#multipart type=" (mm-handle-media-subtype handle))
788         (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
789           (when start
790             (insert " start=\"" start "\"")))
791         (insert ">\n"))
792     (if mmlp
793         (insert "<#mml type=" (mm-handle-media-type handle))
794       (insert "<#part type=" (mm-handle-media-type handle)))
795     (dolist (elem (append (cdr (mm-handle-type handle))
796                           (cdr (mm-handle-disposition handle))))
797       (unless (symbolp (cdr elem))
798         (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
799     (when (mm-handle-id handle)
800       (insert " id=\"" (mm-handle-id handle) "\""))
801     (when (mm-handle-disposition handle)
802       (insert " disposition=" (car (mm-handle-disposition handle))))
803     (when buffer
804       (insert " buffer=\"" (buffer-name buffer) "\""))
805     (when nofile
806       (insert " nofile=yes"))
807     (when (mm-handle-description handle)
808       (insert " description=\"" (mm-handle-description handle) "\""))
809     (insert ">\n")))
810
811 (defun mml-insert-parameter (&rest parameters)
812   "Insert PARAMETERS in a nice way."
813   (dolist (param parameters)
814     (insert ";")
815     (let ((point (point)))
816       (insert " " param)
817       (when (> (current-column) 71)
818         (goto-char point)
819         (insert "\n")
820         (end-of-line)))))
821
822 ;;;
823 ;;; Mode for inserting and editing MML forms
824 ;;;
825
826 (defvar mml-mode-map
827   (let ((sign (make-sparse-keymap))
828         (encrypt (make-sparse-keymap))
829         (signpart (make-sparse-keymap))
830         (encryptpart (make-sparse-keymap))
831         (map (make-sparse-keymap))
832         (main (make-sparse-keymap)))
833     (define-key sign "p" 'mml-secure-message-sign-pgpmime)
834     (define-key sign "o" 'mml-secure-message-sign-pgp)
835     (define-key sign "s" 'mml-secure-message-sign-smime)
836     (define-key signpart "p" 'mml-secure-sign-pgpmime)
837     (define-key signpart "o" 'mml-secure-sign-pgp)
838     (define-key signpart "s" 'mml-secure-sign-smime)
839     (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
840     (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
841     (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
842     (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
843     (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
844     (define-key encryptpart "s" 'mml-secure-encrypt-smime)
845     (define-key map "\C-n" 'mml-unsecure-message)
846     (define-key map "f" 'mml-attach-file)
847     (define-key map "b" 'mml-attach-buffer)
848     (define-key map "e" 'mml-attach-external)
849     (define-key map "q" 'mml-quote-region)
850     (define-key map "m" 'mml-insert-multipart)
851     (define-key map "p" 'mml-insert-part)
852     (define-key map "v" 'mml-validate)
853     (define-key map "P" 'mml-preview)
854     (define-key map "s" sign)
855     (define-key map "S" signpart)
856     (define-key map "c" encrypt)
857     (define-key map "C" encryptpart)
858     ;;(define-key map "n" 'mml-narrow-to-part)
859     ;; `M-m' conflicts with `back-to-indentation'.
860     ;; (define-key main "\M-m" map)
861     (define-key main "\C-c\C-m" map)
862     main))
863
864 (easy-menu-define
865   mml-menu mml-mode-map ""
866   `("Attachments"
867     ["Attach File..." mml-attach-file
868      ,@(if (featurep 'xemacs) '(t)
869          '(:help "Attach a file at point"))]
870     ["Attach Buffer..." mml-attach-buffer t]
871     ["Attach External..." mml-attach-external t]
872     ["Insert Part..." mml-insert-part t]
873     ["Insert Multipart..." mml-insert-multipart t]
874     ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
875     ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
876     ["PGP Sign" mml-secure-message-sign-pgp t]
877     ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
878     ["S/MIME Sign" mml-secure-message-sign-smime t]
879     ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
880     ("Secure MIME part"
881      ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
882      ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
883      ["PGP Sign Part" mml-secure-sign-pgp t]
884      ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
885      ["S/MIME Sign Part" mml-secure-sign-smime t]
886      ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
887     ["Encrypt/Sign off" mml-unsecure-message t]
888     ;;["Narrow" mml-narrow-to-part t]
889     ["Quote MML" mml-quote-region
890      :active (message-mark-active-p)
891      ,@(if (featurep 'xemacs) nil
892          '(:help "Quote MML tags in region"))]
893     ["Validate MML" mml-validate t]
894     ["Preview" mml-preview t]))
895
896 (defvar mml-mode nil
897   "Minor mode for editing MML.")
898
899 (defun mml-mode (&optional arg)
900   "Minor mode for editing MML.
901 MML is the MIME Meta Language, a minor mode for composing MIME articles.
902 See Info node `(emacs-mime)Composing'.
903
904 \\{mml-mode-map}"
905   (interactive "P")
906   (when (set (make-local-variable 'mml-mode)
907              (if (null arg) (not mml-mode)
908                (> (prefix-numeric-value arg) 0)))
909     (add-minor-mode 'mml-mode " MML" mml-mode-map)
910     (easy-menu-add mml-menu mml-mode-map)
911     (when (boundp 'x-dnd-protocol-alist)
912       (set (make-local-variable 'x-dnd-protocol-alist)
913            '(("^file:///" . mml-x-dnd-attach-file)
914              ("^file://"  . x-dnd-open-file)
915              ("^file:"    . mml-x-dnd-attach-file))))
916     (run-hooks 'mml-mode-hook)))
917
918 ;;;
919 ;;; Helper functions for reading MIME stuff from the minibuffer and
920 ;;; inserting stuff to the buffer.
921 ;;;
922
923 (defun mml-minibuffer-read-file (prompt)
924   (let* ((completion-ignored-extensions nil)
925          (file (read-file-name prompt nil nil t)))
926     ;; Prevent some common errors.  This is inspired by similar code in
927     ;; VM.
928     (when (file-directory-p file)
929       (error "%s is a directory, cannot attach" file))
930     (unless (file-exists-p file)
931       (error "No such file: %s" file))
932     (unless (file-readable-p file)
933       (error "Permission denied: %s" file))
934     file))
935
936 (defun mml-minibuffer-read-type (name &optional default)
937   (mailcap-parse-mimetypes)
938   (let* ((default (or default
939                       (mm-default-file-encoding name)
940                       ;; Perhaps here we should check what the file
941                       ;; looks like, and offer text/plain if it looks
942                       ;; like text/plain.
943                       "application/octet-stream"))
944          (string (completing-read
945                   (format "Content type (default %s): " default)
946                   (mapcar 'list (mailcap-mime-types)))))
947     (if (not (equal string ""))
948         string
949       default)))
950
951 (defun mml-minibuffer-read-description ()
952   (let ((description (read-string "One line description: ")))
953     (when (string-match "\\`[ \t]*\\'" description)
954       (setq description nil))
955     description))
956
957 (defun mml-minibuffer-read-disposition (type &optional default)
958   (let* ((default (or default
959                       (if (string-match "^text/.*" type)
960                           "inline"
961                         "attachment")))
962          (disposition (completing-read "Disposition: "
963                                        '(("attachment") ("inline") (""))
964                                        nil t)))
965     (if (not (equal disposition ""))
966         disposition
967       default)))
968
969 (defun mml-quote-region (beg end)
970   "Quote the MML tags in the region."
971   (interactive "r")
972   (save-excursion
973     (save-restriction
974       ;; Temporarily narrow the region to defend from changes
975       ;; invalidating END.
976       (narrow-to-region beg end)
977       (goto-char (point-min))
978       ;; Quote parts.
979       (while (re-search-forward
980               "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
981         ;; Insert ! after the #.
982         (goto-char (+ (match-beginning 0) 2))
983         (insert "!")))))
984
985 (defun mml-insert-tag (name &rest plist)
986   "Insert an MML tag described by NAME and PLIST."
987   (when (symbolp name)
988     (setq name (symbol-name name)))
989   (insert "<#" name)
990   (while plist
991     (let ((key (pop plist))
992           (value (pop plist)))
993       (when value
994         ;; Quote VALUE if it contains suspicious characters.
995         (when (string-match "[\"'\\~/*;() \t\n]" value)
996           (setq value (with-output-to-string
997                         (let (print-escape-nonascii)
998                           (prin1 value)))))
999         (insert (format " %s=%s" key value)))))
1000   (insert ">\n"))
1001
1002 (defun mml-insert-empty-tag (name &rest plist)
1003   "Insert an empty MML tag described by NAME and PLIST."
1004   (when (symbolp name)
1005     (setq name (symbol-name name)))
1006   (apply #'mml-insert-tag name plist)
1007   (insert "<#/" name ">\n"))
1008
1009 ;;; Attachment functions.
1010
1011 (defun mml-attach-file (file &optional type description disposition)
1012   "Attach a file to the outgoing MIME message.
1013 The file is not inserted or encoded until you send the message with
1014 `\\[message-send-and-exit]' or `\\[message-send]'.
1015
1016 FILE is the name of the file to attach.  TYPE is its content-type, a
1017 string of the form \"type/subtype\".  DESCRIPTION is a one-line
1018 description of the attachment."
1019   (interactive
1020    (let* ((file (mml-minibuffer-read-file "Attach file: "))
1021           (type (mml-minibuffer-read-type file))
1022           (description (mml-minibuffer-read-description))
1023           (disposition (mml-minibuffer-read-disposition type)))
1024      (list file type description disposition)))
1025   (mml-insert-empty-tag 'part
1026                         'type type
1027                         'filename file
1028                         'disposition (or disposition "attachment")
1029                         'description description))
1030
1031 (defun mml-x-dnd-attach-file (uri action)
1032   "Attach a drag and drop file."
1033   (let ((file (x-dnd-get-local-file-name uri t)))
1034     (when (and file (file-regular-p file))
1035       (let* ((type (mml-minibuffer-read-type file))
1036             (description (mml-minibuffer-read-description))
1037             (disposition (mml-minibuffer-read-disposition type)))
1038         (mml-attach-file file type description disposition)))))
1039
1040 (defun mml-attach-buffer (buffer &optional type description)
1041   "Attach a buffer to the outgoing MIME message.
1042 See `mml-attach-file' for details of operation."
1043   (interactive
1044    (let* ((buffer (read-buffer "Attach buffer: "))
1045           (type (mml-minibuffer-read-type buffer "text/plain"))
1046           (description (mml-minibuffer-read-description)))
1047      (list buffer type description)))
1048   (mml-insert-empty-tag 'part 'type type 'buffer buffer
1049                         'disposition "attachment" 'description description))
1050
1051 (defun mml-attach-external (file &optional type description)
1052   "Attach an external file into the buffer.
1053 FILE is an ange-ftp/efs specification of the part location.
1054 TYPE is the MIME type to use."
1055   (interactive
1056    (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1057           (type (mml-minibuffer-read-type file))
1058           (description (mml-minibuffer-read-description)))
1059      (list file type description)))
1060   (mml-insert-empty-tag 'external 'type type 'name file
1061                         'disposition "attachment" 'description description))
1062
1063 (defun mml-insert-multipart (&optional type)
1064   (interactive (list (completing-read "Multipart type (default mixed): "
1065                                       '(("mixed") ("alternative") ("digest") ("parallel")
1066                                         ("signed") ("encrypted"))
1067                                       nil nil "mixed")))
1068   (or type
1069       (setq type "mixed"))
1070   (mml-insert-empty-tag "multipart" 'type type)
1071   (forward-line -1))
1072
1073 (defun mml-insert-part (&optional type)
1074   (interactive
1075    (list (mml-minibuffer-read-type "")))
1076   (mml-insert-tag 'part 'type type 'disposition "inline")
1077   (forward-line -1))
1078
1079 (defun mml-preview-insert-mail-followup-to ()
1080   "Insert a Mail-Followup-To header before previewing an article.
1081 Should be adopted if code in `message-send-mail' is changed."
1082   (when (and (message-mail-p)
1083              (message-subscribed-p)
1084              (not (mail-fetch-field "mail-followup-to"))
1085              (message-make-mail-followup-to))
1086     (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1087     (insert (message-make-mail-followup-to))))
1088
1089 (defvar mml-preview-buffer nil)
1090
1091 (defun mml-preview (&optional raw)
1092   "Display current buffer with Gnus, in a new buffer.
1093 If RAW, don't highlight the article."
1094   (interactive "P")
1095   (setq mml-preview-buffer (generate-new-buffer
1096                             (concat (if raw "*Raw MIME preview of "
1097                                       "*MIME preview of ") (buffer-name))))
1098   (save-excursion
1099     (let* ((buf (current-buffer))
1100            (message-options message-options)
1101            (message-this-is-mail (message-mail-p))
1102            (message-this-is-news (message-news-p))
1103            (message-posting-charset (or (gnus-setup-posting-charset
1104                                          (save-restriction
1105                                            (message-narrow-to-headers-or-head)
1106                                            (message-fetch-field "Newsgroups")))
1107                                         message-posting-charset)))
1108       (message-options-set-recipient)
1109       (when (boundp 'gnus-buffers)
1110         (push mml-preview-buffer gnus-buffers))
1111       (save-restriction
1112         (widen)
1113         (set-buffer mml-preview-buffer)
1114         (erase-buffer)
1115         (insert-buffer-substring buf))
1116       (mml-preview-insert-mail-followup-to)
1117       (let ((message-deletable-headers (if (message-news-p)
1118                                            nil
1119                                          message-deletable-headers)))
1120         (message-generate-headers
1121          (copy-sequence (if (message-news-p)
1122                             message-required-news-headers
1123                           message-required-mail-headers))))
1124       (if (re-search-forward
1125            (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1126           (replace-match "\n"))
1127       (let ((mail-header-separator ""));; mail-header-separator is removed.
1128         (message-sort-headers)
1129         (mml-to-mime))
1130       (if raw
1131           (when (fboundp 'set-buffer-multibyte)
1132             (let ((s (buffer-string)))
1133               ;; Insert the content into unibyte buffer.
1134               (erase-buffer)
1135               (mm-disable-multibyte)
1136               (insert s)))
1137         (let ((gnus-newsgroup-charset (car message-posting-charset))
1138               gnus-article-prepare-hook gnus-original-article-buffer)
1139           (run-hooks 'gnus-article-decode-hook)
1140           (let ((gnus-newsgroup-name "dummy")
1141                 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1142                                         (gnus-make-hashtable 5))))
1143             (gnus-article-prepare-display))))
1144       ;; Disable article-mode-map.
1145       (use-local-map nil)
1146       (gnus-make-local-hook 'kill-buffer-hook)
1147       (add-hook 'kill-buffer-hook
1148                 (lambda ()
1149                   (mm-destroy-parts gnus-article-mime-handles)) nil t)
1150       (setq buffer-read-only t)
1151       (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1152       (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1153       (local-set-key "\r"
1154                      (lambda ()
1155                        (interactive)
1156                        (widget-button-press (point))))
1157       (local-set-key gnus-mouse-2
1158                      (lambda (event)
1159                        (interactive "@e")
1160                        (widget-button-press (widget-event-point event) event)))
1161       (goto-char (point-min))))
1162   (if (and (boundp 'gnus-buffer-configuration)
1163            (assq 'mml-preview gnus-buffer-configuration))
1164       (gnus-configure-windows 'mml-preview)
1165     (pop-to-buffer mml-preview-buffer)))
1166
1167 (defun mml-validate ()
1168   "Validate the current MML document."
1169   (interactive)
1170   (mml-parse))
1171
1172 (defun mml-tweak-part (cont)
1173   "Tweak a MML part."
1174   (let ((tweak (cdr (assq 'tweak cont)))
1175         func)
1176     (cond
1177      (tweak
1178       (setq func
1179             (or (cdr (assoc tweak mml-tweak-function-alist))
1180                 (intern tweak))))
1181      (mml-tweak-type-alist
1182       (let ((alist mml-tweak-type-alist)
1183             (type (or (cdr (assq 'type cont)) "text/plain")))
1184         (while alist
1185           (if (string-match (caar alist) type)
1186               (setq func (cdar alist)
1187                     alist nil)
1188             (setq alist (cdr alist)))))))
1189     (if func
1190         (funcall func cont)
1191       cont)
1192     (let ((alist mml-tweak-sexp-alist))
1193       (while alist
1194         (if (eval (caar alist))
1195             (funcall (cdar alist) cont))
1196         (setq alist (cdr alist)))))
1197   cont)
1198
1199 (defun mml-tweak-externalize-attachments (cont)
1200   "Tweak attached files as external parts."
1201   (let (filename-cons)
1202     (when (and (eq (car cont) 'part)
1203                (not (cdr (assq 'buffer cont)))
1204                (and (setq filename-cons (assq 'filename cont))
1205                     (not (equal (cdr (assq 'nofile cont)) "yes"))))
1206       (setcar cont 'external)
1207       (setcar filename-cons 'name))))
1208
1209 (provide 'mml)
1210
1211 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1212 ;;; mml.el ends here