* gnus-art.el (gnus-mime-inline-part): Decode parts according to the coding
[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 "21.4"
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 "21.4"
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 "21.4"
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     (mail-decode-encoded-word-region (point-min) (point-max)))
718   (unless handles
719     (setq handles (mm-dissect-buffer t)))
720   (goto-char (point-min))
721   (search-forward "\n\n" nil t)
722   (delete-region (point) (point-max))
723   (if (stringp (car handles))
724       (mml-insert-mime handles)
725     (mml-insert-mime handles t))
726   (mm-destroy-parts handles)
727   (save-restriction
728     (message-narrow-to-head)
729     ;; Remove them, they are confusing.
730     (message-remove-header "Content-Type")
731     (message-remove-header "MIME-Version")
732     (message-remove-header "Content-Disposition")
733     (message-remove-header "Content-Transfer-Encoding")))
734
735 (defun mml-to-mime ()
736   "Translate the current buffer from MML to MIME."
737   (message-encode-message-body)
738   (save-restriction
739     (message-narrow-to-headers-or-head)
740     ;; Skip past any From_ headers.
741     (while (looking-at "From ")
742       (forward-line 1))
743     (let ((mail-parse-charset message-default-charset))
744       (mail-encode-encoded-word-buffer))))
745
746 (defun mml-insert-mime (handle &optional no-markup)
747   (let (textp buffer mmlp)
748     ;; Determine type and stuff.
749     (unless (stringp (car handle))
750       (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
751         (save-excursion
752           (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
753           (mm-insert-part handle)
754           (if (setq mmlp (equal (mm-handle-media-type handle)
755                                 "message/rfc822"))
756               (mime-to-mml)))))
757     (if mmlp
758         (mml-insert-mml-markup handle nil t t)
759       (unless (and no-markup
760                    (equal (mm-handle-media-type handle) "text/plain"))
761         (mml-insert-mml-markup handle buffer textp)))
762     (cond
763      (mmlp
764       (insert-buffer-substring buffer)
765       (goto-char (point-max))
766       (insert "<#/mml>\n"))
767      ((stringp (car handle))
768       (mapcar 'mml-insert-mime (cdr handle))
769       (insert "<#/multipart>\n"))
770      (textp
771       (let ((charset (mail-content-type-get
772                       (mm-handle-type handle) 'charset))
773             (start (point)))
774         (if (eq charset 'gnus-decoded)
775             (mm-insert-part handle)
776           (insert (mm-decode-string (mm-get-part handle) charset)))
777         (mml-quote-region start (point)))
778       (goto-char (point-max)))
779      (t
780       (insert "<#/part>\n")))))
781
782 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
783   "Take a MIME handle and insert an MML tag."
784   (if (stringp (car handle))
785       (progn
786         (insert "<#multipart type=" (mm-handle-media-subtype handle))
787         (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
788           (when start
789             (insert " start=\"" start "\"")))
790         (insert ">\n"))
791     (if mmlp
792         (insert "<#mml type=" (mm-handle-media-type handle))
793       (insert "<#part type=" (mm-handle-media-type handle)))
794     (dolist (elem (append (cdr (mm-handle-type handle))
795                           (cdr (mm-handle-disposition handle))))
796       (unless (symbolp (cdr elem))
797         (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
798     (when (mm-handle-id handle)
799       (insert " id=\"" (mm-handle-id handle) "\""))
800     (when (mm-handle-disposition handle)
801       (insert " disposition=" (car (mm-handle-disposition handle))))
802     (when buffer
803       (insert " buffer=\"" (buffer-name buffer) "\""))
804     (when nofile
805       (insert " nofile=yes"))
806     (when (mm-handle-description handle)
807       (insert " description=\"" (mm-handle-description handle) "\""))
808     (insert ">\n")))
809
810 (defun mml-insert-parameter (&rest parameters)
811   "Insert PARAMETERS in a nice way."
812   (dolist (param parameters)
813     (insert ";")
814     (let ((point (point)))
815       (insert " " param)
816       (when (> (current-column) 71)
817         (goto-char point)
818         (insert "\n")
819         (end-of-line)))))
820
821 ;;;
822 ;;; Mode for inserting and editing MML forms
823 ;;;
824
825 (defvar mml-mode-map
826   (let ((sign (make-sparse-keymap))
827         (encrypt (make-sparse-keymap))
828         (signpart (make-sparse-keymap))
829         (encryptpart (make-sparse-keymap))
830         (map (make-sparse-keymap))
831         (main (make-sparse-keymap)))
832     (define-key sign "p" 'mml-secure-message-sign-pgpmime)
833     (define-key sign "o" 'mml-secure-message-sign-pgp)
834     (define-key sign "s" 'mml-secure-message-sign-smime)
835     (define-key signpart "p" 'mml-secure-sign-pgpmime)
836     (define-key signpart "o" 'mml-secure-sign-pgp)
837     (define-key signpart "s" 'mml-secure-sign-smime)
838     (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
839     (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
840     (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
841     (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
842     (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
843     (define-key encryptpart "s" 'mml-secure-encrypt-smime)
844     (define-key map "\C-n" 'mml-unsecure-message)
845     (define-key map "f" 'mml-attach-file)
846     (define-key map "b" 'mml-attach-buffer)
847     (define-key map "e" 'mml-attach-external)
848     (define-key map "q" 'mml-quote-region)
849     (define-key map "m" 'mml-insert-multipart)
850     (define-key map "p" 'mml-insert-part)
851     (define-key map "v" 'mml-validate)
852     (define-key map "P" 'mml-preview)
853     (define-key map "s" sign)
854     (define-key map "S" signpart)
855     (define-key map "c" encrypt)
856     (define-key map "C" encryptpart)
857     ;;(define-key map "n" 'mml-narrow-to-part)
858     ;; `M-m' conflicts with `back-to-indentation'.
859     ;; (define-key main "\M-m" map)
860     (define-key main "\C-c\C-m" map)
861     main))
862
863 (easy-menu-define
864   mml-menu mml-mode-map ""
865   `("Attachments"
866     ["Attach File..." mml-attach-file
867      ,@(if (featurep 'xemacs) '(t)
868          '(:help "Attach a file at point"))]
869     ["Attach Buffer..." mml-attach-buffer t]
870     ["Attach External..." mml-attach-external t]
871     ["Insert Part..." mml-insert-part t]
872     ["Insert Multipart..." mml-insert-multipart t]
873     ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
874     ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
875     ["PGP Sign" mml-secure-message-sign-pgp t]
876     ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
877     ["S/MIME Sign" mml-secure-message-sign-smime t]
878     ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
879     ("Secure MIME part"
880      ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
881      ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
882      ["PGP Sign Part" mml-secure-sign-pgp t]
883      ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
884      ["S/MIME Sign Part" mml-secure-sign-smime t]
885      ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
886     ["Encrypt/Sign off" mml-unsecure-message t]
887     ;;["Narrow" mml-narrow-to-part t]
888     ["Quote MML" mml-quote-region
889      :active (message-mark-active-p)
890      ,@(if (featurep 'xemacs) nil
891          '(:help "Quote MML tags in region"))]
892     ["Validate MML" mml-validate t]
893     ["Preview" mml-preview t]))
894
895 (defvar mml-mode nil
896   "Minor mode for editing MML.")
897
898 (defun mml-mode (&optional arg)
899   "Minor mode for editing MML.
900 MML is the MIME Meta Language, a minor mode for composing MIME articles.
901 See Info node `(emacs-mime)Composing'.
902
903 \\{mml-mode-map}"
904   (interactive "P")
905   (when (set (make-local-variable 'mml-mode)
906              (if (null arg) (not mml-mode)
907                (> (prefix-numeric-value arg) 0)))
908     (add-minor-mode 'mml-mode " MML" mml-mode-map)
909     (easy-menu-add mml-menu mml-mode-map)
910     (when (boundp 'x-dnd-protocol-alist)
911       (set (make-local-variable 'x-dnd-protocol-alist)
912            '(("^file:///" . mml-x-dnd-attach-file)
913              ("^file://"  . x-dnd-open-file)
914              ("^file:"    . mml-x-dnd-attach-file))))
915     (run-hooks 'mml-mode-hook)))
916
917 ;;;
918 ;;; Helper functions for reading MIME stuff from the minibuffer and
919 ;;; inserting stuff to the buffer.
920 ;;;
921
922 (defun mml-minibuffer-read-file (prompt)
923   (let* ((completion-ignored-extensions nil)
924          (file (read-file-name prompt nil nil t)))
925     ;; Prevent some common errors.  This is inspired by similar code in
926     ;; VM.
927     (when (file-directory-p file)
928       (error "%s is a directory, cannot attach" file))
929     (unless (file-exists-p file)
930       (error "No such file: %s" file))
931     (unless (file-readable-p file)
932       (error "Permission denied: %s" file))
933     file))
934
935 (defun mml-minibuffer-read-type (name &optional default)
936   (mailcap-parse-mimetypes)
937   (let* ((default (or default
938                       (mm-default-file-encoding name)
939                       ;; Perhaps here we should check what the file
940                       ;; looks like, and offer text/plain if it looks
941                       ;; like text/plain.
942                       "application/octet-stream"))
943          (string (completing-read
944                   (format "Content type (default %s): " default)
945                   (mapcar 'list (mailcap-mime-types)))))
946     (if (not (equal string ""))
947         string
948       default)))
949
950 (defun mml-minibuffer-read-description ()
951   (let ((description (read-string "One line description: ")))
952     (when (string-match "\\`[ \t]*\\'" description)
953       (setq description nil))
954     description))
955
956 (defun mml-minibuffer-read-disposition (type &optional default)
957   (let* ((default (or default
958                       (if (string-match "^text/.*" type)
959                           "inline"
960                         "attachment")))
961          (disposition (completing-read "Disposition: "
962                                        '(("attachment") ("inline") (""))
963                                        nil t)))
964     (if (not (equal disposition ""))
965         disposition
966       default)))
967
968 (defun mml-quote-region (beg end)
969   "Quote the MML tags in the region."
970   (interactive "r")
971   (save-excursion
972     (save-restriction
973       ;; Temporarily narrow the region to defend from changes
974       ;; invalidating END.
975       (narrow-to-region beg end)
976       (goto-char (point-min))
977       ;; Quote parts.
978       (while (re-search-forward
979               "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
980         ;; Insert ! after the #.
981         (goto-char (+ (match-beginning 0) 2))
982         (insert "!")))))
983
984 (defun mml-insert-tag (name &rest plist)
985   "Insert an MML tag described by NAME and PLIST."
986   (when (symbolp name)
987     (setq name (symbol-name name)))
988   (insert "<#" name)
989   (while plist
990     (let ((key (pop plist))
991           (value (pop plist)))
992       (when value
993         ;; Quote VALUE if it contains suspicious characters.
994         (when (string-match "[\"'\\~/*;() \t\n]" value)
995           (setq value (with-output-to-string
996                         (let (print-escape-nonascii)
997                           (prin1 value)))))
998         (insert (format " %s=%s" key value)))))
999   (insert ">\n"))
1000
1001 (defun mml-insert-empty-tag (name &rest plist)
1002   "Insert an empty MML tag described by NAME and PLIST."
1003   (when (symbolp name)
1004     (setq name (symbol-name name)))
1005   (apply #'mml-insert-tag name plist)
1006   (insert "<#/" name ">\n"))
1007
1008 ;;; Attachment functions.
1009
1010 (defun mml-attach-file (file &optional type description disposition)
1011   "Attach a file to the outgoing MIME message.
1012 The file is not inserted or encoded until you send the message with
1013 `\\[message-send-and-exit]' or `\\[message-send]'.
1014
1015 FILE is the name of the file to attach.  TYPE is its content-type, a
1016 string of the form \"type/subtype\".  DESCRIPTION is a one-line
1017 description of the attachment."
1018   (interactive
1019    (let* ((file (mml-minibuffer-read-file "Attach file: "))
1020           (type (mml-minibuffer-read-type file))
1021           (description (mml-minibuffer-read-description))
1022           (disposition (mml-minibuffer-read-disposition type)))
1023      (list file type description disposition)))
1024   (mml-insert-empty-tag 'part
1025                         'type type
1026                         'filename file
1027                         'disposition (or disposition "attachment")
1028                         'description description))
1029
1030 (defun mml-x-dnd-attach-file (uri action)
1031   "Attach a drag and drop file."
1032   (let ((file (x-dnd-get-local-file-name uri t)))
1033     (when (and file (file-regular-p file))
1034       (let* ((type (mml-minibuffer-read-type file))
1035             (description (mml-minibuffer-read-description))
1036             (disposition (mml-minibuffer-read-disposition type)))
1037         (mml-attach-file file type description disposition)))))
1038
1039 (defun mml-attach-buffer (buffer &optional type description)
1040   "Attach a buffer to the outgoing MIME message.
1041 See `mml-attach-file' for details of operation."
1042   (interactive
1043    (let* ((buffer (read-buffer "Attach buffer: "))
1044           (type (mml-minibuffer-read-type buffer "text/plain"))
1045           (description (mml-minibuffer-read-description)))
1046      (list buffer type description)))
1047   (mml-insert-empty-tag 'part 'type type 'buffer buffer
1048                         'disposition "attachment" 'description description))
1049
1050 (defun mml-attach-external (file &optional type description)
1051   "Attach an external file into the buffer.
1052 FILE is an ange-ftp/efs specification of the part location.
1053 TYPE is the MIME type to use."
1054   (interactive
1055    (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1056           (type (mml-minibuffer-read-type file))
1057           (description (mml-minibuffer-read-description)))
1058      (list file type description)))
1059   (mml-insert-empty-tag 'external 'type type 'name file
1060                         'disposition "attachment" 'description description))
1061
1062 (defun mml-insert-multipart (&optional type)
1063   (interactive (list (completing-read "Multipart type (default mixed): "
1064                                       '(("mixed") ("alternative") ("digest") ("parallel")
1065                                         ("signed") ("encrypted"))
1066                                       nil nil "mixed")))
1067   (or type
1068       (setq type "mixed"))
1069   (mml-insert-empty-tag "multipart" 'type type)
1070   (forward-line -1))
1071
1072 (defun mml-insert-part (&optional type)
1073   (interactive
1074    (list (mml-minibuffer-read-type "")))
1075   (mml-insert-tag 'part 'type type 'disposition "inline")
1076   (forward-line -1))
1077
1078 (defun mml-preview-insert-mail-followup-to ()
1079   "Insert a Mail-Followup-To header before previewing an article.
1080 Should be adopted if code in `message-send-mail' is changed."
1081   (when (and (message-mail-p)
1082              (message-subscribed-p)
1083              (not (mail-fetch-field "mail-followup-to"))
1084              (message-make-mail-followup-to))
1085     (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1086     (insert (message-make-mail-followup-to))))
1087
1088 (defvar mml-preview-buffer nil)
1089
1090 (defun mml-preview (&optional raw)
1091   "Display current buffer with Gnus, in a new buffer.
1092 If RAW, don't highlight the article."
1093   (interactive "P")
1094   (setq mml-preview-buffer (generate-new-buffer
1095                             (concat (if raw "*Raw MIME preview of "
1096                                       "*MIME preview of ") (buffer-name))))
1097   (save-excursion
1098     (let* ((buf (current-buffer))
1099            (message-options message-options)
1100            (message-this-is-mail (message-mail-p))
1101            (message-this-is-news (message-news-p))
1102            (message-posting-charset (or (gnus-setup-posting-charset
1103                                          (save-restriction
1104                                            (message-narrow-to-headers-or-head)
1105                                            (message-fetch-field "Newsgroups")))
1106                                         message-posting-charset)))
1107       (message-options-set-recipient)
1108       (when (boundp 'gnus-buffers)
1109         (push mml-preview-buffer gnus-buffers))
1110       (save-restriction
1111         (widen)
1112         (set-buffer mml-preview-buffer)
1113         (erase-buffer)
1114         (insert-buffer-substring buf))
1115       (mml-preview-insert-mail-followup-to)
1116       (let ((message-deletable-headers (if (message-news-p)
1117                                            nil
1118                                          message-deletable-headers)))
1119         (message-generate-headers
1120          (copy-sequence (if (message-news-p)
1121                             message-required-news-headers
1122                           message-required-mail-headers))))
1123       (if (re-search-forward
1124            (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1125           (replace-match "\n"))
1126       (let ((mail-header-separator ""));; mail-header-separator is removed.
1127         (message-sort-headers)
1128         (mml-to-mime))
1129       (if raw
1130           (when (fboundp 'set-buffer-multibyte)
1131             (let ((s (buffer-string)))
1132               ;; Insert the content into unibyte buffer.
1133               (erase-buffer)
1134               (mm-disable-multibyte)
1135               (insert s)))
1136         (let ((gnus-newsgroup-charset (car message-posting-charset))
1137               gnus-article-prepare-hook gnus-original-article-buffer)
1138           (run-hooks 'gnus-article-decode-hook)
1139           (let ((gnus-newsgroup-name "dummy")
1140                 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1141                                         (gnus-make-hashtable 5))))
1142             (gnus-article-prepare-display))))
1143       ;; Disable article-mode-map.
1144       (use-local-map nil)
1145       (gnus-make-local-hook 'kill-buffer-hook)
1146       (add-hook 'kill-buffer-hook
1147                 (lambda ()
1148                   (mm-destroy-parts gnus-article-mime-handles)) nil t)
1149       (setq buffer-read-only t)
1150       (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1151       (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1152       (local-set-key "\r"
1153                      (lambda ()
1154                        (interactive)
1155                        (widget-button-press (point))))
1156       (local-set-key gnus-mouse-2
1157                      (lambda (event)
1158                        (interactive "@e")
1159                        (widget-button-press (widget-event-point event) event)))
1160       (goto-char (point-min))))
1161   (if (and (boundp 'gnus-buffer-configuration)
1162            (assq 'mml-preview gnus-buffer-configuration))
1163       (gnus-configure-windows 'mml-preview)
1164     (pop-to-buffer mml-preview-buffer)))
1165
1166 (defun mml-validate ()
1167   "Validate the current MML document."
1168   (interactive)
1169   (mml-parse))
1170
1171 (defun mml-tweak-part (cont)
1172   "Tweak a MML part."
1173   (let ((tweak (cdr (assq 'tweak cont)))
1174         func)
1175     (cond
1176      (tweak
1177       (setq func
1178             (or (cdr (assoc tweak mml-tweak-function-alist))
1179                 (intern tweak))))
1180      (mml-tweak-type-alist
1181       (let ((alist mml-tweak-type-alist)
1182             (type (or (cdr (assq 'type cont)) "text/plain")))
1183         (while alist
1184           (if (string-match (caar alist) type)
1185               (setq func (cdar alist)
1186                     alist nil)
1187             (setq alist (cdr alist)))))))
1188     (if func
1189         (funcall func cont)
1190       cont)
1191     (let ((alist mml-tweak-sexp-alist))
1192       (while alist
1193         (if (eval (caar alist))
1194             (funcall (cdar alist) cont))
1195         (setq alist (cdr alist)))))
1196   cont)
1197
1198 (defun mml-tweak-externalize-attachments (cont)
1199   "Tweak attached files as external parts."
1200   (let (filename-cons)
1201     (when (and (eq (car cont) 'part)
1202                (not (cdr (assq 'buffer cont)))
1203                (and (setq filename-cons (assq 'filename cont))
1204                     (not (equal (cdr (assq 'nofile cont)) "yes"))))
1205       (setcar cont 'external)
1206       (setcar filename-cons 'name))))
1207
1208 (provide 'mml)
1209
1210 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1211 ;;; mml.el ends here