]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml.el
(mm-uu-copy-to-buffer): Use with-current-buffer.
[gnu-emacs] / lisp / gnus / mml.el
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.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
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)
11 ;; any later version.
12
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.
17
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'mm-util)
28 (require 'mm-bodies)
29 (require 'mm-encode)
30 (require 'mm-decode)
31 (require 'mml-sec)
32 (eval-when-compile (require 'cl))
33
34 (eval-and-compile
35 (autoload 'message-make-message-id "message")
36 (autoload 'gnus-setup-posting-charset "gnus-msg")
37 (autoload 'gnus-add-minor-mode "gnus-ems")
38 (autoload 'gnus-make-local-hook "gnus-util")
39 (autoload 'message-fetch-field "message")
40 (autoload 'fill-flowed-encode "flow-fill")
41 (autoload 'message-posting-charset "message"))
42
43 (defcustom mml-content-type-parameters
44 '(name access-type expiration size permission format)
45 "*A list of acceptable parameters in MML tag.
46 These parameters are generated in Content-Type header if exists."
47 :version "22.1"
48 :type '(repeat (symbol :tag "Parameter"))
49 :group 'message)
50
51 (defcustom mml-content-disposition-parameters
52 '(filename creation-date modification-date read-date)
53 "*A list of acceptable parameters in MML tag.
54 These parameters are generated in Content-Disposition header if exists."
55 :version "22.1"
56 :type '(repeat (symbol :tag "Parameter"))
57 :group 'message)
58
59 (defcustom mml-insert-mime-headers-always nil
60 "If non-nil, always put Content-Type: text/plain at top of empty parts.
61 It is necessary to work against a bug in certain clients."
62 :version "22.1"
63 :type 'boolean
64 :group 'message)
65
66 (defvar mml-tweak-type-alist nil
67 "A list of (TYPE . FUNCTION) for tweaking MML parts.
68 TYPE is a string containing a regexp to match the MIME type. FUNCTION
69 is a Lisp function which is called with the MML handle to tweak the
70 part. This variable is used only when no TWEAK parameter exists in
71 the MML handle.")
72
73 (defvar mml-tweak-function-alist nil
74 "A list of (NAME . FUNCTION) for tweaking MML parts.
75 NAME is a string containing the name of the TWEAK parameter in the MML
76 handle. FUNCTION is a Lisp function which is called with the MML
77 handle to tweak the part.")
78
79 (defvar mml-tweak-sexp-alist
80 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
81 "A list of (SEXP . FUNCTION) for tweaking MML parts.
82 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
83 is called. FUNCTION is a Lisp function which is called with the MML
84 handle to tweak the part.")
85
86 (defvar mml-externalize-attachments nil
87 "*If non-nil, local-file attachments are generated as external parts.")
88
89 (defvar mml-generate-multipart-alist nil
90 "*Alist of multipart generation functions.
91 Each entry has the form (NAME . FUNCTION), where
92 NAME is a string containing the name of the part (without the
93 leading \"/multipart/\"),
94 FUNCTION is a Lisp function which is called to generate the part.
95
96 The Lisp function has to supply the appropriate MIME headers and the
97 contents of this part.")
98
99 (defvar mml-syntax-table
100 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
101 (modify-syntax-entry ?\\ "/" table)
102 (modify-syntax-entry ?< "(" table)
103 (modify-syntax-entry ?> ")" table)
104 (modify-syntax-entry ?@ "w" table)
105 (modify-syntax-entry ?/ "w" table)
106 (modify-syntax-entry ?= " " table)
107 (modify-syntax-entry ?* " " table)
108 (modify-syntax-entry ?\; " " table)
109 (modify-syntax-entry ?\' " " table)
110 table))
111
112 (defvar mml-boundary-function 'mml-make-boundary
113 "A function called to suggest a boundary.
114 The function may be called several times, and should try to make a new
115 suggestion each time. The function is called with one parameter,
116 which is a number that says how many times the function has been
117 called for this message.")
118
119 (defvar mml-confirmation-set nil
120 "A list of symbols, each of which disables some warning.
121 `unknown-encoding': always send messages contain characters with
122 unknown encoding; `use-ascii': always use ASCII for those characters
123 with unknown encoding; `multipart': always send messages with more than
124 one charsets.")
125
126 (defvar mml-generate-default-type "text/plain")
127
128 (defvar mml-buffer-list nil)
129
130 (defun mml-generate-new-buffer (name)
131 (let ((buf (generate-new-buffer name)))
132 (push buf mml-buffer-list)
133 buf))
134
135 (defun mml-destroy-buffers ()
136 (let (kill-buffer-hook)
137 (mapcar 'kill-buffer mml-buffer-list)
138 (setq mml-buffer-list nil)))
139
140 (defun mml-parse ()
141 "Parse the current buffer as an MML document."
142 (save-excursion
143 (goto-char (point-min))
144 (let ((table (syntax-table)))
145 (unwind-protect
146 (progn
147 (set-syntax-table mml-syntax-table)
148 (mml-parse-1))
149 (set-syntax-table table)))))
150
151 (defun mml-parse-1 ()
152 "Parse the current buffer as an MML document."
153 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
154 (while (and (not (eobp))
155 (not (looking-at "<#/multipart")))
156 (cond
157 ((looking-at "<#secure")
158 ;; The secure part is essentially a meta-meta tag, which
159 ;; expands to either a part tag if there are no other parts in
160 ;; the document or a multipart tag if there are other parts
161 ;; included in the message
162 (let* (secure-mode
163 (taginfo (mml-read-tag))
164 (recipients (cdr (assq 'recipients taginfo)))
165 (sender (cdr (assq 'sender taginfo)))
166 (location (cdr (assq 'tag-location taginfo)))
167 (mode (cdr (assq 'mode taginfo)))
168 (method (cdr (assq 'method taginfo)))
169 tags)
170 (save-excursion
171 (if
172 (re-search-forward
173 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
174 (setq secure-mode "multipart")
175 (setq secure-mode "part")))
176 (save-excursion
177 (goto-char location)
178 (re-search-forward "<#secure[^\n]*>\n"))
179 (delete-region (match-beginning 0) (match-end 0))
180 (cond ((string= mode "sign")
181 (setq tags (list "sign" method)))
182 ((string= mode "encrypt")
183 (setq tags (list "encrypt" method)))
184 ((string= mode "signencrypt")
185 (setq tags (list "sign" method "encrypt" method))))
186 (eval `(mml-insert-tag ,secure-mode
187 ,@tags
188 ,(if recipients "recipients")
189 ,recipients
190 ,(if sender "sender")
191 ,sender))
192 ;; restart the parse
193 (goto-char location)))
194 ((looking-at "<#multipart")
195 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
196 ((looking-at "<#external")
197 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
198 struct))
199 (t
200 (if (or (looking-at "<#part") (looking-at "<#mml"))
201 (setq tag (mml-read-tag)
202 no-markup-p nil
203 warn nil)
204 (setq tag (list 'part '(type . "text/plain"))
205 no-markup-p t
206 warn t))
207 (setq raw (cdr (assq 'raw tag))
208 point (point)
209 contents (mml-read-part (eq 'mml (car tag)))
210 charsets (cond
211 (raw nil)
212 ((assq 'charset tag)
213 (list
214 (intern (downcase (cdr (assq 'charset tag))))))
215 (t
216 (mm-find-mime-charset-region point (point)
217 mm-hack-charsets))))
218 (when (and (not raw) (memq nil charsets))
219 (if (or (memq 'unknown-encoding mml-confirmation-set)
220 (message-options-get 'unknown-encoding)
221 (and (y-or-n-p "\
222 Message contains characters with unknown encoding. Really send? ")
223 (message-options-set 'unknown-encoding t)))
224 (if (setq use-ascii
225 (or (memq 'use-ascii mml-confirmation-set)
226 (message-options-get 'use-ascii)
227 (and (y-or-n-p "Use ASCII as charset? ")
228 (message-options-set 'use-ascii t))))
229 (setq charsets (delq nil charsets))
230 (setq warn nil))
231 (error "Edit your message to remove those characters")))
232 (if (or raw
233 (eq 'mml (car tag))
234 (< (length charsets) 2))
235 (if (or (not no-markup-p)
236 (string-match "[^ \t\r\n]" contents))
237 ;; Don't create blank parts.
238 (push (nconc tag (list (cons 'contents contents)))
239 struct))
240 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
241 tag point (point) use-ascii)))
242 (when (and warn
243 (not (memq 'multipart mml-confirmation-set))
244 (not (message-options-get 'multipart))
245 (not (and (y-or-n-p (format "\
246 A message part needs to be split into %d charset parts. Really send? "
247 (length nstruct)))
248 (message-options-set 'multipart t))))
249 (error "Edit your message to use only one charset"))
250 (setq struct (nconc nstruct struct)))))))
251 (unless (eobp)
252 (forward-line 1))
253 (nreverse struct)))
254
255 (defun mml-parse-singlepart-with-multiple-charsets
256 (orig-tag beg end &optional use-ascii)
257 (save-excursion
258 (save-restriction
259 (narrow-to-region beg end)
260 (goto-char (point-min))
261 (let ((current (or (mm-mime-charset (mm-charset-after))
262 (and use-ascii 'us-ascii)))
263 charset struct space newline paragraph)
264 (while (not (eobp))
265 (setq charset (mm-mime-charset (mm-charset-after)))
266 (cond
267 ;; The charset remains the same.
268 ((eq charset 'us-ascii))
269 ((or (and use-ascii (not charset))
270 (eq charset current))
271 (setq space nil
272 newline nil
273 paragraph nil))
274 ;; The initial charset was ascii.
275 ((eq current 'us-ascii)
276 (setq current charset
277 space nil
278 newline nil
279 paragraph nil))
280 ;; We have a change in charsets.
281 (t
282 (push (append
283 orig-tag
284 (list (cons 'contents
285 (buffer-substring-no-properties
286 beg (or paragraph newline space (point))))))
287 struct)
288 (setq beg (or paragraph newline space (point))
289 current charset
290 space nil
291 newline nil
292 paragraph nil)))
293 ;; Compute places where it might be nice to break the part.
294 (cond
295 ((memq (following-char) '(? ?\t))
296 (setq space (1+ (point))))
297 ((and (eq (following-char) ?\n)
298 (not (bobp))
299 (eq (char-after (1- (point))) ?\n))
300 (setq paragraph (point)))
301 ((eq (following-char) ?\n)
302 (setq newline (1+ (point)))))
303 (forward-char 1))
304 ;; Do the final part.
305 (unless (= beg (point))
306 (push (append orig-tag
307 (list (cons 'contents
308 (buffer-substring-no-properties
309 beg (point)))))
310 struct))
311 struct))))
312
313 (defun mml-read-tag ()
314 "Read a tag and return the contents."
315 (let ((orig-point (point))
316 contents name elem val)
317 (forward-char 2)
318 (setq name (buffer-substring-no-properties
319 (point) (progn (forward-sexp 1) (point))))
320 (skip-chars-forward " \t\n")
321 (while (not (looking-at ">[ \t]*\n?"))
322 (setq elem (buffer-substring-no-properties
323 (point) (progn (forward-sexp 1) (point))))
324 (skip-chars-forward "= \t\n")
325 (setq val (buffer-substring-no-properties
326 (point) (progn (forward-sexp 1) (point))))
327 (when (string-match "^\"\\(.*\\)\"$" val)
328 (setq val (match-string 1 val)))
329 (push (cons (intern elem) val) contents)
330 (skip-chars-forward " \t\n"))
331 (goto-char (match-end 0))
332 ;; Don't skip the leading space.
333 ;;(skip-chars-forward " \t\n")
334 ;; Put the tag location into the returned contents
335 (setq contents (append (list (cons 'tag-location orig-point)) contents))
336 (cons (intern name) (nreverse contents))))
337
338 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
339 (let ((str (buffer-substring-no-properties start end))
340 (bufstart start) tmp)
341 (while (setq tmp (text-property-any start end 'hard 't))
342 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
343 '(hard t) str)
344 (setq start (1+ tmp)))
345 str))
346
347 (defun mml-read-part (&optional mml)
348 "Return the buffer up till the next part, multipart or closing part or multipart.
349 If MML is non-nil, return the buffer up till the correspondent mml tag."
350 (let ((beg (point)) (count 1))
351 ;; If the tag ended at the end of the line, we go to the next line.
352 (when (looking-at "[ \t]*\n")
353 (forward-line 1))
354 (if mml
355 (progn
356 (while (and (> count 0) (not (eobp)))
357 (if (re-search-forward "<#\\(/\\)?mml." nil t)
358 (setq count (+ count (if (match-beginning 1) -1 1)))
359 (goto-char (point-max))))
360 (mml-buffer-substring-no-properties-except-hard-newlines
361 beg (if (> count 0)
362 (point)
363 (match-beginning 0))))
364 (if (re-search-forward
365 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
366 (prog1
367 (mml-buffer-substring-no-properties-except-hard-newlines
368 beg (match-beginning 0))
369 (if (or (not (match-beginning 1))
370 (equal (match-string 2) "multipart"))
371 (goto-char (match-beginning 0))
372 (when (looking-at "[ \t]*\n")
373 (forward-line 1))))
374 (mml-buffer-substring-no-properties-except-hard-newlines
375 beg (goto-char (point-max)))))))
376
377 (defvar mml-boundary nil)
378 (defvar mml-base-boundary "-=-=")
379 (defvar mml-multipart-number 0)
380
381 (defun mml-generate-mime ()
382 "Generate a MIME message based on the current MML document."
383 (let ((cont (mml-parse))
384 (mml-multipart-number mml-multipart-number))
385 (if (not cont)
386 nil
387 (with-temp-buffer
388 (if (and (consp (car cont))
389 (= (length cont) 1))
390 (mml-generate-mime-1 (car cont))
391 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
392 cont)))
393 (buffer-string)))))
394
395 (defun mml-generate-mime-1 (cont)
396 (let ((mm-use-ultra-safe-encoding
397 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
398 (save-restriction
399 (narrow-to-region (point) (point))
400 (mml-tweak-part cont)
401 (cond
402 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
403 (let ((raw (cdr (assq 'raw cont)))
404 coded encoding charset filename type flowed)
405 (setq type (or (cdr (assq 'type cont)) "text/plain"))
406 (if (and (not raw)
407 (member (car (split-string type "/")) '("text" "message")))
408 (progn
409 (with-temp-buffer
410 (setq charset (mm-charset-to-coding-system
411 (cdr (assq 'charset cont))))
412 (when (eq charset 'ascii)
413 (setq charset nil))
414 (cond
415 ((cdr (assq 'buffer cont))
416 (insert-buffer-substring (cdr (assq 'buffer cont))))
417 ((and (setq filename (cdr (assq 'filename cont)))
418 (not (equal (cdr (assq 'nofile cont)) "yes")))
419 (let ((coding-system-for-read charset))
420 (mm-insert-file-contents filename)))
421 ((eq 'mml (car cont))
422 (insert (cdr (assq 'contents cont))))
423 (t
424 (save-restriction
425 (narrow-to-region (point) (point))
426 (insert (cdr (assq 'contents cont)))
427 ;; Remove quotes from quoted tags.
428 (goto-char (point-min))
429 (while (re-search-forward
430 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
431 nil t)
432 (delete-region (+ (match-beginning 0) 2)
433 (+ (match-beginning 0) 3))))))
434 (cond
435 ((eq (car cont) 'mml)
436 (let ((mml-boundary (mml-compute-boundary cont))
437 (mml-generate-default-type "text/plain"))
438 (mml-to-mime))
439 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
440 ;; ignore 0x1b, it is part of iso-2022-jp
441 (setq encoding (mm-body-7-or-8))))
442 ((string= (car (split-string type "/")) "message")
443 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
444 ;; ignore 0x1b, it is part of iso-2022-jp
445 (setq encoding (mm-body-7-or-8))))
446 (t
447 ;; Only perform format=flowed filling on text/plain
448 ;; parts where there either isn't a format parameter
449 ;; in the mml tag or it says "flowed" and there
450 ;; actually are hard newlines in the text.
451 (let (use-hard-newlines)
452 (when (and (string= type "text/plain")
453 (not (string= (cdr (assq 'sign cont)) "pgp"))
454 (or (null (assq 'format cont))
455 (string= (cdr (assq 'format cont))
456 "flowed"))
457 (setq use-hard-newlines
458 (text-property-any
459 (point-min) (point-max) 'hard 't)))
460 (fill-flowed-encode)
461 ;; Indicate that `mml-insert-mime-headers' should
462 ;; insert a "; format=flowed" string unless the
463 ;; user has already specified it.
464 (setq flowed (null (assq 'format cont)))))
465 (setq charset (mm-encode-body charset))
466 (setq encoding (mm-body-encoding
467 charset (cdr (assq 'encoding cont))))))
468 (setq coded (buffer-string)))
469 (mml-insert-mime-headers cont type charset encoding flowed)
470 (insert "\n")
471 (insert coded))
472 (mm-with-unibyte-buffer
473 (cond
474 ((cdr (assq 'buffer cont))
475 (insert (with-current-buffer (cdr (assq 'buffer cont))
476 (mm-with-unibyte-current-buffer
477 (buffer-string)))))
478 ((and (setq filename (cdr (assq 'filename cont)))
479 (not (equal (cdr (assq 'nofile cont)) "yes")))
480 (let ((coding-system-for-read mm-binary-coding-system))
481 (mm-insert-file-contents filename nil nil nil nil t)))
482 (t
483 (insert (cdr (assq 'contents cont)))))
484 (setq encoding (mm-encode-buffer type)
485 coded (mm-string-as-multibyte (buffer-string))))
486 (mml-insert-mime-headers cont type charset encoding nil)
487 (insert "\n")
488 (mm-with-unibyte-current-buffer
489 (insert coded)))))
490 ((eq (car cont) 'external)
491 (insert "Content-Type: message/external-body")
492 (let ((parameters (mml-parameter-string
493 cont '(expiration size permission)))
494 (name (cdr (assq 'name cont)))
495 (url (cdr (assq 'url cont))))
496 (when name
497 (setq name (mml-parse-file-name name))
498 (if (stringp name)
499 (mml-insert-parameter
500 (mail-header-encode-parameter "name" name)
501 "access-type=local-file")
502 (mml-insert-parameter
503 (mail-header-encode-parameter
504 "name" (file-name-nondirectory (nth 2 name)))
505 (mail-header-encode-parameter "site" (nth 1 name))
506 (mail-header-encode-parameter
507 "directory" (file-name-directory (nth 2 name))))
508 (mml-insert-parameter
509 (concat "access-type="
510 (if (member (nth 0 name) '("ftp@" "anonymous@"))
511 "anon-ftp"
512 "ftp")))))
513 (when url
514 (mml-insert-parameter
515 (mail-header-encode-parameter "url" url)
516 "access-type=url"))
517 (when parameters
518 (mml-insert-parameter-string
519 cont '(expiration size permission))))
520 (insert "\n\n")
521 (insert "Content-Type: " (cdr (assq 'type cont)) "\n")
522 (insert "Content-ID: " (message-make-message-id) "\n")
523 (insert "Content-Transfer-Encoding: "
524 (or (cdr (assq 'encoding cont)) "binary"))
525 (insert "\n\n")
526 (insert (or (cdr (assq 'contents cont))))
527 (insert "\n"))
528 ((eq (car cont) 'multipart)
529 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
530 (mml-generate-default-type (if (equal type "digest")
531 "message/rfc822"
532 "text/plain"))
533 (handler (assoc type mml-generate-multipart-alist)))
534 (if handler
535 (funcall (cdr handler) cont)
536 ;; No specific handler. Use default one.
537 (let ((mml-boundary (mml-compute-boundary cont)))
538 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
539 type mml-boundary)
540 (if (cdr (assq 'start cont))
541 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
542 "\n"))
543 (let ((cont cont) part)
544 (while (setq part (pop cont))
545 ;; Skip `multipart' and attributes.
546 (when (and (consp part) (consp (cdr part)))
547 (insert "\n--" mml-boundary "\n")
548 (mml-generate-mime-1 part))))
549 (insert "\n--" mml-boundary "--\n")))))
550 (t
551 (error "Invalid element: %S" cont)))
552 ;; handle sign & encrypt tags in a semi-smart way.
553 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
554 (encrypt-item (assoc (cdr (assq 'encrypt cont))
555 mml-encrypt-alist))
556 sender recipients)
557 (when (or sign-item encrypt-item)
558 (when (setq sender (cdr (assq 'sender cont)))
559 (message-options-set 'mml-sender sender)
560 (message-options-set 'message-sender sender))
561 (if (setq recipients (cdr (assq 'recipients cont)))
562 (message-options-set 'message-recipients recipients))
563 (let ((style (mml-signencrypt-style (first (or sign-item encrypt-item)))))
564 ;; check if: we're both signing & encrypting, both methods
565 ;; are the same (why would they be different?!), and that
566 ;; the signencrypt style allows for combined operation.
567 (if (and sign-item encrypt-item (equal (first sign-item)
568 (first encrypt-item))
569 (equal style 'combined))
570 (funcall (nth 1 encrypt-item) cont t)
571 ;; otherwise, revert to the old behavior.
572 (when sign-item
573 (funcall (nth 1 sign-item) cont))
574 (when encrypt-item
575 (funcall (nth 1 encrypt-item) cont)))))))))
576
577 (defun mml-compute-boundary (cont)
578 "Return a unique boundary that does not exist in CONT."
579 (let ((mml-boundary (funcall mml-boundary-function
580 (incf mml-multipart-number))))
581 ;; This function tries again and again until it has found
582 ;; a unique boundary.
583 (while (not (catch 'not-unique
584 (mml-compute-boundary-1 cont))))
585 mml-boundary))
586
587 (defun mml-compute-boundary-1 (cont)
588 (let (filename)
589 (cond
590 ((eq (car cont) 'part)
591 (with-temp-buffer
592 (cond
593 ((cdr (assq 'buffer cont))
594 (insert-buffer-substring (cdr (assq 'buffer cont))))
595 ((and (setq filename (cdr (assq 'filename cont)))
596 (not (equal (cdr (assq 'nofile cont)) "yes")))
597 (mm-insert-file-contents filename nil nil nil nil t))
598 (t
599 (insert (cdr (assq 'contents cont)))))
600 (goto-char (point-min))
601 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
602 nil t)
603 (setq mml-boundary (funcall mml-boundary-function
604 (incf mml-multipart-number)))
605 (throw 'not-unique nil))))
606 ((eq (car cont) 'multipart)
607 (mapcar 'mml-compute-boundary-1 (cddr cont))))
608 t))
609
610 (defun mml-make-boundary (number)
611 (concat (make-string (% number 60) ?=)
612 (if (> number 17)
613 (format "%x" number)
614 "")
615 mml-base-boundary))
616
617 (defun mml-insert-mime-headers (cont type charset encoding flowed)
618 (let (parameters id disposition description)
619 (setq parameters
620 (mml-parameter-string
621 cont mml-content-type-parameters))
622 (when (or charset
623 parameters
624 flowed
625 (not (equal type mml-generate-default-type))
626 mml-insert-mime-headers-always)
627 (when (consp charset)
628 (error
629 "Can't encode a part with several charsets"))
630 (insert "Content-Type: " type)
631 (when charset
632 (insert "; " (mail-header-encode-parameter
633 "charset" (symbol-name charset))))
634 (when flowed
635 (insert "; format=flowed"))
636 (when parameters
637 (mml-insert-parameter-string
638 cont mml-content-type-parameters))
639 (insert "\n"))
640 (when (setq id (cdr (assq 'id cont)))
641 (insert "Content-ID: " id "\n"))
642 (setq parameters
643 (mml-parameter-string
644 cont mml-content-disposition-parameters))
645 (when (or (setq disposition (cdr (assq 'disposition cont)))
646 parameters)
647 (insert "Content-Disposition: " (or disposition "inline"))
648 (when parameters
649 (mml-insert-parameter-string
650 cont mml-content-disposition-parameters))
651 (insert "\n"))
652 (unless (eq encoding '7bit)
653 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
654 (when (setq description (cdr (assq 'description cont)))
655 (insert "Content-Description: "
656 (mail-encode-encoded-word-string description) "\n"))))
657
658 (defun mml-parameter-string (cont types)
659 (let ((string "")
660 value type)
661 (while (setq type (pop types))
662 (when (setq value (cdr (assq type cont)))
663 ;; Strip directory component from the filename parameter.
664 (when (eq type 'filename)
665 (setq value (file-name-nondirectory value)))
666 (setq string (concat string "; "
667 (mail-header-encode-parameter
668 (symbol-name type) value)))))
669 (when (not (zerop (length string)))
670 string)))
671
672 (defun mml-insert-parameter-string (cont types)
673 (let (value type)
674 (while (setq type (pop types))
675 (when (setq value (cdr (assq type cont)))
676 ;; Strip directory component from the filename parameter.
677 (when (eq type 'filename)
678 (setq value (file-name-nondirectory value)))
679 (mml-insert-parameter
680 (mail-header-encode-parameter
681 (symbol-name type) value))))))
682
683 (eval-when-compile
684 (defvar ange-ftp-name-format)
685 (defvar efs-path-regexp))
686 (defun mml-parse-file-name (path)
687 (if (if (boundp 'efs-path-regexp)
688 (string-match efs-path-regexp path)
689 (if (boundp 'ange-ftp-name-format)
690 (string-match (car ange-ftp-name-format) path)))
691 (list (match-string 1 path) (match-string 2 path)
692 (substring path (1+ (match-end 2))))
693 path))
694
695 (defun mml-insert-buffer (buffer)
696 "Insert BUFFER at point and quote any MML markup."
697 (save-restriction
698 (narrow-to-region (point) (point))
699 (insert-buffer-substring buffer)
700 (mml-quote-region (point-min) (point-max))
701 (goto-char (point-max))))
702
703 ;;;
704 ;;; Transforming MIME to MML
705 ;;;
706
707 (defun mime-to-mml (&optional handles)
708 "Translate the current buffer (which should be a message) into MML.
709 If HANDLES is non-nil, use it instead reparsing the buffer."
710 ;; First decode the head.
711 (save-restriction
712 (message-narrow-to-head)
713 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
714 (mail-decode-encoded-word-region (point-min) (point-max))))
715 (unless handles
716 (setq handles (mm-dissect-buffer t)))
717 (goto-char (point-min))
718 (search-forward "\n\n" nil t)
719 (delete-region (point) (point-max))
720 (if (stringp (car handles))
721 (mml-insert-mime handles)
722 (mml-insert-mime handles t))
723 (mm-destroy-parts handles)
724 (save-restriction
725 (message-narrow-to-head)
726 ;; Remove them, they are confusing.
727 (message-remove-header "Content-Type")
728 (message-remove-header "MIME-Version")
729 (message-remove-header "Content-Disposition")
730 (message-remove-header "Content-Transfer-Encoding")))
731
732 (defun mml-to-mime ()
733 "Translate the current buffer from MML to MIME."
734 (message-encode-message-body)
735 (save-restriction
736 (message-narrow-to-headers-or-head)
737 ;; Skip past any From_ headers.
738 (while (looking-at "From ")
739 (forward-line 1))
740 (let ((mail-parse-charset message-default-charset))
741 (mail-encode-encoded-word-buffer))))
742
743 (defun mml-insert-mime (handle &optional no-markup)
744 (let (textp buffer mmlp)
745 ;; Determine type and stuff.
746 (unless (stringp (car handle))
747 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
748 (save-excursion
749 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
750 (mm-insert-part handle)
751 (if (setq mmlp (equal (mm-handle-media-type handle)
752 "message/rfc822"))
753 (mime-to-mml)))))
754 (if mmlp
755 (mml-insert-mml-markup handle nil t t)
756 (unless (and no-markup
757 (equal (mm-handle-media-type handle) "text/plain"))
758 (mml-insert-mml-markup handle buffer textp)))
759 (cond
760 (mmlp
761 (insert-buffer-substring buffer)
762 (goto-char (point-max))
763 (insert "<#/mml>\n"))
764 ((stringp (car handle))
765 (mapcar 'mml-insert-mime (cdr handle))
766 (insert "<#/multipart>\n"))
767 (textp
768 (let ((charset (mail-content-type-get
769 (mm-handle-type handle) 'charset))
770 (start (point)))
771 (if (eq charset 'gnus-decoded)
772 (mm-insert-part handle)
773 (insert (mm-decode-string (mm-get-part handle) charset)))
774 (mml-quote-region start (point)))
775 (goto-char (point-max)))
776 (t
777 (insert "<#/part>\n")))))
778
779 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
780 "Take a MIME handle and insert an MML tag."
781 (if (stringp (car handle))
782 (progn
783 (insert "<#multipart type=" (mm-handle-media-subtype handle))
784 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
785 (when start
786 (insert " start=\"" start "\"")))
787 (insert ">\n"))
788 (if mmlp
789 (insert "<#mml type=" (mm-handle-media-type handle))
790 (insert "<#part type=" (mm-handle-media-type handle)))
791 (dolist (elem (append (cdr (mm-handle-type handle))
792 (cdr (mm-handle-disposition handle))))
793 (unless (symbolp (cdr elem))
794 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
795 (when (mm-handle-id handle)
796 (insert " id=\"" (mm-handle-id handle) "\""))
797 (when (mm-handle-disposition handle)
798 (insert " disposition=" (car (mm-handle-disposition handle))))
799 (when buffer
800 (insert " buffer=\"" (buffer-name buffer) "\""))
801 (when nofile
802 (insert " nofile=yes"))
803 (when (mm-handle-description handle)
804 (insert " description=\"" (mm-handle-description handle) "\""))
805 (insert ">\n")))
806
807 (defun mml-insert-parameter (&rest parameters)
808 "Insert PARAMETERS in a nice way."
809 (dolist (param parameters)
810 (insert ";")
811 (let ((point (point)))
812 (insert " " param)
813 (when (> (current-column) 71)
814 (goto-char point)
815 (insert "\n ")
816 (end-of-line)))))
817
818 ;;;
819 ;;; Mode for inserting and editing MML forms
820 ;;;
821
822 (defvar mml-mode-map
823 (let ((sign (make-sparse-keymap))
824 (encrypt (make-sparse-keymap))
825 (signpart (make-sparse-keymap))
826 (encryptpart (make-sparse-keymap))
827 (map (make-sparse-keymap))
828 (main (make-sparse-keymap)))
829 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
830 (define-key sign "o" 'mml-secure-message-sign-pgp)
831 (define-key sign "s" 'mml-secure-message-sign-smime)
832 (define-key signpart "p" 'mml-secure-sign-pgpmime)
833 (define-key signpart "o" 'mml-secure-sign-pgp)
834 (define-key signpart "s" 'mml-secure-sign-smime)
835 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
836 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
837 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
838 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
839 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
840 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
841 (define-key map "\C-n" 'mml-unsecure-message)
842 (define-key map "f" 'mml-attach-file)
843 (define-key map "b" 'mml-attach-buffer)
844 (define-key map "e" 'mml-attach-external)
845 (define-key map "q" 'mml-quote-region)
846 (define-key map "m" 'mml-insert-multipart)
847 (define-key map "p" 'mml-insert-part)
848 (define-key map "v" 'mml-validate)
849 (define-key map "P" 'mml-preview)
850 (define-key map "s" sign)
851 (define-key map "S" signpart)
852 (define-key map "c" encrypt)
853 (define-key map "C" encryptpart)
854 ;;(define-key map "n" 'mml-narrow-to-part)
855 ;; `M-m' conflicts with `back-to-indentation'.
856 ;; (define-key main "\M-m" map)
857 (define-key main "\C-c\C-m" map)
858 main))
859
860 (easy-menu-define
861 mml-menu mml-mode-map ""
862 `("Attachments"
863 ["Attach File..." mml-attach-file
864 ,@(if (featurep 'xemacs) '(t)
865 '(:help "Attach a file at point"))]
866 ["Attach Buffer..." mml-attach-buffer t]
867 ["Attach External..." mml-attach-external t]
868 ["Insert Part..." mml-insert-part t]
869 ["Insert Multipart..." mml-insert-multipart t]
870 ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t]
871 ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t]
872 ["PGP Sign" mml-secure-message-sign-pgp t]
873 ["PGP Encrypt" mml-secure-message-encrypt-pgp t]
874 ["S/MIME Sign" mml-secure-message-sign-smime t]
875 ["S/MIME Encrypt" mml-secure-message-encrypt-smime t]
876 ("Secure MIME part"
877 ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t]
878 ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t]
879 ["PGP Sign Part" mml-secure-sign-pgp t]
880 ["PGP Encrypt Part" mml-secure-encrypt-pgp t]
881 ["S/MIME Sign Part" mml-secure-sign-smime t]
882 ["S/MIME Encrypt Part" mml-secure-encrypt-smime t])
883 ["Encrypt/Sign off" mml-unsecure-message t]
884 ;;["Narrow" mml-narrow-to-part t]
885 ["Quote MML" mml-quote-region t]
886 ["Validate MML" mml-validate t]
887 ["Preview" mml-preview t]))
888
889 (defvar mml-mode nil
890 "Minor mode for editing MML.")
891
892 (defun mml-mode (&optional arg)
893 "Minor mode for editing MML.
894 MML is the MIME Meta Language, a minor mode for composing MIME articles.
895 See Info node `(emacs-mime)Composing'.
896
897 \\{mml-mode-map}"
898 (interactive "P")
899 (when (set (make-local-variable 'mml-mode)
900 (if (null arg) (not mml-mode)
901 (> (prefix-numeric-value arg) 0)))
902 (gnus-add-minor-mode 'mml-mode " MML" mml-mode-map)
903 (easy-menu-add mml-menu mml-mode-map)
904 (run-hooks 'mml-mode-hook)))
905
906 ;;;
907 ;;; Helper functions for reading MIME stuff from the minibuffer and
908 ;;; inserting stuff to the buffer.
909 ;;;
910
911 (defun mml-minibuffer-read-file (prompt)
912 (let* ((completion-ignored-extensions nil)
913 (file (read-file-name prompt nil nil t)))
914 ;; Prevent some common errors. This is inspired by similar code in
915 ;; VM.
916 (when (file-directory-p file)
917 (error "%s is a directory, cannot attach" file))
918 (unless (file-exists-p file)
919 (error "No such file: %s" file))
920 (unless (file-readable-p file)
921 (error "Permission denied: %s" file))
922 file))
923
924 (defun mml-minibuffer-read-type (name &optional default)
925 (mailcap-parse-mimetypes)
926 (let* ((default (or default
927 (mm-default-file-encoding name)
928 ;; Perhaps here we should check what the file
929 ;; looks like, and offer text/plain if it looks
930 ;; like text/plain.
931 "application/octet-stream"))
932 (string (completing-read
933 (format "Content type (default %s): " default)
934 (mapcar 'list (mailcap-mime-types)))))
935 (if (not (equal string ""))
936 string
937 default)))
938
939 (defun mml-minibuffer-read-description ()
940 (let ((description (read-string "One line description: ")))
941 (when (string-match "\\`[ \t]*\\'" description)
942 (setq description nil))
943 description))
944
945 (defun mml-minibuffer-read-disposition (type &optional default)
946 (unless default (setq default
947 (if (and (string-match "\\`text/" type)
948 (not (string-match "\\`text/rtf\\'" type)))
949 "inline"
950 "attachment")))
951 (let ((disposition (completing-read
952 (format "Disposition (default %s): " default)
953 '(("attachment") ("inline") (""))
954 nil t nil nil default)))
955 (if (not (equal disposition ""))
956 disposition
957 default)))
958
959 (defun mml-quote-region (beg end)
960 "Quote the MML tags in the region."
961 (interactive "r")
962 (save-excursion
963 (save-restriction
964 ;; Temporarily narrow the region to defend from changes
965 ;; invalidating END.
966 (narrow-to-region beg end)
967 (goto-char (point-min))
968 ;; Quote parts.
969 (while (re-search-forward
970 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
971 ;; Insert ! after the #.
972 (goto-char (+ (match-beginning 0) 2))
973 (insert "!")))))
974
975 (defun mml-insert-tag (name &rest plist)
976 "Insert an MML tag described by NAME and PLIST."
977 (when (symbolp name)
978 (setq name (symbol-name name)))
979 (insert "<#" name)
980 (while plist
981 (let ((key (pop plist))
982 (value (pop plist)))
983 (when value
984 ;; Quote VALUE if it contains suspicious characters.
985 (when (string-match "[\"'\\~/*;() \t\n]" value)
986 (setq value (with-output-to-string
987 (let (print-escape-nonascii)
988 (prin1 value)))))
989 (insert (format " %s=%s" key value)))))
990 (insert ">\n"))
991
992 (defun mml-insert-empty-tag (name &rest plist)
993 "Insert an empty MML tag described by NAME and PLIST."
994 (when (symbolp name)
995 (setq name (symbol-name name)))
996 (apply #'mml-insert-tag name plist)
997 (insert "<#/" name ">\n"))
998
999 ;;; Attachment functions.
1000
1001 (defun mml-attach-file (file &optional type description disposition)
1002 "Attach a file to the outgoing MIME message.
1003 The file is not inserted or encoded until you send the message with
1004 `\\[message-send-and-exit]' or `\\[message-send]'.
1005
1006 FILE is the name of the file to attach. TYPE is its content-type, a
1007 string of the form \"type/subtype\". DESCRIPTION is a one-line
1008 description of the attachment."
1009 (interactive
1010 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1011 (type (mml-minibuffer-read-type file))
1012 (description (mml-minibuffer-read-description))
1013 (disposition (mml-minibuffer-read-disposition type)))
1014 (list file type description disposition)))
1015 (mml-insert-empty-tag 'part
1016 'type type
1017 'filename file
1018 'disposition (or disposition "attachment")
1019 'description description))
1020
1021 (defun mml-attach-buffer (buffer &optional type description)
1022 "Attach a buffer to the outgoing MIME message.
1023 See `mml-attach-file' for details of operation."
1024 (interactive
1025 (let* ((buffer (read-buffer "Attach buffer: "))
1026 (type (mml-minibuffer-read-type buffer "text/plain"))
1027 (description (mml-minibuffer-read-description)))
1028 (list buffer type description)))
1029 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1030 'disposition "attachment" 'description description))
1031
1032 (defun mml-attach-external (file &optional type description)
1033 "Attach an external file into the buffer.
1034 FILE is an ange-ftp/efs specification of the part location.
1035 TYPE is the MIME type to use."
1036 (interactive
1037 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1038 (type (mml-minibuffer-read-type file))
1039 (description (mml-minibuffer-read-description)))
1040 (list file type description)))
1041 (mml-insert-empty-tag 'external 'type type 'name file
1042 'disposition "attachment" 'description description))
1043
1044 (defun mml-insert-multipart (&optional type)
1045 (interactive (list (completing-read "Multipart type (default mixed): "
1046 '(("mixed") ("alternative") ("digest") ("parallel")
1047 ("signed") ("encrypted"))
1048 nil nil "mixed")))
1049 (or type
1050 (setq type "mixed"))
1051 (mml-insert-empty-tag "multipart" 'type type)
1052 (forward-line -1))
1053
1054 (defun mml-insert-part (&optional type)
1055 (interactive
1056 (list (mml-minibuffer-read-type "")))
1057 (mml-insert-tag 'part 'type type 'disposition "inline")
1058 (forward-line -1))
1059
1060 (defun mml-preview-insert-mail-followup-to ()
1061 "Insert a Mail-Followup-To header before previewing an article.
1062 Should be adopted if code in `message-send-mail' is changed."
1063 (when (and (message-mail-p)
1064 (message-subscribed-p)
1065 (not (mail-fetch-field "mail-followup-to"))
1066 (message-make-mail-followup-to))
1067 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1068 (insert (message-make-mail-followup-to))))
1069
1070 (defun mml-preview (&optional raw)
1071 "Display current buffer with Gnus, in a new buffer.
1072 If RAW, don't highlight the article."
1073 (interactive "P")
1074 (save-excursion
1075 (let* ((buf (current-buffer))
1076 (message-options message-options)
1077 (message-this-is-mail (message-mail-p))
1078 (message-this-is-news (message-news-p))
1079 (message-posting-charset (or (gnus-setup-posting-charset
1080 (save-restriction
1081 (message-narrow-to-headers-or-head)
1082 (message-fetch-field "Newsgroups")))
1083 message-posting-charset)))
1084 (message-options-set-recipient)
1085 (pop-to-buffer (generate-new-buffer
1086 (concat (if raw "*Raw MIME preview of "
1087 "*MIME preview of ") (buffer-name))))
1088 (when (boundp 'gnus-buffers)
1089 (push (current-buffer) gnus-buffers))
1090 (erase-buffer)
1091 (insert-buffer-substring buf)
1092 (mml-preview-insert-mail-followup-to)
1093 (let ((message-deletable-headers (if (message-news-p)
1094 nil
1095 message-deletable-headers)))
1096 (message-generate-headers
1097 (copy-sequence (if (message-news-p)
1098 message-required-news-headers
1099 message-required-mail-headers))))
1100 (if (re-search-forward
1101 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1102 (replace-match "\n"))
1103 (let ((mail-header-separator ""));; mail-header-separator is removed.
1104 (mml-to-mime))
1105 (if raw
1106 (when (fboundp 'set-buffer-multibyte)
1107 (let ((s (buffer-string)))
1108 ;; Insert the content into unibyte buffer.
1109 (erase-buffer)
1110 (mm-disable-multibyte)
1111 (insert s)))
1112 (let ((gnus-newsgroup-charset (car message-posting-charset))
1113 gnus-article-prepare-hook gnus-original-article-buffer)
1114 (run-hooks 'gnus-article-decode-hook)
1115 (let ((gnus-newsgroup-name "dummy")
1116 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1117 (gnus-make-hashtable 5))))
1118 (gnus-article-prepare-display))))
1119 ;; Disable article-mode-map.
1120 (use-local-map nil)
1121 (gnus-make-local-hook 'kill-buffer-hook)
1122 (add-hook 'kill-buffer-hook
1123 (lambda ()
1124 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1125 (setq buffer-read-only t)
1126 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1127 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1128 (local-set-key "\r"
1129 (lambda ()
1130 (interactive)
1131 (widget-button-press (point))))
1132 (local-set-key gnus-mouse-2
1133 (lambda (event)
1134 (interactive "@e")
1135 (widget-button-press (widget-event-point event) event)))
1136 (goto-char (point-min)))))
1137
1138 (defun mml-validate ()
1139 "Validate the current MML document."
1140 (interactive)
1141 (mml-parse))
1142
1143 (defun mml-tweak-part (cont)
1144 "Tweak a MML part."
1145 (let ((tweak (cdr (assq 'tweak cont)))
1146 func)
1147 (cond
1148 (tweak
1149 (setq func
1150 (or (cdr (assoc tweak mml-tweak-function-alist))
1151 (intern tweak))))
1152 (mml-tweak-type-alist
1153 (let ((alist mml-tweak-type-alist)
1154 (type (or (cdr (assq 'type cont)) "text/plain")))
1155 (while alist
1156 (if (string-match (caar alist) type)
1157 (setq func (cdar alist)
1158 alist nil)
1159 (setq alist (cdr alist)))))))
1160 (if func
1161 (funcall func cont)
1162 cont)
1163 (let ((alist mml-tweak-sexp-alist))
1164 (while alist
1165 (if (eval (caar alist))
1166 (funcall (cdar alist) cont))
1167 (setq alist (cdr alist)))))
1168 cont)
1169
1170 (defun mml-tweak-externalize-attachments (cont)
1171 "Tweak attached files as external parts."
1172 (let (filename-cons)
1173 (when (and (eq (car cont) 'part)
1174 (not (cdr (assq 'buffer cont)))
1175 (and (setq filename-cons (assq 'filename cont))
1176 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1177 (setcar cont 'external)
1178 (setcar filename-cons 'name))))
1179
1180 (provide 'mml)
1181
1182 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1183 ;;; mml.el ends here