1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; Jaap-Henk Hoepman (jhh@xs4all.nl):
27 ;; Added support for delayed destroy of external MIME viewers. All external
28 ;; viewers for mime types in mm-keep-viewer-alive-types will remain active
29 ;; after switching articles or groups, and will only be removed when exiting
38 (eval-when-compile (require 'cl)
42 (autoload 'mm-inline-partial "mm-partial")
43 (autoload 'mm-inline-external-body "mm-extern")
44 (autoload 'mm-insert-inline "mm-view"))
46 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
48 (defgroup mime-display ()
49 "Display of MIME in mail and news articles."
50 :link '(custom-manual "(emacs-mime)Customization")
56 (defgroup mime-security ()
57 "MIME security in mail and news articles."
58 :link '(custom-manual "(emacs-mime)Customization")
63 ;;; Convenience macros.
65 (defmacro mm-handle-buffer (handle)
67 (defmacro mm-handle-type (handle)
69 (defsubst mm-handle-media-type (handle)
70 (if (stringp (car handle))
72 (car (mm-handle-type handle))))
73 (defsubst mm-handle-media-supertype (handle)
74 (car (split-string (mm-handle-media-type handle) "/")))
75 (defsubst mm-handle-media-subtype (handle)
76 (cadr (split-string (mm-handle-media-type handle) "/")))
77 (defmacro mm-handle-encoding (handle)
79 (defmacro mm-handle-undisplayer (handle)
81 (defmacro mm-handle-set-undisplayer (handle function)
82 `(setcar (nthcdr 3 ,handle) ,function))
83 (defmacro mm-handle-disposition (handle)
85 (defmacro mm-handle-description (handle)
87 (defmacro mm-handle-cache (handle)
89 (defmacro mm-handle-set-cache (handle contents)
90 `(setcar (nthcdr 6 ,handle) ,contents))
91 (defmacro mm-handle-id (handle)
93 (defmacro mm-handle-multipart-original-buffer (handle)
94 `(get-text-property 0 'buffer (car ,handle)))
95 (defmacro mm-handle-multipart-from (handle)
96 `(get-text-property 0 'from (car ,handle)))
97 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
98 `(get-text-property 0 ,parameter (car ,handle)))
100 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
101 disposition description cache
103 `(list ,buffer ,type ,encoding ,undisplayer
104 ,disposition ,description ,cache ,id))
106 (defcustom mm-inline-media-tests
110 (mm-valid-and-fit-image-p 'jpeg handle)))
114 (mm-valid-and-fit-image-p 'png handle)))
118 (mm-valid-and-fit-image-p 'gif handle)))
122 (mm-valid-and-fit-image-p 'tiff handle)) )
126 (mm-valid-and-fit-image-p 'xbm handle)))
130 (mm-valid-and-fit-image-p 'xbm handle)))
134 (mm-valid-and-fit-image-p 'xpm handle)))
138 (mm-valid-and-fit-image-p 'xpm handle)))
142 (mm-valid-and-fit-image-p 'bmp handle)))
143 ("image/x-portable-bitmap"
146 (mm-valid-and-fit-image-p 'pbm handle)))
147 ("text/plain" mm-inline-text identity)
148 ("text/enriched" mm-inline-text identity)
149 ("text/richtext" mm-inline-text identity)
150 ("text/x-patch" mm-display-patch-inline
152 (locate-library "diff-mode")))
153 ("application/emacs-lisp" mm-display-elisp-inline identity)
157 (locate-library "w3")))
161 (or (featurep 'vcard)
162 (locate-library "vcard"))))
163 ("message/delivery-status" mm-inline-text identity)
164 ("message/rfc822" mm-inline-message identity)
165 ("message/partial" mm-inline-partial identity)
166 ("message/external-body" mm-inline-external-body identity)
167 ("text/.*" mm-inline-text identity)
168 ("audio/wav" mm-inline-audio
170 (and (or (featurep 'nas-sound) (featurep 'native-sound))
171 (device-sound-enabled-p))))
175 (and (or (featurep 'nas-sound) (featurep 'native-sound))
176 (device-sound-enabled-p))))
177 ("application/pgp-signature" ignore identity)
178 ("application/x-pkcs7-signature" ignore identity)
179 ("application/pkcs7-signature" ignore identity)
180 ("multipart/alternative" ignore identity)
181 ("multipart/mixed" ignore identity)
182 ("multipart/related" ignore identity)
183 ;; Disable audio and image
184 ("audio/.*" ignore ignore)
185 ("image/.*" ignore ignore)
186 ;; Default to displaying as text
187 (".*" mm-inline-text mm-readable-p))
188 "Alist of media types/tests saying whether types can be displayed inline."
189 :type '(repeat (list (string :tag "MIME type")
190 (function :tag "Display function")
191 (function :tag "Display test")))
192 :group 'mime-display)
194 (defcustom mm-inlined-types
195 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
196 "message/partial" "message/external-body" "application/emacs-lisp"
197 "application/pgp-signature" "application/x-pkcs7-signature"
198 "application/pkcs7-signature")
199 "List of media types that are to be displayed inline.
200 See also `mm-inline-media-tests', which says how to display a media
202 :type '(repeat string)
203 :group 'mime-display)
205 (defcustom mm-keep-viewer-alive-types
206 '("application/postscript" "application/msword" "application/vnd.ms-excel"
207 "application/pdf" "application/x-dvi")
208 "List of media types for which the external viewer will not be killed
209 when selecting a different article."
210 :type '(repeat string)
211 :group 'mime-display)
213 (defcustom mm-automatic-display
214 '("text/plain" "text/enriched" "text/richtext" "text/html"
215 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
216 "message/rfc822" "text/x-patch" "application/pgp-signature"
217 "application/emacs-lisp" "application/x-pkcs7-signature"
218 "application/pkcs7-signature")
219 "A list of MIME types to be displayed automatically."
220 :type '(repeat string)
221 :group 'mime-display)
223 (defcustom mm-attachment-override-types '("text/x-vcard")
224 "Types to have \"attachment\" ignored if they can be displayed inline."
225 :type '(repeat string)
226 :group 'mime-display)
228 (defcustom mm-inline-override-types nil
229 "Types to be treated as attachments even if they can be displayed inline."
230 :type '(repeat string)
231 :group 'mime-display)
233 (defcustom mm-automatic-external-display nil
234 "List of MIME type regexps that will be displayed externally automatically."
235 :type '(repeat string)
236 :group 'mime-display)
238 (defcustom mm-discouraged-alternatives nil
239 "List of MIME types that are discouraged when viewing multipart/alternative.
240 Viewing agents are supposed to view the last possible part of a message,
241 as that is supposed to be the richest. However, users may prefer other
242 types instead, and this list says what types are most unwanted. If,
243 for instance, text/html parts are very unwanted, and text/richtext are
244 somewhat unwanted, then the value of this variable should be set
247 (\"text/html\" \"text/richtext\")"
248 :type '(repeat string)
249 :group 'mime-display)
251 (defcustom mm-tmp-directory
252 (cond ((fboundp 'temp-directory) (temp-directory))
253 ((boundp 'temporary-file-directory) temporary-file-directory)
255 "Where mm will store its temporary files."
257 :group 'mime-display)
259 (defcustom mm-inline-large-images nil
260 "If non-nil, then all images fit in the buffer."
262 :group 'mime-display)
264 (defvar mm-file-name-rewrite-functions nil
265 "*List of functions used for rewriting file names of MIME parts.
266 Each function takes a file name as input and returns a file name.
268 Ready-made functions include
269 `mm-file-name-delete-whitespace',
270 `mm-file-name-trim-whitespace',
271 `mm-file-name-collapse-whitespace',
272 `mm-file-name-replace-whitespace',
273 `capitalize', `downcase', `upcase', and
276 (defvar mm-file-name-replace-whitespace nil
277 "String used for replacing whitespace characters; default is `\"_\"'.")
279 (defcustom mm-default-directory nil
280 "The default directory where mm will save files.
281 If not set, `default-directory' will be used."
283 :group 'mime-display)
285 (defcustom mm-external-terminal-program "xterm"
286 "The program to start an external terminal."
288 :group 'mime-display)
290 ;;; Internal variables.
292 (defvar mm-dissection-list nil)
293 (defvar mm-last-shell-command "")
294 (defvar mm-content-id-alist nil)
295 (defvar mm-postponed-undisplay-list nil)
297 ;; According to RFC2046, in particular, in a digest, the default
298 ;; Content-Type value for a body part is changed from "text/plain" to
300 (defvar mm-dissect-default-type "text/plain")
302 (autoload 'mml2015-verify "mml2015")
303 (autoload 'mml2015-verify-test "mml2015")
304 (autoload 'mml-smime-verify "mml-smime")
305 (autoload 'mml-smime-verify-test "mml-smime")
307 (defvar mm-verify-function-alist
308 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
309 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
310 mm-uu-pgp-signed-test)
311 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
312 mml-smime-verify-test)
313 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
314 mml-smime-verify-test)))
316 (defcustom mm-verify-option 'never
317 "Option of verifying signed parts.
318 `never', not verify; `always', always verify;
319 `known', only verify known protocols. Otherwise, ask user."
320 :type '(choice (item always)
322 (item :tag "only known protocols" known)
323 (item :tag "ask" nil))
324 :group 'mime-security)
326 (autoload 'mml2015-decrypt "mml2015")
327 (autoload 'mml2015-decrypt-test "mml2015")
329 (defvar mm-decrypt-function-alist
330 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
331 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
332 mm-uu-pgp-encrypted-test)))
334 (defcustom mm-decrypt-option nil
335 "Option of decrypting encrypted parts.
336 `never', not decrypt; `always', always decrypt;
337 `known', only decrypt known protocols. Otherwise, ask user."
338 :type '(choice (item always)
340 (item :tag "only known protocols" known)
341 (item :tag "ask" nil))
342 :group 'mime-security)
344 (defvar mm-viewer-completion-map
345 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
346 (set-keymap-parent map minibuffer-local-completion-map)
348 "Keymap for input viewer with completion.")
350 ;; Should we bind other key to minibuffer-complete-word?
351 (define-key mm-viewer-completion-map " " 'self-insert-command)
353 (defvar mm-viewer-completion-map
354 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
355 (set-keymap-parent map minibuffer-local-completion-map)
357 "Keymap for input viewer with completion.")
359 ;; Should we bind other key to minibuffer-complete-word?
360 (define-key mm-viewer-completion-map " " 'self-insert-command)
364 (defun mm-alist-to-plist (alist)
365 "Convert association list ALIST into the equivalent property-list form.
366 The plist is returned. This converts from
368 \((a . 1) (b . 2) (c . 3))
374 The original alist is not modified. See also `destructive-alist-to-plist'."
377 (let ((el (car alist)))
378 (setq plist (cons (cdr el) (cons (car el) plist))))
379 (setq alist (cdr alist)))
382 (defun mm-keep-viewer-alive-p (handle)
383 "Say whether external viewer for HANDLE should stay alive."
384 (let ((types mm-keep-viewer-alive-types)
385 (type (mm-handle-media-type handle))
388 (while (setq ty (pop types))
389 (when (string-match ty type)
390 (throw 'found t))))))
392 (defun mm-handle-set-external-undisplayer (handle function)
393 "Set the undisplayer for this handle; postpone undisplaying of viewers
394 for types in mm-keep-viewer-alive-types."
395 (if (mm-keep-viewer-alive-p handle)
396 (let ((new-handle (copy-sequence handle)))
397 (mm-handle-set-undisplayer new-handle function)
398 (mm-handle-set-undisplayer handle nil)
399 (push new-handle mm-postponed-undisplay-list))
400 (mm-handle-set-undisplayer handle function)))
402 (defun mm-destroy-postponed-undisplay-list ()
403 (message "Destroying external MIME viewers")
404 (mm-destroy-parts mm-postponed-undisplay-list))
406 (defun mm-dissect-buffer (&optional no-strict-mime)
407 "Dissect the current buffer and return a list of MIME handles."
409 (let (ct ctl type subtype cte cd description id result from)
411 (mail-narrow-to-head)
412 (when (or no-strict-mime
413 (mail-fetch-field "mime-version"))
414 (setq ct (mail-fetch-field "content-type")
415 ctl (ignore-errors (mail-header-parse-content-type ct))
416 cte (mail-fetch-field "content-transfer-encoding")
417 cd (mail-fetch-field "content-disposition")
418 description (mail-fetch-field "content-description")
419 from (mail-fetch-field "from")
420 id (mail-fetch-field "content-id"))
421 ;; FIXME: In some circumstances, this code is running within
422 ;; an unibyte macro. mail-extract-address-components
423 ;; creates unibyte buffers. This `if', though not a perfect
424 ;; solution, avoids most of them.
426 (setq from (cadr (mail-extract-address-components from))))))
428 (setq cte (mail-header-strip cte)))
430 (not (string-match "/" (car ctl))))
431 (mm-dissect-singlepart
432 (list mm-dissect-default-type)
433 (and cte (intern (downcase (mail-header-remove-whitespace
434 (mail-header-remove-comments
437 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
439 (setq type (split-string (car ctl) "/"))
440 (setq subtype (cadr type)
445 ((equal type "multipart")
446 (let ((mm-dissect-default-type (if (equal subtype "digest")
449 (add-text-properties 0 (length (car ctl))
450 (mm-alist-to-plist (cdr ctl)) (car ctl))
452 ;; what really needs to be done here is a way to link a
453 ;; MIME handle back to it's parent MIME handle (in a multilevel
454 ;; MIME article). That would probably require changing
455 ;; the mm-handle API so we simply store the multipart buffert
456 ;; name as a text property of the "multipart/whatever" string.
457 (add-text-properties 0 (length (car ctl))
458 (list 'buffer (mm-copy-to-buffer))
460 (add-text-properties 0 (length (car ctl))
463 (cons (car ctl) (mm-dissect-multipart ctl))))
465 (mm-dissect-singlepart
467 (and cte (intern (downcase (mail-header-remove-whitespace
468 (mail-header-remove-comments
471 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
474 (when (string-match " *<\\(.*\\)> *" id)
475 (setq id (match-string 1 id)))
476 (push (cons id result) mm-content-id-alist))
479 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
481 (if (equal "text/plain" (car ctl))
484 (let ((res (mm-make-handle
485 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
486 (push (car res) mm-dissection-list)
489 (defun mm-remove-all-parts ()
490 "Remove all MIME handles."
492 (mapcar 'mm-remove-part mm-dissection-list)
493 (setq mm-dissection-list nil))
495 (defun mm-dissect-multipart (ctl)
496 (goto-char (point-min))
497 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
498 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
501 (goto-char (point-max))
502 (if (re-search-backward close-delimiter nil t)
505 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
506 (while (and (< (point) end) (re-search-forward boundary end t))
507 (goto-char (match-beginning 0))
511 (narrow-to-region start (point))
512 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
514 (setq start (point)))
515 (when (and start (< start end))
518 (narrow-to-region start end)
519 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
520 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
522 (defun mm-copy-to-buffer ()
523 "Copy the contents of the current buffer to a fresh buffer."
525 (let ((obuf (current-buffer))
527 (goto-char (point-min))
528 (search-forward-regexp "^\n" nil t)
530 (set-buffer (generate-new-buffer " *mm*"))
531 (insert-buffer-substring obuf beg)
534 (defun mm-display-parts (handle &optional no-default)
535 (if (stringp (car handle))
536 (mapcar 'mm-display-parts (cdr handle))
537 (if (bufferp (car handle))
539 (narrow-to-region (point) (point))
540 (mm-display-part handle)
541 (goto-char (point-max)))
542 (mapcar 'mm-display-parts handle))))
544 (defun mm-display-part (handle &optional no-default)
545 "Display the MIME part represented by HANDLE.
546 Returns nil if the part is removed; inline if displayed inline;
547 external if displayed external."
549 (mailcap-parse-mailcaps)
550 (if (mm-handle-displayed-p handle)
551 (mm-remove-part handle)
552 (let* ((type (mm-handle-media-type handle))
553 (method (mailcap-mime-info type)))
554 (if (and (mm-inlinable-p handle)
555 (mm-inlined-p handle))
558 (mm-display-inline handle)
562 (if (and (not method)
563 (equal "text" (car (split-string type))))
566 (mm-insert-inline handle (mm-get-part handle))
569 handle (or method 'mailcap-save-binary-file)))))))))
571 (defun mm-display-external (handle method)
572 "Display HANDLE using METHOD."
573 (let ((outbuf (current-buffer)))
574 (mm-with-unibyte-buffer
575 (if (functionp method)
576 (let ((cur (current-buffer)))
577 (if (eq method 'mailcap-save-binary-file)
579 (set-buffer (generate-new-buffer " *mm*"))
581 (mm-insert-part handle)
582 (let ((win (get-buffer-window cur t)))
584 (select-window win)))
585 (switch-to-buffer (generate-new-buffer " *mm*")))
586 (buffer-disable-undo)
587 (mm-set-buffer-file-coding-system mm-binary-coding-system)
588 (insert-buffer-substring cur)
589 (goto-char (point-min))
590 (message "Viewing with %s" method)
591 (let ((mm (current-buffer))
592 (non-viewer (assq 'non-viewer
594 (mm-handle-media-type handle) t))))
598 (mm-save-part handle))
599 (when (and (not non-viewer)
601 (mm-handle-set-undisplayer handle mm)))))
602 ;; The function is a string to be executed.
603 (mm-insert-part handle)
604 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
605 (filename (mail-content-type-get
606 (mm-handle-disposition handle) 'filename))
607 (mime-info (mailcap-mime-info
608 (mm-handle-media-type handle) t))
609 (needsterm (or (assoc "needsterm" mime-info)
610 (assoc "needsterminal" mime-info)))
611 (copiousoutput (assoc "copiousoutput" mime-info))
613 ;; We create a private sub-directory where we store our files.
615 (set-file-modes dir 448)
617 (setq file (expand-file-name (file-name-nondirectory filename)
619 (setq file (make-temp-name (expand-file-name "mm." dir))))
620 (let ((coding-system-for-write mm-binary-coding-system))
621 (write-region (point-min) (point-max) file nil 'nomesg))
622 (message "Viewing with %s" method)
626 (start-process "*display*" nil
627 mm-external-terminal-program
631 method file (mm-handle-type handle)))
642 (mm-handle-type handle)))))
645 (set-process-sentinel
646 (get-buffer-process buffer)
647 `(lambda (process state)
648 (if (eq 'exit (process-status process))
649 (gnus-configure-windows
650 ',gnus-current-window-configuration))))
651 (gnus-configure-windows 'display-term))
652 (mm-handle-set-external-undisplayer handle (cons file buffer)))
653 (message "Displaying %s..." (format method file))
656 (with-current-buffer outbuf
662 (call-process shell-file-name nil
664 (generate-new-buffer " *mm*"))
668 method file (mm-handle-type handle)))
669 (if (buffer-live-p buffer)
674 (ignore-errors (delete-file file))
675 (ignore-errors (delete-directory
676 (file-name-directory file)))
677 (ignore-errors (kill-buffer buffer))))))
681 (start-process "*display*"
683 (generate-new-buffer " *mm*"))
687 method file (mm-handle-type handle)))
688 (mm-handle-set-external-undisplayer handle (cons file buffer)))
689 (message "Displaying %s..." (format method file))
692 (defun mm-mailcap-command (method file type-list)
693 (let ((ctl (cdr type-list))
697 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
698 (push (substring method beg (match-beginning 0)) out)
699 (setq beg (match-end 0)
700 total (match-string 0 method)
701 sub (match-string 1 method))
703 ((string= total "%%")
705 ((string= total "%s")
706 (setq uses-stdin nil)
707 (push (mm-quote-arg file) out))
708 ((string= total "%t")
709 (push (mm-quote-arg (car type-list)) out))
711 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
712 (push (substring method beg (length method)) out)
716 (push (mm-quote-arg file) out)))
717 (mapconcat 'identity (nreverse out) "")))
719 (defun mm-remove-parts (handles)
720 "Remove the displayed MIME parts represented by HANDLES."
721 (if (and (listp handles)
722 (bufferp (car handles)))
723 (mm-remove-part handles)
725 (while (setq handle (pop handles))
728 (when (buffer-live-p (get-text-property 0 'buffer handle))
729 (kill-buffer (get-text-property 0 'buffer handle))))
731 (stringp (car handle)))
732 (mm-remove-parts (cdr handle)))
734 (mm-remove-part handle)))))))
736 (defun mm-destroy-parts (handles)
737 "Remove the displayed MIME parts represented by HANDLES."
738 (if (and (listp handles)
739 (bufferp (car handles)))
740 (mm-destroy-part handles)
742 (while (setq handle (pop handles))
745 (when (buffer-live-p (get-text-property 0 'buffer handle))
746 (kill-buffer (get-text-property 0 'buffer handle))))
748 (stringp (car handle)))
749 (mm-destroy-parts handle))
751 (mm-destroy-part handle)))))))
753 (defun mm-remove-part (handle)
754 "Remove the displayed MIME part represented by HANDLE."
756 (let ((object (mm-handle-undisplayer handle)))
759 ;; Internally displayed part.
760 ((mm-annotationp object)
761 (delete-annotation object))
762 ((or (functionp object)
764 (eq (car object) 'lambda)))
766 ;; Externally displayed part.
768 (ignore-errors (delete-file (car object)))
769 (ignore-errors (delete-directory (file-name-directory (car object))))
770 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
772 (when (buffer-live-p object)
773 (kill-buffer object)))))
774 (mm-handle-set-undisplayer handle nil))))
776 (defun mm-display-inline (handle)
777 (let* ((type (mm-handle-media-type handle))
778 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
779 (funcall function handle)
780 (goto-char (point-min))))
782 (defun mm-assoc-string-match (alist type)
784 (when (string-match (car elem) type)
787 (defun mm-automatic-display-p (handle)
788 "Say whether the user wants HANDLE to be displayed automatically."
789 (let ((methods mm-automatic-display)
790 (type (mm-handle-media-type handle))
792 (while (setq method (pop methods))
793 (when (and (not (mm-inline-override-p handle))
794 (string-match method type))
799 (defun mm-inlinable-p (handle)
800 "Say whether HANDLE can be displayed inline."
801 (let ((alist mm-inline-media-tests)
802 (type (mm-handle-media-type handle))
805 (when (string-match (caar alist) type)
806 (setq test (caddar alist)
808 (setq test (funcall test handle)))
812 (defun mm-inlined-p (handle)
813 "Say whether the user wants HANDLE to be displayed inline."
814 (let ((methods mm-inlined-types)
815 (type (mm-handle-media-type handle))
817 (while (setq method (pop methods))
818 (when (and (not (mm-inline-override-p handle))
819 (string-match method type))
824 (defun mm-attachment-override-p (handle)
825 "Say whether HANDLE should have attachment behavior overridden."
826 (let ((types mm-attachment-override-types)
827 (type (mm-handle-media-type handle))
830 (while (setq ty (pop types))
831 (when (and (string-match ty type)
832 (mm-inlinable-p handle))
833 (throw 'found t))))))
835 (defun mm-inline-override-p (handle)
836 "Say whether HANDLE should have inline behavior overridden."
837 (let ((types mm-inline-override-types)
838 (type (mm-handle-media-type handle))
841 (while (setq ty (pop types))
842 (when (string-match ty type)
843 (throw 'found t))))))
845 (defun mm-automatic-external-display-p (type)
846 "Return the user-defined method for TYPE."
847 (let ((methods mm-automatic-external-display)
849 (while (setq method (pop methods))
850 (when (string-match method type)
855 (defun mm-destroy-part (handle)
856 "Destroy the data structures connected to HANDLE."
858 (mm-remove-part handle)
859 (when (buffer-live-p (mm-handle-buffer handle))
860 (kill-buffer (mm-handle-buffer handle)))))
862 (defun mm-handle-displayed-p (handle)
863 "Say whether HANDLE is displayed or not."
864 (mm-handle-undisplayer handle))
867 ;;; Functions for outputting parts
870 (defun mm-get-part (handle)
871 "Return the contents of HANDLE as a string."
872 (mm-with-unibyte-buffer
873 (insert (with-current-buffer (mm-handle-buffer handle)
874 (mm-with-unibyte-current-buffer-mule4
876 (mm-decode-content-transfer-encoding
877 (mm-handle-encoding handle)
878 (mm-handle-media-type handle))
881 (defun mm-insert-part (handle)
882 "Insert the contents of HANDLE in the current buffer."
883 (let ((cur (current-buffer)))
885 (if (member (mm-handle-media-supertype handle) '("text" "message"))
887 (insert-buffer-substring (mm-handle-buffer handle))
888 (mm-decode-content-transfer-encoding
889 (mm-handle-encoding handle)
890 (mm-handle-media-type handle))
891 (let ((temp (current-buffer)))
893 (insert-buffer-substring temp)))
894 (mm-with-unibyte-buffer
895 (insert-buffer-substring (mm-handle-buffer handle))
896 (mm-decode-content-transfer-encoding
897 (mm-handle-encoding handle)
898 (mm-handle-media-type handle))
899 (let ((temp (current-buffer)))
901 (insert-buffer-substring temp)))))))
903 (defun mm-file-name-delete-whitespace (file-name)
904 "Remove all whitespace characters from FILE-NAME."
905 (while (string-match "\\s-+" file-name)
906 (setq file-name (replace-match "" t t file-name)))
909 (defun mm-file-name-trim-whitespace (file-name)
910 "Remove leading and trailing whitespace characters from FILE-NAME."
911 (when (string-match "\\`\\s-+" file-name)
912 (setq file-name (substring file-name (match-end 0))))
913 (when (string-match "\\s-+\\'" file-name)
914 (setq file-name (substring file-name 0 (match-beginning 0))))
917 (defun mm-file-name-collapse-whitespace (file-name)
918 "Collapse multiple whitespace characters in FILE-NAME."
919 (while (string-match "\\s-\\s-+" file-name)
920 (setq file-name (replace-match " " t t file-name)))
923 (defun mm-file-name-replace-whitespace (file-name)
924 "Replace whitespace characters in FILE-NAME with underscores.
925 Set `mm-file-name-replace-whitespace' to any other string if you do not
927 (let ((s (or mm-file-name-replace-whitespace "_")))
928 (while (string-match "\\s-" file-name)
929 (setq file-name (replace-match s t t file-name))))
932 (defun mm-save-part (handle)
933 "Write HANDLE to a file."
934 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
935 (filename (mail-content-type-get
936 (mm-handle-disposition handle) 'filename))
939 (setq filename (gnus-map-function mm-file-name-rewrite-functions
940 (file-name-nondirectory filename))))
942 (read-file-name "Save MIME part to: "
944 (or filename name "")
945 (or mm-default-directory default-directory))))
946 (setq mm-default-directory (file-name-directory file))
947 (and (or (not (file-exists-p file))
948 (yes-or-no-p (format "File %s already exists; overwrite? "
951 (mm-save-part-to-file handle file)
954 (defun mm-save-part-to-file (handle file)
955 (mm-with-unibyte-buffer
956 (mm-insert-part handle)
957 (let ((coding-system-for-write 'binary)
958 ;; Don't re-compress .gz & al. Arguably we should make
959 ;; `file-name-handler-alist' nil, but that would chop
960 ;; ange-ftp, which is reasonable to use here.
961 (inhibit-file-name-operation 'write-region)
962 (inhibit-file-name-handlers
963 (cons 'jka-compr-handler inhibit-file-name-handlers)))
964 (write-region (point-min) (point-max) file))))
966 (defun mm-pipe-part (handle)
967 "Pipe HANDLE to a process."
968 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
970 (read-string "Shell command on MIME part: " mm-last-shell-command)))
971 (mm-with-unibyte-buffer
972 (mm-insert-part handle)
973 (let ((coding-system-for-write 'binary))
974 (shell-command-on-region (point-min) (point-max) command nil)))))
976 (defun mm-interactively-view-part (handle)
977 "Display HANDLE using METHOD."
978 (let* ((type (mm-handle-media-type handle))
980 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
981 (mailcap-mime-info type 'all)))
982 (method (let ((minibuffer-local-completion-map
983 mm-viewer-completion-map))
984 (completing-read "Viewer: " methods))))
985 (when (string= method "")
986 (error "No method given"))
987 (if (string-match "^[^% \t]+$" method)
988 (setq method (concat method " %s")))
989 (mm-display-external handle method)))
991 (defun mm-preferred-alternative (handles &optional preferred)
992 "Say which of HANDLES are preferred."
993 (let ((prec (if preferred (list preferred)
994 (mm-preferred-alternative-precedence handles)))
995 p h result type handle)
996 (while (setq p (pop prec))
999 (setq handle (car h))
1000 (setq type (mm-handle-media-type handle))
1001 (when (and (equal p type)
1002 (mm-automatic-display-p handle)
1003 (or (stringp (car handle))
1004 (not (mm-handle-disposition handle))
1005 (equal (car (mm-handle-disposition handle))
1013 (defun mm-preferred-alternative-precedence (handles)
1014 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1015 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1017 (dolist (disc (reverse mm-discouraged-alternatives))
1018 (dolist (elem (copy-sequence seq))
1019 (when (string-match disc elem)
1020 (setq seq (nconc (delete elem seq) (list elem))))))
1023 (defun mm-get-content-id (id)
1024 "Return the handle(s) referred to by ID."
1025 (cdr (assoc id mm-content-id-alist)))
1027 (defconst mm-image-type-regexps
1028 '(("/\\*.*XPM.\\*/" . xpm)
1032 ("\211PNG\r\n" . png)
1034 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1035 ("%!PS" . postscript))
1036 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1037 When the first bytes of an image file match REGEXP, it is assumed to
1038 be of image type IMAGE-TYPE.")
1040 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1041 (defun mm-image-type-from-buffer ()
1042 "Determine the image type from data in the current buffer.
1043 Value is a symbol specifying the image type or nil if type cannot
1045 (let ((types mm-image-type-regexps)
1047 (goto-char (point-min))
1048 (while (and types (null type))
1049 (let ((regexp (car (car types)))
1050 (image-type (cdr (car types))))
1051 (when (looking-at regexp)
1052 (setq type image-type))
1053 (setq types (cdr types))))
1056 (defun mm-get-image (handle)
1057 "Return an image instance based on HANDLE."
1058 (let ((type (mm-handle-media-subtype handle))
1060 ;; Allow some common translations.
1063 ((equal type "x-pixmap")
1065 ((equal type "x-xbitmap")
1067 ((equal type "x-portable-bitmap")
1070 (or (mm-handle-cache handle)
1071 (mm-with-unibyte-buffer
1072 (mm-insert-part handle)
1076 ;; Avoid testing `make-glyph' since W3 may define
1077 ;; a bogus version of it.
1078 (if (fboundp 'create-image)
1079 (create-image (buffer-string)
1080 (or (mm-image-type-from-buffer)
1085 ;; xbm images require special handling, since
1086 ;; the only way to create glyphs from these
1087 ;; (without a ton of work) is to write them
1088 ;; out to a file, and then create a file
1090 (let ((file (make-temp-name
1091 (expand-file-name "emm.xbm"
1092 mm-tmp-directory))))
1095 (write-region (point-min) (point-max) file)
1096 (make-glyph (list (cons 'x file))))
1098 (delete-file file)))))
1102 (or (mm-image-type-from-buffer)
1104 :data (buffer-string))))))))
1105 (mm-handle-set-cache handle spec))))))
1107 (defun mm-image-fit-p (handle)
1108 "Say whether the image in HANDLE will fit the current window."
1109 (let ((image (mm-get-image handle)))
1110 (if (fboundp 'glyph-width)
1111 ;; XEmacs' glyphs can actually tell us about their width, so
1112 ;; lets be nice and smart about them.
1113 (or mm-inline-large-images
1114 (and (< (glyph-width image) (window-pixel-width))
1115 (< (glyph-height image) (window-pixel-height))))
1116 (let* ((size (image-size image))
1119 (or mm-inline-large-images
1120 (and (< h (1- (window-height))) ; Don't include mode line.
1121 (< w (window-width))))))))
1123 (defun mm-valid-image-format-p (format)
1124 "Say whether FORMAT can be displayed natively by Emacs."
1127 ((fboundp 'valid-image-instantiator-format-p)
1128 (valid-image-instantiator-format-p format))
1130 ((fboundp 'image-type-available-p)
1131 (and (display-graphic-p)
1132 (image-type-available-p format)))
1133 ;; Nobody else can do images yet.
1137 (defun mm-valid-and-fit-image-p (format handle)
1138 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1139 (and (mm-valid-image-format-p format)
1140 (mm-image-fit-p handle)))
1142 (defun mm-find-part-by-type (handles type &optional notp recursive)
1143 "Search in HANDLES for part with TYPE.
1144 If NOTP, returns first non-matching part.
1145 If RECURSIVE, search recursively."
1148 (if (and recursive (stringp (caar handles)))
1149 (if (setq handle (mm-find-part-by-type (cdar handles) type
1153 (not (equal (mm-handle-media-type (car handles)) type))
1154 (equal (mm-handle-media-type (car handles)) type))
1155 (setq handle (car handles)
1157 (setq handles (cdr handles)))
1160 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1161 (goto-char (point-min))
1162 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1164 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1166 (end (save-excursion
1167 (goto-char (point-max))
1168 (if (re-search-backward close-delimiter nil t)
1172 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1173 (while (and (not result)
1174 (re-search-forward boundary end t))
1175 (goto-char (match-beginning 0))
1179 (narrow-to-region start (1- (point)))
1180 (when (let ((ctl (ignore-errors
1181 (mail-header-parse-content-type
1182 (mail-fetch-field "content-type")))))
1184 (not (equal (car ctl) type))
1185 (equal (car ctl) type)))
1186 (setq result (buffer-substring (point-min) (point-max)))))))
1188 (setq start (point)))
1189 (when (and (not result) start)
1192 (narrow-to-region start end)
1193 (when (let ((ctl (ignore-errors
1194 (mail-header-parse-content-type
1195 (mail-fetch-field "content-type")))))
1197 (not (equal (car ctl) type))
1198 (equal (car ctl) type)))
1199 (setq result (buffer-substring (point-min) (point-max)))))))
1202 (defvar mm-security-handle nil)
1204 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1205 ;; HANDLE could be a CTL.
1207 (put-text-property 0 (length (car handle)) parameter value
1210 (defun mm-possibly-verify-or-decrypt (parts ctl)
1211 (let ((subtype (cadr (split-string (car ctl) "/")))
1212 (mm-security-handle ctl) ;; (car CTL) is the type.
1213 protocol func functest)
1215 ((equal subtype "signed")
1216 (unless (and (setq protocol
1217 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1218 (not (equal protocol "multipart/mixed")))
1219 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1220 (let ((protocols mm-verify-function-alist))
1222 (if (and (or (not (setq functest (nth 3 (car protocols))))
1223 (funcall functest parts ctl))
1224 (mm-find-part-by-type parts (caar protocols) nil t))
1225 (setq protocol (caar protocols)
1227 (setq protocols (cdr protocols))))))
1228 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1230 ((eq mm-verify-option 'never) nil)
1231 ((eq mm-verify-option 'always) t)
1232 ((eq mm-verify-option 'known)
1234 (or (not (setq functest
1235 (nth 3 (assoc protocol
1236 mm-verify-function-alist))))
1237 (funcall functest parts ctl))))
1239 (format "Verify signed (%s) part? "
1240 (or (nth 2 (assoc protocol mm-verify-function-alist))
1241 (format "protocol=%s" protocol))))))
1244 (funcall func parts ctl)
1245 (mm-set-handle-multipart-parameter
1246 mm-security-handle 'gnus-details
1247 (format "Unknown sign protocol (%s)" protocol))))))
1248 ((equal subtype "encrypted")
1249 (unless (setq protocol
1250 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1251 ;; The message is broken.
1252 (let ((parts parts))
1254 (if (assoc (mm-handle-media-type (car parts))
1255 mm-decrypt-function-alist)
1256 (setq protocol (mm-handle-media-type (car parts))
1258 (setq parts (cdr parts))))))
1259 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1261 ((eq mm-decrypt-option 'never) nil)
1262 ((eq mm-decrypt-option 'always) t)
1263 ((eq mm-decrypt-option 'known)
1265 (or (not (setq functest
1266 (nth 3 (assoc protocol
1267 mm-decrypt-function-alist))))
1268 (funcall functest parts ctl))))
1270 (format "Decrypt (%s) part? "
1271 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1272 (format "protocol=%s" protocol))))))
1275 (setq parts (funcall func parts ctl))
1276 (mm-set-handle-multipart-parameter
1277 mm-security-handle 'gnus-details
1278 (format "Unknown encrypt protocol (%s)" protocol))))))
1282 (defun mm-multiple-handles (handles)
1283 (and (listp (car handles))
1284 (> (length handles) 1)))
1286 (defun mm-merge-handles (handles1 handles2)
1288 (if (listp (car handles1))
1291 (if (listp (car handles2))
1295 (defun mm-readable-p (handle)
1296 "Say whether the content of HANDLE is readable."
1297 (and (< (buffer-size (mm-handle-buffer handle)) 10000)
1298 (mm-with-unibyte-buffer
1299 (mm-insert-part handle)
1300 (and (eq (mm-body-7-or-8) '7bit)
1301 (not (mm-long-lines-p 76))))))
1303 (provide 'mm-decode)
1305 ;;; mm-decode.el ends here