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