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