]> 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 (with-temp-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 (setq charset (mm-encode-body)))
519 (insert contents)))))
520 (setq encoding (mm-encode-buffer type)
521 coded (mm-string-as-multibyte (buffer-string))))
522 (mml-insert-mime-headers cont type charset encoding nil)
523 (insert "\n")
524 (mm-with-unibyte-current-buffer
525 (insert coded)))))
526 ((eq (car cont) 'external)
527 (insert "Content-Type: message/external-body")
528 (let ((parameters (mml-parameter-string
529 cont '(expiration size permission)))
530 (name (cdr (assq 'name cont)))
531 (url (cdr (assq 'url cont))))
532 (when name
533 (setq name (mml-parse-file-name name))
534 (if (stringp name)
535 (mml-insert-parameter
536 (mail-header-encode-parameter "name" name)
537 "access-type=local-file")
538 (mml-insert-parameter
539 (mail-header-encode-parameter
540 "name" (file-name-nondirectory (nth 2 name)))
541 (mail-header-encode-parameter "site" (nth 1 name))
542 (mail-header-encode-parameter
543 "directory" (file-name-directory (nth 2 name))))
544 (mml-insert-parameter
545 (concat "access-type="
546 (if (member (nth 0 name) '("ftp@" "anonymous@"))
547 "anon-ftp"
548 "ftp")))))
549 (when url
550 (mml-insert-parameter
551 (mail-header-encode-parameter "url" url)
552 "access-type=url"))
553 (when parameters
554 (mml-insert-parameter-string
555 cont '(expiration size permission)))
556 (insert "\n\n")
557 (insert "Content-Type: "
558 (or (cdr (assq 'type cont))
559 (if name
560 (or (mm-default-file-encoding name)
561 "application/octet-stream")
562 "text/plain"))
563 "\n")
564 (insert "Content-ID: " (message-make-message-id) "\n")
565 (insert "Content-Transfer-Encoding: "
566 (or (cdr (assq 'encoding cont)) "binary"))
567 (insert "\n\n")
568 (insert (or (cdr (assq 'contents cont))))
569 (insert "\n")))
570 ((eq (car cont) 'multipart)
571 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
572 (mml-generate-default-type (if (equal type "digest")
573 "message/rfc822"
574 "text/plain"))
575 (handler (assoc type mml-generate-multipart-alist)))
576 (if handler
577 (funcall (cdr handler) cont)
578 ;; No specific handler. Use default one.
579 (let ((mml-boundary (mml-compute-boundary cont)))
580 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
581 type mml-boundary)
582 (if (cdr (assq 'start cont))
583 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
584 "\n"))
585 (let ((cont cont) part)
586 (while (setq part (pop cont))
587 ;; Skip `multipart' and attributes.
588 (when (and (consp part) (consp (cdr part)))
589 (insert "\n--" mml-boundary "\n")
590 (mml-generate-mime-1 part)
591 (goto-char (point-max)))))
592 (insert "\n--" mml-boundary "--\n")))))
593 (t
594 (error "Invalid element: %S" cont)))
595 ;; handle sign & encrypt tags in a semi-smart way.
596 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
597 (encrypt-item (assoc (cdr (assq 'encrypt cont))
598 mml-encrypt-alist))
599 sender recipients)
600 (when (or sign-item encrypt-item)
601 (when (setq sender (cdr (assq 'sender cont)))
602 (message-options-set 'mml-sender sender)
603 (message-options-set 'message-sender sender))
604 (if (setq recipients (cdr (assq 'recipients cont)))
605 (message-options-set 'message-recipients recipients))
606 (let ((style (mml-signencrypt-style
607 (first (or sign-item encrypt-item)))))
608 ;; check if: we're both signing & encrypting, both methods
609 ;; are the same (why would they be different?!), and that
610 ;; the signencrypt style allows for combined operation.
611 (if (and sign-item encrypt-item (equal (first sign-item)
612 (first encrypt-item))
613 (equal style 'combined))
614 (funcall (nth 1 encrypt-item) cont t)
615 ;; otherwise, revert to the old behavior.
616 (when sign-item
617 (funcall (nth 1 sign-item) cont))
618 (when encrypt-item
619 (funcall (nth 1 encrypt-item) cont)))))))))
620
621 (defun mml-compute-boundary (cont)
622 "Return a unique boundary that does not exist in CONT."
623 (let ((mml-boundary (funcall mml-boundary-function
624 (incf mml-multipart-number))))
625 ;; This function tries again and again until it has found
626 ;; a unique boundary.
627 (while (not (catch 'not-unique
628 (mml-compute-boundary-1 cont))))
629 mml-boundary))
630
631 (defun mml-compute-boundary-1 (cont)
632 (let (filename)
633 (cond
634 ((eq (car cont) 'part)
635 (with-temp-buffer
636 (cond
637 ((cdr (assq 'buffer cont))
638 (insert-buffer-substring (cdr (assq 'buffer cont))))
639 ((and (setq filename (cdr (assq 'filename cont)))
640 (not (equal (cdr (assq 'nofile cont)) "yes")))
641 (mm-insert-file-contents filename nil nil nil nil t))
642 (t
643 (insert (cdr (assq 'contents cont)))))
644 (goto-char (point-min))
645 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
646 nil t)
647 (setq mml-boundary (funcall mml-boundary-function
648 (incf mml-multipart-number)))
649 (throw 'not-unique nil))))
650 ((eq (car cont) 'multipart)
651 (mapcar 'mml-compute-boundary-1 (cddr cont))))
652 t))
653
654 (defun mml-make-boundary (number)
655 (concat (make-string (% number 60) ?=)
656 (if (> number 17)
657 (format "%x" number)
658 "")
659 mml-base-boundary))
660
661 (defun mml-insert-mime-headers (cont type charset encoding flowed)
662 (let (parameters id disposition description)
663 (setq parameters
664 (mml-parameter-string
665 cont mml-content-type-parameters))
666 (when (or charset
667 parameters
668 flowed
669 (not (equal type mml-generate-default-type))
670 mml-insert-mime-headers-always)
671 (when (consp charset)
672 (error
673 "Can't encode a part with several charsets"))
674 (insert "Content-Type: " type)
675 (when charset
676 (mml-insert-parameter
677 (mail-header-encode-parameter "charset" (symbol-name charset))))
678 (when flowed
679 (mml-insert-parameter "format=flowed"))
680 (when parameters
681 (mml-insert-parameter-string
682 cont mml-content-type-parameters))
683 (insert "\n"))
684 (when (setq id (cdr (assq 'id cont)))
685 (insert "Content-ID: " id "\n"))
686 (setq parameters
687 (mml-parameter-string
688 cont mml-content-disposition-parameters))
689 (when (or (setq disposition (cdr (assq 'disposition cont)))
690 parameters)
691 (insert "Content-Disposition: " (or disposition "inline"))
692 (when parameters
693 (mml-insert-parameter-string
694 cont mml-content-disposition-parameters))
695 (insert "\n"))
696 (unless (eq encoding '7bit)
697 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
698 (when (setq description (cdr (assq 'description cont)))
699 (insert "Content-Description: ")
700 (setq description (prog1
701 (point)
702 (insert description "\n")))
703 (mail-encode-encoded-word-region description (point)))))
704
705 (defun mml-parameter-string (cont types)
706 (let ((string "")
707 value type)
708 (while (setq type (pop types))
709 (when (setq value (cdr (assq type cont)))
710 ;; Strip directory component from the filename parameter.
711 (when (eq type 'filename)
712 (setq value (file-name-nondirectory value)))
713 (setq string (concat string "; "
714 (mail-header-encode-parameter
715 (symbol-name type) value)))))
716 (when (not (zerop (length string)))
717 string)))
718
719 (defun mml-insert-parameter-string (cont types)
720 (let (value type)
721 (while (setq type (pop types))
722 (when (setq value (cdr (assq type cont)))
723 ;; Strip directory component from the filename parameter.
724 (when (eq type 'filename)
725 (setq value (file-name-nondirectory value)))
726 (mml-insert-parameter
727 (mail-header-encode-parameter
728 (symbol-name type) value))))))
729
730 (eval-when-compile
731 (defvar ange-ftp-name-format)
732 (defvar efs-path-regexp))
733 (defun mml-parse-file-name (path)
734 (if (if (boundp 'efs-path-regexp)
735 (string-match efs-path-regexp path)
736 (if (boundp 'ange-ftp-name-format)
737 (string-match (car ange-ftp-name-format) path)))
738 (list (match-string 1 path) (match-string 2 path)
739 (substring path (1+ (match-end 2))))
740 path))
741
742 (defun mml-insert-buffer (buffer)
743 "Insert BUFFER at point and quote any MML markup."
744 (save-restriction
745 (narrow-to-region (point) (point))
746 (insert-buffer-substring buffer)
747 (mml-quote-region (point-min) (point-max))
748 (goto-char (point-max))))
749
750 ;;;
751 ;;; Transforming MIME to MML
752 ;;;
753
754 (defun mime-to-mml (&optional handles)
755 "Translate the current buffer (which should be a message) into MML.
756 If HANDLES is non-nil, use it instead reparsing the buffer."
757 ;; First decode the head.
758 (save-restriction
759 (message-narrow-to-head)
760 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
761 (mail-decode-encoded-word-region (point-min) (point-max))))
762 (unless handles
763 (setq handles (mm-dissect-buffer t)))
764 (goto-char (point-min))
765 (search-forward "\n\n" nil t)
766 (delete-region (point) (point-max))
767 (if (stringp (car handles))
768 (mml-insert-mime handles)
769 (mml-insert-mime handles t))
770 (mm-destroy-parts handles)
771 (save-restriction
772 (message-narrow-to-head)
773 ;; Remove them, they are confusing.
774 (message-remove-header "Content-Type")
775 (message-remove-header "MIME-Version")
776 (message-remove-header "Content-Disposition")
777 (message-remove-header "Content-Transfer-Encoding")))
778
779 (defun mml-to-mime ()
780 "Translate the current buffer from MML to MIME."
781 (message-encode-message-body)
782 (save-restriction
783 (message-narrow-to-headers-or-head)
784 ;; Skip past any From_ headers.
785 (while (looking-at "From ")
786 (forward-line 1))
787 (let ((mail-parse-charset message-default-charset))
788 (mail-encode-encoded-word-buffer))))
789
790 (defun mml-insert-mime (handle &optional no-markup)
791 (let (textp buffer mmlp)
792 ;; Determine type and stuff.
793 (unless (stringp (car handle))
794 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
795 (save-excursion
796 (set-buffer (setq buffer (mml-generate-new-buffer " *mml*")))
797 (mm-insert-part handle 'no-cache)
798 (if (setq mmlp (equal (mm-handle-media-type handle)
799 "message/rfc822"))
800 (mime-to-mml)))))
801 (if mmlp
802 (mml-insert-mml-markup handle nil t t)
803 (unless (and no-markup
804 (equal (mm-handle-media-type handle) "text/plain"))
805 (mml-insert-mml-markup handle buffer textp)))
806 (cond
807 (mmlp
808 (insert-buffer-substring buffer)
809 (goto-char (point-max))
810 (insert "<#/mml>\n"))
811 ((stringp (car handle))
812 (mapcar 'mml-insert-mime (cdr handle))
813 (insert "<#/multipart>\n"))
814 (textp
815 (let ((charset (mail-content-type-get
816 (mm-handle-type handle) 'charset))
817 (start (point)))
818 (if (eq charset 'gnus-decoded)
819 (mm-insert-part handle)
820 (insert (mm-decode-string (mm-get-part handle) charset)))
821 (mml-quote-region start (point)))
822 (goto-char (point-max)))
823 (t
824 (insert "<#/part>\n")))))
825
826 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
827 "Take a MIME handle and insert an MML tag."
828 (if (stringp (car handle))
829 (progn
830 (insert "<#multipart type=" (mm-handle-media-subtype handle))
831 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
832 (when start
833 (insert " start=\"" start "\"")))
834 (insert ">\n"))
835 (if mmlp
836 (insert "<#mml type=" (mm-handle-media-type handle))
837 (insert "<#part type=" (mm-handle-media-type handle)))
838 (dolist (elem (append (cdr (mm-handle-type handle))
839 (cdr (mm-handle-disposition handle))))
840 (unless (symbolp (cdr elem))
841 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
842 (when (mm-handle-id handle)
843 (insert " id=\"" (mm-handle-id handle) "\""))
844 (when (mm-handle-disposition handle)
845 (insert " disposition=" (car (mm-handle-disposition handle))))
846 (when buffer
847 (insert " buffer=\"" (buffer-name buffer) "\""))
848 (when nofile
849 (insert " nofile=yes"))
850 (when (mm-handle-description handle)
851 (insert " description=\"" (mm-handle-description handle) "\""))
852 (insert ">\n")))
853
854 (defun mml-insert-parameter (&rest parameters)
855 "Insert PARAMETERS in a nice way."
856 (let (start end)
857 (dolist (param parameters)
858 (insert ";")
859 (setq start (point))
860 (insert " " param)
861 (setq end (point))
862 (goto-char start)
863 (end-of-line)
864 (if (> (current-column) 76)
865 (progn
866 (goto-char start)
867 (insert "\n")
868 (goto-char (1+ end)))
869 (goto-char end)))))
870
871 ;;;
872 ;;; Mode for inserting and editing MML forms
873 ;;;
874
875 (defvar mml-mode-map
876 (let ((sign (make-sparse-keymap))
877 (encrypt (make-sparse-keymap))
878 (signpart (make-sparse-keymap))
879 (encryptpart (make-sparse-keymap))
880 (map (make-sparse-keymap))
881 (main (make-sparse-keymap)))
882 (define-key map "\C-s" 'mml-secure-message-sign)
883 (define-key map "\C-c" 'mml-secure-message-encrypt)
884 (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
885 (define-key map "\C-p\C-s" 'mml-secure-sign)
886 (define-key map "\C-p\C-c" 'mml-secure-encrypt)
887 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
888 (define-key sign "o" 'mml-secure-message-sign-pgp)
889 (define-key sign "s" 'mml-secure-message-sign-smime)
890 (define-key signpart "p" 'mml-secure-sign-pgpmime)
891 (define-key signpart "o" 'mml-secure-sign-pgp)
892 (define-key signpart "s" 'mml-secure-sign-smime)
893 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
894 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
895 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
896 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
897 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
898 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
899 (define-key map "\C-n" 'mml-unsecure-message)
900 (define-key map "f" 'mml-attach-file)
901 (define-key map "b" 'mml-attach-buffer)
902 (define-key map "e" 'mml-attach-external)
903 (define-key map "q" 'mml-quote-region)
904 (define-key map "m" 'mml-insert-multipart)
905 (define-key map "p" 'mml-insert-part)
906 (define-key map "v" 'mml-validate)
907 (define-key map "P" 'mml-preview)
908 (define-key map "s" sign)
909 (define-key map "S" signpart)
910 (define-key map "c" encrypt)
911 (define-key map "C" encryptpart)
912 ;;(define-key map "n" 'mml-narrow-to-part)
913 ;; `M-m' conflicts with `back-to-indentation'.
914 ;; (define-key main "\M-m" map)
915 (define-key main "\C-c\C-m" map)
916 main))
917
918 (easy-menu-define
919 mml-menu mml-mode-map ""
920 `("Attachments"
921 ["Attach File..." mml-attach-file
922 ,@(if (featurep 'xemacs) '(t)
923 '(:help "Attach a file at point"))]
924 ["Attach Buffer..." mml-attach-buffer
925 ,@(if (featurep 'xemacs) '(t)
926 '(:help "Attach a buffer to the outgoing MIME message"))]
927 ["Attach External..." mml-attach-external
928 ,@(if (featurep 'xemacs) '(t)
929 '(:help "Attach reference to file"))]
930 ;;
931 ("Change Security Method"
932 ["PGP/MIME"
933 (lambda () (interactive) (setq mml-secure-method "pgpmime"))
934 ,@(if (featurep 'xemacs) nil
935 '(:help "Set Security Method to PGP/MIME"))
936 :style radio
937 :selected (equal mml-secure-method "pgpmime") ]
938 ["S/MIME"
939 (lambda () (interactive) (setq mml-secure-method "smime"))
940 ,@(if (featurep 'xemacs) nil
941 '(:help "Set Security Method to S/MIME"))
942 :style radio
943 :selected (equal mml-secure-method "smime") ]
944 ["Inline PGP"
945 (lambda () (interactive) (setq mml-secure-method "pgp"))
946 ,@(if (featurep 'xemacs) nil
947 '(:help "Set Security Method to inline PGP"))
948 :style radio
949 :selected (equal mml-secure-method "pgp") ] )
950 ;;
951 ["Sign Message" mml-secure-message-sign t]
952 ["Encrypt Message" mml-secure-message-encrypt t]
953 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
954 ["Encrypt/Sign off" mml-unsecure-message
955 ,@(if (featurep 'xemacs) '(t)
956 '(:help "Don't Encrypt/Sign Message"))]
957 ;; Maybe we could remove these, because people who write MML most probably
958 ;; don't use the menu:
959 ["Insert Part..." mml-insert-part
960 :active (message-in-body-p)]
961 ["Insert Multipart..." mml-insert-multipart
962 :active (message-in-body-p)]
963 ;;
964 ;; Do we have separate encrypt and encrypt/sign commands for parts?
965 ["Sign Part" mml-secure-sign t]
966 ["Encrypt Part" mml-secure-encrypt t]
967 ;;["Narrow" mml-narrow-to-part t]
968 ["Quote MML in region" mml-quote-region
969 :active (message-mark-active-p)
970 ,@(if (featurep 'xemacs) nil
971 '(:help "Quote MML tags in region"))]
972 ["Validate MML" mml-validate t]
973 ["Preview" mml-preview t]
974 "----"
975 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
976 ,@(if (featurep 'xemacs) '(t)
977 '(:help "Display the Emacs MIME manual"))]
978 ["PGG manual" (lambda () (interactive) (message-info 16))
979 ,@(if (featurep 'xemacs) '(t)
980 '(:help "Display the PGG manual"))]))
981
982 (defvar mml-mode nil
983 "Minor mode for editing MML.")
984
985 (defun mml-mode (&optional arg)
986 "Minor mode for editing MML.
987 MML is the MIME Meta Language, a minor mode for composing MIME articles.
988 See Info node `(emacs-mime)Composing'.
989
990 \\{mml-mode-map}"
991 (interactive "P")
992 (when (set (make-local-variable 'mml-mode)
993 (if (null arg) (not mml-mode)
994 (> (prefix-numeric-value arg) 0)))
995 (add-minor-mode 'mml-mode " MML" mml-mode-map)
996 (easy-menu-add mml-menu mml-mode-map)
997 (when (boundp 'dnd-protocol-alist)
998 (set (make-local-variable 'dnd-protocol-alist)
999 (append mml-dnd-protocol-alist dnd-protocol-alist)))
1000 (run-hooks 'mml-mode-hook)))
1001
1002 ;;;
1003 ;;; Helper functions for reading MIME stuff from the minibuffer and
1004 ;;; inserting stuff to the buffer.
1005 ;;;
1006
1007 (defun mml-minibuffer-read-file (prompt)
1008 (let* ((completion-ignored-extensions nil)
1009 (file (read-file-name prompt nil nil t)))
1010 ;; Prevent some common errors. This is inspired by similar code in
1011 ;; VM.
1012 (when (file-directory-p file)
1013 (error "%s is a directory, cannot attach" file))
1014 (unless (file-exists-p file)
1015 (error "No such file: %s" file))
1016 (unless (file-readable-p file)
1017 (error "Permission denied: %s" file))
1018 file))
1019
1020 (defun mml-minibuffer-read-type (name &optional default)
1021 (mailcap-parse-mimetypes)
1022 (let* ((default (or default
1023 (mm-default-file-encoding name)
1024 ;; Perhaps here we should check what the file
1025 ;; looks like, and offer text/plain if it looks
1026 ;; like text/plain.
1027 "application/octet-stream"))
1028 (string (completing-read
1029 (format "Content type (default %s): " default)
1030 (mapcar 'list (mailcap-mime-types)))))
1031 (if (not (equal string ""))
1032 string
1033 default)))
1034
1035 (defun mml-minibuffer-read-description ()
1036 (let ((description (read-string "One line description: ")))
1037 (when (string-match "\\`[ \t]*\\'" description)
1038 (setq description nil))
1039 description))
1040
1041 (defun mml-minibuffer-read-disposition (type &optional default)
1042 (unless default (setq default
1043 (if (and (string-match "\\`text/" type)
1044 (not (string-match "\\`text/rtf\\'" type)))
1045 "inline"
1046 "attachment")))
1047 (let ((disposition (completing-read
1048 (format "Disposition (default %s): " default)
1049 '(("attachment") ("inline") (""))
1050 nil t nil nil default)))
1051 (if (not (equal disposition ""))
1052 disposition
1053 default)))
1054
1055 (defun mml-quote-region (beg end)
1056 "Quote the MML tags in the region."
1057 (interactive "r")
1058 (save-excursion
1059 (save-restriction
1060 ;; Temporarily narrow the region to defend from changes
1061 ;; invalidating END.
1062 (narrow-to-region beg end)
1063 (goto-char (point-min))
1064 ;; Quote parts.
1065 (while (re-search-forward
1066 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
1067 ;; Insert ! after the #.
1068 (goto-char (+ (match-beginning 0) 2))
1069 (insert "!")))))
1070
1071 (defun mml-insert-tag (name &rest plist)
1072 "Insert an MML tag described by NAME and PLIST."
1073 (when (symbolp name)
1074 (setq name (symbol-name name)))
1075 (insert "<#" name)
1076 (while plist
1077 (let ((key (pop plist))
1078 (value (pop plist)))
1079 (when value
1080 ;; Quote VALUE if it contains suspicious characters.
1081 (when (string-match "[\"'\\~/*;() \t\n]" value)
1082 (setq value (with-output-to-string
1083 (let (print-escape-nonascii)
1084 (prin1 value)))))
1085 (insert (format " %s=%s" key value)))))
1086 (insert ">\n"))
1087
1088 (defun mml-insert-empty-tag (name &rest plist)
1089 "Insert an empty MML tag described by NAME and PLIST."
1090 (when (symbolp name)
1091 (setq name (symbol-name name)))
1092 (apply #'mml-insert-tag name plist)
1093 (insert "<#/" name ">\n"))
1094
1095 ;;; Attachment functions.
1096
1097 (defcustom mml-dnd-protocol-alist
1098 '(("^file:///" . mml-dnd-attach-file)
1099 ("^file://" . dnd-open-file)
1100 ("^file:" . mml-dnd-attach-file))
1101 "The functions to call when a drop in `mml-mode' is made.
1102 See `dnd-protocol-alist' for more information. When nil, behave
1103 as in other buffers."
1104 :type '(choice (repeat (cons (regexp) (function)))
1105 (const :tag "Behave as in other buffers" nil))
1106 :version "22.1" ;; Gnus 5.10.9
1107 :group 'message)
1108
1109 (defcustom mml-dnd-attach-options nil
1110 "Which options should be queried when attaching a file via drag and drop.
1111
1112 If it is a list, valid members are `type', `description' and
1113 `disposition'. `disposition' implies `type'. If it is nil,
1114 don't ask for options. If it is t, ask the user whether or not
1115 to specify options."
1116 :type '(choice
1117 (const :tag "Non" nil)
1118 (const :tag "Query" t)
1119 (list :value (type description disposition)
1120 (set :inline t
1121 (const type)
1122 (const description)
1123 (const disposition))))
1124 :version "22.1" ;; Gnus 5.10.9
1125 :group 'message)
1126
1127 (defun mml-attach-file (file &optional type description disposition)
1128 "Attach a file to the outgoing MIME message.
1129 The file is not inserted or encoded until you send the message with
1130 `\\[message-send-and-exit]' or `\\[message-send]'.
1131
1132 FILE is the name of the file to attach. TYPE is its
1133 content-type, a string of the form \"type/subtype\". DESCRIPTION
1134 is a one-line description of the attachment. The DISPOSITION
1135 specifies how the attachment is intended to be displayed. It can
1136 be either \"inline\" (displayed automatically within the message
1137 body) or \"attachment\" (separate from the body)."
1138 (interactive
1139 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1140 (type (mml-minibuffer-read-type file))
1141 (description (mml-minibuffer-read-description))
1142 (disposition (mml-minibuffer-read-disposition type)))
1143 (list file type description disposition)))
1144 (save-excursion
1145 (unless (message-in-body-p) (goto-char (point-max)))
1146 (mml-insert-empty-tag 'part
1147 'type type
1148 'filename file
1149 'disposition (or disposition "attachment")
1150 'description description)))
1151
1152 (defun mml-dnd-attach-file (uri action)
1153 "Attach a drag and drop file.
1154
1155 Ask for type, description or disposition according to
1156 `mml-dnd-attach-options'."
1157 (let ((file (dnd-get-local-file-name uri t)))
1158 (when (and file (file-regular-p file))
1159 (let ((mml-dnd-attach-options mml-dnd-attach-options)
1160 type description disposition)
1161 (setq mml-dnd-attach-options
1162 (when (and (eq mml-dnd-attach-options t)
1163 (not
1164 (y-or-n-p
1165 "Use default type, disposition and description? ")))
1166 '(type description disposition)))
1167 (when (or (memq 'type mml-dnd-attach-options)
1168 (memq 'disposition mml-dnd-attach-options))
1169 (setq type (mml-minibuffer-read-type file)))
1170 (when (memq 'description mml-dnd-attach-options)
1171 (setq description (mml-minibuffer-read-description)))
1172 (when (memq 'disposition mml-dnd-attach-options)
1173 (setq disposition (mml-minibuffer-read-disposition type)))
1174 (mml-attach-file file type description disposition)))))
1175
1176 (defun mml-attach-buffer (buffer &optional type description)
1177 "Attach a buffer to the outgoing MIME message.
1178 See `mml-attach-file' for details of operation."
1179 (interactive
1180 (let* ((buffer (read-buffer "Attach buffer: "))
1181 (type (mml-minibuffer-read-type buffer "text/plain"))
1182 (description (mml-minibuffer-read-description)))
1183 (list buffer type description)))
1184 (save-excursion
1185 (unless (message-in-body-p) (goto-char (point-max)))
1186 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1187 'disposition "attachment"
1188 'description description)))
1189
1190 (defun mml-attach-external (file &optional type description)
1191 "Attach an external file into the buffer.
1192 FILE is an ange-ftp/efs specification of the part location.
1193 TYPE is the MIME type to use."
1194 (interactive
1195 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1196 (type (mml-minibuffer-read-type file))
1197 (description (mml-minibuffer-read-description)))
1198 (list file type description)))
1199 (save-excursion
1200 (unless (message-in-body-p) (goto-char (point-max)))
1201 (mml-insert-empty-tag 'external 'type type 'name file
1202 'disposition "attachment" 'description description)))
1203
1204 (defun mml-insert-multipart (&optional type)
1205 (interactive (list (completing-read "Multipart type (default mixed): "
1206 '(("mixed") ("alternative") ("digest") ("parallel")
1207 ("signed") ("encrypted"))
1208 nil nil "mixed")))
1209 (or type
1210 (setq type "mixed"))
1211 (mml-insert-empty-tag "multipart" 'type type)
1212 (forward-line -1))
1213
1214 (defun mml-insert-part (&optional type)
1215 (interactive
1216 (list (mml-minibuffer-read-type "")))
1217 (mml-insert-tag 'part 'type type 'disposition "inline")
1218 (forward-line -1))
1219
1220 (defun mml-preview-insert-mail-followup-to ()
1221 "Insert a Mail-Followup-To header before previewing an article.
1222 Should be adopted if code in `message-send-mail' is changed."
1223 (when (and (message-mail-p)
1224 (message-subscribed-p)
1225 (not (mail-fetch-field "mail-followup-to"))
1226 (message-make-mail-followup-to))
1227 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1228 (insert (message-make-mail-followup-to))))
1229
1230 (defun mml-preview (&optional raw)
1231 "Display current buffer with Gnus, in a new buffer.
1232 If RAW, display a raw encoded MIME message."
1233 (interactive "P")
1234 (save-excursion
1235 (let* ((buf (current-buffer))
1236 (message-options message-options)
1237 (message-this-is-mail (message-mail-p))
1238 (message-this-is-news (message-news-p))
1239 (message-posting-charset (or (gnus-setup-posting-charset
1240 (save-restriction
1241 (message-narrow-to-headers-or-head)
1242 (message-fetch-field "Newsgroups")))
1243 message-posting-charset)))
1244 (message-options-set-recipient)
1245 (pop-to-buffer (generate-new-buffer
1246 (concat (if raw "*Raw MIME preview of "
1247 "*MIME preview of ") (buffer-name))))
1248 (when (boundp 'gnus-buffers)
1249 (push (current-buffer) gnus-buffers))
1250 (erase-buffer)
1251 (insert-buffer-substring buf)
1252 (mml-preview-insert-mail-followup-to)
1253 (let ((message-deletable-headers (if (message-news-p)
1254 nil
1255 message-deletable-headers)))
1256 (message-generate-headers
1257 (copy-sequence (if (message-news-p)
1258 message-required-news-headers
1259 message-required-mail-headers))))
1260 (if (re-search-forward
1261 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1262 (replace-match "\n"))
1263 (let ((mail-header-separator ""));; mail-header-separator is removed.
1264 (mml-to-mime))
1265 (if raw
1266 (when (fboundp 'set-buffer-multibyte)
1267 (let ((s (buffer-string)))
1268 ;; Insert the content into unibyte buffer.
1269 (erase-buffer)
1270 (mm-disable-multibyte)
1271 (insert s)))
1272 (let ((gnus-newsgroup-charset (car message-posting-charset))
1273 gnus-article-prepare-hook gnus-original-article-buffer)
1274 (run-hooks 'gnus-article-decode-hook)
1275 (let ((gnus-newsgroup-name "dummy")
1276 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1277 (gnus-make-hashtable 5))))
1278 (gnus-article-prepare-display))))
1279 ;; Disable article-mode-map.
1280 (use-local-map nil)
1281 (gnus-make-local-hook 'kill-buffer-hook)
1282 (add-hook 'kill-buffer-hook
1283 (lambda ()
1284 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1285 (setq buffer-read-only t)
1286 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1287 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1288 (local-set-key "\r"
1289 (lambda ()
1290 (interactive)
1291 (widget-button-press (point))))
1292 (local-set-key gnus-mouse-2
1293 (lambda (event)
1294 (interactive "@e")
1295 (widget-button-press (widget-event-point event) event)))
1296 (goto-char (point-min)))))
1297
1298 (defun mml-validate ()
1299 "Validate the current MML document."
1300 (interactive)
1301 (mml-parse))
1302
1303 (defun mml-tweak-part (cont)
1304 "Tweak a MML part."
1305 (let ((tweak (cdr (assq 'tweak cont)))
1306 func)
1307 (cond
1308 (tweak
1309 (setq func
1310 (or (cdr (assoc tweak mml-tweak-function-alist))
1311 (intern tweak))))
1312 (mml-tweak-type-alist
1313 (let ((alist mml-tweak-type-alist)
1314 (type (or (cdr (assq 'type cont)) "text/plain")))
1315 (while alist
1316 (if (string-match (caar alist) type)
1317 (setq func (cdar alist)
1318 alist nil)
1319 (setq alist (cdr alist)))))))
1320 (if func
1321 (funcall func cont)
1322 cont)
1323 (let ((alist mml-tweak-sexp-alist))
1324 (while alist
1325 (if (eval (caar alist))
1326 (funcall (cdar alist) cont))
1327 (setq alist (cdr alist)))))
1328 cont)
1329
1330 (defun mml-tweak-externalize-attachments (cont)
1331 "Tweak attached files as external parts."
1332 (let (filename-cons)
1333 (when (and (eq (car cont) 'part)
1334 (not (cdr (assq 'buffer cont)))
1335 (and (setq filename-cons (assq 'filename cont))
1336 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1337 (setcar cont 'external)
1338 (setcar filename-cons 'name))))
1339
1340 (provide 'mml)
1341
1342 ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1343 ;;; mml.el ends here