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