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