1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
28 (require 'gnus-mailcap)
30 (eval-when-compile (require 'cl)
34 (autoload 'executable-find "executable")
35 (autoload 'mm-inline-partial "mm-partial")
36 (autoload 'mm-inline-external-body "mm-extern")
37 (autoload 'mm-insert-inline "mm-view"))
39 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
41 (defgroup mime-display ()
42 "Display of MIME in mail and news articles."
43 :link '(custom-manual "(emacs-mime)Customization")
49 (defgroup mime-security ()
50 "MIME security in mail and news articles."
51 :link '(custom-manual "(emacs-mime)Customization")
56 ;;; Convenience macros.
58 (defmacro mm-handle-buffer (handle)
60 (defmacro mm-handle-type (handle)
62 (defsubst mm-handle-media-type (handle)
63 (if (stringp (car handle))
65 (car (mm-handle-type handle))))
66 (defsubst mm-handle-media-supertype (handle)
67 (car (split-string (mm-handle-media-type handle) "/")))
68 (defsubst mm-handle-media-subtype (handle)
69 (cadr (split-string (mm-handle-media-type handle) "/")))
70 (defmacro mm-handle-encoding (handle)
72 (defmacro mm-handle-undisplayer (handle)
74 (defmacro mm-handle-set-undisplayer (handle function)
75 `(setcar (nthcdr 3 ,handle) ,function))
76 (defmacro mm-handle-disposition (handle)
78 (defmacro mm-handle-description (handle)
80 (defmacro mm-handle-cache (handle)
82 (defmacro mm-handle-set-cache (handle contents)
83 `(setcar (nthcdr 6 ,handle) ,contents))
84 (defmacro mm-handle-id (handle)
86 (defmacro mm-handle-multipart-original-buffer (handle)
87 `(get-text-property 0 'buffer (car ,handle)))
88 (defmacro mm-handle-multipart-from (handle)
89 `(get-text-property 0 'from (car ,handle)))
90 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
91 `(get-text-property 0 ,parameter (car ,handle)))
93 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
94 disposition description cache
96 `(list ,buffer ,type ,encoding ,undisplayer
97 ,disposition ,description ,cache ,id))
99 (defcustom mm-text-html-renderer
100 (cond ((locate-library "w3") 'w3)
101 ((locate-library "w3m") 'w3m)
102 ((executable-find "links") 'links)
103 ((executable-find "lynx") 'lynx)
105 "Render of HTML contents.
106 It is one of defined renderer types, or a rendering function.
107 The defined renderer types are:
108 `w3' : using Emacs/W3;
109 `w3m' : using emacs-w3m;
110 `links': using links;
112 `html2text' : using html2text;
113 `nil' : using external viewer."
114 :type '(choice (const w3)
122 :group 'mime-display)
124 (defvar mm-inline-text-html-renderer nil
125 "Function used for rendering inline HTML contents.
126 It is suggested to customize `mm-text-html-renderer' instead.")
128 (defcustom mm-inline-text-html-with-images nil
129 "If non-nil, Gnus will allow retrieving images in the HTML contents
130 with <img> tags. It has no effect on Emacs/w3. See also
131 the documentation for the option `mm-w3m-safe-url-regexp'."
133 :group 'mime-display)
135 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
136 "Regexp that matches safe url names. Some HTML mails might have a
137 trick of spammers using <img> tags. It is likely to be intended to
138 verify whether you have read the mail. You can prevent your personal
139 informations from leaking by setting this to the regexp which matches
140 the safe url names. The value of the variable `w3m-safe-url-regexp'
141 will be bound with this value. You may set this value to nil if you
142 consider all the urls to be safe."
143 :type '(choice (regexp :tag "Regexp")
144 (const :tag "All URLs are safe" nil))
145 :group 'mime-display)
147 (defcustom mm-inline-text-html-with-w3m-keymap t
148 "If non-nil, use emacs-w3m command keys in the article buffer."
150 :group 'mime-display)
152 (defcustom mm-inline-media-tests
156 (mm-valid-and-fit-image-p 'jpeg handle)))
160 (mm-valid-and-fit-image-p 'png handle)))
164 (mm-valid-and-fit-image-p 'gif handle)))
168 (mm-valid-and-fit-image-p 'tiff handle)) )
172 (mm-valid-and-fit-image-p 'xbm handle)))
176 (mm-valid-and-fit-image-p 'xbm handle)))
180 (mm-valid-and-fit-image-p 'xpm handle)))
184 (mm-valid-and-fit-image-p 'xpm handle)))
188 (mm-valid-and-fit-image-p 'bmp handle)))
189 ("image/x-portable-bitmap"
192 (mm-valid-and-fit-image-p 'pbm handle)))
193 ("text/plain" mm-inline-text identity)
194 ("text/enriched" mm-inline-text identity)
195 ("text/richtext" mm-inline-text identity)
196 ("text/x-patch" mm-display-patch-inline
198 (locate-library "diff-mode")))
199 ("application/emacs-lisp" mm-display-elisp-inline identity)
200 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
204 (or mm-inline-text-html-renderer
205 mm-text-html-renderer)))
209 (or (featurep 'vcard)
210 (locate-library "vcard"))))
211 ("message/delivery-status" mm-inline-text identity)
212 ("message/rfc822" mm-inline-message identity)
213 ("message/partial" mm-inline-partial identity)
214 ("message/external-body" mm-inline-external-body identity)
215 ("text/.*" mm-inline-text identity)
216 ("audio/wav" mm-inline-audio
218 (and (or (featurep 'nas-sound) (featurep 'native-sound))
219 (device-sound-enabled-p))))
223 (and (or (featurep 'nas-sound) (featurep 'native-sound))
224 (device-sound-enabled-p))))
225 ("application/pgp-signature" ignore identity)
226 ("application/x-pkcs7-signature" ignore identity)
227 ("application/pkcs7-signature" ignore identity)
228 ("application/x-pkcs7-mime" ignore identity)
229 ("application/pkcs7-mime" ignore identity)
230 ("multipart/alternative" ignore identity)
231 ("multipart/mixed" ignore identity)
232 ("multipart/related" ignore identity)
233 ;; Disable audio and image
234 ("audio/.*" ignore ignore)
235 ("image/.*" ignore ignore)
236 ;; Default to displaying as text
237 (".*" mm-inline-text mm-readable-p))
238 "Alist of media types/tests saying whether types can be displayed inline."
239 :type '(repeat (list (string :tag "MIME type")
240 (function :tag "Display function")
241 (function :tag "Display test")))
242 :group 'mime-display)
244 (defcustom mm-inlined-types
245 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
246 "message/partial" "message/external-body" "application/emacs-lisp"
247 "application/x-emacs-lisp"
248 "application/pgp-signature" "application/x-pkcs7-signature"
249 "application/pkcs7-signature" "application/x-pkcs7-mime"
250 "application/pkcs7-mime")
251 "List of media types that are to be displayed inline.
252 See also `mm-inline-media-tests', which says how to display a media
254 :type '(repeat string)
255 :group 'mime-display)
257 (defcustom mm-keep-viewer-alive-types
258 '("application/postscript" "application/msword" "application/vnd.ms-excel"
259 "application/pdf" "application/x-dvi")
260 "List of media types for which the external viewer will not be killed
261 when selecting a different article."
262 :type '(repeat string)
263 :group 'mime-display)
265 (defcustom mm-automatic-display
266 '("text/plain" "text/enriched" "text/richtext" "text/html"
267 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
268 "message/rfc822" "text/x-patch" "application/pgp-signature"
269 "application/emacs-lisp" "application/x-emacs-lisp"
270 "application/x-pkcs7-signature"
271 "application/pkcs7-signature" "application/x-pkcs7-mime"
272 "application/pkcs7-mime")
273 "A list of MIME types to be displayed automatically."
274 :type '(repeat string)
275 :group 'mime-display)
277 (defcustom mm-attachment-override-types '("text/x-vcard"
278 "application/pkcs7-mime"
279 "application/x-pkcs7-mime"
280 "application/pkcs7-signature"
281 "application/x-pkcs7-signature")
282 "Types to have \"attachment\" ignored if they can be displayed inline."
283 :type '(repeat string)
284 :group 'mime-display)
286 (defcustom mm-inline-override-types nil
287 "Types to be treated as attachments even if they can be displayed inline."
288 :type '(repeat string)
289 :group 'mime-display)
291 (defcustom mm-automatic-external-display nil
292 "List of MIME type regexps that will be displayed externally automatically."
293 :type '(repeat string)
294 :group 'mime-display)
296 (defcustom mm-discouraged-alternatives nil
297 "List of MIME types that are discouraged when viewing multipart/alternative.
298 Viewing agents are supposed to view the last possible part of a message,
299 as that is supposed to be the richest. However, users may prefer other
300 types instead, and this list says what types are most unwanted. If,
301 for instance, text/html parts are very unwanted, and text/richtext are
302 somewhat unwanted, then the value of this variable should be set
305 (\"text/html\" \"text/richtext\")"
306 :type '(repeat string)
307 :group 'mime-display)
309 (defcustom mm-tmp-directory
310 (cond ((fboundp 'temp-directory) (temp-directory))
311 ((boundp 'temporary-file-directory) temporary-file-directory)
313 "Where mm will store its temporary files."
315 :group 'mime-display)
317 (defcustom mm-inline-large-images nil
318 "If non-nil, then all images fit in the buffer."
320 :group 'mime-display)
322 (defvar mm-file-name-rewrite-functions nil
323 "*List of functions used for rewriting file names of MIME parts.
324 Each function takes a file name as input and returns a file name.
326 Ready-made functions include
327 `mm-file-name-delete-whitespace',
328 `mm-file-name-trim-whitespace',
329 `mm-file-name-collapse-whitespace',
330 `mm-file-name-replace-whitespace',
331 `capitalize', `downcase', `upcase', and
334 (defvar mm-path-name-rewrite-functions nil
335 "*List of functions used for rewriting path names of MIME parts.
336 This is used when viewing parts externally , and is meant for
337 transforming the path name so that non-compliant programs can
338 find the file where it's saved.
340 Each function takes a file name as input and returns a file name.")
342 (defvar mm-file-name-replace-whitespace nil
343 "String used for replacing whitespace characters; default is `\"_\"'.")
345 (defcustom mm-default-directory nil
346 "The default directory where mm will save files.
347 If not set, `default-directory' will be used."
348 :type '(choice directory (const :tag "Default" nil))
349 :group 'mime-display)
351 (defcustom mm-external-terminal-program "xterm"
352 "The program to start an external terminal."
354 :group 'mime-display)
356 ;;; Internal variables.
358 (defvar mm-dissection-list nil)
359 (defvar mm-last-shell-command "")
360 (defvar mm-content-id-alist nil)
361 (defvar mm-postponed-undisplay-list nil)
363 ;; According to RFC2046, in particular, in a digest, the default
364 ;; Content-Type value for a body part is changed from "text/plain" to
366 (defvar mm-dissect-default-type "text/plain")
368 (autoload 'mml2015-verify "mml2015")
369 (autoload 'mml2015-verify-test "mml2015")
370 (autoload 'mml-smime-verify "mml-smime")
371 (autoload 'mml-smime-verify-test "mml-smime")
373 (defvar mm-verify-function-alist
374 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
375 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
376 mm-uu-pgp-signed-test)
377 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
378 mml-smime-verify-test)
379 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
380 mml-smime-verify-test)))
382 (defcustom mm-verify-option 'never
383 "Option of verifying signed parts.
384 `never', not verify; `always', always verify;
385 `known', only verify known protocols. Otherwise, ask user."
386 :type '(choice (item always)
388 (item :tag "only known protocols" known)
389 (item :tag "ask" nil))
390 :group 'mime-security)
392 (autoload 'mml2015-decrypt "mml2015")
393 (autoload 'mml2015-decrypt-test "mml2015")
395 (defvar mm-decrypt-function-alist
396 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
397 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
398 mm-uu-pgp-encrypted-test)))
400 (defcustom mm-decrypt-option nil
401 "Option of decrypting encrypted parts.
402 `never', not decrypt; `always', always decrypt;
403 `known', only decrypt known protocols. Otherwise, ask user."
404 :type '(choice (item always)
406 (item :tag "only known protocols" known)
407 (item :tag "ask" nil))
408 :group 'mime-security)
410 (defvar mm-viewer-completion-map
411 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
412 (set-keymap-parent map minibuffer-local-completion-map)
414 "Keymap for input viewer with completion.")
416 ;; Should we bind other key to minibuffer-complete-word?
417 (define-key mm-viewer-completion-map " " 'self-insert-command)
419 (defvar mm-viewer-completion-map
420 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
421 (set-keymap-parent map minibuffer-local-completion-map)
423 "Keymap for input viewer with completion.")
425 ;; Should we bind other key to minibuffer-complete-word?
426 (define-key mm-viewer-completion-map " " 'self-insert-command)
430 (defun mm-alist-to-plist (alist)
431 "Convert association list ALIST into the equivalent property-list form.
432 The plist is returned. This converts from
434 \((a . 1) (b . 2) (c . 3))
440 The original alist is not modified. See also `destructive-alist-to-plist'."
443 (let ((el (car alist)))
444 (setq plist (cons (cdr el) (cons (car el) plist))))
445 (setq alist (cdr alist)))
448 (defun mm-keep-viewer-alive-p (handle)
449 "Say whether external viewer for HANDLE should stay alive."
450 (let ((types mm-keep-viewer-alive-types)
451 (type (mm-handle-media-type handle))
454 (while (setq ty (pop types))
455 (when (string-match ty type)
456 (throw 'found t))))))
458 (defun mm-handle-set-external-undisplayer (handle function)
459 "Set the undisplayer for this handle; postpone undisplaying of viewers
460 for types in mm-keep-viewer-alive-types."
461 (if (mm-keep-viewer-alive-p handle)
462 (let ((new-handle (copy-sequence handle)))
463 (mm-handle-set-undisplayer new-handle function)
464 (mm-handle-set-undisplayer handle nil)
465 (push new-handle mm-postponed-undisplay-list))
466 (mm-handle-set-undisplayer handle function)))
468 (defun mm-destroy-postponed-undisplay-list ()
469 (when mm-postponed-undisplay-list
470 (message "Destroying external MIME viewers")
471 (mm-destroy-parts mm-postponed-undisplay-list)))
473 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
474 "Dissect the current buffer and return a list of MIME handles."
476 (let (ct ctl type subtype cte cd description id result from)
478 (mail-narrow-to-head)
479 (when (or no-strict-mime
481 (mail-fetch-field "mime-version"))
482 (setq ct (mail-fetch-field "content-type")
483 ctl (ignore-errors (mail-header-parse-content-type ct))
484 cte (mail-fetch-field "content-transfer-encoding")
485 cd (mail-fetch-field "content-disposition")
486 description (mail-fetch-field "content-description")
487 from (mail-fetch-field "from")
488 id (mail-fetch-field "content-id"))
489 ;; FIXME: In some circumstances, this code is running within
490 ;; an unibyte macro. mail-extract-address-components
491 ;; creates unibyte buffers. This `if', though not a perfect
492 ;; solution, avoids most of them.
494 (setq from (cadr (mail-extract-address-components from))))))
496 (setq cte (mail-header-strip cte)))
498 (not (string-match "/" (car ctl))))
499 (mm-dissect-singlepart
500 (list mm-dissect-default-type)
501 (and cte (intern (downcase (mail-header-remove-whitespace
502 (mail-header-remove-comments
505 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
507 (setq type (split-string (car ctl) "/"))
508 (setq subtype (cadr type)
513 ((equal type "multipart")
514 (let ((mm-dissect-default-type (if (equal subtype "digest")
517 (add-text-properties 0 (length (car ctl))
518 (mm-alist-to-plist (cdr ctl)) (car ctl))
520 ;; what really needs to be done here is a way to link a
521 ;; MIME handle back to it's parent MIME handle (in a multilevel
522 ;; MIME article). That would probably require changing
523 ;; the mm-handle API so we simply store the multipart buffert
524 ;; name as a text property of the "multipart/whatever" string.
525 (add-text-properties 0 (length (car ctl))
526 (list 'buffer (mm-copy-to-buffer))
528 (add-text-properties 0 (length (car ctl))
531 (cons (car ctl) (mm-dissect-multipart ctl))))
533 (mm-possibly-verify-or-decrypt
534 (mm-dissect-singlepart
536 (and cte (intern (downcase (mail-header-remove-whitespace
537 (mail-header-remove-comments
540 (and cd (ignore-errors
541 (mail-header-parse-content-disposition cd)))
545 (when (string-match " *<\\(.*\\)> *" id)
546 (setq id (match-string 1 id)))
547 (push (cons id result) mm-content-id-alist))
550 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
552 (if (equal "text/plain" (car ctl))
555 (let ((res (mm-make-handle
556 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
557 (push (car res) mm-dissection-list)
560 (defun mm-remove-all-parts ()
561 "Remove all MIME handles."
563 (mapcar 'mm-remove-part mm-dissection-list)
564 (setq mm-dissection-list nil))
566 (defun mm-dissect-multipart (ctl)
567 (goto-char (point-min))
568 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
569 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
572 (goto-char (point-max))
573 (if (re-search-backward close-delimiter nil t)
576 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
577 (while (and (< (point) end) (re-search-forward boundary end t))
578 (goto-char (match-beginning 0))
582 (narrow-to-region start (point))
583 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
585 (or (looking-at boundary)
587 (setq start (point)))
588 (when (and start (< start end))
591 (narrow-to-region start end)
592 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
593 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
595 (defun mm-copy-to-buffer ()
596 "Copy the contents of the current buffer to a fresh buffer."
598 (let ((flag enable-multibyte-characters)
599 (new-buffer (generate-new-buffer " *mm*")))
600 (goto-char (point-min))
601 (search-forward-regexp "^\n" nil t)
603 (narrow-to-region (point) (point-max))
605 (set-buffer-multibyte nil))
606 (copy-to-buffer new-buffer (point-min) (point-max))
608 (set-buffer-multibyte t)))
611 (defun mm-display-parts (handle &optional no-default)
612 (if (stringp (car handle))
613 (mapcar 'mm-display-parts (cdr handle))
614 (if (bufferp (car handle))
616 (narrow-to-region (point) (point))
617 (mm-display-part handle)
618 (goto-char (point-max)))
619 (mapcar 'mm-display-parts handle))))
621 (defun mm-display-part (handle &optional no-default)
622 "Display the MIME part represented by HANDLE.
623 Returns nil if the part is removed; inline if displayed inline;
624 external if displayed external."
626 (mailcap-parse-mailcaps)
627 (if (mm-handle-displayed-p handle)
628 (mm-remove-part handle)
629 (let* ((type (mm-handle-media-type handle))
630 (method (mailcap-mime-info type)))
631 (if (and (mm-inlinable-p handle)
632 (mm-inlined-p handle))
635 (mm-display-inline handle)
639 (if (and (not method)
640 (equal "text" (car (split-string type))))
643 (mm-insert-inline handle (mm-get-part handle))
646 handle (or method 'mailcap-save-binary-file)))))))))
648 (defun mm-display-external (handle method)
649 "Display HANDLE using METHOD."
650 (let ((outbuf (current-buffer)))
651 (mm-with-unibyte-buffer
652 (if (functionp method)
653 (let ((cur (current-buffer)))
654 (if (eq method 'mailcap-save-binary-file)
656 (set-buffer (generate-new-buffer " *mm*"))
658 (mm-insert-part handle)
659 (let ((win (get-buffer-window cur t)))
661 (select-window win)))
662 (switch-to-buffer (generate-new-buffer " *mm*")))
663 (buffer-disable-undo)
664 (mm-set-buffer-file-coding-system mm-binary-coding-system)
665 (insert-buffer-substring cur)
666 (goto-char (point-min))
667 (message "Viewing with %s" method)
668 (let ((mm (current-buffer))
669 (non-viewer (assq 'non-viewer
671 (mm-handle-media-type handle) t))))
675 (mm-save-part handle))
676 (when (and (not non-viewer)
678 (mm-handle-set-undisplayer handle mm)))))
679 ;; The function is a string to be executed.
680 (mm-insert-part handle)
681 (let* ((dir (mm-make-temp-file
682 (expand-file-name "emm." mm-tmp-directory) 'dir))
684 (mail-content-type-get
685 (mm-handle-disposition handle) 'filename)
686 (mail-content-type-get
687 (mm-handle-type handle) 'name)))
688 (mime-info (mailcap-mime-info
689 (mm-handle-media-type handle) t))
690 (needsterm (or (assoc "needsterm" mime-info)
691 (assoc "needsterminal" mime-info)))
692 (copiousoutput (assoc "copiousoutput" mime-info))
694 ;; We create a private sub-directory where we store our files.
695 (set-file-modes dir 448)
697 (setq file (expand-file-name
698 (gnus-map-function mm-file-name-rewrite-functions
699 (file-name-nondirectory filename))
701 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
702 (let ((coding-system-for-write mm-binary-coding-system))
703 (write-region (point-min) (point-max) file nil 'nomesg))
704 (message "Viewing with %s" method)
707 (let ((command (mm-mailcap-command
708 method file (mm-handle-type handle))))
711 (start-process "*display*" nil
712 mm-external-terminal-program
714 shell-command-switch command)
722 shell-command-switch command)))
725 (set-process-sentinel
726 (get-buffer-process buffer)
727 `(lambda (process state)
728 (if (eq 'exit (process-status process))
729 (gnus-configure-windows
730 ',gnus-current-window-configuration))))
731 (gnus-configure-windows 'display-term))
732 (mm-handle-set-external-undisplayer handle (cons file buffer)))
733 (message "Displaying %s..." command))
736 (with-current-buffer outbuf
742 (call-process shell-file-name nil
744 (generate-new-buffer " *mm*"))
748 method file (mm-handle-type handle)))
749 (if (buffer-live-p buffer)
754 (ignore-errors (delete-file file))
755 (ignore-errors (delete-directory
756 (file-name-directory file)))
757 (ignore-errors (kill-buffer buffer))))))
760 (let ((command (mm-mailcap-command
761 method file (mm-handle-type handle))))
763 (start-process "*display*"
765 (generate-new-buffer " *mm*"))
767 shell-command-switch command)
768 (mm-handle-set-external-undisplayer
769 handle (cons file buffer)))
770 (message "Displaying %s..." command))
773 (defun mm-mailcap-command (method file type-list)
774 (let ((ctl (cdr type-list))
778 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
780 (push (substring method beg (match-beginning 0)) out)
781 (setq beg (match-end 0)
782 total (match-string 0 method)
783 sub (match-string 1 method))
785 ((string= total "%%")
787 ((or (string= total "%s")
788 ;; We do our own quoting.
789 (string= total "'%s'")
790 (string= total "\"%s\""))
791 (setq uses-stdin nil)
793 (gnus-map-function mm-path-name-rewrite-functions file)) out))
794 ((string= total "%t")
795 (push (mm-quote-arg (car type-list)) out))
797 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
798 (push (substring method beg (length method)) out)
802 (gnus-map-function mm-path-name-rewrite-functions file))
804 (mapconcat 'identity (nreverse out) "")))
806 (defun mm-remove-parts (handles)
807 "Remove the displayed MIME parts represented by HANDLES."
808 (if (and (listp handles)
809 (bufferp (car handles)))
810 (mm-remove-part handles)
812 (while (setq handle (pop handles))
815 (when (buffer-live-p (get-text-property 0 'buffer handle))
816 (kill-buffer (get-text-property 0 'buffer handle))))
818 (stringp (car handle)))
819 (mm-remove-parts (cdr handle)))
821 (mm-remove-part handle)))))))
823 (defun mm-destroy-parts (handles)
824 "Remove the displayed MIME parts represented by HANDLES."
825 (if (and (listp handles)
826 (bufferp (car handles)))
827 (mm-destroy-part handles)
829 (while (setq handle (pop handles))
832 (when (buffer-live-p (get-text-property 0 'buffer handle))
833 (kill-buffer (get-text-property 0 'buffer handle))))
835 (stringp (car handle)))
836 (mm-destroy-parts handle))
838 (mm-destroy-part handle)))))))
840 (defun mm-remove-part (handle)
841 "Remove the displayed MIME part represented by HANDLE."
843 (let ((object (mm-handle-undisplayer handle)))
846 ;; Internally displayed part.
847 ((mm-annotationp object)
848 (delete-annotation object))
849 ((or (functionp object)
851 (eq (car object) 'lambda)))
853 ;; Externally displayed part.
855 (ignore-errors (delete-file (car object)))
856 (ignore-errors (delete-directory (file-name-directory (car object))))
857 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
859 (when (buffer-live-p object)
860 (kill-buffer object)))))
861 (mm-handle-set-undisplayer handle nil))))
863 (defun mm-display-inline (handle)
864 (let* ((type (mm-handle-media-type handle))
865 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
866 (funcall function handle)
867 (goto-char (point-min))))
869 (defun mm-assoc-string-match (alist type)
871 (when (string-match (car elem) type)
874 (defun mm-automatic-display-p (handle)
875 "Say whether the user wants HANDLE to be displayed automatically."
876 (let ((methods mm-automatic-display)
877 (type (mm-handle-media-type handle))
879 (while (setq method (pop methods))
880 (when (and (not (mm-inline-override-p handle))
881 (string-match method type))
886 (defun mm-inlinable-p (handle)
887 "Say whether HANDLE can be displayed inline."
888 (let ((alist mm-inline-media-tests)
889 (type (mm-handle-media-type handle))
892 (when (string-match (caar alist) type)
893 (setq test (caddar alist)
895 (setq test (funcall test handle)))
899 (defun mm-inlined-p (handle)
900 "Say whether the user wants HANDLE to be displayed inline."
901 (let ((methods mm-inlined-types)
902 (type (mm-handle-media-type handle))
904 (while (setq method (pop methods))
905 (when (and (not (mm-inline-override-p handle))
906 (string-match method type))
911 (defun mm-attachment-override-p (handle)
912 "Say whether HANDLE should have attachment behavior overridden."
913 (let ((types mm-attachment-override-types)
914 (type (mm-handle-media-type handle))
917 (while (setq ty (pop types))
918 (when (and (string-match ty type)
919 (mm-inlinable-p handle))
920 (throw 'found t))))))
922 (defun mm-inline-override-p (handle)
923 "Say whether HANDLE should have inline behavior overridden."
924 (let ((types mm-inline-override-types)
925 (type (mm-handle-media-type handle))
928 (while (setq ty (pop types))
929 (when (string-match ty type)
930 (throw 'found t))))))
932 (defun mm-automatic-external-display-p (type)
933 "Return the user-defined method for TYPE."
934 (let ((methods mm-automatic-external-display)
936 (while (setq method (pop methods))
937 (when (string-match method type)
942 (defun mm-destroy-part (handle)
943 "Destroy the data structures connected to HANDLE."
945 (mm-remove-part handle)
946 (when (buffer-live-p (mm-handle-buffer handle))
947 (kill-buffer (mm-handle-buffer handle)))))
949 (defun mm-handle-displayed-p (handle)
950 "Say whether HANDLE is displayed or not."
951 (mm-handle-undisplayer handle))
954 ;;; Functions for outputting parts
957 (defun mm-get-part (handle)
958 "Return the contents of HANDLE as a string."
959 (mm-with-unibyte-buffer
960 (insert (with-current-buffer (mm-handle-buffer handle)
961 (mm-with-unibyte-current-buffer-mule4
963 (mm-decode-content-transfer-encoding
964 (mm-handle-encoding handle)
965 (mm-handle-media-type handle))
968 (defun mm-insert-part (handle)
969 "Insert the contents of HANDLE in the current buffer."
970 (let ((cur (current-buffer)))
972 (if (member (mm-handle-media-supertype handle) '("text" "message"))
974 (insert-buffer-substring (mm-handle-buffer handle))
976 (mm-decode-content-transfer-encoding
977 (mm-handle-encoding handle)
978 (mm-handle-media-type handle))
979 (let ((temp (current-buffer)))
981 (insert-buffer-substring temp))))
982 (mm-with-unibyte-buffer
983 (insert-buffer-substring (mm-handle-buffer handle))
985 (mm-decode-content-transfer-encoding
986 (mm-handle-encoding handle)
987 (mm-handle-media-type handle))
988 (let ((temp (current-buffer)))
990 (insert-buffer-substring temp))))))))
992 (defun mm-file-name-delete-whitespace (file-name)
993 "Remove all whitespace characters from FILE-NAME."
994 (while (string-match "\\s-+" file-name)
995 (setq file-name (replace-match "" t t file-name)))
998 (defun mm-file-name-trim-whitespace (file-name)
999 "Remove leading and trailing whitespace characters from FILE-NAME."
1000 (when (string-match "\\`\\s-+" file-name)
1001 (setq file-name (substring file-name (match-end 0))))
1002 (when (string-match "\\s-+\\'" file-name)
1003 (setq file-name (substring file-name 0 (match-beginning 0))))
1006 (defun mm-file-name-collapse-whitespace (file-name)
1007 "Collapse multiple whitespace characters in FILE-NAME."
1008 (while (string-match "\\s-\\s-+" file-name)
1009 (setq file-name (replace-match " " t t file-name)))
1012 (defun mm-file-name-replace-whitespace (file-name)
1013 "Replace whitespace characters in FILE-NAME with underscores.
1014 Set `mm-file-name-replace-whitespace' to any other string if you do not
1016 (let ((s (or mm-file-name-replace-whitespace "_")))
1017 (while (string-match "\\s-" file-name)
1018 (setq file-name (replace-match s t t file-name))))
1021 (defun mm-save-part (handle)
1022 "Write HANDLE to a file."
1023 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1024 (filename (mail-content-type-get
1025 (mm-handle-disposition handle) 'filename))
1028 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1029 (file-name-nondirectory filename))))
1031 (read-file-name "Save MIME part to: "
1032 (or mm-default-directory default-directory)
1033 nil nil (or filename name "")))
1034 (setq mm-default-directory (file-name-directory file))
1035 (and (or (not (file-exists-p file))
1036 (yes-or-no-p (format "File %s already exists; overwrite? "
1039 (mm-save-part-to-file handle file)
1042 (defun mm-save-part-to-file (handle file)
1043 (mm-with-unibyte-buffer
1044 (mm-insert-part handle)
1045 (let ((coding-system-for-write 'binary)
1046 ;; Don't re-compress .gz & al. Arguably we should make
1047 ;; `file-name-handler-alist' nil, but that would chop
1048 ;; ange-ftp, which is reasonable to use here.
1049 (inhibit-file-name-operation 'write-region)
1050 (inhibit-file-name-handlers
1051 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1052 (write-region (point-min) (point-max) file))))
1054 (defun mm-pipe-part (handle)
1055 "Pipe HANDLE to a process."
1056 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1058 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1059 (mm-with-unibyte-buffer
1060 (mm-insert-part handle)
1061 (let ((coding-system-for-write 'binary))
1062 (shell-command-on-region (point-min) (point-max) command nil)))))
1064 (defun mm-interactively-view-part (handle)
1065 "Display HANDLE using METHOD."
1066 (let* ((type (mm-handle-media-type handle))
1068 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1069 (mailcap-mime-info type 'all)))
1070 (method (let ((minibuffer-local-completion-map
1071 mm-viewer-completion-map))
1072 (completing-read "Viewer: " methods))))
1073 (when (string= method "")
1074 (error "No method given"))
1075 (if (string-match "^[^% \t]+$" method)
1076 (setq method (concat method " %s")))
1077 (mm-display-external handle method)))
1079 (defun mm-preferred-alternative (handles &optional preferred)
1080 "Say which of HANDLES are preferred."
1081 (let ((prec (if preferred (list preferred)
1082 (mm-preferred-alternative-precedence handles)))
1083 p h result type handle)
1084 (while (setq p (pop prec))
1087 (setq handle (car h))
1088 (setq type (mm-handle-media-type handle))
1089 (when (and (equal p type)
1090 (mm-automatic-display-p handle)
1091 (or (stringp (car handle))
1092 (not (mm-handle-disposition handle))
1093 (equal (car (mm-handle-disposition handle))
1101 (defun mm-preferred-alternative-precedence (handles)
1102 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1103 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1105 (dolist (disc (reverse mm-discouraged-alternatives))
1106 (dolist (elem (copy-sequence seq))
1107 (when (string-match disc elem)
1108 (setq seq (nconc (delete elem seq) (list elem))))))
1111 (defun mm-get-content-id (id)
1112 "Return the handle(s) referred to by ID."
1113 (cdr (assoc id mm-content-id-alist)))
1115 (defconst mm-image-type-regexps
1116 '(("/\\*.*XPM.\\*/" . xpm)
1120 ("\211PNG\r\n" . png)
1122 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1123 ("%!PS" . postscript))
1124 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1125 When the first bytes of an image file match REGEXP, it is assumed to
1126 be of image type IMAGE-TYPE.")
1128 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1129 (defun mm-image-type-from-buffer ()
1130 "Determine the image type from data in the current buffer.
1131 Value is a symbol specifying the image type or nil if type cannot
1133 (let ((types mm-image-type-regexps)
1135 (goto-char (point-min))
1136 (while (and types (null type))
1137 (let ((regexp (car (car types)))
1138 (image-type (cdr (car types))))
1139 (when (looking-at regexp)
1140 (setq type image-type))
1141 (setq types (cdr types))))
1144 (defun mm-get-image (handle)
1145 "Return an image instance based on HANDLE."
1146 (let ((type (mm-handle-media-subtype handle))
1148 ;; Allow some common translations.
1151 ((equal type "x-pixmap")
1153 ((equal type "x-xbitmap")
1155 ((equal type "x-portable-bitmap")
1158 (or (mm-handle-cache handle)
1159 (mm-with-unibyte-buffer
1160 (mm-insert-part handle)
1164 ;; Avoid testing `make-glyph' since W3 may define
1165 ;; a bogus version of it.
1166 (if (fboundp 'create-image)
1167 (create-image (buffer-string)
1168 (or (mm-image-type-from-buffer)
1171 (mm-create-image-xemacs type))))
1172 (mm-handle-set-cache handle spec))))))
1174 (defun mm-create-image-xemacs (type)
1177 ;; xbm images require special handling, since
1178 ;; the only way to create glyphs from these
1179 ;; (without a ton of work) is to write them
1180 ;; out to a file, and then create a file
1182 (let ((file (mm-make-temp-file
1183 (expand-file-name "emm.xbm"
1184 mm-tmp-directory))))
1187 (write-region (point-min) (point-max) file)
1188 (make-glyph (list (cons 'x file))))
1190 (delete-file file)))))
1194 (or (mm-image-type-from-buffer)
1196 :data (buffer-string))))))
1198 (defun mm-image-fit-p (handle)
1199 "Say whether the image in HANDLE will fit the current window."
1200 (let ((image (mm-get-image handle)))
1201 (if (fboundp 'glyph-width)
1202 ;; XEmacs' glyphs can actually tell us about their width, so
1203 ;; lets be nice and smart about them.
1204 (or mm-inline-large-images
1205 (and (< (glyph-width image) (window-pixel-width))
1206 (< (glyph-height image) (window-pixel-height))))
1207 (let* ((size (image-size image))
1210 (or mm-inline-large-images
1211 (and (< h (1- (window-height))) ; Don't include mode line.
1212 (< w (window-width))))))))
1214 (defun mm-valid-image-format-p (format)
1215 "Say whether FORMAT can be displayed natively by Emacs."
1218 ((fboundp 'valid-image-instantiator-format-p)
1219 (valid-image-instantiator-format-p format))
1221 ((fboundp 'image-type-available-p)
1222 (and (display-graphic-p)
1223 (image-type-available-p format)))
1224 ;; Nobody else can do images yet.
1228 (defun mm-valid-and-fit-image-p (format handle)
1229 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1230 (and (mm-valid-image-format-p format)
1231 (mm-image-fit-p handle)))
1233 (defun mm-find-part-by-type (handles type &optional notp recursive)
1234 "Search in HANDLES for part with TYPE.
1235 If NOTP, returns first non-matching part.
1236 If RECURSIVE, search recursively."
1239 (if (and recursive (stringp (caar handles)))
1240 (if (setq handle (mm-find-part-by-type (cdar handles) type
1244 (not (equal (mm-handle-media-type (car handles)) type))
1245 (equal (mm-handle-media-type (car handles)) type))
1246 (setq handle (car handles)
1248 (setq handles (cdr handles)))
1251 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1252 (goto-char (point-min))
1253 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1255 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1257 (end (save-excursion
1258 (goto-char (point-max))
1259 (if (re-search-backward close-delimiter nil t)
1263 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1264 (while (and (not result)
1265 (re-search-forward boundary end t))
1266 (goto-char (match-beginning 0))
1270 (narrow-to-region start (1- (point)))
1271 (when (let ((ctl (ignore-errors
1272 (mail-header-parse-content-type
1273 (mail-fetch-field "content-type")))))
1275 (not (equal (car ctl) type))
1276 (equal (car ctl) type)))
1277 (setq result (buffer-substring (point-min) (point-max)))))))
1279 (setq start (point)))
1280 (when (and (not result) start)
1283 (narrow-to-region start end)
1284 (when (let ((ctl (ignore-errors
1285 (mail-header-parse-content-type
1286 (mail-fetch-field "content-type")))))
1288 (not (equal (car ctl) type))
1289 (equal (car ctl) type)))
1290 (setq result (buffer-substring (point-min) (point-max)))))))
1293 (defvar mm-security-handle nil)
1295 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1296 ;; HANDLE could be a CTL.
1298 (put-text-property 0 (length (car handle)) parameter value
1301 (defun mm-possibly-verify-or-decrypt (parts ctl)
1302 (let ((type (car ctl))
1303 (subtype (cadr (split-string (car ctl) "/")))
1304 (mm-security-handle ctl) ;; (car CTL) is the type.
1305 protocol func functest)
1307 ((or (equal type "application/x-pkcs7-mime")
1308 (equal type "application/pkcs7-mime"))
1311 ((eq mm-decrypt-option 'never) nil)
1312 ((eq mm-decrypt-option 'always) t)
1313 ((eq mm-decrypt-option 'known) t)
1315 (format "Decrypt (S/MIME) part? "))))
1316 (mm-view-pkcs7 parts))
1317 (setq parts (mm-dissect-buffer t)))))
1318 ((equal subtype "signed")
1319 (unless (and (setq protocol
1320 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1321 (not (equal protocol "multipart/mixed")))
1322 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1323 (let ((protocols mm-verify-function-alist))
1325 (if (and (or (not (setq functest (nth 3 (car protocols))))
1326 (funcall functest parts ctl))
1327 (mm-find-part-by-type parts (caar protocols) nil t))
1328 (setq protocol (caar protocols)
1330 (setq protocols (cdr protocols))))))
1331 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1333 ((eq mm-verify-option 'never) nil)
1334 ((eq mm-verify-option 'always) t)
1335 ((eq mm-verify-option 'known)
1337 (or (not (setq functest
1338 (nth 3 (assoc protocol
1339 mm-verify-function-alist))))
1340 (funcall functest parts ctl))))
1342 (format "Verify signed (%s) part? "
1343 (or (nth 2 (assoc protocol mm-verify-function-alist))
1344 (format "protocol=%s" protocol))))))
1347 (funcall func parts ctl)
1348 (mm-set-handle-multipart-parameter
1349 mm-security-handle 'gnus-details
1350 (format "Unknown sign protocol (%s)" protocol))))))
1351 ((equal subtype "encrypted")
1352 (unless (setq protocol
1353 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1354 ;; The message is broken.
1355 (let ((parts parts))
1357 (if (assoc (mm-handle-media-type (car parts))
1358 mm-decrypt-function-alist)
1359 (setq protocol (mm-handle-media-type (car parts))
1361 (setq parts (cdr parts))))))
1362 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1364 ((eq mm-decrypt-option 'never) nil)
1365 ((eq mm-decrypt-option 'always) t)
1366 ((eq mm-decrypt-option 'known)
1368 (or (not (setq functest
1369 (nth 3 (assoc protocol
1370 mm-decrypt-function-alist))))
1371 (funcall functest parts ctl))))
1373 (format "Decrypt (%s) part? "
1374 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1375 (format "protocol=%s" protocol))))))
1378 (setq parts (funcall func parts ctl))
1379 (mm-set-handle-multipart-parameter
1380 mm-security-handle 'gnus-details
1381 (format "Unknown encrypt protocol (%s)" protocol))))))
1385 (defun mm-multiple-handles (handles)
1386 (and (listp (car handles))
1387 (> (length handles) 1)))
1389 (defun mm-merge-handles (handles1 handles2)
1391 (if (listp (car handles1))
1394 (if (listp (car handles2))
1398 (defun mm-readable-p (handle)
1399 "Say whether the content of HANDLE is readable."
1400 (and (< (with-current-buffer (mm-handle-buffer handle)
1401 (buffer-size)) 10000)
1402 (mm-with-unibyte-buffer
1403 (mm-insert-part handle)
1404 (and (eq (mm-body-7-or-8) '7bit)
1405 (not (mm-long-lines-p 76))))))
1407 (provide 'mm-decode)
1409 ;;; mm-decode.el ends here