1 ;;; mml.el --- A package for parsing and validating MML documents
3 ;; Copyright (C) 1998-2014 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
)))
265 (error "Unknown secure mode %s" mode
)))
266 (eval `(mml-insert-tag ,secure-mode
268 ,(if keyfile
"keyfile")
271 (mapcar (lambda (certfile)
272 (list "certfile" certfile
))
274 ,(if recipients
"recipients")
276 ,(if sender
"sender")
279 (goto-char location
)))
280 ((looking-at "<#multipart")
281 (push (nconc (mml-read-tag) (mml-parse-1)) struct
))
282 ((looking-at "<#external")
283 (push (nconc (mml-read-tag) (list (cons 'contents
(mml-read-part))))
286 (if (or (looking-at "<#part") (looking-at "<#mml"))
287 (setq tag
(mml-read-tag)
290 (setq tag
(list 'part
'(type .
"text/plain"))
293 (setq raw
(cdr (assq 'raw tag
))
295 contents
(mml-read-part (eq 'mml
(car tag
)))
300 (intern (downcase (cdr (assq 'charset tag
))))))
302 (mm-find-mime-charset-region point
(point)
304 (when (and (not raw
) (memq nil charsets
))
305 (if (or (memq 'unknown-encoding mml-confirmation-set
)
306 (message-options-get 'unknown-encoding
)
308 Message contains characters with unknown encoding. Really send? ")
309 (message-options-set 'unknown-encoding t
)))
311 (or (memq 'use-ascii mml-confirmation-set
)
312 (message-options-get 'use-ascii
)
313 (and (y-or-n-p "Use ASCII as charset? ")
314 (message-options-set 'use-ascii t
))))
315 (setq charsets
(delq nil charsets
))
317 (error "Edit your message to remove those characters")))
320 (< (length charsets
) 2))
321 (if (or (not no-markup-p
)
322 (string-match "[^ \t\r\n]" contents
))
323 ;; Don't create blank parts.
324 (push (nconc tag
(list (cons 'contents contents
)))
326 (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
327 tag point
(point) use-ascii
)))
329 (not (memq 'multipart mml-confirmation-set
))
330 (not (message-options-get 'multipart
))
331 (not (and (y-or-n-p (format "\
332 A message part needs to be split into %d charset parts. Really send? "
334 (message-options-set 'multipart t
))))
335 (error "Edit your message to use only one charset"))
336 (setq struct
(nconc nstruct struct
)))))))
341 (defun mml-parse-singlepart-with-multiple-charsets
342 (orig-tag beg end
&optional use-ascii
)
345 (narrow-to-region beg end
)
346 (goto-char (point-min))
347 (let ((current (or (mm-mime-charset (mm-charset-after))
348 (and use-ascii
'us-ascii
)))
349 charset struct space newline paragraph
)
351 (setq charset
(mm-mime-charset (mm-charset-after)))
353 ;; The charset remains the same.
354 ((eq charset
'us-ascii
))
355 ((or (and use-ascii
(not charset
))
356 (eq charset current
))
360 ;; The initial charset was ascii.
361 ((eq current
'us-ascii
)
362 (setq current charset
366 ;; We have a change in charsets.
370 (list (cons 'contents
371 (buffer-substring-no-properties
372 beg
(or paragraph newline space
(point))))))
374 (setq beg
(or paragraph newline space
(point))
379 ;; Compute places where it might be nice to break the part.
381 ((memq (following-char) '(? ?
\t))
382 (setq space
(1+ (point))))
383 ((and (eq (following-char) ?
\n)
385 (eq (char-after (1- (point))) ?
\n))
386 (setq paragraph
(point)))
387 ((eq (following-char) ?
\n)
388 (setq newline
(1+ (point)))))
390 ;; Do the final part.
391 (unless (= beg
(point))
392 (push (append orig-tag
393 (list (cons 'contents
394 (buffer-substring-no-properties
399 (defun mml-read-tag ()
400 "Read a tag and return the contents."
401 (let ((orig-point (point))
402 contents name elem val
)
404 (setq name
(buffer-substring-no-properties
405 (point) (progn (forward-sexp 1) (point))))
406 (skip-chars-forward " \t\n")
407 (while (not (looking-at ">[ \t]*\n?"))
408 (setq elem
(buffer-substring-no-properties
409 (point) (progn (forward-sexp 1) (point))))
410 (skip-chars-forward "= \t\n")
411 (setq val
(buffer-substring-no-properties
412 (point) (progn (forward-sexp 1) (point))))
413 (when (string-match "\\`\"" val
)
414 (setq val
(read val
))) ;; inverse of prin1 in mml-insert-tag
415 (push (cons (intern elem
) val
) contents
)
416 (skip-chars-forward " \t\n"))
417 (goto-char (match-end 0))
418 ;; Don't skip the leading space.
419 ;;(skip-chars-forward " \t\n")
420 ;; Put the tag location into the returned contents
421 (setq contents
(append (list (cons 'tag-location orig-point
)) contents
))
422 (cons (intern name
) (nreverse contents
))))
424 (defun mml-buffer-substring-no-properties-except-hard-newlines (start end
)
425 (let ((str (buffer-substring-no-properties start end
))
426 (bufstart start
) tmp
)
427 (while (setq tmp
(text-property-any start end
'hard
't
))
428 (set-text-properties (- tmp bufstart
) (- tmp bufstart -
1)
430 (setq start
(1+ tmp
)))
433 (defun mml-read-part (&optional mml
)
434 "Return the buffer up till the next part, multipart or closing part or multipart.
435 If MML is non-nil, return the buffer up till the correspondent mml tag."
436 (let ((beg (point)) (count 1))
437 ;; If the tag ended at the end of the line, we go to the next line.
438 (when (looking-at "[ \t]*\n")
442 (while (and (> count
0) (not (eobp)))
443 (if (re-search-forward "<#\\(/\\)?mml." nil t
)
444 (setq count
(+ count
(if (match-beginning 1) -
1 1)))
445 (goto-char (point-max))))
446 (mml-buffer-substring-no-properties-except-hard-newlines
449 (match-beginning 0))))
450 (if (re-search-forward
451 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t
)
453 (mml-buffer-substring-no-properties-except-hard-newlines
454 beg
(match-beginning 0))
455 (if (or (not (match-beginning 1))
456 (equal (match-string 2) "multipart"))
457 (goto-char (match-beginning 0))
458 (when (looking-at "[ \t]*\n")
460 (mml-buffer-substring-no-properties-except-hard-newlines
461 beg
(goto-char (point-max)))))))
463 (defvar mml-boundary nil
)
464 (defvar mml-base-boundary
"-=-=")
465 (defvar mml-multipart-number
0)
466 (defvar mml-inhibit-compute-boundary nil
)
468 (defun mml-generate-mime (&optional multipart-type
)
469 "Generate a MIME message based on the current MML document.
470 MULTIPART-TYPE defaults to \"mixed\", but can also
471 be \"related\" or \"alternate\"."
472 (let ((cont (mml-parse))
473 (mml-multipart-number mml-multipart-number
)
474 (options message-options
))
478 (mm-with-multibyte-buffer
479 (setq message-options options
)
480 (if (and (consp (car cont
))
482 (mml-generate-mime-1 (car cont
))
484 (nconc (list 'multipart
(cons 'type
(or multipart-type
"mixed")))
486 (setq options message-options
)
488 (setq message-options options
)))))
490 (defun mml-generate-mime-1 (cont)
491 (let ((mm-use-ultra-safe-encoding
492 (or mm-use-ultra-safe-encoding
(assq 'sign cont
))))
494 (narrow-to-region (point) (point))
495 (mml-tweak-part cont
)
497 ((or (eq (car cont
) 'part
) (eq (car cont
) 'mml
))
498 (let* ((raw (cdr (assq 'raw cont
)))
499 (filename (cdr (assq 'filename cont
)))
500 (type (or (cdr (assq 'type cont
))
502 (or (mm-default-file-encoding filename
)
503 "application/octet-stream")
505 (charset (cdr (assq 'charset cont
)))
506 (coding (mm-charset-to-coding-system charset
))
507 encoding flowed coded
)
508 (cond ((eq coding
'ascii
)
512 ;; The value of `charset' might be a bogus alias that
513 ;; `mm-charset-synonym-alist' provides, like `utf8',
514 ;; so we prefer the MIME charset that Emacs knows for
515 ;; the coding system `coding'.
516 (setq charset
(or (mm-coding-system-to-mime-charset coding
)
517 (intern (downcase charset
))))))
519 (member (car (split-string type
"/")) '("text" "message")))
523 ((cdr (assq 'buffer cont
))
524 (insert-buffer-substring (cdr (assq 'buffer cont
))))
526 (not (equal (cdr (assq 'nofile cont
)) "yes")))
527 (let ((coding-system-for-read coding
))
528 (mm-insert-file-contents filename
)))
529 ((eq 'mml
(car cont
))
530 (insert (cdr (assq 'contents cont
))))
533 (narrow-to-region (point) (point))
534 (insert (cdr (assq 'contents cont
)))
535 ;; Remove quotes from quoted tags.
536 (goto-char (point-min))
537 (while (re-search-forward
538 "<#!+/?\\(part\\|multipart\\|external\\|mml\\|secure\\)"
540 (delete-region (+ (match-beginning 0) 2)
541 (+ (match-beginning 0) 3))))))
543 ((eq (car cont
) 'mml
)
544 (let ((mml-boundary (mml-compute-boundary cont
))
545 ;; It is necessary for the case where this
546 ;; function is called recursively since
547 ;; `m-g-d-t' will be bound to "message/rfc822"
548 ;; when encoding an article to be forwarded.
549 (mml-generate-default-type "text/plain"))
551 ;; Update handle so mml-compute-boundary can
552 ;; detect collisions with the nested parts.
553 (unless mml-inhibit-compute-boundary
554 (setcdr (assoc 'contents cont
) (buffer-string))))
555 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
556 ;; ignore 0x1b, it is part of iso-2022-jp
557 (setq encoding
(mm-body-7-or-8))))
558 ((string= (car (split-string type
"/")) "message")
559 (let ((mm-7bit-chars (concat mm-7bit-chars
"\x1b")))
560 ;; ignore 0x1b, it is part of iso-2022-jp
561 (setq encoding
(mm-body-7-or-8))))
563 ;; Only perform format=flowed filling on text/plain
564 ;; parts where there either isn't a format parameter
565 ;; in the mml tag or it says "flowed" and there
566 ;; actually are hard newlines in the text.
567 (let (use-hard-newlines)
568 (when (and mml-enable-flowed
569 (string= type
"text/plain")
570 (not (string= (cdr (assq 'sign cont
)) "pgp"))
571 (or (null (assq 'format cont
))
572 (string= (cdr (assq 'format cont
))
574 (setq use-hard-newlines
576 (point-min) (point-max) 'hard
't
)))
578 ;; Indicate that `mml-insert-mime-headers' should
579 ;; insert a "; format=flowed" string unless the
580 ;; user has already specified it.
581 (setq flowed
(null (assq 'format cont
)))))
582 ;; Prefer `utf-8' for text/calendar parts.
584 (not (string= type
"text/calendar")))
585 (setq charset
(mm-encode-body charset
))
586 (let ((mm-coding-system-priorities
587 (cons 'utf-8 mm-coding-system-priorities
)))
588 (setq charset
(mm-encode-body))))
589 (setq encoding
(mm-body-encoding
590 charset
(cdr (assq 'encoding cont
))))))
591 (setq coded
(buffer-string)))
592 (mml-insert-mime-headers cont type charset encoding flowed
)
595 (mm-with-unibyte-buffer
597 ((cdr (assq 'buffer cont
))
598 (insert (mm-string-as-unibyte
599 (with-current-buffer (cdr (assq 'buffer cont
))
602 (not (equal (cdr (assq 'nofile cont
)) "yes")))
603 (let ((coding-system-for-read mm-binary-coding-system
))
604 (mm-insert-file-contents filename nil nil nil nil t
))
606 (setq charset
(mm-coding-system-to-mime-charset
607 (mm-find-buffer-file-coding-system
610 (let ((contents (cdr (assq 'contents cont
))))
611 (if (if (featurep 'xemacs
)
612 (string-match "[^\000-\377]" contents
)
613 (mm-multibyte-string-p contents
))
615 (mm-enable-multibyte)
618 (setq charset
(mm-encode-body charset
))))
619 (insert contents
)))))
620 (if (setq encoding
(cdr (assq 'encoding cont
)))
621 (setq encoding
(intern (downcase encoding
))))
622 (setq encoding
(mm-encode-buffer type encoding
)
623 coded
(mm-string-as-multibyte (buffer-string))))
624 (mml-insert-mime-headers cont type charset encoding nil
)
625 (insert "\n" coded
))))
626 ((eq (car cont
) 'external
)
627 (insert "Content-Type: message/external-body")
628 (let ((parameters (mml-parameter-string
629 cont
'(expiration size permission
)))
630 (name (cdr (assq 'name cont
)))
631 (url (cdr (assq 'url cont
))))
633 (setq name
(mml-parse-file-name name
))
635 (mml-insert-parameter
636 (mail-header-encode-parameter "name" name
)
637 "access-type=local-file")
638 (mml-insert-parameter
639 (mail-header-encode-parameter
640 "name" (file-name-nondirectory (nth 2 name
)))
641 (mail-header-encode-parameter "site" (nth 1 name
))
642 (mail-header-encode-parameter
643 "directory" (file-name-directory (nth 2 name
))))
644 (mml-insert-parameter
645 (concat "access-type="
646 (if (member (nth 0 name
) '("ftp@" "anonymous@"))
650 (mml-insert-parameter
651 (mail-header-encode-parameter "url" url
)
654 (mml-insert-parameter-string
655 cont
'(expiration size permission
)))
657 (insert "Content-Type: "
658 (or (cdr (assq 'type cont
))
660 (or (mm-default-file-encoding name
)
661 "application/octet-stream")
664 (insert "Content-ID: " (message-make-message-id) "\n")
665 (insert "Content-Transfer-Encoding: "
666 (or (cdr (assq 'encoding cont
)) "binary"))
668 (insert (or (cdr (assq 'contents cont
))))
670 ((eq (car cont
) 'multipart
)
671 (let* ((type (or (cdr (assq 'type cont
)) "mixed"))
672 (mml-generate-default-type (if (equal type
"digest")
675 (handler (assoc type mml-generate-multipart-alist
)))
677 (funcall (cdr handler
) cont
)
678 ;; No specific handler. Use default one.
679 (let ((mml-boundary (mml-compute-boundary cont
)))
680 (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
682 (if (cdr (assq 'start cont
))
683 (format "; start=\"%s\"\n" (cdr (assq 'start cont
)))
685 (let ((cont cont
) part
)
686 (while (setq part
(pop cont
))
687 ;; Skip `multipart' and attributes.
688 (when (and (consp part
) (consp (cdr part
)))
689 (insert "\n--" mml-boundary
"\n")
690 (mml-generate-mime-1 part
)
691 (goto-char (point-max)))))
692 (insert "\n--" mml-boundary
"--\n")))))
694 (error "Invalid element: %S" cont
)))
695 ;; handle sign & encrypt tags in a semi-smart way.
696 (let ((sign-item (assoc (cdr (assq 'sign cont
)) mml-sign-alist
))
697 (encrypt-item (assoc (cdr (assq 'encrypt cont
))
700 (when (or sign-item encrypt-item
)
701 (when (setq sender
(cdr (assq 'sender cont
)))
702 (message-options-set 'mml-sender sender
)
703 (message-options-set 'message-sender sender
))
704 (if (setq recipients
(cdr (assq 'recipients cont
)))
705 (message-options-set 'message-recipients recipients
))
706 (let ((style (mml-signencrypt-style
707 (first (or sign-item encrypt-item
)))))
708 ;; check if: we're both signing & encrypting, both methods
709 ;; are the same (why would they be different?!), and that
710 ;; the signencrypt style allows for combined operation.
711 (if (and sign-item encrypt-item
(equal (first sign-item
)
712 (first encrypt-item
))
713 (equal style
'combined
))
714 (funcall (nth 1 encrypt-item
) cont t
)
715 ;; otherwise, revert to the old behavior.
717 (funcall (nth 1 sign-item
) cont
))
719 (funcall (nth 1 encrypt-item
) cont
)))))))))
721 (defun mml-compute-boundary (cont)
722 "Return a unique boundary that does not exist in CONT."
723 (let ((mml-boundary (funcall mml-boundary-function
724 (incf mml-multipart-number
))))
725 (unless mml-inhibit-compute-boundary
726 ;; This function tries again and again until it has found
727 ;; a unique boundary.
728 (while (not (catch 'not-unique
729 (mml-compute-boundary-1 cont
)))))
732 (defun mml-compute-boundary-1 (cont)
734 ((member (car cont
) '(part mml
))
735 (mm-with-multibyte-buffer
736 (let ((mml-inhibit-compute-boundary t
)
737 (mml-multipart-number 0)
738 mml-sign-alist mml-encrypt-alist
)
739 (mml-generate-mime-1 cont
))
740 (goto-char (point-min))
741 (when (re-search-forward (concat "^--" (regexp-quote mml-boundary
))
743 (setq mml-boundary
(funcall mml-boundary-function
744 (incf mml-multipart-number
)))
745 (throw 'not-unique nil
))))
746 ((eq (car cont
) 'multipart
)
747 (mapc 'mml-compute-boundary-1
(cddr cont
))))
750 (defun mml-make-boundary (number)
751 (concat (make-string (% number
60) ?
=)
757 (defun mml-content-disposition (type &optional filename
)
758 "Return a default disposition name suitable to TYPE or FILENAME."
759 (let ((defs mml-content-disposition-alist
)
760 disposition def types
)
761 (while (and (not disposition
) defs
)
762 (setq def
(pop defs
))
763 (cond ((stringp (car def
))
765 (string-match (car def
) filename
))
766 (setq disposition
(cdr def
))))
768 (when (string= (car (setq types
(split-string type
"/")))
770 (setq type
(cadr types
)
772 (while (and (not disposition
) types
)
773 (setq def
(pop types
))
774 (when (or (eq (car def
) t
) (string= type
(car def
)))
775 (setq disposition
(cdr def
))))))
777 (when (or (eq (car def
) t
) (string= type
(car def
)))
778 (setq disposition
(cdr def
))))))
779 (or disposition
"attachment")))
781 (defun mml-insert-mime-headers (cont type charset encoding flowed
)
782 (let (parameters id disposition description
)
784 (mml-parameter-string
785 cont mml-content-type-parameters
))
789 (not (equal type mml-generate-default-type
))
790 mml-insert-mime-headers-always
)
791 (when (consp charset
)
793 "Can't encode a part with several charsets"))
794 (insert "Content-Type: " type
)
796 (mml-insert-parameter
797 (mail-header-encode-parameter "charset" (symbol-name charset
))))
799 (mml-insert-parameter "format=flowed"))
801 (mml-insert-parameter-string
802 cont mml-content-type-parameters
))
804 (when (setq id
(cdr (assq 'id cont
)))
805 (insert "Content-ID: " id
"\n"))
807 (mml-parameter-string
808 cont mml-content-disposition-parameters
))
809 (when (or (setq disposition
(cdr (assq 'disposition cont
)))
811 (insert "Content-Disposition: "
813 (mml-content-disposition type
(cdr (assq 'filename cont
)))))
815 (mml-insert-parameter-string
816 cont mml-content-disposition-parameters
))
818 (unless (eq encoding
'7bit
)
819 (insert (format "Content-Transfer-Encoding: %s\n" encoding
)))
820 (when (setq description
(cdr (assq 'description cont
)))
821 (insert "Content-Description: ")
822 (setq description
(prog1
824 (insert description
"\n")))
825 (mail-encode-encoded-word-region description
(point)))))
827 (defun mml-parameter-string (cont types
)
830 (while (setq type
(pop types
))
831 (when (setq value
(cdr (assq type cont
)))
832 ;; Strip directory component from the filename parameter.
833 (when (eq type
'filename
)
834 (setq value
(file-name-nondirectory value
)))
835 (setq string
(concat string
"; "
836 (mail-header-encode-parameter
837 (symbol-name type
) value
)))))
838 (when (not (zerop (length string
)))
841 (defun mml-insert-parameter-string (cont types
)
843 (while (setq type
(pop types
))
844 (when (setq value
(cdr (assq type cont
)))
845 ;; Strip directory component from the filename parameter.
846 (when (eq type
'filename
)
847 (setq value
(file-name-nondirectory value
)))
848 (mml-insert-parameter
849 (mail-header-encode-parameter
850 (symbol-name type
) value
))))))
852 (defvar ange-ftp-name-format
)
853 (defvar efs-path-regexp
)
855 (defun mml-parse-file-name (path)
856 (if (if (boundp 'efs-path-regexp
)
857 (string-match efs-path-regexp path
)
858 (if (boundp 'ange-ftp-name-format
)
859 (string-match (car ange-ftp-name-format
) path
)))
860 (list (match-string 1 path
) (match-string 2 path
)
861 (substring path
(1+ (match-end 2))))
864 (defun mml-insert-buffer (buffer)
865 "Insert BUFFER at point and quote any MML markup."
867 (narrow-to-region (point) (point))
868 (insert-buffer-substring buffer
)
869 (mml-quote-region (point-min) (point-max))
870 (goto-char (point-max))))
873 ;;; Transforming MIME to MML
876 ;; message-narrow-to-head autoloads message.
877 (declare-function message-remove-header
"message"
878 (header &optional is-regexp first reverse
))
880 (defun mime-to-mml (&optional handles
)
881 "Translate the current buffer (which should be a message) into MML.
882 If HANDLES is non-nil, use it instead reparsing the buffer."
883 ;; First decode the head.
885 (message-narrow-to-head)
886 (let ((rfc2047-quote-decoded-words-containing-tspecials t
))
887 (mail-decode-encoded-word-region (point-min) (point-max))))
889 (setq handles
(mm-dissect-buffer t
)))
890 (goto-char (point-min))
891 (search-forward "\n\n" nil t
)
892 (delete-region (point) (point-max))
893 (if (stringp (car handles
))
894 (mml-insert-mime handles
)
895 (mml-insert-mime handles t
))
896 (mm-destroy-parts handles
)
898 (message-narrow-to-head)
899 ;; Remove them, they are confusing.
900 (message-remove-header "Content-Type")
901 (message-remove-header "MIME-Version")
902 (message-remove-header "Content-Disposition")
903 (message-remove-header "Content-Transfer-Encoding")))
905 (autoload 'message-encode-message-body
"message")
906 (declare-function message-narrow-to-headers-or-head
"message" ())
909 (defun mml-to-mime ()
910 "Translate the current buffer from MML to MIME."
911 ;; `message-encode-message-body' will insert an encoded Content-Description
912 ;; header in the message header if the body contains a single part
913 ;; that is specified by a user with a MML tag containing a description
914 ;; token. So, we encode the message header first to prevent the encoded
915 ;; Content-Description header from being encoded again.
917 (message-narrow-to-headers-or-head)
918 ;; Skip past any From_ headers.
919 (while (looking-at "From ")
921 (let ((mail-parse-charset message-default-charset
))
922 (mail-encode-encoded-word-buffer)))
923 (message-encode-message-body))
925 (defun mml-insert-mime (handle &optional no-markup
)
926 (let (textp buffer mmlp
)
927 ;; Determine type and stuff.
928 (unless (stringp (car handle
))
929 (unless (setq textp
(equal (mm-handle-media-supertype handle
) "text"))
930 (with-current-buffer (setq buffer
(mml-generate-new-buffer " *mml*"))
931 (if (eq (mail-content-type-get (mm-handle-type handle
) 'charset
)
933 ;; A part that mm-uu dissected from a non-MIME message
934 ;; because of `gnus-article-emulate-mime'.
936 (mm-enable-multibyte)
937 (insert-buffer-substring (mm-handle-buffer handle
)))
938 (mm-insert-part handle
'no-cache
)
939 (if (setq mmlp
(equal (mm-handle-media-type handle
)
943 (mml-insert-mml-markup handle nil t t
)
944 (unless (and no-markup
945 (equal (mm-handle-media-type handle
) "text/plain"))
946 (mml-insert-mml-markup handle buffer textp
)))
949 (insert-buffer-substring buffer
)
950 (goto-char (point-max))
951 (insert "<#/mml>\n"))
952 ((stringp (car handle
))
953 (mapc 'mml-insert-mime
(cdr handle
))
954 (insert "<#/multipart>\n"))
956 (let ((charset (mail-content-type-get
957 (mm-handle-type handle
) 'charset
))
959 (if (eq charset
'gnus-decoded
)
960 (mm-insert-part handle
)
961 (insert (mm-decode-string (mm-get-part handle
) charset
)))
962 (mml-quote-region start
(point)))
963 (goto-char (point-max)))
965 (insert "<#/part>\n")))))
967 (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp
)
968 "Take a MIME handle and insert an MML tag."
969 (if (stringp (car handle
))
971 (insert "<#multipart type=" (mm-handle-media-subtype handle
))
972 (let ((start (mm-handle-multipart-ctl-parameter handle
'start
)))
974 (insert " start=\"" start
"\"")))
977 (insert "<#mml type=" (mm-handle-media-type handle
))
978 (insert "<#part type=" (mm-handle-media-type handle
)))
979 (dolist (elem (append (cdr (mm-handle-type handle
))
980 (cdr (mm-handle-disposition handle
))))
981 (unless (symbolp (cdr elem
))
982 (insert " " (symbol-name (car elem
)) "=\"" (cdr elem
) "\"")))
983 (when (mm-handle-id handle
)
984 (insert " id=\"" (mm-handle-id handle
) "\""))
985 (when (mm-handle-disposition handle
)
986 (insert " disposition=" (car (mm-handle-disposition handle
))))
988 (insert " buffer=\"" (buffer-name buffer
) "\""))
990 (insert " nofile=yes"))
991 (when (mm-handle-description handle
)
992 (insert " description=\"" (mm-handle-description handle
) "\""))
995 (defun mml-insert-parameter (&rest parameters
)
996 "Insert PARAMETERS in a nice way."
998 (dolist (param parameters
)
1000 (setq start
(point))
1005 (if (> (current-column) 76)
1009 (goto-char (1+ end
)))
1013 ;;; Mode for inserting and editing MML forms
1016 (defvar mml-mode-map
1017 (let ((sign (make-sparse-keymap))
1018 (encrypt (make-sparse-keymap))
1019 (signpart (make-sparse-keymap))
1020 (encryptpart (make-sparse-keymap))
1021 (map (make-sparse-keymap))
1022 (main (make-sparse-keymap)))
1023 (define-key map
"\C-s" 'mml-secure-message-sign
)
1024 (define-key map
"\C-c" 'mml-secure-message-encrypt
)
1025 (define-key map
"\C-e" 'mml-secure-message-sign-encrypt
)
1026 (define-key map
"\C-p\C-s" 'mml-secure-sign
)
1027 (define-key map
"\C-p\C-c" 'mml-secure-encrypt
)
1028 (define-key sign
"p" 'mml-secure-message-sign-pgpmime
)
1029 (define-key sign
"o" 'mml-secure-message-sign-pgp
)
1030 (define-key sign
"s" 'mml-secure-message-sign-smime
)
1031 (define-key signpart
"p" 'mml-secure-sign-pgpmime
)
1032 (define-key signpart
"o" 'mml-secure-sign-pgp
)
1033 (define-key signpart
"s" 'mml-secure-sign-smime
)
1034 (define-key encrypt
"p" 'mml-secure-message-encrypt-pgpmime
)
1035 (define-key encrypt
"o" 'mml-secure-message-encrypt-pgp
)
1036 (define-key encrypt
"s" 'mml-secure-message-encrypt-smime
)
1037 (define-key encryptpart
"p" 'mml-secure-encrypt-pgpmime
)
1038 (define-key encryptpart
"o" 'mml-secure-encrypt-pgp
)
1039 (define-key encryptpart
"s" 'mml-secure-encrypt-smime
)
1040 (define-key map
"\C-n" 'mml-unsecure-message
)
1041 (define-key map
"f" 'mml-attach-file
)
1042 (define-key map
"b" 'mml-attach-buffer
)
1043 (define-key map
"e" 'mml-attach-external
)
1044 (define-key map
"q" 'mml-quote-region
)
1045 (define-key map
"m" 'mml-insert-multipart
)
1046 (define-key map
"p" 'mml-insert-part
)
1047 (define-key map
"v" 'mml-validate
)
1048 (define-key map
"P" 'mml-preview
)
1049 (define-key map
"s" sign
)
1050 (define-key map
"S" signpart
)
1051 (define-key map
"c" encrypt
)
1052 (define-key map
"C" encryptpart
)
1053 ;;(define-key map "n" 'mml-narrow-to-part)
1054 ;; `M-m' conflicts with `back-to-indentation'.
1055 ;; (define-key main "\M-m" map)
1056 (define-key main
"\C-c\C-m" map
)
1060 mml-menu mml-mode-map
""
1062 ["Attach File..." mml-attach-file
1063 ,@(if (featurep 'xemacs
) '(t)
1064 '(:help
"Attach a file at point"))]
1065 ["Attach Buffer..." mml-attach-buffer
1066 ,@(if (featurep 'xemacs
) '(t)
1067 '(:help
"Attach a buffer to the outgoing message"))]
1068 ["Attach External..." mml-attach-external
1069 ,@(if (featurep 'xemacs
) '(t)
1070 '(:help
"Attach reference to an external file"))]
1071 ;; FIXME: Is it possible to do this without using
1072 ;; `gnus-gcc-externalize-attachments'?
1073 ["Externalize Attachments"
1076 (if (not (and (boundp 'gnus-gcc-externalize-attachments
)
1077 (memq gnus-gcc-externalize-attachments
1079 ;; Stupid workaround for XEmacs not honoring :visible.
1080 (message "Can't handle this value of `gnus-gcc-externalize-attachments'")
1081 (setq gnus-gcc-externalize-attachments
1082 (not gnus-gcc-externalize-attachments
))
1083 (message "gnus-gcc-externalize-attachments is `%s'."
1084 gnus-gcc-externalize-attachments
)))
1085 ;; XEmacs barfs on :visible.
1086 ,@(if (featurep 'xemacs
) nil
1087 '(:visible
(and (boundp 'gnus-gcc-externalize-attachments
)
1088 (memq gnus-gcc-externalize-attachments
1091 :selected gnus-gcc-externalize-attachments
1092 ,@(if (featurep 'xemacs
) nil
1093 '(:help
"Save attachments as external parts in Gcc copies"))]
1096 ("Change Security Method"
1098 (lambda () (interactive) (setq mml-secure-method
"pgpmime"))
1099 ,@(if (featurep 'xemacs
) nil
1100 '(:help
"Set Security Method to PGP/MIME"))
1102 :selected
(equal mml-secure-method
"pgpmime") ]
1104 (lambda () (interactive) (setq mml-secure-method
"smime"))
1105 ,@(if (featurep 'xemacs
) nil
1106 '(:help
"Set Security Method to S/MIME"))
1108 :selected
(equal mml-secure-method
"smime") ]
1110 (lambda () (interactive) (setq mml-secure-method
"pgp"))
1111 ,@(if (featurep 'xemacs
) nil
1112 '(:help
"Set Security Method to inline PGP"))
1114 :selected
(equal mml-secure-method
"pgp") ] )
1116 ["Sign Message" mml-secure-message-sign t
]
1117 ["Encrypt Message" mml-secure-message-encrypt t
]
1118 ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t
]
1119 ["Encrypt/Sign off" mml-unsecure-message
1120 ,@(if (featurep 'xemacs
) '(t)
1121 '(:help
"Don't Encrypt/Sign Message"))]
1122 ;; Do we have separate encrypt and encrypt/sign commands for parts?
1123 ["Sign Part" mml-secure-sign t
]
1124 ["Encrypt Part" mml-secure-encrypt t
]
1126 ;; Maybe we could remove these, because people who write MML most probably
1127 ;; don't use the menu:
1128 ["Insert Part..." mml-insert-part
1129 :active
(message-in-body-p)]
1130 ["Insert Multipart..." mml-insert-multipart
1131 :active
(message-in-body-p)]
1133 ;;["Narrow" mml-narrow-to-part t]
1134 ["Quote MML in region" mml-quote-region
1135 :active
(message-mark-active-p)
1136 ,@(if (featurep 'xemacs
) nil
1137 '(:help
"Quote MML tags in region"))]
1138 ["Validate MML" mml-validate t
]
1139 ["Preview" mml-preview t
]
1141 ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
1142 ,@(if (featurep 'xemacs
) '(t)
1143 '(:help
"Display the Emacs MIME manual"))]
1144 ["PGG manual" (lambda () (interactive) (message-info mml2015-use
))
1145 ;; XEmacs barfs on :visible.
1146 ,@(if (featurep 'xemacs
) nil
1147 '(:visible
(and (boundp 'mml2015-use
) (equal mml2015-use
'pgg
))))
1148 ,@(if (featurep 'xemacs
) '(t)
1149 '(:help
"Display the PGG manual"))]
1150 ["EasyPG manual" (lambda () (interactive) (require 'mml2015
) (message-info mml2015-use
))
1151 ;; XEmacs barfs on :visible.
1152 ,@(if (featurep 'xemacs
) nil
1153 '(:visible
(and (boundp 'mml2015-use
) (equal mml2015-use
'epg
))))
1154 ,@(if (featurep 'xemacs
) '(t)
1155 '(:help
"Display the EasyPG manual"))]))
1157 (define-minor-mode mml-mode
1158 "Minor mode for editing MML.
1159 MML is the MIME Meta Language, a minor mode for composing MIME articles.
1160 See Info node `(emacs-mime)Composing'.
1163 :lighter
" MML" :keymap mml-mode-map
1165 (easy-menu-add mml-menu mml-mode-map
)
1166 (when (boundp 'dnd-protocol-alist
)
1167 (set (make-local-variable 'dnd-protocol-alist
)
1168 (append mml-dnd-protocol-alist dnd-protocol-alist
)))))
1171 ;;; Helper functions for reading MIME stuff from the minibuffer and
1172 ;;; inserting stuff to the buffer.
1175 (defcustom mml-default-directory mm-default-directory
1176 "The default directory where mml will find files.
1177 If not set, `default-directory' will be used."
1178 :type
'(choice directory
(const :tag
"Default" nil
))
1179 :version
"23.1" ;; No Gnus
1182 (defun mml-minibuffer-read-file (prompt)
1183 (let* ((completion-ignored-extensions nil
)
1184 (file (read-file-name prompt
1185 (or mml-default-directory default-directory
)
1187 ;; Prevent some common errors. This is inspired by similar code in
1189 (when (file-directory-p file
)
1190 (error "%s is a directory, cannot attach" file
))
1191 (unless (file-exists-p file
)
1192 (error "No such file: %s" file
))
1193 (unless (file-readable-p file
)
1194 (error "Permission denied: %s" file
))
1197 (declare-function mailcap-parse-mimetypes
"mailcap" (&optional path force
))
1198 (declare-function mailcap-mime-types
"mailcap" ())
1200 (defun mml-minibuffer-read-type (name &optional default
)
1202 (mailcap-parse-mimetypes)
1203 (let* ((default (or default
1204 (mm-default-file-encoding name
)
1205 ;; Perhaps here we should check what the file
1206 ;; looks like, and offer text/plain if it looks
1208 "application/octet-stream"))
1209 (string (gnus-completing-read
1211 (mailcap-mime-types)
1212 nil nil nil default
)))
1213 (if (not (equal string
""))
1217 (defun mml-minibuffer-read-description (&optional default
)
1218 (let ((description (read-string "One line description: " default
)))
1219 (when (string-match "\\`[ \t]*\\'" description
)
1220 (setq description nil
))
1223 (defun mml-minibuffer-read-disposition (type &optional default filename
)
1225 (setq default
(mml-content-disposition type filename
)))
1226 (let ((disposition (gnus-completing-read
1228 '("attachment" "inline")
1229 t nil nil default
)))
1230 (if (not (equal disposition
""))
1234 (defun mml-quote-region (beg end
)
1235 "Quote the MML tags in the region."
1239 ;; Temporarily narrow the region to defend from changes
1240 ;; invalidating END.
1241 (narrow-to-region beg end
)
1242 (goto-char (point-min))
1244 (while (re-search-forward
1245 "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)" nil t
)
1246 ;; Insert ! after the #.
1247 (goto-char (+ (match-beginning 0) 2))
1250 (defun mml-insert-tag (name &rest plist
)
1251 "Insert an MML tag described by NAME and PLIST."
1252 (when (symbolp name
)
1253 (setq name
(symbol-name name
)))
1256 (let ((key (pop plist
))
1257 (value (pop plist
)))
1259 ;; Quote VALUE if it contains suspicious characters.
1260 (when (string-match "[\"'\\~/*;() \t\n]" value
)
1261 (setq value
(with-output-to-string
1262 (let (print-escape-nonascii)
1264 (insert (format " %s=%s" key value
)))))
1267 (defun mml-insert-empty-tag (name &rest plist
)
1268 "Insert an empty MML tag described by NAME and PLIST."
1269 (when (symbolp name
)
1270 (setq name
(symbol-name name
)))
1271 (apply #'mml-insert-tag name plist
)
1272 (insert "<#/" name
">\n"))
1274 ;;; Attachment functions.
1276 (defcustom mml-dnd-protocol-alist
1277 '(("^file:///" . mml-dnd-attach-file
)
1278 ("^file://" . dnd-open-file
)
1279 ("^file:" . mml-dnd-attach-file
))
1280 "The functions to call when a drop in `mml-mode' is made.
1281 See `dnd-protocol-alist' for more information. When nil, behave
1282 as in other buffers."
1283 :type
'(choice (repeat (cons (regexp) (function)))
1284 (const :tag
"Behave as in other buffers" nil
))
1285 :version
"22.1" ;; Gnus 5.10.9
1288 (defcustom mml-dnd-attach-options nil
1289 "Which options should be queried when attaching a file via drag and drop.
1291 If it is a list, valid members are `type', `description' and
1292 `disposition'. `disposition' implies `type'. If it is nil,
1293 don't ask for options. If it is t, ask the user whether or not
1294 to specify options."
1296 (const :tag
"None" nil
)
1297 (const :tag
"Query" t
)
1298 (list :value
(type description disposition
)
1302 (const disposition
))))
1303 :version
"22.1" ;; Gnus 5.10.9
1307 (defun mml-attach-file (file &optional type description disposition
)
1308 "Attach a file to the outgoing MIME message.
1309 The file is not inserted or encoded until you send the message with
1310 `\\[message-send-and-exit]' or `\\[message-send]' in Message mode,
1311 or `\\[mail-send-and-exit]' or `\\[mail-send]' in Mail mode.
1313 FILE is the name of the file to attach. TYPE is its
1314 content-type, a string of the form \"type/subtype\". DESCRIPTION
1315 is a one-line description of the attachment. The DISPOSITION
1316 specifies how the attachment is intended to be displayed. It can
1317 be either \"inline\" (displayed automatically within the message
1318 body) or \"attachment\" (separate from the body)."
1320 (let* ((file (mml-minibuffer-read-file "Attach file: "))
1321 (type (mml-minibuffer-read-type file
))
1322 (description (mml-minibuffer-read-description))
1323 (disposition (mml-minibuffer-read-disposition type nil file
)))
1324 (list file type description disposition
)))
1325 ;; If in the message header, attach at the end and leave point unchanged.
1326 (let ((head (unless (message-in-body-p) (point))))
1327 (if head
(goto-char (point-max)))
1328 (mml-insert-empty-tag 'part
1330 ;; icicles redefines read-file-name and returns a
1331 ;; string w/ text properties :-/
1332 'filename
(mm-substring-no-properties file
)
1333 'disposition
(or disposition
"attachment")
1334 'description description
)
1335 ;; When using Mail mode, make sure it does the mime encoding
1336 ;; when you send the message.
1337 (or (eq mail-user-agent
'message-user-agent
)
1338 (setq mail-encode-mml t
))
1340 (unless (pos-visible-in-window-p)
1341 (message "The file \"%s\" has been attached at the end of the message"
1342 (file-name-nondirectory file
)))
1345 (defun mml-dnd-attach-file (uri action
)
1346 "Attach a drag and drop file.
1348 Ask for type, description or disposition according to
1349 `mml-dnd-attach-options'."
1350 (let ((file (dnd-get-local-file-name uri t
)))
1351 (when (and file
(file-regular-p file
))
1352 (let ((mml-dnd-attach-options mml-dnd-attach-options
)
1353 type description disposition
)
1354 (setq mml-dnd-attach-options
1355 (when (and (eq mml-dnd-attach-options t
)
1358 "Use default type, disposition and description? ")))
1359 '(type description disposition
)))
1360 (when (or (memq 'type mml-dnd-attach-options
)
1361 (memq 'disposition mml-dnd-attach-options
))
1362 (setq type
(mml-minibuffer-read-type file
)))
1363 (when (memq 'description mml-dnd-attach-options
)
1364 (setq description
(mml-minibuffer-read-description)))
1365 (when (memq 'disposition mml-dnd-attach-options
)
1366 (setq disposition
(mml-minibuffer-read-disposition type nil file
)))
1367 (mml-attach-file file type description disposition
)))))
1369 (defun mml-attach-buffer (buffer &optional type description disposition
)
1370 "Attach a buffer to the outgoing MIME message.
1371 BUFFER is the name of the buffer to attach. See
1372 `mml-attach-file' for details of operation."
1374 (let* ((buffer (read-buffer "Attach buffer: "))
1375 (type (mml-minibuffer-read-type buffer
"text/plain"))
1376 (description (mml-minibuffer-read-description))
1377 (disposition (mml-minibuffer-read-disposition type nil
)))
1378 (list buffer type description disposition
)))
1379 ;; If in the message header, attach at the end and leave point unchanged.
1380 (let ((head (unless (message-in-body-p) (point))))
1381 (if head
(goto-char (point-max)))
1382 (mml-insert-empty-tag 'part
'type type
'buffer buffer
1383 'disposition disposition
1384 'description description
)
1385 ;; When using Mail mode, make sure it does the mime encoding
1386 ;; when you send the message.
1387 (or (eq mail-user-agent
'message-user-agent
)
1388 (setq mail-encode-mml t
))
1390 (unless (pos-visible-in-window-p)
1392 "The buffer \"%s\" has been attached at the end of the message"
1396 (defun mml-attach-external (file &optional type description
)
1397 "Attach an external file into the buffer.
1398 FILE is an ange-ftp/efs specification of the part location.
1399 TYPE is the MIME type to use."
1401 (let* ((file (mml-minibuffer-read-file "Attach external file: "))
1402 (type (mml-minibuffer-read-type file
))
1403 (description (mml-minibuffer-read-description)))
1404 (list file type description
)))
1405 ;; If in the message header, attach at the end and leave point unchanged.
1406 (let ((head (unless (message-in-body-p) (point))))
1407 (if head
(goto-char (point-max)))
1408 (mml-insert-empty-tag 'external
'type type
'name file
1409 'disposition
"attachment" 'description description
)
1410 ;; When using Mail mode, make sure it does the mime encoding
1411 ;; when you send the message.
1412 (or (eq mail-user-agent
'message-user-agent
)
1413 (setq mail-encode-mml t
))
1415 (unless (pos-visible-in-window-p)
1416 (message "The file \"%s\" has been attached at the end of the message"
1417 (file-name-nondirectory file
)))
1420 (defun mml-insert-multipart (&optional type
)
1421 (interactive (if (message-in-body-p)
1422 (list (gnus-completing-read "Multipart type"
1423 '("mixed" "alternative"
1425 "signed" "encrypted")
1427 (error "Use this command in the message body")))
1429 (setq type
"mixed"))
1430 (mml-insert-empty-tag "multipart" 'type type
)
1431 ;; When using Mail mode, make sure it does the mime encoding
1432 ;; when you send the message.
1433 (or (eq mail-user-agent
'message-user-agent
)
1434 (setq mail-encode-mml t
))
1437 (defun mml-insert-part (&optional type
)
1438 (interactive (if (message-in-body-p)
1439 (list (mml-minibuffer-read-type ""))
1440 (error "Use this command in the message body")))
1441 ;; When using Mail mode, make sure it does the mime encoding
1442 ;; when you send the message.
1443 (or (eq mail-user-agent
'message-user-agent
)
1444 (setq mail-encode-mml t
))
1445 (mml-insert-tag 'part
'type type
'disposition
"inline")
1447 (mml-insert-tag '/part
)))
1449 (declare-function message-subscribed-p
"message" ())
1450 (declare-function message-make-mail-followup-to
"message"
1451 (&optional only-show-subscribed
))
1452 (declare-function message-position-on-field
"message" (header &rest afters
))
1454 (defun mml-preview-insert-mail-followup-to ()
1455 "Insert a Mail-Followup-To header before previewing an article.
1456 Should be adopted if code in `message-send-mail' is changed."
1457 (when (and (message-mail-p)
1458 (message-subscribed-p)
1459 (not (mail-fetch-field "mail-followup-to"))
1460 (message-make-mail-followup-to))
1461 (message-position-on-field "Mail-Followup-To" "X-Draft-From")
1462 (insert (message-make-mail-followup-to))))
1464 (defvar mml-preview-buffer nil
)
1466 (autoload 'gnus-make-hashtable
"gnus-util")
1467 (autoload 'widget-button-press
"wid-edit" nil t
)
1468 (declare-function widget-event-point
"wid-edit" (event))
1469 ;; If gnus-buffer-configuration is bound this is loaded.
1470 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
1471 ;; Called after message-mail-p, which autoloads message.
1472 (declare-function message-news-p
"message" ())
1473 (declare-function message-options-set-recipient
"message" ())
1474 (declare-function message-generate-headers
"message" (headers))
1475 (declare-function message-sort-headers
"message" ())
1477 (defun mml-preview (&optional raw
)
1478 "Display current buffer with Gnus, in a new buffer.
1479 If RAW, display a raw encoded MIME message.
1481 The window layout for the preview buffer is controlled by the variables
1482 `special-display-buffer-names', `special-display-regexps', or
1483 `gnus-buffer-configuration' (the first match made will be used),
1484 or the `pop-to-buffer' function."
1486 (setq mml-preview-buffer
(generate-new-buffer
1487 (concat (if raw
"*Raw MIME preview of "
1488 "*MIME preview of ") (buffer-name))))
1489 (require 'gnus-msg
) ; for gnus-setup-posting-charset
1491 (let* ((buf (current-buffer))
1492 (article-editing (eq major-mode
'gnus-article-edit-mode
))
1493 (message-options message-options
)
1494 (message-this-is-mail (message-mail-p))
1495 (message-this-is-news (message-news-p))
1496 (message-posting-charset (or (gnus-setup-posting-charset
1498 (message-narrow-to-headers-or-head)
1499 (message-fetch-field "Newsgroups")))
1500 message-posting-charset
)))
1501 (message-options-set-recipient)
1502 (when (boundp 'gnus-buffers
)
1503 (push mml-preview-buffer gnus-buffers
))
1506 (set-buffer mml-preview-buffer
)
1508 (insert-buffer-substring buf
))
1509 (mml-preview-insert-mail-followup-to)
1510 (let ((message-deletable-headers (if (message-news-p)
1512 message-deletable-headers
))
1513 (mail-header-separator (if article-editing
1515 mail-header-separator
)))
1516 (message-generate-headers
1517 (copy-sequence (if (message-news-p)
1518 message-required-news-headers
1519 message-required-mail-headers
)))
1520 (unless article-editing
1521 (if (re-search-forward
1522 (concat "^" (regexp-quote mail-header-separator
) "\n") nil t
)
1523 (replace-match "\n"))
1524 (setq mail-header-separator
""))
1525 (message-sort-headers)
1528 (when (fboundp 'set-buffer-multibyte
)
1529 (let ((s (buffer-string)))
1530 ;; Insert the content into unibyte buffer.
1532 (mm-disable-multibyte)
1534 (let ((gnus-newsgroup-charset (car message-posting-charset
))
1535 gnus-article-prepare-hook gnus-original-article-buffer
1536 gnus-displaying-mime
)
1537 (run-hooks 'gnus-article-decode-hook
)
1538 (let ((gnus-newsgroup-name "dummy")
1539 (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
1540 (gnus-make-hashtable 5))))
1541 (gnus-article-prepare-display))))
1542 ;; Disable article-mode-map.
1544 (gnus-make-local-hook 'kill-buffer-hook
)
1545 (add-hook 'kill-buffer-hook
1547 (mm-destroy-parts gnus-article-mime-handles
)) nil t
)
1548 (setq buffer-read-only t
)
1549 (local-set-key "q" (lambda () (interactive) (kill-buffer nil
)))
1550 (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
1554 (widget-button-press (point))))
1555 (local-set-key gnus-mouse-2
1558 (widget-button-press (widget-event-point event
) event
)))
1559 ;; FIXME: Buffer is in article mode, but most tool bar commands won't
1560 ;; work. Maybe only keep the following icons: search, print, quit
1561 (goto-char (point-min))))
1562 (if (and (not (mm-special-display-p (buffer-name mml-preview-buffer
)))
1563 (boundp 'gnus-buffer-configuration
)
1564 (assq 'mml-preview gnus-buffer-configuration
))
1565 (let ((gnus-message-buffer (current-buffer)))
1566 (gnus-configure-windows 'mml-preview
))
1567 (pop-to-buffer mml-preview-buffer
)))
1569 (defun mml-validate ()
1570 "Validate the current MML document."
1574 (defun mml-tweak-part (cont)
1576 (let ((tweak (cdr (assq 'tweak cont
)))
1581 (or (cdr (assoc tweak mml-tweak-function-alist
))
1583 (mml-tweak-type-alist
1584 (let ((alist mml-tweak-type-alist
)
1585 (type (or (cdr (assq 'type cont
)) "text/plain")))
1587 (if (string-match (caar alist
) type
)
1588 (setq func
(cdar alist
)
1590 (setq alist
(cdr alist
)))))))
1594 (let ((alist mml-tweak-sexp-alist
))
1596 (if (eval (caar alist
))
1597 (funcall (cdar alist
) cont
))
1598 (setq alist
(cdr alist
)))))
1601 (defun mml-tweak-externalize-attachments (cont)
1602 "Tweak attached files as external parts."
1603 (let (filename-cons)
1604 (when (and (eq (car cont
) 'part
)
1605 (not (cdr (assq 'buffer cont
)))
1606 (and (setq filename-cons
(assq 'filename cont
))
1607 (not (equal (cdr (assq 'nofile cont
)) "yes"))))
1608 (setcar cont
'external
)
1609 (setcar filename-cons
'name
))))
1613 ;;; mml.el ends here