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