1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000 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))
33 (autoload 'mm-inline-partial "mm-partial")
34 (autoload 'mm-inline-external-body "mm-extern"))
36 (defgroup mime-display ()
37 "Display of MIME in mail and news articles."
38 :link '(custom-manual "(emacs-mime)Customization")
43 (defgroup mime-security ()
44 "MIME security in mail and news articles."
45 :link '(custom-manual "(emacs-mime)Customization")
50 ;;; Convenience macros.
52 (defmacro mm-handle-buffer (handle)
54 (defmacro mm-handle-type (handle)
56 (defsubst mm-handle-media-type (handle)
57 (if (stringp (car handle))
59 (car (mm-handle-type handle))))
60 (defsubst mm-handle-media-supertype (handle)
61 (car (split-string (mm-handle-media-type handle) "/")))
62 (defsubst mm-handle-media-subtype (handle)
63 (cadr (split-string (mm-handle-media-type handle) "/")))
64 (defmacro mm-handle-encoding (handle)
66 (defmacro mm-handle-undisplayer (handle)
68 (defmacro mm-handle-set-undisplayer (handle function)
69 `(setcar (nthcdr 3 ,handle) ,function))
70 (defmacro mm-handle-disposition (handle)
72 (defmacro mm-handle-description (handle)
74 (defmacro mm-handle-cache (handle)
76 (defmacro mm-handle-set-cache (handle contents)
77 `(setcar (nthcdr 6 ,handle) ,contents))
78 (defmacro mm-handle-id (handle)
80 (defmacro mm-handle-multipart-original-buffer (handle)
81 `(get-text-property 0 'buffer (car ,handle)))
82 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
83 `(get-text-property 0 ,parameter (car ,handle)))
85 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
86 disposition description cache
88 `(list ,buffer ,type ,encoding ,undisplayer
89 ,disposition ,description ,cache ,id))
91 (defcustom mm-inline-media-tests
95 (mm-valid-and-fit-image-p 'jpeg handle)))
99 (mm-valid-and-fit-image-p 'png handle)))
103 (mm-valid-and-fit-image-p 'gif handle)))
107 (mm-valid-and-fit-image-p 'tiff handle)) )
111 (mm-valid-and-fit-image-p 'xbm handle)))
115 (mm-valid-and-fit-image-p 'xbm handle)))
119 (mm-valid-and-fit-image-p 'xpm handle)))
123 (mm-valid-and-fit-image-p 'xpm handle)))
127 (mm-valid-and-fit-image-p 'bmp handle)))
128 ("text/plain" mm-inline-text identity)
129 ("text/enriched" mm-inline-text identity)
130 ("text/richtext" mm-inline-text identity)
131 ("text/x-patch" mm-display-patch-inline
133 (locate-library "diff-mode")))
134 ("application/emacs-lisp" mm-display-elisp-inline identity)
138 (locate-library "w3")))
142 (or (featurep 'vcard)
143 (locate-library "vcard"))))
144 ("message/delivery-status" mm-inline-text identity)
145 ("message/rfc822" mm-inline-message identity)
146 ("message/partial" mm-inline-partial identity)
147 ("message/external-body" mm-inline-external-body identity)
148 ("text/.*" mm-inline-text identity)
149 ("audio/wav" mm-inline-audio
151 (and (or (featurep 'nas-sound) (featurep 'native-sound))
152 (device-sound-enabled-p))))
156 (and (or (featurep 'nas-sound) (featurep 'native-sound))
157 (device-sound-enabled-p))))
158 ("application/pgp-signature" ignore identity)
159 ("application/x-pkcs7-signature" ignore identity)
160 ("application/pkcs7-signature" ignore identity)
161 ("multipart/alternative" ignore identity)
162 ("multipart/mixed" ignore identity)
163 ("multipart/related" ignore identity))
164 "Alist of media types/tests saying whether types can be displayed inline."
165 :type '(repeat (list (string :tag "MIME type")
166 (function :tag "Display function")
167 (function :tag "Display test")))
168 :group 'mime-display)
170 (defcustom mm-inlined-types
171 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
172 "message/partial" "message/external-body" "application/emacs-lisp"
173 "application/pgp-signature" "application/x-pkcs7-signature"
174 "application/pkcs7-signature")
175 "List of media types that are to be displayed inline."
176 :type '(repeat string)
177 :group 'mime-display)
179 (defcustom mm-automatic-display
180 '("text/plain" "text/enriched" "text/richtext" "text/html"
181 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
182 "message/rfc822" "text/x-patch" "application/pgp-signature"
183 "application/emacs-lisp" "application/x-pkcs7-signature"
184 "application/pkcs7-signature")
185 "A list of MIME types to be displayed automatically."
186 :type '(repeat string)
187 :group 'mime-display)
189 (defcustom mm-attachment-override-types '("text/x-vcard")
190 "Types to have \"attachment\" ignored if they can be displayed inline."
191 :type '(repeat string)
192 :group 'mime-display)
194 (defcustom mm-inline-override-types nil
195 "Types to be treated as attachments even if they can be displayed inline."
196 :type '(repeat string)
197 :group 'mime-display)
199 (defcustom mm-automatic-external-display nil
200 "List of MIME type regexps that will be displayed externally automatically."
201 :type '(repeat string)
202 :group 'mime-display)
204 (defcustom mm-discouraged-alternatives nil
205 "List of MIME types that are discouraged when viewing multipart/alternative.
206 Viewing agents are supposed to view the last possible part of a message,
207 as that is supposed to be the richest. However, users may prefer other
208 types instead, and this list says what types are most unwanted. If,
209 for instance, text/html parts are very unwanted, and text/richtext are
210 somewhat unwanted, then the value of this variable should be set
213 (\"text/html\" \"text/richtext\")"
214 :type '(repeat string)
215 :group 'mime-display)
217 (defvar mm-tmp-directory
218 (cond ((fboundp 'temp-directory) (temp-directory))
219 ((boundp 'temporary-file-directory) temporary-file-directory)
221 "Where mm will store its temporary files.")
223 (defcustom mm-inline-large-images nil
224 "If non-nil, then all images fit in the buffer."
226 :group 'mime-display)
228 ;;; Internal variables.
230 (defvar mm-dissection-list nil)
231 (defvar mm-last-shell-command "")
232 (defvar mm-content-id-alist nil)
234 ;; According to RFC2046, in particular, in a digest, the default
235 ;; Content-Type value for a body part is changed from "text/plain" to
237 (defvar mm-dissect-default-type "text/plain")
239 (autoload 'mml2015-verify "mml2015")
240 (autoload 'mml2015-verify-test "mml2015")
241 (autoload 'mml-smime-verify "mml-smime")
242 (autoload 'mml-smime-verify-test "mml-smime")
244 (defvar mm-verify-function-alist
245 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
246 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
247 mml-smime-verify-test)
248 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
249 mml-smime-verify-test)))
251 (defcustom mm-verify-option nil
252 "Option of verifying signed parts.
253 `never', not verify; `always', always verify;
254 `known', only verify known protocols. Otherwise, ask user."
255 :type '(choice (item always)
257 (item :tag "only known protocols" known)
258 (item :tag "ask" nil))
259 :group 'mime-security)
261 (autoload 'mml2015-decrypt "mml2015")
262 (autoload 'mml2015-decrypt-test "mml2015")
264 (defvar mm-decrypt-function-alist
265 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)))
267 (defcustom mm-decrypt-option nil
268 "Option of decrypting signed parts.
269 `never', not decrypt; `always', always decrypt;
270 `known', only decrypt known protocols. Otherwise, ask user."
271 :type '(choice (item always)
273 (item :tag "only known protocols" known)
274 (item :tag "ask" nil))
275 :group 'mime-security)
277 (defvar mm-viewer-completion-map
278 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
279 (set-keymap-parent map minibuffer-local-completion-map)
281 "Keymap for input viewer with completion.")
283 ;; Should we bind other key to minibuffer-complete-word?
284 (define-key mm-viewer-completion-map " " 'self-insert-command)
288 (defun mm-alist-to-plist (alist)
289 "Convert association list ALIST into the equivalent property-list form.
290 The plist is returned. This converts from
292 \((a . 1) (b . 2) (c . 3))
298 The original alist is not modified. See also `destructive-alist-to-plist'."
301 (let ((el (car alist)))
302 (setq plist (cons (cdr el) (cons (car el) plist))))
303 (setq alist (cdr alist)))
306 (defun mm-dissect-buffer (&optional no-strict-mime)
307 "Dissect the current buffer and return a list of MIME handles."
309 (let (ct ctl type subtype cte cd description id result)
311 (mail-narrow-to-head)
312 (when (or no-strict-mime
313 (mail-fetch-field "mime-version"))
314 (setq ct (mail-fetch-field "content-type")
315 ctl (ignore-errors (mail-header-parse-content-type ct))
316 cte (mail-fetch-field "content-transfer-encoding")
317 cd (mail-fetch-field "content-disposition")
318 description (mail-fetch-field "content-description")
319 id (mail-fetch-field "content-id"))))
321 (setq cte (mail-header-strip cte)))
323 (not (string-match "/" (car ctl))))
324 (mm-dissect-singlepart
325 (list mm-dissect-default-type)
326 (and cte (intern (downcase (mail-header-remove-whitespace
327 (mail-header-remove-comments
330 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
332 (setq type (split-string (car ctl) "/"))
333 (setq subtype (cadr type)
338 ((equal type "multipart")
339 (let ((mm-dissect-default-type (if (equal subtype "digest")
342 (add-text-properties 0 (length (car ctl))
343 (mm-alist-to-plist (cdr ctl)) (car ctl))
345 ;; what really needs to be done here is a way to link a
346 ;; MIME handle back to it's parent MIME handle (in a multilevel
347 ;; MIME article). That would probably require changing
348 ;; the mm-handle API so we simply store the multipart buffert
349 ;; name as a text property of the "multipart/whatever" string.
350 (add-text-properties 0 (length (car ctl))
351 (list 'buffer (mm-copy-to-buffer))
353 (cons (car ctl) (mm-dissect-multipart ctl))))
355 (mm-dissect-singlepart
357 (and cte (intern (downcase (mail-header-remove-whitespace
358 (mail-header-remove-comments
361 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
364 (when (string-match " *<\\(.*\\)> *" id)
365 (setq id (match-string 1 id)))
366 (push (cons id result) mm-content-id-alist))
369 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
371 (if (equal "text/plain" (car ctl))
374 (let ((res (mm-make-handle
375 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
376 (push (car res) mm-dissection-list)
379 (defun mm-remove-all-parts ()
380 "Remove all MIME handles."
382 (mapcar 'mm-remove-part mm-dissection-list)
383 (setq mm-dissection-list nil))
385 (defun mm-dissect-multipart (ctl)
386 (goto-char (point-min))
387 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
388 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
391 (goto-char (point-max))
392 (if (re-search-backward close-delimiter nil t)
395 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
396 (while (re-search-forward boundary end t)
397 (goto-char (match-beginning 0))
401 (narrow-to-region start (point))
402 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
404 (setq start (point)))
408 (narrow-to-region start end)
409 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
410 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
412 (defun mm-copy-to-buffer ()
413 "Copy the contents of the current buffer to a fresh buffer."
415 (let ((obuf (current-buffer))
417 (goto-char (point-min))
418 (search-forward-regexp "^\n" nil t)
420 (set-buffer (generate-new-buffer " *mm*"))
421 (insert-buffer-substring obuf beg)
424 (defun mm-display-parts (handle &optional no-default)
425 (if (stringp (car handle))
426 (mapcar 'mm-display-parts (cdr handle))
427 (if (bufferp (car handle))
429 (narrow-to-region (point) (point))
430 (mm-display-part handle)
431 (goto-char (point-max)))
432 (mapcar 'mm-display-parts handle))))
434 (defun mm-display-part (handle &optional no-default)
435 "Display the MIME part represented by HANDLE.
436 Returns nil if the part is removed; inline if displayed inline;
437 external if displayed external."
439 (mailcap-parse-mailcaps)
440 (if (mm-handle-displayed-p handle)
441 (mm-remove-part handle)
442 (let* ((type (mm-handle-media-type handle))
443 (method (mailcap-mime-info type)))
444 (if (mm-inlined-p handle)
447 (mm-display-inline handle)
451 (if (and (not method)
452 (equal "text" (car (split-string type))))
455 (mm-insert-inline handle (mm-get-part handle))
458 handle (or method 'mailcap-save-binary-file)))))))))
460 (defun mm-display-external (handle method)
461 "Display HANDLE using METHOD."
462 (let ((outbuf (current-buffer)))
463 (mm-with-unibyte-buffer
464 (if (functionp method)
465 (let ((cur (current-buffer)))
466 (if (eq method 'mailcap-save-binary-file)
468 (set-buffer (generate-new-buffer " *mm*"))
470 (mm-insert-part handle)
471 (let ((win (get-buffer-window cur t)))
473 (select-window win)))
474 (switch-to-buffer (generate-new-buffer " *mm*")))
475 (buffer-disable-undo)
476 (mm-set-buffer-file-coding-system mm-binary-coding-system)
477 (insert-buffer-substring cur)
478 (goto-char (point-min))
479 (message "Viewing with %s" method)
480 (let ((mm (current-buffer))
481 (non-viewer (assq 'non-viewer
483 (mm-handle-media-type handle) t))))
487 (mm-save-part handle))
488 (when (and (not non-viewer)
490 (mm-handle-set-undisplayer handle mm)))))
491 ;; The function is a string to be executed.
492 (mm-insert-part handle)
493 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
494 (filename (mail-content-type-get
495 (mm-handle-disposition handle) 'filename))
496 (mime-info (mailcap-mime-info
497 (mm-handle-media-type handle) t))
498 (needsterm (or (assoc "needsterm" mime-info)
499 (assoc "needsterminal" mime-info)))
500 (copiousoutput (assoc "copiousoutput" mime-info))
502 ;; We create a private sub-directory where we store our files.
504 (set-file-modes dir 448)
506 (setq file (expand-file-name (file-name-nondirectory filename)
508 (setq file (make-temp-name (expand-file-name "mm." dir))))
509 (let ((coding-system-for-write mm-binary-coding-system))
510 (write-region (point-min) (point-max) file nil 'nomesg))
511 (message "Viewing with %s" method)
514 (start-process "*display*" nil
519 method file (mm-handle-type handle)))
520 (mm-handle-set-undisplayer handle (cons file buffer)))
521 (message "Displaying %s..." (format method file))
524 (with-current-buffer outbuf
530 (call-process shell-file-name nil
532 (generate-new-buffer " *mm*"))
536 method file (mm-handle-type handle)))
537 (if (buffer-live-p buffer)
542 (ignore-errors (delete-file file))
543 (ignore-errors (delete-directory
544 (file-name-directory file)))
545 (ignore-errors (kill-buffer buffer))))))
549 (start-process "*display*"
551 (generate-new-buffer " *mm*"))
555 method file (mm-handle-type handle)))
556 (mm-handle-set-undisplayer handle (cons file buffer)))
557 (message "Displaying %s..." (format method file))
560 (defun mm-mailcap-command (method file type-list)
561 (let ((ctl (cdr type-list))
565 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
566 (push (substring method beg (match-beginning 0)) out)
567 (setq beg (match-end 0)
568 total (match-string 0 method)
569 sub (match-string 1 method))
571 ((string= total "%%")
573 ((string= total "%s")
574 (setq uses-stdin nil)
575 (push (mm-quote-arg file) out))
576 ((string= total "%t")
577 (push (mm-quote-arg (car type-list)) out))
579 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
580 (push (substring method beg (length method)) out)
584 (push (mm-quote-arg file) out)))
585 (mapconcat 'identity (nreverse out) "")))
587 (defun mm-remove-parts (handles)
588 "Remove the displayed MIME parts represented by HANDLES."
589 (if (and (listp handles)
590 (bufferp (car handles)))
591 (mm-remove-part handles)
593 (while (setq handle (pop handles))
596 (when (buffer-live-p (get-text-property 0 'buffer handle))
597 (kill-buffer (get-text-property 0 'buffer handle))))
599 (stringp (car handle)))
600 (mm-remove-parts (cdr handle)))
602 (mm-remove-part handle)))))))
604 (defun mm-destroy-parts (handles)
605 "Remove the displayed MIME parts represented by HANDLES."
606 (if (and (listp handles)
607 (bufferp (car handles)))
608 (mm-destroy-part handles)
610 (while (setq handle (pop handles))
613 (when (buffer-live-p (get-text-property 0 'buffer handle))
614 (kill-buffer (get-text-property 0 'buffer handle))))
616 (stringp (car handle)))
617 (mm-destroy-parts (cdr handle)))
619 (mm-destroy-part handle)))))))
621 (defun mm-remove-part (handle)
622 "Remove the displayed MIME part represented by HANDLE."
624 (let ((object (mm-handle-undisplayer handle)))
627 ;; Internally displayed part.
628 ((mm-annotationp object)
629 (delete-annotation object))
630 ((or (functionp object)
632 (eq (car object) 'lambda)))
634 ;; Externally displayed part.
636 (ignore-errors (delete-file (car object)))
637 (ignore-errors (delete-directory (file-name-directory (car object))))
638 (ignore-errors (kill-buffer (cdr object))))
640 (when (buffer-live-p object)
641 (kill-buffer object)))))
642 (mm-handle-set-undisplayer handle nil))))
644 (defun mm-display-inline (handle)
645 (let* ((type (mm-handle-media-type handle))
646 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
647 (funcall function handle)
648 (goto-char (point-min))))
650 (defun mm-assoc-string-match (alist type)
652 (when (string-match (car elem) type)
655 (defun mm-inlinable-p (handle)
656 "Say whether HANDLE can be displayed inline."
657 (let ((alist mm-inline-media-tests)
658 (type (mm-handle-media-type handle))
661 (when (string-match (caar alist) type)
662 (setq test (caddar alist)
664 (setq test (funcall test handle)))
668 (defun mm-automatic-display-p (handle)
669 "Say whether the user wants HANDLE to be displayed automatically."
670 (let ((methods mm-automatic-display)
671 (type (mm-handle-media-type handle))
673 (while (setq method (pop methods))
674 (when (and (not (mm-inline-override-p handle))
675 (string-match method type)
676 (mm-inlinable-p handle))
681 (defun mm-inlined-p (handle)
682 "Say whether the user wants HANDLE to be displayed automatically."
683 (let ((methods mm-inlined-types)
684 (type (mm-handle-media-type handle))
686 (while (setq method (pop methods))
687 (when (and (not (mm-inline-override-p handle))
688 (string-match method type)
689 (mm-inlinable-p handle))
694 (defun mm-attachment-override-p (handle)
695 "Say whether HANDLE should have attachment behavior overridden."
696 (let ((types mm-attachment-override-types)
697 (type (mm-handle-media-type handle))
700 (while (setq ty (pop types))
701 (when (and (string-match ty type)
702 (mm-inlinable-p handle))
703 (throw 'found t))))))
705 (defun mm-inline-override-p (handle)
706 "Say whether HANDLE should have inline behavior overridden."
707 (let ((types mm-inline-override-types)
708 (type (mm-handle-media-type handle))
711 (while (setq ty (pop types))
712 (when (string-match ty type)
713 (throw 'found t))))))
715 (defun mm-automatic-external-display-p (type)
716 "Return the user-defined method for TYPE."
717 (let ((methods mm-automatic-external-display)
719 (while (setq method (pop methods))
720 (when (string-match method type)
725 (defun mm-destroy-part (handle)
726 "Destroy the data structures connected to HANDLE."
728 (mm-remove-part handle)
729 (when (buffer-live-p (mm-handle-buffer handle))
730 (kill-buffer (mm-handle-buffer handle)))))
732 (defun mm-handle-displayed-p (handle)
733 "Say whether HANDLE is displayed or not."
734 (mm-handle-undisplayer handle))
737 ;;; Functions for outputting parts
740 (defun mm-get-part (handle)
741 "Return the contents of HANDLE as a string."
742 (mm-with-unibyte-buffer
743 (insert (with-current-buffer (mm-handle-buffer handle)
744 (mm-with-unibyte-current-buffer-mule4
746 (mm-decode-content-transfer-encoding
747 (mm-handle-encoding handle)
748 (mm-handle-media-type handle))
751 (defun mm-insert-part (handle)
752 "Insert the contents of HANDLE in the current buffer."
753 (let ((cur (current-buffer)))
755 (if (member (mm-handle-media-supertype handle) '("text" "message"))
757 (insert-buffer-substring (mm-handle-buffer handle))
758 (mm-decode-content-transfer-encoding
759 (mm-handle-encoding handle)
760 (mm-handle-media-type handle))
761 (let ((temp (current-buffer)))
763 (insert-buffer-substring temp)))
764 (mm-with-unibyte-buffer
765 (insert-buffer-substring (mm-handle-buffer handle))
766 (mm-decode-content-transfer-encoding
767 (mm-handle-encoding handle)
768 (mm-handle-media-type handle))
769 (let ((temp (current-buffer)))
771 (insert-buffer-substring temp)))))))
773 (defvar mm-default-directory nil)
775 (defun mm-save-part (handle)
776 "Write HANDLE to a file."
777 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
778 (filename (mail-content-type-get
779 (mm-handle-disposition handle) 'filename))
782 (setq filename (file-name-nondirectory filename)))
784 (read-file-name "Save MIME part to: "
786 (or filename name "")
787 (or mm-default-directory default-directory))))
788 (setq mm-default-directory (file-name-directory file))
789 (and (or (not (file-exists-p file))
790 (yes-or-no-p (format "File %s already exists; overwrite? "
793 (mm-save-part-to-file handle file)
796 (defun mm-save-part-to-file (handle file)
797 (mm-with-unibyte-buffer
798 (mm-insert-part handle)
799 (let ((coding-system-for-write 'binary)
800 ;; Don't re-compress .gz & al. Arguably we should make
801 ;; `file-name-handler-alist' nil, but that would chop
802 ;; ange-ftp, which is reasonable to use here.
803 (inhibit-file-name-operation 'write-region)
804 (inhibit-file-name-handlers
805 (cons 'jka-compr-handler inhibit-file-name-handlers)))
806 (write-region (point-min) (point-max) file))))
808 (defun mm-pipe-part (handle)
809 "Pipe HANDLE to a process."
810 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
812 (read-string "Shell command on MIME part: " mm-last-shell-command)))
813 (mm-with-unibyte-buffer
814 (mm-insert-part handle)
815 (shell-command-on-region (point-min) (point-max) command nil))))
817 (defun mm-interactively-view-part (handle)
818 "Display HANDLE using METHOD."
819 (let* ((type (mm-handle-media-type handle))
821 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
822 (mailcap-mime-info type 'all)))
823 (method (let ((minibuffer-local-completion-map
824 mm-viewer-completion-map))
825 (completing-read "Viewer: " methods))))
826 (when (string= method "")
827 (error "No method given"))
828 (if (string-match "^[^% \t]+$" method)
829 (setq method (concat method " %s")))
830 (mm-display-external (copy-sequence handle) method)))
832 (defun mm-preferred-alternative (handles &optional preferred)
833 "Say which of HANDLES are preferred."
834 (let ((prec (if preferred (list preferred)
835 (mm-preferred-alternative-precedence handles)))
836 p h result type handle)
837 (while (setq p (pop prec))
840 (setq handle (car h))
841 (setq type (mm-handle-media-type handle))
842 (when (and (equal p type)
843 (mm-automatic-display-p handle)
844 (or (stringp (car handle))
845 (not (mm-handle-disposition handle))
846 (equal (car (mm-handle-disposition handle))
854 (defun mm-preferred-alternative-precedence (handles)
855 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
856 (let ((seq (nreverse (mapcar #'mm-handle-media-type
858 (dolist (disc (reverse mm-discouraged-alternatives))
859 (dolist (elem (copy-sequence seq))
860 (when (string-match disc elem)
861 (setq seq (nconc (delete elem seq) (list elem))))))
864 (defun mm-get-content-id (id)
865 "Return the handle(s) referred to by ID."
866 (cdr (assoc id mm-content-id-alist)))
868 (defun mm-get-image (handle)
869 "Return an image instance based on HANDLE."
870 (let ((type (mm-handle-media-subtype handle))
872 ;; Allow some common translations.
875 ((equal type "x-pixmap")
877 ((equal type "x-xbitmap")
880 (or (mm-handle-cache handle)
881 (mm-with-unibyte-buffer
882 (mm-insert-part handle)
886 ;; Avoid testing `make-glyph' since W3 may define
887 ;; a bogus version of it.
888 (if (fboundp 'create-image)
889 (create-image (buffer-string) (intern type) 'data-p)
892 ;; xbm images require special handling, since
893 ;; the only way to create glyphs from these
894 ;; (without a ton of work) is to write them
895 ;; out to a file, and then create a file
897 (let ((file (make-temp-name
898 (expand-file-name "emm.xbm"
902 (write-region (point-min) (point-max) file)
903 (make-glyph (list (cons 'x file))))
905 (delete-file file)))))
908 (vector (intern type) :data (buffer-string))))))))
909 (mm-handle-set-cache handle spec))))))
911 (defun mm-image-fit-p (handle)
912 "Say whether the image in HANDLE will fit the current window."
913 (let ((image (mm-get-image handle)))
914 (if (fboundp 'glyph-width)
915 ;; XEmacs' glyphs can actually tell us about their width, so
916 ;; lets be nice and smart about them.
917 (or mm-inline-large-images
918 (and (< (glyph-width image) (window-pixel-width))
919 (< (glyph-height image) (window-pixel-height))))
920 (let* ((size (image-size image))
923 (or mm-inline-large-images
924 (and (< h (1- (window-height))) ; Don't include mode line.
925 (< w (window-width))))))))
927 (defun mm-valid-image-format-p (format)
928 "Say whether FORMAT can be displayed natively by Emacs."
931 ((fboundp 'valid-image-instantiator-format-p)
932 (valid-image-instantiator-format-p format))
934 ((fboundp 'image-type-available-p)
935 (and (display-graphic-p)
936 (image-type-available-p format)))
937 ;; Nobody else can do images yet.
941 (defun mm-valid-and-fit-image-p (format handle)
942 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
943 (and (mm-valid-image-format-p format)
944 (mm-image-fit-p handle)))
946 (defun mm-find-part-by-type (handles type &optional notp recursive)
947 "Search in HANDLES for part with TYPE.
948 If NOTP, returns first non-matching part.
949 If RECURSIVE, search recursively."
952 (if (and recursive (stringp (caar handles)))
953 (if (setq handle (mm-find-part-by-type (cdar handles) type
957 (not (equal (mm-handle-media-type (car handles)) type))
958 (equal (mm-handle-media-type (car handles)) type))
959 (setq handle (car handles)
961 (setq handles (cdr handles)))
964 (defun mm-find-raw-part-by-type (ctl type &optional notp)
965 (goto-char (point-min))
966 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
967 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
970 (goto-char (point-max))
971 (if (re-search-backward close-delimiter nil t)
975 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
976 (while (and (not result)
977 (re-search-forward boundary end t))
978 (goto-char (match-beginning 0))
982 (narrow-to-region start (point))
983 (when (let ((ctl (ignore-errors
984 (mail-header-parse-content-type
985 (mail-fetch-field "content-type")))))
987 (not (equal (car ctl) type))
988 (equal (car ctl) type)))
989 (setq result (buffer-substring (point-min) (point-max)))))))
991 (setq start (point)))
992 (when (and (not result) start)
995 (narrow-to-region start end)
996 (when (let ((ctl (ignore-errors
997 (mail-header-parse-content-type
998 (mail-fetch-field "content-type")))))
1000 (not (equal (car ctl) type))
1001 (equal (car ctl) type)))
1002 (setq result (buffer-substring (point-min) (point-max)))))))
1005 (defvar mm-security-handle nil)
1007 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1008 ;; HANDLE could be a CTL.
1010 (put-text-property 0 (length (car handle)) parameter value
1013 (defun mm-possibly-verify-or-decrypt (parts ctl)
1014 (let ((subtype (cadr (split-string (car ctl) "/")))
1015 (mm-security-handle ctl) ;; (car CTL) is the type.
1016 protocol func functest)
1018 ((equal subtype "signed")
1019 (unless (and (setq protocol (mail-content-type-get ctl 'protocol))
1020 (not (equal protocol "multipart/mixed")))
1021 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1022 (let ((protocols mm-verify-function-alist))
1024 (if (and (or (not (setq functest (nth 3 (car protocols))))
1025 (funcall functest parts ctl))
1026 (mm-find-part-by-type parts (caar protocols) nil t))
1027 (setq protocol (caar protocols)
1029 (setq protocols (cdr protocols))))))
1030 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1032 ((eq mm-verify-option 'never) nil)
1033 ((eq mm-verify-option 'always) t)
1034 ((eq mm-verify-option 'known)
1036 (or (not (setq functest
1037 (nth 3 (assoc protocol
1038 mm-verify-function-alist))))
1039 (funcall functest parts ctl))))
1041 (format "Verify signed (%s) part? "
1042 (or (nth 2 (assoc protocol mm-verify-function-alist))
1043 (format "protocol=%s" protocol))))))
1046 (funcall func parts ctl)
1047 (mm-set-handle-multipart-parameter
1048 mm-security-handle 'gnus-details
1049 (format "Unknown sign protocol (%s)" protocol))))))
1050 ((equal subtype "encrypted")
1051 (unless (setq protocol (mail-content-type-get ctl 'protocol))
1052 ;; The message is broken.
1053 (let ((parts parts))
1055 (if (assoc (mm-handle-media-type (car parts))
1056 mm-decrypt-function-alist)
1057 (setq protocol (mm-handle-media-type (car parts))
1059 (setq parts (cdr parts))))))
1060 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1062 ((eq mm-decrypt-option 'never) nil)
1063 ((eq mm-decrypt-option 'always) t)
1064 ((eq mm-decrypt-option 'known)
1066 (or (not (setq functest
1067 (nth 3 (assoc protocol
1068 mm-decrypt-function-alist))))
1069 (funcall functest parts ctl))))
1071 (format "Decrypt (%s) part? "
1072 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1073 (format "protocol=%s" protocol))))))
1076 (setq parts (funcall func parts ctl))
1077 (mm-set-handle-multipart-parameter
1078 mm-security-handle 'gnus-details
1079 (format "Unknown encrypt protocol (%s)" protocol))))))
1083 (provide 'mm-decode)
1085 ;;; mm-decode.el ends here