Document reserved keys
[emacs.git] / lisp / gnus / mml.el
blob3c9476333fa87b16077d233414812bb39a7d2b3c
1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998-2018 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 <https://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 (progn
610 (with-temp-buffer
611 (cond
612 ((cdr (assq 'buffer cont))
613 (insert-buffer-substring (cdr (assq 'buffer cont))))
614 ((and filename
615 (not (equal (cdr (assq 'nofile cont)) "yes")))
616 (let ((coding-system-for-read coding))
617 (mm-insert-file-contents filename)))
618 ((eq 'mml (car cont))
619 (insert (cdr (assq 'contents cont))))
621 (save-restriction
622 (narrow-to-region (point) (point))
623 (insert (cdr (assq 'contents cont)))
624 ;; Remove quotes from quoted tags.
625 (goto-char (point-min))
626 (while (re-search-forward
627 "<#!+/?\\(part\\|multipart\\|external\\|mml\\|secure\\)"
628 nil t)
629 (delete-region (+ (match-beginning 0) 2)
630 (+ (match-beginning 0) 3))))))
631 (cond
632 ((eq (car cont) 'mml)
633 (let ((mml-boundary (mml-compute-boundary cont))
634 ;; It is necessary for the case where this
635 ;; function is called recursively since
636 ;; `m-g-d-t' will be bound to "message/rfc822"
637 ;; when encoding an article to be forwarded.
638 (mml-generate-default-type "text/plain"))
639 (mml-to-mime)
640 ;; Update handle so mml-compute-boundary can
641 ;; detect collisions with the nested parts.
642 (unless mml-inhibit-compute-boundary
643 (setcdr (assoc 'contents cont) (buffer-string))))
644 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
645 ;; ignore 0x1b, it is part of iso-2022-jp
646 (setq encoding (mm-body-7-or-8))))
647 ((string= (car (split-string type "/")) "message")
648 (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
649 ;; ignore 0x1b, it is part of iso-2022-jp
650 (setq encoding (mm-body-7-or-8))))
652 ;; Only perform format=flowed filling on text/plain
653 ;; parts where there either isn't a format parameter
654 ;; in the mml tag or it says "flowed" and there
655 ;; actually are hard newlines in the text.
656 (let (use-hard-newlines)
657 (when (and mml-enable-flowed
658 (string= type "text/plain")
659 (not (string= (cdr (assq 'sign cont)) "pgp"))
660 (or (null (assq 'format cont))
661 (string= (cdr (assq 'format cont))
662 "flowed"))
663 (setq use-hard-newlines
664 (text-property-any
665 (point-min) (point-max) 'hard 't)))
666 (fill-flowed-encode)
667 ;; Indicate that `mml-insert-mime-headers' should
668 ;; insert a "; format=flowed" string unless the
669 ;; user has already specified it.
670 (setq flowed (null (assq 'format cont)))))
671 ;; Prefer `utf-8' for text/calendar parts.
672 (if (or charset
673 (not (string= type "text/calendar")))
674 (setq charset (mm-encode-body charset))
675 (let ((mm-coding-system-priorities
676 (cons 'utf-8 mm-coding-system-priorities)))
677 (setq charset (mm-encode-body))))
678 (setq encoding (mm-body-encoding
679 charset (cdr (assq 'encoding cont))))))
680 (setq coded (buffer-string)))
681 (mml-insert-mime-headers cont type charset encoding flowed)
682 (insert "\n")
683 (insert coded))
684 (with-temp-buffer
685 (set-buffer-multibyte nil)
686 (cond
687 ((cdr (assq 'buffer cont))
688 ;; multibyte string that inserted to a unibyte buffer
689 ;; will be converted to the unibyte version safely.
690 (insert (with-current-buffer (cdr (assq 'buffer cont))
691 (buffer-string))))
692 ((and filename
693 (not (equal (cdr (assq 'nofile cont)) "yes")))
694 (let ((coding-system-for-read mm-binary-coding-system))
695 (mm-insert-file-contents filename nil nil nil nil t))
696 (unless charset
697 (setq charset (mm-coding-system-to-mime-charset
698 (mm-find-buffer-file-coding-system
699 filename)))))
701 (let ((contents (cdr (assq 'contents cont))))
702 (if (if (featurep 'xemacs)
703 (string-match "[^\000-\377]" contents)
704 (multibyte-string-p contents))
705 (progn
706 (set-buffer-multibyte t)
707 (insert contents)
708 (unless raw
709 (setq charset (mm-encode-body charset))))
710 (insert contents)))))
711 (if (setq encoding (cdr (assq 'encoding cont)))
712 (setq encoding (intern (downcase encoding))))
713 (setq encoding (mm-encode-buffer type encoding))
714 (setq coded (decode-coding-string (buffer-string) 'us-ascii)))
715 (mml-insert-mime-headers cont type charset encoding nil)
716 (insert "\n" coded))))
717 ((eq (car cont) 'external)
718 (insert "Content-Type: message/external-body")
719 (let ((parameters (mml-parameter-string
720 cont '(expiration size permission)))
721 (name (cdr (assq 'name cont)))
722 (url (cdr (assq 'url cont))))
723 (when name
724 (setq name (mml-parse-file-name name))
725 (if (stringp name)
726 (mml-insert-parameter
727 (mail-header-encode-parameter "name" name)
728 "access-type=local-file")
729 (mml-insert-parameter
730 (mail-header-encode-parameter
731 "name" (file-name-nondirectory (nth 2 name)))
732 (mail-header-encode-parameter "site" (nth 1 name))
733 (mail-header-encode-parameter
734 "directory" (file-name-directory (nth 2 name))))
735 (mml-insert-parameter
736 (concat "access-type="
737 (if (member (nth 0 name) '("ftp@" "anonymous@"))
738 "anon-ftp"
739 "ftp")))))
740 (when url
741 (mml-insert-parameter
742 (mail-header-encode-parameter "url" url)
743 "access-type=url"))
744 (when parameters
745 (mml-insert-parameter-string
746 cont '(expiration size permission)))
747 (insert "\n\n")
748 (insert "Content-Type: "
749 (or (cdr (assq 'type cont))
750 (if name
751 (or (mm-default-file-encoding name)
752 "application/octet-stream")
753 "text/plain"))
754 "\n")
755 (insert "Content-ID: " (message-make-message-id) "\n")
756 (insert "Content-Transfer-Encoding: "
757 (or (cdr (assq 'encoding cont)) "binary"))
758 (insert "\n\n")
759 (insert (or (cdr (assq 'contents cont))))
760 (insert "\n")))
761 ((eq (car cont) 'multipart)
762 (let* ((type (or (cdr (assq 'type cont)) "mixed"))
763 (mml-generate-default-type (if (equal type "digest")
764 "message/rfc822"
765 "text/plain"))
766 (handler (assoc type mml-generate-multipart-alist)))
767 (if handler
768 (funcall (cdr handler) cont)
769 ;; No specific handler. Use default one.
770 (let ((mml-boundary (mml-compute-boundary cont)))
771 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
772 type mml-boundary)
773 (if (cdr (assq 'start cont))
774 (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
775 "\n"))
776 (let ((cont cont) part)
777 (while (setq part (pop cont))
778 ;; Skip `multipart' and attributes.
779 (when (and (consp part) (consp (cdr part)))
780 (insert "\n--" mml-boundary "\n")
781 (mml-generate-mime-1 part)
782 (goto-char (point-max)))))
783 (insert "\n--" mml-boundary "--\n")))))
785 (error "Invalid element: %S" cont)))
786 ;; handle sign & encrypt tags in a semi-smart way.
787 (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
788 (encrypt-item (assoc (cdr (assq 'encrypt cont))
789 mml-encrypt-alist))
790 sender recipients)
791 (when (or sign-item encrypt-item)
792 (when (setq sender (cdr (assq 'sender cont)))
793 (message-options-set 'mml-sender sender)
794 (message-options-set 'message-sender sender))
795 (if (setq recipients (cdr (assq 'recipients cont)))
796 (message-options-set 'message-recipients recipients))
797 (let ((style (mml-signencrypt-style
798 (first (or sign-item encrypt-item)))))
799 ;; check if: we're both signing & encrypting, both methods
800 ;; are the same (why would they be different?!), and that
801 ;; the signencrypt style allows for combined operation.
802 (if (and sign-item encrypt-item (equal (first sign-item)
803 (first encrypt-item))
804 (equal style 'combined))
805 (funcall (nth 1 encrypt-item) cont t)
806 ;; otherwise, revert to the old behavior.
807 (when sign-item
808 (funcall (nth 1 sign-item) cont))
809 (when encrypt-item
810 (funcall (nth 1 encrypt-item) cont)))))))))
812 (defun mml-compute-boundary (cont)
813 "Return a unique boundary that does not exist in CONT."
814 (let ((mml-boundary (funcall mml-boundary-function
815 (incf mml-multipart-number))))
816 (unless mml-inhibit-compute-boundary
817 ;; This function tries again and again until it has found
818 ;; a unique boundary.
819 (while (not (catch 'not-unique
820 (mml-compute-boundary-1 cont)))))
821 mml-boundary))
823 (defun mml-compute-boundary-1 (cont)
824 (cond
825 ((member (car cont) '(part mml))
826 (mm-with-multibyte-buffer
827 (let ((mml-inhibit-compute-boundary t)
828 (mml-multipart-number 0)
829 mml-sign-alist mml-encrypt-alist)
830 (mml-generate-mime-1 cont))
831 (goto-char (point-min))
832 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
833 nil t)
834 (setq mml-boundary (funcall mml-boundary-function
835 (incf mml-multipart-number)))
836 (throw 'not-unique nil))))
837 ((eq (car cont) 'multipart)
838 (mapc 'mml-compute-boundary-1 (cddr cont))))
841 (defun mml-make-boundary (number)
842 (concat (make-string (% number 60) ?=)
843 (if (> number 17)
844 (format "%x" number)
846 mml-base-boundary))
848 (defun mml-content-disposition (type &optional filename)
849 "Return a default disposition name suitable to TYPE or FILENAME."
850 (let ((defs mml-content-disposition-alist)
851 disposition def types)
852 (while (and (not disposition) defs)
853 (setq def (pop defs))
854 (cond ((stringp (car def))
855 (when (and filename
856 (string-match (car def) filename))
857 (setq disposition (cdr def))))
858 ((consp (cdr def))
859 (when (string= (car (setq types (split-string type "/")))
860 (car def))
861 (setq type (cadr types)
862 types (cdr def))
863 (while (and (not disposition) types)
864 (setq def (pop types))
865 (when (or (eq (car def) t) (string= type (car def)))
866 (setq disposition (cdr def))))))
868 (when (or (eq (car def) t) (string= type (car def)))
869 (setq disposition (cdr def))))))
870 (or disposition "attachment")))
872 (defun mml-insert-mime-headers (cont type charset encoding flowed)
873 (let (parameters id disposition description)
874 (setq parameters
875 (mml-parameter-string
876 cont mml-content-type-parameters))
877 (when (or charset
878 parameters
879 flowed
880 (not (equal type mml-generate-default-type))
881 mml-insert-mime-headers-always)
882 (when (consp charset)
883 (error
884 "Can't encode a part with several charsets"))
885 (insert "Content-Type: " type)
886 (when charset
887 (mml-insert-parameter
888 (mail-header-encode-parameter "charset" (symbol-name charset))))
889 (when flowed
890 (mml-insert-parameter "format=flowed"))
891 (when parameters
892 (mml-insert-parameter-string
893 cont mml-content-type-parameters))
894 (insert "\n"))
895 (when (setq id (cdr (assq 'id cont)))
896 (insert "Content-ID: " id "\n"))
897 (setq parameters
898 (mml-parameter-string
899 cont mml-content-disposition-parameters))
900 (when (or (setq disposition (cdr (assq 'disposition cont)))
901 parameters)
902 (insert "Content-Disposition: "
903 (or disposition
904 (mml-content-disposition type (cdr (assq 'filename cont)))))
905 (when parameters
906 (mml-insert-parameter-string
907 cont mml-content-disposition-parameters))
908 (insert "\n"))
909 (unless (eq encoding '7bit)
910 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
911 (when (setq description (cdr (assq 'description cont)))
912 (insert "Content-Description: ")
913 (setq description (prog1
914 (point)
915 (insert description "\n")))
916 (mail-encode-encoded-word-region description (point)))))
918 (defun mml-parameter-string (cont types)
919 (let ((string "")
920 value type)
921 (while (setq type (pop types))
922 (when (setq value (cdr (assq type cont)))
923 ;; Strip directory component from the filename parameter.
924 (when (eq type 'filename)
925 (setq value (file-name-nondirectory value)))
926 (setq string (concat string "; "
927 (mail-header-encode-parameter
928 (symbol-name type) value)))))
929 (when (not (zerop (length string)))
930 string)))
932 (defun mml-insert-parameter-string (cont types)
933 (let (value type)
934 (while (setq type (pop types))
935 (when (setq value (cdr (assq type cont)))
936 ;; Strip directory component from the filename parameter.
937 (when (eq type 'filename)
938 (setq value (file-name-nondirectory value)))
939 (mml-insert-parameter
940 (mail-header-encode-parameter
941 (symbol-name type) value))))))
943 (defvar ange-ftp-name-format)
944 (defvar efs-path-regexp)
946 (defun mml-parse-file-name (path)
947 (if (if (boundp 'efs-path-regexp)
948 (string-match efs-path-regexp path)
949 (if (boundp 'ange-ftp-name-format)
950 (string-match (car ange-ftp-name-format) path)))
951 (list (match-string 1 path) (match-string 2 path)
952 (substring path (1+ (match-end 2))))
953 path))
955 (defun mml-insert-buffer (buffer)
956 "Insert BUFFER at point and quote any MML markup."
957 (save-restriction
958 (narrow-to-region (point) (point))
959 (insert-buffer-substring buffer)
960 (mml-quote-region (point-min) (point-max))
961 (goto-char (point-max))))
964 ;;; Transforming MIME to MML
967 ;; message-narrow-to-head autoloads message.
968 (declare-function message-remove-header "message"
969 (header &optional is-regexp first reverse))
971 (defun mime-to-mml (&optional handles)
972 "Translate the current buffer (which should be a message) into MML.
973 If HANDLES is non-nil, use it instead reparsing the buffer."
974 ;; First decode the head.
975 (save-restriction
976 (message-narrow-to-head)
977 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
978 (mail-decode-encoded-word-region (point-min) (point-max))))
979 (unless handles
980 (setq handles (mm-dissect-buffer t)))
981 (goto-char (point-min))
982 (search-forward "\n\n" nil t)
983 (delete-region (point) (point-max))
984 (if (stringp (car handles))
985 (mml-insert-mime handles)
986 (mml-insert-mime handles t))
987 (mm-destroy-parts handles)
988 (save-restriction
989 (message-narrow-to-head)
990 ;; Remove them, they are confusing.
991 (message-remove-header "Content-Type")
992 (message-remove-header "MIME-Version")
993 (message-remove-header "Content-Disposition")
994 (message-remove-header "Content-Transfer-Encoding")))
996 (autoload 'message-encode-message-body "message")
997 (declare-function message-narrow-to-headers-or-head "message" ())
999 ;;;###autoload
1000 (defun mml-to-mime ()
1001 "Translate the current buffer from MML to MIME."
1002 ;; `message-encode-message-body' will insert an encoded Content-Description
1003 ;; header in the message header if the body contains a single part
1004 ;; that is specified by a user with a MML tag containing a description
1005 ;; token. So, we encode the message header first to prevent the encoded
1006 ;; Content-Description header from being encoded again.
1007 (save-restriction
1008 (message-narrow-to-headers-or-head)
1009 ;; Skip past any From_ headers.
1010 (while (looking-at "From ")
1011 (forward-line 1))
1012 (let ((mail-parse-charset message-default-charset))
1013 (mail-encode-encoded-word-buffer)))
1014 (message-encode-message-body))
1016 (defun mml-insert-mime (handle &optional no-markup)
1017 (let (textp buffer mmlp)
1018 ;; Determine type and stuff.
1019 (unless (stringp (car handle))
1020 (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
1021 (with-current-buffer (setq buffer (mml-generate-new-buffer " *mml*"))
1022 (if (eq (mail-content-type-get (mm-handle-type handle) 'charset)
1023 'gnus-decoded)
1024 ;; A part that mm-uu dissected from a non-MIME message
1025 ;; because of `gnus-article-emulate-mime'.
1026 (progn
1027 (mm-enable-multibyte)
1028 (insert-buffer-substring (mm-handle-buffer handle)))
1029 (mm-insert-part handle 'no-cache)
1030 (if (setq mmlp (equal (mm-handle-media-type handle)
1031 "message/rfc822"))
1032 (mime-to-mml))))))
1033 (if mmlp
1034 (mml-insert-mml-markup handle nil t t)
1035 (unless (and no-markup
1036 (equal (mm-handle-media-type handle) "text/plain"))
1037 (mml-insert-mml-markup handle buffer textp)))
1038 (cond
1039 (mmlp
1040 (insert-buffer-substring buffer)
1041 (goto-char (point-max))
1042 (insert "<#/mml>\n"))
1043 ((stringp (car handle))
1044 (mapc 'mml-insert-mime (cdr handle))
1045 (insert "<#/multipart>\n"))
1046 (textp
1047 (let ((charset (mail-content-type-get
1048 (mm-handle-type handle) 'charset))
1049 (start (point)))
1050 (if (eq charset 'gnus-decoded)
1051 (mm-insert-part handle)
1052 (insert (mm-decode-string (mm-get-part handle) charset)))
1053 (mml-quote-region start (point)))
1054 (goto-char (point-max)))
1056 (insert "<#/part>\n")))))
1058 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
1059 "Take a MIME handle and insert an MML tag."
1060 (if (stringp (car handle))
1061 (progn
1062 (insert "<#multipart type=" (mm-handle-media-subtype handle))
1063 (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
1064 (when start
1065 (insert " start=\"" start "\"")))
1066 (insert ">\n"))
1067 (if mmlp
1068 (insert "<#mml type=" (mm-handle-media-type handle))
1069 (insert "<#part type=" (mm-handle-media-type handle)))
1070 (dolist (elem (append (cdr (mm-handle-type handle))
1071 (cdr (mm-handle-disposition handle))))
1072 (unless (symbolp (cdr elem))
1073 (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
1074 (when (mm-handle-id handle)
1075 (insert " id=\"" (mm-handle-id handle) "\""))
1076 (when (mm-handle-disposition handle)
1077 (insert " disposition=" (car (mm-handle-disposition handle))))
1078 (when buffer
1079 (insert " buffer=\"" (buffer-name buffer) "\""))
1080 (when nofile
1081 (insert " nofile=yes"))
1082 (when (mm-handle-description handle)
1083 (insert " description=\"" (mm-handle-description handle) "\""))
1084 (insert ">\n")))
1086 (defun mml-insert-parameter (&rest parameters)
1087 "Insert PARAMETERS in a nice way."
1088 (let (start end)
1089 (dolist (param parameters)
1090 (insert ";")
1091 (setq start (point))
1092 (insert " " param)
1093 (setq end (point))
1094 (goto-char start)
1095 (end-of-line)
1096 (if (> (current-column) 76)
1097 (progn
1098 (goto-char start)
1099 (insert "\n")
1100 (goto-char (1+ end)))
1101 (goto-char end)))))
1104 ;;; Mode for inserting and editing MML forms
1107 (defvar mml-mode-map
1108 (let ((sign (make-sparse-keymap))
1109 (encrypt (make-sparse-keymap))
1110 (signpart (make-sparse-keymap))
1111 (encryptpart (make-sparse-keymap))
1112 (map (make-sparse-keymap))
1113 (main (make-sparse-keymap)))
1114 (define-key map "\C-s" 'mml-secure-message-sign)
1115 (define-key map "\C-c" 'mml-secure-message-encrypt)
1116 (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
1117 (define-key map "\C-p\C-s" 'mml-secure-sign)
1118 (define-key map "\C-p\C-c" 'mml-secure-encrypt)
1119 (define-key sign "p" 'mml-secure-message-sign-pgpmime)
1120 (define-key sign "o" 'mml-secure-message-sign-pgp)
1121 (define-key sign "s" 'mml-secure-message-sign-smime)
1122 (define-key signpart "p" 'mml-secure-sign-pgpmime)
1123 (define-key signpart "o" 'mml-secure-sign-pgp)
1124 (define-key signpart "s" 'mml-secure-sign-smime)
1125 (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
1126 (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
1127 (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
1128 (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
1129 (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
1130 (define-key encryptpart "s" 'mml-secure-encrypt-smime)
1131 (define-key map "\C-n" 'mml-unsecure-message)
1132 (define-key map "f" 'mml-attach-file)
1133 (define-key map "b" 'mml-attach-buffer)
1134 (define-key map "e" 'mml-attach-external)
1135 (define-key map "q" 'mml-quote-region)
1136 (define-key map "m" 'mml-insert-multipart)
1137 (define-key map "p" 'mml-insert-part)
1138 (define-key map "v" 'mml-validate)
1139 (define-key map "P" 'mml-preview)
1140 (define-key map "s" sign)
1141 (define-key map "S" signpart)
1142 (define-key map "c" encrypt)
1143 (define-key map "C" encryptpart)
1144 ;;(define-key map "n" 'mml-narrow-to-part)
1145 ;; `M-m' conflicts with `back-to-indentation'.
1146 ;; (define-key main "\M-m" map)
1147 (define-key main "\C-c\C-m" map)
1148 main))
1150 (easy-menu-define
1151 mml-menu mml-mode-map ""
1152 `("Attachments"
1153 ["Attach File..." mml-attach-file :help "Attach a file at point"]
1154 ["Attach Buffer..." mml-attach-buffer
1155 :help "Attach a buffer to the outgoing message"]
1156 ["Attach External..." mml-attach-external
1157 :help "Attach reference to an external file"]
1158 ;; FIXME: Is it possible to do this without using
1159 ;; `gnus-gcc-externalize-attachments'?
1160 ["Externalize Attachments"
1161 (lambda ()
1162 (interactive)
1163 (setq gnus-gcc-externalize-attachments
1164 (not gnus-gcc-externalize-attachments))
1165 (message "gnus-gcc-externalize-attachments is `%s'."
1166 gnus-gcc-externalize-attachments))
1167 :visible (and (boundp 'gnus-gcc-externalize-attachments)
1168 (memq gnus-gcc-externalize-attachments
1169 '(all t nil)))
1170 :style toggle
1171 :selected gnus-gcc-externalize-attachments
1172 :help "Save attachments as external parts in Gcc copies"]
1173 "----"
1175 ("Change Security Method"
1176 ["PGP/MIME"
1177 (lambda () (interactive) (setq mml-secure-method "pgpmime"))
1178 :help "Set Security Method to PGP/MIME"
1179 :style radio
1180 :selected (equal mml-secure-method "pgpmime") ]
1181 ["S/MIME"
1182 (lambda () (interactive) (setq mml-secure-method "smime"))
1183 :help "Set Security Method to S/MIME"
1184 :style radio
1185 :selected (equal mml-secure-method "smime") ]
1186 ["Inline PGP"
1187 (lambda () (interactive) (setq mml-secure-method "pgp"))
1188 :help "Set Security Method to inline PGP"
1189 :style radio
1190 :selected (equal mml-secure-method "pgp") ] )
1192 ["Sign Message" mml-secure-message-sign t]
1193 ["Encrypt Message" mml-secure-message-encrypt t]
1194 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
1195 ["Encrypt/Sign off" mml-unsecure-message
1196 :help "Don't Encrypt/Sign Message"]
1197 ;; Do we have separate encrypt and encrypt/sign commands for parts?
1198 ["Sign Part" mml-secure-sign t]
1199 ["Encrypt Part" mml-secure-encrypt t]
1200 "----"
1201 ;; Maybe we could remove these, because people who write MML most probably
1202 ;; don't use the menu:
1203 ["Insert Part..." mml-insert-part
1204 :active (message-in-body-p)]
1205 ["Insert Multipart..." mml-insert-multipart
1206 :active (message-in-body-p)]
1208 ;;["Narrow" mml-narrow-to-part t]
1209 ["Quote MML in region" mml-quote-region
1210 :active (message-mark-active-p)
1211 :help "Quote MML tags in region"]
1212 ["Validate MML" mml-validate t]
1213 ["Preview" mml-preview t]
1214 "----"
1215 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
1216 :help "Display the Emacs MIME manual"]
1217 ["PGG manual" (lambda () (interactive) (message-info mml2015-use))
1218 :visible (and (boundp 'mml2015-use) (equal mml2015-use 'pgg))
1219 :help "Display the PGG manual"]
1220 ["EasyPG manual" (lambda () (interactive) (require 'mml2015) (message-info mml2015-use))
1221 :visible (and (boundp 'mml2015-use) (equal mml2015-use 'epg))
1222 :help "Display the EasyPG manual"]))
1224 (define-minor-mode mml-mode
1225 "Minor mode for editing MML.
1226 MML is the MIME Meta Language, a minor mode for composing MIME articles.
1227 See Info node `(emacs-mime)Composing'.
1229 \\{mml-mode-map}"
1230 :lighter " MML" :keymap mml-mode-map
1231 (when mml-mode
1232 (easy-menu-add mml-menu mml-mode-map)
1233 (when (boundp 'dnd-protocol-alist)
1234 (set (make-local-variable 'dnd-protocol-alist)
1235 (append mml-dnd-protocol-alist dnd-protocol-alist)))))
1238 ;;; Helper functions for reading MIME stuff from the minibuffer and
1239 ;;; inserting stuff to the buffer.
1242 (defcustom mml-default-directory mm-default-directory
1243 "The default directory where mml will find files.
1244 If not set, `default-directory' will be used."
1245 :type '(choice directory (const :tag "Default" nil))
1246 :version "23.1" ;; No Gnus
1247 :group 'message)
1249 (defun mml-minibuffer-read-file (prompt)
1250 (let* ((completion-ignored-extensions nil)
1251 (buffer-file-name nil)
1252 (file (read-file-name prompt
1253 (or mml-default-directory default-directory)
1254 nil t)))
1255 ;; Prevent some common errors. This is inspired by similar code in
1256 ;; VM.
1257 (when (file-directory-p file)
1258 (error "%s is a directory, cannot attach" file))
1259 (unless (file-exists-p file)
1260 (error "No such file: %s" file))
1261 (unless (file-readable-p file)
1262 (error "Permission denied: %s" file))
1263 file))
1265 (declare-function mailcap-parse-mimetypes "mailcap" (&optional path force))
1266 (declare-function mailcap-mime-types "mailcap" ())
1268 (defun mml-minibuffer-read-type (name &optional default)
1269 (require 'mailcap)
1270 (mailcap-parse-mimetypes)
1271 (let* ((default (or default
1272 (mm-default-file-encoding name)
1273 ;; Perhaps here we should check what the file
1274 ;; looks like, and offer text/plain if it looks
1275 ;; like text/plain.
1276 "application/octet-stream"))
1277 (string (gnus-completing-read
1278 "Content type"
1279 (mailcap-mime-types)
1280 nil nil nil default)))
1281 (if (not (equal string ""))
1282 string
1283 default)))
1285 (defun mml-minibuffer-read-description (&optional default)
1286 (let ((description (read-string "One line description: " default)))
1287 (when (string-match "\\`[ \t]*\\'" description)
1288 (setq description nil))
1289 description))
1291 (defun mml-minibuffer-read-disposition (type &optional default filename)
1292 (unless default
1293 (setq default (mml-content-disposition type filename)))
1294 (let ((disposition (gnus-completing-read
1295 "Disposition"
1296 '("attachment" "inline")
1297 t nil nil default)))
1298 (if (not (equal disposition ""))
1299 disposition
1300 default)))
1302 (defun mml-quote-region (beg end)
1303 "Quote the MML tags in the region."
1304 (interactive "r")
1305 (save-excursion
1306 (save-restriction
1307 ;; Temporarily narrow the region to defend from changes
1308 ;; invalidating END.
1309 (narrow-to-region beg end)
1310 (goto-char (point-min))
1311 ;; Quote parts.
1312 (while (re-search-forward
1313 "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)" nil t)
1314 ;; Insert ! after the #.
1315 (goto-char (+ (match-beginning 0) 2))
1316 (insert "!")))))
1318 (defun mml-insert-tag (name &rest plist)
1319 "Insert an MML tag described by NAME and PLIST."
1320 (when (symbolp name)
1321 (setq name (symbol-name name)))
1322 (insert "<#" name)
1323 (while plist
1324 (let ((key (pop plist))
1325 (value (pop plist)))
1326 (when value
1327 ;; Quote VALUE if it contains suspicious characters.
1328 (when (string-match "[\"'\\~/*;() \t\n]" value)
1329 (setq value (with-output-to-string
1330 (let (print-escape-nonascii)
1331 (prin1 value)))))
1332 (insert (format " %s=%s" key value)))))
1333 (insert ">\n"))
1335 (defun mml-insert-empty-tag (name &rest plist)
1336 "Insert an empty MML tag described by NAME and PLIST."
1337 (when (symbolp name)
1338 (setq name (symbol-name name)))
1339 (apply #'mml-insert-tag name plist)
1340 (insert "<#/" name ">\n"))
1342 ;;; Attachment functions.
1344 (defcustom mml-dnd-protocol-alist
1345 '(("^file:///" . mml-dnd-attach-file)
1346 ("^file://" . dnd-open-file)
1347 ("^file:" . mml-dnd-attach-file))
1348 "The functions to call when a drop in `mml-mode' is made.
1349 See `dnd-protocol-alist' for more information. When nil, behave
1350 as in other buffers."
1351 :type '(choice (repeat (cons (regexp) (function)))
1352 (const :tag "Behave as in other buffers" nil))
1353 :version "22.1" ;; Gnus 5.10.9
1354 :group 'message)
1356 (defcustom mml-dnd-attach-options nil
1357 "Which options should be queried when attaching a file via drag and drop.
1359 If it is a list, valid members are `type', `description' and
1360 `disposition'. `disposition' implies `type'. If it is nil,
1361 don't ask for options. If it is t, ask the user whether or not
1362 to specify options."
1363 :type '(choice
1364 (const :tag "None" nil)
1365 (const :tag "Query" t)
1366 (list :value (type description disposition)
1367 (set :inline t
1368 (const type)
1369 (const description)
1370 (const disposition))))
1371 :version "22.1" ;; Gnus 5.10.9
1372 :group 'message)
1374 ;;;###autoload
1375 (defun mml-attach-file (file &optional type description disposition)
1376 "Attach a file to the outgoing MIME message.
1377 The file is not inserted or encoded until you send the message with
1378 `\\[message-send-and-exit]' or `\\[message-send]' in Message mode,
1379 or `\\[mail-send-and-exit]' or `\\[mail-send]' in Mail mode.
1381 FILE is the name of the file to attach. TYPE is its
1382 content-type, a string of the form \"type/subtype\". DESCRIPTION
1383 is a one-line description of the attachment. The DISPOSITION
1384 specifies how the attachment is intended to be displayed. It can
1385 be either \"inline\" (displayed automatically within the message
1386 body) or \"attachment\" (separate from the body).
1388 If given a prefix interactively, no prompting will be done for
1389 the TYPE, DESCRIPTION or DISPOSITION values. Instead defaults
1390 will be computed and used."
1391 (interactive
1392 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1393 (type (if current-prefix-arg
1394 (or (mm-default-file-encoding file)
1395 "application/octet-stream")
1396 (mml-minibuffer-read-type file)))
1397 (description (if current-prefix-arg
1399 (mml-minibuffer-read-description)))
1400 (disposition (if current-prefix-arg
1401 (mml-content-disposition type file)
1402 (mml-minibuffer-read-disposition type nil file))))
1403 (list file type description disposition)))
1404 ;; If in the message header, attach at the end and leave point unchanged.
1405 (let ((head (unless (message-in-body-p) (point))))
1406 (if head (goto-char (point-max)))
1407 (mml-insert-empty-tag 'part
1408 'type type
1409 ;; icicles redefines read-file-name and returns a
1410 ;; string w/ text properties :-/
1411 'filename (substring-no-properties file)
1412 'disposition (or disposition "attachment")
1413 'description description)
1414 ;; When using Mail mode, make sure it does the mime encoding
1415 ;; when you send the message.
1416 (or (eq mail-user-agent 'message-user-agent)
1417 (setq mail-encode-mml t))
1418 (when head
1419 (unless (pos-visible-in-window-p)
1420 (message "The file \"%s\" has been attached at the end of the message"
1421 (file-name-nondirectory file)))
1422 (goto-char head))))
1424 (defun mml-dnd-attach-file (uri action)
1425 "Attach a drag and drop file.
1427 Ask for type, description or disposition according to
1428 `mml-dnd-attach-options'."
1429 (let ((file (dnd-get-local-file-name uri t)))
1430 (when (and file (file-regular-p file))
1431 (let ((mml-dnd-attach-options mml-dnd-attach-options)
1432 type description disposition)
1433 (setq mml-dnd-attach-options
1434 (when (and (eq mml-dnd-attach-options t)
1435 (not
1436 (y-or-n-p
1437 "Use default type, disposition and description? ")))
1438 '(type description disposition)))
1439 (when (or (memq 'type mml-dnd-attach-options)
1440 (memq 'disposition mml-dnd-attach-options))
1441 (setq type (mml-minibuffer-read-type file)))
1442 (when (memq 'description mml-dnd-attach-options)
1443 (setq description (mml-minibuffer-read-description)))
1444 (when (memq 'disposition mml-dnd-attach-options)
1445 (setq disposition (mml-minibuffer-read-disposition type nil file)))
1446 (mml-attach-file file type description disposition)))))
1448 (defun mml-attach-buffer (buffer &optional type description disposition)
1449 "Attach a buffer to the outgoing MIME message.
1450 BUFFER is the name of the buffer to attach. See
1451 `mml-attach-file' for details of operation."
1452 (interactive
1453 (let* ((buffer (read-buffer "Attach buffer: "))
1454 (type (mml-minibuffer-read-type buffer "text/plain"))
1455 (description (mml-minibuffer-read-description))
1456 (disposition (mml-minibuffer-read-disposition type nil)))
1457 (list buffer type description disposition)))
1458 ;; If in the message header, attach at the end and leave point unchanged.
1459 (let ((head (unless (message-in-body-p) (point))))
1460 (if head (goto-char (point-max)))
1461 (mml-insert-empty-tag 'part 'type type 'buffer buffer
1462 'disposition disposition
1463 'description description)
1464 ;; When using Mail mode, make sure it does the mime encoding
1465 ;; when you send the message.
1466 (or (eq mail-user-agent 'message-user-agent)
1467 (setq mail-encode-mml t))
1468 (when head
1469 (unless (pos-visible-in-window-p)
1470 (message
1471 "The buffer \"%s\" has been attached at the end of the message"
1472 buffer))
1473 (goto-char head))))
1475 (defun mml-attach-external (file &optional type description)
1476 "Attach an external file into the buffer.
1477 FILE is an ange-ftp/efs specification of the part location.
1478 TYPE is the MIME type to use."
1479 (interactive
1480 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1481 (type (mml-minibuffer-read-type file))
1482 (description (mml-minibuffer-read-description)))
1483 (list file type description)))
1484 ;; If in the message header, attach at the end and leave point unchanged.
1485 (let ((head (unless (message-in-body-p) (point))))
1486 (if head (goto-char (point-max)))
1487 (mml-insert-empty-tag 'external 'type type 'name file
1488 'disposition "attachment" 'description description)
1489 ;; When using Mail mode, make sure it does the mime encoding
1490 ;; when you send the message.
1491 (or (eq mail-user-agent 'message-user-agent)
1492 (setq mail-encode-mml t))
1493 (when head
1494 (unless (pos-visible-in-window-p)
1495 (message "The file \"%s\" has been attached at the end of the message"
1496 (file-name-nondirectory file)))
1497 (goto-char head))))
1499 (defun mml-insert-multipart (&optional type)
1500 (interactive (if (message-in-body-p)
1501 (list (gnus-completing-read "Multipart type"
1502 '("mixed" "alternative"
1503 "digest" "parallel"
1504 "signed" "encrypted")
1505 nil "mixed"))
1506 (error "Use this command in the message body")))
1507 (or type
1508 (setq type "mixed"))
1509 (mml-insert-empty-tag "multipart" 'type type)
1510 ;; When using Mail mode, make sure it does the mime encoding
1511 ;; when you send the message.
1512 (or (eq mail-user-agent 'message-user-agent)
1513 (setq mail-encode-mml t))
1514 (forward-line -1))
1516 (defun mml-insert-part (&optional type)
1517 (interactive (if (message-in-body-p)
1518 (list (mml-minibuffer-read-type ""))
1519 (error "Use this command in the message body")))
1520 ;; When using Mail mode, make sure it does the mime encoding
1521 ;; when you send the message.
1522 (or (eq mail-user-agent 'message-user-agent)
1523 (setq mail-encode-mml t))
1524 (mml-insert-tag 'part 'type type 'disposition "inline")
1525 (save-excursion
1526 (mml-insert-tag '/part)))
1528 (declare-function message-subscribed-p "message" ())
1529 (declare-function message-make-mail-followup-to "message"
1530 (&optional only-show-subscribed))
1531 (declare-function message-position-on-field "message" (header &rest afters))
1533 (defun mml-preview-insert-mail-followup-to ()
1534 "Insert a Mail-Followup-To header before previewing an article.
1535 Should be adopted if code in `message-send-mail' is changed."
1536 (when (and (message-mail-p)
1537 (message-subscribed-p)
1538 (not (mail-fetch-field "mail-followup-to"))
1539 (message-make-mail-followup-to))
1540 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1541 (insert (message-make-mail-followup-to))))
1543 (defvar mml-preview-buffer nil)
1545 (autoload 'gnus-make-hashtable "gnus-util")
1546 (autoload 'widget-button-press "wid-edit" nil t)
1547 (declare-function widget-event-point "wid-edit" (event))
1548 ;; If gnus-buffer-configuration is bound this is loaded.
1549 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
1550 ;; Called after message-mail-p, which autoloads message.
1551 (declare-function message-news-p "message" ())
1552 (declare-function message-options-set-recipient "message" ())
1553 (declare-function message-generate-headers "message" (headers))
1554 (declare-function message-sort-headers "message" ())
1556 (defun mml-preview (&optional raw)
1557 "Display current buffer with Gnus, in a new buffer.
1558 If RAW, display a raw encoded MIME message.
1560 The window layout for the preview buffer is controlled by the variables
1561 `special-display-buffer-names', `special-display-regexps', or
1562 `gnus-buffer-configuration' (the first match made will be used),
1563 or the `pop-to-buffer' function."
1564 (interactive "P")
1565 (setq mml-preview-buffer (generate-new-buffer
1566 (concat (if raw "*Raw MIME preview of "
1567 "*MIME preview of ") (buffer-name))))
1568 (require 'gnus-msg) ; for gnus-setup-posting-charset
1569 (save-excursion
1570 (let* ((buf (current-buffer))
1571 (article-editing (eq major-mode 'gnus-article-edit-mode))
1572 (message-options message-options)
1573 (message-this-is-mail (message-mail-p))
1574 (message-this-is-news (message-news-p))
1575 (message-posting-charset (or (gnus-setup-posting-charset
1576 (save-restriction
1577 (message-narrow-to-headers-or-head)
1578 (message-fetch-field "Newsgroups")))
1579 message-posting-charset)))
1580 (message-options-set-recipient)
1581 (when (boundp 'gnus-buffers)
1582 (push mml-preview-buffer gnus-buffers))
1583 (save-restriction
1584 (widen)
1585 (set-buffer mml-preview-buffer)
1586 (erase-buffer)
1587 (insert-buffer-substring buf))
1588 (mml-preview-insert-mail-followup-to)
1589 (let ((message-deletable-headers (if (message-news-p)
1591 message-deletable-headers))
1592 (mail-header-separator (if article-editing
1594 mail-header-separator)))
1595 (message-generate-headers
1596 (copy-sequence (if (message-news-p)
1597 message-required-news-headers
1598 message-required-mail-headers)))
1599 (unless article-editing
1600 (if (re-search-forward
1601 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1602 (replace-match "\n"))
1603 (setq mail-header-separator ""))
1604 (message-sort-headers)
1605 (mml-to-mime))
1606 (if raw
1607 (let ((s (buffer-string)))
1608 ;; Insert the content into unibyte buffer.
1609 (erase-buffer)
1610 (mm-disable-multibyte)
1611 (insert s))
1612 (let ((gnus-newsgroup-charset (car message-posting-charset))
1613 gnus-article-prepare-hook gnus-original-article-buffer
1614 gnus-displaying-mime)
1615 (run-hooks 'gnus-article-decode-hook)
1616 (let ((gnus-newsgroup-name "dummy")
1617 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1618 (gnus-make-hashtable 5))))
1619 (gnus-article-prepare-display))))
1620 ;; Disable article-mode-map.
1621 (use-local-map nil)
1622 (add-hook 'kill-buffer-hook
1623 (lambda ()
1624 (mm-destroy-parts gnus-article-mime-handles)) nil t)
1625 (setq buffer-read-only t)
1626 (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
1627 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1628 (local-set-key "\r"
1629 (lambda ()
1630 (interactive)
1631 (widget-button-press (point))))
1632 (local-set-key [mouse-2]
1633 (lambda (event)
1634 (interactive "@e")
1635 (widget-button-press (widget-event-point event) event)))
1636 ;; FIXME: Buffer is in article mode, but most tool bar commands won't
1637 ;; work. Maybe only keep the following icons: search, print, quit
1638 (goto-char (point-min))))
1639 (if (and (not (special-display-p (buffer-name mml-preview-buffer)))
1640 (boundp 'gnus-buffer-configuration)
1641 (assq 'mml-preview gnus-buffer-configuration))
1642 (let ((gnus-message-buffer (current-buffer)))
1643 (gnus-configure-windows 'mml-preview))
1644 (pop-to-buffer mml-preview-buffer)))
1646 (defun mml-validate ()
1647 "Validate the current MML document."
1648 (interactive)
1649 (mml-parse))
1651 (defun mml-tweak-part (cont)
1652 "Tweak a MML part."
1653 (let ((tweak (cdr (assq 'tweak cont)))
1654 func)
1655 (cond
1656 (tweak
1657 (setq func
1658 (or (cdr (assoc tweak mml-tweak-function-alist))
1659 (intern tweak))))
1660 (mml-tweak-type-alist
1661 (let ((alist mml-tweak-type-alist)
1662 (type (or (cdr (assq 'type cont)) "text/plain")))
1663 (while alist
1664 (if (string-match (caar alist) type)
1665 (setq func (cdar alist)
1666 alist nil)
1667 (setq alist (cdr alist)))))))
1668 (if func
1669 (funcall func cont)
1670 cont)
1671 (let ((alist mml-tweak-sexp-alist))
1672 (while alist
1673 (if (eval (caar alist))
1674 (funcall (cdar alist) cont))
1675 (setq alist (cdr alist)))))
1676 cont)
1678 (defun mml-tweak-externalize-attachments (cont)
1679 "Tweak attached files as external parts."
1680 (let (filename-cons)
1681 (when (and (eq (car cont) 'part)
1682 (not (cdr (assq 'buffer cont)))
1683 (and (setq filename-cons (assq 'filename cont))
1684 (not (equal (cdr (assq 'nofile cont)) "yes"))))
1685 (setcar cont 'external)
1686 (setcar filename-cons 'name))))
1688 (provide 'mml)
1690 ;;; mml.el ends here