merge emacs-23
[bpt/emacs.git] / lisp / mail / rmailmm.el
CommitLineData
537ab246
BG
1;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
2
5df4f04c 3;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
537ab246 4
6ea97e7e
GM
5;; Author: Alexander Pohoyda
6;; Alex Schroeder
537ab246
BG
7;; Maintainer: FSF
8;; Keywords: mail
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Essentially based on the design of Alexander Pohoyda's MIME
73422054 28;; extensions (mime-display.el and mime.el).
d1be4ec2
KH
29
30;; This file provides two operation modes for viewing a MIME message.
31
32;; (1) When rmail-enable-mime is non-nil (now it is the default), the
33;; function `rmail-show-mime' is automatically called. That function
34;; shows a MIME message directly in RMAIL's view buffer.
35
36;; (2) When rmail-enable-mime is nil, the command 'v' (or M-x
37;; rmail-mime) shows a MIME message in a new buffer "*RMAIL*".
38
39;; Both operations share the intermediate functions rmail-mime-process
40;; and rmail-mime-process-multipart as below.
41
42;; rmail-show-mime
43;; +- rmail-mime-parse
44;; | +- rmail-mime-process <--+------------+
45;; | | +---------+ |
46;; | + rmail-mime-process-multipart --+
47;; |
48;; + rmail-mime-insert <----------------+
49;; +- rmail-mime-insert-text |
50;; +- rmail-mime-insert-bulk |
51;; +- rmail-mime-insert-multipart --+
52;;
53;; rmail-mime
54;; +- rmail-mime-show <----------------------------------+
55;; +- rmail-mime-process |
56;; +- rmail-mime-handle |
57;; +- rmail-mime-text-handler |
58;; +- rmail-mime-bulk-handler |
59;; | + rmail-mime-insert-bulk
60;; +- rmail-mime-multipart-handler |
61;; +- rmail-mime-process-multipart --+
62
63;; In addition, for the case of rmail-enable-mime being non-nil, this
64;; file provides two functions rmail-insert-mime-forwarded-message and
65;; rmail-insert-mime-resent-message for composing forwarded and resent
66;; messages respectively.
537ab246 67
2e9075d3
GM
68;; Todo:
69
d1be4ec2
KH
70;; Make rmail-mime-media-type-handlers-alist usable in the first
71;; operation mode.
72;; Handle multipart/alternative in the second operation mode.
a92cdd49 73;; Offer the option to call external/internal viewers (doc-view, xpdf, etc).
2e9075d3 74
537ab246
BG
75;;; Code:
76
77(require 'rmail)
78(require 'mail-parse)
d1be4ec2 79(require 'message)
537ab246 80
f4ce6150 81;;; User options.
537ab246 82
e8652dd9
GM
83(defgroup rmail-mime nil
84 "Rmail MIME handling options."
85 :prefix "rmail-mime-"
86 :group 'rmail)
87
537ab246
BG
88(defcustom rmail-mime-media-type-handlers-alist
89 '(("multipart/.*" rmail-mime-multipart-handler)
90 ("text/.*" rmail-mime-text-handler)
91 ("text/\\(x-\\)?patch" rmail-mime-bulk-handler)
e8652dd9 92 ("\\(image\\|audio\\|video\\|application\\)/.*" rmail-mime-bulk-handler))
f4ce6150 93 "Functions to handle various content types.
c8644de0
GM
94This is an alist with elements of the form (REGEXP FUNCTION ...).
95The first item is a regular expression matching a content-type.
96The remaining elements are handler functions to run, in order of
2e9075d3
GM
97decreasing preference. These are called until one returns non-nil.
98Note that this only applies to items with an inline Content-Disposition,
186f7f0b
KH
99all others are handled by `rmail-mime-bulk-handler'.
100Note also that this alist is ignored when the variable
101`rmail-enable-mime' is non-nil."
c8644de0 102 :type '(alist :key-type regexp :value-type (repeat function))
e8652dd9
GM
103 :version "23.1"
104 :group 'rmail-mime)
537ab246
BG
105
106(defcustom rmail-mime-attachment-dirs-alist
f265b4de 107 `(("text/.*" "~/Documents")
537ab246 108 ("image/.*" "~/Pictures")
f265b4de 109 (".*" "~/Desktop" "~" ,temporary-file-directory))
f4ce6150
GM
110 "Default directories to save attachments of various types into.
111This is an alist with elements of the form (REGEXP DIR ...).
112The first item is a regular expression matching a content-type.
113The remaining elements are directories, in order of decreasing preference.
114The first directory that exists is used."
115 :type '(alist :key-type regexp :value-type (repeat directory))
116 :version "23.1"
e8652dd9
GM
117 :group 'rmail-mime)
118
119(defcustom rmail-mime-show-images 'button
120 "What to do with image attachments that Emacs is capable of displaying.
121If nil, do nothing special. If `button', add an extra button
a92cdd49
GM
122that when pushed displays the image in the buffer. If a number,
123automatically show images if they are smaller than that size (in
124bytes), otherwise add a display button. Anything else means to
125automatically display the image in the buffer."
e8652dd9
GM
126 :type '(choice (const :tag "Add button to view image" button)
127 (const :tag "No special treatment" nil)
a92cdd49 128 (number :tag "Show if smaller than certain size")
e8652dd9
GM
129 (other :tag "Always show" show))
130 :version "23.2"
131 :group 'rmail-mime)
537ab246 132
f4ce6150
GM
133;;; End of user options.
134
186f7f0b
KH
135;;; Global variables that always have let-binding when referred.
136
137(defvar rmail-mime-mbox-buffer nil
138 "Buffer containing the mbox data.
139The value is usually nil, and bound to a proper value while
140processing MIME.")
141
142(defvar rmail-mime-view-buffer nil
143 "Buffer showing a message.
144The value is usually nil, and bound to a proper value while
145processing MIME.")
146
147(defvar rmail-mime-coding-system nil
148 "The first coding-system used for decoding a MIME entity.
149The value is usually nil, and bound to non-nil while inserting
150MIME entities.")
151
d1be4ec2
KH
152;;; MIME-entity object
153
154(defun rmail-mime-entity (type disposition transfer-encoding
186f7f0b
KH
155 display header tagline body children handler)
156 "Retrun a newly created MIME-entity object from arguments.
d1be4ec2 157
186f7f0b 158A MIME-entity is a vector of 9 elements:
d1be4ec2 159
186f7f0b
KH
160 [TYPE DISPOSITION TRANSFER-ENCODING DISPLAY HEADER TAGLINE BODY
161 CHILDREN HANDLER]
d1be4ec2 162
186f7f0b
KH
163TYPE and DISPOSITION correspond to MIME headers Content-Type and
164Cotent-Disposition respectively, and has this format:
d1be4ec2
KH
165
166 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
167
168VALUE is a string and ATTRIBUTE is a symbol.
169
170Consider the following header, for example:
171
172Content-Type: multipart/mixed;
173 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
174
175The corresponding TYPE argument must be:
176
177\(\"multipart/mixed\"
178 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))
179
180TRANSFER-ENCODING corresponds to MIME header
181Content-Transfer-Encoding, and is a lowercased string.
182
186f7f0b
KH
183DISPLAY is a vector [CURRENT NEW], where CURRENT indicates how
184the header, tagline, and body of the entity are displayed now,
185and NEW indicates how their displaying should be updated.
186Both elements are vector [HEADER-DISPLAY TAGLINE-DISPLAY BODY-DISPLAY],
187where each element is a symbol for the corresponding item that
188has these values:
189 nil: not displayed
190 t: displayed by the decoded presentation form
191 raw: displayed by the raw MIME data (for the header and body only)
192
193HEADER and BODY are vectors [BEG END DISPLAY-FLAG], where BEG and
194END specify the region of the header or body lines in RMAIL's
195data (mbox) buffer, and DISPLAY-FLAG non-nil means that the
196header or body is, by default, displayed by the decoded
197presentation form.
198
199TAGLINE is a vector [TAG BULK-DATA DISPLAY-FLAG], where TAG is a
200string indicating the depth and index number of the entity,
201BULK-DATA is a cons (SIZE . TYPE) indicating the size and type of
202an attached data, DISPLAY-FLAG non-nil means that the tagline is,
203by default, displayed.
204
205CHILDREN is a list of child MIME-entities. A \"multipart/*\"
206entity have one or more children. A \"message/rfc822\" entity
207has just one child. Any other entity has no child.
208
209HANDLER is a function to insert the entity according to DISPLAY.
210It is called with one argument ENTITY."
211 (vector type disposition transfer-encoding
212 display header tagline body children handler))
d1be4ec2
KH
213
214;; Accessors for a MIME-entity object.
215(defsubst rmail-mime-entity-type (entity) (aref entity 0))
216(defsubst rmail-mime-entity-disposition (entity) (aref entity 1))
217(defsubst rmail-mime-entity-transfer-encoding (entity) (aref entity 2))
186f7f0b
KH
218(defsubst rmail-mime-entity-display (entity) (aref entity 3))
219(defsubst rmail-mime-entity-header (entity) (aref entity 4))
220(defsubst rmail-mime-entity-tagline (entity) (aref entity 5))
221(defsubst rmail-mime-entity-body (entity) (aref entity 6))
222(defsubst rmail-mime-entity-children (entity) (aref entity 7))
223(defsubst rmail-mime-entity-handler (entity) (aref entity 8))
224
225(defsubst rmail-mime-message-p ()
226 "Non-nil if and only if the current message is a MIME."
227 (or (get-text-property (point) 'rmail-mime-entity)
228 (get-text-property (point-min) 'rmail-mime-entity)))
f4ce6150 229
537ab246
BG
230;;; Buttons
231
232(defun rmail-mime-save (button)
233 "Save the attachment using info in the BUTTON."
186f7f0b
KH
234 (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
235 (filename (button-get button 'filename))
537ab246 236 (directory (button-get button 'directory))
fe6793d4
GM
237 (data (button-get button 'data))
238 (ofilename filename))
537ab246
BG
239 (setq filename (expand-file-name
240 (read-file-name (format "Save as (default: %s): " filename)
241 directory
242 (expand-file-name filename directory))
243 directory))
fe6793d4
GM
244 ;; If arg is just a directory, use the default file name, but in
245 ;; that directory (copied from write-file).
246 (if (file-directory-p filename)
247 (setq filename (expand-file-name
248 (file-name-nondirectory ofilename)
249 (file-name-as-directory filename))))
250 (with-temp-buffer
537ab246 251 (set-buffer-file-coding-system 'no-conversion)
134a027f
EZ
252 ;; Needed e.g. by jka-compr, so if the attachment is a compressed
253 ;; file, the magic signature compares equal with the unibyte
254 ;; signature string recorded in jka-compr-compression-info-list.
255 (set-buffer-multibyte nil)
d1be4ec2
KH
256 (setq buffer-undo-list t)
257 (if (stringp data)
258 (insert data)
259 ;; DATA is a MIME-entity object.
260 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding data))
261 (body (rmail-mime-entity-body data)))
186f7f0b
KH
262 (insert-buffer-substring rmail-mime-mbox-buffer
263 (aref body 0) (aref body 1))
d1be4ec2
KH
264 (cond ((string= transfer-encoding "base64")
265 (ignore-errors (base64-decode-region (point-min) (point-max))))
266 ((string= transfer-encoding "quoted-printable")
267 (quoted-printable-decode-region (point-min) (point-max))))))
fe6793d4 268 (write-region nil nil filename nil nil nil t))))
537ab246 269
fe6793d4 270(define-button-type 'rmail-mime-save 'action 'rmail-mime-save)
537ab246 271
186f7f0b
KH
272(defun rmail-mime-entity-segment (pos &optional entity)
273 "Return a vector describing the displayed region of a MIME-entity at POS.
274Optional 2nd argument ENTITY is the MIME-entity at POS.
275The value is a vector [ INDEX HEADER TAGLINE BODY END], where
e7ca0062 276 INDEX: index into the returned vector indicating where POS is (1..3).
186f7f0b
KH
277 HEADER: the position of the beginning of a header
278 TAGLINE: the position of the beginning of a tagline
279 BODY: the position of the beginning of a body
e7ca0062 280 END: the position of the end of the entity."
186f7f0b
KH
281 (save-excursion
282 (or entity
283 (setq entity (get-text-property pos 'rmail-mime-entity)))
284 (if (not entity)
285 (vector 1 (point) (point) (point) (point))
286 (let ((current (aref (rmail-mime-entity-display entity) 0))
287 (beg (if (and (> pos (point-min))
288 (eq (get-text-property (1- pos) 'rmail-mime-entity)
289 entity))
290 (previous-single-property-change pos 'rmail-mime-entity
291 nil (point-min))
292 pos))
293 (index 1)
294 tagline-beg body-beg end)
295 (goto-char beg)
296 (if (aref current 0)
297 (search-forward "\n\n" nil t))
298 (setq tagline-beg (point))
299 (if (>= pos tagline-beg)
300 (setq index 2))
301 (if (aref current 1)
302 (forward-line 1))
303 (setq body-beg (point))
304 (if (>= pos body-beg)
305 (setq index 3))
306 (if (aref current 2)
307 (let ((tag (aref (rmail-mime-entity-tagline entity) 0))
308 tag2)
309 (setq end (next-single-property-change beg 'rmail-mime-entity
310 nil (point-max)))
311 (while (and (< end (point-max))
312 (setq entity (get-text-property end 'rmail-mime-entity)
313 tag2 (aref (rmail-mime-entity-tagline entity) 0))
314 (and (> (length tag2) 0)
315 (eq (string-match tag tag2) 0)))
316 (setq end (next-single-property-change end 'rmail-mime-entity
317 nil (point-max)))))
318 (setq end body-beg))
319 (vector index beg tagline-beg body-beg end)))))
320
186f7f0b
KH
321(defun rmail-mime-shown-mode (entity)
322 "Make MIME-entity ENTITY displayed by the default way."
323 (let ((new (aref (rmail-mime-entity-display entity) 1)))
324 (aset new 0 (aref (rmail-mime-entity-header entity) 2))
325 (aset new 1 (aref (rmail-mime-entity-tagline entity) 2))
e7ca0062
KH
326 (aset new 2 (aref (rmail-mime-entity-body entity) 2)))
327 (dolist (child (rmail-mime-entity-children entity))
328 (rmail-mime-shown-mode child)))
186f7f0b 329
e7ca0062
KH
330(defun rmail-mime-hidden-mode (entity)
331 "Make MIME-entity ENTITY displayed in the hidden mode."
332 (let ((new (aref (rmail-mime-entity-display entity) 1)))
333 (aset new 0 nil)
334 (aset new 1 t)
335 (aset new 2 nil))
186f7f0b 336 (dolist (child (rmail-mime-entity-children entity))
e7ca0062 337 (rmail-mime-hidden-mode child)))
186f7f0b
KH
338
339(defun rmail-mime-raw-mode (entity)
340 "Make MIME-entity ENTITY displayed in the raw mode."
341 (let ((new (aref (rmail-mime-entity-display entity) 1)))
342 (aset new 0 'raw)
343 (aset new 1 nil)
e7ca0062
KH
344 (aset new 2 'raw))
345 (dolist (child (rmail-mime-entity-children entity))
346 (rmail-mime-raw-mode child)))
186f7f0b
KH
347
348(defun rmail-mime-toggle-raw (entity)
349 "Toggle on and off the raw display mode of MIME-entity ENTITY."
350 (let* ((pos (if (eobp) (1- (point-max)) (point)))
351 (entity (get-text-property pos 'rmail-mime-entity))
352 (current (aref (rmail-mime-entity-display entity) 0))
353 (segment (rmail-mime-entity-segment pos entity)))
354 (if (not (eq (aref current 0) 'raw))
355 ;; Enter the raw mode.
356 (rmail-mime-raw-mode entity)
357 ;; Enter the shown mode.
358 (rmail-mime-shown-mode entity))
359 (let ((inhibit-read-only t)
360 (modified (buffer-modified-p)))
361 (save-excursion
362 (goto-char (aref segment 1))
363 (rmail-mime-insert entity)
364 (restore-buffer-modified-p modified)))))
365
366(defun rmail-mime-toggle-hidden ()
e7ca0062 367 "Hide or show the body of MIME-entity at point."
186f7f0b
KH
368 (interactive)
369 (when (rmail-mime-message-p)
370 (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
371 (rmail-mime-view-buffer (current-buffer))
372 (pos (if (eobp) (1- (point-max)) (point)))
373 (entity (get-text-property pos 'rmail-mime-entity))
374 (current (aref (rmail-mime-entity-display entity) 0))
375 (segment (rmail-mime-entity-segment pos entity)))
376 (if (aref current 2)
377 ;; Enter the hidden mode.
378 (progn
379 ;; If point is in the body part, move it to the tagline
e7ca0062 380 ;; (or the header if tagline is not displayed).
186f7f0b
KH
381 (if (= (aref segment 0) 3)
382 (goto-char (aref segment 2)))
e7ca0062 383 (rmail-mime-hidden-mode entity)
186f7f0b
KH
384 ;; If the current entity is the topmost one, display the
385 ;; header.
386 (if (and rmail-mime-mbox-buffer (= (aref segment 1) (point-min)))
387 (let ((new (aref (rmail-mime-entity-display entity) 1)))
388 (aset new 0 t))))
389 ;; Enter the shown mode.
e7ca0062
KH
390 (rmail-mime-shown-mode entity)
391 ;; Force this body shown.
392 (aset (aref (rmail-mime-entity-display entity) 1) 2 t))
186f7f0b
KH
393 (let ((inhibit-read-only t)
394 (modified (buffer-modified-p))
395 (rmail-mime-mbox-buffer rmail-view-buffer)
396 (rmail-mime-view-buffer rmail-buffer))
397 (save-excursion
398 (goto-char (aref segment 1))
399 (rmail-mime-insert entity)
400 (restore-buffer-modified-p modified))))))
401
e7ca0062
KH
402(define-key rmail-mode-map "\t" 'forward-button)
403(define-key rmail-mode-map [backtab] 'backward-button)
186f7f0b
KH
404(define-key rmail-mode-map "\r" 'rmail-mime-toggle-hidden)
405
537ab246
BG
406;;; Handlers
407
186f7f0b
KH
408(defun rmail-mime-insert-tagline (entity &rest item-list)
409 "Insert a tag line for MIME-entity ENTITY.
410ITEM-LIST is a list of strings or button-elements (list) to be added
411to the tag line."
412 (insert "[")
413 (let ((tag (aref (rmail-mime-entity-tagline entity) 0)))
414 (if (> (length tag) 0) (insert (substring tag 1) ":")))
e7ca0062
KH
415 (insert (car (rmail-mime-entity-type entity)) " ")
416 (insert-button (let ((new (aref (rmail-mime-entity-display entity) 1)))
417 (if (aref new 2) "Hide" "Show"))
418 :type 'rmail-mime-toggle
419 'help-echo "mouse-2, RET: Toggle show/hide")
186f7f0b
KH
420 (dolist (item item-list)
421 (when item
422 (if (stringp item)
423 (insert item)
424 (apply 'insert-button item))))
425 (insert "]\n"))
426
e7ca0062
KH
427(defun rmail-mime-update-tagline (entity)
428 "Update the current tag line for MIME-entity ENTITY."
429 (let ((inhibit-read-only t)
430 (modified (buffer-modified-p))
431 ;; If we are going to show the body, the new button label is
432 ;; "Hide". Otherwise, it's "Show".
433 (label (if (aref (aref (rmail-mime-entity-display entity) 1) 2) "Hide"
434 "Show"))
435 (button (next-button (point))))
436 ;; Go to the second character of the button "Show" or "Hide".
437 (goto-char (1+ (button-start button)))
438 (setq button (button-at (point)))
439 (save-excursion
440 (insert label)
441 (delete-region (point) (button-end button)))
442 (delete-region (button-start button) (point))
443 (put-text-property (point) (button-end button) 'rmail-mime-entity entity)
444 (restore-buffer-modified-p modified)
445 (forward-line 1)))
446
186f7f0b
KH
447(defun rmail-mime-insert-header (header)
448 "Decode and insert a MIME-entity header HEADER in the current buffer.
449HEADER is a vector [BEG END DEFAULT-STATUS].
450See `rmail-mime-entity' for the detail."
451 (let ((pos (point))
452 (last-coding-system-used nil))
453 (save-restriction
454 (narrow-to-region pos pos)
455 (with-current-buffer rmail-mime-mbox-buffer
456 (let ((rmail-buffer rmail-mime-mbox-buffer)
457 (rmail-view-buffer rmail-mime-view-buffer))
458 (save-excursion
459 (goto-char (aref header 0))
460 (rmail-copy-headers (point) (aref header 1)))))
461 (rfc2047-decode-region pos (point))
462 (if (and last-coding-system-used (not rmail-mime-coding-system))
463 (setq rmail-mime-coding-system last-coding-system-used))
464 (goto-char (point-min))
465 (rmail-highlight-headers)
466 (goto-char (point-max))
467 (insert "\n"))))
468
537ab246
BG
469(defun rmail-mime-text-handler (content-type
470 content-disposition
471 content-transfer-encoding)
472 "Handle the current buffer as a plain text MIME part."
186f7f0b
KH
473 (rmail-mime-insert-text
474 (rmail-mime-entity content-type content-disposition
475 content-transfer-encoding
476 (vector (vector nil nil nil) (vector nil nil t))
477 (vector nil nil nil) (vector "" (cons nil nil) t)
478 (vector nil nil nil) nil 'rmail-mime-insert-text))
479 t)
480
481(defun rmail-mime-insert-decoded-text (entity)
482 "Decode and insert the text body of MIME-entity ENTITY."
d1be4ec2
KH
483 (let* ((content-type (rmail-mime-entity-type entity))
484 (charset (cdr (assq 'charset (cdr content-type))))
186f7f0b
KH
485 (coding-system (if charset
486 (coding-system-from-name charset)))
487 (body (rmail-mime-entity-body entity))
488 (pos (point)))
489 (or (and coding-system (coding-system-p coding-system))
490 (setq coding-system 'undecided))
491 (if (stringp (aref body 0))
492 (insert (aref body 0))
493 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
494 (insert-buffer-substring rmail-mime-mbox-buffer
495 (aref body 0) (aref body 1))
496 (cond ((string= transfer-encoding "base64")
497 (ignore-errors (base64-decode-region pos (point))))
498 ((string= transfer-encoding "quoted-printable")
499 (quoted-printable-decode-region pos (point))))))
500 (decode-coding-region pos (point) coding-system)
501 (or rmail-mime-coding-system
502 (setq rmail-mime-coding-system coding-system))
503 (or (bolp) (insert "\n"))))
504
505(defun rmail-mime-insert-text (entity)
506 "Presentation handler for a plain text MIME entity."
507 (let ((current (aref (rmail-mime-entity-display entity) 0))
508 (new (aref (rmail-mime-entity-display entity) 1))
509 (header (rmail-mime-entity-header entity))
510 (tagline (rmail-mime-entity-tagline entity))
511 (body (rmail-mime-entity-body entity))
512 (beg (point))
513 (segment (rmail-mime-entity-segment (point) entity)))
514
515 (or (integerp (aref body 0))
516 (let ((data (buffer-string)))
517 (aset body 0 data)
518 (delete-region (point-min) (point-max))))
519
520 ;; header
521 (if (eq (aref current 0) (aref new 0))
522 (goto-char (aref segment 2))
523 (if (aref current 0)
524 (delete-char (- (aref segment 2) (aref segment 1))))
525 (if (aref new 0)
526 (rmail-mime-insert-header header)))
527 ;; tagline
528 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
529 (if (or (not (aref current 1))
530 (eq (aref current 2) (aref new 2)))
531 (forward-char (- (aref segment 3) (aref segment 2)))
532 (rmail-mime-update-tagline entity))
186f7f0b
KH
533 (if (aref current 1)
534 (delete-char (- (aref segment 3) (aref segment 2))))
535 (if (aref new 1)
536 (rmail-mime-insert-tagline entity)))
537 ;; body
538 (if (eq (aref current 2) (aref new 2))
539 (forward-char (- (aref segment 4) (aref segment 3)))
540 (if (aref current 2)
541 (delete-char (- (aref segment 4) (aref segment 3))))
542 (if (aref new 2)
543 (rmail-mime-insert-decoded-text entity)))
544 (put-text-property beg (point) 'rmail-mime-entity entity)))
d1be4ec2 545
f4ce6150 546;; FIXME move to the test/ directory?
537ab246
BG
547(defun test-rmail-mime-handler ()
548 "Test of a mail using no MIME parts at all."
549 (let ((mail "To: alex@gnu.org
550Content-Type: text/plain; charset=koi8-r
551Content-Transfer-Encoding: 8bit
552MIME-Version: 1.0
553
554\372\304\322\301\327\323\324\327\325\312\324\305\41"))
555 (switch-to-buffer (get-buffer-create "*test*"))
556 (erase-buffer)
557 (set-buffer-multibyte nil)
558 (insert mail)
559 (rmail-mime-show t)
560 (set-buffer-multibyte t)))
561
e8652dd9 562
186f7f0b
KH
563(defun rmail-mime-insert-image (entity)
564 "Decode and insert the image body of MIME-entity ENTITY."
565 (let* ((content-type (car (rmail-mime-entity-type entity)))
566 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
567 (body (rmail-mime-entity-body entity))
568 data)
569 (if (stringp (aref body 0))
570 (setq data (aref body 0))
571 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
572 (transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
d1be4ec2
KH
573 (with-temp-buffer
574 (set-buffer-multibyte nil)
575 (setq buffer-undo-list t)
186f7f0b
KH
576 (insert-buffer-substring rmail-mime-mbox-buffer
577 (aref body 0) (aref body 1))
d1be4ec2
KH
578 (cond ((string= transfer-encoding "base64")
579 (ignore-errors (base64-decode-region (point-min) (point-max))))
580 ((string= transfer-encoding "quoted-printable")
581 (quoted-printable-decode-region (point-min) (point-max))))
582 (setq data
583 (buffer-substring-no-properties (point-min) (point-max))))))
186f7f0b
KH
584 (insert-image (create-image data (cdr bulk-data) t))
585 (insert "\n")))
e8652dd9 586
e7ca0062
KH
587(defun rmail-mime-toggle-button (button)
588 "Hide or show the body of the MIME-entity associated with BUTTON."
186f7f0b 589 (save-excursion
e7ca0062 590 (goto-char (button-start button))
186f7f0b 591 (rmail-mime-toggle-hidden)))
e8652dd9 592
e7ca0062 593(define-button-type 'rmail-mime-toggle 'action 'rmail-mime-toggle-button)
e8652dd9
GM
594
595
537ab246
BG
596(defun rmail-mime-bulk-handler (content-type
597 content-disposition
e8652dd9 598 content-transfer-encoding)
2e9075d3 599 "Handle the current buffer as an attachment to download.
e8652dd9
GM
600For images that Emacs is capable of displaying, the behavior
601depends upon the value of `rmail-mime-show-images'."
d1be4ec2
KH
602 (rmail-mime-insert-bulk
603 (rmail-mime-entity content-type content-disposition content-transfer-encoding
186f7f0b
KH
604 (vector (vector nil nil nil) (vector nil t nil))
605 (vector nil nil nil) (vector "" (cons nil nil) t)
606 (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
607
608(defun rmail-mime-set-bulk-data (entity)
609 "Setup the information about the attachment object for MIME-entity ENTITY.
610The value is non-nil if and only if the attachment object should be shown
611directly."
612 (let ((content-type (car (rmail-mime-entity-type entity)))
613 (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
614 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
615 (body (rmail-mime-entity-body entity))
e7ca0062 616 type to-show)
186f7f0b
KH
617 (cond (size
618 (setq size (string-to-number size)))
619 ((stringp (aref body 0))
620 (setq size (length (aref body 0))))
621 (t
622 ;; Rough estimation of the size.
623 (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
624 (setq size (- (aref body 1) (aref body 0)))
625 (cond ((string= encoding "base64")
626 (setq size (/ (* size 3) 4)))
627 ((string= encoding "quoted-printable")
628 (setq size (/ (* size 7) 3)))))))
629
630 (cond
631 ((string-match "text/" content-type)
632 (setq type 'text))
633 ((string-match "image/\\(.*\\)" content-type)
634 (setq type (image-type-from-file-name
635 (concat "." (match-string 1 content-type))))
636 (if (and (memq type image-types)
637 (image-type-available-p type))
638 (if (and rmail-mime-show-images
639 (not (eq rmail-mime-show-images 'button))
640 (or (not (numberp rmail-mime-show-images))
641 (< size rmail-mime-show-images)))
642 (setq to-show t))
643 (setq type nil))))
644 (setcar bulk-data size)
645 (setcdr bulk-data type)
646 to-show))
d1be4ec2
KH
647
648(defun rmail-mime-insert-bulk (entity)
186f7f0b 649 "Presentation handler for an attachment MIME entity."
d1be4ec2
KH
650 (let* ((content-type (rmail-mime-entity-type entity))
651 (content-disposition (rmail-mime-entity-disposition entity))
186f7f0b
KH
652 (current (aref (rmail-mime-entity-display entity) 0))
653 (new (aref (rmail-mime-entity-display entity) 1))
654 (header (rmail-mime-entity-header entity))
655 (tagline (rmail-mime-entity-tagline entity))
656 (bulk-data (aref tagline 1))
d1be4ec2 657 (body (rmail-mime-entity-body entity))
e7ca0062 658 ;; Find the default directory for this media type.
d1be4ec2 659 (directory (catch 'directory
537ab246
BG
660 (dolist (entry rmail-mime-attachment-dirs-alist)
661 (when (string-match (car entry) (car content-type))
662 (dolist (dir (cdr entry))
663 (when (file-directory-p dir)
664 (throw 'directory dir)))))))
665 (filename (or (cdr (assq 'name (cdr content-type)))
666 (cdr (assq 'filename (cdr content-disposition)))
667 "noname"))
69220882 668 (units '(B kB MB GB))
186f7f0b
KH
669 (segment (rmail-mime-entity-segment (point) entity))
670 beg data size)
671
672 (if (integerp (aref body 0))
d1be4ec2 673 (setq data entity
186f7f0b
KH
674 size (car bulk-data))
675 (if (stringp (aref body 0))
676 (setq data (aref body 0))
677 (setq data (string-as-unibyte (buffer-string)))
678 (aset body 0 data)
679 (rmail-mime-set-bulk-data entity)
680 (delete-region (point-min) (point-max)))
681 (setq size (length data)))
d1be4ec2 682 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
69220882
GM
683 (cdr units))
684 (setq size (/ size 1024.0)
685 units (cdr units)))
186f7f0b
KH
686
687 (setq beg (point))
688
689 ;; header
690 (if (eq (aref current 0) (aref new 0))
691 (goto-char (aref segment 2))
692 (if (aref current 0)
693 (delete-char (- (aref segment 2) (aref segment 1))))
694 (if (aref new 0)
695 (rmail-mime-insert-header header)))
696
697 ;; tagline
698 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
699 (if (or (not (aref current 1))
700 (eq (aref current 2) (aref new 2)))
701 (forward-char (- (aref segment 3) (aref segment 2)))
702 (rmail-mime-update-tagline entity))
186f7f0b
KH
703 (if (aref current 1)
704 (delete-char (- (aref segment 3) (aref segment 2))))
705 (if (aref new 1)
706 (rmail-mime-insert-tagline
707 entity
e7ca0062 708 " Save:"
186f7f0b
KH
709 (list filename
710 :type 'rmail-mime-save
711 'help-echo "mouse-2, RET: Save attachment"
712 'filename filename
713 'directory (file-name-as-directory directory)
714 'data data)
715 (format " (%.0f%s)" size (car units))
e7ca0062
KH
716 ;; We don't need this button because the "type" string of a
717 ;; tagline is the button to do this.
718 ;; (if (cdr bulk-data)
719 ;; " ")
720 ;; (if (cdr bulk-data)
721 ;; (list "Toggle show/hide"
722 ;; :type 'rmail-mime-image
723 ;; 'help-echo "mouse-2, RET: Toggle show/hide"
724 ;; 'image-type (cdr bulk-data)
725 ;; 'image-data data))
726 )))
186f7f0b
KH
727 ;; body
728 (if (eq (aref current 2) (aref new 2))
729 (forward-char (- (aref segment 4) (aref segment 3)))
730 (if (aref current 2)
731 (delete-char (- (aref segment 4) (aref segment 3))))
732 (if (aref new 2)
733 (cond ((eq (cdr bulk-data) 'text)
734 (rmail-mime-insert-decoded-text entity))
735 ((cdr bulk-data)
8434f239
KH
736 (rmail-mime-insert-image entity))
737 (t
738 ;; As we don't know how to display the body, just
739 ;; insert it as a text.
740 (rmail-mime-insert-decoded-text entity)))))
186f7f0b 741 (put-text-property beg (point) 'rmail-mime-entity entity)))
537ab246
BG
742
743(defun test-rmail-mime-bulk-handler ()
744 "Test of a mail used as an example in RFC 2183."
745 (let ((mail "Content-Type: image/jpeg
746Content-Disposition: attachment; filename=genome.jpeg;
747 modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";
748Content-Description: a complete map of the human genome
749Content-Transfer-Encoding: base64
750
751iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAZQ
752TFRF////AAAAVcLTfgAAAPZJREFUeNq9ldsOwzAIQ+3//+l1WlvA5ZLsoUiTto4TB+ISoAjy
753+ITfRBfcAmgRFFeAm+J6uhdKdFhFWUgDkFsK0oUp/9G2//Kj7Jx+5tSKOdBscgUYiKHRS/me
754WATQdRUvAK0Bnmshmtn79PpaLBbbOZkjKvRnjRZoRswOkG1wFchKew2g9wXVJVZL/m4+B+vv
7559AxQQR2Q33SgAYJzzVACdAWjAfRYzYFO9n6SLnydtQHSMxYDMAKqZ/8FS/lTK+zuq3CtK64L
756UDwbgUEAUmk2Zyg101d6PhCDySgAvTvDgKiuOrc4dLxUb7UMnhGIexyI+d6U+ABuNAP4Simx
757lgAAAABJRU5ErkJggg==
758"))
759 (switch-to-buffer (get-buffer-create "*test*"))
760 (erase-buffer)
761 (insert mail)
762 (rmail-mime-show)))
763
764(defun rmail-mime-multipart-handler (content-type
765 content-disposition
766 content-transfer-encoding)
767 "Handle the current buffer as a multipart MIME body.
768The current buffer should be narrowed to the body. CONTENT-TYPE,
769CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
770of the respective parsed headers. See `rmail-mime-handle' for their
771format."
d1be4ec2 772 (rmail-mime-process-multipart
186f7f0b
KH
773 content-type content-disposition content-transfer-encoding nil)
774 t)
d1be4ec2
KH
775
776(defun rmail-mime-process-multipart (content-type
777 content-disposition
778 content-transfer-encoding
186f7f0b 779 parse-tag)
d1be4ec2
KH
780 "Process the current buffer as a multipart MIME body.
781
186f7f0b
KH
782If PARSE-TAG is nil, modify the current buffer directly for
783showing the MIME body and return nil.
d1be4ec2 784
186f7f0b
KH
785Otherwise, PARSE-TAG is a string indicating the depth and index
786number of the entity. In this case, parse the current buffer and
787return a list of MIME-entity objects.
d1be4ec2
KH
788
789The other arguments are the same as `rmail-mime-multipart-handler'."
537ab246
BG
790 ;; Some MUAs start boundaries with "--", while it should start
791 ;; with "CRLF--", as defined by RFC 2046:
792 ;; The boundary delimiter MUST occur at the beginning of a line,
793 ;; i.e., following a CRLF, and the initial CRLF is considered to
794 ;; be attached to the boundary delimiter line rather than part
795 ;; of the preceding part.
796 ;; We currently don't handle that.
797 (let ((boundary (cdr (assq 'boundary content-type)))
186f7f0b
KH
798 (subtype (cadr (split-string (car content-type) "/")))
799 (index 0)
d1be4ec2 800 beg end next entities)
537ab246
BG
801 (unless boundary
802 (rmail-mm-get-boundary-error-message
803 "No boundary defined" content-type content-disposition
804 content-transfer-encoding))
805 (setq boundary (concat "\n--" boundary))
806 ;; Hide the body before the first bodypart
807 (goto-char (point-min))
808 (when (and (search-forward boundary nil t)
809 (looking-at "[ \t]*\n"))
186f7f0b 810 (if parse-tag
d1be4ec2
KH
811 (narrow-to-region (match-end 0) (point-max))
812 (delete-region (point-min) (match-end 0))))
186f7f0b
KH
813
814 ;; Change content-type to the proper default one for the children.
815 (cond ((string-match "mixed" subtype)
816 (setq content-type '("text/plain")))
817 ((string-match "digest" subtype)
8434f239
KH
818 (setq content-type '("message/rfc822")))
819 (t
820 (setq content-type nil)))
186f7f0b 821
537ab246
BG
822 ;; Loop over all body parts, where beg points at the beginning of
823 ;; the part and end points at the end of the part. next points at
186f7f0b
KH
824 ;; the beginning of the next part. The current point is just
825 ;; after the boundary tag.
537ab246
BG
826 (setq beg (point-min))
827 (while (search-forward boundary nil t)
828 (setq end (match-beginning 0))
829 ;; If this is the last boundary according to RFC 2046, hide the
830 ;; epilogue, else hide the boundary only. Use a marker for
831 ;; `next' because `rmail-mime-show' may change the buffer.
ffa1fed6 832 (cond ((looking-at "--[ \t]*$")
537ab246
BG
833 (setq next (point-max-marker)))
834 ((looking-at "[ \t]*\n")
ffa1fed6 835 (setq next (copy-marker (match-end 0) t)))
537ab246
BG
836 (t
837 (rmail-mm-get-boundary-error-message
838 "Malformed boundary" content-type content-disposition
839 content-transfer-encoding)))
186f7f0b
KH
840
841 (setq index (1+ index))
537ab246 842 ;; Handle the part.
186f7f0b 843 (if parse-tag
d1be4ec2
KH
844 (save-restriction
845 (narrow-to-region beg end)
186f7f0b
KH
846 (let ((child (rmail-mime-process
847 nil (format "%s/%d" parse-tag index)
848 content-type content-disposition)))
849 ;; Display a tagline.
850 (aset (aref (rmail-mime-entity-display child) 1) 1
851 (aset (rmail-mime-entity-tagline child) 2 t))
852 (push child entities)))
853
d1be4ec2
KH
854 (delete-region end next)
855 (save-restriction
856 (narrow-to-region beg end)
857 (rmail-mime-show)))
858 (goto-char (setq beg next)))
186f7f0b
KH
859
860 (when parse-tag
861 (setq entities (nreverse entities))
862 (if (string-match "alternative" subtype)
863 ;; Find the best entity to show, and hide all the others.
864 (let (best second)
865 (dolist (child entities)
866 (if (string= (or (car (rmail-mime-entity-disposition child))
867 (car content-disposition))
868 "inline")
869 (if (string-match "text/plain"
870 (car (rmail-mime-entity-type child)))
871 (setq best child)
872 (if (string-match "text/.*"
873 (car (rmail-mime-entity-type child)))
874 (setq second child)))))
875 (or best (not second) (setq best second))
876 (dolist (child entities)
e7ca0062
KH
877 (unless (eq best child)
878 (aset (rmail-mime-entity-body child) 2 nil)
879 (rmail-mime-hidden-mode child)))))
186f7f0b 880 entities)))
537ab246
BG
881
882(defun test-rmail-mime-multipart-handler ()
883 "Test of a mail used as an example in RFC 2046."
884 (let ((mail "From: Nathaniel Borenstein <nsb@bellcore.com>
885To: Ned Freed <ned@innosoft.com>
886Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
887Subject: Sample message
888MIME-Version: 1.0
889Content-type: multipart/mixed; boundary=\"simple boundary\"
890
891This is the preamble. It is to be ignored, though it
892is a handy place for composition agents to include an
893explanatory note to non-MIME conformant readers.
894
895--simple boundary
896
897This is implicitly typed plain US-ASCII text.
898It does NOT end with a linebreak.
899--simple boundary
900Content-type: text/plain; charset=us-ascii
901
902This is explicitly typed plain US-ASCII text.
903It DOES end with a linebreak.
904
905--simple boundary--
906
907This is the epilogue. It is also to be ignored."))
908 (switch-to-buffer (get-buffer-create "*test*"))
909 (erase-buffer)
910 (insert mail)
911 (rmail-mime-show t)))
912
186f7f0b
KH
913(defun rmail-mime-insert-multipart (entity)
914 "Presentation handler for a multipart MIME entity."
915 (let ((current (aref (rmail-mime-entity-display entity) 0))
916 (new (aref (rmail-mime-entity-display entity) 1))
917 (header (rmail-mime-entity-header entity))
918 (tagline (rmail-mime-entity-tagline entity))
919 (body (rmail-mime-entity-body entity))
920 (beg (point))
921 (segment (rmail-mime-entity-segment (point) entity)))
922 ;; header
923 (if (eq (aref current 0) (aref new 0))
924 (goto-char (aref segment 2))
925 (if (aref current 0)
926 (delete-char (- (aref segment 2) (aref segment 1))))
927 (if (aref new 0)
928 (rmail-mime-insert-header header)))
929 ;; tagline
930 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
931 (if (or (not (aref current 1))
932 (eq (aref current 2) (aref new 2)))
933 (forward-char (- (aref segment 3) (aref segment 2)))
934 (rmail-mime-update-tagline entity))
186f7f0b
KH
935 (if (aref current 1)
936 (delete-char (- (aref segment 3) (aref segment 2))))
937 (if (aref new 1)
938 (rmail-mime-insert-tagline entity)))
939
940 (put-text-property beg (point) 'rmail-mime-entity entity)
e7ca0062 941
186f7f0b
KH
942 ;; body
943 (if (eq (aref current 2) (aref new 2))
944 (forward-char (- (aref segment 4) (aref segment 3)))
e7ca0062
KH
945 (dolist (child (rmail-mime-entity-children entity))
946 (rmail-mime-insert child)))
947 entity))
186f7f0b 948
537ab246
BG
949;;; Main code
950
951(defun rmail-mime-handle (content-type
952 content-disposition
953 content-transfer-encoding)
954 "Handle the current buffer as a MIME part.
955The current buffer should be narrowed to the respective body, and
956point should be at the beginning of the body.
957
958CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
0c9ff2c5
GM
959are the values of the respective parsed headers. The latter should
960be downcased. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
961have the form
537ab246
BG
962
963 \(VALUE . ALIST)
964
965In other words:
966
967 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
968
969VALUE is a string and ATTRIBUTE is a symbol.
970
971Consider the following header, for example:
972
973Content-Type: multipart/mixed;
974 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
975
976The parsed header value:
977
978\(\"multipart/mixed\"
979 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
980 ;; Handle the content transfer encodings we know. Unknown transfer
981 ;; encodings will be passed on to the various handlers.
982 (cond ((string= content-transfer-encoding "base64")
983 (when (ignore-errors
984 (base64-decode-region (point) (point-max)))
985 (setq content-transfer-encoding nil)))
986 ((string= content-transfer-encoding "quoted-printable")
987 (quoted-printable-decode-region (point) (point-max))
988 (setq content-transfer-encoding nil))
989 ((string= content-transfer-encoding "8bit")
990 ;; FIXME: Is this the correct way?
c893016b
SM
991 ;; No, of course not, it just means there's no decoding to do.
992 ;; (set-buffer-multibyte nil)
993 (setq content-transfer-encoding nil)
994 ))
537ab246
BG
995 ;; Inline stuff requires work. Attachments are handled by the bulk
996 ;; handler.
997 (if (string= "inline" (car content-disposition))
998 (let ((stop nil))
999 (dolist (entry rmail-mime-media-type-handlers-alist)
1000 (when (and (string-match (car entry) (car content-type)) (not stop))
1001 (progn
1002 (setq stop (funcall (cadr entry) content-type
1003 content-disposition
1004 content-transfer-encoding))))))
1005 ;; Everything else is an attachment.
1006 (rmail-mime-bulk-handler content-type
1007 content-disposition
e7ca0062
KH
1008 content-transfer-encoding))
1009 (save-restriction
1010 (widen)
1011 (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
1012 current new)
1013 (when entity
1014 (setq current (aref (rmail-mime-entity-display entity) 0)
1015 new (aref (rmail-mime-entity-display entity) 1))
1016 (dotimes (i 3)
1017 (aset current i (aref new i)))))))
537ab246
BG
1018
1019(defun rmail-mime-show (&optional show-headers)
1020 "Handle the current buffer as a MIME message.
1021If SHOW-HEADERS is non-nil, then the headers of the current part
1022will shown as usual for a MIME message. The headers are also
1023shown for the content type message/rfc822. This function will be
1024called recursively if multiple parts are available.
1025
1026The current buffer must contain a single message. It will be
1027modified."
d1be4ec2
KH
1028 (rmail-mime-process show-headers nil))
1029
186f7f0b
KH
1030(defun rmail-mime-process (show-headers parse-tag &optional
1031 default-content-type
1032 default-content-disposition)
537ab246
BG
1033 (let ((end (point-min))
1034 content-type
1035 content-transfer-encoding
1036 content-disposition)
1037 ;; `point-min' returns the beginning and `end' points at the end
1038 ;; of the headers.
1039 (goto-char (point-min))
1040 ;; If we're showing a part without headers, then it will start
1041 ;; with a newline.
1042 (if (eq (char-after) ?\n)
1043 (setq end (1+ (point)))
1044 (when (search-forward "\n\n" nil t)
1045 (setq end (match-end 0))
1046 (save-restriction
1047 (narrow-to-region (point-min) end)
1048 ;; FIXME: Default disposition of the multipart entities should
1049 ;; be inherited.
1050 (setq content-type
1051 (mail-fetch-field "Content-Type")
1052 content-transfer-encoding
1053 (mail-fetch-field "Content-Transfer-Encoding")
1054 content-disposition
1055 (mail-fetch-field "Content-Disposition")))))
0c9ff2c5
GM
1056 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1057 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1058 (if content-transfer-encoding
1059 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1060 (setq content-type
1061 (if content-type
e7ca0062
KH
1062 (or (mail-header-parse-content-type content-type)
1063 '("text/plain"))
186f7f0b 1064 (or default-content-type '("text/plain"))))
537ab246
BG
1065 (setq content-disposition
1066 (if content-disposition
1067 (mail-header-parse-content-disposition content-disposition)
1068 ;; If none specified, we are free to choose what we deem
1069 ;; suitable according to RFC 2183. We like inline.
186f7f0b 1070 (or default-content-disposition '("inline"))))
537ab246
BG
1071 ;; Unrecognized disposition types are to be treated like
1072 ;; attachment according to RFC 2183.
1073 (unless (member (car content-disposition) '("inline" "attachment"))
1074 (setq content-disposition '("attachment")))
d1be4ec2 1075
186f7f0b
KH
1076 (if parse-tag
1077 (let* ((is-inline (string= (car content-disposition) "inline"))
1078 (header (vector (point-min) end nil))
1079 (tagline (vector parse-tag (cons nil nil) t))
1080 (body (vector end (point-max) is-inline))
1081 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1082 children handler entity)
1083 (cond ((string-match "multipart/.*" (car content-type))
1084 (save-restriction
1085 (narrow-to-region (1- end) (point-max))
1086 (setq children (rmail-mime-process-multipart
1087 content-type
1088 content-disposition
1089 content-transfer-encoding
1090 parse-tag)
1091 handler 'rmail-mime-insert-multipart)))
1092 ((string-match "message/rfc822" (car content-type))
1093 (save-restriction
d1be4ec2 1094 (narrow-to-region end (point-max))
186f7f0b
KH
1095 (let* ((msg (rmail-mime-process t parse-tag
1096 '("text/plain") '("inline")))
1097 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1098 ;; Show header of the child.
1099 (aset msg-new 0 t)
1100 (aset (rmail-mime-entity-header msg) 2 t)
1101 ;; Hide tagline of the child.
1102 (aset msg-new 1 nil)
1103 (aset (rmail-mime-entity-tagline msg) 2 nil)
1104 (setq children (list msg)
1105 handler 'rmail-mime-insert-multipart))))
1106 ((and is-inline (string-match "text/" (car content-type)))
1107 ;; Don't need a tagline.
1108 (aset new 1 (aset tagline 2 nil))
1109 (setq handler 'rmail-mime-insert-text))
1110 (t
1111 ;; Force hidden mode.
1112 (aset new 1 (aset tagline 2 t))
1113 (aset new 2 (aset body 2 nil))
1114 (setq handler 'rmail-mime-insert-bulk)))
1115 (setq entity (rmail-mime-entity content-type
1116 content-disposition
1117 content-transfer-encoding
1118 (vector (vector nil nil nil) new)
1119 header tagline body children handler))
1120 (if (and (eq handler 'rmail-mime-insert-bulk)
1121 (rmail-mime-set-bulk-data entity))
1122 ;; Show the body.
1123 (aset new 2 (aset body 2 t)))
1124 entity)
1125
d1be4ec2 1126 ;; Hide headers and handle the part.
186f7f0b
KH
1127 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1128 (rmail-mime-entity
1129 content-type content-disposition
1130 content-transfer-encoding
1131 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1132 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1133 (vector nil nil 'raw) nil nil))
d1be4ec2
KH
1134 (save-restriction
1135 (cond ((string= (car content-type) "message/rfc822")
1136 (narrow-to-region end (point-max)))
1137 ((not show-headers)
1138 (delete-region (point-min) end)))
1139 (rmail-mime-handle content-type content-disposition
1140 content-transfer-encoding)))))
1141
d1be4ec2
KH
1142(defun rmail-mime-parse ()
1143 "Parse the current Rmail message as a MIME message.
8258ae3f
KH
1144The value is a MIME-entiy object (see `rmail-mime-entity').
1145If an error occurs, return an error message string."
186f7f0b
KH
1146 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1147 rmail-view-buffer
1148 (current-buffer))))
8258ae3f 1149 (condition-case err
186f7f0b
KH
1150 (with-current-buffer rmail-mime-mbox-buffer
1151 (save-excursion
1152 (goto-char (point-min))
1153 (let* ((entity (rmail-mime-process t ""
1154 '("text/plain") '("inline")))
1155 (new (aref (rmail-mime-entity-display entity) 1)))
1156 ;; Show header.
1157 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1158 ;; Show tagline if and only if body is not shown.
1159 (if (aref new 2)
1160 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 nil))
1161 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 t)))
1162 entity)))
8258ae3f 1163 (error (format "%s" err)))))
186f7f0b
KH
1164
1165(defun rmail-mime-insert (entity)
d1be4ec2
KH
1166 "Insert a MIME-entity ENTITY in the current buffer.
1167
1168This function will be called recursively if multiple parts are
1169available."
186f7f0b
KH
1170 (let ((current (aref (rmail-mime-entity-display entity) 0))
1171 (new (aref (rmail-mime-entity-display entity) 1)))
1172 (if (not (eq (aref new 0) 'raw))
1173 ;; Not a raw-mode. Each handler should handle it.
1174 (funcall (rmail-mime-entity-handler entity) entity)
1175 (let ((header (rmail-mime-entity-header entity))
1176 (tagline (rmail-mime-entity-tagline entity))
1177 (body (rmail-mime-entity-body entity))
1178 (beg (point))
1179 (segment (rmail-mime-entity-segment (point) entity)))
1180 ;; header
1181 (if (eq (aref current 0) (aref new 0))
1182 (goto-char (aref segment 2))
1183 (if (aref current 0)
1184 (delete-char (- (aref segment 2) (aref segment 1))))
1185 (insert-buffer-substring rmail-mime-mbox-buffer
1186 (aref header 0) (aref header 1)))
1187 ;; tagline
1188 (if (aref current 1)
1189 (delete-char (- (aref segment 3) (aref segment 2))))
1190 ;; body
e7ca0062
KH
1191 (let ((children (rmail-mime-entity-children entity)))
1192 (if children
1193 (progn
1194 (put-text-property beg (point) 'rmail-mime-entity entity)
1195 (dolist (child children)
1196 (rmail-mime-insert child)))
1197 (if (eq (aref current 2) (aref new 2))
1198 (forward-char (- (aref segment 4) (aref segment 3)))
1199 (if (aref current 2)
1200 (delete-char (- (aref segment 4) (aref segment 3))))
1201 (insert-buffer-substring rmail-mime-mbox-buffer
1202 (aref body 0) (aref body 1))
1203 (or (bolp) (insert "\n")))
1204 (put-text-property beg (point) 'rmail-mime-entity entity)))))
186f7f0b
KH
1205 (dotimes (i 3)
1206 (aset current i (aref new i)))))
537ab246 1207
2e9075d3
GM
1208(define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1209 "Major mode used in `rmail-mime' buffers."
1210 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1211
73422054 1212;;;###autoload
186f7f0b
KH
1213(defun rmail-mime (&optional arg)
1214 "Toggle displaying of a MIME message.
1215
1216The actualy behavior depends on the value of `rmail-enable-mime'.
1217
1218If `rmail-enable-mime' is t (default), this command change the
1219displaying of a MIME message between decoded presentation form
1220and raw data.
1221
1222With ARG, toggle the displaying of the current MIME entity only.
1223
1224If `rmail-enable-mime' is nil, this creates a temporary
1225\"*RMAIL*\" buffer holding a decoded copy of the message. Inline
1226content-types are handled according to
f4ce6150
GM
1227`rmail-mime-media-type-handlers-alist'. By default, this
1228displays text and multipart messages, and offers to download
1229attachments as specfied by `rmail-mime-attachment-dirs-alist'."
186f7f0b
KH
1230 (interactive "P")
1231 (if rmail-enable-mime
e7ca0062
KH
1232 (with-current-buffer rmail-buffer
1233 (if (rmail-mime-message-p)
1234 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
1235 (rmail-mime-view-buffer rmail-buffer)
1236 (entity (get-text-property (point) 'rmail-mime-entity)))
1237 (if arg
1238 (if entity
1239 (rmail-mime-toggle-raw entity))
1240 (goto-char (point-min))
1241 (rmail-mime-toggle-raw
1242 (get-text-property (point) 'rmail-mime-entity))))
1243 (message "Not a MIME message")))
186f7f0b
KH
1244 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1245 (buf (get-buffer-create "*RMAIL*"))
1246 (rmail-mime-mbox-buffer rmail-view-buffer)
1247 (rmail-mime-view-buffer buf))
1248 (set-buffer buf)
1249 (setq buffer-undo-list t)
1250 (let ((inhibit-read-only t))
1251 ;; Decoding the message in fundamental mode for speed, only
1252 ;; switching to rmail-mime-mode at the end for display. Eg
1253 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1254 (fundamental-mode)
1255 (erase-buffer)
1256 (insert data)
1257 (rmail-mime-show t)
1258 (rmail-mime-mode)
1259 (set-buffer-modified-p nil))
1260 (view-buffer buf))))
537ab246
BG
1261
1262(defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1263 "Return MESSAGE with more information on the main mime components."
1264 (error "%s; type: %s; disposition: %s; encoding: %s"
1265 message type disposition encoding))
1266
d1be4ec2 1267(defun rmail-show-mime ()
7e116860 1268 "Function to set in `rmail-show-mime-function' (which see)."
186f7f0b
KH
1269 (let ((entity (rmail-mime-parse))
1270 (rmail-mime-mbox-buffer rmail-buffer)
1271 (rmail-mime-view-buffer rmail-view-buffer)
1272 (rmail-mime-coding-system nil))
8258ae3f 1273 (if (vectorp entity)
186f7f0b
KH
1274 (with-current-buffer rmail-mime-view-buffer
1275 (erase-buffer)
1276 (rmail-mime-insert entity)
1277 (if rmail-mime-coding-system
1278 (set-buffer-file-coding-system rmail-mime-coding-system t t)))
8258ae3f
KH
1279 ;; Decoding failed. ENTITY is an error message. Insert the
1280 ;; original message body as is, and show warning.
186f7f0b 1281 (let ((region (with-current-buffer rmail-mime-mbox-buffer
7e116860
KH
1282 (goto-char (point-min))
1283 (re-search-forward "^$" nil t)
1284 (forward-line 1)
8258ae3f 1285 (vector (point-min) (point) (point-max)))))
186f7f0b 1286 (with-current-buffer rmail-mime-view-buffer
7e116860
KH
1287 (let ((inhibit-read-only t))
1288 (erase-buffer)
8258ae3f 1289 (rmail-mime-insert-header region)
186f7f0b 1290 (insert-buffer-substring rmail-mime-mbox-buffer
8258ae3f 1291 (aref region 1) (aref region 2))))
186f7f0b 1292 (set-buffer-file-coding-system 'no-conversion t t)
8258ae3f 1293 (message "MIME decoding failed: %s" entity)))))
d1be4ec2
KH
1294
1295(setq rmail-show-mime-function 'rmail-show-mime)
1296
1297(defun rmail-insert-mime-forwarded-message (forward-buffer)
7e116860 1298 "Function to set in `rmail-insert-mime-forwarded-message-function' (which see)."
186f7f0b
KH
1299 (let ((rmail-mime-mbox-buffer
1300 (with-current-buffer forward-buffer rmail-view-buffer)))
d1be4ec2
KH
1301 (save-restriction
1302 (narrow-to-region (point) (point))
186f7f0b 1303 (message-forward-make-body-mime rmail-mime-mbox-buffer))))
d1be4ec2
KH
1304
1305(setq rmail-insert-mime-forwarded-message-function
1306 'rmail-insert-mime-forwarded-message)
1307
1308(defun rmail-insert-mime-resent-message (forward-buffer)
7e116860 1309 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
d1be4ec2
KH
1310 (insert-buffer-substring
1311 (with-current-buffer forward-buffer rmail-view-buffer))
1312 (goto-char (point-min))
1313 (when (looking-at "From ")
1314 (forward-line 1)
1315 (delete-region (point-min) (point))))
1316
1317(setq rmail-insert-mime-resent-message-function
1318 'rmail-insert-mime-resent-message)
1319
7e116860
KH
1320(defun rmail-search-mime-message (msg regexp)
1321 "Function to set in `rmail-search-mime-message-function' (which see)."
1322 (save-restriction
1323 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
186f7f0b
KH
1324 (let* ((rmail-mime-mbox-buffer (current-buffer))
1325 (rmail-mime-view-buffer rmail-view-buffer)
1326 (header-end (save-excursion
1327 (re-search-forward "^$" nil 'move) (point)))
1328 (body-end (point-max))
1329 (entity (rmail-mime-parse)))
7e116860
KH
1330 (or
1331 ;; At first, just search the headers.
1332 (with-temp-buffer
186f7f0b 1333 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
7e116860
KH
1334 (rfc2047-decode-region (point-min) (point))
1335 (goto-char (point-min))
1336 (re-search-forward regexp nil t))
1337 ;; Next, search the body.
1338 (if (and entity
1339 (let* ((content-type (rmail-mime-entity-type entity))
1340 (charset (cdr (assq 'charset (cdr content-type)))))
1341 (or (not (string-match "text/.*" (car content-type)))
1342 (and charset
1343 (not (string= (downcase charset) "us-ascii"))))))
1344 ;; Search the decoded MIME message.
1345 (with-temp-buffer
186f7f0b 1346 (rmail-mime-insert entity)
7e116860
KH
1347 (goto-char (point-min))
1348 (re-search-forward regexp nil t))
1349 ;; Search the body without decoding.
1350 (goto-char header-end)
1351 (re-search-forward regexp nil t))))))
1352
1353(setq rmail-search-mime-message-function 'rmail-search-mime-message)
1354
537ab246
BG
1355(provide 'rmailmm)
1356
35426db4
GM
1357;; Local Variables:
1358;; generated-autoload-file: "rmail.el"
1359;; End:
1360
537ab246
BG
1361;; arch-tag: 3f2c5e5d-1aef-4512-bc20-fd737c9d5dd9
1362;;; rmailmm.el ends here