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")
243 (defvar mm-verify-function-alist
244 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
245 ("application/pkcs7-signature" mml-smime-verify "S/MIME" nil)
246 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME" nil)))
248 (defcustom mm-verify-option 'known
249 "Option of verifying signed parts.
250 `never', not verify; `always', always verify;
251 `known', only verify known protocols. Otherwise, ask user."
252 :type '(choice (item always)
254 (item :tag "only known protocols" known)
255 (item :tag "ask" nil))
256 :group 'mime-security)
258 (autoload 'mml2015-decrypt "mml2015")
259 (autoload 'mml2015-decrypt-test "mml2015")
261 (defvar mm-decrypt-function-alist
262 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)))
264 (defcustom mm-decrypt-option 'known
265 "Option of decrypting signed parts.
266 `never', not decrypt; `always', always decrypt;
267 `known', only decrypt known protocols. Otherwise, ask user."
268 :type '(choice (item always)
270 (item :tag "only known protocols" known)
271 (item :tag "ask" nil))
272 :group 'mime-security)
274 (defvar mm-viewer-completion-map
275 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
276 (set-keymap-parent map minibuffer-local-completion-map)
278 "Keymap for input viewer with completion.")
280 ;; Should we bind other key to minibuffer-complete-word?
281 (define-key mm-viewer-completion-map " " 'self-insert-command)
285 (defun mm-alist-to-plist (alist)
286 "Convert association list ALIST into the equivalent property-list form.
287 The plist is returned. This converts from
289 \((a . 1) (b . 2) (c . 3))
295 The original alist is not modified. See also `destructive-alist-to-plist'."
298 (let ((el (car alist)))
299 (setq plist (cons (cdr el) (cons (car el) plist))))
300 (setq alist (cdr alist)))
303 (defun mm-dissect-buffer (&optional no-strict-mime)
304 "Dissect the current buffer and return a list of MIME handles."
306 (let (ct ctl type subtype cte cd description id result)
308 (mail-narrow-to-head)
309 (when (or no-strict-mime
310 (mail-fetch-field "mime-version"))
311 (setq ct (mail-fetch-field "content-type")
312 ctl (ignore-errors (mail-header-parse-content-type ct))
313 cte (mail-fetch-field "content-transfer-encoding")
314 cd (mail-fetch-field "content-disposition")
315 description (mail-fetch-field "content-description")
316 id (mail-fetch-field "content-id"))))
318 (setq cte (mail-header-strip cte)))
320 (not (string-match "/" (car ctl))))
321 (mm-dissect-singlepart
322 (list mm-dissect-default-type)
323 (and cte (intern (downcase (mail-header-remove-whitespace
324 (mail-header-remove-comments
327 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
329 (setq type (split-string (car ctl) "/"))
330 (setq subtype (cadr type)
335 ((equal type "multipart")
336 (let ((mm-dissect-default-type (if (equal subtype "digest")
339 (add-text-properties 0 (length (car ctl))
340 (mm-alist-to-plist (cdr ctl)) (car ctl))
342 ;; what really needs to be done here is a way to link a
343 ;; MIME handle back to it's parent MIME handle (in a multilevel
344 ;; MIME article). That would probably require changing
345 ;; the mm-handle API so we simply store the multipart buffert
346 ;; name as a text property of the "multipart/whatever" string.
347 (add-text-properties 0 (length (car ctl))
348 (list 'buffer (mm-copy-to-buffer))
350 (cons (car ctl) (mm-dissect-multipart ctl))))
352 (mm-dissect-singlepart
354 (and cte (intern (downcase (mail-header-remove-whitespace
355 (mail-header-remove-comments
358 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
361 (when (string-match " *<\\(.*\\)> *" id)
362 (setq id (match-string 1 id)))
363 (push (cons id result) mm-content-id-alist))
366 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
368 (if (equal "text/plain" (car ctl))
371 (let ((res (mm-make-handle
372 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
373 (push (car res) mm-dissection-list)
376 (defun mm-remove-all-parts ()
377 "Remove all MIME handles."
379 (mapcar 'mm-remove-part mm-dissection-list)
380 (setq mm-dissection-list nil))
382 (defun mm-dissect-multipart (ctl)
383 (goto-char (point-min))
384 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
385 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
388 (goto-char (point-max))
389 (if (re-search-backward close-delimiter nil t)
392 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
393 (while (re-search-forward boundary end t)
394 (goto-char (match-beginning 0))
398 (narrow-to-region start (point))
399 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
401 (setq start (point)))
405 (narrow-to-region start end)
406 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
407 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
409 (defun mm-copy-to-buffer ()
410 "Copy the contents of the current buffer to a fresh buffer."
412 (let ((obuf (current-buffer))
414 (goto-char (point-min))
415 (search-forward-regexp "^\n" nil t)
417 (set-buffer (generate-new-buffer " *mm*"))
418 (insert-buffer-substring obuf beg)
421 (defun mm-display-parts (handle &optional no-default)
422 (if (stringp (car handle))
423 (mapcar 'mm-display-parts (cdr handle))
424 (if (bufferp (car handle))
426 (narrow-to-region (point) (point))
427 (mm-display-part handle)
428 (goto-char (point-max)))
429 (mapcar 'mm-display-parts handle))))
431 (defun mm-display-part (handle &optional no-default)
432 "Display the MIME part represented by HANDLE.
433 Returns nil if the part is removed; inline if displayed inline;
434 external if displayed external."
436 (mailcap-parse-mailcaps)
437 (if (mm-handle-displayed-p handle)
438 (mm-remove-part handle)
439 (let* ((type (mm-handle-media-type handle))
440 (method (mailcap-mime-info type)))
441 (if (mm-inlined-p handle)
444 (mm-display-inline handle)
448 (if (and (not method)
449 (equal "text" (car (split-string type))))
452 (mm-insert-inline handle (mm-get-part handle))
455 handle (or method 'mailcap-save-binary-file)))))))))
457 (defun mm-display-external (handle method)
458 "Display HANDLE using METHOD."
459 (let ((outbuf (current-buffer)))
460 (mm-with-unibyte-buffer
461 (if (functionp method)
462 (let ((cur (current-buffer)))
463 (if (eq method 'mailcap-save-binary-file)
465 (set-buffer (generate-new-buffer " *mm*"))
467 (mm-insert-part handle)
468 (let ((win (get-buffer-window cur t)))
470 (select-window win)))
471 (switch-to-buffer (generate-new-buffer " *mm*")))
472 (buffer-disable-undo)
473 (mm-set-buffer-file-coding-system mm-binary-coding-system)
474 (insert-buffer-substring cur)
475 (goto-char (point-min))
476 (message "Viewing with %s" method)
477 (let ((mm (current-buffer))
478 (non-viewer (assq 'non-viewer
480 (mm-handle-media-type handle) t))))
484 (mm-save-part handle))
485 (when (and (not non-viewer)
487 (mm-handle-set-undisplayer handle mm)))))
488 ;; The function is a string to be executed.
489 (mm-insert-part handle)
490 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
491 (filename (mail-content-type-get
492 (mm-handle-disposition handle) 'filename))
493 (mime-info (mailcap-mime-info
494 (mm-handle-media-type handle) t))
495 (needsterm (or (assoc "needsterm" mime-info)
496 (assoc "needsterminal" mime-info)))
497 (copiousoutput (assoc "copiousoutput" mime-info))
499 ;; We create a private sub-directory where we store our files.
501 (set-file-modes dir 448)
503 (setq file (expand-file-name (file-name-nondirectory filename)
505 (setq file (make-temp-name (expand-file-name "mm." dir))))
506 (let ((coding-system-for-write mm-binary-coding-system))
507 (write-region (point-min) (point-max) file nil 'nomesg))
508 (message "Viewing with %s" method)
511 (start-process "*display*" nil
516 method file (mm-handle-type handle)))
517 (mm-handle-set-undisplayer handle (cons file buffer)))
518 (message "Displaying %s..." (format method file))
521 (with-current-buffer outbuf
527 (call-process shell-file-name nil
529 (generate-new-buffer " *mm*"))
533 method file (mm-handle-type handle)))
534 (if (buffer-live-p buffer)
539 (ignore-errors (delete-file file))
540 (ignore-errors (delete-directory
541 (file-name-directory file)))
542 (ignore-errors (kill-buffer buffer))))))
546 (start-process "*display*"
548 (generate-new-buffer " *mm*"))
552 method file (mm-handle-type handle)))
553 (mm-handle-set-undisplayer handle (cons file buffer)))
554 (message "Displaying %s..." (format method file))
557 (defun mm-mailcap-command (method file type-list)
558 (let ((ctl (cdr type-list))
562 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
563 (push (substring method beg (match-beginning 0)) out)
564 (setq beg (match-end 0)
565 total (match-string 0 method)
566 sub (match-string 1 method))
568 ((string= total "%%")
570 ((string= total "%s")
571 (setq uses-stdin nil)
572 (push (mm-quote-arg file) out))
573 ((string= total "%t")
574 (push (mm-quote-arg (car type-list)) out))
576 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
577 (push (substring method beg (length method)) out)
581 (push (mm-quote-arg file) out)))
582 (mapconcat 'identity (nreverse out) "")))
584 (defun mm-remove-parts (handles)
585 "Remove the displayed MIME parts represented by HANDLES."
586 (if (and (listp handles)
587 (bufferp (car handles)))
588 (mm-remove-part handles)
590 (while (setq handle (pop handles))
593 (when (buffer-live-p (get-text-property 0 'buffer handle))
594 (kill-buffer (get-text-property 0 'buffer handle))))
596 (stringp (car handle)))
597 (mm-remove-parts (cdr handle)))
599 (mm-remove-part handle)))))))
601 (defun mm-destroy-parts (handles)
602 "Remove the displayed MIME parts represented by HANDLES."
603 (if (and (listp handles)
604 (bufferp (car handles)))
605 (mm-destroy-part handles)
607 (while (setq handle (pop handles))
610 (when (buffer-live-p (get-text-property 0 'buffer handle))
611 (kill-buffer (get-text-property 0 'buffer handle))))
613 (stringp (car handle)))
614 (mm-destroy-parts (cdr handle)))
616 (mm-destroy-part handle)))))))
618 (defun mm-remove-part (handle)
619 "Remove the displayed MIME part represented by HANDLE."
621 (let ((object (mm-handle-undisplayer handle)))
624 ;; Internally displayed part.
625 ((mm-annotationp object)
626 (delete-annotation object))
627 ((or (functionp object)
629 (eq (car object) 'lambda)))
631 ;; Externally displayed part.
633 (ignore-errors (delete-file (car object)))
634 (ignore-errors (delete-directory (file-name-directory (car object))))
635 (ignore-errors (kill-buffer (cdr object))))
637 (when (buffer-live-p object)
638 (kill-buffer object)))))
639 (mm-handle-set-undisplayer handle nil))))
641 (defun mm-display-inline (handle)
642 (let* ((type (mm-handle-media-type handle))
643 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
644 (funcall function handle)
645 (goto-char (point-min))))
647 (defun mm-assoc-string-match (alist type)
649 (when (string-match (car elem) type)
652 (defun mm-inlinable-p (handle)
653 "Say whether HANDLE can be displayed inline."
654 (let ((alist mm-inline-media-tests)
655 (type (mm-handle-media-type handle))
658 (when (string-match (caar alist) type)
659 (setq test (caddar alist)
661 (setq test (funcall test handle)))
665 (defun mm-automatic-display-p (handle)
666 "Say whether the user wants HANDLE to be displayed automatically."
667 (let ((methods mm-automatic-display)
668 (type (mm-handle-media-type handle))
670 (while (setq method (pop methods))
671 (when (and (not (mm-inline-override-p handle))
672 (string-match method type)
673 (mm-inlinable-p handle))
678 (defun mm-inlined-p (handle)
679 "Say whether the user wants HANDLE to be displayed automatically."
680 (let ((methods mm-inlined-types)
681 (type (mm-handle-media-type handle))
683 (while (setq method (pop methods))
684 (when (and (not (mm-inline-override-p handle))
685 (string-match method type)
686 (mm-inlinable-p handle))
691 (defun mm-attachment-override-p (handle)
692 "Say whether HANDLE should have attachment behavior overridden."
693 (let ((types mm-attachment-override-types)
694 (type (mm-handle-media-type handle))
697 (while (setq ty (pop types))
698 (when (and (string-match ty type)
699 (mm-inlinable-p handle))
700 (throw 'found t))))))
702 (defun mm-inline-override-p (handle)
703 "Say whether HANDLE should have inline behavior overridden."
704 (let ((types mm-inline-override-types)
705 (type (mm-handle-media-type handle))
708 (while (setq ty (pop types))
709 (when (string-match ty type)
710 (throw 'found t))))))
712 (defun mm-automatic-external-display-p (type)
713 "Return the user-defined method for TYPE."
714 (let ((methods mm-automatic-external-display)
716 (while (setq method (pop methods))
717 (when (string-match method type)
722 (defun mm-destroy-part (handle)
723 "Destroy the data structures connected to HANDLE."
725 (mm-remove-part handle)
726 (when (buffer-live-p (mm-handle-buffer handle))
727 (kill-buffer (mm-handle-buffer handle)))))
729 (defun mm-handle-displayed-p (handle)
730 "Say whether HANDLE is displayed or not."
731 (mm-handle-undisplayer handle))
734 ;;; Functions for outputting parts
737 (defun mm-get-part (handle)
738 "Return the contents of HANDLE as a string."
739 (mm-with-unibyte-buffer
740 (mm-insert-part handle)
743 (defun mm-insert-part (handle)
744 "Insert the contents of HANDLE in the current buffer."
745 (let ((cur (current-buffer)))
747 (if (member (mm-handle-media-supertype handle) '("text" "message"))
749 (insert-buffer-substring (mm-handle-buffer handle))
750 (mm-decode-content-transfer-encoding
751 (mm-handle-encoding handle)
752 (mm-handle-media-type handle))
753 (let ((temp (current-buffer)))
755 (insert-buffer-substring temp)))
756 (mm-with-unibyte-buffer
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)))))))
765 (defvar mm-default-directory nil)
767 (defun mm-save-part (handle)
768 "Write HANDLE to a file."
769 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
770 (filename (mail-content-type-get
771 (mm-handle-disposition handle) 'filename))
774 (setq filename (file-name-nondirectory filename)))
776 (read-file-name "Save MIME part to: "
778 (or filename name "")
779 (or mm-default-directory default-directory))))
780 (setq mm-default-directory (file-name-directory file))
781 (and (or (not (file-exists-p file))
782 (yes-or-no-p (format "File %s already exists; overwrite? "
785 (mm-save-part-to-file handle file)
788 (defun mm-save-part-to-file (handle file)
789 (mm-with-unibyte-buffer
790 (mm-insert-part handle)
791 (let ((coding-system-for-write 'binary)
792 ;; Don't re-compress .gz & al. Arguably we should make
793 ;; `file-name-handler-alist' nil, but that would chop
794 ;; ange-ftp, which is reasonable to use here.
795 (inhibit-file-name-operation 'write-region)
796 (inhibit-file-name-handlers
797 (cons 'jka-compr-handler inhibit-file-name-handlers)))
798 (write-region (point-min) (point-max) file))))
800 (defun mm-pipe-part (handle)
801 "Pipe HANDLE to a process."
802 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
804 (read-string "Shell command on MIME part: " mm-last-shell-command)))
805 (mm-with-unibyte-buffer
806 (mm-insert-part handle)
807 (shell-command-on-region (point-min) (point-max) command nil))))
809 (defun mm-interactively-view-part (handle)
810 "Display HANDLE using METHOD."
811 (let* ((type (mm-handle-media-type handle))
813 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
814 (mailcap-mime-info type 'all)))
815 (method (let ((minibuffer-local-completion-map
816 mm-viewer-completion-map))
817 (completing-read "Viewer: " methods))))
818 (when (string= method "")
819 (error "No method given"))
820 (if (string-match "^[^% \t]+$" method)
821 (setq method (concat method " %s")))
822 (mm-display-external (copy-sequence handle) method)))
824 (defun mm-preferred-alternative (handles &optional preferred)
825 "Say which of HANDLES are preferred."
826 (let ((prec (if preferred (list preferred)
827 (mm-preferred-alternative-precedence handles)))
828 p h result type handle)
829 (while (setq p (pop prec))
832 (setq handle (car h))
833 (setq type (mm-handle-media-type handle))
834 (when (and (equal p type)
835 (mm-automatic-display-p handle)
836 (or (stringp (car handle))
837 (not (mm-handle-disposition handle))
838 (equal (car (mm-handle-disposition handle))
846 (defun mm-preferred-alternative-precedence (handles)
847 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
848 (let ((seq (nreverse (mapcar #'mm-handle-media-type
850 (dolist (disc (reverse mm-discouraged-alternatives))
851 (dolist (elem (copy-sequence seq))
852 (when (string-match disc elem)
853 (setq seq (nconc (delete elem seq) (list elem))))))
856 (defun mm-get-content-id (id)
857 "Return the handle(s) referred to by ID."
858 (cdr (assoc id mm-content-id-alist)))
860 (defun mm-get-image (handle)
861 "Return an image instance based on HANDLE."
862 (let ((type (mm-handle-media-subtype handle))
864 ;; Allow some common translations.
867 ((equal type "x-pixmap")
869 ((equal type "x-xbitmap")
872 (or (mm-handle-cache handle)
873 (mm-with-unibyte-buffer
874 (mm-insert-part handle)
878 ;; Avoid testing `make-glyph' since W3 may define
879 ;; a bogus version of it.
880 (if (fboundp 'create-image)
881 (create-image (buffer-string) (intern type) 'data-p)
884 ;; xbm images require special handling, since
885 ;; the only way to create glyphs from these
886 ;; (without a ton of work) is to write them
887 ;; out to a file, and then create a file
889 (let ((file (make-temp-name
890 (expand-file-name "emm.xbm"
894 (write-region (point-min) (point-max) file)
895 (make-glyph (list (cons 'x file))))
897 (delete-file file)))))
900 (vector (intern type) :data (buffer-string))))))))
901 (mm-handle-set-cache handle spec))))))
903 (defun mm-image-fit-p (handle)
904 "Say whether the image in HANDLE will fit the current window."
905 (let ((image (mm-get-image handle)))
906 (if (fboundp 'glyph-width)
907 ;; XEmacs' glyphs can actually tell us about their width, so
908 ;; lets be nice and smart about them.
909 (or mm-inline-large-images
910 (and (< (glyph-width image) (window-pixel-width))
911 (< (glyph-height image) (window-pixel-height))))
912 (let* ((size (image-size image))
915 (or mm-inline-large-images
916 (and (< h (1- (window-height))) ; Don't include mode line.
917 (< w (window-width))))))))
919 (defun mm-valid-image-format-p (format)
920 "Say whether FORMAT can be displayed natively by Emacs."
923 ((fboundp 'valid-image-instantiator-format-p)
924 (valid-image-instantiator-format-p format))
926 ((fboundp 'image-type-available-p)
927 (and (display-graphic-p)
928 (image-type-available-p format)))
929 ;; Nobody else can do images yet.
933 (defun mm-valid-and-fit-image-p (format handle)
934 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
935 (and (mm-valid-image-format-p format)
936 (mm-image-fit-p handle)))
938 (defun mm-find-part-by-type (handles type &optional notp recursive)
939 "Search in HANDLES for part with TYPE.
940 If NOTP, returns first non-matching part.
941 If RECURSIVE, search recursively."
944 (if (and recursive (stringp (caar handles)))
945 (if (setq handle (mm-find-part-by-type (cdar handles) type
949 (not (equal (mm-handle-media-type (car handles)) type))
950 (equal (mm-handle-media-type (car handles)) type))
951 (setq handle (car handles)
953 (setq handles (cdr handles)))
956 (defun mm-find-raw-part-by-type (ctl type &optional notp)
957 (goto-char (point-min))
958 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
959 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
962 (goto-char (point-max))
963 (if (re-search-backward close-delimiter nil t)
967 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
968 (while (and (not result)
969 (re-search-forward boundary end t))
970 (goto-char (match-beginning 0))
974 (narrow-to-region start (point))
975 (when (let ((ctl (ignore-errors
976 (mail-header-parse-content-type
977 (mail-fetch-field "content-type")))))
979 (not (equal (car ctl) type))
980 (equal (car ctl) type)))
981 (setq result (buffer-substring (point-min) (point-max)))))))
983 (setq start (point)))
984 (when (and (not result) start)
987 (narrow-to-region start end)
988 (when (let ((ctl (ignore-errors
989 (mail-header-parse-content-type
990 (mail-fetch-field "content-type")))))
992 (not (equal (car ctl) type))
993 (equal (car ctl) type)))
994 (setq result (buffer-substring (point-min) (point-max)))))))
997 (defvar mm-security-handle nil)
999 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1000 ;; HANDLE could be a CTL.
1002 (put-text-property 0 (length (car handle)) parameter value
1005 (defun mm-possibly-verify-or-decrypt (parts ctl)
1006 (let ((subtype (cadr (split-string (car ctl) "/")))
1007 (mm-security-handle ctl) ;; (car CTL) is the type.
1008 protocol func functest)
1010 ((equal subtype "signed")
1011 (unless (and (setq protocol (mail-content-type-get ctl 'protocol))
1012 (not (equal protocol "multipart/mixed")))
1013 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1014 (let ((protocols mm-verify-function-alist))
1016 (if (and (or (not (setq functest (nth 3 (car protocols))))
1017 (funcall functest parts ctl))
1018 (mm-find-part-by-type parts (caar protocols) nil t))
1019 (setq protocol (caar protocols)
1021 (setq protocols (cdr protocols))))))
1022 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1024 ((eq mm-verify-option 'never) nil)
1025 ((eq mm-verify-option 'always) t)
1026 ((eq mm-verify-option 'known)
1028 (or (not (setq functest
1029 (nth 3 (assoc protocol
1030 mm-verify-function-alist))))
1031 (funcall functest parts ctl))))
1033 (format "Verify signed (%s) part? "
1034 (or (nth 2 (assoc protocol mm-verify-function-alist))
1035 (format "protocol=%s" protocol))))))
1038 (funcall func parts ctl)
1039 (mm-set-handle-multipart-parameter
1040 mm-security-handle 'gnus-details
1041 (format "Unknown sign protocol (%s)" protocol))))))
1042 ((equal subtype "encrypted")
1043 (unless (setq protocol (mail-content-type-get ctl 'protocol))
1044 ;; The message is broken.
1045 (let ((parts parts))
1047 (if (assoc (mm-handle-media-type (car parts))
1048 mm-decrypt-function-alist)
1049 (setq protocol (mm-handle-media-type (car parts))
1051 (setq parts (cdr parts))))))
1052 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1054 ((eq mm-decrypt-option 'never) nil)
1055 ((eq mm-decrypt-option 'always) t)
1056 ((eq mm-decrypt-option 'known)
1058 (or (not (setq functest
1059 (nth 3 (assoc protocol
1060 mm-decrypt-function-alist))))
1061 (funcall functest parts ctl))))
1063 (format "Decrypt (%s) part? "
1064 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1065 (format "protocol=%s" protocol))))))
1068 (setq parts (funcall func parts ctl))
1069 (mm-set-handle-multipart-parameter
1070 mm-security-handle 'gnus-details
1071 (format "Unknown encrypt protocol (%s)" protocol))))))
1075 (provide 'mm-decode)
1077 ;;; mm-decode.el ends here