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