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