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