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