1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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.
25 ;; Jaap-Henk Hoepman (jhh@xs4all.nl):
27 ;; Added support for delayed destroy of external MIME viewers. All external
28 ;; viewers for mime types in mm-keep-viewer-alive-types will remain active
29 ;; after switching articles or groups, and will only be removed when exiting
36 (require 'gnus-mailcap)
38 (eval-when-compile (require 'cl)
42 (autoload 'mm-inline-partial "mm-partial")
43 (autoload 'mm-inline-external-body "mm-extern")
44 (autoload 'mm-insert-inline "mm-view"))
46 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
48 (defgroup mime-display ()
49 "Display of MIME in mail and news articles."
50 :link '(custom-manual "(emacs-mime)Customization")
56 (defgroup mime-security ()
57 "MIME security in mail and news articles."
58 :link '(custom-manual "(emacs-mime)Customization")
63 ;;; Convenience macros.
65 (defmacro mm-handle-buffer (handle)
67 (defmacro mm-handle-type (handle)
69 (defsubst mm-handle-media-type (handle)
70 (if (stringp (car handle))
72 (car (mm-handle-type handle))))
73 (defsubst mm-handle-media-supertype (handle)
74 (car (split-string (mm-handle-media-type handle) "/")))
75 (defsubst mm-handle-media-subtype (handle)
76 (cadr (split-string (mm-handle-media-type handle) "/")))
77 (defmacro mm-handle-encoding (handle)
79 (defmacro mm-handle-undisplayer (handle)
81 (defmacro mm-handle-set-undisplayer (handle function)
82 `(setcar (nthcdr 3 ,handle) ,function))
83 (defmacro mm-handle-disposition (handle)
85 (defmacro mm-handle-description (handle)
87 (defmacro mm-handle-cache (handle)
89 (defmacro mm-handle-set-cache (handle contents)
90 `(setcar (nthcdr 6 ,handle) ,contents))
91 (defmacro mm-handle-id (handle)
93 (defmacro mm-handle-multipart-original-buffer (handle)
94 `(get-text-property 0 'buffer (car ,handle)))
95 (defmacro mm-handle-multipart-from (handle)
96 `(get-text-property 0 'from (car ,handle)))
97 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
98 `(get-text-property 0 ,parameter (car ,handle)))
100 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
101 disposition description cache
103 `(list ,buffer ,type ,encoding ,undisplayer
104 ,disposition ,description ,cache ,id))
106 (defcustom mm-inline-media-tests
110 (mm-valid-and-fit-image-p 'jpeg handle)))
114 (mm-valid-and-fit-image-p 'png handle)))
118 (mm-valid-and-fit-image-p 'gif handle)))
122 (mm-valid-and-fit-image-p 'tiff handle)) )
126 (mm-valid-and-fit-image-p 'xbm handle)))
130 (mm-valid-and-fit-image-p 'xbm handle)))
134 (mm-valid-and-fit-image-p 'xpm handle)))
138 (mm-valid-and-fit-image-p 'xpm handle)))
142 (mm-valid-and-fit-image-p 'bmp handle)))
143 ("image/x-portable-bitmap"
146 (mm-valid-and-fit-image-p 'pbm handle)))
147 ("text/plain" mm-inline-text identity)
148 ("text/enriched" mm-inline-text identity)
149 ("text/richtext" mm-inline-text identity)
150 ("text/x-patch" mm-display-patch-inline
152 (locate-library "diff-mode")))
153 ("application/emacs-lisp" mm-display-elisp-inline identity)
157 (locate-library "w3")))
161 (or (featurep 'vcard)
162 (locate-library "vcard"))))
163 ("message/delivery-status" mm-inline-text identity)
164 ("message/rfc822" mm-inline-message identity)
165 ("message/partial" mm-inline-partial identity)
166 ("message/external-body" mm-inline-external-body identity)
167 ("text/.*" mm-inline-text identity)
168 ("audio/wav" mm-inline-audio
170 (and (or (featurep 'nas-sound) (featurep 'native-sound))
171 (device-sound-enabled-p))))
175 (and (or (featurep 'nas-sound) (featurep 'native-sound))
176 (device-sound-enabled-p))))
177 ("application/pgp-signature" ignore identity)
178 ("application/x-pkcs7-signature" ignore identity)
179 ("application/pkcs7-signature" ignore identity)
180 ("multipart/alternative" ignore identity)
181 ("multipart/mixed" ignore identity)
182 ("multipart/related" ignore identity)
183 ;; Default to displaying as text
184 (".*" mm-inline-text identity))
185 "Alist of media types/tests saying whether types can be displayed inline."
186 :type '(repeat (list (string :tag "MIME type")
187 (function :tag "Display function")
188 (function :tag "Display test")))
189 :group 'mime-display)
191 (defcustom mm-inlined-types
192 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
193 "message/partial" "message/external-body" "application/emacs-lisp"
194 "application/pgp-signature" "application/x-pkcs7-signature"
195 "application/pkcs7-signature")
196 "List of media types that are to be displayed inline.
197 See also `mm-inline-media-tests', which says how to display a media
199 :type '(repeat string)
200 :group 'mime-display)
202 (defcustom mm-keep-viewer-alive-types
203 '("application/postscript" "application/msword" "application/vnd.ms-excel"
204 "application/pdf" "application/x-dvi")
205 "List of media types for which the external viewer will not be killed
206 when selecting a different article."
207 :type '(repeat string)
208 :group 'mime-display)
210 (defcustom mm-automatic-display
211 '("text/plain" "text/enriched" "text/richtext" "text/html"
212 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
213 "message/rfc822" "text/x-patch" "application/pgp-signature"
214 "application/emacs-lisp" "application/x-pkcs7-signature"
215 "application/pkcs7-signature")
216 "A list of MIME types to be displayed automatically."
217 :type '(repeat string)
218 :group 'mime-display)
220 (defcustom mm-attachment-override-types '("text/x-vcard")
221 "Types to have \"attachment\" ignored if they can be displayed inline."
222 :type '(repeat string)
223 :group 'mime-display)
225 (defcustom mm-inline-override-types nil
226 "Types to be treated as attachments even if they can be displayed inline."
227 :type '(repeat string)
228 :group 'mime-display)
230 (defcustom mm-automatic-external-display nil
231 "List of MIME type regexps that will be displayed externally automatically."
232 :type '(repeat string)
233 :group 'mime-display)
235 (defcustom mm-discouraged-alternatives nil
236 "List of MIME types that are discouraged when viewing multipart/alternative.
237 Viewing agents are supposed to view the last possible part of a message,
238 as that is supposed to be the richest. However, users may prefer other
239 types instead, and this list says what types are most unwanted. If,
240 for instance, text/html parts are very unwanted, and text/richtext are
241 somewhat unwanted, then the value of this variable should be set
244 (\"text/html\" \"text/richtext\")"
245 :type '(repeat string)
246 :group 'mime-display)
248 (defcustom mm-tmp-directory
249 (cond ((fboundp 'temp-directory) (temp-directory))
250 ((boundp 'temporary-file-directory) temporary-file-directory)
252 "Where mm will store its temporary files."
254 :group 'mime-display)
256 (defcustom mm-inline-large-images nil
257 "If non-nil, then all images fit in the buffer."
259 :group 'mime-display)
261 (defvar mm-file-name-rewrite-functions nil
262 "*List of functions used for rewriting file names of MIME parts.
263 Each function takes a file name as input and returns a file name.
265 Ready-made functions include
266 `mm-file-name-delete-whitespace',
267 `mm-file-name-trim-whitespace',
268 `mm-file-name-collapse-whitespace',
269 `mm-file-name-replace-whitespace',
270 `capitalize', `downcase', `upcase', and
273 (defvar mm-file-name-replace-whitespace nil
274 "String used for replacing whitespace characters; default is `\"_\"'.")
276 (defcustom mm-default-directory nil
277 "The default directory where mm will save files.
278 If not set, `default-directory' will be used."
280 :group 'mime-display)
282 (defcustom mm-external-terminal-program "xterm"
283 "The program to start an external terminal."
285 :group 'mime-display)
287 ;;; Internal variables.
289 (defvar mm-dissection-list nil)
290 (defvar mm-last-shell-command "")
291 (defvar mm-content-id-alist nil)
292 (defvar mm-postponed-undisplay-list nil)
294 ;; According to RFC2046, in particular, in a digest, the default
295 ;; Content-Type value for a body part is changed from "text/plain" to
297 (defvar mm-dissect-default-type "text/plain")
299 (autoload 'mml2015-verify "mml2015")
300 (autoload 'mml2015-verify-test "mml2015")
301 (autoload 'mml-smime-verify "mml-smime")
302 (autoload 'mml-smime-verify-test "mml-smime")
304 (defvar mm-verify-function-alist
305 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
306 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
307 mm-uu-pgp-signed-test)
308 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
309 mml-smime-verify-test)
310 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
311 mml-smime-verify-test)))
313 (defcustom mm-verify-option 'never
314 "Option of verifying signed parts.
315 `never', not verify; `always', always verify;
316 `known', only verify known protocols. Otherwise, ask user."
317 :type '(choice (item always)
319 (item :tag "only known protocols" known)
320 (item :tag "ask" nil))
321 :group 'mime-security)
323 (autoload 'mml2015-decrypt "mml2015")
324 (autoload 'mml2015-decrypt-test "mml2015")
326 (defvar mm-decrypt-function-alist
327 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
328 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
329 mm-uu-pgp-encrypted-test)))
331 (defcustom mm-decrypt-option nil
332 "Option of decrypting encrypted parts.
333 `never', not decrypt; `always', always decrypt;
334 `known', only decrypt known protocols. Otherwise, ask user."
335 :type '(choice (item always)
337 (item :tag "only known protocols" known)
338 (item :tag "ask" nil))
339 :group 'mime-security)
341 (defvar mm-viewer-completion-map
342 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
343 (set-keymap-parent map minibuffer-local-completion-map)
345 "Keymap for input viewer with completion.")
347 ;; Should we bind other key to minibuffer-complete-word?
348 (define-key mm-viewer-completion-map " " 'self-insert-command)
350 (defvar mm-viewer-completion-map
351 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
352 (set-keymap-parent map minibuffer-local-completion-map)
354 "Keymap for input viewer with completion.")
356 ;; Should we bind other key to minibuffer-complete-word?
357 (define-key mm-viewer-completion-map " " 'self-insert-command)
361 (defun mm-alist-to-plist (alist)
362 "Convert association list ALIST into the equivalent property-list form.
363 The plist is returned. This converts from
365 \((a . 1) (b . 2) (c . 3))
371 The original alist is not modified. See also `destructive-alist-to-plist'."
374 (let ((el (car alist)))
375 (setq plist (cons (cdr el) (cons (car el) plist))))
376 (setq alist (cdr alist)))
379 (defun mm-keep-viewer-alive-p (handle)
380 "Say whether external viewer for HANDLE should stay alive."
381 (let ((types mm-keep-viewer-alive-types)
382 (type (mm-handle-media-type handle))
385 (while (setq ty (pop types))
386 (when (string-match ty type)
387 (throw 'found t))))))
389 (defun mm-handle-set-external-undisplayer (handle function)
390 "Set the undisplayer for this handle; postpone undisplaying of viewers
391 for types in mm-keep-viewer-alive-types."
392 (if (mm-keep-viewer-alive-p handle)
393 (let ((new-handle (copy-sequence handle)))
394 (mm-handle-set-undisplayer new-handle function)
395 (mm-handle-set-undisplayer handle nil)
396 (push new-handle mm-postponed-undisplay-list))
397 (mm-handle-set-undisplayer handle function)))
399 (defun mm-destroy-postponed-undisplay-list ()
400 (message "Destroying external MIME viewers")
401 (mm-destroy-parts mm-postponed-undisplay-list))
403 (defun mm-dissect-buffer (&optional no-strict-mime)
404 "Dissect the current buffer and return a list of MIME handles."
406 (let (ct ctl type subtype cte cd description id result from)
408 (mail-narrow-to-head)
409 (when (or no-strict-mime
410 (mail-fetch-field "mime-version"))
411 (setq ct (mail-fetch-field "content-type")
412 ctl (ignore-errors (mail-header-parse-content-type ct))
413 cte (mail-fetch-field "content-transfer-encoding")
414 cd (mail-fetch-field "content-disposition")
415 description (mail-fetch-field "content-description")
416 from (mail-fetch-field "from")
417 id (mail-fetch-field "content-id"))
418 ;; FIXME: In some circumstances, this code is running within
419 ;; an unibyte macro. mail-extract-address-components
420 ;; creates unibyte buffers. This `if', though not a perfect
421 ;; solution, avoids most of them.
423 (setq from (cadr (mail-extract-address-components from))))))
425 (setq cte (mail-header-strip cte)))
427 (not (string-match "/" (car ctl))))
428 (mm-dissect-singlepart
429 (list mm-dissect-default-type)
430 (and cte (intern (downcase (mail-header-remove-whitespace
431 (mail-header-remove-comments
434 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
436 (setq type (split-string (car ctl) "/"))
437 (setq subtype (cadr type)
442 ((equal type "multipart")
443 (let ((mm-dissect-default-type (if (equal subtype "digest")
446 (add-text-properties 0 (length (car ctl))
447 (mm-alist-to-plist (cdr ctl)) (car ctl))
449 ;; what really needs to be done here is a way to link a
450 ;; MIME handle back to it's parent MIME handle (in a multilevel
451 ;; MIME article). That would probably require changing
452 ;; the mm-handle API so we simply store the multipart buffert
453 ;; name as a text property of the "multipart/whatever" string.
454 (add-text-properties 0 (length (car ctl))
455 (list 'buffer (mm-copy-to-buffer))
457 (add-text-properties 0 (length (car ctl))
460 (cons (car ctl) (mm-dissect-multipart ctl))))
462 (mm-dissect-singlepart
464 (and cte (intern (downcase (mail-header-remove-whitespace
465 (mail-header-remove-comments
468 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
471 (when (string-match " *<\\(.*\\)> *" id)
472 (setq id (match-string 1 id)))
473 (push (cons id result) mm-content-id-alist))
476 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
478 (if (equal "text/plain" (car ctl))
481 (let ((res (mm-make-handle
482 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
483 (push (car res) mm-dissection-list)
486 (defun mm-remove-all-parts ()
487 "Remove all MIME handles."
489 (mapcar 'mm-remove-part mm-dissection-list)
490 (setq mm-dissection-list nil))
492 (defun mm-dissect-multipart (ctl)
493 (goto-char (point-min))
494 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
495 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
498 (goto-char (point-max))
499 (if (re-search-backward close-delimiter nil t)
502 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
503 (while (and (< (point) end) (re-search-forward boundary end t))
504 (goto-char (match-beginning 0))
508 (narrow-to-region start (point))
509 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
511 (setq start (point)))
512 (when (and start (< start end))
515 (narrow-to-region start end)
516 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
517 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
519 (defun mm-copy-to-buffer ()
520 "Copy the contents of the current buffer to a fresh buffer."
522 (let ((flag enable-multibyte-characters)
523 (new-buffer (generate-new-buffer " *mm*")))
524 (goto-char (point-min))
525 (search-forward-regexp "^\n" nil t)
527 (narrow-to-region (point) (point-max))
529 (set-buffer-multibyte nil))
530 (copy-to-buffer new-buffer (point-min) (point-max))
532 (set-buffer-multibyte t)))
535 (defun mm-display-parts (handle &optional no-default)
536 (if (stringp (car handle))
537 (mapcar 'mm-display-parts (cdr handle))
538 (if (bufferp (car handle))
540 (narrow-to-region (point) (point))
541 (mm-display-part handle)
542 (goto-char (point-max)))
543 (mapcar 'mm-display-parts handle))))
545 (defun mm-display-part (handle &optional no-default)
546 "Display the MIME part represented by HANDLE.
547 Returns nil if the part is removed; inline if displayed inline;
548 external if displayed external."
550 (mailcap-parse-mailcaps)
551 (if (mm-handle-displayed-p handle)
552 (mm-remove-part handle)
553 (let* ((type (mm-handle-media-type handle))
554 (method (mailcap-mime-info type)))
555 (if (and (mm-inlinable-p handle)
556 (mm-inlined-p handle))
559 (mm-display-inline handle)
563 (if (and (not method)
564 (equal "text" (car (split-string type))))
567 (mm-insert-inline handle (mm-get-part handle))
570 handle (or method 'mailcap-save-binary-file)))))))))
572 (defun mm-display-external (handle method)
573 "Display HANDLE using METHOD."
574 (let ((outbuf (current-buffer)))
575 (mm-with-unibyte-buffer
576 (if (functionp method)
577 (let ((cur (current-buffer)))
578 (if (eq method 'mailcap-save-binary-file)
580 (set-buffer (generate-new-buffer " *mm*"))
582 (mm-insert-part handle)
583 (let ((win (get-buffer-window cur t)))
585 (select-window win)))
586 (switch-to-buffer (generate-new-buffer " *mm*")))
587 (buffer-disable-undo)
588 (mm-set-buffer-file-coding-system mm-binary-coding-system)
589 (insert-buffer-substring cur)
590 (goto-char (point-min))
591 (message "Viewing with %s" method)
592 (let ((mm (current-buffer))
593 (non-viewer (assq 'non-viewer
595 (mm-handle-media-type handle) t))))
599 (mm-save-part handle))
600 (when (and (not non-viewer)
602 (mm-handle-set-undisplayer handle mm)))))
603 ;; The function is a string to be executed.
604 (mm-insert-part handle)
605 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
606 (filename (mail-content-type-get
607 (mm-handle-disposition handle) 'filename))
608 (mime-info (mailcap-mime-info
609 (mm-handle-media-type handle) t))
610 (needsterm (or (assoc "needsterm" mime-info)
611 (assoc "needsterminal" mime-info)))
612 (copiousoutput (assoc "copiousoutput" mime-info))
614 ;; We create a private sub-directory where we store our files.
616 (set-file-modes dir 448)
618 (setq file (expand-file-name (file-name-nondirectory filename)
620 (setq file (make-temp-name (expand-file-name "mm." dir))))
621 (let ((coding-system-for-write mm-binary-coding-system))
622 (write-region (point-min) (point-max) file nil 'nomesg))
623 (message "Viewing with %s" method)
627 (start-process "*display*" nil
628 mm-external-terminal-program
632 method file (mm-handle-type handle)))
643 (mm-handle-type handle)))))
646 (set-process-sentinel
647 (get-buffer-process buffer)
648 `(lambda (process state)
649 (if (eq 'exit (process-status process))
650 (gnus-configure-windows
651 ',gnus-current-window-configuration))))
652 (gnus-configure-windows 'display-term))
653 (mm-handle-set-external-undisplayer handle (cons file buffer)))
654 (message "Displaying %s..." (format method file))
657 (with-current-buffer outbuf
663 (call-process shell-file-name nil
665 (generate-new-buffer " *mm*"))
669 method file (mm-handle-type handle)))
670 (if (buffer-live-p buffer)
675 (ignore-errors (delete-file file))
676 (ignore-errors (delete-directory
677 (file-name-directory file)))
678 (ignore-errors (kill-buffer buffer))))))
682 (start-process "*display*"
684 (generate-new-buffer " *mm*"))
688 method file (mm-handle-type handle)))
689 (mm-handle-set-external-undisplayer handle (cons file buffer)))
690 (message "Displaying %s..." (format method file))
693 (defun mm-mailcap-command (method file type-list)
694 (let ((ctl (cdr type-list))
698 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
699 (push (substring method beg (match-beginning 0)) out)
700 (setq beg (match-end 0)
701 total (match-string 0 method)
702 sub (match-string 1 method))
704 ((string= total "%%")
706 ((string= total "%s")
707 (setq uses-stdin nil)
708 (push (mm-quote-arg file) out))
709 ((string= total "%t")
710 (push (mm-quote-arg (car type-list)) out))
712 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
713 (push (substring method beg (length method)) out)
717 (push (mm-quote-arg file) out)))
718 (mapconcat 'identity (nreverse out) "")))
720 (defun mm-remove-parts (handles)
721 "Remove the displayed MIME parts represented by HANDLES."
722 (if (and (listp handles)
723 (bufferp (car handles)))
724 (mm-remove-part handles)
726 (while (setq handle (pop handles))
729 (when (buffer-live-p (get-text-property 0 'buffer handle))
730 (kill-buffer (get-text-property 0 'buffer handle))))
732 (stringp (car handle)))
733 (mm-remove-parts (cdr handle)))
735 (mm-remove-part handle)))))))
737 (defun mm-destroy-parts (handles)
738 "Remove the displayed MIME parts represented by HANDLES."
739 (if (and (listp handles)
740 (bufferp (car handles)))
741 (mm-destroy-part handles)
743 (while (setq handle (pop handles))
746 (when (buffer-live-p (get-text-property 0 'buffer handle))
747 (kill-buffer (get-text-property 0 'buffer handle))))
749 (stringp (car handle)))
750 (mm-destroy-parts handle))
752 (mm-destroy-part handle)))))))
754 (defun mm-remove-part (handle)
755 "Remove the displayed MIME part represented by HANDLE."
757 (let ((object (mm-handle-undisplayer handle)))
760 ;; Internally displayed part.
761 ((mm-annotationp object)
762 (delete-annotation object))
763 ((or (functionp object)
765 (eq (car object) 'lambda)))
767 ;; Externally displayed part.
769 (ignore-errors (delete-file (car object)))
770 (ignore-errors (delete-directory (file-name-directory (car object))))
771 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
773 (when (buffer-live-p object)
774 (kill-buffer object)))))
775 (mm-handle-set-undisplayer handle nil))))
777 (defun mm-display-inline (handle)
778 (let* ((type (mm-handle-media-type handle))
779 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
780 (funcall function handle)
781 (goto-char (point-min))))
783 (defun mm-assoc-string-match (alist type)
785 (when (string-match (car elem) type)
788 (defun mm-automatic-display-p (handle)
789 "Say whether the user wants HANDLE to be displayed automatically."
790 (let ((methods mm-automatic-display)
791 (type (mm-handle-media-type handle))
793 (while (setq method (pop methods))
794 (when (and (not (mm-inline-override-p handle))
795 (string-match method type))
800 (defun mm-inlinable-p (handle)
801 "Say whether HANDLE can be displayed inline."
802 (let ((alist mm-inline-media-tests)
803 (type (mm-handle-media-type handle))
806 (when (string-match (caar alist) type)
807 (setq test (caddar alist)
809 (setq test (funcall test handle)))
813 (defun mm-inlined-p (handle)
814 "Say whether the user wants HANDLE to be displayed inline."
815 (let ((methods mm-inlined-types)
816 (type (mm-handle-media-type handle))
818 (while (setq method (pop methods))
819 (when (and (not (mm-inline-override-p handle))
820 (string-match method type))
825 (defun mm-attachment-override-p (handle)
826 "Say whether HANDLE should have attachment behavior overridden."
827 (let ((types mm-attachment-override-types)
828 (type (mm-handle-media-type handle))
831 (while (setq ty (pop types))
832 (when (and (string-match ty type)
833 (mm-inlinable-p handle))
834 (throw 'found t))))))
836 (defun mm-inline-override-p (handle)
837 "Say whether HANDLE should have inline behavior overridden."
838 (let ((types mm-inline-override-types)
839 (type (mm-handle-media-type handle))
842 (while (setq ty (pop types))
843 (when (string-match ty type)
844 (throw 'found t))))))
846 (defun mm-automatic-external-display-p (type)
847 "Return the user-defined method for TYPE."
848 (let ((methods mm-automatic-external-display)
850 (while (setq method (pop methods))
851 (when (string-match method type)
856 (defun mm-destroy-part (handle)
857 "Destroy the data structures connected to HANDLE."
859 (mm-remove-part handle)
860 (when (buffer-live-p (mm-handle-buffer handle))
861 (kill-buffer (mm-handle-buffer handle)))))
863 (defun mm-handle-displayed-p (handle)
864 "Say whether HANDLE is displayed or not."
865 (mm-handle-undisplayer handle))
868 ;;; Functions for outputting parts
871 (defun mm-get-part (handle)
872 "Return the contents of HANDLE as a string."
873 (mm-with-unibyte-buffer
874 (insert (with-current-buffer (mm-handle-buffer handle)
875 (mm-with-unibyte-current-buffer-mule4
877 (mm-decode-content-transfer-encoding
878 (mm-handle-encoding handle)
879 (mm-handle-media-type handle))
882 (defun mm-insert-part (handle)
883 "Insert the contents of HANDLE in the current buffer."
884 (let ((cur (current-buffer)))
886 (if (member (mm-handle-media-supertype handle) '("text" "message"))
888 (insert-buffer-substring (mm-handle-buffer handle))
889 (mm-decode-content-transfer-encoding
890 (mm-handle-encoding handle)
891 (mm-handle-media-type handle))
892 (let ((temp (current-buffer)))
894 (insert-buffer-substring temp)))
895 (mm-with-unibyte-buffer
896 (insert-buffer-substring (mm-handle-buffer handle))
897 (mm-decode-content-transfer-encoding
898 (mm-handle-encoding handle)
899 (mm-handle-media-type handle))
900 (let ((temp (current-buffer)))
902 (insert-buffer-substring temp)))))))
904 (defun mm-file-name-delete-whitespace (file-name)
905 "Remove all whitespace characters from FILE-NAME."
906 (while (string-match "\\s-+" file-name)
907 (setq file-name (replace-match "" t t file-name)))
910 (defun mm-file-name-trim-whitespace (file-name)
911 "Remove leading and trailing whitespace characters from FILE-NAME."
912 (when (string-match "\\`\\s-+" file-name)
913 (setq file-name (substring file-name (match-end 0))))
914 (when (string-match "\\s-+\\'" file-name)
915 (setq file-name (substring file-name 0 (match-beginning 0))))
918 (defun mm-file-name-collapse-whitespace (file-name)
919 "Collapse multiple whitespace characters in FILE-NAME."
920 (while (string-match "\\s-\\s-+" file-name)
921 (setq file-name (replace-match " " t t file-name)))
924 (defun mm-file-name-replace-whitespace (file-name)
925 "Replace whitespace characters in FILE-NAME with underscores.
926 Set `mm-file-name-replace-whitespace' to any other string if you do not
928 (let ((s (or mm-file-name-replace-whitespace "_")))
929 (while (string-match "\\s-" file-name)
930 (setq file-name (replace-match s t t file-name))))
933 (defun mm-save-part (handle)
934 "Write HANDLE to a file."
935 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
936 (filename (mail-content-type-get
937 (mm-handle-disposition handle) 'filename))
940 (setq filename (gnus-map-function mm-file-name-rewrite-functions
941 (file-name-nondirectory filename))))
943 (read-file-name "Save MIME part to: "
945 (or filename name "")
946 (or mm-default-directory default-directory))))
947 (setq mm-default-directory (file-name-directory file))
948 (and (or (not (file-exists-p file))
949 (yes-or-no-p (format "File %s already exists; overwrite? "
952 (mm-save-part-to-file handle file)
955 (defun mm-save-part-to-file (handle file)
956 (mm-with-unibyte-buffer
957 (mm-insert-part handle)
958 (let ((coding-system-for-write 'binary)
959 ;; Don't re-compress .gz & al. Arguably we should make
960 ;; `file-name-handler-alist' nil, but that would chop
961 ;; ange-ftp, which is reasonable to use here.
962 (inhibit-file-name-operation 'write-region)
963 (inhibit-file-name-handlers
964 (cons 'jka-compr-handler inhibit-file-name-handlers)))
965 (write-region (point-min) (point-max) file))))
967 (defun mm-pipe-part (handle)
968 "Pipe HANDLE to a process."
969 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
971 (read-string "Shell command on MIME part: " mm-last-shell-command)))
972 (mm-with-unibyte-buffer
973 (mm-insert-part handle)
974 (let ((coding-system-for-write 'binary))
975 (shell-command-on-region (point-min) (point-max) command nil)))))
977 (defun mm-interactively-view-part (handle)
978 "Display HANDLE using METHOD."
979 (let* ((type (mm-handle-media-type handle))
981 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
982 (mailcap-mime-info type 'all)))
983 (method (let ((minibuffer-local-completion-map
984 mm-viewer-completion-map))
985 (completing-read "Viewer: " methods))))
986 (when (string= method "")
987 (error "No method given"))
988 (if (string-match "^[^% \t]+$" method)
989 (setq method (concat method " %s")))
990 (mm-display-external handle method)))
992 (defun mm-preferred-alternative (handles &optional preferred)
993 "Say which of HANDLES are preferred."
994 (let ((prec (if preferred (list preferred)
995 (mm-preferred-alternative-precedence handles)))
996 p h result type handle)
997 (while (setq p (pop prec))
1000 (setq handle (car h))
1001 (setq type (mm-handle-media-type handle))
1002 (when (and (equal p type)
1003 (mm-automatic-display-p handle)
1004 (or (stringp (car handle))
1005 (not (mm-handle-disposition handle))
1006 (equal (car (mm-handle-disposition handle))
1014 (defun mm-preferred-alternative-precedence (handles)
1015 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1016 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1018 (dolist (disc (reverse mm-discouraged-alternatives))
1019 (dolist (elem (copy-sequence seq))
1020 (when (string-match disc elem)
1021 (setq seq (nconc (delete elem seq) (list elem))))))
1024 (defun mm-get-content-id (id)
1025 "Return the handle(s) referred to by ID."
1026 (cdr (assoc id mm-content-id-alist)))
1028 (defun mm-get-image (handle)
1029 "Return an image instance based on HANDLE."
1030 (let ((type (mm-handle-media-subtype handle))
1032 ;; Allow some common translations.
1035 ((equal type "x-pixmap")
1037 ((equal type "x-xbitmap")
1039 ((equal type "x-portable-bitmap")
1042 (or (mm-handle-cache handle)
1043 (mm-with-unibyte-buffer
1044 (mm-insert-part handle)
1048 ;; Avoid testing `make-glyph' since W3 may define
1049 ;; a bogus version of it.
1050 (if (fboundp 'create-image)
1051 (create-image (buffer-string) (intern type) 'data-p)
1054 ;; xbm images require special handling, since
1055 ;; the only way to create glyphs from these
1056 ;; (without a ton of work) is to write them
1057 ;; out to a file, and then create a file
1059 (let ((file (make-temp-name
1060 (expand-file-name "emm.xbm"
1061 mm-tmp-directory))))
1064 (write-region (point-min) (point-max) file)
1065 (make-glyph (list (cons 'x file))))
1067 (delete-file file)))))
1070 (vector (intern type) :data (buffer-string))))))))
1071 (mm-handle-set-cache handle spec))))))
1073 (defun mm-image-fit-p (handle)
1074 "Say whether the image in HANDLE will fit the current window."
1075 (let ((image (mm-get-image handle)))
1076 (if (fboundp 'glyph-width)
1077 ;; XEmacs' glyphs can actually tell us about their width, so
1078 ;; lets be nice and smart about them.
1079 (or mm-inline-large-images
1080 (and (< (glyph-width image) (window-pixel-width))
1081 (< (glyph-height image) (window-pixel-height))))
1082 (let* ((size (image-size image))
1085 (or mm-inline-large-images
1086 (and (< h (1- (window-height))) ; Don't include mode line.
1087 (< w (window-width))))))))
1089 (defun mm-valid-image-format-p (format)
1090 "Say whether FORMAT can be displayed natively by Emacs."
1093 ((fboundp 'valid-image-instantiator-format-p)
1094 (valid-image-instantiator-format-p format))
1096 ((fboundp 'image-type-available-p)
1097 (and (display-graphic-p)
1098 (image-type-available-p format)))
1099 ;; Nobody else can do images yet.
1103 (defun mm-valid-and-fit-image-p (format handle)
1104 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1105 (and (mm-valid-image-format-p format)
1106 (mm-image-fit-p handle)))
1108 (defun mm-find-part-by-type (handles type &optional notp recursive)
1109 "Search in HANDLES for part with TYPE.
1110 If NOTP, returns first non-matching part.
1111 If RECURSIVE, search recursively."
1114 (if (and recursive (stringp (caar handles)))
1115 (if (setq handle (mm-find-part-by-type (cdar handles) type
1119 (not (equal (mm-handle-media-type (car handles)) type))
1120 (equal (mm-handle-media-type (car handles)) type))
1121 (setq handle (car handles)
1123 (setq handles (cdr handles)))
1126 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1127 (goto-char (point-min))
1128 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1130 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1132 (end (save-excursion
1133 (goto-char (point-max))
1134 (if (re-search-backward close-delimiter nil t)
1138 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1139 (while (and (not result)
1140 (re-search-forward boundary end t))
1141 (goto-char (match-beginning 0))
1145 (narrow-to-region start (1- (point)))
1146 (when (let ((ctl (ignore-errors
1147 (mail-header-parse-content-type
1148 (mail-fetch-field "content-type")))))
1150 (not (equal (car ctl) type))
1151 (equal (car ctl) type)))
1152 (setq result (buffer-substring (point-min) (point-max)))))))
1154 (setq start (point)))
1155 (when (and (not result) start)
1158 (narrow-to-region start end)
1159 (when (let ((ctl (ignore-errors
1160 (mail-header-parse-content-type
1161 (mail-fetch-field "content-type")))))
1163 (not (equal (car ctl) type))
1164 (equal (car ctl) type)))
1165 (setq result (buffer-substring (point-min) (point-max)))))))
1168 (defvar mm-security-handle nil)
1170 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1171 ;; HANDLE could be a CTL.
1173 (put-text-property 0 (length (car handle)) parameter value
1176 (defun mm-possibly-verify-or-decrypt (parts ctl)
1177 (let ((subtype (cadr (split-string (car ctl) "/")))
1178 (mm-security-handle ctl) ;; (car CTL) is the type.
1179 protocol func functest)
1181 ((equal subtype "signed")
1182 (unless (and (setq protocol
1183 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1184 (not (equal protocol "multipart/mixed")))
1185 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1186 (let ((protocols mm-verify-function-alist))
1188 (if (and (or (not (setq functest (nth 3 (car protocols))))
1189 (funcall functest parts ctl))
1190 (mm-find-part-by-type parts (caar protocols) nil t))
1191 (setq protocol (caar protocols)
1193 (setq protocols (cdr protocols))))))
1194 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1196 ((eq mm-verify-option 'never) nil)
1197 ((eq mm-verify-option 'always) t)
1198 ((eq mm-verify-option 'known)
1200 (or (not (setq functest
1201 (nth 3 (assoc protocol
1202 mm-verify-function-alist))))
1203 (funcall functest parts ctl))))
1205 (format "Verify signed (%s) part? "
1206 (or (nth 2 (assoc protocol mm-verify-function-alist))
1207 (format "protocol=%s" protocol))))))
1210 (funcall func parts ctl)
1211 (mm-set-handle-multipart-parameter
1212 mm-security-handle 'gnus-details
1213 (format "Unknown sign protocol (%s)" protocol))))))
1214 ((equal subtype "encrypted")
1215 (unless (setq protocol
1216 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1217 ;; The message is broken.
1218 (let ((parts parts))
1220 (if (assoc (mm-handle-media-type (car parts))
1221 mm-decrypt-function-alist)
1222 (setq protocol (mm-handle-media-type (car parts))
1224 (setq parts (cdr parts))))))
1225 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1227 ((eq mm-decrypt-option 'never) nil)
1228 ((eq mm-decrypt-option 'always) t)
1229 ((eq mm-decrypt-option 'known)
1231 (or (not (setq functest
1232 (nth 3 (assoc protocol
1233 mm-decrypt-function-alist))))
1234 (funcall functest parts ctl))))
1236 (format "Decrypt (%s) part? "
1237 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1238 (format "protocol=%s" protocol))))))
1241 (setq parts (funcall func parts ctl))
1242 (mm-set-handle-multipart-parameter
1243 mm-security-handle 'gnus-details
1244 (format "Unknown encrypt protocol (%s)" protocol))))))
1248 (defun mm-multiple-handles (handles)
1249 (and (listp (car handles))
1250 (> (length handles) 1)))
1252 (defun mm-merge-handles (handles1 handles2)
1254 (if (listp (car handles1))
1257 (if (listp (car handles2))
1261 (provide 'mm-decode)
1263 ;;; mm-decode.el ends here