1 ;;; mml.el --- A package for parsing and validating MML documents
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, 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"))
44 (autoload 'dnd-get-local-file-name "dnd"))
46 (defcustom mml-content-type-parameters
47 '(name access-type expiration size permission format)
48 "*A list of acceptable parameters in MML tag.
49 These parameters are generated in Content-Type header if exists."
51 :type '(repeat (symbol :tag "Parameter"))
54 (defcustom mml-content-disposition-parameters
55 '(filename creation-date modification-date read-date)
56 "*A list of acceptable parameters in MML tag.
57 These parameters are generated in Content-Disposition header if exists."
59 :type '(repeat (symbol :tag "Parameter"))
62 (defcustom mml-insert-mime-headers-always nil
63 "If non-nil, always put Content-Type: text/plain at top of empty parts.
64 It is necessary to work against a bug in certain clients."
69 (defvar mml-tweak-type-alist nil
70 "A list of (TYPE . FUNCTION) for tweaking MML parts.
71 TYPE is a string containing a regexp to match the MIME type. FUNCTION
72 is a Lisp function which is called with the MML handle to tweak the
73 part. This variable is used only when no TWEAK parameter exists in
76 (defvar mml-tweak-function-alist nil
77 "A list of (NAME . FUNCTION) for tweaking MML parts.
78 NAME is a string containing the name of the TWEAK parameter in the MML
79 handle. FUNCTION is a Lisp function which is called with the MML
80 handle to tweak the part.")
82 (defvar mml-tweak-sexp-alist
83 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
84 "A list of (SEXP . FUNCTION) for tweaking MML parts.
85 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
86 is called. FUNCTION is a Lisp function which is called with the MML
87 handle to tweak the part.")
89 (defvar mml-externalize-attachments nil
90 "*If non-nil, local-file attachments are generated as external parts.")
92 (defvar mml-generate-multipart-alist nil
93 "*Alist of multipart generation functions.
94 Each entry has the form (NAME . FUNCTION), where
95 NAME is a string containing the name of the part (without the
96 leading \"/multipart/\"),
97 FUNCTION is a Lisp function which is called to generate the part.
99 The Lisp function has to supply the appropriate MIME headers and the
100 contents of this part.")
102 (defvar mml-syntax-table
103 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
104 (modify-syntax-entry ?\\ "/" table)
105 (modify-syntax-entry ?< "(" table)
106 (modify-syntax-entry ?> ")" table)
107 (modify-syntax-entry ?@ "w" table)
108 (modify-syntax-entry ?/ "w" table)
109 (modify-syntax-entry ?= " " table)
110 (modify-syntax-entry ?* " " table)
111 (modify-syntax-entry ?\; " " table)
112 (modify-syntax-entry ?\' " " table)
115 (defvar mml-boundary-function 'mml-make-boundary
116 "A function called to suggest a boundary.
117 The function may be called several times, and should try to make a new
118 suggestion each time. The function is called with one parameter,
119 which is a number that says how many times the function has been
120 called for this message.")
122 (defvar mml-confirmation-set nil
123 "A list of symbols, each of which disables some warning.
124 `unknown-encoding': always send messages contain characters with
125 unknown encoding; `use-ascii': always use ASCII for those characters
126 with unknown encoding; `multipart': always send messages with more than
129 (defvar mml-generate-default-type "text/plain"
130 "Content type by which the Content-Type header can be omitted.
131 The Content-Type header will not be put in the MIME part if the type
132 equals the value and there's no parameter (e.g. charset, format, etc.)
133 and `mml-insert-mime-headers-always' is nil. The value will be bound
134 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
135 part. This is for the internal use, you should never modify the value.")
137 (defvar mml-buffer-list nil)
139 (defun mml-generate-new-buffer (name)
140 (let ((buf (generate-new-buffer name)))
141 (push buf mml-buffer-list)
144 (defun mml-destroy-buffers ()
145 (let (kill-buffer-hook)
146 (mapc 'kill-buffer mml-buffer-list)
147 (setq mml-buffer-list nil)))
150 "Parse the current buffer as an MML document."
152 (goto-char (point-min))
153 (with-syntax-table mml-syntax-table
156 (defun mml-parse-1 ()
157 "Parse the current buffer as an MML document."
158 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
159 (while (and (not (eobp))
160 (not (looking-at "<#/multipart")))
162 ((looking-at "<#secure")
163 ;; The secure part is essentially a meta-meta tag, which
164 ;; expands to either a part tag if there are no other parts in
165 ;; the document or a multipart tag if there are other parts
166 ;; included in the message
168 (taginfo (mml-read-tag))
169 (keyfile (cdr (assq 'keyfile taginfo)))
170 (certfile (cdr (assq 'certfile taginfo)))
171 (recipients (cdr (assq 'recipients taginfo)))
172 (sender (cdr (assq 'sender taginfo)))
173 (location (cdr (assq 'tag-location taginfo)))
174 (mode (cdr (assq 'mode taginfo)))
175 (method (cdr (assq 'method taginfo)))
178 (if (re-search-forward
179 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
180 (setq secure-mode "multipart")
181 (setq secure-mode "part")))
184 (re-search-forward "<#secure[^\n]*>\n"))
185 (delete-region (match-beginning 0) (match-end 0))
186 (cond ((string= mode "sign")
187 (setq tags (list "sign" method)))
188 ((string= mode "encrypt")
189 (setq tags (list "encrypt" method)))
190 ((string= mode "signencrypt")
191 (setq tags (list "sign" method "encrypt" method))))
192 (eval `(mml-insert-tag ,secure-mode
194 ,(if keyfile "keyfile")
196 ,(if certfile "certfile")
198 ,(if recipients "recipients")
200 ,(if sender "sender")
203 (goto-char location)))
204 ((looking-at "<#multipart")
205 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
206 ((looking-at "<#external")
207 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
210 (if (or (looking-at "<#part") (looking-at "<#mml"))
211 (setq tag (mml-read-tag)
214 (setq tag (list 'part '(type . "text/plain"))
217 (setq raw (cdr (assq 'raw tag))
219 contents (mml-read-part (eq 'mml (car tag)))
224 (intern (downcase (cdr (assq 'charset tag))))))
226 (mm-find-mime-charset-region point (point)
228 (when (and (not raw) (memq nil charsets))
229 (if (or (memq 'unknown-encoding mml-confirmation-set)
230 (message-options-get 'unknown-encoding)
232 Message contains characters with unknown encoding. Really send? ")
233 (message-options-set 'unknown-encoding t)))
235 (or (memq 'use-ascii mml-confirmation-set)
236 (message-options-get 'use-ascii)
237 (and (y-or-n-p "Use ASCII as charset? ")
238 (message-options-set 'use-ascii t))))
239 (setq charsets (delq nil charsets))
241 (error "Edit your message to remove those characters")))
244 (< (length charsets) 2))
245 (if (or (not no-markup-p)
246 (string-match "[^ \t\r\n]" contents))
247 ;; Don't create blank parts.
248 (push (nconc tag (list (cons 'contents contents)))
250 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
251 tag point (point) use-ascii)))
253 (not (memq 'multipart mml-confirmation-set))
254 (not (message-options-get 'multipart))
255 (not (and (y-or-n-p (format "\
256 A message part needs to be split into %d charset parts. Really send? "
258 (message-options-set 'multipart t))))
259 (error "Edit your message to use only one charset"))
260 (setq struct (nconc nstruct struct)))))))
265 (defun mml-parse-singlepart-with-multiple-charsets
266 (orig-tag beg end &optional use-ascii)
269 (narrow-to-region beg end)
270 (goto-char (point-min))
271 (let ((current (or (mm-mime-charset (mm-charset-after))
272 (and use-ascii 'us-ascii)))
273 charset struct space newline paragraph)
275 (setq charset (mm-mime-charset (mm-charset-after)))
277 ;; The charset remains the same.
278 ((eq charset 'us-ascii))
279 ((or (and use-ascii (not charset))
280 (eq charset current))
284 ;; The initial charset was ascii.
285 ((eq current 'us-ascii)
286 (setq current charset
290 ;; We have a change in charsets.
294 (list (cons 'contents
295 (buffer-substring-no-properties
296 beg (or paragraph newline space (point))))))
298 (setq beg (or paragraph newline space (point))
303 ;; Compute places where it might be nice to break the part.
305 ((memq (following-char) '(? ?\t))
306 (setq space (1+ (point))))
307 ((and (eq (following-char) ?\n)
309 (eq (char-after (1- (point))) ?\n))
310 (setq paragraph (point)))
311 ((eq (following-char) ?\n)
312 (setq newline (1+ (point)))))
314 ;; Do the final part.
315 (unless (= beg (point))
316 (push (append orig-tag
317 (list (cons 'contents
318 (buffer-substring-no-properties
323 (defun mml-read-tag ()
324 "Read a tag and return the contents."
325 (let ((orig-point (point))
326 contents name elem val)
328 (setq name (buffer-substring-no-properties
329 (point) (progn (forward-sexp 1) (point))))
330 (skip-chars-forward " \t\n")
331 (while (not (looking-at ">[ \t]*\n?"))
332 (setq elem (buffer-substring-no-properties
333 (point) (progn (forward-sexp 1) (point))))
334 (skip-chars-forward "= \t\n")
335 (setq val (buffer-substring-no-properties
336 (point) (progn (forward-sexp 1) (point))))
337 (when (string-match "^\"\\(.*\\)\"$" val)
338 (setq val (match-string 1 val)))
339 (push (cons (intern elem) val) contents)
340 (skip-chars-forward " \t\n"))
341 (goto-char (match-end 0))
342 ;; Don't skip the leading space.
343 ;;(skip-chars-forward " \t\n")
344 ;; Put the tag location into the returned contents
345 (setq contents (append (list (cons 'tag-location orig-point)) contents))
346 (cons (intern name) (nreverse contents))))
348 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
349 (let ((str (buffer-substring-no-properties start end))
350 (bufstart start) tmp)
351 (while (setq tmp (text-property-any start end 'hard 't))
352 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
354 (setq start (1+ tmp)))
357 (defun mml-read-part (&optional mml)
358 "Return the buffer up till the next part, multipart or closing part or multipart.
359 If MML is non-nil, return the buffer up till the correspondent mml tag."
360 (let ((beg (point)) (count 1))
361 ;; If the tag ended at the end of the line, we go to the next line.
362 (when (looking-at "[ \t]*\n")
366 (while (and (> count 0) (not (eobp)))
367 (if (re-search-forward "<#\\(/\\)?mml." nil t)
368 (setq count (+ count (if (match-beginning 1) -1 1)))
369 (goto-char (point-max))))
370 (mml-buffer-substring-no-properties-except-hard-newlines
373 (match-beginning 0))))
374 (if (re-search-forward
375 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
377 (mml-buffer-substring-no-properties-except-hard-newlines
378 beg (match-beginning 0))
379 (if (or (not (match-beginning 1))
380 (equal (match-string 2) "multipart"))
381 (goto-char (match-beginning 0))
382 (when (looking-at "[ \t]*\n")
384 (mml-buffer-substring-no-properties-except-hard-newlines
385 beg (goto-char (point-max)))))))
387 (defvar mml-boundary nil)
388 (defvar mml-base-boundary "-=-=")
389 (defvar mml-multipart-number 0)
391 (defun mml-generate-mime ()
392 "Generate a MIME message based on the current MML document."
393 (let ((cont (mml-parse))
394 (mml-multipart-number mml-multipart-number))
398 (if (and (consp (car cont))
400 (mml-generate-mime-1 (car cont))
401 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
405 (defun mml-generate-mime-1 (cont)
406 (let ((mm-use-ultra-safe-encoding
407 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
409 (narrow-to-region (point) (point))
410 (mml-tweak-part cont)
412 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
413 (let* ((raw (cdr (assq 'raw cont)))
414 (filename (cdr (assq 'filename cont)))
415 (type (or (cdr (assq 'type cont))
417 (or (mm-default-file-encoding filename)
418 "application/octet-stream")
420 (charset (cdr (assq 'charset cont)))
421 (coding (mm-charset-to-coding-system charset))
422 encoding flowed coded)
423 (cond ((eq coding 'ascii)
427 (setq charset (intern (downcase charset)))))
429 (member (car (split-string type "/")) '("text" "message")))
433 ((cdr (assq 'buffer cont))
434 (insert-buffer-substring (cdr (assq 'buffer cont))))
436 (not (equal (cdr (assq 'nofile cont)) "yes")))
437 (let ((coding-system-for-read coding))
438 (mm-insert-file-contents filename)))
439 ((eq 'mml (car cont))
440 (insert (cdr (assq 'contents cont))))
443 (narrow-to-region (point) (point))
444 (insert (cdr (assq 'contents cont)))
445 ;; Remove quotes from quoted tags.
446 (goto-char (point-min))
447 (while (re-search-forward
448 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
450 (delete-region (+ (match-beginning 0) 2)
451 (+ (match-beginning 0) 3))))))
453 ((eq (car cont) 'mml)
454 (let ((mml-boundary (mml-compute-boundary cont))
455 ;; It is necessary for the case where this
456 ;; function is called recursively since
457 ;; `m-g-d-t' will be bound to "message/rfc822"
458 ;; when encoding an article to be forwarded.
459 (mml-generate-default-type "text/plain"))
461 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
462 ;; ignore 0x1b, it is part of iso-2022-jp
463 (setq encoding (mm-body-7-or-8))))
464 ((string= (car (split-string type "/")) "message")
465 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
466 ;; ignore 0x1b, it is part of iso-2022-jp
467 (setq encoding (mm-body-7-or-8))))
469 ;; Only perform format=flowed filling on text/plain
470 ;; parts where there either isn't a format parameter
471 ;; in the mml tag or it says "flowed" and there
472 ;; actually are hard newlines in the text.
473 (let (use-hard-newlines)
474 (when (and (string= type "text/plain")
475 (not (string= (cdr (assq 'sign cont)) "pgp"))
476 (or (null (assq 'format cont))
477 (string= (cdr (assq 'format cont))
479 (setq use-hard-newlines
481 (point-min) (point-max) 'hard 't)))
483 ;; Indicate that `mml-insert-mime-headers' should
484 ;; insert a "; format=flowed" string unless the
485 ;; user has already specified it.
486 (setq flowed (null (assq 'format cont)))))
487 (setq charset (mm-encode-body charset))
488 (setq encoding (mm-body-encoding
489 charset (cdr (assq 'encoding cont))))))
490 (setq coded (buffer-string)))
491 (mml-insert-mime-headers cont type charset encoding flowed)
494 (mm-with-unibyte-buffer
496 ((cdr (assq 'buffer cont))
497 (insert (with-current-buffer (cdr (assq 'buffer cont))
498 (mm-with-unibyte-current-buffer
501 (not (equal (cdr (assq 'nofile cont)) "yes")))
502 (let ((coding-system-for-read mm-binary-coding-system))
503 (mm-insert-file-contents filename nil nil nil nil t))
505 (setq charset (mm-coding-system-to-mime-charset
506 (mm-find-buffer-file-coding-system
509 (insert (cdr (assq 'contents cont)))))
510 (setq encoding (mm-encode-buffer type)
511 coded (mm-string-as-multibyte (buffer-string))))
512 (mml-insert-mime-headers cont type charset encoding nil)
514 (mm-with-unibyte-current-buffer
516 ((eq (car cont) 'external)
517 (insert "Content-Type: message/external-body")
518 (let ((parameters (mml-parameter-string
519 cont '(expiration size permission)))
520 (name (cdr (assq 'name cont)))
521 (url (cdr (assq 'url cont))))
523 (setq name (mml-parse-file-name name))
525 (mml-insert-parameter
526 (mail-header-encode-parameter "name" name)
527 "access-type=local-file")
528 (mml-insert-parameter
529 (mail-header-encode-parameter
530 "name" (file-name-nondirectory (nth 2 name)))
531 (mail-header-encode-parameter "site" (nth 1 name))
532 (mail-header-encode-parameter
533 "directory" (file-name-directory (nth 2 name))))
534 (mml-insert-parameter
535 (concat "access-type="
536 (if (member (nth 0 name) '("ftp@" "anonymous@"))
540 (mml-insert-parameter
541 (mail-header-encode-parameter "url" url)
544 (mml-insert-parameter-string
545 cont '(expiration size permission)))
547 (insert "Content-Type: "
548 (or (cdr (assq 'type cont))
550 (or (mm-default-file-encoding name)
551 "application/octet-stream")
554 (insert "Content-ID: " (message-make-message-id) "\n")
555 (insert "Content-Transfer-Encoding: "
556 (or (cdr (assq 'encoding cont)) "binary"))
558 (insert (or (cdr (assq 'contents cont))))
560 ((eq (car cont) 'multipart)
561 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
562 (mml-generate-default-type (if (equal type "digest")
565 (handler (assoc type mml-generate-multipart-alist)))
567 (funcall (cdr handler) cont)
568 ;; No specific handler. Use default one.
569 (let ((mml-boundary (mml-compute-boundary cont)))
570 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
572 (if (cdr (assq 'start cont))
573 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
575 (let ((cont cont) part)
576 (while (setq part (pop cont))
577 ;; Skip `multipart' and attributes.
578 (when (and (consp part) (consp (cdr part)))
579 (insert "\n--" mml-boundary "\n")
580 (mml-generate-mime-1 part))))
581 (insert "\n--" mml-boundary "--\n")))))
583 (error "Invalid element: %S" cont)))
584 ;; handle sign & encrypt tags in a semi-smart way.
585 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
586 (encrypt-item (assoc (cdr (assq 'encrypt cont))
589 (when (or sign-item encrypt-item)
590 (when (setq sender (cdr (assq 'sender cont)))
591 (message-options-set 'mml-sender sender)
592 (message-options-set 'message-sender sender))
593 (if (setq recipients (cdr (assq 'recipients cont)))
594 (message-options-set 'message-recipients recipients))
595 (let ((style (mml-signencrypt-style
596 (first (or sign-item encrypt-item)))))
597 ;; check if: we're both signing & encrypting, both methods
598 ;; are the same (why would they be different?!), and that
599 ;; the signencrypt style allows for combined operation.
600 (if (and sign-item encrypt-item (equal (first sign-item)
601 (first encrypt-item))
602 (equal style 'combined))
603 (funcall (nth 1 encrypt-item) cont t)
604 ;; otherwise, revert to the old behavior.
606 (funcall (nth 1 sign-item) cont))
608 (funcall (nth 1 encrypt-item) cont)))))))))
610 (defun mml-compute-boundary (cont)
611 "Return a unique boundary that does not exist in CONT."
612 (let ((mml-boundary (funcall mml-boundary-function
613 (incf mml-multipart-number))))
614 ;; This function tries again and again until it has found
615 ;; a unique boundary.
616 (while (not (catch 'not-unique
617 (mml-compute-boundary-1 cont))))
620 (defun mml-compute-boundary-1 (cont)
623 ((eq (car cont) 'part)
626 ((cdr (assq 'buffer cont))
627 (insert-buffer-substring (cdr (assq 'buffer cont))))
628 ((and (setq filename (cdr (assq 'filename cont)))
629 (not (equal (cdr (assq 'nofile cont)) "yes")))
630 (mm-insert-file-contents filename nil nil nil nil t))
632 (insert (cdr (assq 'contents cont)))))
633 (goto-char (point-min))
634 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
636 (setq mml-boundary (funcall mml-boundary-function
637 (incf mml-multipart-number)))
638 (throw 'not-unique nil))))
639 ((eq (car cont) 'multipart)
640 (mapc 'mml-compute-boundary-1 (cddr cont))))
643 (defun mml-make-boundary (number)
644 (concat (make-string (% number 60) ?=)
650 (defun mml-insert-mime-headers (cont type charset encoding flowed)
651 (let (parameters id disposition description)
653 (mml-parameter-string
654 cont mml-content-type-parameters))
658 (not (equal type mml-generate-default-type))
659 mml-insert-mime-headers-always)
660 (when (consp charset)
662 "Can't encode a part with several charsets"))
663 (insert "Content-Type: " type)
665 (insert "; " (mail-header-encode-parameter
666 "charset" (symbol-name charset))))
668 (insert "; format=flowed"))
670 (mml-insert-parameter-string
671 cont mml-content-type-parameters))
673 (when (setq id (cdr (assq 'id cont)))
674 (insert "Content-ID: " id "\n"))
676 (mml-parameter-string
677 cont mml-content-disposition-parameters))
678 (when (or (setq disposition (cdr (assq 'disposition cont)))
680 (insert "Content-Disposition: " (or disposition "inline"))
682 (mml-insert-parameter-string
683 cont mml-content-disposition-parameters))
685 (unless (eq encoding '7bit)
686 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
687 (when (setq description (cdr (assq 'description cont)))
688 (insert "Content-Description: "
689 (mail-encode-encoded-word-string description) "\n"))))
691 (defun mml-parameter-string (cont types)
694 (while (setq type (pop types))
695 (when (setq value (cdr (assq type cont)))
696 ;; Strip directory component from the filename parameter.
697 (when (eq type 'filename)
698 (setq value (file-name-nondirectory value)))
699 (setq string (concat string "; "
700 (mail-header-encode-parameter
701 (symbol-name type) value)))))
702 (when (not (zerop (length string)))
705 (defun mml-insert-parameter-string (cont types)
707 (while (setq type (pop types))
708 (when (setq value (cdr (assq type cont)))
709 ;; Strip directory component from the filename parameter.
710 (when (eq type 'filename)
711 (setq value (file-name-nondirectory value)))
712 (mml-insert-parameter
713 (mail-header-encode-parameter
714 (symbol-name type) value))))))
717 (defvar ange-ftp-name-format)
718 (defvar efs-path-regexp))
719 (defun mml-parse-file-name (path)
720 (if (if (boundp 'efs-path-regexp)
721 (string-match efs-path-regexp path)
722 (if (boundp 'ange-ftp-name-format)
723 (string-match (car ange-ftp-name-format) path)))
724 (list (match-string 1 path) (match-string 2 path)
725 (substring path (1+ (match-end 2))))
728 (defun mml-insert-buffer (buffer)
729 "Insert BUFFER at point and quote any MML markup."
731 (narrow-to-region (point) (point))
732 (insert-buffer-substring buffer)
733 (mml-quote-region (point-min) (point-max))
734 (goto-char (point-max))))
737 ;;; Transforming MIME to MML
740 (defun mime-to-mml (&optional handles)
741 "Translate the current buffer (which should be a message) into MML.
742 If HANDLES is non-nil, use it instead reparsing the buffer."
743 ;; First decode the head.
745 (message-narrow-to-head)
746 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
747 (mail-decode-encoded-word-region (point-min) (point-max))))
749 (setq handles (mm-dissect-buffer t)))
750 (goto-char (point-min))
751 (search-forward "\n\n" nil t)
752 (delete-region (point) (point-max))
753 (if (stringp (car handles))
754 (mml-insert-mime handles)
755 (mml-insert-mime handles t))
756 (mm-destroy-parts handles)
758 (message-narrow-to-head)
759 ;; Remove them, they are confusing.
760 (message-remove-header "Content-Type")
761 (message-remove-header "MIME-Version")
762 (message-remove-header "Content-Disposition")
763 (message-remove-header "Content-Transfer-Encoding")))
765 (defun mml-to-mime ()
766 "Translate the current buffer from MML to MIME."
767 (message-encode-message-body)
769 (message-narrow-to-headers-or-head)
770 ;; Skip past any From_ headers.
771 (while (looking-at "From ")
773 (let ((mail-parse-charset message-default-charset))
774 (mail-encode-encoded-word-buffer))))
776 (defun mml-insert-mime (handle &optional no-markup)
777 (let (textp buffer mmlp)
778 ;; Determine type and stuff.
779 (unless (stringp (car handle))
780 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
782 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
783 (mm-insert-part handle)
784 (if (setq mmlp (equal (mm-handle-media-type handle)
788 (mml-insert-mml-markup handle nil t t)
789 (unless (and no-markup
790 (equal (mm-handle-media-type handle) "text/plain"))
791 (mml-insert-mml-markup handle buffer textp)))
794 (insert-buffer-substring buffer)
795 (goto-char (point-max))
796 (insert "<#/mml>\n"))
797 ((stringp (car handle))
798 (mapcar 'mml-insert-mime (cdr handle))
799 (insert "<#/multipart>\n"))
801 (let ((charset (mail-content-type-get
802 (mm-handle-type handle) 'charset))
804 (if (eq charset 'gnus-decoded)
805 (mm-insert-part handle)
806 (insert (mm-decode-string (mm-get-part handle) charset)))
807 (mml-quote-region start (point)))
808 (goto-char (point-max)))
810 (insert "<#/part>\n")))))
812 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
813 "Take a MIME handle and insert an MML tag."
814 (if (stringp (car handle))
816 (insert "<#multipart type=" (mm-handle-media-subtype handle))
817 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
819 (insert " start=\"" start "\"")))
822 (insert "<#mml type=" (mm-handle-media-type handle))
823 (insert "<#part type=" (mm-handle-media-type handle)))
824 (dolist (elem (append (cdr (mm-handle-type handle))
825 (cdr (mm-handle-disposition handle))))
826 (unless (symbolp (cdr elem))
827 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
828 (when (mm-handle-id handle)
829 (insert " id=\"" (mm-handle-id handle) "\""))
830 (when (mm-handle-disposition handle)
831 (insert " disposition=" (car (mm-handle-disposition handle))))
833 (insert " buffer=\"" (buffer-name buffer) "\""))
835 (insert " nofile=yes"))
836 (when (mm-handle-description handle)
837 (insert " description=\"" (mm-handle-description handle) "\""))
840 (defun mml-insert-parameter (&rest parameters)
841 "Insert PARAMETERS in a nice way."
842 (dolist (param parameters)
844 (let ((point (point)))
846 (when (> (current-column) 71)
852 ;;; Mode for inserting and editing MML forms
856 (let ((sign (make-sparse-keymap))
857 (encrypt (make-sparse-keymap))
858 (signpart (make-sparse-keymap))
859 (encryptpart (make-sparse-keymap))
860 (map (make-sparse-keymap))
861 (main (make-sparse-keymap)))
862 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
863 (define-key sign "o" 'mml-secure-message-sign-pgp)
864 (define-key sign "s" 'mml-secure-message-sign-smime)
865 (define-key signpart "p" 'mml-secure-sign-pgpmime)
866 (define-key signpart "o" 'mml-secure-sign-pgp)
867 (define-key signpart "s" 'mml-secure-sign-smime)
868 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
869 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
870 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
871 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
872 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
873 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
874 (define-key map "\C-n" 'mml-unsecure-message)
875 (define-key map "f" 'mml-attach-file)
876 (define-key map "b" 'mml-attach-buffer)
877 (define-key map "e" 'mml-attach-external)
878 (define-key map "q" 'mml-quote-region)
879 (define-key map "m" 'mml-insert-multipart)
880 (define-key map "p" 'mml-insert-part)
881 (define-key map "v" 'mml-validate)
882 (define-key map "P" 'mml-preview)
883 (define-key map "s" sign)
884 (define-key map "S" signpart)
885 (define-key map "c" encrypt)
886 (define-key map "C" encryptpart)
887 ;;(define-key map "n" 'mml-narrow-to-part)
888 ;; `M-m' conflicts with `back-to-indentation'.
889 ;; (define-key main "\M-m" map)
890 (define-key main "\C-c\C-m" map)
894 mml-menu mml-mode-map ""
896 ["Attach File..." mml-attach-file
897 ,@(if (featurep 'xemacs) '(t)
898 '(:help "Attach a file at point"))]
899 ["Attach Buffer..." mml-attach-buffer t]
900 ["Attach External..." mml-attach-external t]
901 ["Insert Part..." mml-insert-part t]
902 ["Insert Multipart..." mml-insert-multipart t]
903 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
904 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
905 ["PGP Sign" mml-secure-message-sign-pgp t]
906 ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
907 ["S/MIME Sign" mml-secure-message-sign-smime t]
908 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
910 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
911 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
912 ["PGP Sign Part" mml-secure-sign-pgp t]
913 ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
914 ["S/MIME Sign Part" mml-secure-sign-smime t]
915 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
916 ["Encrypt/Sign off" mml-unsecure-message t]
917 ;;["Narrow" mml-narrow-to-part t]
918 ["Quote MML" mml-quote-region
919 :active (message-mark-active-p)
920 ,@(if (featurep 'xemacs) nil
921 '(:help "Quote MML tags in region"))]
922 ["Validate MML" mml-validate t]
923 ["Preview" mml-preview t]))
926 "Minor mode for editing MML.")
928 (defun mml-mode (&optional arg)
929 "Minor mode for editing MML.
930 MML is the MIME Meta Language, a minor mode for composing MIME articles.
931 See Info node `(emacs-mime)Composing'.
935 (when (set (make-local-variable 'mml-mode)
936 (if (null arg) (not mml-mode)
937 (> (prefix-numeric-value arg) 0)))
938 (add-minor-mode 'mml-mode " MML" mml-mode-map)
939 (easy-menu-add mml-menu mml-mode-map)
940 (when (boundp 'dnd-protocol-alist)
941 (set (make-local-variable 'dnd-protocol-alist)
942 '(("^file:///" . mml-dnd-attach-file)
943 ("^file://" . dnd-open-file)
944 ("^file:" . mml-dnd-attach-file))))
945 (run-hooks 'mml-mode-hook)))
948 ;;; Helper functions for reading MIME stuff from the minibuffer and
949 ;;; inserting stuff to the buffer.
952 (defun mml-minibuffer-read-file (prompt)
953 (let* ((completion-ignored-extensions nil)
954 (file (read-file-name prompt nil nil t)))
955 ;; Prevent some common errors. This is inspired by similar code in
957 (when (file-directory-p file)
958 (error "%s is a directory, cannot attach" file))
959 (unless (file-exists-p file)
960 (error "No such file: %s" file))
961 (unless (file-readable-p file)
962 (error "Permission denied: %s" file))
965 (defun mml-minibuffer-read-type (name &optional default)
966 (mailcap-parse-mimetypes)
967 (let* ((default (or default
968 (mm-default-file-encoding name)
969 ;; Perhaps here we should check what the file
970 ;; looks like, and offer text/plain if it looks
972 "application/octet-stream"))
973 (string (completing-read
974 (format "Content type (default %s): " default)
975 (mapcar 'list (mailcap-mime-types)))))
976 (if (not (equal string ""))
980 (defun mml-minibuffer-read-description ()
981 (let ((description (read-string "One line description: ")))
982 (when (string-match "\\`[ \t]*\\'" description)
983 (setq description nil))
986 (defun mml-minibuffer-read-disposition (type &optional default)
987 (unless default (setq default
988 (if (and (string-match "\\`text/" type)
989 (not (string-match "\\`text/rtf\\'" type)))
992 (let ((disposition (completing-read
993 (format "Disposition (default %s): " default)
994 '(("attachment") ("inline") (""))
995 nil t nil nil default)))
996 (if (not (equal disposition ""))
1000 (defun mml-quote-region (beg end)
1001 "Quote the MML tags in the region."
1005 ;; Temporarily narrow the region to defend from changes
1006 ;; invalidating END.
1007 (narrow-to-region beg end)
1008 (goto-char (point-min))
1010 (while (re-search-forward
1011 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
1012 ;; Insert ! after the #.
1013 (goto-char (+ (match-beginning 0) 2))
1016 (defun mml-insert-tag (name &rest plist)
1017 "Insert an MML tag described by NAME and PLIST."
1018 (when (symbolp name)
1019 (setq name (symbol-name name)))
1022 (let ((key (pop plist))
1023 (value (pop plist)))
1025 ;; Quote VALUE if it contains suspicious characters.
1026 (when (string-match "[\"'\\~/*;() \t\n]" value)
1027 (setq value (with-output-to-string
1028 (let (print-escape-nonascii)
1030 (insert (format " %s=%s" key value)))))
1033 (defun mml-insert-empty-tag (name &rest plist)
1034 "Insert an empty MML tag described by NAME and PLIST."
1035 (when (symbolp name)
1036 (setq name (symbol-name name)))
1037 (apply #'mml-insert-tag name plist)
1038 (insert "<#/" name ">\n"))
1040 ;;; Attachment functions.
1042 (defun mml-attach-file (file &optional type description disposition)
1043 "Attach a file to the outgoing MIME message.
1044 The file is not inserted or encoded until you send the message with
1045 `\\[message-send-and-exit]' or `\\[message-send]'.
1047 FILE is the name of the file to attach. TYPE is its content-type, a
1048 string of the form \"type/subtype\". DESCRIPTION is a one-line
1049 description of the attachment."
1051 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1052 (type (mml-minibuffer-read-type file))
1053 (description (mml-minibuffer-read-description))
1054 (disposition (mml-minibuffer-read-disposition type)))
1055 (list file type description disposition)))
1056 (mml-insert-empty-tag 'part
1059 'disposition (or disposition "attachment")
1060 'description description))
1062 (defun mml-dnd-attach-file (uri action)
1063 "Attach a drag and drop file."
1064 (let ((file (dnd-get-local-file-name uri t)))
1065 (when (and file (file-regular-p file))
1066 (let* ((type (mml-minibuffer-read-type file))
1067 (description (mml-minibuffer-read-description))
1068 (disposition (mml-minibuffer-read-disposition type)))
1069 (mml-attach-file file type description disposition)))))
1071 (defun mml-attach-buffer (buffer &optional type description)
1072 "Attach a buffer to the outgoing MIME message.
1073 See `mml-attach-file' for details of operation."
1075 (let* ((buffer (read-buffer "Attach buffer: "))
1076 (type (mml-minibuffer-read-type buffer "text/plain"))
1077 (description (mml-minibuffer-read-description)))
1078 (list buffer type description)))
1079 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1080 'disposition "attachment" 'description description))
1082 (defun mml-attach-external (file &optional type description)
1083 "Attach an external file into the buffer.
1084 FILE is an ange-ftp/efs specification of the part location.
1085 TYPE is the MIME type to use."
1087 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1088 (type (mml-minibuffer-read-type file))
1089 (description (mml-minibuffer-read-description)))
1090 (list file type description)))
1091 (mml-insert-empty-tag 'external 'type type 'name file
1092 'disposition "attachment" 'description description))
1094 (defun mml-insert-multipart (&optional type)
1095 (interactive (list (completing-read "Multipart type (default mixed): "
1096 '(("mixed") ("alternative") ("digest") ("parallel")
1097 ("signed") ("encrypted"))
1100 (setq type "mixed"))
1101 (mml-insert-empty-tag "multipart" 'type type)
1104 (defun mml-insert-part (&optional type)
1106 (list (mml-minibuffer-read-type "")))
1107 (mml-insert-tag 'part 'type type 'disposition "inline")
1110 (defun mml-preview-insert-mail-followup-to ()
1111 "Insert a Mail-Followup-To header before previewing an article.
1112 Should be adopted if code in `message-send-mail' is changed."
1113 (when (and (message-mail-p)
1114 (message-subscribed-p)
1115 (not (mail-fetch-field "mail-followup-to"))
1116 (message-make-mail-followup-to))
1117 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1118 (insert (message-make-mail-followup-to))))
1120 (defvar mml-preview-buffer nil)
1122 (defun mml-preview (&optional raw)
1123 "Display current buffer with Gnus, in a new buffer.
1124 If RAW, don't highlight the article."
1126 (setq mml-preview-buffer (generate-new-buffer
1127 (concat (if raw "*Raw MIME preview of "
1128 "*MIME preview of ") (buffer-name))))
1130 (let* ((buf (current-buffer))
1131 (message-options message-options)
1132 (message-this-is-mail (message-mail-p))
1133 (message-this-is-news (message-news-p))
1134 (message-posting-charset (or (gnus-setup-posting-charset
1136 (message-narrow-to-headers-or-head)
1137 (message-fetch-field "Newsgroups")))
1138 message-posting-charset)))
1139 (message-options-set-recipient)
1140 (when (boundp 'gnus-buffers)
1141 (push mml-preview-buffer gnus-buffers))
1144 (set-buffer mml-preview-buffer)
1146 (insert-buffer-substring buf))
1147 (mml-preview-insert-mail-followup-to)
1148 (let ((message-deletable-headers (if (message-news-p)
1150 message-deletable-headers)))
1151 (message-generate-headers
1152 (copy-sequence (if (message-news-p)
1153 message-required-news-headers
1154 message-required-mail-headers))))
1155 (if (re-search-forward
1156 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1157 (replace-match "\n"))
1158 (let ((mail-header-separator ""));; mail-header-separator is removed.
1159 (message-sort-headers)
1162 (when (fboundp 'set-buffer-multibyte)
1163 (let ((s (buffer-string)))
1164 ;; Insert the content into unibyte buffer.
1166 (mm-disable-multibyte)
1168 (let ((gnus-newsgroup-charset (car message-posting-charset))
1169 gnus-article-prepare-hook gnus-original-article-buffer)
1170 (run-hooks 'gnus-article-decode-hook)
1171 (let ((gnus-newsgroup-name "dummy")
1172 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1173 (gnus-make-hashtable 5))))
1174 (gnus-article-prepare-display))))
1175 ;; Disable article-mode-map.
1177 (gnus-make-local-hook 'kill-buffer-hook)
1178 (add-hook 'kill-buffer-hook
1180 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1181 (setq buffer-read-only t)
1182 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1183 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1187 (widget-button-press (point))))
1188 (local-set-key gnus-mouse-2
1191 (widget-button-press (widget-event-point event) event)))
1192 (goto-char (point-min))))
1193 (if (and (boundp 'gnus-buffer-configuration)
1194 (assq 'mml-preview gnus-buffer-configuration))
1195 (let ((gnus-message-buffer (current-buffer)))
1196 (gnus-configure-windows 'mml-preview))
1197 (pop-to-buffer mml-preview-buffer)))
1199 (defun mml-validate ()
1200 "Validate the current MML document."
1204 (defun mml-tweak-part (cont)
1206 (let ((tweak (cdr (assq 'tweak cont)))
1211 (or (cdr (assoc tweak mml-tweak-function-alist))
1213 (mml-tweak-type-alist
1214 (let ((alist mml-tweak-type-alist)
1215 (type (or (cdr (assq 'type cont)) "text/plain")))
1217 (if (string-match (caar alist) type)
1218 (setq func (cdar alist)
1220 (setq alist (cdr alist)))))))
1224 (let ((alist mml-tweak-sexp-alist))
1226 (if (eval (caar alist))
1227 (funcall (cdar alist) cont))
1228 (setq alist (cdr alist)))))
1231 (defun mml-tweak-externalize-attachments (cont)
1232 "Tweak attached files as external parts."
1233 (let (filename-cons)
1234 (when (and (eq (car cont) 'part)
1235 (not (cdr (assq 'buffer cont)))
1236 (and (setq filename-cons (assq 'filename cont))
1237 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1238 (setcar cont 'external)
1239 (setcar filename-cons 'name))))
1243 ;;; mml.el ends here