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