Synch with Gnus trunk.
[emacs.git] / lisp / gnus / mml.el
blobf55b2ccae922a8891cc78799c9a98ad7577dc6b1
1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 ;; For Emacs < 22.2.
27 (eval-and-compile
28 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (require 'mm-util)
31 (require 'mm-bodies)
32 (require 'mm-encode)
33 (require 'mm-decode)
34 (require 'mml-sec)
35 (eval-when-compile (require 'cl))
37 (autoload 'message-make-message-id "message")
38 (declare-function gnus-setup-posting-charset "gnus-msg" (group))
39 (autoload 'gnus-make-local-hook "gnus-util")
40 (autoload 'message-fetch-field "message")
41 (autoload 'message-mark-active-p "message")
42 (autoload 'message-info "message")
43 (autoload 'fill-flowed-encode "flow-fill")
44 (autoload 'message-posting-charset "message")
45 (autoload 'dnd-get-local-file-name "dnd")
47 (autoload 'message-options-set "message")
48 (autoload 'message-narrow-to-head "message")
49 (autoload 'message-in-body-p "message")
50 (autoload 'message-mail-p "message")
52 (defvar gnus-article-mime-handles)
53 (defvar gnus-mouse-2)
54 (defvar gnus-newsrc-hashtb)
55 (defvar message-default-charset)
56 (defvar message-deletable-headers)
57 (defvar message-options)
58 (defvar message-posting-charset)
59 (defvar message-required-mail-headers)
60 (defvar message-required-news-headers)
61 (defvar dnd-protocol-alist)
62 (defvar mml-dnd-protocol-alist)
64 (defcustom mml-content-type-parameters
65 '(name access-type expiration size permission format)
66 "*A list of acceptable parameters in MML tag.
67 These parameters are generated in Content-Type header if exists."
68 :version "22.1"
69 :type '(repeat (symbol :tag "Parameter"))
70 :group 'message)
72 (defcustom mml-content-disposition-parameters
73 '(filename creation-date modification-date read-date)
74 "*A list of acceptable parameters in MML tag.
75 These parameters are generated in Content-Disposition header if exists."
76 :version "22.1"
77 :type '(repeat (symbol :tag "Parameter"))
78 :group 'message)
80 (defcustom mml-content-disposition-alist
81 '((text (rtf . "attachment") (t . "inline"))
82 (t . "attachment"))
83 "Alist of MIME types or regexps matching file names and default dispositions.
84 Each element should be one of the following three forms:
86 (REGEXP . DISPOSITION)
87 (SUPERTYPE (SUBTYPE . DISPOSITION) (SUBTYPE . DISPOSITION)...)
88 (TYPE . DISPOSITION)
90 Where REGEXP is a string which matches the file name (if any) of an
91 attachment, SUPERTYPE, SUBTYPE and TYPE should be symbols which are a
92 MIME supertype (e.g., text), a MIME subtype (e.g., plain) and a MIME
93 type (e.g., text/plain) respectively, and DISPOSITION should be either
94 the string \"attachment\" or the string \"inline\". The value t for
95 SUPERTYPE, SUBTYPE or TYPE matches any of those types. The first
96 match found will be used."
97 :version "23.1" ;; No Gnus
98 :type (let ((dispositions '(radio :format "DISPOSITION: %v"
99 :value "attachment"
100 (const :format "%v " "attachment")
101 (const :format "%v\n" "inline"))))
102 `(repeat
103 :offset 0
104 (choice :format "%[Value Menu%]%v"
105 (cons :tag "(REGEXP . DISPOSITION)" :extra-offset 4
106 (regexp :tag "REGEXP" :value ".*")
107 ,dispositions)
108 (cons :tag "(SUPERTYPE (SUBTYPE . DISPOSITION)...)"
109 :indent 0
110 (symbol :tag " SUPERTYPE" :value text)
111 (repeat :format "%v%i\n" :offset 0 :extra-offset 4
112 (cons :format "%v" :extra-offset 5
113 (symbol :tag "SUBTYPE" :value t)
114 ,dispositions)))
115 (cons :tag "(TYPE . DISPOSITION)" :extra-offset 4
116 (symbol :tag "TYPE" :value t)
117 ,dispositions))))
118 :group 'message)
120 (defcustom mml-insert-mime-headers-always nil
121 "If non-nil, always put Content-Type: text/plain at top of empty parts.
122 It is necessary to work against a bug in certain clients."
123 :version "22.1"
124 :type 'boolean
125 :group 'message)
127 (defvar mml-tweak-type-alist nil
128 "A list of (TYPE . FUNCTION) for tweaking MML parts.
129 TYPE is a string containing a regexp to match the MIME type. FUNCTION
130 is a Lisp function which is called with the MML handle to tweak the
131 part. This variable is used only when no TWEAK parameter exists in
132 the MML handle.")
134 (defvar mml-tweak-function-alist nil
135 "A list of (NAME . FUNCTION) for tweaking MML parts.
136 NAME is a string containing the name of the TWEAK parameter in the MML
137 handle. FUNCTION is a Lisp function which is called with the MML
138 handle to tweak the part.")
140 (defvar mml-tweak-sexp-alist
141 '((mml-externalize-attachments . mml-tweak-externalize-attachments))
142 "A list of (SEXP . FUNCTION) for tweaking MML parts.
143 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
144 is called. FUNCTION is a Lisp function which is called with the MML
145 handle to tweak the part.")
147 (defvar mml-externalize-attachments nil
148 "*If non-nil, local-file attachments are generated as external parts.")
150 (defvar mml-generate-multipart-alist nil
151 "*Alist of multipart generation functions.
152 Each entry has the form (NAME . FUNCTION), where
153 NAME is a string containing the name of the part (without the
154 leading \"/multipart/\"),
155 FUNCTION is a Lisp function which is called to generate the part.
157 The Lisp function has to supply the appropriate MIME headers and the
158 contents of this part.")
160 (defvar mml-syntax-table
161 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
162 (modify-syntax-entry ?\\ "/" table)
163 (modify-syntax-entry ?< "(" table)
164 (modify-syntax-entry ?> ")" table)
165 (modify-syntax-entry ?@ "w" table)
166 (modify-syntax-entry ?/ "w" table)
167 (modify-syntax-entry ?= " " table)
168 (modify-syntax-entry ?* " " table)
169 (modify-syntax-entry ?\; " " table)
170 (modify-syntax-entry ?\' " " table)
171 table))
173 (defvar mml-boundary-function 'mml-make-boundary
174 "A function called to suggest a boundary.
175 The function may be called several times, and should try to make a new
176 suggestion each time. The function is called with one parameter,
177 which is a number that says how many times the function has been
178 called for this message.")
180 (defvar mml-confirmation-set nil
181 "A list of symbols, each of which disables some warning.
182 `unknown-encoding': always send messages contain characters with
183 unknown encoding; `use-ascii': always use ASCII for those characters
184 with unknown encoding; `multipart': always send messages with more than
185 one charsets.")
187 (defvar mml-generate-default-type "text/plain"
188 "Content type by which the Content-Type header can be omitted.
189 The Content-Type header will not be put in the MIME part if the type
190 equals the value and there's no parameter (e.g. charset, format, etc.)
191 and `mml-insert-mime-headers-always' is nil. The value will be bound
192 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
193 part. This is for the internal use, you should never modify the value.")
195 (defvar mml-buffer-list nil)
197 (defun mml-generate-new-buffer (name)
198 (let ((buf (generate-new-buffer name)))
199 (push buf mml-buffer-list)
200 buf))
202 (defun mml-destroy-buffers ()
203 (let (kill-buffer-hook)
204 (mapc 'kill-buffer mml-buffer-list)
205 (setq mml-buffer-list nil)))
207 (defun mml-parse ()
208 "Parse the current buffer as an MML document."
209 (save-excursion
210 (goto-char (point-min))
211 (with-syntax-table mml-syntax-table
212 (mml-parse-1))))
214 (defun mml-parse-1 ()
215 "Parse the current buffer as an MML document."
216 (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
217 (while (and (not (eobp))
218 (not (looking-at "<#/multipart")))
219 (cond
220 ((looking-at "<#secure")
221 ;; The secure part is essentially a meta-meta tag, which
222 ;; expands to either a part tag if there are no other parts in
223 ;; the document or a multipart tag if there are other parts
224 ;; included in the message
225 (let* (secure-mode
226 (taginfo (mml-read-tag))
227 (keyfile (cdr (assq 'keyfile taginfo)))
228 (certfile (cdr (assq 'certfile taginfo)))
229 (recipients (cdr (assq 'recipients taginfo)))
230 (sender (cdr (assq 'sender taginfo)))
231 (location (cdr (assq 'tag-location taginfo)))
232 (mode (cdr (assq 'mode taginfo)))
233 (method (cdr (assq 'method taginfo)))
234 tags)
235 (save-excursion
236 (if (re-search-forward
237 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
238 (setq secure-mode "multipart")
239 (setq secure-mode "part")))
240 (save-excursion
241 (goto-char location)
242 (re-search-forward "<#secure[^\n]*>\n"))
243 (delete-region (match-beginning 0) (match-end 0))
244 (cond ((string= mode "sign")
245 (setq tags (list "sign" method)))
246 ((string= mode "encrypt")
247 (setq tags (list "encrypt" method)))
248 ((string= mode "signencrypt")
249 (setq tags (list "sign" method "encrypt" method))))
250 (eval `(mml-insert-tag ,secure-mode
251 ,@tags
252 ,(if keyfile "keyfile")
253 ,keyfile
254 ,(if certfile "certfile")
255 ,certfile
256 ,(if recipients "recipients")
257 ,recipients
258 ,(if sender "sender")
259 ,sender))
260 ;; restart the parse
261 (goto-char location)))
262 ((looking-at "<#multipart")
263 (push (nconc (mml-read-tag) (mml-parse-1)) struct))
264 ((looking-at "<#external")
265 (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
266 struct))
268 (if (or (looking-at "<#part") (looking-at "<#mml"))
269 (setq tag (mml-read-tag)
270 no-markup-p nil
271 warn nil)
272 (setq tag (list 'part '(type . "text/plain"))
273 no-markup-p t
274 warn t))
275 (setq raw (cdr (assq 'raw tag))
276 point (point)
277 contents (mml-read-part (eq 'mml (car tag)))
278 charsets (cond
279 (raw nil)
280 ((assq 'charset tag)
281 (list
282 (intern (downcase (cdr (assq 'charset tag))))))
284 (mm-find-mime-charset-region point (point)
285 mm-hack-charsets))))
286 (when (and (not raw) (memq nil charsets))
287 (if (or (memq 'unknown-encoding mml-confirmation-set)
288 (message-options-get 'unknown-encoding)
289 (and (y-or-n-p "\
290 Message contains characters with unknown encoding. Really send? ")
291 (message-options-set 'unknown-encoding t)))
292 (if (setq use-ascii
293 (or (memq 'use-ascii mml-confirmation-set)
294 (message-options-get 'use-ascii)
295 (and (y-or-n-p "Use ASCII as charset? ")
296 (message-options-set 'use-ascii t))))
297 (setq charsets (delq nil charsets))
298 (setq warn nil))
299 (error "Edit your message to remove those characters")))
300 (if (or raw
301 (eq 'mml (car tag))
302 (< (length charsets) 2))
303 (if (or (not no-markup-p)
304 (string-match "[^ \t\r\n]" contents))
305 ;; Don't create blank parts.
306 (push (nconc tag (list (cons 'contents contents)))
307 struct))
308 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
309 tag point (point) use-ascii)))
310 (when (and warn
311 (not (memq 'multipart mml-confirmation-set))
312 (not (message-options-get 'multipart))
313 (not (and (y-or-n-p (format "\
314 A message part needs to be split into %d charset parts. Really send? "
315 (length nstruct)))
316 (message-options-set 'multipart t))))
317 (error "Edit your message to use only one charset"))
318 (setq struct (nconc nstruct struct)))))))
319 (unless (eobp)
320 (forward-line 1))
321 (nreverse struct)))
323 (defun mml-parse-singlepart-with-multiple-charsets
324 (orig-tag beg end &optional use-ascii)
325 (save-excursion
326 (save-restriction
327 (narrow-to-region beg end)
328 (goto-char (point-min))
329 (let ((current (or (mm-mime-charset (mm-charset-after))
330 (and use-ascii 'us-ascii)))
331 charset struct space newline paragraph)
332 (while (not (eobp))
333 (setq charset (mm-mime-charset (mm-charset-after)))
334 (cond
335 ;; The charset remains the same.
336 ((eq charset 'us-ascii))
337 ((or (and use-ascii (not charset))
338 (eq charset current))
339 (setq space nil
340 newline nil
341 paragraph nil))
342 ;; The initial charset was ascii.
343 ((eq current 'us-ascii)
344 (setq current charset
345 space nil
346 newline nil
347 paragraph nil))
348 ;; We have a change in charsets.
350 (push (append
351 orig-tag
352 (list (cons 'contents
353 (buffer-substring-no-properties
354 beg (or paragraph newline space (point))))))
355 struct)
356 (setq beg (or paragraph newline space (point))
357 current charset
358 space nil
359 newline nil
360 paragraph nil)))
361 ;; Compute places where it might be nice to break the part.
362 (cond
363 ((memq (following-char) '(? ?\t))
364 (setq space (1+ (point))))
365 ((and (eq (following-char) ?\n)
366 (not (bobp))
367 (eq (char-after (1- (point))) ?\n))
368 (setq paragraph (point)))
369 ((eq (following-char) ?\n)
370 (setq newline (1+ (point)))))
371 (forward-char 1))
372 ;; Do the final part.
373 (unless (= beg (point))
374 (push (append orig-tag
375 (list (cons 'contents
376 (buffer-substring-no-properties
377 beg (point)))))
378 struct))
379 struct))))
381 (defun mml-read-tag ()
382 "Read a tag and return the contents."
383 (let ((orig-point (point))
384 contents name elem val)
385 (forward-char 2)
386 (setq name (buffer-substring-no-properties
387 (point) (progn (forward-sexp 1) (point))))
388 (skip-chars-forward " \t\n")
389 (while (not (looking-at ">[ \t]*\n?"))
390 (setq elem (buffer-substring-no-properties
391 (point) (progn (forward-sexp 1) (point))))
392 (skip-chars-forward "= \t\n")
393 (setq val (buffer-substring-no-properties
394 (point) (progn (forward-sexp 1) (point))))
395 (when (string-match "\\`\"" val)
396 (setq val (read val))) ;; inverse of prin1 in mml-insert-tag
397 (push (cons (intern elem) val) contents)
398 (skip-chars-forward " \t\n"))
399 (goto-char (match-end 0))
400 ;; Don't skip the leading space.
401 ;;(skip-chars-forward " \t\n")
402 ;; Put the tag location into the returned contents
403 (setq contents (append (list (cons 'tag-location orig-point)) contents))
404 (cons (intern name) (nreverse contents))))
406 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
407 (let ((str (buffer-substring-no-properties start end))
408 (bufstart start) tmp)
409 (while (setq tmp (text-property-any start end 'hard 't))
410 (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
411 '(hard t) str)
412 (setq start (1+ tmp)))
413 str))
415 (defun mml-read-part (&optional mml)
416 "Return the buffer up till the next part, multipart or closing part or multipart.
417 If MML is non-nil, return the buffer up till the correspondent mml tag."
418 (let ((beg (point)) (count 1))
419 ;; If the tag ended at the end of the line, we go to the next line.
420 (when (looking-at "[ \t]*\n")
421 (forward-line 1))
422 (if mml
423 (progn
424 (while (and (> count 0) (not (eobp)))
425 (if (re-search-forward "<#\\(/\\)?mml." nil t)
426 (setq count (+ count (if (match-beginning 1) -1 1)))
427 (goto-char (point-max))))
428 (mml-buffer-substring-no-properties-except-hard-newlines
429 beg (if (> count 0)
430 (point)
431 (match-beginning 0))))
432 (if (re-search-forward
433 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
434 (prog1
435 (mml-buffer-substring-no-properties-except-hard-newlines
436 beg (match-beginning 0))
437 (if (or (not (match-beginning 1))
438 (equal (match-string 2) "multipart"))
439 (goto-char (match-beginning 0))
440 (when (looking-at "[ \t]*\n")
441 (forward-line 1))))
442 (mml-buffer-substring-no-properties-except-hard-newlines
443 beg (goto-char (point-max)))))))
445 (defvar mml-boundary nil)
446 (defvar mml-base-boundary "-=-=")
447 (defvar mml-multipart-number 0)
449 (defun mml-generate-mime ()
450 "Generate a MIME message based on the current MML document."
451 (let ((cont (mml-parse))
452 (mml-multipart-number mml-multipart-number))
453 (if (not cont)
455 (mm-with-multibyte-buffer
456 (if (and (consp (car cont))
457 (= (length cont) 1))
458 (mml-generate-mime-1 (car cont))
459 (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed"))
460 cont)))
461 (buffer-string)))))
463 (defun mml-generate-mime-1 (cont)
464 (let ((mm-use-ultra-safe-encoding
465 (or mm-use-ultra-safe-encoding (assq 'sign cont))))
466 (save-restriction
467 (narrow-to-region (point) (point))
468 (mml-tweak-part cont)
469 (cond
470 ((or (eq (car cont) 'part) (eq (car cont) 'mml))
471 (let* ((raw (cdr (assq 'raw cont)))
472 (filename (cdr (assq 'filename cont)))
473 (type (or (cdr (assq 'type cont))
474 (if filename
475 (or (mm-default-file-encoding filename)
476 "application/octet-stream")
477 "text/plain")))
478 (charset (cdr (assq 'charset cont)))
479 (coding (mm-charset-to-coding-system charset))
480 encoding flowed coded)
481 (cond ((eq coding 'ascii)
482 (setq charset nil
483 coding nil))
484 (charset
485 ;; The value of `charset' might be a bogus alias that
486 ;; `mm-charset-synonym-alist' provides, like `utf8',
487 ;; so we prefer the MIME charset that Emacs knows for
488 ;; the coding system `coding'.
489 (setq charset (or (mm-coding-system-to-mime-charset coding)
490 (intern (downcase charset))))))
491 (if (and (not raw)
492 (member (car (split-string type "/")) '("text" "message")))
493 (progn
494 (with-temp-buffer
495 (cond
496 ((cdr (assq 'buffer cont))
497 (insert-buffer-substring (cdr (assq 'buffer cont))))
498 ((and filename
499 (not (equal (cdr (assq 'nofile cont)) "yes")))
500 (let ((coding-system-for-read coding))
501 (mm-insert-file-contents filename)))
502 ((eq 'mml (car cont))
503 (insert (cdr (assq 'contents cont))))
505 (save-restriction
506 (narrow-to-region (point) (point))
507 (insert (cdr (assq 'contents cont)))
508 ;; Remove quotes from quoted tags.
509 (goto-char (point-min))
510 (while (re-search-forward
511 "<#!+/?\\(part\\|multipart\\|external\\|mml\\)"
512 nil t)
513 (delete-region (+ (match-beginning 0) 2)
514 (+ (match-beginning 0) 3))))))
515 (cond
516 ((eq (car cont) 'mml)
517 (let ((mml-boundary (mml-compute-boundary cont))
518 ;; It is necessary for the case where this
519 ;; function is called recursively since
520 ;; `m-g-d-t' will be bound to "message/rfc822"
521 ;; when encoding an article to be forwarded.
522 (mml-generate-default-type "text/plain"))
523 (mml-to-mime)
524 ;; Update handle so mml-compute-boundary can
525 ;; detect collisions with the nested parts.
526 (setcdr (assoc 'contents cont) (buffer-string)))
527 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
528 ;; ignore 0x1b, it is part of iso-2022-jp
529 (setq encoding (mm-body-7-or-8))))
530 ((string= (car (split-string type "/")) "message")
531 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
532 ;; ignore 0x1b, it is part of iso-2022-jp
533 (setq encoding (mm-body-7-or-8))))
535 ;; Only perform format=flowed filling on text/plain
536 ;; parts where there either isn't a format parameter
537 ;; in the mml tag or it says "flowed" and there
538 ;; actually are hard newlines in the text.
539 (let (use-hard-newlines)
540 (when (and (string= type "text/plain")
541 (not (string= (cdr (assq 'sign cont)) "pgp"))
542 (or (null (assq 'format cont))
543 (string= (cdr (assq 'format cont))
544 "flowed"))
545 (setq use-hard-newlines
546 (text-property-any
547 (point-min) (point-max) 'hard 't)))
548 (fill-flowed-encode)
549 ;; Indicate that `mml-insert-mime-headers' should
550 ;; insert a "; format=flowed" string unless the
551 ;; user has already specified it.
552 (setq flowed (null (assq 'format cont)))))
553 ;; Prefer `utf-8' for text/calendar parts.
554 (if (or charset
555 (not (string= type "text/calendar")))
556 (setq charset (mm-encode-body charset))
557 (let ((mm-coding-system-priorities
558 (cons 'utf-8 mm-coding-system-priorities)))
559 (setq charset (mm-encode-body))))
560 (setq encoding (mm-body-encoding
561 charset (cdr (assq 'encoding cont))))))
562 (setq coded (buffer-string)))
563 (mml-insert-mime-headers cont type charset encoding flowed)
564 (insert "\n")
565 (insert coded))
566 (mm-with-unibyte-buffer
567 (cond
568 ((cdr (assq 'buffer cont))
569 (insert (mm-string-as-unibyte
570 (with-current-buffer (cdr (assq 'buffer cont))
571 (buffer-string)))))
572 ((and filename
573 (not (equal (cdr (assq 'nofile cont)) "yes")))
574 (let ((coding-system-for-read mm-binary-coding-system))
575 (mm-insert-file-contents filename nil nil nil nil t))
576 (unless charset
577 (setq charset (mm-coding-system-to-mime-charset
578 (mm-find-buffer-file-coding-system
579 filename)))))
581 (let ((contents (cdr (assq 'contents cont))))
582 (if (if (featurep 'xemacs)
583 (string-match "[^\000-\377]" contents)
584 (mm-multibyte-string-p contents))
585 (progn
586 (mm-enable-multibyte)
587 (insert contents)
588 (unless raw
589 (setq charset (mm-encode-body charset))))
590 (insert contents)))))
591 (if (setq encoding (cdr (assq 'encoding cont)))
592 (setq encoding (intern (downcase encoding))))
593 (setq encoding (mm-encode-buffer type encoding)
594 coded (mm-string-as-multibyte (buffer-string))))
595 (mml-insert-mime-headers cont type charset encoding nil)
596 (insert "\n" coded))))
597 ((eq (car cont) 'external)
598 (insert "Content-Type: message/external-body")
599 (let ((parameters (mml-parameter-string
600 cont '(expiration size permission)))
601 (name (cdr (assq 'name cont)))
602 (url (cdr (assq 'url cont))))
603 (when name
604 (setq name (mml-parse-file-name name))
605 (if (stringp name)
606 (mml-insert-parameter
607 (mail-header-encode-parameter "name" name)
608 "access-type=local-file")
609 (mml-insert-parameter
610 (mail-header-encode-parameter
611 "name" (file-name-nondirectory (nth 2 name)))
612 (mail-header-encode-parameter "site" (nth 1 name))
613 (mail-header-encode-parameter
614 "directory" (file-name-directory (nth 2 name))))
615 (mml-insert-parameter
616 (concat "access-type="
617 (if (member (nth 0 name) '("ftp@" "anonymous@"))
618 "anon-ftp"
619 "ftp")))))
620 (when url
621 (mml-insert-parameter
622 (mail-header-encode-parameter "url" url)
623 "access-type=url"))
624 (when parameters
625 (mml-insert-parameter-string
626 cont '(expiration size permission)))
627 (insert "\n\n")
628 (insert "Content-Type: "
629 (or (cdr (assq 'type cont))
630 (if name
631 (or (mm-default-file-encoding name)
632 "application/octet-stream")
633 "text/plain"))
634 "\n")
635 (insert "Content-ID: " (message-make-message-id) "\n")
636 (insert "Content-Transfer-Encoding: "
637 (or (cdr (assq 'encoding cont)) "binary"))
638 (insert "\n\n")
639 (insert (or (cdr (assq 'contents cont))))
640 (insert "\n")))
641 ((eq (car cont) 'multipart)
642 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
643 (mml-generate-default-type (if (equal type "digest")
644 "message/rfc822"
645 "text/plain"))
646 (handler (assoc type mml-generate-multipart-alist)))
647 (if handler
648 (funcall (cdr handler) cont)
649 ;; No specific handler. Use default one.
650 (let ((mml-boundary (mml-compute-boundary cont)))
651 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
652 type mml-boundary)
653 (if (cdr (assq 'start cont))
654 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
655 "\n"))
656 (let ((cont cont) part)
657 (while (setq part (pop cont))
658 ;; Skip `multipart' and attributes.
659 (when (and (consp part) (consp (cdr part)))
660 (insert "\n--" mml-boundary "\n")
661 (mml-generate-mime-1 part)
662 (goto-char (point-max)))))
663 (insert "\n--" mml-boundary "--\n")))))
665 (error "Invalid element: %S" cont)))
666 ;; handle sign & encrypt tags in a semi-smart way.
667 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
668 (encrypt-item (assoc (cdr (assq 'encrypt cont))
669 mml-encrypt-alist))
670 sender recipients)
671 (when (or sign-item encrypt-item)
672 (when (setq sender (cdr (assq 'sender cont)))
673 (message-options-set 'mml-sender sender)
674 (message-options-set 'message-sender sender))
675 (if (setq recipients (cdr (assq 'recipients cont)))
676 (message-options-set 'message-recipients recipients))
677 (let ((style (mml-signencrypt-style
678 (first (or sign-item encrypt-item)))))
679 ;; check if: we're both signing & encrypting, both methods
680 ;; are the same (why would they be different?!), and that
681 ;; the signencrypt style allows for combined operation.
682 (if (and sign-item encrypt-item (equal (first sign-item)
683 (first encrypt-item))
684 (equal style 'combined))
685 (funcall (nth 1 encrypt-item) cont t)
686 ;; otherwise, revert to the old behavior.
687 (when sign-item
688 (funcall (nth 1 sign-item) cont))
689 (when encrypt-item
690 (funcall (nth 1 encrypt-item) cont)))))))))
692 (defun mml-compute-boundary (cont)
693 "Return a unique boundary that does not exist in CONT."
694 (let ((mml-boundary (funcall mml-boundary-function
695 (incf mml-multipart-number))))
696 ;; This function tries again and again until it has found
697 ;; a unique boundary.
698 (while (not (catch 'not-unique
699 (mml-compute-boundary-1 cont))))
700 mml-boundary))
702 (defun mml-compute-boundary-1 (cont)
703 (let (filename)
704 (cond
705 ((member (car cont) '(part mml))
706 (with-temp-buffer
707 (cond
708 ((cdr (assq 'buffer cont))
709 (insert-buffer-substring (cdr (assq 'buffer cont))))
710 ((and (setq filename (cdr (assq 'filename cont)))
711 (not (equal (cdr (assq 'nofile cont)) "yes")))
712 (mm-insert-file-contents filename nil nil nil nil t))
714 (insert (cdr (assq 'contents cont)))))
715 (goto-char (point-min))
716 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
717 nil t)
718 (setq mml-boundary (funcall mml-boundary-function
719 (incf mml-multipart-number)))
720 (throw 'not-unique nil))))
721 ((eq (car cont) 'multipart)
722 (mapc 'mml-compute-boundary-1 (cddr cont))))
725 (defun mml-make-boundary (number)
726 (concat (make-string (% number 60) ?=)
727 (if (> number 17)
728 (format "%x" number)
730 mml-base-boundary))
732 (defun mml-content-disposition (type &optional filename)
733 "Return a default disposition name suitable to TYPE or FILENAME."
734 (let ((defs mml-content-disposition-alist)
735 disposition def types)
736 (while (and (not disposition) defs)
737 (setq def (pop defs))
738 (cond ((stringp (car def))
739 (when (and filename
740 (string-match (car def) filename))
741 (setq disposition (cdr def))))
742 ((consp (cdr def))
743 (when (string= (car (setq types (split-string type "/")))
744 (car def))
745 (setq type (cadr types)
746 types (cdr def))
747 (while (and (not disposition) types)
748 (setq def (pop types))
749 (when (or (eq (car def) t) (string= type (car def)))
750 (setq disposition (cdr def))))))
752 (when (or (eq (car def) t) (string= type (car def)))
753 (setq disposition (cdr def))))))
754 (or disposition "attachment")))
756 (defun mml-insert-mime-headers (cont type charset encoding flowed)
757 (let (parameters id disposition description)
758 (setq parameters
759 (mml-parameter-string
760 cont mml-content-type-parameters))
761 (when (or charset
762 parameters
763 flowed
764 (not (equal type mml-generate-default-type))
765 mml-insert-mime-headers-always)
766 (when (consp charset)
767 (error
768 "Can't encode a part with several charsets"))
769 (insert "Content-Type: " type)
770 (when charset
771 (mml-insert-parameter
772 (mail-header-encode-parameter "charset" (symbol-name charset))))
773 (when flowed
774 (mml-insert-parameter "format=flowed"))
775 (when parameters
776 (mml-insert-parameter-string
777 cont mml-content-type-parameters))
778 (insert "\n"))
779 (when (setq id (cdr (assq 'id cont)))
780 (insert "Content-ID: " id "\n"))
781 (setq parameters
782 (mml-parameter-string
783 cont mml-content-disposition-parameters))
784 (when (or (setq disposition (cdr (assq 'disposition cont)))
785 parameters)
786 (insert "Content-Disposition: "
787 (or disposition
788 (mml-content-disposition type (cdr (assq 'filename cont)))))
789 (when parameters
790 (mml-insert-parameter-string
791 cont mml-content-disposition-parameters))
792 (insert "\n"))
793 (unless (eq encoding '7bit)
794 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
795 (when (setq description (cdr (assq 'description cont)))
796 (insert "Content-Description: ")
797 (setq description (prog1
798 (point)
799 (insert description "\n")))
800 (mail-encode-encoded-word-region description (point)))))
802 (defun mml-parameter-string (cont types)
803 (let ((string "")
804 value type)
805 (while (setq type (pop types))
806 (when (setq value (cdr (assq type cont)))
807 ;; Strip directory component from the filename parameter.
808 (when (eq type 'filename)
809 (setq value (file-name-nondirectory value)))
810 (setq string (concat string "; "
811 (mail-header-encode-parameter
812 (symbol-name type) value)))))
813 (when (not (zerop (length string)))
814 string)))
816 (defun mml-insert-parameter-string (cont types)
817 (let (value type)
818 (while (setq type (pop types))
819 (when (setq value (cdr (assq type cont)))
820 ;; Strip directory component from the filename parameter.
821 (when (eq type 'filename)
822 (setq value (file-name-nondirectory value)))
823 (mml-insert-parameter
824 (mail-header-encode-parameter
825 (symbol-name type) value))))))
827 (defvar ange-ftp-name-format)
828 (defvar efs-path-regexp)
830 (defun mml-parse-file-name (path)
831 (if (if (boundp 'efs-path-regexp)
832 (string-match efs-path-regexp path)
833 (if (boundp 'ange-ftp-name-format)
834 (string-match (car ange-ftp-name-format) path)))
835 (list (match-string 1 path) (match-string 2 path)
836 (substring path (1+ (match-end 2))))
837 path))
839 (defun mml-insert-buffer (buffer)
840 "Insert BUFFER at point and quote any MML markup."
841 (save-restriction
842 (narrow-to-region (point) (point))
843 (insert-buffer-substring buffer)
844 (mml-quote-region (point-min) (point-max))
845 (goto-char (point-max))))
848 ;;; Transforming MIME to MML
851 ;; message-narrow-to-head autoloads message.
852 (declare-function message-remove-header "message"
853 (header &optional is-regexp first reverse))
855 (defun mime-to-mml (&optional handles)
856 "Translate the current buffer (which should be a message) into MML.
857 If HANDLES is non-nil, use it instead reparsing the buffer."
858 ;; First decode the head.
859 (save-restriction
860 (message-narrow-to-head)
861 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
862 (mail-decode-encoded-word-region (point-min) (point-max))))
863 (unless handles
864 (setq handles (mm-dissect-buffer t)))
865 (goto-char (point-min))
866 (search-forward "\n\n" nil t)
867 (delete-region (point) (point-max))
868 (if (stringp (car handles))
869 (mml-insert-mime handles)
870 (mml-insert-mime handles t))
871 (mm-destroy-parts handles)
872 (save-restriction
873 (message-narrow-to-head)
874 ;; Remove them, they are confusing.
875 (message-remove-header "Content-Type")
876 (message-remove-header "MIME-Version")
877 (message-remove-header "Content-Disposition")
878 (message-remove-header "Content-Transfer-Encoding")))
880 (autoload 'message-encode-message-body "message")
881 (declare-function message-narrow-to-headers-or-head "message" ())
883 (defun mml-to-mime ()
884 "Translate the current buffer from MML to MIME."
885 ;; `message-encode-message-body' will insert an encoded Content-Description
886 ;; header in the message header if the body contains a single part
887 ;; that is specified by a user with a MML tag containing a description
888 ;; token. So, we encode the message header first to prevent the encoded
889 ;; Content-Description header from being encoded again.
890 (save-restriction
891 (message-narrow-to-headers-or-head)
892 ;; Skip past any From_ headers.
893 (while (looking-at "From ")
894 (forward-line 1))
895 (let ((mail-parse-charset message-default-charset))
896 (mail-encode-encoded-word-buffer)))
897 (message-encode-message-body))
899 (defun mml-insert-mime (handle &optional no-markup)
900 (let (textp buffer mmlp)
901 ;; Determine type and stuff.
902 (unless (stringp (car handle))
903 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
904 (with-current-buffer (setq buffer (mml-generate-new-buffer " *mml*"))
905 (if (eq (mail-content-type-get (mm-handle-type handle) 'charset)
906 'gnus-decoded)
907 ;; A part that mm-uu dissected from a non-MIME message
908 ;; because of `gnus-article-emulate-mime'.
909 (progn
910 (mm-enable-multibyte)
911 (insert-buffer-substring (mm-handle-buffer handle)))
912 (mm-insert-part handle 'no-cache)
913 (if (setq mmlp (equal (mm-handle-media-type handle)
914 "message/rfc822"))
915 (mime-to-mml))))))
916 (if mmlp
917 (mml-insert-mml-markup handle nil t t)
918 (unless (and no-markup
919 (equal (mm-handle-media-type handle) "text/plain"))
920 (mml-insert-mml-markup handle buffer textp)))
921 (cond
922 (mmlp
923 (insert-buffer-substring buffer)
924 (goto-char (point-max))
925 (insert "<#/mml>\n"))
926 ((stringp (car handle))
927 (mapc 'mml-insert-mime (cdr handle))
928 (insert "<#/multipart>\n"))
929 (textp
930 (let ((charset (mail-content-type-get
931 (mm-handle-type handle) 'charset))
932 (start (point)))
933 (if (eq charset 'gnus-decoded)
934 (mm-insert-part handle)
935 (insert (mm-decode-string (mm-get-part handle) charset)))
936 (mml-quote-region start (point)))
937 (goto-char (point-max)))
939 (insert "<#/part>\n")))))
941 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
942 "Take a MIME handle and insert an MML tag."
943 (if (stringp (car handle))
944 (progn
945 (insert "<#multipart type=" (mm-handle-media-subtype handle))
946 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
947 (when start
948 (insert " start=\"" start "\"")))
949 (insert ">\n"))
950 (if mmlp
951 (insert "<#mml type=" (mm-handle-media-type handle))
952 (insert "<#part type=" (mm-handle-media-type handle)))
953 (dolist (elem (append (cdr (mm-handle-type handle))
954 (cdr (mm-handle-disposition handle))))
955 (unless (symbolp (cdr elem))
956 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
957 (when (mm-handle-id handle)
958 (insert " id=\"" (mm-handle-id handle) "\""))
959 (when (mm-handle-disposition handle)
960 (insert " disposition=" (car (mm-handle-disposition handle))))
961 (when buffer
962 (insert " buffer=\"" (buffer-name buffer) "\""))
963 (when nofile
964 (insert " nofile=yes"))
965 (when (mm-handle-description handle)
966 (insert " description=\"" (mm-handle-description handle) "\""))
967 (insert ">\n")))
969 (defun mml-insert-parameter (&rest parameters)
970 "Insert PARAMETERS in a nice way."
971 (let (start end)
972 (dolist (param parameters)
973 (insert ";")
974 (setq start (point))
975 (insert " " param)
976 (setq end (point))
977 (goto-char start)
978 (end-of-line)
979 (if (> (current-column) 76)
980 (progn
981 (goto-char start)
982 (insert "\n")
983 (goto-char (1+ end)))
984 (goto-char end)))))
987 ;;; Mode for inserting and editing MML forms
990 (defvar mml-mode-map
991 (let ((sign (make-sparse-keymap))
992 (encrypt (make-sparse-keymap))
993 (signpart (make-sparse-keymap))
994 (encryptpart (make-sparse-keymap))
995 (map (make-sparse-keymap))
996 (main (make-sparse-keymap)))
997 (define-key map "\C-s" 'mml-secure-message-sign)
998 (define-key map "\C-c" 'mml-secure-message-encrypt)
999 (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
1000 (define-key map "\C-p\C-s" 'mml-secure-sign)
1001 (define-key map "\C-p\C-c" 'mml-secure-encrypt)
1002 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
1003 (define-key sign "o" 'mml-secure-message-sign-pgp)
1004 (define-key sign "s" 'mml-secure-message-sign-smime)
1005 (define-key signpart "p" 'mml-secure-sign-pgpmime)
1006 (define-key signpart "o" 'mml-secure-sign-pgp)
1007 (define-key signpart "s" 'mml-secure-sign-smime)
1008 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
1009 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
1010 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
1011 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
1012 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
1013 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
1014 (define-key map "\C-n" 'mml-unsecure-message)
1015 (define-key map "f" 'mml-attach-file)
1016 (define-key map "b" 'mml-attach-buffer)
1017 (define-key map "e" 'mml-attach-external)
1018 (define-key map "q" 'mml-quote-region)
1019 (define-key map "m" 'mml-insert-multipart)
1020 (define-key map "p" 'mml-insert-part)
1021 (define-key map "v" 'mml-validate)
1022 (define-key map "P" 'mml-preview)
1023 (define-key map "s" sign)
1024 (define-key map "S" signpart)
1025 (define-key map "c" encrypt)
1026 (define-key map "C" encryptpart)
1027 ;;(define-key map "n" 'mml-narrow-to-part)
1028 ;; `M-m' conflicts with `back-to-indentation'.
1029 ;; (define-key main "\M-m" map)
1030 (define-key main "\C-c\C-m" map)
1031 main))
1033 (easy-menu-define
1034 mml-menu mml-mode-map ""
1035 `("Attachments"
1036 ["Attach File..." mml-attach-file
1037 ,@(if (featurep 'xemacs) '(t)
1038 '(:help "Attach a file at point"))]
1039 ["Attach Buffer..." mml-attach-buffer
1040 ,@(if (featurep 'xemacs) '(t)
1041 '(:help "Attach a buffer to the outgoing message"))]
1042 ["Attach External..." mml-attach-external
1043 ,@(if (featurep 'xemacs) '(t)
1044 '(:help "Attach reference to an external file"))]
1045 ;; FIXME: Is it possible to do this without using
1046 ;; `gnus-gcc-externalize-attachments'?
1047 ["Externalize Attachments"
1048 (lambda ()
1049 (interactive)
1050 (if (not (and (boundp 'gnus-gcc-externalize-attachments)
1051 (memq gnus-gcc-externalize-attachments
1052 '(all t nil))))
1053 ;; Stupid workaround for XEmacs not honoring :visible.
1054 (message "Can't handle this value of `gnus-gcc-externalize-attachments'")
1055 (setq gnus-gcc-externalize-attachments
1056 (not gnus-gcc-externalize-attachments))
1057 (message "gnus-gcc-externalize-attachments is `%s'."
1058 gnus-gcc-externalize-attachments)))
1059 ;; XEmacs barfs on :visible.
1060 ,@(if (featurep 'xemacs) nil
1061 '(:visible (and (boundp 'gnus-gcc-externalize-attachments)
1062 (memq gnus-gcc-externalize-attachments
1063 '(all t nil)))))
1064 :style toggle
1065 :selected gnus-gcc-externalize-attachments
1066 ,@(if (featurep 'xemacs) nil
1067 '(:help "Save attachments as external parts in Gcc copies"))]
1068 "----"
1070 ("Change Security Method"
1071 ["PGP/MIME"
1072 (lambda () (interactive) (setq mml-secure-method "pgpmime"))
1073 ,@(if (featurep 'xemacs) nil
1074 '(:help "Set Security Method to PGP/MIME"))
1075 :style radio
1076 :selected (equal mml-secure-method "pgpmime") ]
1077 ["S/MIME"
1078 (lambda () (interactive) (setq mml-secure-method "smime"))
1079 ,@(if (featurep 'xemacs) nil
1080 '(:help "Set Security Method to S/MIME"))
1081 :style radio
1082 :selected (equal mml-secure-method "smime") ]
1083 ["Inline PGP"
1084 (lambda () (interactive) (setq mml-secure-method "pgp"))
1085 ,@(if (featurep 'xemacs) nil
1086 '(:help "Set Security Method to inline PGP"))
1087 :style radio
1088 :selected (equal mml-secure-method "pgp") ] )
1090 ["Sign Message" mml-secure-message-sign t]
1091 ["Encrypt Message" mml-secure-message-encrypt t]
1092 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
1093 ["Encrypt/Sign off" mml-unsecure-message
1094 ,@(if (featurep 'xemacs) '(t)
1095 '(:help "Don't Encrypt/Sign Message"))]
1096 ;; Do we have separate encrypt and encrypt/sign commands for parts?
1097 ["Sign Part" mml-secure-sign t]
1098 ["Encrypt Part" mml-secure-encrypt t]
1099 "----"
1100 ;; Maybe we could remove these, because people who write MML most probably
1101 ;; don't use the menu:
1102 ["Insert Part..." mml-insert-part
1103 :active (message-in-body-p)]
1104 ["Insert Multipart..." mml-insert-multipart
1105 :active (message-in-body-p)]
1107 ;;["Narrow" mml-narrow-to-part t]
1108 ["Quote MML in region" mml-quote-region
1109 :active (message-mark-active-p)
1110 ,@(if (featurep 'xemacs) nil
1111 '(:help "Quote MML tags in region"))]
1112 ["Validate MML" mml-validate t]
1113 ["Preview" mml-preview t]
1114 "----"
1115 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
1116 ,@(if (featurep 'xemacs) '(t)
1117 '(:help "Display the Emacs MIME manual"))]
1118 ["PGG manual" (lambda () (interactive) (message-info mml2015-use))
1119 ;; XEmacs barfs on :visible.
1120 ,@(if (featurep 'xemacs) nil
1121 '(:visible (and (boundp 'mml2015-use) (equal mml2015-use 'pgg))))
1122 ,@(if (featurep 'xemacs) '(t)
1123 '(:help "Display the PGG manual"))]
1124 ["EasyPG manual" (lambda () (interactive) (require 'mml2015) (message-info mml2015-use))
1125 ;; XEmacs barfs on :visible.
1126 ,@(if (featurep 'xemacs) nil
1127 '(:visible (and (boundp 'mml2015-use) (equal mml2015-use 'epg))))
1128 ,@(if (featurep 'xemacs) '(t)
1129 '(:help "Display the EasyPG manual"))]))
1131 (define-minor-mode mml-mode
1132 "Minor mode for editing MML.
1133 MML is the MIME Meta Language, a minor mode for composing MIME articles.
1134 See Info node `(emacs-mime)Composing'.
1136 \\{mml-mode-map}"
1137 :lighter " MML" :keymap mml-mode-map
1138 (when mml-mode
1139 (easy-menu-add mml-menu mml-mode-map)
1140 (when (boundp 'dnd-protocol-alist)
1141 (set (make-local-variable 'dnd-protocol-alist)
1142 (append mml-dnd-protocol-alist dnd-protocol-alist)))))
1145 ;;; Helper functions for reading MIME stuff from the minibuffer and
1146 ;;; inserting stuff to the buffer.
1149 (defcustom mml-default-directory mm-default-directory
1150 "The default directory where mml will find files.
1151 If not set, `default-directory' will be used."
1152 :type '(choice directory (const :tag "Default" nil))
1153 :version "23.1" ;; No Gnus
1154 :group 'message)
1156 (defun mml-minibuffer-read-file (prompt)
1157 (let* ((completion-ignored-extensions nil)
1158 (file (read-file-name prompt
1159 (or mml-default-directory default-directory)
1160 nil t)))
1161 ;; Prevent some common errors. This is inspired by similar code in
1162 ;; VM.
1163 (when (file-directory-p file)
1164 (error "%s is a directory, cannot attach" file))
1165 (unless (file-exists-p file)
1166 (error "No such file: %s" file))
1167 (unless (file-readable-p file)
1168 (error "Permission denied: %s" file))
1169 file))
1171 (declare-function mailcap-parse-mimetypes "mailcap" (&optional path force))
1172 (declare-function mailcap-mime-types "mailcap" ())
1174 (defun mml-minibuffer-read-type (name &optional default)
1175 (require 'mailcap)
1176 (mailcap-parse-mimetypes)
1177 (let* ((default (or default
1178 (mm-default-file-encoding name)
1179 ;; Perhaps here we should check what the file
1180 ;; looks like, and offer text/plain if it looks
1181 ;; like text/plain.
1182 "application/octet-stream"))
1183 (string (completing-read
1184 (format "Content type (default %s): " default)
1185 (mapcar 'list (mailcap-mime-types)))))
1186 (if (not (equal string ""))
1187 string
1188 default)))
1190 (defun mml-minibuffer-read-description ()
1191 (let ((description (read-string "One line description: ")))
1192 (when (string-match "\\`[ \t]*\\'" description)
1193 (setq description nil))
1194 description))
1196 (defun mml-minibuffer-read-disposition (type &optional default filename)
1197 (unless default
1198 (setq default (mml-content-disposition type filename)))
1199 (let ((disposition (completing-read
1200 (format "Disposition (default %s): " default)
1201 '(("attachment") ("inline") (""))
1202 nil t nil nil default)))
1203 (if (not (equal disposition ""))
1204 disposition
1205 default)))
1207 (defun mml-quote-region (beg end)
1208 "Quote the MML tags in the region."
1209 (interactive "r")
1210 (save-excursion
1211 (save-restriction
1212 ;; Temporarily narrow the region to defend from changes
1213 ;; invalidating END.
1214 (narrow-to-region beg end)
1215 (goto-char (point-min))
1216 ;; Quote parts.
1217 (while (re-search-forward
1218 "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
1219 ;; Insert ! after the #.
1220 (goto-char (+ (match-beginning 0) 2))
1221 (insert "!")))))
1223 (defun mml-insert-tag (name &rest plist)
1224 "Insert an MML tag described by NAME and PLIST."
1225 (when (symbolp name)
1226 (setq name (symbol-name name)))
1227 (insert "<#" name)
1228 (while plist
1229 (let ((key (pop plist))
1230 (value (pop plist)))
1231 (when value
1232 ;; Quote VALUE if it contains suspicious characters.
1233 (when (string-match "[\"'\\~/*;() \t\n]" value)
1234 (setq value (with-output-to-string
1235 (let (print-escape-nonascii)
1236 (prin1 value)))))
1237 (insert (format " %s=%s" key value)))))
1238 (insert ">\n"))
1240 (defun mml-insert-empty-tag (name &rest plist)
1241 "Insert an empty MML tag described by NAME and PLIST."
1242 (when (symbolp name)
1243 (setq name (symbol-name name)))
1244 (apply #'mml-insert-tag name plist)
1245 (insert "<#/" name ">\n"))
1247 ;;; Attachment functions.
1249 (defcustom mml-dnd-protocol-alist
1250 '(("^file:///" . mml-dnd-attach-file)
1251 ("^file://" . dnd-open-file)
1252 ("^file:" . mml-dnd-attach-file))
1253 "The functions to call when a drop in `mml-mode' is made.
1254 See `dnd-protocol-alist' for more information. When nil, behave
1255 as in other buffers."
1256 :type '(choice (repeat (cons (regexp) (function)))
1257 (const :tag "Behave as in other buffers" nil))
1258 :version "22.1" ;; Gnus 5.10.9
1259 :group 'message)
1261 (defcustom mml-dnd-attach-options nil
1262 "Which options should be queried when attaching a file via drag and drop.
1264 If it is a list, valid members are `type', `description' and
1265 `disposition'. `disposition' implies `type'. If it is nil,
1266 don't ask for options. If it is t, ask the user whether or not
1267 to specify options."
1268 :type '(choice
1269 (const :tag "None" nil)
1270 (const :tag "Query" t)
1271 (list :value (type description disposition)
1272 (set :inline t
1273 (const type)
1274 (const description)
1275 (const disposition))))
1276 :version "22.1" ;; Gnus 5.10.9
1277 :group 'message)
1279 (defun mml-attach-file (file &optional type description disposition)
1280 "Attach a file to the outgoing MIME message.
1281 The file is not inserted or encoded until you send the message with
1282 `\\[message-send-and-exit]' or `\\[message-send]'.
1284 FILE is the name of the file to attach. TYPE is its
1285 content-type, a string of the form \"type/subtype\". DESCRIPTION
1286 is a one-line description of the attachment. The DISPOSITION
1287 specifies how the attachment is intended to be displayed. It can
1288 be either \"inline\" (displayed automatically within the message
1289 body) or \"attachment\" (separate from the body)."
1290 (interactive
1291 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1292 (type (mml-minibuffer-read-type file))
1293 (description (mml-minibuffer-read-description))
1294 (disposition (mml-minibuffer-read-disposition type nil file)))
1295 (list file type description disposition)))
1296 ;; Don't move point if this command is invoked inside the message header.
1297 (let ((head (unless (message-in-body-p)
1298 (prog1
1299 (point)
1300 (goto-char (point-max))))))
1301 (mml-insert-empty-tag 'part
1302 'type type
1303 ;; icicles redefines read-file-name and returns a
1304 ;; string w/ text properties :-/
1305 'filename (mm-substring-no-properties file)
1306 'disposition (or disposition "attachment")
1307 'description description)
1308 (when head
1309 (unless (prog1
1310 (pos-visible-in-window-p)
1311 (goto-char head))
1312 (message "The file \"%s\" has been attached at the end of the message"
1313 (file-name-nondirectory file))))))
1315 (defun mml-dnd-attach-file (uri action)
1316 "Attach a drag and drop file.
1318 Ask for type, description or disposition according to
1319 `mml-dnd-attach-options'."
1320 (let ((file (dnd-get-local-file-name uri t)))
1321 (when (and file (file-regular-p file))
1322 (let ((mml-dnd-attach-options mml-dnd-attach-options)
1323 type description disposition)
1324 (setq mml-dnd-attach-options
1325 (when (and (eq mml-dnd-attach-options t)
1326 (not
1327 (y-or-n-p
1328 "Use default type, disposition and description? ")))
1329 '(type description disposition)))
1330 (when (or (memq 'type mml-dnd-attach-options)
1331 (memq 'disposition mml-dnd-attach-options))
1332 (setq type (mml-minibuffer-read-type file)))
1333 (when (memq 'description mml-dnd-attach-options)
1334 (setq description (mml-minibuffer-read-description)))
1335 (when (memq 'disposition mml-dnd-attach-options)
1336 (setq disposition (mml-minibuffer-read-disposition type nil file)))
1337 (mml-attach-file file type description disposition)))))
1339 (defun mml-attach-buffer (buffer &optional type description disposition)
1340 "Attach a buffer to the outgoing MIME message.
1341 BUFFER is the name of the buffer to attach. See
1342 `mml-attach-file' for details of operation."
1343 (interactive
1344 (let* ((buffer (read-buffer "Attach buffer: "))
1345 (type (mml-minibuffer-read-type buffer "text/plain"))
1346 (description (mml-minibuffer-read-description))
1347 (disposition (mml-minibuffer-read-disposition type nil)))
1348 (list buffer type description disposition)))
1349 ;; Don't move point if this command is invoked inside the message header.
1350 (let ((head (unless (message-in-body-p)
1351 (prog1
1352 (point)
1353 (goto-char (point-max))))))
1354 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1355 'disposition disposition
1356 'description description)
1357 (when head
1358 (unless (prog1
1359 (pos-visible-in-window-p)
1360 (goto-char head))
1361 (message
1362 "The buffer \"%s\" has been attached at the end of the message"
1363 buffer)))))
1365 (defun mml-attach-external (file &optional type description)
1366 "Attach an external file into the buffer.
1367 FILE is an ange-ftp/efs specification of the part location.
1368 TYPE is the MIME type to use."
1369 (interactive
1370 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1371 (type (mml-minibuffer-read-type file))
1372 (description (mml-minibuffer-read-description)))
1373 (list file type description)))
1374 ;; Don't move point if this command is invoked inside the message header.
1375 (let ((head (unless (message-in-body-p)
1376 (prog1
1377 (point)
1378 (goto-char (point-max))))))
1379 (mml-insert-empty-tag 'external 'type type 'name file
1380 'disposition "attachment" 'description description)
1381 (when head
1382 (unless (prog1
1383 (pos-visible-in-window-p)
1384 (goto-char head))
1385 (message "The file \"%s\" has been attached at the end of the message"
1386 (file-name-nondirectory file))))))
1388 (defun mml-insert-multipart (&optional type)
1389 (interactive (if (message-in-body-p)
1390 (list (completing-read "Multipart type (default mixed): "
1391 '(("mixed") ("alternative")
1392 ("digest") ("parallel")
1393 ("signed") ("encrypted"))
1394 nil nil "mixed"))
1395 (error "Use this command in the message body")))
1396 (or type
1397 (setq type "mixed"))
1398 (mml-insert-empty-tag "multipart" 'type type)
1399 (forward-line -1))
1401 (defun mml-insert-part (&optional type)
1402 (interactive (if (message-in-body-p)
1403 (list (mml-minibuffer-read-type ""))
1404 (error "Use this command in the message body")))
1405 (mml-insert-tag 'part 'type type 'disposition "inline"))
1407 (declare-function message-subscribed-p "message" ())
1408 (declare-function message-make-mail-followup-to "message"
1409 (&optional only-show-subscribed))
1410 (declare-function message-position-on-field "message" (header &rest afters))
1412 (defun mml-preview-insert-mail-followup-to ()
1413 "Insert a Mail-Followup-To header before previewing an article.
1414 Should be adopted if code in `message-send-mail' is changed."
1415 (when (and (message-mail-p)
1416 (message-subscribed-p)
1417 (not (mail-fetch-field "mail-followup-to"))
1418 (message-make-mail-followup-to))
1419 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1420 (insert (message-make-mail-followup-to))))
1422 (defvar mml-preview-buffer nil)
1424 (autoload 'gnus-make-hashtable "gnus-util")
1425 (autoload 'widget-button-press "wid-edit" nil t)
1426 (declare-function widget-event-point "wid-edit" (event))
1427 ;; If gnus-buffer-configuration is bound this is loaded.
1428 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
1429 ;; Called after message-mail-p, which autoloads message.
1430 (declare-function message-news-p "message" ())
1431 (declare-function message-options-set-recipient "message" ())
1432 (declare-function message-generate-headers "message" (headers))
1433 (declare-function message-sort-headers "message" ())
1435 (defun mml-preview (&optional raw)
1436 "Display current buffer with Gnus, in a new buffer.
1437 If RAW, display a raw encoded MIME message.
1439 The window layout for the preview buffer is controled by the variables
1440 `special-display-buffer-names', `special-display-regexps', or
1441 `gnus-buffer-configuration' (the first match made will be used),
1442 or the `pop-to-buffer' function."
1443 (interactive "P")
1444 (setq mml-preview-buffer (generate-new-buffer
1445 (concat (if raw "*Raw MIME preview of "
1446 "*MIME preview of ") (buffer-name))))
1447 (require 'gnus-msg) ; for gnus-setup-posting-charset
1448 (save-excursion
1449 (let* ((buf (current-buffer))
1450 (message-options message-options)
1451 (message-this-is-mail (message-mail-p))
1452 (message-this-is-news (message-news-p))
1453 (message-posting-charset (or (gnus-setup-posting-charset
1454 (save-restriction
1455 (message-narrow-to-headers-or-head)
1456 (message-fetch-field "Newsgroups")))
1457 message-posting-charset)))
1458 (message-options-set-recipient)
1459 (when (boundp 'gnus-buffers)
1460 (push mml-preview-buffer gnus-buffers))
1461 (save-restriction
1462 (widen)
1463 (set-buffer mml-preview-buffer)
1464 (erase-buffer)
1465 (insert-buffer-substring buf))
1466 (mml-preview-insert-mail-followup-to)
1467 (let ((message-deletable-headers (if (message-news-p)
1469 message-deletable-headers)))
1470 (message-generate-headers
1471 (copy-sequence (if (message-news-p)
1472 message-required-news-headers
1473 message-required-mail-headers))))
1474 (if (re-search-forward
1475 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1476 (replace-match "\n"))
1477 (let ((mail-header-separator ""));; mail-header-separator is removed.
1478 (message-sort-headers)
1479 (mml-to-mime))
1480 (if raw
1481 (when (fboundp 'set-buffer-multibyte)
1482 (let ((s (buffer-string)))
1483 ;; Insert the content into unibyte buffer.
1484 (erase-buffer)
1485 (mm-disable-multibyte)
1486 (insert s)))
1487 (let ((gnus-newsgroup-charset (car message-posting-charset))
1488 gnus-article-prepare-hook gnus-original-article-buffer)
1489 (run-hooks 'gnus-article-decode-hook)
1490 (let ((gnus-newsgroup-name "dummy")
1491 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1492 (gnus-make-hashtable 5))))
1493 (gnus-article-prepare-display))))
1494 ;; Disable article-mode-map.
1495 (use-local-map nil)
1496 (gnus-make-local-hook 'kill-buffer-hook)
1497 (add-hook 'kill-buffer-hook
1498 (lambda ()
1499 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1500 (setq buffer-read-only t)
1501 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1502 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1503 (local-set-key "\r"
1504 (lambda ()
1505 (interactive)
1506 (widget-button-press (point))))
1507 (local-set-key gnus-mouse-2
1508 (lambda (event)
1509 (interactive "@e")
1510 (widget-button-press (widget-event-point event) event)))
1511 ;; FIXME: Buffer is in article mode, but most tool bar commands won't
1512 ;; work. Maybe only keep the following icons: search, print, quit
1513 (goto-char (point-min))))
1514 (if (and (not (mm-special-display-p (buffer-name mml-preview-buffer)))
1515 (boundp 'gnus-buffer-configuration)
1516 (assq 'mml-preview gnus-buffer-configuration))
1517 (let ((gnus-message-buffer (current-buffer)))
1518 (gnus-configure-windows 'mml-preview))
1519 (pop-to-buffer mml-preview-buffer)))
1521 (defun mml-validate ()
1522 "Validate the current MML document."
1523 (interactive)
1524 (mml-parse))
1526 (defun mml-tweak-part (cont)
1527 "Tweak a MML part."
1528 (let ((tweak (cdr (assq 'tweak cont)))
1529 func)
1530 (cond
1531 (tweak
1532 (setq func
1533 (or (cdr (assoc tweak mml-tweak-function-alist))
1534 (intern tweak))))
1535 (mml-tweak-type-alist
1536 (let ((alist mml-tweak-type-alist)
1537 (type (or (cdr (assq 'type cont)) "text/plain")))
1538 (while alist
1539 (if (string-match (caar alist) type)
1540 (setq func (cdar alist)
1541 alist nil)
1542 (setq alist (cdr alist)))))))
1543 (if func
1544 (funcall func cont)
1545 cont)
1546 (let ((alist mml-tweak-sexp-alist))
1547 (while alist
1548 (if (eval (caar alist))
1549 (funcall (cdar alist) cont))
1550 (setq alist (cdr alist)))))
1551 cont)
1553 (defun mml-tweak-externalize-attachments (cont)
1554 "Tweak attached files as external parts."
1555 (let (filename-cons)
1556 (when (and (eq (car cont) 'part)
1557 (not (cdr (assq 'buffer cont)))
1558 (and (setq filename-cons (assq 'filename cont))
1559 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1560 (setcar cont 'external)
1561 (setcar filename-cons 'name))))
1563 (provide 'mml)
1565 ;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12
1566 ;;; mml.el ends here