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-make-local-hook "gnus-util")
39 (autoload 'message-fetch-field "message")
40 (autoload 'message-mark-active-p "message")
41 (autoload 'message-info "message")
42 (autoload 'fill-flowed-encode "flow-fill")
43 (autoload 'message-posting-charset "message")
44 (autoload 'dnd-get-local-file-name "dnd"))
46 (defvar gnus-article-mime-handles)
48 (defvar gnus-newsrc-hashtb)
49 (defvar message-default-charset)
50 (defvar message-deletable-headers)
51 (defvar message-options)
52 (defvar message-posting-charset)
53 (defvar message-required-mail-headers)
54 (defvar message-required-news-headers)
55 (defvar dnd-protocol-alist)
57 (defcustom mml-content-type-parameters
58 '(name access-type expiration size permission format)
59 "*A list of acceptable parameters in MML tag.
60 These parameters are generated in Content-Type header if exists."
62 :type '(repeat (symbol :tag "Parameter"))
65 (defcustom mml-content-disposition-parameters
66 '(filename creation-date modification-date read-date)
67 "*A list of acceptable parameters in MML tag.
68 These parameters are generated in Content-Disposition header if exists."
70 :type '(repeat (symbol :tag "Parameter"))
73 (defcustom mml-insert-mime-headers-always nil
74 "If non-nil, always put Content-Type: text/plain at top of empty parts.
75 It is necessary to work against a bug in certain clients."
80 (defvar mml-tweak-type-alist nil
81 "A list of (TYPE . FUNCTION) for tweaking MML parts.
82 TYPE is a string containing a regexp to match the MIME type. FUNCTION
83 is a Lisp function which is called with the MML handle to tweak the
84 part. This variable is used only when no TWEAK parameter exists in
87 (defvar mml-tweak-function-alist nil
88 "A list of (NAME . FUNCTION) for tweaking MML parts.
89 NAME is a string containing the name of the TWEAK parameter in the MML
90 handle. FUNCTION is a Lisp function which is called with the MML
91 handle to tweak the part.")
93 (defvar mml-tweak-sexp-alist
94 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
95 "A list of (SEXP . FUNCTION) for tweaking MML parts.
96 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
97 is called. FUNCTION is a Lisp function which is called with the MML
98 handle to tweak the part.")
100 (defvar mml-externalize-attachments nil
101 "*If non-nil, local-file attachments are generated as external parts.")
103 (defvar mml-generate-multipart-alist nil
104 "*Alist of multipart generation functions.
105 Each entry has the form (NAME . FUNCTION), where
106 NAME is a string containing the name of the part (without the
107 leading \"/multipart/\"),
108 FUNCTION is a Lisp function which is called to generate the part.
110 The Lisp function has to supply the appropriate MIME headers and the
111 contents of this part.")
113 (defvar mml-syntax-table
114 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
115 (modify-syntax-entry ?\\ "/" table)
116 (modify-syntax-entry ?< "(" table)
117 (modify-syntax-entry ?> ")" table)
118 (modify-syntax-entry ?@ "w" table)
119 (modify-syntax-entry ?/ "w" table)
120 (modify-syntax-entry ?= " " table)
121 (modify-syntax-entry ?* " " table)
122 (modify-syntax-entry ?\; " " table)
123 (modify-syntax-entry ?\' " " table)
126 (defvar mml-boundary-function 'mml-make-boundary
127 "A function called to suggest a boundary.
128 The function may be called several times, and should try to make a new
129 suggestion each time. The function is called with one parameter,
130 which is a number that says how many times the function has been
131 called for this message.")
133 (defvar mml-confirmation-set nil
134 "A list of symbols, each of which disables some warning.
135 `unknown-encoding': always send messages contain characters with
136 unknown encoding; `use-ascii': always use ASCII for those characters
137 with unknown encoding; `multipart': always send messages with more than
140 (defvar mml-generate-default-type "text/plain"
141 "Content type by which the Content-Type header can be omitted.
142 The Content-Type header will not be put in the MIME part if the type
143 equals the value and there's no parameter (e.g. charset, format, etc.)
144 and `mml-insert-mime-headers-always' is nil. The value will be bound
145 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
146 part. This is for the internal use, you should never modify the value.")
148 (defvar mml-buffer-list nil)
150 (defun mml-generate-new-buffer (name)
151 (let ((buf (generate-new-buffer name)))
152 (push buf mml-buffer-list)
155 (defun mml-destroy-buffers ()
156 (let (kill-buffer-hook)
157 (mapc 'kill-buffer mml-buffer-list)
158 (setq mml-buffer-list nil)))
161 "Parse the current buffer as an MML document."
163 (goto-char (point-min))
164 (with-syntax-table mml-syntax-table
167 (defun mml-parse-1 ()
168 "Parse the current buffer as an MML document."
169 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
170 (while (and (not (eobp))
171 (not (looking-at "<#/multipart")))
173 ((looking-at "<#secure")
174 ;; The secure part is essentially a meta-meta tag, which
175 ;; expands to either a part tag if there are no other parts in
176 ;; the document or a multipart tag if there are other parts
177 ;; included in the message
179 (taginfo (mml-read-tag))
180 (keyfile (cdr (assq 'keyfile taginfo)))
181 (certfile (cdr (assq 'certfile taginfo)))
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)))
189 (if (re-search-forward
190 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
191 (setq secure-mode "multipart")
192 (setq secure-mode "part")))
195 (re-search-forward "<#secure[^\n]*>\n"))
196 (delete-region (match-beginning 0) (match-end 0))
197 (cond ((string= mode "sign")
198 (setq tags (list "sign" method)))
199 ((string= mode "encrypt")
200 (setq tags (list "encrypt" method)))
201 ((string= mode "signencrypt")
202 (setq tags (list "sign" method "encrypt" method))))
203 (eval `(mml-insert-tag ,secure-mode
205 ,(if keyfile "keyfile")
207 ,(if certfile "certfile")
209 ,(if recipients "recipients")
211 ,(if sender "sender")
214 (goto-char location)))
215 ((looking-at "<#multipart")
216 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
217 ((looking-at "<#external")
218 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
221 (if (or (looking-at "<#part") (looking-at "<#mml"))
222 (setq tag (mml-read-tag)
225 (setq tag (list 'part '(type . "text/plain"))
228 (setq raw (cdr (assq 'raw tag))
230 contents (mml-read-part (eq 'mml (car tag)))
235 (intern (downcase (cdr (assq 'charset tag))))))
237 (mm-find-mime-charset-region point (point)
239 (when (and (not raw) (memq nil charsets))
240 (if (or (memq 'unknown-encoding mml-confirmation-set)
241 (message-options-get 'unknown-encoding)
243 Message contains characters with unknown encoding. Really send? ")
244 (message-options-set 'unknown-encoding t)))
246 (or (memq 'use-ascii mml-confirmation-set)
247 (message-options-get 'use-ascii)
248 (and (y-or-n-p "Use ASCII as charset? ")
249 (message-options-set 'use-ascii t))))
250 (setq charsets (delq nil charsets))
252 (error "Edit your message to remove those characters")))
255 (< (length charsets) 2))
256 (if (or (not no-markup-p)
257 (string-match "[^ \t\r\n]" contents))
258 ;; Don't create blank parts.
259 (push (nconc tag (list (cons 'contents contents)))
261 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
262 tag point (point) use-ascii)))
264 (not (memq 'multipart mml-confirmation-set))
265 (not (message-options-get 'multipart))
266 (not (and (y-or-n-p (format "\
267 A message part needs to be split into %d charset parts. Really send? "
269 (message-options-set 'multipart t))))
270 (error "Edit your message to use only one charset"))
271 (setq struct (nconc nstruct struct)))))))
276 (defun mml-parse-singlepart-with-multiple-charsets
277 (orig-tag beg end &optional use-ascii)
280 (narrow-to-region beg end)
281 (goto-char (point-min))
282 (let ((current (or (mm-mime-charset (mm-charset-after))
283 (and use-ascii 'us-ascii)))
284 charset struct space newline paragraph)
286 (setq charset (mm-mime-charset (mm-charset-after)))
288 ;; The charset remains the same.
289 ((eq charset 'us-ascii))
290 ((or (and use-ascii (not charset))
291 (eq charset current))
295 ;; The initial charset was ascii.
296 ((eq current 'us-ascii)
297 (setq current charset
301 ;; We have a change in charsets.
305 (list (cons 'contents
306 (buffer-substring-no-properties
307 beg (or paragraph newline space (point))))))
309 (setq beg (or paragraph newline space (point))
314 ;; Compute places where it might be nice to break the part.
316 ((memq (following-char) '(? ?\t))
317 (setq space (1+ (point))))
318 ((and (eq (following-char) ?\n)
320 (eq (char-after (1- (point))) ?\n))
321 (setq paragraph (point)))
322 ((eq (following-char) ?\n)
323 (setq newline (1+ (point)))))
325 ;; Do the final part.
326 (unless (= beg (point))
327 (push (append orig-tag
328 (list (cons 'contents
329 (buffer-substring-no-properties
334 (defun mml-read-tag ()
335 "Read a tag and return the contents."
336 (let ((orig-point (point))
337 contents name elem val)
339 (setq name (buffer-substring-no-properties
340 (point) (progn (forward-sexp 1) (point))))
341 (skip-chars-forward " \t\n")
342 (while (not (looking-at ">[ \t]*\n?"))
343 (setq elem (buffer-substring-no-properties
344 (point) (progn (forward-sexp 1) (point))))
345 (skip-chars-forward "= \t\n")
346 (setq val (buffer-substring-no-properties
347 (point) (progn (forward-sexp 1) (point))))
348 (when (string-match "^\"\\(.*\\)\"$" val)
349 (setq val (match-string 1 val)))
350 (push (cons (intern elem) val) contents)
351 (skip-chars-forward " \t\n"))
352 (goto-char (match-end 0))
353 ;; Don't skip the leading space.
354 ;;(skip-chars-forward " \t\n")
355 ;; Put the tag location into the returned contents
356 (setq contents (append (list (cons 'tag-location orig-point)) contents))
357 (cons (intern name) (nreverse contents))))
359 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
360 (let ((str (buffer-substring-no-properties start end))
361 (bufstart start) tmp)
362 (while (setq tmp (text-property-any start end 'hard 't))
363 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
365 (setq start (1+ tmp)))
368 (defun mml-read-part (&optional mml)
369 "Return the buffer up till the next part, multipart or closing part or multipart.
370 If MML is non-nil, return the buffer up till the correspondent mml tag."
371 (let ((beg (point)) (count 1))
372 ;; If the tag ended at the end of the line, we go to the next line.
373 (when (looking-at "[ \t]*\n")
377 (while (and (> count 0) (not (eobp)))
378 (if (re-search-forward "<#\\(/\\)?mml." nil t)
379 (setq count (+ count (if (match-beginning 1) -1 1)))
380 (goto-char (point-max))))
381 (mml-buffer-substring-no-properties-except-hard-newlines
384 (match-beginning 0))))
385 (if (re-search-forward
386 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
388 (mml-buffer-substring-no-properties-except-hard-newlines
389 beg (match-beginning 0))
390 (if (or (not (match-beginning 1))
391 (equal (match-string 2) "multipart"))
392 (goto-char (match-beginning 0))
393 (when (looking-at "[ \t]*\n")
395 (mml-buffer-substring-no-properties-except-hard-newlines
396 beg (goto-char (point-max)))))))
398 (defvar mml-boundary nil)
399 (defvar mml-base-boundary "-=-=")
400 (defvar mml-multipart-number 0)
402 (defun mml-generate-mime ()
403 "Generate a MIME message based on the current MML document."
404 (let ((cont (mml-parse))
405 (mml-multipart-number mml-multipart-number))
409 (if (and (consp (car cont))
411 (mml-generate-mime-1 (car cont))
412 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
416 (defun mml-generate-mime-1 (cont)
417 (let ((mm-use-ultra-safe-encoding
418 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
420 (narrow-to-region (point) (point))
421 (mml-tweak-part cont)
423 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
424 (let* ((raw (cdr (assq 'raw cont)))
425 (filename (cdr (assq 'filename cont)))
426 (type (or (cdr (assq 'type cont))
428 (or (mm-default-file-encoding filename)
429 "application/octet-stream")
431 (charset (cdr (assq 'charset cont)))
432 (coding (mm-charset-to-coding-system charset))
433 encoding flowed coded)
434 (cond ((eq coding 'ascii)
438 (setq charset (intern (downcase charset)))))
440 (member (car (split-string type "/")) '("text" "message")))
444 ((cdr (assq 'buffer cont))
445 (insert-buffer-substring (cdr (assq 'buffer cont))))
447 (not (equal (cdr (assq 'nofile cont)) "yes")))
448 (let ((coding-system-for-read coding))
449 (mm-insert-file-contents filename)))
450 ((eq 'mml (car cont))
451 (insert (cdr (assq 'contents cont))))
454 (narrow-to-region (point) (point))
455 (insert (cdr (assq 'contents cont)))
456 ;; Remove quotes from quoted tags.
457 (goto-char (point-min))
458 (while (re-search-forward
459 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
461 (delete-region (+ (match-beginning 0) 2)
462 (+ (match-beginning 0) 3))))))
464 ((eq (car cont) 'mml)
465 (let ((mml-boundary (mml-compute-boundary cont))
466 ;; It is necessary for the case where this
467 ;; function is called recursively since
468 ;; `m-g-d-t' will be bound to "message/rfc822"
469 ;; when encoding an article to be forwarded.
470 (mml-generate-default-type "text/plain"))
472 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
473 ;; ignore 0x1b, it is part of iso-2022-jp
474 (setq encoding (mm-body-7-or-8))))
475 ((string= (car (split-string type "/")) "message")
476 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
477 ;; ignore 0x1b, it is part of iso-2022-jp
478 (setq encoding (mm-body-7-or-8))))
480 ;; Only perform format=flowed filling on text/plain
481 ;; parts where there either isn't a format parameter
482 ;; in the mml tag or it says "flowed" and there
483 ;; actually are hard newlines in the text.
484 (let (use-hard-newlines)
485 (when (and (string= type "text/plain")
486 (not (string= (cdr (assq 'sign cont)) "pgp"))
487 (or (null (assq 'format cont))
488 (string= (cdr (assq 'format cont))
490 (setq use-hard-newlines
492 (point-min) (point-max) 'hard 't)))
494 ;; Indicate that `mml-insert-mime-headers' should
495 ;; insert a "; format=flowed" string unless the
496 ;; user has already specified it.
497 (setq flowed (null (assq 'format cont)))))
498 (setq charset (mm-encode-body charset))
499 (setq encoding (mm-body-encoding
500 charset (cdr (assq 'encoding cont))))))
501 (setq coded (buffer-string)))
502 (mml-insert-mime-headers cont type charset encoding flowed)
505 (mm-with-unibyte-buffer
507 ((cdr (assq 'buffer cont))
508 (insert (with-current-buffer (cdr (assq 'buffer cont))
509 (mm-with-unibyte-current-buffer
512 (not (equal (cdr (assq 'nofile cont)) "yes")))
513 (let ((coding-system-for-read mm-binary-coding-system))
514 (mm-insert-file-contents filename nil nil nil nil t))
516 (setq charset (mm-coding-system-to-mime-charset
517 (mm-find-buffer-file-coding-system
520 (let ((contents (cdr (assq 'contents cont))))
521 (if (if (featurep 'xemacs)
522 (string-match "[^\000-\377]" contents)
523 (mm-multibyte-string-p contents))
525 (mm-enable-multibyte)
527 (setq charset (mm-encode-body charset)))
528 (insert contents)))))
529 (setq encoding (mm-encode-buffer type)
530 coded (mm-string-as-multibyte (buffer-string))))
531 (mml-insert-mime-headers cont type charset encoding nil)
533 (mm-with-unibyte-current-buffer
535 ((eq (car cont) 'external)
536 (insert "Content-Type: message/external-body")
537 (let ((parameters (mml-parameter-string
538 cont '(expiration size permission)))
539 (name (cdr (assq 'name cont)))
540 (url (cdr (assq 'url cont))))
542 (setq name (mml-parse-file-name name))
544 (mml-insert-parameter
545 (mail-header-encode-parameter "name" name)
546 "access-type=local-file")
547 (mml-insert-parameter
548 (mail-header-encode-parameter
549 "name" (file-name-nondirectory (nth 2 name)))
550 (mail-header-encode-parameter "site" (nth 1 name))
551 (mail-header-encode-parameter
552 "directory" (file-name-directory (nth 2 name))))
553 (mml-insert-parameter
554 (concat "access-type="
555 (if (member (nth 0 name) '("ftp@" "anonymous@"))
559 (mml-insert-parameter
560 (mail-header-encode-parameter "url" url)
563 (mml-insert-parameter-string
564 cont '(expiration size permission)))
566 (insert "Content-Type: "
567 (or (cdr (assq 'type cont))
569 (or (mm-default-file-encoding name)
570 "application/octet-stream")
573 (insert "Content-ID: " (message-make-message-id) "\n")
574 (insert "Content-Transfer-Encoding: "
575 (or (cdr (assq 'encoding cont)) "binary"))
577 (insert (or (cdr (assq 'contents cont))))
579 ((eq (car cont) 'multipart)
580 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
581 (mml-generate-default-type (if (equal type "digest")
584 (handler (assoc type mml-generate-multipart-alist)))
586 (funcall (cdr handler) cont)
587 ;; No specific handler. Use default one.
588 (let ((mml-boundary (mml-compute-boundary cont)))
589 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
591 (if (cdr (assq 'start cont))
592 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
594 (let ((cont cont) part)
595 (while (setq part (pop cont))
596 ;; Skip `multipart' and attributes.
597 (when (and (consp part) (consp (cdr part)))
598 (insert "\n--" mml-boundary "\n")
599 (mml-generate-mime-1 part)
600 (goto-char (point-max)))))
601 (insert "\n--" mml-boundary "--\n")))))
603 (error "Invalid element: %S" cont)))
604 ;; handle sign & encrypt tags in a semi-smart way.
605 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
606 (encrypt-item (assoc (cdr (assq 'encrypt cont))
609 (when (or sign-item encrypt-item)
610 (when (setq sender (cdr (assq 'sender cont)))