1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl)
34 (autoload 'executable-find "executable")
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)Customization")
49 (defgroup mime-security ()
50 "MIME security in mail and news articles."
51 :link '(custom-manual "(emacs-mime)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 ((locate-library "w3m") 'w3m)
102 ((executable-find "links") 'links)
103 ((executable-find "lynx") 'lynx)
105 "Render of HTML contents.
106 It is one of defined renderer types, or a rendering function.
107 The defined renderer types are:
108 `w3' : using Emacs/W3;
109 `w3m' : using emacs-w3m;
110 `links': using links;
112 `html2text' : using html2text;
113 `nil' : using external viewer."
114 :type '(choice (symbol w3)
122 :group 'mime-display)
124 (defvar mm-inline-text-html-renderer nil
125 "Function used for rendering inline HTML contents.
126 It is suggested to customize `mm-text-html-renderer' instead.")
128 (defcustom mm-inline-text-html-with-images nil
129 "If non-nil, Gnus will allow retrieving images in the HTML contents
130 with <img> tags. It has no effect on Emacs/w3. For emacs-w3m, the
131 value of the option `w3m-display-inline-images' will be bound with
132 this value. In addition, the variable `w3m-safe-url-regexp' will be
133 bound with the value nil if it is non-nil to make emacs-w3m show all
134 images, however this behavior may be changed in the future."
136 :group 'mime-display)
138 (defcustom mm-inline-text-html-with-w3m-keymap t
139 "If non-nil, use emacs-w3m command keys in the article buffer."
141 :group 'mime-display)
143 (defcustom mm-inline-media-tests
147 (mm-valid-and-fit-image-p 'jpeg handle)))
151 (mm-valid-and-fit-image-p 'png handle)))
155 (mm-valid-and-fit-image-p 'gif handle)))
159 (mm-valid-and-fit-image-p 'tiff handle)) )
163 (mm-valid-and-fit-image-p 'xbm handle)))
167 (mm-valid-and-fit-image-p 'xbm handle)))
171 (mm-valid-and-fit-image-p 'xpm handle)))
175 (mm-valid-and-fit-image-p 'xpm handle)))
179 (mm-valid-and-fit-image-p 'bmp handle)))
180 ("image/x-portable-bitmap"
183 (mm-valid-and-fit-image-p 'pbm handle)))
184 ("text/plain" mm-inline-text identity)
185 ("text/enriched" mm-inline-text identity)
186 ("text/richtext" mm-inline-text identity)
187 ("text/x-patch" mm-display-patch-inline
189 (locate-library "diff-mode")))
190 ("application/emacs-lisp" mm-display-elisp-inline identity)
191 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
195 (or mm-inline-text-html-renderer
196 mm-text-html-renderer)))
200 (or (featurep 'vcard)
201 (locate-library "vcard"))))
202 ("message/delivery-status" mm-inline-text identity)
203 ("message/rfc822" mm-inline-message identity)
204 ("message/partial" mm-inline-partial identity)
205 ("message/external-body" mm-inline-external-body identity)
206 ("text/.*" mm-inline-text identity)
207 ("audio/wav" mm-inline-audio
209 (and (or (featurep 'nas-sound) (featurep 'native-sound))
210 (device-sound-enabled-p))))
214 (and (or (featurep 'nas-sound) (featurep 'native-sound))
215 (device-sound-enabled-p))))
216 ("application/pgp-signature" ignore identity)
217 ("application/x-pkcs7-signature" ignore identity)
218 ("application/pkcs7-signature" ignore identity)
219 ("application/x-pkcs7-mime" ignore identity)
220 ("application/pkcs7-mime" ignore identity)
221 ("multipart/alternative" ignore identity)
222 ("multipart/mixed" ignore identity)
223 ("multipart/related" ignore identity)
224 ;; Disable audio and image
225 ("audio/.*" ignore ignore)
226 ("image/.*" ignore ignore)
227 ;; Default to displaying as text
228 (".*" mm-inline-text mm-readable-p))
229 "Alist of media types/tests saying whether types can be displayed inline."
230 :type '(repeat (list (string :tag "MIME type")
231 (function :tag "Display function")
232 (function :tag "Display test")))
233 :group 'mime-display)
235 (defcustom mm-inlined-types
236 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
237 "message/partial" "message/external-body" "application/emacs-lisp"
238 "application/x-emacs-lisp"
239 "application/pgp-signature" "application/x-pkcs7-signature"
240 "application/pkcs7-signature" "application/x-pkcs7-mime"
241 "application/pkcs7-mime")
242 "List of media types that are to be displayed inline.
243 See also `mm-inline-media-tests', which says how to display a media
245 :type '(repeat string)
246 :group 'mime-display)
248 (defcustom mm-keep-viewer-alive-types
249 '("application/postscript" "application/msword" "application/vnd.ms-excel"
250 "application/pdf" "application/x-dvi")
251 "List of media types for which the external viewer will not be killed
252 when selecting a different article."
253 :type '(repeat string)
254 :group 'mime-display)
256 (defcustom mm-automatic-display
257 '("text/plain" "text/enriched" "text/richtext" "text/html"
258 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
259 "message/rfc822" "text/x-patch" "application/pgp-signature"
260 "application/emacs-lisp" "application/x-emacs-lisp"
261 "application/x-pkcs7-signature"
262 "application/pkcs7-signature" "application/x-pkcs7-mime"
263 "application/pkcs7-mime")
264 "A list of MIME types to be displayed automatically."
265 :type '(repeat string)
266 :group 'mime-display)
268 (defcustom mm-attachment-override-types '("text/x-vcard"
269 "application/pkcs7-mime"
270 "application/x-pkcs7-mime"
271 "application/pkcs7-signature"
272 "application/x-pkcs7-signature")
273 "Types to have \"attachment\" ignored if they can be displayed inline."
274 :type '(repeat string)
275 :group 'mime-display)
277 (defcustom mm-inline-override-types nil
278 "Types to be treated as attachments even if they can be displayed inline."
279 :type '(repeat string)
280 :group 'mime-display)
282 (defcustom mm-automatic-external-display nil
283 "List of MIME type regexps that will be displayed externally automatically."
284 :type '(repeat string)
285 :group 'mime-display)
287 (defcustom mm-discouraged-alternatives nil
288 "List of MIME types that are discouraged when viewing multipart/alternative.
289 Viewing agents are supposed to view the last possible part of a message,
290 as that is supposed to be the richest. However, users may prefer other
291 types instead, and this list says what types are most unwanted. If,
292 for instance, text/html parts are very unwanted, and text/richtext are
293 somewhat unwanted, then the value of this variable should be set
296 (\"text/html\" \"text/richtext\")"
297 :type '(repeat string)
298 :group 'mime-display)
300 (defcustom mm-tmp-directory
301 (cond ((fboundp 'temp-directory) (temp-directory))
302 ((boundp 'temporary-file-directory) temporary-file-directory)
304 "Where mm will store its temporary files."
306 :group 'mime-display)
308 (defcustom mm-inline-large-images nil
309 "If non-nil, then all images fit in the buffer."
311 :group 'mime-display)
313 (defvar mm-file-name-rewrite-functions nil
314 "*List of functions used for rewriting file names of MIME parts.
315 Each function takes a file name as input and returns a file name.
317 Ready-made functions include
318 `mm-file-name-delete-whitespace',
319 `mm-file-name-trim-whitespace',
320 `mm-file-name-collapse-whitespace',
321 `mm-file-name-replace-whitespace',
322 `capitalize', `downcase', `upcase', and
325 (defvar mm-path-name-rewrite-functions nil
326 "*List of functions used for rewriting path names of MIME parts.
327 This is used when viewing parts externally , and is meant for
328 transforming the path name so that non-compliant programs can
329 find the file where it's saved.
331 Each function takes a file name as input and returns a file name.")
333 (defvar mm-file-name-replace-whitespace nil
334 "String used for replacing whitespace characters; default is `\"_\"'.")
336 (defcustom mm-default-directory nil
337 "The default directory where mm will save files.
338 If not set, `default-directory' will be used."
339 :type '(choice directory (const :tag "Default" nil))
340 :group 'mime-display)
342 (defcustom mm-external-terminal-program "xterm"
343 "The program to start an external terminal."
345 :group 'mime-display)
347 ;;; Internal variables.
349 (defvar mm-dissection-list nil)
350 (defvar mm-last-shell-command "")
351 (defvar mm-content-id-alist nil)
352 (defvar mm-postponed-undisplay-list nil)
354 ;; According to RFC2046, in particular, in a digest, the default
355 ;; Content-Type value for a body part is changed from "text/plain" to
357 (defvar mm-dissect-default-type "text/plain")
359 (autoload 'mml2015-verify "mml2015")
360 (autoload 'mml2015-verify-test "mml2015")
361 (autoload 'mml-smime-verify "mml-smime")
362 (autoload 'mml-smime-verify-test "mml-smime")
364 (defvar mm-verify-function-alist
365 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
366 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
367 mm-uu-pgp-signed-test)
368 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
369 mml-smime-verify-test)
370 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
371 mml-smime-verify-test)))
373 (defcustom mm-verify-option 'never
374 "Option of verifying signed parts.
375 `never', not verify; `always', always verify;
376 `known', only verify known protocols. Otherwise, ask user."
377 :type '(choice (item always)
379 (item :tag "only known protocols" known)
380 (item :tag "ask" nil))
381 :group 'mime-security)
383 (autoload 'mml2015-decrypt "mml2015")
384 (autoload 'mml2015-decrypt-test "mml2015")
386 (defvar mm-decrypt-function-alist
387 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
388 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
389 mm-uu-pgp-encrypted-test)))
391 (defcustom mm-decrypt-option nil
392 "Option of decrypting encrypted parts.
393 `never', not decrypt; `always', always decrypt;
394 `known', only decrypt known protocols. Otherwise, ask user."
395 :type '(choice (item always)
397 (item :tag "only known protocols" known)
398 (item :tag "ask" nil))
399 :group 'mime-security)
401 (defvar mm-viewer-completion-map
402 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
403 (set-keymap-parent map minibuffer-local-completion-map)
405 "Keymap for input viewer with completion.")
407 ;; Should we bind other key to minibuffer-complete-word?
408 (define-key mm-viewer-completion-map " " 'self-insert-command)
410 (defvar mm-viewer-completion-map
411 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
412 (set-keymap-parent map minibuffer-local-completion-map)
414 "Keymap for input viewer with completion.")
416 ;; Should we bind other key to minibuffer-complete-word?
417 (define-key mm-viewer-completion-map " " 'self-insert-command)
421 (defun mm-alist-to-plist (alist)
422 "Convert association list ALIST into the equivalent property-list form.
423 The plist is returned. This converts from
425 \((a . 1) (b . 2) (c . 3))
431 The original alist is not modified. See also `destructive-alist-to-plist'."
434 (let ((el (car alist)))
435 (setq plist (cons (cdr el) (cons (car el) plist))))
436 (setq alist (cdr alist)))
439 (defun mm-keep-viewer-alive-p (handle)
440 "Say whether external viewer for HANDLE should stay alive."
441 (let ((types mm-keep-viewer-alive-types)
442 (type (mm-handle-media-type handle))
445 (while (setq ty (pop types))
446 (when (string-match ty type)
447 (throw 'found t))))))
449 (defun mm-handle-set-external-undisplayer (handle function)
450 "Set the undisplayer for this handle; postpone undisplaying of viewers
451 for types in mm-keep-viewer-alive-types."
452 (if (mm-keep-viewer-alive-p handle)
453 (let ((new-handle (copy-sequence handle)))
454 (mm-handle-set-undisplayer new-handle function)
455 (mm-handle-set-undisplayer handle nil)
456 (push new-handle mm-postponed-undisplay-list))
457 (mm-handle-set-undisplayer handle function)))
459 (defun mm-destroy-postponed-undisplay-list ()
460 (when mm-postponed-undisplay-list
461 (message "Destroying external MIME viewers")
462 (mm-destroy-parts mm-postponed-undisplay-list)))
464 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
465 "Dissect the current buffer and return a list of MIME handles."
467 (let (ct ctl type subtype cte cd description id result from)
469 (mail-narrow-to-head)
470 (when (or no-strict-mime
472 (mail-fetch-field "mime-version"))
473 (setq ct (mail-fetch-field "content-type")
474 ctl (ignore-errors (mail-header-parse-content-type ct))
475 cte (mail-fetch-field "content-transfer-encoding")
476 cd (mail-fetch-field "content-disposition")
477 description (mail-fetch-field "content-description")
478 from (mail-fetch-field "from")
479 id (mail-fetch-field "content-id"))
480 ;; FIXME: In some circumstances, this code is running within
481 ;; an unibyte macro. mail-extract-address-components
482 ;; creates unibyte buffers. This `if', though not a perfect
483 ;; solution, avoids most of them.
485 (setq from (cadr (mail-extract-address-components from))))))
487 (setq cte (mail-header-strip cte)))
489 (not (string-match "/" (car ctl))))
490 (mm-dissect-singlepart
491 (list mm-dissect-default-type)
492 (and cte (intern (downcase (mail-header-remove-whitespace
493 (mail-header-remove-comments
496 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
498 (setq type (split-string (car ctl) "/"))
499 (setq subtype (cadr type)
504 ((equal type "multipart")
505 (let ((mm-dissect-default-type (if (equal subtype "digest")
508 (add-text-properties 0 (length (car ctl))
509 (mm-alist-to-plist (cdr ctl)) (car ctl))
511 ;; what really needs to be done here is a way to link a
512 ;; MIME handle back to it's parent MIME handle (in a multilevel
513 ;; MIME article). That would probably require changing
514 ;; the mm-handle API so we simply store the multipart buffert
515 ;; name as a text property of the "multipart/whatever" string.
516 (add-text-properties 0 (length (car ctl))
517 (list 'buffer (mm-copy-to-buffer))
519 (add-text-properties 0 (length (car ctl))
522 (cons (car ctl) (mm-dissect-multipart ctl))))
524 (mm-possibly-verify-or-decrypt
525 (mm-dissect-singlepart
527 (and cte (intern (downcase (mail-header-remove-whitespace
528 (mail-header-remove-comments
531 (and cd (ignore-errors
532 (mail-header-parse-content-disposition cd)))
536 (when (string-match " *<\\(.*\\)> *" id)
537 (setq id (match-string 1 id)))
538 (push (cons id result) mm-content-id-alist))
541 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
543 (if (equal "text/plain" (car ctl))
546 (let ((res (mm-make-handle
547 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
548 (push (car res) mm-dissection-list)
551 (defun mm-remove-all-parts ()
552 "Remove all MIME handles."
554 (mapcar 'mm-remove-part mm-dissection-list)
555 (setq mm-dissection-list nil))
557 (defun mm-dissect-multipart (ctl)
558 (goto-char (point-min))
559 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
560 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
563 (goto-char (point-max))
564 (if (re-search-backward close-delimiter nil t)
567 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
568 (while (and (< (point) end) (re-search-forward boundary end t))
569 (goto-char (match-beginning 0))
573 (narrow-to-region start (point))
574 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
576 (or (looking-at boundary)
578 (setq start (point)))
579 (when (and start (< start end))
582 (narrow-to-region start end)
583 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
584 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
586 (defun mm-copy-to-buffer ()
587 "Copy the contents of the current buffer to a fresh buffer."
589 (let ((obuf (current-buffer))
591 (goto-char (point-min))
592 (search-forward-regexp "^\n" nil t)
594 (set-buffer (generate-new-buffer " *mm*"))
595 (insert-buffer-substring obuf beg)
598 (defun mm-display-parts (handle &optional no-default)
599 (if (stringp (car handle))
600 (mapcar 'mm-display-parts (cdr handle))
601 (if (bufferp (car handle))
603 (narrow-to-region (point) (point))
604 (mm-display-part handle)
605 (goto-char (point-max)))
606 (mapcar 'mm-display-parts handle))))
608 (defun mm-display-part (handle &optional no-default)
609 "Display the MIME part represented by HANDLE.
610 Returns nil if the part is removed; inline if displayed inline;
611 external if displayed external."
613 (mailcap-parse-mailcaps)
614 (if (mm-handle-displayed-p handle)
615 (mm-remove-part handle)
616 (let* ((type (mm-handle-media-type handle))
617 (method (mailcap-mime-info type)))
618 (if (and (mm-inlinable-p handle)
619 (mm-inlined-p handle))
622 (mm-display-inline handle)
626 (if (and (not method)
627 (equal "text" (car (split-string type))))
630 (mm-insert-inline handle (mm-get-part handle))
633 handle (or method 'mailcap-save-binary-file)))))))))
635 (defun mm-display-external (handle method)
636 "Display HANDLE using METHOD."
637 (let ((outbuf (current-buffer)))
638 (mm-with-unibyte-buffer
639 (if (functionp method)
640 (let ((cur (current-buffer)))
641 (if (eq method 'mailcap-save-binary-file)
643 (set-buffer (generate-new-buffer " *mm*"))
645 (mm-insert-part handle)
646 (let ((win (get-buffer-window cur t)))
648 (select-window win)))
649 (switch-to-buffer (generate-new-buffer " *mm*")))
650 (buffer-disable-undo)
651 (mm-set-buffer-file-coding-system mm-binary-coding-system)
652 (insert-buffer-substring cur)
653 (goto-char (point-min))
654 (message "Viewing with %s" method)
655 (let ((mm (current-buffer))
656 (non-viewer (assq 'non-viewer
658 (mm-handle-media-type handle) t))))
662 (mm-save-part handle))
663 (when (and (not non-viewer)
665 (mm-handle-set-undisplayer handle mm)))))
666 ;; The function is a string to be executed.
667 (mm-insert-part handle)
668 (let* ((dir (mm-make-temp-file
669 (expand-file-name "emm." mm-tmp-directory) 'dir))
671 (mail-content-type-get
672 (mm-handle-disposition handle) 'filename)
673 (mail-content-type-get
674 (mm-handle-type handle) 'name)))
675 (mime-info (mailcap-mime-info
676 (mm-handle-media-type handle) t))
677 (needsterm (or (assoc "needsterm" mime-info)
678 (assoc "needsterminal" mime-info)))
679 (copiousoutput (assoc "copiousoutput" mime-info))
681 ;; We create a private sub-directory where we store our files.
682 (set-file-modes dir 448)
684 (setq file (expand-file-name
685 (gnus-map-function mm-file-name-rewrite-functions
686 (file-name-nondirectory filename))
688 (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
689 (let ((coding-system-for-write mm-binary-coding-system))
690 (write-region (point-min) (point-max) file nil 'nomesg))
691 (message "Viewing with %s" method)
696 (start-process "*display*" nil
697 mm-external-terminal-program
701 method file (mm-handle-type handle)))
712 (mm-handle-type handle)))))
715 (set-process-sentinel
716 (get-buffer-process buffer)
717 `(lambda (process state)
718 (if (eq 'exit (process-status process))
719 (gnus-configure-windows
720 ',gnus-current-window-configuration))))
721 (gnus-configure-windows 'display-term))
722 (mm-handle-set-external-undisplayer handle (cons file buffer)))
723 (message "Displaying %s..." (format method file))
726 (with-current-buffer outbuf
732 (call-process shell-file-name nil
734 (generate-new-buffer " *mm*"))
738 method file (mm-handle-type handle)))
739 (if (buffer-live-p buffer)
744 (ignore-errors (delete-file file))
745 (ignore-errors (delete-directory
746 (file-name-directory file)))
747 (ignore-errors (kill-buffer buffer))))))
751 (start-process "*display*"
753 (generate-new-buffer " *mm*"))
757 method file (mm-handle-type handle)))
758 (mm-handle-set-external-undisplayer
759 handle (cons file buffer)))
760 (message "Displaying %s..." (format method file))
763 (defun mm-mailcap-command (method file type-list)
764 (let ((ctl (cdr type-list))
768 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
769 (push (substring method beg (match-beginning 0)) out)
770 (setq beg (match-end 0)
771 total (match-string 0 method)
772 sub (match-string 1 method))
774 ((string= total "%%")
776 ((string= total "%s")
777 (setq uses-stdin nil)
779 (gnus-map-function mm-path-name-rewrite-functions file)) out))
780 ((string= total "%t")
781 (push (mm-quote-arg (car type-list)) out))
783 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
784 (push (substring method beg (length method)) out)
788 (gnus-map-function mm-path-name-rewrite-functions file))
790 (mapconcat 'identity (nreverse out) "")))
792 (defun mm-remove-parts (handles)
793 "Remove the displayed MIME parts represented by HANDLES."
794 (if (and (listp handles)
795 (bufferp (car handles)))
796 (mm-remove-part handles)
798 (while (setq handle (pop handles))
801 (when (buffer-live-p (get-text-property 0 'buffer handle))
802 (kill-buffer (get-text-property 0 'buffer handle))))
804 (stringp (car handle)))
805 (mm-remove-parts (cdr handle)))
807 (mm-remove-part handle)))))))
809 (defun mm-destroy-parts (handles)
810 "Remove the displayed MIME parts represented by HANDLES."
811 (if (and (listp handles)
812 (bufferp (car handles)))
813 (mm-destroy-part handles)
815 (while (setq handle (pop handles))
818 (when (buffer-live-p (get-text-property 0 'buffer handle))
819 (kill-buffer (get-text-property 0 'buffer handle))))
821 (stringp (car handle)))
822 (mm-destroy-parts handle))
824 (mm-destroy-part handle)))))))
826 (defun mm-remove-part (handle)
827 "Remove the displayed MIME part represented by HANDLE."
829 (let ((object (mm-handle-undisplayer handle)))
832 ;; Internally displayed part.
833 ((mm-annotationp object)
834 (delete-annotation object))
835 ((or (functionp object)
837 (eq (car object) 'lambda)))
839 ;; Externally displayed part.
841 (ignore-errors (delete-file (car object)))
842 (ignore-errors (delete-directory (file-name-directory (car object))))
843 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
845 (when (buffer-live-p object)
846 (kill-buffer object)))))
847 (mm-handle-set-undisplayer handle nil))))
849 (defun mm-display-inline (handle)
850 (let* ((type (mm-handle-media-type handle))
851 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
852 (funcall function handle)
853 (goto-char (point-min))))
855 (defun mm-assoc-string-match (alist type)
857 (when (string-match (car elem) type)
860 (defun mm-automatic-display-p (handle)
861 "Say whether the user wants HANDLE to be displayed automatically."
862 (let ((methods mm-automatic-display)
863 (type (mm-handle-media-type handle))
865 (while (setq method (pop methods))
866 (when (and (not (mm-inline-override-p handle))
867 (string-match method type))
872 (defun mm-inlinable-p (handle)
873 "Say whether HANDLE can be displayed inline."
874 (let ((alist mm-inline-media-tests)
875 (type (mm-handle-media-type handle))
878 (when (string-match (caar alist) type)
879 (setq test (caddar alist)
881 (setq test (funcall test handle)))
885 (defun mm-inlined-p (handle)
886 "Say whether the user wants HANDLE to be displayed inline."
887 (let ((methods mm-inlined-types)
888 (type (mm-handle-media-type handle))
890 (while (setq method (pop methods))
891 (when (and (not (mm-inline-override-p handle))
892 (string-match method type))
897 (defun mm-attachment-override-p (handle)
898 "Say whether HANDLE should have attachment behavior overridden."
899 (let ((types mm-attachment-override-types)
900 (type (mm-handle-media-type handle))
903 (while (setq ty (pop types))
904 (when (and (string-match ty type)
905 (mm-inlinable-p handle))
906 (throw 'found t))))))
908 (defun mm-inline-override-p (handle)
909 "Say whether HANDLE should have inline behavior overridden."
910 (let ((types mm-inline-override-types)
911 (type (mm-handle-media-type handle))
914 (while (setq ty (pop types))
915 (when (string-match ty type)
916 (throw 'found t))))))
918 (defun mm-automatic-external-display-p (type)
919 "Return the user-defined method for TYPE."
920 (let ((methods mm-automatic-external-display)
922 (while (setq method (pop methods))
923 (when (string-match method type)
928 (defun mm-destroy-part (handle)
929 "Destroy the data structures connected to HANDLE."
931 (mm-remove-part handle)
932 (when (buffer-live-p (mm-handle-buffer handle))
933 (kill-buffer (mm-handle-buffer handle)))))
935 (defun mm-handle-displayed-p (handle)
936 "Say whether HANDLE is displayed or not."
937 (mm-handle-undisplayer handle))
940 ;;; Functions for outputting parts
943 (defun mm-get-part (handle)
944 "Return the contents of HANDLE as a string."
945 (mm-with-unibyte-buffer
946 (insert (with-current-buffer (mm-handle-buffer handle)
947 (mm-with-unibyte-current-buffer-mule4
949 (mm-decode-content-transfer-encoding
950 (mm-handle-encoding handle)
951 (mm-handle-media-type handle))
954 (defun mm-insert-part (handle)
955 "Insert the contents of HANDLE in the current buffer."
956 (let ((cur (current-buffer)))
958 (if (member (mm-handle-media-supertype handle) '("text" "message"))
960 (insert-buffer-substring (mm-handle-buffer handle))
962 (mm-decode-content-transfer-encoding
963 (mm-handle-encoding handle)
964 (mm-handle-media-type handle))
965 (let ((temp (current-buffer)))
967 (insert-buffer-substring temp))))
968 (mm-with-unibyte-buffer
969 (insert-buffer-substring (mm-handle-buffer handle))
971 (mm-decode-content-transfer-encoding
972 (mm-handle-encoding handle)
973 (mm-handle-media-type handle))
974 (let ((temp (current-buffer)))
976 (insert-buffer-substring temp))))))))
978 (defun mm-file-name-delete-whitespace (file-name)
979 "Remove all whitespace characters from FILE-NAME."
980 (while (string-match "\\s-+" file-name)
981 (setq file-name (replace-match "" t t file-name)))
984 (defun mm-file-name-trim-whitespace (file-name)
985 "Remove leading and trailing whitespace characters from FILE-NAME."
986 (when (string-match "\\`\\s-+" file-name)
987 (setq file-name (substring file-name (match-end 0))))
988 (when (string-match "\\s-+\\'" file-name)
989 (setq file-name (substring file-name 0 (match-beginning 0))))
992 (defun mm-file-name-collapse-whitespace (file-name)
993 "Collapse multiple whitespace characters in FILE-NAME."
994 (while (string-match "\\s-\\s-+" file-name)
995 (setq file-name (replace-match " " t t file-name)))
998 (defun mm-file-name-replace-whitespace (file-name)
999 "Replace whitespace characters in FILE-NAME with underscores.
1000 Set `mm-file-name-replace-whitespace' to any other string if you do not
1002 (let ((s (or mm-file-name-replace-whitespace "_")))
1003 (while (string-match "\\s-" file-name)
1004 (setq file-name (replace-match s t t file-name))))
1007 (defun mm-save-part (handle)
1008 "Write HANDLE to a file."
1009 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1010 (filename (mail-content-type-get
1011 (mm-handle-disposition handle) 'filename))
1014 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1015 (file-name-nondirectory filename))))
1017 (read-file-name "Save MIME part to: "
1019 (or filename name "")
1020 (or mm-default-directory default-directory))))
1021 (setq mm-default-directory (file-name-directory file))
1022 (and (or (not (file-exists-p file))
1023 (yes-or-no-p (format "File %s already exists; overwrite? "
1026 (mm-save-part-to-file handle file)
1029 (defun mm-save-part-to-file (handle file)
1030 (mm-with-unibyte-buffer
1031 (mm-insert-part handle)
1032 (let ((coding-system-for-write 'binary)
1033 ;; Don't re-compress .gz & al. Arguably we should make
1034 ;; `file-name-handler-alist' nil, but that would chop
1035 ;; ange-ftp, which is reasonable to use here.
1036 (inhibit-file-name-operation 'write-region)
1037 (inhibit-file-name-handlers
1038 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1039 (write-region (point-min) (point-max) file))))
1041 (defun mm-pipe-part (handle)
1042 "Pipe HANDLE to a process."
1043 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1045 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1046 (mm-with-unibyte-buffer
1047 (mm-insert-part handle)
1048 (let ((coding-system-for-write 'binary))
1049 (shell-command-on-region (point-min) (point-max) command nil)))))
1051 (defun mm-interactively-view-part (handle)
1052 "Display HANDLE using METHOD."
1053 (let* ((type (mm-handle-media-type handle))
1055 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1056 (mailcap-mime-info type 'all)))
1057 (method (let ((minibuffer-local-completion-map
1058 mm-viewer-completion-map))
1059 (completing-read "Viewer: " methods))))
1060 (when (string= method "")
1061 (error "No method given"))
1062 (if (string-match "^[^% \t]+$" method)
1063 (setq method (concat method " %s")))
1064 (mm-display-external handle method)))
1066 (defun mm-preferred-alternative (handles &optional preferred)
1067 "Say which of HANDLES are preferred."
1068 (let ((prec (if preferred (list preferred)
1069 (mm-preferred-alternative-precedence handles)))
1070 p h result type handle)
1071 (while (setq p (pop prec))
1074 (setq handle (car h))
1075 (setq type (mm-handle-media-type handle))
1076 (when (and (equal p type)
1077 (mm-automatic-display-p handle)
1078 (or (stringp (car handle))
1079 (not (mm-handle-disposition handle))
1080 (equal (car (mm-handle-disposition handle))
1088 (defun mm-preferred-alternative-precedence (handles)
1089 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1090 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1092 (dolist (disc (reverse mm-discouraged-alternatives))
1093 (dolist (elem (copy-sequence seq))
1094 (when (string-match disc elem)
1095 (setq seq (nconc (delete elem seq) (list elem))))))
1098 (defun mm-get-content-id (id)
1099 "Return the handle(s) referred to by ID."
1100 (cdr (assoc id mm-content-id-alist)))
1102 (defconst mm-image-type-regexps
1103 '(("/\\*.*XPM.\\*/" . xpm)
1107 ("\211PNG\r\n" . png)
1109 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1110 ("%!PS" . postscript))
1111 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1112 When the first bytes of an image file match REGEXP, it is assumed to
1113 be of image type IMAGE-TYPE.")
1115 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1116 (defun mm-image-type-from-buffer ()
1117 "Determine the image type from data in the current buffer.
1118 Value is a symbol specifying the image type or nil if type cannot
1120 (let ((types mm-image-type-regexps)
1122 (goto-char (point-min))
1123 (while (and types (null type))
1124 (let ((regexp (car (car types)))
1125 (image-type (cdr (car types))))
1126 (when (looking-at regexp)
1127 (setq type image-type))
1128 (setq types (cdr types))))
1131 (defun mm-get-image (handle)
1132 "Return an image instance based on HANDLE."
1133 (let ((type (mm-handle-media-subtype handle))
1135 ;; Allow some common translations.
1138 ((equal type "x-pixmap")
1140 ((equal type "x-xbitmap")
1142 ((equal type "x-portable-bitmap")
1145 (or (mm-handle-cache handle)
1146 (mm-with-unibyte-buffer
1147 (mm-insert-part handle)
1151 ;; Avoid testing `make-glyph' since W3 may define
1152 ;; a bogus version of it.
1153 (if (fboundp 'create-image)
1154 (create-image (buffer-string)
1155 (or (mm-image-type-from-buffer)
1158 (mm-create-image-xemacs type))))
1159 (mm-handle-set-cache handle spec))))))
1161 (defun mm-create-image-xemacs (type)
1164 ;; xbm images require special handling, since
1165 ;; the only way to create glyphs from these
1166 ;; (without a ton of work) is to write them
1167 ;; out to a file, and then create a file
1169 (let ((file (mm-make-temp-file
1170 (expand-file-name "emm.xbm"
1171 mm-tmp-directory))))
1174 (write-region (point-min) (point-max) file)
1175 (make-glyph (list (cons 'x file))))
1177 (delete-file file)))))
1181 (or (mm-image-type-from-buffer)
1183 :data (buffer-string))))))
1185 (defun mm-image-fit-p (handle)
1186 "Say whether the image in HANDLE will fit the current window."
1187 (let ((image (mm-get-image handle)))
1188 (if (fboundp 'glyph-width)
1189 ;; XEmacs' glyphs can actually tell us about their width, so
1190 ;; lets be nice and smart about them.
1191 (or mm-inline-large-images
1192 (and (< (glyph-width image) (window-pixel-width))
1193 (< (glyph-height image) (window-pixel-height))))
1194 (let* ((size (image-size image))
1197 (or mm-inline-large-images
1198 (and (< h (1- (window-height))) ; Don't include mode line.
1199 (< w (window-width))))))))
1201 (defun mm-valid-image-format-p (format)
1202 "Say whether FORMAT can be displayed natively by Emacs."
1205 ((fboundp 'valid-image-instantiator-format-p)
1206 (valid-image-instantiator-format-p format))
1208 ((fboundp 'image-type-available-p)
1209 (and (display-graphic-p)
1210 (image-type-available-p format)))
1211 ;; Nobody else can do images yet.
1215 (defun mm-valid-and-fit-image-p (format handle)
1216 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1217 (and (mm-valid-image-format-p format)
1218 (mm-image-fit-p handle)))
1220 (defun mm-find-part-by-type (handles type &optional notp recursive)
1221 "Search in HANDLES for part with TYPE.
1222 If NOTP, returns first non-matching part.
1223 If RECURSIVE, search recursively."
1226 (if (and recursive (stringp (caar handles)))
1227 (if (setq handle (mm-find-part-by-type (cdar handles) type
1231 (not (equal (mm-handle-media-type (car handles)) type))
1232 (equal (mm-handle-media-type (car handles)) type))
1233 (setq handle (car handles)
1235 (setq handles (cdr handles)))
1238 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1239 (goto-char (point-min))
1240 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1242 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1244 (end (save-excursion
1245 (goto-char (point-max))
1246 (if (re-search-backward close-delimiter nil t)
1250 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1251 (while (and (not result)
1252 (re-search-forward boundary end t))
1253 (goto-char (match-beginning 0))
1257 (narrow-to-region start (1- (point)))
1258 (when (let ((ctl (ignore-errors
1259 (mail-header-parse-content-type
1260 (mail-fetch-field "content-type")))))
1262 (not (equal (car ctl) type))
1263 (equal (car ctl) type)))
1264 (setq result (buffer-substring (point-min) (point-max)))))))
1266 (setq start (point)))
1267 (when (and (not result) start)
1270 (narrow-to-region start end)
1271 (when (let ((ctl (ignore-errors
1272 (mail-header-parse-content-type
1273 (mail-fetch-field "content-type")))))
1275 (not (equal (car ctl) type))
1276 (equal (car ctl) type)))
1277 (setq result (buffer-substring (point-min) (point-max)))))))
1280 (defvar mm-security-handle nil)
1282 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1283 ;; HANDLE could be a CTL.
1285 (put-text-property 0 (length (car handle)) parameter value
1288 (defun mm-possibly-verify-or-decrypt (parts ctl)
1289 (let ((type (car ctl))
1290 (subtype (cadr (split-string (car ctl) "/")))
1291 (mm-security-handle ctl) ;; (car CTL) is the type.
1292 protocol func functest)
1294 ((or (equal type "application/x-pkcs7-mime")
1295 (equal type "application/pkcs7-mime"))
1298 ((eq mm-decrypt-option 'never) nil)
1299 ((eq mm-decrypt-option 'always) t)
1300 ((eq mm-decrypt-option 'known) t)
1302 (format "Decrypt (S/MIME) part? "))))
1303 (mm-view-pkcs7 parts))
1304 (setq parts (mm-dissect-buffer t)))))
1305 ((equal subtype "signed")
1306 (unless (and (setq protocol
1307 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1308 (not (equal protocol "multipart/mixed")))
1309 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1310 (let ((protocols mm-verify-function-alist))
1312 (if (and (or (not (setq functest (nth 3 (car protocols))))
1313 (funcall functest parts ctl))
1314 (mm-find-part-by-type parts (caar protocols) nil t))
1315 (setq protocol (caar protocols)
1317 (setq protocols (cdr protocols))))))
1318 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1320 ((eq mm-verify-option 'never) nil)
1321 ((eq mm-verify-option 'always) t)
1322 ((eq mm-verify-option 'known)
1324 (or (not (setq functest
1325 (nth 3 (assoc protocol
1326 mm-verify-function-alist))))
1327 (funcall functest parts ctl))))
1329 (format "Verify signed (%s) part? "
1330 (or (nth 2 (assoc protocol mm-verify-function-alist))
1331 (format "protocol=%s" protocol))))))
1334 (funcall func parts ctl)
1335 (mm-set-handle-multipart-parameter
1336 mm-security-handle 'gnus-details
1337 (format "Unknown sign protocol (%s)" protocol))))))
1338 ((equal subtype "encrypted")
1339 (unless (setq protocol
1340 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1341 ;; The message is broken.
1342 (let ((parts parts))
1344 (if (assoc (mm-handle-media-type (car parts))
1345 mm-decrypt-function-alist)
1346 (setq protocol (mm-handle-media-type (car parts))
1348 (setq parts (cdr parts))))))
1349 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1351 ((eq mm-decrypt-option 'never) nil)
1352 ((eq mm-decrypt-option 'always) t)
1353 ((eq mm-decrypt-option 'known)
1355 (or (not (setq functest
1356 (nth 3 (assoc protocol
1357 mm-decrypt-function-alist))))
1358 (funcall functest parts ctl))))
1360 (format "Decrypt (%s) part? "
1361 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1362 (format "protocol=%s" protocol))))))
1365 (setq parts (funcall func parts ctl))
1366 (mm-set-handle-multipart-parameter
1367 mm-security-handle 'gnus-details
1368 (format "Unknown encrypt protocol (%s)" protocol))))))
1372 (defun mm-multiple-handles (handles)
1373 (and (listp (car handles))
1374 (> (length handles) 1)))
1376 (defun mm-merge-handles (handles1 handles2)
1378 (if (listp (car handles1))
1381 (if (listp (car handles2))
1385 (defun mm-readable-p (handle)
1386 "Say whether the content of HANDLE is readable."
1387 (and (< (with-current-buffer (mm-handle-buffer handle)
1388 (buffer-size)) 10000)
1389 (mm-with-unibyte-buffer
1390 (mm-insert-part handle)
1391 (and (eq (mm-body-7-or-8) '7bit)
1392 (not (mm-long-lines-p 76))))))
1394 (provide 'mm-decode)
1396 ;;; mm-decode.el ends here