1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 ;; 2003 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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
29 (require 'gnus-mailcap)
31 (eval-when-compile (require 'cl)
35 (autoload 'executable-find "executable")
36 (autoload 'mm-inline-partial "mm-partial")
37 (autoload 'mm-inline-external-body "mm-extern")
38 (autoload 'mm-insert-inline "mm-view"))
40 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
42 (defgroup mime-display ()
43 "Display of MIME in mail and news articles."
44 :link '(custom-manual "(emacs-mime)Customization")
50 (defgroup mime-security ()
51 "MIME security in mail and news articles."
52 :link '(custom-manual "(emacs-mime)Customization")
57 ;;; Convenience macros.
59 (defmacro mm-handle-buffer (handle)
61 (defmacro mm-handle-type (handle)
63 (defsubst mm-handle-media-type (handle)
64 (if (stringp (car handle))
66 (car (mm-handle-type handle))))
67 (defsubst mm-handle-media-supertype (handle)
68 (car (split-string (mm-handle-media-type handle) "/")))
69 (defsubst mm-handle-media-subtype (handle)
70 (cadr (split-string (mm-handle-media-type handle) "/")))
71 (defmacro mm-handle-encoding (handle)
73 (defmacro mm-handle-undisplayer (handle)
75 (defmacro mm-handle-set-undisplayer (handle function)
76 `(setcar (nthcdr 3 ,handle) ,function))
77 (defmacro mm-handle-disposition (handle)
79 (defmacro mm-handle-description (handle)
81 (defmacro mm-handle-cache (handle)
83 (defmacro mm-handle-set-cache (handle contents)
84 `(setcar (nthcdr 6 ,handle) ,contents))
85 (defmacro mm-handle-id (handle)
87 (defmacro mm-handle-multipart-original-buffer (handle)
88 `(get-text-property 0 'buffer (car ,handle)))
89 (defmacro mm-handle-multipart-from (handle)
90 `(get-text-property 0 'from (car ,handle)))
91 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
92 `(get-text-property 0 ,parameter (car ,handle)))
94 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
95 disposition description cache
97 `(list ,buffer ,type ,encoding ,undisplayer
98 ,disposition ,description ,cache ,id))
100 (defcustom mm-text-html-renderer
101 (cond ((locate-library "w3") 'w3)
102 ((locate-library "w3m") 'w3m)
103 ((executable-find "links") 'links)
104 ((executable-find "lynx") 'lynx)
106 "Render of HTML contents.
107 It is one of defined renderer types, or a rendering function.
108 The defined renderer types are:
109 `w3' : using Emacs/W3;
110 `w3m' : using emacs-w3m;
111 `links': using links;
113 `html2text' : using html2text;
114 nil : using external viewer."
115 :type '(choice (const w3)
123 :group 'mime-display)
125 (defvar mm-inline-text-html-renderer nil
126 "Function used for rendering inline HTML contents.
127 It is suggested to customize `mm-text-html-renderer' instead.")
129 (defcustom mm-inline-text-html-with-images nil
130 "If non-nil, Gnus will allow retrieving images in the HTML contents
131 with <img> tags. It has no effect on Emacs/w3. See also
132 the documentation for the option `mm-w3m-safe-url-regexp'."
134 :group 'mime-display)
136 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
137 "Regexp that matches safe url names. Some HTML mails might have a
138 trick of spammers using <img> tags. It is likely to be intended to
139 verify whether you have read the mail. You can prevent your personal
140 informations from leaking by setting this to the regexp which matches
141 the safe url names. The value of the variable `w3m-safe-url-regexp'
142 will be bound with this value. You may set this value to nil if you
143 consider all the urls to be safe."
144 :type '(choice (regexp :tag "Regexp")
145 (const :tag "All URLs are safe" nil))
146 :group 'mime-display)
148 (defcustom mm-inline-text-html-with-w3m-keymap t
149 "If non-nil, use emacs-w3m command keys in the article buffer."
151 :group 'mime-display)
153 (defcustom mm-inline-media-tests
157 (mm-valid-and-fit-image-p 'jpeg handle)))
161 (mm-valid-and-fit-image-p 'png handle)))
165 (mm-valid-and-fit-image-p 'gif handle)))
169 (mm-valid-and-fit-image-p 'tiff handle)) )
173 (mm-valid-and-fit-image-p 'xbm handle)))
177 (mm-valid-and-fit-image-p 'xbm handle)))
181 (mm-valid-and-fit-image-p 'xpm handle)))
185 (mm-valid-and-fit-image-p 'xpm handle)))
189 (mm-valid-and-fit-image-p 'bmp handle)))
190 ("image/x-portable-bitmap"
193 (mm-valid-and-fit-image-p 'pbm handle)))
194 ("text/plain" mm-inline-text identity)
195 ("text/enriched" mm-inline-text identity)
196 ("text/richtext" mm-inline-text identity)
197 ("text/x-patch" mm-display-patch-inline
199 (locate-library "diff-mode")))
200 ("application/emacs-lisp" mm-display-elisp-inline identity)
201 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
205 (or mm-inline-text-html-renderer
206 mm-text-html-renderer)))
210 (or (featurep 'vcard)
211 (locate-library "vcard"))))
212 ("message/delivery-status" mm-inline-text identity)
213 ("message/rfc822" mm-inline-message identity)
214 ("message/partial" mm-inline-partial identity)
215 ("message/external-body" mm-inline-external-body identity)
216 ("text/.*" mm-inline-text identity)
217 ("audio/wav" mm-inline-audio
219 (and (or (featurep 'nas-sound) (featurep 'native-sound))
220 (device-sound-enabled-p))))
224 (and (or (featurep 'nas-sound) (featurep 'native-sound))
225 (device-sound-enabled-p))))
226 ("application/pgp-signature" ignore identity)
227 ("application/x-pkcs7-signature" ignore identity)
228 ("application/pkcs7-signature" ignore identity)
229 ("application/x-pkcs7-mime" ignore identity)
230 ("application/pkcs7-mime" ignore identity)
231 ("multipart/alternative" ignore identity)
232 ("multipart/mixed" ignore identity)
233 ("multipart/related" ignore identity)
234 ;; Disable audio and image
235 ("audio/.*" ignore ignore)
236 ("image/.*" ignore ignore)
237 ;; Default to displaying as text
238 (".*" mm-inline-text mm-readable-p))
239 "Alist of media types/tests saying whether types can be displayed inline."
240 :type '(repeat (list (string :tag "MIME type")
241 (function :tag "Display function")
242 (function :tag "Display test")))
243 :group 'mime-display)
245 (defcustom mm-inlined-types
246 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
247 "message/partial" "message/external-body" "application/emacs-lisp"
248 "application/x-emacs-lisp"
249 "application/pgp-signature" "application/x-pkcs7-signature"
250 "application/pkcs7-signature" "application/x-pkcs7-mime"
251 "application/pkcs7-mime")
252 "List of media types that are to be displayed inline.
253 See also `mm-inline-media-tests', which says how to display a media
255 :type '(repeat string)
256 :group 'mime-display)
258 (defcustom mm-keep-viewer-alive-types
259 '("application/postscript" "application/msword" "application/vnd.ms-excel"
260 "application/pdf" "application/x-dvi")
261 "List of media types for which the external viewer will not be killed
262 when selecting a different article."
263 :type '(repeat string)
264 :group 'mime-display)
266 (defcustom mm-automatic-display
267 '("text/plain" "text/enriched" "text/richtext" "text/html"
268 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
269 "message/rfc822" "text/x-patch" "application/pgp-signature"
270 "application/emacs-lisp" "application/x-emacs-lisp"
271 "application/x-pkcs7-signature"
272 "application/pkcs7-signature" "application/x-pkcs7-mime"
273 "application/pkcs7-mime")
274 "A list of MIME types to be displayed automatically."
275 :type '(repeat string)
276 :group 'mime-display)
278 (defcustom mm-attachment-override-types '("text/x-vcard"
279 "application/pkcs7-mime"
280 "application/x-pkcs7-mime"
281 "application/pkcs7-signature"
282 "application/x-pkcs7-signature")
283 "Types to have \"attachment\" ignored if they can be displayed inline."
284 :type '(repeat string)
285 :group 'mime-display)
287 (defcustom mm-inline-override-types nil
288 "Types to be treated as attachments even if they can be displayed inline."
289 :type '(repeat string)
290 :group 'mime-display)
292 (defcustom mm-automatic-external-display nil
293 "List of MIME type regexps that will be displayed externally automatically."
294 :type '(repeat string)
295 :group 'mime-display)
297 (defcustom mm-discouraged-alternatives nil
298 "List of MIME types that are discouraged when viewing multipart/alternative.
299 Viewing agents are supposed to view the last possible part of a message,
300 as that is supposed to be the richest. However, users may prefer other
301 types instead, and this list says what types are most unwanted. If,
302 for instance, text/html parts are very unwanted, and text/richtext are
303 somewhat unwanted, then the value of this variable should be set
306 (\"text/html\" \"text/richtext\")"
307 :type '(repeat string)
308 :group 'mime-display)
310 (defcustom mm-tmp-directory
311 (cond ((fboundp 'temp-directory) (temp-directory))
312 ((boundp 'temporary-file-directory) temporary-file-directory)
314 "Where mm will store its temporary files."
316 :group 'mime-display)
318 (defcustom mm-inline-large-images nil
319 "If non-nil, then all images fit in the buffer."
321 :group 'mime-display)
323 (defvar mm-file-name-rewrite-functions nil
324 "*List of functions used for rewriting file names of MIME parts.
325 Each function takes a file name as input and returns a file name.
327 Ready-made functions include
328 `mm-file-name-delete-whitespace',
329 `mm-file-name-trim-whitespace',
330 `mm-file-name-collapse-whitespace',
331 `mm-file-name-replace-whitespace',
332 `capitalize', `downcase', `upcase', and
335 (defvar mm-path-name-rewrite-functions nil
336 "*List of functions for rewriting the full file names of MIME parts.
337 This is used when viewing parts externally, and is meant for
338 transforming the absolute name so that non-compliant programs can find
339 the file where it's saved.
341 Each function takes a file name as input and returns a file name.")
343 (defvar mm-file-name-replace-whitespace nil
344 "String used for replacing whitespace characters; default is `\"_\"'.")
346 (defcustom mm-default-directory nil
347 "The default directory where mm will save files.
348 If not set, `default-directory' will be used."
349 :type '(choice directory (const :tag "Default" nil))
350 :group 'mime-display)
352 (defcustom mm-external-terminal-program "xterm"
353 "The program to start an external terminal."
355 :group 'mime-display)
357 ;;; Internal variables.
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))
556 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
558 (defun mm-dissect-multipart (ctl)
559 (goto-char (point-min))
560 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
561 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
564 (goto-char (point-max))
565 (if (re-search-backward close-delimiter nil t)
568 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
569 (while (and (< (point) end) (re-search-forward boundary end t))
570 (goto-char (match-beginning 0))
574 (narrow-to-region start (point))
575 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
577 (or (looking-at boundary)
579 (setq start (point)))
580 (when (and start (< start end))
583 (narrow-to-region start end)
584 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
585 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
587 (defun mm-copy-to-buffer ()
588 "Copy the contents of the current buffer to a fresh buffer."
590 (let ((flag enable-multibyte-characters)
591 (new-buffer (generate-new-buffer " *mm*")))
592 (goto-char (point-min))
593 (search-forward-regexp "^\n" nil t)
595 (narrow-to-region (point) (point-max))
597 (set-buffer-multibyte nil))
598 (copy-to-buffer new-buffer (point-min) (point-max))
600 (set-buffer-multibyte t)))
603 (defun mm-display-parts (handle &optional no-default)
604 (if (stringp (car handle))
605 (mapcar 'mm-display-parts (cdr handle))
606 (if (bufferp (car handle))
608 (narrow-to-region (point) (point))
609 (mm-display-part handle)
610 (goto-char (point-max)))
611 (mapcar 'mm-display-parts handle))))
613 (defun mm-display-part (handle &optional no-default)
614 "Display the MIME part represented by HANDLE.
615 Returns nil if the part is removed; inline if displayed inline;
616 external if displayed external."
618 (mailcap-parse-mailcaps)
619 (if (mm-handle-displayed-p handle)
620 (mm-remove-part handle)
621 (let* ((type (mm-handle-media-type handle))
622 (method (mailcap-mime-info type)))
623 (if (and (mm-inlinable-p handle)
624 (mm-inlined-p handle))
627 (mm-display-inline handle)
631 (if (and (not method)
632 (equal "text" (car (split-string type))))
635 (mm-insert-inline handle (mm-get-part handle))
638 handle (or method 'mailcap-save-binary-file)))))))))
640 (defun mm-display-external (handle method)
641 "Display HANDLE using METHOD."
642 (let ((outbuf (current-buffer)))
643 (mm-with-unibyte-buffer
644 (if (functionp method)
645 (let ((cur (current-buffer)))
646 (if (eq method 'mailcap-save-binary-file)
648 (set-buffer (generate-new-buffer " *mm*"))
650 (mm-insert-part handle)
651 (let ((win (get-buffer-window cur t)))
653 (select-window win)))
654 (switch-to-buffer (generate-new-buffer " *mm*")))
655 (buffer-disable-undo)
656 (mm-set-buffer-file-coding-system mm-binary-coding-system)
657 (insert-buffer-substring cur)
658 (goto-char (point-min))
659 (message "Viewing with %s" method)
660 (let ((mm (current-buffer))
661 (non-viewer (assq 'non-viewer
663 (mm-handle-media-type handle) t))))
667 (mm-save-part handle))
668 (when (and (not non-viewer)
670 (mm-handle-set-undisplayer handle mm)))))
671 ;; The function is a string to be executed.
672 (mm-insert-part handle)
673 (let* ((dir (mm-make-temp-file
674 (expand-file-name "emm." mm-tmp-directory) 'dir))
676 (mail-content-type-get
677 (mm-handle-disposition handle) 'filename)
678 (mail-content-type-get
679 (mm-handle-type handle) 'name)))
680 (mime-info (mailcap-mime-info
681 (mm-handle-media-type handle) t))
682 (needsterm (or (assoc "needsterm" mime-info)
683 (assoc "needsterminal" mime-info)))
684 (copiousoutput (assoc "copiousoutput" mime-info))
686 ;; We create a private sub-directory where we store our files.
687 (set-file-modes dir 448)
689 (setq file (expand-file-name
690 (gnus-map-function mm-file-name-rewrite-functions
691 (file-name-nondirectory filename))
693 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
694 (let ((coding-system-for-write mm-binary-coding-system))
695 (write-region (point-min) (point-max) file nil 'nomesg))
696 (message "Viewing with %s" method)
699 (let ((command (mm-mailcap-command
700 method file (mm-handle-type handle))))
703 (start-process "*display*" nil
704 mm-external-terminal-program
706 shell-command-switch command)
714 shell-command-switch command)))
717 (set-process-sentinel
718 (get-buffer-process buffer)
719 `(lambda (process state)
720 (if (eq 'exit (process-status process))
721 (gnus-configure-windows
722 ',gnus-current-window-configuration))))
723 (gnus-configure-windows 'display-term))
724 (mm-handle-set-external-undisplayer handle (cons file buffer)))
725 (message "Displaying %s..." command))
728 (with-current-buffer outbuf
734 (call-process shell-file-name nil
736 (generate-new-buffer " *mm*"))
740 method file (mm-handle-type handle)))
741 (if (buffer-live-p buffer)
746 (ignore-errors (delete-file file))
747 (ignore-errors (delete-directory
748 (file-name-directory file)))
749 (ignore-errors (kill-buffer buffer))))))
752 (let ((command (mm-mailcap-command
753 method file (mm-handle-type handle))))
755 (start-process "*display*"
757 (generate-new-buffer " *mm*"))
759 shell-command-switch command)
760 (mm-handle-set-external-undisplayer
761 handle (cons file buffer)))
762 (message "Displaying %s..." command))
765 (defun mm-mailcap-command (method file type-list)
766 (let ((ctl (cdr type-list))
770 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
772 (push (substring method beg (match-beginning 0)) out)
773 (setq beg (match-end 0)
774 total (match-string 0 method)
775 sub (match-string 1 method))
777 ((string= total "%%")
779 ((or (string= total "%s")
780 ;; We do our own quoting.
781 (string= total "'%s'")
782 (string= total "\"%s\""))
783 (setq uses-stdin nil)
785 (gnus-map-function mm-path-name-rewrite-functions file)) out))
786 ((string= total "%t")
787 (push (mm-quote-arg (car type-list)) out))
789 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
790 (push (substring method beg (length method)) out)
794 (gnus-map-function mm-path-name-rewrite-functions file))
796 (mapconcat 'identity (nreverse out) "")))
798 (defun mm-remove-parts (handles)
799 "Remove the displayed MIME parts represented by HANDLES."
800 (if (and (listp handles)
801 (bufferp (car handles)))
802 (mm-remove-part handles)
804 (while (setq handle (pop handles))
807 (when (buffer-live-p (get-text-property 0 'buffer handle))
808 (kill-buffer (get-text-property 0 'buffer handle))))
810 (stringp (car handle)))
811 (mm-remove-parts (cdr handle)))
813 (mm-remove-part handle)))))))
815 (defun mm-destroy-parts (handles)
816 "Remove the displayed MIME parts represented by HANDLES."
817 (if (and (listp handles)
818 (bufferp (car handles)))
819 (mm-destroy-part handles)
821 (while (setq handle (pop handles))
824 (when (buffer-live-p (get-text-property 0 'buffer handle))
825 (kill-buffer (get-text-property 0 'buffer handle))))
827 (stringp (car handle)))
828 (mm-destroy-parts handle))
830 (mm-destroy-part handle)))))))
832 (defun mm-remove-part (handle)
833 "Remove the displayed MIME part represented by HANDLE."
835 (let ((object (mm-handle-undisplayer handle)))
838 ;; Internally displayed part.
839 ((mm-annotationp object)
840 (delete-annotation object))
841 ((or (functionp object)
843 (eq (car object) 'lambda)))
845 ;; Externally displayed part.
847 (ignore-errors (delete-file (car object)))
848 (ignore-errors (delete-directory (file-name-directory (car object))))
849 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
851 (when (buffer-live-p object)
852 (kill-buffer object)))))
853 (mm-handle-set-undisplayer handle nil))))
855 (defun mm-display-inline (handle)
856 (let* ((type (mm-handle-media-type handle))
857 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
858 (funcall function handle)
859 (goto-char (point-min))))
861 (defun mm-assoc-string-match (alist type)
863 (when (string-match (car elem) type)
866 (defun mm-automatic-display-p (handle)
867 "Say whether the user wants HANDLE to be displayed automatically."
868 (let ((methods mm-automatic-display)
869 (type (mm-handle-media-type handle))
871 (while (setq method (pop methods))
872 (when (and (not (mm-inline-override-p handle))
873 (string-match method type))
878 (defun mm-inlinable-p (handle)
879 "Say whether HANDLE can be displayed inline."
880 (let ((alist mm-inline-media-tests)
881 (type (mm-handle-media-type handle))
884 (when (string-match (caar alist) type)
885 (setq test (caddar alist)
887 (setq test (funcall test handle)))
891 (defun mm-inlined-p (handle)
892 "Say whether the user wants HANDLE to be displayed inline."
893 (let ((methods mm-inlined-types)
894 (type (mm-handle-media-type handle))
896 (while (setq method (pop methods))
897 (when (and (not (mm-inline-override-p handle))
898 (string-match method type))
903 (defun mm-attachment-override-p (handle)
904 "Say whether HANDLE should have attachment behavior overridden."
905 (let ((types mm-attachment-override-types)
906 (type (mm-handle-media-type handle))
909 (while (setq ty (pop types))
910 (when (and (string-match ty type)
911 (mm-inlinable-p handle))
912 (throw 'found t))))))
914 (defun mm-inline-override-p (handle)
915 "Say whether HANDLE should have inline behavior overridden."
916 (let ((types mm-inline-override-types)
917 (type (mm-handle-media-type handle))
920 (while (setq ty (pop types))
921 (when (string-match ty type)
922 (throw 'found t))))))
924 (defun mm-automatic-external-display-p (type)
925 "Return the user-defined method for TYPE."
926 (let ((methods mm-automatic-external-display)
928 (while (setq method (pop methods))
929 (when (string-match method type)
934 (defun mm-destroy-part (handle)
935 "Destroy the data structures connected to HANDLE."
937 (mm-remove-part handle)
938 (when (buffer-live-p (mm-handle-buffer handle))
939 (kill-buffer (mm-handle-buffer handle)))))
941 (defun mm-handle-displayed-p (handle)
942 "Say whether HANDLE is displayed or not."
943 (mm-handle-undisplayer handle))
946 ;;; Functions for outputting parts
949 (defun mm-get-part (handle)
950 "Return the contents of HANDLE as a string."
951 (mm-with-unibyte-buffer
952 (insert (with-current-buffer (mm-handle-buffer handle)
953 (mm-with-unibyte-current-buffer
955 (mm-decode-content-transfer-encoding
956 (mm-handle-encoding handle)
957 (mm-handle-media-type handle))
960 (defun mm-insert-part (handle)
961 "Insert the contents of HANDLE in the current buffer."
962 (let ((cur (current-buffer)))
964 (if (member (mm-handle-media-supertype handle) '("text" "message"))
966 (insert-buffer-substring (mm-handle-buffer handle))
968 (mm-decode-content-transfer-encoding
969 (mm-handle-encoding handle)
970 (mm-handle-media-type handle))
971 (let ((temp (current-buffer)))
973 (insert-buffer-substring temp))))
974 (mm-with-unibyte-buffer
975 (insert-buffer-substring (mm-handle-buffer handle))
977 (mm-decode-content-transfer-encoding
978 (mm-handle-encoding handle)
979 (mm-handle-media-type handle))
980 (let ((temp (current-buffer)))
982 (insert-buffer-substring temp))))))))
984 (defun mm-file-name-delete-whitespace (file-name)
985 "Remove all whitespace characters from FILE-NAME."
986 (while (string-match "\\s-+" file-name)
987 (setq file-name (replace-match "" t t file-name)))
990 (defun mm-file-name-trim-whitespace (file-name)
991 "Remove leading and trailing whitespace characters from FILE-NAME."
992 (when (string-match "\\`\\s-+" file-name)
993 (setq file-name (substring file-name (match-end 0))))
994 (when (string-match "\\s-+\\'" file-name)
995 (setq file-name (substring file-name 0 (match-beginning 0))))
998 (defun mm-file-name-collapse-whitespace (file-name)
999 "Collapse multiple whitespace characters in FILE-NAME."
1000 (while (string-match "\\s-\\s-+" file-name)
1001 (setq file-name (replace-match " " t t file-name)))
1004 (defun mm-file-name-replace-whitespace (file-name)
1005 "Replace whitespace characters in FILE-NAME with underscores.
1006 Set `mm-file-name-replace-whitespace' to any other string if you do not
1008 (let ((s (or mm-file-name-replace-whitespace "_")))
1009 (while (string-match "\\s-" file-name)
1010 (setq file-name (replace-match s t t file-name))))
1013 (defun mm-save-part (handle)
1014 "Write HANDLE to a file."
1015 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1016 (filename (mail-content-type-get
1017 (mm-handle-disposition handle) 'filename))
1020 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1021 (file-name-nondirectory filename))))
1023 (read-file-name "Save MIME part to: "
1024 (or mm-default-directory default-directory)
1025 nil nil (or filename name "")))
1026 (setq mm-default-directory (file-name-directory file))
1027 (and (or (not (file-exists-p file))
1028 (yes-or-no-p (format "File %s already exists; overwrite? "
1031 (mm-save-part-to-file handle file)
1034 (defun mm-save-part-to-file (handle file)
1035 (mm-with-unibyte-buffer
1036 (mm-insert-part handle)
1037 (let ((coding-system-for-write 'binary)
1038 ;; Don't re-compress .gz & al. Arguably we should make
1039 ;; `file-name-handler-alist' nil, but that would chop
1040 ;; ange-ftp, which is reasonable to use here.
1041 (inhibit-file-name-operation 'write-region)
1042 (inhibit-file-name-handlers
1043 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1044 (write-region (point-min) (point-max) file))))
1046 (defun mm-pipe-part (handle)
1047 "Pipe HANDLE to a process."
1048 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1050 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1051 (mm-with-unibyte-buffer
1052 (mm-insert-part handle)
1053 (let ((coding-system-for-write 'binary))
1054 (shell-command-on-region (point-min) (point-max) command nil)))))
1056 (defun mm-interactively-view-part (handle)
1057 "Display HANDLE using METHOD."
1058 (let* ((type (mm-handle-media-type handle))
1060 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1061 (mailcap-mime-info type 'all)))
1062 (method (let ((minibuffer-local-completion-map
1063 mm-viewer-completion-map))
1064 (completing-read "Viewer: " methods))))
1065 (when (string= method "")
1066 (error "No method given"))
1067 (if (string-match "^[^% \t]+$" method)
1068 (setq method (concat method " %s")))
1069 (mm-display-external handle method)))
1071 (defun mm-preferred-alternative (handles &optional preferred)
1072 "Say which of HANDLES are preferred."
1073 (let ((prec (if preferred (list preferred)
1074 (mm-preferred-alternative-precedence handles)))
1075 p h result type handle)
1076 (while (setq p (pop prec))
1079 (setq handle (car h))
1080 (setq type (mm-handle-media-type handle))
1081 (when (and (equal p type)
1082 (mm-automatic-display-p handle)
1083 (or (stringp (car handle))
1084 (not (mm-handle-disposition handle))
1085 (equal (car (mm-handle-disposition handle))
1093 (defun mm-preferred-alternative-precedence (handles)
1094 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1095 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1097 (dolist (disc (reverse mm-discouraged-alternatives))
1098 (dolist (elem (copy-sequence seq))
1099 (when (string-match disc elem)
1100 (setq seq (nconc (delete elem seq) (list elem))))))
1103 (defun mm-get-content-id (id)
1104 "Return the handle(s) referred to by ID."
1105 (cdr (assoc id mm-content-id-alist)))
1107 (defconst mm-image-type-regexps
1108 '(("/\\*.*XPM.\\*/" . xpm)
1112 ("\211PNG\r\n" . png)
1114 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1115 ("%!PS" . postscript))
1116 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1117 When the first bytes of an image file match REGEXP, it is assumed to
1118 be of image type IMAGE-TYPE.")
1120 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1121 (defun mm-image-type-from-buffer ()
1122 "Determine the image type from data in the current buffer.
1123 Value is a symbol specifying the image type or nil if type cannot
1125 (let ((types mm-image-type-regexps)
1127 (goto-char (point-min))
1128 (while (and types (null type))
1129 (let ((regexp (car (car types)))
1130 (image-type (cdr (car types))))
1131 (when (looking-at regexp)
1132 (setq type image-type))
1133 (setq types (cdr types))))
1136 (defun mm-get-image (handle)
1137 "Return an image instance based on HANDLE."
1138 (let ((type (mm-handle-media-subtype handle))
1140 ;; Allow some common translations.
1143 ((equal type "x-pixmap")
1145 ((equal type "x-xbitmap")
1147 ((equal type "x-portable-bitmap")
1150 (or (mm-handle-cache handle)
1151 (mm-with-unibyte-buffer
1152 (mm-insert-part handle)
1156 ;; Avoid testing `make-glyph' since W3 may define
1157 ;; a bogus version of it.
1158 (if (fboundp 'create-image)
1159 (create-image (buffer-string)
1160 (or (mm-image-type-from-buffer)
1163 (mm-create-image-xemacs type))))
1164 (mm-handle-set-cache handle spec))))))
1166 (defun mm-create-image-xemacs (type)
1169 ;; xbm images require special handling, since
1170 ;; the only way to create glyphs from these
1171 ;; (without a ton of work) is to write them
1172 ;; out to a file, and then create a file
1174 (let ((file (mm-make-temp-file
1175 (expand-file-name "emm.xbm"
1176 mm-tmp-directory))))
1179 (write-region (point-min) (point-max) file)
1180 (make-glyph (list (cons 'x file))))
1182 (delete-file file)))))
1186 (or (mm-image-type-from-buffer)
1188 :data (buffer-string))))))
1190 (defun mm-image-fit-p (handle)
1191 "Say whether the image in HANDLE will fit the current window."
1192 (let ((image (mm-get-image handle)))
1193 (if (fboundp 'glyph-width)
1194 ;; XEmacs' glyphs can actually tell us about their width, so
1195 ;; lets be nice and smart about them.
1196 (or mm-inline-large-images
1197 (and (< (glyph-width image) (window-pixel-width))
1198 (< (glyph-height image) (window-pixel-height))))
1199 (let* ((size (image-size image))
1202 (or mm-inline-large-images
1203 (and (< h (1- (window-height))) ; Don't include mode line.
1204 (< w (window-width))))))))
1206 (defun mm-valid-image-format-p (format)
1207 "Say whether FORMAT can be displayed natively by Emacs."
1210 ((fboundp 'valid-image-instantiator-format-p)
1211 (valid-image-instantiator-format-p format))
1213 ((fboundp 'image-type-available-p)
1214 (and (display-graphic-p)
1215 (image-type-available-p format)))
1216 ;; Nobody else can do images yet.
1220 (defun mm-valid-and-fit-image-p (format handle)
1221 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1222 (and (mm-valid-image-format-p format)
1223 (mm-image-fit-p handle)))
1225 (defun mm-find-part-by-type (handles type &optional notp recursive)
1226 "Search in HANDLES for part with TYPE.
1227 If NOTP, returns first non-matching part.
1228 If RECURSIVE, search recursively."
1231 (if (and recursive (stringp (caar handles)))
1232 (if (setq handle (mm-find-part-by-type (cdar handles) type
1236 (not (equal (mm-handle-media-type (car handles)) type))
1237 (equal (mm-handle-media-type (car handles)) type))
1238 (setq handle (car handles)
1240 (setq handles (cdr handles)))
1243 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1244 (goto-char (point-min))
1245 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1247 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1249 (end (save-excursion
1250 (goto-char (point-max))
1251 (if (re-search-backward close-delimiter nil t)
1255 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1256 (while (and (not result)
1257 (re-search-forward boundary end t))
1258 (goto-char (match-beginning 0))
1262 (narrow-to-region start (1- (point)))
1263 (when (let ((ctl (ignore-errors
1264 (mail-header-parse-content-type
1265 (mail-fetch-field "content-type")))))
1267 (not (equal (car ctl) type))
1268 (equal (car ctl) type)))
1269 (setq result (buffer-string))))))
1271 (setq start (point)))
1272 (when (and (not result) start)
1275 (narrow-to-region start end)
1276 (when (let ((ctl (ignore-errors
1277 (mail-header-parse-content-type
1278 (mail-fetch-field "content-type")))))
1280 (not (equal (car ctl) type))
1281 (equal (car ctl) type)))
1282 (setq result (buffer-string))))))
1285 (defvar mm-security-handle nil)
1287 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1288 ;; HANDLE could be a CTL.
1290 (put-text-property 0 (length (car handle)) parameter value
1293 (defun mm-possibly-verify-or-decrypt (parts ctl)
1294 (let ((type (car ctl))
1295 (subtype (cadr (split-string (car ctl) "/")))
1296 (mm-security-handle ctl) ;; (car CTL) is the type.
1297 protocol func functest)
1299 ((or (equal type "application/x-pkcs7-mime")
1300 (equal type "application/pkcs7-mime"))
1303 ((eq mm-decrypt-option 'never) nil)
1304 ((eq mm-decrypt-option 'always) t)
1305 ((eq mm-decrypt-option 'known) t)
1307 (format "Decrypt (S/MIME) part? "))))
1308 (mm-view-pkcs7 parts))
1309 (setq parts (mm-dissect-buffer t)))))
1310 ((equal subtype "signed")
1311 (unless (and (setq protocol
1312 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1313 (not (equal protocol "multipart/mixed")))
1314 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1315 (let ((protocols mm-verify-function-alist))
1317 (if (and (or (not (setq functest (nth 3 (car protocols))))
1318 (funcall functest parts ctl))
1319 (mm-find-part-by-type parts (caar protocols) nil t))
1320 (setq protocol (caar protocols)
1322 (setq protocols (cdr protocols))))))
1323 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1325 ((eq mm-verify-option 'never) nil)
1326 ((eq mm-verify-option 'always) t)
1327 ((eq mm-verify-option 'known)
1329 (or (not (setq functest
1330 (nth 3 (assoc protocol
1331 mm-verify-function-alist))))
1332 (funcall functest parts ctl))))
1335 (format "Verify signed (%s) part? "
1336 (or (nth 2 (assoc protocol mm-verify-function-alist))
1337 (format "protocol=%s" protocol))))))
1340 (funcall func parts ctl)
1341 (mm-set-handle-multipart-parameter
1342 mm-security-handle 'gnus-details
1343 (format "Unknown sign protocol (%s)" protocol))))))
1344 ((equal subtype "encrypted")
1345 (unless (setq protocol
1346 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1347 ;; The message is broken.
1348 (let ((parts parts))
1350 (if (assoc (mm-handle-media-type (car parts))
1351 mm-decrypt-function-alist)
1352 (setq protocol (mm-handle-media-type (car parts))
1354 (setq parts (cdr parts))))))
1355 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1357 ((eq mm-decrypt-option 'never) nil)
1358 ((eq mm-decrypt-option 'always) t)
1359 ((eq mm-decrypt-option 'known)
1361 (or (not (setq functest
1362 (nth 3 (assoc protocol
1363 mm-decrypt-function-alist))))
1364 (funcall functest parts ctl))))
1367 (format "Decrypt (%s) part? "
1368 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1369 (format "protocol=%s" protocol))))))
1372 (setq parts (funcall func parts ctl))
1373 (mm-set-handle-multipart-parameter
1374 mm-security-handle 'gnus-details
1375 (format "Unknown encrypt protocol (%s)" protocol))))))
1379 (defun mm-multiple-handles (handles)
1380 (and (listp (car handles))
1381 (> (length handles) 1)))
1383 (defun mm-merge-handles (handles1 handles2)
1385 (if (listp (car handles1))
1388 (if (listp (car handles2))
1392 (defun mm-readable-p (handle)
1393 "Say whether the content of HANDLE is readable."
1394 (and (< (with-current-buffer (mm-handle-buffer handle)
1395 (buffer-size)) 10000)
1396 (mm-with-unibyte-buffer
1397 (mm-insert-part handle)
1398 (and (eq (mm-body-7-or-8) '7bit)
1399 (not (mm-long-lines-p 76))))))
1401 (provide 'mm-decode)
1403 ;;; mm-decode.el ends here