1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl))
30 (autoload 'gnus-map-function "gnus-util")
31 (autoload 'gnus-replace-in-string "gnus-util")
32 (autoload 'gnus-read-shell-command "gnus-util")
34 (autoload 'mm-inline-partial "mm-partial")
35 (autoload 'mm-inline-external-body "mm-extern")
36 (autoload 'mm-extern-cache-contents "mm-extern")
37 (autoload 'mm-insert-inline "mm-view")
39 (autoload 'mm-archive-decoders "mm-archive")
40 (autoload 'mm-archive-dissect-and-inline "mm-archive")
41 (autoload 'mm-dissect-archive "mm-archive")
43 (defvar gnus-current-window-configuration)
45 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
46 (add-hook 'gnus-exit-gnus-hook 'mm-temp-files-delete)
48 (defgroup mime-display ()
49 "Display of MIME in mail and news articles."
50 :link '(custom-manual "(emacs-mime)Display Customization")
56 (defgroup mime-security ()
57 "MIME security in mail and news articles."
58 :link '(custom-manual "(emacs-mime)Display Customization")
63 (defface mm-command-output
66 (:foreground "ForestGreen"))
72 "Face used for displaying output from commands."
75 ;;; Convenience macros.
77 (defmacro mm-handle-buffer (handle)
79 (defmacro mm-handle-type (handle)
81 (defsubst mm-handle-media-type (handle)
82 (if (stringp (car handle))
84 (car (mm-handle-type handle))))
85 (defsubst mm-handle-media-supertype (handle)
86 (car (split-string (mm-handle-media-type handle) "/")))
87 (defsubst mm-handle-media-subtype (handle)
88 (cadr (split-string (mm-handle-media-type handle) "/")))
89 (defmacro mm-handle-encoding (handle)
91 (defmacro mm-handle-undisplayer (handle)
93 (defmacro mm-handle-set-undisplayer (handle function)
94 `(setcar (nthcdr 3 ,handle) ,function))
95 (defmacro mm-handle-disposition (handle)
97 (defmacro mm-handle-description (handle)
99 (defmacro mm-handle-cache (handle)
101 (defmacro mm-handle-set-cache (handle contents)
102 `(setcar (nthcdr 6 ,handle) ,contents))
103 (defmacro mm-handle-id (handle)
105 (defmacro mm-handle-multipart-original-buffer (handle)
106 `(get-text-property 0 'buffer (car ,handle)))
107 (defmacro mm-handle-multipart-from (handle)
108 `(get-text-property 0 'from (car ,handle)))
109 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
110 `(get-text-property 0 ,parameter (car ,handle)))
112 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
113 disposition description cache
115 `(list ,buffer ,type ,encoding ,undisplayer
116 ,disposition ,description ,cache ,id))
118 (defcustom mm-text-html-renderer
119 (cond ((fboundp 'libxml-parse-html-region) 'shr)
120 ((executable-find "w3m") 'gnus-w3m)
121 ((executable-find "links") 'links)
122 ((executable-find "lynx") 'lynx)
123 ((locate-library "html2text") 'html2text)
125 "Render of HTML contents.
126 It is one of defined renderer types, or a rendering function.
127 The defined renderer types are:
128 `shr': use the built-in Gnus HTML renderer;
129 `gnus-w3m': use Gnus renderer based on w3m;
130 `w3m': use emacs-w3m;
131 `w3m-standalone': use plain w3m;
134 `html2text': use html2text;
135 nil : use external viewer (default web browser)."
137 :type '(choice (const shr)
139 (const w3m :tag "emacs-w3m")
140 (const w3m-standalone :tag "standalone w3m" )
144 (const nil :tag "External viewer")
146 :group 'mime-display)
148 (defcustom mm-html-inhibit-images
149 (if (boundp 'mm-inline-text-html-with-images)
150 (not (symbol-value 'mm-inline-text-html-with-images))
152 "Non-nil means inhibit displaying of images inline in the article body."
155 :group 'mime-display)
157 (defcustom mm-html-blocked-images ""
158 "Regexp matching image URLs to be blocked, or nil meaning not to block.
159 Note that cid images that are embedded in a message won't be blocked."
161 :type '(choice (const :tag "Allow all" nil)
162 (regexp :tag "Regular expression"))
163 :group 'mime-display)
165 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
166 "Regexp matching URLs which are considered to be safe.
167 Some HTML mails might contain a nasty trick used by spammers, using
168 the <img> tag which is far more evil than the [Click Here!] button.
169 It is most likely intended to check whether the ominous spam mail has
170 reached your eyes or not, in which case the spammer knows for sure
171 that your email address is valid. It is done by embedding an
172 identifier string into a URL that you might automatically retrieve
173 when displaying the image. The default value is \"\\\\`cid:\" which only
174 matches parts embedded to the Multipart/Related type MIME contents and
175 Gnus will never connect to the spammer's site arbitrarily. You may
176 set this variable to nil if you consider all urls to be safe."
178 :type '(choice (regexp :tag "Regexp")
179 (const :tag "All URLs are safe" nil))
180 :group 'mime-display)
182 (defcustom mm-inline-text-html-with-w3m-keymap t
183 "If non-nil, use emacs-w3m command keys in the article buffer."
186 :group 'mime-display)
188 (defcustom mm-enable-external t
189 "Indicate whether external MIME handlers should be used.
191 If t, all defined external MIME handlers are used. If nil, files are saved by
192 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
193 before the external MIME handler is invoked."
195 :type '(choice (const :tag "Always" t)
196 (const :tag "Never" nil)
197 (const :tag "Ask" ask))
198 :group 'mime-display)
200 (defcustom mm-inline-media-tests
204 (mm-valid-and-fit-image-p 'jpeg handle)))
208 (mm-valid-and-fit-image-p 'png handle)))
212 (mm-valid-and-fit-image-p 'gif handle)))
216 (mm-valid-and-fit-image-p 'tiff handle)))
220 (mm-valid-and-fit-image-p 'xbm handle)))
224 (mm-valid-and-fit-image-p 'xbm handle)))
228 (mm-valid-and-fit-image-p 'xpm handle)))
232 (mm-valid-and-fit-image-p 'xpm handle)))
236 (mm-valid-and-fit-image-p 'bmp handle)))
237 ("image/x-portable-bitmap"
240 (mm-valid-and-fit-image-p 'pbm handle)))
241 ("text/plain" mm-inline-text identity)
242 ("text/enriched" mm-inline-text identity)
243 ("text/richtext" mm-inline-text identity)
244 ("text/x-patch" mm-display-patch-inline identity)
245 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
246 ("text/x-diff" mm-display-patch-inline identity)
247 ("application/emacs-lisp" mm-display-elisp-inline identity)
248 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
249 ("application/x-shellscript" mm-display-shell-script-inline identity)
250 ("application/x-sh" mm-display-shell-script-inline identity)
251 ("text/x-sh" mm-display-shell-script-inline identity)
252 ("application/javascript" mm-display-javascript-inline identity)
253 ("text/dns" mm-display-dns-inline identity)
254 ("text/x-org" mm-display-org-inline identity)
258 mm-text-html-renderer))
262 (or (featurep 'vcard)
263 (locate-library "vcard"))))
264 ("message/delivery-status" mm-inline-text identity)
265 ("message/rfc822" mm-inline-message identity)
266 ("message/partial" mm-inline-partial identity)
267 ("message/external-body" mm-inline-external-body identity)
268 ("text/.*" mm-inline-text identity)
269 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity)
270 ("application/zip" mm-archive-dissect-and-inline identity)
271 ("audio/wav" mm-inline-audio
273 (and (or (featurep 'nas-sound) (featurep 'native-sound))
274 (device-sound-enabled-p))))
278 (and (or (featurep 'nas-sound) (featurep 'native-sound))
279 (device-sound-enabled-p))))
280 ("application/pgp-signature" ignore identity)
281 ("application/x-pkcs7-signature" ignore identity)
282 ("application/pkcs7-signature" ignore identity)
283 ("application/x-pkcs7-mime" ignore identity)
284 ("application/pkcs7-mime" ignore identity)
285 ("multipart/alternative" ignore identity)
286 ("multipart/mixed" ignore identity)
287 ("multipart/related" ignore identity)
291 (and (mm-valid-image-format-p 'imagemagick)
292 (mm-with-unibyte-buffer
293 (mm-insert-part handle)
296 (if (fboundp 'create-image)
297 (create-image (buffer-string) 'imagemagick 'data-p)
298 (mm-create-image-xemacs
299 (mm-handle-media-subtype handle))))))
301 (setcar (cdr handle) (list "image/imagemagick"))
302 (mm-image-fit-p handle)))))))
303 ;; Disable audio and image
304 ("audio/.*" ignore ignore)
305 ("image/.*" ignore ignore)
306 ;; Default to displaying as text
307 (".*" mm-inline-text mm-readable-p))
308 "Alist of media types/tests saying whether types can be displayed inline."
309 :type '(repeat (list (regexp :tag "MIME type")
310 (function :tag "Display function")
311 (function :tag "Display test")))
312 :group 'mime-display)
314 (defcustom mm-inlined-types
315 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
316 "message/partial" "message/external-body" "application/emacs-lisp"
317 "application/x-emacs-lisp"
318 "application/pgp-signature" "application/x-pkcs7-signature"
319 "application/pkcs7-signature" "application/x-pkcs7-mime"
320 "application/pkcs7-mime"
321 "application/x-gtar-compressed"
324 ;; Mutt still uses this even though it has already been withdrawn.
326 "List of media types that are to be displayed inline.
327 See also `mm-inline-media-tests', which says how to display a media
329 :type '(repeat regexp)
330 :group 'mime-display)
332 (defcustom mm-keep-viewer-alive-types
333 '("application/postscript" "application/msword" "application/vnd.ms-excel"
334 "application/pdf" "application/x-dvi")
335 "List of media types for which the external viewer will not be killed
336 when selecting a different article."
338 :type '(repeat regexp)
339 :group 'mime-display)
341 (defcustom mm-automatic-display
342 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
343 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
344 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
345 "application/emacs-lisp" "application/x-emacs-lisp"
346 "application/x-pkcs7-signature"
347 "application/pkcs7-signature" "application/x-pkcs7-mime"
348 "application/pkcs7-mime"
349 ;; Mutt still uses this even though it has already been withdrawn.
352 "A list of MIME types to be displayed automatically."
353 :type '(repeat regexp)
354 :group 'mime-display)
356 (defcustom mm-attachment-override-types '("text/x-vcard"
357 "application/pkcs7-mime"
358 "application/x-pkcs7-mime"
359 "application/pkcs7-signature"
360 "application/x-pkcs7-signature")
361 "Types to have \"attachment\" ignored if they can be displayed inline."
362 :type '(repeat regexp)
363 :group 'mime-display)
365 (defcustom mm-inline-override-types nil
366 "Types to be treated as attachments even if they can be displayed inline."
367 :type '(repeat regexp)
368 :group 'mime-display)
370 (defcustom mm-automatic-external-display nil
371 "List of MIME type regexps that will be displayed externally automatically."
372 :type '(repeat regexp)
373 :group 'mime-display)
375 (defcustom mm-discouraged-alternatives nil
376 "List of MIME types that are discouraged when viewing multipart/alternative.
377 Viewing agents are supposed to view the last possible part of a message,
378 as that is supposed to be the richest. However, users may prefer other
379 types instead, and this list says what types are most unwanted. If,
380 for instance, text/html parts are very unwanted, and text/richtext are
381 somewhat unwanted, then the value of this variable should be set
384 (\"text/html\" \"text/richtext\")
386 Adding \"image/.*\" might also be useful. Spammers use it as the
387 preferred part of multipart/alternative messages. See also
388 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
389 enables you to choose manually one of two types those mails include."
390 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
391 :group 'mime-display)
393 (defcustom mm-tmp-directory
394 (if (fboundp 'temp-directory)
396 (if (boundp 'temporary-file-directory)
397 temporary-file-directory
399 "Where mm will store its temporary files."
401 :group 'mime-display)
403 (defcustom mm-inline-large-images nil
404 "If t, then all images fit in the buffer.
405 If `resize', try to resize the images so they fit."
407 (const :tag "Inline large images as they are." t)
408 (const :tag "Resize large images." resize)
409 (const :tag "Do not inline large images." nil))
410 :group 'mime-display)
412 (defcustom mm-file-name-rewrite-functions
413 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
414 "List of functions used for rewriting file names of MIME parts.
415 Each function takes a file name as input and returns a file name.
417 Ready-made functions include `mm-file-name-delete-control',
418 `mm-file-name-delete-gotchas' (you should not remove these two
419 functions), `mm-file-name-delete-whitespace',
420 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
421 `mm-file-name-replace-whitespace', `capitalize', `downcase',
422 `upcase', and `upcase-initials'."
423 :type '(list (set :inline t
424 (const mm-file-name-delete-control)
425 (const mm-file-name-delete-gotchas)
426 (const mm-file-name-delete-whitespace)
427 (const mm-file-name-trim-whitespace)
428 (const mm-file-name-collapse-whitespace)
429 (const mm-file-name-replace-whitespace)
433 (const upcase-initials)
437 :version "23.1" ;; No Gnus
438 :group 'mime-display)
441 (defvar mm-path-name-rewrite-functions nil
442 "*List of functions for rewriting the full file names of MIME parts.
443 This is used when viewing parts externally, and is meant for
444 transforming the absolute name so that non-compliant programs can find
445 the file where it's saved.
447 Each function takes a file name as input and returns a file name.")
449 (defvar mm-file-name-replace-whitespace nil
450 "String used for replacing whitespace characters; default is `\"_\"'.")
452 (defcustom mm-default-directory nil
453 "The default directory where mm will save files.
454 If not set, `default-directory' will be used."
455 :type '(choice directory (const :tag "Default" nil))
456 :group 'mime-display)
458 (defcustom mm-attachment-file-modes 384
459 "Set the mode bits of saved attachments to this integer."
462 :group 'mime-display)
464 (defcustom mm-external-terminal-program "xterm"
465 "The program to start an external terminal."
468 :group 'mime-display)
470 ;;; Internal variables.
472 (defvar mm-last-shell-command "")
473 (defvar mm-content-id-alist nil)
474 (defvar mm-postponed-undisplay-list nil)
475 (defvar mm-inhibit-auto-detect-attachment nil)
476 (defvar mm-temp-files-to-be-deleted nil
477 "List of temporary files scheduled to be deleted.")
478 (defvar mm-temp-files-cache-file (concat ".mm-temp-files-" (user-login-name))
479 "Name of a file that caches a list of temporary files to be deleted.
480 The file will be saved in the directory `mm-tmp-directory'.")
482 ;; According to RFC2046, in particular, in a digest, the default
483 ;; Content-Type value for a body part is changed from "text/plain" to
485 (defvar mm-dissect-default-type "text/plain")
487 (autoload 'mml2015-verify "mml2015")
488 (autoload 'mml2015-verify-test "mml2015")
489 (autoload 'mml-smime-verify "mml-smime")
490 (autoload 'mml-smime-verify-test "mml-smime")
492 (defvar mm-verify-function-alist
493 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
494 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
495 mm-uu-pgp-signed-test)
496 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
497 mml-smime-verify-test)
498 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
499 mml-smime-verify-test)))
501 (defcustom mm-verify-option 'never
502 "Option of verifying signed parts.
503 `never', not verify; `always', always verify;
504 `known', only verify known protocols. Otherwise, ask user.
506 When set to `always' or `known', you should add
507 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
508 result of the verification."
510 :type '(choice (item always)
512 (item :tag "only known protocols" known)
513 (item :tag "ask" nil))
514 :group 'mime-security)
516 (autoload 'mml2015-decrypt "mml2015")
517 (autoload 'mml2015-decrypt-test "mml2015")
519 (defvar mm-decrypt-function-alist
520 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
521 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
522 mm-uu-pgp-encrypted-test)))
524 (defcustom mm-decrypt-option nil
525 "Option of decrypting encrypted parts.
526 `never', not decrypt; `always', always decrypt;
527 `known', only decrypt known protocols. Otherwise, ask user."
529 :type '(choice (item always)
531 (item :tag "only known protocols" known)
532 (item :tag "ask" nil))
533 :group 'mime-security)
535 (defvar mm-viewer-completion-map
536 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
537 (set-keymap-parent map minibuffer-local-completion-map)
538 ;; Should we bind other key to minibuffer-complete-word?
539 (define-key map " " 'self-insert-command)
541 "Keymap for input viewer with completion.")
545 (defun mm-alist-to-plist (alist)
546 "Convert association list ALIST into the equivalent property-list form.
547 The plist is returned. This converts from
549 \((a . 1) (b . 2) (c . 3))
555 The original alist is not modified."
558 (let ((el (car alist)))
559 (setq plist (cons (cdr el) (cons (car el) plist))))
560 (setq alist (cdr alist)))
563 (defun mm-keep-viewer-alive-p (handle)
564 "Say whether external viewer for HANDLE should stay alive."
565 (let ((types mm-keep-viewer-alive-types)
566 (type (mm-handle-media-type handle))
569 (while (setq ty (pop types))
570 (when (string-match ty type)
571 (throw 'found t))))))
573 (defun mm-handle-set-external-undisplayer (handle function)
574 "Set the undisplayer for HANDLE to FUNCTION.
575 Postpone undisplaying of viewers for types in
576 `mm-keep-viewer-alive-types'."
577 (if (mm-keep-viewer-alive-p handle)
578 (let ((new-handle (copy-sequence handle)))
579 (mm-handle-set-undisplayer new-handle function)
580 (mm-handle-set-undisplayer handle nil)
581 (push new-handle mm-postponed-undisplay-list))
582 (mm-handle-set-undisplayer handle function)))
584 (defun mm-destroy-postponed-undisplay-list ()
585 (when mm-postponed-undisplay-list
586 (message "Destroying external MIME viewers")
587 (mm-destroy-parts mm-postponed-undisplay-list)))
589 (defun mm-temp-files-delete ()
590 "Delete temporary files and those parent directories.
591 Note that the deletion may fail if a program is catching hold of a file
592 under Windows or Cygwin. In that case, it schedules the deletion of
593 files left at the next time."
594 (let* ((coding-system-for-read mm-universal-coding-system)
595 (coding-system-for-write mm-universal-coding-system)
596 (cache-file (expand-file-name mm-temp-files-cache-file
598 (cache (when (file-exists-p cache-file)
599 (mm-with-multibyte-buffer
600 (insert-file-contents cache-file)
601 (split-string (buffer-string) "\n" t))))
603 (dolist (temp (append cache mm-temp-files-to-be-deleted))
604 (when (and (file-exists-p temp)
605 (if (file-directory-p temp)
606 ;; A parent directory left at the previous time.
608 (ignore-errors (delete-directory temp))
609 (file-exists-p temp))
610 ;; Delete a temporary file and its parent directory.
611 (ignore-errors (delete-file temp))
612 (or (file-exists-p temp)
614 (setq temp (file-name-directory temp))
615 (ignore-errors (delete-directory temp))
616 (file-exists-p temp)))))
619 ;; Schedule the deletion of the files left at the next time.
621 (write-region (concat (mapconcat 'identity (nreverse fails) "\n")
623 nil cache-file nil 'silent)
624 (set-file-modes cache-file #o600))
625 (when (file-exists-p cache-file)
626 (ignore-errors (delete-file cache-file))))
627 (setq mm-temp-files-to-be-deleted nil)))
629 (autoload 'message-fetch-field "message")
631 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
632 "Dissect the current buffer and return a list of MIME handles.
633 If NO-STRICT-MIME, don't require the message to have a
634 MIME-Version header before proceeding."
636 (let (ct ctl type subtype cte cd description id result)
638 (mail-narrow-to-head)
639 (when (or no-strict-mime
641 (mail-fetch-field "mime-version"))
642 (setq ct (mail-fetch-field "content-type")
643 ctl (and ct (mail-header-parse-content-type ct))
644 cte (mail-fetch-field "content-transfer-encoding")
645 cd (or (mail-fetch-field "content-disposition")
648 (cadr (mm-assoc-string-match
649 mm-inline-media-tests
652 ;; Newlines in description should be stripped so as
653 ;; not to break the MIME tag into two or more lines.
654 description (message-fetch-field "content-description")
655 id (mail-fetch-field "content-id"))
657 (setq from (mail-fetch-field "from")))
658 ;; FIXME: In some circumstances, this code is running within
659 ;; a unibyte macro. mail-extract-address-components
660 ;; creates unibyte buffers. This `if', though not a perfect
661 ;; solution, avoids most of them.
663 (setq from (cadr (mail-extract-address-components from))))
665 (setq description (mail-decode-encoded-word-string
668 (not (string-match "/" (car ctl))))
669 (mm-dissect-singlepart
670 (list mm-dissect-default-type)
671 (and cte (intern (downcase (mail-header-strip cte))))
673 (and cd (mail-header-parse-content-disposition cd))
675 (setq type (split-string (car ctl) "/"))
676 (setq subtype (cadr type)
681 ((equal type "multipart")
682 (let ((mm-dissect-default-type (if (equal subtype "digest")
685 (start (cdr (assq 'start (cdr ctl)))))
686 (add-text-properties 0 (length (car ctl))
687 (mm-alist-to-plist (cdr ctl)) (car ctl))
689 ;; what really needs to be done here is a way to link a
690 ;; MIME handle back to it's parent MIME handle (in a multilevel
691 ;; MIME article). That would probably require changing
692 ;; the mm-handle API so we simply store the multipart buffer
693 ;; name as a text property of the "multipart/whatever" string.
694 (add-text-properties 0 (length (car ctl))
695 (list 'buffer (mm-copy-to-buffer)
699 (cons (car ctl) (mm-dissect-multipart ctl from))))
701 (mm-possibly-verify-or-decrypt
702 (mm-dissect-singlepart
704 (and cte (intern (downcase (mail-header-strip cte))))
706 (and cd (mail-header-parse-content-disposition cd))
710 (when (string-match " *<\\(.*\\)> *" id)
711 (setq id (match-string 1 id)))
712 (push (cons id result) mm-content-id-alist))
715 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
717 (if (equal "text/plain" (car ctl))
720 ;; Guess what the type of application/octet-stream parts should
722 (let ((filename (cdr (assq 'filename (cdr cdl)))))
723 (when (and (not mm-inhibit-auto-detect-attachment)
724 (equal (car ctl) "application/octet-stream")
726 (string-match "\\.\\([^.]+\\)$" filename))
727 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
729 (setcar ctl new-type)))))
732 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
733 (decoder (assoc (car ctl) (mm-archive-decoders))))
735 ;; Do automatic decoding
737 (executable-find (caddr decoder)))
738 (mm-dissect-archive handle)
741 (defun mm-dissect-multipart (ctl from)
742 (goto-char (point-min))
743 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
744 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
747 (goto-char (point-max))
748 (if (re-search-backward close-delimiter nil t)
751 (mm-inhibit-auto-detect-attachment
752 (equal (car ctl) "multipart/encrypted")))
753 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
754 (while (and (< (point) end) (re-search-forward boundary end t))
755 (goto-char (match-beginning 0))
759 (narrow-to-region start (point))
760 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
762 (or (looking-at boundary)
764 (setq start (point)))
765 (when (and start (< start end))
768 (narrow-to-region start end)
769 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
770 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
772 (defun mm-copy-to-buffer ()
773 "Copy the contents of the current buffer to a fresh buffer."
774 (let ((obuf (current-buffer))
775 (mb (mm-multibyte-p))
777 (goto-char (point-min))
778 (search-forward-regexp "^\n" nil t)
781 (generate-new-buffer " *mm*")
782 ;; Preserve the data's unibyteness (for url-insert-file-contents).
783 (mm-set-buffer-multibyte mb)
784 (insert-buffer-substring obuf beg)
787 (defun mm-display-parts (handle &optional no-default)
788 (if (stringp (car handle))
789 (mapcar 'mm-display-parts (cdr handle))
790 (if (bufferp (car handle))
792 (narrow-to-region (point) (point))
793 (mm-display-part handle)
794 (goto-char (point-max)))
795 (mapcar 'mm-display-parts handle))))
797 (autoload 'mailcap-parse-mailcaps "mailcap")
798 (autoload 'mailcap-mime-info "mailcap")
800 (defun mm-head-p (&optional point)
801 "Return non-nil if point is in the article header."
802 (let ((point (or point (point))))
805 (and (not (re-search-backward "^$" nil t))
806 (re-search-forward "^$" nil t)))))
808 (defun mm-display-part (handle &optional no-default force)
809 "Display the MIME part represented by HANDLE.
810 Returns nil if the part is removed; inline if displayed inline;
811 external if displayed external."
813 (mailcap-parse-mailcaps)
815 (mm-handle-displayed-p handle))
816 (mm-remove-part handle)
817 (let* ((ehandle (if (equal (mm-handle-media-type handle)
818 "message/external-body")
820 (unless (mm-handle-cache handle)
821 (mm-extern-cache-contents handle))
822 (mm-handle-cache handle))
824 (type (mm-handle-media-type ehandle))
825 (method (mailcap-mime-info type))
826 (filename (or (mail-content-type-get
827 (mm-handle-disposition handle) 'filename)
828 (mail-content-type-get
829 (mm-handle-type handle) 'name)
831 (external mm-enable-external)
832 (decoder (assoc (car (mm-handle-type handle))
833 (mm-archive-decoders))))
836 (executable-find (caddr decoder)))
837 (mm-archive-dissect-and-inline handle)
839 ((and (mm-inlinable-p ehandle)
840 (mm-inlined-p ehandle))
843 (re-search-forward "^$" nil t)
845 (mm-display-inline handle)
849 (if (and (not method)
850 (equal "text" (car (split-string type "/"))))
853 (mm-insert-inline handle (mm-get-part handle))
856 (and method ;; If nil, we always use "save".
857 (or (eq mm-enable-external t)
858 (and (eq mm-enable-external 'ask)
861 "Display part (" type
865 "using external program \""
866 (format method filename) "\"")
868 "by calling `%s' on the contents)" method))
872 handle (or method 'mailcap-save-binary-file))
874 handle 'mailcap-save-binary-file)))))))))
876 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
877 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
878 (declare-function term-mode "term" ())
879 (declare-function term-char-mode "term" ())
881 (defun mm-display-external (handle method)
882 "Display HANDLE using METHOD."
883 (let ((outbuf (current-buffer)))
884 (mm-with-unibyte-buffer
885 (if (functionp method)
886 (let ((cur (current-buffer)))
887 (if (eq method 'mailcap-save-binary-file)
889 (set-buffer (generate-new-buffer " *mm*"))
891 (mm-insert-part handle)
892 (mm-add-meta-html-tag handle)
893 (let ((win (get-buffer-window cur t)))
895 (select-window win)))
896 (switch-to-buffer (generate-new-buffer " *mm*")))
897 (buffer-disable-undo)
898 (mm-set-buffer-file-coding-system mm-binary-coding-system)
899 (insert-buffer-substring cur)
900 (goto-char (point-min))
902 (message "Viewing with %s" method))
903 (let ((mm (current-buffer))
904 (non-viewer (assq 'non-viewer
906 (mm-handle-media-type handle) t))))
910 (when (and (boundp 'gnus-summary-buffer)
911 (bufferp gnus-summary-buffer)
912 (buffer-name gnus-summary-buffer))
913 ;; So that we pop back to the right place, sort of.
914 (switch-to-buffer gnus-summary-buffer)
915 (switch-to-buffer mm))
916 (delete-other-windows)
918 (mm-save-part handle))
919 (when (and (not non-viewer)
921 (mm-handle-set-undisplayer handle mm)))))
922 ;; The function is a string to be executed.
923 (mm-insert-part handle)
924 (mm-add-meta-html-tag handle)
925 (let* ((dir (mm-make-temp-file
926 (expand-file-name "emm." mm-tmp-directory) 'dir))
928 (mail-content-type-get
929 (mm-handle-disposition handle) 'filename)
930 (mail-content-type-get
931 (mm-handle-type handle) 'name)))
932 (mime-info (mailcap-mime-info
933 (mm-handle-media-type handle) t))
934 (needsterm (or (assoc "needsterm" mime-info)
935 (assoc "needsterminal" mime-info)))
936 (copiousoutput (assoc "copiousoutput" mime-info))
938 ;; We create a private sub-directory where we store our files.
939 (set-file-modes dir #o700)
941 (setq file (expand-file-name
942 (gnus-map-function mm-file-name-rewrite-functions
943 (file-name-nondirectory filename))
945 ;; Use nametemplate (defined in RFC1524) if it is specified
947 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
949 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
950 (setq suffix (match-string 1 suffix))
951 ;; Otherwise, use a suffix according to
952 ;; `mailcap-mime-extensions'.
953 (setq suffix (car (rassoc (mm-handle-media-type handle)
954 mailcap-mime-extensions))))
955 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
957 (let ((coding-system-for-write mm-binary-coding-system))
958 (write-region (point-min) (point-max) file nil 'nomesg))
959 ;; The file is deleted after the viewer exists. If the users edits
960 ;; the file, changes will be lost. Set file to read-only to make it
962 (set-file-modes file #o400)
963 (message "Viewing with %s" method)
966 (let ((command (mm-mailcap-command
967 method file (mm-handle-type handle))))
970 (set-process-sentinel
971 (start-process "*display*" nil
972 mm-external-terminal-program
974 shell-command-switch command)
975 `(lambda (process state)
976 (if (eq 'exit (process-status process))
980 (ignore-errors (delete-file ,file))
981 (ignore-errors (delete-directory
982 ,(file-name-directory
991 shell-command-switch command)))
994 (set-process-sentinel
995 (get-buffer-process buffer)
996 `(lambda (process state)
997 (when (eq 'exit (process-status process))
998 (ignore-errors (delete-file ,file))
1000 (delete-directory ,(file-name-directory file)))
1001 (gnus-configure-windows
1002 ',gnus-current-window-configuration))))
1003 (gnus-configure-windows 'display-term))
1004 (mm-handle-set-external-undisplayer handle (cons file buffer))
1005 (add-to-list 'mm-temp-files-to-be-deleted file t))
1006 (message "Displaying %s..." command))
1009 (with-current-buffer outbuf
1015 (call-process shell-file-name nil
1017 (generate-new-buffer " *mm*"))
1019 shell-command-switch
1021 method file (mm-handle-type handle)))
1022 (if (buffer-live-p buffer)
1023 (with-current-buffer buffer
1026 (ignore-errors (delete-file file))
1027 (ignore-errors (delete-directory
1028 (file-name-directory file)))
1029 (ignore-errors (kill-buffer buffer))))))
1032 ;; Deleting the temp file should be postponed for some wrappers,
1033 ;; shell scripts, and so on, which might exit right after having
1034 ;; started a viewer command as a background job.
1035 (let ((command (mm-mailcap-command
1036 method file (mm-handle-type handle))))
1038 (let ((process-connection-type nil))
1039 (start-process "*display*"
1041 (generate-new-buffer " *mm*"))
1043 shell-command-switch command)
1044 (set-process-sentinel
1045 (get-buffer-process buffer)
1046 (lexical-let ((outbuf outbuf)
1051 (lambda (process state)
1052 (when (eq (process-status process) 'exit)
1056 (ignore-errors (delete-file file))
1057 (ignore-errors (delete-directory
1058 (file-name-directory file)))))
1059 (when (buffer-live-p outbuf)
1060 (with-current-buffer outbuf
1061 (let ((buffer-read-only nil)
1064 (let ((start (point)))
1066 handle (with-current-buffer buffer
1068 (put-text-property start (point)
1069 'face 'mm-command-output))
1070 (goto-char point))))
1071 (when (buffer-live-p buffer)
1072 (kill-buffer buffer)))
1073 (message "Displaying %s...done" command)))))
1074 (mm-handle-set-external-undisplayer
1075 handle (cons file buffer))
1076 (add-to-list 'mm-temp-files-to-be-deleted file t))
1077 (message "Displaying %s..." command))
1080 (defun mm-mailcap-command (method file type-list)
1081 (let ((ctl (cdr type-list))
1085 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1087 (push (substring method beg (match-beginning 0)) out)
1088 (setq beg (match-end 0)
1089 total (match-string 0 method)
1090 sub (match-string 1 method))
1092 ((string= total "%%")
1094 ((or (string= total "%s")
1095 ;; We do our own quoting.
1096 (string= total "'%s'")
1097 (string= total "\"%s\""))
1098 (setq uses-stdin nil)
1099 (push (shell-quote-argument
1100 (gnus-map-function mm-path-name-rewrite-functions file)) out))
1101 ((string= total "%t")
1102 (push (shell-quote-argument (car type-list)) out))
1104 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
1105 (push (substring method beg (length method)) out)
1108 (push (shell-quote-argument
1109 (gnus-map-function mm-path-name-rewrite-functions file))
1111 (mapconcat 'identity (nreverse out) "")))
1113 (defun mm-remove-parts (handles)
1114 "Remove the displayed MIME parts represented by HANDLES."
1115 (if (and (listp handles)
1116 (bufferp (car handles)))
1117 (mm-remove-part handles)
1119 (while (setq handle (pop handles))
1122 (when (buffer-live-p (get-text-property 0 'buffer handle))
1123 (kill-buffer (get-text-property 0 'buffer handle))))
1124 ((and (listp handle)
1125 (stringp (car handle)))
1126 (mm-remove-parts (cdr handle)))
1128 (mm-remove-part handle)))))))
1130 (defun mm-destroy-parts (handles)
1131 "Remove the displayed MIME parts represented by HANDLES."
1132 (if (and (listp handles)
1133 (bufferp (car handles)))
1134 (mm-destroy-part handles)
1136 (while (setq handle (pop handles))
1139 (when (buffer-live-p (get-text-property 0 'buffer handle))
1140 (kill-buffer (get-text-property 0 'buffer handle))))
1141 ((and (listp handle)
1142 (stringp (car handle)))
1143 (mm-destroy-parts handle))
1145 (mm-destroy-part handle)))))))
1147 (defun mm-remove-part (handle)
1148 "Remove the displayed MIME part represented by HANDLE."
1149 (when (listp handle)
1150 (let ((object (mm-handle-undisplayer handle)))
1153 ;; Internally displayed part.
1154 ((mm-annotationp object)
1155 (if (featurep 'xemacs)
1156 (delete-annotation object)))
1157 ((or (functionp object)
1159 (eq (car object) 'lambda)))
1161 ;; Externally displayed part.
1164 (while (get-buffer-process (cdr object))
1165 (interrupt-process (get-buffer-process (cdr object)))
1166 (message "Waiting for external displayer to die...")
1170 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1171 (message "Waiting for external displayer to die...done")
1172 (ignore-errors (delete-file (car object)))
1173 (ignore-errors (delete-directory (file-name-directory
1176 (when (buffer-live-p object)
1177 (kill-buffer object)))))
1178 (mm-handle-set-undisplayer handle nil))))
1180 (defun mm-display-inline (handle)
1181 (let* ((type (mm-handle-media-type handle))
1182 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1183 (funcall function handle)
1184 (goto-char (point-min))))
1186 (defun mm-assoc-string-match (alist type)
1187 (dolist (elem alist)
1188 (when (string-match (car elem) type)
1191 (defun mm-automatic-display-p (handle)
1192 "Say whether the user wants HANDLE to be displayed automatically."
1193 (let ((methods mm-automatic-display)
1194 (type (mm-handle-media-type handle))
1196 (while (setq method (pop methods))
1197 (when (and (not (mm-inline-override-p handle))
1198 (string-match method type))
1203 (defun mm-inlinable-p (handle &optional type)
1204 "Say whether HANDLE can be displayed inline.
1205 TYPE is the mime-type of the object; it defaults to the one given
1207 (unless type (setq type (mm-handle-media-type handle)))
1208 (let ((alist mm-inline-media-tests)
1211 (when (string-match (caar alist) type)
1212 (setq test (caddar alist)
1214 (setq test (funcall test handle)))
1218 (defun mm-inlined-p (handle)
1219 "Say whether the user wants HANDLE to be displayed inline."
1220 (let ((methods mm-inlined-types)
1221 (type (mm-handle-media-type handle))
1223 (while (setq method (pop methods))
1224 (when (and (not (mm-inline-override-p handle))
1225 (string-match method type))
1230 (defun mm-attachment-override-p (handle)
1231 "Say whether HANDLE should have attachment behavior overridden."
1232 (let ((types mm-attachment-override-types)
1233 (type (mm-handle-media-type handle))
1236 (while (setq ty (pop types))
1237 (when (and (string-match ty type)
1238 (mm-inlinable-p handle))
1239 (throw 'found t))))))
1241 (defun mm-inline-override-p (handle)
1242 "Say whether HANDLE should have inline behavior overridden."
1243 (let ((types mm-inline-override-types)
1244 (type (mm-handle-media-type handle))
1247 (while (setq ty (pop types))
1248 (when (string-match ty type)
1249 (throw 'found t))))))
1251 (defun mm-automatic-external-display-p (type)
1252 "Return the user-defined method for TYPE."
1253 (let ((methods mm-automatic-external-display)
1255 (while (setq method (pop methods))
1256 (when (string-match method type)
1261 (defun mm-destroy-part (handle)
1262 "Destroy the data structures connected to HANDLE."
1263 (when (listp handle)
1264 (mm-remove-part handle)
1265 (when (buffer-live-p (mm-handle-buffer handle))
1266 (kill-buffer (mm-handle-buffer handle)))))
1268 (defun mm-handle-displayed-p (handle)
1269 "Say whether HANDLE is displayed or not."
1270 (mm-handle-undisplayer handle))
1273 ;;; Functions for outputting parts
1276 (defmacro mm-with-part (handle &rest forms)
1277 "Run FORMS in the temp buffer containing the contents of HANDLE."
1278 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1279 ;; chars, so the buffer should be unibyte. It may happen that the
1280 ;; handle-buffer is multibyte for some reason, in which case now is a good
1281 ;; time to adjust it, since we know at this point that it should
1283 `(let* ((handle ,handle))
1284 (when (and (mm-handle-buffer handle)
1285 (buffer-name (mm-handle-buffer handle)))
1287 (mm-disable-multibyte)
1288 (insert-buffer-substring (mm-handle-buffer handle))
1289 (mm-decode-content-transfer-encoding
1290 (mm-handle-encoding handle)
1291 (mm-handle-media-type handle))
1293 (put 'mm-with-part 'lisp-indent-function 1)
1294 (put 'mm-with-part 'edebug-form-spec '(body))
1296 (defun mm-get-part (handle &optional no-cache)
1297 "Return the contents of HANDLE as a string.
1298 If NO-CACHE is non-nil, cached contents of a message/external-body part
1300 (if (and (not no-cache)
1301 (equal (mm-handle-media-type handle) "message/external-body"))
1303 (unless (mm-handle-cache handle)
1304 (mm-extern-cache-contents handle))
1305 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1307 (mm-with-part handle
1310 (defun mm-insert-part (handle &optional no-cache)
1311 "Insert the contents of HANDLE in the current buffer.
1312 If NO-CACHE is non-nil, cached contents of a message/external-body part
1314 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1317 (with-current-buffer (mm-handle-buffer handle)
1320 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1322 (mm-get-part handle no-cache)))))
1328 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1330 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1331 ;; Separate the extracted parts that have the same faces.
1335 (defun mm-file-name-delete-whitespace (file-name)
1336 "Remove all whitespace characters from FILE-NAME."
1337 (while (string-match "\\s-+" file-name)
1338 (setq file-name (replace-match "" t t file-name)))
1341 (defun mm-file-name-trim-whitespace (file-name)
1342 "Remove leading and trailing whitespace characters from FILE-NAME."
1343 (when (string-match "\\`\\s-+" file-name)
1344 (setq file-name (substring file-name (match-end 0))))
1345 (when (string-match "\\s-+\\'" file-name)
1346 (setq file-name (substring file-name 0 (match-beginning 0))))
1349 (defun mm-file-name-collapse-whitespace (file-name)
1350 "Collapse multiple whitespace characters in FILE-NAME."
1351 (while (string-match "\\s-\\s-+" file-name)
1352 (setq file-name (replace-match " " t t file-name)))
1355 (defun mm-file-name-replace-whitespace (file-name)
1356 "Replace whitespace characters in FILE-NAME with underscores.
1357 Set the option `mm-file-name-replace-whitespace' to any other
1358 string if you do not like underscores."
1359 (let ((s (or mm-file-name-replace-whitespace "_")))
1360 (while (string-match "\\s-" file-name)
1361 (setq file-name (replace-match s t t file-name))))
1364 (defun mm-file-name-delete-control (filename)
1365 "Delete control characters from FILENAME."
1366 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1368 (defun mm-file-name-delete-gotchas (filename)
1369 "Delete shell gotchas from FILENAME."
1370 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1371 (gnus-replace-in-string filename "^[.-]+" ""))
1373 (defun mm-save-part (handle &optional prompt)
1374 "Write HANDLE to a file.
1375 PROMPT overrides the default one used to ask user for a file name."
1376 (let ((filename (or (mail-content-type-get
1377 (mm-handle-disposition handle) 'filename)
1378 (mail-content-type-get
1379 (mm-handle-type handle) 'name)))
1382 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1383 (file-name-nondirectory filename))))
1389 (format "Save MIME part to (default %s): "
1391 (or mm-default-directory default-directory)
1392 (expand-file-name (or filename "")
1393 (or mm-default-directory default-directory))))
1394 (cond ((or (not file) (equal file ""))
1395 (message "Please enter a file name")
1397 ((and (file-directory-p file)
1399 (message "Please enter a non-directory file name")
1404 (if (file-directory-p file)
1405 (setq file (expand-file-name filename file))
1406 (setq file (expand-file-name
1407 file (or mm-default-directory default-directory))))
1408 (setq mm-default-directory (file-name-directory file))
1409 (and (or (not (file-exists-p file))
1410 (yes-or-no-p (format "File %s already exists; overwrite? "
1413 (mm-save-part-to-file handle file)
1416 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1417 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1418 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1419 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1420 Return t if meta tag is added or replaced."
1421 (when (equal (mm-handle-media-type handle) "text/html")
1423 (setq charset (mail-content-type-get (mm-handle-type handle)
1425 (setq charset (format "\
1426 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1427 (let ((case-fold-search t))
1428 (goto-char (point-min))
1429 (if (re-search-forward "\
1430 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1431 text/\\(\\sw+\\)\\(?:;\\s-*charset=\\([^\"'>]+\\)\\)?[^>]*>" nil t)
1432 (if (and (not force-charset)
1434 (string-match "\\`html\\'" (match-string 1)))
1435 ;; Don't modify existing meta tag.
1437 ;; Replace it with the one specifying charset.
1438 (replace-match charset)
1440 (if (re-search-forward "<head>\\s-*" nil t)
1441 (insert charset "\n")
1442 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1443 (insert "<head>\n" charset "\n</head>\n"))
1446 (defun mm-save-part-to-file (handle file)
1447 (mm-with-unibyte-buffer
1448 (mm-insert-part handle)
1449 (mm-add-meta-html-tag handle)
1450 (let ((current-file-modes (default-file-modes)))
1451 (set-default-file-modes mm-attachment-file-modes)
1453 ;; Don't re-compress .gz & al. Arguably we should make
1454 ;; `file-name-handler-alist' nil, but that would chop
1455 ;; ange-ftp, which is reasonable to use here.
1456 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1457 (set-default-file-modes current-file-modes)))))
1459 (defun mm-pipe-part (handle &optional cmd)
1460 "Pipe HANDLE to a process.
1461 Use CMD as the process."
1462 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1464 (gnus-read-shell-command
1465 "Shell command on MIME part: " mm-last-shell-command))))
1466 (mm-with-unibyte-buffer
1467 (mm-insert-part handle)
1468 (mm-add-meta-html-tag handle)
1469 (let ((coding-system-for-write 'binary))
1470 (shell-command-on-region (point-min) (point-max) command nil)))))
1472 (autoload 'gnus-completing-read "gnus-util")
1474 (defun mm-interactively-view-part (handle)
1475 "Display HANDLE using METHOD."
1476 (let* ((type (mm-handle-media-type handle))
1478 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1479 (mailcap-mime-info type 'all)))
1480 (method (let ((minibuffer-local-completion-map
1481 mm-viewer-completion-map))
1482 (completing-read "Viewer: " methods))))
1483 (when (string= method "")
1484 (error "No method given"))
1485 (if (string-match "^[^% \t]+$" method)
1486 (setq method (concat method " %s")))
1487 (mm-display-external handle method)))
1489 (defun mm-preferred-alternative (handles &optional preferred)
1490 "Say which of HANDLES are preferred."
1491 (let ((prec (if preferred (list preferred)
1492 (mm-preferred-alternative-precedence handles)))
1493 p h result type handle)
1494 (while (setq p (pop prec))
1497 (setq handle (car h))
1498 (setq type (mm-handle-media-type handle))
1499 (when (and (equal p type)
1500 (mm-automatic-display-p handle)
1501 (or (stringp (car handle))
1502 (not (mm-handle-disposition handle))
1503 (equal (car (mm-handle-disposition handle))
1511 (defun mm-preferred-alternative-precedence (handles)
1512 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1513 (setq handles (reverse handles))
1514 (dolist (disc (reverse mm-discouraged-alternatives))
1515 (dolist (handle (copy-sequence handles))
1516 (when (string-match disc (mm-handle-media-type handle))
1517 (setq handles (nconc (delete handle handles) (list handle))))))
1518 ;; Remove empty parts.
1519 (dolist (handle (copy-sequence handles))
1520 (when (and (bufferp (mm-handle-buffer handle))
1521 (not (with-current-buffer (mm-handle-buffer handle)
1522 (goto-char (point-min))
1523 (re-search-forward "[^ \t\n]" nil t))))
1524 (setq handles (nconc (delete handle handles) (list handle)))))
1525 (mapcar #'mm-handle-media-type handles))
1527 (defun mm-get-content-id (id)
1528 "Return the handle(s) referred to by ID."
1529 (cdr (assoc id mm-content-id-alist)))
1531 (defconst mm-image-type-regexps
1532 '(("/\\*.*XPM.\\*/" . xpm)
1536 ("\211PNG\r\n" . png)
1538 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1539 ("%!PS" . postscript))
1540 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1541 When the first bytes of an image file match REGEXP, it is assumed to
1542 be of image type IMAGE-TYPE.")
1544 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1545 (defun mm-image-type-from-buffer ()
1546 "Determine the image type from data in the current buffer.
1547 Value is a symbol specifying the image type or nil if type cannot
1549 (let ((types mm-image-type-regexps)
1551 (goto-char (point-min))
1552 (while (and types (null type))
1553 (let ((regexp (car (car types)))
1554 (image-type (cdr (car types))))
1555 (when (looking-at regexp)
1556 (setq type image-type))
1557 (setq types (cdr types))))
1560 (defun mm-get-image (handle)
1561 "Return an image instance based on HANDLE."
1562 (let ((type (mm-handle-media-subtype handle))
1564 ;; Allow some common translations.
1567 ((equal type "x-pixmap")
1569 ((equal type "x-xbitmap")
1571 ((equal type "x-portable-bitmap")
1574 (or (mm-handle-cache handle)
1575 (mm-with-unibyte-buffer
1576 (mm-insert-part handle)
1580 ;; Avoid testing `make-glyph' since W3 may define
1581 ;; a bogus version of it.
1582 (if (fboundp 'create-image)
1583 (create-image (buffer-string)
1584 (or (mm-image-type-from-buffer)
1587 (mm-create-image-xemacs type))))
1588 (mm-handle-set-cache handle spec))))))
1590 (defun mm-create-image-xemacs (type)
1591 (when (featurep 'xemacs)
1594 ;; xbm images require special handling, since
1595 ;; the only way to create glyphs from these
1596 ;; (without a ton of work) is to write them
1597 ;; out to a file, and then create a file
1599 (let ((file (mm-make-temp-file
1600 (expand-file-name "emm" mm-tmp-directory)
1604 (write-region (point-min) (point-max) file)
1605 (make-glyph (list (cons 'x file))))
1607 (delete-file file)))))
1611 (or (mm-image-type-from-buffer)
1613 :data (buffer-string)))))))
1615 (declare-function image-size "image.c" (spec &optional pixels frame))
1617 (defun mm-image-fit-p (handle)
1618 "Say whether the image in HANDLE will fit the current window."
1619 (let ((image (mm-get-image handle)))
1621 (if (featurep 'xemacs)
1622 ;; XEmacs's glyphs can actually tell us about their width, so
1623 ;; let's be nice and smart about them.
1624 (or mm-inline-large-images
1625 (and (<= (glyph-width image) (window-pixel-width))
1626 (<= (glyph-height image) (window-pixel-height))))
1627 (let* ((size (image-size image))
1630 (or mm-inline-large-images
1631 (and (<= h (1- (window-height))) ; Don't include mode line.
1632 (<= w (window-width)))))))))
1634 (defun mm-valid-image-format-p (format)
1635 "Say whether FORMAT can be displayed natively by Emacs."
1638 ((fboundp 'valid-image-instantiator-format-p)
1639 (valid-image-instantiator-format-p format))
1641 ((fboundp 'image-type-available-p)
1642 (and (display-graphic-p)
1643 (image-type-available-p format)))
1644 ;; Nobody else can do images yet.
1648 (defun mm-valid-and-fit-image-p (format handle)
1649 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1650 (and (mm-valid-image-format-p format)
1651 (mm-image-fit-p handle)))
1653 (defun mm-find-part-by-type (handles type &optional notp recursive)
1654 "Search in HANDLES for part with TYPE.
1655 If NOTP, returns first non-matching part.
1656 If RECURSIVE, search recursively."
1659 (if (and recursive (stringp (caar handles)))
1660 (if (setq handle (mm-find-part-by-type (cdar handles) type
1664 (not (equal (mm-handle-media-type (car handles)) type))
1665 (equal (mm-handle-media-type (car handles)) type))
1666 (setq handle (car handles)
1668 (setq handles (cdr handles)))
1671 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1672 (goto-char (point-min))
1673 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1675 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1677 (end (save-excursion
1678 (goto-char (point-max))
1679 (if (re-search-backward close-delimiter nil t)
1683 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1684 (while (and (not result)
1685 (re-search-forward boundary end t))
1686 (goto-char (match-beginning 0))
1690 (narrow-to-region start (1- (point)))
1691 (when (let* ((ct (mail-fetch-field "content-type"))
1692 (ctl (and ct (mail-header-parse-content-type ct))))
1694 (not (equal (car ctl) type))
1695 (equal (car ctl) type)))
1696 (setq result (buffer-string))))))
1698 (setq start (point)))
1699 (when (and (not result) start)
1702 (narrow-to-region start end)
1703 (when (let* ((ct (mail-fetch-field "content-type"))
1704 (ctl (and ct (mail-header-parse-content-type ct))))
1706 (not (equal (car ctl) type))
1707 (equal (car ctl) type)))
1708 (setq result (buffer-string))))))
1711 (defvar mm-security-handle nil)
1713 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1714 ;; HANDLE could be a CTL.
1716 (put-text-property 0 (length (car handle)) parameter value
1719 (autoload 'mm-view-pkcs7 "mm-view")
1721 (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
1722 (let ((type (car ctl))
1723 (subtype (cadr (split-string (car ctl) "/")))
1724 (mm-security-handle ctl) ;; (car CTL) is the type.
1725 protocol func functest)
1727 ((or (equal type "application/x-pkcs7-mime")
1728 (equal type "application/pkcs7-mime"))
1731 ((eq mm-decrypt-option 'never) nil)
1732 ((eq mm-decrypt-option 'always) t)
1733 ((eq mm-decrypt-option 'known) t)
1735 (format "Decrypt (S/MIME) part? "))))
1736 (mm-view-pkcs7 parts from))
1737 (setq parts (mm-dissect-buffer t)))))
1738 ((equal subtype "signed")
1739 (unless (and (setq protocol
1740 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1741 (not (equal protocol "multipart/mixed")))
1742 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1743 (let ((protocols mm-verify-function-alist))
1745 (if (and (or (not (setq functest (nth 3 (car protocols))))
1746 (funcall functest parts ctl))
1747 (mm-find-part-by-type parts (caar protocols) nil t))
1748 (setq protocol (caar protocols)
1750 (setq protocols (cdr protocols))))))
1751 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1753 ((eq mm-verify-option 'never) nil)
1754 ((eq mm-verify-option 'always) t)
1755 ((eq mm-verify-option 'known)
1757 (or (not (setq functest
1758 (nth 3 (assoc protocol
1759 mm-verify-function-alist))))
1760 (funcall functest parts ctl))))
1763 (format "Verify signed (%s) part? "
1764 (or (nth 2 (assoc protocol mm-verify-function-alist))
1765 (format "protocol=%s" protocol))))))
1768 (setq parts (funcall func parts ctl))
1769 (mm-set-handle-multipart-parameter
1770 mm-security-handle 'gnus-details
1771 (format "Unknown sign protocol (%s)" protocol))))))
1772 ((equal subtype "encrypted")
1773 (unless (setq protocol
1774 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1775 ;; The message is broken.
1776 (let ((parts parts))
1778 (if (assoc (mm-handle-media-type (car parts))
1779 mm-decrypt-function-alist)
1780 (setq protocol (mm-handle-media-type (car parts))
1782 (setq parts (cdr parts))))))
1783 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1785 ((eq mm-decrypt-option 'never) nil)
1786 ((eq mm-decrypt-option 'always) t)
1787 ((eq mm-decrypt-option 'known)
1789 (or (not (setq functest
1790 (nth 3 (assoc protocol
1791 mm-decrypt-function-alist))))
1792 (funcall functest parts ctl))))
1795 (format "Decrypt (%s) part? "
1796 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1797 (format "protocol=%s" protocol))))))
1800 (setq parts (funcall func parts ctl))
1801 (mm-set-handle-multipart-parameter
1802 mm-security-handle 'gnus-details
1803 (format "Unknown encrypt protocol (%s)" protocol))))))
1807 (defun mm-multiple-handles (handles)
1808 (and (listp handles)
1809 (> (length handles) 1)
1810 (or (listp (car handles))
1811 (stringp (car handles)))))
1813 (defun mm-complicated-handles (handles)
1814 (and (listp (car handles))
1815 (> (length handles) 1)))
1817 (defun mm-merge-handles (handles1 handles2)
1819 (if (listp (car handles1))
1822 (if (listp (car handles2))
1826 (defun mm-readable-p (handle)
1827 "Say whether the content of HANDLE is readable."
1828 (and (< (with-current-buffer (mm-handle-buffer handle)
1829 (buffer-size)) 10000)
1830 (mm-with-unibyte-buffer
1831 (mm-insert-part handle)
1832 (and (eq (mm-body-7-or-8) '7bit)
1833 (not (mm-long-lines-p 76))))))
1835 (declare-function libxml-parse-html-region "xml.c"
1836 (start end &optional base-url discard-comments))
1837 (declare-function shr-insert-document "shr" (dom))
1838 (defvar shr-blocked-images)
1839 (defvar shr-use-fonts)
1841 (defun mm-shr (handle)
1842 ;; Require since we bind its variables.
1844 (let ((shr-width (if (and (boundp 'shr-use-fonts)
1848 (shr-content-function (lambda (id)
1849 (let ((handle (mm-get-content-id id)))
1851 (mm-with-part handle
1852 (buffer-string))))))
1853 (shr-inhibit-images mm-html-inhibit-images)
1854 (shr-blocked-images mm-html-blocked-images)
1857 (setq handle (mm-dissect-buffer t)))
1858 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1860 (narrow-to-region (point) (point))
1861 (shr-insert-document
1862 (mm-with-part handle
1866 (mm-charset-to-coding-system charset
1868 (not (eq charset 'ascii)))
1869 (mm-decode-coding-string (buffer-string) charset)
1870 (mm-string-as-multibyte (buffer-string)))
1872 (mm-enable-multibyte)))
1873 (goto-char (point-min))
1874 (setq case-fold-search t)
1875 (while (re-search-forward
1876 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1878 (cdr (assq (if (match-beginning 1)
1879 (string-to-number (match-string 1) 16)
1880 (string-to-number (match-string 2)))
1881 mm-extra-numeric-entities)))
1882 (replace-match (char-to-string char))))
1883 ;; Remove "soft hyphens".
1884 (goto-char (point-min))
1885 (while (search-forward "" nil t)
1886 (replace-match "" t t))
1887 (libxml-parse-html-region (point-min) (point-max))))
1890 (mm-convert-shr-links)
1891 (mm-handle-set-undisplayer
1894 (let ((inhibit-read-only t))
1895 (delete-region ,(point-min-marker)
1896 ,(point-max-marker))))))))
1900 (autoload 'widget-convert-button "wid-edit")
1902 (defun mm-convert-shr-links ()
1903 (let ((start (point-min))
1906 (< start (point-max)))
1907 (when (setq start (text-property-not-all start (point-max) 'shr-url nil))
1908 (setq end (next-single-property-change start 'shr-url nil (point-max)))
1909 (widget-convert-button
1911 :help-echo (get-text-property start 'help-echo)
1913 (get-text-property start 'shr-url))
1914 (put-text-property start end 'local-map nil)
1915 (dolist (overlay (overlays-at start))
1916 (overlay-put overlay 'face nil))
1917 (setq start end)))))
1919 (defun mm-handle-filename (handle)
1920 "Return filename of HANDLE if any."
1921 (or (mail-content-type-get (mm-handle-type handle)
1923 (mail-content-type-get (mm-handle-disposition handle)
1926 (provide 'mm-decode)
1932 ;;; mm-decode.el ends here