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