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