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