1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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."
153 :type '(choice (regexp :tag "Regexp")
154 (const :tag "All URLs are safe" nil))
155 :group 'mime-display)
157 (defcustom mm-inline-text-html-with-w3m-keymap t
158 "If non-nil, use emacs-w3m command keys in the article buffer."
161 :group 'mime-display)
163 (defcustom mm-enable-external t
164 "Indicate whether external MIME handlers should be used.
166 If t, all defined external MIME handlers are used. If nil, files are saved by
167 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
168 before the external MIME handler is invoked."
170 :type '(choice (const :tag "Always" t)
171 (const :tag "Never" nil)
172 (const :tag "Ask" ask))
173 :group 'mime-display)
175 (defcustom mm-inline-media-tests
179 (mm-valid-and-fit-image-p 'jpeg handle)))
183 (mm-valid-and-fit-image-p 'png handle)))
187 (mm-valid-and-fit-image-p 'gif handle)))
191 (mm-valid-and-fit-image-p 'tiff handle)) )
195 (mm-valid-and-fit-image-p 'xbm handle)))
199 (mm-valid-and-fit-image-p 'xbm handle)))
203 (mm-valid-and-fit-image-p 'xpm handle)))
207 (mm-valid-and-fit-image-p 'xpm handle)))
211 (mm-valid-and-fit-image-p 'bmp handle)))
212 ("image/x-portable-bitmap"
215 (mm-valid-and-fit-image-p 'pbm handle)))
216 ("text/plain" mm-inline-text identity)
217 ("text/enriched" mm-inline-text identity)
218 ("text/richtext" mm-inline-text identity)
219 ("text/x-patch" mm-display-patch-inline
221 (locate-library "diff-mode")))
222 ("application/emacs-lisp" mm-display-elisp-inline identity)
223 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
224 ("text/dns" mm-display-dns-inline identity)
228 (or mm-inline-text-html-renderer
229 mm-text-html-renderer)))
233 (or (featurep 'vcard)
234 (locate-library "vcard"))))
235 ("message/delivery-status" mm-inline-text identity)
236 ("message/rfc822" mm-inline-message identity)
237 ("message/partial" mm-inline-partial identity)
238 ("message/external-body" mm-inline-external-body identity)
239 ("text/.*" mm-inline-text identity)
240 ("audio/wav" mm-inline-audio
242 (and (or (featurep 'nas-sound) (featurep 'native-sound))
243 (device-sound-enabled-p))))
247 (and (or (featurep 'nas-sound) (featurep 'native-sound))
248 (device-sound-enabled-p))))
249 ("application/pgp-signature" ignore identity)
250 ("application/x-pkcs7-signature" ignore identity)
251 ("application/pkcs7-signature" ignore identity)
252 ("application/x-pkcs7-mime" ignore identity)
253 ("application/pkcs7-mime" ignore identity)
254 ("multipart/alternative" ignore identity)
255 ("multipart/mixed" ignore identity)
256 ("multipart/related" ignore identity)
257 ;; Disable audio and image
258 ("audio/.*" ignore ignore)
259 ("image/.*" ignore ignore)
260 ;; Default to displaying as text
261 (".*" mm-inline-text mm-readable-p))
262 "Alist of media types/tests saying whether types can be displayed inline."
263 :type '(repeat (list (regexp :tag "MIME type")
264 (function :tag "Display function")
265 (function :tag "Display test")))
266 :group 'mime-display)
268 (defcustom mm-inlined-types
269 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
270 "message/partial" "message/external-body" "application/emacs-lisp"
271 "application/x-emacs-lisp"
272 "application/pgp-signature" "application/x-pkcs7-signature"
273 "application/pkcs7-signature" "application/x-pkcs7-mime"
274 "application/pkcs7-mime")
275 "List of media types that are to be displayed inline.
276 See also `mm-inline-media-tests', which says how to display a media
278 :type '(repeat string)
279 :group 'mime-display)
281 (defcustom mm-keep-viewer-alive-types
282 '("application/postscript" "application/msword" "application/vnd.ms-excel"
283 "application/pdf" "application/x-dvi")
284 "List of media types for which the external viewer will not be killed
285 when selecting a different article."
287 :type '(repeat string)
288 :group 'mime-display)
290 (defcustom mm-automatic-display
291 '("text/plain" "text/enriched" "text/richtext" "text/html"
292 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
293 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
294 "application/emacs-lisp" "application/x-emacs-lisp"
295 "application/x-pkcs7-signature"
296 "application/pkcs7-signature" "application/x-pkcs7-mime"
297 "application/pkcs7-mime")
298 "A list of MIME types to be displayed automatically."
299 :type '(repeat string)
300 :group 'mime-display)
302 (defcustom mm-attachment-override-types '("text/x-vcard"
303 "application/pkcs7-mime"
304 "application/x-pkcs7-mime"
305 "application/pkcs7-signature"
306 "application/x-pkcs7-signature")
307 "Types to have \"attachment\" ignored if they can be displayed inline."
308 :type '(repeat string)
309 :group 'mime-display)
311 (defcustom mm-inline-override-types nil
312 "Types to be treated as attachments even if they can be displayed inline."
313 :type '(repeat string)
314 :group 'mime-display)
316 (defcustom mm-automatic-external-display nil
317 "List of MIME type regexps that will be displayed externally automatically."
318 :type '(repeat string)
319 :group 'mime-display)
321 (defcustom mm-discouraged-alternatives nil
322 "List of MIME types that are discouraged when viewing multipart/alternative.
323 Viewing agents are supposed to view the last possible part of a message,
324 as that is supposed to be the richest. However, users may prefer other
325 types instead, and this list says what types are most unwanted. If,
326 for instance, text/html parts are very unwanted, and text/richtext are
327 somewhat unwanted, then the value of this variable should be set
330 (\"text/html\" \"text/richtext\")"
331 :type '(repeat string)
332 :group 'mime-display)
334 (defcustom mm-tmp-directory
335 (if (fboundp 'temp-directory)
337 (if (boundp 'temporary-file-directory)
338 temporary-file-directory
340 "Where mm will store its temporary files."
342 :group 'mime-display)
344 (defcustom mm-inline-large-images nil
345 "If non-nil, then all images fit in the buffer."
347 :group 'mime-display)
349 (defvar mm-file-name-rewrite-functions
350 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
351 "*List of functions used for rewriting file names of MIME parts.
352 Each function takes a file name as input and returns a file name.
354 Ready-made functions include
355 `mm-file-name-delete-control'
356 `mm-file-name-delete-gotchas'
357 `mm-file-name-delete-whitespace',
358 `mm-file-name-trim-whitespace',
359 `mm-file-name-collapse-whitespace',
360 `mm-file-name-replace-whitespace',
361 `capitalize', `downcase', `upcase', and
364 (defvar mm-path-name-rewrite-functions nil
365 "*List of functions for rewriting the full file names of MIME parts.
366 This is used when viewing parts externally, and is meant for
367 transforming the absolute name so that non-compliant programs can find
368 the file where it's saved.
370 Each function takes a file name as input and returns a file name.")
372 (defvar mm-file-name-replace-whitespace nil
373 "String used for replacing whitespace characters; default is `\"_\"'.")
375 (defcustom mm-default-directory nil
376 "The default directory where mm will save files.
377 If not set, `default-directory' will be used."
378 :type '(choice directory (const :tag "Default" nil))
379 :group 'mime-display)
381 (defcustom mm-attachment-file-modes 384
382 "Set the mode bits of saved attachments to this integer."
385 :group 'mime-display)
387 (defcustom mm-external-terminal-program "xterm"
388 "The program to start an external terminal."
391 :group 'mime-display)
393 ;;; Internal variables.
395 (defvar mm-last-shell-command "")
396 (defvar mm-content-id-alist nil)
397 (defvar mm-postponed-undisplay-list nil)
399 ;; According to RFC2046, in particular, in a digest, the default
400 ;; Content-Type value for a body part is changed from "text/plain" to
402 (defvar mm-dissect-default-type "text/plain")
404 (autoload 'mml2015-verify "mml2015")
405 (autoload 'mml2015-verify-test "mml2015")
406 (autoload 'mml-smime-verify "mml-smime")
407 (autoload 'mml-smime-verify-test "mml-smime")
409 (defvar mm-verify-function-alist
410 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
411 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
412 mm-uu-pgp-signed-test)
413 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
414 mml-smime-verify-test)
415 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
416 mml-smime-verify-test)))
418 (defcustom mm-verify-option 'never
419 "Option of verifying signed parts.
420 `never', not verify; `always', always verify;
421 `known', only verify known protocols. Otherwise, ask user."
423 :type '(choice (item always)
425 (item :tag "only known protocols" known)
426 (item :tag "ask" nil))
427 :group 'mime-security)
429 (autoload 'mml2015-decrypt "mml2015")
430 (autoload 'mml2015-decrypt-test "mml2015")
432 (defvar mm-decrypt-function-alist
433 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
434 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
435 mm-uu-pgp-encrypted-test)))
437 (defcustom mm-decrypt-option nil
438 "Option of decrypting encrypted parts.
439 `never', not decrypt; `always', always decrypt;
440 `known', only decrypt known protocols. Otherwise, ask user."
442 :type '(choice (item always)
444 (item :tag "only known protocols" known)
445 (item :tag "ask" nil))
446 :group 'mime-security)
448 (defvar mm-viewer-completion-map
449 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
450 (set-keymap-parent map minibuffer-local-completion-map)
452 "Keymap for input viewer with completion.")
454 ;; Should we bind other key to minibuffer-complete-word?
455 (define-key mm-viewer-completion-map " " 'self-insert-command)
457 (defvar mm-viewer-completion-map
458 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
459 (set-keymap-parent map minibuffer-local-completion-map)
461 "Keymap for input viewer with completion.")
463 ;; Should we bind other key to minibuffer-complete-word?
464 (define-key mm-viewer-completion-map " " 'self-insert-command)
468 (defun mm-alist-to-plist (alist)
469 "Convert association list ALIST into the equivalent property-list form.
470 The plist is returned. This converts from
472 \((a . 1) (b . 2) (c . 3))
478 The original alist is not modified. See also `destructive-alist-to-plist'."
481 (let ((el (car alist)))
482 (setq plist (cons (cdr el) (cons (car el) plist))))
483 (setq alist (cdr alist)))
486 (defun mm-keep-viewer-alive-p (handle)
487 "Say whether external viewer for HANDLE should stay alive."
488 (let ((types mm-keep-viewer-alive-types)
489 (type (mm-handle-media-type handle))
492 (while (setq ty (pop types))
493 (when (string-match ty type)
494 (throw 'found t))))))
496 (defun mm-handle-set-external-undisplayer (handle function)
497 "Set the undisplayer for HANDLE to FUNCTION.
498 Postpone undisplaying of viewers for types in
499 `mm-keep-viewer-alive-types'."
500 (if (mm-keep-viewer-alive-p handle)
501 (let ((new-handle (copy-sequence handle)))
502 (mm-handle-set-undisplayer new-handle function)
503 (mm-handle-set-undisplayer handle nil)
504 (push new-handle mm-postponed-undisplay-list))
505 (mm-handle-set-undisplayer handle function)))
507 (defun mm-destroy-postponed-undisplay-list ()
508 (when mm-postponed-undisplay-list
509 (message "Destroying external MIME viewers")
510 (mm-destroy-parts mm-postponed-undisplay-list)))
512 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
513 "Dissect the current buffer and return a list of MIME handles."
515 (let (ct ctl type subtype cte cd description id result)
517 (mail-narrow-to-head)
518 (when (or no-strict-mime
520 (mail-fetch-field "mime-version"))
521 (setq ct (mail-fetch-field "content-type")
522 ctl (ignore-errors (mail-header-parse-content-type ct))
523 cte (mail-fetch-field "content-transfer-encoding")
524 cd (mail-fetch-field "content-disposition")
525 description (mail-fetch-field "content-description")
526 id (mail-fetch-field "content-id"))
528 (setq from (mail-fetch-field "from")))
529 ;; FIXME: In some circumstances, this code is running within
530 ;; an unibyte macro. mail-extract-address-components
531 ;; creates unibyte buffers. This `if', though not a perfect
532 ;; solution, avoids most of them.
534 (setq from (cadr (mail-extract-address-components from))))))
536 (not (string-match "/" (car ctl))))
537 (mm-dissect-singlepart
538 (list mm-dissect-default-type)
539 (and cte (intern (downcase (mail-header-strip cte))))
541 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
543 (setq type (split-string (car ctl) "/"))
544 (setq subtype (cadr type)
549 ((equal type "multipart")
550 (let ((mm-dissect-default-type (if (equal subtype "digest")
553 (start (cdr (assq 'start (cdr ctl)))))
554 (add-text-properties 0 (length (car ctl))
555 (mm-alist-to-plist (cdr ctl)) (car ctl))
557 ;; what really needs to be done here is a way to link a
558 ;; MIME handle back to it's parent MIME handle (in a multilevel
559 ;; MIME article). That would probably require changing
560 ;; the mm-handle API so we simply store the multipart buffert
561 ;; name as a text property of the "multipart/whatever" string.
562 (add-text-properties 0 (length (car ctl))
563 (list 'buffer (mm-copy-to-buffer)
567 (cons (car ctl) (mm-dissect-multipart ctl from))))
569 (mm-possibly-verify-or-decrypt
570 (mm-dissect-singlepart
572 (and cte (intern (downcase (mail-header-strip cte))))
574 (and cd (ignore-errors
575 (mail-header-parse-content-disposition cd)))
579 (when (string-match " *<\\(.*\\)> *" id)
580 (setq id (match-string 1 id)))
581 (push (cons id result) mm-content-id-alist))
584 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
586 (if (equal "text/plain" (car ctl))
590 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
592 (defun mm-dissect-multipart (ctl from)
593 (goto-char (point-min))
594 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
595 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
598 (goto-char (point-max))
599 (if (re-search-backward close-delimiter nil t)
602 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
603 (while (and (< (point) end) (re-search-forward boundary end t))
604 (goto-char (match-beginning 0))
608 (narrow-to-region start (point))
609 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
611 (or (looking-at boundary)
613 (setq start (point)))
614 (when (and start (< start end))
617 (narrow-to-region start end)
618 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
619 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
621 (defun mm-copy-to-buffer ()
622 "Copy the contents of the current buffer to a fresh buffer."
624 (let ((obuf (current-buffer))
626 (goto-char (point-min))
627 (search-forward-regexp "^\n" nil t)
630 ;; Preserve the data's unibyteness (for url-insert-file-contents).
631 (let ((default-enable-multibyte-characters (mm-multibyte-p)))
632 (generate-new-buffer " *mm*")))
633 (insert-buffer-substring obuf beg)
636 (defun mm-display-parts (handle &optional no-default)
637 (if (stringp (car handle))
638 (mapcar 'mm-display-parts (cdr handle))
639 (if (bufferp (car handle))
641 (narrow-to-region (point) (point))
642 (mm-display-part handle)
643 (goto-char (point-max)))
644 (mapcar 'mm-display-parts handle))))
646 (defun mm-display-part (handle &optional no-default)
647 "Display the MIME part represented by HANDLE.
648 Returns nil if the part is removed; inline if displayed inline;
649 external if displayed external."
651 (mailcap-parse-mailcaps)
652 (if (mm-handle-displayed-p handle)
653 (mm-remove-part handle)
654 (let* ((type (mm-handle-media-type handle))
655 (method (mailcap-mime-info type))
656 (filename (or (mail-content-type-get
657 (mm-handle-disposition handle) 'filename)
658 (mail-content-type-get
659 (mm-handle-type handle) 'name)
661 (external mm-enable-external))
662 (if (and (mm-inlinable-p handle)
663 (mm-inlined-p handle))
666 (mm-display-inline handle)
670 (if (and (not method)
671 (equal "text" (car (split-string type))))
674 (mm-insert-inline handle (mm-get-part handle))
676 (if (and method ;; If nil, we always use "save".
677 (stringp method) ;; 'mailcap-save-binary-file
678 (or (eq mm-enable-external t)
679 (and (eq mm-enable-external 'ask)
682 "Display part (" type
683 ") using external program"
684 ;; Can non-string method ever happen?
687 " \"" (format method filename) "\"")
694 handle (or method 'mailcap-save-binary-file))
696 handle 'mailcap-save-binary-file)))))))))
698 (defun mm-display-external (handle method)
699 "Display HANDLE using METHOD."
700 (let ((outbuf (current-buffer)))
701 (mm-with-unibyte-buffer
702 (if (functionp method)
703 (let ((cur (current-buffer)))
704 (if (eq method 'mailcap-save-binary-file)
706 (set-buffer (generate-new-buffer " *mm*"))
708 (mm-insert-part handle)
709 (let ((win (get-buffer-window cur t)))
711 (select-window win)))
712 (switch-to-buffer (generate-new-buffer " *mm*")))
713 (buffer-disable-undo)
714 (mm-set-buffer-file-coding-system mm-binary-coding-system)
715 (insert-buffer-substring cur)
716 (goto-char (point-min))
718 (message "Viewing with %s" method))
719 (let ((mm (current-buffer))
720 (non-viewer (assq 'non-viewer
722 (mm-handle-media-type handle) t))))
726 (mm-save-part handle))
727 (when (and (not non-viewer)
729 (mm-handle-set-undisplayer handle mm)))))
730 ;; The function is a string to be executed.
731 (mm-insert-part handle)
732 (let* ((dir (mm-make-temp-file
733 (expand-file-name "emm." mm-tmp-directory) 'dir))
735 (mail-content-type-get
736 (mm-handle-disposition handle) 'filename)
737 (mail-content-type-get
738 (mm-handle-type handle) 'name)))
739 (mime-info (mailcap-mime-info
740 (mm-handle-media-type handle) t))
741 (needsterm (or (assoc "needsterm" mime-info)
742 (assoc "needsterminal" mime-info)))
743 (copiousoutput (assoc "copiousoutput" mime-info))
745 ;; We create a private sub-directory where we store our files.
746 (set-file-modes dir 448)
748 (setq file (expand-file-name
749 (gnus-map-function mm-file-name-rewrite-functions
750 (file-name-nondirectory filename))
752 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
753 (let ((coding-system-for-write mm-binary-coding-system))
754 (write-region (point-min) (point-max) file nil 'nomesg))
755 (message "Viewing with %s" method)
758 (let ((command (mm-mailcap-command
759 method file (mm-handle-type handle))))
762 (start-process "*display*" nil
763 mm-external-terminal-program
765 shell-command-switch command)
773 shell-command-switch command)))
776 (set-process-sentinel
777 (get-buffer-process buffer)
778 `(lambda (process state)
779 (if (eq 'exit (process-status process))
780 (gnus-configure-windows
781 ',gnus-current-window-configuration))))
782 (gnus-configure-windows 'display-term))
783 (mm-handle-set-external-undisplayer handle (cons file buffer)))
784 (message "Displaying %s..." command))
787 (with-current-buffer outbuf
793 (call-process shell-file-name nil
795 (generate-new-buffer " *mm*"))
799 method file (mm-handle-type handle)))
800 (if (buffer-live-p buffer)
805 (ignore-errors (delete-file file))
806 (ignore-errors (delete-directory
807 (file-name-directory file)))
808 (ignore-errors (kill-buffer buffer))))))
811 (let ((command (mm-mailcap-command
812 method file (mm-handle-type handle))))
814 (start-process "*display*"
816 (generate-new-buffer " *mm*"))
818 shell-command-switch command)
819 (mm-handle-set-external-undisplayer
820 handle (cons file buffer)))
821 (message "Displaying %s..." command))
824 (defun mm-mailcap-command (method file type-list)
825 (let ((ctl (cdr type-list))
829 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
831 (push (substring method beg (match-beginning 0)) out)
832 (setq beg (match-end 0)
833 total (match-string 0 method)
834 sub (match-string 1 method))
836 ((string= total "%%")
838 ((or (string= total "%s")
839 ;; We do our own quoting.
840 (string= total "'%s'")
841 (string= total "\"%s\""))
842 (setq uses-stdin nil)
843 (push (shell-quote-argument
844 (gnus-map-function mm-path-name-rewrite-functions file)) out))
845 ((string= total "%t")
846 (push (shell-quote-argument (car type-list)) out))
848 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
849 (push (substring method beg (length method)) out)
852 (push (shell-quote-argument
853 (gnus-map-function mm-path-name-rewrite-functions file))
855 (mapconcat 'identity (nreverse out) "")))
857 (defun mm-remove-parts (handles)
858 "Remove the displayed MIME parts represented by HANDLES."
859 (if (and (listp handles)
860 (bufferp (car handles)))
861 (mm-remove-part handles)
863 (while (setq handle (pop handles))
866 (when (buffer-live-p (get-text-property 0 'buffer handle))
867 (kill-buffer (get-text-property 0 'buffer handle))))
869 (stringp (car handle)))
870 (mm-remove-parts (cdr handle)))
872 (mm-remove-part handle)))))))
874 (defun mm-destroy-parts (handles)
875 "Remove the displayed MIME parts represented by HANDLES."
876 (if (and (listp handles)
877 (bufferp (car handles)))
878 (mm-destroy-part handles)
880 (while (setq handle (pop handles))
883 (when (buffer-live-p (get-text-property 0 'buffer handle))
884 (kill-buffer (get-text-property 0 'buffer handle))))
886 (stringp (car handle)))
887 (mm-destroy-parts handle))
889 (mm-destroy-part handle)))))))
891 (defun mm-remove-part (handle)
892 "Remove the displayed MIME part represented by HANDLE."
894 (let ((object (mm-handle-undisplayer handle)))
897 ;; Internally displayed part.
898 ((mm-annotationp object)
899 (delete-annotation object))
900 ((or (functionp object)
902 (eq (car object) 'lambda)))
904 ;; Externally displayed part.
907 (while (get-buffer-process (cdr object))
908 (interrupt-process (get-buffer-process (cdr object)))
909 (message "Waiting for external displayer to die...")
913 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
914 (message "Waiting for external displayer to die...done")
915 (ignore-errors (delete-file (car object)))
916 (ignore-errors (delete-directory (file-name-directory
919 (when (buffer-live-p object)
920 (kill-buffer object)))))
921 (mm-handle-set-undisplayer handle nil))))
923 (defun mm-display-inline (handle)
924 (let* ((type (mm-handle-media-type handle))
925 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
926 (funcall function handle)
927 (goto-char (point-min))))
929 (defun mm-assoc-string-match (alist type)
931 (when (string-match (car elem) type)
934 (defun mm-automatic-display-p (handle)
935 "Say whether the user wants HANDLE to be displayed automatically."
936 (let ((methods mm-automatic-display)
937 (type (mm-handle-media-type handle))
939 (while (setq method (pop methods))
940 (when (and (not (mm-inline-override-p handle))
941 (string-match method type))
946 (defun mm-inlinable-p (handle)
947 "Say whether HANDLE can be displayed inline."
948 (let ((alist mm-inline-media-tests)
949 (type (mm-handle-media-type handle))
952 (when (string-match (caar alist) type)
953 (setq test (caddar alist)
955 (setq test (funcall test handle)))
959 (defun mm-inlined-p (handle)
960 "Say whether the user wants HANDLE to be displayed inline."
961 (let ((methods mm-inlined-types)
962 (type (mm-handle-media-type handle))
964 (while (setq method (pop methods))
965 (when (and (not (mm-inline-override-p handle))
966 (string-match method type))
971 (defun mm-attachment-override-p (handle)
972 "Say whether HANDLE should have attachment behavior overridden."
973 (let ((types mm-attachment-override-types)
974 (type (mm-handle-media-type handle))
977 (while (setq ty (pop types))
978 (when (and (string-match ty type)
979 (mm-inlinable-p handle))
980 (throw 'found t))))))
982 (defun mm-inline-override-p (handle)
983 "Say whether HANDLE should have inline behavior overridden."
984 (let ((types mm-inline-override-types)
985 (type (mm-handle-media-type handle))
988 (while (setq ty (pop types))
989 (when (string-match ty type)
990 (throw 'found t))))))
992 (defun mm-automatic-external-display-p (type)
993 "Return the user-defined method for TYPE."
994 (let ((methods mm-automatic-external-display)
996 (while (setq method (pop methods))
997 (when (string-match method type)
1002 (defun mm-destroy-part (handle)
1003 "Destroy the data structures connected to HANDLE."
1004 (when (listp handle)
1005 (mm-remove-part handle)
1006 (when (buffer-live-p (mm-handle-buffer handle))
1007 (kill-buffer (mm-handle-buffer handle)))))
1009 (defun mm-handle-displayed-p (handle)
1010 "Say whether HANDLE is displayed or not."
1011 (mm-handle-undisplayer handle))
1014 ;;; Functions for outputting parts
1017 (defun mm-get-part (handle)
1018 "Return the contents of HANDLE as a string."
1019 (mm-with-unibyte-buffer
1020 (insert (with-current-buffer (mm-handle-buffer handle)
1021 (mm-with-unibyte-current-buffer
1023 (mm-decode-content-transfer-encoding
1024 (mm-handle-encoding handle)
1025 (mm-handle-media-type handle))
1028 (defun mm-insert-part (handle)
1029 "Insert the contents of HANDLE in the current buffer."
1031 (insert (if (mm-multibyte-p)
1032 (mm-string-as-multibyte (mm-get-part handle))
1033 (mm-get-part handle)))))
1035 (defun mm-file-name-delete-whitespace (file-name)
1036 "Remove all whitespace characters from FILE-NAME."
1037 (while (string-match "\\s-+" file-name)
1038 (setq file-name (replace-match "" t t file-name)))
1041 (defun mm-file-name-trim-whitespace (file-name)
1042 "Remove leading and trailing whitespace characters from FILE-NAME."
1043 (when (string-match "\\`\\s-+" file-name)
1044 (setq file-name (substring file-name (match-end 0))))
1045 (when (string-match "\\s-+\\'" file-name)
1046 (setq file-name (substring file-name 0 (match-beginning 0))))
1049 (defun mm-file-name-collapse-whitespace (file-name)
1050 "Collapse multiple whitespace characters in FILE-NAME."
1051 (while (string-match "\\s-\\s-+" file-name)
1052 (setq file-name (replace-match " " t t file-name)))
1055 (defun mm-file-name-replace-whitespace (file-name)
1056 "Replace whitespace characters in FILE-NAME with underscores.
1057 Set the option `mm-file-name-replace-whitespace' to any other
1058 string if you do not like underscores."
1059 (let ((s (or mm-file-name-replace-whitespace "_")))
1060 (while (string-match "\\s-" file-name)
1061 (setq file-name (replace-match s t t file-name))))
1064 (defun mm-file-name-delete-control (filename)
1065 "Delete control characters from FILENAME."
1066 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1068 (defun mm-file-name-delete-gotchas (filename)
1069 "Delete shell gotchas from FILENAME."
1070 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1071 (gnus-replace-in-string filename "^[.-]+" ""))
1073 (defun mm-save-part (handle)
1074 "Write HANDLE to a file."
1075 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1076 (filename (mail-content-type-get
1077 (mm-handle-disposition handle) 'filename))
1080 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1081 (file-name-nondirectory filename))))
1084 (read-file-name "Save MIME part to: "
1085 (or mm-default-directory default-directory)
1086 nil nil (or filename name ""))))
1087 (setq mm-default-directory (file-name-directory file))
1088 (and (or (not (file-exists-p file))
1089 (yes-or-no-p (format "File %s already exists; overwrite? "
1092 (mm-save-part-to-file handle file)
1095 (defun mm-save-part-to-file (handle file)
1096 (mm-with-unibyte-buffer
1097 (mm-insert-part handle)
1098 (let ((coding-system-for-write 'binary)
1099 (current-file-modes (default-file-modes))
1100 ;; Don't re-compress .gz & al. Arguably we should make
1101 ;; `file-name-handler-alist' nil, but that would chop
1102 ;; ange-ftp, which is reasonable to use here.
1103 (inhibit-file-name-operation 'write-region)
1104 (inhibit-file-name-handlers
1105 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1106 (set-default-file-modes mm-attachment-file-modes)
1108 (write-region (point-min) (point-max) file)
1109 (set-default-file-modes current-file-modes)))))
1111 (defun mm-pipe-part (handle)
1112 "Pipe HANDLE to a process."
1113 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1115 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1116 (mm-with-unibyte-buffer
1117 (mm-insert-part handle)
1118 (let ((coding-system-for-write 'binary))
1119 (shell-command-on-region (point-min) (point-max) command nil)))))
1121 (defun mm-interactively-view-part (handle)
1122 "Display HANDLE using METHOD."
1123 (let* ((type (mm-handle-media-type handle))
1125 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1126 (mailcap-mime-info type 'all)))
1127 (method (let ((minibuffer-local-completion-map
1128 mm-viewer-completion-map))
1129 (completing-read "Viewer: " methods))))
1130 (when (string= method "")
1131 (error "No method given"))
1132 (if (string-match "^[^% \t]+$" method)
1133 (setq method (concat method " %s")))
1134 (mm-display-external handle method)))
1136 (defun mm-preferred-alternative (handles &optional preferred)
1137 "Say which of HANDLES are preferred."
1138 (let ((prec (if preferred (list preferred)
1139 (mm-preferred-alternative-precedence handles)))
1140 p h result type handle)
1141 (while (setq p (pop prec))
1144 (setq handle (car h))
1145 (setq type (mm-handle-media-type handle))
1146 (when (and (equal p type)
1147 (mm-automatic-display-p handle)
1148 (or (stringp (car handle))
1149 (not (mm-handle-disposition handle))
1150 (equal (car (mm-handle-disposition handle))
1158 (defun mm-preferred-alternative-precedence (handles)
1159 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1160 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1162 (dolist (disc (reverse mm-discouraged-alternatives))
1163 (dolist (elem (copy-sequence seq))
1164 (when (string-match disc elem)
1165 (setq seq (nconc (delete elem seq) (list elem))))))
1168 (defun mm-get-content-id (id)
1169 "Return the handle(s) referred to by ID."
1170 (cdr (assoc id mm-content-id-alist)))
1172 (defconst mm-image-type-regexps
1173 '(("/\\*.*XPM.\\*/" . xpm)
1177 ("\211PNG\r\n" . png)
1179 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1180 ("%!PS" . postscript))
1181 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1182 When the first bytes of an image file match REGEXP, it is assumed to
1183 be of image type IMAGE-TYPE.")
1185 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1186 (defun mm-image-type-from-buffer ()
1187 "Determine the image type from data in the current buffer.
1188 Value is a symbol specifying the image type or nil if type cannot
1190 (let ((types mm-image-type-regexps)
1192 (goto-char (point-min))
1193 (while (and types (null type))
1194 (let ((regexp (car (car types)))
1195 (image-type (cdr (car types))))
1196 (when (looking-at regexp)
1197 (setq type image-type))
1198 (setq types (cdr types))))
1201 (defun mm-get-image (handle)
1202 "Return an image instance based on HANDLE."
1203 (let ((type (mm-handle-media-subtype handle))
1205 ;; Allow some common translations.
1208 ((equal type "x-pixmap")
1210 ((equal type "x-xbitmap")
1212 ((equal type "x-portable-bitmap")
1215 (or (mm-handle-cache handle)
1216 (mm-with-unibyte-buffer
1217 (mm-insert-part handle)
1221 ;; Avoid testing `make-glyph' since W3 may define
1222 ;; a bogus version of it.
1223 (if (fboundp 'create-image)
1224 (create-image (buffer-string)
1225 (or (mm-image-type-from-buffer)
1228 (mm-create-image-xemacs type))))
1229 (mm-handle-set-cache handle spec))))))
1231 (defun mm-create-image-xemacs (type)
1234 ;; xbm images require special handling, since
1235 ;; the only way to create glyphs from these
1236 ;; (without a ton of work) is to write them
1237 ;; out to a file, and then create a file
1239 (let ((file (mm-make-temp-file
1240 (expand-file-name "emm.xbm"
1241 mm-tmp-directory))))
1244 (write-region (point-min) (point-max) file)
1245 (make-glyph (list (cons 'x file))))
1247 (delete-file file)))))
1251 (or (mm-image-type-from-buffer)
1253 :data (buffer-string))))))
1255 (defun mm-image-fit-p (handle)
1256 "Say whether the image in HANDLE will fit the current window."
1257 (let ((image (mm-get-image handle)))
1258 (if (fboundp 'glyph-width)
1259 ;; XEmacs' glyphs can actually tell us about their width, so
1260 ;; lets be nice and smart about them.
1261 (or mm-inline-large-images
1262 (and (< (glyph-width image) (window-pixel-width))
1263 (< (glyph-height image) (window-pixel-height))))
1264 (let* ((size (image-size image))
1267 (or mm-inline-large-images
1268 (and (< h (1- (window-height))) ; Don't include mode line.
1269 (< w (window-width))))))))
1271 (defun mm-valid-image-format-p (format)
1272 "Say whether FORMAT can be displayed natively by Emacs."
1275 ((fboundp 'valid-image-instantiator-format-p)
1276 (valid-image-instantiator-format-p format))
1278 ((fboundp 'image-type-available-p)
1279 (and (display-graphic-p)
1280 (image-type-available-p format)))
1281 ;; Nobody else can do images yet.
1285 (defun mm-valid-and-fit-image-p (format handle)
1286 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1287 (and (mm-valid-image-format-p format)
1288 (mm-image-fit-p handle)))
1290 (defun mm-find-part-by-type (handles type &optional notp recursive)
1291 "Search in HANDLES for part with TYPE.
1292 If NOTP, returns first non-matching part.
1293 If RECURSIVE, search recursively."
1296 (if (and recursive (stringp (caar handles)))
1297 (if (setq handle (mm-find-part-by-type (cdar handles) type
1301 (not (equal (mm-handle-media-type (car handles)) type))
1302 (equal (mm-handle-media-type (car handles)) type))
1303 (setq handle (car handles)
1305 (setq handles (cdr handles)))
1308 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1309 (goto-char (point-min))
1310 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1312 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1314 (end (save-excursion
1315 (goto-char (point-max))
1316 (if (re-search-backward close-delimiter nil t)
1320 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1321 (while (and (not result)
1322 (re-search-forward boundary end t))
1323 (goto-char (match-beginning 0))
1327 (narrow-to-region start (1- (point)))
1328 (when (let ((ctl (ignore-errors
1329 (mail-header-parse-content-type
1330 (mail-fetch-field "content-type")))))
1332 (not (equal (car ctl) type))
1333 (equal (car ctl) type)))
1334 (setq result (buffer-string))))))
1336 (setq start (point)))
1337 (when (and (not result) start)
1340 (narrow-to-region start end)
1341 (when (let ((ctl (ignore-errors
1342 (mail-header-parse-content-type
1343 (mail-fetch-field "content-type")))))
1345 (not (equal (car ctl) type))
1346 (equal (car ctl) type)))
1347 (setq result (buffer-string))))))
1350 (defvar mm-security-handle nil)
1352 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1353 ;; HANDLE could be a CTL.
1355 (put-text-property 0 (length (car handle)) parameter value
1358 (defun mm-possibly-verify-or-decrypt (parts ctl)
1359 (let ((type (car ctl))
1360 (subtype (cadr (split-string (car ctl) "/")))
1361 (mm-security-handle ctl) ;; (car CTL) is the type.
1362 protocol func functest)
1364 ((or (equal type "application/x-pkcs7-mime")
1365 (equal type "application/pkcs7-mime"))
1368 ((eq mm-decrypt-option 'never) nil)
1369 ((eq mm-decrypt-option 'always) t)
1370 ((eq mm-decrypt-option 'known) t)
1372 (format "Decrypt (S/MIME) part? "))))
1373 (mm-view-pkcs7 parts))
1374 (setq parts (mm-dissect-buffer t)))))
1375 ((equal subtype "signed")
1376 (unless (and (setq protocol
1377 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1378 (not (equal protocol "multipart/mixed")))
1379 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1380 (let ((protocols mm-verify-function-alist))
1382 (if (and (or (not (setq functest (nth 3 (car protocols))))
1383 (funcall functest parts ctl))
1384 (mm-find-part-by-type parts (caar protocols) nil t))
1385 (setq protocol (caar protocols)
1387 (setq protocols (cdr protocols))))))
1388 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1390 ((eq mm-verify-option 'never) nil)
1391 ((eq mm-verify-option 'always) t)
1392 ((eq mm-verify-option 'known)
1394 (or (not (setq functest
1395 (nth 3 (assoc protocol
1396 mm-verify-function-alist))))
1397 (funcall functest parts ctl))))
1400 (format "Verify signed (%s) part? "
1401 (or (nth 2 (assoc protocol mm-verify-function-alist))
1402 (format "protocol=%s" protocol))))))
1405 (funcall func parts ctl)
1406 (mm-set-handle-multipart-parameter
1407 mm-security-handle 'gnus-details
1408 (format "Unknown sign protocol (%s)" protocol))))))
1409 ((equal subtype "encrypted")
1410 (unless (setq protocol
1411 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1412 ;; The message is broken.
1413 (let ((parts parts))
1415 (if (assoc (mm-handle-media-type (car parts))
1416 mm-decrypt-function-alist)
1417 (setq protocol (mm-handle-media-type (car parts))
1419 (setq parts (cdr parts))))))
1420 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1422 ((eq mm-decrypt-option 'never) nil)
1423 ((eq mm-decrypt-option 'always) t)
1424 ((eq mm-decrypt-option 'known)
1426 (or (not (setq functest
1427 (nth 3 (assoc protocol
1428 mm-decrypt-function-alist))))
1429 (funcall functest parts ctl))))
1432 (format "Decrypt (%s) part? "
1433 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1434 (format "protocol=%s" protocol))))))
1437 (setq parts (funcall func parts ctl))
1438 (mm-set-handle-multipart-parameter
1439 mm-security-handle 'gnus-details
1440 (format "Unknown encrypt protocol (%s)" protocol))))))
1444 (defun mm-multiple-handles (handles)
1445 (and (listp handles)
1446 (> (length handles) 1)
1447 (or (listp (car handles))
1448 (stringp (car handles)))))
1450 (defun mm-complicated-handles (handles)
1451 (and (listp (car handles))
1452 (> (length handles) 1)))
1454 (defun mm-merge-handles (handles1 handles2)
1456 (if (listp (car handles1))
1459 (if (listp (car handles2))
1463 (defun mm-readable-p (handle)
1464 "Say whether the content of HANDLE is readable."
1465 (and (< (with-current-buffer (mm-handle-buffer handle)
1466 (buffer-size)) 10000)
1467 (mm-with-unibyte-buffer
1468 (mm-insert-part handle)
1469 (and (eq (mm-body-7-or-8) '7bit)
1470 (not (mm-long-lines-p 76))))))
1472 (provide 'mm-decode)
1474 ;;; mm-decode.el ends here