1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; For Emacs <22.2 and XEmacs.
27 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
34 (eval-when-compile (require 'cl
))
36 (when (featurep 'xemacs
)
37 (require 'easy-mmode
))) ; for `define-minor-mode'
39 (autoload 'message-make-message-id
"message")
40 (declare-function gnus-setup-posting-charset
"gnus-msg" (group))
41 (autoload 'gnus-make-local-hook
"gnus-util")
42 (autoload 'gnus-completing-read
"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")
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")
55 (defvar gnus-article-mime-handles
)
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
)
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."
72 :type
'(repeat (symbol :tag
"Parameter"))
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."
80 :type
'(repeat (symbol :tag
"Parameter"))
83 (defcustom mml-content-disposition-alist
84 '((text (rtf .
"attachment") (t .
"inline"))
86 "Alist of MIME types or regexps matching file names and default dispositions.
87 Each element should be one of the following three forms:
89 (REGEXP . DISPOSITION)
90 (SUPERTYPE (SUBTYPE . DISPOSITION) (SUBTYPE . DISPOSITION)...)
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.1" ;; No Gnus
101 :type
(let ((dispositions '(radio :format
"DISPOSITION: %v"
103 (const :format
"%v " "attachment")
104 (const :format
"%v\n" "inline"))))
107 (choice :format
"%[Value Menu%]%v"
108 (cons :tag
"(REGEXP . DISPOSITION)" :extra-offset
4
109 (regexp :tag
"REGEXP" :value
".*")
111 (cons :tag
"(SUPERTYPE (SUBTYPE . DISPOSITION)...)"
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
)
118 (cons :tag
"(TYPE . DISPOSITION)" :extra-offset
4
119 (symbol :tag
"TYPE" :value t
)
123 (defcustom mml-insert-mime-headers-always t
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."
130 (defcustom mml-enable-flowed t
131 "If non-nil, enable format=flowed usage when encoding a message.
132 This is only performed when filling on text/plain with hard
133 newlines in the text."
138 (defvar mml-tweak-type-alist nil
139 "A list of (TYPE . FUNCTION) for tweaking MML parts.
140 TYPE is a string containing a regexp to match the MIME type. FUNCTION
141 is a Lisp function which is called with the MML handle to tweak the
142 part. This variable is used only when no TWEAK parameter exists in
145 (defvar mml-tweak-function-alist nil
146 "A list of (NAME . FUNCTION) for tweaking MML parts.
147 NAME is a string containing the name of the TWEAK parameter in the MML
148 handle. FUNCTION is a Lisp function which is called with the MML
149 handle to tweak the part.")
151 (defvar mml-tweak-sexp-alist
152 '((mml-externalize-attachments . mml-tweak-externalize-attachments
))
153 "A list of (SEXP . FUNCTION) for tweaking MML parts.
154 SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
155 is called. FUNCTION is a Lisp function which is called with the MML
156 handle to tweak the part.")
158 (defvar mml-externalize-attachments nil
159 "*If non-nil, local-file attachments are generated as external parts.")
161 (defvar mml-generate-multipart-alist nil
162 "*Alist of multipart generation functions.
163 Each entry has the form (NAME . FUNCTION), where
164 NAME is a string containing the name of the part (without the
165 leading \"/multipart/\"),
166 FUNCTION is a Lisp function which is called to generate the part.
168 The Lisp function has to supply the appropriate MIME headers and the
169 contents of this part.")
171 (defvar mml-syntax-table
172 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
173 (modify-syntax-entry ?
\\ "/" table
)
174 (modify-syntax-entry ?
< "(" table
)
175 (modify-syntax-entry ?
> ")" table
)
176 (modify-syntax-entry ?
@ "w" table
)
177 (modify-syntax-entry ?
/ "w" table
)
178 (modify-syntax-entry ?
= " " table
)
179 (modify-syntax-entry ?
* " " table
)
180 (modify-syntax-entry ?\
; " " table)
181 (modify-syntax-entry ?
\' " " table
)
184 (defvar mml-boundary-function
'mml-make-boundary
185 "A function called to suggest a boundary.
186 The function may be called several times, and should try to make a new
187 suggestion each time. The function is called with one parameter,
188 which is a number that says how many times the function has been
189 called for this message.")
191 (defvar mml-confirmation-set nil
192 "A list of symbols, each of which disables some warning.
193 `unknown-encoding': always send messages contain characters with
194 unknown encoding; `use-ascii': always use ASCII for those characters
195 with unknown encoding; `multipart': always send messages with more than
198 (defvar mml-generate-default-type
"text/plain"
199 "Content type by which the Content-Type header can be omitted.
200 The Content-Type header will not be put in the MIME part if the type
201 equals the value and there's no parameter (e.g. charset, format, etc.)
202 and `mml-insert-mime-headers-always' is nil. The value will be bound
203 to \"message/rfc822\" when encoding an article to be forwarded as a MIME
204 part. This is for the internal use, you should never modify the value.")
206 (defvar mml-buffer-list nil
)
208 (defun mml-generate-new-buffer (name)
209 (let ((buf (generate-new-buffer name
)))
210 (push buf mml-buffer-list
)
213 (defun mml-destroy-buffers ()
214 (let (kill-buffer-hook)
215 (mapc 'kill-buffer mml-buffer-list
)
216 (setq mml-buffer-list nil
)))
219 "Parse the current buffer as an MML document."
221 (goto-char (point-min))
222 (with-syntax-table mml-syntax-table
225 (defun mml-parse-1 ()
226 "Parse the current buffer as an MML document."
227 (let (struct tag point contents charsets warn use-ascii no-markup-p raw
)
228 (while (and (not (eobp))
229 (not (looking-at "<#/multipart")))
231 ((looking-at "<#secure")
232 ;; The secure part is essentially a meta-meta tag, which
233 ;; expands to either a part tag if there are no other parts in
234 ;; the document or a multipart tag if there are other parts
235 ;; included in the message
237 (taginfo (mml-read-tag))
238 (keyfile (cdr (assq 'keyfile taginfo
)))
239 (certfiles (delq nil
(mapcar (lambda (tag)
240 (if (eq (car-safe tag
) 'certfile
)
243 (recipients (cdr (assq 'recipients taginfo
)))
244 (sender (cdr (assq 'sender taginfo
)))
245 (location (cdr (assq 'tag-location taginfo
)))
246 (mode (cdr (assq 'mode taginfo
)))
247 (method (cdr (assq 'method taginfo
)))
250 (if (re-search-forward
251 "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t
)
252 (setq secure-mode
"multipart")
253 (setq secure-mode
"part")))
256 (re-search-forward "<#secure[^\n]*>\n"))
257 (delete-region (match-beginning 0) (match-end 0))
258 (cond ((string= mode
"sign")
259 (setq tags
(list "sign" method
)))
260 ((string= mode
"encrypt")
261 (setq tags
(list "encrypt" method
)))
262 ((string= mode
"signencrypt")
263 (setq tags
(list "sign" method
"encrypt" method
))))
264 (eval `(mml-insert-tag ,secure-mode
266 ,(if keyfile
"keyfile")
269 (mapcar (lambda (certfile)
270 (list "certfile" certfile
))
272 ,(if recipients
"recipients")
274 ,(if sender
"sender")
277 (goto-char location
)))
278 ((looking-at "<#multipart")
279 (push (nconc (mml-read-tag) (mml-parse-1)) struct
))
280 ((looking-at "<#external")
281 (push (nconc (mml-read-tag) (list (cons 'contents
(mml-read-part))))
284 (if (or (looking-at "<#part") (looking-at "<#mml"))
285 (setq tag
(mml-read-tag)
288 (setq tag
(list 'part
'(type .
"text/plain"))
291 (setq raw
(cdr (assq 'raw tag
))
293 contents
(mml-read-part (eq 'mml
(car tag
)))
298 (intern (downcase (cdr (assq 'charset tag
))))))
300 (mm-find-mime-charset-region point
(point)
302 (when (and (not raw
) (memq nil charsets
))
303 (if (or (memq 'unknown-encoding mml-confirmation-set
)
304 (message-options-get 'unknown-encoding
)
306 Message contains characters with unknown encoding. Really send? ")
307 (message-options-set 'unknown-encoding t
)))
309 (or (memq 'use-ascii mml-confirmation-set
)
310 (message-options-get 'use-ascii
)
311 (and (y-or-n-p "Use ASCII as charset? ")
312 (message-options-set 'use-ascii t
))))
313 (setq charsets
(delq nil charsets
))
315 (error "Edit your message to remove those characters")))
318 (< (length charsets
) 2))
319 (if (or (not no-markup-p
)
320 (string-match "[^ \t\r\n]" contents
))
321 ;; Don't create blank parts.
322 (push (nconc tag
(list (cons 'contents contents
)))
324 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
325 tag point
(point) use-ascii
)))
327 (not (memq 'multipart mml-confirmation-set
))
328 (not (message-options-get 'multipart
))
329 (not (and (y-or-n-p (format "\
330 A message part needs to be split into %d charset parts. Really send? "
332 (message-options-set 'multipart t
))))
333 (error "Edit your message to use only one charset"))
334 (setq struct
(nconc nstruct struct
)))))))
339 (defun mml-parse-singlepart-with-multiple-charsets
340 (orig-tag beg end
&optional use-ascii
)
343 (narrow-to-region beg end
)
344 (goto-char (point-min))
345 (let ((current (or (mm-mime-charset (mm-charset-after))
346 (and use-ascii
'us-ascii
)))
347 charset struct space newline paragraph
)
349 (setq charset
(mm-mime-charset (mm-charset-after)))
351 ;; The charset remains the same.
352 ((eq charset
'us-ascii
))
353 ((or (and use-ascii
(not charset
))
354 (eq charset current
))
358 ;; The initial charset was ascii.
359 ((eq current
'us-ascii
)
360 (setq current charset
364 ;; We have a change in charsets.
368 (list (cons 'contents
369 (buffer-substring-no-properties
370 beg
(or paragraph newline space
(point))))))
372 (setq beg
(or paragraph newline space
(point))
377 ;; Compute places where it might be nice to break the part.
379 ((memq (following-char) '(? ?
\t))
380 (setq space
(1+ (point))))
381 ((and (eq (following-char) ?
\n)
383 (eq (char-after (1- (point))) ?
\n))
384 (setq paragraph
(point)))
385 ((eq (following-char) ?
\n)
386 (setq newline
(1+ (point)))))
388 ;; Do the final part.
389 (unless (= beg
(point))
390 (push (append orig-tag
391 (list (cons 'contents
392 (buffer-substring-no-properties
397 (defun mml-read-tag ()
398 "Read a tag and return the contents."
399 (let ((orig-point (point))
400 contents name elem val
)
402 (setq name
(buffer-substring-no-properties
403 (point) (progn (forward-sexp 1) (point))))
404 (skip-chars-forward " \t\n")
405 (while (not (looking-at ">[ \t]*\n?"))
406 (setq elem
(buffer-substring-no-properties
407 (point) (progn (forward-sexp 1) (point))))
408 (skip-chars-forward "= \t\n")
409 (setq val
(buffer-substring-no-properties
410 (point) (progn (forward-sexp 1) (point))))
411 (when (string-match "\\`\"" val
)
412 (setq val
(read val
))) ;; inverse of prin1 in mml-insert-tag
413 (push (cons (intern elem
) val
) contents
)
414 (skip-chars-forward " \t\n"))
415 (goto-char (match-end 0))
416 ;; Don't skip the leading space.
417 ;;(skip-chars-forward " \t\n")
418 ;; Put the tag location into the returned contents
419 (setq contents
(append (list (cons 'tag-location orig-point
)) contents
))
420 (cons (intern name
) (nreverse contents
))))
422 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end
)
423 (let ((str (buffer-substring-no-properties start end
))
424 (bufstart start
) tmp
)
425 (while (setq tmp
(text-property-any start end
'hard
't
))
426 (set-text-properties (- tmp bufstart
) (- tmp bufstart -
1)
428 (setq start
(1+ tmp
)))
431 (defun mml-read-part (&optional mml
)
432 "Return the buffer up till the next part, multipart or closing part or multipart.
433 If MML is non-nil, return the buffer up till the correspondent mml tag."
434 (let ((beg (point)) (count 1))
435 ;; If the tag ended at the end of the line, we go to the next line.
436 (when (looking-at "[ \t]*\n")
440 (while (and (> count
0) (not (eobp)))
441 (if (re-search-forward "<#\\(/\\)?mml." nil t
)
442 (setq count
(+ count
(if (match-beginning 1) -
1 1)))
443 (goto-char (point-max))))
444 (mml-buffer-substring-no-properties-except-hard-newlines
447 (match-beginning 0))))
448 (if (re-search-forward
449 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t
)
451 (mml-buffer-substring-no-properties-except-hard-newlines
452 beg
(match-beginning 0))
453 (if (or (not (match-beginning 1))
454 (equal (match-string 2) "multipart"))
455 (goto-char (match-beginning 0))
456 (when (looking-at "[ \t]*\n")
458 (mml-buffer-substring-no-properties-except-hard-newlines
459 beg
(goto-char (point-max)))))))
461 (defvar mml-boundary nil
)
462 (defvar mml-base-boundary
"-=-=")
463 (defvar mml-multipart-number
0)
464 (defvar mml-inhibit-compute-boundary nil
)
466 (defun mml-generate-mime (&optional multipart-type
)
467 "Generate a MIME message based on the current MML document.
468 MULTIPART-TYPE defaults to \"mixed\", but can also
469 be \"related\" or \"alternate\"."
470 (let ((cont (mml-parse))
471 (mml-multipart-number mml-multipart-number
)
472 (options message-options
))
476 (mm-with-multibyte-buffer
477 (setq message-options options
)
478 (if (and (consp (car cont
))
480 (mml-generate-mime-1 (car cont
))
482 (nconc (list 'multipart
(cons 'type
(or multipart-type
"mixed")))
484 (setq options message-options
)
486 (setq message-options options
)))))
488 (defun mml-generate-mime-1 (cont)
489 (let ((mm-use-ultra-safe-encoding
490 (or mm-use-ultra-safe-encoding
(assq 'sign cont
))))
492 (narrow-to-region (point) (point))
493 (mml-tweak-part cont
)
495 ((or (eq (car cont
) 'part
) (eq (car cont
) 'mml
))
496 (let* ((raw (cdr (assq 'raw cont
)))
497 (filename (cdr (assq 'filename cont
)))
498 (type (or (cdr (assq 'type cont
))
500 (or (mm-default-file-encoding filename
)
501 "application/octet-stream")
503 (charset (cdr (assq 'charset cont
)))
504 (coding (mm-charset-to-coding-system charset
))
505 encoding flowed coded
)
506 (cond ((eq coding
'ascii
)
510 ;; The value of `charset' might be a bogus alias that
511 ;; `mm-charset-synonym-alist' provides, like `utf8',
512 ;; so we prefer the MIME charset that Emacs knows for
513 ;; the coding system `coding'.
514 (setq charset
(or (mm-coding-system-to-mime-charset coding
)
515 (intern (downcase charset
))))))
517 (member (car (split-string type
"/")) '("text" "message")))
521 ((cdr (assq 'buffer cont
))
522 (insert-buffer-substring (cdr (assq 'buffer cont
))))
524 (not (equal (cdr (assq 'nofile cont
)) "yes")))
525 (let ((coding-system-for-read coding
))
526 (mm-insert-file-contents filename
)))
527 ((eq 'mml
(car cont
))
528 (insert (cdr (assq 'contents cont
))))
531 (narrow-to-region (point) (point))
532 (insert (cdr (assq 'contents cont
)))
533 ;; Remove quotes from quoted tags.
534 (goto-char (point-min))
535 (while (re-search-forward
536 "<#!+/?\\(part\\|multipart\\|external\\|mml\\|secure\\)"
538 (delete-region (+ (match-beginning 0) 2)
539 (+ (match-beginning 0) 3))))))
541 ((eq (car cont
) 'mml
)
542 (let ((mml-boundary (mml-compute-boundary cont
))
543 ;; It is necessary for the case where this
544 ;; function is called recursively since
545 ;; `m-g-d-t' will be bound to "message/rfc822"
546 ;; when encoding an article to be forwarded.
547 (mml-generate-default-type "text/plain"))
549 ;; Update handle so mml-compute-boundary can
550 ;; detect collisions with the nested parts.
551 (unless mml-inhibit-compute-boundary
552 (setcdr (assoc 'contents cont
) (buffer-string))))
553 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
554 ;; ignore 0x1b, it is part of iso-2022-jp
555 (setq encoding
(mm-body-7-or-8))))
556 ((string= (car (split-string type
"/")) "message")
557 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
558 ;; ignore 0x1b, it is part of iso-2022-jp
559 (setq encoding
(mm-body-7-or-8))))
561 ;; Only perform format=flowed filling on text/plain
562 ;; parts where there either isn't a format parameter
563 ;; in the mml tag or it says "flowed" and there
564 ;; actually are hard newlines in the text.
565 (let (use-hard-newlines)
566 (when (and mml-enable-flowed
567 (string= type
"text/plain")
568 (not (string= (cdr (assq 'sign cont
)) "pgp"))
569 (or (null (assq 'format cont
))
570 (string= (cdr (assq 'format cont
))
572 (setq use-hard-newlines
574 (point-min) (point-max) 'hard
't
)))
576 ;; Indicate that `mml-insert-mime-headers' should
577 ;; insert a "; format=flowed" string unless the
578 ;; user has already specified it.
579 (setq flowed
(null (assq 'format cont
)))))
580 ;; Prefer `utf-8' for text/calendar parts.
582 (not (string= type
"text/calendar")))
583 (setq charset
(mm-encode-body charset
))
584 (let ((mm-coding-system-priorities
585 (cons 'utf-8 mm-coding-system-priorities
)))
586 (setq charset
(mm-encode-body))))
587 (setq encoding
(mm-body-encoding
588 charset
(cdr (assq 'encoding cont
))))))
589 (setq coded
(buffer-string)))
590 (mml-insert-mime-headers cont type charset encoding flowed
)
593 (mm-with-unibyte-buffer
595 ((cdr (assq 'buffer cont
))
596 (insert (mm-string-as-unibyte
597 (with-current-buffer (cdr (assq 'buffer cont
))
600 (not (equal (cdr (assq 'nofile cont
)) "yes")))
601 (let ((coding-system-for-read mm-binary-coding-system
))
602 (mm-insert-file-contents filename nil nil nil nil t
))
604 (setq charset
(mm-coding-system-to-mime-charset
605 (mm-find-buffer-file-coding-system
608 (let ((contents (cdr (assq 'contents cont
))))
609 (if (if (featurep 'xemacs
)
610 (string-match "[^\000-\377]" contents
)
611 (mm-multibyte-string-p contents
))
613 (mm-enable-multibyte)
616 (setq charset
(mm-encode-body charset
))))
617 (insert contents
)))))
618 (if (setq encoding
(cdr (assq 'encoding cont
)))
619 (setq encoding
(intern (downcase encoding
))))
620 (setq encoding
(mm-encode-buffer type encoding
)
621 coded
(mm-string-as-multibyte (buffer-string))))
622 (mml-insert-mime-headers cont type charset encoding nil
)
623 (insert "\n" coded
))))
624 ((eq (car cont
) 'external
)
625 (insert "Content-Type: message/external-body")
626 (let ((parameters (mml-parameter-string
627 cont
'(expiration size permission
)))
628 (name (cdr (assq 'name cont
)))
629 (url (cdr (assq 'url cont
))))
631 (setq name
(mml-parse-file-name name
))
633 (mml-insert-parameter
634 (mail-header-encode-parameter "name" name
)
635 "access-type=local-file")
636 (mml-insert-parameter
637 (mail-header-encode-parameter
638 "name" (file-name-nondirectory (nth 2 name
)))
639 (mail-header-encode-parameter "site" (nth 1 name
))
640 (mail-header-encode-parameter
641 "directory" (file-name-directory (nth 2 name
))))
642 (mml-insert-parameter
643 (concat "access-type="
644 (if (member (nth 0 name
) '("ftp@" "anonymous@"))
648 (mml-insert-parameter
649 (mail-header-encode-parameter "url" url
)
652 (mml-insert-parameter-string
653 cont
'(expiration size permission
)))
655 (insert "Content-Type: "
656 (or (cdr (assq 'type cont
))
658 (or (mm-default-file-encoding name
)
659 "application/octet-stream")
662 (insert "Content-ID: " (message-make-message-id) "\n")
663 (insert "Content-Transfer-Encoding: "
664 (or (cdr (assq 'encoding cont
)) "binary"))
666 (insert (or (cdr (assq 'contents cont
))))
668 ((eq (car cont
) 'multipart
)
669 (let* ((type (or (cdr (assq 'type cont
)) "mixed"))
670 (mml-generate-default-type (if (equal type
"digest")
673 (handler (assoc type mml-generate-multipart-alist
)))
675 (funcall (cdr handler
) cont
)
676 ;; No specific handler. Use default one.
677 (let ((mml-boundary (mml-compute-boundary cont
)))
678 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
680 (if (cdr (assq 'start cont
))
681 (format "; start=\"%s\"\n" (cdr (assq 'start cont
)))
683 (let ((cont cont
) part
)
684 (while (setq part
(pop cont
))
685 ;; Skip `multipart' and attributes.
686 (when (and (consp part
) (consp (cdr part
)))
687 (insert "\n--" mml-boundary
"\n")
688 (mml-generate-mime-1 part
)
689 (goto-char (point-max)))))
690 (insert "\n--" mml-boundary
"--\n")))))
692 (error "Invalid element: %S" cont
)))
693 ;; handle sign & encrypt tags in a semi-smart way.
694 (let ((sign-item (assoc (cdr (assq 'sign cont
)) mml-sign-alist
))
695 (encrypt-item (assoc (cdr (assq 'encrypt cont
))
698 (when (or sign-item encrypt-item
)
699 (when (setq sender
(cdr (assq 'sender cont
)))
700 (message-options-set 'mml-sender sender
)
701 (message-options-set 'message-sender sender
))
702 (if (setq recipients
(cdr (assq 'recipients cont
)))
703 (message-options-set 'message-recipients recipients
))
704 (let ((style (mml-signencrypt-style
705 (first (or sign-item encrypt-item
)))))
706 ;; check if: we're both signing & encrypting, both methods
707 ;; are the same (why would they be different?!), and that
708 ;; the signencrypt style allows for combined operation.
709 (if (and sign-item encrypt-item
(equal (first sign-item
)
710 (first encrypt-item
))
711 (equal style
'combined
))
712 (funcall (nth 1 encrypt-item
) cont t
)
713 ;; otherwise, revert to the old behavior.
715 (funcall (nth 1 sign-item
) cont
))
717 (funcall (nth 1 encrypt-item
) cont
)))))))))
719 (defun mml-compute-boundary (cont)
720 "Return a unique boundary that does not exist in CONT."
721 (let ((mml-boundary (funcall mml-boundary-function
722 (incf mml-multipart-number
))))
723 (unless mml-inhibit-compute-boundary
724 ;; This function tries again and again until it has found
725 ;; a unique boundary.
726 (while (not (catch 'not-unique
727 (mml-compute-boundary-1 cont
)))))
730 (defun mml-compute-boundary-1 (cont)
732 ((member (car cont
) '(part mml
))
733 (mm-with-multibyte-buffer
734 (let ((mml-inhibit-compute-boundary t
)
735 (mml-multipart-number 0)
736 mml-sign-alist mml-encrypt-alist
)
737 (mml-generate-mime-1 cont
))
738 (goto-char (point-min))
739 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary
))
741 (setq mml-boundary
(funcall mml-boundary-function
742 (incf mml-multipart-number
)))
743 (throw 'not-unique nil
))))
744 ((eq (car cont
) 'multipart
)
745 (mapc 'mml-compute-boundary-1
(cddr cont
))))
748 (defun mml-make-boundary (number)
749 (concat (make-string (% number
60) ?
=)
755 (defun mml-content-disposition (type &optional filename
)
756 "Return a default disposition name suitable to TYPE or FILENAME."
757 (let ((defs mml-content-disposition-alist
)
758 disposition def types
)
759 (while (and (not disposition
) defs
)
760 (setq def
(pop defs
))
761 (cond ((stringp (car def
))
763 (string-match (car def
) filename
))
764 (setq disposition
(cdr def
))))
766 (when (string= (car (setq types
(split-string type
"/")))
768 (setq type
(cadr types
)
770 (while (and (not disposition
) types
)
771 (setq def
(pop types
))
772 (when (or (eq (car def
) t
) (string= type
(car def
)))
773 (setq disposition
(cdr def
))))))
775 (when (or (eq (car def
) t
) (string= type
(car def
)))
776 (setq disposition
(cdr def
))))))
777 (or disposition
"attachment")))
779 (defun mml-insert-mime-headers (cont type charset encoding flowed
)
780 (let (parameters id disposition description
)
782 (mml-parameter-string
783 cont mml-content-type-parameters
))
787 (not (equal type mml-generate-default-type
))
788 mml-insert-mime-headers-always
)
789 (when (consp charset
)
791 "Can't encode a part with several charsets"))
792 (insert "Content-Type: " type
)
794 (mml-insert-parameter
795 (mail-header-encode-parameter "charset" (symbol-name charset
))))
797 (mml-insert-parameter "format=flowed"))
799 (mml-insert-parameter-string
800 cont mml-content-type-parameters
))
802 (when (setq id
(cdr (assq 'id cont
)))
803 (insert "Content-ID: " id
"\n"))
805 (mml-parameter-string
806 cont mml-content-disposition-parameters
))
807 (when (or (setq disposition
(cdr (assq 'disposition cont
)))
809 (insert "Content-Disposition: "
811 (mml-content-disposition type
(cdr (assq 'filename cont
)))))
813 (mml-insert-parameter-string
814 cont mml-content-disposition-parameters
))
816 (unless (eq encoding
'7bit
)
817 (insert (format "Content-Transfer-Encoding: %s\n" encoding
)))
818 (when (setq description
(cdr (assq 'description cont
)))
819 (insert "Content-Description: ")
820 (setq description
(prog1
822 (insert description
"\n")))
823 (mail-encode-encoded-word-region description
(point)))))
825 (defun mml-parameter-string (cont types
)
828 (while (setq type
(pop types
))
829 (when (setq value
(cdr (assq type cont
)))
830 ;; Strip directory component from the filename parameter.
831 (when (eq type
'filename
)
832 (setq value
(file-name-nondirectory value
)))
833 (setq string
(concat string
"; "
834 (mail-header-encode-parameter
835 (symbol-name type
) value
)))))
836 (when (not (zerop (length string
)))
839 (defun mml-insert-parameter-string (cont types
)
841 (while (setq type
(pop types
))
842 (when (setq value
(cdr (assq type cont
)))
843 ;; Strip directory component from the filename parameter.
844 (when (eq type
'filename
)
845 (setq value
(file-name-nondirectory value
)))
846 (mml-insert-parameter
847 (mail-header-encode-parameter
848 (symbol-name type
) value
))))))
850 (defvar ange-ftp-name-format
)
851 (defvar efs-path-regexp
)
853 (defun mml-parse-file-name (path)
854 (if (if (boundp 'efs-path-regexp
)
855 (string-match efs-path-regexp path
)
856 (if (boundp 'ange-ftp-name-format
)
857 (string-match (car ange-ftp-name-format
) path
)))
858 (list (match-string 1 path
) (match-string 2 path
)
859 (substring path
(1+ (match-end 2))))
862 (defun mml-insert-buffer (buffer)
863 "Insert BUFFER at point and quote any MML markup."
865 (narrow-to-region (point) (point))
866 (insert-buffer-substring buffer
)
867 (mml-quote-region (point-min) (point-max))
868 (goto-char (point-max))))
871 ;;; Transforming MIME to MML
874 ;; message-narrow-to-head autoloads message.
875 (declare-function message-remove-header
"message"
876 (header &optional is-regexp first reverse
))
878 (defun mime-to-mml (&optional handles
)
879 "Translate the current buffer (which should be a message) into MML.
880 If HANDLES is non-nil, use it instead reparsing the buffer."
881 ;; First decode the head.
883 (message-narrow-to-head)
884 (let ((rfc2047-quote-decoded-words-containing-tspecials t
))
885 (mail-decode-encoded-word-region (point-min) (point-max))))
887 (setq handles
(mm-dissect-buffer t
)))
888 (goto-char (point-min))
889 (search-forward "\n\n" nil t
)
890 (delete-region (point) (point-max))
891 (if (stringp (car handles
))
892 (mml-insert-mime handles
)
893 (mml-insert-mime handles t
))
894 (mm-destroy-parts handles
)
896 (message-narrow-to-head)
897 ;; Remove them, they are confusing.
898 (message-remove-header "Content-Type")
899 (message-remove-header "MIME-Version")
900 (message-remove-header "Content-Disposition")
901 (message-remove-header "Content-Transfer-Encoding")))
903 (autoload 'message-encode-message-body
"message")
904 (declare-function message-narrow-to-headers-or-head
"message" ())
907 (defun mml-to-mime ()
908 "Translate the current buffer from MML to MIME."
909 ;; `message-encode-message-body' will insert an encoded Content-Description
910 ;; header in the message header if the body contains a single part
911 ;; that is specified by a user with a MML tag containing a description
912 ;; token. So, we encode the message header first to prevent the encoded
913 ;; Content-Description header from being encoded again.
915 (message-narrow-to-headers-or-head)
916 ;; Skip past any From_ headers.
917 (while (looking-at "From ")
919 (let ((mail-parse-charset message-default-charset
))
920 (mail-encode-encoded-word-buffer)))
921 (message-encode-message-body))
923 (defun mml-insert-mime (handle &optional no-markup
)
924 (let (textp buffer mmlp
)
925 ;; Determine type and stuff.
926 (unless (stringp (car handle
))
927 (unless (setq textp
(equal (mm-handle-media-supertype handle
) "text"))
928 (with-current-buffer (setq buffer
(mml-generate-new-buffer " *mml*"))
929 (if (eq (mail-content-type-get (mm-handle-type handle
) 'charset
)
931 ;; A part that mm-uu dissected from a non-MIME message
932 ;; because of `gnus-article-emulate-mime'.
934 (mm-enable-multibyte)
935 (insert-buffer-substring (mm-handle-buffer handle
)))
936 (mm-insert-part handle
'no-cache
)
937 (if (setq mmlp
(equal (mm-handle-media-type handle
)
941 (mml-insert-mml-markup handle nil t t
)
942 (unless (and no-markup
943 (equal (mm-handle-media-type handle
) "text/plain"))
944 (mml-insert-mml-markup handle buffer textp
)))
947 (insert-buffer-substring buffer
)
948 (goto-char (point-max))
949 (insert "<#/mml>\n"))
950 ((stringp (car handle
))
951 (mapc 'mml-insert-mime
(cdr handle
))
952 (insert "<#/multipart>\n"))
954 (let ((charset (mail-content-type-get
955 (mm-handle-type handle
) 'charset
))
957 (if (eq charset
'gnus-decoded
)
958 (mm-insert-part handle
)
959 (insert (mm-decode-string (mm-get-part handle
) charset
)))
960 (mml-quote-region start
(point)))
961 (goto-char (point-max)))
963 (insert "<#/part>\n")))))
965 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp
)
966 "Take a MIME handle and insert an MML tag."
967 (if (stringp (car handle
))
969 (insert "<#multipart type=" (mm-handle-media-subtype handle
))
970 (let ((start (mm-handle-multipart-ctl-parameter handle
'start
)))
972 (insert " start=\"" start
"\"")))
975 (insert "<#mml type=" (mm-handle-media-type handle
))
976 (insert "<#part type=" (mm-handle-media-type handle
)))
977 (dolist (elem (append (cdr (mm-handle-type handle
))
978 (cdr (mm-handle-disposition handle
))))
979 (unless (symbolp (cdr elem
))
980 (insert " " (symbol-name (car elem
)) "=\"" (cdr elem
) "\"")))
981 (when (mm-handle-id handle
)
982 (insert " id=\"" (mm-handle-id handle
) "\""))
983 (when (mm-handle-disposition handle
)
984 (insert " disposition=" (car (mm-handle-disposition handle
))))
986 (insert " buffer=\"" (buffer-name buffer
) "\""))
988 (insert " nofile=yes"))
989 (when (mm-handle-description handle
)
990 (insert " description=\"" (mm-handle-description handle
) "\""))
993 (defun mml-insert-parameter (&rest parameters
)
994 "Insert PARAMETERS in a nice way."
996 (dolist (param parameters
)
1003 (if (> (current-column) 76)
1007 (goto-char (1+ end
)))
1011 ;;; Mode for inserting and editing MML forms
1014 (defvar mml-mode-map
1015 (let ((sign (make-sparse-keymap))
1016 (encrypt (make-sparse-keymap))
1017 (signpart (make-sparse-keymap))
1018 (encryptpart (make-sparse-keymap))
1019 (map (make-sparse-keymap))
1020 (main (make-sparse-keymap)))
1021 (define-key map
"\C-s" 'mml-secure-message-sign
)
1022 (define-key map
"\C-c" 'mml-secure-message-encrypt
)
1023 (define-key map
"\C-e" 'mml-secure-message-sign-encrypt
)
1024 (define-key map
"\C-p\C-s" 'mml-secure-sign
)
1025 (define-key map
"\C-p\C-c" 'mml-secure-encrypt
)
1026 (define-key sign
"p" 'mml-secure-message-sign-pgpmime
)
1027 (define-key sign
"o" 'mml-secure-message-sign-pgp
)
1028 (define-key sign
"s" 'mml-secure-message-sign-smime
)
1029 (define-key signpart
"p" 'mml-secure-sign-pgpmime
)
1030 (define-key signpart
"o" 'mml-secure-sign-pgp
)
1031 (define-key signpart
"s" 'mml-secure-sign-smime
)
1032 (define-key encrypt
"p" 'mml-secure-message-encrypt-pgpmime
)
1033 (define-key encrypt
"o" 'mml-secure-message-encrypt-pgp
)
1034 (define-key encrypt
"s" 'mml-secure-message-encrypt-smime
)
1035 (define-key encryptpart
"p" 'mml-secure-encrypt-pgpmime
)
1036 (define-key encryptpart
"o" 'mml-secure-encrypt-pgp
)
1037 (define-key encryptpart
"s" 'mml-secure-encrypt-smime
)
1038 (define-key map
"\C-n" 'mml-unsecure-message
)
1039 (define-key map
"f" 'mml-attach-file
)
1040 (define-key map
"b" 'mml-attach-buffer
)
1041 (define-key map
"e" 'mml-attach-external
)
1042 (define-key map
"q" 'mml-quote-region
)
1043 (define-key map
"m" 'mml-insert-multipart
)
1044 (define-key map
"p" 'mml-insert-part
)
1045 (define-key map
"v" 'mml-validate
)
1046 (define-key map
"P" 'mml-preview
)
1047 (define-key map
"s" sign
)
1048 (define-key map
"S" signpart
)
1049 (define-key map
"c" encrypt
)
1050 (define-key map
"C" encryptpart
)
1051 ;;(define-key map "n" 'mml-narrow-to-part)
1052 ;; `M-m' conflicts with `back-to-indentation'.
1053 ;; (define-key main "\M-m" map)
1054 (define-key main
"\C-c\C-m" map
)
1058 mml-menu mml-mode-map
""
1060 ["Attach File..." mml-attach-file
1061 ,@(if (featurep 'xemacs
) '(t)
1062 '(:help
"Attach a file at point"))]
1063 ["Attach Buffer..." mml-attach-buffer
1064 ,@(if (featurep 'xemacs
) '(t)
1065 '(:help
"Attach a buffer to the outgoing message"))]
1066 ["Attach External..." mml-attach-external
1067 ,@(if (featurep 'xemacs
) '(t)
1068 '(:help
"Attach reference to an external file"))]
1069 ;; FIXME: Is it possible to do this without using
1070 ;; `gnus-gcc-externalize-attachments'?
1071 ["Externalize Attachments"
1074 (if (not (and (boundp 'gnus-gcc-externalize-attachments
)
1075 (memq gnus-gcc-externalize-attachments
1077 ;; Stupid workaround for XEmacs not honoring :visible.
1078 (message "Can't handle this value of `gnus-gcc-externalize-attachments'")
1079 (setq gnus-gcc-externalize-attachments
1080 (not gnus-gcc-externalize-attachments
))
1081 (message "gnus-gcc-externalize-attachments is `%s'."
1082 gnus-gcc-externalize-attachments
)))
1083 ;; XEmacs barfs on :visible.
1084 ,@(if (featurep 'xemacs
) nil
1085 '(:visible
(and (boundp 'gnus-gcc-externalize-attachments
)
1086 (memq gnus-gcc-externalize-attachments
1089 :selected gnus-gcc-externalize-attachments
1090 ,@(if (featurep 'xemacs
) nil
1091 '(:help
"Save attachments as external parts in Gcc copies"))]
1094 ("Change Security Method"
1096 (lambda () (interactive) (setq mml-secure-method
"pgpmime"))
1097 ,@(if (featurep 'xemacs
) nil
1098 '(:help
"Set Security Method to PGP/MIME"))
1100 :selected
(equal mml-secure-method
"pgpmime") ]
1102 (lambda () (interactive) (setq mml-secure-method
"smime"))
1103 ,@(if (featurep 'xemacs
) nil
1104 '(:help
"Set Security Method to S/MIME"))
1106 :selected
(equal mml-secure-method
"smime") ]
1108 (lambda () (interactive) (setq mml-secure-method
"pgp"))
1109 ,@(if (featurep 'xemacs
) nil
1110 '(:help
"Set Security Method to inline PGP"))
1112 :selected
(equal mml-secure-method
"pgp") ] )
1114 ["Sign Message" mml-secure-message-sign t
]
1115 ["Encrypt Message" mml-secure-message-encrypt t
]
1116 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t
]
1117 ["Encrypt/Sign off" mml-unsecure-message
1118 ,@(if (featurep 'xemacs
) '(t)
1119 '(:help
"Don't Encrypt/Sign Message"))]
1120 ;; Do we have separate encrypt and encrypt/sign commands for parts?
1121 ["Sign Part" mml-secure-sign t
]
1122 ["Encrypt Part" mml-secure-encrypt t
]
1124 ;; Maybe we could remove these, because people who write MML most probably
1125 ;; don't use the menu:
1126 ["Insert Part..." mml-insert-part
1127 :active
(message-in-body-p)]
1128 ["Insert Multipart..." mml-insert-multipart
1129 :active
(message-in-body-p)]
1131 ;;["Narrow" mml-narrow-to-part t]
1132 ["Quote MML in region" mml-quote-region
1133 :active
(message-mark-active-p)
1134 ,@(if (featurep 'xemacs
) nil
1135 '(:help
"Quote MML tags in region"))]
1136 ["Validate MML" mml-validate t
]
1137 ["Preview" mml-preview t
]
1139 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
1140 ,@(if (featurep 'xemacs
) '(t)
1141 '(:help
"Display the Emacs MIME manual"))]
1142 ["PGG manual" (lambda () (interactive) (message-info mml2015-use
))
1143 ;; XEmacs barfs on :visible.
1144 ,@(if (featurep 'xemacs
) nil
1145 '(:visible
(and (boundp 'mml2015-use
) (equal mml2015-use
'pgg
))))
1146 ,@(if (featurep 'xemacs
) '(t)
1147 '(:help
"Display the PGG manual"))]
1148 ["EasyPG manual" (lambda () (interactive) (require 'mml2015
) (message-info mml2015-use
))
1149 ;; XEmacs barfs on :visible.
1150 ,@(if (featurep 'xemacs
) nil
1151 '(:visible
(and (boundp 'mml2015-use
) (equal mml2015-use
'epg
))))
1152 ,@(if (featurep 'xemacs
) '(t)
1153 '(:help
"Display the EasyPG manual"))]))
1155 (define-minor-mode mml-mode
1156 "Minor mode for editing MML.
1157 MML is the MIME Meta Language, a minor mode for composing MIME articles.
1158 See Info node `(emacs-mime)Composing'.
1161 :lighter
" MML" :keymap mml-mode-map
1163 (easy-menu-add mml-menu mml-mode-map
)
1164 (when (boundp 'dnd-protocol-alist
)
1165 (set (make-local-variable 'dnd-protocol-alist
)
1166 (append mml-dnd-protocol-alist dnd-protocol-alist
)))))
1169 ;;; Helper functions for reading MIME stuff from the minibuffer and
1170 ;;; inserting stuff to the buffer.
1173 (defcustom mml-default-directory mm-default-directory
1174 "The default directory where mml will find files.
1175 If not set, `default-directory' will be used."
1176 :type
'(choice directory
(const :tag
"Default" nil
))
1177 :version
"23.1" ;; No Gnus
1180 (defun mml-minibuffer-read-file (prompt)
1181 (let* ((completion-ignored-extensions nil
)
1182 (file (read-file-name prompt
1183 (or mml-default-directory default-directory
)
1185 ;; Prevent some common errors. This is inspired by similar code in
1187 (when (file-directory-p file
)
1188 (error "%s is a directory, cannot attach" file
))
1189 (unless (file-exists-p file
)
1190 (error "No such file: %s" file
))
1191 (unless (file-readable-p file
)
1192 (error "Permission denied: %s" file
))
1195 (declare-function mailcap-parse-mimetypes
"mailcap" (&optional path force
))
1196 (declare-function mailcap-mime-types
"mailcap" ())
1198 (defun mml-minibuffer-read-type (name &optional default
)
1200 (mailcap-parse-mimetypes)
1201 (let* ((default (or default
1202 (mm-default-file-encoding name
)
1203 ;; Perhaps here we should check what the file
1204 ;; looks like, and offer text/plain if it looks
1206 "application/octet-stream"))
1207 (string (gnus-completing-read
1209 (mailcap-mime-types)
1210 nil nil nil default
)))
1211 (if (not (equal string
""))
1215 (defun mml-minibuffer-read-description ()
1216 (let ((description (read-string "One line description: ")))
1217 (when (string-match "\\`[ \t]*\\'" description
)
1218 (setq description nil
))
1221 (defun mml-minibuffer-read-disposition (type &optional default filename
)
1223 (setq default
(mml-content-disposition type filename
)))
1224 (let ((disposition (gnus-completing-read
1226 '("attachment" "inline")
1227 t nil nil default
)))
1228 (if (not (equal disposition
""))
1232 (defun mml-quote-region (beg end
)
1233 "Quote the MML tags in the region."
1237 ;; Temporarily narrow the region to defend from changes
1238 ;; invalidating END.
1239 (narrow-to-region beg end
)
1240 (goto-char (point-min))
1242 (while (re-search-forward
1243 "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)" nil t
)
1244 ;; Insert ! after the #.
1245 (goto-char (+ (match-beginning 0) 2))
1248 (defun mml-insert-tag (name &rest plist
)
1249 "Insert an MML tag described by NAME and PLIST."
1250 (when (symbolp name
)
1251 (setq name
(symbol-name name
)))
1254 (let ((key (pop plist
))
1255 (value (pop plist
)))
1257 ;; Quote VALUE if it contains suspicious characters.
1258 (when (string-match "[\"'\\~/*;() \t\n]" value
)
1259 (setq value
(with-output-to-string
1260 (let (print-escape-nonascii)
1262 (insert (format " %s=%s" key value
)))))
1265 (defun mml-insert-empty-tag (name &rest plist
)
1266 "Insert an empty MML tag described by NAME and PLIST."
1267 (when (symbolp name
)
1268 (setq name
(symbol-name name
)))
1269 (apply #'mml-insert-tag name plist
)
1270 (insert "<#/" name
">\n"))
1272 ;;; Attachment functions.
1274 (defcustom mml-dnd-protocol-alist
1275 '(("^file:///" . mml-dnd-attach-file
)
1276 ("^file://" . dnd-open-file
)
1277 ("^file:" . mml-dnd-attach-file
))
1278 "The functions to call when a drop in `mml-mode' is made.
1279 See `dnd-protocol-alist' for more information. When nil, behave
1280 as in other buffers."
1281 :type
'(choice (repeat (cons (regexp) (function)))
1282 (const :tag
"Behave as in other buffers" nil
))
1283 :version
"22.1" ;; Gnus 5.10.9
1286 (defcustom mml-dnd-attach-options nil
1287 "Which options should be queried when attaching a file via drag and drop.
1289 If it is a list, valid members are `type', `description' and
1290 `disposition'. `disposition' implies `type'. If it is nil,
1291 don't ask for options. If it is t, ask the user whether or not
1292 to specify options."
1294 (const :tag
"None" nil
)
1295 (const :tag
"Query" t
)
1296 (list :value
(type description disposition
)
1300 (const disposition
))))
1301 :version
"22.1" ;; Gnus 5.10.9
1305 (defun mml-attach-file (file &optional type description disposition
)
1306 "Attach a file to the outgoing MIME message.
1307 The file is not inserted or encoded until you send the message with
1308 `\\[message-send-and-exit]' or `\\[message-send]' in Message mode,
1309 or `\\[mail-send-and-exit]' or `\\[mail-send]' in Mail mode.
1311 FILE is the name of the file to attach. TYPE is its
1312 content-type, a string of the form \"type/subtype\". DESCRIPTION
1313 is a one-line description of the attachment. The DISPOSITION
1314 specifies how the attachment is intended to be displayed. It can
1315 be either \"inline\" (displayed automatically within the message
1316 body) or \"attachment\" (separate from the body)."
1318 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1319 (type (mml-minibuffer-read-type file
))
1320 (description (mml-minibuffer-read-description))
1321 (disposition (mml-minibuffer-read-disposition type nil file
)))
1322 (list file type description disposition
)))
1323 ;; If in the message header, attach at the end and leave point unchanged.
1324 (let ((head (unless (message-in-body-p) (point))))
1325 (if head
(goto-char (point-max)))
1326 (mml-insert-empty-tag 'part
1328 ;; icicles redefines read-file-name and returns a
1329 ;; string w/ text properties :-/
1330 'filename
(mm-substring-no-properties file
)
1331 'disposition
(or disposition
"attachment")
1332 'description description
)
1333 ;; When using Mail mode, make sure it does the mime encoding
1334 ;; when you send the message.
1335 (or (eq mail-user-agent
'message-user-agent
)
1336 (setq mail-encode-mml t
))
1338 (unless (pos-visible-in-window-p)
1339 (message "The file \"%s\" has been attached at the end of the message"
1340 (file-name-nondirectory file
)))
1343 (defun mml-dnd-attach-file (uri action
)
1344 "Attach a drag and drop file.
1346 Ask for type, description or disposition according to
1347 `mml-dnd-attach-options'."
1348 (let ((file (dnd-get-local-file-name uri t
)))
1349 (when (and file
(file-regular-p file
))
1350 (let ((mml-dnd-attach-options mml-dnd-attach-options
)
1351 type description disposition
)
1352 (setq mml-dnd-attach-options
1353 (when (and (eq mml-dnd-attach-options t
)
1356 "Use default type, disposition and description? ")))
1357 '(type description disposition
)))
1358 (when (or (memq 'type mml-dnd-attach-options
)
1359 (memq 'disposition mml-dnd-attach-options
))
1360 (setq type
(mml-minibuffer-read-type file
)))
1361 (when (memq 'description mml-dnd-attach-options
)
1362 (setq description
(mml-minibuffer-read-description)))
1363 (when (memq 'disposition mml-dnd-attach-options
)
1364 (setq disposition
(mml-minibuffer-read-disposition type nil file
)))
1365 (mml-attach-file file type description disposition
)))))
1367 (defun mml-attach-buffer (buffer &optional type description disposition
)
1368 "Attach a buffer to the outgoing MIME message.
1369 BUFFER is the name of the buffer to attach. See
1370 `mml-attach-file' for details of operation."
1372 (let* ((buffer (read-buffer "Attach buffer: "))
1373 (type (mml-minibuffer-read-type buffer
"text/plain"))
1374 (description (mml-minibuffer-read-description))
1375 (disposition (mml-minibuffer-read-disposition type nil
)))
1376 (list buffer type description disposition
)))
1377 ;; If in the message header, attach at the end and leave point unchanged.
1378 (let ((head (unless (message-in-body-p) (point))))
1379 (if head
(goto-char (point-max)))
1380 (mml-insert-empty-tag 'part
'type type
'buffer buffer
1381 'disposition disposition
1382 'description description
)
1383 ;; When using Mail mode, make sure it does the mime encoding
1384 ;; when you send the message.
1385 (or (eq mail-user-agent
'message-user-agent
)
1386 (setq mail-encode-mml t
))
1388 (unless (pos-visible-in-window-p)
1390 "The buffer \"%s\" has been attached at the end of the message"
1394 (defun mml-attach-external (file &optional type description
)
1395 "Attach an external file into the buffer.
1396 FILE is an ange-ftp/efs specification of the part location.
1397 TYPE is the MIME type to use."
1399 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1400 (type (mml-minibuffer-read-type file
))
1401 (description (mml-minibuffer-read-description)))
1402 (list file type description
)))
1403 ;; If in the message header, attach at the end and leave point unchanged.
1404 (let ((head (unless (message-in-body-p) (point))))
1405 (if head
(goto-char (point-max)))
1406 (mml-insert-empty-tag 'external
'type type
'name file
1407 'disposition
"attachment" 'description description
)
1408 ;; When using Mail mode, make sure it does the mime encoding
1409 ;; when you send the message.
1410 (or (eq mail-user-agent
'message-user-agent
)
1411 (setq mail-encode-mml t
))
1413 (unless (pos-visible-in-window-p)
1414 (message "The file \"%s\" has been attached at the end of the message"
1415 (file-name-nondirectory file
)))
1418 (defun mml-insert-multipart (&optional type
)
1419 (interactive (if (message-in-body-p)
1420 (list (gnus-completing-read "Multipart type"
1421 '("mixed" "alternative"
1423 "signed" "encrypted")
1425 (error "Use this command in the message body")))
1427 (setq type
"mixed"))
1428 (mml-insert-empty-tag "multipart" 'type type
)
1429 ;; When using Mail mode, make sure it does the mime encoding
1430 ;; when you send the message.
1431 (or (eq mail-user-agent
'message-user-agent
)
1432 (setq mail-encode-mml t
))
1435 (defun mml-insert-part (&optional type
)
1436 (interactive (if (message-in-body-p)
1437 (list (mml-minibuffer-read-type ""))
1438 (error "Use this command in the message body")))
1439 ;; When using Mail mode, make sure it does the mime encoding
1440 ;; when you send the message.
1441 (or (eq mail-user-agent
'message-user-agent
)
1442 (setq mail-encode-mml t
))
1443 (mml-insert-tag 'part
'type type
'disposition
"inline")
1445 (mml-insert-tag '/part
)))
1447 (declare-function message-subscribed-p
"message" ())
1448 (declare-function message-make-mail-followup-to
"message"
1449 (&optional only-show-subscribed
))
1450 (declare-function message-position-on-field
"message" (header &rest afters
))
1452 (defun mml-preview-insert-mail-followup-to ()
1453 "Insert a Mail-Followup-To header before previewing an article.
1454 Should be adopted if code in `message-send-mail' is changed."
1455 (when (and (message-mail-p)
1456 (message-subscribed-p)
1457 (not (mail-fetch-field "mail-followup-to"))
1458 (message-make-mail-followup-to))
1459 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1460 (insert (message-make-mail-followup-to))))
1462 (defvar mml-preview-buffer nil
)
1464 (autoload 'gnus-make-hashtable
"gnus-util")
1465 (autoload 'widget-button-press
"wid-edit" nil t
)
1466 (declare-function widget-event-point
"wid-edit" (event))
1467 ;; If gnus-buffer-configuration is bound this is loaded.
1468 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
1469 ;; Called after message-mail-p, which autoloads message.
1470 (declare-function message-news-p
"message" ())
1471 (declare-function message-options-set-recipient
"message" ())
1472 (declare-function message-generate-headers
"message" (headers))
1473 (declare-function message-sort-headers
"message" ())
1475 (defun mml-preview (&optional raw
)
1476 "Display current buffer with Gnus, in a new buffer.
1477 If RAW, display a raw encoded MIME message.
1479 The window layout for the preview buffer is controlled by the variables
1480 `special-display-buffer-names', `special-display-regexps', or
1481 `gnus-buffer-configuration' (the first match made will be used),
1482 or the `pop-to-buffer' function."
1484 (setq mml-preview-buffer
(generate-new-buffer
1485 (concat (if raw
"*Raw MIME preview of "
1486 "*MIME preview of ") (buffer-name))))
1487 (require 'gnus-msg
) ; for gnus-setup-posting-charset
1489 (let* ((buf (current-buffer))
1490 (article-editing (eq major-mode
'gnus-article-edit-mode
))
1491 (message-options message-options
)
1492 (message-this-is-mail (message-mail-p))
1493 (message-this-is-news (message-news-p))
1494 (message-posting-charset (or (gnus-setup-posting-charset
1496 (message-narrow-to-headers-or-head)
1497 (message-fetch-field "Newsgroups")))
1498 message-posting-charset
)))
1499 (message-options-set-recipient)
1500 (when (boundp 'gnus-buffers
)
1501 (push mml-preview-buffer gnus-buffers
))
1504 (set-buffer mml-preview-buffer
)
1506 (insert-buffer-substring buf
))
1507 (mml-preview-insert-mail-followup-to)
1508 (let ((message-deletable-headers (if (message-news-p)
1510 message-deletable-headers
))
1511 (mail-header-separator (if article-editing
1513 mail-header-separator
)))
1514 (message-generate-headers
1515 (copy-sequence (if (message-news-p)
1516 message-required-news-headers
1517 message-required-mail-headers
)))
1518 (unless article-editing
1519 (if (re-search-forward
1520 (concat "^" (regexp-quote mail-header-separator
) "\n") nil t
)
1521 (replace-match "\n"))
1522 (setq mail-header-separator
""))
1523 (message-sort-headers)
1526 (when (fboundp 'set-buffer-multibyte
)
1527 (let ((s (buffer-string)))
1528 ;; Insert the content into unibyte buffer.
1530 (mm-disable-multibyte)
1532 (let ((gnus-newsgroup-charset (car message-posting-charset
))
1533 gnus-article-prepare-hook gnus-original-article-buffer
1534 gnus-displaying-mime
)
1535 (run-hooks 'gnus-article-decode-hook
)
1536 (let ((gnus-newsgroup-name "dummy")
1537 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1538 (gnus-make-hashtable 5))))
1539 (gnus-article-prepare-display))))
1540 ;; Disable article-mode-map.
1542 (gnus-make-local-hook 'kill-buffer-hook
)
1543 (add-hook 'kill-buffer-hook
1545 (mm-destroy-parts gnus-article-mime-handles
)) nil t
)
1546 (setq buffer-read-only t
)
1547 (local-set-key "q" (lambda () (interactive) (kill-buffer nil
)))
1548 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1552 (widget-button-press (point))))
1553 (local-set-key gnus-mouse-2
1556 (widget-button-press (widget-event-point event
) event
)))
1557 ;; FIXME: Buffer is in article mode, but most tool bar commands won't
1558 ;; work. Maybe only keep the following icons: search, print, quit
1559 (goto-char (point-min))))
1560 (if (and (not (mm-special-display-p (buffer-name mml-preview-buffer
)))
1561 (boundp 'gnus-buffer-configuration
)
1562 (assq 'mml-preview gnus-buffer-configuration
))
1563 (let ((gnus-message-buffer (current-buffer)))
1564 (gnus-configure-windows 'mml-preview
))
1565 (pop-to-buffer mml-preview-buffer
)))
1567 (defun mml-validate ()
1568 "Validate the current MML document."
1572 (defun mml-tweak-part (cont)
1574 (let ((tweak (cdr (assq 'tweak cont
)))
1579 (or (cdr (assoc tweak mml-tweak-function-alist
))
1581 (mml-tweak-type-alist
1582 (let ((alist mml-tweak-type-alist
)
1583 (type (or (cdr (assq 'type cont
)) "text/plain")))
1585 (if (string-match (caar alist
) type
)
1586 (setq func
(cdar alist
)
1588 (setq alist
(cdr alist
)))))))
1592 (let ((alist mml-tweak-sexp-alist
))
1594 (if (eval (caar alist
))
1595 (funcall (cdar alist
) cont
))
1596 (setq alist
(cdr alist
)))))
1599 (defun mml-tweak-externalize-attachments (cont)
1600 "Tweak attached files as external parts."
1601 (let (filename-cons)
1602 (when (and (eq (car cont
) 'part
)
1603 (not (cdr (assq 'buffer cont
)))
1604 (and (setq filename-cons
(assq 'filename cont
))
1605 (not (equal (cdr (assq 'nofile cont
)) "yes"))))
1606 (setcar cont
'external
)
1607 (setcar filename-cons
'name
))))
1611 ;;; mml.el ends here