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