Commit | Line | Data |
---|---|---|
23f87bed MB |
1 | ;;; mml.el --- A package for parsing and validating MML documents |
2 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 | |
3 | ;; Free Software Foundation, Inc. | |
c113de23 GM |
4 | |
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
6 | ;; This file is part of GNU Emacs. | |
7 | ||
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 2, or (at your option) | |
11 | ;; any later version. | |
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 | |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
17 | ||
18 | ;; You should have received a copy of the GNU General Public License | |
19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
20 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | ;; Boston, MA 02111-1307, USA. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;;; Code: | |
26 | ||
27 | (require 'mm-util) | |
28 | (require 'mm-bodies) | |
29 | (require 'mm-encode) | |
30 | (require 'mm-decode) | |
23f87bed | 31 | (require 'mml-sec) |
160ff4e5 | 32 | (eval-when-compile (require 'cl)) |
c113de23 GM |
33 | |
34 | (eval-and-compile | |
35 | (autoload 'message-make-message-id "message") | |
36 | (autoload 'gnus-setup-posting-charset "gnus-msg") | |
88818fbe | 37 | (autoload 'gnus-add-minor-mode "gnus-ems") |
23f87bed | 38 | (autoload 'gnus-make-local-hook "gnus-util") |
c113de23 | 39 | (autoload 'message-fetch-field "message") |
23f87bed | 40 | (autoload 'fill-flowed-encode "flow-fill") |
c113de23 GM |
41 | (autoload 'message-posting-charset "message")) |
42 | ||
23f87bed MB |
43 | (defcustom mml-content-type-parameters |
44 | '(name access-type expiration size permission format) | |
45 | "*A list of acceptable parameters in MML tag. | |
46 | These parameters are generated in Content-Type header if exists." | |
a08b59c9 | 47 | :version "21.4" |
23f87bed MB |
48 | :type '(repeat (symbol :tag "Parameter")) |
49 | :group 'message) | |
50 | ||
51 | (defcustom mml-content-disposition-parameters | |
52 | '(filename creation-date modification-date read-date) | |
53 | "*A list of acceptable parameters in MML tag. | |
54 | These parameters are generated in Content-Disposition header if exists." | |
a08b59c9 | 55 | :version "21.4" |
23f87bed MB |
56 | :type '(repeat (symbol :tag "Parameter")) |
57 | :group 'message) | |
58 | ||
59 | (defcustom mml-insert-mime-headers-always nil | |
60 | "If non-nil, always put Content-Type: text/plain at top of empty parts. | |
61 | It is necessary to work against a bug in certain clients." | |
a08b59c9 | 62 | :version "21.4" |
23f87bed MB |
63 | :type 'boolean |
64 | :group 'message) | |
65 | ||
66 | (defvar mml-tweak-type-alist nil | |
67 | "A list of (TYPE . FUNCTION) for tweaking MML parts. | |
68 | TYPE is a string containing a regexp to match the MIME type. FUNCTION | |
69 | is a Lisp function which is called with the MML handle to tweak the | |
70 | part. This variable is used only when no TWEAK parameter exists in | |
71 | the MML handle.") | |
72 | ||
73 | (defvar mml-tweak-function-alist nil | |
74 | "A list of (NAME . FUNCTION) for tweaking MML parts. | |
75 | NAME is a string containing the name of the TWEAK parameter in the MML | |
76 | handle. FUNCTION is a Lisp function which is called with the MML | |
77 | handle to tweak the part.") | |
78 | ||
79 | (defvar mml-tweak-sexp-alist | |
80 | '((mml-externalize-attachments . mml-tweak-externalize-attachments)) | |
81 | "A list of (SEXP . FUNCTION) for tweaking MML parts. | |
82 | SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION | |
83 | is called. FUNCTION is a Lisp function which is called with the MML | |
84 | handle to tweak the part.") | |
85 | ||
86 | (defvar mml-externalize-attachments nil | |
87 | "*If non-nil, local-file attachments are generated as external parts.") | |
88 | ||
c113de23 GM |
89 | (defvar mml-generate-multipart-alist nil |
90 | "*Alist of multipart generation functions. | |
91 | Each entry has the form (NAME . FUNCTION), where | |
a1506d29 | 92 | NAME is a string containing the name of the part (without the |
c113de23 GM |
93 | leading \"/multipart/\"), |
94 | FUNCTION is a Lisp function which is called to generate the part. | |
95 | ||
96 | The Lisp function has to supply the appropriate MIME headers and the | |
97 | contents of this part.") | |
98 | ||
99 | (defvar mml-syntax-table | |
100 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
101 | (modify-syntax-entry ?\\ "/" table) | |
102 | (modify-syntax-entry ?< "(" table) | |
103 | (modify-syntax-entry ?> ")" table) | |
104 | (modify-syntax-entry ?@ "w" table) | |
105 | (modify-syntax-entry ?/ "w" table) | |
106 | (modify-syntax-entry ?= " " table) | |
107 | (modify-syntax-entry ?* " " table) | |
108 | (modify-syntax-entry ?\; " " table) | |
109 | (modify-syntax-entry ?\' " " table) | |
110 | table)) | |
111 | ||
112 | (defvar mml-boundary-function 'mml-make-boundary | |
113 | "A function called to suggest a boundary. | |
114 | The function may be called several times, and should try to make a new | |
115 | suggestion each time. The function is called with one parameter, | |
116 | which is a number that says how many times the function has been | |
117 | called for this message.") | |
118 | ||
119 | (defvar mml-confirmation-set nil | |
120 | "A list of symbols, each of which disables some warning. | |
121 | `unknown-encoding': always send messages contain characters with | |
122 | unknown encoding; `use-ascii': always use ASCII for those characters | |
123 | with unknown encoding; `multipart': always send messages with more than | |
124 | one charsets.") | |
125 | ||
c113de23 GM |
126 | (defvar mml-generate-default-type "text/plain") |
127 | ||
128 | (defvar mml-buffer-list nil) | |
129 | ||
a1506d29 | 130 | (defun mml-generate-new-buffer (name) |
c113de23 GM |
131 | (let ((buf (generate-new-buffer name))) |
132 | (push buf mml-buffer-list) | |
133 | buf)) | |
134 | ||
135 | (defun mml-destroy-buffers () | |
136 | (let (kill-buffer-hook) | |
137 | (mapcar 'kill-buffer mml-buffer-list) | |
138 | (setq mml-buffer-list nil))) | |
139 | ||
140 | (defun mml-parse () | |
141 | "Parse the current buffer as an MML document." | |
23f87bed MB |
142 | (save-excursion |
143 | (goto-char (point-min)) | |
144 | (let ((table (syntax-table))) | |
145 | (unwind-protect | |
146 | (progn | |
147 | (set-syntax-table mml-syntax-table) | |
148 | (mml-parse-1)) | |
149 | (set-syntax-table table))))) | |
c113de23 GM |
150 | |
151 | (defun mml-parse-1 () | |
152 | "Parse the current buffer as an MML document." | |
153 | (let (struct tag point contents charsets warn use-ascii no-markup-p raw) | |
154 | (while (and (not (eobp)) | |
155 | (not (looking-at "<#/multipart"))) | |
156 | (cond | |
23f87bed MB |
157 | ((looking-at "<#secure") |
158 | ;; The secure part is essentially a meta-meta tag, which | |
159 | ;; expands to either a part tag if there are no other parts in | |
160 | ;; the document or a multipart tag if there are other parts | |
161 | ;; included in the message | |
162 | (let* (secure-mode | |
163 | (taginfo (mml-read-tag)) | |
164 | (recipients (cdr (assq 'recipients taginfo))) | |
165 | (sender (cdr (assq 'sender taginfo))) | |
166 | (location (cdr (assq 'tag-location taginfo))) | |
167 | (mode (cdr (assq 'mode taginfo))) | |
168 | (method (cdr (assq 'method taginfo))) | |
169 | tags) | |
170 | (save-excursion | |
171 | (if | |
172 | (re-search-forward | |
173 | "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t) | |
174 | (setq secure-mode "multipart") | |
175 | (setq secure-mode "part"))) | |
176 | (save-excursion | |
177 | (goto-char location) | |
178 | (re-search-forward "<#secure[^\n]*>\n")) | |
179 | (delete-region (match-beginning 0) (match-end 0)) | |
180 | (cond ((string= mode "sign") | |
181 | (setq tags (list "sign" method))) | |
182 | ((string= mode "encrypt") | |
183 | (setq tags (list "encrypt" method))) | |
184 | ((string= mode "signencrypt") | |
185 | (setq tags (list "sign" method "encrypt" method)))) | |
186 | (eval `(mml-insert-tag ,secure-mode | |
187 | ,@tags | |
188 | ,(if recipients "recipients") | |
189 | ,recipients | |
190 | ,(if sender "sender") | |
191 | ,sender)) | |
192 | ;; restart the parse | |
193 | (goto-char location))) | |
c113de23 GM |
194 | ((looking-at "<#multipart") |
195 | (push (nconc (mml-read-tag) (mml-parse-1)) struct)) | |
196 | ((looking-at "<#external") | |
197 | (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part)))) | |
198 | struct)) | |
199 | (t | |
200 | (if (or (looking-at "<#part") (looking-at "<#mml")) | |
201 | (setq tag (mml-read-tag) | |
202 | no-markup-p nil | |
203 | warn nil) | |
204 | (setq tag (list 'part '(type . "text/plain")) | |
205 | no-markup-p t | |
206 | warn t)) | |
207 | (setq raw (cdr (assq 'raw tag)) | |
208 | point (point) | |
ce9401f3 | 209 | contents (mml-read-part (eq 'mml (car tag))) |
23f87bed MB |
210 | charsets (cond |
211 | (raw nil) | |
212 | ((assq 'charset tag) | |
213 | (list | |
214 | (intern (downcase (cdr (assq 'charset tag)))))) | |
215 | (t | |
216 | (mm-find-mime-charset-region point (point) | |
217 | mm-hack-charsets)))) | |
c113de23 GM |
218 | (when (and (not raw) (memq nil charsets)) |
219 | (if (or (memq 'unknown-encoding mml-confirmation-set) | |
23f87bed MB |
220 | (message-options-get 'unknown-encoding) |
221 | (and (y-or-n-p "\ | |
222 | Message contains characters with unknown encoding. Really send? ") | |
223 | (message-options-set 'unknown-encoding t))) | |
a1506d29 | 224 | (if (setq use-ascii |
c113de23 | 225 | (or (memq 'use-ascii mml-confirmation-set) |
23f87bed MB |
226 | (message-options-get 'use-ascii) |
227 | (and (y-or-n-p "Use ASCII as charset? ") | |
228 | (message-options-set 'use-ascii t)))) | |
c113de23 GM |
229 | (setq charsets (delq nil charsets)) |
230 | (setq warn nil)) | |
231 | (error "Edit your message to remove those characters"))) | |
232 | (if (or raw | |
233 | (eq 'mml (car tag)) | |
234 | (< (length charsets) 2)) | |
235 | (if (or (not no-markup-p) | |
236 | (string-match "[^ \t\r\n]" contents)) | |
237 | ;; Don't create blank parts. | |
238 | (push (nconc tag (list (cons 'contents contents))) | |
239 | struct)) | |
240 | (let ((nstruct (mml-parse-singlepart-with-multiple-charsets | |
241 | tag point (point) use-ascii))) | |
242 | (when (and warn | |
243 | (not (memq 'multipart mml-confirmation-set)) | |
23f87bed MB |
244 | (not (message-options-get 'multipart)) |
245 | (not (and (y-or-n-p (format "\ | |
2453b2e8 | 246 | A message part needs to be split into %d charset parts. Really send? " |
23f87bed MB |
247 | (length nstruct))) |
248 | (message-options-set 'multipart t)))) | |
c113de23 GM |
249 | (error "Edit your message to use only one charset")) |
250 | (setq struct (nconc nstruct struct))))))) | |
251 | (unless (eobp) | |
252 | (forward-line 1)) | |
253 | (nreverse struct))) | |
254 | ||
a1506d29 | 255 | (defun mml-parse-singlepart-with-multiple-charsets |
c113de23 GM |
256 | (orig-tag beg end &optional use-ascii) |
257 | (save-excursion | |
258 | (save-restriction | |
259 | (narrow-to-region beg end) | |
260 | (goto-char (point-min)) | |
261 | (let ((current (or (mm-mime-charset (mm-charset-after)) | |
262 | (and use-ascii 'us-ascii))) | |
263 | charset struct space newline paragraph) | |
264 | (while (not (eobp)) | |
265 | (setq charset (mm-mime-charset (mm-charset-after))) | |
266 | (cond | |
267 | ;; The charset remains the same. | |
268 | ((eq charset 'us-ascii)) | |
269 | ((or (and use-ascii (not charset)) | |
270 | (eq charset current)) | |
271 | (setq space nil | |
272 | newline nil | |
273 | paragraph nil)) | |
274 | ;; The initial charset was ascii. | |
275 | ((eq current 'us-ascii) | |
276 | (setq current charset | |
277 | space nil | |
278 | newline nil | |
279 | paragraph nil)) | |
280 | ;; We have a change in charsets. | |
281 | (t | |
282 | (push (append | |
283 | orig-tag | |
284 | (list (cons 'contents | |
285 | (buffer-substring-no-properties | |
286 | beg (or paragraph newline space (point)))))) | |
287 | struct) | |
288 | (setq beg (or paragraph newline space (point)) | |
289 | current charset | |
290 | space nil | |
291 | newline nil | |
292 | paragraph nil))) | |
293 | ;; Compute places where it might be nice to break the part. | |
294 | (cond | |
295 | ((memq (following-char) '(? ?\t)) | |
296 | (setq space (1+ (point)))) | |
297 | ((and (eq (following-char) ?\n) | |
298 | (not (bobp)) | |
299 | (eq (char-after (1- (point))) ?\n)) | |
300 | (setq paragraph (point))) | |
301 | ((eq (following-char) ?\n) | |
302 | (setq newline (1+ (point))))) | |
303 | (forward-char 1)) | |
304 | ;; Do the final part. | |
305 | (unless (= beg (point)) | |
306 | (push (append orig-tag | |
307 | (list (cons 'contents | |
308 | (buffer-substring-no-properties | |
309 | beg (point))))) | |
310 | struct)) | |
311 | struct)))) | |
312 | ||
313 | (defun mml-read-tag () | |
314 | "Read a tag and return the contents." | |
23f87bed MB |
315 | (let ((orig-point (point)) |
316 | contents name elem val) | |
c113de23 GM |
317 | (forward-char 2) |
318 | (setq name (buffer-substring-no-properties | |
319 | (point) (progn (forward-sexp 1) (point)))) | |
320 | (skip-chars-forward " \t\n") | |
23f87bed | 321 | (while (not (looking-at ">[ \t]*\n?")) |
c113de23 GM |
322 | (setq elem (buffer-substring-no-properties |
323 | (point) (progn (forward-sexp 1) (point)))) | |
324 | (skip-chars-forward "= \t\n") | |
325 | (setq val (buffer-substring-no-properties | |
326 | (point) (progn (forward-sexp 1) (point)))) | |
327 | (when (string-match "^\"\\(.*\\)\"$" val) | |
328 | (setq val (match-string 1 val))) | |
329 | (push (cons (intern elem) val) contents) | |
330 | (skip-chars-forward " \t\n")) | |
23f87bed MB |
331 | (goto-char (match-end 0)) |
332 | ;; Don't skip the leading space. | |
333 | ;;(skip-chars-forward " \t\n") | |
334 | ;; Put the tag location into the returned contents | |
335 | (setq contents (append (list (cons 'tag-location orig-point)) contents)) | |
c113de23 GM |
336 | (cons (intern name) (nreverse contents)))) |
337 | ||
23f87bed MB |
338 | (defun mml-buffer-substring-no-properties-except-hard-newlines (start end) |
339 | (let ((str (buffer-substring-no-properties start end)) | |
340 | (bufstart start) tmp) | |
341 | (while (setq tmp (text-property-any start end 'hard 't)) | |
342 | (set-text-properties (- tmp bufstart) (- tmp bufstart -1) | |
343 | '(hard t) str) | |
344 | (setq start (1+ tmp))) | |
345 | str)) | |
346 | ||
c113de23 GM |
347 | (defun mml-read-part (&optional mml) |
348 | "Return the buffer up till the next part, multipart or closing part or multipart. | |
349 | If MML is non-nil, return the buffer up till the correspondent mml tag." | |
350 | (let ((beg (point)) (count 1)) | |
23f87bed | 351 | ;; If the tag ended at the end of the line, we go to the next line. |
c113de23 GM |
352 | (when (looking-at "[ \t]*\n") |
353 | (forward-line 1)) | |
354 | (if mml | |
355 | (progn | |
356 | (while (and (> count 0) (not (eobp))) | |
357 | (if (re-search-forward "<#\\(/\\)?mml." nil t) | |
358 | (setq count (+ count (if (match-beginning 1) -1 1))) | |
359 | (goto-char (point-max)))) | |
23f87bed MB |
360 | (mml-buffer-substring-no-properties-except-hard-newlines |
361 | beg (if (> count 0) | |
362 | (point) | |
363 | (match-beginning 0)))) | |
c113de23 GM |
364 | (if (re-search-forward |
365 | "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t) | |
366 | (prog1 | |
23f87bed MB |
367 | (mml-buffer-substring-no-properties-except-hard-newlines |
368 | beg (match-beginning 0)) | |
c113de23 GM |
369 | (if (or (not (match-beginning 1)) |
370 | (equal (match-string 2) "multipart")) | |
371 | (goto-char (match-beginning 0)) | |
372 | (when (looking-at "[ \t]*\n") | |
373 | (forward-line 1)))) | |
23f87bed MB |
374 | (mml-buffer-substring-no-properties-except-hard-newlines |
375 | beg (goto-char (point-max))))))) | |
c113de23 GM |
376 | |
377 | (defvar mml-boundary nil) | |
378 | (defvar mml-base-boundary "-=-=") | |
379 | (defvar mml-multipart-number 0) | |
380 | ||
381 | (defun mml-generate-mime () | |
382 | "Generate a MIME message based on the current MML document." | |
383 | (let ((cont (mml-parse)) | |
384 | (mml-multipart-number mml-multipart-number)) | |
385 | (if (not cont) | |
386 | nil | |
387 | (with-temp-buffer | |
388 | (if (and (consp (car cont)) | |
389 | (= (length cont) 1)) | |
390 | (mml-generate-mime-1 (car cont)) | |
391 | (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed")) | |
392 | cont))) | |
393 | (buffer-string))))) | |
394 | ||
395 | (defun mml-generate-mime-1 (cont) | |
23f87bed MB |
396 | (let ((mm-use-ultra-safe-encoding |
397 | (or mm-use-ultra-safe-encoding (assq 'sign cont)))) | |
398 | (save-restriction | |
399 | (narrow-to-region (point) (point)) | |
400 | (mml-tweak-part cont) | |
401 | (cond | |
402 | ((or (eq (car cont) 'part) (eq (car cont) 'mml)) | |
403 | (let ((raw (cdr (assq 'raw cont))) | |
404 | coded encoding charset filename type flowed) | |
405 | (setq type (or (cdr (assq 'type cont)) "text/plain")) | |
406 | (if (and (not raw) | |
407 | (member (car (split-string type "/")) '("text" "message"))) | |
408 | (progn | |
409 | (with-temp-buffer | |
410 | (setq charset (mm-charset-to-coding-system | |
411 | (cdr (assq 'charset cont)))) | |
412 | (when (eq charset 'ascii) | |
413 | (setq charset nil)) | |
414 | (cond | |
415 | ((cdr (assq 'buffer cont)) | |
416 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
417 | ((and (setq filename (cdr (assq 'filename cont))) | |
418 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
419 | (let ((coding-system-for-read charset)) | |
420 | (mm-insert-file-contents filename))) | |
421 | ((eq 'mml (car cont)) | |
422 | (insert (cdr (assq 'contents cont)))) | |
423 | (t | |
424 | (save-restriction | |
425 | (narrow-to-region (point) (point)) | |
426 | (insert (cdr (assq 'contents cont))) | |
427 | ;; Remove quotes from quoted tags. | |
428 | (goto-char (point-min)) | |
429 | (while (re-search-forward | |
430 | "<#!+/?\\(part\\|multipart\\|external\\|mml\\)" | |
431 | nil t) | |
432 | (delete-region (+ (match-beginning 0) 2) | |
433 | (+ (match-beginning 0) 3)))))) | |
434 | (cond | |
435 | ((eq (car cont) 'mml) | |
436 | (let ((mml-boundary (mml-compute-boundary cont)) | |
437 | (mml-generate-default-type "text/plain")) | |
438 | (mml-to-mime)) | |
439 | (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b"))) | |
440 | ;; ignore 0x1b, it is part of iso-2022-jp | |
441 | (setq encoding (mm-body-7-or-8)))) | |
442 | ((string= (car (split-string type "/")) "message") | |
443 | (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b"))) | |
444 | ;; ignore 0x1b, it is part of iso-2022-jp | |
445 | (setq encoding (mm-body-7-or-8)))) | |
446 | (t | |
447 | ;; Only perform format=flowed filling on text/plain | |
448 | ;; parts where there either isn't a format parameter | |
449 | ;; in the mml tag or it says "flowed" and there | |
450 | ;; actually are hard newlines in the text. | |
451 | (let (use-hard-newlines) | |
452 | (when (and (string= type "text/plain") | |
453 | (or (null (assq 'format cont)) | |
454 | (string= (cdr (assq 'format cont)) | |
455 | "flowed")) | |
456 | (setq use-hard-newlines | |
457 | (text-property-any | |
458 | (point-min) (point-max) 'hard 't))) | |
459 | (fill-flowed-encode) | |
460 | ;; Indicate that `mml-insert-mime-headers' should | |
461 | ;; insert a "; format=flowed" string unless the | |
462 | ;; user has already specified it. | |
463 | (setq flowed (null (assq 'format cont))))) | |
464 | (setq charset (mm-encode-body charset)) | |
465 | (setq encoding (mm-body-encoding | |
466 | charset (cdr (assq 'encoding cont)))))) | |
467 | (setq coded (buffer-string))) | |
468 | (mml-insert-mime-headers cont type charset encoding flowed) | |
469 | (insert "\n") | |
470 | (insert coded)) | |
471 | (mm-with-unibyte-buffer | |
472 | (cond | |
473 | ((cdr (assq 'buffer cont)) | |
474 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
475 | ((and (setq filename (cdr (assq 'filename cont))) | |
476 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
477 | (let ((coding-system-for-read mm-binary-coding-system)) | |
478 | (mm-insert-file-contents filename nil nil nil nil t))) | |
479 | (t | |
480 | (insert (cdr (assq 'contents cont))))) | |
481 | (setq encoding (mm-encode-buffer type) | |
482 | coded (mm-string-as-multibyte (buffer-string)))) | |
483 | (mml-insert-mime-headers cont type charset encoding nil) | |
484 | (insert "\n") | |
485 | (mm-with-unibyte-current-buffer | |
486 | (insert coded))))) | |
487 | ((eq (car cont) 'external) | |
488 | (insert "Content-Type: message/external-body") | |
489 | (let ((parameters (mml-parameter-string | |
490 | cont '(expiration size permission))) | |
491 | (name (cdr (assq 'name cont))) | |
492 | (url (cdr (assq 'url cont)))) | |
493 | (when name | |
494 | (setq name (mml-parse-file-name name)) | |
495 | (if (stringp name) | |
496 | (mml-insert-parameter | |
497 | (mail-header-encode-parameter "name" name) | |
498 | "access-type=local-file") | |
c113de23 | 499 | (mml-insert-parameter |
23f87bed MB |
500 | (mail-header-encode-parameter |
501 | "name" (file-name-nondirectory (nth 2 name))) | |
502 | (mail-header-encode-parameter "site" (nth 1 name)) | |
503 | (mail-header-encode-parameter | |
504 | "directory" (file-name-directory (nth 2 name)))) | |
505 | (mml-insert-parameter | |
506 | (concat "access-type=" | |
507 | (if (member (nth 0 name) '("ftp@" "anonymous@")) | |
508 | "anon-ftp" | |
509 | "ftp"))))) | |
510 | (when url | |
c113de23 | 511 | (mml-insert-parameter |
23f87bed MB |
512 | (mail-header-encode-parameter "url" url) |
513 | "access-type=url")) | |
514 | (when parameters | |
515 | (mml-insert-parameter-string | |
516 | cont '(expiration size permission)))) | |
517 | (insert "\n\n") | |
518 | (insert "Content-Type: " (cdr (assq 'type cont)) "\n") | |
519 | (insert "Content-ID: " (message-make-message-id) "\n") | |
520 | (insert "Content-Transfer-Encoding: " | |
521 | (or (cdr (assq 'encoding cont)) "binary")) | |
522 | (insert "\n\n") | |
523 | (insert (or (cdr (assq 'contents cont)))) | |
524 | (insert "\n")) | |
525 | ((eq (car cont) 'multipart) | |
526 | (let* ((type (or (cdr (assq 'type cont)) "mixed")) | |
527 | (mml-generate-default-type (if (equal type "digest") | |
528 | "message/rfc822" | |
529 | "text/plain")) | |
530 | (handler (assoc type mml-generate-multipart-alist))) | |
531 | (if handler | |
532 | (funcall (cdr handler) cont) | |
533 | ;; No specific handler. Use default one. | |
534 | (let ((mml-boundary (mml-compute-boundary cont))) | |
535 | (insert (format "Content-Type: multipart/%s; boundary=\"%s\"" | |
536 | type mml-boundary) | |
537 | (if (cdr (assq 'start cont)) | |
538 | (format "; start=\"%s\"\n" (cdr (assq 'start cont))) | |
539 | "\n")) | |
540 | (let ((cont cont) part) | |
541 | (while (setq part (pop cont)) | |
542 | ;; Skip `multipart' and attributes. | |
543 | (when (and (consp part) (consp (cdr part))) | |
544 | (insert "\n--" mml-boundary "\n") | |
545 | (mml-generate-mime-1 part)))) | |
546 | (insert "\n--" mml-boundary "--\n"))))) | |
547 | (t | |
548 | (error "Invalid element: %S" cont))) | |
549 | ;; handle sign & encrypt tags in a semi-smart way. | |
550 | (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist)) | |
551 | (encrypt-item (assoc (cdr (assq 'encrypt cont)) | |
552 | mml-encrypt-alist)) | |
553 | sender recipients) | |
554 | (when (or sign-item encrypt-item) | |
555 | (when (setq sender (cdr (assq 'sender cont))) | |
556 | (message-options-set 'mml-sender sender) | |
557 | (message-options-set 'message-sender sender)) | |
558 | (if (setq recipients (cdr (assq 'recipients cont))) | |
559 | (message-options-set 'message-recipients recipients)) | |
560 | (let ((style (mml-signencrypt-style (first (or sign-item encrypt-item))))) | |
561 | ;; check if: we're both signing & encrypting, both methods | |
562 | ;; are the same (why would they be different?!), and that | |
563 | ;; the signencrypt style allows for combined operation. | |
564 | (if (and sign-item encrypt-item (equal (first sign-item) | |
565 | (first encrypt-item)) | |
566 | (equal style 'combined)) | |
567 | (funcall (nth 1 encrypt-item) cont t) | |
568 | ;; otherwise, revert to the old behavior. | |
569 | (when sign-item | |
570 | (funcall (nth 1 sign-item) cont)) | |
571 | (when encrypt-item | |
572 | (funcall (nth 1 encrypt-item) cont))))))))) | |
c113de23 GM |
573 | |
574 | (defun mml-compute-boundary (cont) | |
575 | "Return a unique boundary that does not exist in CONT." | |
576 | (let ((mml-boundary (funcall mml-boundary-function | |
577 | (incf mml-multipart-number)))) | |
578 | ;; This function tries again and again until it has found | |
579 | ;; a unique boundary. | |
580 | (while (not (catch 'not-unique | |
581 | (mml-compute-boundary-1 cont)))) | |
582 | mml-boundary)) | |
583 | ||
584 | (defun mml-compute-boundary-1 (cont) | |
585 | (let (filename) | |
586 | (cond | |
587 | ((eq (car cont) 'part) | |
588 | (with-temp-buffer | |
589 | (cond | |
590 | ((cdr (assq 'buffer cont)) | |
591 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
592 | ((and (setq filename (cdr (assq 'filename cont))) | |
593 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
594 | (mm-insert-file-contents filename)) | |
595 | (t | |
596 | (insert (cdr (assq 'contents cont))))) | |
597 | (goto-char (point-min)) | |
598 | (when (re-search-forward (concat "^--" (regexp-quote mml-boundary)) | |
599 | nil t) | |
600 | (setq mml-boundary (funcall mml-boundary-function | |
601 | (incf mml-multipart-number))) | |
602 | (throw 'not-unique nil)))) | |
603 | ((eq (car cont) 'multipart) | |
604 | (mapcar 'mml-compute-boundary-1 (cddr cont)))) | |
605 | t)) | |
606 | ||
607 | (defun mml-make-boundary (number) | |
608 | (concat (make-string (% number 60) ?=) | |
609 | (if (> number 17) | |
610 | (format "%x" number) | |
611 | "") | |
612 | mml-base-boundary)) | |
613 | ||
23f87bed MB |
614 | (defun mml-insert-mime-headers (cont type charset encoding flowed) |
615 | (let (parameters id disposition description) | |
c113de23 GM |
616 | (setq parameters |
617 | (mml-parameter-string | |
23f87bed | 618 | cont mml-content-type-parameters)) |
c113de23 GM |
619 | (when (or charset |
620 | parameters | |
23f87bed MB |
621 | flowed |
622 | (not (equal type mml-generate-default-type)) | |
623 | mml-insert-mime-headers-always) | |
c113de23 GM |
624 | (when (consp charset) |
625 | (error | |
23f87bed | 626 | "Can't encode a part with several charsets")) |
c113de23 GM |
627 | (insert "Content-Type: " type) |
628 | (when charset | |
629 | (insert "; " (mail-header-encode-parameter | |
630 | "charset" (symbol-name charset)))) | |
23f87bed MB |
631 | (when flowed |
632 | (insert "; format=flowed")) | |
c113de23 GM |
633 | (when parameters |
634 | (mml-insert-parameter-string | |
23f87bed | 635 | cont mml-content-type-parameters)) |
c113de23 | 636 | (insert "\n")) |
23f87bed MB |
637 | (when (setq id (cdr (assq 'id cont))) |
638 | (insert "Content-ID: " id "\n")) | |
c113de23 GM |
639 | (setq parameters |
640 | (mml-parameter-string | |
23f87bed | 641 | cont mml-content-disposition-parameters)) |
c113de23 GM |
642 | (when (or (setq disposition (cdr (assq 'disposition cont))) |
643 | parameters) | |
644 | (insert "Content-Disposition: " (or disposition "inline")) | |
645 | (when parameters | |
646 | (mml-insert-parameter-string | |
23f87bed | 647 | cont mml-content-disposition-parameters)) |
c113de23 GM |
648 | (insert "\n")) |
649 | (unless (eq encoding '7bit) | |
650 | (insert (format "Content-Transfer-Encoding: %s\n" encoding))) | |
651 | (when (setq description (cdr (assq 'description cont))) | |
652 | (insert "Content-Description: " | |
653 | (mail-encode-encoded-word-string description) "\n")))) | |
654 | ||
655 | (defun mml-parameter-string (cont types) | |
656 | (let ((string "") | |
657 | value type) | |
658 | (while (setq type (pop types)) | |
659 | (when (setq value (cdr (assq type cont))) | |
660 | ;; Strip directory component from the filename parameter. | |
661 | (when (eq type 'filename) | |
662 | (setq value (file-name-nondirectory value))) | |
663 | (setq string (concat string "; " | |
664 | (mail-header-encode-parameter | |
665 | (symbol-name type) value))))) | |
666 | (when (not (zerop (length string))) | |
667 | string))) | |
668 | ||
669 | (defun mml-insert-parameter-string (cont types) | |
670 | (let (value type) | |
671 | (while (setq type (pop types)) | |
672 | (when (setq value (cdr (assq type cont))) | |
673 | ;; Strip directory component from the filename parameter. | |
674 | (when (eq type 'filename) | |
675 | (setq value (file-name-nondirectory value))) | |
676 | (mml-insert-parameter | |
677 | (mail-header-encode-parameter | |
678 | (symbol-name type) value)))))) | |
679 | ||
534aa266 DL |
680 | (eval-when-compile |
681 | (defvar ange-ftp-name-format) | |
682 | (defvar efs-path-regexp)) | |
c113de23 GM |
683 | (defun mml-parse-file-name (path) |
684 | (if (if (boundp 'efs-path-regexp) | |
685 | (string-match efs-path-regexp path) | |
686 | (if (boundp 'ange-ftp-name-format) | |
687 | (string-match (car ange-ftp-name-format) path))) | |
688 | (list (match-string 1 path) (match-string 2 path) | |
689 | (substring path (1+ (match-end 2)))) | |
690 | path)) | |
691 | ||
692 | (defun mml-insert-buffer (buffer) | |
693 | "Insert BUFFER at point and quote any MML markup." | |
694 | (save-restriction | |
695 | (narrow-to-region (point) (point)) | |
696 | (insert-buffer-substring buffer) | |
697 | (mml-quote-region (point-min) (point-max)) | |
698 | (goto-char (point-max)))) | |
699 | ||
700 | ;;; | |
701 | ;;; Transforming MIME to MML | |
702 | ;;; | |
703 | ||
23f87bed MB |
704 | (defun mime-to-mml (&optional handles) |
705 | "Translate the current buffer (which should be a message) into MML. | |
706 | If HANDLES is non-nil, use it instead reparsing the buffer." | |
c113de23 GM |
707 | ;; First decode the head. |
708 | (save-restriction | |
709 | (message-narrow-to-head) | |
710 | (mail-decode-encoded-word-region (point-min) (point-max))) | |
23f87bed MB |
711 | (unless handles |
712 | (setq handles (mm-dissect-buffer t))) | |
713 | (goto-char (point-min)) | |
714 | (search-forward "\n\n" nil t) | |
715 | (delete-region (point) (point-max)) | |
716 | (if (stringp (car handles)) | |
717 | (mml-insert-mime handles) | |
718 | (mml-insert-mime handles t)) | |
719 | (mm-destroy-parts handles) | |
c113de23 GM |
720 | (save-restriction |
721 | (message-narrow-to-head) | |
722 | ;; Remove them, they are confusing. | |
723 | (message-remove-header "Content-Type") | |
724 | (message-remove-header "MIME-Version") | |
23f87bed | 725 | (message-remove-header "Content-Disposition") |
c113de23 GM |
726 | (message-remove-header "Content-Transfer-Encoding"))) |
727 | ||
728 | (defun mml-to-mime () | |
729 | "Translate the current buffer from MML to MIME." | |
730 | (message-encode-message-body) | |
731 | (save-restriction | |
732 | (message-narrow-to-headers-or-head) | |
23f87bed MB |
733 | ;; Skip past any From_ headers. |
734 | (while (looking-at "From ") | |
735 | (forward-line 1)) | |
c113de23 GM |
736 | (let ((mail-parse-charset message-default-charset)) |
737 | (mail-encode-encoded-word-buffer)))) | |
738 | ||
739 | (defun mml-insert-mime (handle &optional no-markup) | |
740 | (let (textp buffer mmlp) | |
741 | ;; Determine type and stuff. | |
742 | (unless (stringp (car handle)) | |
743 | (unless (setq textp (equal (mm-handle-media-supertype handle) "text")) | |
744 | (save-excursion | |
745 | (set-buffer (setq buffer (mml-generate-new-buffer " *mml*"))) | |
746 | (mm-insert-part handle) | |
a1506d29 | 747 | (if (setq mmlp (equal (mm-handle-media-type handle) |
c113de23 GM |
748 | "message/rfc822")) |
749 | (mime-to-mml))))) | |
750 | (if mmlp | |
751 | (mml-insert-mml-markup handle nil t t) | |
752 | (unless (and no-markup | |
753 | (equal (mm-handle-media-type handle) "text/plain")) | |
754 | (mml-insert-mml-markup handle buffer textp))) | |
755 | (cond | |
a1506d29 | 756 | (mmlp |
23f87bed | 757 | (insert-buffer-substring buffer) |
c113de23 GM |
758 | (goto-char (point-max)) |
759 | (insert "<#/mml>\n")) | |
760 | ((stringp (car handle)) | |
761 | (mapcar 'mml-insert-mime (cdr handle)) | |
762 | (insert "<#/multipart>\n")) | |
763 | (textp | |
23f87bed MB |
764 | (let ((charset (mail-content-type-get |
765 | (mm-handle-type handle) 'charset)) | |
766 | (start (point))) | |
767 | (if (eq charset 'gnus-decoded) | |
768 | (mm-insert-part handle) | |
769 | (insert (mm-decode-string (mm-get-part handle) charset))) | |
770 | (mml-quote-region start (point))) | |
c113de23 GM |
771 | (goto-char (point-max))) |
772 | (t | |
773 | (insert "<#/part>\n"))))) | |
774 | ||
775 | (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp) | |
776 | "Take a MIME handle and insert an MML tag." | |
777 | (if (stringp (car handle)) | |
23f87bed MB |
778 | (progn |
779 | (insert "<#multipart type=" (mm-handle-media-subtype handle)) | |
780 | (let ((start (mm-handle-multipart-ctl-parameter handle 'start))) | |
781 | (when start | |
782 | (insert " start=\"" start "\""))) | |
783 | (insert ">\n")) | |
c113de23 GM |
784 | (if mmlp |
785 | (insert "<#mml type=" (mm-handle-media-type handle)) | |
786 | (insert "<#part type=" (mm-handle-media-type handle))) | |
787 | (dolist (elem (append (cdr (mm-handle-type handle)) | |
788 | (cdr (mm-handle-disposition handle)))) | |
23f87bed MB |
789 | (unless (symbolp (cdr elem)) |
790 | (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\""))) | |
791 | (when (mm-handle-id handle) | |
792 | (insert " id=\"" (mm-handle-id handle) "\"")) | |
c113de23 GM |
793 | (when (mm-handle-disposition handle) |
794 | (insert " disposition=" (car (mm-handle-disposition handle)))) | |
795 | (when buffer | |
796 | (insert " buffer=\"" (buffer-name buffer) "\"")) | |
797 | (when nofile | |
798 | (insert " nofile=yes")) | |
799 | (when (mm-handle-description handle) | |
800 | (insert " description=\"" (mm-handle-description handle) "\"")) | |
801 | (insert ">\n"))) | |
802 | ||
803 | (defun mml-insert-parameter (&rest parameters) | |
804 | "Insert PARAMETERS in a nice way." | |
805 | (dolist (param parameters) | |
806 | (insert ";") | |
807 | (let ((point (point))) | |
808 | (insert " " param) | |
809 | (when (> (current-column) 71) | |
810 | (goto-char point) | |
811 | (insert "\n ") | |
812 | (end-of-line))))) | |
813 | ||
814 | ;;; | |
815 | ;;; Mode for inserting and editing MML forms | |
816 | ;;; | |
817 | ||
818 | (defvar mml-mode-map | |
23f87bed MB |
819 | (let ((sign (make-sparse-keymap)) |
820 | (encrypt (make-sparse-keymap)) | |
821 | (signpart (make-sparse-keymap)) | |
822 | (encryptpart (make-sparse-keymap)) | |
823 | (map (make-sparse-keymap)) | |
c113de23 | 824 | (main (make-sparse-keymap))) |
23f87bed MB |
825 | (define-key sign "p" 'mml-secure-message-sign-pgpmime) |
826 | (define-key sign "o" 'mml-secure-message-sign-pgp) | |
827 | (define-key sign "s" 'mml-secure-message-sign-smime) | |
828 | (define-key signpart "p" 'mml-secure-sign-pgpmime) | |
829 | (define-key signpart "o" 'mml-secure-sign-pgp) | |
830 | (define-key signpart "s" 'mml-secure-sign-smime) | |
831 | (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime) | |
832 | (define-key encrypt "o" 'mml-secure-message-encrypt-pgp) | |
833 | (define-key encrypt "s" 'mml-secure-message-encrypt-smime) | |
834 | (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime) | |
835 | (define-key encryptpart "o" 'mml-secure-encrypt-pgp) | |
836 | (define-key encryptpart "s" 'mml-secure-encrypt-smime) | |
837 | (define-key map "\C-n" 'mml-unsecure-message) | |
c113de23 GM |
838 | (define-key map "f" 'mml-attach-file) |
839 | (define-key map "b" 'mml-attach-buffer) | |
840 | (define-key map "e" 'mml-attach-external) | |
841 | (define-key map "q" 'mml-quote-region) | |
842 | (define-key map "m" 'mml-insert-multipart) | |
843 | (define-key map "p" 'mml-insert-part) | |
844 | (define-key map "v" 'mml-validate) | |
845 | (define-key map "P" 'mml-preview) | |
23f87bed MB |
846 | (define-key map "s" sign) |
847 | (define-key map "S" signpart) | |
848 | (define-key map "c" encrypt) | |
849 | (define-key map "C" encryptpart) | |
c113de23 | 850 | ;;(define-key map "n" 'mml-narrow-to-part) |
23f87bed MB |
851 | ;; `M-m' conflicts with `back-to-indentation'. |
852 | ;; (define-key main "\M-m" map) | |
853 | (define-key main "\C-c\C-m" map) | |
c113de23 GM |
854 | main)) |
855 | ||
856 | (easy-menu-define | |
f4bb9409 | 857 | mml-menu mml-mode-map "" |
23f87bed MB |
858 | `("Attachments" |
859 | ["Attach File..." mml-attach-file | |
860 | ,@(if (featurep 'xemacs) '(t) | |
861 | '(:help "Attach a file at point"))] | |
862 | ["Attach Buffer..." mml-attach-buffer t] | |
863 | ["Attach External..." mml-attach-external t] | |
864 | ["Insert Part..." mml-insert-part t] | |
865 | ["Insert Multipart..." mml-insert-multipart t] | |
866 | ["PGP/MIME Sign" mml-secure-message-sign-pgpmime t] | |
867 | ["PGP/MIME Encrypt" mml-secure-message-encrypt-pgpmime t] | |
868 | ["PGP Sign" mml-secure-message-sign-pgp t] | |
869 | ["PGP Encrypt" mml-secure-message-encrypt-pgp t] | |
870 | ["S/MIME Sign" mml-secure-message-sign-smime t] | |
871 | ["S/MIME Encrypt" mml-secure-message-encrypt-smime t] | |
872 | ("Secure MIME part" | |
873 | ["PGP/MIME Sign Part" mml-secure-sign-pgpmime t] | |
874 | ["PGP/MIME Encrypt Part" mml-secure-encrypt-pgpmime t] | |
875 | ["PGP Sign Part" mml-secure-sign-pgp t] | |
876 | ["PGP Encrypt Part" mml-secure-encrypt-pgp t] | |
877 | ["S/MIME Sign Part" mml-secure-sign-smime t] | |
878 | ["S/MIME Encrypt Part" mml-secure-encrypt-smime t]) | |
879 | ["Encrypt/Sign off" mml-unsecure-message t] | |
f4bb9409 | 880 | ;;["Narrow" mml-narrow-to-part t] |
23f87bed MB |
881 | ["Quote MML" mml-quote-region t] |
882 | ["Validate MML" mml-validate t] | |
f4bb9409 | 883 | ["Preview" mml-preview t])) |
c113de23 GM |
884 | |
885 | (defvar mml-mode nil | |
886 | "Minor mode for editing MML.") | |
887 | ||
888 | (defun mml-mode (&optional arg) | |
889 | "Minor mode for editing MML. | |
23f87bed MB |
890 | MML is the MIME Meta Language, a minor mode for composing MIME articles. |
891 | See Info node `(emacs-mime)Composing'. | |
c113de23 GM |
892 | |
893 | \\{mml-mode-map}" | |
894 | (interactive "P") | |
23f87bed MB |
895 | (when (set (make-local-variable 'mml-mode) |
896 | (if (null arg) (not mml-mode) | |
897 | (> (prefix-numeric-value arg) 0))) | |
898 | (gnus-add-minor-mode 'mml-mode " MML" mml-mode-map) | |
899 | (easy-menu-add mml-menu mml-mode-map) | |
900 | (run-hooks 'mml-mode-hook))) | |
c113de23 GM |
901 | |
902 | ;;; | |
903 | ;;; Helper functions for reading MIME stuff from the minibuffer and | |
904 | ;;; inserting stuff to the buffer. | |
905 | ;;; | |
906 | ||
907 | (defun mml-minibuffer-read-file (prompt) | |
23f87bed MB |
908 | (let* ((completion-ignored-extensions nil) |
909 | (file (read-file-name prompt nil nil t))) | |
910 | ;; Prevent some common errors. This is inspired by similar code in | |
c113de23 GM |
911 | ;; VM. |
912 | (when (file-directory-p file) | |
913 | (error "%s is a directory, cannot attach" file)) | |
914 | (unless (file-exists-p file) | |
915 | (error "No such file: %s" file)) | |
916 | (unless (file-readable-p file) | |
917 | (error "Permission denied: %s" file)) | |
918 | file)) | |
919 | ||
920 | (defun mml-minibuffer-read-type (name &optional default) | |
921 | (mailcap-parse-mimetypes) | |
922 | (let* ((default (or default | |
923 | (mm-default-file-encoding name) | |
924 | ;; Perhaps here we should check what the file | |
925 | ;; looks like, and offer text/plain if it looks | |
926 | ;; like text/plain. | |
927 | "application/octet-stream")) | |
928 | (string (completing-read | |
929 | (format "Content type (default %s): " default) | |
8538cc2a | 930 | (mapcar 'list (mailcap-mime-types))))) |
c113de23 GM |
931 | (if (not (equal string "")) |
932 | string | |
933 | default))) | |
934 | ||
935 | (defun mml-minibuffer-read-description () | |
936 | (let ((description (read-string "One line description: "))) | |
937 | (when (string-match "\\`[ \t]*\\'" description) | |
938 | (setq description nil)) | |
939 | description)) | |
940 | ||
23f87bed MB |
941 | (defun mml-minibuffer-read-disposition (type &optional default) |
942 | (let* ((default (or default | |
943 | (if (string-match "^text/.*" type) | |
944 | "inline" | |
945 | "attachment"))) | |
946 | (disposition (completing-read "Disposition: " | |
947 | '(("attachment") ("inline") ("")) | |
948 | nil | |
949 | nil))) | |
950 | (if (not (equal disposition "")) | |
951 | disposition | |
952 | default))) | |
953 | ||
c113de23 GM |
954 | (defun mml-quote-region (beg end) |
955 | "Quote the MML tags in the region." | |
956 | (interactive "r") | |
957 | (save-excursion | |
958 | (save-restriction | |
959 | ;; Temporarily narrow the region to defend from changes | |
960 | ;; invalidating END. | |
961 | (narrow-to-region beg end) | |
962 | (goto-char (point-min)) | |
963 | ;; Quote parts. | |
964 | (while (re-search-forward | |
965 | "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t) | |
966 | ;; Insert ! after the #. | |
967 | (goto-char (+ (match-beginning 0) 2)) | |
968 | (insert "!"))))) | |
969 | ||
970 | (defun mml-insert-tag (name &rest plist) | |
971 | "Insert an MML tag described by NAME and PLIST." | |
972 | (when (symbolp name) | |
973 | (setq name (symbol-name name))) | |
974 | (insert "<#" name) | |
975 | (while plist | |
976 | (let ((key (pop plist)) | |
977 | (value (pop plist))) | |
978 | (when value | |
979 | ;; Quote VALUE if it contains suspicious characters. | |
980 | (when (string-match "[\"'\\~/*;() \t\n]" value) | |
23f87bed MB |
981 | (setq value (with-output-to-string |
982 | (let (print-escape-nonascii) | |
983 | (prin1 value))))) | |
c113de23 GM |
984 | (insert (format " %s=%s" key value))))) |
985 | (insert ">\n")) | |
986 | ||
987 | (defun mml-insert-empty-tag (name &rest plist) | |
988 | "Insert an empty MML tag described by NAME and PLIST." | |
989 | (when (symbolp name) | |
990 | (setq name (symbol-name name))) | |
991 | (apply #'mml-insert-tag name plist) | |
992 | (insert "<#/" name ">\n")) | |
993 | ||
994 | ;;; Attachment functions. | |
995 | ||
23f87bed | 996 | (defun mml-attach-file (file &optional type description disposition) |
c113de23 GM |
997 | "Attach a file to the outgoing MIME message. |
998 | The file is not inserted or encoded until you send the message with | |
999 | `\\[message-send-and-exit]' or `\\[message-send]'. | |
1000 | ||
1001 | FILE is the name of the file to attach. TYPE is its content-type, a | |
1002 | string of the form \"type/subtype\". DESCRIPTION is a one-line | |
1003 | description of the attachment." | |
1004 | (interactive | |
1005 | (let* ((file (mml-minibuffer-read-file "Attach file: ")) | |
1006 | (type (mml-minibuffer-read-type file)) | |
23f87bed MB |
1007 | (description (mml-minibuffer-read-description)) |
1008 | (disposition (mml-minibuffer-read-disposition type))) | |
1009 | (list file type description disposition))) | |
1010 | (mml-insert-empty-tag 'part | |
1011 | 'type type | |
1012 | 'filename file | |
1013 | 'disposition (or disposition "attachment") | |
1014 | 'description description)) | |
c113de23 GM |
1015 | |
1016 | (defun mml-attach-buffer (buffer &optional type description) | |
1017 | "Attach a buffer to the outgoing MIME message. | |
1018 | See `mml-attach-file' for details of operation." | |
1019 | (interactive | |
1020 | (let* ((buffer (read-buffer "Attach buffer: ")) | |
1021 | (type (mml-minibuffer-read-type buffer "text/plain")) | |
1022 | (description (mml-minibuffer-read-description))) | |
1023 | (list buffer type description))) | |
1024 | (mml-insert-empty-tag 'part 'type type 'buffer buffer | |
1025 | 'disposition "attachment" 'description description)) | |
1026 | ||
1027 | (defun mml-attach-external (file &optional type description) | |
1028 | "Attach an external file into the buffer. | |
1029 | FILE is an ange-ftp/efs specification of the part location. | |
1030 | TYPE is the MIME type to use." | |
1031 | (interactive | |
1032 | (let* ((file (mml-minibuffer-read-file "Attach external file: ")) | |
1033 | (type (mml-minibuffer-read-type file)) | |
1034 | (description (mml-minibuffer-read-description))) | |
1035 | (list file type description))) | |
1036 | (mml-insert-empty-tag 'external 'type type 'name file | |
1037 | 'disposition "attachment" 'description description)) | |
1038 | ||
1039 | (defun mml-insert-multipart (&optional type) | |
1040 | (interactive (list (completing-read "Multipart type (default mixed): " | |
1041 | '(("mixed") ("alternative") ("digest") ("parallel") | |
1042 | ("signed") ("encrypted")) | |
1043 | nil nil "mixed"))) | |
1044 | (or type | |
1045 | (setq type "mixed")) | |
1046 | (mml-insert-empty-tag "multipart" 'type type) | |
1047 | (forward-line -1)) | |
1048 | ||
1049 | (defun mml-insert-part (&optional type) | |
1050 | (interactive | |
1051 | (list (mml-minibuffer-read-type ""))) | |
1052 | (mml-insert-tag 'part 'type type 'disposition "inline") | |
1053 | (forward-line -1)) | |
1054 | ||
23f87bed MB |
1055 | (defun mml-preview-insert-mail-followup-to () |
1056 | "Insert a Mail-Followup-To header before previewing an article. | |
1057 | Should be adopted if code in `message-send-mail' is changed." | |
1058 | (when (and (message-mail-p) | |
1059 | (message-subscribed-p) | |
1060 | (not (mail-fetch-field "mail-followup-to")) | |
1061 | (message-make-mail-followup-to)) | |
1062 | (message-position-on-field "Mail-Followup-To" "X-Draft-From") | |
1063 | (insert (message-make-mail-followup-to)))) | |
1064 | ||
c113de23 GM |
1065 | (defun mml-preview (&optional raw) |
1066 | "Display current buffer with Gnus, in a new buffer. | |
1067 | If RAW, don't highlight the article." | |
1068 | (interactive "P") | |
23f87bed MB |
1069 | (save-excursion |
1070 | (let* ((buf (current-buffer)) | |
1071 | (message-options message-options) | |
1072 | (message-this-is-mail (message-mail-p)) | |
1073 | (message-this-is-news (message-news-p)) | |
1074 | (message-posting-charset (or (gnus-setup-posting-charset | |
1075 | (save-restriction | |
1076 | (message-narrow-to-headers-or-head) | |
1077 | (message-fetch-field "Newsgroups"))) | |
1078 | message-posting-charset))) | |
1079 | (message-options-set-recipient) | |
1080 | (switch-to-buffer (generate-new-buffer | |
1081 | (concat (if raw "*Raw MIME preview of " | |
1082 | "*MIME preview of ") (buffer-name)))) | |
1083 | (when (boundp 'gnus-buffers) | |
1084 | (push (current-buffer) gnus-buffers)) | |
1085 | (erase-buffer) | |
1086 | (insert-buffer-substring buf) | |
1087 | (mml-preview-insert-mail-followup-to) | |
1088 | (let ((message-deletable-headers (if (message-news-p) | |
1089 | nil | |
1090 | message-deletable-headers))) | |
1091 | (message-generate-headers | |
1092 | (copy-sequence (if (message-news-p) | |
1093 | message-required-news-headers | |
1094 | message-required-mail-headers)))) | |
1095 | (if (re-search-forward | |
1096 | (concat "^" (regexp-quote mail-header-separator) "\n") nil t) | |
1097 | (replace-match "\n")) | |
1098 | (let ((mail-header-separator ""));; mail-header-separator is removed. | |
1099 | (mml-to-mime)) | |
1100 | (if raw | |
1101 | (when (fboundp 'set-buffer-multibyte) | |
1102 | (let ((s (buffer-string))) | |
1103 | ;; Insert the content into unibyte buffer. | |
1104 | (erase-buffer) | |
1105 | (mm-disable-multibyte) | |
1106 | (insert s))) | |
1107 | (let ((gnus-newsgroup-charset (car message-posting-charset)) | |
1108 | gnus-article-prepare-hook gnus-original-article-buffer) | |
1109 | (run-hooks 'gnus-article-decode-hook) | |
1110 | (let ((gnus-newsgroup-name "dummy") | |
1111 | (gnus-newsrc-hashtb (or gnus-newsrc-hashtb | |
1112 | (gnus-make-hashtable 5)))) | |
1113 | (gnus-article-prepare-display)))) | |
1114 | ;; Disable article-mode-map. | |
1115 | (use-local-map nil) | |
1116 | (gnus-make-local-hook 'kill-buffer-hook) | |
1117 | (add-hook 'kill-buffer-hook | |
1118 | (lambda () | |
1119 | (mm-destroy-parts gnus-article-mime-handles)) nil t) | |
1120 | (setq buffer-read-only t) | |
1121 | (local-set-key "q" (lambda () (interactive) (kill-buffer nil))) | |
1122 | (local-set-key "=" (lambda () (interactive) (delete-other-windows))) | |
1123 | (local-set-key "\r" | |
1124 | (lambda () | |
1125 | (interactive) | |
1126 | (widget-button-press (point)))) | |
1127 | (local-set-key gnus-mouse-2 | |
1128 | (lambda (event) | |
1129 | (interactive "@e") | |
1130 | (widget-button-press (widget-event-point event) event))) | |
1131 | (goto-char (point-min))))) | |
c113de23 GM |
1132 | |
1133 | (defun mml-validate () | |
1134 | "Validate the current MML document." | |
1135 | (interactive) | |
1136 | (mml-parse)) | |
1137 | ||
23f87bed MB |
1138 | (defun mml-tweak-part (cont) |
1139 | "Tweak a MML part." | |
1140 | (let ((tweak (cdr (assq 'tweak cont))) | |
1141 | func) | |
1142 | (cond | |
1143 | (tweak | |
1144 | (setq func | |
1145 | (or (cdr (assoc tweak mml-tweak-function-alist)) | |
1146 | (intern tweak)))) | |
1147 | (mml-tweak-type-alist | |
1148 | (let ((alist mml-tweak-type-alist) | |
1149 | (type (or (cdr (assq 'type cont)) "text/plain"))) | |
1150 | (while alist | |
1151 | (if (string-match (caar alist) type) | |
1152 | (setq func (cdar alist) | |
1153 | alist nil) | |
1154 | (setq alist (cdr alist))))))) | |
1155 | (if func | |
1156 | (funcall func cont) | |
1157 | cont) | |
1158 | (let ((alist mml-tweak-sexp-alist)) | |
1159 | (while alist | |
1160 | (if (eval (caar alist)) | |
1161 | (funcall (cdar alist) cont)) | |
1162 | (setq alist (cdr alist))))) | |
1163 | cont) | |
1164 | ||
1165 | (defun mml-tweak-externalize-attachments (cont) | |
1166 | "Tweak attached files as external parts." | |
1167 | (let (filename-cons) | |
1168 | (when (and (eq (car cont) 'part) | |
1169 | (not (cdr (assq 'buffer cont))) | |
1170 | (and (setq filename-cons (assq 'filename cont)) | |
1171 | (not (equal (cdr (assq 'nofile cont)) "yes")))) | |
1172 | (setcar cont 'external) | |
1173 | (setcar filename-cons 'name)))) | |
1174 | ||
c113de23 GM |
1175 | (provide 'mml) |
1176 | ||
ab5796a9 | 1177 | ;;; arch-tag: 583c96cf-1ffe-451b-a5e5-4733ae9ddd12 |
c113de23 | 1178 | ;;; mml.el ends here |