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