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.
30 (eval-when-compile (require 'cl))
33 (autoload 'mm-inline-partial "mm-partial")
34 (autoload 'mm-inline-external-body "mm-extern")
35 (autoload 'mm-insert-inline "mm-view"))
37 (defgroup mime-display ()
38 "Display of MIME in mail and news articles."
39 :link '(custom-manual "(emacs-mime)Customization")
45 (defgroup mime-security ()
46 "MIME security in mail and news articles."
47 :link '(custom-manual "(emacs-mime)Customization")
52 ;;; Convenience macros.
54 (defmacro mm-handle-buffer (handle)
56 (defmacro mm-handle-type (handle)
58 (defsubst mm-handle-media-type (handle)
59 (if (stringp (car handle))
61 (car (mm-handle-type handle))))
62 (defsubst mm-handle-media-supertype (handle)
63 (car (split-string (mm-handle-media-type handle) "/")))
64 (defsubst mm-handle-media-subtype (handle)
65 (cadr (split-string (mm-handle-media-type handle) "/")))
66 (defmacro mm-handle-encoding (handle)
68 (defmacro mm-handle-undisplayer (handle)
70 (defmacro mm-handle-set-undisplayer (handle function)
71 `(setcar (nthcdr 3 ,handle) ,function))
72 (defmacro mm-handle-disposition (handle)
74 (defmacro mm-handle-description (handle)
76 (defmacro mm-handle-cache (handle)
78 (defmacro mm-handle-set-cache (handle contents)
79 `(setcar (nthcdr 6 ,handle) ,contents))
80 (defmacro mm-handle-id (handle)
82 (defmacro mm-handle-multipart-original-buffer (handle)
83 `(get-text-property 0 'buffer (car ,handle)))
84 (defmacro mm-handle-multipart-from (handle)
85 `(get-text-property 0 'from (car ,handle)))
86 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
87 `(get-text-property 0 ,parameter (car ,handle)))
89 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
90 disposition description cache
92 `(list ,buffer ,type ,encoding ,undisplayer
93 ,disposition ,description ,cache ,id))
95 (defcustom mm-inline-media-tests
99 (mm-valid-and-fit-image-p 'jpeg handle)))
103 (mm-valid-and-fit-image-p 'png handle)))
107 (mm-valid-and-fit-image-p 'gif handle)))
111 (mm-valid-and-fit-image-p 'tiff handle)) )
115 (mm-valid-and-fit-image-p 'xbm handle)))
119 (mm-valid-and-fit-image-p 'xbm handle)))
123 (mm-valid-and-fit-image-p 'xpm handle)))
127 (mm-valid-and-fit-image-p 'xpm handle)))
131 (mm-valid-and-fit-image-p 'bmp handle)))
132 ("image/x-portable-bitmap"
135 (mm-valid-and-fit-image-p 'pbm handle)))
136 ("text/plain" mm-inline-text identity)
137 ("text/enriched" mm-inline-text identity)
138 ("text/richtext" mm-inline-text identity)
139 ("text/x-patch" mm-display-patch-inline
141 (locate-library "diff-mode")))
142 ("application/emacs-lisp" mm-display-elisp-inline identity)
146 (locate-library "w3")))
150 (or (featurep 'vcard)
151 (locate-library "vcard"))))
152 ("message/delivery-status" mm-inline-text identity)
153 ("message/rfc822" mm-inline-message identity)
154 ("message/partial" mm-inline-partial identity)
155 ("message/external-body" mm-inline-external-body identity)
156 ("text/.*" mm-inline-text identity)
157 ("audio/wav" mm-inline-audio
159 (and (or (featurep 'nas-sound) (featurep 'native-sound))
160 (device-sound-enabled-p))))
164 (and (or (featurep 'nas-sound) (featurep 'native-sound))
165 (device-sound-enabled-p))))
166 ("application/pgp-signature" ignore identity)
167 ("application/x-pkcs7-signature" ignore identity)
168 ("application/pkcs7-signature" ignore identity)
169 ("multipart/alternative" ignore identity)
170 ("multipart/mixed" ignore identity)
171 ("multipart/related" ignore identity))
172 "Alist of media types/tests saying whether types can be displayed inline."
173 :type '(repeat (list (string :tag "MIME type")
174 (function :tag "Display function")
175 (function :tag "Display test")))
176 :group 'mime-display)
178 (defcustom mm-inlined-types
179 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
180 "message/partial" "message/external-body" "application/emacs-lisp"
181 "application/pgp-signature" "application/x-pkcs7-signature"
182 "application/pkcs7-signature")
183 "List of media types that are to be displayed inline."
184 :type '(repeat string)
185 :group 'mime-display)
187 (defcustom mm-automatic-display
188 '("text/plain" "text/enriched" "text/richtext" "text/html"
189 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
190 "message/rfc822" "text/x-patch" "application/pgp-signature"
191 "application/emacs-lisp" "application/x-pkcs7-signature"
192 "application/pkcs7-signature")
193 "A list of MIME types to be displayed automatically."
194 :type '(repeat string)
195 :group 'mime-display)
197 (defcustom mm-attachment-override-types '("text/x-vcard")
198 "Types to have \"attachment\" ignored if they can be displayed inline."
199 :type '(repeat string)
200 :group 'mime-display)
202 (defcustom mm-inline-override-types nil
203 "Types to be treated as attachments even if they can be displayed inline."
204 :type '(repeat string)
205 :group 'mime-display)
207 (defcustom mm-automatic-external-display nil
208 "List of MIME type regexps that will be displayed externally automatically."
209 :type '(repeat string)
210 :group 'mime-display)
212 (defcustom mm-discouraged-alternatives nil
213 "List of MIME types that are discouraged when viewing multipart/alternative.
214 Viewing agents are supposed to view the last possible part of a message,
215 as that is supposed to be the richest. However, users may prefer other
216 types instead, and this list says what types are most unwanted. If,
217 for instance, text/html parts are very unwanted, and text/richtext are
218 somewhat unwanted, then the value of this variable should be set
221 (\"text/html\" \"text/richtext\")"
222 :type '(repeat string)
223 :group 'mime-display)
225 (defvar mm-tmp-directory
226 (cond ((fboundp 'temp-directory) (temp-directory))
227 ((boundp 'temporary-file-directory) temporary-file-directory)
229 "Where mm will store its temporary files.")
231 (defcustom mm-inline-large-images nil
232 "If non-nil, then all images fit in the buffer."
234 :group 'mime-display)
236 ;;; Internal variables.
238 (defvar mm-dissection-list nil)
239 (defvar mm-last-shell-command "")
240 (defvar mm-content-id-alist nil)
242 ;; According to RFC2046, in particular, in a digest, the default
243 ;; Content-Type value for a body part is changed from "text/plain" to
245 (defvar mm-dissect-default-type "text/plain")
247 (autoload 'mml2015-verify "mml2015")
248 (autoload 'mml2015-verify-test "mml2015")
249 (autoload 'mml-smime-verify "mml-smime")
250 (autoload 'mml-smime-verify-test "mml-smime")
252 (defvar mm-verify-function-alist
253 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
254 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
255 mm-uu-pgp-signed-test)
256 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
257 mml-smime-verify-test)
258 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
259 mml-smime-verify-test)))
261 (defcustom mm-verify-option 'never
262 "Option of verifying signed parts.
263 `never', not verify; `always', always verify;
264 `known', only verify known protocols. Otherwise, ask user."
265 :type '(choice (item always)
267 (item :tag "only known protocols" known)
268 (item :tag "ask" nil))
269 :group 'mime-security)
271 (autoload 'mml2015-decrypt "mml2015")
272 (autoload 'mml2015-decrypt-test "mml2015")
274 (defvar mm-decrypt-function-alist
275 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
276 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
277 mm-uu-pgp-encrypted-test)))
279 (defcustom mm-decrypt-option nil
280 "Option of decrypting encrypted parts.
281 `never', not decrypt; `always', always decrypt;
282 `known', only decrypt known protocols. Otherwise, ask user."
283 :type '(choice (item always)
285 (item :tag "only known protocols" known)
286 (item :tag "ask" nil))
287 :group 'mime-security)
289 (defvar mm-viewer-completion-map
290 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
291 (set-keymap-parent map minibuffer-local-completion-map)
293 "Keymap for input viewer with completion.")
295 ;; Should we bind other key to minibuffer-complete-word?
296 (define-key mm-viewer-completion-map " " 'self-insert-command)
298 (defvar mm-viewer-completion-map
299 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
300 (set-keymap-parent map minibuffer-local-completion-map)
302 "Keymap for input viewer with completion.")
304 ;; Should we bind other key to minibuffer-complete-word?
305 (define-key mm-viewer-completion-map " " 'self-insert-command)
309 (defun mm-alist-to-plist (alist)
310 "Convert association list ALIST into the equivalent property-list form.
311 The plist is returned. This converts from
313 \((a . 1) (b . 2) (c . 3))
319 The original alist is not modified. See also `destructive-alist-to-plist'."
322 (let ((el (car alist)))
323 (setq plist (cons (cdr el) (cons (car el) plist))))
324 (setq alist (cdr alist)))
327 (defun mm-dissect-buffer (&optional no-strict-mime)
328 "Dissect the current buffer and return a list of MIME handles."
330 (let (ct ctl type subtype cte cd description id result from)
332 (mail-narrow-to-head)
333 (when (or no-strict-mime
334 (mail-fetch-field "mime-version"))
335 (setq ct (mail-fetch-field "content-type")
336 ctl (ignore-errors (mail-header-parse-content-type ct))
337 cte (mail-fetch-field "content-transfer-encoding")
338 cd (mail-fetch-field "content-disposition")
339 description (mail-fetch-field "content-description")
340 from (mail-fetch-field "from")
341 id (mail-fetch-field "content-id"))
342 ;; FIXME: In some circumstances, this code is running within
343 ;; an unibyte macro. mail-extract-address-components
344 ;; creates unibyte buffers. This `if', though not a perfect
345 ;; solution, avoids most of them.
347 (setq from (cadr (mail-extract-address-components from))))))
349 (setq cte (mail-header-strip cte)))
351 (not (string-match "/" (car ctl))))
352 (mm-dissect-singlepart
353 (list mm-dissect-default-type)
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)))
360 (setq type (split-string (car ctl) "/"))
361 (setq subtype (cadr type)
366 ((equal type "multipart")
367 (let ((mm-dissect-default-type (if (equal subtype "digest")
370 (add-text-properties 0 (length (car ctl))
371 (mm-alist-to-plist (cdr ctl)) (car ctl))
373 ;; what really needs to be done here is a way to link a
374 ;; MIME handle back to it's parent MIME handle (in a multilevel
375 ;; MIME article). That would probably require changing
376 ;; the mm-handle API so we simply store the multipart buffert
377 ;; name as a text property of the "multipart/whatever" string.
378 (add-text-properties 0 (length (car ctl))
379 (list 'buffer (mm-copy-to-buffer))
381 (add-text-properties 0 (length (car ctl))
384 (cons (car ctl) (mm-dissect-multipart ctl))))
386 (mm-dissect-singlepart
388 (and cte (intern (downcase (mail-header-remove-whitespace
389 (mail-header-remove-comments
392 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
395 (when (string-match " *<\\(.*\\)> *" id)
396 (setq id (match-string 1 id)))
397 (push (cons id result) mm-content-id-alist))
400 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
402 (if (equal "text/plain" (car ctl))
405 (let ((res (mm-make-handle
406 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
407 (push (car res) mm-dissection-list)
410 (defun mm-remove-all-parts ()
411 "Remove all MIME handles."
413 (mapcar 'mm-remove-part mm-dissection-list)
414 (setq mm-dissection-list nil))
416 (defun mm-dissect-multipart (ctl)
417 (goto-char (point-min))
418 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
419 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
422 (goto-char (point-max))
423 (if (re-search-backward close-delimiter nil t)
426 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
427 (while (and (< (point) end) (re-search-forward boundary end t))
428 (goto-char (match-beginning 0))
432 (narrow-to-region start (point))
433 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
435 (setq start (point)))
436 (when (and start (< start end))
439 (narrow-to-region start end)
440 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
441 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
443 (defun mm-copy-to-buffer ()
444 "Copy the contents of the current buffer to a fresh buffer."
446 (let ((obuf (current-buffer))
448 (goto-char (point-min))
449 (search-forward-regexp "^\n" nil t)
451 (set-buffer (generate-new-buffer " *mm*"))
452 (insert-buffer-substring obuf beg)
455 (defun mm-display-parts (handle &optional no-default)
456 (if (stringp (car handle))
457 (mapcar 'mm-display-parts (cdr handle))
458 (if (bufferp (car handle))
460 (narrow-to-region (point) (point))
461 (mm-display-part handle)
462 (goto-char (point-max)))
463 (mapcar 'mm-display-parts handle))))
465 (defun mm-display-part (handle &optional no-default)
466 "Display the MIME part represented by HANDLE.
467 Returns nil if the part is removed; inline if displayed inline;
468 external if displayed external."
470 (mailcap-parse-mailcaps)
471 (if (mm-handle-displayed-p handle)
472 (mm-remove-part handle)
473 (let* ((type (mm-handle-media-type handle))
474 (method (mailcap-mime-info type)))
475 (if (mm-inlined-p handle)
478 (mm-display-inline handle)
482 (if (and (not method)
483 (equal "text" (car (split-string type))))
486 (mm-insert-inline handle (mm-get-part handle))
489 handle (or method 'mailcap-save-binary-file)))))))))
491 (defun mm-display-external (handle method)
492 "Display HANDLE using METHOD."
493 (let ((outbuf (current-buffer)))
494 (mm-with-unibyte-buffer
495 (if (functionp method)
496 (let ((cur (current-buffer)))
497 (if (eq method 'mailcap-save-binary-file)
499 (set-buffer (generate-new-buffer " *mm*"))
501 (mm-insert-part handle)
502 (let ((win (get-buffer-window cur t)))
504 (select-window win)))
505 (switch-to-buffer (generate-new-buffer " *mm*")))
506 (buffer-disable-undo)
507 (mm-set-buffer-file-coding-system mm-binary-coding-system)
508 (insert-buffer-substring cur)
509 (goto-char (point-min))
510 (message "Viewing with %s" method)
511 (let ((mm (current-buffer))
512 (non-viewer (assq 'non-viewer
514 (mm-handle-media-type handle) t))))
518 (mm-save-part handle))
519 (when (and (not non-viewer)
521 (mm-handle-set-undisplayer handle mm)))))
522 ;; The function is a string to be executed.
523 (mm-insert-part handle)
524 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
525 (filename (mail-content-type-get
526 (mm-handle-disposition handle) 'filename))
527 (mime-info (mailcap-mime-info
528 (mm-handle-media-type handle) t))
529 (needsterm (or (assoc "needsterm" mime-info)
530 (assoc "needsterminal" mime-info)))
531 (copiousoutput (assoc "copiousoutput" mime-info))
533 ;; We create a private sub-directory where we store our files.
535 (set-file-modes dir 448)
537 (setq file (expand-file-name (file-name-nondirectory filename)
539 (setq file (make-temp-name (expand-file-name "mm." dir))))
540 (let ((coding-system-for-write mm-binary-coding-system))
541 (write-region (point-min) (point-max) file nil 'nomesg))
542 (message "Viewing with %s" method)
545 (start-process "*display*" nil
550 method file (mm-handle-type handle)))
551 (mm-handle-set-undisplayer handle (cons file buffer)))
552 (message "Displaying %s..." (format method file))
555 (with-current-buffer outbuf
561 (call-process shell-file-name nil
563 (generate-new-buffer " *mm*"))
567 method file (mm-handle-type handle)))
568 (if (buffer-live-p buffer)
573 (ignore-errors (delete-file file))
574 (ignore-errors (delete-directory
575 (file-name-directory file)))
576 (ignore-errors (kill-buffer buffer))))))
580 (start-process "*display*"
582 (generate-new-buffer " *mm*"))
586 method file (mm-handle-type handle)))
587 (mm-handle-set-undisplayer handle (cons file buffer)))
588 (message "Displaying %s..." (format method file))
591 (defun mm-mailcap-command (method file type-list)
592 (let ((ctl (cdr type-list))
596 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
597 (push (substring method beg (match-beginning 0)) out)
598 (setq beg (match-end 0)
599 total (match-string 0 method)
600 sub (match-string 1 method))
602 ((string= total "%%")
604 ((string= total "%s")
605 (setq uses-stdin nil)
606 (push (mm-quote-arg file) out))
607 ((string= total "%t")
608 (push (mm-quote-arg (car type-list)) out))
610 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
611 (push (substring method beg (length method)) out)
615 (push (mm-quote-arg file) out)))
616 (mapconcat 'identity (nreverse out) "")))
618 (defun mm-remove-parts (handles)
619 "Remove the displayed MIME parts represented by HANDLES."
620 (if (and (listp handles)
621 (bufferp (car handles)))
622 (mm-remove-part handles)
624 (while (setq handle (pop handles))
627 (when (buffer-live-p (get-text-property 0 'buffer handle))
628 (kill-buffer (get-text-property 0 'buffer handle))))
630 (stringp (car handle)))
631 (mm-remove-parts (cdr handle)))
633 (mm-remove-part handle)))))))
635 (defun mm-destroy-parts (handles)
636 "Remove the displayed MIME parts represented by HANDLES."
637 (if (and (listp handles)
638 (bufferp (car handles)))
639 (mm-destroy-part handles)
641 (while (setq handle (pop handles))
644 (when (buffer-live-p (get-text-property 0 'buffer handle))
645 (kill-buffer (get-text-property 0 'buffer handle))))
647 (stringp (car handle)))
648 (mm-destroy-parts handle))
650 (mm-destroy-part handle)))))))
652 (defun mm-remove-part (handle)
653 "Remove the displayed MIME part represented by HANDLE."
655 (let ((object (mm-handle-undisplayer handle)))
658 ;; Internally displayed part.
659 ((mm-annotationp object)
660 (delete-annotation object))
661 ((or (functionp object)
663 (eq (car object) 'lambda)))
665 ;; Externally displayed part.
667 (ignore-errors (delete-file (car object)))
668 (ignore-errors (delete-directory (file-name-directory (car object))))
669 (ignore-errors (kill-buffer (cdr object))))
671 (when (buffer-live-p object)
672 (kill-buffer object)))))
673 (mm-handle-set-undisplayer handle nil))))
675 (defun mm-display-inline (handle)
676 (let* ((type (mm-handle-media-type handle))
677 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
678 (funcall function handle)
679 (goto-char (point-min))))
681 (defun mm-assoc-string-match (alist type)
683 (when (string-match (car elem) type)
686 (defun mm-inlinable-p (handle)
687 "Say whether HANDLE can be displayed inline."
688 (let ((alist mm-inline-media-tests)
689 (type (mm-handle-media-type handle))
692 (when (string-match (caar alist) type)
693 (setq test (caddar alist)
695 (setq test (funcall test handle)))
699 (defun mm-automatic-display-p (handle)
700 "Say whether the user wants HANDLE to be displayed automatically."
701 (let ((methods mm-automatic-display)
702 (type (mm-handle-media-type handle))
704 (while (setq method (pop methods))
705 (when (and (not (mm-inline-override-p handle))
706 (string-match method type)
707 (mm-inlinable-p handle))
712 (defun mm-inlined-p (handle)
713 "Say whether the user wants HANDLE to be displayed automatically."
714 (let ((methods mm-inlined-types)
715 (type (mm-handle-media-type handle))
717 (while (setq method (pop methods))
718 (when (and (not (mm-inline-override-p handle))
719 (string-match method type)
720 (mm-inlinable-p handle))
725 (defun mm-attachment-override-p (handle)
726 "Say whether HANDLE should have attachment behavior overridden."
727 (let ((types mm-attachment-override-types)
728 (type (mm-handle-media-type handle))
731 (while (setq ty (pop types))
732 (when (and (string-match ty type)
733 (mm-inlinable-p handle))
734 (throw 'found t))))))
736 (defun mm-inline-override-p (handle)
737 "Say whether HANDLE should have inline behavior overridden."
738 (let ((types mm-inline-override-types)
739 (type (mm-handle-media-type handle))
742 (while (setq ty (pop types))
743 (when (string-match ty type)
744 (throw 'found t))))))
746 (defun mm-automatic-external-display-p (type)
747 "Return the user-defined method for TYPE."
748 (let ((methods mm-automatic-external-display)
750 (while (setq method (pop methods))
751 (when (string-match method type)
756 (defun mm-destroy-part (handle)
757 "Destroy the data structures connected to HANDLE."
759 (mm-remove-part handle)
760 (when (buffer-live-p (mm-handle-buffer handle))
761 (kill-buffer (mm-handle-buffer handle)))))
763 (defun mm-handle-displayed-p (handle)
764 "Say whether HANDLE is displayed or not."
765 (mm-handle-undisplayer handle))
768 ;;; Functions for outputting parts
771 (defun mm-get-part (handle)
772 "Return the contents of HANDLE as a string."
773 (mm-with-unibyte-buffer
774 (insert (with-current-buffer (mm-handle-buffer handle)
775 (mm-with-unibyte-current-buffer-mule4
777 (mm-decode-content-transfer-encoding
778 (mm-handle-encoding handle)
779 (mm-handle-media-type handle))
782 (defun mm-insert-part (handle)
783 "Insert the contents of HANDLE in the current buffer."
784 (let ((cur (current-buffer)))
786 (if (member (mm-handle-media-supertype handle) '("text" "message"))
788 (insert-buffer-substring (mm-handle-buffer handle))
789 (mm-decode-content-transfer-encoding
790 (mm-handle-encoding handle)
791 (mm-handle-media-type handle))
792 (let ((temp (current-buffer)))
794 (insert-buffer-substring temp)))
795 (mm-with-unibyte-buffer
796 (insert-buffer-substring (mm-handle-buffer handle))
797 (mm-decode-content-transfer-encoding
798 (mm-handle-encoding handle)
799 (mm-handle-media-type handle))
800 (let ((temp (current-buffer)))
802 (insert-buffer-substring temp)))))))
804 (defvar mm-default-directory nil)
806 (defun mm-save-part (handle)
807 "Write HANDLE to a file."
808 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
809 (filename (mail-content-type-get
810 (mm-handle-disposition handle) 'filename))
813 (setq filename (file-name-nondirectory filename)))
815 (read-file-name "Save MIME part to: "
817 (or filename name "")
818 (or mm-default-directory default-directory))))
819 (setq mm-default-directory (file-name-directory file))
820 (and (or (not (file-exists-p file))
821 (yes-or-no-p (format "File %s already exists; overwrite? "
824 (mm-save-part-to-file handle file)
827 (defun mm-save-part-to-file (handle file)
828 (mm-with-unibyte-buffer
829 (mm-insert-part handle)
830 (let ((coding-system-for-write 'binary)
831 ;; Don't re-compress .gz & al. Arguably we should make
832 ;; `file-name-handler-alist' nil, but that would chop
833 ;; ange-ftp, which is reasonable to use here.
834 (inhibit-file-name-operation 'write-region)
835 (inhibit-file-name-handlers
836 (cons 'jka-compr-handler inhibit-file-name-handlers)))
837 (write-region (point-min) (point-max) file))))
839 (defun mm-pipe-part (handle)
840 "Pipe HANDLE to a process."
841 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
843 (read-string "Shell command on MIME part: " mm-last-shell-command)))
844 (mm-with-unibyte-buffer
845 (mm-insert-part handle)
846 (shell-command-on-region (point-min) (point-max) command nil))))
848 (defun mm-interactively-view-part (handle)
849 "Display HANDLE using METHOD."
850 (let* ((type (mm-handle-media-type handle))
852 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
853 (mailcap-mime-info type 'all)))
854 (method (let ((minibuffer-local-completion-map
855 mm-viewer-completion-map))
856 (completing-read "Viewer: " methods))))
857 (when (string= method "")
858 (error "No method given"))
859 (if (string-match "^[^% \t]+$" method)
860 (setq method (concat method " %s")))
861 (mm-display-external handle method)))
863 (defun mm-preferred-alternative (handles &optional preferred)
864 "Say which of HANDLES are preferred."
865 (let ((prec (if preferred (list preferred)
866 (mm-preferred-alternative-precedence handles)))
867 p h result type handle)
868 (while (setq p (pop prec))
871 (setq handle (car h))
872 (setq type (mm-handle-media-type handle))
873 (when (and (equal p type)
874 (mm-automatic-display-p handle)
875 (or (stringp (car handle))
876 (not (mm-handle-disposition handle))
877 (equal (car (mm-handle-disposition handle))
885 (defun mm-preferred-alternative-precedence (handles)
886 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
887 (let ((seq (nreverse (mapcar #'mm-handle-media-type
889 (dolist (disc (reverse mm-discouraged-alternatives))
890 (dolist (elem (copy-sequence seq))
891 (when (string-match disc elem)
892 (setq seq (nconc (delete elem seq) (list elem))))))
895 (defun mm-get-content-id (id)
896 "Return the handle(s) referred to by ID."
897 (cdr (assoc id mm-content-id-alist)))
899 (defun mm-get-image (handle)
900 "Return an image instance based on HANDLE."
901 (let ((type (mm-handle-media-subtype handle))
903 ;; Allow some common translations.
906 ((equal type "x-pixmap")
908 ((equal type "x-xbitmap")
910 ((equal type "x-portable-bitmap")
913 (or (mm-handle-cache handle)
914 (mm-with-unibyte-buffer
915 (mm-insert-part handle)
919 ;; Avoid testing `make-glyph' since W3 may define
920 ;; a bogus version of it.
921 (if (fboundp 'create-image)
922 (create-image (buffer-string) (intern type) 'data-p)
925 ;; xbm images require special handling, since
926 ;; the only way to create glyphs from these
927 ;; (without a ton of work) is to write them
928 ;; out to a file, and then create a file
930 (let ((file (make-temp-name
931 (expand-file-name "emm.xbm"
935 (write-region (point-min) (point-max) file)
936 (make-glyph (list (cons 'x file))))
938 (delete-file file)))))
941 (vector (intern type) :data (buffer-string))))))))
942 (mm-handle-set-cache handle spec))))))
944 (defun mm-image-fit-p (handle)
945 "Say whether the image in HANDLE will fit the current window."
946 (let ((image (mm-get-image handle)))
947 (if (fboundp 'glyph-width)
948 ;; XEmacs' glyphs can actually tell us about their width, so
949 ;; lets be nice and smart about them.
950 (or mm-inline-large-images
951 (and (< (glyph-width image) (window-pixel-width))
952 (< (glyph-height image) (window-pixel-height))))
953 (let* ((size (image-size image))
956 (or mm-inline-large-images
957 (and (< h (1- (window-height))) ; Don't include mode line.
958 (< w (window-width))))))))
960 (defun mm-valid-image-format-p (format)
961 "Say whether FORMAT can be displayed natively by Emacs."
964 ((fboundp 'valid-image-instantiator-format-p)
965 (valid-image-instantiator-format-p format))
967 ((fboundp 'image-type-available-p)
968 (and (display-graphic-p)
969 (image-type-available-p format)))
970 ;; Nobody else can do images yet.
974 (defun mm-valid-and-fit-image-p (format handle)
975 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
976 (and (mm-valid-image-format-p format)
977 (mm-image-fit-p handle)))
979 (defun mm-find-part-by-type (handles type &optional notp recursive)
980 "Search in HANDLES for part with TYPE.
981 If NOTP, returns first non-matching part.
982 If RECURSIVE, search recursively."
985 (if (and recursive (stringp (caar handles)))
986 (if (setq handle (mm-find-part-by-type (cdar handles) type
990 (not (equal (mm-handle-media-type (car handles)) type))
991 (equal (mm-handle-media-type (car handles)) type))
992 (setq handle (car handles)
994 (setq handles (cdr handles)))
997 (defun mm-find-raw-part-by-type (ctl type &optional notp)
998 (goto-char (point-min))
999 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1001 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1003 (end (save-excursion
1004 (goto-char (point-max))
1005 (if (re-search-backward close-delimiter nil t)
1009 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1010 (while (and (not result)
1011 (re-search-forward boundary end t))
1012 (goto-char (match-beginning 0))
1016 (narrow-to-region start (1- (point)))
1017 (when (let ((ctl (ignore-errors
1018 (mail-header-parse-content-type
1019 (mail-fetch-field "content-type")))))
1021 (not (equal (car ctl) type))
1022 (equal (car ctl) type)))
1023 (setq result (buffer-substring (point-min) (point-max)))))))
1025 (setq start (point)))
1026 (when (and (not result) start)
1029 (narrow-to-region start end)
1030 (when (let ((ctl (ignore-errors
1031 (mail-header-parse-content-type
1032 (mail-fetch-field "content-type")))))
1034 (not (equal (car ctl) type))
1035 (equal (car ctl) type)))
1036 (setq result (buffer-substring (point-min) (point-max)))))))
1039 (defvar mm-security-handle nil)
1041 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1042 ;; HANDLE could be a CTL.
1044 (put-text-property 0 (length (car handle)) parameter value
1047 (defun mm-possibly-verify-or-decrypt (parts ctl)
1048 (let ((subtype (cadr (split-string (car ctl) "/")))
1049 (mm-security-handle ctl) ;; (car CTL) is the type.
1050 protocol func functest)
1052 ((equal subtype "signed")
1053 (unless (and (setq protocol
1054 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1055 (not (equal protocol "multipart/mixed")))
1056 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1057 (let ((protocols mm-verify-function-alist))
1059 (if (and (or (not (setq functest (nth 3 (car protocols))))
1060 (funcall functest parts ctl))
1061 (mm-find-part-by-type parts (caar protocols) nil t))
1062 (setq protocol (caar protocols)
1064 (setq protocols (cdr protocols))))))
1065 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1067 ((eq mm-verify-option 'never) nil)
1068 ((eq mm-verify-option 'always) t)
1069 ((eq mm-verify-option 'known)
1071 (or (not (setq functest
1072 (nth 3 (assoc protocol
1073 mm-verify-function-alist))))
1074 (funcall functest parts ctl))))
1076 (format "Verify signed (%s) part? "
1077 (or (nth 2 (assoc protocol mm-verify-function-alist))
1078 (format "protocol=%s" protocol))))))
1081 (funcall func parts ctl)
1082 (mm-set-handle-multipart-parameter
1083 mm-security-handle 'gnus-details
1084 (format "Unknown sign protocol (%s)" protocol))))))
1085 ((equal subtype "encrypted")
1086 (unless (setq protocol
1087 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1088 ;; The message is broken.
1089 (let ((parts parts))
1091 (if (assoc (mm-handle-media-type (car parts))
1092 mm-decrypt-function-alist)
1093 (setq protocol (mm-handle-media-type (car parts))
1095 (setq parts (cdr parts))))))
1096 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1098 ((eq mm-decrypt-option 'never) nil)
1099 ((eq mm-decrypt-option 'always) t)
1100 ((eq mm-decrypt-option 'known)
1102 (or (not (setq functest
1103 (nth 3 (assoc protocol
1104 mm-decrypt-function-alist))))
1105 (funcall functest parts ctl))))
1107 (format "Decrypt (%s) part? "
1108 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1109 (format "protocol=%s" protocol))))))
1112 (setq parts (funcall func parts ctl))
1113 (mm-set-handle-multipart-parameter
1114 mm-security-handle 'gnus-details
1115 (format "Unknown encrypt protocol (%s)" protocol))))))
1119 (defun mm-multiple-handles (handles)
1120 (and (listp (car handles))
1121 (> (length handles) 1)))
1123 (defun mm-merge-handles (handles1 handles2)
1125 (if (listp (car handles1))
1128 (if (listp (car handles2))
1132 (provide 'mm-decode)
1134 ;;; mm-decode.el ends here