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-add-minor-mode "gnus-ems")
38 (autoload 'gnus-make-local-hook "gnus-util")
39 (autoload 'message-fetch-field "message")
40 (autoload 'fill-flowed-encode "flow-fill")
41 (autoload 'message-posting-charset "message"))
43 (defcustom mml-content-type-parameters
44 '(name access-type expiration size permission format)
45 "*A list of acceptable parameters in MML tag.
46 These parameters are generated in Content-Type header if exists."
47 :type '(repeat (symbol :tag "Parameter"))
50 (defcustom mml-content-disposition-parameters
51 '(filename creation-date modification-date read-date)
52 "*A list of acceptable parameters in MML tag.
53 These parameters are generated in Content-Disposition header if exists."
54 :type '(repeat (symbol :tag "Parameter"))
57 (defcustom mml-insert-mime-headers-always nil
58 "If non-nil, always put Content-Type: text/plain at top of empty parts.
59 It is necessary to work against a bug in certain clients."
63 (defvar mml-tweak-type-alist nil
64 "A list of (TYPE . FUNCTION) for tweaking MML parts.
65 TYPE is a string containing a regexp to match the MIME type. FUNCTION
66 is a Lisp function which is called with the MML handle to tweak the
67 part. This variable is used only when no TWEAK parameter exists in
70 (defvar mml-tweak-function-alist nil
71 "A list of (NAME . FUNCTION) for tweaking MML parts.
72 NAME is a string containing the name of the TWEAK parameter in the MML
73 handle. FUNCTION is a Lisp function which is called with the MML
74 handle to tweak the part.")
76 (defvar mml-tweak-sexp-alist
77 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
78 "A list of (SEXP . FUNCTION) for tweaking MML parts.
79 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
80 is called. FUNCTION is a Lisp function which is called with the MML
81 handle to tweak the part.")
83 (defvar mml-externalize-attachments nil
84 "*If non-nil, local-file attachments are generated as external parts.")
86 (defvar mml-generate-multipart-alist nil
87 "*Alist of multipart generation functions.
88 Each entry has the form (NAME . FUNCTION), where
89 NAME is a string containing the name of the part (without the
90 leading \"/multipart/\"),
91 FUNCTION is a Lisp function which is called to generate the part.
93 The Lisp function has to supply the appropriate MIME headers and the
94 contents of this part.")
96 (defvar mml-syntax-table
97 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
98 (modify-syntax-entry ?\\ "/" table)
99 (modify-syntax-entry ?< "(" table)
100 (modify-syntax-entry ?> ")" table)
101 (modify-syntax-entry ?@ "w" table)
102 (modify-syntax-entry ?/ "w" table)
103 (modify-syntax-entry ?= " " table)
104 (modify-syntax-entry ?* " " table)
105 (modify-syntax-entry ?\; " " table)
106 (modify-syntax-entry ?\' " " table)
109 (defvar mml-boundary-function 'mml-make-boundary
110 "A function called to suggest a boundary.
111 The function may be called several times, and should try to make a new
112 suggestion each time. The function is called with one parameter,
113 which is a number that says how many times the function has been
114 called for this message.")
116 (defvar mml-confirmation-set nil
117 "A list of symbols, each of which disables some warning.
118 `unknown-encoding': always send messages contain characters with
119 unknown encoding; `use-ascii': always use ASCII for those characters
120 with unknown encoding; `multipart': always send messages with more than
123 (defvar mml-generate-default-type "text/plain")
125 (defvar mml-buffer-list nil)
127 (defun mml-generate-new-buffer (name)
128 (let ((buf (generate-new-buffer name)))
129 (push buf mml-buffer-list)
132 (defun mml-destroy-buffers ()
133 (let (kill-buffer-hook)
134 (mapcar 'kill-buffer mml-buffer-list)
135 (setq mml-buffer-list nil)))
138 "Parse the current buffer as an MML document."
140 (goto-char (point-min))
141 (let ((table (syntax-table)))
144 (set-syntax-table mml-syntax-table)
146 (set-syntax-table table)))))
148 (defun mml-parse-1 ()
149 "Parse the current buffer as an MML document."
150 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
151 (while (and (not (eobp))
152 (not (looking-at "<#/multipart")))
154 ((looking-at "<#secure")
155 ;; The secure part is essentially a meta-meta tag, which
156 ;; expands to either a part tag if there are no other parts in
157 ;; the document or a multipart tag if there are other parts
158 ;; included in the message
160 (taginfo (mml-read-tag))
161 (recipients (cdr (assq 'recipients taginfo)))
162 (sender (cdr (assq 'sender taginfo)))
163 (location (cdr (assq 'tag-location taginfo)))
164 (mode (cdr (assq 'mode taginfo)))
165 (method (cdr (assq 'method taginfo)))
170 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
171 (setq secure-mode "multipart")
172 (setq secure-mode "part")))
175 (re-search-forward "<#secure[^\n]*>\n"))
176 (delete-region (match-beginning 0) (match-end 0))
177 (cond ((string= mode "sign")
178 (setq tags (list "sign" method)))
179 ((string= mode "encrypt")
180 (setq tags (list "encrypt" method)))
181 ((string= mode "signencrypt")
182 (setq tags (list "sign" method "encrypt" method))))
183 (eval `(mml-insert-tag ,secure-mode
185 ,(if recipients "recipients")
187 ,(if sender "sender")
190 (goto-char location)))
191 ((looking-at "<#multipart")
192 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
193 ((looking-at "<#external")
194 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
197 (if (or (looking-at "<#part") (looking-at "<#mml"))
198 (setq tag (mml-read-tag)
201 (setq tag (list 'part '(type . "text/plain"))
204 (setq raw (cdr (assq 'raw tag))
206 contents (mml-read-part (eq 'mml (car tag)))
211 (intern (downcase (cdr (assq 'charset tag))))))
213 (mm-find-mime-charset-region point (point)
215 (when (and (not raw) (memq nil charsets))
216 (if (or (memq 'unknown-encoding mml-confirmation-set)
217 (message-options-get 'unknown-encoding)
219 Message contains characters with unknown encoding. Really send? ")
220 (message-options-set 'unknown-encoding t)))
222 (or (memq 'use-ascii mml-confirmation-set)
223 (message-options-get 'use-ascii)
224 (and (y-or-n-p "Use ASCII as charset? ")
225 (message-options-set 'use-ascii t))))
226 (setq charsets (delq nil charsets))
228 (error "Edit your message to remove those characters")))
231 (< (length charsets) 2))
232 (if (or (not no-markup-p)
233 (string-match "[^ \t\r\n]" contents))
234 ;; Don't create blank parts.
235 (push (nconc tag (list (cons 'contents contents)))
237 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
238 tag point (point) use-ascii)))
240 (not (memq 'multipart mml-confirmation-set))
241 (not (message-options-get 'multipart))
242 (not (and (y-or-n-p (format "\
243 A message part needs to be split into %d charset parts. Really send? "
245 (message-options-set 'multipart t))))
246 (error "Edit your message to use only one charset"))
247 (setq struct (nconc nstruct struct)))))))
252 (defun mml-parse-singlepart-with-multiple-charsets
253 (orig-tag beg end &optional use-ascii)
256 (narrow-to-region beg end)
257 (goto-char (point-min))
258 (let ((current (or (mm-mime-charset (mm-charset-after))
259 (and use-ascii 'us-ascii)))
260 charset struct space newline paragraph)
262 (setq charset (mm-mime-charset (mm-charset-after)))
264 ;; The charset remains the same.
265 ((eq charset 'us-ascii))
266 ((or (and use-ascii (not charset))
267 (eq charset current))
271 ;; The initial charset was ascii.
272 ((eq current 'us-ascii)
273 (setq current charset
277 ;; We have a change in charsets.
281 (list (cons 'contents
282 (buffer-substring-no-properties
283 beg (or paragraph newline space (point))))))
285 (setq beg (or paragraph newline space (point))
290 ;; Compute places where it might be nice to break the part.
292 ((memq (following-char) '(? ?\t))
293 (setq space (1+ (point))))
294 ((and (eq (following-char) ?\n)
296 (eq (char-after (1- (point))) ?\n))
297 (setq paragraph (point)))
298 ((eq (following-char) ?\n)
299 (setq newline (1+ (point)))))
301 ;; Do the final part.
302 (unless (= beg (point))
303 (push (append orig-tag
304 (list (cons 'contents
305 (buffer-substring-no-properties
310 (defun mml-read-tag ()
311 "Read a tag and return the contents."
312 (let ((orig-point (point))
313 contents name elem val)
315 (setq name (buffer-substring-no-properties
316 (point) (progn (forward-sexp 1) (point))))
317 (skip-chars-forward " \t\n")
318 (while (not (looking-at ">[ \t]*\n?"))
319 (setq elem (buffer-substring-no-properties
320 (point) (progn (forward-sexp 1) (point))))
321 (skip-chars-forward "= \t\n")
322 (setq val (buffer-substring-no-properties
323 (point) (progn (forward-sexp 1) (point))))
324 (when (string-match "^\"\\(.*\\)\"$" val)
325 (setq val (match-string 1 val)))
326 (push (cons (intern elem) val) contents)
327 (skip-chars-forward " \t\n"))
328 (goto-char (match-end 0))
329 ;; Don't skip the leading space.
330 ;;(skip-chars-forward " \t\n")
331 ;; Put the tag location into the returned contents
332 (setq contents (append (list (cons 'tag-location orig-point)) contents))
333 (cons (intern name) (nreverse contents))))
335 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
336 (let ((str (buffer-substring-no-properties start end))
337 (bufstart start) tmp)
338 (while (setq tmp (text-property-any start end 'hard 't))
339 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
341 (setq start (1+ tmp)))
344 (defun mml-read-part (&optional mml)
345 "Return the buffer up till the next part, multipart or closing part or multipart.
346 If MML is non-nil, return the buffer up till the correspondent mml tag."
347 (let ((beg (point)) (count 1))
348 ;; If the tag ended at the end of the line, we go to the next line.
349 (when (looking-at "[ \t]*\n")
353 (while (and (> count 0) (not (eobp)))
354 (if (re-search-forward "<#\\(/\\)?mml." nil t)
355 (setq count (+ count (if (match-beginning 1) -1 1)))
356 (goto-char (point-max))))
357 (mml-buffer-substring-no-properties-except-hard-newlines
360 (match-beginning 0))))
361 (if (re-search-forward
362 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
364 (mml-buffer-substring-no-properties-except-hard-newlines
365 beg (match-beginning 0))
366 (if (or (not (match-beginning 1))
367 (equal (match-string 2) "multipart"))
368 (goto-char (match-beginning 0))
369 (when (looking-at "[ \t]*\n")
371 (mml-buffer-substring-no-properties-except-hard-newlines
372 beg (goto-char (point-max)))))))
374 (defvar mml-boundary nil)
375 (defvar mml-base-boundary "-=-=")
376 (defvar mml-multipart-number 0)
378 (defun mml-generate-mime ()
379 "Generate a MIME message based on the current MML document."
380 (let ((cont (mml-parse))
381 (mml-multipart-number mml-multipart-number))
385 (if (and (consp (car cont))
387 (mml-generate-mime-1 (car cont))
388 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
392 (defun mml-generate-mime-1 (cont)
393 (let ((mm-use-ultra-safe-encoding
394 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
396 (narrow-to-region (point) (point))
397 (mml-tweak-part cont)
399 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
400 (let ((raw (cdr (assq 'raw cont)))
401 coded encoding charset filename type flowed)
402 (setq type (or (cdr (assq 'type cont)) "text/plain"))
404 (member (car (split-string type "/")) '("text" "message")))
407 (setq charset (mm-charset-to-coding-system
408 (cdr (assq 'charset cont))))
409 (when (eq charset 'ascii)
412 ((cdr (assq 'buffer cont))
413 (insert-buffer-substring (cdr (assq 'buffer cont))))
414 ((and (setq filename (cdr (assq 'filename cont)))
415 (not (equal (cdr (assq 'nofile cont)) "yes")))
416 (let ((coding-system-for-read charset))
417 (mm-insert-file-contents filename)))
418 ((eq 'mml (car cont))
419 (insert (cdr (assq 'contents cont))))
422 (narrow-to-region (point) (point))
423 (insert (cdr (assq 'contents cont)))
424 ;; Remove quotes from quoted tags.
425 (goto-char (point-min))
426 (while (re-search-forward
427 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
429 (delete-region (+ (match-beginning 0) 2)
430 (+ (match-beginning 0) 3))))))
432 ((eq (car cont) 'mml)
433 (let ((mml-boundary (funcall mml-boundary-function
434 (incf mml-multipart-number)))
435 (mml-generate-default-type "text/plain"))
437 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
438 ;; ignore 0x1b, it is part of iso-2022-jp
439 (setq encoding (mm-body-7-or-8))))
440 ((string= (car (split-string type "/")) "message")
441 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
442 ;; ignore 0x1b, it is part of iso-2022-jp
443 (setq encoding (mm-body-7-or-8))))
445 ;; Only perform format=flowed filling on text/plain
446 ;; parts where there either isn't a format parameter
447 ;; in the mml tag or it says "flowed" and there
448 ;; actually are hard newlines in the text.
449 (let (use-hard-newlines)
450 (when (and (string= type "text/plain")
451 (or (null (assq 'format cont))
452 (string= (cdr (assq 'format cont))
454 (setq use-hard-newlines
456 (point-min) (point-max) 'hard 't)))
458 ;; Indicate that `mml-insert-mime-headers' should
459 ;; insert a "; format=flowed" string unless the
460 ;; user has already specified it.
461 (setq flowed (null (assq 'format cont)))))
462 (setq charset (mm-encode-body charset))
463 (setq encoding (mm-body-encoding
464 charset (cdr (assq 'encoding cont))))))
465 (setq coded (buffer-string)))
466 (mml-insert-mime-headers cont type charset encoding flowed)
469 (mm-with-unibyte-buffer
471 ((cdr (assq 'buffer cont))
472 (insert-buffer-substring (cdr (assq 'buffer cont))))
473 ((and (setq filename (cdr (assq 'filename cont)))
474 (not (equal (cdr (assq 'nofile cont)) "yes")))
475 (let ((coding-system-for-read mm-binary-coding-system))
476 (mm-insert-file-contents filename nil nil nil nil t)))
478 (insert (cdr (assq 'contents cont)))))
479 (setq encoding (mm-encode-buffer type)
480 coded (mm-string-as-multibyte (buffer-string))))
481 (mml-insert-mime-headers cont type charset encoding nil)
483 (mm-with-unibyte-current-buffer
485 ((eq (car cont) 'external)
486 (insert "Content-Type: message/external-body")
487 (let ((parameters (mml-parameter-string
488 cont '(expiration size permission)))
489 (name (cdr (assq 'name cont)))
490 (url (cdr (assq 'url cont))))
492 (setq name (mml-parse-file-name name))
494 (mml-insert-parameter
495 (mail-header-encode-parameter "name" name)
496 "access-type=local-file")
497 (mml-insert-parameter
498 (mail-header-encode-parameter
499 "name" (file-name-nondirectory (nth 2 name)))
500 (mail-header-encode-parameter "site" (nth 1 name))
501 (mail-header-encode-parameter
502 "directory" (file-name-directory (nth 2 name))))
503 (mml-insert-parameter
504 (concat "access-type="
505 (if (member (nth 0 name) '("ftp@" "anonymous@"))
509 (mml-insert-parameter
510 (mail-header-encode-parameter "url" url)
513 (mml-insert-parameter-string
514 cont '(expiration size permission))))
516 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
517 (insert "Content-ID: " (message-make-message-id) "\n")
518 (insert "Content-Transfer-Encoding: "
519 (or (cdr (assq 'encoding cont)) "binary"))
521 (insert (or (cdr (assq 'contents cont))))
523 ((eq (car cont) 'multipart)
524 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
525 (mml-generate-default-type (if (equal type "digest")
528 (handler (assoc type mml-generate-multipart-alist)))
530 (funcall (cdr handler) cont)
531 ;; No specific handler. Use default one.
532 (let ((mml-boundary (mml-compute-boundary cont)))
533 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
535 (if (cdr (assq 'start cont))
536 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
538 (let ((cont cont) part)
539 (while (setq part (pop cont))
540 ;; Skip `multipart' and attributes.
541 (when (and (consp part) (consp (cdr part)))
542 (insert "\n--" mml-boundary "\n")
543 (mml-generate-mime-1 part))))
544 (insert "\n--" mml-boundary "--\n")))))
546 (error "Invalid element: %S" cont)))
547 ;; handle sign & encrypt tags in a semi-smart way.
548 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
549 (encrypt-item (assoc (cdr (assq 'encrypt cont))
552 (when (or sign-item encrypt-item)
553 (when (setq sender (cdr (assq 'sender cont)))
554 (message-options-set 'mml-sender sender)
555 (message-options-set 'message-sender sender))
556 (if (setq recipients (cdr (assq 'recipients cont)))
557 (message-options-set 'message-recipients recipients))
558 (let ((style (mml-signencrypt-style (first (or sign-item encrypt-item)))))
559 ;; check if: we're both signing & encrypting, both methods
560 ;; are the same (why would they be different?!), and that
561 ;; the signencrypt style allows for combined operation.
562 (if (and sign-item encrypt-item (equal (first sign-item)
563 (first encrypt-item))
564 (equal style 'combined))
565 (funcall (nth 1 encrypt-item) cont t)
566 ;; otherwise, revert to the old behavior.
568 (funcall (nth 1 sign-item) cont))
570 (funcall (nth 1 encrypt-item) cont)))))))))
572 (defun mml-compute-boundary (cont)
573 "Return a unique boundary that does not exist in CONT."
574 (let ((mml-boundary (funcall mml-boundary-function
575 (incf mml-multipart-number))))
576 ;; This function tries again and again until it has found
577 ;; a unique boundary.
578 (while (not (catch 'not-unique
579 (mml-compute-boundary-1 cont))))
582 (defun mml-compute-boundary-1 (cont)
585 ((eq (car cont) 'part)
588 ((cdr (assq 'buffer cont))
589 (insert-buffer-substring (cdr (assq 'buffer cont))))
590 ((and (setq filename (cdr (assq 'filename cont)))
591 (not (equal (cdr (assq 'nofile cont)) "yes")))
592 (mm-insert-file-contents filename))
594 (insert (cdr (assq 'contents cont)))))
595 (goto-char (point-min))
596 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
598 (setq mml-boundary (funcall mml-boundary-function
599 (incf mml-multipart-number)))
600 (throw 'not-unique nil))))
601 ((eq (car cont) 'multipart)
602 (mapcar 'mml-compute-boundary-1 (cddr cont))))
605 (defun mml-make-boundary (number)
606 (concat (make-string (% number 60) ?=)
612 (defun mml-insert-mime-headers (cont type charset encoding flowed)
613 (let (parameters id disposition description)
615 (mml-parameter-string
616 cont mml-content-type-parameters))
620 (not (equal type mml-generate-default-type))
621 mml-insert-mime-headers-always)
622 (when (consp charset)
624 "Can't encode a part with several charsets"))
625 (insert "Content-Type: " type)
627 (insert "; " (mail-header-encode-parameter
628 "charset" (symbol-name charset))))
630 (insert "; format=flowed"))
632 (mml-insert-parameter-string
633 cont mml-content-type-parameters))
635 (when (setq id (cdr (assq 'id cont)))
636 (insert "Content-ID: " id "\n"))
638 (mml-parameter-string
639 cont mml-content-disposition-parameters))
640 (when (or (setq disposition (cdr (assq 'disposition cont)))
642 (insert "Content-Disposition: " (or disposition "inline"))
644 (mml-insert-parameter-string
645 cont mml-content-disposition-parameters))
647 (unless (eq encoding '7bit)
648 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
649 (when (setq description (cdr (assq 'description cont)))
650 (insert "Content-Description: "
651 (mail-encode-encoded-word-string description) "\n"))))
653 (defun mml-parameter-string (cont types)
656 (while (setq type (pop types))
657 (when (setq value (cdr (assq type cont)))
658 ;; Strip directory component from the filename parameter.
659 (when (eq type 'filename)
660 (setq value (file-name-nondirectory value)))
661 (setq string (concat string "; "
662 (mail-header-encode-parameter
663 (symbol-name type) value)))))
664 (when (not (zerop (length string)))
667 (defun mml-insert-parameter-string (cont types)
669 (while (setq type (pop types))
670 (when (setq value (cdr (assq type cont)))
671 ;; Strip directory component from the filename parameter.
672 (when (eq type 'filename)
673 (setq value (file-name-nondirectory value)))
674 (mml-insert-parameter
675 (mail-header-encode-parameter
676 (symbol-name type) value))))))
679 (defvar ange-ftp-name-format)
680 (defvar efs-path-regexp))
681 (defun mml-parse-file-name (path)
682 (if (if (boundp 'efs-path-regexp)
683 (string-match efs-path-regexp path)
684 (if (boundp 'ange-ftp-name-format)
685 (string-match (car ange-ftp-name-format) path)))
686 (list (match-string 1 path) (match-string 2 path)
687 (substring path (1+ (match-end 2))))
690 (defun mml-insert-buffer (buffer)
691 "Insert BUFFER at point and quote any MML markup."
693 (narrow-to-region (point) (point))
694 (insert-buffer-substring buffer)
695 (mml-quote-region (point-min) (point-max))
696 (goto-char (point-max))))
699 ;;; Transforming MIME to MML
702 (defun mime-to-mml (&optional handles)
703 "Translate the current buffer (which should be a message) into MML.
704 If HANDLES is non-nil, use it instead reparsing the buffer."
705 ;; First decode the head.
707 (message-narrow-to-head)
708 (mail-decode-encoded-word-region (point-min) (point-max)))
710 (setq handles (mm-dissect-buffer t)))
711 (goto-char (point-min))
712 (search-forward "\n\n" nil t)
713 (delete-region (point) (point-max))
714 (if (stringp (car handles))
715 (mml-insert-mime handles)
716 (mml-insert-mime handles t))
717 (mm-destroy-parts handles)
719 (message-narrow-to-head)
720 ;; Remove them, they are confusing.
721 (message-remove-header "Content-Type")
722 (message-remove-header "MIME-Version")
723 (message-remove-header "Content-Disposition")
724 (message-remove-header "Content-Transfer-Encoding")))
726 (defun mml-to-mime ()
727 "Translate the current buffer from MML to MIME."
728 (message-encode-message-body)
730 (message-narrow-to-headers-or-head)
731 ;; Skip past any From_ headers.
732 (while (looking-at "From ")
734 (let ((mail-parse-charset message-default-charset))
735 (mail-encode-encoded-word-buffer))))
737 (defun mml-insert-mime (handle &optional no-markup)
738 (let (textp buffer mmlp)
739 ;; Determine type and stuff.
740 (unless (stringp (car handle))
741 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
743 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
744 (mm-insert-part handle)
745 (if (setq mmlp (equal (mm-handle-media-type handle)
749 (mml-insert-mml-markup handle nil t t)
750 (unless (and no-markup
751 (equal (mm-handle-media-type handle) "text/plain"))
752 (mml-insert-mml-markup handle buffer textp)))
755 (insert-buffer-substring buffer)
756 (goto-char (point-max))
757 (insert "<#/mml>\n"))
758 ((stringp (car handle))
759 (mapcar 'mml-insert-mime (cdr handle))
760 (insert "<#/multipart>\n"))
762 (let ((charset (mail-content-type-get
763 (mm-handle-type handle) 'charset))
765 (if (eq charset 'gnus-decoded)
766 (mm-insert-part handle)
767 (insert (mm-decode-string (mm-get-part handle) charset)))
768 (mml-quote-region start (point)))
769 (goto-char (point-max)))
771 (insert "<#/part>\n")))))
773 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
774 "Take a MIME handle and insert an MML tag."
775 (if (stringp (car handle))
777 (insert "<#multipart type=" (mm-handle-media-subtype handle))
778 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
780 (insert " start=\"" start "\"")))
783 (insert "<#mml type=" (mm-handle-media-type handle))
784 (insert "<#part type=" (mm-handle-media-type handle)))
785 (dolist (elem (append (cdr (mm-handle-type handle))
786 (cdr (mm-handle-disposition handle))))
787 (unless (symbolp (cdr elem))
788 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
789 (when (mm-handle-id handle)
790 (insert " id=\"" (mm-handle-id handle) "\""))
791 (when (mm-handle-disposition handle)
792 (insert " disposition=" (car (mm-handle-disposition handle))))
794 (insert " buffer=\"" (buffer-name buffer) "\""))
796 (insert " nofile=yes"))
797 (when (mm-handle-description handle)
798 (insert " description=\"" (mm-handle-description handle) "\""))
801 (defun mml-insert-parameter (&rest parameters)
802 "Insert PARAMETERS in a nice way."
803 (dolist (param parameters)
805 (let ((point (point)))
807 (when (> (current-column) 71)
813 ;;; Mode for inserting and editing MML forms
817 (let ((sign (make-sparse-keymap))
818 (encrypt (make-sparse-keymap))
819 (signpart (make-sparse-keymap))
820 (encryptpart (make-sparse-keymap))
821 (map (make-sparse-keymap))
822 (main (make-sparse-keymap)))
823 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
824 (define-key sign "o" 'mml-secure-message-sign-pgp)
825 (define-key sign "s" 'mml-secure-message-sign-smime)
826 (define-key signpart "p" 'mml-secure-sign-pgpmime)
827 (define-key signpart "o" 'mml-secure-sign-pgp)
828 (define-key signpart "s" 'mml-secure-sign-smime)
829 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
830 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
831 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
832 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
833 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
834 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
835 (define-key map "\C-n" 'mml-unsecure-message)
836 (define-key map "f" 'mml-attach-file)
837 (define-key map "b" 'mml-attach-buffer)
838 (define-key map "e" 'mml-attach-external)
839 (define-key map "q" 'mml-quote-region)
840 (define-key map "m" 'mml-insert-multipart)
841 (define-key map "p" 'mml-insert-part)
842 (define-key map "v" 'mml-validate)
843 (define-key map "P" 'mml-preview)
844 (define-key map "s" sign)
845 (define-key map "S" signpart)
846 (define-key map "c" encrypt)
847 (define-key map "C" encryptpart)
848 ;;(define-key map "n" 'mml-narrow-to-part)
849 ;; `M-m' conflicts with `back-to-indentation'.
850 ;; (define-key main "\M-m" map)
851 (define-key main "\C-c\C-m" map)
855 mml-menu mml-mode-map ""
857 ["Attach File..." mml-attach-file
858 ,@(if (featurep 'xemacs) '(t)
859 '(:help "Attach a file at point"))]
860 ["Attach Buffer..." mml-attach-buffer t]
861 ["Attach External..." mml-attach-external t]
862 ["Insert Part..." mml-insert-part t]
863 ["Insert Multipart..." mml-insert-multipart t]
864 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
865 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
866 ["PGP Sign" mml-secure-message-sign-pgp t]
867 ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
868 ["S/MIME Sign" mml-secure-message-sign-smime t]
869 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
871 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
872 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
873 ["PGP Sign Part" mml-secure-sign-pgp t]
874 ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
875 ["S/MIME Sign Part" mml-secure-sign-smime t]
876 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
877 ["Encrypt/Sign off" mml-unsecure-message t]
878 ;;["Narrow" mml-narrow-to-part t]
879 ["Quote MML" mml-quote-region t]
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 (gnus-add-minor-mode 'mml-mode " MML" mml-mode-map)
897 (easy-menu-add mml-menu mml-mode-map)
898 (run-hooks 'mml-mode-hook)))
901 ;;; Helper functions for reading MIME stuff from the minibuffer and
902 ;;; inserting stuff to the buffer.
905 (defun mml-minibuffer-read-file (prompt)
906 (let* ((completion-ignored-extensions nil)
907 (file (read-file-name prompt nil nil t)))
908 ;; Prevent some common errors. This is inspired by similar code in
910 (when (file-directory-p file)
911 (error "%s is a directory, cannot attach" file))
912 (unless (file-exists-p file)
913 (error "No such file: %s" file))
914 (unless (file-readable-p file)
915 (error "Permission denied: %s" file))
918 (defun mml-minibuffer-read-type (name &optional default)
919 (mailcap-parse-mimetypes)
920 (let* ((default (or default
921 (mm-default-file-encoding name)
922 ;; Perhaps here we should check what the file
923 ;; looks like, and offer text/plain if it looks
925 "application/octet-stream"))
926 (string (completing-read
927 (format "Content type (default %s): " default)
928 (mapcar 'list (mailcap-mime-types)))))
929 (if (not (equal string ""))
933 (defun mml-minibuffer-read-description ()
934 (let ((description (read-string "One line description: ")))
935 (when (string-match "\\`[ \t]*\\'" description)
936 (setq description nil))
939 (defun mml-minibuffer-read-disposition (type &optional default)
940 (let* ((default (or default
941 (if (string-match "^text/.*" type)
944 (disposition (completing-read "Disposition: "
945 '(("attachment") ("inline") (""))
948 (if (not (equal disposition ""))
952 (defun mml-quote-region (beg end)
953 "Quote the MML tags in the region."
957 ;; Temporarily narrow the region to defend from changes
959 (narrow-to-region beg end)
960 (goto-char (point-min))
962 (while (re-search-forward
963 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
964 ;; Insert ! after the #.
965 (goto-char (+ (match-beginning 0) 2))
968 (defun mml-insert-tag (name &rest plist)
969 "Insert an MML tag described by NAME and PLIST."
971 (setq name (symbol-name name)))
974 (let ((key (pop plist))
977 ;; Quote VALUE if it contains suspicious characters.
978 (when (string-match "[\"'\\~/*;() \t\n]" value)
979 (setq value (with-output-to-string
980 (let (print-escape-nonascii)
982 (insert (format " %s=%s" key value)))))
985 (defun mml-insert-empty-tag (name &rest plist)
986 "Insert an empty MML tag described by NAME and PLIST."
988 (setq name (symbol-name name)))
989 (apply #'mml-insert-tag name plist)
990 (insert "<#/" name ">\n"))
992 ;;; Attachment functions.
994 (defun mml-attach-file (file &optional type description disposition)
995 "Attach a file to the outgoing MIME message.
996 The file is not inserted or encoded until you send the message with
997 `\\[message-send-and-exit]' or `\\[message-send]'.
999 FILE is the name of the file to attach. TYPE is its content-type, a
1000 string of the form \"type/subtype\". DESCRIPTION is a one-line
1001 description of the attachment."
1003 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1004 (type (mml-minibuffer-read-type file))
1005 (description (mml-minibuffer-read-description))
1006 (disposition (mml-minibuffer-read-disposition type)))
1007 (list file type description disposition)))
1008 (mml-insert-empty-tag 'part
1011 'disposition (or disposition "attachment")
1012 'description description))
1014 (defun mml-attach-buffer (buffer &optional type description)
1015 "Attach a buffer to the outgoing MIME message.
1016 See `mml-attach-file' for details of operation."
1018 (let* ((buffer (read-buffer "Attach buffer: "))
1019 (type (mml-minibuffer-read-type buffer "text/plain"))
1020 (description (mml-minibuffer-read-description)))
1021 (list buffer type description)))
1022 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1023 'disposition "attachment" 'description description))
1025 (defun mml-attach-external (file &optional type description)
1026 "Attach an external file into the buffer.
1027 FILE is an ange-ftp/efs specification of the part location.
1028 TYPE is the MIME type to use."
1030 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1031 (type (mml-minibuffer-read-type file))
1032 (description (mml-minibuffer-read-description)))
1033 (list file type description)))
1034 (mml-insert-empty-tag 'external 'type type 'name file
1035 'disposition "attachment" 'description description))
1037 (defun mml-insert-multipart (&optional type)
1038 (interactive (list (completing-read "Multipart type (default mixed): "
1039 '(("mixed") ("alternative") ("digest") ("parallel")
1040 ("signed") ("encrypted"))
1043 (setq type "mixed"))
1044 (mml-insert-empty-tag "multipart" 'type type)
1047 (defun mml-insert-part (&optional type)
1049 (list (mml-minibuffer-read-type "")))
1050 (mml-insert-tag 'part 'type type 'disposition "inline")
1053 (defun mml-preview-insert-mail-followup-to ()
1054 "Insert a Mail-Followup-To header before previewing an article.
1055 Should be adopted if code in `message-send-mail' is changed."
1056 (when (and (message-mail-p)
1057 (message-subscribed-p)
1058 (not (mail-fetch-field "mail-followup-to"))
1059 (message-make-mail-followup-to))
1060 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1061 (insert (message-make-mail-followup-to))))
1063 (defun mml-preview (&optional raw)
1064 "Display current buffer with Gnus, in a new buffer.
1065 If RAW, don't highlight the article."
1068 (let* ((buf (current-buffer))
1069 (message-options message-options)
1070 (message-this-is-mail (message-mail-p))
1071 (message-this-is-news (message-news-p))
1072 (message-posting-charset (or (gnus-setup-posting-charset
1074 (message-narrow-to-headers-or-head)
1075 (message-fetch-field "Newsgroups")))
1076 message-posting-charset)))
1077 (message-options-set-recipient)
1078 (switch-to-buffer (generate-new-buffer
1079 (concat (if raw "*Raw MIME preview of "
1080 "*MIME preview of ") (buffer-name))))
1081 (when (boundp 'gnus-buffers)
1082 (push (current-buffer) gnus-buffers))
1084 (insert-buffer-substring buf)
1085 (mml-preview-insert-mail-followup-to)
1086 (let ((message-deletable-headers (if (message-news-p)
1088 message-deletable-headers)))
1089 (message-generate-headers
1090 (copy-sequence (if (message-news-p)
1091 message-required-news-headers
1092 message-required-mail-headers))))
1093 (if (re-search-forward
1094 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1095 (replace-match "\n"))
1096 (let ((mail-header-separator ""));; mail-header-separator is removed.
1099 (when (fboundp 'set-buffer-multibyte)
1100 (let ((s (buffer-string)))
1101 ;; Insert the content into unibyte buffer.
1103 (mm-disable-multibyte)
1105 (let ((gnus-newsgroup-charset (car message-posting-charset))
1106 gnus-article-prepare-hook gnus-original-article-buffer)
1107 (run-hooks 'gnus-article-decode-hook)
1108 (let ((gnus-newsgroup-name "dummy")
1109 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1110 (gnus-make-hashtable 5))))
1111 (gnus-article-prepare-display))))
1112 ;; Disable article-mode-map.
1114 (gnus-make-local-hook 'kill-buffer-hook)
1115 (add-hook 'kill-buffer-hook
1117 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1118 (setq buffer-read-only t)
1119 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1120 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1124 (widget-button-press (point))))
1125 (local-set-key gnus-mouse-2
1128 (widget-button-press (widget-event-point event) event)))
1129 (goto-char (point-min)))))
1131 (defun mml-validate ()
1132 "Validate the current MML document."
1136 (defun mml-tweak-part (cont)
1138 (let ((tweak (cdr (assq 'tweak cont)))
1143 (or (cdr (assoc tweak mml-tweak-function-alist))
1145 (mml-tweak-type-alist
1146 (let ((alist mml-tweak-type-alist)
1147 (type (or (cdr (assq 'type cont)) "text/plain")))
1149 (if (string-match (caar alist) type)
1150 (setq func (cdar alist)
1152 (setq alist (cdr alist)))))))
1156 (let ((alist mml-tweak-sexp-alist))
1158 (if (eval (caar alist))
1159 (funcall (cdar alist) cont))
1160 (setq alist (cdr alist)))))
1163 (defun mml-tweak-externalize-attachments (cont)
1164 "Tweak attached files as external parts."
1165 (let (filename-cons)
1166 (when (and (eq (car cont) 'part)
1167 (not (cdr (assq 'buffer cont)))
1168 (and (setq filename-cons (assq 'filename cont))
1169 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1170 (setcar cont 'external)
1171 (setcar filename-cons 'name))))
1175 ;;; mml.el ends here