1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
32 (eval-when-compile (require 'cl))
35 (autoload 'message-make-message-id "message")
36 (autoload 'gnus-setup-posting-charset "gnus-msg")
37 (autoload 'gnus-make-local-hook "gnus-util")
38 (autoload 'message-fetch-field "message")
39 (autoload 'message-mark-active-p "message")
40 (autoload 'fill-flowed-encode "flow-fill")
41 (autoload 'message-posting-charset "message")
42 (autoload 'x-dnd-get-local-file-name "x-dnd"))
44 (defcustom mml-content-type-parameters
45 '(name access-type expiration size permission format)
46 "*A list of acceptable parameters in MML tag.
47 These parameters are generated in Content-Type header if exists."
48 :type '(repeat (symbol :tag "Parameter"))
51 (defcustom mml-content-disposition-parameters
52 '(filename creation-date modification-date read-date)
53 "*A list of acceptable parameters in MML tag.
54 These parameters are generated in Content-Disposition header if exists."
55 :type '(repeat (symbol :tag "Parameter"))
58 (defcustom mml-insert-mime-headers-always nil
59 "If non-nil, always put Content-Type: text/plain at top of empty parts.
60 It is necessary to work against a bug in certain clients."
64 (defvar mml-tweak-type-alist nil
65 "A list of (TYPE . FUNCTION) for tweaking MML parts.
66 TYPE is a string containing a regexp to match the MIME type. FUNCTION
67 is a Lisp function which is called with the MML handle to tweak the
68 part. This variable is used only when no TWEAK parameter exists in
71 (defvar mml-tweak-function-alist nil
72 "A list of (NAME . FUNCTION) for tweaking MML parts.
73 NAME is a string containing the name of the TWEAK parameter in the MML
74 handle. FUNCTION is a Lisp function which is called with the MML
75 handle to tweak the part.")
77 (defvar mml-tweak-sexp-alist
78 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
79 "A list of (SEXP . FUNCTION) for tweaking MML parts.
80 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
81 is called. FUNCTION is a Lisp function which is called with the MML
82 handle to tweak the part.")
84 (defvar mml-externalize-attachments nil
85 "*If non-nil, local-file attachments are generated as external parts.")
87 (defvar mml-generate-multipart-alist nil
88 "*Alist of multipart generation functions.
89 Each entry has the form (NAME . FUNCTION), where
90 NAME is a string containing the name of the part (without the
91 leading \"/multipart/\"),
92 FUNCTION is a Lisp function which is called to generate the part.
94 The Lisp function has to supply the appropriate MIME headers and the
95 contents of this part.")
97 (defvar mml-syntax-table
98 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
99 (modify-syntax-entry ?\\ "/" table)
100 (modify-syntax-entry ?< "(" table)
101 (modify-syntax-entry ?> ")" table)
102 (modify-syntax-entry ?@ "w" table)
103 (modify-syntax-entry ?/ "w" table)
104 (modify-syntax-entry ?= " " table)
105 (modify-syntax-entry ?* " " table)
106 (modify-syntax-entry ?\; " " table)
107 (modify-syntax-entry ?\' " " table)
110 (defvar mml-boundary-function 'mml-make-boundary
111 "A function called to suggest a boundary.
112 The function may be called several times, and should try to make a new
113 suggestion each time. The function is called with one parameter,
114 which is a number that says how many times the function has been
115 called for this message.")
117 (defvar mml-confirmation-set nil
118 "A list of symbols, each of which disables some warning.
119 `unknown-encoding': always send messages contain characters with
120 unknown encoding; `use-ascii': always use ASCII for those characters
121 with unknown encoding; `multipart': always send messages with more than
124 (defvar mml-generate-default-type "text/plain")
126 (defvar mml-buffer-list nil)
128 (defun mml-generate-new-buffer (name)
129 (let ((buf (generate-new-buffer name)))
130 (push buf mml-buffer-list)
133 (defun mml-destroy-buffers ()
134 (let (kill-buffer-hook)
135 (mapcar 'kill-buffer mml-buffer-list)
136 (setq mml-buffer-list nil)))
139 "Parse the current buffer as an MML document."
141 (goto-char (point-min))
142 (with-syntax-table mml-syntax-table
145 (defun mml-parse-1 ()
146 "Parse the current buffer as an MML document."
147 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
148 (while (and (not (eobp))
149 (not (looking-at "<#/multipart")))
151 ((looking-at "<#secure")
152 ;; The secure part is essentially a meta-meta tag, which
153 ;; expands to either a part tag if there are no other parts in
154 ;; the document or a multipart tag if there are other parts
155 ;; included in the message
157 (taginfo (mml-read-tag))
158 (recipients (cdr (assq 'recipients taginfo)))
159 (sender (cdr (assq 'sender taginfo)))
160 (location (cdr (assq 'tag-location taginfo)))
161 (mode (cdr (assq 'mode taginfo)))
162 (method (cdr (assq 'method taginfo)))
165 (if (re-search-forward
166 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
167 (setq secure-mode "multipart")
168 (setq secure-mode "part")))
171 (re-search-forward "<#secure[^\n]*>\n"))
172 (delete-region (match-beginning 0) (match-end 0))
173 (cond ((string= mode "sign")
174 (setq tags (list "sign" method)))
175 ((string= mode "encrypt")
176 (setq tags (list "encrypt" method)))
177 ((string= mode "signencrypt")
178 (setq tags (list "sign" method "encrypt" method))))
179 (eval `(mml-insert-tag ,secure-mode
181 ,(if recipients "recipients")
183 ,(if sender "sender")
186 (goto-char location)))
187 ((looking-at "<#multipart")
188 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
189 ((looking-at "<#external")
190 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
193 (if (or (looking-at "<#part") (looking-at "<#mml"))
194 (setq tag (mml-read-tag)
197 (setq tag (list 'part '(type . "text/plain"))
200 (setq raw (cdr (assq 'raw tag))
202 contents (mml-read-part (eq 'mml (car tag)))
207 (intern (downcase (cdr (assq 'charset tag))))))
209 (mm-find-mime-charset-region point (point)
211 (when (and (not raw) (memq nil charsets))
212 (if (or (memq 'unknown-encoding mml-confirmation-set)
213 (message-options-get 'unknown-encoding)
215 Message contains characters with unknown encoding. Really send? ")
216 (message-options-set 'unknown-encoding t)))
218 (or (memq 'use-ascii mml-confirmation-set)
219 (message-options-get 'use-ascii)
220 (and (y-or-n-p "Use ASCII as charset? ")
221 (message-options-set 'use-ascii t))))
222 (setq charsets (delq nil charsets))
224 (error "Edit your message to remove those characters")))
227 (< (length charsets) 2))
228 (if (or (not no-markup-p)
229 (string-match "[^ \t\r\n]" contents))
230 ;; Don't create blank parts.
231 (push (nconc tag (list (cons 'contents contents)))
233 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
234 tag point (point) use-ascii)))
236 (not (memq 'multipart mml-confirmation-set))
237 (not (message-options-get 'multipart))
238 (not (and (y-or-n-p (format "\
239 A message part needs to be split into %d charset parts. Really send? "
241 (message-options-set 'multipart t))))
242 (error "Edit your message to use only one charset"))
243 (setq struct (nconc nstruct struct)))))))
248 (defun mml-parse-singlepart-with-multiple-charsets
249 (orig-tag beg end &optional use-ascii)
252 (narrow-to-region beg end)
253 (goto-char (point-min))
254 (let ((current (or (mm-mime-charset (mm-charset-after))
255 (and use-ascii 'us-ascii)))
256 charset struct space newline paragraph)
258 (setq charset (mm-mime-charset (mm-charset-after)))
260 ;; The charset remains the same.
261 ((eq charset 'us-ascii))
262 ((or (and use-ascii (not charset))
263 (eq charset current))
267 ;; The initial charset was ascii.
268 ((eq current 'us-ascii)
269 (setq current charset
273 ;; We have a change in charsets.
277 (list (cons 'contents
278 (buffer-substring-no-properties
279 beg (or paragraph newline space (point))))))
281 (setq beg (or paragraph newline space (point))
286 ;; Compute places where it might be nice to break the part.
288 ((memq (following-char) '(? ?\t))
289 (setq space (1+ (point))))
290 ((and (eq (following-char) ?\n)
292 (eq (char-after (1- (point))) ?\n))
293 (setq paragraph (point)))
294 ((eq (following-char) ?\n)
295 (setq newline (1+ (point)))))
297 ;; Do the final part.
298 (unless (= beg (point))
299 (push (append orig-tag
300 (list (cons 'contents
301 (buffer-substring-no-properties
306 (defun mml-read-tag ()
307 "Read a tag and return the contents."
308 (let ((orig-point (point))
309 contents name elem val)
311 (setq name (buffer-substring-no-properties
312 (point) (progn (forward-sexp 1) (point))))
313 (skip-chars-forward " \t\n")
314 (while (not (looking-at ">[ \t]*\n?"))
315 (setq elem (buffer-substring-no-properties
316 (point) (progn (forward-sexp 1) (point))))
317 (skip-chars-forward "= \t\n")
318 (setq val (buffer-substring-no-properties
319 (point) (progn (forward-sexp 1) (point))))
320 (when (string-match "^\"\\(.*\\)\"$" val)
321 (setq val (match-string 1 val)))
322 (push (cons (intern elem) val) contents)
323 (skip-chars-forward " \t\n"))
324 (goto-char (match-end 0))
325 ;; Don't skip the leading space.
326 ;;(skip-chars-forward " \t\n")
327 ;; Put the tag location into the returned contents
328 (setq contents (append (list (cons 'tag-location orig-point)) contents))
329 (cons (intern name) (nreverse contents))))
331 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
332 (let ((str (buffer-substring-no-properties start end))
333 (bufstart start) tmp)
334 (while (setq tmp (text-property-any start end 'hard 't))
335 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
337 (setq start (1+ tmp)))
340 (defun mml-read-part (&optional mml)
341 "Return the buffer up till the next part, multipart or closing part or multipart.
342 If MML is non-nil, return the buffer up till the correspondent mml tag."
343 (let ((beg (point)) (count 1))
344 ;; If the tag ended at the end of the line, we go to the next line.
345 (when (looking-at "[ \t]*\n")
349 (while (and (> count 0) (not (eobp)))
350 (if (re-search-forward "<#\\(/\\)?mml." nil t)
351 (setq count (+ count (if (match-beginning 1) -1 1)))
352 (goto-char (point-max))))
353 (mml-buffer-substring-no-properties-except-hard-newlines
356 (match-beginning 0))))
357 (if (re-search-forward
358 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
360 (mml-buffer-substring-no-properties-except-hard-newlines
361 beg (match-beginning 0))
362 (if (or (not (match-beginning 1))
363 (equal (match-string 2) "multipart"))
364 (goto-char (match-beginning 0))
365 (when (looking-at "[ \t]*\n")
367 (mml-buffer-substring-no-properties-except-hard-newlines
368 beg (goto-char (point-max)))))))
370 (defvar mml-boundary nil)
371 (defvar mml-base-boundary "-=-=")
372 (defvar mml-multipart-number 0)
374 (defun mml-generate-mime ()
375 "Generate a MIME message based on the current MML document."
376 (let ((cont (mml-parse))
377 (mml-multipart-number mml-multipart-number))
381 (if (and (consp (car cont))
383 (mml-generate-mime-1 (car cont))
384 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
388 (defun mml-generate-mime-1 (cont)
389 (let ((mm-use-ultra-safe-encoding
390 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
392 (narrow-to-region (point) (point))
393 (mml-tweak-part cont)
395 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
396 (let ((raw (cdr (assq 'raw cont)))
397 coded encoding charset filename type flowed)
398 (setq type (or (cdr (assq 'type cont)) "text/plain"))
400 (member (car (split-string type "/")) '("text" "message")))
403 (setq charset (mm-charset-to-coding-system
404 (cdr (assq 'charset cont))))
405 (when (eq charset 'ascii)
408 ((cdr (assq 'buffer cont))
409 (insert-buffer-substring (cdr (assq 'buffer cont))))
410 ((and (setq filename (cdr (assq 'filename cont)))
411 (not (equal (cdr (assq 'nofile cont)) "yes")))
412 (let ((coding-system-for-read charset))
413 (mm-insert-file-contents filename)))
414 ((eq 'mml (car cont))
415 (insert (cdr (assq 'contents cont))))
418 (narrow-to-region (point) (point))
419 (insert (cdr (assq 'contents cont)))
420 ;; Remove quotes from quoted tags.
421 (goto-char (point-min))
422 (while (re-search-forward
423 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
425 (delete-region (+ (match-beginning 0) 2)
426 (+ (match-beginning 0) 3))))))
428 ((eq (car cont) 'mml)
429 (let ((mml-boundary (mml-compute-boundary cont))
430 (mml-generate-default-type "text/plain"))
432 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
433 ;; ignore 0x1b, it is part of iso-2022-jp
434 (setq encoding (mm-body-7-or-8))))
435 ((string= (car (split-string type "/")) "message")
436 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
437 ;; ignore 0x1b, it is part of iso-2022-jp
438 (setq encoding (mm-body-7-or-8))))
440 ;; Only perform format=flowed filling on text/plain
441 ;; parts where there either isn't a format parameter
442 ;; in the mml tag or it says "flowed" and there
443 ;; actually are hard newlines in the text.
444 (let (use-hard-newlines)
445 (when (and (string= type "text/plain")
446 (not (string= (cdr (assq 'sign cont)) "pgp"))
447 (or (null (assq 'format cont))
448 (string= (cdr (assq 'format cont))
450 (setq use-hard-newlines
452 (point-min) (point-max) 'hard 't)))
454 ;; Indicate that `mml-insert-mime-headers' should
455 ;; insert a "; format=flowed" string unless the
456 ;; user has already specified it.
457 (setq flowed (null (assq 'format cont)))))
458 (setq charset (mm-encode-body charset))
459 (setq encoding (mm-body-encoding
460 charset (cdr (assq 'encoding cont))))))
461 (setq coded (buffer-string)))
462 (mml-insert-mime-headers cont type charset encoding flowed)
465 (mm-with-unibyte-buffer
467 ((cdr (assq 'buffer cont))
468 (insert-buffer-substring (cdr (assq 'buffer cont))))
469 ((and (setq filename (cdr (assq 'filename cont)))
470 (not (equal (cdr (assq 'nofile cont)) "yes")))
471 (let ((coding-system-for-read mm-binary-coding-system))
472 (mm-insert-file-contents filename nil nil nil nil t)))
474 (insert (cdr (assq 'contents cont)))))
475 (setq encoding (mm-encode-buffer type)
476 coded (mm-string-as-multibyte (buffer-string))))
477 (mml-insert-mime-headers cont type charset encoding nil)
479 (mm-with-unibyte-current-buffer
481 ((eq (car cont) 'external)
482 (insert "Content-Type: message/external-body")
483 (let ((parameters (mml-parameter-string
484 cont '(expiration size permission)))
485 (name (cdr (assq 'name cont)))
486 (url (cdr (assq 'url cont))))
488 (setq name (mml-parse-file-name name))
490 (mml-insert-parameter
491 (mail-header-encode-parameter "name" name)
492 "access-type=local-file")
493 (mml-insert-parameter
494 (mail-header-encode-parameter
495 "name" (file-name-nondirectory (nth 2 name)))
496 (mail-header-encode-parameter "site" (nth 1 name))
497 (mail-header-encode-parameter
498 "directory" (file-name-directory (nth 2 name))))
499 (mml-insert-parameter
500 (concat "access-type="
501 (if (member (nth 0 name) '("ftp@" "anonymous@"))
505 (mml-insert-parameter
506 (mail-header-encode-parameter "url" url)
509 (mml-insert-parameter-string
510 cont '(expiration size permission))))
512 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
513 (insert "Content-ID: " (message-make-message-id) "\n")
514 (insert "Content-Transfer-Encoding: "
515 (or (cdr (assq 'encoding cont)) "binary"))
517 (insert (or (cdr (assq 'contents cont))))
519 ((eq (car cont) 'multipart)
520 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
521 (mml-generate-default-type (if (equal type "digest")
524 (handler (assoc type mml-generate-multipart-alist)))
526 (funcall (cdr handler) cont)
527 ;; No specific handler. Use default one.
528 (let ((mml-boundary (mml-compute-boundary cont)))
529 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
531 (if (cdr (assq 'start cont))
532 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
534 (let ((cont cont) part)
535 (while (setq part (pop cont))
536 ;; Skip `multipart' and attributes.
537 (when (and (consp part) (consp (cdr part)))
538 (insert "\n--" mml-boundary "\n")
539 (mml-generate-mime-1 part))))
540 (insert "\n--" mml-boundary "--\n")))))
542 (error "Invalid element: %S" cont)))
543 ;; handle sign & encrypt tags in a semi-smart way.
544 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
545 (encrypt-item (assoc (cdr (assq 'encrypt cont))
548 (when (or sign-item encrypt-item)
549 (when (setq sender (cdr (assq 'sender cont)))
550 (message-options-set 'mml-sender sender)
551 (message-options-set 'message-sender sender))
552 (if (setq recipients (cdr (assq 'recipients cont)))
553 (message-options-set 'message-recipients recipients))
554 (let ((style (mml-signencrypt-style
555 (first (or sign-item encrypt-item)))))
556 ;; check if: we're both signing & encrypting, both methods
557 ;; are the same (why would they be different?!), and that
558 ;; the signencrypt style allows for combined operation.
559 (if (and sign-item encrypt-item (equal (first sign-item)
560 (first encrypt-item))
561 (equal style 'combined))
562 (funcall (nth 1 encrypt-item) cont t)
563 ;; otherwise, revert to the old behavior.
565 (funcall (nth 1 sign-item) cont))
567 (funcall (nth 1 encrypt-item) cont)))))))))
569 (defun mml-compute-boundary (cont)
570 "Return a unique boundary that does not exist in CONT."
571 (let ((mml-boundary (funcall mml-boundary-function
572 (incf mml-multipart-number))))
573 ;; This function tries again and again until it has found
574 ;; a unique boundary.
575 (while (not (catch 'not-unique
576 (mml-compute-boundary-1 cont))))
579 (defun mml-compute-boundary-1 (cont)
582 ((eq (car cont) 'part)
585 ((cdr (assq 'buffer cont))
586 (insert-buffer-substring (cdr (assq 'buffer cont))))
587 ((and (setq filename (cdr (assq 'filename cont)))
588 (not (equal (cdr (assq 'nofile cont)) "yes")))
589 (mm-insert-file-contents filename nil nil nil nil t))
591 (insert (cdr (assq 'contents cont)))))
592 (goto-char (point-min))
593 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
595 (setq mml-boundary (funcall mml-boundary-function
596 (incf mml-multipart-number)))
597 (throw 'not-unique nil))))
598 ((eq (car cont) 'multipart)
599 (mapcar 'mml-compute-boundary-1 (cddr cont))))
602 (defun mml-make-boundary (number)
603 (concat (make-string (% number 60) ?=)
609 (defun mml-insert-mime-headers (cont type charset encoding flowed)
610 (let (parameters id disposition description)
612 (mml-parameter-string
613 cont mml-content-type-parameters))
617 (not (equal type mml-generate-default-type))
618 mml-insert-mime-headers-always)
619 (when (consp charset)
621 "Can't encode a part with several charsets"))
622 (insert "Content-Type: " type)
624 (insert "; " (mail-header-encode-parameter
625 "charset" (symbol-name charset))))
627 (insert "; format=flowed"))
629 (mml-insert-parameter-string
630 cont mml-content-type-parameters))
632 (when (setq id (cdr (assq 'id cont)))
633 (insert "Content-ID: " id "\n"))
635 (mml-parameter-string
636 cont mml-content-disposition-parameters))
637 (when (or (setq disposition (cdr (assq 'disposition cont)))
639 (insert "Content-Disposition: " (or disposition "inline"))
641 (mml-insert-parameter-string
642 cont mml-content-disposition-parameters))
644 (unless (eq encoding '7bit)
645 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
646 (when (setq description (cdr (assq 'description cont)))
647 (insert "Content-Description: "
648 (mail-encode-encoded-word-string description) "\n"))))
650 (defun mml-parameter-string (cont types)
653 (while (setq type (pop types))
654 (when (setq value (cdr (assq type cont)))
655 ;; Strip directory component from the filename parameter.
656 (when (eq type 'filename)
657 (setq value (file-name-nondirectory value)))
658 (setq string (concat string "; "
659 (mail-header-encode-parameter
660 (symbol-name type) value)))))
661 (when (not (zerop (length string)))
664 (defun mml-insert-parameter-string (cont types)
666 (while (setq type (pop types))
667 (when (setq value (cdr (assq type cont)))
668 ;; Strip directory component from the filename parameter.
669 (when (eq type 'filename)
670 (setq value (file-name-nondirectory value)))
671 (mml-insert-parameter
672 (mail-header-encode-parameter
673 (symbol-name type) value))))))
676 (defvar ange-ftp-name-format)
677 (defvar efs-path-regexp))
678 (defun mml-parse-file-name (path)
679 (if (if (boundp 'efs-path-regexp)
680 (string-match efs-path-regexp path)
681 (if (boundp 'ange-ftp-name-format)
682 (string-match (car ange-ftp-name-format) path)))
683 (list (match-string 1 path) (match-string 2 path)
684 (substring path (1+ (match-end 2))))
687 (defun mml-insert-buffer (buffer)
688 "Insert BUFFER at point and quote any MML markup."
690 (narrow-to-region (point) (point))
691 (insert-buffer-substring buffer)
692 (mml-quote-region (point-min) (point-max))
693 (goto-char (point-max))))
696 ;;; Transforming MIME to MML
699 (defun mime-to-mml (&optional handles)
700 "Translate the current buffer (which should be a message) into MML.
701 If HANDLES is non-nil, use it instead reparsing the buffer."
702 ;; First decode the head.
704 (message-narrow-to-head)
705 (mail-decode-encoded-word-region (point-min) (point-max)))
707 (setq handles (mm-dissect-buffer t)))
708 (goto-char (point-min))
709 (search-forward "\n\n" nil t)
710 (delete-region (point) (point-max))
711 (if (stringp (car handles))
712 (mml-insert-mime handles)
713 (mml-insert-mime handles t))
714 (mm-destroy-parts handles)
716 (message-narrow-to-head)
717 ;; Remove them, they are confusing.
718 (message-remove-header "Content-Type")
719 (message-remove-header "MIME-Version")
720 (message-remove-header "Content-Disposition")
721 (message-remove-header "Content-Transfer-Encoding")))
723 (defun mml-to-mime ()
724 "Translate the current buffer from MML to MIME."
725 (message-encode-message-body)
727 (message-narrow-to-headers-or-head)
728 ;; Skip past any From_ headers.
729 (while (looking-at "From ")
731 (let ((mail-parse-charset message-default-charset))
732 (mail-encode-encoded-word-buffer))))
734 (defun mml-insert-mime (handle &optional no-markup)
735 (let (textp buffer mmlp)
736 ;; Determine type and stuff.
737 (unless (stringp (car handle))
738 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
740 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
741 (mm-insert-part handle)
742 (if (setq mmlp (equal (mm-handle-media-type handle)
746 (mml-insert-mml-markup handle nil t t)
747 (unless (and no-markup
748 (equal (mm-handle-media-type handle) "text/plain"))
749 (mml-insert-mml-markup handle buffer textp)))
752 (insert-buffer-substring buffer)
753 (goto-char (point-max))
754 (insert "<#/mml>\n"))
755 ((stringp (car handle))
756 (mapcar 'mml-insert-mime (cdr handle))
757 (insert "<#/multipart>\n"))
759 (let ((charset (mail-content-type-get
760 (mm-handle-type handle) 'charset))
762 (if (eq charset 'gnus-decoded)
763 (mm-insert-part handle)
764 (insert (mm-decode-string (mm-get-part handle) charset)))
765 (mml-quote-region start (point)))
766 (goto-char (point-max)))
768 (insert "<#/part>\n")))))
770 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
771 "Take a MIME handle and insert an MML tag."
772 (if (stringp (car handle))
774 (insert "<#multipart type=" (mm-handle-media-subtype handle))
775 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
777 (insert " start=\"" start "\"")))
780 (insert "<#mml type=" (mm-handle-media-type handle))
781 (insert "<#part type=" (mm-handle-media-type handle)))
782 (dolist (elem (append (cdr (mm-handle-type handle))
783 (cdr (mm-handle-disposition handle))))
784 (unless (symbolp (cdr elem))
785 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
786 (when (mm-handle-id handle)
787 (insert " id=\"" (mm-handle-id handle) "\""))
788 (when (mm-handle-disposition handle)
789 (insert " disposition=" (car (mm-handle-disposition handle))))
791 (insert " buffer=\"" (buffer-name buffer) "\""))
793 (insert " nofile=yes"))
794 (when (mm-handle-description handle)
795 (insert " description=\"" (mm-handle-description handle) "\""))
798 (defun mml-insert-parameter (&rest parameters)
799 "Insert PARAMETERS in a nice way."
800 (dolist (param parameters)
802 (let ((point (point)))
804 (when (> (current-column) 71)
810 ;;; Mode for inserting and editing MML forms
814 (let ((sign (make-sparse-keymap))
815 (encrypt (make-sparse-keymap))
816 (signpart (make-sparse-keymap))
817 (encryptpart (make-sparse-keymap))
818 (map (make-sparse-keymap))
819 (main (make-sparse-keymap)))
820 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
821 (define-key sign "o" 'mml-secure-message-sign-pgp)
822 (define-key sign "s" 'mml-secure-message-sign-smime)
823 (define-key signpart "p" 'mml-secure-sign-pgpmime)
824 (define-key signpart "o" 'mml-secure-sign-pgp)
825 (define-key signpart "s" 'mml-secure-sign-smime)
826 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
827 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
828 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
829 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
830 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
831 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
832 (define-key map "\C-n" 'mml-unsecure-message)
833 (define-key map "f" 'mml-attach-file)
834 (define-key map "b" 'mml-attach-buffer)
835 (define-key map "e" 'mml-attach-external)
836 (define-key map "q" 'mml-quote-region)
837 (define-key map "m" 'mml-insert-multipart)
838 (define-key map "p" 'mml-insert-part)
839 (define-key map "v" 'mml-validate)
840 (define-key map "P" 'mml-preview)
841 (define-key map "s" sign)
842 (define-key map "S" signpart)
843 (define-key map "c" encrypt)
844 (define-key map "C" encryptpart)
845 ;;(define-key map "n" 'mml-narrow-to-part)
846 ;; `M-m' conflicts with `back-to-indentation'.
847 ;; (define-key main "\M-m" map)
848 (define-key main "\C-c\C-m" map)
852 mml-menu mml-mode-map ""
854 ["Attach File..." mml-attach-file
855 ,@(if (featurep 'xemacs) '(t)
856 '(:help "Attach a file at point"))]
857 ["Attach Buffer..." mml-attach-buffer t]
858 ["Attach External..." mml-attach-external t]
859 ["Insert Part..." mml-insert-part t]
860 ["Insert Multipart..." mml-insert-multipart t]
861 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
862 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
863 ["PGP Sign" mml-secure-message-sign-pgp t]
864 ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
865 ["S/MIME Sign" mml-secure-message-sign-smime t]
866 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
868 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
869 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
870 ["PGP Sign Part" mml-secure-sign-pgp t]
871 ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
872 ["S/MIME Sign Part" mml-secure-sign-smime t]
873 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
874 ["Encrypt/Sign off" mml-unsecure-message t]
875 ;;["Narrow" mml-narrow-to-part t]
876 ["Quote MML" mml-quote-region
877 :active (message-mark-active-p)
878 ,@(if (featurep 'xemacs) nil
879 '(:help "Quote MML tags in region"))]
880 ["Validate MML" mml-validate t]
881 ["Preview" mml-preview t]))
884 "Minor mode for editing MML.")
886 (defun mml-mode (&optional arg)
887 "Minor mode for editing MML.
888 MML is the MIME Meta Language, a minor mode for composing MIME articles.
889 See Info node `(emacs-mime)Composing'.
893 (when (set (make-local-variable 'mml-mode)
894 (if (null arg) (not mml-mode)
895 (> (prefix-numeric-value arg) 0)))
896 (add-minor-mode 'mml-mode " MML" mml-mode-map)
897 (easy-menu-add mml-menu mml-mode-map)
898 (when (boundp 'x-dnd-protocol-alist)
899 (set (make-local-variable 'x-dnd-protocol-alist)
900 '(("^file:///" . mml-x-dnd-attach-file)
901 ("^file://" . x-dnd-open-file)
902 ("^file:" . mml-x-dnd-attach-file))))
903 (run-hooks 'mml-mode-hook)))
906 ;;; Helper functions for reading MIME stuff from the minibuffer and
907 ;;; inserting stuff to the buffer.
910 (defun mml-minibuffer-read-file (prompt)
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
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))
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
930 "application/octet-stream"))
931 (string (completing-read
932 (format "Content type (default %s): " default)
933 (mapcar 'list (mailcap-mime-types)))))
934 (if (not (equal string ""))
938 (defun mml-minibuffer-read-description ()
939 (let ((description (read-string "One line description: ")))
940 (when (string-match "\\`[ \t]*\\'" description)
941 (setq description nil))
944 (defun mml-minibuffer-read-disposition (type &optional default)
945 (let* ((default (or default
946 (if (string-match "^text/.*" type)
949 (disposition (completing-read
950 (format "Disposition: (default %s): " default)
951 '(("attachment") ("inline") (""))
954 (if (not (equal disposition ""))
958 (defun mml-quote-region (beg end)
959 "Quote the MML tags in the region."
963 ;; Temporarily narrow the region to defend from changes
965 (narrow-to-region beg end)
966 (goto-char (point-min))
968 (while (re-search-forward
969 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
970 ;; Insert ! after the #.
971 (goto-char (+ (match-beginning 0) 2))
974 (defun mml-insert-tag (name &rest plist)
975 "Insert an MML tag described by NAME and PLIST."
977 (setq name (symbol-name name)))
980 (let ((key (pop plist))
983 ;; Quote VALUE if it contains suspicious characters.
984 (when (string-match "[\"'\\~/*;() \t\n]" value)
985 (setq value (with-output-to-string
986 (let (print-escape-nonascii)
988 (insert (format " %s=%s" key value)))))
991 (defun mml-insert-empty-tag (name &rest plist)
992 "Insert an empty MML tag described by NAME and PLIST."
994 (setq name (symbol-name name)))
995 (apply #'mml-insert-tag name plist)
996 (insert "<#/" name ">\n"))
998 ;;; Attachment functions.
1000 (defun mml-attach-file (file &optional type description disposition)
1001 "Attach a file to the outgoing MIME message.
1002 The file is not inserted or encoded until you send the message with
1003 `\\[message-send-and-exit]' or `\\[message-send]'.
1005 FILE is the name of the file to attach. TYPE is its content-type, a
1006 string of the form \"type/subtype\". DESCRIPTION is a one-line
1007 description of the attachment."
1009 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1010 (type (mml-minibuffer-read-type file))
1011 (description (mml-minibuffer-read-description))
1012 (disposition (mml-minibuffer-read-disposition type)))
1013 (list file type description disposition)))
1014 (mml-insert-empty-tag 'part
1017 'disposition (or disposition "attachment")
1018 'description description))
1020 (defun mml-x-dnd-attach-file (uri action)
1021 "Attach a drag and drop file."
1022 (let ((file (x-dnd-get-local-file-name uri t)))
1023 (when (and file (file-regular-p file))
1024 (let* ((type (mml-minibuffer-read-type file))
1025 (description (mml-minibuffer-read-description))
1026 (disposition (mml-minibuffer-read-disposition type)))
1027 (mml-attach-file file type description disposition)))))
1029 (defun mml-attach-buffer (buffer &optional type description)
1030 "Attach a buffer to the outgoing MIME message.
1031 See `mml-attach-file' for details of operation."
1033 (let* ((buffer (read-buffer "Attach buffer: "))
1034 (type (mml-minibuffer-read-type buffer "text/plain"))
1035 (description (mml-minibuffer-read-description)))
1036 (list buffer type description)))
1037 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1038 'disposition "attachment" 'description description))
1040 (defun mml-attach-external (file &optional type description)
1041 "Attach an external file into the buffer.
1042 FILE is an ange-ftp/efs specification of the part location.
1043 TYPE is the MIME type to use."
1045 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1046 (type (mml-minibuffer-read-type file))
1047 (description (mml-minibuffer-read-description)))
1048 (list file type description)))
1049 (mml-insert-empty-tag 'external 'type type 'name file
1050 'disposition "attachment" 'description description))
1052 (defun mml-insert-multipart (&optional type)
1053 (interactive (list (completing-read "Multipart type (default mixed): "
1054 '(("mixed") ("alternative") ("digest") ("parallel")
1055 ("signed") ("encrypted"))
1058 (setq type "mixed"))
1059 (mml-insert-empty-tag "multipart" 'type type)
1062 (defun mml-insert-part (&optional type)
1064 (list (mml-minibuffer-read-type "")))
1065 (mml-insert-tag 'part 'type type 'disposition "inline")
1068 (defun mml-preview-insert-mail-followup-to ()
1069 "Insert a Mail-Followup-To header before previewing an article.
1070 Should be adopted if code in `message-send-mail' is changed."
1071 (when (and (message-mail-p)
1072 (message-subscribed-p)
1073 (not (mail-fetch-field "mail-followup-to"))
1074 (message-make-mail-followup-to))
1075 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1076 (insert (message-make-mail-followup-to))))
1078 (defun mml-preview (&optional raw)
1079 "Display current buffer with Gnus, in a new buffer.
1080 If RAW, don't highlight the article."
1083 (let* ((buf (current-buffer))
1084 (message-options message-options)
1085 (message-this-is-mail (message-mail-p))
1086 (message-this-is-news (message-news-p))
1087 (message-posting-charset (or (gnus-setup-posting-charset
1089 (message-narrow-to-headers-or-head)
1090 (message-fetch-field "Newsgroups")))
1091 message-posting-charset)))
1092 (message-options-set-recipient)
1093 (switch-to-buffer (generate-new-buffer
1094 (concat (if raw "*Raw MIME preview of "
1095 "*MIME preview of ") (buffer-name))))
1096 (when (boundp 'gnus-buffers)
1097 (push (current-buffer) gnus-buffers))
1099 (insert-buffer-substring buf)
1100 (mml-preview-insert-mail-followup-to)
1101 (let ((message-deletable-headers (if (message-news-p)
1103 message-deletable-headers)))
1104 (message-generate-headers
1105 (copy-sequence (if (message-news-p)
1106 message-required-news-headers
1107 message-required-mail-headers))))
1108 (if (re-search-forward
1109 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1110 (replace-match "\n"))
1111 (let ((mail-header-separator ""));; mail-header-separator is removed.
1114 (when (fboundp 'set-buffer-multibyte)
1115 (let ((s (buffer-string)))
1116 ;; Insert the content into unibyte buffer.
1118 (mm-disable-multibyte)
1120 (let ((gnus-newsgroup-charset (car message-posting-charset))
1121 gnus-article-prepare-hook gnus-original-article-buffer)
1122 (run-hooks 'gnus-article-decode-hook)
1123 (let ((gnus-newsgroup-name "dummy")
1124 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1125 (gnus-make-hashtable 5))))
1126 (gnus-article-prepare-display))))
1127 ;; Disable article-mode-map.
1129 (gnus-make-local-hook 'kill-buffer-hook)
1130 (add-hook 'kill-buffer-hook
1132 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1133 (setq buffer-read-only t)
1134 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1135 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1139 (widget-button-press (point))))
1140 (local-set-key gnus-mouse-2
1143 (widget-button-press (widget-event-point event) event)))
1144 (goto-char (point-min)))))
1146 (defun mml-validate ()
1147 "Validate the current MML document."
1151 (defun mml-tweak-part (cont)
1153 (let ((tweak (cdr (assq 'tweak cont)))
1158 (or (cdr (assoc tweak mml-tweak-function-alist))
1160 (mml-tweak-type-alist
1161 (let ((alist mml-tweak-type-alist)
1162 (type (or (cdr (assq 'type cont)) "text/plain")))
1164 (if (string-match (caar alist) type)
1165 (setq func (cdar alist)
1167 (setq alist (cdr alist)))))))
1171 (let ((alist mml-tweak-sexp-alist))
1173 (if (eval (caar alist))
1174 (funcall (cdar alist) cont))
1175 (setq alist (cdr alist)))))
1178 (defun mml-tweak-externalize-attachments (cont)
1179 "Tweak attached files as external parts."
1180 (let (filename-cons)
1181 (when (and (eq (car cont) 'part)
1182 (not (cdr (assq 'buffer cont)))
1183 (and (setq filename-cons (assq 'filename cont))
1184 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1185 (setcar cont 'external)
1186 (setcar filename-cons 'name))))
1190 ;;; mml.el ends here