]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-decode.el
* lisp/gnus/mm-decode.el (mm-alist-to-plist): Doc fix.
[gnu-emacs] / lisp / gnus / mm-decode.el
1 ;;; mm-decode.el --- Functions for decoding MIME things
2
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'mail-parse)
27 (require 'mm-bodies)
28 (eval-when-compile (require 'cl))
29
30 (autoload 'gnus-map-function "gnus-util")
31 (autoload 'gnus-replace-in-string "gnus-util")
32 (autoload 'gnus-read-shell-command "gnus-util")
33
34 (autoload 'mm-inline-partial "mm-partial")
35 (autoload 'mm-inline-external-body "mm-extern")
36 (autoload 'mm-extern-cache-contents "mm-extern")
37 (autoload 'mm-insert-inline "mm-view")
38
39 (autoload 'mm-archive-decoders "mm-archive")
40 (autoload 'mm-archive-dissect-and-inline "mm-archive")
41 (autoload 'mm-dissect-archive "mm-archive")
42
43 (defvar gnus-current-window-configuration)
44
45 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
46 (add-hook 'gnus-exit-gnus-hook 'mm-temp-files-delete)
47
48 (defgroup mime-display ()
49 "Display of MIME in mail and news articles."
50 :link '(custom-manual "(emacs-mime)Display Customization")
51 :version "21.1"
52 :group 'mail
53 :group 'news
54 :group 'multimedia)
55
56 (defgroup mime-security ()
57 "MIME security in mail and news articles."
58 :link '(custom-manual "(emacs-mime)Display Customization")
59 :group 'mail
60 :group 'news
61 :group 'multimedia)
62
63 (defface mm-command-output
64 '((((class color)
65 (background dark))
66 (:foreground "ForestGreen"))
67 (((class color)
68 (background light))
69 (:foreground "red3"))
70 (t
71 (:italic t)))
72 "Face used for displaying output from commands."
73 :group 'mime-display)
74
75 ;;; Convenience macros.
76
77 (defmacro mm-handle-buffer (handle)
78 `(nth 0 ,handle))
79 (defmacro mm-handle-type (handle)
80 `(nth 1 ,handle))
81 (defsubst mm-handle-media-type (handle)
82 (if (stringp (car handle))
83 (car handle)
84 (car (mm-handle-type handle))))
85 (defsubst mm-handle-media-supertype (handle)
86 (car (split-string (mm-handle-media-type handle) "/")))
87 (defsubst mm-handle-media-subtype (handle)
88 (cadr (split-string (mm-handle-media-type handle) "/")))
89 (defmacro mm-handle-encoding (handle)
90 `(nth 2 ,handle))
91 (defmacro mm-handle-undisplayer (handle)
92 `(nth 3 ,handle))
93 (defmacro mm-handle-set-undisplayer (handle function)
94 `(setcar (nthcdr 3 ,handle) ,function))
95 (defmacro mm-handle-disposition (handle)
96 `(nth 4 ,handle))
97 (defmacro mm-handle-description (handle)
98 `(nth 5 ,handle))
99 (defmacro mm-handle-cache (handle)
100 `(nth 6 ,handle))
101 (defmacro mm-handle-set-cache (handle contents)
102 `(setcar (nthcdr 6 ,handle) ,contents))
103 (defmacro mm-handle-id (handle)
104 `(nth 7 ,handle))
105 (defmacro mm-handle-multipart-original-buffer (handle)
106 `(get-text-property 0 'buffer (car ,handle)))
107 (defmacro mm-handle-multipart-from (handle)
108 `(get-text-property 0 'from (car ,handle)))
109 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
110 `(get-text-property 0 ,parameter (car ,handle)))
111
112 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
113 disposition description cache
114 id)
115 `(list ,buffer ,type ,encoding ,undisplayer
116 ,disposition ,description ,cache ,id))
117
118 (defcustom mm-text-html-renderer
119 (cond ((fboundp 'libxml-parse-html-region) 'shr)
120 ((executable-find "w3m") 'gnus-w3m)
121 ((executable-find "links") 'links)
122 ((executable-find "lynx") 'lynx)
123 ((locate-library "html2text") 'html2text)
124 (t nil))
125 "Render of HTML contents.
126 It is one of defined renderer types, or a rendering function.
127 The defined renderer types are:
128 `shr': use the built-in Gnus HTML renderer;
129 `gnus-w3m': use Gnus renderer based on w3m;
130 `w3m': use emacs-w3m;
131 `w3m-standalone': use plain w3m;
132 `links': use links;
133 `lynx': use lynx;
134 `html2text': use html2text;
135 nil : use external viewer (default web browser)."
136 :version "24.1"
137 :type '(choice (const shr)
138 (const gnus-w3m)
139 (const w3m :tag "emacs-w3m")
140 (const w3m-standalone :tag "standalone w3m" )
141 (const links)
142 (const lynx)
143 (const html2text)
144 (const nil :tag "External viewer")
145 (function))
146 :group 'mime-display)
147
148 (defcustom mm-html-inhibit-images
149 (if (boundp 'mm-inline-text-html-with-images)
150 (not (symbol-value 'mm-inline-text-html-with-images))
151 t)
152 "Non-nil means inhibit displaying of images inline in the article body."
153 :version "25.1"
154 :type 'boolean
155 :group 'mime-display)
156
157 (defcustom mm-html-blocked-images ""
158 "Regexp matching image URLs to be blocked, or nil meaning not to block.
159 Note that cid images that are embedded in a message won't be blocked."
160 :version "25.1"
161 :type '(choice (const :tag "Allow all" nil)
162 (regexp :tag "Regular expression"))
163 :group 'mime-display)
164
165 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
166 "Regexp matching URLs which are considered to be safe.
167 Some HTML mails might contain a nasty trick used by spammers, using
168 the <img> tag which is far more evil than the [Click Here!] button.
169 It is most likely intended to check whether the ominous spam mail has
170 reached your eyes or not, in which case the spammer knows for sure
171 that your email address is valid. It is done by embedding an
172 identifier string into a URL that you might automatically retrieve
173 when displaying the image. The default value is \"\\\\`cid:\" which only
174 matches parts embedded to the Multipart/Related type MIME contents and
175 Gnus will never connect to the spammer's site arbitrarily. You may
176 set this variable to nil if you consider all urls to be safe."
177 :version "22.1"
178 :type '(choice (regexp :tag "Regexp")
179 (const :tag "All URLs are safe" nil))
180 :group 'mime-display)
181
182 (defcustom mm-inline-text-html-with-w3m-keymap t
183 "If non-nil, use emacs-w3m command keys in the article buffer."
184 :version "22.1"
185 :type 'boolean
186 :group 'mime-display)
187
188 (defcustom mm-enable-external t
189 "Indicate whether external MIME handlers should be used.
190
191 If t, all defined external MIME handlers are used. If nil, files are saved by
192 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
193 before the external MIME handler is invoked."
194 :version "22.1"
195 :type '(choice (const :tag "Always" t)
196 (const :tag "Never" nil)
197 (const :tag "Ask" ask))
198 :group 'mime-display)
199
200 (defcustom mm-inline-media-tests
201 '(("image/p?jpeg"
202 mm-inline-image
203 (lambda (handle)
204 (mm-valid-and-fit-image-p 'jpeg handle)))
205 ("image/png"
206 mm-inline-image
207 (lambda (handle)
208 (mm-valid-and-fit-image-p 'png handle)))
209 ("image/gif"
210 mm-inline-image
211 (lambda (handle)
212 (mm-valid-and-fit-image-p 'gif handle)))
213 ("image/tiff"
214 mm-inline-image
215 (lambda (handle)
216 (mm-valid-and-fit-image-p 'tiff handle)))
217 ("image/xbm"
218 mm-inline-image
219 (lambda (handle)
220 (mm-valid-and-fit-image-p 'xbm handle)))
221 ("image/x-xbitmap"
222 mm-inline-image
223 (lambda (handle)
224 (mm-valid-and-fit-image-p 'xbm handle)))
225 ("image/xpm"
226 mm-inline-image
227 (lambda (handle)
228 (mm-valid-and-fit-image-p 'xpm handle)))
229 ("image/x-xpixmap"
230 mm-inline-image
231 (lambda (handle)
232 (mm-valid-and-fit-image-p 'xpm handle)))
233 ("image/bmp"
234 mm-inline-image
235 (lambda (handle)
236 (mm-valid-and-fit-image-p 'bmp handle)))
237 ("image/x-portable-bitmap"
238 mm-inline-image
239 (lambda (handle)
240 (mm-valid-and-fit-image-p 'pbm handle)))
241 ("text/plain" mm-inline-text identity)
242 ("text/enriched" mm-inline-text identity)
243 ("text/richtext" mm-inline-text identity)
244 ("text/x-patch" mm-display-patch-inline identity)
245 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
246 ("text/x-diff" mm-display-patch-inline identity)
247 ("application/emacs-lisp" mm-display-elisp-inline identity)
248 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
249 ("application/x-shellscript" mm-display-shell-script-inline identity)
250 ("application/x-sh" mm-display-shell-script-inline identity)
251 ("text/x-sh" mm-display-shell-script-inline identity)
252 ("application/javascript" mm-display-javascript-inline identity)
253 ("text/dns" mm-display-dns-inline identity)
254 ("text/x-org" mm-display-org-inline identity)
255 ("text/html"
256 mm-inline-text-html
257 (lambda (handle)
258 mm-text-html-renderer))
259 ("text/x-vcard"
260 mm-inline-text-vcard
261 (lambda (handle)
262 (or (featurep 'vcard)
263 (locate-library "vcard"))))
264 ("message/delivery-status" mm-inline-text identity)
265 ("message/rfc822" mm-inline-message identity)
266 ("message/partial" mm-inline-partial identity)
267 ("message/external-body" mm-inline-external-body identity)
268 ("text/.*" mm-inline-text identity)
269 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity)
270 ("application/zip" mm-archive-dissect-and-inline identity)
271 ("audio/wav" mm-inline-audio
272 (lambda (handle)
273 (and (or (featurep 'nas-sound) (featurep 'native-sound))
274 (device-sound-enabled-p))))
275 ("audio/au"
276 mm-inline-audio
277 (lambda (handle)
278 (and (or (featurep 'nas-sound) (featurep 'native-sound))
279 (device-sound-enabled-p))))
280 ("application/pgp-signature" ignore identity)
281 ("application/x-pkcs7-signature" ignore identity)
282 ("application/pkcs7-signature" ignore identity)
283 ("application/x-pkcs7-mime" ignore identity)
284 ("application/pkcs7-mime" ignore identity)
285 ("multipart/alternative" ignore identity)
286 ("multipart/mixed" ignore identity)
287 ("multipart/related" ignore identity)
288 ("image/.*"
289 mm-inline-image
290 (lambda (handle)
291 (and (mm-valid-image-format-p 'imagemagick)
292 (mm-with-unibyte-buffer
293 (mm-insert-part handle)
294 (let ((image
295 (ignore-errors
296 (if (fboundp 'create-image)
297 (create-image (buffer-string) 'imagemagick 'data-p)
298 (mm-create-image-xemacs
299 (mm-handle-media-subtype handle))))))
300 (when image
301 (setcar (cdr handle) (list "image/imagemagick"))
302 (mm-image-fit-p handle)))))))
303 ;; Disable audio and image
304 ("audio/.*" ignore ignore)
305 ("image/.*" ignore ignore)
306 ;; Default to displaying as text
307 (".*" mm-inline-text mm-readable-p))
308 "Alist of media types/tests saying whether types can be displayed inline."
309 :type '(repeat (list (regexp :tag "MIME type")
310 (function :tag "Display function")
311 (function :tag "Display test")))
312 :group 'mime-display)
313
314 (defcustom mm-inlined-types
315 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
316 "message/partial" "message/external-body" "application/emacs-lisp"
317 "application/x-emacs-lisp"
318 "application/pgp-signature" "application/x-pkcs7-signature"
319 "application/pkcs7-signature" "application/x-pkcs7-mime"
320 "application/pkcs7-mime"
321 "application/x-gtar-compressed"
322 "application/x-tar"
323 "application/zip"
324 ;; Mutt still uses this even though it has already been withdrawn.
325 "application/pgp")
326 "List of media types that are to be displayed inline.
327 See also `mm-inline-media-tests', which says how to display a media
328 type inline."
329 :type '(repeat regexp)
330 :group 'mime-display)
331
332 (defcustom mm-keep-viewer-alive-types
333 '("application/postscript" "application/msword" "application/vnd.ms-excel"
334 "application/pdf" "application/x-dvi")
335 "List of media types for which the external viewer will not be killed
336 when selecting a different article."
337 :version "22.1"
338 :type '(repeat regexp)
339 :group 'mime-display)
340
341 (defcustom mm-automatic-display
342 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
343 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
344 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
345 "application/emacs-lisp" "application/x-emacs-lisp"
346 "application/x-pkcs7-signature"
347 "application/pkcs7-signature" "application/x-pkcs7-mime"
348 "application/pkcs7-mime"
349 ;; Mutt still uses this even though it has already been withdrawn.
350 "application/pgp\\'"
351 "text/x-org")
352 "A list of MIME types to be displayed automatically."
353 :type '(repeat regexp)
354 :group 'mime-display)
355
356 (defcustom mm-attachment-override-types '("text/x-vcard"
357 "application/pkcs7-mime"
358 "application/x-pkcs7-mime"
359 "application/pkcs7-signature"
360 "application/x-pkcs7-signature")
361 "Types to have \"attachment\" ignored if they can be displayed inline."
362 :type '(repeat regexp)
363 :group 'mime-display)
364
365 (defcustom mm-inline-override-types nil
366 "Types to be treated as attachments even if they can be displayed inline."
367 :type '(repeat regexp)
368 :group 'mime-display)
369
370 (defcustom mm-automatic-external-display nil
371 "List of MIME type regexps that will be displayed externally automatically."
372 :type '(repeat regexp)
373 :group 'mime-display)
374
375 (defcustom mm-discouraged-alternatives nil
376 "List of MIME types that are discouraged when viewing multipart/alternative.
377 Viewing agents are supposed to view the last possible part of a message,
378 as that is supposed to be the richest. However, users may prefer other
379 types instead, and this list says what types are most unwanted. If,
380 for instance, text/html parts are very unwanted, and text/richtext are
381 somewhat unwanted, then the value of this variable should be set
382 to:
383
384 (\"text/html\" \"text/richtext\")
385
386 Adding \"image/.*\" might also be useful. Spammers use it as the
387 preferred part of multipart/alternative messages. See also
388 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
389 enables you to choose manually one of two types those mails include."
390 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
391 :group 'mime-display)
392
393 (defcustom mm-tmp-directory
394 (if (fboundp 'temp-directory)
395 (temp-directory)
396 (if (boundp 'temporary-file-directory)
397 temporary-file-directory
398 "/tmp/"))
399 "Where mm will store its temporary files."
400 :type 'directory
401 :group 'mime-display)
402
403 (defcustom mm-inline-large-images nil
404 "If t, then all images fit in the buffer.
405 If `resize', try to resize the images so they fit."
406 :type '(radio
407 (const :tag "Inline large images as they are." t)
408 (const :tag "Resize large images." resize)
409 (const :tag "Do not inline large images." nil))
410 :group 'mime-display)
411
412 (defcustom mm-file-name-rewrite-functions
413 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
414 "List of functions used for rewriting file names of MIME parts.
415 Each function takes a file name as input and returns a file name.
416
417 Ready-made functions include `mm-file-name-delete-control',
418 `mm-file-name-delete-gotchas' (you should not remove these two
419 functions), `mm-file-name-delete-whitespace',
420 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
421 `mm-file-name-replace-whitespace', `capitalize', `downcase',
422 `upcase', and `upcase-initials'."
423 :type '(list (set :inline t
424 (const mm-file-name-delete-control)
425 (const mm-file-name-delete-gotchas)
426 (const mm-file-name-delete-whitespace)
427 (const mm-file-name-trim-whitespace)
428 (const mm-file-name-collapse-whitespace)
429 (const mm-file-name-replace-whitespace)
430 (const capitalize)
431 (const downcase)
432 (const upcase)
433 (const upcase-initials)
434 (repeat :inline t
435 :tag "Function"
436 function)))
437 :version "23.1" ;; No Gnus
438 :group 'mime-display)
439
440
441 (defvar mm-path-name-rewrite-functions nil
442 "*List of functions for rewriting the full file names of MIME parts.
443 This is used when viewing parts externally, and is meant for
444 transforming the absolute name so that non-compliant programs can find
445 the file where it's saved.
446
447 Each function takes a file name as input and returns a file name.")
448
449 (defvar mm-file-name-replace-whitespace nil
450 "String used for replacing whitespace characters; default is `\"_\"'.")
451
452 (defcustom mm-default-directory nil
453 "The default directory where mm will save files.
454 If not set, `default-directory' will be used."
455 :type '(choice directory (const :tag "Default" nil))
456 :group 'mime-display)
457
458 (defcustom mm-attachment-file-modes 384
459 "Set the mode bits of saved attachments to this integer."
460 :version "22.1"
461 :type 'integer
462 :group 'mime-display)
463
464 (defcustom mm-external-terminal-program "xterm"
465 "The program to start an external terminal."
466 :version "22.1"
467 :type 'string
468 :group 'mime-display)
469
470 ;;; Internal variables.
471
472 (defvar mm-last-shell-command "")
473 (defvar mm-content-id-alist nil)
474 (defvar mm-postponed-undisplay-list nil)
475 (defvar mm-inhibit-auto-detect-attachment nil)
476 (defvar mm-temp-files-to-be-deleted nil
477 "List of temporary files scheduled to be deleted.")
478 (defvar mm-temp-files-cache-file (concat ".mm-temp-files-" (user-login-name))
479 "Name of a file that caches a list of temporary files to be deleted.
480 The file will be saved in the directory `mm-tmp-directory'.")
481
482 ;; According to RFC2046, in particular, in a digest, the default
483 ;; Content-Type value for a body part is changed from "text/plain" to
484 ;; "message/rfc822".
485 (defvar mm-dissect-default-type "text/plain")
486
487 (autoload 'mml2015-verify "mml2015")
488 (autoload 'mml2015-verify-test "mml2015")
489 (autoload 'mml-smime-verify "mml-smime")
490 (autoload 'mml-smime-verify-test "mml-smime")
491
492 (defvar mm-verify-function-alist
493 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
494 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
495 mm-uu-pgp-signed-test)
496 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
497 mml-smime-verify-test)
498 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
499 mml-smime-verify-test)))
500
501 (defcustom mm-verify-option 'never
502 "Option of verifying signed parts.
503 `never', not verify; `always', always verify;
504 `known', only verify known protocols. Otherwise, ask user.
505
506 When set to `always' or `known', you should add
507 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
508 result of the verification."
509 :version "22.1"
510 :type '(choice (item always)
511 (item never)
512 (item :tag "only known protocols" known)
513 (item :tag "ask" nil))
514 :group 'mime-security)
515
516 (autoload 'mml2015-decrypt "mml2015")
517 (autoload 'mml2015-decrypt-test "mml2015")
518
519 (defvar mm-decrypt-function-alist
520 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
521 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
522 mm-uu-pgp-encrypted-test)))
523
524 (defcustom mm-decrypt-option nil
525 "Option of decrypting encrypted parts.
526 `never', not decrypt; `always', always decrypt;
527 `known', only decrypt known protocols. Otherwise, ask user."
528 :version "22.1"
529 :type '(choice (item always)
530 (item never)
531 (item :tag "only known protocols" known)
532 (item :tag "ask" nil))
533 :group 'mime-security)
534
535 (defvar mm-viewer-completion-map
536 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
537 (set-keymap-parent map minibuffer-local-completion-map)
538 ;; Should we bind other key to minibuffer-complete-word?
539 (define-key map " " 'self-insert-command)
540 map)
541 "Keymap for input viewer with completion.")
542
543 ;;; The functions.
544
545 (defun mm-alist-to-plist (alist)
546 "Convert association list ALIST into the equivalent property-list form.
547 The plist is returned. This converts from
548
549 \((a . 1) (b . 2) (c . 3))
550
551 into
552
553 \(a 1 b 2 c 3)
554
555 The original alist is not modified."
556 (let (plist)
557 (while alist
558 (let ((el (car alist)))
559 (setq plist (cons (cdr el) (cons (car el) plist))))
560 (setq alist (cdr alist)))
561 (nreverse plist)))
562
563 (defun mm-keep-viewer-alive-p (handle)
564 "Say whether external viewer for HANDLE should stay alive."
565 (let ((types mm-keep-viewer-alive-types)
566 (type (mm-handle-media-type handle))
567 ty)
568 (catch 'found
569 (while (setq ty (pop types))
570 (when (string-match ty type)
571 (throw 'found t))))))
572
573 (defun mm-handle-set-external-undisplayer (handle function)
574 "Set the undisplayer for HANDLE to FUNCTION.
575 Postpone undisplaying of viewers for types in
576 `mm-keep-viewer-alive-types'."
577 (if (mm-keep-viewer-alive-p handle)
578 (let ((new-handle (copy-sequence handle)))
579 (mm-handle-set-undisplayer new-handle function)
580 (mm-handle-set-undisplayer handle nil)
581 (push new-handle mm-postponed-undisplay-list))
582 (mm-handle-set-undisplayer handle function)))
583
584 (defun mm-destroy-postponed-undisplay-list ()
585 (when mm-postponed-undisplay-list
586 (message "Destroying external MIME viewers")
587 (mm-destroy-parts mm-postponed-undisplay-list)))
588
589 (defun mm-temp-files-delete ()
590 "Delete temporary files and those parent directories.
591 Note that the deletion may fail if a program is catching hold of a file
592 under Windows or Cygwin. In that case, it schedules the deletion of
593 files left at the next time."
594 (let* ((coding-system-for-read mm-universal-coding-system)
595 (coding-system-for-write mm-universal-coding-system)
596 (cache-file (expand-file-name mm-temp-files-cache-file
597 mm-tmp-directory))
598 (cache (when (file-exists-p cache-file)
599 (mm-with-multibyte-buffer
600 (insert-file-contents cache-file)
601 (split-string (buffer-string) "\n" t))))
602 fails)
603 (dolist (temp (append cache mm-temp-files-to-be-deleted))
604 (when (and (file-exists-p temp)
605 (if (file-directory-p temp)
606 ;; A parent directory left at the previous time.
607 (progn
608 (ignore-errors (delete-directory temp))
609 (file-exists-p temp))
610 ;; Delete a temporary file and its parent directory.
611 (ignore-errors (delete-file temp))
612 (or (file-exists-p temp)
613 (progn
614 (setq temp (file-name-directory temp))
615 (ignore-errors (delete-directory temp))
616 (file-exists-p temp)))))
617 (push temp fails)))
618 (if fails
619 ;; Schedule the deletion of the files left at the next time.
620 (progn
621 (write-region (concat (mapconcat 'identity (nreverse fails) "\n")
622 "\n")
623 nil cache-file nil 'silent)
624 (set-file-modes cache-file #o600))
625 (when (file-exists-p cache-file)
626 (ignore-errors (delete-file cache-file))))
627 (setq mm-temp-files-to-be-deleted nil)))
628
629 (autoload 'message-fetch-field "message")
630
631 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
632 "Dissect the current buffer and return a list of MIME handles.
633 If NO-STRICT-MIME, don't require the message to have a
634 MIME-Version header before proceeding."
635 (save-excursion
636 (let (ct ctl type subtype cte cd description id result)
637 (save-restriction
638 (mail-narrow-to-head)
639 (when (or no-strict-mime
640 loose-mime
641 (mail-fetch-field "mime-version"))
642 (setq ct (mail-fetch-field "content-type")
643 ctl (and ct (mail-header-parse-content-type ct))
644 cte (mail-fetch-field "content-transfer-encoding")
645 cd (or (mail-fetch-field "content-disposition")
646 (when (and ctl
647 (eq 'mm-inline-text
648 (cadr (mm-assoc-string-match
649 mm-inline-media-tests
650 (car ctl)))))
651 "inline"))
652 ;; Newlines in description should be stripped so as
653 ;; not to break the MIME tag into two or more lines.
654 description (message-fetch-field "content-description")
655 id (mail-fetch-field "content-id"))
656 (unless from
657 (setq from (mail-fetch-field "from")))
658 ;; FIXME: In some circumstances, this code is running within
659 ;; a unibyte macro. mail-extract-address-components
660 ;; creates unibyte buffers. This `if', though not a perfect
661 ;; solution, avoids most of them.
662 (if from
663 (setq from (cadr (mail-extract-address-components from))))
664 (if description
665 (setq description (mail-decode-encoded-word-string
666 description)))))
667 (if (or (not ctl)
668 (not (string-match "/" (car ctl))))
669 (mm-dissect-singlepart
670 (list mm-dissect-default-type)
671 (and cte (intern (downcase (mail-header-strip cte))))
672 no-strict-mime
673 (and cd (mail-header-parse-content-disposition cd))
674 description)
675 (setq type (split-string (car ctl) "/"))
676 (setq subtype (cadr type)
677 type (car type))
678 (setq
679 result
680 (cond
681 ((equal type "multipart")
682 (let ((mm-dissect-default-type (if (equal subtype "digest")
683 "message/rfc822"
684 "text/plain"))
685 (start (cdr (assq 'start (cdr ctl)))))
686 (add-text-properties 0 (length (car ctl))
687 (mm-alist-to-plist (cdr ctl)) (car ctl))
688
689 ;; what really needs to be done here is a way to link a
690 ;; MIME handle back to it's parent MIME handle (in a multilevel
691 ;; MIME article). That would probably require changing
692 ;; the mm-handle API so we simply store the multipart buffer
693 ;; name as a text property of the "multipart/whatever" string.
694 (add-text-properties 0 (length (car ctl))
695 (list 'buffer (mm-copy-to-buffer)
696 'from from
697 'start start)
698 (car ctl))
699 (cons (car ctl) (mm-dissect-multipart ctl from))))
700 (t
701 (mm-possibly-verify-or-decrypt
702 (mm-dissect-singlepart
703 ctl
704 (and cte (intern (downcase (mail-header-strip cte))))
705 no-strict-mime
706 (and cd (mail-header-parse-content-disposition cd))
707 description id)
708 ctl from))))
709 (when id
710 (when (string-match " *<\\(.*\\)> *" id)
711 (setq id (match-string 1 id)))
712 (push (cons id result) mm-content-id-alist))
713 result))))
714
715 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
716 (when (or force
717 (if (equal "text/plain" (car ctl))
718 (assoc 'format ctl)
719 t))
720 ;; Guess what the type of application/octet-stream parts should
721 ;; really be.
722 (let ((filename (cdr (assq 'filename (cdr cdl)))))
723 (when (and (not mm-inhibit-auto-detect-attachment)
724 (equal (car ctl) "application/octet-stream")
725 filename
726 (string-match "\\.\\([^.]+\\)$" filename))
727 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
728 (when new-type
729 (setcar ctl new-type)))))
730 (let ((handle
731 (mm-make-handle
732 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
733 (decoder (assoc (car ctl) (mm-archive-decoders))))
734 (if (and decoder
735 ;; Do automatic decoding
736 (cadr decoder)
737 (executable-find (caddr decoder)))
738 (mm-dissect-archive handle)
739 handle))))
740
741 (defun mm-dissect-multipart (ctl from)
742 (goto-char (point-min))
743 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
744 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
745 start parts
746 (end (save-excursion
747 (goto-char (point-max))
748 (if (re-search-backward close-delimiter nil t)
749 (match-beginning 0)
750 (point-max))))
751 (mm-inhibit-auto-detect-attachment
752 (equal (car ctl) "multipart/encrypted")))
753 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
754 (while (and (< (point) end) (re-search-forward boundary end t))
755 (goto-char (match-beginning 0))
756 (when start
757 (save-excursion
758 (save-restriction
759 (narrow-to-region start (point))
760 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
761 (end-of-line 2)
762 (or (looking-at boundary)
763 (forward-line 1))
764 (setq start (point)))
765 (when (and start (< start end))
766 (save-excursion
767 (save-restriction
768 (narrow-to-region start end)
769 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
770 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
771
772 (defun mm-copy-to-buffer ()
773 "Copy the contents of the current buffer to a fresh buffer."
774 (let ((obuf (current-buffer))
775 (mb (mm-multibyte-p))
776 beg)
777 (goto-char (point-min))
778 (search-forward-regexp "^\n" nil t)
779 (setq beg (point))
780 (with-current-buffer
781 (generate-new-buffer " *mm*")
782 ;; Preserve the data's unibyteness (for url-insert-file-contents).
783 (mm-set-buffer-multibyte mb)
784 (insert-buffer-substring obuf beg)
785 (current-buffer))))
786
787 (defun mm-display-parts (handle &optional no-default)
788 (if (stringp (car handle))
789 (mapcar 'mm-display-parts (cdr handle))
790 (if (bufferp (car handle))
791 (save-restriction
792 (narrow-to-region (point) (point))
793 (mm-display-part handle)
794 (goto-char (point-max)))
795 (mapcar 'mm-display-parts handle))))
796
797 (autoload 'mailcap-parse-mailcaps "mailcap")
798 (autoload 'mailcap-mime-info "mailcap")
799
800 (defun mm-head-p (&optional point)
801 "Return non-nil if point is in the article header."
802 (let ((point (or point (point))))
803 (save-excursion
804 (goto-char point)
805 (and (not (re-search-backward "^$" nil t))
806 (re-search-forward "^$" nil t)))))
807
808 (defun mm-display-part (handle &optional no-default force)
809 "Display the MIME part represented by HANDLE.
810 Returns nil if the part is removed; inline if displayed inline;
811 external if displayed external."
812 (save-excursion
813 (mailcap-parse-mailcaps)
814 (if (and (not force)
815 (mm-handle-displayed-p handle))
816 (mm-remove-part handle)
817 (let* ((ehandle (if (equal (mm-handle-media-type handle)
818 "message/external-body")
819 (progn
820 (unless (mm-handle-cache handle)
821 (mm-extern-cache-contents handle))
822 (mm-handle-cache handle))
823 handle))
824 (type (mm-handle-media-type ehandle))
825 (method (mailcap-mime-info type))
826 (filename (or (mail-content-type-get
827 (mm-handle-disposition handle) 'filename)
828 (mail-content-type-get
829 (mm-handle-type handle) 'name)
830 "<file>"))
831 (external mm-enable-external)
832 (decoder (assoc (car (mm-handle-type handle))
833 (mm-archive-decoders))))
834 (cond
835 ((and decoder
836 (executable-find (caddr decoder)))
837 (mm-archive-dissect-and-inline handle)
838 'inline)
839 ((and (mm-inlinable-p ehandle)
840 (mm-inlined-p ehandle))
841 (when force
842 (if (mm-head-p)
843 (re-search-forward "^$" nil t)
844 (forward-line 1)))
845 (mm-display-inline handle)
846 'inline)
847 ((or method
848 (not no-default))
849 (if (and (not method)
850 (equal "text" (car (split-string type "/"))))
851 (progn
852 (forward-line 1)
853 (mm-insert-inline handle (mm-get-part handle))
854 'inline)
855 (setq external
856 (and method ;; If nil, we always use "save".
857 (or (eq mm-enable-external t)
858 (and (eq mm-enable-external 'ask)
859 (y-or-n-p
860 (concat
861 "Display part (" type
862 ") "
863 (if (stringp method)
864 (concat
865 "using external program \""
866 (format method filename) "\"")
867 (gnus-format-message
868 "by calling `%s' on the contents)" method))
869 "? "))))))
870 (if external
871 (mm-display-external
872 handle (or method 'mailcap-save-binary-file))
873 (mm-display-external
874 handle 'mailcap-save-binary-file)))))))))
875
876 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
877 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
878 (declare-function term-mode "term" ())
879 (declare-function term-char-mode "term" ())
880
881 (defun mm-display-external (handle method)
882 "Display HANDLE using METHOD."
883 (let ((outbuf (current-buffer)))
884 (mm-with-unibyte-buffer
885 (if (functionp method)
886 (let ((cur (current-buffer)))
887 (if (eq method 'mailcap-save-binary-file)
888 (progn
889 (set-buffer (generate-new-buffer " *mm*"))
890 (setq method nil))
891 (mm-insert-part handle)
892 (mm-add-meta-html-tag handle)
893 (let ((win (get-buffer-window cur t)))
894 (when win
895 (select-window win)))
896 (switch-to-buffer (generate-new-buffer " *mm*")))
897 (buffer-disable-undo)
898 (mm-set-buffer-file-coding-system mm-binary-coding-system)
899 (insert-buffer-substring cur)
900 (goto-char (point-min))
901 (when method
902 (message "Viewing with %s" method))
903 (let ((mm (current-buffer))
904 (non-viewer (assq 'non-viewer
905 (mailcap-mime-info
906 (mm-handle-media-type handle) t))))
907 (unwind-protect
908 (if method
909 (progn
910 (when (and (boundp 'gnus-summary-buffer)
911 (bufferp gnus-summary-buffer)
912 (buffer-name gnus-summary-buffer))
913 ;; So that we pop back to the right place, sort of.
914 (switch-to-buffer gnus-summary-buffer)
915 (switch-to-buffer mm))
916 (delete-other-windows)
917 (funcall method))
918 (mm-save-part handle))
919 (when (and (not non-viewer)
920 method)
921 (mm-handle-set-undisplayer handle mm)))))
922 ;; The function is a string to be executed.
923 (mm-insert-part handle)
924 (mm-add-meta-html-tag handle)
925 (let* ((dir (mm-make-temp-file
926 (expand-file-name "emm." mm-tmp-directory) 'dir))
927 (filename (or
928 (mail-content-type-get
929 (mm-handle-disposition handle) 'filename)
930 (mail-content-type-get
931 (mm-handle-type handle) 'name)))
932 (mime-info (mailcap-mime-info
933 (mm-handle-media-type handle) t))
934 (needsterm (or (assoc "needsterm" mime-info)
935 (assoc "needsterminal" mime-info)))
936 (copiousoutput (assoc "copiousoutput" mime-info))
937 file buffer)
938 ;; We create a private sub-directory where we store our files.
939 (set-file-modes dir #o700)
940 (if filename
941 (setq file (expand-file-name
942 (gnus-map-function mm-file-name-rewrite-functions
943 (file-name-nondirectory filename))
944 dir))
945 ;; Use nametemplate (defined in RFC1524) if it is specified
946 ;; in mailcap.
947 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
948 (if (and suffix
949 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
950 (setq suffix (match-string 1 suffix))
951 ;; Otherwise, use a suffix according to
952 ;; `mailcap-mime-extensions'.
953 (setq suffix (car (rassoc (mm-handle-media-type handle)
954 mailcap-mime-extensions))))
955 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
956 nil suffix))))
957 (let ((coding-system-for-write mm-binary-coding-system))
958 (write-region (point-min) (point-max) file nil 'nomesg))
959 ;; The file is deleted after the viewer exists. If the users edits
960 ;; the file, changes will be lost. Set file to read-only to make it
961 ;; clear.
962 (set-file-modes file #o400)
963 (message "Viewing with %s" method)
964 (cond
965 (needsterm
966 (let ((command (mm-mailcap-command
967 method file (mm-handle-type handle))))
968 (unwind-protect
969 (if window-system
970 (set-process-sentinel
971 (start-process "*display*" nil
972 mm-external-terminal-program
973 "-e" shell-file-name
974 shell-command-switch command)
975 `(lambda (process state)
976 (if (eq 'exit (process-status process))
977 (run-at-time
978 60.0 nil
979 (lambda ()
980 (ignore-errors (delete-file ,file))
981 (ignore-errors (delete-directory
982 ,(file-name-directory
983 file))))))))
984 (require 'term)
985 (require 'gnus-win)
986 (set-buffer
987 (setq buffer
988 (make-term "display"
989 shell-file-name
990 nil
991 shell-command-switch command)))
992 (term-mode)
993 (term-char-mode)
994 (set-process-sentinel
995 (get-buffer-process buffer)
996 `(lambda (process state)
997 (when (eq 'exit (process-status process))
998 (ignore-errors (delete-file ,file))
999 (ignore-errors
1000 (delete-directory ,(file-name-directory file)))
1001 (gnus-configure-windows
1002 ',gnus-current-window-configuration))))
1003 (gnus-configure-windows 'display-term))
1004 (mm-handle-set-external-undisplayer handle (cons file buffer))
1005 (add-to-list 'mm-temp-files-to-be-deleted file t))
1006 (message "Displaying %s..." command))
1007 'external)
1008 (copiousoutput
1009 (with-current-buffer outbuf
1010 (forward-line 1)
1011 (mm-insert-inline
1012 handle
1013 (unwind-protect
1014 (progn
1015 (call-process shell-file-name nil
1016 (setq buffer
1017 (generate-new-buffer " *mm*"))
1018 nil
1019 shell-command-switch
1020 (mm-mailcap-command
1021 method file (mm-handle-type handle)))
1022 (if (buffer-live-p buffer)
1023 (with-current-buffer buffer
1024 (buffer-string))))
1025 (progn
1026 (ignore-errors (delete-file file))
1027 (ignore-errors (delete-directory
1028 (file-name-directory file)))
1029 (ignore-errors (kill-buffer buffer))))))
1030 'inline)
1031 (t
1032 ;; Deleting the temp file should be postponed for some wrappers,
1033 ;; shell scripts, and so on, which might exit right after having
1034 ;; started a viewer command as a background job.
1035 (let ((command (mm-mailcap-command
1036 method file (mm-handle-type handle))))
1037 (unwind-protect
1038 (let ((process-connection-type nil))
1039 (start-process "*display*"
1040 (setq buffer
1041 (generate-new-buffer " *mm*"))
1042 shell-file-name
1043 shell-command-switch command)
1044 (set-process-sentinel
1045 (get-buffer-process buffer)
1046 (lexical-let ((outbuf outbuf)
1047 (file file)
1048 (buffer buffer)
1049 (command command)
1050 (handle handle))
1051 (lambda (process state)
1052 (when (eq (process-status process) 'exit)
1053 (run-at-time
1054 60.0 nil
1055 (lambda ()
1056 (ignore-errors (delete-file file))
1057 (ignore-errors (delete-directory
1058 (file-name-directory file)))))
1059 (when (buffer-live-p outbuf)
1060 (with-current-buffer outbuf
1061 (let ((buffer-read-only nil)
1062 (point (point)))
1063 (forward-line 2)
1064 (let ((start (point)))
1065 (mm-insert-inline
1066 handle (with-current-buffer buffer
1067 (buffer-string)))
1068 (put-text-property start (point)
1069 'face 'mm-command-output))
1070 (goto-char point))))
1071 (when (buffer-live-p buffer)
1072 (kill-buffer buffer)))
1073 (message "Displaying %s...done" command)))))
1074 (mm-handle-set-external-undisplayer
1075 handle (cons file buffer))
1076 (add-to-list 'mm-temp-files-to-be-deleted file t))
1077 (message "Displaying %s..." command))
1078 'external)))))))
1079
1080 (defun mm-mailcap-command (method file type-list)
1081 (let ((ctl (cdr type-list))
1082 (beg 0)
1083 (uses-stdin t)
1084 out sub total)
1085 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1086 method beg)
1087 (push (substring method beg (match-beginning 0)) out)
1088 (setq beg (match-end 0)
1089 total (match-string 0 method)
1090 sub (match-string 1 method))
1091 (cond
1092 ((string= total "%%")
1093 (push "%" out))
1094 ((or (string= total "%s")
1095 ;; We do our own quoting.
1096 (string= total "'%s'")
1097 (string= total "\"%s\""))
1098 (setq uses-stdin nil)
1099 (push (shell-quote-argument
1100 (gnus-map-function mm-path-name-rewrite-functions file)) out))
1101 ((string= total "%t")
1102 (push (shell-quote-argument (car type-list)) out))
1103 (t
1104 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
1105 (push (substring method beg (length method)) out)
1106 (when uses-stdin
1107 (push "<" out)
1108 (push (shell-quote-argument
1109 (gnus-map-function mm-path-name-rewrite-functions file))
1110 out))
1111 (mapconcat 'identity (nreverse out) "")))
1112
1113 (defun mm-remove-parts (handles)
1114 "Remove the displayed MIME parts represented by HANDLES."
1115 (if (and (listp handles)
1116 (bufferp (car handles)))
1117 (mm-remove-part handles)
1118 (let (handle)
1119 (while (setq handle (pop handles))
1120 (cond
1121 ((stringp handle)
1122 (when (buffer-live-p (get-text-property 0 'buffer handle))
1123 (kill-buffer (get-text-property 0 'buffer handle))))
1124 ((and (listp handle)
1125 (stringp (car handle)))
1126 (mm-remove-parts (cdr handle)))
1127 (t
1128 (mm-remove-part handle)))))))
1129
1130 (defun mm-destroy-parts (handles)
1131 "Remove the displayed MIME parts represented by HANDLES."
1132 (if (and (listp handles)
1133 (bufferp (car handles)))
1134 (mm-destroy-part handles)
1135 (let (handle)
1136 (while (setq handle (pop handles))
1137 (cond
1138 ((stringp handle)
1139 (when (buffer-live-p (get-text-property 0 'buffer handle))
1140 (kill-buffer (get-text-property 0 'buffer handle))))
1141 ((and (listp handle)
1142 (stringp (car handle)))
1143 (mm-destroy-parts handle))
1144 (t
1145 (mm-destroy-part handle)))))))
1146
1147 (defun mm-remove-part (handle)
1148 "Remove the displayed MIME part represented by HANDLE."
1149 (when (listp handle)
1150 (let ((object (mm-handle-undisplayer handle)))
1151 (ignore-errors
1152 (cond
1153 ;; Internally displayed part.
1154 ((mm-annotationp object)
1155 (if (featurep 'xemacs)
1156 (delete-annotation object)))
1157 ((or (functionp object)
1158 (and (listp object)
1159 (eq (car object) 'lambda)))
1160 (funcall object))
1161 ;; Externally displayed part.
1162 ((consp object)
1163 (condition-case ()
1164 (while (get-buffer-process (cdr object))
1165 (interrupt-process (get-buffer-process (cdr object)))
1166 (message "Waiting for external displayer to die...")
1167 (sit-for 1))
1168 (quit)
1169 (error))
1170 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1171 (message "Waiting for external displayer to die...done")
1172 (ignore-errors (delete-file (car object)))
1173 (ignore-errors (delete-directory (file-name-directory
1174 (car object)))))
1175 ((bufferp object)
1176 (when (buffer-live-p object)
1177 (kill-buffer object)))))
1178 (mm-handle-set-undisplayer handle nil))))
1179
1180 (defun mm-display-inline (handle)
1181 (let* ((type (mm-handle-media-type handle))
1182 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1183 (funcall function handle)
1184 (goto-char (point-min))))
1185
1186 (defun mm-assoc-string-match (alist type)
1187 (dolist (elem alist)
1188 (when (string-match (car elem) type)
1189 (return elem))))
1190
1191 (defun mm-automatic-display-p (handle)
1192 "Say whether the user wants HANDLE to be displayed automatically."
1193 (let ((methods mm-automatic-display)
1194 (type (mm-handle-media-type handle))
1195 method result)
1196 (while (setq method (pop methods))
1197 (when (and (not (mm-inline-override-p handle))
1198 (string-match method type))
1199 (setq result t
1200 methods nil)))
1201 result))
1202
1203 (defun mm-inlinable-p (handle &optional type)
1204 "Say whether HANDLE can be displayed inline.
1205 TYPE is the mime-type of the object; it defaults to the one given
1206 in HANDLE."
1207 (unless type (setq type (mm-handle-media-type handle)))
1208 (let ((alist mm-inline-media-tests)
1209 test)
1210 (while alist
1211 (when (string-match (caar alist) type)
1212 (setq test (caddar alist)
1213 alist nil)
1214 (setq test (funcall test handle)))
1215 (pop alist))
1216 test))
1217
1218 (defun mm-inlined-p (handle)
1219 "Say whether the user wants HANDLE to be displayed inline."
1220 (let ((methods mm-inlined-types)
1221 (type (mm-handle-media-type handle))
1222 method result)
1223 (while (setq method (pop methods))
1224 (when (and (not (mm-inline-override-p handle))
1225 (string-match method type))
1226 (setq result t
1227 methods nil)))
1228 result))
1229
1230 (defun mm-attachment-override-p (handle)
1231 "Say whether HANDLE should have attachment behavior overridden."
1232 (let ((types mm-attachment-override-types)
1233 (type (mm-handle-media-type handle))
1234 ty)
1235 (catch 'found
1236 (while (setq ty (pop types))
1237 (when (and (string-match ty type)
1238 (mm-inlinable-p handle))
1239 (throw 'found t))))))
1240
1241 (defun mm-inline-override-p (handle)
1242 "Say whether HANDLE should have inline behavior overridden."
1243 (let ((types mm-inline-override-types)
1244 (type (mm-handle-media-type handle))
1245 ty)
1246 (catch 'found
1247 (while (setq ty (pop types))
1248 (when (string-match ty type)
1249 (throw 'found t))))))
1250
1251 (defun mm-automatic-external-display-p (type)
1252 "Return the user-defined method for TYPE."
1253 (let ((methods mm-automatic-external-display)
1254 method result)
1255 (while (setq method (pop methods))
1256 (when (string-match method type)
1257 (setq result t
1258 methods nil)))
1259 result))
1260
1261 (defun mm-destroy-part (handle)
1262 "Destroy the data structures connected to HANDLE."
1263 (when (listp handle)
1264 (mm-remove-part handle)
1265 (when (buffer-live-p (mm-handle-buffer handle))
1266 (kill-buffer (mm-handle-buffer handle)))))
1267
1268 (defun mm-handle-displayed-p (handle)
1269 "Say whether HANDLE is displayed or not."
1270 (mm-handle-undisplayer handle))
1271
1272 ;;;
1273 ;;; Functions for outputting parts
1274 ;;;
1275
1276 (defmacro mm-with-part (handle &rest forms)
1277 "Run FORMS in the temp buffer containing the contents of HANDLE."
1278 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1279 ;; chars, so the buffer should be unibyte. It may happen that the
1280 ;; handle-buffer is multibyte for some reason, in which case now is a good
1281 ;; time to adjust it, since we know at this point that it should
1282 ;; be unibyte.
1283 `(let* ((handle ,handle))
1284 (when (and (mm-handle-buffer handle)
1285 (buffer-name (mm-handle-buffer handle)))
1286 (with-temp-buffer
1287 (mm-disable-multibyte)
1288 (insert-buffer-substring (mm-handle-buffer handle))
1289 (mm-decode-content-transfer-encoding
1290 (mm-handle-encoding handle)
1291 (mm-handle-media-type handle))
1292 ,@forms))))
1293 (put 'mm-with-part 'lisp-indent-function 1)
1294 (put 'mm-with-part 'edebug-form-spec '(body))
1295
1296 (defun mm-get-part (handle &optional no-cache)
1297 "Return the contents of HANDLE as a string.
1298 If NO-CACHE is non-nil, cached contents of a message/external-body part
1299 are ignored."
1300 (if (and (not no-cache)
1301 (equal (mm-handle-media-type handle) "message/external-body"))
1302 (progn
1303 (unless (mm-handle-cache handle)
1304 (mm-extern-cache-contents handle))
1305 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1306 (buffer-string)))
1307 (mm-with-part handle
1308 (buffer-string))))
1309
1310 (defun mm-insert-part (handle &optional no-cache)
1311 "Insert the contents of HANDLE in the current buffer.
1312 If NO-CACHE is non-nil, cached contents of a message/external-body part
1313 are ignored."
1314 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1315 'charset)
1316 'gnus-decoded)
1317 (with-current-buffer (mm-handle-buffer handle)
1318 (buffer-string)))
1319 ((mm-multibyte-p)
1320 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1321 (t
1322 (mm-get-part handle no-cache)))))
1323 (save-restriction
1324 (widen)
1325 (goto-char
1326 (prog1
1327 (point)
1328 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1329 'mm-uu-extract)
1330 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1331 ;; Separate the extracted parts that have the same faces.
1332 (insert "\n" text)
1333 (insert text)))))))
1334
1335 (defun mm-file-name-delete-whitespace (file-name)
1336 "Remove all whitespace characters from FILE-NAME."
1337 (while (string-match "\\s-+" file-name)
1338 (setq file-name (replace-match "" t t file-name)))
1339 file-name)
1340
1341 (defun mm-file-name-trim-whitespace (file-name)
1342 "Remove leading and trailing whitespace characters from FILE-NAME."
1343 (when (string-match "\\`\\s-+" file-name)
1344 (setq file-name (substring file-name (match-end 0))))
1345 (when (string-match "\\s-+\\'" file-name)
1346 (setq file-name (substring file-name 0 (match-beginning 0))))
1347 file-name)
1348
1349 (defun mm-file-name-collapse-whitespace (file-name)
1350 "Collapse multiple whitespace characters in FILE-NAME."
1351 (while (string-match "\\s-\\s-+" file-name)
1352 (setq file-name (replace-match " " t t file-name)))
1353 file-name)
1354
1355 (defun mm-file-name-replace-whitespace (file-name)
1356 "Replace whitespace characters in FILE-NAME with underscores.
1357 Set the option `mm-file-name-replace-whitespace' to any other
1358 string if you do not like underscores."
1359 (let ((s (or mm-file-name-replace-whitespace "_")))
1360 (while (string-match "\\s-" file-name)
1361 (setq file-name (replace-match s t t file-name))))
1362 file-name)
1363
1364 (defun mm-file-name-delete-control (filename)
1365 "Delete control characters from FILENAME."
1366 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1367
1368 (defun mm-file-name-delete-gotchas (filename)
1369 "Delete shell gotchas from FILENAME."
1370 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1371 (gnus-replace-in-string filename "^[.-]+" ""))
1372
1373 (defun mm-save-part (handle &optional prompt)
1374 "Write HANDLE to a file.
1375 PROMPT overrides the default one used to ask user for a file name."
1376 (let ((filename (or (mail-content-type-get
1377 (mm-handle-disposition handle) 'filename)
1378 (mail-content-type-get
1379 (mm-handle-type handle) 'name)))
1380 file)
1381 (when filename
1382 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1383 (file-name-nondirectory filename))))
1384 (while
1385 (progn
1386 (setq file
1387 (read-file-name
1388 (or prompt
1389 (format "Save MIME part to (default %s): "
1390 (or filename "")))
1391 (or mm-default-directory default-directory)
1392 (expand-file-name (or filename "")
1393 (or mm-default-directory default-directory))))
1394 (cond ((or (not file) (equal file ""))
1395 (message "Please enter a file name")
1396 t)
1397 ((and (file-directory-p file)
1398 (not filename))
1399 (message "Please enter a non-directory file name")
1400 t)
1401 (t nil)))
1402 (sit-for 2)
1403 (discard-input))
1404 (if (file-directory-p file)
1405 (setq file (expand-file-name filename file))
1406 (setq file (expand-file-name
1407 file (or mm-default-directory default-directory))))
1408 (setq mm-default-directory (file-name-directory file))
1409 (and (or (not (file-exists-p file))
1410 (yes-or-no-p (format "File %s already exists; overwrite? "
1411 file)))
1412 (progn
1413 (mm-save-part-to-file handle file)
1414 file))))
1415
1416 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1417 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1418 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1419 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1420 Return t if meta tag is added or replaced."
1421 (when (equal (mm-handle-media-type handle) "text/html")
1422 (when (or charset
1423 (setq charset (mail-content-type-get (mm-handle-type handle)
1424 'charset)))
1425 (setq charset (format "\
1426 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1427 (let ((case-fold-search t))
1428 (goto-char (point-min))
1429 (if (re-search-forward "\
1430 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1431 text/\\(\\sw+\\)\\(?:;\\s-*charset=\\([^\"'>]+\\)\\)?[^>]*>" nil t)
1432 (if (and (not force-charset)
1433 (match-beginning 2)
1434 (string-match "\\`html\\'" (match-string 1)))
1435 ;; Don't modify existing meta tag.
1436 nil
1437 ;; Replace it with the one specifying charset.
1438 (replace-match charset)
1439 t)
1440 (if (re-search-forward "<head>\\s-*" nil t)
1441 (insert charset "\n")
1442 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1443 (insert "<head>\n" charset "\n</head>\n"))
1444 t)))))
1445
1446 (defun mm-save-part-to-file (handle file)
1447 (mm-with-unibyte-buffer
1448 (mm-insert-part handle)
1449 (mm-add-meta-html-tag handle)
1450 (let ((current-file-modes (default-file-modes)))
1451 (set-default-file-modes mm-attachment-file-modes)
1452 (unwind-protect
1453 ;; Don't re-compress .gz & al. Arguably we should make
1454 ;; `file-name-handler-alist' nil, but that would chop
1455 ;; ange-ftp, which is reasonable to use here.
1456 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1457 (set-default-file-modes current-file-modes)))))
1458
1459 (defun mm-pipe-part (handle &optional cmd)
1460 "Pipe HANDLE to a process.
1461 Use CMD as the process."
1462 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1463 (command (or cmd
1464 (gnus-read-shell-command
1465 "Shell command on MIME part: " mm-last-shell-command))))
1466 (mm-with-unibyte-buffer
1467 (mm-insert-part handle)
1468 (mm-add-meta-html-tag handle)
1469 (let ((coding-system-for-write 'binary))
1470 (shell-command-on-region (point-min) (point-max) command nil)))))
1471
1472 (autoload 'gnus-completing-read "gnus-util")
1473
1474 (defun mm-interactively-view-part (handle)
1475 "Display HANDLE using METHOD."
1476 (let* ((type (mm-handle-media-type handle))
1477 (methods
1478 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1479 (mailcap-mime-info type 'all)))
1480 (method (let ((minibuffer-local-completion-map
1481 mm-viewer-completion-map))
1482 (completing-read "Viewer: " methods))))
1483 (when (string= method "")
1484 (error "No method given"))
1485 (if (string-match "^[^% \t]+$" method)
1486 (setq method (concat method " %s")))
1487 (mm-display-external handle method)))
1488
1489 (defun mm-preferred-alternative (handles &optional preferred)
1490 "Say which of HANDLES are preferred."
1491 (let ((prec (if preferred (list preferred)
1492 (mm-preferred-alternative-precedence handles)))
1493 p h result type handle)
1494 (while (setq p (pop prec))
1495 (setq h handles)
1496 (while h
1497 (setq handle (car h))
1498 (setq type (mm-handle-media-type handle))
1499 (when (and (equal p type)
1500 (mm-automatic-display-p handle)
1501 (or (stringp (car handle))
1502 (not (mm-handle-disposition handle))
1503 (equal (car (mm-handle-disposition handle))
1504 "inline")))
1505 (setq result handle
1506 h nil
1507 prec nil))
1508 (pop h)))
1509 result))
1510
1511 (defun mm-preferred-alternative-precedence (handles)
1512 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1513 (setq handles (reverse handles))
1514 (dolist (disc (reverse mm-discouraged-alternatives))
1515 (dolist (handle (copy-sequence handles))
1516 (when (string-match disc (mm-handle-media-type handle))
1517 (setq handles (nconc (delete handle handles) (list handle))))))
1518 ;; Remove empty parts.
1519 (dolist (handle (copy-sequence handles))
1520 (when (and (bufferp (mm-handle-buffer handle))
1521 (not (with-current-buffer (mm-handle-buffer handle)
1522 (goto-char (point-min))
1523 (re-search-forward "[^ \t\n]" nil t))))
1524 (setq handles (nconc (delete handle handles) (list handle)))))
1525 (mapcar #'mm-handle-media-type handles))
1526
1527 (defun mm-get-content-id (id)
1528 "Return the handle(s) referred to by ID."
1529 (cdr (assoc id mm-content-id-alist)))
1530
1531 (defconst mm-image-type-regexps
1532 '(("/\\*.*XPM.\\*/" . xpm)
1533 ("P[1-6]" . pbm)
1534 ("GIF8" . gif)
1535 ("\377\330" . jpeg)
1536 ("\211PNG\r\n" . png)
1537 ("#define" . xbm)
1538 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1539 ("%!PS" . postscript))
1540 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1541 When the first bytes of an image file match REGEXP, it is assumed to
1542 be of image type IMAGE-TYPE.")
1543
1544 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1545 (defun mm-image-type-from-buffer ()
1546 "Determine the image type from data in the current buffer.
1547 Value is a symbol specifying the image type or nil if type cannot
1548 be determined."
1549 (let ((types mm-image-type-regexps)
1550 type)
1551 (goto-char (point-min))
1552 (while (and types (null type))
1553 (let ((regexp (car (car types)))
1554 (image-type (cdr (car types))))
1555 (when (looking-at regexp)
1556 (setq type image-type))
1557 (setq types (cdr types))))
1558 type))
1559
1560 (defun mm-get-image (handle)
1561 "Return an image instance based on HANDLE."
1562 (let ((type (mm-handle-media-subtype handle))
1563 spec)
1564 ;; Allow some common translations.
1565 (setq type
1566 (cond
1567 ((equal type "x-pixmap")
1568 "xpm")
1569 ((equal type "x-xbitmap")
1570 "xbm")
1571 ((equal type "x-portable-bitmap")
1572 "pbm")
1573 (t type)))
1574 (or (mm-handle-cache handle)
1575 (mm-with-unibyte-buffer
1576 (mm-insert-part handle)
1577 (prog1
1578 (setq spec
1579 (ignore-errors
1580 ;; Avoid testing `make-glyph' since W3 may define
1581 ;; a bogus version of it.
1582 (if (fboundp 'create-image)
1583 (create-image (buffer-string)
1584 (or (mm-image-type-from-buffer)
1585 (intern type))
1586 'data-p)
1587 (mm-create-image-xemacs type))))
1588 (mm-handle-set-cache handle spec))))))
1589
1590 (defun mm-create-image-xemacs (type)
1591 (when (featurep 'xemacs)
1592 (cond
1593 ((equal type "xbm")
1594 ;; xbm images require special handling, since
1595 ;; the only way to create glyphs from these
1596 ;; (without a ton of work) is to write them
1597 ;; out to a file, and then create a file
1598 ;; specifier.
1599 (let ((file (mm-make-temp-file
1600 (expand-file-name "emm" mm-tmp-directory)
1601 nil ".xbm")))
1602 (unwind-protect
1603 (progn
1604 (write-region (point-min) (point-max) file)
1605 (make-glyph (list (cons 'x file))))
1606 (ignore-errors
1607 (delete-file file)))))
1608 (t
1609 (make-glyph
1610 (vector
1611 (or (mm-image-type-from-buffer)
1612 (intern type))
1613 :data (buffer-string)))))))
1614
1615 (declare-function image-size "image.c" (spec &optional pixels frame))
1616
1617 (defun mm-image-fit-p (handle)
1618 "Say whether the image in HANDLE will fit the current window."
1619 (let ((image (mm-get-image handle)))
1620 (or (not image)
1621 (if (featurep 'xemacs)
1622 ;; XEmacs's glyphs can actually tell us about their width, so
1623 ;; let's be nice and smart about them.
1624 (or mm-inline-large-images
1625 (and (<= (glyph-width image) (window-pixel-width))
1626 (<= (glyph-height image) (window-pixel-height))))
1627 (let* ((size (image-size image))
1628 (w (car size))
1629 (h (cdr size)))
1630 (or mm-inline-large-images
1631 (and (<= h (1- (window-height))) ; Don't include mode line.
1632 (<= w (window-width)))))))))
1633
1634 (defun mm-valid-image-format-p (format)
1635 "Say whether FORMAT can be displayed natively by Emacs."
1636 (cond
1637 ;; Handle XEmacs
1638 ((fboundp 'valid-image-instantiator-format-p)
1639 (valid-image-instantiator-format-p format))
1640 ;; Handle Emacs
1641 ((fboundp 'image-type-available-p)
1642 (and (display-graphic-p)
1643 (image-type-available-p format)))
1644 ;; Nobody else can do images yet.
1645 (t
1646 nil)))
1647
1648 (defun mm-valid-and-fit-image-p (format handle)
1649 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1650 (and (mm-valid-image-format-p format)
1651 (mm-image-fit-p handle)))
1652
1653 (defun mm-find-part-by-type (handles type &optional notp recursive)
1654 "Search in HANDLES for part with TYPE.
1655 If NOTP, returns first non-matching part.
1656 If RECURSIVE, search recursively."
1657 (let (handle)
1658 (while handles
1659 (if (and recursive (stringp (caar handles)))
1660 (if (setq handle (mm-find-part-by-type (cdar handles) type
1661 notp recursive))
1662 (setq handles nil))
1663 (if (if notp
1664 (not (equal (mm-handle-media-type (car handles)) type))
1665 (equal (mm-handle-media-type (car handles)) type))
1666 (setq handle (car handles)
1667 handles nil)))
1668 (setq handles (cdr handles)))
1669 handle))
1670
1671 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1672 (goto-char (point-min))
1673 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1674 'boundary)))
1675 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1676 start
1677 (end (save-excursion
1678 (goto-char (point-max))
1679 (if (re-search-backward close-delimiter nil t)
1680 (match-beginning 0)
1681 (point-max))))
1682 result)
1683 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1684 (while (and (not result)
1685 (re-search-forward boundary end t))
1686 (goto-char (match-beginning 0))
1687 (when start
1688 (save-excursion
1689 (save-restriction
1690 (narrow-to-region start (1- (point)))
1691 (when (let* ((ct (mail-fetch-field "content-type"))
1692 (ctl (and ct (mail-header-parse-content-type ct))))
1693 (if notp
1694 (not (equal (car ctl) type))
1695 (equal (car ctl) type)))
1696 (setq result (buffer-string))))))
1697 (forward-line 1)
1698 (setq start (point)))
1699 (when (and (not result) start)
1700 (save-excursion
1701 (save-restriction
1702 (narrow-to-region start end)
1703 (when (let* ((ct (mail-fetch-field "content-type"))
1704 (ctl (and ct (mail-header-parse-content-type ct))))
1705 (if notp
1706 (not (equal (car ctl) type))
1707 (equal (car ctl) type)))
1708 (setq result (buffer-string))))))
1709 result))
1710
1711 (defvar mm-security-handle nil)
1712
1713 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1714 ;; HANDLE could be a CTL.
1715 (when handle
1716 (put-text-property 0 (length (car handle)) parameter value
1717 (car handle))))
1718
1719 (autoload 'mm-view-pkcs7 "mm-view")
1720
1721 (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
1722 (let ((type (car ctl))
1723 (subtype (cadr (split-string (car ctl) "/")))
1724 (mm-security-handle ctl) ;; (car CTL) is the type.
1725 protocol func functest)
1726 (cond
1727 ((or (equal type "application/x-pkcs7-mime")
1728 (equal type "application/pkcs7-mime"))
1729 (with-temp-buffer
1730 (when (and (cond
1731 ((eq mm-decrypt-option 'never) nil)
1732 ((eq mm-decrypt-option 'always) t)
1733 ((eq mm-decrypt-option 'known) t)
1734 (t (y-or-n-p
1735 (format "Decrypt (S/MIME) part? "))))
1736 (mm-view-pkcs7 parts from))
1737 (setq parts (mm-dissect-buffer t)))))
1738 ((equal subtype "signed")
1739 (unless (and (setq protocol
1740 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1741 (not (equal protocol "multipart/mixed")))
1742 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1743 (let ((protocols mm-verify-function-alist))
1744 (while protocols
1745 (if (and (or (not (setq functest (nth 3 (car protocols))))
1746 (funcall functest parts ctl))
1747 (mm-find-part-by-type parts (caar protocols) nil t))
1748 (setq protocol (caar protocols)
1749 protocols nil)
1750 (setq protocols (cdr protocols))))))
1751 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1752 (when (cond
1753 ((eq mm-verify-option 'never) nil)
1754 ((eq mm-verify-option 'always) t)
1755 ((eq mm-verify-option 'known)
1756 (and func
1757 (or (not (setq functest
1758 (nth 3 (assoc protocol
1759 mm-verify-function-alist))))
1760 (funcall functest parts ctl))))
1761 (t
1762 (y-or-n-p
1763 (format "Verify signed (%s) part? "
1764 (or (nth 2 (assoc protocol mm-verify-function-alist))
1765 (format "protocol=%s" protocol))))))
1766 (save-excursion
1767 (if func
1768 (setq parts (funcall func parts ctl))
1769 (mm-set-handle-multipart-parameter
1770 mm-security-handle 'gnus-details
1771 (format "Unknown sign protocol (%s)" protocol))))))
1772 ((equal subtype "encrypted")
1773 (unless (setq protocol
1774 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1775 ;; The message is broken.
1776 (let ((parts parts))
1777 (while parts
1778 (if (assoc (mm-handle-media-type (car parts))
1779 mm-decrypt-function-alist)
1780 (setq protocol (mm-handle-media-type (car parts))
1781 parts nil)
1782 (setq parts (cdr parts))))))
1783 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1784 (when (cond
1785 ((eq mm-decrypt-option 'never) nil)
1786 ((eq mm-decrypt-option 'always) t)
1787 ((eq mm-decrypt-option 'known)
1788 (and func
1789 (or (not (setq functest
1790 (nth 3 (assoc protocol
1791 mm-decrypt-function-alist))))
1792 (funcall functest parts ctl))))
1793 (t
1794 (y-or-n-p
1795 (format "Decrypt (%s) part? "
1796 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1797 (format "protocol=%s" protocol))))))
1798 (save-excursion
1799 (if func
1800 (setq parts (funcall func parts ctl))
1801 (mm-set-handle-multipart-parameter
1802 mm-security-handle 'gnus-details
1803 (format "Unknown encrypt protocol (%s)" protocol))))))
1804 (t nil))
1805 parts))
1806
1807 (defun mm-multiple-handles (handles)
1808 (and (listp handles)
1809 (> (length handles) 1)
1810 (or (listp (car handles))
1811 (stringp (car handles)))))
1812
1813 (defun mm-complicated-handles (handles)
1814 (and (listp (car handles))
1815 (> (length handles) 1)))
1816
1817 (defun mm-merge-handles (handles1 handles2)
1818 (append
1819 (if (listp (car handles1))
1820 handles1
1821 (list handles1))
1822 (if (listp (car handles2))
1823 handles2
1824 (list handles2))))
1825
1826 (defun mm-readable-p (handle)
1827 "Say whether the content of HANDLE is readable."
1828 (and (< (with-current-buffer (mm-handle-buffer handle)
1829 (buffer-size)) 10000)
1830 (mm-with-unibyte-buffer
1831 (mm-insert-part handle)
1832 (and (eq (mm-body-7-or-8) '7bit)
1833 (not (mm-long-lines-p 76))))))
1834
1835 (declare-function libxml-parse-html-region "xml.c"
1836 (start end &optional base-url discard-comments))
1837 (declare-function shr-insert-document "shr" (dom))
1838 (defvar shr-blocked-images)
1839 (defvar shr-use-fonts)
1840
1841 (defun mm-shr (handle)
1842 ;; Require since we bind its variables.
1843 (require 'shr)
1844 (let ((shr-width (if (and (boundp 'shr-use-fonts)
1845 shr-use-fonts)
1846 nil
1847 fill-column))
1848 (shr-content-function (lambda (id)
1849 (let ((handle (mm-get-content-id id)))
1850 (when handle
1851 (mm-with-part handle
1852 (buffer-string))))))
1853 (shr-inhibit-images mm-html-inhibit-images)
1854 (shr-blocked-images mm-html-blocked-images)
1855 charset char)
1856 (unless handle
1857 (setq handle (mm-dissect-buffer t)))
1858 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1859 (save-restriction
1860 (narrow-to-region (point) (point))
1861 (shr-insert-document
1862 (mm-with-part handle
1863 (insert (prog1
1864 (if (and charset
1865 (setq charset
1866 (mm-charset-to-coding-system charset
1867 nil t))
1868 (not (eq charset 'ascii)))
1869 (mm-decode-coding-string (buffer-string) charset)
1870 (mm-string-as-multibyte (buffer-string)))
1871 (erase-buffer)
1872 (mm-enable-multibyte)))
1873 (goto-char (point-min))
1874 (setq case-fold-search t)
1875 (while (re-search-forward
1876 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1877 (when (setq char
1878 (cdr (assq (if (match-beginning 1)
1879 (string-to-number (match-string 1) 16)
1880 (string-to-number (match-string 2)))
1881 mm-extra-numeric-entities)))
1882 (replace-match (char-to-string char))))
1883 ;; Remove "soft hyphens".
1884 (goto-char (point-min))
1885 (while (search-forward "­" nil t)
1886 (replace-match "" t t))
1887 (libxml-parse-html-region (point-min) (point-max))))
1888 (unless (bobp)
1889 (insert "\n"))
1890 (mm-convert-shr-links)
1891 (mm-handle-set-undisplayer
1892 handle
1893 `(lambda ()
1894 (let ((inhibit-read-only t))
1895 (delete-region ,(point-min-marker)
1896 ,(point-max-marker))))))))
1897
1898 (defvar shr-map)
1899
1900 (autoload 'widget-convert-button "wid-edit")
1901
1902 (defun mm-convert-shr-links ()
1903 (let ((start (point-min))
1904 end)
1905 (while (and start
1906 (< start (point-max)))
1907 (when (setq start (text-property-not-all start (point-max) 'shr-url nil))
1908 (setq end (next-single-property-change start 'shr-url nil (point-max)))
1909 (widget-convert-button
1910 'url-link start end
1911 :help-echo (get-text-property start 'help-echo)
1912 :keymap shr-map
1913 (get-text-property start 'shr-url))
1914 (put-text-property start end 'local-map nil)
1915 (dolist (overlay (overlays-at start))
1916 (overlay-put overlay 'face nil))
1917 (setq start end)))))
1918
1919 (defun mm-handle-filename (handle)
1920 "Return filename of HANDLE if any."
1921 (or (mail-content-type-get (mm-handle-type handle)
1922 'name)
1923 (mail-content-type-get (mm-handle-disposition handle)
1924 'filename)))
1925
1926 (provide 'mm-decode)
1927
1928 ;; Local Variables:
1929 ;; coding: utf-8
1930 ;; End:
1931
1932 ;;; mm-decode.el ends here