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