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