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