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.
138 Some HTML mails might have a trick of spammers using <img> tags.
139 It is likely to be intended to verify whether you have read the
140 mail. You can prevent your personal informations from leaking by
141 setting this to the regexp which matches the safe url names. The
142 value of the variable `w3m-safe-url-regexp' will be bound with
143 this value. You may set this value to nil if you consider all
145 :type '(choice (regexp :tag "Regexp")
146 (const :tag "All URLs are safe" nil))
147 :group 'mime-display)
149 (defcustom mm-inline-text-html-with-w3m-keymap t
150 "If non-nil, use emacs-w3m command keys in the article buffer."
152 :group 'mime-display)
154 (defcustom mm-inline-media-tests
158 (mm-valid-and-fit-image-p 'jpeg handle)))
162 (mm-valid-and-fit-image-p 'png handle)))
166 (mm-valid-and-fit-image-p 'gif handle)))
170 (mm-valid-and-fit-image-p 'tiff handle)) )
174 (mm-valid-and-fit-image-p 'xbm handle)))
178 (mm-valid-and-fit-image-p 'xbm handle)))
182 (mm-valid-and-fit-image-p 'xpm handle)))
186 (mm-valid-and-fit-image-p 'xpm handle)))
190 (mm-valid-and-fit-image-p 'bmp handle)))
191 ("image/x-portable-bitmap"
194 (mm-valid-and-fit-image-p 'pbm handle)))
195 ("text/plain" mm-inline-text identity)
196 ("text/enriched" mm-inline-text identity)
197 ("text/richtext" mm-inline-text identity)
198 ("text/x-patch" mm-display-patch-inline
200 (locate-library "diff-mode")))
201 ("application/emacs-lisp" mm-display-elisp-inline identity)
202 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
206 (or mm-inline-text-html-renderer
207 mm-text-html-renderer)))
211 (or (featurep 'vcard)
212 (locate-library "vcard"))))
213 ("message/delivery-status" mm-inline-text identity)
214 ("message/rfc822" mm-inline-message identity)
215 ("message/partial" mm-inline-partial identity)
216 ("message/external-body" mm-inline-external-body identity)
217 ("text/.*" mm-inline-text identity)
218 ("audio/wav" mm-inline-audio
220 (and (or (featurep 'nas-sound) (featurep 'native-sound))
221 (device-sound-enabled-p))))
225 (and (or (featurep 'nas-sound) (featurep 'native-sound))
226 (device-sound-enabled-p))))
227 ("application/pgp-signature" ignore identity)
228 ("application/x-pkcs7-signature" ignore identity)
229 ("application/pkcs7-signature" ignore identity)
230 ("application/x-pkcs7-mime" ignore identity)
231 ("application/pkcs7-mime" ignore identity)
232 ("multipart/alternative" ignore identity)
233 ("multipart/mixed" ignore identity)
234 ("multipart/related" ignore identity)
235 ;; Disable audio and image
236 ("audio/.*" ignore ignore)
237 ("image/.*" ignore ignore)
238 ;; Default to displaying as text
239 (".*" mm-inline-text mm-readable-p))
240 "Alist of media types/tests saying whether types can be displayed inline."
241 :type '(repeat (list (string :tag "MIME type")
242 (function :tag "Display function")
243 (function :tag "Display test")))
244 :group 'mime-display)
246 (defcustom mm-inlined-types
247 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
248 "message/partial" "message/external-body" "application/emacs-lisp"
249 "application/x-emacs-lisp"
250 "application/pgp-signature" "application/x-pkcs7-signature"
251 "application/pkcs7-signature" "application/x-pkcs7-mime"
252 "application/pkcs7-mime")
253 "List of media types that are to be displayed inline.
254 See also `mm-inline-media-tests', which says how to display a media
256 :type '(repeat string)
257 :group 'mime-display)
259 (defcustom mm-keep-viewer-alive-types
260 '("application/postscript" "application/msword" "application/vnd.ms-excel"
261 "application/pdf" "application/x-dvi")
262 "List of media types for which the external viewer will not be killed
263 when selecting a different article."
264 :type '(repeat string)
265 :group 'mime-display)
267 (defcustom mm-automatic-display
268 '("text/plain" "text/enriched" "text/richtext" "text/html"
269 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
270 "message/rfc822" "text/x-patch" "application/pgp-signature"
271 "application/emacs-lisp" "application/x-emacs-lisp"
272 "application/x-pkcs7-signature"
273 "application/pkcs7-signature" "application/x-pkcs7-mime"
274 "application/pkcs7-mime")
275 "A list of MIME types to be displayed automatically."
276 :type '(repeat string)
277 :group 'mime-display)
279 (defcustom mm-attachment-override-types '("text/x-vcard"
280 "application/pkcs7-mime"
281 "application/x-pkcs7-mime"
282 "application/pkcs7-signature"
283 "application/x-pkcs7-signature")
284 "Types to have \"attachment\" ignored if they can be displayed inline."
285 :type '(repeat string)
286 :group 'mime-display)
288 (defcustom mm-inline-override-types nil
289 "Types to be treated as attachments even if they can be displayed inline."
290 :type '(repeat string)
291 :group 'mime-display)
293 (defcustom mm-automatic-external-display nil
294 "List of MIME type regexps that will be displayed externally automatically."
295 :type '(repeat string)
296 :group 'mime-display)
298 (defcustom mm-discouraged-alternatives nil
299 "List of MIME types that are discouraged when viewing multipart/alternative.
300 Viewing agents are supposed to view the last possible part of a message,
301 as that is supposed to be the richest. However, users may prefer other
302 types instead, and this list says what types are most unwanted. If,
303 for instance, text/html parts are very unwanted, and text/richtext are
304 somewhat unwanted, then the value of this variable should be set
307 (\"text/html\" \"text/richtext\")"
308 :type '(repeat string)
309 :group 'mime-display)
311 (defcustom mm-tmp-directory
312 (if (fboundp 'temp-directory)
314 (if (boundp 'temporary-file-directory)
315 temporary-file-directory
317 "Where mm will store its temporary files."
319 :group 'mime-display)
321 (defcustom mm-inline-large-images nil
322 "If non-nil, then all images fit in the buffer."
324 :group 'mime-display)
326 (defvar mm-file-name-rewrite-functions nil
327 "*List of functions used for rewriting file names of MIME parts.
328 Each function takes a file name as input and returns a file name.
330 Ready-made functions include
331 `mm-file-name-delete-whitespace',
332 `mm-file-name-trim-whitespace',
333 `mm-file-name-collapse-whitespace',
334 `mm-file-name-replace-whitespace',
335 `capitalize', `downcase', `upcase', and
338 (defvar mm-path-name-rewrite-functions nil
339 "*List of functions for rewriting the full file names of MIME parts.
340 This is used when viewing parts externally, and is meant for
341 transforming the absolute name so that non-compliant programs can find
342 the file where it's saved.
344 Each function takes a file name as input and returns a file name.")
346 (defvar mm-file-name-replace-whitespace nil
347 "String used for replacing whitespace characters; default is `\"_\"'.")
349 (defcustom mm-default-directory nil
350 "The default directory where mm will save files.
351 If not set, `default-directory' will be used."
352 :type '(choice directory (const :tag "Default" nil))
353 :group 'mime-display)
355 (defcustom mm-external-terminal-program "xterm"
356 "The program to start an external terminal."
358 :group 'mime-display)
360 ;;; Internal variables.
362 (defvar mm-last-shell-command "")
363 (defvar mm-content-id-alist nil)
364 (defvar mm-postponed-undisplay-list nil)
366 ;; According to RFC2046, in particular, in a digest, the default
367 ;; Content-Type value for a body part is changed from "text/plain" to
369 (defvar mm-dissect-default-type "text/plain")
371 (autoload 'mml2015-verify "mml2015")
372 (autoload 'mml2015-verify-test "mml2015")
373 (autoload 'mml-smime-verify "mml-smime")
374 (autoload 'mml-smime-verify-test "mml-smime")
376 (defvar mm-verify-function-alist
377 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
378 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
379 mm-uu-pgp-signed-test)
380 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
381 mml-smime-verify-test)
382 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
383 mml-smime-verify-test)))
385 (defcustom mm-verify-option 'never
386 "Option of verifying signed parts.
387 `never', not verify; `always', always verify;
388 `known', only verify known protocols. Otherwise, ask user."
389 :type '(choice (item always)
391 (item :tag "only known protocols" known)
392 (item :tag "ask" nil))
393 :group 'mime-security)
395 (autoload 'mml2015-decrypt "mml2015")
396 (autoload 'mml2015-decrypt-test "mml2015")
398 (defvar mm-decrypt-function-alist
399 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
400 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
401 mm-uu-pgp-encrypted-test)))
403 (defcustom mm-decrypt-option nil
404 "Option of decrypting encrypted parts.
405 `never', not decrypt; `always', always decrypt;
406 `known', only decrypt known protocols. Otherwise, ask user."
407 :type '(choice (item always)
409 (item :tag "only known protocols" known)
410 (item :tag "ask" nil))
411 :group 'mime-security)
413 (defvar mm-viewer-completion-map
414 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
415 (set-keymap-parent map minibuffer-local-completion-map)
417 "Keymap for input viewer with completion.")
419 ;; Should we bind other key to minibuffer-complete-word?
420 (define-key mm-viewer-completion-map " " 'self-insert-command)
422 (defvar mm-viewer-completion-map
423 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
424 (set-keymap-parent map minibuffer-local-completion-map)
426 "Keymap for input viewer with completion.")
428 ;; Should we bind other key to minibuffer-complete-word?
429 (define-key mm-viewer-completion-map " " 'self-insert-command)
433 (defun mm-alist-to-plist (alist)
434 "Convert association list ALIST into the equivalent property-list form.
435 The plist is returned. This converts from
437 \((a . 1) (b . 2) (c . 3))
443 The original alist is not modified. See also `destructive-alist-to-plist'."
446 (let ((el (car alist)))
447 (setq plist (cons (cdr el) (cons (car el) plist))))
448 (setq alist (cdr alist)))
451 (defun mm-keep-viewer-alive-p (handle)
452 "Say whether external viewer for HANDLE should stay alive."
453 (let ((types mm-keep-viewer-alive-types)
454 (type (mm-handle-media-type handle))
457 (while (setq ty (pop types))
458 (when (string-match ty type)
459 (throw 'found t))))))
461 (defun mm-handle-set-external-undisplayer (handle function)
462 "Set the undisplayer for HANDLE to FUNCTION.
463 Postpone undisplaying of viewers for types in
464 `mm-keep-viewer-alive-types'."
465 (if (mm-keep-viewer-alive-p handle)
466 (let ((new-handle (copy-sequence handle)))
467 (mm-handle-set-undisplayer new-handle function)
468 (mm-handle-set-undisplayer handle nil)
469 (push new-handle mm-postponed-undisplay-list))
470 (mm-handle-set-undisplayer handle function)))
472 (defun mm-destroy-postponed-undisplay-list ()
473 (when mm-postponed-undisplay-list
474 (message "Destroying external MIME viewers")
475 (mm-destroy-parts mm-postponed-undisplay-list)))
477 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
478 "Dissect the current buffer and return a list of MIME handles."
480 (let (ct ctl type subtype cte cd description id result from)
482 (mail-narrow-to-head)
483 (when (or no-strict-mime
485 (mail-fetch-field "mime-version"))
486 (setq ct (mail-fetch-field "content-type")
487 ctl (ignore-errors (mail-header-parse-content-type ct))
488 cte (mail-fetch-field "content-transfer-encoding")
489 cd (mail-fetch-field "content-disposition")
490 description (mail-fetch-field "content-description")
491 from (mail-fetch-field "from")
492 id (mail-fetch-field "content-id"))
493 ;; FIXME: In some circumstances, this code is running within
494 ;; an unibyte macro. mail-extract-address-components
495 ;; creates unibyte buffers. This `if', though not a perfect
496 ;; solution, avoids most of them.
498 (setq from (cadr (mail-extract-address-components from))))))
500 (setq cte (mail-header-strip cte)))
502 (not (string-match "/" (car ctl))))
503 (mm-dissect-singlepart
504 (list mm-dissect-default-type)
505 (and cte (intern (downcase (mail-header-remove-whitespace
506 (mail-header-remove-comments
509 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
511 (setq type (split-string (car ctl) "/"))
512 (setq subtype (cadr type)
517 ((equal type "multipart")
518 (let ((mm-dissect-default-type (if (equal subtype "digest")
521 (add-text-properties 0 (length (car ctl))
522 (mm-alist-to-plist (cdr ctl)) (car ctl))
524 ;; what really needs to be done here is a way to link a
525 ;; MIME handle back to it's parent MIME handle (in a multilevel
526 ;; MIME article). That would probably require changing
527 ;; the mm-handle API so we simply store the multipart buffert
528 ;; name as a text property of the "multipart/whatever" string.
529 (add-text-properties 0 (length (car ctl))
530 (list 'buffer (mm-copy-to-buffer))
532 (add-text-properties 0 (length (car ctl))
535 (cons (car ctl) (mm-dissect-multipart ctl))))
537 (mm-possibly-verify-or-decrypt
538 (mm-dissect-singlepart
540 (and cte (intern (downcase (mail-header-remove-whitespace
541 (mail-header-remove-comments
544 (and cd (ignore-errors
545 (mail-header-parse-content-disposition cd)))
549 (when (string-match " *<\\(.*\\)> *" id)
550 (setq id (match-string 1 id)))
551 (push (cons id result) mm-content-id-alist))
554 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
556 (if (equal "text/plain" (car ctl))
560 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
562 (defun mm-dissect-multipart (ctl)
563 (goto-char (point-min))
564 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
565 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
568 (goto-char (point-max))
569 (if (re-search-backward close-delimiter nil t)
572 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
573 (while (and (< (point) end) (re-search-forward boundary end t))
574 (goto-char (match-beginning 0))
578 (narrow-to-region start (point))
579 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
581 (or (looking-at boundary)
583 (setq start (point)))
584 (when (and start (< start end))
587 (narrow-to-region start end)
588 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
589 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
591 (defun mm-copy-to-buffer ()
592 "Copy the contents of the current buffer to a fresh buffer."
594 (let ((flag enable-multibyte-characters)
595 (new-buffer (generate-new-buffer " *mm*")))
596 (goto-char (point-min))
597 (search-forward-regexp "^\n" nil t)
599 (narrow-to-region (point) (point-max))
601 (set-buffer-multibyte nil))
602 (copy-to-buffer new-buffer (point-min) (point-max))
604 (set-buffer-multibyte t)))
607 (defun mm-display-parts (handle &optional no-default)
608 (if (stringp (car handle))
609 (mapcar 'mm-display-parts (cdr handle))
610 (if (bufferp (car handle))
612 (narrow-to-region (point) (point))
613 (mm-display-part handle)
614 (goto-char (point-max)))
615 (mapcar 'mm-display-parts handle))))
617 (defun mm-display-part (handle &optional no-default)
618 "Display the MIME part represented by HANDLE.
619 Returns nil if the part is removed; inline if displayed inline;
620 external if displayed external."
622 (mailcap-parse-mailcaps)
623 (if (mm-handle-displayed-p handle)
624 (mm-remove-part handle)
625 (let* ((type (mm-handle-media-type handle))
626 (method (mailcap-mime-info type)))
627 (if (and (mm-inlinable-p handle)
628 (mm-inlined-p handle))
631 (mm-display-inline handle)
635 (if (and (not method)
636 (equal "text" (car (split-string type))))
639 (mm-insert-inline handle (mm-get-part handle))
642 handle (or method 'mailcap-save-binary-file)))))))))
644 (defun mm-display-external (handle method)
645 "Display HANDLE using METHOD."
646 (let ((outbuf (current-buffer)))
647 (mm-with-unibyte-buffer
648 (if (functionp method)
649 (let ((cur (current-buffer)))
650 (if (eq method 'mailcap-save-binary-file)
652 (set-buffer (generate-new-buffer " *mm*"))
654 (mm-insert-part handle)
655 (let ((win (get-buffer-window cur t)))
657 (select-window win)))
658 (switch-to-buffer (generate-new-buffer " *mm*")))
659 (buffer-disable-undo)
660 (mm-set-buffer-file-coding-system mm-binary-coding-system)
661 (insert-buffer-substring cur)
662 (goto-char (point-min))
663 (message "Viewing with %s" method)
664 (let ((mm (current-buffer))
665 (non-viewer (assq 'non-viewer
667 (mm-handle-media-type handle) t))))
671 (mm-save-part handle))
672 (when (and (not non-viewer)
674 (mm-handle-set-undisplayer handle mm)))))
675 ;; The function is a string to be executed.
676 (mm-insert-part handle)
677 (let* ((dir (mm-make-temp-file
678 (expand-file-name "emm." mm-tmp-directory) 'dir))
680 (mail-content-type-get
681 (mm-handle-disposition handle) 'filename)
682 (mail-content-type-get
683 (mm-handle-type handle) 'name)))
684 (mime-info (mailcap-mime-info
685 (mm-handle-media-type handle) t))
686 (needsterm (or (assoc "needsterm" mime-info)
687 (assoc "needsterminal" mime-info)))
688 (copiousoutput (assoc "copiousoutput" mime-info))
690 ;; We create a private sub-directory where we store our files.
691 (set-file-modes dir 448)
693 (setq file (expand-file-name
694 (gnus-map-function mm-file-name-rewrite-functions
695 (file-name-nondirectory filename))
697 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
698 (let ((coding-system-for-write mm-binary-coding-system))
699 (write-region (point-min) (point-max) file nil 'nomesg))
700 (message "Viewing with %s" method)
703 (let ((command (mm-mailcap-command
704 method file (mm-handle-type handle))))
707 (start-process "*display*" nil
708 mm-external-terminal-program
710 shell-command-switch command)
718 shell-command-switch command)))
721 (set-process-sentinel
722 (get-buffer-process buffer)
723 `(lambda (process state)
724 (if (eq 'exit (process-status process))
725 (gnus-configure-windows
726 ',gnus-current-window-configuration))))
727 (gnus-configure-windows 'display-term))
728 (mm-handle-set-external-undisplayer handle (cons file buffer)))
729 (message "Displaying %s..." command))
732 (with-current-buffer outbuf
738 (call-process shell-file-name nil
740 (generate-new-buffer " *mm*"))
744 method file (mm-handle-type handle)))
745 (if (buffer-live-p buffer)
750 (ignore-errors (delete-file file))
751 (ignore-errors (delete-directory
752 (file-name-directory file)))
753 (ignore-errors (kill-buffer buffer))))))
756 (let ((command (mm-mailcap-command
757 method file (mm-handle-type handle))))
759 (start-process "*display*"
761 (generate-new-buffer " *mm*"))
763 shell-command-switch command)
764 (mm-handle-set-external-undisplayer
765 handle (cons file buffer)))
766 (message "Displaying %s..." command))
769 (defun mm-mailcap-command (method file type-list)
770 (let ((ctl (cdr type-list))
774 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
776 (push (substring method beg (match-beginning 0)) out)
777 (setq beg (match-end 0)
778 total (match-string 0 method)
779 sub (match-string 1 method))
781 ((string= total "%%")
783 ((or (string= total "%s")
784 ;; We do our own quoting.
785 (string= total "'%s'")
786 (string= total "\"%s\""))
787 (setq uses-stdin nil)
789 (gnus-map-function mm-path-name-rewrite-functions file)) out))
790 ((string= total "%t")
791 (push (mm-quote-arg (car type-list)) out))
793 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
794 (push (substring method beg (length method)) out)
798 (gnus-map-function mm-path-name-rewrite-functions file))
800 (mapconcat 'identity (nreverse out) "")))
802 (defun mm-remove-parts (handles)
803 "Remove the displayed MIME parts represented by HANDLES."
804 (if (and (listp handles)
805 (bufferp (car handles)))
806 (mm-remove-part handles)
808 (while (setq handle (pop handles))
811 (when (buffer-live-p (get-text-property 0 'buffer handle))
812 (kill-buffer (get-text-property 0 'buffer handle))))
814 (stringp (car handle)))
815 (mm-remove-parts (cdr handle)))
817 (mm-remove-part handle)))))))
819 (defun mm-destroy-parts (handles)
820 "Remove the displayed MIME parts represented by HANDLES."
821 (if (and (listp handles)
822 (bufferp (car handles)))
823 (mm-destroy-part handles)
825 (while (setq handle (pop handles))
828 (when (buffer-live-p (get-text-property 0 'buffer handle))
829 (kill-buffer (get-text-property 0 'buffer handle))))
831 (stringp (car handle)))
832 (mm-destroy-parts handle))
834 (mm-destroy-part handle)))))))
836 (defun mm-remove-part (handle)
837 "Remove the displayed MIME part represented by HANDLE."
839 (let ((object (mm-handle-undisplayer handle)))
842 ;; Internally displayed part.
843 ((mm-annotationp object)
844 (delete-annotation object))
845 ((or (functionp object)
847 (eq (car object) 'lambda)))
849 ;; Externally displayed part.
851 (ignore-errors (delete-file (car object)))
852 (ignore-errors (delete-directory (file-name-directory (car object))))
853 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
855 (when (buffer-live-p object)
856 (kill-buffer object)))))
857 (mm-handle-set-undisplayer handle nil))))
859 (defun mm-display-inline (handle)
860 (let* ((type (mm-handle-media-type handle))
861 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
862 (funcall function handle)
863 (goto-char (point-min))))
865 (defun mm-assoc-string-match (alist type)
867 (when (string-match (car elem) type)
870 (defun mm-automatic-display-p (handle)
871 "Say whether the user wants HANDLE to be displayed automatically."
872 (let ((methods mm-automatic-display)
873 (type (mm-handle-media-type handle))
875 (while (setq method (pop methods))
876 (when (and (not (mm-inline-override-p handle))
877 (string-match method type))
882 (defun mm-inlinable-p (handle)
883 "Say whether HANDLE can be displayed inline."
884 (let ((alist mm-inline-media-tests)
885 (type (mm-handle-media-type handle))
888 (when (string-match (caar alist) type)
889 (setq test (caddar alist)
891 (setq test (funcall test handle)))
895 (defun mm-inlined-p (handle)
896 "Say whether the user wants HANDLE to be displayed inline."
897 (let ((methods mm-inlined-types)
898 (type (mm-handle-media-type handle))
900 (while (setq method (pop methods))
901 (when (and (not (mm-inline-override-p handle))
902 (string-match method type))
907 (defun mm-attachment-override-p (handle)
908 "Say whether HANDLE should have attachment behavior overridden."
909 (let ((types mm-attachment-override-types)
910 (type (mm-handle-media-type handle))
913 (while (setq ty (pop types))
914 (when (and (string-match ty type)
915 (mm-inlinable-p handle))
916 (throw 'found t))))))
918 (defun mm-inline-override-p (handle)
919 "Say whether HANDLE should have inline behavior overridden."
920 (let ((types mm-inline-override-types)
921 (type (mm-handle-media-type handle))
924 (while (setq ty (pop types))
925 (when (string-match ty type)
926 (throw 'found t))))))
928 (defun mm-automatic-external-display-p (type)
929 "Return the user-defined method for TYPE."
930 (let ((methods mm-automatic-external-display)
932 (while (setq method (pop methods))
933 (when (string-match method type)
938 (defun mm-destroy-part (handle)
939 "Destroy the data structures connected to HANDLE."
941 (mm-remove-part handle)
942 (when (buffer-live-p (mm-handle-buffer handle))
943 (kill-buffer (mm-handle-buffer handle)))))
945 (defun mm-handle-displayed-p (handle)
946 "Say whether HANDLE is displayed or not."
947 (mm-handle-undisplayer handle))
950 ;;; Functions for outputting parts
953 (defun mm-get-part (handle)
954 "Return the contents of HANDLE as a string."
955 (mm-with-unibyte-buffer
956 (insert (with-current-buffer (mm-handle-buffer handle)
957 (mm-with-unibyte-current-buffer
959 (mm-decode-content-transfer-encoding
960 (mm-handle-encoding handle)
961 (mm-handle-media-type handle))
964 (defun mm-insert-part (handle)
965 "Insert the contents of HANDLE in the current buffer."
966 (let ((cur (current-buffer)))
968 (if (member (mm-handle-media-supertype handle) '("text" "message"))
970 (insert-buffer-substring (mm-handle-buffer handle))
972 (mm-decode-content-transfer-encoding
973 (mm-handle-encoding handle)
974 (mm-handle-media-type handle))
975 (let ((temp (current-buffer)))
977 (insert-buffer-substring temp))))
978 (mm-with-unibyte-buffer
979 (insert-buffer-substring (mm-handle-buffer handle))
981 (mm-decode-content-transfer-encoding
982 (mm-handle-encoding handle)
983 (mm-handle-media-type handle))
984 (let ((temp (current-buffer)))
986 (insert-buffer-substring temp))))))))
988 (defun mm-file-name-delete-whitespace (file-name)
989 "Remove all whitespace characters from FILE-NAME."
990 (while (string-match "\\s-+" file-name)
991 (setq file-name (replace-match "" t t file-name)))
994 (defun mm-file-name-trim-whitespace (file-name)
995 "Remove leading and trailing whitespace characters from FILE-NAME."
996 (when (string-match "\\`\\s-+" file-name)
997 (setq file-name (substring file-name (match-end 0))))
998 (when (string-match "\\s-+\\'" file-name)
999 (setq file-name (substring file-name 0 (match-beginning 0))))
1002 (defun mm-file-name-collapse-whitespace (file-name)
1003 "Collapse multiple whitespace characters in FILE-NAME."
1004 (while (string-match "\\s-\\s-+" file-name)
1005 (setq file-name (replace-match " " t t file-name)))
1008 (defun mm-file-name-replace-whitespace (file-name)
1009 "Replace whitespace characters in FILE-NAME with underscores.
1010 Set the option `mm-file-name-replace-whitespace' to any other
1011 string if you do not like underscores."
1012 (let ((s (or mm-file-name-replace-whitespace "_")))
1013 (while (string-match "\\s-" file-name)
1014 (setq file-name (replace-match s t t file-name))))
1017 (defun mm-save-part (handle)
1018 "Write HANDLE to a file."
1019 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1020 (filename (mail-content-type-get
1021 (mm-handle-disposition handle) 'filename))
1024 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1025 (file-name-nondirectory filename))))
1027 (read-file-name "Save MIME part to: "
1028 (or mm-default-directory default-directory)
1029 nil nil (or filename name "")))
1030 (setq mm-default-directory (file-name-directory file))
1031 (and (or (not (file-exists-p file))
1032 (yes-or-no-p (format "File %s already exists; overwrite? "
1035 (mm-save-part-to-file handle file)
1038 (defun mm-save-part-to-file (handle file)
1039 (mm-with-unibyte-buffer
1040 (mm-insert-part handle)
1041 (let ((coding-system-for-write 'binary)
1042 ;; Don't re-compress .gz & al. Arguably we should make
1043 ;; `file-name-handler-alist' nil, but that would chop
1044 ;; ange-ftp, which is reasonable to use here.
1045 (inhibit-file-name-operation 'write-region)
1046 (inhibit-file-name-handlers
1047 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1048 (write-region (point-min) (point-max) file))))
1050 (defun mm-pipe-part (handle)
1051 "Pipe HANDLE to a process."
1052 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1054 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1055 (mm-with-unibyte-buffer
1056 (mm-insert-part handle)
1057 (let ((coding-system-for-write 'binary))
1058 (shell-command-on-region (point-min) (point-max) command nil)))))
1060 (defun mm-interactively-view-part (handle)
1061 "Display HANDLE using METHOD."
1062 (let* ((type (mm-handle-media-type handle))
1064 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1065 (mailcap-mime-info type 'all)))
1066 (method (let ((minibuffer-local-completion-map
1067 mm-viewer-completion-map))
1068 (completing-read "Viewer: " methods))))
1069 (when (string= method "")
1070 (error "No method given"))
1071 (if (string-match "^[^% \t]+$" method)
1072 (setq method (concat method " %s")))
1073 (mm-display-external handle method)))
1075 (defun mm-preferred-alternative (handles &optional preferred)
1076 "Say which of HANDLES are preferred."
1077 (let ((prec (if preferred (list preferred)
1078 (mm-preferred-alternative-precedence handles)))
1079 p h result type handle)
1080 (while (setq p (pop prec))
1083 (setq handle (car h))
1084 (setq type (mm-handle-media-type handle))
1085 (when (and (equal p type)
1086 (mm-automatic-display-p handle)
1087 (or (stringp (car handle))
1088 (not (mm-handle-disposition handle))
1089 (equal (car (mm-handle-disposition handle))
1097 (defun mm-preferred-alternative-precedence (handles)
1098 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1099 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1101 (dolist (disc (reverse mm-discouraged-alternatives))
1102 (dolist (elem (copy-sequence seq))
1103 (when (string-match disc elem)
1104 (setq seq (nconc (delete elem seq) (list elem))))))
1107 (defun mm-get-content-id (id)
1108 "Return the handle(s) referred to by ID."
1109 (cdr (assoc id mm-content-id-alist)))
1111 (defconst mm-image-type-regexps
1112 '(("/\\*.*XPM.\\*/" . xpm)
1116 ("\211PNG\r\n" . png)
1118 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1119 ("%!PS" . postscript))
1120 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1121 When the first bytes of an image file match REGEXP, it is assumed to
1122 be of image type IMAGE-TYPE.")
1124 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1125 (defun mm-image-type-from-buffer ()
1126 "Determine the image type from data in the current buffer.
1127 Value is a symbol specifying the image type or nil if type cannot
1129 (let ((types mm-image-type-regexps)
1131 (goto-char (point-min))
1132 (while (and types (null type))
1133 (let ((regexp (car (car types)))
1134 (image-type (cdr (car types))))
1135 (when (looking-at regexp)
1136 (setq type image-type))
1137 (setq types (cdr types))))
1140 (defun mm-get-image (handle)
1141 "Return an image instance based on HANDLE."
1142 (let ((type (mm-handle-media-subtype handle))
1144 ;; Allow some common translations.
1147 ((equal type "x-pixmap")
1149 ((equal type "x-xbitmap")
1151 ((equal type "x-portable-bitmap")
1154 (or (mm-handle-cache handle)
1155 (mm-with-unibyte-buffer
1156 (mm-insert-part handle)
1160 ;; Avoid testing `make-glyph' since W3 may define
1161 ;; a bogus version of it.
1162 (if (fboundp 'create-image)
1163 (create-image (buffer-string)
1164 (or (mm-image-type-from-buffer)
1167 (mm-create-image-xemacs type))))
1168 (mm-handle-set-cache handle spec))))))
1170 (defun mm-create-image-xemacs (type)
1173 ;; xbm images require special handling, since
1174 ;; the only way to create glyphs from these
1175 ;; (without a ton of work) is to write them
1176 ;; out to a file, and then create a file
1178 (let ((file (mm-make-temp-file
1179 (expand-file-name "emm.xbm"
1180 mm-tmp-directory))))
1183 (write-region (point-min) (point-max) file)
1184 (make-glyph (list (cons 'x file))))
1186 (delete-file file)))))
1190 (or (mm-image-type-from-buffer)
1192 :data (buffer-string))))))
1194 (defun mm-image-fit-p (handle)
1195 "Say whether the image in HANDLE will fit the current window."
1196 (let ((image (mm-get-image handle)))
1197 (if (fboundp 'glyph-width)
1198 ;; XEmacs' glyphs can actually tell us about their width, so
1199 ;; lets be nice and smart about them.
1200 (or mm-inline-large-images
1201 (and (< (glyph-width image) (window-pixel-width))
1202 (< (glyph-height image) (window-pixel-height))))
1203 (let* ((size (image-size image))
1206 (or mm-inline-large-images
1207 (and (< h (1- (window-height))) ; Don't include mode line.
1208 (< w (window-width))))))))
1210 (defun mm-valid-image-format-p (format)
1211 "Say whether FORMAT can be displayed natively by Emacs."
1214 ((fboundp 'valid-image-instantiator-format-p)
1215 (valid-image-instantiator-format-p format))
1217 ((fboundp 'image-type-available-p)
1218 (and (display-graphic-p)
1219 (image-type-available-p format)))
1220 ;; Nobody else can do images yet.
1224 (defun mm-valid-and-fit-image-p (format handle)
1225 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1226 (and (mm-valid-image-format-p format)
1227 (mm-image-fit-p handle)))
1229 (defun mm-find-part-by-type (handles type &optional notp recursive)
1230 "Search in HANDLES for part with TYPE.
1231 If NOTP, returns first non-matching part.
1232 If RECURSIVE, search recursively."
1235 (if (and recursive (stringp (caar handles)))
1236 (if (setq handle (mm-find-part-by-type (cdar handles) type
1240 (not (equal (mm-handle-media-type (car handles)) type))
1241 (equal (mm-handle-media-type (car handles)) type))
1242 (setq handle (car handles)
1244 (setq handles (cdr handles)))
1247 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1248 (goto-char (point-min))
1249 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1251 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1253 (end (save-excursion
1254 (goto-char (point-max))
1255 (if (re-search-backward close-delimiter nil t)
1259 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1260 (while (and (not result)
1261 (re-search-forward boundary end t))
1262 (goto-char (match-beginning 0))
1266 (narrow-to-region start (1- (point)))
1267 (when (let ((ctl (ignore-errors
1268 (mail-header-parse-content-type
1269 (mail-fetch-field "content-type")))))
1271 (not (equal (car ctl) type))
1272 (equal (car ctl) type)))
1273 (setq result (buffer-string))))))
1275 (setq start (point)))
1276 (when (and (not result) start)
1279 (narrow-to-region start end)
1280 (when (let ((ctl (ignore-errors
1281 (mail-header-parse-content-type
1282 (mail-fetch-field "content-type")))))
1284 (not (equal (car ctl) type))
1285 (equal (car ctl) type)))
1286 (setq result (buffer-string))))))
1289 (defvar mm-security-handle nil)
1291 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1292 ;; HANDLE could be a CTL.
1294 (put-text-property 0 (length (car handle)) parameter value
1297 (defun mm-possibly-verify-or-decrypt (parts ctl)
1298 (let ((type (car ctl))
1299 (subtype (cadr (split-string (car ctl) "/")))
1300 (mm-security-handle ctl) ;; (car CTL) is the type.
1301 protocol func functest)
1303 ((or (equal type "application/x-pkcs7-mime")
1304 (equal type "application/pkcs7-mime"))
1307 ((eq mm-decrypt-option 'never) nil)
1308 ((eq mm-decrypt-option 'always) t)
1309 ((eq mm-decrypt-option 'known) t)
1311 (format "Decrypt (S/MIME) part? "))))
1312 (mm-view-pkcs7 parts))
1313 (setq parts (mm-dissect-buffer t)))))
1314 ((equal subtype "signed")
1315 (unless (and (setq protocol
1316 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1317 (not (equal protocol "multipart/mixed")))
1318 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1319 (let ((protocols mm-verify-function-alist))
1321 (if (and (or (not (setq functest (nth 3 (car protocols))))
1322 (funcall functest parts ctl))
1323 (mm-find-part-by-type parts (caar protocols) nil t))
1324 (setq protocol (caar protocols)
1326 (setq protocols (cdr protocols))))))
1327 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1329 ((eq mm-verify-option 'never) nil)
1330 ((eq mm-verify-option 'always) t)
1331 ((eq mm-verify-option 'known)
1333 (or (not (setq functest
1334 (nth 3 (assoc protocol
1335 mm-verify-function-alist))))
1336 (funcall functest parts ctl))))
1339 (format "Verify signed (%s) part? "
1340 (or (nth 2 (assoc protocol mm-verify-function-alist))
1341 (format "protocol=%s" protocol))))))
1344 (funcall func parts ctl)
1345 (mm-set-handle-multipart-parameter
1346 mm-security-handle 'gnus-details
1347 (format "Unknown sign protocol (%s)" protocol))))))
1348 ((equal subtype "encrypted")
1349 (unless (setq protocol
1350 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1351 ;; The message is broken.
1352 (let ((parts parts))
1354 (if (assoc (mm-handle-media-type (car parts))
1355 mm-decrypt-function-alist)
1356 (setq protocol (mm-handle-media-type (car parts))
1358 (setq parts (cdr parts))))))
1359 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1361 ((eq mm-decrypt-option 'never) nil)
1362 ((eq mm-decrypt-option 'always) t)
1363 ((eq mm-decrypt-option 'known)
1365 (or (not (setq functest
1366 (nth 3 (assoc protocol
1367 mm-decrypt-function-alist))))
1368 (funcall functest parts ctl))))
1371 (format "Decrypt (%s) part? "
1372 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1373 (format "protocol=%s" protocol))))))
1376 (setq parts (funcall func parts ctl))
1377 (mm-set-handle-multipart-parameter
1378 mm-security-handle 'gnus-details
1379 (format "Unknown encrypt protocol (%s)" protocol))))))
1383 (defun mm-multiple-handles (handles)
1384 (and (listp (car handles))
1385 (> (length handles) 1)))
1387 (defun mm-merge-handles (handles1 handles2)
1389 (if (listp (car handles1))
1392 (if (listp (car handles2))
1396 (defun mm-readable-p (handle)
1397 "Say whether the content of HANDLE is readable."
1398 (and (< (with-current-buffer (mm-handle-buffer handle)
1399 (buffer-size)) 10000)
1400 (mm-with-unibyte-buffer
1401 (mm-insert-part handle)
1402 (and (eq (mm-body-7-or-8) '7bit)
1403 (not (mm-long-lines-p 76))))))
1405 (provide 'mm-decode)
1407 ;;; mm-decode.el ends here