1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; 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 '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)Display Customization")
49 (defgroup mime-security ()
50 "MIME security in mail and news articles."
51 :link '(custom-manual "(emacs-mime)Display 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 ((executable-find "w3m") (if (locate-library "w3m")
104 ((executable-find "links") 'links)
105 ((executable-find "lynx") 'lynx)
107 "Render of HTML contents.
108 It is one of defined renderer types, or a rendering function.
109 The defined renderer types are:
111 `w3m' : use emacs-w3m;
112 `w3m-standalone': use w3m;
115 `html2text' : use html2text;
116 nil : use external viewer."
118 :type '(choice (const w3)
120 (const w3m-standalone)
126 :group 'mime-display)
128 (defvar mm-inline-text-html-renderer nil
129 "Function used for rendering inline HTML contents.
130 It is suggested to customize `mm-text-html-renderer' instead.")
132 (defcustom mm-inline-text-html-with-images nil
133 "If non-nil, Gnus will allow retrieving images in HTML contents with
134 the <img> tags. It has no effect on Emacs/w3. See also the
135 documentation for the `mm-w3m-safe-url-regexp' variable."
138 :group 'mime-display)
140 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
141 "Regexp matching URLs which are considered to be safe.
142 Some HTML mails might contain a nasty trick used by spammers, using
143 the <img> tag which is far more evil than the [Click Here!] button.
144 It is most likely intended to check whether the ominous spam mail has
145 reached your eyes or not, in which case the spammer knows for sure
146 that your email address is valid. It is done by embedding an
147 identifier string into a URL that you might automatically retrieve
148 when displaying the image. The default value is \"\\\\`cid:\" which only
149 matches parts embedded to the Multipart/Related type MIME contents and
150 Gnus will never connect to the spammer's site arbitrarily. You may
151 set this variable to nil if you consider all urls to be safe."
152 :type '(choice (regexp :tag "Regexp")
153 (const :tag "All URLs are safe" nil))
154 :group 'mime-display)
156 (defcustom mm-inline-text-html-with-w3m-keymap t
157 "If non-nil, use emacs-w3m command keys in the article buffer."
159 :group 'mime-display)
161 (defcustom mm-enable-external t
162 "Indicate whether external MIME handlers should be used.
164 If t, all defined external MIME handlers are used. If nil, files are saved by
165 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
166 before the external MIME handler is invoked."
168 :type '(choice (const :tag "Always" t)
169 (const :tag "Never" nil)
170 (const :tag "Ask" ask))
171 :group 'mime-display)
173 (defcustom mm-inline-media-tests
177 (mm-valid-and-fit-image-p 'jpeg handle)))
181 (mm-valid-and-fit-image-p 'png handle)))
185 (mm-valid-and-fit-image-p 'gif handle)))
189 (mm-valid-and-fit-image-p 'tiff handle)) )
193 (mm-valid-and-fit-image-p 'xbm handle)))
197 (mm-valid-and-fit-image-p 'xbm handle)))
201 (mm-valid-and-fit-image-p 'xpm handle)))
205 (mm-valid-and-fit-image-p 'xpm handle)))
209 (mm-valid-and-fit-image-p 'bmp handle)))
210 ("image/x-portable-bitmap"
213 (mm-valid-and-fit-image-p 'pbm handle)))
214 ("text/plain" mm-inline-text identity)
215 ("text/enriched" mm-inline-text identity)
216 ("text/richtext" mm-inline-text identity)
217 ("text/x-patch" mm-display-patch-inline
219 (locate-library "diff-mode")))
220 ("application/emacs-lisp" mm-display-elisp-inline identity)
221 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
222 ("text/dns" mm-display-dns-inline identity)
226 (or mm-inline-text-html-renderer
227 mm-text-html-renderer)))
231 (or (featurep 'vcard)
232 (locate-library "vcard"))))
233 ("message/delivery-status" mm-inline-text identity)
234 ("message/rfc822" mm-inline-message identity)
235 ("message/partial" mm-inline-partial identity)
236 ("message/external-body" mm-inline-external-body identity)
237 ("text/.*" mm-inline-text identity)
238 ("audio/wav" mm-inline-audio
240 (and (or (featurep 'nas-sound) (featurep 'native-sound))
241 (device-sound-enabled-p))))
245 (and (or (featurep 'nas-sound) (featurep 'native-sound))
246 (device-sound-enabled-p))))
247 ("application/pgp-signature" ignore identity)
248 ("application/x-pkcs7-signature" ignore identity)
249 ("application/pkcs7-signature" ignore identity)
250 ("application/x-pkcs7-mime" ignore identity)
251 ("application/pkcs7-mime" ignore identity)
252 ("multipart/alternative" ignore identity)
253 ("multipart/mixed" ignore identity)
254 ("multipart/related" ignore identity)
255 ;; Disable audio and image
256 ("audio/.*" ignore ignore)
257 ("image/.*" ignore ignore)
258 ;; Default to displaying as text
259 (".*" mm-inline-text mm-readable-p))
260 "Alist of media types/tests saying whether types can be displayed inline."
261 :type '(repeat (list (regexp :tag "MIME type")
262 (function :tag "Display function")
263 (function :tag "Display test")))
264 :group 'mime-display)
266 (defcustom mm-inlined-types
267 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
268 "message/partial" "message/external-body" "application/emacs-lisp"
269 "application/x-emacs-lisp"
270 "application/pgp-signature" "application/x-pkcs7-signature"
271 "application/pkcs7-signature" "application/x-pkcs7-mime"
272 "application/pkcs7-mime")
273 "List of media types that are to be displayed inline.
274 See also `mm-inline-media-tests', which says how to display a media
276 :type '(repeat string)
277 :group 'mime-display)
279 (defcustom mm-keep-viewer-alive-types
280 '("application/postscript" "application/msword" "application/vnd.ms-excel"
281 "application/pdf" "application/x-dvi")
282 "List of media types for which the external viewer will not be killed
283 when selecting a different article."
285 :type '(repeat string)
286 :group 'mime-display)
288 (defcustom mm-automatic-display
289 '("text/plain" "text/enriched" "text/richtext" "text/html"
290 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
291 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
292 "application/emacs-lisp" "application/x-emacs-lisp"
293 "application/x-pkcs7-signature"
294 "application/pkcs7-signature" "application/x-pkcs7-mime"
295 "application/pkcs7-mime")
296 "A list of MIME types to be displayed automatically."
297 :type '(repeat string)
298 :group 'mime-display)
300 (defcustom mm-attachment-override-types '("text/x-vcard"
301 "application/pkcs7-mime"
302 "application/x-pkcs7-mime"
303 "application/pkcs7-signature"
304 "application/x-pkcs7-signature")
305 "Types to have \"attachment\" ignored if they can be displayed inline."
306 :type '(repeat string)
307 :group 'mime-display)
309 (defcustom mm-inline-override-types nil
310 "Types to be treated as attachments even if they can be displayed inline."
311 :type '(repeat string)
312 :group 'mime-display)
314 (defcustom mm-automatic-external-display nil
315 "List of MIME type regexps that will be displayed externally automatically."
316 :type '(repeat string)
317 :group 'mime-display)
319 (defcustom mm-discouraged-alternatives nil
320 "List of MIME types that are discouraged when viewing multipart/alternative.
321 Viewing agents are supposed to view the last possible part of a message,
322 as that is supposed to be the richest. However, users may prefer other
323 types instead, and this list says what types are most unwanted. If,
324 for instance, text/html parts are very unwanted, and text/richtext are
325 somewhat unwanted, then the value of this variable should be set
328 (\"text/html\" \"text/richtext\")"
329 :type '(repeat string)
330 :group 'mime-display)
332 (defcustom mm-tmp-directory
333 (if (fboundp 'temp-directory)
335 (if (boundp 'temporary-file-directory)
336 temporary-file-directory
338 "Where mm will store its temporary files."
340 :group 'mime-display)
342 (defcustom mm-inline-large-images nil
343 "If non-nil, then all images fit in the buffer."
345 :group 'mime-display)
347 (defvar mm-file-name-rewrite-functions
348 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
349 "*List of functions used for rewriting file names of MIME parts.
350 Each function takes a file name as input and returns a file name.
352 Ready-made functions include
353 `mm-file-name-delete-control'
354 `mm-file-name-delete-gotchas'
355 `mm-file-name-delete-whitespace',
356 `mm-file-name-trim-whitespace',
357 `mm-file-name-collapse-whitespace',
358 `mm-file-name-replace-whitespace',
359 `capitalize', `downcase', `upcase', and
362 (defvar mm-path-name-rewrite-functions nil
363 "*List of functions for rewriting the full file names of MIME parts.
364 This is used when viewing parts externally, and is meant for
365 transforming the absolute name so that non-compliant programs can find
366 the file where it's saved.
368 Each function takes a file name as input and returns a file name.")
370 (defvar mm-file-name-replace-whitespace nil
371 "String used for replacing whitespace characters; default is `\"_\"'.")
373 (defcustom mm-default-directory nil
374 "The default directory where mm will save files.
375 If not set, `default-directory' will be used."
376 :type '(choice directory (const :tag "Default" nil))
377 :group 'mime-display)
379 (defcustom mm-attachment-file-modes 384
380 "Set the mode bits of saved attachments to this integer."
382 :group 'mime-display)
384 (defcustom mm-external-terminal-program "xterm"
385 "The program to start an external terminal."
388 :group 'mime-display)
390 ;;; Internal variables.
392 (defvar mm-last-shell-command "")
393 (defvar mm-content-id-alist nil)
394 (defvar mm-postponed-undisplay-list nil)
396 ;; According to RFC2046, in particular, in a digest, the default
397 ;; Content-Type value for a body part is changed from "text/plain" to
399 (defvar mm-dissect-default-type "text/plain")
401 (autoload 'mml2015-verify "mml2015")
402 (autoload 'mml2015-verify-test "mml2015")
403 (autoload 'mml-smime-verify "mml-smime")
404 (autoload 'mml-smime-verify-test "mml-smime")
406 (defvar mm-verify-function-alist
407 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
408 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
409 mm-uu-pgp-signed-test)
410 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
411 mml-smime-verify-test)
412 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
413 mml-smime-verify-test)))
415 (defcustom mm-verify-option 'never
416 "Option of verifying signed parts.
417 `never', not verify; `always', always verify;
418 `known', only verify known protocols. Otherwise, ask user."
420 :type '(choice (item always)
422 (item :tag "only known protocols" known)
423 (item :tag "ask" nil))
424 :group 'mime-security)
426 (autoload 'mml2015-decrypt "mml2015")
427 (autoload 'mml2015-decrypt-test "mml2015")
429 (defvar mm-decrypt-function-alist
430 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
431 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
432 mm-uu-pgp-encrypted-test)))
434 (defcustom mm-decrypt-option nil
435 "Option of decrypting encrypted parts.
436 `never', not decrypt; `always', always decrypt;
437 `known', only decrypt known protocols. Otherwise, ask user."
438 :type '(choice (item always)
440 (item :tag "only known protocols" known)
441 (item :tag "ask" nil))
442 :group 'mime-security)
444 (defvar mm-viewer-completion-map
445 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
446 (set-keymap-parent map minibuffer-local-completion-map)
448 "Keymap for input viewer with completion.")
450 ;; Should we bind other key to minibuffer-complete-word?
451 (define-key mm-viewer-completion-map " " 'self-insert-command)
453 (defvar mm-viewer-completion-map
454 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
455 (set-keymap-parent map minibuffer-local-completion-map)
457 "Keymap for input viewer with completion.")
459 ;; Should we bind other key to minibuffer-complete-word?
460 (define-key mm-viewer-completion-map " " 'self-insert-command)
464 (defun mm-alist-to-plist (alist)
465 "Convert association list ALIST into the equivalent property-list form.
466 The plist is returned. This converts from
468 \((a . 1) (b . 2) (c . 3))
474 The original alist is not modified. See also `destructive-alist-to-plist'."
477 (let ((el (car alist)))
478 (setq plist (cons (cdr el) (cons (car el) plist))))
479 (setq alist (cdr alist)))
482 (defun mm-keep-viewer-alive-p (handle)
483 "Say whether external viewer for HANDLE should stay alive."
484 (let ((types mm-keep-viewer-alive-types)
485 (type (mm-handle-media-type handle))
488 (while (setq ty (pop types))
489 (when (string-match ty type)
490 (throw 'found t))))))
492 (defun mm-handle-set-external-undisplayer (handle function)
493 "Set the undisplayer for HANDLE to FUNCTION.
494 Postpone undisplaying of viewers for types in
495 `mm-keep-viewer-alive-types'."
496 (if (mm-keep-viewer-alive-p handle)
497 (let ((new-handle (copy-sequence handle)))
498 (mm-handle-set-undisplayer new-handle function)
499 (mm-handle-set-undisplayer handle nil)
500 (push new-handle mm-postponed-undisplay-list))
501 (mm-handle-set-undisplayer handle function)))
503 (defun mm-destroy-postponed-undisplay-list ()
504 (when mm-postponed-undisplay-list
505 (message "Destroying external MIME viewers")
506 (mm-destroy-parts mm-postponed-undisplay-list)))
508 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
509 "Dissect the current buffer and return a list of MIME handles."
511 (let (ct ctl type subtype cte cd description id result from)
513 (mail-narrow-to-head)
514 (when (or no-strict-mime
516 (mail-fetch-field "mime-version"))
517 (setq ct (mail-fetch-field "content-type")
518 ctl (ignore-errors (mail-header-parse-content-type ct))
519 cte (mail-fetch-field "content-transfer-encoding")
520 cd (mail-fetch-field "content-disposition")
521 description (mail-fetch-field "content-description")
522 from (mail-fetch-field "from")
523 id (mail-fetch-field "content-id"))
524 ;; FIXME: In some circumstances, this code is running within
525 ;; an unibyte macro. mail-extract-address-components
526 ;; creates unibyte buffers. This `if', though not a perfect
527 ;; solution, avoids most of them.
529 (setq from (cadr (mail-extract-address-components from))))))
531 (not (string-match "/" (car ctl))))
532 (mm-dissect-singlepart
533 (list mm-dissect-default-type)
534 (and cte (intern (downcase (mail-header-strip cte))))
536 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
538 (setq type (split-string (car ctl) "/"))
539 (setq subtype (cadr type)
544 ((equal type "multipart")
545 (let ((mm-dissect-default-type (if (equal subtype "digest")
548 (start (cdr (assq 'start (cdr ctl)))))
549 (add-text-properties 0 (length (car ctl))
550 (mm-alist-to-plist (cdr ctl)) (car ctl))
552 ;; what really needs to be done here is a way to link a
553 ;; MIME handle back to it's parent MIME handle (in a multilevel
554 ;; MIME article). That would probably require changing
555 ;; the mm-handle API so we simply store the multipart buffert
556 ;; name as a text property of the "multipart/whatever" string.
557 (add-text-properties 0 (length (car ctl))
558 (list 'buffer (mm-copy-to-buffer)
562 (cons (car ctl) (mm-dissect-multipart ctl))))
564 (mm-possibly-verify-or-decrypt
565 (mm-dissect-singlepart
567 (and cte (intern (downcase (mail-header-strip cte))))
569 (and cd (ignore-errors
570 (mail-header-parse-content-disposition cd)))
574 (when (string-match " *<\\(.*\\)> *" id)
575 (setq id (match-string 1 id)))
576 (push (cons id result) mm-content-id-alist))
579 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
581 (if (equal "text/plain" (car ctl))
582 ;; FIXME: This is a kludge. Proper fix is to make
583 ;; gnus-display-mime invoke mm-uu-dissect on all
584 ;; textual MIME parts, and stop using mm-fill-flowed
586 (and mm-fill-flowed (assoc 'format ctl))
589 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
591 (defun mm-dissect-multipart (ctl)
592 (goto-char (point-min))
593 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
594 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
597 (goto-char (point-max))
598 (if (re-search-backward close-delimiter nil t)
601 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
602 (while (and (< (point) end) (re-search-forward boundary end t))
603 (goto-char (match-beginning 0))
607 (narrow-to-region start (point))
608 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
610 (or (looking-at boundary)
612 (setq start (point)))
613 (when (and start (< start end))
616 (narrow-to-region start end)
617 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
618 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
620 (defun mm-copy-to-buffer ()
621 "Copy the contents of the current buffer to a fresh buffer."
623 (let ((obuf (current-buffer))
625 (goto-char (point-min))
626 (search-forward-regexp "^\n" nil t)
629 ;; Preserve the data's unibyteness (for url-insert-file-contents).
630 (let ((default-enable-multibyte-characters (mm-multibyte-p)))
631 (generate-new-buffer " *mm*")))
632 (insert-buffer-substring obuf beg)
635 (defun mm-display-parts (handle &optional no-default)
636 (if (stringp (car handle))
637 (mapcar 'mm-display-parts (cdr handle))
638 (if (bufferp (car handle))
640 (narrow-to-region (point) (point))
641 (mm-display-part handle)
642 (goto-char (point-max)))
643 (mapcar 'mm-display-parts handle))))
645 (defun mm-display-part (handle &optional no-default)
646 "Display the MIME part represented by HANDLE.
647 Returns nil if the part is removed; inline if displayed inline;
648 external if displayed external."
650 (mailcap-parse-mailcaps)
651 (if (mm-handle-displayed-p handle)
652 (mm-remove-part handle)
653 (let* ((type (mm-handle-media-type handle))
654 (method (mailcap-mime-info type))
655 (filename (or (mail-content-type-get
656 (mm-handle-disposition handle) 'filename)
657 (mail-content-type-get
658 (mm-handle-type handle) 'name)
660 (external mm-enable-external))
661 (if (and (mm-inlinable-p handle)
662 (mm-inlined-p handle))
665 (mm-display-inline handle)
669 (if (and (not method)
670 (equal "text" (car (split-string type))))
673 (mm-insert-inline handle (mm-get-part handle))
675 (if (and method ;; If nil, we always use "save".
676 (stringp method) ;; 'mailcap-save-binary-file
677 (or (eq mm-enable-external t)
678 (and (eq mm-enable-external 'ask)
681 "Display part (" type
682 ") using external program"
683 ;; Can non-string method ever happen?
686 " \"" (format method filename) "\"")
693 handle (or method 'mailcap-save-binary-file))
695 handle 'mailcap-save-binary-file)))))))))
697 (defun mm-display-external (handle method)
698 "Display HANDLE using METHOD."
699 (let ((outbuf (current-buffer)))
700 (mm-with-unibyte-buffer
701 (if (functionp method)
702 (let ((cur (current-buffer)))
703 (if (eq method 'mailcap-save-binary-file)
705 (set-buffer (generate-new-buffer " *mm*"))
707 (mm-insert-part handle)
708 (let ((win (get-buffer-window cur t)))
710 (select-window win)))
711 (switch-to-buffer (generate-new-buffer " *mm*")))
712 (buffer-disable-undo)
713 (mm-set-buffer-file-coding-system mm-binary-coding-system)
714 (insert-buffer-substring cur)
715 (goto-char (point-min))
717 (message "Viewing with %s" method))
718 (let ((mm (current-buffer))
719 (non-viewer (assq 'non-viewer
721 (mm-handle-media-type handle) t))))
725 (mm-save-part handle))
726 (when (and (not non-viewer)
728 (mm-handle-set-undisplayer handle mm)))))
729 ;; The function is a string to be executed.
730 (mm-insert-part handle)
731 (let* ((dir (mm-make-temp-file
732 (expand-file-name "emm." mm-tmp-directory) 'dir))
734 (mail-content-type-get
735 (mm-handle-disposition handle) 'filename)
736 (mail-content-type-get
737 (mm-handle-type handle) 'name)))
738 (mime-info (mailcap-mime-info
739 (mm-handle-media-type handle) t))
740 (needsterm (or (assoc "needsterm" mime-info)
741 (assoc "needsterminal" mime-info)))
742 (copiousoutput (assoc "copiousoutput" mime-info))
744 ;; We create a private sub-directory where we store our files.
745 (set-file-modes dir 448)
747 (setq file (expand-file-name
748 (gnus-map-function mm-file-name-rewrite-functions
749 (file-name-nondirectory filename))
751 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
752 (let ((coding-system-for-write mm-binary-coding-system))
753 (write-region (point-min) (point-max) file nil 'nomesg))
754 (message "Viewing with %s" method)
757 (let ((command (mm-mailcap-command
758 method file (mm-handle-type handle))))
761 (start-process "*display*" nil
762 mm-external-terminal-program
764 shell-command-switch command)
772 shell-command-switch command)))
775 (set-process-sentinel
776 (get-buffer-process buffer)
777 `(lambda (process state)
778 (if (eq 'exit (process-status process))
779 (gnus-configure-windows
780 ',gnus-current-window-configuration))))
781 (gnus-configure-windows 'display-term))
782 (mm-handle-set-external-undisplayer handle (cons file buffer)))
783 (message "Displaying %s..." command))
786 (with-current-buffer outbuf
792 (call-process shell-file-name nil
794 (generate-new-buffer " *mm*"))
798 method file (mm-handle-type handle)))
799 (if (buffer-live-p buffer)
804 (ignore-errors (delete-file file))
805 (ignore-errors (delete-directory
806 (file-name-directory file)))
807 (ignore-errors (kill-buffer buffer))))))
810 (let ((command (mm-mailcap-command
811 method file (mm-handle-type handle))))
813 (start-process "*display*"
815 (generate-new-buffer " *mm*"))
817 shell-command-switch command)
818 (mm-handle-set-external-undisplayer
819 handle (cons file buffer)))
820 (message "Displaying %s..." command))
823 (defun mm-mailcap-command (method file type-list)
824 (let ((ctl (cdr type-list))
828 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
830 (push (substring method beg (match-beginning 0)) out)
831 (setq beg (match-end 0)
832 total (match-string 0 method)
833 sub (match-string 1 method))
835 ((string= total "%%")
837 ((or (string= total "%s")
838 ;; We do our own quoting.
839 (string= total "'%s'")
840 (string= total "\"%s\""))
841 (setq uses-stdin nil)
842 (push (shell-quote-argument
843 (gnus-map-function mm-path-name-rewrite-functions file)) out))
844 ((string= total "%t")
845 (push (shell-quote-argument (car type-list)) out))
847 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
848 (push (substring method beg (length method)) out)
851 (push (shell-quote-argument
852 (gnus-map-function mm-path-name-rewrite-functions file))
854 (mapconcat 'identity (nreverse out) "")))
856 (defun mm-remove-parts (handles)
857 "Remove the displayed MIME parts represented by HANDLES."
858 (if (and (listp handles)
859 (bufferp (car handles)))
860 (mm-remove-part handles)
862 (while (setq handle (pop handles))
865 (when (buffer-live-p (get-text-property 0 'buffer handle))
866 (kill-buffer (get-text-property 0 'buffer handle))))
868 (stringp (car handle)))
869 (mm-remove-parts (cdr handle)))
871 (mm-remove-part handle)))))))
873 (defun mm-destroy-parts (handles)
874 "Remove the displayed MIME parts represented by HANDLES."
875 (if (and (listp handles)
876 (bufferp (car handles)))
877 (mm-destroy-part handles)
879 (while (setq handle (pop handles))
882 (when (buffer-live-p (get-text-property 0 'buffer handle))
883 (kill-buffer (get-text-property 0 'buffer handle))))
885 (stringp (car handle)))
886 (mm-destroy-parts handle))
888 (mm-destroy-part handle)))))))
890 (defun mm-remove-part (handle)
891 "Remove the displayed MIME part represented by HANDLE."
893 (let ((object (mm-handle-undisplayer handle)))
896 ;; Internally displayed part.
897 ((mm-annotationp object)
898 (delete-annotation object))
899 ((or (functionp object)
901 (eq (car object) 'lambda)))
903 ;; Externally displayed part.
906 (while (get-buffer-process (cdr object))
907 (interrupt-process (get-buffer-process (cdr object)))
908 (message "Waiting for external displayer to die...")
912 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
913 (message "Waiting for external displayer to die...done")
914 (ignore-errors (delete-file (car object)))
915 (ignore-errors (delete-directory (file-name-directory
918 (when (buffer-live-p object)
919 (kill-buffer object)))))
920 (mm-handle-set-undisplayer handle nil))))
922 (defun mm-display-inline (handle)
923 (let* ((type (mm-handle-media-type handle))
924 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
925 (funcall function handle)
926 (goto-char (point-min))))
928 (defun mm-assoc-string-match (alist type)
930 (when (string-match (car elem) type)
933 (defun mm-automatic-display-p (handle)
934 "Say whether the user wants HANDLE to be displayed automatically."
935 (let ((methods mm-automatic-display)
936 (type (mm-handle-media-type handle))
938 (while (setq method (pop methods))
939 (when (and (not (mm-inline-override-p handle))
940 (string-match method type))
945 (defun mm-inlinable-p (handle)
946 "Say whether HANDLE can be displayed inline."
947 (let ((alist mm-inline-media-tests)
948 (type (mm-handle-media-type handle))
951 (when (string-match (caar alist) type)
952 (setq test (caddar alist)
954 (setq test (funcall test handle)))
958 (defun mm-inlined-p (handle)
959 "Say whether the user wants HANDLE to be displayed inline."
960 (let ((methods mm-inlined-types)
961 (type (mm-handle-media-type handle))
963 (while (setq method (pop methods))
964 (when (and (not (mm-inline-override-p handle))
965 (string-match method type))
970 (defun mm-attachment-override-p (handle)
971 "Say whether HANDLE should have attachment behavior overridden."
972 (let ((types mm-attachment-override-types)
973 (type (mm-handle-media-type handle))
976 (while (setq ty (pop types))
977 (when (and (string-match ty type)
978 (mm-inlinable-p handle))
979 (throw 'found t))))))
981 (defun mm-inline-override-p (handle)
982 "Say whether HANDLE should have inline behavior overridden."
983 (let ((types mm-inline-override-types)
984 (type (mm-handle-media-type handle))
987 (while (setq ty (pop types))
988 (when (string-match ty type)
989 (throw 'found t))))))
991 (defun mm-automatic-external-display-p (type)
992 "Return the user-defined method for TYPE."
993 (let ((methods mm-automatic-external-display)
995 (while (setq method (pop methods))
996 (when (string-match method type)
1001 (defun mm-destroy-part (handle)
1002 "Destroy the data structures connected to HANDLE."
1003 (when (listp handle)
1004 (mm-remove-part handle)
1005 (when (buffer-live-p (mm-handle-buffer handle))
1006 (kill-buffer (mm-handle-buffer handle)))))
1008 (defun mm-handle-displayed-p (handle)
1009 "Say whether HANDLE is displayed or not."
1010 (mm-handle-undisplayer handle))
1013 ;;; Functions for outputting parts
1016 (defun mm-get-part (handle)
1017 "Return the contents of HANDLE as a string."
1018 (mm-with-unibyte-buffer
1019 (insert (with-current-buffer (mm-handle-buffer handle)
1020 (mm-with-unibyte-current-buffer
1022 (mm-decode-content-transfer-encoding
1023 (mm-handle-encoding handle)
1024 (mm-handle-media-type handle))
1027 (defun mm-insert-part (handle)
1028 "Insert the contents of HANDLE in the current buffer."
1029 (let ((cur (current-buffer)))
1031 (if (member (mm-handle-media-supertype handle) '("text" "message"))
1033 (insert-buffer-substring (mm-handle-buffer handle))
1035 (mm-decode-content-transfer-encoding
1036 (mm-handle-encoding handle)
1037 (mm-handle-media-type handle))
1038 (let ((temp (current-buffer)))
1040 (insert-buffer-substring temp))))
1041 (mm-with-unibyte-buffer
1042 (insert-buffer-substring (mm-handle-buffer handle))
1044 (mm-decode-content-transfer-encoding
1045 (mm-handle-encoding handle)
1046 (mm-handle-media-type handle))
1047 (let ((temp (current-buffer)))
1049 (insert-buffer-substring temp))))))))
1051 (defun mm-file-name-delete-whitespace (file-name)
1052 "Remove all whitespace characters from FILE-NAME."
1053 (while (string-match "\\s-+" file-name)
1054 (setq file-name (replace-match "" t t file-name)))
1057 (defun mm-file-name-trim-whitespace (file-name)
1058 "Remove leading and trailing whitespace characters from FILE-NAME."
1059 (when (string-match "\\`\\s-+" file-name)
1060 (setq file-name (substring file-name (match-end 0))))
1061 (when (string-match "\\s-+\\'" file-name)
1062 (setq file-name (substring file-name 0 (match-beginning 0))))
1065 (defun mm-file-name-collapse-whitespace (file-name)
1066 "Collapse multiple whitespace characters in FILE-NAME."
1067 (while (string-match "\\s-\\s-+" file-name)
1068 (setq file-name (replace-match " " t t file-name)))
1071 (defun mm-file-name-replace-whitespace (file-name)
1072 "Replace whitespace characters in FILE-NAME with underscores.
1073 Set the option `mm-file-name-replace-whitespace' to any other
1074 string if you do not like underscores."
1075 (let ((s (or mm-file-name-replace-whitespace "_")))
1076 (while (string-match "\\s-" file-name)
1077 (setq file-name (replace-match s t t file-name))))
1080 (defun mm-file-name-delete-control (filename)
1081 "Delete control characters from FILENAME."
1082 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1084 (defun mm-file-name-delete-gotchas (filename)
1085 "Delete shell gotchas from FILENAME."
1086 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1087 (gnus-replace-in-string filename "^[.-]+" ""))
1089 (defun mm-save-part (handle)
1090 "Write HANDLE to a file."
1091 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1092 (filename (mail-content-type-get
1093 (mm-handle-disposition handle) 'filename))
1096 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1097 (file-name-nondirectory filename))))
1100 (read-file-name "Save MIME part to: "
1101 (or mm-default-directory default-directory)
1102 nil nil (or filename name ""))))
1103 (setq mm-default-directory (file-name-directory file))
1104 (and (or (not (file-exists-p file))
1105 (yes-or-no-p (format "File %s already exists; overwrite? "
1108 (mm-save-part-to-file handle file)
1111 (defun mm-save-part-to-file (handle file)
1112 (mm-with-unibyte-buffer
1113 (mm-insert-part handle)
1114 (let ((coding-system-for-write 'binary)
1115 (current-file-modes (default-file-modes))
1116 ;; Don't re-compress .gz & al. Arguably we should make
1117 ;; `file-name-handler-alist' nil, but that would chop
1118 ;; ange-ftp, which is reasonable to use here.
1119 (inhibit-file-name-operation 'write-region)
1120 (inhibit-file-name-handlers
1121 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1122 (set-default-file-modes mm-attachment-file-modes)
1124 (write-region (point-min) (point-max) file)
1125 (set-default-file-modes current-file-modes)))))
1127 (defun mm-pipe-part (handle)
1128 "Pipe HANDLE to a process."
1129 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1131 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1132 (mm-with-unibyte-buffer
1133 (mm-insert-part handle)
1134 (let ((coding-system-for-write 'binary))
1135 (shell-command-on-region (point-min) (point-max) command nil)))))
1137 (defun mm-interactively-view-part (handle)
1138 "Display HANDLE using METHOD."
1139 (let* ((type (mm-handle-media-type handle))
1141 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1142 (mailcap-mime-info type 'all)))
1143 (method (let ((minibuffer-local-completion-map
1144 mm-viewer-completion-map))
1145 (completing-read "Viewer: " methods))))
1146 (when (string= method "")
1147 (error "No method given"))
1148 (if (string-match "^[^% \t]+$" method)
1149 (setq method (concat method " %s")))
1150 (mm-display-external handle method)))
1152 (defun mm-preferred-alternative (handles &optional preferred)
1153 "Say which of HANDLES are preferred."
1154 (let ((prec (if preferred (list preferred)
1155 (mm-preferred-alternative-precedence handles)))
1156 p h result type handle)
1157 (while (setq p (pop prec))
1160 (setq handle (car h))
1161 (setq type (mm-handle-media-type handle))
1162 (when (and (equal p type)
1163 (mm-automatic-display-p handle)
1164 (or (stringp (car handle))
1165 (not (mm-handle-disposition handle))
1166 (equal (car (mm-handle-disposition handle))
1174 (defun mm-preferred-alternative-precedence (handles)
1175 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1176 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1178 (dolist (disc (reverse mm-discouraged-alternatives))
1179 (dolist (elem (copy-sequence seq))
1180 (when (string-match disc elem)
1181 (setq seq (nconc (delete elem seq) (list elem))))))
1184 (defun mm-get-content-id (id)
1185 "Return the handle(s) referred to by ID."
1186 (cdr (assoc id mm-content-id-alist)))
1188 (defconst mm-image-type-regexps
1189 '(("/\\*.*XPM.\\*/" . xpm)
1193 ("\211PNG\r\n" . png)
1195 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1196 ("%!PS" . postscript))
1197 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1198 When the first bytes of an image file match REGEXP, it is assumed to
1199 be of image type IMAGE-TYPE.")
1201 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1202 (defun mm-image-type-from-buffer ()
1203 "Determine the image type from data in the current buffer.
1204 Value is a symbol specifying the image type or nil if type cannot
1206 (let ((types mm-image-type-regexps)
1208 (goto-char (point-min))
1209 (while (and types (null type))
1210 (let ((regexp (car (car types)))
1211 (image-type (cdr (car types))))
1212 (when (looking-at regexp)
1213 (setq type image-type))
1214 (setq types (cdr types))))
1217 (defun mm-get-image (handle)
1218 "Return an image instance based on HANDLE."
1219 (let ((type (mm-handle-media-subtype handle))
1221 ;; Allow some common translations.
1224 ((equal type "x-pixmap")
1226 ((equal type "x-xbitmap")
1228 ((equal type "x-portable-bitmap")
1231 (or (mm-handle-cache handle)
1232 (mm-with-unibyte-buffer
1233 (mm-insert-part handle)
1237 ;; Avoid testing `make-glyph' since W3 may define
1238 ;; a bogus version of it.
1239 (if (fboundp 'create-image)
1240 (create-image (buffer-string)
1241 (or (mm-image-type-from-buffer)
1244 (mm-create-image-xemacs type))))
1245 (mm-handle-set-cache handle spec))))))
1247 (defun mm-create-image-xemacs (type)
1250 ;; xbm images require special handling, since
1251 ;; the only way to create glyphs from these
1252 ;; (without a ton of work) is to write them
1253 ;; out to a file, and then create a file
1255 (let ((file (mm-make-temp-file
1256 (expand-file-name "emm.xbm"
1257 mm-tmp-directory))))
1260 (write-region (point-min) (point-max) file)
1261 (make-glyph (list (cons 'x file))))
1263 (delete-file file)))))
1267 (or (mm-image-type-from-buffer)
1269 :data (buffer-string))))))
1271 (defun mm-image-fit-p (handle)
1272 "Say whether the image in HANDLE will fit the current window."
1273 (let ((image (mm-get-image handle)))
1274 (if (fboundp 'glyph-width)
1275 ;; XEmacs' glyphs can actually tell us about their width, so
1276 ;; lets be nice and smart about them.
1277 (or mm-inline-large-images
1278 (and (< (glyph-width image) (window-pixel-width))
1279 (< (glyph-height image) (window-pixel-height))))
1280 (let* ((size (image-size image))
1283 (or mm-inline-large-images
1284 (and (< h (1- (window-height))) ; Don't include mode line.
1285 (< w (window-width))))))))
1287 (defun mm-valid-image-format-p (format)
1288 "Say whether FORMAT can be displayed natively by Emacs."
1291 ((fboundp 'valid-image-instantiator-format-p)
1292 (valid-image-instantiator-format-p format))
1294 ((fboundp 'image-type-available-p)
1295 (and (display-graphic-p)
1296 (image-type-available-p format)))
1297 ;; Nobody else can do images yet.
1301 (defun mm-valid-and-fit-image-p (format handle)
1302 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1303 (and (mm-valid-image-format-p format)
1304 (mm-image-fit-p handle)))
1306 (defun mm-find-part-by-type (handles type &optional notp recursive)
1307 "Search in HANDLES for part with TYPE.
1308 If NOTP, returns first non-matching part.
1309 If RECURSIVE, search recursively."
1312 (if (and recursive (stringp (caar handles)))
1313 (if (setq handle (mm-find-part-by-type (cdar handles) type
1317 (not (equal (mm-handle-media-type (car handles)) type))
1318 (equal (mm-handle-media-type (car handles)) type))
1319 (setq handle (car handles)
1321 (setq handles (cdr handles)))
1324 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1325 (goto-char (point-min))
1326 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1328 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1330 (end (save-excursion
1331 (goto-char (point-max))
1332 (if (re-search-backward close-delimiter nil t)
1336 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1337 (while (and (not result)
1338 (re-search-forward boundary end t))
1339 (goto-char (match-beginning 0))
1343 (narrow-to-region start (1- (point)))
1344 (when (let ((ctl (ignore-errors
1345 (mail-header-parse-content-type
1346 (mail-fetch-field "content-type")))))
1348 (not (equal (car ctl) type))
1349 (equal (car ctl) type)))
1350 (setq result (buffer-string))))))
1352 (setq start (point)))
1353 (when (and (not result) start)
1356 (narrow-to-region start end)
1357 (when (let ((ctl (ignore-errors
1358 (mail-header-parse-content-type
1359 (mail-fetch-field "content-type")))))
1361 (not (equal (car ctl) type))
1362 (equal (car ctl) type)))
1363 (setq result (buffer-string))))))
1366 (defvar mm-security-handle nil)
1368 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1369 ;; HANDLE could be a CTL.
1371 (put-text-property 0 (length (car handle)) parameter value
1374 (defun mm-possibly-verify-or-decrypt (parts ctl)
1375 (let ((type (car ctl))
1376 (subtype (cadr (split-string (car ctl) "/")))
1377 (mm-security-handle ctl) ;; (car CTL) is the type.
1378 protocol func functest)
1380 ((or (equal type "application/x-pkcs7-mime")
1381 (equal type "application/pkcs7-mime"))
1384 ((eq mm-decrypt-option 'never) nil)
1385 ((eq mm-decrypt-option 'always) t)
1386 ((eq mm-decrypt-option 'known) t)
1388 (format "Decrypt (S/MIME) part? "))))
1389 (mm-view-pkcs7 parts))
1390 (setq parts (mm-dissect-buffer t)))))
1391 ((equal subtype "signed")
1392 (unless (and (setq protocol
1393 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1394 (not (equal protocol "multipart/mixed")))
1395 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1396 (let ((protocols mm-verify-function-alist))
1398 (if (and (or (not (setq functest (nth 3 (car protocols))))
1399 (funcall functest parts ctl))
1400 (mm-find-part-by-type parts (caar protocols) nil t))
1401 (setq protocol (caar protocols)
1403 (setq protocols (cdr protocols))))))
1404 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1406 ((eq mm-verify-option 'never) nil)
1407 ((eq mm-verify-option 'always) t)
1408 ((eq mm-verify-option 'known)
1410 (or (not (setq functest
1411 (nth 3 (assoc protocol
1412 mm-verify-function-alist))))
1413 (funcall functest parts ctl))))
1416 (format "Verify signed (%s) part? "
1417 (or (nth 2 (assoc protocol mm-verify-function-alist))
1418 (format "protocol=%s" protocol))))))
1421 (funcall func parts ctl)
1422 (mm-set-handle-multipart-parameter
1423 mm-security-handle 'gnus-details
1424 (format "Unknown sign protocol (%s)" protocol))))))
1425 ((equal subtype "encrypted")
1426 (unless (setq protocol
1427 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1428 ;; The message is broken.
1429 (let ((parts parts))
1431 (if (assoc (mm-handle-media-type (car parts))
1432 mm-decrypt-function-alist)
1433 (setq protocol (mm-handle-media-type (car parts))
1435 (setq parts (cdr parts))))))
1436 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1438 ((eq mm-decrypt-option 'never) nil)
1439 ((eq mm-decrypt-option 'always) t)
1440 ((eq mm-decrypt-option 'known)
1442 (or (not (setq functest
1443 (nth 3 (assoc protocol
1444 mm-decrypt-function-alist))))
1445 (funcall functest parts ctl))))
1448 (format "Decrypt (%s) part? "
1449 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1450 (format "protocol=%s" protocol))))))
1453 (setq parts (funcall func parts ctl))
1454 (mm-set-handle-multipart-parameter
1455 mm-security-handle 'gnus-details
1456 (format "Unknown encrypt protocol (%s)" protocol))))))
1460 (defun mm-multiple-handles (handles)
1461 (and (listp handles)
1462 (> (length handles) 1)
1463 (or (listp (car handles))
1464 (stringp (car handles)))))
1466 (defun mm-complicated-handles (handles)
1467 (and (listp (car handles))
1468 (> (length handles) 1)))
1470 (defun mm-merge-handles (handles1 handles2)
1472 (if (listp (car handles1))
1475 (if (listp (car handles2))
1479 (defun mm-readable-p (handle)
1480 "Say whether the content of HANDLE is readable."
1481 (and (< (with-current-buffer (mm-handle-buffer handle)
1482 (buffer-size)) 10000)
1483 (mm-with-unibyte-buffer
1484 (mm-insert-part handle)
1485 (and (eq (mm-body-7-or-8) '7bit)
1486 (not (mm-long-lines-p 76))))))
1488 (provide 'mm-decode)
1490 ;;; mm-decode.el ends here