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.
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 "w3m") 'w3m-standalone)
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."
117 :type '(choice (const w3)
119 (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."
137 :group 'mime-display)
139 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
140 "Regexp matching URLs which are considered to be safe.
141 Some HTML mails might contain a nasty trick used by spammers, using
142 the <img> tag which is far more evil than the [Click Here!] button.
143 It is most likely intended to check whether the ominous spam mail has
144 reached your eyes or not, in which case the spammer knows for sure
145 that your email address is valid. It is done by embedding an
146 identifier string into a URL that you might automatically retrieve
147 when displaying the image. The default value is \"\\\\`cid:\" which only
148 matches parts embedded to the Multipart/Related type MIME contents and
149 Gnus will never connect to the spammer's site arbitrarily. You may
150 set this variable to nil if you consider all urls to be safe."
151 :type '(choice (regexp :tag "Regexp")
152 (const :tag "All URLs are safe" nil))
153 :group 'mime-display)
155 (defcustom mm-inline-text-html-with-w3m-keymap t
156 "If non-nil, use emacs-w3m command keys in the article buffer."
158 :group 'mime-display)
160 (defcustom mm-inline-media-tests
164 (mm-valid-and-fit-image-p 'jpeg handle)))
168 (mm-valid-and-fit-image-p 'png handle)))
172 (mm-valid-and-fit-image-p 'gif handle)))
176 (mm-valid-and-fit-image-p 'tiff handle)) )
180 (mm-valid-and-fit-image-p 'xbm handle)))
184 (mm-valid-and-fit-image-p 'xbm handle)))
188 (mm-valid-and-fit-image-p 'xpm handle)))
192 (mm-valid-and-fit-image-p 'xpm handle)))
196 (mm-valid-and-fit-image-p 'bmp handle)))
197 ("image/x-portable-bitmap"
200 (mm-valid-and-fit-image-p 'pbm handle)))
201 ("text/plain" mm-inline-text identity)
202 ("text/enriched" mm-inline-text identity)
203 ("text/richtext" mm-inline-text identity)
204 ("text/x-patch" mm-display-patch-inline
206 (locate-library "diff-mode")))
207 ("application/emacs-lisp" mm-display-elisp-inline identity)
208 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
212 (or mm-inline-text-html-renderer
213 mm-text-html-renderer)))
217 (or (featurep 'vcard)
218 (locate-library "vcard"))))
219 ("message/delivery-status" mm-inline-text identity)
220 ("message/rfc822" mm-inline-message identity)
221 ("message/partial" mm-inline-partial identity)
222 ("message/external-body" mm-inline-external-body identity)
223 ("text/.*" mm-inline-text identity)
224 ("audio/wav" mm-inline-audio
226 (and (or (featurep 'nas-sound) (featurep 'native-sound))
227 (device-sound-enabled-p))))
231 (and (or (featurep 'nas-sound) (featurep 'native-sound))
232 (device-sound-enabled-p))))
233 ("application/pgp-signature" ignore identity)
234 ("application/x-pkcs7-signature" ignore identity)
235 ("application/pkcs7-signature" ignore identity)
236 ("application/x-pkcs7-mime" ignore identity)
237 ("application/pkcs7-mime" ignore identity)
238 ("multipart/alternative" ignore identity)
239 ("multipart/mixed" ignore identity)
240 ("multipart/related" ignore identity)
241 ;; Disable audio and image
242 ("audio/.*" ignore ignore)
243 ("image/.*" ignore ignore)
244 ;; Default to displaying as text
245 (".*" mm-inline-text mm-readable-p))
246 "Alist of media types/tests saying whether types can be displayed inline."
247 :type '(repeat (list (regexp :tag "MIME type")
248 (function :tag "Display function")
249 (function :tag "Display test")))
250 :group 'mime-display)
252 (defcustom mm-inlined-types
253 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
254 "message/partial" "message/external-body" "application/emacs-lisp"
255 "application/x-emacs-lisp"
256 "application/pgp-signature" "application/x-pkcs7-signature"
257 "application/pkcs7-signature" "application/x-pkcs7-mime"
258 "application/pkcs7-mime")
259 "List of media types that are to be displayed inline.
260 See also `mm-inline-media-tests', which says how to display a media
262 :type '(repeat string)
263 :group 'mime-display)
265 (defcustom mm-keep-viewer-alive-types
266 '("application/postscript" "application/msword" "application/vnd.ms-excel"
267 "application/pdf" "application/x-dvi")
268 "List of media types for which the external viewer will not be killed
269 when selecting a different article."
270 :type '(repeat string)
271 :group 'mime-display)
273 (defcustom mm-automatic-display
274 '("text/plain" "text/enriched" "text/richtext" "text/html"
275 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
276 "message/rfc822" "text/x-patch" "application/pgp-signature"
277 "application/emacs-lisp" "application/x-emacs-lisp"
278 "application/x-pkcs7-signature"
279 "application/pkcs7-signature" "application/x-pkcs7-mime"
280 "application/pkcs7-mime")
281 "A list of MIME types to be displayed automatically."
282 :type '(repeat string)
283 :group 'mime-display)
285 (defcustom mm-attachment-override-types '("text/x-vcard"
286 "application/pkcs7-mime"
287 "application/x-pkcs7-mime"
288 "application/pkcs7-signature"
289 "application/x-pkcs7-signature")
290 "Types to have \"attachment\" ignored if they can be displayed inline."
291 :type '(repeat string)
292 :group 'mime-display)
294 (defcustom mm-inline-override-types nil
295 "Types to be treated as attachments even if they can be displayed inline."
296 :type '(repeat string)
297 :group 'mime-display)
299 (defcustom mm-automatic-external-display nil
300 "List of MIME type regexps that will be displayed externally automatically."
301 :type '(repeat string)
302 :group 'mime-display)
304 (defcustom mm-discouraged-alternatives nil
305 "List of MIME types that are discouraged when viewing multipart/alternative.
306 Viewing agents are supposed to view the last possible part of a message,
307 as that is supposed to be the richest. However, users may prefer other
308 types instead, and this list says what types are most unwanted. If,
309 for instance, text/html parts are very unwanted, and text/richtext are
310 somewhat unwanted, then the value of this variable should be set
313 (\"text/html\" \"text/richtext\")"
314 :type '(repeat string)
315 :group 'mime-display)
317 (defcustom mm-tmp-directory
318 (if (fboundp 'temp-directory)
320 (if (boundp 'temporary-file-directory)
321 temporary-file-directory
323 "Where mm will store its temporary files."
325 :group 'mime-display)
327 (defcustom mm-inline-large-images nil
328 "If non-nil, then all images fit in the buffer."
330 :group 'mime-display)
332 (defvar mm-file-name-rewrite-functions
333 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
334 "*List of functions used for rewriting file names of MIME parts.
335 Each function takes a file name as input and returns a file name.
337 Ready-made functions include
338 `mm-file-name-delete-control'
339 `mm-file-name-delete-gotchas'
340 `mm-file-name-delete-whitespace',
341 `mm-file-name-trim-whitespace',
342 `mm-file-name-collapse-whitespace',
343 `mm-file-name-replace-whitespace',
344 `capitalize', `downcase', `upcase', and
347 (defvar mm-path-name-rewrite-functions nil
348 "*List of functions for rewriting the full file names of MIME parts.
349 This is used when viewing parts externally, and is meant for
350 transforming the absolute name so that non-compliant programs can find
351 the file where it's saved.
353 Each function takes a file name as input and returns a file name.")
355 (defvar mm-file-name-replace-whitespace nil
356 "String used for replacing whitespace characters; default is `\"_\"'.")
358 (defcustom mm-default-directory nil
359 "The default directory where mm will save files.
360 If not set, `default-directory' will be used."
361 :type '(choice directory (const :tag "Default" nil))
362 :group 'mime-display)
364 (defcustom mm-attachment-file-modes 384
365 "Set the mode bits of saved attachments to this integer."
367 :group 'mime-display)
369 (defcustom mm-external-terminal-program "xterm"
370 "The program to start an external terminal."
372 :group 'mime-display)
374 ;;; Internal variables.
376 (defvar mm-last-shell-command "")
377 (defvar mm-content-id-alist nil)
378 (defvar mm-postponed-undisplay-list nil)
380 ;; According to RFC2046, in particular, in a digest, the default
381 ;; Content-Type value for a body part is changed from "text/plain" to
383 (defvar mm-dissect-default-type "text/plain")
385 (autoload 'mml2015-verify "mml2015")
386 (autoload 'mml2015-verify-test "mml2015")
387 (autoload 'mml-smime-verify "mml-smime")
388 (autoload 'mml-smime-verify-test "mml-smime")
390 (defvar mm-verify-function-alist
391 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
392 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
393 mm-uu-pgp-signed-test)
394 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
395 mml-smime-verify-test)
396 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
397 mml-smime-verify-test)))
399 (defcustom mm-verify-option 'never
400 "Option of verifying signed parts.
401 `never', not verify; `always', always verify;
402 `known', only verify known protocols. Otherwise, ask user."
403 :type '(choice (item always)
405 (item :tag "only known protocols" known)
406 (item :tag "ask" nil))
407 :group 'mime-security)
409 (autoload 'mml2015-decrypt "mml2015")
410 (autoload 'mml2015-decrypt-test "mml2015")
412 (defvar mm-decrypt-function-alist
413 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
414 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
415 mm-uu-pgp-encrypted-test)))
417 (defcustom mm-decrypt-option nil
418 "Option of decrypting encrypted parts.
419 `never', not decrypt; `always', always decrypt;
420 `known', only decrypt known protocols. Otherwise, ask user."
421 :type '(choice (item always)
423 (item :tag "only known protocols" known)
424 (item :tag "ask" nil))
425 :group 'mime-security)
427 (defvar mm-viewer-completion-map
428 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
429 (set-keymap-parent map minibuffer-local-completion-map)
431 "Keymap for input viewer with completion.")
433 ;; Should we bind other key to minibuffer-complete-word?
434 (define-key mm-viewer-completion-map " " 'self-insert-command)
436 (defvar mm-viewer-completion-map
437 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
438 (set-keymap-parent map minibuffer-local-completion-map)
440 "Keymap for input viewer with completion.")
442 ;; Should we bind other key to minibuffer-complete-word?
443 (define-key mm-viewer-completion-map " " 'self-insert-command)
447 (defun mm-alist-to-plist (alist)
448 "Convert association list ALIST into the equivalent property-list form.
449 The plist is returned. This converts from
451 \((a . 1) (b . 2) (c . 3))
457 The original alist is not modified. See also `destructive-alist-to-plist'."
460 (let ((el (car alist)))
461 (setq plist (cons (cdr el) (cons (car el) plist))))
462 (setq alist (cdr alist)))
465 (defun mm-keep-viewer-alive-p (handle)
466 "Say whether external viewer for HANDLE should stay alive."
467 (let ((types mm-keep-viewer-alive-types)
468 (type (mm-handle-media-type handle))
471 (while (setq ty (pop types))
472 (when (string-match ty type)
473 (throw 'found t))))))
475 (defun mm-handle-set-external-undisplayer (handle function)
476 "Set the undisplayer for HANDLE to FUNCTION.
477 Postpone undisplaying of viewers for types in
478 `mm-keep-viewer-alive-types'."
479 (if (mm-keep-viewer-alive-p handle)
480 (let ((new-handle (copy-sequence handle)))
481 (mm-handle-set-undisplayer new-handle function)
482 (mm-handle-set-undisplayer handle nil)
483 (push new-handle mm-postponed-undisplay-list))
484 (mm-handle-set-undisplayer handle function)))
486 (defun mm-destroy-postponed-undisplay-list ()
487 (when mm-postponed-undisplay-list
488 (message "Destroying external MIME viewers")
489 (mm-destroy-parts mm-postponed-undisplay-list)))
491 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
492 "Dissect the current buffer and return a list of MIME handles."
494 (let (ct ctl type subtype cte cd description id result from)
496 (mail-narrow-to-head)
497 (when (or no-strict-mime
499 (mail-fetch-field "mime-version"))
500 (setq ct (mail-fetch-field "content-type")
501 ctl (ignore-errors (mail-header-parse-content-type ct))
502 cte (mail-fetch-field "content-transfer-encoding")
503 cd (mail-fetch-field "content-disposition")
504 description (mail-fetch-field "content-description")
505 from (mail-fetch-field "from")
506 id (mail-fetch-field "content-id"))
507 ;; FIXME: In some circumstances, this code is running within
508 ;; an unibyte macro. mail-extract-address-components
509 ;; creates unibyte buffers. This `if', though not a perfect
510 ;; solution, avoids most of them.
512 (setq from (cadr (mail-extract-address-components from))))))
514 (setq cte (mail-header-strip cte)))
516 (not (string-match "/" (car ctl))))
517 (mm-dissect-singlepart
518 (list mm-dissect-default-type)
519 (and cte (intern (downcase (mail-header-remove-whitespace
520 (mail-header-remove-comments
523 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
525 (setq type (split-string (car ctl) "/"))
526 (setq subtype (cadr type)
531 ((equal type "multipart")
532 (let ((mm-dissect-default-type (if (equal subtype "digest")
535 (start (cdr (assq 'start (cdr ctl)))))
536 (add-text-properties 0 (length (car ctl))
537 (mm-alist-to-plist (cdr ctl)) (car ctl))
539 ;; what really needs to be done here is a way to link a
540 ;; MIME handle back to it's parent MIME handle (in a multilevel
541 ;; MIME article). That would probably require changing
542 ;; the mm-handle API so we simply store the multipart buffert
543 ;; name as a text property of the "multipart/whatever" string.
544 (add-text-properties 0 (length (car ctl))
545 (list 'buffer (mm-copy-to-buffer)
549 (cons (car ctl) (mm-dissect-multipart ctl))))
551 (mm-possibly-verify-or-decrypt
552 (mm-dissect-singlepart
554 (and cte (intern (downcase (mail-header-remove-whitespace
555 (mail-header-remove-comments
558 (and cd (ignore-errors
559 (mail-header-parse-content-disposition cd)))
563 (when (string-match " *<\\(.*\\)> *" id)
564 (setq id (match-string 1 id)))
565 (push (cons id result) mm-content-id-alist))
568 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
570 (if (equal "text/plain" (car ctl))
574 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
576 (defun mm-dissect-multipart (ctl)
577 (goto-char (point-min))
578 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
579 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
582 (goto-char (point-max))
583 (if (re-search-backward close-delimiter nil t)
586 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
587 (while (and (< (point) end) (re-search-forward boundary end t))
588 (goto-char (match-beginning 0))
592 (narrow-to-region start (point))
593 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
595 (or (looking-at boundary)
597 (setq start (point)))
598 (when (and start (< start end))
601 (narrow-to-region start end)
602 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
603 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
605 (defun mm-copy-to-buffer ()
606 "Copy the contents of the current buffer to a fresh buffer."
608 (let ((obuf (current-buffer))
610 (goto-char (point-min))
611 (search-forward-regexp "^\n" nil t)
613 (set-buffer (generate-new-buffer " *mm*"))
614 (insert-buffer-substring obuf beg)
617 (defun mm-display-parts (handle &optional no-default)
618 (if (stringp (car handle))
619 (mapcar 'mm-display-parts (cdr handle))
620 (if (bufferp (car handle))
622 (narrow-to-region (point) (point))
623 (mm-display-part handle)
624 (goto-char (point-max)))
625 (mapcar 'mm-display-parts handle))))
627 (defun mm-display-part (handle &optional no-default)
628 "Display the MIME part represented by HANDLE.
629 Returns nil if the part is removed; inline if displayed inline;
630 external if displayed external."
632 (mailcap-parse-mailcaps)
633 (if (mm-handle-displayed-p handle)
634 (mm-remove-part handle)
635 (let* ((type (mm-handle-media-type handle))
636 (method (mailcap-mime-info type)))
637 (if (and (mm-inlinable-p handle)
638 (mm-inlined-p handle))
641 (mm-display-inline handle)
645 (if (and (not method)
646 (equal "text" (car (split-string type))))
649 (mm-insert-inline handle (mm-get-part handle))
652 handle (or method 'mailcap-save-binary-file)))))))))
654 (defun mm-display-external (handle method)
655 "Display HANDLE using METHOD."
656 (let ((outbuf (current-buffer)))
657 (mm-with-unibyte-buffer
658 (if (functionp method)
659 (let ((cur (current-buffer)))
660 (if (eq method 'mailcap-save-binary-file)
662 (set-buffer (generate-new-buffer " *mm*"))
664 (mm-insert-part handle)
665 (let ((win (get-buffer-window cur t)))
667 (select-window win)))
668 (switch-to-buffer (generate-new-buffer " *mm*")))
669 (buffer-disable-undo)
670 (mm-set-buffer-file-coding-system mm-binary-coding-system)
671 (insert-buffer-substring cur)
672 (goto-char (point-min))
673 (message "Viewing with %s" method)
674 (let ((mm (current-buffer))
675 (non-viewer (assq 'non-viewer
677 (mm-handle-media-type handle) t))))
681 (mm-save-part handle))
682 (when (and (not non-viewer)
684 (mm-handle-set-undisplayer handle mm)))))
685 ;; The function is a string to be executed.
686 (mm-insert-part handle)
687 (let* ((dir (mm-make-temp-file
688 (expand-file-name "emm." mm-tmp-directory) 'dir))
690 (mail-content-type-get
691 (mm-handle-disposition handle) 'filename)
692 (mail-content-type-get
693 (mm-handle-type handle) 'name)))
694 (mime-info (mailcap-mime-info
695 (mm-handle-media-type handle) t))
696 (needsterm (or (assoc "needsterm" mime-info)
697 (assoc "needsterminal" mime-info)))
698 (copiousoutput (assoc "copiousoutput" mime-info))
700 ;; We create a private sub-directory where we store our files.
701 (set-file-modes dir 448)
703 (setq file (expand-file-name
704 (gnus-map-function mm-file-name-rewrite-functions
705 (file-name-nondirectory filename))
707 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
708 (let ((coding-system-for-write mm-binary-coding-system))
709 (write-region (point-min) (point-max) file nil 'nomesg))
710 (message "Viewing with %s" method)
713 (let ((command (mm-mailcap-command
714 method file (mm-handle-type handle))))
717 (start-process "*display*" nil
718 mm-external-terminal-program
720 shell-command-switch command)
728 shell-command-switch command)))
731 (set-process-sentinel
732 (get-buffer-process buffer)
733 `(lambda (process state)
734 (if (eq 'exit (process-status process))
735 (gnus-configure-windows
736 ',gnus-current-window-configuration))))
737 (gnus-configure-windows 'display-term))
738 (mm-handle-set-external-undisplayer handle (cons file buffer)))
739 (message "Displaying %s..." command))
742 (with-current-buffer outbuf
748 (call-process shell-file-name nil
750 (generate-new-buffer " *mm*"))
754 method file (mm-handle-type handle)))
755 (if (buffer-live-p buffer)
760 (ignore-errors (delete-file file))
761 (ignore-errors (delete-directory
762 (file-name-directory file)))
763 (ignore-errors (kill-buffer buffer))))))
766 (let ((command (mm-mailcap-command
767 method file (mm-handle-type handle))))
769 (start-process "*display*"
771 (generate-new-buffer " *mm*"))
773 shell-command-switch command)
774 (mm-handle-set-external-undisplayer
775 handle (cons file buffer)))
776 (message "Displaying %s..." command))
779 (defun mm-mailcap-command (method file type-list)
780 (let ((ctl (cdr type-list))
784 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
786 (push (substring method beg (match-beginning 0)) out)
787 (setq beg (match-end 0)
788 total (match-string 0 method)
789 sub (match-string 1 method))
791 ((string= total "%%")
793 ((or (string= total "%s")
794 ;; We do our own quoting.
795 (string= total "'%s'")
796 (string= total "\"%s\""))
797 (setq uses-stdin nil)
799 (gnus-map-function mm-path-name-rewrite-functions file)) out))
800 ((string= total "%t")
801 (push (mm-quote-arg (car type-list)) out))
803 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
804 (push (substring method beg (length method)) out)
808 (gnus-map-function mm-path-name-rewrite-functions file))
810 (mapconcat 'identity (nreverse out) "")))
812 (defun mm-remove-parts (handles)
813 "Remove the displayed MIME parts represented by HANDLES."
814 (if (and (listp handles)
815 (bufferp (car handles)))
816 (mm-remove-part handles)
818 (while (setq handle (pop handles))
821 (when (buffer-live-p (get-text-property 0 'buffer handle))
822 (kill-buffer (get-text-property 0 'buffer handle))))
824 (stringp (car handle)))
825 (mm-remove-parts (cdr handle)))
827 (mm-remove-part handle)))))))
829 (defun mm-destroy-parts (handles)
830 "Remove the displayed MIME parts represented by HANDLES."
831 (if (and (listp handles)
832 (bufferp (car handles)))
833 (mm-destroy-part handles)
835 (while (setq handle (pop handles))
838 (when (buffer-live-p (get-text-property 0 'buffer handle))
839 (kill-buffer (get-text-property 0 'buffer handle))))
841 (stringp (car handle)))
842 (mm-destroy-parts handle))
844 (mm-destroy-part handle)))))))
846 (defun mm-remove-part (handle)
847 "Remove the displayed MIME part represented by HANDLE."
849 (let ((object (mm-handle-undisplayer handle)))
852 ;; Internally displayed part.
853 ((mm-annotationp object)
854 (delete-annotation object))
855 ((or (functionp object)
857 (eq (car object) 'lambda)))
859 ;; Externally displayed part.
862 (while (get-buffer-process (cdr object))
863 (interrupt-process (get-buffer-process (cdr object)))
864 (message "Waiting for external displayer to die...")
868 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
869 (message "Waiting for external displayer to die...done")
870 (ignore-errors (delete-file (car object)))
871 (ignore-errors (delete-directory (file-name-directory
874 (when (buffer-live-p object)
875 (kill-buffer object)))))
876 (mm-handle-set-undisplayer handle nil))))
878 (defun mm-display-inline (handle)
879 (let* ((type (mm-handle-media-type handle))
880 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
881 (funcall function handle)
882 (goto-char (point-min))))
884 (defun mm-assoc-string-match (alist type)
886 (when (string-match (car elem) type)
889 (defun mm-automatic-display-p (handle)
890 "Say whether the user wants HANDLE to be displayed automatically."
891 (let ((methods mm-automatic-display)
892 (type (mm-handle-media-type handle))
894 (while (setq method (pop methods))
895 (when (and (not (mm-inline-override-p handle))
896 (string-match method type))
901 (defun mm-inlinable-p (handle)
902 "Say whether HANDLE can be displayed inline."
903 (let ((alist mm-inline-media-tests)
904 (type (mm-handle-media-type handle))
907 (when (string-match (caar alist) type)
908 (setq test (caddar alist)
910 (setq test (funcall test handle)))
914 (defun mm-inlined-p (handle)
915 "Say whether the user wants HANDLE to be displayed inline."
916 (let ((methods mm-inlined-types)
917 (type (mm-handle-media-type handle))
919 (while (setq method (pop methods))
920 (when (and (not (mm-inline-override-p handle))
921 (string-match method type))
926 (defun mm-attachment-override-p (handle)
927 "Say whether HANDLE should have attachment behavior overridden."
928 (let ((types mm-attachment-override-types)
929 (type (mm-handle-media-type handle))
932 (while (setq ty (pop types))
933 (when (and (string-match ty type)
934 (mm-inlinable-p handle))
935 (throw 'found t))))))
937 (defun mm-inline-override-p (handle)
938 "Say whether HANDLE should have inline behavior overridden."
939 (let ((types mm-inline-override-types)
940 (type (mm-handle-media-type handle))
943 (while (setq ty (pop types))
944 (when (string-match ty type)
945 (throw 'found t))))))
947 (defun mm-automatic-external-display-p (type)
948 "Return the user-defined method for TYPE."
949 (let ((methods mm-automatic-external-display)
951 (while (setq method (pop methods))
952 (when (string-match method type)
957 (defun mm-destroy-part (handle)
958 "Destroy the data structures connected to HANDLE."
960 (mm-remove-part handle)
961 (when (buffer-live-p (mm-handle-buffer handle))
962 (kill-buffer (mm-handle-buffer handle)))))
964 (defun mm-handle-displayed-p (handle)
965 "Say whether HANDLE is displayed or not."
966 (mm-handle-undisplayer handle))
969 ;;; Functions for outputting parts
972 (defun mm-get-part (handle)
973 "Return the contents of HANDLE as a string."
974 (mm-with-unibyte-buffer
975 (insert (with-current-buffer (mm-handle-buffer handle)
976 (mm-with-unibyte-current-buffer
978 (mm-decode-content-transfer-encoding
979 (mm-handle-encoding handle)
980 (mm-handle-media-type handle))
983 (defun mm-insert-part (handle)
984 "Insert the contents of HANDLE in the current buffer."
985 (let ((cur (current-buffer)))
987 (if (member (mm-handle-media-supertype handle) '("text" "message"))
989 (insert-buffer-substring (mm-handle-buffer handle))
991 (mm-decode-content-transfer-encoding
992 (mm-handle-encoding handle)
993 (mm-handle-media-type handle))
994 (let ((temp (current-buffer)))
996 (insert-buffer-substring temp))))
997 (mm-with-unibyte-buffer
998 (insert-buffer-substring (mm-handle-buffer handle))
1000 (mm-decode-content-transfer-encoding
1001 (mm-handle-encoding handle)
1002 (mm-handle-media-type handle))
1003 (let ((temp (current-buffer)))
1005 (insert-buffer-substring temp))))))))
1007 (defun mm-file-name-delete-whitespace (file-name)
1008 "Remove all whitespace characters from FILE-NAME."
1009 (while (string-match "\\s-+" file-name)
1010 (setq file-name (replace-match "" t t file-name)))
1013 (defun mm-file-name-trim-whitespace (file-name)
1014 "Remove leading and trailing whitespace characters from FILE-NAME."
1015 (when (string-match "\\`\\s-+" file-name)
1016 (setq file-name (substring file-name (match-end 0))))
1017 (when (string-match "\\s-+\\'" file-name)
1018 (setq file-name (substring file-name 0 (match-beginning 0))))
1021 (defun mm-file-name-collapse-whitespace (file-name)
1022 "Collapse multiple whitespace characters in FILE-NAME."
1023 (while (string-match "\\s-\\s-+" file-name)
1024 (setq file-name (replace-match " " t t file-name)))
1027 (defun mm-file-name-replace-whitespace (file-name)
1028 "Replace whitespace characters in FILE-NAME with underscores.
1029 Set the option `mm-file-name-replace-whitespace' to any other
1030 string if you do not like underscores."
1031 (let ((s (or mm-file-name-replace-whitespace "_")))
1032 (while (string-match "\\s-" file-name)
1033 (setq file-name (replace-match s t t file-name))))
1036 (defun mm-file-name-delete-control (filename)
1037 "Delete control characters from FILENAME."
1038 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1040 (defun mm-file-name-delete-gotchas (filename)
1041 "Delete shell gotchas from FILENAME."
1042 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1043 (gnus-replace-in-string filename "^[.-]+" ""))
1045 (defun mm-save-part (handle)
1046 "Write HANDLE to a file."
1047 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1048 (filename (mail-content-type-get
1049 (mm-handle-disposition handle) 'filename))
1052 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1053 (file-name-nondirectory filename))))
1055 (read-file-name "Save MIME part to: "
1056 (or mm-default-directory default-directory)
1057 nil nil (or filename name "")))
1058 (setq mm-default-directory (file-name-directory file))
1059 (and (or (not (file-exists-p file))
1060 (yes-or-no-p (format "File %s already exists; overwrite? "
1063 (mm-save-part-to-file handle file)
1066 (defun mm-save-part-to-file (handle file)
1067 (mm-with-unibyte-buffer
1068 (mm-insert-part handle)
1069 (let ((coding-system-for-write 'binary)
1070 (current-file-modes (default-file-modes))
1071 ;; Don't re-compress .gz & al. Arguably we should make
1072 ;; `file-name-handler-alist' nil, but that would chop
1073 ;; ange-ftp, which is reasonable to use here.
1074 (inhibit-file-name-operation 'write-region)
1075 (inhibit-file-name-handlers
1076 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1077 (set-default-file-modes mm-attachment-file-modes)
1079 (write-region (point-min) (point-max) file)
1080 (set-default-file-modes current-file-modes)))))
1082 (defun mm-pipe-part (handle)
1083 "Pipe HANDLE to a process."
1084 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1086 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1087 (mm-with-unibyte-buffer
1088 (mm-insert-part handle)
1089 (let ((coding-system-for-write 'binary))
1090 (shell-command-on-region (point-min) (point-max) command nil)))))
1092 (defun mm-interactively-view-part (handle)
1093 "Display HANDLE using METHOD."
1094 (let* ((type (mm-handle-media-type handle))
1096 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1097 (mailcap-mime-info type 'all)))
1098 (method (let ((minibuffer-local-completion-map
1099 mm-viewer-completion-map))
1100 (completing-read "Viewer: " methods))))
1101 (when (string= method "")
1102 (error "No method given"))
1103 (if (string-match "^[^% \t]+$" method)
1104 (setq method (concat method " %s")))
1105 (mm-display-external handle method)))
1107 (defun mm-preferred-alternative (handles &optional preferred)
1108 "Say which of HANDLES are preferred."
1109 (let ((prec (if preferred (list preferred)
1110 (mm-preferred-alternative-precedence handles)))
1111 p h result type handle)
1112 (while (setq p (pop prec))
1115 (setq handle (car h))
1116 (setq type (mm-handle-media-type handle))
1117 (when (and (equal p type)
1118 (mm-automatic-display-p handle)
1119 (or (stringp (car handle))
1120 (not (mm-handle-disposition handle))
1121 (equal (car (mm-handle-disposition handle))
1129 (defun mm-preferred-alternative-precedence (handles)
1130 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1131 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1133 (dolist (disc (reverse mm-discouraged-alternatives))
1134 (dolist (elem (copy-sequence seq))
1135 (when (string-match disc elem)
1136 (setq seq (nconc (delete elem seq) (list elem))))))
1139 (defun mm-get-content-id (id)
1140 "Return the handle(s) referred to by ID."
1141 (cdr (assoc id mm-content-id-alist)))
1143 (defconst mm-image-type-regexps
1144 '(("/\\*.*XPM.\\*/" . xpm)
1148 ("\211PNG\r\n" . png)
1150 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1151 ("%!PS" . postscript))
1152 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1153 When the first bytes of an image file match REGEXP, it is assumed to
1154 be of image type IMAGE-TYPE.")
1156 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1157 (defun mm-image-type-from-buffer ()
1158 "Determine the image type from data in the current buffer.
1159 Value is a symbol specifying the image type or nil if type cannot
1161 (let ((types mm-image-type-regexps)
1163 (goto-char (point-min))
1164 (while (and types (null type))
1165 (let ((regexp (car (car types)))
1166 (image-type (cdr (car types))))
1167 (when (looking-at regexp)
1168 (setq type image-type))
1169 (setq types (cdr types))))
1172 (defun mm-get-image (handle)
1173 "Return an image instance based on HANDLE."
1174 (let ((type (mm-handle-media-subtype handle))
1176 ;; Allow some common translations.
1179 ((equal type "x-pixmap")
1181 ((equal type "x-xbitmap")
1183 ((equal type "x-portable-bitmap")
1186 (or (mm-handle-cache handle)
1187 (mm-with-unibyte-buffer
1188 (mm-insert-part handle)
1192 ;; Avoid testing `make-glyph' since W3 may define
1193 ;; a bogus version of it.
1194 (if (fboundp 'create-image)
1195 (create-image (buffer-string)
1196 (or (mm-image-type-from-buffer)
1199 (mm-create-image-xemacs type))))
1200 (mm-handle-set-cache handle spec))))))
1202 (defun mm-create-image-xemacs (type)
1205 ;; xbm images require special handling, since
1206 ;; the only way to create glyphs from these
1207 ;; (without a ton of work) is to write them
1208 ;; out to a file, and then create a file
1210 (let ((file (mm-make-temp-file
1211 (expand-file-name "emm.xbm"
1212 mm-tmp-directory))))
1215 (write-region (point-min) (point-max) file)
1216 (make-glyph (list (cons 'x file))))
1218 (delete-file file)))))
1222 (or (mm-image-type-from-buffer)
1224 :data (buffer-string))))))
1226 (defun mm-image-fit-p (handle)
1227 "Say whether the image in HANDLE will fit the current window."
1228 (let ((image (mm-get-image handle)))
1229 (if (fboundp 'glyph-width)
1230 ;; XEmacs' glyphs can actually tell us about their width, so
1231 ;; lets be nice and smart about them.
1232 (or mm-inline-large-images
1233 (and (< (glyph-width image) (window-pixel-width))
1234 (< (glyph-height image) (window-pixel-height))))
1235 (let* ((size (image-size image))
1238 (or mm-inline-large-images
1239 (and (< h (1- (window-height))) ; Don't include mode line.
1240 (< w (window-width))))))))
1242 (defun mm-valid-image-format-p (format)
1243 "Say whether FORMAT can be displayed natively by Emacs."
1246 ((fboundp 'valid-image-instantiator-format-p)
1247 (valid-image-instantiator-format-p format))
1249 ((fboundp 'image-type-available-p)
1250 (and (display-graphic-p)
1251 (image-type-available-p format)))
1252 ;; Nobody else can do images yet.
1256 (defun mm-valid-and-fit-image-p (format handle)
1257 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1258 (and (mm-valid-image-format-p format)
1259 (mm-image-fit-p handle)))
1261 (defun mm-find-part-by-type (handles type &optional notp recursive)
1262 "Search in HANDLES for part with TYPE.
1263 If NOTP, returns first non-matching part.
1264 If RECURSIVE, search recursively."
1267 (if (and recursive (stringp (caar handles)))
1268 (if (setq handle (mm-find-part-by-type (cdar handles) type
1272 (not (equal (mm-handle-media-type (car handles)) type))
1273 (equal (mm-handle-media-type (car handles)) type))
1274 (setq handle (car handles)
1276 (setq handles (cdr handles)))
1279 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1280 (goto-char (point-min))
1281 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1283 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1285 (end (save-excursion
1286 (goto-char (point-max))
1287 (if (re-search-backward close-delimiter nil t)
1291 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1292 (while (and (not result)
1293 (re-search-forward boundary end t))
1294 (goto-char (match-beginning 0))
1298 (narrow-to-region start (1- (point)))
1299 (when (let ((ctl (ignore-errors
1300 (mail-header-parse-content-type
1301 (mail-fetch-field "content-type")))))
1303 (not (equal (car ctl) type))
1304 (equal (car ctl) type)))
1305 (setq result (buffer-string))))))
1307 (setq start (point)))
1308 (when (and (not result) start)
1311 (narrow-to-region start end)
1312 (when (let ((ctl (ignore-errors
1313 (mail-header-parse-content-type
1314 (mail-fetch-field "content-type")))))
1316 (not (equal (car ctl) type))
1317 (equal (car ctl) type)))
1318 (setq result (buffer-string))))))
1321 (defvar mm-security-handle nil)
1323 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1324 ;; HANDLE could be a CTL.
1326 (put-text-property 0 (length (car handle)) parameter value
1329 (defun mm-possibly-verify-or-decrypt (parts ctl)
1330 (let ((type (car ctl))
1331 (subtype (cadr (split-string (car ctl) "/")))
1332 (mm-security-handle ctl) ;; (car CTL) is the type.
1333 protocol func functest)
1335 ((or (equal type "application/x-pkcs7-mime")
1336 (equal type "application/pkcs7-mime"))
1339 ((eq mm-decrypt-option 'never) nil)
1340 ((eq mm-decrypt-option 'always) t)
1341 ((eq mm-decrypt-option 'known) t)
1343 (format "Decrypt (S/MIME) part? "))))
1344 (mm-view-pkcs7 parts))
1345 (setq parts (mm-dissect-buffer t)))))
1346 ((equal subtype "signed")
1347 (unless (and (setq protocol
1348 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1349 (not (equal protocol "multipart/mixed")))
1350 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1351 (let ((protocols mm-verify-function-alist))
1353 (if (and (or (not (setq functest (nth 3 (car protocols))))
1354 (funcall functest parts ctl))
1355 (mm-find-part-by-type parts (caar protocols) nil t))
1356 (setq protocol (caar protocols)
1358 (setq protocols (cdr protocols))))))
1359 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1361 ((eq mm-verify-option 'never) nil)
1362 ((eq mm-verify-option 'always) t)
1363 ((eq mm-verify-option 'known)
1365 (or (not (setq functest
1366 (nth 3 (assoc protocol
1367 mm-verify-function-alist))))
1368 (funcall functest parts ctl))))
1371 (format "Verify signed (%s) part? "
1372 (or (nth 2 (assoc protocol mm-verify-function-alist))
1373 (format "protocol=%s" protocol))))))
1376 (funcall func parts ctl)
1377 (mm-set-handle-multipart-parameter
1378 mm-security-handle 'gnus-details
1379 (format "Unknown sign protocol (%s)" protocol))))))
1380 ((equal subtype "encrypted")
1381 (unless (setq protocol
1382 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1383 ;; The message is broken.
1384 (let ((parts parts))
1386 (if (assoc (mm-handle-media-type (car parts))
1387 mm-decrypt-function-alist)
1388 (setq protocol (mm-handle-media-type (car parts))
1390 (setq parts (cdr parts))))))
1391 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1393 ((eq mm-decrypt-option 'never) nil)
1394 ((eq mm-decrypt-option 'always) t)
1395 ((eq mm-decrypt-option 'known)
1397 (or (not (setq functest
1398 (nth 3 (assoc protocol
1399 mm-decrypt-function-alist))))
1400 (funcall functest parts ctl))))
1403 (format "Decrypt (%s) part? "
1404 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1405 (format "protocol=%s" protocol))))))
1408 (setq parts (funcall func parts ctl))
1409 (mm-set-handle-multipart-parameter
1410 mm-security-handle 'gnus-details
1411 (format "Unknown encrypt protocol (%s)" protocol))))))
1415 (defun mm-multiple-handles (handles)
1416 (and (listp (car handles))
1417 (> (length handles) 1)))
1419 (defun mm-merge-handles (handles1 handles2)
1421 (if (listp (car handles1))
1424 (if (listp (car handles2))
1428 (defun mm-readable-p (handle)
1429 "Say whether the content of HANDLE is readable."
1430 (and (< (with-current-buffer (mm-handle-buffer handle)
1431 (buffer-size)) 10000)
1432 (mm-with-unibyte-buffer
1433 (mm-insert-part handle)
1434 (and (eq (mm-body-7-or-8) '7bit)
1435 (not (mm-long-lines-p 76))))))
1437 (provide 'mm-decode)
1439 ;;; mm-decode.el ends here