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))
1a6a03e4 463 (setq rmail-mime-coding-system (cons last-coding-system-used nil)))
186f7f0b
KH
464 (goto-char (point-min))
465 (rmail-highlight-headers)
466 (goto-char (point-max))
467 (insert "\n"))))
468
1a6a03e4
KH
469(defun rmail-mime-find-header-encoding (header)
470 "Retun the last coding system used to decode HEADER.
471HEADER is a header component of a MIME-entity object (see
472`rmail-mime-entity')."
473 (with-temp-buffer
16bc9688 474 (let ((buf (current-buffer)))
1a6a03e4 475 (with-current-buffer rmail-mime-mbox-buffer
16bc9688
KH
476 (let ((last-coding-system-used nil)
477 (rmail-buffer rmail-mime-mbox-buffer)
478 (rmail-view-buffer buf))
1a6a03e4
KH
479 (save-excursion
480 (goto-char (aref header 0))
481 (rmail-copy-headers (point) (aref header 1)))))
482 (rfc2047-decode-region (point-min) (point-max))
483 last-coding-system-used)))
484
537ab246
BG
485(defun rmail-mime-text-handler (content-type
486 content-disposition
487 content-transfer-encoding)
488 "Handle the current buffer as a plain text MIME part."
186f7f0b
KH
489 (rmail-mime-insert-text
490 (rmail-mime-entity content-type content-disposition
491 content-transfer-encoding
492 (vector (vector nil nil nil) (vector nil nil t))
493 (vector nil nil nil) (vector "" (cons nil nil) t)
494 (vector nil nil nil) nil 'rmail-mime-insert-text))
495 t)
496
497(defun rmail-mime-insert-decoded-text (entity)
498 "Decode and insert the text body of MIME-entity ENTITY."
d1be4ec2
KH
499 (let* ((content-type (rmail-mime-entity-type entity))
500 (charset (cdr (assq 'charset (cdr content-type))))
186f7f0b
KH
501 (coding-system (if charset
502 (coding-system-from-name charset)))
503 (body (rmail-mime-entity-body entity))
504 (pos (point)))
505 (or (and coding-system (coding-system-p coding-system))
506 (setq coding-system 'undecided))
507 (if (stringp (aref body 0))
508 (insert (aref body 0))
509 (let ((transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
510 (insert-buffer-substring rmail-mime-mbox-buffer
511 (aref body 0) (aref body 1))
512 (cond ((string= transfer-encoding "base64")
513 (ignore-errors (base64-decode-region pos (point))))
514 ((string= transfer-encoding "quoted-printable")
515 (quoted-printable-decode-region pos (point))))))
516 (decode-coding-region pos (point) coding-system)
16bc9688
KH
517 (if (and
518 (or (not rmail-mime-coding-system) (consp rmail-mime-coding-system))
519 (not (eq (coding-system-base coding-system) 'us-ascii)))
186f7f0b
KH
520 (setq rmail-mime-coding-system coding-system))
521 (or (bolp) (insert "\n"))))
522
523(defun rmail-mime-insert-text (entity)
524 "Presentation handler for a plain text MIME entity."
525 (let ((current (aref (rmail-mime-entity-display entity) 0))
526 (new (aref (rmail-mime-entity-display entity) 1))
527 (header (rmail-mime-entity-header entity))
528 (tagline (rmail-mime-entity-tagline entity))
529 (body (rmail-mime-entity-body entity))
530 (beg (point))
531 (segment (rmail-mime-entity-segment (point) entity)))
532
533 (or (integerp (aref body 0))
534 (let ((data (buffer-string)))
535 (aset body 0 data)
536 (delete-region (point-min) (point-max))))
537
538 ;; header
539 (if (eq (aref current 0) (aref new 0))
540 (goto-char (aref segment 2))
541 (if (aref current 0)
542 (delete-char (- (aref segment 2) (aref segment 1))))
543 (if (aref new 0)
544 (rmail-mime-insert-header header)))
545 ;; tagline
546 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
547 (if (or (not (aref current 1))
548 (eq (aref current 2) (aref new 2)))
549 (forward-char (- (aref segment 3) (aref segment 2)))
550 (rmail-mime-update-tagline entity))
186f7f0b
KH
551 (if (aref current 1)
552 (delete-char (- (aref segment 3) (aref segment 2))))
553 (if (aref new 1)
554 (rmail-mime-insert-tagline entity)))
555 ;; body
556 (if (eq (aref current 2) (aref new 2))
557 (forward-char (- (aref segment 4) (aref segment 3)))
558 (if (aref current 2)
559 (delete-char (- (aref segment 4) (aref segment 3))))
560 (if (aref new 2)
561 (rmail-mime-insert-decoded-text entity)))
562 (put-text-property beg (point) 'rmail-mime-entity entity)))
d1be4ec2 563
f4ce6150 564;; FIXME move to the test/ directory?
537ab246
BG
565(defun test-rmail-mime-handler ()
566 "Test of a mail using no MIME parts at all."
567 (let ((mail "To: alex@gnu.org
568Content-Type: text/plain; charset=koi8-r
569Content-Transfer-Encoding: 8bit
570MIME-Version: 1.0
571
572\372\304\322\301\327\323\324\327\325\312\324\305\41"))
573 (switch-to-buffer (get-buffer-create "*test*"))
574 (erase-buffer)
575 (set-buffer-multibyte nil)
576 (insert mail)
577 (rmail-mime-show t)
578 (set-buffer-multibyte t)))
579
e8652dd9 580
186f7f0b
KH
581(defun rmail-mime-insert-image (entity)
582 "Decode and insert the image body of MIME-entity ENTITY."
583 (let* ((content-type (car (rmail-mime-entity-type entity)))
584 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
585 (body (rmail-mime-entity-body entity))
586 data)
587 (if (stringp (aref body 0))
588 (setq data (aref body 0))
589 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
590 (transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
d1be4ec2
KH
591 (with-temp-buffer
592 (set-buffer-multibyte nil)
593 (setq buffer-undo-list t)
186f7f0b
KH
594 (insert-buffer-substring rmail-mime-mbox-buffer
595 (aref body 0) (aref body 1))
d1be4ec2
KH
596 (cond ((string= transfer-encoding "base64")
597 (ignore-errors (base64-decode-region (point-min) (point-max))))
598 ((string= transfer-encoding "quoted-printable")
599 (quoted-printable-decode-region (point-min) (point-max))))
600 (setq data
601 (buffer-substring-no-properties (point-min) (point-max))))))
186f7f0b
KH
602 (insert-image (create-image data (cdr bulk-data) t))
603 (insert "\n")))
e8652dd9 604
e7ca0062
KH
605(defun rmail-mime-toggle-button (button)
606 "Hide or show the body of the MIME-entity associated with BUTTON."
186f7f0b 607 (save-excursion
e7ca0062 608 (goto-char (button-start button))
186f7f0b 609 (rmail-mime-toggle-hidden)))
e8652dd9 610
e7ca0062 611(define-button-type 'rmail-mime-toggle 'action 'rmail-mime-toggle-button)
e8652dd9
GM
612
613
537ab246
BG
614(defun rmail-mime-bulk-handler (content-type
615 content-disposition
e8652dd9 616 content-transfer-encoding)
2e9075d3 617 "Handle the current buffer as an attachment to download.
e8652dd9
GM
618For images that Emacs is capable of displaying, the behavior
619depends upon the value of `rmail-mime-show-images'."
d1be4ec2
KH
620 (rmail-mime-insert-bulk
621 (rmail-mime-entity content-type content-disposition content-transfer-encoding
186f7f0b
KH
622 (vector (vector nil nil nil) (vector nil t nil))
623 (vector nil nil nil) (vector "" (cons nil nil) t)
624 (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
625
626(defun rmail-mime-set-bulk-data (entity)
627 "Setup the information about the attachment object for MIME-entity ENTITY.
628The value is non-nil if and only if the attachment object should be shown
629directly."
630 (let ((content-type (car (rmail-mime-entity-type entity)))
631 (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
632 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
633 (body (rmail-mime-entity-body entity))
e7ca0062 634 type to-show)
186f7f0b
KH
635 (cond (size
636 (setq size (string-to-number size)))
637 ((stringp (aref body 0))
638 (setq size (length (aref body 0))))
639 (t
640 ;; Rough estimation of the size.
641 (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
642 (setq size (- (aref body 1) (aref body 0)))
643 (cond ((string= encoding "base64")
644 (setq size (/ (* size 3) 4)))
645 ((string= encoding "quoted-printable")
646 (setq size (/ (* size 7) 3)))))))
647
648 (cond
649 ((string-match "text/" content-type)
650 (setq type 'text))
651 ((string-match "image/\\(.*\\)" content-type)
652 (setq type (image-type-from-file-name
653 (concat "." (match-string 1 content-type))))
654 (if (and (memq type image-types)
655 (image-type-available-p type))
656 (if (and rmail-mime-show-images
657 (not (eq rmail-mime-show-images 'button))
658 (or (not (numberp rmail-mime-show-images))
659 (< size rmail-mime-show-images)))
660 (setq to-show t))
661 (setq type nil))))
662 (setcar bulk-data size)
663 (setcdr bulk-data type)
664 to-show))
d1be4ec2
KH
665
666(defun rmail-mime-insert-bulk (entity)
186f7f0b 667 "Presentation handler for an attachment MIME entity."
d1be4ec2
KH
668 (let* ((content-type (rmail-mime-entity-type entity))
669 (content-disposition (rmail-mime-entity-disposition entity))
186f7f0b
KH
670 (current (aref (rmail-mime-entity-display entity) 0))
671 (new (aref (rmail-mime-entity-display entity) 1))
672 (header (rmail-mime-entity-header entity))
673 (tagline (rmail-mime-entity-tagline entity))
674 (bulk-data (aref tagline 1))
d1be4ec2 675 (body (rmail-mime-entity-body entity))
e7ca0062 676 ;; Find the default directory for this media type.
d1be4ec2 677 (directory (catch 'directory
537ab246
BG
678 (dolist (entry rmail-mime-attachment-dirs-alist)
679 (when (string-match (car entry) (car content-type))
680 (dolist (dir (cdr entry))
681 (when (file-directory-p dir)
682 (throw 'directory dir)))))))
683 (filename (or (cdr (assq 'name (cdr content-type)))
684 (cdr (assq 'filename (cdr content-disposition)))
685 "noname"))
69220882 686 (units '(B kB MB GB))
186f7f0b
KH
687 (segment (rmail-mime-entity-segment (point) entity))
688 beg data size)
689
690 (if (integerp (aref body 0))
d1be4ec2 691 (setq data entity
186f7f0b
KH
692 size (car bulk-data))
693 (if (stringp (aref body 0))
694 (setq data (aref body 0))
695 (setq data (string-as-unibyte (buffer-string)))
696 (aset body 0 data)
697 (rmail-mime-set-bulk-data entity)
698 (delete-region (point-min) (point-max)))
699 (setq size (length data)))
d1be4ec2 700 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
69220882
GM
701 (cdr units))
702 (setq size (/ size 1024.0)
703 units (cdr units)))
186f7f0b
KH
704
705 (setq beg (point))
706
707 ;; header
708 (if (eq (aref current 0) (aref new 0))
709 (goto-char (aref segment 2))
710 (if (aref current 0)
711 (delete-char (- (aref segment 2) (aref segment 1))))
712 (if (aref new 0)
713 (rmail-mime-insert-header header)))
714
715 ;; tagline
716 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
717 (if (or (not (aref current 1))
718 (eq (aref current 2) (aref new 2)))
719 (forward-char (- (aref segment 3) (aref segment 2)))
720 (rmail-mime-update-tagline entity))
186f7f0b
KH
721 (if (aref current 1)
722 (delete-char (- (aref segment 3) (aref segment 2))))
723 (if (aref new 1)
724 (rmail-mime-insert-tagline
725 entity
e7ca0062 726 " Save:"
186f7f0b
KH
727 (list filename
728 :type 'rmail-mime-save
729 'help-echo "mouse-2, RET: Save attachment"
730 'filename filename
731 'directory (file-name-as-directory directory)
732 'data data)
733 (format " (%.0f%s)" size (car units))
e7ca0062
KH
734 ;; We don't need this button because the "type" string of a
735 ;; tagline is the button to do this.
736 ;; (if (cdr bulk-data)
737 ;; " ")
738 ;; (if (cdr bulk-data)
739 ;; (list "Toggle show/hide"
740 ;; :type 'rmail-mime-image
741 ;; 'help-echo "mouse-2, RET: Toggle show/hide"
742 ;; 'image-type (cdr bulk-data)
743 ;; 'image-data data))
744 )))
186f7f0b
KH
745 ;; body
746 (if (eq (aref current 2) (aref new 2))
747 (forward-char (- (aref segment 4) (aref segment 3)))
748 (if (aref current 2)
749 (delete-char (- (aref segment 4) (aref segment 3))))
750 (if (aref new 2)
751 (cond ((eq (cdr bulk-data) 'text)
752 (rmail-mime-insert-decoded-text entity))
753 ((cdr bulk-data)
8434f239
KH
754 (rmail-mime-insert-image entity))
755 (t
756 ;; As we don't know how to display the body, just
757 ;; insert it as a text.
758 (rmail-mime-insert-decoded-text entity)))))
186f7f0b 759 (put-text-property beg (point) 'rmail-mime-entity entity)))
537ab246
BG
760
761(defun test-rmail-mime-bulk-handler ()
762 "Test of a mail used as an example in RFC 2183."
763 (let ((mail "Content-Type: image/jpeg
764Content-Disposition: attachment; filename=genome.jpeg;
765 modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";
766Content-Description: a complete map of the human genome
767Content-Transfer-Encoding: base64
768
769iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAZQ
770TFRF////AAAAVcLTfgAAAPZJREFUeNq9ldsOwzAIQ+3//+l1WlvA5ZLsoUiTto4TB+ISoAjy
771+ITfRBfcAmgRFFeAm+J6uhdKdFhFWUgDkFsK0oUp/9G2//Kj7Jx+5tSKOdBscgUYiKHRS/me
772WATQdRUvAK0Bnmshmtn79PpaLBbbOZkjKvRnjRZoRswOkG1wFchKew2g9wXVJVZL/m4+B+vv
7739AxQQR2Q33SgAYJzzVACdAWjAfRYzYFO9n6SLnydtQHSMxYDMAKqZ/8FS/lTK+zuq3CtK64L
774UDwbgUEAUmk2Zyg101d6PhCDySgAvTvDgKiuOrc4dLxUb7UMnhGIexyI+d6U+ABuNAP4Simx
775lgAAAABJRU5ErkJggg==
776"))
777 (switch-to-buffer (get-buffer-create "*test*"))
778 (erase-buffer)
779 (insert mail)
780 (rmail-mime-show)))
781
782(defun rmail-mime-multipart-handler (content-type
783 content-disposition
784 content-transfer-encoding)
785 "Handle the current buffer as a multipart MIME body.
786The current buffer should be narrowed to the body. CONTENT-TYPE,
787CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
788of the respective parsed headers. See `rmail-mime-handle' for their
789format."
d1be4ec2 790 (rmail-mime-process-multipart
186f7f0b
KH
791 content-type content-disposition content-transfer-encoding nil)
792 t)
d1be4ec2
KH
793
794(defun rmail-mime-process-multipart (content-type
795 content-disposition
796 content-transfer-encoding
186f7f0b 797 parse-tag)
d1be4ec2
KH
798 "Process the current buffer as a multipart MIME body.
799
186f7f0b
KH
800If PARSE-TAG is nil, modify the current buffer directly for
801showing the MIME body and return nil.
d1be4ec2 802
186f7f0b
KH
803Otherwise, PARSE-TAG is a string indicating the depth and index
804number of the entity. In this case, parse the current buffer and
805return a list of MIME-entity objects.
d1be4ec2
KH
806
807The other arguments are the same as `rmail-mime-multipart-handler'."
537ab246
BG
808 ;; Some MUAs start boundaries with "--", while it should start
809 ;; with "CRLF--", as defined by RFC 2046:
810 ;; The boundary delimiter MUST occur at the beginning of a line,
811 ;; i.e., following a CRLF, and the initial CRLF is considered to
812 ;; be attached to the boundary delimiter line rather than part
813 ;; of the preceding part.
814 ;; We currently don't handle that.
815 (let ((boundary (cdr (assq 'boundary content-type)))
186f7f0b
KH
816 (subtype (cadr (split-string (car content-type) "/")))
817 (index 0)
d1be4ec2 818 beg end next entities)
537ab246
BG
819 (unless boundary
820 (rmail-mm-get-boundary-error-message
821 "No boundary defined" content-type content-disposition
822 content-transfer-encoding))
823 (setq boundary (concat "\n--" boundary))
824 ;; Hide the body before the first bodypart
825 (goto-char (point-min))
826 (when (and (search-forward boundary nil t)
827 (looking-at "[ \t]*\n"))
186f7f0b 828 (if parse-tag
d1be4ec2
KH
829 (narrow-to-region (match-end 0) (point-max))
830 (delete-region (point-min) (match-end 0))))
186f7f0b
KH
831
832 ;; Change content-type to the proper default one for the children.
833 (cond ((string-match "mixed" subtype)
834 (setq content-type '("text/plain")))
835 ((string-match "digest" subtype)
8434f239
KH
836 (setq content-type '("message/rfc822")))
837 (t
838 (setq content-type nil)))
186f7f0b 839
537ab246
BG
840 ;; Loop over all body parts, where beg points at the beginning of
841 ;; the part and end points at the end of the part. next points at
186f7f0b
KH
842 ;; the beginning of the next part. The current point is just
843 ;; after the boundary tag.
537ab246
BG
844 (setq beg (point-min))
845 (while (search-forward boundary nil t)
846 (setq end (match-beginning 0))
847 ;; If this is the last boundary according to RFC 2046, hide the
848 ;; epilogue, else hide the boundary only. Use a marker for
849 ;; `next' because `rmail-mime-show' may change the buffer.
ffa1fed6 850 (cond ((looking-at "--[ \t]*$")
537ab246
BG
851 (setq next (point-max-marker)))
852 ((looking-at "[ \t]*\n")
ffa1fed6 853 (setq next (copy-marker (match-end 0) t)))
537ab246
BG
854 (t
855 (rmail-mm-get-boundary-error-message
856 "Malformed boundary" content-type content-disposition
857 content-transfer-encoding)))
186f7f0b
KH
858
859 (setq index (1+ index))
537ab246 860 ;; Handle the part.
186f7f0b 861 (if parse-tag
d1be4ec2
KH
862 (save-restriction
863 (narrow-to-region beg end)
186f7f0b
KH
864 (let ((child (rmail-mime-process
865 nil (format "%s/%d" parse-tag index)
866 content-type content-disposition)))
867 ;; Display a tagline.
868 (aset (aref (rmail-mime-entity-display child) 1) 1
869 (aset (rmail-mime-entity-tagline child) 2 t))
870 (push child entities)))
871
d1be4ec2
KH
872 (delete-region end next)
873 (save-restriction
874 (narrow-to-region beg end)
875 (rmail-mime-show)))
876 (goto-char (setq beg next)))
186f7f0b
KH
877
878 (when parse-tag
879 (setq entities (nreverse entities))
880 (if (string-match "alternative" subtype)
881 ;; Find the best entity to show, and hide all the others.
882 (let (best second)
883 (dolist (child entities)
884 (if (string= (or (car (rmail-mime-entity-disposition child))
885 (car content-disposition))
886 "inline")
887 (if (string-match "text/plain"
888 (car (rmail-mime-entity-type child)))
889 (setq best child)
890 (if (string-match "text/.*"
891 (car (rmail-mime-entity-type child)))
892 (setq second child)))))
893 (or best (not second) (setq best second))
894 (dolist (child entities)
e7ca0062
KH
895 (unless (eq best child)
896 (aset (rmail-mime-entity-body child) 2 nil)
897 (rmail-mime-hidden-mode child)))))
186f7f0b 898 entities)))
537ab246
BG
899
900(defun test-rmail-mime-multipart-handler ()
901 "Test of a mail used as an example in RFC 2046."
902 (let ((mail "From: Nathaniel Borenstein <nsb@bellcore.com>
903To: Ned Freed <ned@innosoft.com>
904Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
905Subject: Sample message
906MIME-Version: 1.0
907Content-type: multipart/mixed; boundary=\"simple boundary\"
908
909This is the preamble. It is to be ignored, though it
910is a handy place for composition agents to include an
911explanatory note to non-MIME conformant readers.
912
913--simple boundary
914
915This is implicitly typed plain US-ASCII text.
916It does NOT end with a linebreak.
917--simple boundary
918Content-type: text/plain; charset=us-ascii
919
920This is explicitly typed plain US-ASCII text.
921It DOES end with a linebreak.
922
923--simple boundary--
924
925This is the epilogue. It is also to be ignored."))
926 (switch-to-buffer (get-buffer-create "*test*"))
927 (erase-buffer)
928 (insert mail)
929 (rmail-mime-show t)))
930
186f7f0b
KH
931(defun rmail-mime-insert-multipart (entity)
932 "Presentation handler for a multipart MIME entity."
933 (let ((current (aref (rmail-mime-entity-display entity) 0))
934 (new (aref (rmail-mime-entity-display entity) 1))
935 (header (rmail-mime-entity-header entity))
936 (tagline (rmail-mime-entity-tagline entity))
937 (body (rmail-mime-entity-body entity))
938 (beg (point))
939 (segment (rmail-mime-entity-segment (point) entity)))
940 ;; header
941 (if (eq (aref current 0) (aref new 0))
942 (goto-char (aref segment 2))
943 (if (aref current 0)
944 (delete-char (- (aref segment 2) (aref segment 1))))
945 (if (aref new 0)
946 (rmail-mime-insert-header header)))
947 ;; tagline
948 (if (eq (aref current 1) (aref new 1))
e7ca0062
KH
949 (if (or (not (aref current 1))
950 (eq (aref current 2) (aref new 2)))
951 (forward-char (- (aref segment 3) (aref segment 2)))
952 (rmail-mime-update-tagline entity))
186f7f0b
KH
953 (if (aref current 1)
954 (delete-char (- (aref segment 3) (aref segment 2))))
955 (if (aref new 1)
956 (rmail-mime-insert-tagline entity)))
957
958 (put-text-property beg (point) 'rmail-mime-entity entity)
e7ca0062 959
186f7f0b
KH
960 ;; body
961 (if (eq (aref current 2) (aref new 2))
962 (forward-char (- (aref segment 4) (aref segment 3)))
e7ca0062
KH
963 (dolist (child (rmail-mime-entity-children entity))
964 (rmail-mime-insert child)))
965 entity))
186f7f0b 966
537ab246
BG
967;;; Main code
968
969(defun rmail-mime-handle (content-type
970 content-disposition
971 content-transfer-encoding)
972 "Handle the current buffer as a MIME part.
973The current buffer should be narrowed to the respective body, and
974point should be at the beginning of the body.
975
976CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
0c9ff2c5
GM
977are the values of the respective parsed headers. The latter should
978be downcased. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
979have the form
537ab246
BG
980
981 \(VALUE . ALIST)
982
983In other words:
984
985 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
986
987VALUE is a string and ATTRIBUTE is a symbol.
988
989Consider the following header, for example:
990
991Content-Type: multipart/mixed;
992 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
993
994The parsed header value:
995
996\(\"multipart/mixed\"
997 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
998 ;; Handle the content transfer encodings we know. Unknown transfer
999 ;; encodings will be passed on to the various handlers.
1000 (cond ((string= content-transfer-encoding "base64")
1001 (when (ignore-errors
1002 (base64-decode-region (point) (point-max)))
1003 (setq content-transfer-encoding nil)))
1004 ((string= content-transfer-encoding "quoted-printable")
1005 (quoted-printable-decode-region (point) (point-max))
1006 (setq content-transfer-encoding nil))
1007 ((string= content-transfer-encoding "8bit")
1008 ;; FIXME: Is this the correct way?
c893016b
SM
1009 ;; No, of course not, it just means there's no decoding to do.
1010 ;; (set-buffer-multibyte nil)
1011 (setq content-transfer-encoding nil)
1012 ))
537ab246
BG
1013 ;; Inline stuff requires work. Attachments are handled by the bulk
1014 ;; handler.
1015 (if (string= "inline" (car content-disposition))
1016 (let ((stop nil))
1017 (dolist (entry rmail-mime-media-type-handlers-alist)
1018 (when (and (string-match (car entry) (car content-type)) (not stop))
1019 (progn
1020 (setq stop (funcall (cadr entry) content-type
1021 content-disposition
1022 content-transfer-encoding))))))
1023 ;; Everything else is an attachment.
1024 (rmail-mime-bulk-handler content-type
1025 content-disposition
e7ca0062
KH
1026 content-transfer-encoding))
1027 (save-restriction
1028 (widen)
1029 (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
1030 current new)
1031 (when entity
1032 (setq current (aref (rmail-mime-entity-display entity) 0)
1033 new (aref (rmail-mime-entity-display entity) 1))
1034 (dotimes (i 3)
1035 (aset current i (aref new i)))))))
537ab246
BG
1036
1037(defun rmail-mime-show (&optional show-headers)
1038 "Handle the current buffer as a MIME message.
1039If SHOW-HEADERS is non-nil, then the headers of the current part
1040will shown as usual for a MIME message. The headers are also
1041shown for the content type message/rfc822. This function will be
1042called recursively if multiple parts are available.
1043
1044The current buffer must contain a single message. It will be
1045modified."
d1be4ec2
KH
1046 (rmail-mime-process show-headers nil))
1047
186f7f0b
KH
1048(defun rmail-mime-process (show-headers parse-tag &optional
1049 default-content-type
1050 default-content-disposition)
537ab246
BG
1051 (let ((end (point-min))
1052 content-type
1053 content-transfer-encoding
1054 content-disposition)
1055 ;; `point-min' returns the beginning and `end' points at the end
1056 ;; of the headers.
1057 (goto-char (point-min))
1058 ;; If we're showing a part without headers, then it will start
1059 ;; with a newline.
1060 (if (eq (char-after) ?\n)
1061 (setq end (1+ (point)))
1062 (when (search-forward "\n\n" nil t)
1063 (setq end (match-end 0))
1064 (save-restriction
1065 (narrow-to-region (point-min) end)
1066 ;; FIXME: Default disposition of the multipart entities should
1067 ;; be inherited.
1068 (setq content-type
1069 (mail-fetch-field "Content-Type")
1070 content-transfer-encoding
1071 (mail-fetch-field "Content-Transfer-Encoding")
1072 content-disposition
1073 (mail-fetch-field "Content-Disposition")))))
0c9ff2c5
GM
1074 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1075 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1076 (if content-transfer-encoding
1077 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1078 (setq content-type
1079 (if content-type
e7ca0062
KH
1080 (or (mail-header-parse-content-type content-type)
1081 '("text/plain"))
186f7f0b 1082 (or default-content-type '("text/plain"))))
537ab246
BG
1083 (setq content-disposition
1084 (if content-disposition
1085 (mail-header-parse-content-disposition content-disposition)
1086 ;; If none specified, we are free to choose what we deem
1087 ;; suitable according to RFC 2183. We like inline.
186f7f0b 1088 (or default-content-disposition '("inline"))))
537ab246
BG
1089 ;; Unrecognized disposition types are to be treated like
1090 ;; attachment according to RFC 2183.
1091 (unless (member (car content-disposition) '("inline" "attachment"))
1092 (setq content-disposition '("attachment")))
d1be4ec2 1093
186f7f0b
KH
1094 (if parse-tag
1095 (let* ((is-inline (string= (car content-disposition) "inline"))
1096 (header (vector (point-min) end nil))
1097 (tagline (vector parse-tag (cons nil nil) t))
1098 (body (vector end (point-max) is-inline))
1099 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1100 children handler entity)
1101 (cond ((string-match "multipart/.*" (car content-type))
1102 (save-restriction
1103 (narrow-to-region (1- end) (point-max))
1104 (setq children (rmail-mime-process-multipart
1105 content-type
1106 content-disposition
1107 content-transfer-encoding
1108 parse-tag)
1109 handler 'rmail-mime-insert-multipart)))
1110 ((string-match "message/rfc822" (car content-type))
1111 (save-restriction
d1be4ec2 1112 (narrow-to-region end (point-max))
186f7f0b
KH
1113 (let* ((msg (rmail-mime-process t parse-tag
1114 '("text/plain") '("inline")))
1115 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1116 ;; Show header of the child.
1117 (aset msg-new 0 t)
1118 (aset (rmail-mime-entity-header msg) 2 t)
1119 ;; Hide tagline of the child.
1120 (aset msg-new 1 nil)
1121 (aset (rmail-mime-entity-tagline msg) 2 nil)
1122 (setq children (list msg)
1123 handler 'rmail-mime-insert-multipart))))
1124 ((and is-inline (string-match "text/" (car content-type)))
1125 ;; Don't need a tagline.
1126 (aset new 1 (aset tagline 2 nil))
1127 (setq handler 'rmail-mime-insert-text))
1128 (t
1129 ;; Force hidden mode.
1130 (aset new 1 (aset tagline 2 t))
1131 (aset new 2 (aset body 2 nil))
1132 (setq handler 'rmail-mime-insert-bulk)))
1133 (setq entity (rmail-mime-entity content-type
1134 content-disposition
1135 content-transfer-encoding
1136 (vector (vector nil nil nil) new)
1137 header tagline body children handler))
1138 (if (and (eq handler 'rmail-mime-insert-bulk)
1139 (rmail-mime-set-bulk-data entity))
1140 ;; Show the body.
1141 (aset new 2 (aset body 2 t)))
1142 entity)
1143
d1be4ec2 1144 ;; Hide headers and handle the part.
186f7f0b
KH
1145 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1146 (rmail-mime-entity
1147 content-type content-disposition
1148 content-transfer-encoding
1149 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1150 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1151 (vector nil nil 'raw) nil nil))
d1be4ec2
KH
1152 (save-restriction
1153 (cond ((string= (car content-type) "message/rfc822")
1154 (narrow-to-region end (point-max)))
1155 ((not show-headers)
1156 (delete-region (point-min) end)))
1157 (rmail-mime-handle content-type content-disposition
1158 content-transfer-encoding)))))
1159
d1be4ec2
KH
1160(defun rmail-mime-parse ()
1161 "Parse the current Rmail message as a MIME message.
8258ae3f
KH
1162The value is a MIME-entiy object (see `rmail-mime-entity').
1163If an error occurs, return an error message string."
186f7f0b
KH
1164 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1165 rmail-view-buffer
1166 (current-buffer))))
8258ae3f 1167 (condition-case err
186f7f0b
KH
1168 (with-current-buffer rmail-mime-mbox-buffer
1169 (save-excursion
1170 (goto-char (point-min))
1171 (let* ((entity (rmail-mime-process t ""
1172 '("text/plain") '("inline")))
1173 (new (aref (rmail-mime-entity-display entity) 1)))
1174 ;; Show header.
1175 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1176 ;; Show tagline if and only if body is not shown.
1177 (if (aref new 2)
1178 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 nil))
1179 (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 t)))
1180 entity)))
8258ae3f 1181 (error (format "%s" err)))))
186f7f0b
KH
1182
1183(defun rmail-mime-insert (entity)
d1be4ec2
KH
1184 "Insert a MIME-entity ENTITY in the current buffer.
1185
1186This function will be called recursively if multiple parts are
1187available."
186f7f0b
KH
1188 (let ((current (aref (rmail-mime-entity-display entity) 0))
1189 (new (aref (rmail-mime-entity-display entity) 1)))
1190 (if (not (eq (aref new 0) 'raw))
1191 ;; Not a raw-mode. Each handler should handle it.
1192 (funcall (rmail-mime-entity-handler entity) entity)
1193 (let ((header (rmail-mime-entity-header entity))
1194 (tagline (rmail-mime-entity-tagline entity))
1195 (body (rmail-mime-entity-body entity))
1196 (beg (point))
1197 (segment (rmail-mime-entity-segment (point) entity)))
1198 ;; header
1199 (if (eq (aref current 0) (aref new 0))
1200 (goto-char (aref segment 2))
1201 (if (aref current 0)
1202 (delete-char (- (aref segment 2) (aref segment 1))))
1203 (insert-buffer-substring rmail-mime-mbox-buffer
1204 (aref header 0) (aref header 1)))
1205 ;; tagline
1206 (if (aref current 1)
1207 (delete-char (- (aref segment 3) (aref segment 2))))
1208 ;; body
e7ca0062
KH
1209 (let ((children (rmail-mime-entity-children entity)))
1210 (if children
1211 (progn
1212 (put-text-property beg (point) 'rmail-mime-entity entity)
1213 (dolist (child children)
1214 (rmail-mime-insert child)))
1215 (if (eq (aref current 2) (aref new 2))
1216 (forward-char (- (aref segment 4) (aref segment 3)))
1217 (if (aref current 2)
1218 (delete-char (- (aref segment 4) (aref segment 3))))
1219 (insert-buffer-substring rmail-mime-mbox-buffer
1220 (aref body 0) (aref body 1))
1221 (or (bolp) (insert "\n")))
1222 (put-text-property beg (point) 'rmail-mime-entity entity)))))
186f7f0b
KH
1223 (dotimes (i 3)
1224 (aset current i (aref new i)))))
537ab246 1225
2e9075d3
GM
1226(define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1227 "Major mode used in `rmail-mime' buffers."
1228 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1229
73422054 1230;;;###autoload
186f7f0b
KH
1231(defun rmail-mime (&optional arg)
1232 "Toggle displaying of a MIME message.
1233
1234The actualy behavior depends on the value of `rmail-enable-mime'.
1235
1236If `rmail-enable-mime' is t (default), this command change the
1237displaying of a MIME message between decoded presentation form
1238and raw data.
1239
1240With ARG, toggle the displaying of the current MIME entity only.
1241
1242If `rmail-enable-mime' is nil, this creates a temporary
1243\"*RMAIL*\" buffer holding a decoded copy of the message. Inline
1244content-types are handled according to
f4ce6150
GM
1245`rmail-mime-media-type-handlers-alist'. By default, this
1246displays text and multipart messages, and offers to download
1247attachments as specfied by `rmail-mime-attachment-dirs-alist'."
186f7f0b
KH
1248 (interactive "P")
1249 (if rmail-enable-mime
e7ca0062
KH
1250 (with-current-buffer rmail-buffer
1251 (if (rmail-mime-message-p)
1252 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
1253 (rmail-mime-view-buffer rmail-buffer)
1254 (entity (get-text-property (point) 'rmail-mime-entity)))
1255 (if arg
1256 (if entity
1257 (rmail-mime-toggle-raw entity))
1258 (goto-char (point-min))
1259 (rmail-mime-toggle-raw
1260 (get-text-property (point) 'rmail-mime-entity))))
1261 (message "Not a MIME message")))
186f7f0b
KH
1262 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1263 (buf (get-buffer-create "*RMAIL*"))
1264 (rmail-mime-mbox-buffer rmail-view-buffer)
1265 (rmail-mime-view-buffer buf))
1266 (set-buffer buf)
1267 (setq buffer-undo-list t)
1268 (let ((inhibit-read-only t))
1269 ;; Decoding the message in fundamental mode for speed, only
1270 ;; switching to rmail-mime-mode at the end for display. Eg
1271 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1272 (fundamental-mode)
1273 (erase-buffer)
1274 (insert data)
1275 (rmail-mime-show t)
1276 (rmail-mime-mode)
1277 (set-buffer-modified-p nil))
1278 (view-buffer buf))))
537ab246
BG
1279
1280(defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1281 "Return MESSAGE with more information on the main mime components."
1282 (error "%s; type: %s; disposition: %s; encoding: %s"
1283 message type disposition encoding))
1284
d1be4ec2 1285(defun rmail-show-mime ()
7e116860 1286 "Function to set in `rmail-show-mime-function' (which see)."
186f7f0b
KH
1287 (let ((entity (rmail-mime-parse))
1288 (rmail-mime-mbox-buffer rmail-buffer)
1289 (rmail-mime-view-buffer rmail-view-buffer)
1290 (rmail-mime-coding-system nil))
8258ae3f 1291 (if (vectorp entity)
186f7f0b
KH
1292 (with-current-buffer rmail-mime-view-buffer
1293 (erase-buffer)
1294 (rmail-mime-insert entity)
1a6a03e4
KH
1295 (if (consp rmail-mime-coding-system)
1296 ;; Decoding is done by rfc2047-decode-region only for a
1297 ;; header. But, as the used coding system may have been
1298 ;; overriden by mm-charset-override-alist, we can't
1299 ;; trust (car rmail-mime-coding-system). So, here we
1300 ;; try the decoding again with mm-charset-override-alist
1301 ;; bound to nil.
1302 (let ((mm-charset-override-alist nil))
1303 (setq rmail-mime-coding-system
1304 (rmail-mime-find-header-encoding
1305 (rmail-mime-entity-header entity)))))
1306 (set-buffer-file-coding-system
16bc9688
KH
1307 (if rmail-mime-coding-system
1308 (coding-system-base rmail-mime-coding-system)
1309 'undecided)
1310 t t))
8258ae3f
KH
1311 ;; Decoding failed. ENTITY is an error message. Insert the
1312 ;; original message body as is, and show warning.
186f7f0b 1313 (let ((region (with-current-buffer rmail-mime-mbox-buffer
7e116860
KH
1314 (goto-char (point-min))
1315 (re-search-forward "^$" nil t)
1316 (forward-line 1)
8258ae3f 1317 (vector (point-min) (point) (point-max)))))
186f7f0b 1318 (with-current-buffer rmail-mime-view-buffer
7e116860
KH
1319 (let ((inhibit-read-only t))
1320 (erase-buffer)
8258ae3f 1321 (rmail-mime-insert-header region)
186f7f0b 1322 (insert-buffer-substring rmail-mime-mbox-buffer
8258ae3f 1323 (aref region 1) (aref region 2))))
186f7f0b 1324 (set-buffer-file-coding-system 'no-conversion t t)
8258ae3f 1325 (message "MIME decoding failed: %s" entity)))))
d1be4ec2
KH
1326
1327(setq rmail-show-mime-function 'rmail-show-mime)
1328
1329(defun rmail-insert-mime-forwarded-message (forward-buffer)
7e116860 1330 "Function to set in `rmail-insert-mime-forwarded-message-function' (which see)."
186f7f0b
KH
1331 (let ((rmail-mime-mbox-buffer
1332 (with-current-buffer forward-buffer rmail-view-buffer)))
d1be4ec2
KH
1333 (save-restriction
1334 (narrow-to-region (point) (point))
186f7f0b 1335 (message-forward-make-body-mime rmail-mime-mbox-buffer))))
d1be4ec2
KH
1336
1337(setq rmail-insert-mime-forwarded-message-function
1338 'rmail-insert-mime-forwarded-message)
1339
1340(defun rmail-insert-mime-resent-message (forward-buffer)
7e116860 1341 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
d1be4ec2
KH
1342 (insert-buffer-substring
1343 (with-current-buffer forward-buffer rmail-view-buffer))
1344 (goto-char (point-min))
1345 (when (looking-at "From ")
1346 (forward-line 1)
1347 (delete-region (point-min) (point))))
1348
1349(setq rmail-insert-mime-resent-message-function
1350 'rmail-insert-mime-resent-message)
1351
7e116860
KH
1352(defun rmail-search-mime-message (msg regexp)
1353 "Function to set in `rmail-search-mime-message-function' (which see)."
1354 (save-restriction
1355 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
186f7f0b
KH
1356 (let* ((rmail-mime-mbox-buffer (current-buffer))
1357 (rmail-mime-view-buffer rmail-view-buffer)
1358 (header-end (save-excursion
1359 (re-search-forward "^$" nil 'move) (point)))
1360 (body-end (point-max))
1361 (entity (rmail-mime-parse)))
7e116860
KH
1362 (or
1363 ;; At first, just search the headers.
1364 (with-temp-buffer
186f7f0b 1365 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
7e116860
KH
1366 (rfc2047-decode-region (point-min) (point))
1367 (goto-char (point-min))
1368 (re-search-forward regexp nil t))
1369 ;; Next, search the body.
1370 (if (and entity
1371 (let* ((content-type (rmail-mime-entity-type entity))
1372 (charset (cdr (assq 'charset (cdr content-type)))))
1373 (or (not (string-match "text/.*" (car content-type)))
1374 (and charset
1375 (not (string= (downcase charset) "us-ascii"))))))
1376 ;; Search the decoded MIME message.
1377 (with-temp-buffer
186f7f0b 1378 (rmail-mime-insert entity)
7e116860
KH
1379 (goto-char (point-min))
1380 (re-search-forward regexp nil t))
1381 ;; Search the body without decoding.
1382 (goto-char header-end)
1383 (re-search-forward regexp nil t))))))
1384
1385(setq rmail-search-mime-message-function 'rmail-search-mime-message)
1386
537ab246
BG
1387(provide 'rmailmm)
1388
35426db4
GM
1389;; Local Variables:
1390;; generated-autoload-file: "rmail.el"
1391;; End:
1392
537ab246
BG
1393;; arch-tag: 3f2c5e5d-1aef-4512-bc20-fd737c9d5dd9
1394;;; rmailmm.el ends here