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