1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; For Emacs <22.2 and XEmacs.
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile (require 'cl)
36 (autoload 'gnus-map-function "gnus-util")
37 (autoload 'gnus-replace-in-string "gnus-util")
38 (autoload 'gnus-read-shell-command "gnus-util")
40 (autoload 'mm-inline-partial "mm-partial")
41 (autoload 'mm-inline-external-body "mm-extern")
42 (autoload 'mm-extern-cache-contents "mm-extern")
43 (autoload 'mm-insert-inline "mm-view")
45 (defvar gnus-current-window-configuration)
47 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
49 (defgroup mime-display ()
50 "Display of MIME in mail and news articles."
51 :link '(custom-manual "(emacs-mime)Display Customization")
57 (defgroup mime-security ()
58 "MIME security in mail and news articles."
59 :link '(custom-manual "(emacs-mime)Display Customization")
64 ;;; Convenience macros.
66 (defmacro mm-handle-buffer (handle)
68 (defmacro mm-handle-type (handle)
70 (defsubst mm-handle-media-type (handle)
71 (if (stringp (car handle))
73 (car (mm-handle-type handle))))
74 (defsubst mm-handle-media-supertype (handle)
75 (car (split-string (mm-handle-media-type handle) "/")))
76 (defsubst mm-handle-media-subtype (handle)
77 (cadr (split-string (mm-handle-media-type handle) "/")))
78 (defmacro mm-handle-encoding (handle)
80 (defmacro mm-handle-undisplayer (handle)
82 (defmacro mm-handle-set-undisplayer (handle function)
83 `(setcar (nthcdr 3 ,handle) ,function))
84 (defmacro mm-handle-disposition (handle)
86 (defmacro mm-handle-description (handle)
88 (defmacro mm-handle-cache (handle)
90 (defmacro mm-handle-set-cache (handle contents)
91 `(setcar (nthcdr 6 ,handle) ,contents))
92 (defmacro mm-handle-id (handle)
94 (defmacro mm-handle-multipart-original-buffer (handle)
95 `(get-text-property 0 'buffer (car ,handle)))
96 (defmacro mm-handle-multipart-from (handle)
97 `(get-text-property 0 'from (car ,handle)))
98 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
99 `(get-text-property 0 ,parameter (car ,handle)))
101 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
102 disposition description cache
104 `(list ,buffer ,type ,encoding ,undisplayer
105 ,disposition ,description ,cache ,id))
107 (defcustom mm-text-html-renderer
108 (cond ((fboundp 'libxml-parse-html-region) 'shr)
109 ((executable-find "w3m") 'gnus-w3m)
110 ((executable-find "links") 'links)
111 ((executable-find "lynx") 'lynx)
112 ((locate-library "w3") 'w3)
113 ((locate-library "html2text") 'html2text)
115 "Render of HTML contents.
116 It is one of defined renderer types, or a rendering function.
117 The defined renderer types are:
118 `shr': use Gnus simple HTML renderer;
119 `gnus-w3m' : use Gnus renderer based on w3m;
120 `w3m' : use emacs-w3m;
121 `w3m-standalone': use w3m;
125 `html2text' : use html2text;
126 nil : use external viewer (default web browser)."
128 :type '(choice (const shr)
131 (const w3m :tag "emacs-w3m")
132 (const w3m-standalone :tag "standalone w3m" )
136 (const nil :tag "External viewer")
138 :group 'mime-display)
140 (defcustom mm-inline-text-html-with-images nil
141 "If non-nil, Gnus will allow retrieving images in HTML contents with
142 the <img> tags. It has no effect on Emacs/w3. See also the
143 documentation for the `mm-w3m-safe-url-regexp' variable."
146 :group 'mime-display)
148 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
149 "Regexp matching URLs which are considered to be safe.
150 Some HTML mails might contain a nasty trick used by spammers, using
151 the <img> tag which is far more evil than the [Click Here!] button.
152 It is most likely intended to check whether the ominous spam mail has
153 reached your eyes or not, in which case the spammer knows for sure
154 that your email address is valid. It is done by embedding an
155 identifier string into a URL that you might automatically retrieve
156 when displaying the image. The default value is \"\\\\`cid:\" which only
157 matches parts embedded to the Multipart/Related type MIME contents and
158 Gnus will never connect to the spammer's site arbitrarily. You may
159 set this variable to nil if you consider all urls to be safe."
161 :type '(choice (regexp :tag "Regexp")
162 (const :tag "All URLs are safe" nil))
163 :group 'mime-display)
165 (defcustom mm-inline-text-html-with-w3m-keymap t
166 "If non-nil, use emacs-w3m command keys in the article buffer."
169 :group 'mime-display)
171 (defcustom mm-enable-external t
172 "Indicate whether external MIME handlers should be used.
174 If t, all defined external MIME handlers are used. If nil, files are saved by
175 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
176 before the external MIME handler is invoked."
178 :type '(choice (const :tag "Always" t)
179 (const :tag "Never" nil)
180 (const :tag "Ask" ask))
181 :group 'mime-display)
183 (defcustom mm-inline-media-tests
187 (mm-valid-and-fit-image-p 'jpeg handle)))
191 (mm-valid-and-fit-image-p 'png handle)))
195 (mm-valid-and-fit-image-p 'gif handle)))
199 (mm-valid-and-fit-image-p 'tiff handle)) )
203 (mm-valid-and-fit-image-p 'xbm handle)))
207 (mm-valid-and-fit-image-p 'xbm handle)))
211 (mm-valid-and-fit-image-p 'xpm handle)))
215 (mm-valid-and-fit-image-p 'xpm handle)))
219 (mm-valid-and-fit-image-p 'bmp handle)))
220 ("image/x-portable-bitmap"
223 (mm-valid-and-fit-image-p 'pbm handle)))
224 ("text/plain" mm-inline-text identity)
225 ("text/enriched" mm-inline-text identity)
226 ("text/richtext" mm-inline-text identity)
227 ("text/x-patch" mm-display-patch-inline
229 ;; If the diff-mode.el package is installed, the function is
230 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
231 ;; to cater to broken installations. OTOH checking the function
232 ;; makes it possible to install another package which provides an
233 ;; alternative implementation of diff-mode. --Stef
234 (fboundp 'diff-mode)))
235 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
236 ("text/x-diff" mm-display-patch-inline
237 (lambda (handle) (fboundp 'diff-mode)))
238 ("application/emacs-lisp" mm-display-elisp-inline identity)
239 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
240 ("text/dns" mm-display-dns-inline identity)
244 mm-text-html-renderer))
248 (or (featurep 'vcard)
249 (locate-library "vcard"))))
250 ("message/delivery-status" mm-inline-text identity)
251 ("message/rfc822" mm-inline-message identity)
252 ("message/partial" mm-inline-partial identity)
253 ("message/external-body" mm-inline-external-body identity)
254 ("text/.*" mm-inline-text identity)
255 ("audio/wav" mm-inline-audio
257 (and (or (featurep 'nas-sound) (featurep 'native-sound))
258 (device-sound-enabled-p))))
262 (and (or (featurep 'nas-sound) (featurep 'native-sound))
263 (device-sound-enabled-p))))
264 ("application/pgp-signature" ignore identity)
265 ("application/x-pkcs7-signature" ignore identity)
266 ("application/pkcs7-signature" ignore identity)
267 ("application/x-pkcs7-mime" ignore identity)
268 ("application/pkcs7-mime" ignore identity)
269 ("multipart/alternative" ignore identity)
270 ("multipart/mixed" ignore identity)
271 ("multipart/related" ignore identity)
272 ;; Disable audio and image
273 ("audio/.*" ignore ignore)
274 ("image/.*" ignore ignore)
275 ;; Default to displaying as text
276 (".*" mm-inline-text mm-readable-p))
277 "Alist of media types/tests saying whether types can be displayed inline."
278 :type '(repeat (list (regexp :tag "MIME type")
279 (function :tag "Display function")
280 (function :tag "Display test")))
281 :group 'mime-display)
283 (defcustom mm-inlined-types
284 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
285 "message/partial" "message/external-body" "application/emacs-lisp"
286 "application/x-emacs-lisp"
287 "application/pgp-signature" "application/x-pkcs7-signature"
288 "application/pkcs7-signature" "application/x-pkcs7-mime"
289 "application/pkcs7-mime"
290 ;; Mutt still uses this even though it has already been withdrawn.
292 "List of media types that are to be displayed inline.
293 See also `mm-inline-media-tests', which says how to display a media
295 :type '(repeat regexp)
296 :group 'mime-display)
298 (defcustom mm-keep-viewer-alive-types
299 '("application/postscript" "application/msword" "application/vnd.ms-excel"
300 "application/pdf" "application/x-dvi")
301 "List of media types for which the external viewer will not be killed
302 when selecting a different article."
304 :type '(repeat regexp)
305 :group 'mime-display)
307 (defcustom mm-automatic-display
308 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
309 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
310 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
311 "application/emacs-lisp" "application/x-emacs-lisp"
312 "application/x-pkcs7-signature"
313 "application/pkcs7-signature" "application/x-pkcs7-mime"
314 "application/pkcs7-mime"
315 ;; Mutt still uses this even though it has already been withdrawn.
316 "application/pgp\\'")
317 "A list of MIME types to be displayed automatically."
318 :type '(repeat regexp)
319 :group 'mime-display)
321 (defcustom mm-attachment-override-types '("text/x-vcard"
322 "application/pkcs7-mime"
323 "application/x-pkcs7-mime"
324 "application/pkcs7-signature"
325 "application/x-pkcs7-signature")
326 "Types to have \"attachment\" ignored if they can be displayed inline."
327 :type '(repeat regexp)
328 :group 'mime-display)
330 (defcustom mm-inline-override-types nil
331 "Types to be treated as attachments even if they can be displayed inline."
332 :type '(repeat regexp)
333 :group 'mime-display)
335 (defcustom mm-automatic-external-display nil
336 "List of MIME type regexps that will be displayed externally automatically."
337 :type '(repeat regexp)
338 :group 'mime-display)
340 (defcustom mm-discouraged-alternatives nil
341 "List of MIME types that are discouraged when viewing multipart/alternative.
342 Viewing agents are supposed to view the last possible part of a message,
343 as that is supposed to be the richest. However, users may prefer other
344 types instead, and this list says what types are most unwanted. If,
345 for instance, text/html parts are very unwanted, and text/richtext are
346 somewhat unwanted, then the value of this variable should be set
349 (\"text/html\" \"text/richtext\")
351 Adding \"image/.*\" might also be useful. Spammers use it as the
352 prefered part of multipart/alternative messages. See also
353 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
354 enables you to choose manually one of two types those mails include."
355 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
356 :group 'mime-display)
358 (defcustom mm-tmp-directory
359 (if (fboundp 'temp-directory)
361 (if (boundp 'temporary-file-directory)
362 temporary-file-directory
364 "Where mm will store its temporary files."
366 :group 'mime-display)
368 (defcustom mm-inline-large-images nil
369 "If t, then all images fit in the buffer.
370 If 'resize, try to resize the images so they fit."
372 (const :tag "Inline large images as they are." t)
373 (const :tag "Resize large images." resize)
374 (const :tag "Do not inline large images." nil))
375 :group 'mime-display)
377 (defcustom mm-file-name-rewrite-functions
378 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
379 "List of functions used for rewriting file names of MIME parts.
380 Each function takes a file name as input and returns a file name.
382 Ready-made functions include `mm-file-name-delete-control',
383 `mm-file-name-delete-gotchas' (you should not remove these two
384 functions), `mm-file-name-delete-whitespace',
385 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
386 `mm-file-name-replace-whitespace', `capitalize', `downcase',
387 `upcase', and `upcase-initials'."
388 :type '(list (set :inline t
389 (const mm-file-name-delete-control)
390 (const mm-file-name-delete-gotchas)
391 (const mm-file-name-delete-whitespace)
392 (const mm-file-name-trim-whitespace)
393 (const mm-file-name-collapse-whitespace)
394 (const mm-file-name-replace-whitespace)
398 (const upcase-initials)
402 :version "23.1" ;; No Gnus
403 :group 'mime-display)
406 (defvar mm-path-name-rewrite-functions nil
407 "*List of functions for rewriting the full file names of MIME parts.
408 This is used when viewing parts externally, and is meant for
409 transforming the absolute name so that non-compliant programs can find
410 the file where it's saved.
412 Each function takes a file name as input and returns a file name.")
414 (defvar mm-file-name-replace-whitespace nil
415 "String used for replacing whitespace characters; default is `\"_\"'.")
417 (defcustom mm-default-directory nil
418 "The default directory where mm will save files.
419 If not set, `default-directory' will be used."
420 :type '(choice directory (const :tag "Default" nil))
421 :group 'mime-display)
423 (defcustom mm-attachment-file-modes 384
424 "Set the mode bits of saved attachments to this integer."
427 :group 'mime-display)
429 (defcustom mm-external-terminal-program "xterm"
430 "The program to start an external terminal."
433 :group 'mime-display)
435 ;;; Internal variables.
437 (defvar mm-last-shell-command "")
438 (defvar mm-content-id-alist nil)
439 (defvar mm-postponed-undisplay-list nil)
441 ;; According to RFC2046, in particular, in a digest, the default
442 ;; Content-Type value for a body part is changed from "text/plain" to
444 (defvar mm-dissect-default-type "text/plain")
446 (autoload 'mml2015-verify "mml2015")
447 (autoload 'mml2015-verify-test "mml2015")
448 (autoload 'mml-smime-verify "mml-smime")
449 (autoload 'mml-smime-verify-test "mml-smime")
451 (defvar mm-verify-function-alist
452 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
453 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
454 mm-uu-pgp-signed-test)
455 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
456 mml-smime-verify-test)
457 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
458 mml-smime-verify-test)))
460 (defcustom mm-verify-option 'never
461 "Option of verifying signed parts.
462 `never', not verify; `always', always verify;
463 `known', only verify known protocols. Otherwise, ask user.
465 When set to `always' or `known', you should add
466 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
467 result of the verification."
469 :type '(choice (item always)
471 (item :tag "only known protocols" known)
472 (item :tag "ask" nil))
473 :group 'mime-security)
475 (autoload 'mml2015-decrypt "mml2015")
476 (autoload 'mml2015-decrypt-test "mml2015")
478 (defvar mm-decrypt-function-alist
479 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
480 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
481 mm-uu-pgp-encrypted-test)))
483 (defcustom mm-decrypt-option nil
484 "Option of decrypting encrypted parts.
485 `never', not decrypt; `always', always decrypt;
486 `known', only decrypt known protocols. Otherwise, ask user."
488 :type '(choice (item always)
490 (item :tag "only known protocols" known)
491 (item :tag "ask" nil))
492 :group 'mime-security)
494 (defvar mm-viewer-completion-map
495 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
496 (set-keymap-parent map minibuffer-local-completion-map)
497 ;; Should we bind other key to minibuffer-complete-word?
498 (define-key map " " 'self-insert-command)
500 "Keymap for input viewer with completion.")
502 (defvar mm-viewer-completion-map
503 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
504 (set-keymap-parent map minibuffer-local-completion-map)
505 ;; Should we bind other key to minibuffer-complete-word?
506 (define-key map " " 'self-insert-command)
508 "Keymap for input viewer with completion.")
512 (defun mm-alist-to-plist (alist)
513 "Convert association list ALIST into the equivalent property-list form.
514 The plist is returned. This converts from
516 \((a . 1) (b . 2) (c . 3))
522 The original alist is not modified. See also `destructive-alist-to-plist'."
525 (let ((el (car alist)))
526 (setq plist (cons (cdr el) (cons (car el) plist))))
527 (setq alist (cdr alist)))
530 (defun mm-keep-viewer-alive-p (handle)
531 "Say whether external viewer for HANDLE should stay alive."
532 (let ((types mm-keep-viewer-alive-types)
533 (type (mm-handle-media-type handle))
536 (while (setq ty (pop types))
537 (when (string-match ty type)
538 (throw 'found t))))))
540 (defun mm-handle-set-external-undisplayer (handle function)
541 "Set the undisplayer for HANDLE to FUNCTION.
542 Postpone undisplaying of viewers for types in
543 `mm-keep-viewer-alive-types'."
544 (if (mm-keep-viewer-alive-p handle)
545 (let ((new-handle (copy-sequence handle)))
546 (mm-handle-set-undisplayer new-handle function)
547 (mm-handle-set-undisplayer handle nil)
548 (push new-handle mm-postponed-undisplay-list))
549 (mm-handle-set-undisplayer handle function)))
551 (defun mm-destroy-postponed-undisplay-list ()
552 (when mm-postponed-undisplay-list
553 (message "Destroying external MIME viewers")
554 (mm-destroy-parts mm-postponed-undisplay-list)))
556 (autoload 'message-fetch-field "message")
558 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
559 "Dissect the current buffer and return a list of MIME handles."
561 (let (ct ctl type subtype cte cd description id result)
563 (mail-narrow-to-head)
564 (when (or no-strict-mime
566 (mail-fetch-field "mime-version"))
567 (setq ct (mail-fetch-field "content-type")
568 ctl (and ct (mail-header-parse-content-type ct))
569 cte (mail-fetch-field "content-transfer-encoding")
570 cd (mail-fetch-field "content-disposition")
571 ;; Newlines in description should be stripped so as
572 ;; not to break the MIME tag into two or more lines.
573 description (message-fetch-field "content-description")
574 id (mail-fetch-field "content-id"))
576 (setq from (mail-fetch-field "from")))
577 ;; FIXME: In some circumstances, this code is running within
578 ;; an unibyte macro. mail-extract-address-components
579 ;; creates unibyte buffers. This `if', though not a perfect
580 ;; solution, avoids most of them.
582 (setq from (cadr (mail-extract-address-components from))))
584 (setq description (mail-decode-encoded-word-string
587 (not (string-match "/" (car ctl))))
588 (mm-dissect-singlepart
589 (list mm-dissect-default-type)
590 (and cte (intern (downcase (mail-header-strip cte))))
592 (and cd (mail-header-parse-content-disposition cd))
594 (setq type (split-string (car ctl) "/"))
595 (setq subtype (cadr type)
600 ((equal type "multipart")
601 (let ((mm-dissect-default-type (if (equal subtype "digest")
604 (start (cdr (assq 'start (cdr ctl)))))
605 (add-text-properties 0 (length (car ctl))
606 (mm-alist-to-plist (cdr ctl)) (car ctl))
608 ;; what really needs to be done here is a way to link a
609 ;; MIME handle back to it's parent MIME handle (in a multilevel
610 ;; MIME article). That would probably require changing
611 ;; the mm-handle API so we simply store the multipart buffer
612 ;; name as a text property of the "multipart/whatever" string.
613 (add-text-properties 0 (length (car ctl))
614 (list 'buffer (mm-copy-to-buffer)
618 (cons (car ctl) (mm-dissect-multipart ctl from))))
620 (mm-possibly-verify-or-decrypt
621 (mm-dissect-singlepart
623 (and cte (intern (downcase (mail-header-strip cte))))
625 (and cd (mail-header-parse-content-disposition cd))
629 (when (string-match " *<\\(.*\\)> *" id)
630 (setq id (match-string 1 id)))
631 (push (cons id result) mm-content-id-alist))
634 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
636 (if (equal "text/plain" (car ctl))
640 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
642 (defun mm-dissect-multipart (ctl from)
643 (goto-char (point-min))
644 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
645 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
648 (goto-char (point-max))
649 (if (re-search-backward close-delimiter nil t)
652 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
653 (while (and (< (point) end) (re-search-forward boundary end t))
654 (goto-char (match-beginning 0))
658 (narrow-to-region start (point))
659 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
661 (or (looking-at boundary)
663 (setq start (point)))
664 (when (and start (< start end))
667 (narrow-to-region start end)
668 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
669 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
671 (defun mm-copy-to-buffer ()
672 "Copy the contents of the current buffer to a fresh buffer."
673 (let ((obuf (current-buffer))
674 (mb (mm-multibyte-p))
676 (goto-char (point-min))
677 (search-forward-regexp "^\n" nil t)
680 (generate-new-buffer " *mm*")
681 ;; Preserve the data's unibyteness (for url-insert-file-contents).
682 (mm-set-buffer-multibyte mb)
683 (insert-buffer-substring obuf beg)
686 (defun mm-display-parts (handle &optional no-default)
687 (if (stringp (car handle))
688 (mapcar 'mm-display-parts (cdr handle))
689 (if (bufferp (car handle))
691 (narrow-to-region (point) (point))
692 (mm-display-part handle)
693 (goto-char (point-max)))
694 (mapcar 'mm-display-parts handle))))
696 (autoload 'mailcap-parse-mailcaps "mailcap")
697 (autoload 'mailcap-mime-info "mailcap")
699 (defun mm-display-part (handle &optional no-default)
700 "Display the MIME part represented by HANDLE.
701 Returns nil if the part is removed; inline if displayed inline;
702 external if displayed external."
704 (mailcap-parse-mailcaps)
705 (if (mm-handle-displayed-p handle)
706 (mm-remove-part handle)
707 (let* ((ehandle (if (equal (mm-handle-media-type handle)
708 "message/external-body")
710 (unless (mm-handle-cache handle)
711 (mm-extern-cache-contents handle))
712 (mm-handle-cache handle))
714 (type (mm-handle-media-type ehandle))
715 (method (mailcap-mime-info type))
716 (filename (or (mail-content-type-get
717 (mm-handle-disposition handle) 'filename)
718 (mail-content-type-get
719 (mm-handle-type handle) 'name)
721 (external mm-enable-external))
722 (if (and (mm-inlinable-p ehandle)
723 (mm-inlined-p ehandle))
726 (mm-display-inline handle)
730 (if (and (not method)
731 (equal "text" (car (split-string type "/"))))
734 (mm-insert-inline handle (mm-get-part handle))
737 (and method ;; If nil, we always use "save".
738 (stringp method) ;; 'mailcap-save-binary-file
739 (or (eq mm-enable-external t)
740 (and (eq mm-enable-external 'ask)
743 "Display part (" type
744 ") using external program"
745 ;; Can non-string method ever happen?
748 " \"" (format method filename) "\"")
753 handle (or method 'mailcap-save-binary-file))
755 handle 'mailcap-save-binary-file)))))))))
757 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
758 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
760 (defun mm-display-external (handle method)
761 "Display HANDLE using METHOD."
762 (let ((outbuf (current-buffer)))
763 (mm-with-unibyte-buffer
764 (if (functionp method)
765 (let ((cur (current-buffer)))
766 (if (eq method 'mailcap-save-binary-file)
768 (set-buffer (generate-new-buffer " *mm*"))
770 (mm-insert-part handle)
771 (mm-add-meta-html-tag handle)
772 (let ((win (get-buffer-window cur t)))
774 (select-window win)))
775 (switch-to-buffer (generate-new-buffer " *mm*")))
776 (buffer-disable-undo)
777 (mm-set-buffer-file-coding-system mm-binary-coding-system)
778 (insert-buffer-substring cur)
779 (goto-char (point-min))
781 (message "Viewing with %s" method))
782 (let ((mm (current-buffer))
783 (non-viewer (assq 'non-viewer
785 (mm-handle-media-type handle) t))))
789 (mm-save-part handle))
790 (when (and (not non-viewer)
792 (mm-handle-set-undisplayer handle mm)))))
793 ;; The function is a string to be executed.
794 (mm-insert-part handle)
795 (mm-add-meta-html-tag handle)
796 (let* ((dir (mm-make-temp-file
797 (expand-file-name "emm." mm-tmp-directory) 'dir))
799 (mail-content-type-get
800 (mm-handle-disposition handle) 'filename)
801 (mail-content-type-get
802 (mm-handle-type handle) 'name)))
803 (mime-info (mailcap-mime-info
804 (mm-handle-media-type handle) t))
805 (needsterm (or (assoc "needsterm" mime-info)
806 (assoc "needsterminal" mime-info)))
807 (copiousoutput (assoc "copiousoutput" mime-info))
809 ;; We create a private sub-directory where we store our files.
810 (set-file-modes dir #o700)
812 (setq file (expand-file-name
813 (gnus-map-function mm-file-name-rewrite-functions
814 (file-name-nondirectory filename))
816 ;; Use nametemplate (defined in RFC1524) if it is specified
818 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
820 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
821 (setq suffix (match-string 1 suffix))
822 ;; Otherwise, use a suffix according to
823 ;; `mailcap-mime-extensions'.
824 (setq suffix (car (rassoc (mm-handle-media-type handle)
825 mailcap-mime-extensions))))
826 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
828 (let ((coding-system-for-write mm-binary-coding-system))
829 (write-region (point-min) (point-max) file nil 'nomesg))
830 ;; The file is deleted after the viewer exists. If the users edits
831 ;; the file, changes will be lost. Set file to read-only to make it
833 (set-file-modes file #o400)
834 (message "Viewing with %s" method)
837 (let ((command (mm-mailcap-command
838 method file (mm-handle-type handle))))
841 (start-process "*display*" nil
842 mm-external-terminal-program
844 shell-command-switch command)
852 shell-command-switch command)))
855 (set-process-sentinel
856 (get-buffer-process buffer)
857 `(lambda (process state)
858 (if (eq 'exit (process-status process))
859 (gnus-configure-windows
860 ',gnus-current-window-configuration))))
861 (gnus-configure-windows 'display-term))
862 (mm-handle-set-external-undisplayer handle (cons file buffer)))
863 (message "Displaying %s..." command))
866 (with-current-buffer outbuf
872 (call-process shell-file-name nil
874 (generate-new-buffer " *mm*"))
878 method file (mm-handle-type handle)))
879 (if (buffer-live-p buffer)
880 (with-current-buffer buffer
883 (ignore-errors (delete-file file))
884 (ignore-errors (delete-directory
885 (file-name-directory file)))
886 (ignore-errors (kill-buffer buffer))))))
889 ;; Deleting the temp file should be postponed for some wrappers,
890 ;; shell scripts, and so on, which might exit right after having
891 ;; started a viewer command as a background job.
892 (let ((command (mm-mailcap-command
893 method file (mm-handle-type handle))))
896 (start-process "*display*"
898 (generate-new-buffer " *mm*"))
900 shell-command-switch command)
901 (set-process-sentinel
902 (get-buffer-process buffer)
903 (lexical-let ;; Don't use `let'.
904 ;; Function used to remove temp file and directory.
906 ;; Don't use `ignore-errors'.
912 ,(file-name-directory file))
914 ;; Form uses to kill the process buffer and
915 ;; remove the undisplayer.
917 (kill-buffer ,buffer)
919 (list 'mm-handle-set-undisplayer
922 ;; Message to be issued when the process exits.
923 (done (format "Displaying %s...done" command))
924 ;; In particular, the timer object (which is
925 ;; a vector in Emacs but is a list in XEmacs)
926 ;; requires that it is lexically scoped.
927 (timer (run-at-time 2.0 nil 'ignore)))
928 (if (featurep 'xemacs)
929 (lambda (process state)
930 (when (eq 'exit (process-status process))
931 (if (memq timer itimer-list)
932 (set-itimer-function timer fn)
934 (ignore-errors (eval fm))
935 (message "%s" done)))
936 (lambda (process state)
937 (when (eq 'exit (process-status process))
938 (if (memq timer timer-list)
939 (timer-set-function timer fn)
941 (ignore-errors (eval fm))
942 (message "%s" done)))))))
943 (mm-handle-set-external-undisplayer
944 handle (cons file buffer)))
945 (message "Displaying %s..." command))
948 (defun mm-mailcap-command (method file type-list)
949 (let ((ctl (cdr type-list))
953 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
955 (push (substring method beg (match-beginning 0)) out)
956 (setq beg (match-end 0)
957 total (match-string 0 method)
958 sub (match-string 1 method))
960 ((string= total "%%")
962 ((or (string= total "%s")
963 ;; We do our own quoting.
964 (string= total "'%s'")
965 (string= total "\"%s\""))
966 (setq uses-stdin nil)
967 (push (shell-quote-argument
968 (gnus-map-function mm-path-name-rewrite-functions file)) out))
969 ((string= total "%t")
970 (push (shell-quote-argument (car type-list)) out))
972 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
973 (push (substring method beg (length method)) out)
976 (push (shell-quote-argument
977 (gnus-map-function mm-path-name-rewrite-functions file))
979 (mapconcat 'identity (nreverse out) "")))
981 (defun mm-remove-parts (handles)
982 "Remove the displayed MIME parts represented by HANDLES."
983 (if (and (listp handles)
984 (bufferp (car handles)))
985 (mm-remove-part handles)
987 (while (setq handle (pop handles))
990 (when (buffer-live-p (get-text-property 0 'buffer handle))
991 (kill-buffer (get-text-property 0 'buffer handle))))
993 (stringp (car handle)))
994 (mm-remove-parts (cdr handle)))
996 (mm-remove-part handle)))))))
998 (defun mm-destroy-parts (handles)
999 "Remove the displayed MIME parts represented by HANDLES."
1000 (if (and (listp handles)
1001 (bufferp (car handles)))
1002 (mm-destroy-part handles)
1004 (while (setq handle (pop handles))
1007 (when (buffer-live-p (get-text-property 0 'buffer handle))
1008 (kill-buffer (get-text-property 0 'buffer handle))))
1009 ((and (listp handle)
1010 (stringp (car handle)))
1011 (mm-destroy-parts handle))
1013 (mm-destroy-part handle)))))))
1015 (defun mm-remove-part (handle)
1016 "Remove the displayed MIME part represented by HANDLE."
1017 (when (listp handle)
1018 (let ((object (mm-handle-undisplayer handle)))
1021 ;; Internally displayed part.
1022 ((mm-annotationp object)
1023 (if (featurep 'xemacs)
1024 (delete-annotation object)))
1025 ((or (functionp object)
1027 (eq (car object) 'lambda)))
1029 ;; Externally displayed part.
1032 (while (get-buffer-process (cdr object))
1033 (interrupt-process (get-buffer-process (cdr object)))
1034 (message "Waiting for external displayer to die...")
1038 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1039 (message "Waiting for external displayer to die...done")
1040 (ignore-errors (delete-file (car object)))
1041 (ignore-errors (delete-directory (file-name-directory
1044 (when (buffer-live-p object)
1045 (kill-buffer object)))))
1046 (mm-handle-set-undisplayer handle nil))))
1048 (defun mm-display-inline (handle)
1049 (let* ((type (mm-handle-media-type handle))
1050 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1051 (funcall function handle)
1052 (goto-char (point-min))))
1054 (defun mm-assoc-string-match (alist type)
1055 (dolist (elem alist)
1056 (when (string-match (car elem) type)
1059 (defun mm-automatic-display-p (handle)
1060 "Say whether the user wants HANDLE to be displayed automatically."
1061 (let ((methods mm-automatic-display)
1062 (type (mm-handle-media-type handle))
1064 (while (setq method (pop methods))
1065 (when (and (not (mm-inline-override-p handle))
1066 (string-match method type))
1071 (defun mm-inlinable-p (handle &optional type)
1072 "Say whether HANDLE can be displayed inline.
1073 TYPE is the mime-type of the object; it defaults to the one given
1075 (unless type (setq type (mm-handle-media-type handle)))
1076 (let ((alist mm-inline-media-tests)
1079 (when (string-match (caar alist) type)
1080 (setq test (caddar alist)
1082 (setq test (funcall test handle)))
1086 (defun mm-inlined-p (handle)
1087 "Say whether the user wants HANDLE to be displayed inline."
1088 (let ((methods mm-inlined-types)
1089 (type (mm-handle-media-type handle))
1091 (while (setq method (pop methods))
1092 (when (and (not (mm-inline-override-p handle))
1093 (string-match method type))
1098 (defun mm-attachment-override-p (handle)
1099 "Say whether HANDLE should have attachment behavior overridden."
1100 (let ((types mm-attachment-override-types)
1101 (type (mm-handle-media-type handle))
1104 (while (setq ty (pop types))
1105 (when (and (string-match ty type)
1106 (mm-inlinable-p handle))
1107 (throw 'found t))))))
1109 (defun mm-inline-override-p (handle)
1110 "Say whether HANDLE should have inline behavior overridden."
1111 (let ((types mm-inline-override-types)
1112 (type (mm-handle-media-type handle))
1115 (while (setq ty (pop types))
1116 (when (string-match ty type)
1117 (throw 'found t))))))
1119 (defun mm-automatic-external-display-p (type)
1120 "Return the user-defined method for TYPE."
1121 (let ((methods mm-automatic-external-display)
1123 (while (setq method (pop methods))
1124 (when (string-match method type)
1129 (defun mm-destroy-part (handle)
1130 "Destroy the data structures connected to HANDLE."
1131 (when (listp handle)
1132 (mm-remove-part handle)
1133 (when (buffer-live-p (mm-handle-buffer handle))
1134 (kill-buffer (mm-handle-buffer handle)))))
1136 (defun mm-handle-displayed-p (handle)
1137 "Say whether HANDLE is displayed or not."
1138 (mm-handle-undisplayer handle))
1141 ;;; Functions for outputting parts
1144 (defmacro mm-with-part (handle &rest forms)
1145 "Run FORMS in the temp buffer containing the contents of HANDLE."
1146 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1147 ;; chars, so the buffer should be unibyte. It may happen that the
1148 ;; handle-buffer is multibyte for some reason, in which case now is a good
1149 ;; time to adjust it, since we know at this point that it should
1151 `(let* ((handle ,handle))
1152 (when (and (mm-handle-buffer handle)
1153 (buffer-name (mm-handle-buffer handle)))
1155 (mm-disable-multibyte)
1156 (insert-buffer-substring (mm-handle-buffer handle))
1157 (mm-decode-content-transfer-encoding
1158 (mm-handle-encoding handle)
1159 (mm-handle-media-type handle))
1161 (put 'mm-with-part 'lisp-indent-function 1)
1162 (put 'mm-with-part 'edebug-form-spec '(body))
1164 (defun mm-get-part (handle &optional no-cache)
1165 "Return the contents of HANDLE as a string.
1166 If NO-CACHE is non-nil, cached contents of a message/external-body part
1168 (if (and (not no-cache)
1169 (equal (mm-handle-media-type handle) "message/external-body"))
1171 (unless (mm-handle-cache handle)
1172 (mm-extern-cache-contents handle))
1173 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1175 (mm-with-part handle
1178 (defun mm-insert-part (handle &optional no-cache)
1179 "Insert the contents of HANDLE in the current buffer.
1180 If NO-CACHE is non-nil, cached contents of a message/external-body part
1182 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1185 (with-current-buffer (mm-handle-buffer handle)
1188 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1190 (mm-get-part handle no-cache)))))
1196 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1198 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1199 ;; Separate the extracted parts that have the same faces.
1203 (defun mm-file-name-delete-whitespace (file-name)
1204 "Remove all whitespace characters from FILE-NAME."
1205 (while (string-match "\\s-+" file-name)
1206 (setq file-name (replace-match "" t t file-name)))
1209 (defun mm-file-name-trim-whitespace (file-name)
1210 "Remove leading and trailing whitespace characters from FILE-NAME."
1211 (when (string-match "\\`\\s-+" file-name)
1212 (setq file-name (substring file-name (match-end 0))))
1213 (when (string-match "\\s-+\\'" file-name)
1214 (setq file-name (substring file-name 0 (match-beginning 0))))
1217 (defun mm-file-name-collapse-whitespace (file-name)
1218 "Collapse multiple whitespace characters in FILE-NAME."
1219 (while (string-match "\\s-\\s-+" file-name)
1220 (setq file-name (replace-match " " t t file-name)))
1223 (defun mm-file-name-replace-whitespace (file-name)
1224 "Replace whitespace characters in FILE-NAME with underscores.
1225 Set the option `mm-file-name-replace-whitespace' to any other
1226 string if you do not like underscores."
1227 (let ((s (or mm-file-name-replace-whitespace "_")))
1228 (while (string-match "\\s-" file-name)
1229 (setq file-name (replace-match s t t file-name))))
1232 (defun mm-file-name-delete-control (filename)
1233 "Delete control characters from FILENAME."
1234 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1236 (defun mm-file-name-delete-gotchas (filename)
1237 "Delete shell gotchas from FILENAME."
1238 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1239 (gnus-replace-in-string filename "^[.-]+" ""))
1241 (defun mm-save-part (handle &optional prompt)
1242 "Write HANDLE to a file.
1243 PROMPT overrides the default one used to ask user for a file name."
1244 (let ((filename (or (mail-content-type-get
1245 (mm-handle-disposition handle) 'filename)
1246 (mail-content-type-get
1247 (mm-handle-type handle) 'name)))
1250 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1251 (file-name-nondirectory filename))))
1253 (read-file-name (or prompt
1254 (format "Save MIME part to (default %s): "
1256 (or mm-default-directory default-directory)
1258 (if (file-directory-p file)
1259 (setq file (expand-file-name filename file))
1260 (setq file (expand-file-name
1261 file (or mm-default-directory default-directory))))
1262 (setq mm-default-directory (file-name-directory file))
1263 (and (or (not (file-exists-p file))
1264 (yes-or-no-p (format "File %s already exists; overwrite? "
1267 (mm-save-part-to-file handle file)
1270 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1271 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1272 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1273 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1274 Return t if meta tag is added or replaced."
1275 (when (equal (mm-handle-media-type handle) "text/html")
1277 (setq charset (mail-content-type-get (mm-handle-type handle)
1279 (setq charset (format "\
1280 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1281 (let ((case-fold-search t))
1282 (goto-char (point-min))
1283 (if (re-search-forward "\
1284 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1285 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
1286 (if (and (not force-charset)
1288 (string-match "\\`html\\'" (match-string 1)))
1289 ;; Don't modify existing meta tag.
1291 ;; Replace it with the one specifying charset.
1292 (replace-match charset)
1294 (if (re-search-forward "<head>\\s-*" nil t)
1295 (insert charset "\n")
1296 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1297 (insert "<head>\n" charset "\n</head>\n"))
1300 (defun mm-save-part-to-file (handle file)
1301 (mm-with-unibyte-buffer
1302 (mm-insert-part handle)
1303 (mm-add-meta-html-tag handle)
1304 (let ((current-file-modes (default-file-modes)))
1305 (set-default-file-modes mm-attachment-file-modes)
1307 ;; Don't re-compress .gz & al. Arguably we should make
1308 ;; `file-name-handler-alist' nil, but that would chop
1309 ;; ange-ftp, which is reasonable to use here.
1310 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1311 (set-default-file-modes current-file-modes)))))
1313 (defun mm-pipe-part (handle &optional cmd)
1314 "Pipe HANDLE to a process.
1315 Use CMD as the process."
1316 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1318 (gnus-read-shell-command
1319 "Shell command on MIME part: " mm-last-shell-command))))
1320 (mm-with-unibyte-buffer
1321 (mm-insert-part handle)
1322 (mm-add-meta-html-tag handle)
1323 (let ((coding-system-for-write 'binary))
1324 (shell-command-on-region (point-min) (point-max) command nil)))))
1326 (defun mm-interactively-view-part (handle)
1327 "Display HANDLE using METHOD."
1328 (let* ((type (mm-handle-media-type handle))
1330 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1331 (mailcap-mime-info type 'all)))
1332 (method (let ((minibuffer-local-completion-map
1333 mm-viewer-completion-map))
1334 (gnus-completing-read "Viewer" methods))))
1335 (when (string= method "")
1336 (error "No method given"))
1337 (if (string-match "^[^% \t]+$" method)
1338 (setq method (concat method " %s")))
1339 (mm-display-external handle method)))
1341 (defun mm-preferred-alternative (handles &optional preferred)
1342 "Say which of HANDLES are preferred."
1343 (let ((prec (if preferred (list preferred)
1344 (mm-preferred-alternative-precedence handles)))
1345 p h result type handle)
1346 (while (setq p (pop prec))
1349 (setq handle (car h))
1350 (setq type (mm-handle-media-type handle))
1351 (when (and (equal p type)
1352 (mm-automatic-display-p handle)
1353 (or (stringp (car handle))
1354 (not (mm-handle-disposition handle))
1355 (equal (car (mm-handle-disposition handle))
1363 (defun mm-preferred-alternative-precedence (handles)
1364 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1365 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1367 (dolist (disc (reverse mm-discouraged-alternatives))
1368 (dolist (elem (copy-sequence seq))
1369 (when (string-match disc elem)
1370 (setq seq (nconc (delete elem seq) (list elem))))))
1373 (defun mm-get-content-id (id)
1374 "Return the handle(s) referred to by ID."
1375 (cdr (assoc id mm-content-id-alist)))
1377 (defconst mm-image-type-regexps
1378 '(("/\\*.*XPM.\\*/" . xpm)
1382 ("\211PNG\r\n" . png)
1384 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1385 ("%!PS" . postscript))
1386 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1387 When the first bytes of an image file match REGEXP, it is assumed to
1388 be of image type IMAGE-TYPE.")
1390 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1391 (defun mm-image-type-from-buffer ()
1392 "Determine the image type from data in the current buffer.
1393 Value is a symbol specifying the image type or nil if type cannot
1395 (let ((types mm-image-type-regexps)
1397 (goto-char (point-min))
1398 (while (and types (null type))
1399 (let ((regexp (car (car types)))
1400 (image-type (cdr (car types))))
1401 (when (looking-at regexp)
1402 (setq type image-type))
1403 (setq types (cdr types))))
1406 (defun mm-get-image (handle)
1407 "Return an image instance based on HANDLE."
1408 (let ((type (mm-handle-media-subtype handle))
1410 ;; Allow some common translations.
1413 ((equal type "x-pixmap")
1415 ((equal type "x-xbitmap")
1417 ((equal type "x-portable-bitmap")
1420 (or (mm-handle-cache handle)
1421 (mm-with-unibyte-buffer
1422 (mm-insert-part handle)
1426 ;; Avoid testing `make-glyph' since W3 may define
1427 ;; a bogus version of it.
1428 (if (fboundp 'create-image)
1429 (create-image (buffer-string)
1430 (or (mm-image-type-from-buffer)
1433 (mm-create-image-xemacs type))))
1434 (mm-handle-set-cache handle spec))))))
1436 (defun mm-create-image-xemacs (type)
1437 (when (featurep 'xemacs)
1440 ;; xbm images require special handling, since
1441 ;; the only way to create glyphs from these
1442 ;; (without a ton of work) is to write them
1443 ;; out to a file, and then create a file
1445 (let ((file (mm-make-temp-file
1446 (expand-file-name "emm" mm-tmp-directory)
1450 (write-region (point-min) (point-max) file)
1451 (make-glyph (list (cons 'x file))))
1453 (delete-file file)))))
1457 (or (mm-image-type-from-buffer)
1459 :data (buffer-string)))))))
1461 (declare-function image-size "image.c" (spec &optional pixels frame))
1463 (defun mm-image-fit-p (handle)
1464 "Say whether the image in HANDLE will fit the current window."
1465 (let ((image (mm-get-image handle)))
1467 (if (featurep 'xemacs)
1468 ;; XEmacs' glyphs can actually tell us about their width, so
1469 ;; lets be nice and smart about them.
1470 (or mm-inline-large-images
1471 (and (<= (glyph-width image) (window-pixel-width))
1472 (<= (glyph-height image) (window-pixel-height))))
1473 (let* ((size (image-size image))
1476 (or mm-inline-large-images
1477 (and (<= h (1- (window-height))) ; Don't include mode line.
1478 (<= w (window-width)))))))))
1480 (defun mm-valid-image-format-p (format)
1481 "Say whether FORMAT can be displayed natively by Emacs."
1484 ((fboundp 'valid-image-instantiator-format-p)
1485 (valid-image-instantiator-format-p format))
1487 ((fboundp 'image-type-available-p)
1488 (and (display-graphic-p)
1489 (image-type-available-p format)))
1490 ;; Nobody else can do images yet.
1494 (defun mm-valid-and-fit-image-p (format handle)
1495 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1496 (and (mm-valid-image-format-p format)
1497 (mm-image-fit-p handle)))
1499 (defun mm-find-part-by-type (handles type &optional notp recursive)
1500 "Search in HANDLES for part with TYPE.
1501 If NOTP, returns first non-matching part.
1502 If RECURSIVE, search recursively."
1505 (if (and recursive (stringp (caar handles)))
1506 (if (setq handle (mm-find-part-by-type (cdar handles) type
1510 (not (equal (mm-handle-media-type (car handles)) type))
1511 (equal (mm-handle-media-type (car handles)) type))
1512 (setq handle (car handles)
1514 (setq handles (cdr handles)))
1517 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1518 (goto-char (point-min))
1519 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1521 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1523 (end (save-excursion
1524 (goto-char (point-max))
1525 (if (re-search-backward close-delimiter nil t)
1529 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1530 (while (and (not result)
1531 (re-search-forward boundary end t))
1532 (goto-char (match-beginning 0))
1536 (narrow-to-region start (1- (point)))
1537 (when (let* ((ct (mail-fetch-field "content-type"))
1538 (ctl (and ct (mail-header-parse-content-type ct))))
1540 (not (equal (car ctl) type))
1541 (equal (car ctl) type)))
1542 (setq result (buffer-string))))))
1544 (setq start (point)))
1545 (when (and (not result) start)
1548 (narrow-to-region start end)
1549 (when (let* ((ct (mail-fetch-field "content-type"))
1550 (ctl (and ct (mail-header-parse-content-type ct))))
1552 (not (equal (car ctl) type))
1553 (equal (car ctl) type)))
1554 (setq result (buffer-string))))))
1557 (defvar mm-security-handle nil)
1559 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1560 ;; HANDLE could be a CTL.
1562 (put-text-property 0 (length (car handle)) parameter value
1565 (autoload 'mm-view-pkcs7 "mm-view")
1567 (defun mm-possibly-verify-or-decrypt (parts ctl)
1568 (let ((type (car ctl))
1569 (subtype (cadr (split-string (car ctl) "/")))
1570 (mm-security-handle ctl) ;; (car CTL) is the type.
1571 protocol func functest)
1573 ((or (equal type "application/x-pkcs7-mime")
1574 (equal type "application/pkcs7-mime"))
1577 ((eq mm-decrypt-option 'never) nil)
1578 ((eq mm-decrypt-option 'always) t)
1579 ((eq mm-decrypt-option 'known) t)
1581 (format "Decrypt (S/MIME) part? "))))
1582 (mm-view-pkcs7 parts))
1583 (setq parts (mm-dissect-buffer t)))))
1584 ((equal subtype "signed")
1585 (unless (and (setq protocol
1586 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1587 (not (equal protocol "multipart/mixed")))
1588 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1589 (let ((protocols mm-verify-function-alist))
1591 (if (and (or (not (setq functest (nth 3 (car protocols))))
1592 (funcall functest parts ctl))
1593 (mm-find-part-by-type parts (caar protocols) nil t))
1594 (setq protocol (caar protocols)
1596 (setq protocols (cdr protocols))))))
1597 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1599 ((eq mm-verify-option 'never) nil)
1600 ((eq mm-verify-option 'always) t)
1601 ((eq mm-verify-option 'known)
1603 (or (not (setq functest
1604 (nth 3 (assoc protocol
1605 mm-verify-function-alist))))
1606 (funcall functest parts ctl))))
1609 (format "Verify signed (%s) part? "
1610 (or (nth 2 (assoc protocol mm-verify-function-alist))
1611 (format "protocol=%s" protocol))))))
1614 (setq parts (funcall func parts ctl))
1615 (mm-set-handle-multipart-parameter
1616 mm-security-handle 'gnus-details
1617 (format "Unknown sign protocol (%s)" protocol))))))
1618 ((equal subtype "encrypted")
1619 (unless (setq protocol
1620 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1621 ;; The message is broken.
1622 (let ((parts parts))
1624 (if (assoc (mm-handle-media-type (car parts))
1625 mm-decrypt-function-alist)
1626 (setq protocol (mm-handle-media-type (car parts))
1628 (setq parts (cdr parts))))))
1629 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1631 ((eq mm-decrypt-option 'never) nil)
1632 ((eq mm-decrypt-option 'always) t)
1633 ((eq mm-decrypt-option 'known)
1635 (or (not (setq functest
1636 (nth 3 (assoc protocol
1637 mm-decrypt-function-alist))))
1638 (funcall functest parts ctl))))
1641 (format "Decrypt (%s) part? "
1642 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1643 (format "protocol=%s" protocol))))))
1646 (setq parts (funcall func parts ctl))
1647 (mm-set-handle-multipart-parameter
1648 mm-security-handle 'gnus-details
1649 (format "Unknown encrypt protocol (%s)" protocol))))))
1653 (defun mm-multiple-handles (handles)
1654 (and (listp handles)
1655 (> (length handles) 1)
1656 (or (listp (car handles))
1657 (stringp (car handles)))))
1659 (defun mm-complicated-handles (handles)
1660 (and (listp (car handles))
1661 (> (length handles) 1)))
1663 (defun mm-merge-handles (handles1 handles2)
1665 (if (listp (car handles1))
1668 (if (listp (car handles2))
1672 (defun mm-readable-p (handle)
1673 "Say whether the content of HANDLE is readable."
1674 (and (< (with-current-buffer (mm-handle-buffer handle)
1675 (buffer-size)) 10000)
1676 (mm-with-unibyte-buffer
1677 (mm-insert-part handle)
1678 (and (eq (mm-body-7-or-8) '7bit)
1679 (not (mm-long-lines-p 76))))))
1681 (declare-function libxml-parse-html-region "xml.c"
1682 (start end &optional base-url))
1683 (declare-function shr-insert-document "shr" (dom))
1684 (defvar shr-blocked-images)
1686 (defun mm-shr (handle)
1687 ;; Require since we bind its variables.
1689 (let ((article-buffer (current-buffer))
1690 (shr-blocked-images (if (and (boundp 'gnus-summary-buffer)
1691 (buffer-name gnus-summary-buffer))
1692 (with-current-buffer gnus-summary-buffer
1693 (gnus-blocked-images))
1694 shr-blocked-images))
1695 (shr-content-function (lambda (id)
1696 (let ((handle (mm-get-content-id id)))
1698 (mm-with-part handle
1699 (buffer-string))))))
1702 (setq handle (mm-dissect-buffer t)))
1703 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1705 (narrow-to-region (point) (point))
1706 (shr-insert-document
1707 (mm-with-part handle
1709 (setq charset (mm-charset-to-coding-system charset))
1710 (not (eq charset 'ascii)))
1712 (mm-decode-coding-string (buffer-string) charset)
1714 (mm-enable-multibyte))))
1715 (libxml-parse-html-region (point-min) (point-max)))))))
1717 (provide 'mm-decode)
1719 ;;; mm-decode.el ends here