1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
33 (eval-when-compile (require 'cl
))
36 (autoload 'message-make-message-id
"message")
37 (autoload 'gnus-setup-posting-charset
"gnus-msg")
38 (autoload 'gnus-add-minor-mode
"gnus-ems")
39 (autoload 'gnus-make-local-hook
"gnus-util")
40 (autoload 'message-fetch-field
"message")
41 (autoload 'message-info
"message")
42 (autoload 'fill-flowed-encode
"flow-fill")
43 (autoload 'message-posting-charset
"message"))
45 (defvar gnus-article-mime-handles
)
47 (defvar gnus-newsrc-hashtb
)
48 (defvar message-default-charset
)
49 (defvar message-deletable-headers
)
50 (defvar message-options
)
51 (defvar message-posting-charset
)
52 (defvar message-required-mail-headers
)
53 (defvar message-required-news-headers
)
55 (defcustom mml-content-type-parameters
56 '(name access-type expiration size permission format
)
57 "*A list of acceptable parameters in MML tag.
58 These parameters are generated in Content-Type header if exists."
60 :type
'(repeat (symbol :tag
"Parameter"))
63 (defcustom mml-content-disposition-parameters
64 '(filename creation-date modification-date read-date
)
65 "*A list of acceptable parameters in MML tag.
66 These parameters are generated in Content-Disposition header if exists."
68 :type
'(repeat (symbol :tag
"Parameter"))
71 (defcustom mml-insert-mime-headers-always nil
72 "If non-nil, always put Content-Type: text/plain at top of empty parts.
73 It is necessary to work against a bug in certain clients."
78 (defvar mml-tweak-type-alist nil
79 "A list of (TYPE . FUNCTION) for tweaking MML parts.
80 TYPE is a string containing a regexp to match the MIME type. FUNCTION
81 is a Lisp function which is called with the MML handle to tweak the
82 part. This variable is used only when no TWEAK parameter exists in
85 (defvar mml-tweak-function-alist nil
86 "A list of (NAME . FUNCTION) for tweaking MML parts.
87 NAME is a string containing the name of the TWEAK parameter in the MML
88 handle. FUNCTION is a Lisp function which is called with the MML
89 handle to tweak the part.")
91 (defvar mml-tweak-sexp-alist
92 '((mml-externalize-attachments . mml-tweak-externalize-attachments
))
93 "A list of (SEXP . FUNCTION) for tweaking MML parts.
94 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
95 is called. FUNCTION is a Lisp function which is called with the MML
96 handle to tweak the part.")
98 (defvar mml-externalize-attachments nil
99 "*If non-nil, local-file attachments are generated as external parts.")
101 (defvar mml-generate-multipart-alist nil
102 "*Alist of multipart generation functions.
103 Each entry has the form (NAME . FUNCTION), where
104 NAME is a string containing the name of the part (without the
105 leading \"/multipart/\"),
106 FUNCTION is a Lisp function which is called to generate the part.
108 The Lisp function has to supply the appropriate MIME headers and the
109 contents of this part.")
111 (defvar mml-syntax-table
112 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
113 (modify-syntax-entry ?
\\ "/" table
)
114 (modify-syntax-entry ?
< "(" table
)
115 (modify-syntax-entry ?
> ")" table
)
116 (modify-syntax-entry ?
@ "w" table
)
117 (modify-syntax-entry ?
/ "w" table
)
118 (modify-syntax-entry ?
= " " table
)
119 (modify-syntax-entry ?
* " " table
)
120 (modify-syntax-entry ?\
; " " table)
121 (modify-syntax-entry ?
\' " " table
)
124 (defvar mml-boundary-function
'mml-make-boundary
125 "A function called to suggest a boundary.
126 The function may be called several times, and should try to make a new
127 suggestion each time. The function is called with one parameter,
128 which is a number that says how many times the function has been
129 called for this message.")
131 (defvar mml-confirmation-set nil
132 "A list of symbols, each of which disables some warning.
133 `unknown-encoding': always send messages contain characters with
134 unknown encoding; `use-ascii': always use ASCII for those characters
135 with unknown encoding; `multipart': always send messages with more than
138 (defvar mml-generate-default-type
"text/plain"
139 "Content type by which the Content-Type header can be omitted.
140 The Content-Type header will not be put in the MIME part if the type
141 equals the value and there's no parameter (e.g. charset, format, etc.)
142 and `mml-insert-mime-headers-always' is nil. The value will be bound
143 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
144 part. This is for the internal use, you should never modify the value.")
146 (defvar mml-buffer-list nil
)
148 (defun mml-generate-new-buffer (name)
149 (let ((buf (generate-new-buffer name
)))
150 (push buf mml-buffer-list
)
153 (defun mml-destroy-buffers ()
154 (let (kill-buffer-hook)
155 (mapcar 'kill-buffer mml-buffer-list
)
156 (setq mml-buffer-list nil
)))
159 "Parse the current buffer as an MML document."
161 (goto-char (point-min))
162 (let ((table (syntax-table)))
165 (set-syntax-table mml-syntax-table
)
167 (set-syntax-table table
)))))
169 (defun mml-parse-1 ()
170 "Parse the current buffer as an MML document."
171 (let (struct tag point contents charsets warn use-ascii no-markup-p raw
)
172 (while (and (not (eobp))
173 (not (looking-at "<#/multipart")))
175 ((looking-at "<#secure")
176 ;; The secure part is essentially a meta-meta tag, which
177 ;; expands to either a part tag if there are no other parts in
178 ;; the document or a multipart tag if there are other parts
179 ;; included in the message
181 (taginfo (mml-read-tag))
182 (recipients (cdr (assq 'recipients taginfo
)))
183 (sender (cdr (assq 'sender taginfo
)))
184 (location (cdr (assq 'tag-location taginfo
)))
185 (mode (cdr (assq 'mode taginfo
)))
186 (method (cdr (assq 'method taginfo
)))
191 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t
)
192 (setq secure-mode
"multipart")
193 (setq secure-mode
"part")))
196 (re-search-forward "<#secure[^\n]*>\n"))
197 (delete-region (match-beginning 0) (match-end 0))
198 (cond ((string= mode
"sign")
199 (setq tags
(list "sign" method
)))
200 ((string= mode
"encrypt")
201 (setq tags
(list "encrypt" method
)))
202 ((string= mode
"signencrypt")
203 (setq tags
(list "sign" method
"encrypt" method
))))
204 (eval `(mml-insert-tag ,secure-mode
206 ,(if recipients
"recipients")
208 ,(if sender
"sender")
211 (goto-char location
)))
212 ((looking-at "<#multipart")
213 (push (nconc (mml-read-tag) (mml-parse-1)) struct
))
214 ((looking-at "<#external")
215 (push (nconc (mml-read-tag) (list (cons 'contents
(mml-read-part))))
218 (if (or (looking-at "<#part") (looking-at "<#mml"))
219 (setq tag
(mml-read-tag)
222 (setq tag
(list 'part
'(type .
"text/plain"))
225 (setq raw
(cdr (assq 'raw tag
))
227 contents
(mml-read-part (eq 'mml
(car tag
)))
232 (intern (downcase (cdr (assq 'charset tag
))))))
234 (mm-find-mime-charset-region point
(point)
236 (when (and (not raw
) (memq nil charsets
))
237 (if (or (memq 'unknown-encoding mml-confirmation-set
)
238 (message-options-get 'unknown-encoding
)
240 Message contains characters with unknown encoding. Really send? ")
241 (message-options-set 'unknown-encoding t
)))
243 (or (memq 'use-ascii mml-confirmation-set
)
244 (message-options-get 'use-ascii
)
245 (and (y-or-n-p "Use ASCII as charset? ")
246 (message-options-set 'use-ascii t
))))
247 (setq charsets
(delq nil charsets
))
249 (error "Edit your message to remove those characters")))
252 (< (length charsets
) 2))
253 (if (or (not no-markup-p
)
254 (string-match "[^ \t\r\n]" contents
))
255 ;; Don't create blank parts.
256 (push (nconc tag
(list (cons 'contents contents
)))
258 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
259 tag point
(point) use-ascii
)))
261 (not (memq 'multipart mml-confirmation-set
))
262 (not (message-options-get 'multipart
))
263 (not (and (y-or-n-p (format "\
264 A message part needs to be split into %d charset parts. Really send? "
266 (message-options-set 'multipart t
))))
267 (error "Edit your message to use only one charset"))
268 (setq struct
(nconc nstruct struct
)))))))
273 (defun mml-parse-singlepart-with-multiple-charsets
274 (orig-tag beg end
&optional use-ascii
)
277 (narrow-to-region beg end
)
278 (goto-char (point-min))
279 (let ((current (or (mm-mime-charset (mm-charset-after))
280 (and use-ascii
'us-ascii
)))
281 charset struct space newline paragraph
)
283 (setq charset
(mm-mime-charset (mm-charset-after)))
285 ;; The charset remains the same.
286 ((eq charset
'us-ascii
))
287 ((or (and use-ascii
(not charset
))
288 (eq charset current
))
292 ;; The initial charset was ascii.
293 ((eq current
'us-ascii
)
294 (setq current charset
298 ;; We have a change in charsets.
302 (list (cons 'contents
303 (buffer-substring-no-properties
304 beg
(or paragraph newline space
(point))))))
306 (setq beg
(or paragraph newline space
(point))
311 ;; Compute places where it might be nice to break the part.
313 ((memq (following-char) '(? ?
\t))
314 (setq space
(1+ (point))))
315 ((and (eq (following-char) ?
\n)
317 (eq (char-after (1- (point))) ?
\n))
318 (setq paragraph
(point)))
319 ((eq (following-char) ?
\n)
320 (setq newline
(1+ (point)))))
322 ;; Do the final part.
323 (unless (= beg
(point))
324 (push (append orig-tag
325 (list (cons 'contents
326 (buffer-substring-no-properties
331 (defun mml-read-tag ()
332 "Read a tag and return the contents."
333 (let ((orig-point (point))
334 contents name elem val
)
336 (setq name
(buffer-substring-no-properties
337 (point) (progn (forward-sexp 1) (point))))
338 (skip-chars-forward " \t\n")
339 (while (not (looking-at ">[ \t]*\n?"))
340 (setq elem
(buffer-substring-no-properties
341 (point) (progn (forward-sexp 1) (point))))
342 (skip-chars-forward "= \t\n")
343 (setq val
(buffer-substring-no-properties
344 (point) (progn (forward-sexp 1) (point))))
345 (when (string-match "^\"\\(.*\\)\"$" val
)
346 (setq val
(match-string 1 val
)))
347 (push (cons (intern elem
) val
) contents
)
348 (skip-chars-forward " \t\n"))
349 (goto-char (match-end 0))
350 ;; Don't skip the leading space.
351 ;;(skip-chars-forward " \t\n")
352 ;; Put the tag location into the returned contents
353 (setq contents
(append (list (cons 'tag-location orig-point
)) contents
))
354 (cons (intern name
) (nreverse contents
))))
356 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end
)
357 (let ((str (buffer-substring-no-properties start end
))
358 (bufstart start
) tmp
)
359 (while (setq tmp
(text-property-any start end
'hard
't
))
360 (set-text-properties (- tmp bufstart
) (- tmp bufstart -
1)
362 (setq start
(1+ tmp
)))
365 (defun mml-read-part (&optional mml
)
366 "Return the buffer up till the next part, multipart or closing part or multipart.
367 If MML is non-nil, return the buffer up till the correspondent mml tag."
368 (let ((beg (point)) (count 1))
369 ;; If the tag ended at the end of the line, we go to the next line.
370 (when (looking-at "[ \t]*\n")
374 (while (and (> count
0) (not (eobp)))
375 (if (re-search-forward "<#\\(/\\)?mml." nil t
)
376 (setq count
(+ count
(if (match-beginning 1) -
1 1)))
377 (goto-char (point-max))))
378 (mml-buffer-substring-no-properties-except-hard-newlines
381 (match-beginning 0))))
382 (if (re-search-forward
383 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t
)
385 (mml-buffer-substring-no-properties-except-hard-newlines
386 beg
(match-beginning 0))
387 (if (or (not (match-beginning 1))
388 (equal (match-string 2) "multipart"))
389 (goto-char (match-beginning 0))
390 (when (looking-at "[ \t]*\n")
392 (mml-buffer-substring-no-properties-except-hard-newlines
393 beg
(goto-char (point-max)))))))
395 (defvar mml-boundary nil
)
396 (defvar mml-base-boundary
"-=-=")
397 (defvar mml-multipart-number
0)
399 (defun mml-generate-mime ()
400 "Generate a MIME message based on the current MML document."
401 (let ((cont (mml-parse))
402 (mml-multipart-number mml-multipart-number
))
406 (if (and (consp (car cont
))
408 (mml-generate-mime-1 (car cont
))
409 (mml-generate-mime-1 (nconc (list 'multipart
'(type .
"mixed"))
413 (defun mml-generate-mime-1 (cont)
414 (let ((mm-use-ultra-safe-encoding
415 (or mm-use-ultra-safe-encoding
(assq 'sign cont
))))
417 (narrow-to-region (point) (point))
418 (mml-tweak-part cont
)
420 ((or (eq (car cont
) 'part
) (eq (car cont
) 'mml
))
421 (let* ((raw (cdr (assq 'raw cont
)))
422 (filename (cdr (assq 'filename cont
)))
423 (type (or (cdr (assq 'type cont
))
425 (or (mm-default-file-encoding filename
)
426 "application/octet-stream")
428 coded encoding charset flowed
)
430 (member (car (split-string type
"/")) '("text" "message")))
433 (setq charset
(mm-charset-to-coding-system
434 (cdr (assq 'charset cont
))))
435 (when (eq charset
'ascii
)
438 ((cdr (assq 'buffer cont
))
439 (insert-buffer-substring (cdr (assq 'buffer cont
))))
441 (not (equal (cdr (assq 'nofile cont
)) "yes")))
442 (let ((coding-system-for-read charset
))
443 (mm-insert-file-contents filename
)))
444 ((eq 'mml
(car cont
))
445 (insert (cdr (assq 'contents cont
))))
448 (narrow-to-region (point) (point))
449 (insert (cdr (assq 'contents cont
)))
450 ;; Remove quotes from quoted tags.
451 (goto-char (point-min))
452 (while (re-search-forward
453 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
455 (delete-region (+ (match-beginning 0) 2)
456 (+ (match-beginning 0) 3))))))
458 ((eq (car cont
) 'mml
)
459 (let ((mml-boundary (mml-compute-boundary cont
))
460 ;; It is necessary for the case where this
461 ;; function is called recursively since
462 ;; `m-g-d-t' will be bound to "message/rfc822"
463 ;; when encoding an article to be forwarded.
464 (mml-generate-default-type "text/plain"))
466 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
467 ;; ignore 0x1b, it is part of iso-2022-jp
468 (setq encoding
(mm-body-7-or-8))))
469 ((string= (car (split-string type
"/")) "message")
470 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
471 ;; ignore 0x1b, it is part of iso-2022-jp
472 (setq encoding
(mm-body-7-or-8))))
474 ;; Only perform format=flowed filling on text/plain
475 ;; parts where there either isn't a format parameter
476 ;; in the mml tag or it says "flowed" and there
477 ;; actually are hard newlines in the text.
478 (let (use-hard-newlines)
479 (when (and (string= type
"text/plain")
480 (not (string= (cdr (assq 'sign cont
)) "pgp"))
481 (or (null (assq 'format cont
))
482 (string= (cdr (assq 'format cont
))
484 (setq use-hard-newlines
486 (point-min) (point-max) 'hard
't
)))
488 ;; Indicate that `mml-insert-mime-headers' should
489 ;; insert a "; format=flowed" string unless the
490 ;; user has already specified it.
491 (setq flowed
(null (assq 'format cont
)))))
492 (setq charset
(mm-encode-body charset
))
493 (setq encoding
(mm-body-encoding
494 charset
(cdr (assq 'encoding cont
))))))
495 (setq coded
(buffer-string)))
496 (mml-insert-mime-headers cont type charset encoding flowed
)
499 (mm-with-unibyte-buffer
501 ((cdr (assq 'buffer cont
))
502 (insert (with-current-buffer (cdr (assq 'buffer cont
))
503 (mm-with-unibyte-current-buffer
506 (not (equal (cdr (assq 'nofile cont
)) "yes")))
507 (let ((coding-system-for-read mm-binary-coding-system
))
508 (mm-insert-file-contents filename nil nil nil nil t
)))
510 (insert (cdr (assq 'contents cont
)))))
511 (setq encoding
(mm-encode-buffer type
)
512 coded
(mm-string-as-multibyte (buffer-string))))
513 (mml-insert-mime-headers cont type charset encoding nil
)
515 (mm-with-unibyte-current-buffer
517 ((eq (car cont
) 'external
)
518 (insert "Content-Type: message/external-body")
519 (let ((parameters (mml-parameter-string
520 cont
'(expiration size permission
)))
521 (name (cdr (assq 'name cont
)))
522 (url (cdr (assq 'url cont
))))
524 (setq name
(mml-parse-file-name name
))
526 (mml-insert-parameter
527 (mail-header-encode-parameter "name" name
)
528 "access-type=local-file")
529 (mml-insert-parameter
530 (mail-header-encode-parameter
531 "name" (file-name-nondirectory (nth 2 name
)))
532 (mail-header-encode-parameter "site" (nth 1 name
))
533 (mail-header-encode-parameter
534 "directory" (file-name-directory (nth 2 name
))))
535 (mml-insert-parameter
536 (concat "access-type="
537 (if (member (nth 0 name
) '("ftp@" "anonymous@"))
541 (mml-insert-parameter
542 (mail-header-encode-parameter "url" url
)
545 (mml-insert-parameter-string
546 cont
'(expiration size permission
)))
548 (insert "Content-Type: "
549 (or (cdr (assq 'type cont
))
551 (or (mm-default-file-encoding name
)
552 "application/octet-stream")
555 (insert "Content-ID: " (message-make-message-id) "\n")
556 (insert "Content-Transfer-Encoding: "
557 (or (cdr (assq 'encoding cont
)) "binary"))
559 (insert (or (cdr (assq 'contents cont
))))
561 ((eq (car cont
) 'multipart
)
562 (let* ((type (or (cdr (assq 'type cont
)) "mixed"))
563 (mml-generate-default-type (if (equal type
"digest")
566 (handler (assoc type mml-generate-multipart-alist
)))
568 (funcall (cdr handler
) cont
)
569 ;; No specific handler. Use default one.
570 (let ((mml-boundary (mml-compute-boundary cont
)))
571 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
573 (if (cdr (assq 'start cont
))
574 (format "; start=\"%s\"\n" (cdr (assq 'start cont
)))
576 (let ((cont cont
) part
)
577 (while (setq part
(pop cont
))
578 ;; Skip `multipart' and attributes.
579 (when (and (consp part
) (consp (cdr part
)))
580 (insert "\n--" mml-boundary
"\n")
581 (mml-generate-mime-1 part
)
582 (goto-char (point-max)))))
583 (insert "\n--" mml-boundary
"--\n")))))
585 (error "Invalid element: %S" cont
)))
586 ;; handle sign & encrypt tags in a semi-smart way.
587 (let ((sign-item (assoc (cdr (assq 'sign cont
)) mml-sign-alist
))
588 (encrypt-item (assoc (cdr (assq 'encrypt cont
))
591 (when (or sign-item encrypt-item
)
592 (when (setq sender
(cdr (assq 'sender cont
)))
593 (message-options-set 'mml-sender sender
)
594 (message-options-set 'message-sender sender
))
595 (if (setq recipients
(cdr (assq 'recipients cont
)))
596 (message-options-set 'message-recipients recipients
))
597 (let ((style (mml-signencrypt-style
598 (first (or sign-item encrypt-item
)))))
599 ;; check if: we're both signing & encrypting, both methods
600 ;; are the same (why would they be different?!), and that
601 ;; the signencrypt style allows for combined operation.
602 (if (and sign-item encrypt-item
(equal (first sign-item
)
603 (first encrypt-item
))
604 (equal style
'combined
))
605 (funcall (nth 1 encrypt-item
) cont t
)
606 ;; otherwise, revert to the old behavior.
608 (funcall (nth 1 sign-item
) cont
))
610 (funcall (nth 1 encrypt-item
) cont
)))))))))
612 (defun mml-compute-boundary (cont)
613 "Return a unique boundary that does not exist in CONT."
614 (let ((mml-boundary (funcall mml-boundary-function
615 (incf mml-multipart-number
))))
616 ;; This function tries again and again until it has found
617 ;; a unique boundary.
618 (while (not (catch 'not-unique
619 (mml-compute-boundary-1 cont
))))
622 (defun mml-compute-boundary-1 (cont)
625 ((eq (car cont
) 'part
)
628 ((cdr (assq 'buffer cont
))
629 (insert-buffer-substring (cdr (assq 'buffer cont
))))
630 ((and (setq filename
(cdr (assq 'filename cont
)))
631 (not (equal (cdr (assq 'nofile cont
)) "yes")))
632 (mm-insert-file-contents filename nil nil nil nil t
))
634 (insert (cdr (assq 'contents cont
)))))
635 (goto-char (point-min))
636 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary
))
638 (setq mml-boundary
(funcall mml-boundary-function
639 (incf mml-multipart-number
)))
640 (throw 'not-unique nil
))))
641 ((eq (car cont
) 'multipart
)
642 (mapcar 'mml-compute-boundary-1
(cddr cont
))))
645 (defun mml-make-boundary (number)
646 (concat (make-string (% number
60) ?
=)
652 (defun mml-insert-mime-headers (cont type charset encoding flowed
)
653 (let (parameters id disposition description
)
655 (mml-parameter-string
656 cont mml-content-type-parameters
))
660 (not (equal type mml-generate-default-type
))
661 mml-insert-mime-headers-always
)
662 (when (consp charset
)
664 "Can't encode a part with several charsets"))
665 (insert "Content-Type: " type
)
667 (insert "; " (mail-header-encode-parameter
668 "charset" (symbol-name charset
))))
670 (insert "; format=flowed"))
672 (mml-insert-parameter-string
673 cont mml-content-type-parameters
))
675 (when (setq id
(cdr (assq 'id cont
)))
676 (insert "Content-ID: " id
"\n"))
678 (mml-parameter-string
679 cont mml-content-disposition-parameters
))
680 (when (or (setq disposition
(cdr (assq 'disposition cont
)))
682 (insert "Content-Disposition: " (or disposition
"inline"))
684 (mml-insert-parameter-string
685 cont mml-content-disposition-parameters
))
687 (unless (eq encoding
'7bit
)
688 (insert (format "Content-Transfer-Encoding: %s\n" encoding
)))
689 (when (setq description
(cdr (assq 'description cont
)))
690 (insert "Content-Description: "
691 (mail-encode-encoded-word-string description
) "\n"))))
693 (defun mml-parameter-string (cont types
)
696 (while (setq type
(pop types
))
697 (when (setq value
(cdr (assq type cont
)))
698 ;; Strip directory component from the filename parameter.
699 (when (eq type
'filename
)
700 (setq value
(file-name-nondirectory value
)))
701 (setq string
(concat string
"; "
702 (mail-header-encode-parameter
703 (symbol-name type
) value
)))))
704 (when (not (zerop (length string
)))
707 (defun mml-insert-parameter-string (cont types
)
709 (while (setq type
(pop types
))
710 (when (setq value
(cdr (assq type cont
)))
711 ;; Strip directory component from the filename parameter.
712 (when (eq type
'filename
)
713 (setq value
(file-name-nondirectory value
)))
714 (mml-insert-parameter
715 (mail-header-encode-parameter
716 (symbol-name type
) value
))))))
719 (defvar ange-ftp-name-format
)
720 (defvar efs-path-regexp
))
721 (defun mml-parse-file-name (path)
722 (if (if (boundp 'efs-path-regexp
)
723 (string-match efs-path-regexp path
)
724 (if (boundp 'ange-ftp-name-format
)
725 (string-match (car ange-ftp-name-format
) path
)))
726 (list (match-string 1 path
) (match-string 2 path
)
727 (substring path
(1+ (match-end 2))))
730 (defun mml-insert-buffer (buffer)
731 "Insert BUFFER at point and quote any MML markup."
733 (narrow-to-region (point) (point))
734 (insert-buffer-substring buffer
)
735 (mml-quote-region (point-min) (point-max))
736 (goto-char (point-max))))
739 ;;; Transforming MIME to MML
742 (defun mime-to-mml (&optional handles
)
743 "Translate the current buffer (which should be a message) into MML.
744 If HANDLES is non-nil, use it instead reparsing the buffer."
745 ;; First decode the head.
747 (message-narrow-to-head)
748 (let ((rfc2047-quote-decoded-words-containing-tspecials t
))
749 (mail-decode-encoded-word-region (point-min) (point-max))))
751 (setq handles
(mm-dissect-buffer t
)))
752 (goto-char (point-min))
753 (search-forward "\n\n" nil t
)
754 (delete-region (point) (point-max))
755 (if (stringp (car handles
))
756 (mml-insert-mime handles
)
757 (mml-insert-mime handles t
))
758 (mm-destroy-parts handles
)
760 (message-narrow-to-head)
761 ;; Remove them, they are confusing.
762 (message-remove-header "Content-Type")
763 (message-remove-header "MIME-Version")
764 (message-remove-header "Content-Disposition")
765 (message-remove-header "Content-Transfer-Encoding")))
767 (defun mml-to-mime ()
768 "Translate the current buffer from MML to MIME."
769 (message-encode-message-body)
771 (message-narrow-to-headers-or-head)
772 ;; Skip past any From_ headers.
773 (while (looking-at "From ")
775 (let ((mail-parse-charset message-default-charset
))
776 (mail-encode-encoded-word-buffer))))
778 (defun mml-insert-mime (handle &optional no-markup
)
779 (let (textp buffer mmlp
)
780 ;; Determine type and stuff.
781 (unless (stringp (car handle
))
782 (unless (setq textp
(equal (mm-handle-media-supertype handle
) "text"))
784 (set-buffer (setq buffer
(mml-generate-new-buffer " *mml*")))
785 (mm-insert-part handle
)
786 (if (setq mmlp
(equal (mm-handle-media-type handle
)
790 (mml-insert-mml-markup handle nil t t
)
791 (unless (and no-markup
792 (equal (mm-handle-media-type handle
) "text/plain"))
793 (mml-insert-mml-markup handle buffer textp
)))
796 (insert-buffer-substring buffer
)
797 (goto-char (point-max))
798 (insert "<#/mml>\n"))
799 ((stringp (car handle
))
800 (mapcar 'mml-insert-mime
(cdr handle
))
801 (insert "<#/multipart>\n"))
803 (let ((charset (mail-content-type-get
804 (mm-handle-type handle
) 'charset
))
806 (if (eq charset
'gnus-decoded
)
807 (mm-insert-part handle
)
808 (insert (mm-decode-string (mm-get-part handle
) charset
)))
809 (mml-quote-region start
(point)))
810 (goto-char (point-max)))
812 (insert "<#/part>\n")))))
814 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp
)
815 "Take a MIME handle and insert an MML tag."
816 (if (stringp (car handle
))
818 (insert "<#multipart type=" (mm-handle-media-subtype handle
))
819 (let ((start (mm-handle-multipart-ctl-parameter handle
'start
)))
821 (insert " start=\"" start
"\"")))
824 (insert "<#mml type=" (mm-handle-media-type handle
))
825 (insert "<#part type=" (mm-handle-media-type handle
)))
826 (dolist (elem (append (cdr (mm-handle-type handle
))
827 (cdr (mm-handle-disposition handle
))))
828 (unless (symbolp (cdr elem
))
829 (insert " " (symbol-name (car elem
)) "=\"" (cdr elem
) "\"")))
830 (when (mm-handle-id handle
)
831 (insert " id=\"" (mm-handle-id handle
) "\""))
832 (when (mm-handle-disposition handle
)
833 (insert " disposition=" (car (mm-handle-disposition handle
))))
835 (insert " buffer=\"" (buffer-name buffer
) "\""))
837 (insert " nofile=yes"))
838 (when (mm-handle-description handle
)
839 (insert " description=\"" (mm-handle-description handle
) "\""))
842 (defun mml-insert-parameter (&rest parameters
)
843 "Insert PARAMETERS in a nice way."
844 (dolist (param parameters
)
846 (let ((point (point)))
848 (when (> (current-column) 71)
854 ;;; Mode for inserting and editing MML forms
858 (let ((sign (make-sparse-keymap))
859 (encrypt (make-sparse-keymap))
860 (signpart (make-sparse-keymap))
861 (encryptpart (make-sparse-keymap))
862 (map (make-sparse-keymap))
863 (main (make-sparse-keymap)))
864 (define-key sign
"p" 'mml-secure-message-sign-pgpmime
)
865 (define-key sign
"o" 'mml-secure-message-sign-pgp
)
866 (define-key sign
"s" 'mml-secure-message-sign-smime
)
867 (define-key signpart
"p" 'mml-secure-sign-pgpmime
)
868 (define-key signpart
"o" 'mml-secure-sign-pgp
)
869 (define-key signpart
"s" 'mml-secure-sign-smime
)
870 (define-key encrypt
"p" 'mml-secure-message-encrypt-pgpmime
)
871 (define-key encrypt
"o" 'mml-secure-message-encrypt-pgp
)
872 (define-key encrypt
"s" 'mml-secure-message-encrypt-smime
)
873 (define-key encryptpart
"p" 'mml-secure-encrypt-pgpmime
)
874 (define-key encryptpart
"o" 'mml-secure-encrypt-pgp
)
875 (define-key encryptpart
"s" 'mml-secure-encrypt-smime
)
876 (define-key map
"\C-n" 'mml-unsecure-message
)
877 (define-key map
"f" 'mml-attach-file
)
878 (define-key map
"b" 'mml-attach-buffer
)
879 (define-key map
"e" 'mml-attach-external
)
880 (define-key map
"q" 'mml-quote-region
)
881 (define-key map
"m" 'mml-insert-multipart
)
882 (define-key map
"p" 'mml-insert-part
)
883 (define-key map
"v" 'mml-validate
)
884 (define-key map
"P" 'mml-preview
)
885 (define-key map
"s" sign
)
886 (define-key map
"S" signpart
)
887 (define-key map
"c" encrypt
)
888 (define-key map
"C" encryptpart
)
889 ;;(define-key map "n" 'mml-narrow-to-part)
890 ;; `M-m' conflicts with `back-to-indentation'.
891 ;; (define-key main "\M-m" map)
892 (define-key main
"\C-c\C-m" map
)
896 mml-menu mml-mode-map
""
898 ["Attach File..." mml-attach-file
899 ,@(if (featurep 'xemacs
) '(t)
900 '(:help
"Attach a file at point"))]
901 ["Attach Buffer..." mml-attach-buffer t
]
902 ["Attach External..." mml-attach-external t
]
903 ["Insert Part..." mml-insert-part t
]
904 ["Insert Multipart..." mml-insert-multipart t
]
905 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t
]
906 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t
]
907 ["PGP Sign" mml-secure-message-sign-pgp t
]
908 ["PGP Encrypt" mml-secure-message-encrypt-pgp t
]
909 ["S/MIME Sign" mml-secure-message-sign-smime t
]
910 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t
]
912 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t
]
913 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t
]
914 ["PGP Sign Part" mml-secure-sign-pgp t
]
915 ["PGP Encrypt Part" mml-secure-encrypt-pgp t
]
916 ["S/MIME Sign Part" mml-secure-sign-smime t
]
917 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t
])
918 ["Encrypt/Sign off" mml-unsecure-message t
]
919 ;;["Narrow" mml-narrow-to-part t]
920 ["Quote MML" mml-quote-region t
]
921 ["Validate MML" mml-validate t
]
922 ["Preview" mml-preview t
]
924 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
925 ,@(if (featurep 'xemacs
) '(t)
926 '(:help
"Display the Emacs MIME manual"))]
927 ["PGG manual" (lambda () (interactive) (message-info 16))
928 ,@(if (featurep 'xemacs
) '(t)
929 '(:help
"Display the PGG manual"))]))
932 "Minor mode for editing MML.")
934 (defun mml-mode (&optional arg
)
935 "Minor mode for editing MML.
936 MML is the MIME Meta Language, a minor mode for composing MIME articles.
937 See Info node `(emacs-mime)Composing'.
941 (when (set (make-local-variable 'mml-mode
)
942 (if (null arg
) (not mml-mode
)
943 (> (prefix-numeric-value arg
) 0)))
944 (gnus-add-minor-mode 'mml-mode
" MML" mml-mode-map
)
945 (easy-menu-add mml-menu mml-mode-map
)
946 (run-hooks 'mml-mode-hook
)))
949 ;;; Helper functions for reading MIME stuff from the minibuffer and
950 ;;; inserting stuff to the buffer.
953 (defun mml-minibuffer-read-file (prompt)
954 (let* ((completion-ignored-extensions nil
)
955 (file (read-file-name prompt nil nil t
)))
956 ;; Prevent some common errors. This is inspired by similar code in
958 (when (file-directory-p file
)
959 (error "%s is a directory, cannot attach" file
))
960 (unless (file-exists-p file
)
961 (error "No such file: %s" file
))
962 (unless (file-readable-p file
)
963 (error "Permission denied: %s" file
))
966 (defun mml-minibuffer-read-type (name &optional default
)
967 (mailcap-parse-mimetypes)
968 (let* ((default (or default
969 (mm-default-file-encoding name
)
970 ;; Perhaps here we should check what the file
971 ;; looks like, and offer text/plain if it looks
973 "application/octet-stream"))
974 (string (completing-read
975 (format "Content type (default %s): " default
)
976 (mapcar 'list
(mailcap-mime-types)))))
977 (if (not (equal string
""))
981 (defun mml-minibuffer-read-description ()
982 (let ((description (read-string "One line description: ")))
983 (when (string-match "\\`[ \t]*\\'" description
)
984 (setq description nil
))
987 (defun mml-minibuffer-read-disposition (type &optional default
)
988 (unless default
(setq default
989 (if (and (string-match "\\`text/" type
)
990 (not (string-match "\\`text/rtf\\'" type
)))
993 (let ((disposition (completing-read
994 (format "Disposition (default %s): " default
)
995 '(("attachment") ("inline") (""))
996 nil t nil nil default
)))
997 (if (not (equal disposition
""))
1001 (defun mml-quote-region (beg end
)
1002 "Quote the MML tags in the region."
1006 ;; Temporarily narrow the region to defend from changes
1007 ;; invalidating END.
1008 (narrow-to-region beg end
)
1009 (goto-char (point-min))
1011 (while (re-search-forward
1012 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t
)
1013 ;; Insert ! after the #.
1014 (goto-char (+ (match-beginning 0) 2))
1017 (defun mml-insert-tag (name &rest plist
)
1018 "Insert an MML tag described by NAME and PLIST."
1019 (when (symbolp name
)
1020 (setq name
(symbol-name name
)))
1023 (let ((key (pop plist
))
1024 (value (pop plist
)))
1026 ;; Quote VALUE if it contains suspicious characters.
1027 (when (string-match "[\"'\\~/*;() \t\n]" value
)
1028 (setq value
(with-output-to-string
1029 (let (print-escape-nonascii)
1031 (insert (format " %s=%s" key value
)))))
1034 (defun mml-insert-empty-tag (name &rest plist
)
1035 "Insert an empty MML tag described by NAME and PLIST."
1036 (when (symbolp name
)
1037 (setq name
(symbol-name name
)))
1038 (apply #'mml-insert-tag name plist
)
1039 (insert "<#/" name
">\n"))
1041 ;;; Attachment functions.
1043 (defun mml-attach-file (file &optional type description disposition
)
1044 "Attach a file to the outgoing MIME message.
1045 The file is not inserted or encoded until you send the message with
1046 `\\[message-send-and-exit]' or `\\[message-send]'.
1048 FILE is the name of the file to attach. TYPE is its
1049 content-type, a string of the form \"type/subtype\". DESCRIPTION
1050 is a one-line description of the attachment. The DISPOSITION
1051 specifies how the attachment is intended to be displayed. It can
1052 be either \"inline\" (displayed automatically within the message
1053 body) or \"attachment\" (separate from the body)."
1055 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1056 (type (mml-minibuffer-read-type file
))
1057 (description (mml-minibuffer-read-description))
1058 (disposition (mml-minibuffer-read-disposition type
)))
1059 (list file type description disposition
)))
1060 (mml-insert-empty-tag 'part
1063 'disposition
(or disposition
"attachment")
1064 'description description
))
1066 (defun mml-attach-buffer (buffer &optional type description
)
1067 "Attach a buffer to the outgoing MIME message.
1068 See `mml-attach-file' for details of operation."
1070 (let* ((buffer (read-buffer "Attach buffer: "))
1071 (type (mml-minibuffer-read-type buffer
"text/plain"))
1072 (description (mml-minibuffer-read-description)))
1073 (list buffer type description
)))
1074 (mml-insert-empty-tag 'part
'type type
'buffer buffer
1075 'disposition
"attachment" 'description description
))
1077 (defun mml-attach-external (file &optional type description
)
1078 "Attach an external file into the buffer.
1079 FILE is an ange-ftp/efs specification of the part location.
1080 TYPE is the MIME type to use."
1082 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1083 (type (mml-minibuffer-read-type file
))
1084 (description (mml-minibuffer-read-description)))
1085 (list file type description
)))
1086 (mml-insert-empty-tag 'external
'type type
'name file
1087 'disposition
"attachment" 'description description
))
1089 (defun mml-insert-multipart (&optional type
)
1090 (interactive (list (completing-read "Multipart type (default mixed): "
1091 '(("mixed") ("alternative") ("digest") ("parallel")
1092 ("signed") ("encrypted"))
1095 (setq type
"mixed"))
1096 (mml-insert-empty-tag "multipart" 'type type
)
1099 (defun mml-insert-part (&optional type
)
1101 (list (mml-minibuffer-read-type "")))
1102 (mml-insert-tag 'part
'type type
'disposition
"inline")
1105 (defun mml-preview-insert-mail-followup-to ()
1106 "Insert a Mail-Followup-To header before previewing an article.
1107 Should be adopted if code in `message-send-mail' is changed."
1108 (when (and (message-mail-p)
1109 (message-subscribed-p)
1110 (not (mail-fetch-field "mail-followup-to"))
1111 (message-make-mail-followup-to))
1112 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1113 (insert (message-make-mail-followup-to))))
1115 (defun mml-preview (&optional raw
)
1116 "Display current buffer with Gnus, in a new buffer.
1117 If RAW, display a raw encoded MIME message."
1120 (let* ((buf (current-buffer))
1121 (message-options message-options
)
1122 (message-this-is-mail (message-mail-p))
1123 (message-this-is-news (message-news-p))
1124 (message-posting-charset (or (gnus-setup-posting-charset
1126 (message-narrow-to-headers-or-head)
1127 (message-fetch-field "Newsgroups")))
1128 message-posting-charset
)))
1129 (message-options-set-recipient)
1130 (pop-to-buffer (generate-new-buffer
1131 (concat (if raw
"*Raw MIME preview of "
1132 "*MIME preview of ") (buffer-name))))
1133 (when (boundp 'gnus-buffers
)
1134 (push (current-buffer) gnus-buffers
))
1136 (insert-buffer-substring buf
)
1137 (mml-preview-insert-mail-followup-to)
1138 (let ((message-deletable-headers (if (message-news-p)
1140 message-deletable-headers
)))
1141 (message-generate-headers
1142 (copy-sequence (if (message-news-p)
1143 message-required-news-headers
1144 message-required-mail-headers
))))
1145 (if (re-search-forward
1146 (concat "^" (regexp-quote mail-header-separator
) "\n") nil t
)
1147 (replace-match "\n"))
1148 (let ((mail-header-separator ""));; mail-header-separator is removed.
1151 (when (fboundp 'set-buffer-multibyte
)
1152 (let ((s (buffer-string)))
1153 ;; Insert the content into unibyte buffer.
1155 (mm-disable-multibyte)
1157 (let ((gnus-newsgroup-charset (car message-posting-charset
))
1158 gnus-article-prepare-hook gnus-original-article-buffer
)
1159 (run-hooks 'gnus-article-decode-hook
)
1160 (let ((gnus-newsgroup-name "dummy")
1161 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1162 (gnus-make-hashtable 5))))
1163 (gnus-article-prepare-display))))
1164 ;; Disable article-mode-map.
1166 (gnus-make-local-hook 'kill-buffer-hook
)
1167 (add-hook 'kill-buffer-hook
1169 (mm-destroy-parts gnus-article-mime-handles
)) nil t
)
1170 (setq buffer-read-only t
)
1171 (local-set-key "q" (lambda () (interactive) (kill-buffer nil
)))
1172 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1176 (widget-button-press (point))))
1177 (local-set-key gnus-mouse-2
1180 (widget-button-press (widget-event-point event
) event
)))
1181 (goto-char (point-min)))))
1183 (defun mml-validate ()
1184 "Validate the current MML document."
1188 (defun mml-tweak-part (cont)
1190 (let ((tweak (cdr (assq 'tweak cont
)))
1195 (or (cdr (assoc tweak mml-tweak-function-alist
))
1197 (mml-tweak-type-alist
1198 (let ((alist mml-tweak-type-alist
)
1199 (type (or (cdr (assq 'type cont
)) "text/plain")))
1201 (if (string-match (caar alist
) type
)
1202 (setq func
(cdar alist
)
1204 (setq alist
(cdr alist
)))))))
1208 (let ((alist mml-tweak-sexp-alist
))
1210 (if (eval (caar alist
))
1211 (funcall (cdar alist
) cont
))
1212 (setq alist
(cdr alist
)))))
1215 (defun mml-tweak-externalize-attachments (cont)
1216 "Tweak attached files as external parts."
1217 (let (filename-cons)
1218 (when (and (eq (car cont
) 'part
)
1219 (not (cdr (assq 'buffer cont
)))
1220 (and (setq filename-cons
(assq 'filename cont
))
1221 (not (equal (cdr (assq 'nofile cont
)) "yes"))))
1222 (setcar cont
'external
)
1223 (setcar filename-cons
'name
))))
1227 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1228 ;;; mml.el ends here