1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl))
34 (autoload 'message-make-message-id "message")
35 (autoload 'gnus-setup-posting-charset "gnus-msg")
36 (autoload 'gnus-add-minor-mode "gnus-ems")
37 (autoload 'message-fetch-field "message")
38 (autoload 'fill-flowed-encode "flow-fill")
39 (autoload 'message-posting-charset "message"))
41 (defcustom mml-content-type-parameters
42 '(name access-type expiration size permission format)
43 "*A list of acceptable parameters in MML tag.
44 These parameters are generated in Content-Type header if exists."
45 :type '(repeat (symbol :tag "Parameter"))
48 (defcustom mml-content-disposition-parameters
49 '(filename creation-date modification-date read-date)
50 "*A list of acceptable parameters in MML tag.
51 These parameters are generated in Content-Disposition header if exists."
52 :type '(repeat (symbol :tag "Parameter"))
55 (defvar mml-tweak-type-alist nil
56 "A list of (TYPE . FUNCTION) for tweaking MML parts.
57 TYPE is a string containing a regexp to match the MIME type. FUNCTION
58 is a Lisp function which is called with the MML handle to tweak the
59 part. This variable is used only when no TWEAK parameter exists in
62 (defvar mml-tweak-function-alist nil
63 "A list of (NAME . FUNCTION) for tweaking MML parts.
64 NAME is a string containing the name of the TWEAK parameter in the MML
65 handle. FUNCTION is a Lisp function which is called with the MML
66 handle to tweak the part.")
68 (defvar mml-tweak-sexp-alist
69 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
70 "A list of (SEXP . FUNCTION) for tweaking MML parts.
71 SEXP is a s-expression. If the evaluation of SEXP is non-nil, FUNCTION
72 is called. FUNCTION is a Lisp function which is called with the MML
73 handle to tweak the part.")
75 (defvar mml-externalize-attachments nil
76 "*If non-nil, local-file attachments are generated as external parts.")
78 (defvar mml-generate-multipart-alist nil
79 "*Alist of multipart generation functions.
80 Each entry has the form (NAME . FUNCTION), where
81 NAME is a string containing the name of the part (without the
82 leading \"/multipart/\"),
83 FUNCTION is a Lisp function which is called to generate the part.
85 The Lisp function has to supply the appropriate MIME headers and the
86 contents of this part.")
88 (defvar mml-syntax-table
89 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
90 (modify-syntax-entry ?\\ "/" table)
91 (modify-syntax-entry ?< "(" table)
92 (modify-syntax-entry ?> ")" table)
93 (modify-syntax-entry ?@ "w" table)
94 (modify-syntax-entry ?/ "w" table)
95 (modify-syntax-entry ?= " " table)
96 (modify-syntax-entry ?* " " table)
97 (modify-syntax-entry ?\; " " table)
98 (modify-syntax-entry ?\' " " table)
101 (defvar mml-boundary-function 'mml-make-boundary
102 "A function called to suggest a boundary.
103 The function may be called several times, and should try to make a new
104 suggestion each time. The function is called with one parameter,
105 which is a number that says how many times the function has been
106 called for this message.")
108 (defvar mml-confirmation-set nil
109 "A list of symbols, each of which disables some warning.
110 `unknown-encoding': always send messages contain characters with
111 unknown encoding; `use-ascii': always use ASCII for those characters
112 with unknown encoding; `multipart': always send messages with more than
115 (defvar mml-generate-default-type "text/plain")
117 (defvar mml-buffer-list nil)
119 (defun mml-generate-new-buffer (name)
120 (let ((buf (generate-new-buffer name)))
121 (push buf mml-buffer-list)
124 (defun mml-destroy-buffers ()
125 (let (kill-buffer-hook)
126 (mapcar 'kill-buffer mml-buffer-list)
127 (setq mml-buffer-list nil)))
130 "Parse the current buffer as an MML document."
132 (goto-char (point-min))
133 (let ((table (syntax-table)))
136 (set-syntax-table mml-syntax-table)
138 (set-syntax-table table)))))
140 (defun mml-parse-1 ()
141 "Parse the current buffer as an MML document."
142 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
143 (while (and (not (eobp))
144 (not (looking-at "<#/multipart")))
146 ((looking-at "<#secure")
147 ;; The secure part is essentially a meta-meta tag, which
148 ;; expands to either a part tag if there are no other parts in
149 ;; the document or a multipart tag if there are other parts
150 ;; included in the message
152 (taginfo (mml-read-tag))
153 (recipients (cdr (assq 'recipients taginfo)))
154 (sender (cdr (assq 'sender taginfo)))
155 (location (cdr (assq 'tag-location taginfo)))
156 (mode (cdr (assq 'mode taginfo)))
157 (method (cdr (assq 'method taginfo)))
162 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
163 (setq secure-mode "multipart")
164 (setq secure-mode "part")))
167 (re-search-forward "<#secure[^\n]*>\n"))
168 (delete-region (match-beginning 0) (match-end 0))
169 (cond ((string= mode "sign")
170 (setq tags (list "sign" method)))
171 ((string= mode "encrypt")
172 (setq tags (list "encrypt" method)))
173 ((string= mode "signencrypt")
174 (setq tags (list "sign" method "encrypt" method))))
175 (eval `(mml-insert-tag ,secure-mode
177 ,(if recipients "recipients")
179 ,(if sender "sender")
182 (goto-char location)))
183 ((looking-at "<#multipart")
184 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
185 ((looking-at "<#external")
186 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
189 (if (or (looking-at "<#part") (looking-at "<#mml"))
190 (setq tag (mml-read-tag)
193 (setq tag (list 'part '(type . "text/plain"))
196 (setq raw (cdr (assq 'raw tag))
198 contents (mml-read-part (eq 'mml (car tag)))
203 (intern (downcase (cdr (assq 'charset tag))))))
205 (mm-find-mime-charset-region point (point)
207 (when (and (not raw) (memq nil charsets))
208 (if (or (memq 'unknown-encoding mml-confirmation-set)
209 (message-options-get 'unknown-encoding)
211 Message contains characters with unknown encoding. Really send?")
212 (message-options-set 'unknown-encoding t)))
214 (or (memq 'use-ascii mml-confirmation-set)
215 (message-options-get 'use-ascii)
216 (and (y-or-n-p "Use ASCII as charset?")
217 (message-options-set 'use-ascii t))))
218 (setq charsets (delq nil charsets))
220 (error "Edit your message to remove those characters")))
223 (< (length charsets) 2))
224 (if (or (not no-markup-p)
225 (string-match "[^ \t\r\n]" contents))
226 ;; Don't create blank parts.
227 (push (nconc tag (list (cons 'contents contents)))
229 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
230 tag point (point) use-ascii)))
232 (not (memq 'multipart mml-confirmation-set))
233 (not (message-options-get 'multipart))
234 (not (and (y-or-n-p (format "\
235 A message part needs to be split into %d charset parts. Really send? "
237 (message-options-set 'multipart t))))
238 (error "Edit your message to use only one charset"))
239 (setq struct (nconc nstruct struct)))))))
244 (defun mml-parse-singlepart-with-multiple-charsets
245 (orig-tag beg end &optional use-ascii)
248 (narrow-to-region beg end)
249 (goto-char (point-min))
250 (let ((current (or (mm-mime-charset (mm-charset-after))
251 (and use-ascii 'us-ascii)))
252 charset struct space newline paragraph)
254 (setq charset (mm-mime-charset (mm-charset-after)))
256 ;; The charset remains the same.
257 ((eq charset 'us-ascii))
258 ((or (and use-ascii (not charset))
259 (eq charset current))
263 ;; The initial charset was ascii.
264 ((eq current 'us-ascii)
265 (setq current charset
269 ;; We have a change in charsets.
273 (list (cons 'contents
274 (buffer-substring-no-properties
275 beg (or paragraph newline space (point))))))
277 (setq beg (or paragraph newline space (point))
282 ;; Compute places where it might be nice to break the part.
284 ((memq (following-char) '(? ?\t))
285 (setq space (1+ (point))))
286 ((and (eq (following-char) ?\n)
288 (eq (char-after (1- (point))) ?\n))
289 (setq paragraph (point)))
290 ((eq (following-char) ?\n)
291 (setq newline (1+ (point)))))
293 ;; Do the final part.
294 (unless (= beg (point))
295 (push (append orig-tag
296 (list (cons 'contents
297 (buffer-substring-no-properties
302 (defun mml-read-tag ()
303 "Read a tag and return the contents."
304 (let ((orig-point (point))
305 contents name elem val)
307 (setq name (buffer-substring-no-properties
308 (point) (progn (forward-sexp 1) (point))))
309 (skip-chars-forward " \t\n")
310 (while (not (looking-at ">[ \t]*\n?"))
311 (setq elem (buffer-substring-no-properties
312 (point) (progn (forward-sexp 1) (point))))
313 (skip-chars-forward "= \t\n")
314 (setq val (buffer-substring-no-properties
315 (point) (progn (forward-sexp 1) (point))))
316 (when (string-match "^\"\\(.*\\)\"$" val)
317 (setq val (match-string 1 val)))
318 (push (cons (intern elem) val) contents)
319 (skip-chars-forward " \t\n"))
320 (goto-char (match-end 0))
321 ;; Don't skip the leading space.
322 ;;(skip-chars-forward " \t\n")
323 ;; Put the tag location into the returned contents
324 (setq contents (append (list (cons 'tag-location orig-point)) contents))
325 (cons (intern name) (nreverse contents))))
327 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
328 (let ((str (buffer-substring-no-properties start end))
329 (bufstart start) tmp)
330 (while (setq tmp (text-property-any start end 'hard 't))
331 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
333 (setq start (1+ tmp)))
336 (defun mml-read-part (&optional mml)
337 "Return the buffer up till the next part, multipart or closing part or multipart.
338 If MML is non-nil, return the buffer up till the correspondent mml tag."
339 (let ((beg (point)) (count 1))
340 ;; If the tag ended at the end of the line, we go to the next line.
341 (when (looking-at "[ \t]*\n")
345 (while (and (> count 0) (not (eobp)))
346 (if (re-search-forward "<#\\(/\\)?mml." nil t)
347 (setq count (+ count (if (match-beginning 1) -1 1)))
348 (goto-char (point-max))))
349 (mml-buffer-substring-no-properties-except-hard-newlines
352 (match-beginning 0))))
353 (if (re-search-forward
354 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
356 (mml-buffer-substring-no-properties-except-hard-newlines
357 beg (match-beginning 0))
358 (if (or (not (match-beginning 1))
359 (equal (match-string 2) "multipart"))
360 (goto-char (match-beginning 0))
361 (when (looking-at "[ \t]*\n")
363 (mml-buffer-substring-no-properties-except-hard-newlines
364 beg (goto-char (point-max)))))))
366 (defvar mml-boundary nil)
367 (defvar mml-base-boundary "-=-=")
368 (defvar mml-multipart-number 0)
370 (defun mml-generate-mime ()
371 "Generate a MIME message based on the current MML document."
372 (let ((cont (mml-parse))
373 (mml-multipart-number mml-multipart-number))
377 (if (and (consp (car cont))
379 (mml-generate-mime-1 (car cont))
380 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
384 (defun mml-generate-mime-1 (cont)
385 (let ((mm-use-ultra-safe-encoding
386 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
388 (narrow-to-region (point) (point))
389 (mml-tweak-part cont)
391 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
392 (let ((raw (cdr (assq 'raw cont)))
393 coded encoding charset filename type flowed)
394 (setq type (or (cdr (assq 'type cont)) "text/plain"))
396 (member (car (split-string type "/")) '("text" "message")))
399 (setq charset (mm-charset-to-coding-system
400 (cdr (assq 'charset cont))))
401 (when (eq charset 'ascii)
404 ((cdr (assq 'buffer cont))
405 (insert-buffer-substring (cdr (assq 'buffer cont))))
406 ((and (setq filename (cdr (assq 'filename cont)))
407 (not (equal (cdr (assq 'nofile cont)) "yes")))
408 (let ((coding-system-for-read charset))
409 (mm-insert-file-contents filename)))
410 ((eq 'mml (car cont))
411 (insert (cdr (assq 'contents cont))))
414 (narrow-to-region (point) (point))
415 (insert (cdr (assq 'contents cont)))
416 ;; Remove quotes from quoted tags.
417 (goto-char (point-min))
418 (while (re-search-forward
419 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
421 (delete-region (+ (match-beginning 0) 2)
422 (+ (match-beginning 0) 3))))))
424 ((eq (car cont) 'mml)
425 (let ((mml-boundary (funcall mml-boundary-function
426 (incf mml-multipart-number)))
427 (mml-generate-default-type "text/plain"))
429 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
430 ;; ignore 0x1b, it is part of iso-2022-jp
431 (setq encoding (mm-body-7-or-8))))
432 ((string= (car (split-string type "/")) "message")
433 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
434 ;; ignore 0x1b, it is part of iso-2022-jp
435 (setq encoding (mm-body-7-or-8))))
437 ;; Only perform format=flowed filling on text/plain
438 ;; parts where there either isn't a format parameter
439 ;; in the mml tag or it says "flowed" and there
440 ;; actually are hard newlines in the text.
441 (let (use-hard-newlines)
442 (when (and (string= type "text/plain")
443 (or (null (assq 'format cont))
444 (string= (cdr (assq 'format cont))
446 (setq use-hard-newlines
448 (point-min) (point-max) 'hard 't)))
450 ;; Indicate that `mml-insert-mime-headers' should
451 ;; insert a "; format=flowed" string unless the
452 ;; user has already specified it.
453 (setq flowed (null (assq 'format cont)))))
454 (setq charset (mm-encode-body charset))
455 (setq encoding (mm-body-encoding
456 charset (cdr (assq 'encoding cont))))))
457 (setq coded (buffer-string)))
458 (mml-insert-mime-headers cont type charset encoding flowed)
461 (mm-with-unibyte-buffer
463 ((cdr (assq 'buffer cont))
464 (insert-buffer-substring (cdr (assq 'buffer cont))))
465 ((and (setq filename (cdr (assq 'filename cont)))
466 (not (equal (cdr (assq 'nofile cont)) "yes")))
467 (let ((coding-system-for-read mm-binary-coding-system))
468 (mm-insert-file-contents filename nil nil nil nil t)))
470 (insert (cdr (assq 'contents cont)))))
471 (setq encoding (mm-encode-buffer type)
472 coded (mm-string-as-multibyte (buffer-string))))
473 (mml-insert-mime-headers cont type charset encoding nil)
475 (mm-with-unibyte-current-buffer
477 ((eq (car cont) 'external)
478 (insert "Content-Type: message/external-body")
479 (let ((parameters (mml-parameter-string
480 cont '(expiration size permission)))
481 (name (cdr (assq 'name cont)))
482 (url (cdr (assq 'url cont))))
484 (setq name (mml-parse-file-name name))
486 (mml-insert-parameter
487 (mail-header-encode-parameter "name" name)
488 "access-type=local-file")
489 (mml-insert-parameter
490 (mail-header-encode-parameter
491 "name" (file-name-nondirectory (nth 2 name)))
492 (mail-header-encode-parameter "site" (nth 1 name))
493 (mail-header-encode-parameter
494 "directory" (file-name-directory (nth 2 name))))
495 (mml-insert-parameter
496 (concat "access-type="
497 (if (member (nth 0 name) '("ftp@" "anonymous@"))
501 (mml-insert-parameter
502 (mail-header-encode-parameter "url" url)
505 (mml-insert-parameter-string
506 cont '(expiration size permission))))
508 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
509 (insert "Content-ID: " (message-make-message-id) "\n")
510 (insert "Content-Transfer-Encoding: "
511 (or (cdr (assq 'encoding cont)) "binary"))
513 (insert (or (cdr (assq 'contents cont))))
515 ((eq (car cont) 'multipart)
516 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
517 (mml-generate-default-type (if (equal type "digest")
520 (handler (assoc type mml-generate-multipart-alist)))
522 (funcall (cdr handler) cont)
523 ;; No specific handler. Use default one.
524 (let ((mml-boundary (mml-compute-boundary cont)))
525 (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n"
527 (let ((cont cont) part)
528 (while (setq part (pop cont))
529 ;; Skip `multipart' and attributes.
530 (when (and (consp part) (consp (cdr part)))
531 (insert "\n--" mml-boundary "\n")
532 (mml-generate-mime-1 part))))
533 (insert "\n--" mml-boundary "--\n")))))
535 (error "Invalid element: %S" cont)))
536 ;; handle sign & encrypt tags in a semi-smart way.
537 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
538 (encrypt-item (assoc (cdr (assq 'encrypt cont))
541 (when (or sign-item encrypt-item)
542 (if (setq sender (cdr (assq 'sender cont)))
543 (message-options-set 'message-sender sender))
544 (if (setq recipients (cdr (assq 'recipients cont)))
545 (message-options-set 'message-recipients recipients))
546 (let ((style (mml-signencrypt-style (first (or sign-item encrypt-item)))))
547 ;; check if: we're both signing & encrypting, both methods
548 ;; are the same (why would they be different?!), and that
549 ;; the signencrypt style allows for combined operation.
550 (if (and sign-item encrypt-item (equal (first sign-item)
551 (first encrypt-item))
552 (equal style 'combined))
553 (funcall (nth 1 encrypt-item) cont t)
554 ;; otherwise, revert to the old behavior.
556 (funcall (nth 1 sign-item) cont))
558 (funcall (nth 1 encrypt-item) cont)))))))))
560 (defun mml-compute-boundary (cont)
561 "Return a unique boundary that does not exist in CONT."
562 (let ((mml-boundary (funcall mml-boundary-function
563 (incf mml-multipart-number))))
564 ;; This function tries again and again until it has found
565 ;; a unique boundary.
566 (while (not (catch 'not-unique
567 (mml-compute-boundary-1 cont))))
570 (defun mml-compute-boundary-1 (cont)
573 ((eq (car cont) 'part)
576 ((cdr (assq 'buffer cont))
577 (insert-buffer-substring (cdr (assq 'buffer cont))))
578 ((and (setq filename (cdr (assq 'filename cont)))
579 (not (equal (cdr (assq 'nofile cont)) "yes")))
580 (mm-insert-file-contents filename))
582 (insert (cdr (assq 'contents cont)))))
583 (goto-char (point-min))
584 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
586 (setq mml-boundary (funcall mml-boundary-function
587 (incf mml-multipart-number)))
588 (throw 'not-unique nil))))
589 ((eq (car cont) 'multipart)
590 (mapcar 'mml-compute-boundary-1 (cddr cont))))
593 (defun mml-make-boundary (number)
594 (concat (make-string (% number 60) ?=)
600 (defun mml-insert-mime-headers (cont type charset encoding flowed)
601 (let (parameters disposition description)
603 (mml-parameter-string
604 cont mml-content-type-parameters))
608 (not (equal type mml-generate-default-type)))
609 (when (consp charset)
611 "Can't encode a part with several charsets."))
612 (insert "Content-Type: " type)
614 (insert "; " (mail-header-encode-parameter
615 "charset" (symbol-name charset))))
617 (insert "; format=flowed"))
619 (mml-insert-parameter-string
620 cont mml-content-type-parameters))
623 (mml-parameter-string
624 cont mml-content-disposition-parameters))
625 (when (or (setq disposition (cdr (assq 'disposition cont)))
627 (insert "Content-Disposition: " (or disposition "inline"))
629 (mml-insert-parameter-string
630 cont mml-content-disposition-parameters))
632 (unless (eq encoding '7bit)
633 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
634 (when (setq description (cdr (assq 'description cont)))
635 (insert "Content-Description: "
636 (mail-encode-encoded-word-string description) "\n"))))
638 (defun mml-parameter-string (cont types)
641 (while (setq type (pop types))
642 (when (setq value (cdr (assq type cont)))
643 ;; Strip directory component from the filename parameter.
644 (when (eq type 'filename)
645 (setq value (file-name-nondirectory value)))
646 (setq string (concat string "; "
647 (mail-header-encode-parameter
648 (symbol-name type) value)))))
649 (when (not (zerop (length string)))
652 (defun mml-insert-parameter-string (cont types)
654 (while (setq type (pop types))
655 (when (setq value (cdr (assq type cont)))
656 ;; Strip directory component from the filename parameter.
657 (when (eq type 'filename)
658 (setq value (file-name-nondirectory value)))
659 (mml-insert-parameter
660 (mail-header-encode-parameter
661 (symbol-name type) value))))))
664 (defvar ange-ftp-name-format)
665 (defvar efs-path-regexp))
666 (defun mml-parse-file-name (path)
667 (if (if (boundp 'efs-path-regexp)
668 (string-match efs-path-regexp path)
669 (if (boundp 'ange-ftp-name-format)
670 (string-match (car ange-ftp-name-format) path)))
671 (list (match-string 1 path) (match-string 2 path)
672 (substring path (1+ (match-end 2))))
675 (defun mml-insert-buffer (buffer)
676 "Insert BUFFER at point and quote any MML markup."
678 (narrow-to-region (point) (point))
679 (insert-buffer-substring buffer)
680 (mml-quote-region (point-min) (point-max))
681 (goto-char (point-max))))
684 ;;; Transforming MIME to MML
687 (defun mime-to-mml (&optional handles)
688 "Translate the current buffer (which should be a message) into MML.
689 If HANDLES is non-nil, use it instead reparsing the buffer."
690 ;; First decode the head.
692 (message-narrow-to-head)
693 (mail-decode-encoded-word-region (point-min) (point-max)))
695 (setq handles (mm-dissect-buffer t)))
696 (goto-char (point-min))
697 (search-forward "\n\n" nil t)
698 (delete-region (point) (point-max))
699 (if (stringp (car handles))
700 (mml-insert-mime handles)
701 (mml-insert-mime handles t))
702 (mm-destroy-parts handles)
704 (message-narrow-to-head)
705 ;; Remove them, they are confusing.
706 (message-remove-header "Content-Type")
707 (message-remove-header "MIME-Version")
708 (message-remove-header "Content-Disposition")
709 (message-remove-header "Content-Transfer-Encoding")))
711 (defun mml-to-mime ()
712 "Translate the current buffer from MML to MIME."
713 (message-encode-message-body)
715 (message-narrow-to-headers-or-head)
716 ;; Skip past any From_ headers.
717 (while (looking-at "From ")
719 (let ((mail-parse-charset message-default-charset))
720 (mail-encode-encoded-word-buffer))))
722 (defun mml-insert-mime (handle &optional no-markup)
723 (let (textp buffer mmlp)
724 ;; Determine type and stuff.
725 (unless (stringp (car handle))
726 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
728 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
729 (mm-insert-part handle)
730 (if (setq mmlp (equal (mm-handle-media-type handle)
734 (mml-insert-mml-markup handle nil t t)
735 (unless (and no-markup
736 (equal (mm-handle-media-type handle) "text/plain"))
737 (mml-insert-mml-markup handle buffer textp)))
740 (insert-buffer buffer)
741 (goto-char (point-max))
742 (insert "<#/mml>\n"))
743 ((stringp (car handle))
744 (mapcar 'mml-insert-mime (cdr handle))
745 (insert "<#/multipart>\n"))
747 (let ((charset (mail-content-type-get
748 (mm-handle-type handle) 'charset)))
749 (if (eq charset 'gnus-decoded)
750 (mm-insert-part handle)
751 (insert (mm-decode-string (mm-get-part handle) charset))))
752 (goto-char (point-max)))
754 (insert "<#/part>\n")))))
756 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
757 "Take a MIME handle and insert an MML tag."
758 (if (stringp (car handle))
759 (insert "<#multipart type=" (mm-handle-media-subtype handle)
762 (insert "<#mml type=" (mm-handle-media-type handle))
763 (insert "<#part type=" (mm-handle-media-type handle)))
764 (dolist (elem (append (cdr (mm-handle-type handle))
765 (cdr (mm-handle-disposition handle))))
766 (unless (symbolp (cdr elem))
767 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
768 (when (mm-handle-disposition handle)
769 (insert " disposition=" (car (mm-handle-disposition handle))))
771 (insert " buffer=\"" (buffer-name buffer) "\""))
773 (insert " nofile=yes"))
774 (when (mm-handle-description handle)
775 (insert " description=\"" (mm-handle-description handle) "\""))
778 (defun mml-insert-parameter (&rest parameters)
779 "Insert PARAMETERS in a nice way."
780 (dolist (param parameters)
782 (let ((point (point)))
784 (when (> (current-column) 71)
790 ;;; Mode for inserting and editing MML forms
794 (let ((sign (make-sparse-keymap))
795 (encrypt (make-sparse-keymap))
796 (signpart (make-sparse-keymap))
797 (encryptpart (make-sparse-keymap))
798 (map (make-sparse-keymap))
799 (main (make-sparse-keymap)))
800 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
801 (define-key sign "o" 'mml-secure-message-sign-pgp)
802 (define-key sign "s" 'mml-secure-message-sign-smime)
803 (define-key signpart "p" 'mml-secure-sign-pgpmime)
804 (define-key signpart "o" 'mml-secure-sign-pgp)
805 (define-key signpart "s" 'mml-secure-sign-smime)
806 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
807 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
808 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
809 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
810 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
811 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
812 (define-key map "\C-n" 'mml-unsecure-message)
813 (define-key map "f" 'mml-attach-file)
814 (define-key map "b" 'mml-attach-buffer)
815 (define-key map "e" 'mml-attach-external)
816 (define-key map "q" 'mml-quote-region)
817 (define-key map "m" 'mml-insert-multipart)
818 (define-key map "p" 'mml-insert-part)
819 (define-key map "v" 'mml-validate)
820 (define-key map "P" 'mml-preview)
821 (define-key map "s" sign)
822 (define-key map "S" signpart)
823 (define-key map "c" encrypt)
824 (define-key map "C" encryptpart)
825 ;;(define-key map "n" 'mml-narrow-to-part)
826 ;; `M-m' conflicts with `back-to-indentation'.
827 ;; (define-key main "\M-m" map)
828 (define-key main "\C-c\C-m" map)
832 mml-menu mml-mode-map ""
834 ["Attach File..." mml-attach-file
835 ,@(if (featurep 'xemacs) '(t)
836 '(:help "Attach a file at point"))]
837 ["Attach Buffer..." mml-attach-buffer t]
838 ["Attach External..." mml-attach-external t]
839 ["Insert Part..." mml-insert-part t]
840 ["Insert Multipart..." mml-insert-multipart t]
841 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
842 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
843 ["PGP Sign" mml-secure-message-sign-pgp t]
844 ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
845 ["S/MIME Sign" mml-secure-message-sign-smime t]
846 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
848 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
849 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
850 ["PGP Sign Part" mml-secure-sign-pgp t]
851 ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
852 ["S/MIME Sign Part" mml-secure-sign-smime t]
853 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
854 ["Encrypt/Sign off" mml-unsecure-message t]
855 ;;["Narrow" mml-narrow-to-part t]
856 ["Quote MML" mml-quote-region t]
857 ["Validate MML" mml-validate t]
858 ["Preview" mml-preview t]))
861 "Minor mode for editing MML.")
863 (defun mml-mode (&optional arg)
864 "Minor mode for editing MML.
865 MML is the MIME Meta Language, a minor mode for composing MIME articles.
866 See Info node `(emacs-mime)Composing'.
870 (when (set (make-local-variable 'mml-mode)
871 (if (null arg) (not mml-mode)
872 (> (prefix-numeric-value arg) 0)))
873 (gnus-add-minor-mode 'mml-mode " MML" mml-mode-map)
874 (easy-menu-add mml-menu mml-mode-map)
875 (run-hooks 'mml-mode-hook)))
878 ;;; Helper functions for reading MIME stuff from the minibuffer and
879 ;;; inserting stuff to the buffer.
882 (defun mml-minibuffer-read-file (prompt)
883 (let ((file (read-file-name prompt nil nil t)))
884 ;; Prevent some common errors. This is inspired by similar code in
886 (when (file-directory-p file)
887 (error "%s is a directory, cannot attach" file))
888 (unless (file-exists-p file)
889 (error "No such file: %s" file))
890 (unless (file-readable-p file)
891 (error "Permission denied: %s" file))
894 (defun mml-minibuffer-read-type (name &optional default)
895 (mailcap-parse-mimetypes)
896 (let* ((default (or default
897 (mm-default-file-encoding name)
898 ;; Perhaps here we should check what the file
899 ;; looks like, and offer text/plain if it looks
901 "application/octet-stream"))
902 (string (completing-read
903 (format "Content type (default %s): " default)
904 (mapcar 'list (mailcap-mime-types)))))
905 (if (not (equal string ""))
909 (defun mml-minibuffer-read-description ()
910 (let ((description (read-string "One line description: ")))
911 (when (string-match "\\`[ \t]*\\'" description)
912 (setq description nil))
915 (defun mml-quote-region (beg end)
916 "Quote the MML tags in the region."
920 ;; Temporarily narrow the region to defend from changes
922 (narrow-to-region beg end)
923 (goto-char (point-min))
925 (while (re-search-forward
926 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
927 ;; Insert ! after the #.
928 (goto-char (+ (match-beginning 0) 2))
931 (defun mml-insert-tag (name &rest plist)
932 "Insert an MML tag described by NAME and PLIST."
934 (setq name (symbol-name name)))
937 (let ((key (pop plist))
940 ;; Quote VALUE if it contains suspicious characters.
941 (when (string-match "[\"'\\~/*;() \t\n]" value)
942 (setq value (prin1-to-string value)))
943 (insert (format " %s=%s" key value)))))
946 (defun mml-insert-empty-tag (name &rest plist)
947 "Insert an empty MML tag described by NAME and PLIST."
949 (setq name (symbol-name name)))
950 (apply #'mml-insert-tag name plist)
951 (insert "<#/" name ">\n"))
953 ;;; Attachment functions.
955 (defun mml-attach-file (file &optional type description)
956 "Attach a file to the outgoing MIME message.
957 The file is not inserted or encoded until you send the message with
958 `\\[message-send-and-exit]' or `\\[message-send]'.
960 FILE is the name of the file to attach. TYPE is its content-type, a
961 string of the form \"type/subtype\". DESCRIPTION is a one-line
962 description of the attachment."
964 (let* ((file (mml-minibuffer-read-file "Attach file: "))
965 (type (mml-minibuffer-read-type file))
966 (description (mml-minibuffer-read-description)))
967 (list file type description)))
968 (mml-insert-empty-tag 'part 'type type 'filename file
969 'disposition "attachment" 'description description))
971 (defun mml-attach-buffer (buffer &optional type description)
972 "Attach a buffer to the outgoing MIME message.
973 See `mml-attach-file' for details of operation."
975 (let* ((buffer (read-buffer "Attach buffer: "))
976 (type (mml-minibuffer-read-type buffer "text/plain"))
977 (description (mml-minibuffer-read-description)))
978 (list buffer type description)))
979 (mml-insert-empty-tag 'part 'type type 'buffer buffer
980 'disposition "attachment" 'description description))
982 (defun mml-attach-external (file &optional type description)
983 "Attach an external file into the buffer.
984 FILE is an ange-ftp/efs specification of the part location.
985 TYPE is the MIME type to use."
987 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
988 (type (mml-minibuffer-read-type file))
989 (description (mml-minibuffer-read-description)))
990 (list file type description)))
991 (mml-insert-empty-tag 'external 'type type 'name file
992 'disposition "attachment" 'description description))
994 (defun mml-insert-multipart (&optional type)
995 (interactive (list (completing-read "Multipart type (default mixed): "
996 '(("mixed") ("alternative") ("digest") ("parallel")
997 ("signed") ("encrypted"))
1000 (setq type "mixed"))
1001 (mml-insert-empty-tag "multipart" 'type type)
1004 (defun mml-insert-part (&optional type)
1006 (list (mml-minibuffer-read-type "")))
1007 (mml-insert-tag 'part 'type type 'disposition "inline")
1010 (defun mml-preview (&optional raw)
1011 "Display current buffer with Gnus, in a new buffer.
1012 If RAW, don't highlight the article."
1015 (let* ((buf (current-buffer))
1016 (message-options message-options)
1017 (message-this-is-mail (message-mail-p))
1018 (message-this-is-news (message-news-p))
1019 (message-posting-charset (or (gnus-setup-posting-charset
1021 (message-narrow-to-headers-or-head)
1022 (message-fetch-field "Newsgroups")))
1023 message-posting-charset)))
1024 (message-options-set-recipient)
1025 (switch-to-buffer (generate-new-buffer
1026 (concat (if raw "*Raw MIME preview of "
1027 "*MIME preview of ") (buffer-name))))
1030 (let ((message-deletable-headers (if (message-news-p)
1032 message-deletable-headers)))
1033 (message-generate-headers
1034 (copy-sequence (if (message-news-p)
1035 message-required-news-headers
1036 message-required-mail-headers))))
1037 (if (re-search-forward
1038 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1039 (replace-match "\n"))
1040 (let ((mail-header-separator ""));; mail-header-separator is removed.
1043 (when (fboundp 'set-buffer-multibyte)
1044 (let ((s (buffer-string)))
1045 ;; Insert the content into unibyte buffer.
1047 (mm-disable-multibyte)
1049 (let ((gnus-newsgroup-charset (car message-posting-charset))
1050 gnus-article-prepare-hook gnus-original-article-buffer)
1051 (run-hooks 'gnus-article-decode-hook)
1052 (let ((gnus-newsgroup-name "dummy")
1053 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1054 (gnus-make-hashtable 5))))
1055 (gnus-article-prepare-display))))
1056 ;; Disable article-mode-map.
1058 (setq buffer-read-only t)
1059 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1060 (goto-char (point-min)))))
1062 (defun mml-validate ()
1063 "Validate the current MML document."
1067 (defun mml-tweak-part (cont)
1069 (let ((tweak (cdr (assq 'tweak cont)))
1074 (or (cdr (assoc tweak mml-tweak-function-alist))
1076 (mml-tweak-type-alist
1077 (let ((alist mml-tweak-type-alist)
1078 (type (or (cdr (assq 'type cont)) "text/plain")))
1080 (if (string-match (caar alist) type)
1081 (setq func (cdar alist)
1083 (setq alist (cdr alist)))))))
1087 (let ((alist mml-tweak-sexp-alist))
1089 (if (eval (caar alist))
1090 (funcall (cdar alist) cont))
1091 (setq alist (cdr alist)))))
1094 (defun mml-tweak-externalize-attachments (cont)
1095 "Tweak attached files as external parts."
1096 (let (filename-cons)
1097 (when (and (eq (car cont) 'part)
1098 (not (cdr (assq 'buffer cont)))
1099 (and (setq filename-cons (assq 'filename cont))
1100 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1101 (setcar cont 'external)
1102 (setcar filename-cons 'name))))
1106 ;;; mml.el ends here