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