Commit | Line | Data |
---|---|---|
c113de23 GM |
1 | ;;; mml.el --- A package for parsing and validating MML documents |
2 | ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. | |
3 | ||
4 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
9 | ;; the Free Software Foundation; either version 2, or (at your option) | |
10 | ;; any later version. | |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;;; Code: | |
25 | ||
26 | (require 'mm-util) | |
27 | (require 'mm-bodies) | |
28 | (require 'mm-encode) | |
29 | (require 'mm-decode) | |
30 | (eval-when-compile 'cl) | |
31 | ||
32 | (eval-and-compile | |
33 | (autoload 'message-make-message-id "message") | |
34 | (autoload 'gnus-setup-posting-charset "gnus-msg") | |
35 | (autoload 'message-fetch-field "message") | |
36 | (autoload 'message-posting-charset "message")) | |
37 | ||
38 | (defvar mml-generate-multipart-alist nil | |
39 | "*Alist of multipart generation functions. | |
40 | Each entry has the form (NAME . FUNCTION), where | |
41 | NAME is a string containing the name of the part (without the | |
42 | leading \"/multipart/\"), | |
43 | FUNCTION is a Lisp function which is called to generate the part. | |
44 | ||
45 | The Lisp function has to supply the appropriate MIME headers and the | |
46 | contents of this part.") | |
47 | ||
48 | (defvar mml-syntax-table | |
49 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
50 | (modify-syntax-entry ?\\ "/" table) | |
51 | (modify-syntax-entry ?< "(" table) | |
52 | (modify-syntax-entry ?> ")" table) | |
53 | (modify-syntax-entry ?@ "w" table) | |
54 | (modify-syntax-entry ?/ "w" table) | |
55 | (modify-syntax-entry ?= " " table) | |
56 | (modify-syntax-entry ?* " " table) | |
57 | (modify-syntax-entry ?\; " " table) | |
58 | (modify-syntax-entry ?\' " " table) | |
59 | table)) | |
60 | ||
61 | (defvar mml-boundary-function 'mml-make-boundary | |
62 | "A function called to suggest a boundary. | |
63 | The function may be called several times, and should try to make a new | |
64 | suggestion each time. The function is called with one parameter, | |
65 | which is a number that says how many times the function has been | |
66 | called for this message.") | |
67 | ||
68 | (defvar mml-confirmation-set nil | |
69 | "A list of symbols, each of which disables some warning. | |
70 | `unknown-encoding': always send messages contain characters with | |
71 | unknown encoding; `use-ascii': always use ASCII for those characters | |
72 | with unknown encoding; `multipart': always send messages with more than | |
73 | one charsets.") | |
74 | ||
75 | (defvar mml-generate-mime-preprocess-function nil | |
76 | "A function called before generating a mime part. | |
77 | The function is called with one parameter, which is the part to be | |
78 | generated.") | |
79 | ||
80 | (defvar mml-generate-mime-postprocess-function nil | |
81 | "A function called after generating a mime part. | |
82 | The function is called with one parameter, which is the generated part.") | |
83 | ||
84 | (defvar mml-generate-default-type "text/plain") | |
85 | ||
86 | (defvar mml-buffer-list nil) | |
87 | ||
88 | (defun mml-generate-new-buffer (name) | |
89 | (let ((buf (generate-new-buffer name))) | |
90 | (push buf mml-buffer-list) | |
91 | buf)) | |
92 | ||
93 | (defun mml-destroy-buffers () | |
94 | (let (kill-buffer-hook) | |
95 | (mapcar 'kill-buffer mml-buffer-list) | |
96 | (setq mml-buffer-list nil))) | |
97 | ||
98 | (defun mml-parse () | |
99 | "Parse the current buffer as an MML document." | |
100 | (goto-char (point-min)) | |
101 | (let ((table (syntax-table))) | |
102 | (unwind-protect | |
103 | (progn | |
104 | (set-syntax-table mml-syntax-table) | |
105 | (mml-parse-1)) | |
106 | (set-syntax-table table)))) | |
107 | ||
108 | (defun mml-parse-1 () | |
109 | "Parse the current buffer as an MML document." | |
110 | (let (struct tag point contents charsets warn use-ascii no-markup-p raw) | |
111 | (while (and (not (eobp)) | |
112 | (not (looking-at "<#/multipart"))) | |
113 | (cond | |
114 | ((looking-at "<#multipart") | |
115 | (push (nconc (mml-read-tag) (mml-parse-1)) struct)) | |
116 | ((looking-at "<#external") | |
117 | (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part)))) | |
118 | struct)) | |
119 | (t | |
120 | (if (or (looking-at "<#part") (looking-at "<#mml")) | |
121 | (setq tag (mml-read-tag) | |
122 | no-markup-p nil | |
123 | warn nil) | |
124 | (setq tag (list 'part '(type . "text/plain")) | |
125 | no-markup-p t | |
126 | warn t)) | |
127 | (setq raw (cdr (assq 'raw tag)) | |
128 | point (point) | |
ce9401f3 | 129 | contents (mml-read-part (eq 'mml (car tag))) |
c113de23 GM |
130 | charsets (if raw nil |
131 | (mm-find-mime-charset-region point (point)))) | |
132 | (when (and (not raw) (memq nil charsets)) | |
133 | (if (or (memq 'unknown-encoding mml-confirmation-set) | |
134 | (y-or-n-p | |
135 | "Warning: You message contains characters with unknown encoding. Really send?")) | |
136 | (if (setq use-ascii | |
137 | (or (memq 'use-ascii mml-confirmation-set) | |
138 | (y-or-n-p "Use ASCII as charset?"))) | |
139 | (setq charsets (delq nil charsets)) | |
140 | (setq warn nil)) | |
141 | (error "Edit your message to remove those characters"))) | |
142 | (if (or raw | |
143 | (eq 'mml (car tag)) | |
144 | (< (length charsets) 2)) | |
145 | (if (or (not no-markup-p) | |
146 | (string-match "[^ \t\r\n]" contents)) | |
147 | ;; Don't create blank parts. | |
148 | (push (nconc tag (list (cons 'contents contents))) | |
149 | struct)) | |
150 | (let ((nstruct (mml-parse-singlepart-with-multiple-charsets | |
151 | tag point (point) use-ascii))) | |
152 | (when (and warn | |
153 | (not (memq 'multipart mml-confirmation-set)) | |
154 | (not | |
155 | (y-or-n-p | |
156 | (format | |
157 | "Warning: Your message contains more than %d parts. Really send? " | |
158 | (length nstruct))))) | |
159 | (error "Edit your message to use only one charset")) | |
160 | (setq struct (nconc nstruct struct))))))) | |
161 | (unless (eobp) | |
162 | (forward-line 1)) | |
163 | (nreverse struct))) | |
164 | ||
165 | (defun mml-parse-singlepart-with-multiple-charsets | |
166 | (orig-tag beg end &optional use-ascii) | |
167 | (save-excursion | |
168 | (save-restriction | |
169 | (narrow-to-region beg end) | |
170 | (goto-char (point-min)) | |
171 | (let ((current (or (mm-mime-charset (mm-charset-after)) | |
172 | (and use-ascii 'us-ascii))) | |
173 | charset struct space newline paragraph) | |
174 | (while (not (eobp)) | |
175 | (setq charset (mm-mime-charset (mm-charset-after))) | |
176 | (cond | |
177 | ;; The charset remains the same. | |
178 | ((eq charset 'us-ascii)) | |
179 | ((or (and use-ascii (not charset)) | |
180 | (eq charset current)) | |
181 | (setq space nil | |
182 | newline nil | |
183 | paragraph nil)) | |
184 | ;; The initial charset was ascii. | |
185 | ((eq current 'us-ascii) | |
186 | (setq current charset | |
187 | space nil | |
188 | newline nil | |
189 | paragraph nil)) | |
190 | ;; We have a change in charsets. | |
191 | (t | |
192 | (push (append | |
193 | orig-tag | |
194 | (list (cons 'contents | |
195 | (buffer-substring-no-properties | |
196 | beg (or paragraph newline space (point)))))) | |
197 | struct) | |
198 | (setq beg (or paragraph newline space (point)) | |
199 | current charset | |
200 | space nil | |
201 | newline nil | |
202 | paragraph nil))) | |
203 | ;; Compute places where it might be nice to break the part. | |
204 | (cond | |
205 | ((memq (following-char) '(? ?\t)) | |
206 | (setq space (1+ (point)))) | |
207 | ((and (eq (following-char) ?\n) | |
208 | (not (bobp)) | |
209 | (eq (char-after (1- (point))) ?\n)) | |
210 | (setq paragraph (point))) | |
211 | ((eq (following-char) ?\n) | |
212 | (setq newline (1+ (point))))) | |
213 | (forward-char 1)) | |
214 | ;; Do the final part. | |
215 | (unless (= beg (point)) | |
216 | (push (append orig-tag | |
217 | (list (cons 'contents | |
218 | (buffer-substring-no-properties | |
219 | beg (point))))) | |
220 | struct)) | |
221 | struct)))) | |
222 | ||
223 | (defun mml-read-tag () | |
224 | "Read a tag and return the contents." | |
225 | (let (contents name elem val) | |
226 | (forward-char 2) | |
227 | (setq name (buffer-substring-no-properties | |
228 | (point) (progn (forward-sexp 1) (point)))) | |
229 | (skip-chars-forward " \t\n") | |
230 | (while (not (looking-at ">")) | |
231 | (setq elem (buffer-substring-no-properties | |
232 | (point) (progn (forward-sexp 1) (point)))) | |
233 | (skip-chars-forward "= \t\n") | |
234 | (setq val (buffer-substring-no-properties | |
235 | (point) (progn (forward-sexp 1) (point)))) | |
236 | (when (string-match "^\"\\(.*\\)\"$" val) | |
237 | (setq val (match-string 1 val))) | |
238 | (push (cons (intern elem) val) contents) | |
239 | (skip-chars-forward " \t\n")) | |
240 | (forward-char 1) | |
241 | (skip-chars-forward " \t\n") | |
242 | (cons (intern name) (nreverse contents)))) | |
243 | ||
244 | (defun mml-read-part (&optional mml) | |
245 | "Return the buffer up till the next part, multipart or closing part or multipart. | |
246 | If MML is non-nil, return the buffer up till the correspondent mml tag." | |
247 | (let ((beg (point)) (count 1)) | |
248 | ;; If the tag ended at the end of the line, we go to the next line. | |
249 | (when (looking-at "[ \t]*\n") | |
250 | (forward-line 1)) | |
251 | (if mml | |
252 | (progn | |
253 | (while (and (> count 0) (not (eobp))) | |
254 | (if (re-search-forward "<#\\(/\\)?mml." nil t) | |
255 | (setq count (+ count (if (match-beginning 1) -1 1))) | |
256 | (goto-char (point-max)))) | |
257 | (buffer-substring-no-properties beg (if (> count 0) | |
258 | (point) | |
259 | (match-beginning 0)))) | |
260 | (if (re-search-forward | |
261 | "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t) | |
262 | (prog1 | |
263 | (buffer-substring-no-properties beg (match-beginning 0)) | |
264 | (if (or (not (match-beginning 1)) | |
265 | (equal (match-string 2) "multipart")) | |
266 | (goto-char (match-beginning 0)) | |
267 | (when (looking-at "[ \t]*\n") | |
268 | (forward-line 1)))) | |
269 | (buffer-substring-no-properties beg (goto-char (point-max))))))) | |
270 | ||
271 | (defvar mml-boundary nil) | |
272 | (defvar mml-base-boundary "-=-=") | |
273 | (defvar mml-multipart-number 0) | |
274 | ||
275 | (defun mml-generate-mime () | |
276 | "Generate a MIME message based on the current MML document." | |
277 | (let ((cont (mml-parse)) | |
278 | (mml-multipart-number mml-multipart-number)) | |
279 | (if (not cont) | |
280 | nil | |
281 | (with-temp-buffer | |
282 | (if (and (consp (car cont)) | |
283 | (= (length cont) 1)) | |
284 | (mml-generate-mime-1 (car cont)) | |
285 | (mml-generate-mime-1 (nconc (list 'multipart '(type . "mixed")) | |
286 | cont))) | |
287 | (buffer-string))))) | |
288 | ||
289 | (defun mml-generate-mime-1 (cont) | |
290 | (save-restriction | |
291 | (narrow-to-region (point) (point)) | |
292 | (if mml-generate-mime-preprocess-function | |
293 | (funcall mml-generate-mime-preprocess-function cont)) | |
294 | (cond | |
295 | ((or (eq (car cont) 'part) (eq (car cont) 'mml)) | |
296 | (let ((raw (cdr (assq 'raw cont))) | |
297 | coded encoding charset filename type) | |
298 | (setq type (or (cdr (assq 'type cont)) "text/plain")) | |
299 | (if (and (not raw) | |
300 | (member (car (split-string type "/")) '("text" "message"))) | |
301 | (with-temp-buffer | |
302 | (cond | |
303 | ((cdr (assq 'buffer cont)) | |
304 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
305 | ((and (setq filename (cdr (assq 'filename cont))) | |
306 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
307 | (mm-insert-file-contents filename)) | |
308 | ((eq 'mml (car cont)) | |
309 | (insert (cdr (assq 'contents cont)))) | |
310 | (t | |
311 | (save-restriction | |
312 | (narrow-to-region (point) (point)) | |
313 | (insert (cdr (assq 'contents cont))) | |
314 | ;; Remove quotes from quoted tags. | |
315 | (goto-char (point-min)) | |
316 | (while (re-search-forward | |
317 | "<#!+/?\\(part\\|multipart\\|external\\|mml\\)" nil t) | |
318 | (delete-region (+ (match-beginning 0) 2) | |
319 | (+ (match-beginning 0) 3)))))) | |
320 | (cond | |
321 | ((eq (car cont) 'mml) | |
322 | (let ((mml-boundary (funcall mml-boundary-function | |
323 | (incf mml-multipart-number))) | |
324 | (mml-generate-default-type "text/plain")) | |
325 | (mml-to-mime)) | |
326 | (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b"))) | |
327 | ;; ignore 0x1b, it is part of iso-2022-jp | |
328 | (setq encoding (mm-body-7-or-8)))) | |
329 | ((string= (car (split-string type "/")) "message") | |
330 | (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b"))) | |
331 | ;; ignore 0x1b, it is part of iso-2022-jp | |
332 | (setq encoding (mm-body-7-or-8)))) | |
333 | (t | |
334 | (setq charset (mm-encode-body)) | |
335 | (setq encoding (mm-body-encoding | |
336 | charset (cdr (assq 'encoding cont)))))) | |
337 | (setq coded (buffer-string))) | |
338 | (mm-with-unibyte-buffer | |
339 | (cond | |
340 | ((cdr (assq 'buffer cont)) | |
341 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
342 | ((and (setq filename (cdr (assq 'filename cont))) | |
343 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
344 | (let ((coding-system-for-read mm-binary-coding-system)) | |
345 | (mm-insert-file-contents filename nil nil nil nil t))) | |
346 | (t | |
347 | (insert (cdr (assq 'contents cont))))) | |
348 | (setq encoding (mm-encode-buffer type) | |
349 | coded (buffer-string)))) | |
350 | (mml-insert-mime-headers cont type charset encoding) | |
351 | (insert "\n") | |
ce9401f3 | 352 | (insert coded))) |
c113de23 GM |
353 | ((eq (car cont) 'external) |
354 | (insert "Content-Type: message/external-body") | |
355 | (let ((parameters (mml-parameter-string | |
356 | cont '(expiration size permission))) | |
357 | (name (cdr (assq 'name cont)))) | |
358 | (when name | |
359 | (setq name (mml-parse-file-name name)) | |
360 | (if (stringp name) | |
361 | (mml-insert-parameter | |
362 | (mail-header-encode-parameter "name" name) | |
363 | "access-type=local-file") | |
364 | (mml-insert-parameter | |
365 | (mail-header-encode-parameter | |
366 | "name" (file-name-nondirectory (nth 2 name))) | |
367 | (mail-header-encode-parameter "site" (nth 1 name)) | |
368 | (mail-header-encode-parameter | |
369 | "directory" (file-name-directory (nth 2 name)))) | |
370 | (mml-insert-parameter | |
371 | (concat "access-type=" | |
372 | (if (member (nth 0 name) '("ftp@" "anonymous@")) | |
373 | "anon-ftp" | |
374 | "ftp"))))) | |
375 | (when parameters | |
376 | (mml-insert-parameter-string | |
377 | cont '(expiration size permission)))) | |
378 | (insert "\n\n") | |
379 | (insert "Content-Type: " (cdr (assq 'type cont)) "\n") | |
380 | (insert "Content-ID: " (message-make-message-id) "\n") | |
381 | (insert "Content-Transfer-Encoding: " | |
382 | (or (cdr (assq 'encoding cont)) "binary")) | |
383 | (insert "\n\n") | |
384 | (insert (or (cdr (assq 'contents cont)))) | |
385 | (insert "\n")) | |
386 | ((eq (car cont) 'multipart) | |
387 | (let* ((type (or (cdr (assq 'type cont)) "mixed")) | |
388 | (mml-generate-default-type (if (equal type "digest") | |
389 | "message/rfc822" | |
390 | "text/plain")) | |
391 | (handler (assoc type mml-generate-multipart-alist))) | |
392 | (if handler | |
393 | (funcall (cdr handler) cont) | |
394 | ;; No specific handler. Use default one. | |
395 | (let ((mml-boundary (mml-compute-boundary cont))) | |
396 | (insert (format "Content-Type: multipart/%s; boundary=\"%s\"\n" | |
397 | type mml-boundary)) | |
398 | ;; Skip `multipart' and `type' elements. | |
399 | (setq cont (cddr cont)) | |
400 | (while cont | |
401 | (insert "\n--" mml-boundary "\n") | |
402 | (mml-generate-mime-1 (pop cont))) | |
403 | (insert "\n--" mml-boundary "--\n"))))) | |
404 | (t | |
405 | (error "Invalid element: %S" cont))) | |
406 | (if mml-generate-mime-postprocess-function | |
407 | (funcall mml-generate-mime-postprocess-function cont)))) | |
408 | ||
409 | (defun mml-compute-boundary (cont) | |
410 | "Return a unique boundary that does not exist in CONT." | |
411 | (let ((mml-boundary (funcall mml-boundary-function | |
412 | (incf mml-multipart-number)))) | |
413 | ;; This function tries again and again until it has found | |
414 | ;; a unique boundary. | |
415 | (while (not (catch 'not-unique | |
416 | (mml-compute-boundary-1 cont)))) | |
417 | mml-boundary)) | |
418 | ||
419 | (defun mml-compute-boundary-1 (cont) | |
420 | (let (filename) | |
421 | (cond | |
422 | ((eq (car cont) 'part) | |
423 | (with-temp-buffer | |
424 | (cond | |
425 | ((cdr (assq 'buffer cont)) | |
426 | (insert-buffer-substring (cdr (assq 'buffer cont)))) | |
427 | ((and (setq filename (cdr (assq 'filename cont))) | |
428 | (not (equal (cdr (assq 'nofile cont)) "yes"))) | |
429 | (mm-insert-file-contents filename)) | |
430 | (t | |
431 | (insert (cdr (assq 'contents cont))))) | |
432 | (goto-char (point-min)) | |
433 | (when (re-search-forward (concat "^--" (regexp-quote mml-boundary)) | |
434 | nil t) | |
435 | (setq mml-boundary (funcall mml-boundary-function | |
436 | (incf mml-multipart-number))) | |
437 | (throw 'not-unique nil)))) | |
438 | ((eq (car cont) 'multipart) | |
439 | (mapcar 'mml-compute-boundary-1 (cddr cont)))) | |
440 | t)) | |
441 | ||
442 | (defun mml-make-boundary (number) | |
443 | (concat (make-string (% number 60) ?=) | |
444 | (if (> number 17) | |
445 | (format "%x" number) | |
446 | "") | |
447 | mml-base-boundary)) | |
448 | ||
449 | (defun mml-insert-mime-headers (cont type charset encoding) | |
450 | (let (parameters disposition description) | |
451 | (setq parameters | |
452 | (mml-parameter-string | |
453 | cont '(name access-type expiration size permission))) | |
454 | (when (or charset | |
455 | parameters | |
456 | (not (equal type mml-generate-default-type))) | |
457 | (when (consp charset) | |
458 | (error | |
459 | "Can't encode a part with several charsets.")) | |
460 | (insert "Content-Type: " type) | |
461 | (when charset | |
462 | (insert "; " (mail-header-encode-parameter | |
463 | "charset" (symbol-name charset)))) | |
464 | (when parameters | |
465 | (mml-insert-parameter-string | |
466 | cont '(name access-type expiration size permission))) | |
467 | (insert "\n")) | |
468 | (setq parameters | |
469 | (mml-parameter-string | |
470 | cont '(filename creation-date modification-date read-date))) | |
471 | (when (or (setq disposition (cdr (assq 'disposition cont))) | |
472 | parameters) | |
473 | (insert "Content-Disposition: " (or disposition "inline")) | |
474 | (when parameters | |
475 | (mml-insert-parameter-string | |
476 | cont '(filename creation-date modification-date read-date))) | |
477 | (insert "\n")) | |
478 | (unless (eq encoding '7bit) | |
479 | (insert (format "Content-Transfer-Encoding: %s\n" encoding))) | |
480 | (when (setq description (cdr (assq 'description cont))) | |
481 | (insert "Content-Description: " | |
482 | (mail-encode-encoded-word-string description) "\n")))) | |
483 | ||
484 | (defun mml-parameter-string (cont types) | |
485 | (let ((string "") | |
486 | value type) | |
487 | (while (setq type (pop types)) | |
488 | (when (setq value (cdr (assq type cont))) | |
489 | ;; Strip directory component from the filename parameter. | |
490 | (when (eq type 'filename) | |
491 | (setq value (file-name-nondirectory value))) | |
492 | (setq string (concat string "; " | |
493 | (mail-header-encode-parameter | |
494 | (symbol-name type) value))))) | |
495 | (when (not (zerop (length string))) | |
496 | string))) | |
497 | ||
498 | (defun mml-insert-parameter-string (cont types) | |
499 | (let (value type) | |
500 | (while (setq type (pop types)) | |
501 | (when (setq value (cdr (assq type cont))) | |
502 | ;; Strip directory component from the filename parameter. | |
503 | (when (eq type 'filename) | |
504 | (setq value (file-name-nondirectory value))) | |
505 | (mml-insert-parameter | |
506 | (mail-header-encode-parameter | |
507 | (symbol-name type) value)))))) | |
508 | ||
509 | (defvar ange-ftp-name-format) | |
510 | (defvar efs-path-regexp) | |
511 | (defun mml-parse-file-name (path) | |
512 | (if (if (boundp 'efs-path-regexp) | |
513 | (string-match efs-path-regexp path) | |
514 | (if (boundp 'ange-ftp-name-format) | |
515 | (string-match (car ange-ftp-name-format) path))) | |
516 | (list (match-string 1 path) (match-string 2 path) | |
517 | (substring path (1+ (match-end 2)))) | |
518 | path)) | |
519 | ||
520 | (defun mml-insert-buffer (buffer) | |
521 | "Insert BUFFER at point and quote any MML markup." | |
522 | (save-restriction | |
523 | (narrow-to-region (point) (point)) | |
524 | (insert-buffer-substring buffer) | |
525 | (mml-quote-region (point-min) (point-max)) | |
526 | (goto-char (point-max)))) | |
527 | ||
528 | ;;; | |
529 | ;;; Transforming MIME to MML | |
530 | ;;; | |
531 | ||
532 | (defun mime-to-mml () | |
533 | "Translate the current buffer (which should be a message) into MML." | |
534 | ;; First decode the head. | |
535 | (save-restriction | |
536 | (message-narrow-to-head) | |
537 | (mail-decode-encoded-word-region (point-min) (point-max))) | |
538 | (let ((handles (mm-dissect-buffer t))) | |
539 | (goto-char (point-min)) | |
540 | (search-forward "\n\n" nil t) | |
541 | (delete-region (point) (point-max)) | |
542 | (if (stringp (car handles)) | |
543 | (mml-insert-mime handles) | |
544 | (mml-insert-mime handles t)) | |
545 | (mm-destroy-parts handles)) | |
546 | (save-restriction | |
547 | (message-narrow-to-head) | |
548 | ;; Remove them, they are confusing. | |
549 | (message-remove-header "Content-Type") | |
550 | (message-remove-header "MIME-Version") | |
551 | (message-remove-header "Content-Transfer-Encoding"))) | |
552 | ||
553 | (defun mml-to-mime () | |
554 | "Translate the current buffer from MML to MIME." | |
555 | (message-encode-message-body) | |
556 | (save-restriction | |
557 | (message-narrow-to-headers-or-head) | |
558 | (let ((mail-parse-charset message-default-charset)) | |
559 | (mail-encode-encoded-word-buffer)))) | |
560 | ||
561 | (defun mml-insert-mime (handle &optional no-markup) | |
562 | (let (textp buffer mmlp) | |
563 | ;; Determine type and stuff. | |
564 | (unless (stringp (car handle)) | |
565 | (unless (setq textp (equal (mm-handle-media-supertype handle) "text")) | |
566 | (save-excursion | |
567 | (set-buffer (setq buffer (mml-generate-new-buffer " *mml*"))) | |
568 | (mm-insert-part handle) | |
569 | (if (setq mmlp (equal (mm-handle-media-type handle) | |
570 | "message/rfc822")) | |
571 | (mime-to-mml))))) | |
572 | (if mmlp | |
573 | (mml-insert-mml-markup handle nil t t) | |
574 | (unless (and no-markup | |
575 | (equal (mm-handle-media-type handle) "text/plain")) | |
576 | (mml-insert-mml-markup handle buffer textp))) | |
577 | (cond | |
578 | (mmlp | |
579 | (insert-buffer buffer) | |
580 | (goto-char (point-max)) | |
581 | (insert "<#/mml>\n")) | |
582 | ((stringp (car handle)) | |
583 | (mapcar 'mml-insert-mime (cdr handle)) | |
584 | (insert "<#/multipart>\n")) | |
585 | (textp | |
586 | (let ((text (mm-get-part handle)) | |
587 | (charset (mail-content-type-get | |
588 | (mm-handle-type handle) 'charset))) | |
589 | (insert (mm-decode-string text charset))) | |
590 | (goto-char (point-max))) | |
591 | (t | |
592 | (insert "<#/part>\n"))))) | |
593 | ||
594 | (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp) | |
595 | "Take a MIME handle and insert an MML tag." | |
596 | (if (stringp (car handle)) | |
597 | (insert "<#multipart type=" (mm-handle-media-subtype handle) | |
598 | ">\n") | |
599 | (if mmlp | |
600 | (insert "<#mml type=" (mm-handle-media-type handle)) | |
601 | (insert "<#part type=" (mm-handle-media-type handle))) | |
602 | (dolist (elem (append (cdr (mm-handle-type handle)) | |
603 | (cdr (mm-handle-disposition handle)))) | |
604 | (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")) | |
605 | (when (mm-handle-disposition handle) | |
606 | (insert " disposition=" (car (mm-handle-disposition handle)))) | |
607 | (when buffer | |
608 | (insert " buffer=\"" (buffer-name buffer) "\"")) | |
609 | (when nofile | |
610 | (insert " nofile=yes")) | |
611 | (when (mm-handle-description handle) | |
612 | (insert " description=\"" (mm-handle-description handle) "\"")) | |
613 | (insert ">\n"))) | |
614 | ||
615 | (defun mml-insert-parameter (&rest parameters) | |
616 | "Insert PARAMETERS in a nice way." | |
617 | (dolist (param parameters) | |
618 | (insert ";") | |
619 | (let ((point (point))) | |
620 | (insert " " param) | |
621 | (when (> (current-column) 71) | |
622 | (goto-char point) | |
623 | (insert "\n ") | |
624 | (end-of-line))))) | |
625 | ||
626 | ;;; | |
627 | ;;; Mode for inserting and editing MML forms | |
628 | ;;; | |
629 | ||
630 | (defvar mml-mode-map | |
631 | (let ((map (make-sparse-keymap)) | |
632 | (main (make-sparse-keymap))) | |
633 | (define-key map "f" 'mml-attach-file) | |
634 | (define-key map "b" 'mml-attach-buffer) | |
635 | (define-key map "e" 'mml-attach-external) | |
636 | (define-key map "q" 'mml-quote-region) | |
637 | (define-key map "m" 'mml-insert-multipart) | |
638 | (define-key map "p" 'mml-insert-part) | |
639 | (define-key map "v" 'mml-validate) | |
640 | (define-key map "P" 'mml-preview) | |
641 | ;;(define-key map "n" 'mml-narrow-to-part) | |
642 | (define-key main "\M-m" map) | |
643 | main)) | |
644 | ||
645 | (easy-menu-define | |
646 | mml-menu mml-mode-map "" | |
647 | '("MML" | |
648 | ("Attach" | |
649 | ["File" mml-attach-file t] | |
650 | ["Buffer" mml-attach-buffer t] | |
651 | ["External" mml-attach-external t]) | |
652 | ("Insert" | |
653 | ["Multipart" mml-insert-multipart t] | |
654 | ["Part" mml-insert-part t]) | |
655 | ;;["Narrow" mml-narrow-to-part t] | |
656 | ["Quote" mml-quote-region t] | |
657 | ["Validate" mml-validate t] | |
658 | ["Preview" mml-preview t])) | |
659 | ||
660 | (defvar mml-mode nil | |
661 | "Minor mode for editing MML.") | |
662 | ||
663 | (defun mml-mode (&optional arg) | |
664 | "Minor mode for editing MML. | |
665 | ||
666 | \\{mml-mode-map}" | |
667 | (interactive "P") | |
668 | (if (not (set (make-local-variable 'mml-mode) | |
669 | (if (null arg) (not mml-mode) | |
670 | (> (prefix-numeric-value arg) 0)))) | |
671 | nil | |
672 | (set (make-local-variable 'mml-mode) t) | |
673 | (unless (assq 'mml-mode minor-mode-alist) | |
674 | (push `(mml-mode " MML") minor-mode-alist)) | |
675 | (unless (assq 'mml-mode minor-mode-map-alist) | |
676 | (push (cons 'mml-mode mml-mode-map) | |
677 | minor-mode-map-alist))) | |
678 | (run-hooks 'mml-mode-hook)) | |
679 | ||
680 | ;;; | |
681 | ;;; Helper functions for reading MIME stuff from the minibuffer and | |
682 | ;;; inserting stuff to the buffer. | |
683 | ;;; | |
684 | ||
685 | (defun mml-minibuffer-read-file (prompt) | |
686 | (let ((file (read-file-name prompt nil nil t))) | |
687 | ;; Prevent some common errors. This is inspired by similar code in | |
688 | ;; VM. | |
689 | (when (file-directory-p file) | |
690 | (error "%s is a directory, cannot attach" file)) | |
691 | (unless (file-exists-p file) | |
692 | (error "No such file: %s" file)) | |
693 | (unless (file-readable-p file) | |
694 | (error "Permission denied: %s" file)) | |
695 | file)) | |
696 | ||
697 | (defun mml-minibuffer-read-type (name &optional default) | |
698 | (mailcap-parse-mimetypes) | |
699 | (let* ((default (or default | |
700 | (mm-default-file-encoding name) | |
701 | ;; Perhaps here we should check what the file | |
702 | ;; looks like, and offer text/plain if it looks | |
703 | ;; like text/plain. | |
704 | "application/octet-stream")) | |
705 | (string (completing-read | |
706 | (format "Content type (default %s): " default) | |
707 | (mapcar | |
708 | 'list | |
709 | (mm-delete-duplicates | |
710 | (nconc | |
711 | (mapcar 'cdr mailcap-mime-extensions) | |
712 | (apply | |
713 | 'nconc | |
714 | (mapcar | |
715 | (lambda (l) | |
716 | (delq nil | |
717 | (mapcar | |
718 | (lambda (m) | |
719 | (let ((type (cdr (assq 'type (cdr m))))) | |
720 | (if (equal (cadr (split-string type "/")) | |
721 | "*") | |
722 | nil | |
723 | type))) | |
724 | (cdr l)))) | |
725 | mailcap-mime-data)))))))) | |
726 | (if (not (equal string "")) | |
727 | string | |
728 | default))) | |
729 | ||
730 | (defun mml-minibuffer-read-description () | |
731 | (let ((description (read-string "One line description: "))) | |
732 | (when (string-match "\\`[ \t]*\\'" description) | |
733 | (setq description nil)) | |
734 | description)) | |
735 | ||
736 | (defun mml-quote-region (beg end) | |
737 | "Quote the MML tags in the region." | |
738 | (interactive "r") | |
739 | (save-excursion | |
740 | (save-restriction | |
741 | ;; Temporarily narrow the region to defend from changes | |
742 | ;; invalidating END. | |
743 | (narrow-to-region beg end) | |
744 | (goto-char (point-min)) | |
745 | ;; Quote parts. | |
746 | (while (re-search-forward | |
747 | "<#!*/?\\(multipart\\|part\\|external\\|mml\\)" nil t) | |
748 | ;; Insert ! after the #. | |
749 | (goto-char (+ (match-beginning 0) 2)) | |
750 | (insert "!"))))) | |
751 | ||
752 | (defun mml-insert-tag (name &rest plist) | |
753 | "Insert an MML tag described by NAME and PLIST." | |
754 | (when (symbolp name) | |
755 | (setq name (symbol-name name))) | |
756 | (insert "<#" name) | |
757 | (while plist | |
758 | (let ((key (pop plist)) | |
759 | (value (pop plist))) | |
760 | (when value | |
761 | ;; Quote VALUE if it contains suspicious characters. | |
762 | (when (string-match "[\"'\\~/*;() \t\n]" value) | |
763 | (setq value (prin1-to-string value))) | |
764 | (insert (format " %s=%s" key value))))) | |
765 | (insert ">\n")) | |
766 | ||
767 | (defun mml-insert-empty-tag (name &rest plist) | |
768 | "Insert an empty MML tag described by NAME and PLIST." | |
769 | (when (symbolp name) | |
770 | (setq name (symbol-name name))) | |
771 | (apply #'mml-insert-tag name plist) | |
772 | (insert "<#/" name ">\n")) | |
773 | ||
774 | ;;; Attachment functions. | |
775 | ||
776 | (defun mml-attach-file (file &optional type description) | |
777 | "Attach a file to the outgoing MIME message. | |
778 | The file is not inserted or encoded until you send the message with | |
779 | `\\[message-send-and-exit]' or `\\[message-send]'. | |
780 | ||
781 | FILE is the name of the file to attach. TYPE is its content-type, a | |
782 | string of the form \"type/subtype\". DESCRIPTION is a one-line | |
783 | description of the attachment." | |
784 | (interactive | |
785 | (let* ((file (mml-minibuffer-read-file "Attach file: ")) | |
786 | (type (mml-minibuffer-read-type file)) | |
787 | (description (mml-minibuffer-read-description))) | |
788 | (list file type description))) | |
789 | (mml-insert-empty-tag 'part 'type type 'filename file | |
790 | 'disposition "attachment" 'description description)) | |
791 | ||
792 | (defun mml-attach-buffer (buffer &optional type description) | |
793 | "Attach a buffer to the outgoing MIME message. | |
794 | See `mml-attach-file' for details of operation." | |
795 | (interactive | |
796 | (let* ((buffer (read-buffer "Attach buffer: ")) | |
797 | (type (mml-minibuffer-read-type buffer "text/plain")) | |
798 | (description (mml-minibuffer-read-description))) | |
799 | (list buffer type description))) | |
800 | (mml-insert-empty-tag 'part 'type type 'buffer buffer | |
801 | 'disposition "attachment" 'description description)) | |
802 | ||
803 | (defun mml-attach-external (file &optional type description) | |
804 | "Attach an external file into the buffer. | |
805 | FILE is an ange-ftp/efs specification of the part location. | |
806 | TYPE is the MIME type to use." | |
807 | (interactive | |
808 | (let* ((file (mml-minibuffer-read-file "Attach external file: ")) | |
809 | (type (mml-minibuffer-read-type file)) | |
810 | (description (mml-minibuffer-read-description))) | |
811 | (list file type description))) | |
812 | (mml-insert-empty-tag 'external 'type type 'name file | |
813 | 'disposition "attachment" 'description description)) | |
814 | ||
815 | (defun mml-insert-multipart (&optional type) | |
816 | (interactive (list (completing-read "Multipart type (default mixed): " | |
817 | '(("mixed") ("alternative") ("digest") ("parallel") | |
818 | ("signed") ("encrypted")) | |
819 | nil nil "mixed"))) | |
820 | (or type | |
821 | (setq type "mixed")) | |
822 | (mml-insert-empty-tag "multipart" 'type type) | |
823 | (forward-line -1)) | |
824 | ||
825 | (defun mml-insert-part (&optional type) | |
826 | (interactive | |
827 | (list (mml-minibuffer-read-type ""))) | |
828 | (mml-insert-tag 'part 'type type 'disposition "inline") | |
829 | (forward-line -1)) | |
830 | ||
831 | (defun mml-preview (&optional raw) | |
832 | "Display current buffer with Gnus, in a new buffer. | |
833 | If RAW, don't highlight the article." | |
834 | (interactive "P") | |
835 | (let ((buf (current-buffer)) | |
836 | (message-posting-charset (or (gnus-setup-posting-charset | |
837 | (save-restriction | |
838 | (message-narrow-to-headers-or-head) | |
839 | (message-fetch-field "Newsgroups"))) | |
840 | message-posting-charset))) | |
841 | (switch-to-buffer (get-buffer-create | |
842 | (concat (if raw "*Raw MIME preview of " | |
843 | "*MIME preview of ") (buffer-name)))) | |
844 | (erase-buffer) | |
845 | (insert-buffer buf) | |
846 | (if (re-search-forward | |
847 | (concat "^" (regexp-quote mail-header-separator) "\n") nil t) | |
848 | (replace-match "\n")) | |
849 | (mml-to-mime) | |
850 | (if raw | |
ce9401f3 DL |
851 | (when (fboundp 'set-buffer-multibyte) |
852 | (let ((s (buffer-string))) | |
853 | ;; Insert the content into unibyte buffer. | |
854 | (erase-buffer) | |
855 | (mm-disable-multibyte) | |
856 | (insert s))) | |
c113de23 GM |
857 | (let ((gnus-newsgroup-charset (car message-posting-charset))) |
858 | (run-hooks 'gnus-article-decode-hook) | |
859 | (let ((gnus-newsgroup-name "dummy")) | |
860 | (gnus-article-prepare-display)))) | |
861 | (fundamental-mode) | |
862 | (setq buffer-read-only t) | |
863 | (goto-char (point-min)))) | |
864 | ||
865 | (defun mml-validate () | |
866 | "Validate the current MML document." | |
867 | (interactive) | |
868 | (mml-parse)) | |
869 | ||
870 | (provide 'mml) | |
871 | ||
872 | ;;; mml.el ends here |