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