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) 'mm-shr)
109 ((executable-find "w3m") 'gnus-article-html)
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 `mm-shr': use Gnus simple HTML renderer;
119 `gnus-article-html' : 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 mm-shr)
129 (const gnus-article-html)
131 (const w3m :tag "emacs-w3m")
132 (const w3m-standalone :tag "standalone w3m" )
136 (const nil :tag "External viewer")
138 :group 'mime-display)
140 (defvar mm-inline-text-html-renderer nil
141 "Function used for rendering inline HTML contents.
142 It is suggested to customize `mm-text-html-renderer' instead.")
144 (defcustom mm-inline-text-html-with-images nil
145 "If non-nil, Gnus will allow retrieving images in HTML contents with
146 the <img> tags. It has no effect on Emacs/w3. See also the
147 documentation for the `mm-w3m-safe-url-regexp' variable."
150 :group 'mime-display)
152 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
153 "Regexp matching URLs which are considered to be safe.
154 Some HTML mails might contain a nasty trick used by spammers, using
155 the <img> tag which is far more evil than the [Click Here!] button.
156 It is most likely intended to check whether the ominous spam mail has
157 reached your eyes or not, in which case the spammer knows for sure
158 that your email address is valid. It is done by embedding an
159 identifier string into a URL that you might automatically retrieve
160 when displaying the image. The default value is \"\\\\`cid:\" which only
161 matches parts embedded to the Multipart/Related type MIME contents and
162 Gnus will never connect to the spammer's site arbitrarily. You may
163 set this variable to nil if you consider all urls to be safe."
165 :type '(choice (regexp :tag "Regexp")
166 (const :tag "All URLs are safe" nil))
167 :group 'mime-display)
169 (defcustom mm-inline-text-html-with-w3m-keymap t
170 "If non-nil, use emacs-w3m command keys in the article buffer."
173 :group 'mime-display)
175 (defcustom mm-enable-external t
176 "Indicate whether external MIME handlers should be used.
178 If t, all defined external MIME handlers are used. If nil, files are saved by
179 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
180 before the external MIME handler is invoked."
182 :type '(choice (const :tag "Always" t)
183 (const :tag "Never" nil)
184 (const :tag "Ask" ask))
185 :group 'mime-display)
187 (defcustom mm-inline-media-tests
191 (mm-valid-and-fit-image-p 'jpeg handle)))
195 (mm-valid-and-fit-image-p 'png handle)))
199 (mm-valid-and-fit-image-p 'gif handle)))
203 (mm-valid-and-fit-image-p 'tiff handle)) )
207 (mm-valid-and-fit-image-p 'xbm handle)))
211 (mm-valid-and-fit-image-p 'xbm handle)))
215 (mm-valid-and-fit-image-p 'xpm handle)))
219 (mm-valid-and-fit-image-p 'xpm handle)))
223 (mm-valid-and-fit-image-p 'bmp handle)))
224 ("image/x-portable-bitmap"
227 (mm-valid-and-fit-image-p 'pbm handle)))
228 ("text/plain" mm-inline-text identity)
229 ("text/enriched" mm-inline-text identity)
230 ("text/richtext" mm-inline-text identity)
231 ("text/x-patch" mm-display-patch-inline
233 ;; If the diff-mode.el package is installed, the function is
234 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
235 ;; to cater to broken installations. OTOH checking the function
236 ;; makes it possible to install another package which provides an
237 ;; alternative implementation of diff-mode. --Stef
238 (fboundp 'diff-mode)))
239 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
240 ("text/x-diff" mm-display-patch-inline
241 (lambda (handle) (fboundp 'diff-mode)))
242 ("application/emacs-lisp" mm-display-elisp-inline identity)
243 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
244 ("text/dns" mm-display-dns-inline identity)
248 (or mm-inline-text-html-renderer
249 mm-text-html-renderer)))
253 (or (featurep 'vcard)
254 (locate-library "vcard"))))
255 ("message/delivery-status" mm-inline-text identity)
256 ("message/rfc822" mm-inline-message identity)
257 ("message/partial" mm-inline-partial identity)
258 ("message/external-body" mm-inline-external-body identity)
259 ("text/.*" mm-inline-text identity)
260 ("audio/wav" mm-inline-audio
262 (and (or (featurep 'nas-sound) (featurep 'native-sound))
263 (device-sound-enabled-p))))
267 (and (or (featurep 'nas-sound) (featurep 'native-sound))
268 (device-sound-enabled-p))))
269 ("application/pgp-signature" ignore identity)
270 ("application/x-pkcs7-signature" ignore identity)
271 ("application/pkcs7-signature" ignore identity)
272 ("application/x-pkcs7-mime" ignore identity)
273 ("application/pkcs7-mime" ignore identity)
274 ("multipart/alternative" ignore identity)
275 ("multipart/mixed" ignore identity)
276 ("multipart/related" ignore identity)
277 ;; Disable audio and image
278 ("audio/.*" ignore ignore)
279 ("image/.*" ignore ignore)
280 ;; Default to displaying as text
281 (".*" mm-inline-text mm-readable-p))
282 "Alist of media types/tests saying whether types can be displayed inline."
283 :type '(repeat (list (regexp :tag "MIME type")
284 (function :tag "Display function")
285 (function :tag "Display test")))
286 :group 'mime-display)
288 (defcustom mm-inlined-types
289 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
290 "message/partial" "message/external-body" "application/emacs-lisp"
291 "application/x-emacs-lisp"
292 "application/pgp-signature" "application/x-pkcs7-signature"
293 "application/pkcs7-signature" "application/x-pkcs7-mime"
294 "application/pkcs7-mime"
295 ;; Mutt still uses this even though it has already been withdrawn.
297 "List of media types that are to be displayed inline.
298 See also `mm-inline-media-tests', which says how to display a media
300 :type '(repeat regexp)
301 :group 'mime-display)
303 (defcustom mm-keep-viewer-alive-types
304 '("application/postscript" "application/msword" "application/vnd.ms-excel"
305 "application/pdf" "application/x-dvi")
306 "List of media types for which the external viewer will not be killed
307 when selecting a different article."
309 :type '(repeat regexp)
310 :group 'mime-display)
312 (defcustom mm-automatic-display
313 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
314 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
315 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
316 "application/emacs-lisp" "application/x-emacs-lisp"
317 "application/x-pkcs7-signature"
318 "application/pkcs7-signature" "application/x-pkcs7-mime"
319 "application/pkcs7-mime"
320 ;; Mutt still uses this even though it has already been withdrawn.
321 "application/pgp\\'")
322 "A list of MIME types to be displayed automatically."
323 :type '(repeat regexp)
324 :group 'mime-display)
326 (defcustom mm-attachment-override-types '("text/x-vcard"
327 "application/pkcs7-mime"
328 "application/x-pkcs7-mime"
329 "application/pkcs7-signature"
330 "application/x-pkcs7-signature")
331 "Types to have \"attachment\" ignored if they can be displayed inline."
332 :type '(repeat regexp)
333 :group 'mime-display)
335 (defcustom mm-inline-override-types nil
336 "Types to be treated as attachments even if they can be displayed inline."
337 :type '(repeat regexp)
338 :group 'mime-display)
340 (defcustom mm-automatic-external-display nil
341 "List of MIME type regexps that will be displayed externally automatically."
342 :type '(repeat regexp)
343 :group 'mime-display)
345 (defcustom mm-discouraged-alternatives nil
346 "List of MIME types that are discouraged when viewing multipart/alternative.
347 Viewing agents are supposed to view the last possible part of a message,
348 as that is supposed to be the richest. However, users may prefer other
349 types instead, and this list says what types are most unwanted. If,
350 for instance, text/html parts are very unwanted, and text/richtext are
351 somewhat unwanted, then the value of this variable should be set
354 (\"text/html\" \"text/richtext\")
356 Adding \"image/.*\" might also be useful. Spammers use it as the
357 prefered part of multipart/alternative messages. See also
358 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
359 enables you to choose manually one of two types those mails include."
360 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
361 :group 'mime-display)
363 (defcustom mm-tmp-directory
364 (if (fboundp 'temp-directory)
366 (if (boundp 'temporary-file-directory)
367 temporary-file-directory
369 "Where mm will store its temporary files."
371 :group 'mime-display)
373 (defcustom mm-inline-large-images nil
374 "If t, then all images fit in the buffer.
375 If 'resize, try to resize the images so they fit."
377 (const :tag "Inline large images as they are." t)
378 (const :tag "Resize large images." resize)
379 (const :tag "Do not inline large images." nil))
380 :group 'mime-display)
382 (defcustom mm-file-name-rewrite-functions
383 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
384 "List of functions used for rewriting file names of MIME parts.
385 Each function takes a file name as input and returns a file name.
387 Ready-made functions include `mm-file-name-delete-control',
388 `mm-file-name-delete-gotchas' (you should not remove these two
389 functions), `mm-file-name-delete-whitespace',
390 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
391 `mm-file-name-replace-whitespace', `capitalize', `downcase',
392 `upcase', and `upcase-initials'."
393 :type '(list (set :inline t
394 (const mm-file-name-delete-control)
395 (const mm-file-name-delete-gotchas)
396 (const mm-file-name-delete-whitespace)
397 (const mm-file-name-trim-whitespace)
398 (const mm-file-name-collapse-whitespace)
399 (const mm-file-name-replace-whitespace)
403 (const upcase-initials)
407 :version "23.1" ;; No Gnus
408 :group 'mime-display)
411 (defvar mm-path-name-rewrite-functions nil
412 "*List of functions for rewriting the full file names of MIME parts.
413 This is used when viewing parts externally, and is meant for
414 transforming the absolute name so that non-compliant programs can find
415 the file where it's saved.
417 Each function takes a file name as input and returns a file name.")
419 (defvar mm-file-name-replace-whitespace nil
420 "String used for replacing whitespace characters; default is `\"_\"'.")
422 (defcustom mm-default-directory nil
423 "The default directory where mm will save files.
424 If not set, `default-directory' will be used."
425 :type '(choice directory (const :tag "Default" nil))
426 :group 'mime-display)
428 (defcustom mm-attachment-file-modes 384
429 "Set the mode bits of saved attachments to this integer."
432 :group 'mime-display)
434 (defcustom mm-external-terminal-program "xterm"
435 "The program to start an external terminal."
438 :group 'mime-display)
440 ;;; Internal variables.
442 (defvar mm-last-shell-command "")
443 (defvar mm-content-id-alist nil)
444 (defvar mm-postponed-undisplay-list nil)
446 ;; According to RFC2046, in particular, in a digest, the default
447 ;; Content-Type value for a body part is changed from "text/plain" to
449 (defvar mm-dissect-default-type "text/plain")
451 (autoload 'mml2015-verify "mml2015")
452 (autoload 'mml2015-verify-test "mml2015")
453 (autoload 'mml-smime-verify "mml-smime")
454 (autoload 'mml-smime-verify-test "mml-smime")
456 (defvar mm-verify-function-alist
457 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
458 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
459 mm-uu-pgp-signed-test)
460 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
461 mml-smime-verify-test)
462 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
463 mml-smime-verify-test)))
465 (defcustom mm-verify-option 'never
466 "Option of verifying signed parts.
467 `never', not verify; `always', always verify;
468 `known', only verify known protocols. Otherwise, ask user.
470 When set to `always' or `known', you should add
471 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
472 result of the verification."
474 :type '(choice (item always)
476 (item :tag "only known protocols" known)
477 (item :tag "ask" nil))
478 :group 'mime-security)
480 (autoload 'mml2015-decrypt "mml2015")
481 (autoload 'mml2015-decrypt-test "mml2015")
483 (defvar mm-decrypt-function-alist
484 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
485 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
486 mm-uu-pgp-encrypted-test)))
488 (defcustom mm-decrypt-option nil
489 "Option of decrypting encrypted parts.
490 `never', not decrypt; `always', always decrypt;
491 `known', only decrypt known protocols. Otherwise, ask user."
493 :type '(choice (item always)
495 (item :tag "only known protocols" known)
496 (item :tag "ask" nil))
497 :group 'mime-security)
499 (defvar mm-viewer-completion-map
500 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
501 (set-keymap-parent map minibuffer-local-completion-map)
502 ;; Should we bind other key to minibuffer-complete-word?
503 (define-key map " " 'self-insert-command)
505 "Keymap for input viewer with completion.")
507 (defvar mm-viewer-completion-map
508 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
509 (set-keymap-parent map minibuffer-local-completion-map)
510 ;; Should we bind other key to minibuffer-complete-word?
511 (define-key map " " 'self-insert-command)
513 "Keymap for input viewer with completion.")
517 (defun mm-alist-to-plist (alist)
518 "Convert association list ALIST into the equivalent property-list form.
519 The plist is returned. This converts from
521 \((a . 1) (b . 2) (c . 3))
527 The original alist is not modified. See also `destructive-alist-to-plist'."
530 (let ((el (car alist)))
531 (setq plist (cons (cdr el) (cons (car el) plist))))
532 (setq alist (cdr alist)))
535 (defun mm-keep-viewer-alive-p (handle)
536 "Say whether external viewer for HANDLE should stay alive."
537 (let ((types mm-keep-viewer-alive-types)
538 (type (mm-handle-media-type handle))
541 (while (setq ty (pop types))
542 (when (string-match ty type)
543 (throw 'found t))))))
545 (defun mm-handle-set-external-undisplayer (handle function)
546 "Set the undisplayer for HANDLE to FUNCTION.
547 Postpone undisplaying of viewers for types in
548 `mm-keep-viewer-alive-types'."
549 (if (mm-keep-viewer-alive-p handle)
550 (let ((new-handle (copy-sequence handle)))
551 (mm-handle-set-undisplayer new-handle function)
552 (mm-handle-set-undisplayer handle nil)
553 (push new-handle mm-postponed-undisplay-list))
554 (mm-handle-set-undisplayer handle function)))
556 (defun mm-destroy-postponed-undisplay-list ()
557 (when mm-postponed-undisplay-list
558 (message "Destroying external MIME viewers")
559 (mm-destroy-parts mm-postponed-undisplay-list)))
561 (autoload 'message-fetch-field "message")
563 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
564 "Dissect the current buffer and return a list of MIME handles."
566 (let (ct ctl type subtype cte cd description id result)
568 (mail-narrow-to-head)
569 (when (or no-strict-mime
571 (mail-fetch-field "mime-version"))
572 (setq ct (mail-fetch-field "content-type")
573 ctl (and ct (mail-header-parse-content-type ct))
574 cte (mail-fetch-field "content-transfer-encoding")
575 cd (mail-fetch-field "content-disposition")
576 ;; Newlines in description should be stripped so as
577 ;; not to break the MIME tag into two or more lines.
578 description (message-fetch-field "content-description")
579 id (mail-fetch-field "content-id"))
581 (setq from (mail-fetch-field "from")))
582 ;; FIXME: In some circumstances, this code is running within
583 ;; an unibyte macro. mail-extract-address-components
584 ;; creates unibyte buffers. This `if', though not a perfect
585 ;; solution, avoids most of them.
587 (setq from (cadr (mail-extract-address-components from))))
589 (setq description (mail-decode-encoded-word-string
592 (not (string-match "/" (car ctl))))
593 (mm-dissect-singlepart
594 (list mm-dissect-default-type)
595 (and cte (intern (downcase (mail-header-strip cte))))
597 (and cd (mail-header-parse-content-disposition cd))
599 (setq type (split-string (car ctl) "/"))
600 (setq subtype (cadr type)
605 ((equal type "multipart")
606 (let ((mm-dissect-default-type (if (equal subtype "digest")
609 (start (cdr (assq 'start (cdr ctl)))))
610 (add-text-properties 0 (length (car ctl))
611 (mm-alist-to-plist (cdr ctl)) (car ctl))
613 ;; what really needs to be done here is a way to link a
614 ;; MIME handle back to it's parent MIME handle (in a multilevel
615 ;; MIME article). That would probably require changing
616 ;; the mm-handle API so we simply store the multipart buffer
617 ;; name as a text property of the "multipart/whatever" string.
618 (add-text-properties 0 (length (car ctl))
619 (list 'buffer (mm-copy-to-buffer)
623 (cons (car ctl) (mm-dissect-multipart ctl from))))
625 (mm-possibly-verify-or-decrypt
626 (mm-dissect-singlepart
628 (and cte (intern (downcase (mail-header-strip cte))))
630 (and cd (mail-header-parse-content-disposition cd))
634 (when (string-match " *<\\(.*\\)> *" id)
635 (setq id (match-string 1 id)))
636 (push (cons id result) mm-content-id-alist))
639 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
641 (if (equal "text/plain" (car ctl))
645 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
647 (defun mm-dissect-multipart (ctl from)
648 (goto-char (point-min))
649 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
650 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
653 (goto-char (point-max))
654 (if (re-search-backward close-delimiter nil t)
657 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
658 (while (and (< (point) end) (re-search-forward boundary end t))
659 (goto-char (match-beginning 0))
663 (narrow-to-region start (point))
664 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
666 (or (looking-at boundary)
668 (setq start (point)))
669 (when (and start (< start end))
672 (narrow-to-region start end)
673 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
674 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
676 (defun mm-copy-to-buffer ()
677 "Copy the contents of the current buffer to a fresh buffer."
678 (let ((obuf (current-buffer))
679 (mb (mm-multibyte-p))
681 (goto-char (point-min))
682 (search-forward-regexp "^\n" nil t)
685 (generate-new-buffer " *mm*")
686 ;; Preserve the data's unibyteness (for url-insert-file-contents).
687 (mm-set-buffer-multibyte mb)
688 (insert-buffer-substring obuf beg)
691 (defun mm-display-parts (handle &optional no-default)
692 (if (stringp (car handle))
693 (mapcar 'mm-display-parts (cdr handle))
694 (if (bufferp (car handle))
696 (narrow-to-region (point) (point))
697 (mm-display-part handle)
698 (goto-char (point-max)))
699 (mapcar 'mm-display-parts handle))))
701 (autoload 'mailcap-parse-mailcaps "mailcap")
702 (autoload 'mailcap-mime-info "mailcap")
704 (defun mm-display-part (handle &optional no-default)
705 "Display the MIME part represented by HANDLE.
706 Returns nil if the part is removed; inline if displayed inline;
707 external if displayed external."
709 (mailcap-parse-mailcaps)
710 (if (mm-handle-displayed-p handle)
711 (mm-remove-part handle)
712 (let* ((ehandle (if (equal (mm-handle-media-type handle)
713 "message/external-body")
715 (unless (mm-handle-cache handle)
716 (mm-extern-cache-contents handle))
717 (mm-handle-cache handle))
719 (type (mm-handle-media-type ehandle))
720 (method (mailcap-mime-info type))
721 (filename (or (mail-content-type-get
722 (mm-handle-disposition handle) 'filename)
723 (mail-content-type-get
724 (mm-handle-type handle) 'name)
726 (external mm-enable-external))
727 (if (and (mm-inlinable-p ehandle)
728 (mm-inlined-p ehandle))
731 (mm-display-inline handle)
735 (if (and (not method)
736 (equal "text" (car (split-string type "/"))))
739 (mm-insert-inline handle (mm-get-part handle))
742 (and method ;; If nil, we always use "save".
743 (stringp method) ;; 'mailcap-save-binary-file
744 (or (eq mm-enable-external t)
745 (and (eq mm-enable-external 'ask)
748 "Display part (" type
749 ") using external program"
750 ;; Can non-string method ever happen?
753 " \"" (format method filename) "\"")
758 handle (or method 'mailcap-save-binary-file))
760 handle 'mailcap-save-binary-file)))))))))
762 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
763 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
765 (defun mm-display-external (handle method)
766 "Display HANDLE using METHOD."
767 (let ((outbuf (current-buffer)))
768 (mm-with-unibyte-buffer
769 (if (functionp method)
770 (let ((cur (current-buffer)))
771 (if (eq method 'mailcap-save-binary-file)
773 (set-buffer (generate-new-buffer " *mm*"))
775 (mm-insert-part handle)
776 (mm-add-meta-html-tag handle)
777 (let ((win (get-buffer-window cur t)))
779 (select-window win)))
780 (switch-to-buffer (generate-new-buffer " *mm*")))
781 (buffer-disable-undo)
782 (mm-set-buffer-file-coding-system mm-binary-coding-system)
783 (insert-buffer-substring cur)
784 (goto-char (point-min))
786 (message "Viewing with %s" method))
787 (let ((mm (current-buffer))
788 (non-viewer (assq 'non-viewer
790 (mm-handle-media-type handle) t))))
794 (mm-save-part handle))
795 (when (and (not non-viewer)
797 (mm-handle-set-undisplayer handle mm)))))
798 ;; The function is a string to be executed.
799 (mm-insert-part handle)
800 (mm-add-meta-html-tag handle)
801 (let* ((dir (mm-make-temp-file
802 (expand-file-name "emm." mm-tmp-directory) 'dir))
804 (mail-content-type-get
805 (mm-handle-disposition handle) 'filename)
806 (mail-content-type-get
807 (mm-handle-type handle) 'name)))
808 (mime-info (mailcap-mime-info
809 (mm-handle-media-type handle) t))
810 (needsterm (or (assoc "needsterm" mime-info)
811 (assoc "needsterminal" mime-info)))
812 (copiousoutput (assoc "copiousoutput" mime-info))
814 ;; We create a private sub-directory where we store our files.
815 (set-file-modes dir #o700)
817 (setq file (expand-file-name
818 (gnus-map-function mm-file-name-rewrite-functions
819 (file-name-nondirectory filename))
821 ;; Use nametemplate (defined in RFC1524) if it is specified
823 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
825 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
826 (setq suffix (match-string 1 suffix))
827 ;; Otherwise, use a suffix according to
828 ;; `mailcap-mime-extensions'.
829 (setq suffix (car (rassoc (mm-handle-media-type handle)
830 mailcap-mime-extensions))))
831 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
833 (let ((coding-system-for-write mm-binary-coding-system))
834 (write-region (point-min) (point-max) file nil 'nomesg))
835 ;; The file is deleted after the viewer exists. If the users edits
836 ;; the file, changes will be lost. Set file to read-only to make it
838 (set-file-modes file #o400)
839 (message "Viewing with %s" method)
842 (let ((command (mm-mailcap-command
843 method file (mm-handle-type handle))))
846 (start-process "*display*" nil
847 mm-external-terminal-program
849 shell-command-switch command)
857 shell-command-switch command)))
860 (set-process-sentinel
861 (get-buffer-process buffer)
862 `(lambda (process state)
863 (if (eq 'exit (process-status process))
864 (gnus-configure-windows
865 ',gnus-current-window-configuration))))
866 (gnus-configure-windows 'display-term))
867 (mm-handle-set-external-undisplayer handle (cons file buffer)))
868 (message "Displaying %s..." command))
871 (with-current-buffer outbuf
877 (call-process shell-file-name nil
879 (generate-new-buffer " *mm*"))
883 method file (mm-handle-type handle)))
884 (if (buffer-live-p buffer)
885 (with-current-buffer buffer
888 (ignore-errors (delete-file file))
889 (ignore-errors (delete-directory
890 (file-name-directory file)))
891 (ignore-errors (kill-buffer buffer))))))
894 ;; Deleting the temp file should be postponed for some wrappers,
895 ;; shell scripts, and so on, which might exit right after having
896 ;; started a viewer command as a background job.
897 (let ((command (mm-mailcap-command
898 method file (mm-handle-type handle))))
901 (start-process "*display*"
903 (generate-new-buffer " *mm*"))
905 shell-command-switch command)
906 (set-process-sentinel
907 (get-buffer-process buffer)
908 (lexical-let ;; Don't use `let'.
909 ;; Function used to remove temp file and directory.
911 ;; Don't use `ignore-errors'.
917 ,(file-name-directory file))
919 ;; Form uses to kill the process buffer and
920 ;; remove the undisplayer.
922 (kill-buffer ,buffer)
924 (list 'mm-handle-set-undisplayer
927 ;; Message to be issued when the process exits.
928 (done (format "Displaying %s...done" command))
929 ;; In particular, the timer object (which is
930 ;; a vector in Emacs but is a list in XEmacs)
931 ;; requires that it is lexically scoped.
932 (timer (run-at-time 2.0 nil 'ignore)))
933 (if (featurep 'xemacs)
934 (lambda (process state)
935 (when (eq 'exit (process-status process))
936 (if (memq timer itimer-list)
937 (set-itimer-function timer fn)
939 (ignore-errors (eval fm))
940 (message "%s" done)))
941 (lambda (process state)
942 (when (eq 'exit (process-status process))
943 (if (memq timer timer-list)
944 (timer-set-function timer fn)
946 (ignore-errors (eval fm))
947 (message "%s" done)))))))
948 (mm-handle-set-external-undisplayer
949 handle (cons file buffer)))
950 (message "Displaying %s..." command))
953 (defun mm-mailcap-command (method file type-list)
954 (let ((ctl (cdr type-list))
958 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
960 (push (substring method beg (match-beginning 0)) out)
961 (setq beg (match-end 0)
962 total (match-string 0 method)
963 sub (match-string 1 method))
965 ((string= total "%%")
967 ((or (string= total "%s")
968 ;; We do our own quoting.
969 (string= total "'%s'")
970 (string= total "\"%s\""))
971 (setq uses-stdin nil)
972 (push (shell-quote-argument
973 (gnus-map-function mm-path-name-rewrite-functions file)) out))
974 ((string= total "%t")
975 (push (shell-quote-argument (car type-list)) out))
977 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
978 (push (substring method beg (length method)) out)
981 (push (shell-quote-argument
982 (gnus-map-function mm-path-name-rewrite-functions file))
984 (mapconcat 'identity (nreverse out) "")))
986 (defun mm-remove-parts (handles)
987 "Remove the displayed MIME parts represented by HANDLES."
988 (if (and (listp handles)
989 (bufferp (car handles)))
990 (mm-remove-part handles)
992 (while (setq handle (pop handles))
995 (when (buffer-live-p (get-text-property 0 'buffer handle))
996 (kill-buffer (get-text-property 0 'buffer handle))))
998 (stringp (car handle)))
999 (mm-remove-parts (cdr handle)))
1001 (mm-remove-part handle)))))))
1003 (defun mm-destroy-parts (handles)
1004 "Remove the displayed MIME parts represented by HANDLES."
1005 (if (and (listp handles)
1006 (bufferp (car handles)))
1007 (mm-destroy-part handles)
1009 (while (setq handle (pop handles))
1012 (when (buffer-live-p (get-text-property 0 'buffer handle))
1013 (kill-buffer (get-text-property 0 'buffer handle))))
1014 ((and (listp handle)
1015 (stringp (car handle)))
1016 (mm-destroy-parts handle))
1018 (mm-destroy-part handle)))))))
1020 (defun mm-remove-part (handle)
1021 "Remove the displayed MIME part represented by HANDLE."
1022 (when (listp handle)
1023 (let ((object (mm-handle-undisplayer handle)))
1026 ;; Internally displayed part.
1027 ((mm-annotationp object)
1028 (if (featurep 'xemacs)
1029 (delete-annotation object)))
1030 ((or (functionp object)
1032 (eq (car object) 'lambda)))
1034 ;; Externally displayed part.
1037 (while (get-buffer-process (cdr object))
1038 (interrupt-process (get-buffer-process (cdr object)))
1039 (message "Waiting for external displayer to die...")
1043 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1044 (message "Waiting for external displayer to die...done")
1045 (ignore-errors (delete-file (car object)))
1046 (ignore-errors (delete-directory (file-name-directory
1049 (when (buffer-live-p object)
1050 (kill-buffer object)))))
1051 (mm-handle-set-undisplayer handle nil))))
1053 (defun mm-display-inline (handle)
1054 (let* ((type (mm-handle-media-type handle))
1055 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1056 (funcall function handle)
1057 (goto-char (point-min))))
1059 (defun mm-assoc-string-match (alist type)
1060 (dolist (elem alist)
1061 (when (string-match (car elem) type)
1064 (defun mm-automatic-display-p (handle)
1065 "Say whether the user wants HANDLE to be displayed automatically."
1066 (let ((methods mm-automatic-display)
1067 (type (mm-handle-media-type handle))
1069 (while (setq method (pop methods))
1070 (when (and (not (mm-inline-override-p handle))
1071 (string-match method type))
1076 (defun mm-inlinable-p (handle &optional type)
1077 "Say whether HANDLE can be displayed inline.
1078 TYPE is the mime-type of the object; it defaults to the one given
1080 (unless type (setq type (mm-handle-media-type handle)))
1081 (let ((alist mm-inline-media-tests)
1084 (when (string-match (caar alist) type)
1085 (setq test (caddar alist)
1087 (setq test (funcall test handle)))
1091 (defun mm-inlined-p (handle)
1092 "Say whether the user wants HANDLE to be displayed inline."
1093 (let ((methods mm-inlined-types)
1094 (type (mm-handle-media-type handle))
1096 (while (setq method (pop methods))
1097 (when (and (not (mm-inline-override-p handle))
1098 (string-match method type))
1103 (defun mm-attachment-override-p (handle)
1104 "Say whether HANDLE should have attachment behavior overridden."
1105 (let ((types mm-attachment-override-types)
1106 (type (mm-handle-media-type handle))
1109 (while (setq ty (pop types))
1110 (when (and (string-match ty type)
1111 (mm-inlinable-p handle))
1112 (throw 'found t))))))
1114 (defun mm-inline-override-p (handle)
1115 "Say whether HANDLE should have inline behavior overridden."
1116 (let ((types mm-inline-override-types)
1117 (type (mm-handle-media-type handle))
1120 (while (setq ty (pop types))
1121 (when (string-match ty type)
1122 (throw 'found t))))))
1124 (defun mm-automatic-external-display-p (type)
1125 "Return the user-defined method for TYPE."
1126 (let ((methods mm-automatic-external-display)
1128 (while (setq method (pop methods))
1129 (when (string-match method type)
1134 (defun mm-destroy-part (handle)
1135 "Destroy the data structures connected to HANDLE."
1136 (when (listp handle)
1137 (mm-remove-part handle)
1138 (when (buffer-live-p (mm-handle-buffer handle))
1139 (kill-buffer (mm-handle-buffer handle)))))
1141 (defun mm-handle-displayed-p (handle)
1142 "Say whether HANDLE is displayed or not."
1143 (mm-handle-undisplayer handle))
1146 ;;; Functions for outputting parts
1149 (defmacro mm-with-part (handle &rest forms)
1150 "Run FORMS in the temp buffer containing the contents of HANDLE."
1151 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1152 ;; chars, so the buffer should be unibyte. It may happen that the
1153 ;; handle-buffer is multibyte for some reason, in which case now is a good
1154 ;; time to adjust it, since we know at this point that it should
1156 `(let* ((handle ,handle))
1157 (when (and (mm-handle-buffer handle)
1158 (buffer-name (mm-handle-buffer handle)))
1160 (mm-disable-multibyte)
1161 (insert-buffer-substring (mm-handle-buffer handle))
1162 (mm-decode-content-transfer-encoding
1163 (mm-handle-encoding handle)
1164 (mm-handle-media-type handle))
1166 (put 'mm-with-part 'lisp-indent-function 1)
1167 (put 'mm-with-part 'edebug-form-spec '(body))
1169 (defun mm-get-part (handle &optional no-cache)
1170 "Return the contents of HANDLE as a string.
1171 If NO-CACHE is non-nil, cached contents of a message/external-body part
1173 (if (and (not no-cache)
1174 (equal (mm-handle-media-type handle) "message/external-body"))
1176 (unless (mm-handle-cache handle)
1177 (mm-extern-cache-contents handle))
1178 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1180 (mm-with-part handle
1183 (defun mm-insert-part (handle &optional no-cache)
1184 "Insert the contents of HANDLE in the current buffer.
1185 If NO-CACHE is non-nil, cached contents of a message/external-body part
1187 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1190 (with-current-buffer (mm-handle-buffer handle)
1193 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1195 (mm-get-part handle no-cache)))))
1201 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1203 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1204 ;; Separate the extracted parts that have the same faces.
1208 (defun mm-file-name-delete-whitespace (file-name)
1209 "Remove all whitespace characters from FILE-NAME."
1210 (while (string-match "\\s-+" file-name)
1211 (setq file-name (replace-match "" t t file-name)))
1214 (defun mm-file-name-trim-whitespace (file-name)
1215 "Remove leading and trailing whitespace characters from FILE-NAME."
1216 (when (string-match "\\`\\s-+" file-name)
1217 (setq file-name (substring file-name (match-end 0))))
1218 (when (string-match "\\s-+\\'" file-name)
1219 (setq file-name (substring file-name 0 (match-beginning 0))))
1222 (defun mm-file-name-collapse-whitespace (file-name)
1223 "Collapse multiple whitespace characters in FILE-NAME."
1224 (while (string-match "\\s-\\s-+" file-name)
1225 (setq file-name (replace-match " " t t file-name)))
1228 (defun mm-file-name-replace-whitespace (file-name)
1229 "Replace whitespace characters in FILE-NAME with underscores.
1230 Set the option `mm-file-name-replace-whitespace' to any other
1231 string if you do not like underscores."
1232 (let ((s (or mm-file-name-replace-whitespace "_")))
1233 (while (string-match "\\s-" file-name)
1234 (setq file-name (replace-match s t t file-name))))
1237 (defun mm-file-name-delete-control (filename)
1238 "Delete control characters from FILENAME."
1239 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1241 (defun mm-file-name-delete-gotchas (filename)
1242 "Delete shell gotchas from FILENAME."
1243 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1244 (gnus-replace-in-string filename "^[.-]+" ""))
1246 (defun mm-save-part (handle &optional prompt)
1247 "Write HANDLE to a file.
1248 PROMPT overrides the default one used to ask user for a file name."
1249 (let ((filename (or (mail-content-type-get
1250 (mm-handle-disposition handle) 'filename)
1251 (mail-content-type-get
1252 (mm-handle-type handle) 'name)))
1255 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1256 (file-name-nondirectory filename))))
1258 (read-file-name (or prompt
1259 (format "Save MIME part to (default %s): "
1261 (or mm-default-directory default-directory)
1263 (if (file-directory-p file)
1264 (setq file (expand-file-name filename file))
1265 (setq file (expand-file-name
1266 file (or mm-default-directory default-directory))))
1267 (setq mm-default-directory (file-name-directory file))
1268 (and (or (not (file-exists-p file))
1269 (yes-or-no-p (format "File %s already exists; overwrite? "
1272 (mm-save-part-to-file handle file)
1275 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1276 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1277 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1278 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1279 Return t if meta tag is added or replaced."
1280 (when (equal (mm-handle-media-type handle) "text/html")
1282 (setq charset (mail-content-type-get (mm-handle-type handle)
1284 (setq charset (format "\
1285 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1286 (let ((case-fold-search t))
1287 (goto-char (point-min))
1288 (if (re-search-forward "\
1289 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1290 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
1291 (if (and (not force-charset)
1293 (string-match "\\`html\\'" (match-string 1)))
1294 ;; Don't modify existing meta tag.
1296 ;; Replace it with the one specifying charset.
1297 (replace-match charset)
1299 (if (re-search-forward "<head>\\s-*" nil t)
1300 (insert charset "\n")
1301 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1302 (insert "<head>\n" charset "\n</head>\n"))
1305 (defun mm-save-part-to-file (handle file)
1306 (mm-with-unibyte-buffer
1307 (mm-insert-part handle)
1308 (mm-add-meta-html-tag handle)
1309 (let ((current-file-modes (default-file-modes)))
1310 (set-default-file-modes mm-attachment-file-modes)
1312 ;; Don't re-compress .gz & al. Arguably we should make
1313 ;; `file-name-handler-alist' nil, but that would chop
1314 ;; ange-ftp, which is reasonable to use here.
1315 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1316 (set-default-file-modes current-file-modes)))))
1318 (defun mm-pipe-part (handle &optional cmd)
1319 "Pipe HANDLE to a process.
1320 Use CMD as the process."
1321 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1323 (gnus-read-shell-command
1324 "Shell command on MIME part: " mm-last-shell-command))))
1325 (mm-with-unibyte-buffer
1326 (mm-insert-part handle)
1327 (mm-add-meta-html-tag handle)
1328 (let ((coding-system-for-write 'binary))
1329 (shell-command-on-region (point-min) (point-max) command nil)))))
1331 (defun mm-interactively-view-part (handle)
1332 "Display HANDLE using METHOD."
1333 (let* ((type (mm-handle-media-type handle))
1335 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1336 (mailcap-mime-info type 'all)))
1337 (method (let ((minibuffer-local-completion-map
1338 mm-viewer-completion-map))
1339 (gnus-completing-read "Viewer" methods))))
1340 (when (string= method "")
1341 (error "No method given"))
1342 (if (string-match "^[^% \t]+$" method)
1343 (setq method (concat method " %s")))
1344 (mm-display-external handle method)))
1346 (defun mm-preferred-alternative (handles &optional preferred)
1347 "Say which of HANDLES are preferred."
1348 (let ((prec (if preferred (list preferred)
1349 (mm-preferred-alternative-precedence handles)))
1350 p h result type handle)
1351 (while (setq p (pop prec))
1354 (setq handle (car h))
1355 (setq type (mm-handle-media-type handle))
1356 (when (and (equal p type)
1357 (mm-automatic-display-p handle)
1358 (or (stringp (car handle))
1359 (not (mm-handle-disposition handle))
1360 (equal (car (mm-handle-disposition handle))
1368 (defun mm-preferred-alternative-precedence (handles)
1369 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1370 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1372 (dolist (disc (reverse mm-discouraged-alternatives))
1373 (dolist (elem (copy-sequence seq))
1374 (when (string-match disc elem)
1375 (setq seq (nconc (delete elem seq) (list elem))))))
1378 (defun mm-get-content-id (id)
1379 "Return the handle(s) referred to by ID."
1380 (cdr (assoc id mm-content-id-alist)))
1382 (defconst mm-image-type-regexps
1383 '(("/\\*.*XPM.\\*/" . xpm)
1387 ("\211PNG\r\n" . png)
1389 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1390 ("%!PS" . postscript))
1391 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1392 When the first bytes of an image file match REGEXP, it is assumed to
1393 be of image type IMAGE-TYPE.")
1395 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1396 (defun mm-image-type-from-buffer ()
1397 "Determine the image type from data in the current buffer.
1398 Value is a symbol specifying the image type or nil if type cannot
1400 (let ((types mm-image-type-regexps)
1402 (goto-char (point-min))
1403 (while (and types (null type))
1404 (let ((regexp (car (car types)))
1405 (image-type (cdr (car types))))
1406 (when (looking-at regexp)
1407 (setq type image-type))
1408 (setq types (cdr types))))
1411 (defun mm-get-image (handle)
1412 "Return an image instance based on HANDLE."
1413 (let ((type (mm-handle-media-subtype handle))
1415 ;; Allow some common translations.
1418 ((equal type "x-pixmap")
1420 ((equal type "x-xbitmap")
1422 ((equal type "x-portable-bitmap")
1425 (or (mm-handle-cache handle)
1426 (mm-with-unibyte-buffer
1427 (mm-insert-part handle)
1431 ;; Avoid testing `make-glyph' since W3 may define
1432 ;; a bogus version of it.
1433 (if (fboundp 'create-image)
1434 (create-image (buffer-string)
1435 (or (mm-image-type-from-buffer)
1438 (mm-create-image-xemacs type))))
1439 (mm-handle-set-cache handle spec))))))
1441 (defun mm-create-image-xemacs (type)
1442 (when (featurep 'xemacs)
1445 ;; xbm images require special handling, since
1446 ;; the only way to create glyphs from these
1447 ;; (without a ton of work) is to write them
1448 ;; out to a file, and then create a file
1450 (let ((file (mm-make-temp-file
1451 (expand-file-name "emm" mm-tmp-directory)
1455 (write-region (point-min) (point-max) file)
1456 (make-glyph (list (cons 'x file))))
1458 (delete-file file)))))
1462 (or (mm-image-type-from-buffer)
1464 :data (buffer-string)))))))
1466 (declare-function image-size "image.c" (spec &optional pixels frame))
1468 (defun mm-image-fit-p (handle)
1469 "Say whether the image in HANDLE will fit the current window."
1470 (let ((image (mm-get-image handle)))
1472 (if (featurep 'xemacs)
1473 ;; XEmacs' glyphs can actually tell us about their width, so
1474 ;; lets be nice and smart about them.
1475 (or mm-inline-large-images
1476 (and (<= (glyph-width image) (window-pixel-width))
1477 (<= (glyph-height image) (window-pixel-height))))
1478 (let* ((size (image-size image))
1481 (or mm-inline-large-images
1482 (and (<= h (1- (window-height))) ; Don't include mode line.
1483 (<= w (window-width)))))))))
1485 (defun mm-valid-image-format-p (format)
1486 "Say whether FORMAT can be displayed natively by Emacs."
1489 ((fboundp 'valid-image-instantiator-format-p)
1490 (valid-image-instantiator-format-p format))
1492 ((fboundp 'image-type-available-p)
1493 (and (display-graphic-p)
1494 (image-type-available-p format)))
1495 ;; Nobody else can do images yet.
1499 (defun mm-valid-and-fit-image-p (format handle)
1500 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1501 (and (mm-valid-image-format-p format)
1502 (mm-image-fit-p handle)))
1504 (defun mm-find-part-by-type (handles type &optional notp recursive)
1505 "Search in HANDLES for part with TYPE.
1506 If NOTP, returns first non-matching part.
1507 If RECURSIVE, search recursively."
1510 (if (and recursive (stringp (caar handles)))
1511 (if (setq handle (mm-find-part-by-type (cdar handles) type
1515 (not (equal (mm-handle-media-type (car handles)) type))
1516 (equal (mm-handle-media-type (car handles)) type))
1517 (setq handle (car handles)
1519 (setq handles (cdr handles)))
1522 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1523 (goto-char (point-min))
1524 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1526 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1528 (end (save-excursion
1529 (goto-char (point-max))
1530 (if (re-search-backward close-delimiter nil t)
1534 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1535 (while (and (not result)
1536 (re-search-forward boundary end t))
1537 (goto-char (match-beginning 0))
1541 (narrow-to-region start (1- (point)))
1542 (when (let* ((ct (mail-fetch-field "content-type"))
1543 (ctl (and ct (mail-header-parse-content-type ct))))
1545 (not (equal (car ctl) type))
1546 (equal (car ctl) type)))
1547 (setq result (buffer-string))))))
1549 (setq start (point)))
1550 (when (and (not result) start)
1553 (narrow-to-region start end)
1554 (when (let* ((ct (mail-fetch-field "content-type"))
1555 (ctl (and ct (mail-header-parse-content-type ct))))
1557 (not (equal (car ctl) type))
1558 (equal (car ctl) type)))
1559 (setq result (buffer-string))))))
1562 (defvar mm-security-handle nil)
1564 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1565 ;; HANDLE could be a CTL.
1567 (put-text-property 0 (length (car handle)) parameter value
1570 (autoload 'mm-view-pkcs7 "mm-view")
1572 (defun mm-possibly-verify-or-decrypt (parts ctl)
1573 (let ((type (car ctl))
1574 (subtype (cadr (split-string (car ctl) "/")))
1575 (mm-security-handle ctl) ;; (car CTL) is the type.
1576 protocol func functest)
1578 ((or (equal type "application/x-pkcs7-mime")
1579 (equal type "application/pkcs7-mime"))
1582 ((eq mm-decrypt-option 'never) nil)
1583 ((eq mm-decrypt-option 'always) t)
1584 ((eq mm-decrypt-option 'known) t)
1586 (format "Decrypt (S/MIME) part? "))))
1587 (mm-view-pkcs7 parts))
1588 (setq parts (mm-dissect-buffer t)))))
1589 ((equal subtype "signed")
1590 (unless (and (setq protocol
1591 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1592 (not (equal protocol "multipart/mixed")))
1593 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1594 (let ((protocols mm-verify-function-alist))
1596 (if (and (or (not (setq functest (nth 3 (car protocols))))
1597 (funcall functest parts ctl))
1598 (mm-find-part-by-type parts (caar protocols) nil t))
1599 (setq protocol (caar protocols)
1601 (setq protocols (cdr protocols))))))
1602 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1604 ((eq mm-verify-option 'never) nil)
1605 ((eq mm-verify-option 'always) t)
1606 ((eq mm-verify-option 'known)
1608 (or (not (setq functest
1609 (nth 3 (assoc protocol
1610 mm-verify-function-alist))))
1611 (funcall functest parts ctl))))
1614 (format "Verify signed (%s) part? "
1615 (or (nth 2 (assoc protocol mm-verify-function-alist))
1616 (format "protocol=%s" protocol))))))
1619 (setq parts (funcall func parts ctl))
1620 (mm-set-handle-multipart-parameter
1621 mm-security-handle 'gnus-details
1622 (format "Unknown sign protocol (%s)" protocol))))))
1623 ((equal subtype "encrypted")
1624 (unless (setq protocol
1625 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1626 ;; The message is broken.
1627 (let ((parts parts))
1629 (if (assoc (mm-handle-media-type (car parts))
1630 mm-decrypt-function-alist)
1631 (setq protocol (mm-handle-media-type (car parts))
1633 (setq parts (cdr parts))))))
1634 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1636 ((eq mm-decrypt-option 'never) nil)
1637 ((eq mm-decrypt-option 'always) t)
1638 ((eq mm-decrypt-option 'known)
1640 (or (not (setq functest
1641 (nth 3 (assoc protocol
1642 mm-decrypt-function-alist))))
1643 (funcall functest parts ctl))))
1646 (format "Decrypt (%s) part? "
1647 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1648 (format "protocol=%s" protocol))))))
1651 (setq parts (funcall func parts ctl))
1652 (mm-set-handle-multipart-parameter
1653 mm-security-handle 'gnus-details
1654 (format "Unknown encrypt protocol (%s)" protocol))))))
1658 (defun mm-multiple-handles (handles)
1659 (and (listp handles)
1660 (> (length handles) 1)
1661 (or (listp (car handles))
1662 (stringp (car handles)))))
1664 (defun mm-complicated-handles (handles)
1665 (and (listp (car handles))
1666 (> (length handles) 1)))
1668 (defun mm-merge-handles (handles1 handles2)
1670 (if (listp (car handles1))
1673 (if (listp (car handles2))
1677 (defun mm-readable-p (handle)
1678 "Say whether the content of HANDLE is readable."
1679 (and (< (with-current-buffer (mm-handle-buffer handle)
1680 (buffer-size)) 10000)
1681 (mm-with-unibyte-buffer
1682 (mm-insert-part handle)
1683 (and (eq (mm-body-7-or-8) '7bit)
1684 (not (mm-long-lines-p 76))))))
1686 (declare-function libxml-parse-html-region "xml.c"
1687 (start end &optional base-url))
1688 (declare-function shr-insert-document "shr" (dom))
1689 (defvar shr-blocked-images)
1691 (defun mm-shr (handle)
1692 ;; Require since we bind its variables.
1694 (let ((article-buffer (current-buffer))
1695 (shr-blocked-images (if (and (boundp 'gnus-summary-buffer)
1696 (buffer-name gnus-summary-buffer))
1697 (with-current-buffer gnus-summary-buffer
1698 gnus-blocked-images)
1699 shr-blocked-images))
1700 (shr-content-function (lambda (id)
1701 (let ((handle (mm-get-content-id id)))
1703 (mm-with-part handle
1704 (buffer-string))))))
1707 (setq handle (mm-dissect-buffer t)))
1708 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1710 (narrow-to-region (point) (point))
1711 (shr-insert-document
1712 (mm-with-part handle
1714 (setq charset (mm-charset-to-coding-system charset))
1715 (not (eq charset 'ascii)))
1717 (mm-decode-coding-string (buffer-string) charset)
1719 (mm-enable-multibyte))))
1720 (libxml-parse-html-region (point-min) (point-max)))))))
1722 (provide 'mm-decode)
1724 ;;; mm-decode.el ends here