]> 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-2015 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 (case-fold-search t)
665 coding-system)
666 (if charset (setq coding-system (coding-system-from-name charset)))
667 (or (and coding-system (coding-system-p coding-system))
668 (setq coding-system 'undecided))
669 (with-temp-buffer
670 (set-buffer-multibyte nil)
671 (setq buffer-undo-list t)
672 (insert-buffer-substring rmail-mime-mbox-buffer
673 (aref body 0) (aref body 1))
674 (cond ((string= transfer-encoding "base64")
675 (ignore-errors (base64-decode-region (point-min) (point-max))))
676 ((string= transfer-encoding "quoted-printable")
677 (quoted-printable-decode-region (point-min) (point-max))))
678 ;; Some broken MUAs state the charset only in the HTML <head>,
679 ;; so if we don't have a non-trivial coding-system at this
680 ;; point, make one last attempt to find it there.
681 (if (eq coding-system 'undecided)
682 (save-excursion
683 (goto-char (point-min))
684 (when (re-search-forward
685 "^<html><head><meta[^;]*; charset=\\([-a-zA-Z0-9]+\\)"
686 nil t)
687 (setq coding-system (coding-system-from-name (match-string 1)))
688 (or (and coding-system (coding-system-p coding-system))
689 (setq coding-system 'undecided)))
690 ;; Finally, let them manually force decoding if they know it.
691 (if (and (eq coding-system 'undecided)
692 (not (null coding-system-for-read)))
693 (setq coding-system coding-system-for-read))))
694 (decode-coding-region (point-min) (point) coding-system)
695 (if (and
696 (or (not rmail-mime-coding-system) (consp rmail-mime-coding-system))
697 (not (eq (coding-system-base coding-system) 'us-ascii)))
698 (setq rmail-mime-coding-system coding-system))
699 ;; Convert html in temporary buffer to text and insert in original buffer
700 (let ((source-buffer (current-buffer)))
701 (with-current-buffer buffer
702 (let ((start (point)))
703 (if rmail-mime-render-html-function
704 (funcall rmail-mime-render-html-function source-buffer)
705 (insert-buffer-substring source-buffer))
706 (rmail-mime-fix-inserted-faces start)))))))
707
708 (defun rmail-mime-render-html-shr (source-buffer)
709 (let ((dom (with-current-buffer source-buffer
710 (libxml-parse-html-region (point-min) (point-max))))
711 ;; Image retrieval happens asynchronously, but meanwhile
712 ;; `rmail-swap-buffers' may have been run, leaving
713 ;; `shr-image-fetched' trying to insert the image in the wrong buffer.
714 (shr-inhibit-images t)
715 ;; Bind shr-width to nil to force shr-insert-document break
716 ;; the lines at the window margin. The default is
717 ;; fill-column, whose default value is too small, and screws
718 ;; up display of the quoted messages.
719 shr-width)
720 (shr-insert-document dom)))
721
722 (defun rmail-mime-render-html-lynx (source-buffer)
723 (let ((destination-buffer (current-buffer)))
724 (with-current-buffer source-buffer
725 (call-process-region (point-min) (point-max)
726 "lynx" nil destination-buffer nil
727 "-stdin" "-dump" "-force_html"
728 "-dont_wrap_pre" "-width=70"))))
729
730 ;; Put font-lock-face properties matching face properties on text
731 ;; inserted, e.g., by shr, in text from START to point.
732 (defun rmail-mime-fix-inserted-faces (start)
733 (while (< start (point))
734 (let ((face (get-text-property start 'face))
735 (next (next-single-property-change
736 start 'face (current-buffer) (point))))
737 (if face ; anything to do?
738 (put-text-property start next 'font-lock-face face))
739 (setq start next))))
740
741 (defun rmail-mime-toggle-button (button)
742 "Hide or show the body of the MIME-entity associated with BUTTON."
743 (save-excursion
744 (goto-char (button-start button))
745 (rmail-mime-toggle-hidden)))
746
747 (define-button-type 'rmail-mime-toggle 'action 'rmail-mime-toggle-button)
748
749
750 (defun rmail-mime-bulk-handler (content-type
751 content-disposition
752 content-transfer-encoding)
753 "Handle the current buffer as an attachment to download.
754 For images that Emacs is capable of displaying, the behavior
755 depends upon the value of `rmail-mime-show-images'."
756 (rmail-mime-insert-bulk
757 (rmail-mime-entity content-type content-disposition content-transfer-encoding
758 (vector (vector nil nil nil) (vector nil t nil))
759 (vector nil nil nil) (vector "" (cons nil nil) t)
760 (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
761
762 (defun rmail-mime-set-bulk-data (entity)
763 "Setup the information about the attachment object for MIME-entity ENTITY.
764 The value is non-nil if and only if the attachment object should be shown
765 directly."
766 (let ((content-type (car (rmail-mime-entity-type entity)))
767 (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
768 (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
769 (body (rmail-mime-entity-body entity))
770 type to-show)
771 (cond (size
772 (setq size (string-to-number size)))
773 ((stringp (aref body 0))
774 (setq size (length (aref body 0))))
775 (t
776 ;; Rough estimation of the size.
777 (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
778 (setq size (- (aref body 1) (aref body 0)))
779 (cond ((string= encoding "base64")
780 (setq size (/ (* size 3) 4)))
781 ((string= encoding "quoted-printable")
782 (setq size (/ (* size 7) 3)))))))
783
784 (cond
785 ((string-match "text/html" content-type)
786 (setq type 'html))
787 ((string-match "text/" content-type)
788 (setq type 'text))
789 ((string-match "image/\\(.*\\)" content-type)
790 (setq type (image-type-from-file-name
791 (concat "." (match-string 1 content-type))))
792 (if (and (boundp 'image-types)
793 (memq type image-types)
794 (image-type-available-p type))
795 (if (and rmail-mime-show-images
796 (not (eq rmail-mime-show-images 'button))
797 (or (not (numberp rmail-mime-show-images))
798 (< size rmail-mime-show-images)))
799 (setq to-show t))
800 (setq type nil))))
801 (setcar bulk-data size)
802 (setcdr bulk-data type)
803 to-show))
804
805 (defun rmail-mime-insert-bulk (entity)
806 "Presentation handler for an attachment MIME entity."
807 (let* ((content-type (rmail-mime-entity-type entity))
808 (content-disposition (rmail-mime-entity-disposition entity))
809 (current (aref (rmail-mime-entity-display entity) 0))
810 (new (aref (rmail-mime-entity-display entity) 1))
811 (header (rmail-mime-entity-header entity))
812 (tagline (rmail-mime-entity-tagline entity))
813 (bulk-data (aref tagline 1))
814 (body (rmail-mime-entity-body entity))
815 ;; Find the default directory for this media type.
816 (directory (catch 'directory
817 (dolist (entry rmail-mime-attachment-dirs-alist)
818 (when (string-match (car entry) (car content-type))
819 (dolist (dir (cdr entry))
820 (when (file-directory-p dir)
821 (throw 'directory dir)))))))
822 (filename (or (cdr (assq 'name (cdr content-type)))
823 (cdr (assq 'filename (cdr content-disposition)))
824 "noname"))
825 (units '(B kB MB GB))
826 (segment (rmail-mime-entity-segment (point) entity))
827 beg data size)
828
829 (if (or (integerp (aref body 0)) (markerp (aref body 0)))
830 (setq data entity
831 size (car bulk-data))
832 (if (stringp (aref body 0))
833 (setq data (aref body 0))
834 (setq data (string-as-unibyte (buffer-string)))
835 (aset body 0 data)
836 (rmail-mime-set-bulk-data entity)
837 (delete-region (point-min) (point-max)))
838 (setq size (length data)))
839 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
840 (cdr units))
841 (setq size (/ size 1024.0)
842 units (cdr units)))
843
844 (setq beg (point))
845
846 ;; header
847 (if (eq (rmail-mime-display-header current)
848 (rmail-mime-display-header new))
849 (goto-char (aref segment 2))
850 (if (rmail-mime-display-header current)
851 (delete-char (- (aref segment 2) (aref segment 1))))
852 (if (rmail-mime-display-header new)
853 (rmail-mime-insert-header header)))
854
855 ;; tagline
856 (if (eq (rmail-mime-display-tagline current)
857 (rmail-mime-display-tagline new))
858 (if (or (not (rmail-mime-display-tagline current))
859 (eq (rmail-mime-display-body current)
860 (rmail-mime-display-body new)))
861 (forward-char (- (aref segment 3) (aref segment 2)))
862 (rmail-mime-update-tagline entity))
863 (if (rmail-mime-display-tagline current)
864 (delete-char (- (aref segment 3) (aref segment 2))))
865 (if (rmail-mime-display-tagline new)
866 (rmail-mime-insert-tagline
867 entity
868 " Save:"
869 (list filename
870 :type 'rmail-mime-save
871 'help-echo "mouse-2, RET: Save attachment"
872 'filename filename
873 'directory (file-name-as-directory directory)
874 'data data)
875 (format " (%.0f%s)" size (car units))
876 ;; We don't need this button because the "type" string of a
877 ;; tagline is the button to do this.
878 ;; (if (cdr bulk-data)
879 ;; " ")
880 ;; (if (cdr bulk-data)
881 ;; (list "Toggle show/hide"
882 ;; :type 'rmail-mime-image
883 ;; 'help-echo "mouse-2, RET: Toggle show/hide"
884 ;; 'image-type (cdr bulk-data)
885 ;; 'image-data data))
886 )))
887 ;; body
888 (if (eq (rmail-mime-display-body current)
889 (rmail-mime-display-body new))
890 (forward-char (- (aref segment 4) (aref segment 3)))
891 (if (rmail-mime-display-body current)
892 (delete-char (- (aref segment 4) (aref segment 3))))
893 (if (rmail-mime-display-body new)
894 (cond ((eq (cdr bulk-data) 'text)
895 (rmail-mime-insert-decoded-text entity))
896 ((eq (cdr bulk-data) 'html)
897 ;; Render HTML if display single message, but if searching
898 ;; don't render but just search HTML itself.
899 (if rmail-mime-searching
900 (rmail-mime-insert-decoded-text entity)
901 (rmail-mime-insert-html entity)))
902 ((cdr bulk-data)
903 (rmail-mime-insert-image entity))
904 (t
905 ;; As we don't know how to display the body, just
906 ;; insert it as a text.
907 (rmail-mime-insert-decoded-text entity)))))
908 (put-text-property beg (point) 'rmail-mime-entity entity)))
909
910 (defun rmail-mime-multipart-handler (content-type
911 content-disposition
912 content-transfer-encoding)
913 "Handle the current buffer as a multipart MIME body.
914 The current buffer should be narrowed to the body. CONTENT-TYPE,
915 CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
916 of the respective parsed headers. See `rmail-mime-handle' for their
917 format."
918 (rmail-mime-process-multipart
919 content-type content-disposition content-transfer-encoding nil)
920 t)
921
922 (defun rmail-mime-process-multipart (content-type
923 content-disposition
924 content-transfer-encoding
925 parse-tag)
926 "Process the current buffer as a multipart MIME body.
927
928 If PARSE-TAG is nil, modify the current buffer directly for
929 showing the MIME body and return nil.
930
931 Otherwise, PARSE-TAG is a string indicating the depth and index
932 number of the entity. In this case, parse the current buffer and
933 return a list of MIME-entity objects.
934
935 The other arguments are the same as `rmail-mime-multipart-handler'."
936 ;; Some MUAs start boundaries with "--", while it should start
937 ;; with "CRLF--", as defined by RFC 2046:
938 ;; The boundary delimiter MUST occur at the beginning of a line,
939 ;; i.e., following a CRLF, and the initial CRLF is considered to
940 ;; be attached to the boundary delimiter line rather than part
941 ;; of the preceding part.
942 ;; We currently don't handle that.
943 (let ((boundary (cdr (assq 'boundary content-type)))
944 (subtype (cadr (split-string (car content-type) "/")))
945 (index 0)
946 beg end next entities truncated last)
947 (unless boundary
948 (rmail-mm-get-boundary-error-message
949 "No boundary defined" content-type content-disposition
950 content-transfer-encoding))
951 (setq boundary (concat "\n--" boundary))
952 ;; Hide the body before the first bodypart
953 (goto-char (point-min))
954 (when (and (search-forward boundary nil t)
955 (looking-at "[ \t]*\n"))
956 (if parse-tag
957 (narrow-to-region (match-end 0) (point-max))
958 (delete-region (point-min) (match-end 0))))
959
960 ;; Change content-type to the proper default one for the children.
961 (cond ((string-match "mixed" subtype)
962 (setq content-type '("text/plain")))
963 ((string-match "digest" subtype)
964 (setq content-type '("message/rfc822")))
965 (t
966 (setq content-type nil)))
967
968 ;; Loop over all body parts, where beg points at the beginning of
969 ;; the part and end points at the end of the part. next points at
970 ;; the beginning of the next part. The current point is just
971 ;; after the boundary tag.
972 (setq beg (point-min))
973
974 (while (or (and (search-forward boundary nil t)
975 (setq truncated nil end (match-beginning 0)))
976 ;; If the boundary does not appear at all,
977 ;; the message was truncated.
978 ;; Handle the rest of the truncated message
979 ;; (if it isn't empty) by pretending that the boundary
980 ;; appears at the end of the message.
981 ;; We use `last' to distinguish this from the more
982 ;; likely situation of there being an epilogue
983 ;; after the last boundary, which should be ignored.
984 ;; See rmailmm-test-multipart-handler for an example,
985 ;; and also bug#10101.
986 (and (not last)
987 (save-excursion
988 (skip-chars-forward "\n")
989 (> (point-max) (point)))
990 (setq truncated t end (point-max))))
991 ;; If this is the last boundary according to RFC 2046, hide the
992 ;; epilogue, else hide the boundary only. Use a marker for
993 ;; `next' because `rmail-mime-show' may change the buffer.
994 (cond ((looking-at "--[ \t]*$")
995 (setq next (point-max-marker)
996 last t))
997 ((looking-at "[ \t]*\n")
998 (setq next (copy-marker (match-end 0) t)))
999 (truncated
1000 ;; We're handling what's left of a truncated message.
1001 (setq next (point-max-marker)))
1002 (t
1003 ;; The original code signaled an error as below, but
1004 ;; this line may be a boundary of nested multipart. So,
1005 ;; we just set `next' to nil to skip this line
1006 ;; (rmail-mm-get-boundary-error-message
1007 ;; "Malformed boundary" content-type content-disposition
1008 ;; content-transfer-encoding)
1009 (setq next nil)))
1010
1011 (when next
1012 (setq index (1+ index))
1013 ;; Handle the part.
1014 (if parse-tag
1015 (save-restriction
1016 (narrow-to-region beg end)
1017 (let ((child (rmail-mime-process
1018 nil (format "%s/%d" parse-tag index)
1019 content-type content-disposition)))
1020 ;; Display a tagline.
1021 (aset (aref (rmail-mime-entity-display child) 1) 1
1022 (aset (rmail-mime-entity-tagline child) 2 t))
1023 (rmail-mime-entity-set-truncated child truncated)
1024 (push child entities)))
1025
1026 (delete-region end next)
1027 (save-restriction
1028 (narrow-to-region beg end)
1029 (rmail-mime-show)))
1030 (goto-char (setq beg next))))
1031
1032 (when parse-tag
1033 (setq entities (nreverse entities))
1034 (if (string-match "alternative" subtype)
1035 ;; Find the best entity to show, and hide all the others.
1036 ;; If rmail-mime-prefer-html is set, html is best, then plain.
1037 ;; If not, plain is best, then html.
1038 ;; Then comes any other text part.
1039 ;; If thereto of the same type, earlier entities in the message (later
1040 ;; in the reverse list) are preferred.
1041 (let (best best-priority)
1042 (dolist (child entities)
1043 (if (string= (or (car (rmail-mime-entity-disposition child))
1044 (car content-disposition))
1045 "inline")
1046 (let ((type (car (rmail-mime-entity-type child))))
1047 (if (string-match "text/" type)
1048 ;; Consider all inline text parts
1049 (let ((priority
1050 (cond ((string-match "text/html" type)
1051 (if rmail-mime-prefer-html 1 2))
1052 ((string-match "text/plain" type)
1053 (if rmail-mime-prefer-html 2 1))
1054 (t 3))))
1055 (if (or (null best) (<= priority best-priority))
1056 (setq best child
1057 best-priority priority)))))))
1058 (dolist (child entities)
1059 (unless (eq best child)
1060 (aset (rmail-mime-entity-body child) 2 nil)
1061 (rmail-mime-hidden-mode child)))))
1062 entities)))
1063
1064 (defun rmail-mime-insert-multipart (entity)
1065 "Presentation handler for a multipart MIME entity."
1066 (let ((current (aref (rmail-mime-entity-display entity) 0))
1067 (new (aref (rmail-mime-entity-display entity) 1))
1068 (header (rmail-mime-entity-header entity))
1069 (tagline (rmail-mime-entity-tagline entity))
1070 (body (rmail-mime-entity-body entity))
1071 (beg (point))
1072 (segment (rmail-mime-entity-segment (point) entity)))
1073 ;; header
1074 (if (eq (rmail-mime-display-header current)
1075 (rmail-mime-display-header new))
1076 (goto-char (aref segment 2))
1077 (if (rmail-mime-display-header current)
1078 (delete-char (- (aref segment 2) (aref segment 1))))
1079 (if (rmail-mime-display-header new)
1080 (rmail-mime-insert-header header)))
1081 ;; tagline
1082 (if (eq (rmail-mime-display-tagline current)
1083 (rmail-mime-display-tagline new))
1084 (if (or (not (rmail-mime-display-tagline current))
1085 (eq (rmail-mime-display-body current)
1086 (rmail-mime-display-body new)))
1087 (forward-char (- (aref segment 3) (aref segment 2)))
1088 (rmail-mime-update-tagline entity))
1089 (if (rmail-mime-display-tagline current)
1090 (delete-char (- (aref segment 3) (aref segment 2))))
1091 (if (rmail-mime-display-tagline new)
1092 (rmail-mime-insert-tagline entity)))
1093
1094 (put-text-property beg (point) 'rmail-mime-entity entity)
1095
1096 ;; body
1097 (if (eq (rmail-mime-display-body current)
1098 (rmail-mime-display-body new))
1099 (forward-char (- (aref segment 4) (aref segment 3)))
1100 (dolist (child (rmail-mime-entity-children entity))
1101 (rmail-mime-insert child)))
1102 entity))
1103
1104 ;;; Main code
1105
1106 (defun rmail-mime-handle (content-type
1107 content-disposition
1108 content-transfer-encoding)
1109 "Handle the current buffer as a MIME part.
1110 The current buffer should be narrowed to the respective body, and
1111 point should be at the beginning of the body.
1112
1113 CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
1114 are the values of the respective parsed headers. The latter should
1115 be lower-case. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
1116 have the form
1117
1118 \(VALUE . ALIST)
1119
1120 In other words:
1121
1122 \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
1123
1124 VALUE is a string and ATTRIBUTE is a symbol.
1125
1126 Consider the following header, for example:
1127
1128 Content-Type: multipart/mixed;
1129 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
1130
1131 The parsed header value:
1132
1133 \(\"multipart/mixed\"
1134 \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
1135 ;; Handle the content transfer encodings we know. Unknown transfer
1136 ;; encodings will be passed on to the various handlers.
1137 (cond ((string= content-transfer-encoding "base64")
1138 (when (ignore-errors
1139 (base64-decode-region (point) (point-max)))
1140 (setq content-transfer-encoding nil)))
1141 ((string= content-transfer-encoding "quoted-printable")
1142 (quoted-printable-decode-region (point) (point-max))
1143 (setq content-transfer-encoding nil))
1144 ((string= content-transfer-encoding "8bit")
1145 ;; FIXME: Is this the correct way?
1146 ;; No, of course not, it just means there's no decoding to do.
1147 ;; (set-buffer-multibyte nil)
1148 (setq content-transfer-encoding nil)
1149 ))
1150 ;; Inline stuff requires work. Attachments are handled by the bulk
1151 ;; handler.
1152 (if (string= "inline" (car content-disposition))
1153 (let ((stop nil))
1154 (dolist (entry rmail-mime-media-type-handlers-alist)
1155 (when (and (string-match (car entry) (car content-type)) (not stop))
1156 (progn
1157 (setq stop (funcall (cadr entry) content-type
1158 content-disposition
1159 content-transfer-encoding))))))
1160 ;; Everything else is an attachment.
1161 (rmail-mime-bulk-handler content-type
1162 content-disposition
1163 content-transfer-encoding))
1164 (save-restriction
1165 (widen)
1166 (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
1167 current new)
1168 (when entity
1169 (setq current (aref (rmail-mime-entity-display entity) 0)
1170 new (aref (rmail-mime-entity-display entity) 1))
1171 (dotimes (i 3)
1172 (aset current i (aref new i)))))))
1173
1174 (defun rmail-mime-show (&optional show-headers)
1175 "Handle the current buffer as a MIME message.
1176 If SHOW-HEADERS is non-nil, then the headers of the current part
1177 will shown as usual for a MIME message. The headers are also
1178 shown for the content type message/rfc822. This function will be
1179 called recursively if multiple parts are available.
1180
1181 The current buffer must contain a single message. It will be
1182 modified."
1183 (rmail-mime-process show-headers nil))
1184
1185 (defun rmail-mime-process (show-headers parse-tag &optional
1186 default-content-type
1187 default-content-disposition)
1188 (let ((end (point-min))
1189 content-type
1190 content-transfer-encoding
1191 content-disposition)
1192 ;; `point-min' returns the beginning and `end' points at the end
1193 ;; of the headers.
1194 (goto-char (point-min))
1195 ;; If we're showing a part without headers, then it will start
1196 ;; with a newline.
1197 (if (eq (char-after) ?\n)
1198 (setq end (1+ (point)))
1199 (when (search-forward "\n\n" nil t)
1200 (setq end (match-end 0))
1201 (save-restriction
1202 (narrow-to-region (point-min) end)
1203 ;; FIXME: Default disposition of the multipart entities should
1204 ;; be inherited.
1205 (setq content-type
1206 (mail-fetch-field "Content-Type")
1207 content-transfer-encoding
1208 (mail-fetch-field "Content-Transfer-Encoding")
1209 content-disposition
1210 (mail-fetch-field "Content-Disposition")))))
1211 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1212 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1213 (if content-transfer-encoding
1214 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1215 (setq content-type
1216 (if content-type
1217 (or (mail-header-parse-content-type content-type)
1218 '("text/plain"))
1219 (or default-content-type '("text/plain"))))
1220 (setq content-disposition
1221 (if content-disposition
1222 (mail-header-parse-content-disposition content-disposition)
1223 ;; If none specified, we are free to choose what we deem
1224 ;; suitable according to RFC 2183. We like inline.
1225 (or default-content-disposition '("inline"))))
1226 ;; Unrecognized disposition types are to be treated like
1227 ;; attachment according to RFC 2183.
1228 (unless (member (car content-disposition) '("inline" "attachment"))
1229 (setq content-disposition '("attachment")))
1230
1231 (if parse-tag
1232 (let* ((is-inline (string= (car content-disposition) "inline"))
1233 (hdr-end (copy-marker end))
1234 (header (vector (point-min-marker) hdr-end nil))
1235 (tagline (vector parse-tag (cons nil nil) t))
1236 (body (vector hdr-end (point-max-marker) is-inline))
1237 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1238 children handler entity)
1239 (cond ((string-match "multipart/.*" (car content-type))
1240 (save-restriction
1241 (narrow-to-region (1- end) (point-max))
1242 (if (zerop (length parse-tag)) ; top level of message
1243 (aset new 1 (aset tagline 2 nil))) ; don't show tagline
1244 (setq children (rmail-mime-process-multipart
1245 content-type
1246 content-disposition
1247 content-transfer-encoding
1248 parse-tag)
1249 handler 'rmail-mime-insert-multipart)))
1250 ((string-match "message/rfc822" (car content-type))
1251 (save-restriction
1252 (narrow-to-region end (point-max))
1253 (let* ((msg (rmail-mime-process t parse-tag
1254 '("text/plain") '("inline")))
1255 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1256 ;; Show header of the child.
1257 (aset msg-new 0 t)
1258 (aset (rmail-mime-entity-header msg) 2 t)
1259 ;; Hide tagline of the child.
1260 (aset msg-new 1 nil)
1261 (aset (rmail-mime-entity-tagline msg) 2 nil)
1262 (setq children (list msg)
1263 handler 'rmail-mime-insert-multipart))))
1264 ((and is-inline (string-match "text/html" (car content-type)))
1265 ;; Display tagline, so part can be detached
1266 (aset new 1 (aset tagline 2 t))
1267 (aset new 2 (aset body 2 t)) ; display body also.
1268 (setq handler 'rmail-mime-insert-bulk))
1269 ;; Inline non-HTML text
1270 ((and is-inline (string-match "text/" (car content-type)))
1271 ;; Don't need a tagline.
1272 (aset new 1 (aset tagline 2 nil))
1273 (setq handler 'rmail-mime-insert-text))
1274 (t
1275 ;; Force hidden mode.
1276 (aset new 1 (aset tagline 2 t))
1277 (aset new 2 (aset body 2 nil))
1278 (setq handler 'rmail-mime-insert-bulk)))
1279 (setq entity (rmail-mime-entity content-type
1280 content-disposition
1281 content-transfer-encoding
1282 (vector (vector nil nil nil) new)
1283 header tagline body children handler))
1284 (if (and (eq handler 'rmail-mime-insert-bulk)
1285 (rmail-mime-set-bulk-data entity))
1286 ;; Show the body.
1287 (aset new 2 (aset body 2 t)))
1288 entity)
1289
1290 ;; Hide headers and handle the part.
1291 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1292 (rmail-mime-entity
1293 content-type content-disposition
1294 content-transfer-encoding
1295 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1296 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1297 (vector nil nil 'raw) nil nil))
1298 (save-restriction
1299 (cond ((string= (car content-type) "message/rfc822")
1300 (narrow-to-region end (point-max)))
1301 ((not show-headers)
1302 (delete-region (point-min) end)))
1303 (rmail-mime-handle content-type content-disposition
1304 content-transfer-encoding)))))
1305
1306 (defun rmail-mime-parse ()
1307 "Parse the current Rmail message as a MIME message.
1308 The value is a MIME-entity object (see `rmail-mime-entity').
1309 If an error occurs, return an error message string."
1310 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1311 rmail-view-buffer
1312 (current-buffer))))
1313 (condition-case err
1314 (with-current-buffer rmail-mime-mbox-buffer
1315 (save-excursion
1316 (goto-char (point-min))
1317 (let* ((entity (rmail-mime-process t ""
1318 '("text/plain") '("inline")))
1319 (new (aref (rmail-mime-entity-display entity) 1)))
1320 ;; Show header.
1321 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1322 entity)))
1323 (error (format "%s" err)))))
1324
1325 (defun rmail-mime-insert (entity)
1326 "Insert a MIME-entity ENTITY in the current buffer.
1327
1328 This function will be called recursively if multiple parts are
1329 available."
1330 (let ((current (aref (rmail-mime-entity-display entity) 0))
1331 (new (aref (rmail-mime-entity-display entity) 1)))
1332 (if (not (eq (rmail-mime-display-header new) 'raw))
1333 ;; Not a raw-mode. Each handler should handle it.
1334 (funcall (rmail-mime-entity-handler entity) entity)
1335 (let ((header (rmail-mime-entity-header entity))
1336 (tagline (rmail-mime-entity-tagline entity))
1337 (body (rmail-mime-entity-body entity))
1338 (beg (point))
1339 (segment (rmail-mime-entity-segment (point) entity)))
1340 ;; header
1341 (if (eq (rmail-mime-display-header current)
1342 (rmail-mime-display-header new))
1343 (goto-char (aref segment 2))
1344 (if (rmail-mime-display-header current)
1345 (delete-char (- (aref segment 2) (aref segment 1))))
1346 (insert-buffer-substring rmail-mime-mbox-buffer
1347 (aref header 0) (aref header 1)))
1348 ;; tagline
1349 (if (rmail-mime-display-tagline current)
1350 (delete-char (- (aref segment 3) (aref segment 2))))
1351 ;; body
1352 (let ((children (rmail-mime-entity-children entity)))
1353 (if children
1354 (progn
1355 (put-text-property beg (point) 'rmail-mime-entity entity)
1356 (dolist (child children)
1357 (rmail-mime-insert child)))
1358 (if (eq (rmail-mime-display-body current)
1359 (rmail-mime-display-body new))
1360 (forward-char (- (aref segment 4) (aref segment 3)))
1361 (if (rmail-mime-display-body current)
1362 (delete-char (- (aref segment 4) (aref segment 3))))
1363 (insert-buffer-substring rmail-mime-mbox-buffer
1364 (aref body 0) (aref body 1))
1365 (or (bolp) (insert "\n")))
1366 (put-text-property beg (point) 'rmail-mime-entity entity)))))
1367 (dotimes (i 3)
1368 (aset current i (aref new i)))))
1369
1370 (define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1371 "Major mode used in `rmail-mime' buffers."
1372 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1373
1374 ;;;###autoload
1375 (defun rmail-mime (&optional arg state)
1376 "Toggle the display of a MIME message.
1377
1378 The actual behavior depends on the value of `rmail-enable-mime'.
1379
1380 If `rmail-enable-mime' is non-nil (the default), this command toggles
1381 the display of a MIME message between decoded presentation form and
1382 raw data. With optional prefix argument ARG, it toggles the display only
1383 of the MIME entity at point, if there is one. The optional argument
1384 STATE forces a particular display state, rather than toggling.
1385 `raw' forces raw mode, any other non-nil value forces decoded mode.
1386
1387 If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\"
1388 buffer holding a decoded copy of the message. Inline content-types are
1389 handled according to `rmail-mime-media-type-handlers-alist'.
1390 By default, this displays text and multipart messages, and offers to
1391 download attachments as specified by `rmail-mime-attachment-dirs-alist'.
1392 The arguments ARG and STATE have no effect in this case."
1393 (interactive (list current-prefix-arg nil))
1394 (if rmail-enable-mime
1395 (with-current-buffer rmail-buffer
1396 (if (or (rmail-mime-message-p)
1397 (get-text-property (point-min) 'rmail-mime-hidden))
1398 (let* ((hidden (get-text-property (point-min) 'rmail-mime-hidden))
1399 (desired-hidden (if state (eq state 'raw) (not hidden))))
1400 (unless (eq hidden desired-hidden)
1401 (if (not desired-hidden)
1402 (rmail-show-message rmail-current-message)
1403 (let ((rmail-enable-mime nil)
1404 (inhibit-read-only t))
1405 (rmail-show-message rmail-current-message)
1406 (add-text-properties (point-min) (point-max) '(rmail-mime-hidden t))))))
1407 (message "Not a MIME message, just toggling headers")
1408 (rmail-toggle-header)))
1409 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1410 (buf (get-buffer-create "*RMAIL*"))
1411 (rmail-mime-mbox-buffer rmail-view-buffer)
1412 (rmail-mime-view-buffer buf))
1413 (set-buffer buf)
1414 (setq buffer-undo-list t)
1415 (let ((inhibit-read-only t))
1416 ;; Decoding the message in fundamental mode for speed, only
1417 ;; switching to rmail-mime-mode at the end for display. Eg
1418 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1419 (fundamental-mode)
1420 (erase-buffer)
1421 (insert data)
1422 (rmail-mime-show t)
1423 (rmail-mime-mode)
1424 (set-buffer-modified-p nil))
1425 (view-buffer buf))))
1426
1427 (defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1428 "Return MESSAGE with more information on the main MIME components."
1429 (error "%s; type: %s; disposition: %s; encoding: %s"
1430 message type disposition encoding))
1431
1432 (defun rmail-show-mime ()
1433 "Function to use for the value of `rmail-show-mime-function'."
1434 (let ((entity (rmail-mime-parse))
1435 (rmail-mime-mbox-buffer rmail-buffer)
1436 (rmail-mime-view-buffer rmail-view-buffer)
1437 (rmail-mime-coding-system nil))
1438 ;; If ENTITY is not a vector, it is a string describing an error.
1439 (if (vectorp entity)
1440 (with-current-buffer rmail-mime-view-buffer
1441 (erase-buffer)
1442 ;; This condition-case is for catching an error in the
1443 ;; internal MIME decoding (e.g. incorrect BASE64 form) that
1444 ;; may be signaled by rmail-mime-insert.
1445 ;; FIXME: The current code doesn't set a proper error symbol
1446 ;; in ERR. We must find a way to propagate a correct error
1447 ;; symbol that is caused in the very deep code of text
1448 ;; decoding (e.g. an error by base64-decode-region called by
1449 ;; post-read-conversion function of utf-7).
1450 (condition-case err
1451 (progn
1452 (rmail-mime-insert entity)
1453 (if (consp rmail-mime-coding-system)
1454 ;; Decoding is done by rfc2047-decode-region only for a
1455 ;; header. But, as the used coding system may have been
1456 ;; overridden by mm-charset-override-alist, we can't
1457 ;; trust (car rmail-mime-coding-system). So, here we
1458 ;; try the decoding again with mm-charset-override-alist
1459 ;; bound to nil.
1460 (let ((mm-charset-override-alist nil))
1461 (setq rmail-mime-coding-system
1462 (rmail-mime-find-header-encoding
1463 (rmail-mime-entity-header entity)))))
1464 (set-buffer-file-coding-system
1465 (if rmail-mime-coding-system
1466 (coding-system-base rmail-mime-coding-system)
1467 'undecided)
1468 t t))
1469 (error (setq entity (format "%s" err))))))
1470 ;; Re-check ENTITY. It may be set to an error string.
1471 (when (stringp entity)
1472 ;; Decoding failed. ENTITY is an error message. Insert the
1473 ;; original message body as is, and show warning.
1474 (let ((region (with-current-buffer rmail-mime-mbox-buffer
1475 (goto-char (point-min))
1476 (re-search-forward "^$" nil t)
1477 (forward-line 1)
1478 (vector (point-min) (point) (point-max)))))
1479 (with-current-buffer rmail-mime-view-buffer
1480 (let ((inhibit-read-only t))
1481 (erase-buffer)
1482 (rmail-mime-insert-header region)
1483 (insert-buffer-substring rmail-mime-mbox-buffer
1484 (aref region 1) (aref region 2))))
1485 (set-buffer-file-coding-system 'no-conversion t t)
1486 (message "MIME decoding failed: %s" entity)))))
1487
1488 (setq rmail-show-mime-function 'rmail-show-mime)
1489
1490 (defun rmail-insert-mime-forwarded-message (forward-buffer)
1491 "Insert the message in FORWARD-BUFFER as a forwarded message.
1492 This is the usual value of `rmail-insert-mime-forwarded-message-function'."
1493 (let (contents-buffer start end)
1494 (with-current-buffer forward-buffer
1495 (setq contents-buffer
1496 (if rmail-buffer-swapped
1497 rmail-view-buffer
1498 forward-buffer)
1499 start (rmail-msgbeg rmail-current-message)
1500 end (rmail-msgend rmail-current-message)))
1501 (message-forward-make-body-mime contents-buffer start end)))
1502
1503 (setq rmail-insert-mime-forwarded-message-function
1504 'rmail-insert-mime-forwarded-message)
1505
1506 (defun rmail-insert-mime-resent-message (forward-buffer)
1507 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
1508 (insert-buffer-substring
1509 (with-current-buffer forward-buffer rmail-view-buffer))
1510 (goto-char (point-min))
1511 (when (looking-at "From ")
1512 (forward-line 1)
1513 (delete-region (point-min) (point))))
1514
1515 (setq rmail-insert-mime-resent-message-function
1516 'rmail-insert-mime-resent-message)
1517
1518 (defun rmail-search-mime-message (msg regexp)
1519 "Function to set in `rmail-search-mime-message-function' (which see)."
1520 (save-restriction
1521 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
1522 (let* ((rmail-mime-searching t) ; mark inside search
1523 (rmail-mime-mbox-buffer (current-buffer))
1524 (rmail-mime-view-buffer rmail-view-buffer)
1525 (header-end (save-excursion
1526 (re-search-forward "^$" nil 'move) (point)))
1527 (body-end (point-max))
1528 (entity (rmail-mime-parse)))
1529 (or
1530 ;; At first, just search the headers.
1531 (with-temp-buffer
1532 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
1533 (rfc2047-decode-region (point-min) (point))
1534 (goto-char (point-min))
1535 (re-search-forward regexp nil t))
1536 ;; Next, search the body.
1537 (if (and entity
1538 ;; RMS: I am not sure why, but sometimes this is a string.
1539 (not (stringp entity))
1540 (let* ((content-type (rmail-mime-entity-type entity))
1541 (charset (cdr (assq 'charset (cdr content-type)))))
1542 (or (not (string-match "text/.*" (car content-type)))
1543 (and charset
1544 (not (string= (downcase charset) "us-ascii"))))))
1545 ;; Search the decoded MIME message.
1546 (with-temp-buffer
1547 (rmail-mime-insert entity)
1548 (goto-char (point-min))
1549 (re-search-forward regexp nil t))
1550 ;; Search the body without decoding.
1551 (goto-char header-end)
1552 (re-search-forward regexp nil t))))))
1553
1554 (setq rmail-search-mime-message-function 'rmail-search-mime-message)
1555
1556 (provide 'rmailmm)
1557
1558 ;; Local Variables:
1559 ;; generated-autoload-file: "rmail.el"
1560 ;; End:
1561
1562 ;;; rmailmm.el ends here