1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
35 (eval-when-compile (require 'cl))
37 (autoload 'message-make-message-id "message")
38 (autoload 'gnus-setup-posting-charset "gnus-msg")
39 (autoload 'gnus-make-local-hook "gnus-util")
40 (autoload 'message-fetch-field "message")
41 (autoload 'message-mark-active-p "message")
42 (autoload 'message-info "message")
43 (autoload 'fill-flowed-encode "flow-fill")
44 (autoload 'message-posting-charset "message")
45 (autoload 'dnd-get-local-file-name "dnd")
47 (autoload 'message-options-set "message")
48 (autoload 'message-narrow-to-head "message")
49 (autoload 'message-in-body-p "message")
50 (autoload 'message-mail-p "message")
52 (defvar gnus-article-mime-handles)
54 (defvar gnus-newsrc-hashtb)
55 (defvar message-default-charset)
56 (defvar message-deletable-headers)
57 (defvar message-options)
58 (defvar message-posting-charset)
59 (defvar message-required-mail-headers)
60 (defvar message-required-news-headers)
61 (defvar dnd-protocol-alist)
62 (defvar mml-dnd-protocol-alist)
64 (defcustom mml-content-type-parameters
65 '(name access-type expiration size permission format)
66 "*A list of acceptable parameters in MML tag.
67 These parameters are generated in Content-Type header if exists."
69 :type '(repeat (symbol :tag "Parameter"))
72 (defcustom mml-content-disposition-parameters
73 '(filename creation-date modification-date read-date)
74 "*A list of acceptable parameters in MML tag.
75 These parameters are generated in Content-Disposition header if exists."
77 :type '(repeat (symbol :tag "Parameter"))
80 (defcustom mml-content-disposition-alist
81 '((text (rtf . "attachment") (t . "inline"))
83 "Alist of MIME types or regexps matching file names and default dispositions.
84 Each element should be one of the following three forms:
86 (REGEXP . DISPOSITION)
87 (SUPERTYPE (SUBTYPE . DISPOSITION) (SUBTYPE . DISPOSITION)...)
90 Where REGEXP is a string which matches the file name (if any) of an
91 attachment, SUPERTYPE, SUBTYPE and TYPE should be symbols which are a
92 MIME supertype (e.g., text), a MIME subtype (e.g., plain) and a MIME
93 type (e.g., text/plain) respectively, and DISPOSITION should be either
94 the string \"attachment\" or the string \"inline\". The value t for
95 SUPERTYPE, SUBTYPE or TYPE matches any of those types. The first
96 match found will be used."
97 :version "23.1" ;; No Gnus
98 :type (let ((dispositions '(radio :format "DISPOSITION: %v"
100 (const :format "%v " "attachment")
101 (const :format "%v\n" "inline"))))
104 (choice :format "%[Value Menu%]%v"
105 (cons :tag "(REGEXP . DISPOSITION)" :extra-offset 4
106 (regexp :tag "REGEXP" :value ".*")
108 (cons :tag "(SUPERTYPE (SUBTYPE . DISPOSITION)...)"
110 (symbol :tag " SUPERTYPE" :value text)
111 (repeat :format "%v%i\n" :offset 0 :extra-offset 4
112 (cons :format "%v" :extra-offset 5
113 (symbol :tag "SUBTYPE" :value t)
115 (cons :tag "(TYPE . DISPOSITION)" :extra-offset 4
116 (symbol :tag "TYPE" :value t)
120 (defcustom mml-insert-mime-headers-always nil
121 "If non-nil, always put Content-Type: text/plain at top of empty parts.
122 It is necessary to work against a bug in certain clients."
127 (defvar mml-tweak-type-alist nil
128 "A list of (TYPE . FUNCTION) for tweaking MML parts.
129 TYPE is a string containing a regexp to match the MIME type. FUNCTION
130 is a Lisp function which is called with the MML handle to tweak the
131 part. This variable is used only when no TWEAK parameter exists in
134 (defvar mml-tweak-function-alist nil
135 "A list of (NAME . FUNCTION) for tweaking MML parts.
136 NAME is a string containing the name of the TWEAK parameter in the MML
137 handle. FUNCTION is a Lisp function which is called with the MML
138 handle to tweak the part.")
140 (defvar mml-tweak-sexp-alist
141 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
142 "A list of (SEXP . FUNCTION) for tweaking MML parts.
143 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
144 is called. FUNCTION is a Lisp function which is called with the MML
145 handle to tweak the part.")
147 (defvar mml-externalize-attachments nil
148 "*If non-nil, local-file attachments are generated as external parts.")
150 (defvar mml-generate-multipart-alist nil
151 "*Alist of multipart generation functions.
152 Each entry has the form (NAME . FUNCTION), where
153 NAME is a string containing the name of the part (without the
154 leading \"/multipart/\"),
155 FUNCTION is a Lisp function which is called to generate the part.
157 The Lisp function has to supply the appropriate MIME headers and the
158 contents of this part.")
160 (defvar mml-syntax-table
161 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
162 (modify-syntax-entry ?\\ "/" table)
163 (modify-syntax-entry ?< "(" table)
164 (modify-syntax-entry ?> ")" table)
165 (modify-syntax-entry ?@ "w" table)
166 (modify-syntax-entry ?/ "w" table)
167 (modify-syntax-entry ?= " " table)
168 (modify-syntax-entry ?* " " table)
169 (modify-syntax-entry ?\; " " table)
170 (modify-syntax-entry ?\' " " table)
173 (defvar mml-boundary-function 'mml-make-boundary
174 "A function called to suggest a boundary.
175 The function may be called several times, and should try to make a new
176 suggestion each time. The function is called with one parameter,
177 which is a number that says how many times the function has been
178 called for this message.")
180 (defvar mml-confirmation-set nil
181 "A list of symbols, each of which disables some warning.
182 `unknown-encoding': always send messages contain characters with
183 unknown encoding; `use-ascii': always use ASCII for those characters
184 with unknown encoding; `multipart': always send messages with more than
187 (defvar mml-generate-default-type "text/plain"
188 "Content type by which the Content-Type header can be omitted.
189 The Content-Type header will not be put in the MIME part if the type
190 equals the value and there's no parameter (e.g. charset, format, etc.)
191 and `mml-insert-mime-headers-always' is nil. The value will be bound
192 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
193 part. This is for the internal use, you should never modify the value.")
195 (defvar mml-buffer-list nil)
197 (defun mml-generate-new-buffer (name)
198 (let ((buf (generate-new-buffer name)))
199 (push buf mml-buffer-list)
202 (defun mml-destroy-buffers ()
203 (let (kill-buffer-hook)
204 (mapc 'kill-buffer mml-buffer-list)
205 (setq mml-buffer-list nil)))
208 "Parse the current buffer as an MML document."
210 (goto-char (point-min))
211 (with-syntax-table mml-syntax-table
214 (defun mml-parse-1 ()
215 "Parse the current buffer as an MML document."
216 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
217 (while (and (not (eobp))
218 (not (looking-at "<#/multipart")))
220 ((looking-at "<#secure")
221 ;; The secure part is essentially a meta-meta tag, which
222 ;; expands to either a part tag if there are no other parts in
223 ;; the document or a multipart tag if there are other parts
224 ;; included in the message
226 (taginfo (mml-read-tag))
227 (keyfile (cdr (assq 'keyfile taginfo)))
228 (certfile (cdr (assq 'certfile taginfo)))
229 (recipients (cdr (assq 'recipients taginfo)))
230 (sender (cdr (assq 'sender taginfo)))
231 (location (cdr (assq 'tag-location taginfo)))
232 (mode (cdr (assq 'mode taginfo)))
233 (method (cdr (assq 'method taginfo)))
236 (if (re-search-forward
237 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
238 (setq secure-mode "multipart")
239 (setq secure-mode "part")))
242 (re-search-forward "<#secure[^\n]*>\n"))
243 (delete-region (match-beginning 0) (match-end 0))
244 (cond ((string= mode "sign")
245 (setq tags (list "sign" method)))
246 ((string= mode "encrypt")
247 (setq tags (list "encrypt" method)))
248 ((string= mode "signencrypt")
249 (setq tags (list "sign" method "encrypt" method))))
250 (eval `(mml-insert-tag ,secure-mode
252 ,(if keyfile "keyfile")
254 ,(if certfile "certfile")
256 ,(if recipients "recipients")
258 ,(if sender "sender")
261 (goto-char location)))
262 ((looking-at "<#multipart")
263 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
264 ((looking-at "<#external")
265 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
268 (if (or (looking-at "<#part") (looking-at "<#mml"))
269 (setq tag (mml-read-tag)
272 (setq tag (list 'part '(type . "text/plain"))
275 (setq raw (cdr (assq 'raw tag))
277 contents (mml-read-part (eq 'mml (car tag)))
282 (intern (downcase (cdr (assq 'charset tag))))))
284 (mm-find-mime-charset-region point (point)
286 (when (and (not raw) (memq nil charsets))
287 (if (or (memq 'unknown-encoding mml-confirmation-set)
288 (message-options-get 'unknown-encoding)
290 Message contains characters with unknown encoding. Really send? ")
291 (message-options-set 'unknown-encoding t)))
293 (or (memq 'use-ascii mml-confirmation-set)
294 (message-options-get 'use-ascii)
295 (and (y-or-n-p "Use ASCII as charset? ")
296 (message-options-set 'use-ascii t))))
297 (setq charsets (delq nil charsets))
299 (error "Edit your message to remove those characters")))
302 (< (length charsets) 2))
303 (if (or (not no-markup-p)
304 (string-match "[^ \t\r\n]" contents))
305 ;; Don't create blank parts.
306 (push (nconc tag (list (cons 'contents contents)))
308 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
309 tag point (point) use-ascii)))
311 (not (memq 'multipart mml-confirmation-set))
312 (not (message-options-get 'multipart))
313 (not (and (y-or-n-p (format "\
314 A message part needs to be split into %d charset parts. Really send? "
316 (message-options-set 'multipart t))))
317 (error "Edit your message to use only one charset"))
318 (setq struct (nconc nstruct struct)))))))
323 (defun mml-parse-singlepart-with-multiple-charsets
324 (orig-tag beg end &optional use-ascii)
327 (narrow-to-region beg end)
328 (goto-char (point-min))
329 (let ((current (or (mm-mime-charset (mm-charset-after))
330 (and use-ascii 'us-ascii)))
331 charset struct space newline paragraph)
333 (setq charset (mm-mime-charset (mm-charset-after)))
335 ;; The charset remains the same.
336 ((eq charset 'us-ascii))
337 ((or (and use-ascii (not charset))
338 (eq charset current))
342 ;; The initial charset was ascii.
343 ((eq current 'us-ascii)
344 (setq current charset
348 ;; We have a change in charsets.
352 (list (cons 'contents
353 (buffer-substring-no-properties
354 beg (or paragraph newline space (point))))))
356 (setq beg (or paragraph newline space (point))
361 ;; Compute places where it might be nice to break the part.
363 ((memq (following-char) '(? ?\t))
364 (setq space (1+ (point))))
365 ((and (eq (following-char) ?\n)
367 (eq (char-after (1- (point))) ?\n))
368 (setq paragraph (point)))
369 ((eq (following-char) ?\n)
370 (setq newline (1+ (point)))))
372 ;; Do the final part.
373 (unless (= beg (point))
374 (push (append orig-tag
375 (list (cons 'contents
376 (buffer-substring-no-properties
381 (defun mml-read-tag ()
382 "Read a tag and return the contents."
383 (let ((orig-point (point))
384 contents name elem val)
386 (setq name (buffer-substring-no-properties
387 (point) (progn (forward-sexp 1) (point))))
388 (skip-chars-forward " \t\n")
389 (while (not (looking-at ">[ \t]*\n?"))
390 (setq elem (buffer-substring-no-properties
391 (point) (progn (forward-sexp 1) (point))))
392 (skip-chars-forward "= \t\n")
393 (setq val (buffer-substring-no-properties
394 (point) (progn (forward-sexp 1) (point))))
395 (when (string-match "^\"\\(.*\\)\"$" val)
396 (setq val (match-string 1 val)))
397 (push (cons (intern elem) val) contents)
398 (skip-chars-forward " \t\n"))
399 (goto-char (match-end 0))
400 ;; Don't skip the leading space.
401 ;;(skip-chars-forward " \t\n")
402 ;; Put the tag location into the returned contents
403 (setq contents (append (list (cons 'tag-location orig-point)) contents))
404 (cons (intern name) (nreverse contents))))
406 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
407 (let ((str (buffer-substring-no-properties start end))
408 (bufstart start) tmp)
409 (while (setq tmp (text-property-any start end 'hard 't))
410 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
412 (setq start (1+ tmp)))
415 (defun mml-read-part (&optional mml)
416 "Return the buffer up till the next part, multipart or closing part or multipart.
417 If MML is non-nil, return the buffer up till the correspondent mml tag."
418 (let ((beg (point)) (count 1))
419 ;; If the tag ended at the end of the line, we go to the next line.
420 (when (looking-at "[ \t]*\n")
424 (while (and (> count 0) (not (eobp)))
425 (if (re-search-forward "<#\\(/\\)?mml." nil t)
426 (setq count (+ count (if (match-beginning 1) -1 1)))
427 (goto-char (point-max))))
428 (mml-buffer-substring-no-properties-except-hard-newlines
431 (match-beginning 0))))
432 (if (re-search-forward
433 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
435 (mml-buffer-substring-no-properties-except-hard-newlines
436 beg (match-beginning 0))
437 (if (or (not (match-beginning 1))
438 (equal (match-string 2) "multipart"))
439 (goto-char (match-beginning 0))
440 (when (looking-at "[ \t]*\n")
442 (mml-buffer-substring-no-properties-except-hard-newlines
443 beg (goto-char (point-max)))))))
445 (defvar mml-boundary nil)
446 (defvar mml-base-boundary "-=-=")
447 (defvar mml-multipart-number 0)
449 (defun mml-generate-mime ()
450 "Generate a MIME message based on the current MML document."
451 (let ((cont (mml-parse))
452 (mml-multipart-number mml-multipart-number))
455 (mm-with-multibyte-buffer
456 (if (and (consp (car cont))
458 (mml-generate-mime-1 (car cont))
459 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
463 (defun mml-generate-mime-1 (cont)
464 (let ((mm-use-ultra-safe-encoding
465 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
467 (narrow-to-region (point) (point))
468 (mml-tweak-part cont)
470 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
471 (let* ((raw (cdr (assq 'raw cont)))
472 (filename (cdr (assq 'filename cont)))
473 (type (or (cdr (assq 'type cont))
475 (or (mm-default-file-encoding filename)
476 "application/octet-stream")
478 (charset (cdr (assq 'charset cont)))
479 (coding (mm-charset-to-coding-system charset))
480 encoding flowed coded)
481 (cond ((eq coding 'ascii)
485 ;; The value of `charset' might be a bogus alias that
486 ;; `mm-charset-synonym-alist' provides, like `utf8',
487 ;; so we prefer the MIME charset that Emacs knows for
488 ;; the coding system `coding'.
489 (setq charset (or (mm-coding-system-to-mime-charset coding)
490 (intern (downcase charset))))))
492 (member (car (split-string type "/")) '("text" "message")))
496 ((cdr (assq 'buffer cont))
497 (insert-buffer-substring (cdr (assq 'buffer cont))))
499 (not (equal (cdr (assq 'nofile cont)) "yes")))
500 (let ((coding-system-for-read coding))
501 (mm-insert-file-contents filename)))
502 ((eq 'mml (car cont))
503 (insert (cdr (assq 'contents cont))))
506 (narrow-to-region (point) (point))
507 (insert (cdr (assq 'contents cont)))
508 ;; Remove quotes from quoted tags.
509 (goto-char (point-min))
510 (while (re-search-forward
511 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
513 (delete-region (+ (match-beginning 0) 2)
514 (+ (match-beginning 0) 3))))))
516 ((eq (car cont) 'mml)
517 (let ((mml-boundary (mml-compute-boundary cont))
518 ;; It is necessary for the case where this
519 ;; function is called recursively since
520 ;; `m-g-d-t' will be bound to "message/rfc822"
521 ;; when encoding an article to be forwarded.
522 (mml-generate-default-type "text/plain"))
524 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
525 ;; ignore 0x1b, it is part of iso-2022-jp
526 (setq encoding (mm-body-7-or-8))))
527 ((string= (car (split-string type "/")) "message")
528 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
529 ;; ignore 0x1b, it is part of iso-2022-jp
530 (setq encoding (mm-body-7-or-8))))
532 ;; Only perform format=flowed filling on text/plain
533 ;; parts where there either isn't a format parameter
534 ;; in the mml tag or it says "flowed" and there
535 ;; actually are hard newlines in the text.
536 (let (use-hard-newlines)
537 (when (and (string= type "text/plain")
538 (not (string= (cdr (assq 'sign cont)) "pgp"))
539 (or (null (assq 'format cont))
540 (string= (cdr (assq 'format cont))
542 (setq use-hard-newlines
544 (point-min) (point-max) 'hard 't)))
546 ;; Indicate that `mml-insert-mime-headers' should
547 ;; insert a "; format=flowed" string unless the
548 ;; user has already specified it.
549 (setq flowed (null (assq 'format cont)))))
550 ;; Prefer `utf-8' for text/calendar parts.
552 (not (string= type "text/calendar")))
553 (setq charset (mm-encode-body charset))
554 (let ((mm-coding-system-priorities
555 (cons 'utf-8 mm-coding-system-priorities)))
556 (setq charset (mm-encode-body))))
557 (setq encoding (mm-body-encoding
558 charset (cdr (assq 'encoding cont))))))
559 (setq coded (buffer-string)))
560 (mml-insert-mime-headers cont type charset encoding flowed)
563 (mm-with-unibyte-buffer
565 ((cdr (assq 'buffer cont))
566 (insert (mm-string-as-unibyte
567 (with-current-buffer (cdr (assq 'buffer cont))
570 (not (equal (cdr (assq 'nofile cont)) "yes")))
571 (let ((coding-system-for-read mm-binary-coding-system))
572 (mm-insert-file-contents filename nil nil nil nil t))
574 (setq charset (mm-coding-system-to-mime-charset
575 (mm-find-buffer-file-coding-system
578 (let ((contents (cdr (assq 'contents cont))))
579 (if (if (featurep 'xemacs)
580 (string-match "[^\000-\377]" contents)
581 (mm-multibyte-string-p contents))
583 (mm-enable-multibyte)
586 (setq charset (mm-encode-body charset))))
587 (insert contents)))))
588 (setq encoding (mm-encode-buffer type)
589 coded (mm-string-as-multibyte (buffer-string))))
590 (mml-insert-mime-headers cont type charset encoding nil)
591 (insert "\n" coded))))
592 ((eq (car cont) 'external)
593 (insert "Content-Type: message/external-body")
594 (let ((parameters (mml-parameter-string
595 cont '(expiration size permission)))
596 (name (cdr (assq 'name cont)))
597 (url (cdr (assq 'url cont))))
599 (setq name (mml-parse-file-name name))
601 (mml-insert-parameter
602 (mail-header-encode-parameter "name" name)
603 "access-type=local-file")
604 (mml-insert-parameter
605 (mail-header-encode-parameter
606 "name" (file-name-nondirectory (nth 2 name)))
607 (mail-header-encode-parameter "site" (nth 1 name))
608 (mail-header-encode-parameter
609 "directory" (file-name-directory (nth 2 name))))
610 (mml-insert-parameter
611 (concat "access-type="
612 (if (member (nth 0 name) '("ftp@" "anonymous@"))
616 (mml-insert-parameter
617 (mail-header-encode-parameter "url" url)
620 (mml-insert-parameter-string
621 cont '(expiration size permission)))
623 (insert "Content-Type: "
624 (or (cdr (assq 'type cont))
626 (or (mm-default-file-encoding name)
627 "application/octet-stream")
630 (insert "Content-ID: " (message-make-message-id) "\n")
631 (insert "Content-Transfer-Encoding: "
632 (or (cdr (assq 'encoding cont)) "binary"))
634 (insert (or (cdr (assq 'contents cont))))
636 ((eq (car cont) 'multipart)
637 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
638 (mml-generate-default-type (if (equal type "digest")
641 (handler (assoc type mml-generate-multipart-alist)))
643 (funcall (cdr handler) cont)
644 ;; No specific handler. Use default one.
645 (let ((mml-boundary (mml-compute-boundary cont)))
646 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
648 (if (cdr (assq 'start cont))
649 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
651 (let ((cont cont) part)
652 (while (setq part (pop cont))
653 ;; Skip `multipart' and attributes.
654 (when (and (consp part) (consp (cdr part)))
655 (insert "\n--" mml-boundary "\n")
656 (mml-generate-mime-1 part)
657 (goto-char (point-max)))))
658 (insert "\n--" mml-boundary "--\n")))))
660 (error "Invalid element: %S" cont)))
661 ;; handle sign & encrypt tags in a semi-smart way.
662 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
663 (encrypt-item (assoc (cdr (assq 'encrypt cont))
666 (when (or sign-item encrypt-item)
667 (when (setq sender (cdr (assq 'sender cont)))
668 (message-options-set 'mml-sender sender)
669 (message-options-set 'message-sender sender))
670 (if (setq recipients (cdr (assq 'recipients cont)))
671 (message-options-set 'message-recipients recipients))
672 (let ((style (mml-signencrypt-style
673 (first (or sign-item encrypt-item)))))
674 ;; check if: we're both signing & encrypting, both methods
675 ;; are the same (why would they be different?!), and that
676 ;; the signencrypt style allows for combined operation.
677 (if (and sign-item encrypt-item (equal (first sign-item)
678 (first encrypt-item))
679 (equal style 'combined))
680 (funcall (nth 1 encrypt-item) cont t)
681 ;; otherwise, revert to the old behavior.
683 (funcall (nth 1 sign-item) cont))
685 (funcall (nth 1 encrypt-item) cont)))))))))
687 (defun mml-compute-boundary (cont)
688 "Return a unique boundary that does not exist in CONT."
689 (let ((mml-boundary (funcall mml-boundary-function
690 (incf mml-multipart-number))))
691 ;; This function tries again and again until it has found
692 ;; a unique boundary.
693 (while (not (catch 'not-unique
694 (mml-compute-boundary-1 cont))))
697 (defun mml-compute-boundary-1 (cont)
700 ((eq (car cont) 'part)
703 ((cdr (assq 'buffer cont))
704 (insert-buffer-substring (cdr (assq 'buffer cont))))
705 ((and (setq filename (cdr (assq 'filename cont)))
706 (not (equal (cdr (assq 'nofile cont)) "yes")))
707 (mm-insert-file-contents filename nil nil nil nil t))
709 (insert (cdr (assq 'contents cont)))))
710 (goto-char (point-min))
711 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
713 (setq mml-boundary (funcall mml-boundary-function
714 (incf mml-multipart-number)))
715 (throw 'not-unique nil))))
716 ((eq (car cont) 'multipart)
717 (mapc 'mml-compute-boundary-1 (cddr cont))))
720 (defun mml-make-boundary (number)
721 (concat (make-string (% number 60) ?=)
727 (defun mml-content-disposition (type &optional filename)
728 "Return a default disposition name suitable to TYPE or FILENAME."
729 (let ((defs mml-content-disposition-alist)
730 disposition def types)
731 (while (and (not disposition) defs)
732 (setq def (pop defs))
733 (cond ((stringp (car def))
735 (string-match (car def) filename))
736 (setq disposition (cdr def))))
738 (when (string= (car (setq types (split-string type "/")))
740 (setq type (cadr types)
742 (while (and (not disposition) types)
743 (setq def (pop types))
744 (when (or (eq (car def) t) (string= type (car def)))
745 (setq disposition (cdr def))))))
747 (when (or (eq (car def) t) (string= type (car def)))
748 (setq disposition (cdr def))))))
749 (or disposition "attachment")))
751 (defun mml-insert-mime-headers (cont type charset encoding flowed)
752 (let (parameters id disposition description)
754 (mml-parameter-string
755 cont mml-content-type-parameters))
759 (not (equal type mml-generate-default-type))
760 mml-insert-mime-headers-always)
761 (when (consp charset)
763 "Can't encode a part with several charsets"))
764 (insert "Content-Type: " type)
766 (mml-insert-parameter
767 (mail-header-encode-parameter "charset" (symbol-name charset))))
769 (mml-insert-parameter "format=flowed"))
771 (mml-insert-parameter-string
772 cont mml-content-type-parameters))
774 (when (setq id (cdr (assq 'id cont)))
775 (insert "Content-ID: " id "\n"))
777 (mml-parameter-string
778 cont mml-content-disposition-parameters))
779 (when (or (setq disposition (cdr (assq 'disposition cont)))
781 (insert "Content-Disposition: "
783 (mml-content-disposition type (cdr (assq 'filename cont)))))
785 (mml-insert-parameter-string
786 cont mml-content-disposition-parameters))
788 (unless (eq encoding '7bit)
789 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
790 (when (setq description (cdr (assq 'description cont)))
791 (insert "Content-Description: ")
792 (setq description (prog1
794 (insert description "\n")))
795 (mail-encode-encoded-word-region description (point)))))
797 (defun mml-parameter-string (cont types)
800 (while (setq type (pop types))
801 (when (setq value (cdr (assq type cont)))
802 ;; Strip directory component from the filename parameter.
803 (when (eq type 'filename)
804 (setq value (file-name-nondirectory value)))
805 (setq string (concat string "; "
806 (mail-header-encode-parameter
807 (symbol-name type) value)))))
808 (when (not (zerop (length string)))
811 (defun mml-insert-parameter-string (cont types)
813 (while (setq type (pop types))
814 (when (setq value (cdr (assq type cont)))
815 ;; Strip directory component from the filename parameter.
816 (when (eq type 'filename)
817 (setq value (file-name-nondirectory value)))
818 (mml-insert-parameter
819 (mail-header-encode-parameter
820 (symbol-name type) value))))))
822 (defvar ange-ftp-name-format)
823 (defvar efs-path-regexp)
825 (defun mml-parse-file-name (path)
826 (if (if (boundp 'efs-path-regexp)
827 (string-match efs-path-regexp path)
828 (if (boundp 'ange-ftp-name-format)
829 (string-match (car ange-ftp-name-format) path)))
830 (list (match-string 1 path) (match-string 2 path)
831 (substring path (1+ (match-end 2))))
834 (defun mml-insert-buffer (buffer)
835 "Insert BUFFER at point and quote any MML markup."
837 (narrow-to-region (point) (point))
838 (insert-buffer-substring buffer)
839 (mml-quote-region (point-min) (point-max))
840 (goto-char (point-max))))
843 ;;; Transforming MIME to MML
846 ;; message-narrow-to-head autoloads message.
847 (declare-function message-remove-header "message"
848 (header &optional is-regexp first reverse))
850 (defun mime-to-mml (&optional handles)
851 "Translate the current buffer (which should be a message) into MML.
852 If HANDLES is non-nil, use it instead reparsing the buffer."
853 ;; First decode the head.
855 (message-narrow-to-head)
856 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
857 (mail-decode-encoded-word-region (point-min) (point-max))))
859 (setq handles (mm-dissect-buffer t)))
860 (goto-char (point-min))
861 (search-forward "\n\n" nil t)
862 (delete-region (point) (point-max))
863 (if (stringp (car handles))
864 (mml-insert-mime handles)
865 (mml-insert-mime handles t))
866 (mm-destroy-parts handles)
868 (message-narrow-to-head)
869 ;; Remove them, they are confusing.
870 (message-remove-header "Content-Type")
871 (message-remove-header "MIME-Version")
872 (message-remove-header "Content-Disposition")
873 (message-remove-header "Content-Transfer-Encoding")))
875 (autoload 'message-encode-message-body "message")
876 (declare-function message-narrow-to-headers-or-head "message" ())
878 (defun mml-to-mime ()
879 "Translate the current buffer from MML to MIME."
880 ;; `message-encode-message-body' will insert an encoded Content-Description
881 ;; header in the message header if the body contains a single part
882 ;; that is specified by a user with a MML tag containing a description
883 ;; token. So, we encode the message header first to prevent the encoded
884 ;; Content-Description header from being encoded again.
886 (message-narrow-to-headers-or-head)
887 ;; Skip past any From_ headers.