4a97fe62a1551770e76b3f4a219f1ee79c118187
[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
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               coded encoding charset filename type flowed)
401           (setq type (or (cdr (assq 'type cont)) "text/plain"))
402           (if (and (not raw)
403                    (member (car (split-string type "/")) '("text" "message")))
404               (progn
405                 (with-temp-buffer
406                   (setq charset (mm-charset-to-coding-system
407                                  (cdr (assq 'charset cont))))
408                   (when (eq charset 'ascii)
409                     (setq charset nil))
410                   (cond
411                    ((cdr (assq 'buffer cont))
412                     (insert-buffer-substring (cdr (assq 'buffer cont))))
413                    ((and (setq filename (cdr (assq 'filename cont)))
414                          (not (equal (cdr (assq 'nofile cont)) "yes")))
415                     (let ((coding-system-for-read charset))
416                       (mm-insert-file-contents filename)))
417                    ((eq 'mml (car cont))
418                     (insert (cdr (assq 'contents cont))))
419                    (t
420                     (save-restriction
421                       (narrow-to-region (point) (point))
422                       (insert (cdr (assq 'contents cont)))
423                       ;; Remove quotes from quoted tags.
424                       (goto-char (point-min))
425                       (while (re-search-forward
426                               "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
427                               nil t)
428                         (delete-region (+ (match-beginning 0) 2)
429                                        (+ (match-beginning 0) 3))))))
430                   (cond
431                    ((eq (car cont) 'mml)
432                     (let ((mml-boundary (mml-compute-boundary cont))
433                           (mml-generate-default-type "text/plain"))
434                       (mml-to-mime))
435                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
436                       ;; ignore 0x1b, it is part of iso-2022-jp
437                       (setq encoding (mm-body-7-or-8))))
438                    ((string= (car (split-string type "/")) "message")
439                     (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
440                       ;; ignore 0x1b, it is part of iso-2022-jp
441                       (setq encoding (mm-body-7-or-8))))
442                    (t
443                     ;; Only perform format=flowed filling on text/plain
444                     ;; parts where there either isn't a format parameter
445                     ;; in the mml tag or it says "flowed" and there
446                     ;; actually are hard newlines in the text.
447                     (let (use-hard-newlines)
448                       (when (and (string= type "text/plain")
449                                  (not (string= (cdr (assq 'sign cont)) "pgp"))
450                                  (or (null (assq 'format cont))
451                                      (string= (cdr (assq 'format cont))
452                                               "flowed"))
453                                  (setq use-hard-newlines
454                                        (text-property-any
455                                         (point-min) (point-max) 'hard 't)))
456                         (fill-flowed-encode)
457                         ;; Indicate that `mml-insert-mime-headers' should
458                         ;; insert a "; format=flowed" string unless the
459                         ;; user has already specified it.
460                         (setq flowed (null (assq 'format cont)))))
461                     (setq charset (mm-encode-body charset))
462                     (setq encoding (mm-body-encoding
463                                     charset (cdr (assq 'encoding cont))))))
464                   (setq coded (buffer-string)))
465                 (mml-insert-mime-headers cont type charset encoding flowed)
466                 (insert "\n")
467                 (insert coded))
468             (mm-with-unibyte-buffer
469               (cond
470                ((cdr (assq 'buffer cont))
471                 (insert-buffer-substring (cdr (assq 'buffer cont))))
472                ((and (setq filename (cdr (assq 'filename cont)))
473                      (not (equal (cdr (assq 'nofile cont)) "yes")))
474                 (let ((coding-system-for-read mm-binary-coding-system))
475                   (mm-insert-file-contents filename nil nil nil nil t)))
476                (t
477                 (insert (cdr (assq 'contents cont)))))
478               (setq encoding (mm-encode-buffer type)
479                     coded (mm-string-as-multibyte (buffer-string))))
480             (mml-insert-mime-headers cont type charset encoding nil)
481             (insert "\n")
482             (mm-with-unibyte-current-buffer
483               (insert coded)))))
484        ((eq (car cont) 'external)
485         (insert "Content-Type: message/external-body")
486         (let ((parameters (mml-parameter-string
487                            cont '(expiration size permission)))
488               (name (cdr (assq 'name cont)))
489               (url (cdr (assq 'url cont))))
490           (when name
491             (setq name (mml-parse-file-name name))
492             (if (stringp name)
493                 (mml-insert-parameter
494                  (mail-header-encode-parameter "name" name)
495                  "access-type=local-file")
496               (mml-insert-parameter
497                (mail-header-encode-parameter
498                 "name" (file-name-nondirectory (nth 2 name)))
499                (mail-header-encode-parameter "site" (nth 1 name))
500                (mail-header-encode-parameter
501                 "directory" (file-name-directory (nth 2 name))))
502               (mml-insert-parameter
503                (concat "access-type="
504                        (if (member (nth 0 name) '("ftp@" "anonymous@"))
505                            "anon-ftp"
506                          "ftp")))))
507           (when url
508             (mml-insert-parameter
509              (mail-header-encode-parameter "url" url)
510              "access-type=url"))
511           (when parameters
512             (mml-insert-parameter-string
513              cont '(expiration size permission))))
514         (insert "\n\n")
515         (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
516         (insert "Content-ID: " (message-make-message-id) "\n")
517         (insert "Content-Transfer-Encoding: "
518                 (or (cdr (assq 'encoding cont)) "binary"))
519         (insert "\n\n")
520         (insert (or (cdr (assq 'contents cont))))
521         (insert "\n"))
522        ((eq (car cont) 'multipart)
523         (let* ((type (or (cdr (assq 'type cont)) "mixed"))
524                (mml-generate-default-type (if (equal type "digest")
525                                               "message/rfc822"
526                                             "text/plain"))
527                (handler (assoc type mml-generate-multipart-alist)))
528           (if handler
529               (funcall (cdr handler) cont)
530             ;; No specific handler.  Use default one.
531             (let ((mml-boundary (mml-compute-boundary cont)))
532               (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
533                               type mml-boundary)
534                       (if (cdr (assq 'start cont))
535                           (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
536                         "\n"))
537               (let ((cont cont) part)
538                 (while (setq part (pop cont))
539                   ;; Skip `multipart' and attributes.
540                   (when (and (consp part) (consp (cdr part)))
541                     (insert "\n--" mml-boundary "\n")
542                     (mml-generate-mime-1 part))))
543               (insert "\n--" mml-boundary "--\n")))))
544        (t
545         (error "Invalid element: %S" cont)))
546       ;; handle sign & encrypt tags in a semi-smart way.
547       (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
548             (encrypt-item (assoc (cdr (assq 'encrypt cont))
549                                  mml-encrypt-alist))
550             sender recipients)
551         (when (or sign-item encrypt-item)
552           (when (setq sender (cdr (assq 'sender cont)))
553             (message-options-set 'mml-sender sender)
554             (message-options-set 'message-sender sender))
555           (if (setq recipients (cdr (assq 'recipients cont)))
556               (message-options-set 'message-recipients recipients))
557           (let ((style (mml-signencrypt-style
558                         (first (or sign-item encrypt-item)))))
559             ;; check if: we're both signing & encrypting, both methods
560             ;; are the same (why would they be different?!), and that
561             ;; the signencrypt style allows for combined operation.
562             (if (and sign-item encrypt-item (equal (first sign-item)
563                                                    (first encrypt-item))
564                      (equal style 'combined))
565                 (funcall (nth 1 encrypt-item) cont t)
566               ;; otherwise, revert to the old behavior.
567               (when sign-item
568                 (funcall (nth 1 sign-item) cont))
569               (when encrypt-item
570                 (funcall (nth 1 encrypt-item) cont)))))))))
571
572 (defun mml-compute-boundary (cont)
573   "Return a unique boundary that does not exist in CONT."
574   (let ((mml-boundary (funcall mml-boundary-function
575                                (incf mml-multipart-number))))
576     ;; This function tries again and again until it has found
577     ;; a unique boundary.
578     (while (not (catch 'not-unique
579                   (mml-compute-boundary-1 cont))))
580     mml-boundary))
581
582 (defun mml-compute-boundary-1 (cont)
583   (let (filename)
584     (cond
585      ((eq (car cont) 'part)
586       (with-temp-buffer
587         (cond
588          ((cdr (assq 'buffer cont))
589           (insert-buffer-substring (cdr (assq 'buffer cont))))
590          ((and (setq filename (cdr (assq 'filename cont)))
591                (not (equal (cdr (assq 'nofile cont)) "yes")))
592           (mm-insert-file-contents filename nil nil nil nil t))
593          (t
594           (insert (cdr (assq 'contents cont)))))
595         (goto-char (point-min))
596         (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
597                                  nil t)
598           (setq mml-boundary (funcall mml-boundary-function
599                                       (incf mml-multipart-number)))
600           (throw 'not-unique nil))))
601      ((eq (car cont) 'multipart)
602       (mapc 'mml-compute-boundary-1 (cddr cont))))
603     t))
604
605 (defun mml-make-boundary (number)
606   (concat (make-string (% number 60) ?=)
607           (if (> number 17)
608               (format "%x" number)
609             "")
610           mml-base-boundary))
611
612 (defun mml-insert-mime-headers (cont type charset encoding flowed)
613   (let (parameters id disposition description)
614     (setq parameters
615           (mml-parameter-string
616            cont mml-content-type-parameters))
617     (when (or charset
618               parameters
619               flowed
620               (not (equal type mml-generate-default-type))
621               mml-insert-mime-headers-always)
622       (when (consp charset)
623         (error
624          "Can't encode a part with several charsets"))
625       (insert "Content-Type: " type)
626       (when charset
627         (insert "; " (mail-header-encode-parameter
628                       "charset" (symbol-name charset))))
629       (when flowed
630         (insert "; format=flowed"))
631       (when parameters
632         (mml-insert-parameter-string
633          cont mml-content-type-parameters))
634       (insert "\n"))
635     (when (setq id (cdr (assq 'id cont)))
636       (insert "Content-ID: " id "\n"))
637     (setq parameters
638           (mml-parameter-string
639            cont mml-content-disposition-parameters))
640     (when (or (setq disposition (cdr (assq 'disposition cont)))
641               parameters)
642       (insert "Content-Disposition: " (or disposition "inline"))
643       (when parameters
644         (mml-insert-parameter-string
645          cont mml-content-disposition-parameters))
646       (insert "\n"))
647     (unless (eq encoding '7bit)
648       (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
649     (when (setq description (cdr (assq 'description cont)))
650       (insert "Content-Description: "
651               (mail-encode-encoded-word-string description) "\n"))))
652
653 (defun mml-parameter-string (cont types)
654   (let ((string "")
655         value type)
656     (while (setq type (pop types))
657       (when (setq value (cdr (assq type cont)))
658         ;; Strip directory component from the filename parameter.
659         (when (eq type 'filename)
660           (setq value (file-name-nondirectory value)))
661         (setq string (concat string "; "
662                              (mail-header-encode-parameter
663                               (symbol-name type) value)))))
664     (when (not (zerop (length string)))
665       string)))
666
667 (defun mml-insert-parameter-string (cont types)
668   (let (value type)
669     (while (setq type (pop types))
670       (when (setq value (cdr (assq type cont)))
671         ;; Strip directory component from the filename parameter.
672         (when (eq type 'filename)
673           (setq value (file-name-nondirectory value)))
674         (mml-insert-parameter
675          (mail-header-encode-parameter
676           (symbol-name type) value))))))
677
678 (eval-when-compile
679   (defvar ange-ftp-name-format)
680   (defvar efs-path-regexp))
681 (defun mml-parse-file-name (path)
682   (if (if (boundp 'efs-path-regexp)
683           (string-match efs-path-regexp path)
684         (if (boundp 'ange-ftp-name-format)
685             (string-match (car ange-ftp-name-format) path)))
686       (list (match-string 1 path) (match-string 2 path)
687             (substring path (1+ (match-end 2))))
688     path))
689
690 (defun mml-insert-buffer (buffer)
691   "Insert BUFFER at point and quote any MML markup."
692   (save-restriction
693     (narrow-to-region (point) (point))
694     (insert-buffer-substring buffer)
695     (mml-quote-region (point-min) (point-max))
696     (goto-char (point-max))))
697
698 ;;;
699 ;;; Transforming MIME to MML
700 ;;;
701
702 (defun mime-to-mml (&optional handles)
703   "Translate the current buffer (which should be a message) into MML.
704 If HANDLES is non-nil, use it instead reparsing the buffer."
705   ;; First decode the head.
706   (save-restriction
707     (message-narrow-to-head)
708     (mail-decode-encoded-word-region (point-min) (point-max)))
709   (unless handles
710     (setq handles (mm-dissect-buffer t)))
711   (goto-char (point-min))
712   (search-forward "\n\n" nil t)
713   (delete-region (point) (point-max))
714   (if (stringp (car handles))
715       (mml-insert-mime handles)
716     (mml-insert-mime handles t))
717   (mm-destroy-parts handles)
718   (save-restriction
719     (message-narrow-to-head)
720     ;; Remove them, they are confusing.
721     (message-remove-header "Content-Type")
722     (message-remove-header "MIME-Version")
723     (message-remove-header "Content-Disposition")
724     (message-remove-header "Content-Transfer-Encoding")))
725
726 (defun mml-to-mime ()
727   "Translate the current buffer from MML to MIME."
728   (message-encode-message-body)
729   (save-restriction
730     (message-narrow-to-headers-or-head)
731     ;; Skip past any From_ headers.
732     (while (looking-at "From ")
733       (forward-line 1))
734     (let ((mail-parse-charset message-default-charset))
735       (mail-encode-encoded-word-buffer))))
736
737 (defun mml-insert-mime (handle &optional no-markup)
738   (let (textp buffer mmlp)
739     ;; Determine type and stuff.
740     (unless (stringp (car handle))
741       (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
742         (save-excursion
743           (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
744           (mm-insert-part handle)
745           (if (setq mmlp (equal (mm-handle-media-type handle)
746                                 "message/rfc822"))
747               (mime-to-mml)))))
748     (if mmlp
749         (mml-insert-mml-markup handle nil t t)
750       (unless (and no-markup
751                    (equal (mm-handle-media-type handle) "text/plain"))
752         (mml-insert-mml-markup handle buffer textp)))
753     (cond
754      (mmlp
755       (insert-buffer-substring buffer)
756       (goto-char (point-max))
757       (insert "<#/mml>\n"))
758      ((stringp (car handle))
759       (mapcar 'mml-insert-mime (cdr handle))
760       (insert "<#/multipart>\n"))
761      (textp
762       (let ((charset (mail-content-type-get
763                       (mm-handle-type handle) 'charset))
764             (start (point)))
765         (if (eq charset 'gnus-decoded)
766             (mm-insert-part handle)
767           (insert (mm-decode-string (mm-get-part handle) charset)))
768         (mml-quote-region start (point)))
769       (goto-char (point-max)))
770      (t
771       (insert "<#/part>\n")))))
772
773 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
774   "Take a MIME handle and insert an MML tag."
775   (if (stringp (car handle))
776       (progn
777         (insert "<#multipart type=" (mm-handle-media-subtype handle))
778         (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
779           (when start
780             (insert " start=\"" start "\"")))
781         (insert ">\n"))
782     (if mmlp
783         (insert "<#mml type=" (mm-handle-media-type handle))
784       (insert "<#part type=" (mm-handle-media-type handle)))
785     (dolist (elem (append (cdr (mm-handle-type handle))
786                           (cdr (mm-handle-disposition handle))))
787       (unless (symbolp (cdr elem))
788         (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
789     (when (mm-handle-id handle)
790       (insert " id=\"" (mm-handle-id handle) "\""))
791     (when (mm-handle-disposition handle)
792       (insert " disposition=" (car (mm-handle-disposition handle))))
793     (when buffer
794       (insert " buffer=\"" (buffer-name buffer) "\""))
795     (when nofile
796       (insert " nofile=yes"))
797     (when (mm-handle-description handle)
798       (insert " description=\"" (mm-handle-description handle) "\""))
799     (insert ">\n")))
800
801 (defun mml-insert-parameter (&rest parameters)
802   "Insert PARAMETERS in a nice way."
803   (dolist (param parameters)
804     (insert ";")
805     (let ((point (point)))
806       (insert " " param)
807       (when (> (current-column) 71)
808         (goto-char point)
809         (insert "\n")
810         (end-of-line)))))
811
812 ;;;
813 ;;; Mode for inserting and editing MML forms
814 ;;;
815
816 (defvar mml-mode-map
817   (let ((sign (make-sparse-keymap))
818         (encrypt (make-sparse-keymap))
819         (signpart (make-sparse-keymap))
820         (encryptpart (make-sparse-keymap))
821         (map (make-sparse-keymap))
822         (main (make-sparse-keymap)))
823     (define-key sign "p" 'mml-secure-message-sign-pgpmime)
824     (define-key sign "o" 'mml-secure-message-sign-pgp)
825     (define-key sign "s" 'mml-secure-message-sign-smime)
826     (define-key signpart "p" 'mml-secure-sign-pgpmime)
827     (define-key signpart "o" 'mml-secure-sign-pgp)
828     (define-key signpart "s" 'mml-secure-sign-smime)
829     (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
830     (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
831     (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
832     (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
833     (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
834     (define-key encryptpart "s" 'mml-secure-encrypt-smime)
835     (define-key map "\C-n" 'mml-unsecure-message)
836     (define-key map "f" 'mml-attach-file)
837     (define-key map "b" 'mml-attach-buffer)
838     (define-key map "e" 'mml-attach-external)
839     (define-key map "q" 'mml-quote-region)
840     (define-key map "m" 'mml-insert-multipart)
841     (define-key map "p" 'mml-insert-part)
842     (define-key map "v" 'mml-validate)
843     (define-key map "P" 'mml-preview)
844     (define-key map "s" sign)
845     (define-key map "S" signpart)
846     (define-key map "c" encrypt)
847     (define-key map "C" encryptpart)
848     ;;(define-key map "n" 'mml-narrow-to-part)
849     ;; `M-m' conflicts with `back-to-indentation'.
850     ;; (define-key main "\M-m" map)
851     (define-key main "\C-c\C-m" map)
852     main))
853
854 (easy-menu-define
855   mml-menu mml-mode-map ""
856   `("Attachments"
857     ["Attach File..." mml-attach-file
858      ,@(if (featurep 'xemacs) '(t)
859          '(:help "Attach a file at point"))]
860     ["Attach Buffer..." mml-attach-buffer t]
861     ["Attach External..." mml-attach-external t]
862     ["Insert Part..." mml-insert-part t]
863     ["Insert Multipart..." mml-insert-multipart t]
864     ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
865     ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
866     ["PGP Sign" mml-secure-message-sign-pgp t]
867     ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
868     ["S/MIME Sign" mml-secure-message-sign-smime t]
869     ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
870     ("Secure MIME part"
871      ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
872      ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
873      ["PGP Sign Part" mml-secure-sign-pgp t]
874      ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
875      ["S/MIME Sign Part" mml-secure-sign-smime t]
876      ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
877     ["Encrypt/Sign off" mml-unsecure-message t]
878     ;;["Narrow" mml-narrow-to-part t]
879     ["Quote MML" mml-quote-region
880      :active (message-mark-active-p)
881      ,@(if (featurep 'xemacs) nil
882          '(:help "Quote MML tags in region"))]
883     ["Validate MML" mml-validate t]
884     ["Preview" mml-preview t]))
885
886 (defvar mml-mode nil
887   "Minor mode for editing MML.")
888
889 (defun mml-mode (&optional arg)
890   "Minor mode for editing MML.
891 MML is the MIME Meta Language, a minor mode for composing MIME articles.
892 See Info node `(emacs-mime)Composing'.
893
894 \\{mml-mode-map}"
895   (interactive "P")
896   (when (set (make-local-variable 'mml-mode)
897              (if (null arg) (not mml-mode)
898                (> (prefix-numeric-value arg) 0)))
899     (add-minor-mode 'mml-mode " MML" mml-mode-map)
900     (easy-menu-add mml-menu mml-mode-map)
901     (when (boundp 'x-dnd-protocol-alist)
902       (set (make-local-variable 'x-dnd-protocol-alist)
903            '(("^file:///" . mml-x-dnd-attach-file)
904              ("^file://"  . x-dnd-open-file)
905              ("^file:"    . mml-x-dnd-attach-file))))
906     (run-hooks 'mml-mode-hook)))
907
908 ;;;
909 ;;; Helper functions for reading MIME stuff from the minibuffer and
910 ;;; inserting stuff to the buffer.
911 ;;;
912
913 (defun mml-minibuffer-read-file (prompt)
914   (let* ((completion-ignored-extensions nil)
915          (file (read-file-name prompt nil nil t)))
916     ;; Prevent some common errors.  This is inspired by similar code in
917     ;; VM.
918     (when (file-directory-p file)
919       (error "%s is a directory, cannot attach" file))
920     (unless (file-exists-p file)
921       (error "No such file: %s" file))
922     (unless (file-readable-p file)
923       (error "Permission denied: %s" file))
924     file))
925
926 (defun mml-minibuffer-read-type (name &optional default)
927   (mailcap-parse-mimetypes)
928   (let* ((default (or default
929                       (mm-default-file-encoding name)
930                       ;; Perhaps here we should check what the file
931                       ;; looks like, and offer text/plain if it looks
932                       ;; like text/plain.
933                       "application/octet-stream"))
934          (string (completing-read
935                   (format "Content type (default %s): " default)
936                   (mapcar 'list (mailcap-mime-types)))))
937     (if (not (equal string ""))
938         string
939       default)))
940
941 (defun mml-minibuffer-read-description ()
942   (let ((description (read-string "One line description: ")))
943     (when (string-match "\\`[ \t]*\\'" description)
944       (setq description nil))
945     description))
946
947 (defun mml-minibuffer-read-disposition (type &optional default)
948   (let* ((default (or default
949                       (if (string-match "^text/.*" type)
950                           "inline"
951                         "attachment")))
952          (disposition (completing-read "Disposition: "
953                                        '(("attachment") ("inline") (""))
954                                        nil t)))
955     (if (not (equal disposition ""))
956         disposition
957       default)))
958
959 (defun mml-quote-region (beg end)
960   "Quote the MML tags in the region."
961   (interactive "r")
962   (save-excursion
963     (save-restriction
964       ;; Temporarily narrow the region to defend from changes
965       ;; invalidating END.
966       (narrow-to-region beg end)
967       (goto-char (point-min))
968       ;; Quote parts.
969       (while (re-search-forward
970               "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
971         ;; Insert ! after the #.
972         (goto-char (+ (match-beginning 0) 2))
973         (insert "!")))))
974
975 (defun mml-insert-tag (name &rest plist)
976   "Insert an MML tag described by NAME and PLIST."
977   (when (symbolp name)
978     (setq name (symbol-name name)))
979   (insert "<#" name)
980   (while plist
981     (let ((key (pop plist))
982           (value (pop plist)))
983       (when value
984         ;; Quote VALUE if it contains suspicious characters.
985         (when (string-match "[\"'\\~/*;() \t\n]" value)
986           (setq value (with-output-to-string
987                         (let (print-escape-nonascii)
988                           (prin1 value)))))
989         (insert (format " %s=%s" key value)))))
990   (insert ">\n"))
991
992 (defun mml-insert-empty-tag (name &rest plist)
993   "Insert an empty MML tag described by NAME and PLIST."
994   (when (symbolp name)
995     (setq name (symbol-name name)))
996   (apply #'mml-insert-tag name plist)
997   (insert "<#/" name ">\n"))
998
999 ;;; Attachment functions.
1000
1001 (defun mml-attach-file (file &optional type description disposition)
1002   "Attach a file to the outgoing MIME message.
1003 The file is not inserted or encoded until you send the message with
1004 `\\[message-send-and-exit]' or `\\[message-send]'.
1005
1006 FILE is the name of the file to attach.  TYPE is its content-type, a
1007 string of the form \"type/subtype\".  DESCRIPTION is a one-line
1008 description of the attachment."
1009   (interactive
1010    (let* ((file (mml-minibuffer-read-file "Attach file: "))
1011           (type (mml-minibuffer-read-type file))
1012           (description (mml-minibuffer-read-description))
1013           (disposition (mml-minibuffer-read-disposition type)))
1014      (list file type description disposition)))
1015   (mml-insert-empty-tag 'part
1016                         'type type
1017                         'filename file
1018                         'disposition (or disposition "attachment")
1019                         'description description))
1020
1021 (defun mml-x-dnd-attach-file (uri action)
1022   "Attach a drag and drop file."
1023   (let ((file (x-dnd-get-local-file-name uri t)))
1024     (when (and file (file-regular-p file))
1025       (let* ((type (mml-minibuffer-read-type file))
1026             (description (mml-minibuffer-read-description))
1027             (disposition (mml-minibuffer-read-disposition type)))
1028         (mml-attach-file file type description disposition)))))
1029
1030 (defun mml-attach-buffer (buffer &optional type description)
1031   "Attach a buffer to the outgoing MIME message.
1032 See `mml-attach-file' for details of operation."
1033   (interactive
1034    (let* ((buffer (read-buffer "Attach buffer: "))
1035           (type (mml-minibuffer-read-type buffer "text/plain"))
1036           (description (mml-minibuffer-read-description)))
1037      (list buffer type description)))
1038   (mml-insert-empty-tag 'part 'type type 'buffer buffer
1039                         'disposition "attachment" 'description description))
1040
1041 (defun mml-attach-external (file &optional type description)
1042   "Attach an external file into the buffer.
1043 FILE is an ange-ftp/efs specification of the part location.
1044 TYPE is the MIME type to use."
1045   (interactive
1046    (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1047           (type (mml-minibuffer-read-type file))
1048           (description (mml-minibuffer-read-description)))
1049      (list file type description)))
1050   (mml-insert-empty-tag 'external 'type type 'name file
1051                         'disposition "attachment" 'description description))
1052
1053 (defun mml-insert-multipart (&optional type)
1054   (interactive (list (completing-read "Multipart type (default mixed): "
1055                                       '(("mixed") ("alternative") ("digest") ("parallel")
1056                                         ("signed") ("encrypted"))
1057                                       nil nil "mixed")))
1058   (or type
1059       (setq type "mixed"))
1060   (mml-insert-empty-tag "multipart" 'type type)
1061   (forward-line -1))
1062
1063 (defun mml-insert-part (&optional type)
1064   (interactive
1065    (list (mml-minibuffer-read-type "")))
1066   (mml-insert-tag 'part 'type type 'disposition "inline")
1067   (forward-line -1))
1068
1069 (defun mml-preview-insert-mail-followup-to ()
1070   "Insert a Mail-Followup-To header before previewing an article.
1071 Should be adopted if code in `message-send-mail' is changed."
1072   (when (and (message-mail-p)
1073              (message-subscribed-p)
1074              (not (mail-fetch-field "mail-followup-to"))
1075              (message-make-mail-followup-to))
1076     (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1077     (insert (message-make-mail-followup-to))))
1078
1079 (defvar mml-preview-buffer nil)
1080
1081 (defun mml-preview (&optional raw)
1082   "Display current buffer with Gnus, in a new buffer.
1083 If RAW, don't highlight the article."
1084   (interactive "P")
1085   (setq mml-preview-buffer (generate-new-buffer
1086                             (concat (if raw "*Raw MIME preview of "
1087                                       "*MIME preview of ") (buffer-name))))
1088   (save-excursion
1089     (let* ((buf (current-buffer))
1090            (message-options message-options)
1091            (message-this-is-mail (message-mail-p))
1092            (message-this-is-news (message-news-p))
1093            (message-posting-charset (or (gnus-setup-posting-charset
1094                                          (save-restriction
1095                                            (message-narrow-to-headers-or-head)
1096                                            (message-fetch-field "Newsgroups")))
1097                                         message-posting-charset)))
1098       (message-options-set-recipient)
1099       (when (boundp 'gnus-buffers)
1100         (push mml-preview-buffer gnus-buffers))
1101       (set-buffer mml-preview-buffer)
1102       (erase-buffer)
1103       (insert-buffer-substring buf)
1104       (mml-preview-insert-mail-followup-to)
1105       (let ((message-deletable-headers (if (message-news-p)
1106                                            nil
1107                                          message-deletable-headers)))
1108         (message-generate-headers
1109          (copy-sequence (if (message-news-p)
1110                             message-required-news-headers
1111                           message-required-mail-headers))))
1112       (if (re-search-forward
1113            (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1114           (replace-match "\n"))
1115       (let ((mail-header-separator ""));; mail-header-separator is removed.
1116         (mml-to-mime))
1117       (if raw
1118           (when (fboundp 'set-buffer-multibyte)
1119             (let ((s (buffer-string)))
1120               ;; Insert the content into unibyte buffer.
1121               (erase-buffer)
1122               (mm-disable-multibyte)
1123               (insert s)))
1124         (let ((gnus-newsgroup-charset (car message-posting-charset))
1125               gnus-article-prepare-hook gnus-original-article-buffer)
1126           (run-hooks 'gnus-article-decode-hook)
1127           (let ((gnus-newsgroup-name "dummy")
1128                 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1129                                         (gnus-make-hashtable 5))))
1130             (gnus-article-prepare-display))))
1131       ;; Disable article-mode-map.
1132       (use-local-map nil)
1133       (gnus-make-local-hook 'kill-buffer-hook)
1134       (add-hook 'kill-buffer-hook
1135                 (lambda ()
1136                   (mm-destroy-parts gnus-article-mime-handles)) nil t)
1137       (setq buffer-read-only t)
1138       (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1139       (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1140       (local-set-key "\r"
1141                      (lambda ()
1142                        (interactive)
1143                        (widget-button-press (point))))
1144       (local-set-key gnus-mouse-2
1145                      (lambda (event)
1146                        (interactive "@e")
1147                        (widget-button-press (widget-event-point event) event)))
1148       (goto-char (point-min))))
1149   (if (and (boundp 'gnus-buffer-configuration)
1150            (assq 'mml-preview gnus-buffer-configuration))
1151       (gnus-configure-windows 'mml-preview)
1152     (pop-to-buffer mml-preview-buffer)))
1153
1154 (defun mml-validate ()
1155   "Validate the current MML document."
1156   (interactive)
1157   (mml-parse))
1158
1159 (defun mml-tweak-part (cont)
1160   "Tweak a MML part."
1161   (let ((tweak (cdr (assq 'tweak cont)))
1162         func)
1163     (cond
1164      (tweak
1165       (setq func
1166             (or (cdr (assoc tweak mml-tweak-function-alist))
1167                 (intern tweak))))
1168      (mml-tweak-type-alist
1169       (let ((alist mml-tweak-type-alist)
1170             (type (or (cdr (assq 'type cont)) "text/plain")))
1171         (while alist
1172           (if (string-match (caar alist) type)
1173               (setq func (cdar alist)
1174                     alist nil)
1175             (setq alist (cdr alist)))))))
1176     (if func
1177         (funcall func cont)
1178       cont)
1179     (let ((alist mml-tweak-sexp-alist))
1180       (while alist
1181         (if (eval (caar alist))
1182             (funcall (cdar alist) cont))
1183         (setq alist (cdr alist)))))
1184   cont)
1185
1186 (defun mml-tweak-externalize-attachments (cont)
1187   "Tweak attached files as external parts."
1188   (let (filename-cons)
1189     (when (and (eq (car cont) 'part)
1190                (not (cdr (assq 'buffer cont)))
1191                (and (setq filename-cons (assq 'filename cont))
1192                     (not (equal (cdr (assq 'nofile cont)) "yes"))))
1193       (setcar cont 'external)
1194       (setcar filename-cons 'name))))
1195
1196 (provide 'mml)
1197
1198 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1199 ;;; mml.el ends here