* admin/gitmerge.el (gitmerge-skip-regexp): Be stricter in batch mode.
[emacs.git] / lisp / mail / rmailmm.el
blob60b2066b2c25ad15ac9b3111bfb10da15302df23
1 ;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
3 ;; Copyright (C) 2006-2017 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 <https://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 (or (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 "~"))
827 (filename (or (cdr (assq 'name (cdr content-type)))
828 (cdr (assq 'filename (cdr content-disposition)))
829 "noname"))
830 (units '(B kB MB GB))
831 (segment (rmail-mime-entity-segment (point) entity))
832 beg data size)
834 (if (or (integerp (aref body 0)) (markerp (aref body 0)))
835 (setq data entity
836 size (car bulk-data))
837 (if (stringp (aref body 0))
838 (setq data (aref body 0))
839 (setq data (string-as-unibyte (buffer-string)))
840 (aset body 0 data)
841 (rmail-mime-set-bulk-data entity)
842 (delete-region (point-min) (point-max)))
843 (setq size (length data)))
844 (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
845 (cdr units))
846 (setq size (/ size 1024.0)
847 units (cdr units)))
849 (setq beg (point))
851 ;; header
852 (if (eq (rmail-mime-display-header current)
853 (rmail-mime-display-header new))
854 (goto-char (aref segment 2))
855 (if (rmail-mime-display-header current)
856 (delete-char (- (aref segment 2) (aref segment 1))))
857 (if (rmail-mime-display-header new)
858 (rmail-mime-insert-header header)))
860 ;; tagline
861 (if (eq (rmail-mime-display-tagline current)
862 (rmail-mime-display-tagline new))
863 (if (or (not (rmail-mime-display-tagline current))
864 (eq (rmail-mime-display-body current)
865 (rmail-mime-display-body new)))
866 (forward-char (- (aref segment 3) (aref segment 2)))
867 (rmail-mime-update-tagline entity))
868 (if (rmail-mime-display-tagline current)
869 (delete-char (- (aref segment 3) (aref segment 2))))
870 (if (rmail-mime-display-tagline new)
871 (rmail-mime-insert-tagline
872 entity
873 " Save:"
874 (list filename
875 :type 'rmail-mime-save
876 'help-echo "mouse-2, RET: Save attachment"
877 'filename filename
878 'directory (file-name-as-directory directory)
879 'data data)
880 (format " (%.0f%s)" size (car units))
881 ;; We don't need this button because the "type" string of a
882 ;; tagline is the button to do this.
883 ;; (if (cdr bulk-data)
884 ;; " ")
885 ;; (if (cdr bulk-data)
886 ;; (list "Toggle show/hide"
887 ;; :type 'rmail-mime-image
888 ;; 'help-echo "mouse-2, RET: Toggle show/hide"
889 ;; 'image-type (cdr bulk-data)
890 ;; 'image-data data))
892 ;; body
893 (if (eq (rmail-mime-display-body current)
894 (rmail-mime-display-body new))
895 (forward-char (- (aref segment 4) (aref segment 3)))
896 (if (rmail-mime-display-body current)
897 (delete-char (- (aref segment 4) (aref segment 3))))
898 (if (rmail-mime-display-body new)
899 (cond ((eq (cdr bulk-data) 'text)
900 (rmail-mime-insert-decoded-text entity))
901 ((eq (cdr bulk-data) 'html)
902 ;; Render HTML if display single message, but if searching
903 ;; don't render but just search HTML itself.
904 (if rmail-mime-searching
905 (rmail-mime-insert-decoded-text entity)
906 (rmail-mime-insert-html entity)))
907 ((cdr bulk-data)
908 (rmail-mime-insert-image entity))
910 ;; As we don't know how to display the body, just
911 ;; insert it as a text.
912 (rmail-mime-insert-decoded-text entity)))))
913 (put-text-property beg (point) 'rmail-mime-entity entity)))
915 (defun rmail-mime-multipart-handler (content-type
916 content-disposition
917 content-transfer-encoding)
918 "Handle the current buffer as a multipart MIME body.
919 The current buffer should be narrowed to the body. CONTENT-TYPE,
920 CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
921 of the respective parsed headers. See `rmail-mime-handle' for their
922 format."
923 (rmail-mime-process-multipart
924 content-type content-disposition content-transfer-encoding nil)
927 (defun rmail-mime-process-multipart (content-type
928 content-disposition
929 content-transfer-encoding
930 parse-tag)
931 "Process the current buffer as a multipart MIME body.
933 If PARSE-TAG is nil, modify the current buffer directly for
934 showing the MIME body and return nil.
936 Otherwise, PARSE-TAG is a string indicating the depth and index
937 number of the entity. In this case, parse the current buffer and
938 return a list of MIME-entity objects.
940 The other arguments are the same as `rmail-mime-multipart-handler'."
941 ;; Some MUAs start boundaries with "--", while it should start
942 ;; with "CRLF--", as defined by RFC 2046:
943 ;; The boundary delimiter MUST occur at the beginning of a line,
944 ;; i.e., following a CRLF, and the initial CRLF is considered to
945 ;; be attached to the boundary delimiter line rather than part
946 ;; of the preceding part.
947 ;; We currently don't handle that.
948 (let ((boundary (cdr (assq 'boundary content-type)))
949 (subtype (cadr (split-string (car content-type) "/")))
950 (index 0)
951 beg end next entities truncated last)
952 (unless boundary
953 (rmail-mm-get-boundary-error-message
954 "No boundary defined" content-type content-disposition
955 content-transfer-encoding))
956 (setq boundary (concat "\n--" boundary))
957 ;; Hide the body before the first bodypart
958 (goto-char (point-min))
959 (when (and (search-forward boundary nil t)
960 (looking-at "[ \t]*\n"))
961 (if parse-tag
962 (narrow-to-region (match-end 0) (point-max))
963 (delete-region (point-min) (match-end 0))))
965 ;; Change content-type to the proper default one for the children.
966 (cond ((string-match "mixed" subtype)
967 (setq content-type '("text/plain")))
968 ((string-match "digest" subtype)
969 (setq content-type '("message/rfc822")))
971 (setq content-type nil)))
973 ;; Loop over all body parts, where beg points at the beginning of
974 ;; the part and end points at the end of the part. next points at
975 ;; the beginning of the next part. The current point is just
976 ;; after the boundary tag.
977 (setq beg (point-min))
979 (while (or (and (search-forward boundary nil t)
980 (setq truncated nil end (match-beginning 0)))
981 ;; If the boundary does not appear at all,
982 ;; the message was truncated.
983 ;; Handle the rest of the truncated message
984 ;; (if it isn't empty) by pretending that the boundary
985 ;; appears at the end of the message.
986 ;; We use `last' to distinguish this from the more
987 ;; likely situation of there being an epilogue
988 ;; after the last boundary, which should be ignored.
989 ;; See rmailmm-test-multipart-handler for an example,
990 ;; and also bug#10101.
991 (and (not last)
992 (save-excursion
993 (skip-chars-forward "\n")
994 (> (point-max) (point)))
995 (setq truncated t end (point-max))))
996 ;; If this is the last boundary according to RFC 2046, hide the
997 ;; epilogue, else hide the boundary only. Use a marker for
998 ;; `next' because `rmail-mime-show' may change the buffer.
999 (cond ((looking-at "--[ \t]*$")
1000 (setq next (point-max-marker)
1001 last t))
1002 ((looking-at "[ \t]*\n")
1003 (setq next (copy-marker (match-end 0) t)))
1004 (truncated
1005 ;; We're handling what's left of a truncated message.
1006 (setq next (point-max-marker)))
1008 ;; The original code signaled an error as below, but
1009 ;; this line may be a boundary of nested multipart. So,
1010 ;; we just set `next' to nil to skip this line
1011 ;; (rmail-mm-get-boundary-error-message
1012 ;; "Malformed boundary" content-type content-disposition
1013 ;; content-transfer-encoding)
1014 (setq next nil)))
1016 (when next
1017 (setq index (1+ index))
1018 ;; Handle the part.
1019 (if parse-tag
1020 (save-restriction
1021 (narrow-to-region beg end)
1022 (let ((child (rmail-mime-process
1023 nil (format "%s/%d" parse-tag index)
1024 content-type content-disposition)))
1025 ;; Display a tagline.
1026 (aset (aref (rmail-mime-entity-display child) 1) 1
1027 (aset (rmail-mime-entity-tagline child) 2 t))
1028 (rmail-mime-entity-set-truncated child truncated)
1029 (push child entities)))
1031 (delete-region end next)
1032 (save-restriction
1033 (narrow-to-region beg end)
1034 (rmail-mime-show)))
1035 (goto-char (setq beg next))))
1037 (when parse-tag
1038 (setq entities (nreverse entities))
1039 (if (string-match "alternative" subtype)
1040 ;; Find the best entity to show, and hide all the others.
1041 ;; If rmail-mime-prefer-html is set, html is best, then plain.
1042 ;; If not, plain is best, then html.
1043 ;; Then comes any other text part.
1044 ;; If thereto of the same type, earlier entities in the message (later
1045 ;; in the reverse list) are preferred.
1046 (let (best best-priority)
1047 (dolist (child entities)
1048 (if (string= (or (car (rmail-mime-entity-disposition child))
1049 (car content-disposition))
1050 "inline")
1051 (let ((type (car (rmail-mime-entity-type child))))
1052 (if (string-match "text/" type)
1053 ;; Consider all inline text parts
1054 (let ((priority
1055 (cond ((string-match "text/html" type)
1056 (if rmail-mime-prefer-html 1 2))
1057 ((string-match "text/plain" type)
1058 (if rmail-mime-prefer-html 2 1))
1059 (t 3))))
1060 (if (or (null best) (<= priority best-priority))
1061 (setq best child
1062 best-priority priority)))))))
1063 (dolist (child entities)
1064 (unless (eq best child)
1065 (aset (rmail-mime-entity-body child) 2 nil)
1066 (rmail-mime-hidden-mode child)))))
1067 entities)))
1069 (defun rmail-mime-insert-multipart (entity)
1070 "Presentation handler for a multipart MIME entity."
1071 (let ((current (aref (rmail-mime-entity-display entity) 0))
1072 (new (aref (rmail-mime-entity-display entity) 1))
1073 (header (rmail-mime-entity-header entity))
1074 (tagline (rmail-mime-entity-tagline entity))
1075 (body (rmail-mime-entity-body entity))
1076 (beg (point))
1077 (segment (rmail-mime-entity-segment (point) entity)))
1078 ;; header
1079 (if (eq (rmail-mime-display-header current)
1080 (rmail-mime-display-header new))
1081 (goto-char (aref segment 2))
1082 (if (rmail-mime-display-header current)
1083 (delete-char (- (aref segment 2) (aref segment 1))))
1084 (if (rmail-mime-display-header new)
1085 (rmail-mime-insert-header header)))
1086 ;; tagline
1087 (if (eq (rmail-mime-display-tagline current)
1088 (rmail-mime-display-tagline new))
1089 (if (or (not (rmail-mime-display-tagline current))
1090 (eq (rmail-mime-display-body current)
1091 (rmail-mime-display-body new)))
1092 (forward-char (- (aref segment 3) (aref segment 2)))
1093 (rmail-mime-update-tagline entity))
1094 (if (rmail-mime-display-tagline current)
1095 (delete-char (- (aref segment 3) (aref segment 2))))
1096 (if (rmail-mime-display-tagline new)
1097 (rmail-mime-insert-tagline entity)))
1099 (put-text-property beg (point) 'rmail-mime-entity entity)
1101 ;; body
1102 (if (eq (rmail-mime-display-body current)
1103 (rmail-mime-display-body new))
1104 (forward-char (- (aref segment 4) (aref segment 3)))
1105 (dolist (child (rmail-mime-entity-children entity))
1106 (rmail-mime-insert child)))
1107 entity))
1109 ;;; Main code
1111 (defun rmail-mime-handle (content-type
1112 content-disposition
1113 content-transfer-encoding)
1114 "Handle the current buffer as a MIME part.
1115 The current buffer should be narrowed to the respective body, and
1116 point should be at the beginning of the body.
1118 CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
1119 are the values of the respective parsed headers. The latter should
1120 be lower-case. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
1121 have the form
1123 (VALUE . ALIST)
1125 In other words:
1127 (VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
1129 VALUE is a string and ATTRIBUTE is a symbol.
1131 Consider the following header, for example:
1133 Content-Type: multipart/mixed;
1134 boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
1136 The parsed header value:
1138 \(\"multipart/mixed\"
1139 (\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
1140 ;; Handle the content transfer encodings we know. Unknown transfer
1141 ;; encodings will be passed on to the various handlers.
1142 (cond ((string= content-transfer-encoding "base64")
1143 (when (ignore-errors
1144 (base64-decode-region (point) (point-max)))
1145 (setq content-transfer-encoding nil)))
1146 ((string= content-transfer-encoding "quoted-printable")
1147 (quoted-printable-decode-region (point) (point-max))
1148 (setq content-transfer-encoding nil))
1149 ((string= content-transfer-encoding "8bit")
1150 ;; FIXME: Is this the correct way?
1151 ;; No, of course not, it just means there's no decoding to do.
1152 ;; (set-buffer-multibyte nil)
1153 (setq content-transfer-encoding nil)
1155 ;; Inline stuff requires work. Attachments are handled by the bulk
1156 ;; handler.
1157 (if (string= "inline" (car content-disposition))
1158 (let ((stop nil))
1159 (dolist (entry rmail-mime-media-type-handlers-alist)
1160 (when (and (string-match (car entry) (car content-type)) (not stop))
1161 (progn
1162 (setq stop (funcall (cadr entry) content-type
1163 content-disposition
1164 content-transfer-encoding))))))
1165 ;; Everything else is an attachment.
1166 (rmail-mime-bulk-handler content-type
1167 content-disposition
1168 content-transfer-encoding))
1169 (save-restriction
1170 (widen)
1171 (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
1172 current new)
1173 (when entity
1174 (setq current (aref (rmail-mime-entity-display entity) 0)
1175 new (aref (rmail-mime-entity-display entity) 1))
1176 (dotimes (i 3)
1177 (aset current i (aref new i)))))))
1179 (defun rmail-mime-show (&optional show-headers)
1180 "Handle the current buffer as a MIME message.
1181 If SHOW-HEADERS is non-nil, then the headers of the current part
1182 will shown as usual for a MIME message. The headers are also
1183 shown for the content type message/rfc822. This function will be
1184 called recursively if multiple parts are available.
1186 The current buffer must contain a single message. It will be
1187 modified."
1188 (rmail-mime-process show-headers nil))
1190 (defun rmail-mime-process (show-headers parse-tag &optional
1191 default-content-type
1192 default-content-disposition)
1193 (let ((end (point-min))
1194 content-type
1195 content-transfer-encoding
1196 content-disposition)
1197 ;; `point-min' returns the beginning and `end' points at the end
1198 ;; of the headers.
1199 (goto-char (point-min))
1200 ;; If we're showing a part without headers, then it will start
1201 ;; with a newline.
1202 (if (eq (char-after) ?\n)
1203 (setq end (1+ (point)))
1204 (when (search-forward "\n\n" nil t)
1205 (setq end (match-end 0))
1206 (save-restriction
1207 (narrow-to-region (point-min) end)
1208 ;; FIXME: Default disposition of the multipart entities should
1209 ;; be inherited.
1210 (setq content-type
1211 (mail-fetch-field "Content-Type")
1212 content-transfer-encoding
1213 (mail-fetch-field "Content-Transfer-Encoding")
1214 content-disposition
1215 (mail-fetch-field "Content-Disposition")))))
1216 ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
1217 ;; are not completely so. Hopefully mail-header-parse-* DTRT.
1218 (if content-transfer-encoding
1219 (setq content-transfer-encoding (downcase content-transfer-encoding)))
1220 (setq content-type
1221 (if content-type
1222 (or (mail-header-parse-content-type content-type)
1223 '("text/plain"))
1224 (or default-content-type '("text/plain"))))
1225 (setq content-disposition
1226 (if content-disposition
1227 (mail-header-parse-content-disposition content-disposition)
1228 ;; If none specified, we are free to choose what we deem
1229 ;; suitable according to RFC 2183. We like inline.
1230 (or default-content-disposition '("inline"))))
1231 ;; Unrecognized disposition types are to be treated like
1232 ;; attachment according to RFC 2183.
1233 (unless (member (car content-disposition) '("inline" "attachment"))
1234 (setq content-disposition '("attachment")))
1236 (if parse-tag
1237 (let* ((is-inline (string= (car content-disposition) "inline"))
1238 (hdr-end (copy-marker end))
1239 (header (vector (point-min-marker) hdr-end nil))
1240 (tagline (vector parse-tag (cons nil nil) t))
1241 (body (vector hdr-end (point-max-marker) is-inline))
1242 (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
1243 children handler entity)
1244 (cond ((string-match "multipart/.*" (car content-type))
1245 (save-restriction
1246 (narrow-to-region (1- end) (point-max))
1247 (if (zerop (length parse-tag)) ; top level of message
1248 (aset new 1 (aset tagline 2 nil))) ; don't show tagline
1249 (setq children (rmail-mime-process-multipart
1250 content-type
1251 content-disposition
1252 content-transfer-encoding
1253 parse-tag)
1254 handler 'rmail-mime-insert-multipart)))
1255 ((string-match "message/rfc822" (car content-type))
1256 (save-restriction
1257 (narrow-to-region end (point-max))
1258 (let* ((msg (rmail-mime-process t parse-tag
1259 '("text/plain") '("inline")))
1260 (msg-new (aref (rmail-mime-entity-display msg) 1)))
1261 ;; Show header of the child.
1262 (aset msg-new 0 t)
1263 (aset (rmail-mime-entity-header msg) 2 t)
1264 ;; Hide tagline of the child.
1265 (aset msg-new 1 nil)
1266 (aset (rmail-mime-entity-tagline msg) 2 nil)
1267 (setq children (list msg)
1268 handler 'rmail-mime-insert-multipart))))
1269 ((and is-inline (string-match "text/html" (car content-type)))
1270 ;; Display tagline, so part can be detached
1271 (aset new 1 (aset tagline 2 t))
1272 (aset new 2 (aset body 2 t)) ; display body also.
1273 (setq handler 'rmail-mime-insert-bulk))
1274 ;; Inline non-HTML text
1275 ((and is-inline (string-match "text/" (car content-type)))
1276 ;; Don't need a tagline.
1277 (aset new 1 (aset tagline 2 nil))
1278 (setq handler 'rmail-mime-insert-text))
1280 ;; Force hidden mode.
1281 (aset new 1 (aset tagline 2 t))
1282 (aset new 2 (aset body 2 nil))
1283 (setq handler 'rmail-mime-insert-bulk)))
1284 (setq entity (rmail-mime-entity content-type
1285 content-disposition
1286 content-transfer-encoding
1287 (vector (vector nil nil nil) new)
1288 header tagline body children handler))
1289 (if (and (eq handler 'rmail-mime-insert-bulk)
1290 (rmail-mime-set-bulk-data entity))
1291 ;; Show the body.
1292 (aset new 2 (aset body 2 t)))
1293 entity)
1295 ;; Hide headers and handle the part.
1296 (put-text-property (point-min) (point-max) 'rmail-mime-entity
1297 (rmail-mime-entity
1298 content-type content-disposition
1299 content-transfer-encoding
1300 (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
1301 (vector nil nil 'raw) (vector "" (cons nil nil) nil)
1302 (vector nil nil 'raw) nil nil))
1303 (save-restriction
1304 (cond ((string= (car content-type) "message/rfc822")
1305 (narrow-to-region end (point-max)))
1306 ((not show-headers)
1307 (delete-region (point-min) end)))
1308 (rmail-mime-handle content-type content-disposition
1309 content-transfer-encoding)))))
1311 (defun rmail-mime-parse ()
1312 "Parse the current Rmail message as a MIME message.
1313 The value is a MIME-entity object (see `rmail-mime-entity').
1314 If an error occurs, return an error message string."
1315 (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
1316 rmail-view-buffer
1317 (current-buffer))))
1318 (condition-case err
1319 (with-current-buffer rmail-mime-mbox-buffer
1320 (save-excursion
1321 (goto-char (point-min))
1322 (let* ((entity (rmail-mime-process t ""
1323 '("text/plain") '("inline")))
1324 (new (aref (rmail-mime-entity-display entity) 1)))
1325 ;; Show header.
1326 (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
1327 entity)))
1328 (error (format "%s" err)))))
1330 (defun rmail-mime-insert (entity)
1331 "Insert a MIME-entity ENTITY in the current buffer.
1333 This function will be called recursively if multiple parts are
1334 available."
1335 (let ((current (aref (rmail-mime-entity-display entity) 0))
1336 (new (aref (rmail-mime-entity-display entity) 1)))
1337 (if (not (eq (rmail-mime-display-header new) 'raw))
1338 ;; Not a raw-mode. Each handler should handle it.
1339 (funcall (rmail-mime-entity-handler entity) entity)
1340 (let ((header (rmail-mime-entity-header entity))
1341 (tagline (rmail-mime-entity-tagline entity))
1342 (body (rmail-mime-entity-body entity))
1343 (beg (point))
1344 (segment (rmail-mime-entity-segment (point) entity)))
1345 ;; header
1346 (if (eq (rmail-mime-display-header current)
1347 (rmail-mime-display-header new))
1348 (goto-char (aref segment 2))
1349 (if (rmail-mime-display-header current)
1350 (delete-char (- (aref segment 2) (aref segment 1))))
1351 (insert-buffer-substring rmail-mime-mbox-buffer
1352 (aref header 0) (aref header 1)))
1353 ;; tagline
1354 (if (rmail-mime-display-tagline current)
1355 (delete-char (- (aref segment 3) (aref segment 2))))
1356 ;; body
1357 (let ((children (rmail-mime-entity-children entity)))
1358 (if children
1359 (progn
1360 (put-text-property beg (point) 'rmail-mime-entity entity)
1361 (dolist (child children)
1362 (rmail-mime-insert child)))
1363 (if (eq (rmail-mime-display-body current)
1364 (rmail-mime-display-body new))
1365 (forward-char (- (aref segment 4) (aref segment 3)))
1366 (if (rmail-mime-display-body current)
1367 (delete-char (- (aref segment 4) (aref segment 3))))
1368 (insert-buffer-substring rmail-mime-mbox-buffer
1369 (aref body 0) (aref body 1))
1370 (or (bolp) (insert "\n")))
1371 (put-text-property beg (point) 'rmail-mime-entity entity)))))
1372 (dotimes (i 3)
1373 (aset current i (aref new i)))))
1375 (define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
1376 "Major mode used in `rmail-mime' buffers."
1377 (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
1379 ;;;###autoload
1380 (defun rmail-mime (&optional arg state)
1381 "Toggle the display of a MIME message.
1383 The actual behavior depends on the value of `rmail-enable-mime'.
1385 If `rmail-enable-mime' is non-nil (the default), this command toggles
1386 the display of a MIME message between decoded presentation form and
1387 raw data. With optional prefix argument ARG, it toggles the display only
1388 of the MIME entity at point, if there is one. The optional argument
1389 STATE forces a particular display state, rather than toggling.
1390 `raw' forces raw mode, any other non-nil value forces decoded mode.
1392 If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\"
1393 buffer holding a decoded copy of the message. Inline content-types are
1394 handled according to `rmail-mime-media-type-handlers-alist'.
1395 By default, this displays text and multipart messages, and offers to
1396 download attachments as specified by `rmail-mime-attachment-dirs-alist'.
1397 The arguments ARG and STATE have no effect in this case."
1398 (interactive (list current-prefix-arg nil))
1399 (if rmail-enable-mime
1400 (with-current-buffer rmail-buffer
1401 (if (or (rmail-mime-message-p)
1402 (get-text-property (point-min) 'rmail-mime-hidden))
1403 (let* ((hidden (get-text-property (point-min) 'rmail-mime-hidden))
1404 (desired-hidden (if state (eq state 'raw) (not hidden))))
1405 (unless (eq hidden desired-hidden)
1406 (if (not desired-hidden)
1407 (rmail-show-message rmail-current-message)
1408 (let ((rmail-enable-mime nil)
1409 (inhibit-read-only t))
1410 (rmail-show-message rmail-current-message)
1411 (add-text-properties (point-min) (point-max) '(rmail-mime-hidden t))))))
1412 (message "Not a MIME message, just toggling headers")
1413 (rmail-toggle-header)))
1414 (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
1415 (buf (get-buffer-create "*RMAIL*"))
1416 (rmail-mime-mbox-buffer rmail-view-buffer)
1417 (rmail-mime-view-buffer buf))
1418 (set-buffer buf)
1419 (setq buffer-undo-list t)
1420 (let ((inhibit-read-only t))
1421 ;; Decoding the message in fundamental mode for speed, only
1422 ;; switching to rmail-mime-mode at the end for display. Eg
1423 ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
1424 (fundamental-mode)
1425 (erase-buffer)
1426 (insert data)
1427 (rmail-mime-show t)
1428 (rmail-mime-mode)
1429 (set-buffer-modified-p nil))
1430 (view-buffer buf))))
1432 (defun rmail-mm-get-boundary-error-message (message type disposition encoding)
1433 "Return MESSAGE with more information on the main MIME components."
1434 (error "%s; type: %s; disposition: %s; encoding: %s"
1435 message type disposition encoding))
1437 (defun rmail-show-mime ()
1438 "Function to use for the value of `rmail-show-mime-function'."
1439 (let ((entity (rmail-mime-parse))
1440 (rmail-mime-mbox-buffer rmail-buffer)
1441 (rmail-mime-view-buffer rmail-view-buffer)
1442 (rmail-mime-coding-system nil))
1443 ;; If ENTITY is not a vector, it is a string describing an error.
1444 (if (vectorp entity)
1445 (with-current-buffer rmail-mime-view-buffer
1446 (erase-buffer)
1447 ;; This condition-case is for catching an error in the
1448 ;; internal MIME decoding (e.g. incorrect BASE64 form) that
1449 ;; may be signaled by rmail-mime-insert.
1450 ;; FIXME: The current code doesn't set a proper error symbol
1451 ;; in ERR. We must find a way to propagate a correct error
1452 ;; symbol that is caused in the very deep code of text
1453 ;; decoding (e.g. an error by base64-decode-region called by
1454 ;; post-read-conversion function of utf-7).
1455 (condition-case err
1456 (progn
1457 (rmail-mime-insert entity)
1458 (if (consp rmail-mime-coding-system)
1459 ;; Decoding is done by rfc2047-decode-region only for a
1460 ;; header. But, as the used coding system may have been
1461 ;; overridden by mm-charset-override-alist, we can't
1462 ;; trust (car rmail-mime-coding-system). So, here we
1463 ;; try the decoding again with mm-charset-override-alist
1464 ;; bound to nil.
1465 (let ((mm-charset-override-alist nil))
1466 (setq rmail-mime-coding-system
1467 (rmail-mime-find-header-encoding
1468 (rmail-mime-entity-header entity)))))
1469 (set-buffer-file-coding-system
1470 (if rmail-mime-coding-system
1471 (coding-system-base rmail-mime-coding-system)
1472 'undecided)
1473 t t))
1474 (error (setq entity (format "%s" err))))))
1475 ;; Re-check ENTITY. It may be set to an error string.
1476 (when (stringp entity)
1477 ;; Decoding failed. ENTITY is an error message. Insert the
1478 ;; original message body as is, and show warning.
1479 (let ((region (with-current-buffer rmail-mime-mbox-buffer
1480 (goto-char (point-min))
1481 (re-search-forward "^$" nil t)
1482 (forward-line 1)
1483 (vector (point-min) (point) (point-max)))))
1484 (with-current-buffer rmail-mime-view-buffer
1485 (let ((inhibit-read-only t))
1486 (erase-buffer)
1487 (rmail-mime-insert-header region)
1488 (insert-buffer-substring rmail-mime-mbox-buffer
1489 (aref region 1) (aref region 2))))
1490 (set-buffer-file-coding-system 'no-conversion t t)
1491 (message "MIME decoding failed: %s" entity)))))
1493 (setq rmail-show-mime-function 'rmail-show-mime)
1495 (defun rmail-insert-mime-forwarded-message (forward-buffer)
1496 "Insert the message in FORWARD-BUFFER as a forwarded message.
1497 This is the usual value of `rmail-insert-mime-forwarded-message-function'."
1498 (let (contents-buffer start end)
1499 (with-current-buffer forward-buffer
1500 (setq contents-buffer
1501 (if rmail-buffer-swapped
1502 rmail-view-buffer
1503 forward-buffer)
1504 start (rmail-msgbeg rmail-current-message)
1505 end (rmail-msgend rmail-current-message)))
1506 (message-forward-make-body-mime contents-buffer start end)))
1508 (setq rmail-insert-mime-forwarded-message-function
1509 'rmail-insert-mime-forwarded-message)
1511 (defun rmail-insert-mime-resent-message (forward-buffer)
1512 "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
1513 (insert-buffer-substring
1514 (with-current-buffer forward-buffer rmail-view-buffer))
1515 (goto-char (point-min))
1516 (when (looking-at "From ")
1517 (forward-line 1)
1518 (delete-region (point-min) (point))))
1520 (setq rmail-insert-mime-resent-message-function
1521 'rmail-insert-mime-resent-message)
1523 (defun rmail-search-mime-message (msg regexp)
1524 "Function to set in `rmail-search-mime-message-function' (which see)."
1525 (save-restriction
1526 (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
1527 (let* ((rmail-mime-searching t) ; mark inside search
1528 (rmail-mime-mbox-buffer (current-buffer))
1529 (rmail-mime-view-buffer rmail-view-buffer)
1530 (header-end (save-excursion
1531 (re-search-forward "^$" nil 'move) (point)))
1532 (body-end (point-max))
1533 (entity (rmail-mime-parse)))
1535 ;; At first, just search the headers.
1536 (with-temp-buffer
1537 (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
1538 (rfc2047-decode-region (point-min) (point))
1539 (goto-char (point-min))
1540 (re-search-forward regexp nil t))
1541 ;; Next, search the body.
1542 (if (and entity
1543 ;; RMS: I am not sure why, but sometimes this is a string.
1544 (not (stringp entity))
1545 (let* ((content-type (rmail-mime-entity-type entity))
1546 (charset (cdr (assq 'charset (cdr content-type)))))
1547 (or (not (string-match "text/.*" (car content-type)))
1548 (and charset
1549 (not (string= (downcase charset) "us-ascii"))))))
1550 ;; Search the decoded MIME message.
1551 (with-temp-buffer
1552 (rmail-mime-insert entity)
1553 (goto-char (point-min))
1554 (re-search-forward regexp nil t))
1555 ;; Search the body without decoding.
1556 (goto-char header-end)
1557 (re-search-forward regexp nil t))))))
1559 (setq rmail-search-mime-message-function 'rmail-search-mime-message)
1561 (provide 'rmailmm)
1563 ;; Local Variables:
1564 ;; generated-autoload-file: "rmail-loaddefs.el"
1565 ;; End:
1567 ;;; rmailmm.el ends here