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