1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; Jaap-Henk Hoepman (jhh@xs4all.nl):
27 ;; Added support for delayed destroy of external MIME viewers. All external
28 ;; viewers for mime types in mm-keep-viewer-alive-types will remain active
29 ;; after switching articles or groups, and will only be removed when exiting
36 (require 'gnus-mailcap)
38 (eval-when-compile (require 'cl)
42 (autoload 'mm-inline-partial "mm-partial")
43 (autoload 'mm-inline-external-body "mm-extern")
44 (autoload 'mm-insert-inline "mm-view"))
46 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
48 (defgroup mime-display ()
49 "Display of MIME in mail and news articles."
50 :link '(custom-manual "(emacs-mime)Customization")
56 (defgroup mime-security ()
57 "MIME security in mail and news articles."
58 :link '(custom-manual "(emacs-mime)Customization")
63 ;;; Convenience macros.
65 (defmacro mm-handle-buffer (handle)
67 (defmacro mm-handle-type (handle)
69 (defsubst mm-handle-media-type (handle)
70 (if (stringp (car handle))
72 (car (mm-handle-type handle))))
73 (defsubst mm-handle-media-supertype (handle)
74 (car (split-string (mm-handle-media-type handle) "/")))
75 (defsubst mm-handle-media-subtype (handle)
76 (cadr (split-string (mm-handle-media-type handle) "/")))
77 (defmacro mm-handle-encoding (handle)
79 (defmacro mm-handle-undisplayer (handle)
81 (defmacro mm-handle-set-undisplayer (handle function)
82 `(setcar (nthcdr 3 ,handle) ,function))
83 (defmacro mm-handle-disposition (handle)
85 (defmacro mm-handle-description (handle)
87 (defmacro mm-handle-cache (handle)
89 (defmacro mm-handle-set-cache (handle contents)
90 `(setcar (nthcdr 6 ,handle) ,contents))
91 (defmacro mm-handle-id (handle)
93 (defmacro mm-handle-multipart-original-buffer (handle)
94 `(get-text-property 0 'buffer (car ,handle)))
95 (defmacro mm-handle-multipart-from (handle)
96 `(get-text-property 0 'from (car ,handle)))
97 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
98 `(get-text-property 0 ,parameter (car ,handle)))
100 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
101 disposition description cache
103 `(list ,buffer ,type ,encoding ,undisplayer
104 ,disposition ,description ,cache ,id))
106 (defcustom mm-inline-media-tests
110 (mm-valid-and-fit-image-p 'jpeg handle)))
114 (mm-valid-and-fit-image-p 'png handle)))
118 (mm-valid-and-fit-image-p 'gif handle)))
122 (mm-valid-and-fit-image-p 'tiff handle)) )
126 (mm-valid-and-fit-image-p 'xbm handle)))
130 (mm-valid-and-fit-image-p 'xbm handle)))
134 (mm-valid-and-fit-image-p 'xpm handle)))
138 (mm-valid-and-fit-image-p 'xpm handle)))
142 (mm-valid-and-fit-image-p 'bmp handle)))
143 ("image/x-portable-bitmap"
146 (mm-valid-and-fit-image-p 'pbm handle)))
147 ("text/plain" mm-inline-text identity)
148 ("text/enriched" mm-inline-text identity)
149 ("text/richtext" mm-inline-text identity)
150 ("text/x-patch" mm-display-patch-inline
152 (locate-library "diff-mode")))
153 ("application/emacs-lisp" mm-display-elisp-inline identity)
157 (locate-library "w3")))
161 (or (featurep 'vcard)
162 (locate-library "vcard"))))
163 ("message/delivery-status" mm-inline-text identity)
164 ("message/rfc822" mm-inline-message identity)
165 ("message/partial" mm-inline-partial identity)
166 ("message/external-body" mm-inline-external-body identity)
167 ("text/.*" mm-inline-text identity)
168 ("audio/wav" mm-inline-audio
170 (and (or (featurep 'nas-sound) (featurep 'native-sound))
171 (device-sound-enabled-p))))
175 (and (or (featurep 'nas-sound) (featurep 'native-sound))
176 (device-sound-enabled-p))))
177 ("application/pgp-signature" ignore identity)
178 ("application/x-pkcs7-signature" ignore identity)
179 ("application/pkcs7-signature" ignore identity)
180 ("multipart/alternative" ignore identity)
181 ("multipart/mixed" ignore identity)
182 ("multipart/related" ignore identity)
183 ;; 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 ((flag enable-multibyte-characters)
526 (new-buffer (generate-new-buffer " *mm*")))
527 (goto-char (point-min))
528 (search-forward-regexp "^\n" nil t)
530 (narrow-to-region (point) (point-max))
532 (set-buffer-multibyte nil))
533 (copy-to-buffer new-buffer (point-min) (point-max))
535 (set-buffer-multibyte t)))
538 (defun mm-display-parts (handle &optional no-default)
539 (if (stringp (car handle))
540 (mapcar 'mm-display-parts (cdr handle))
541 (if (bufferp (car handle))
543 (narrow-to-region (point) (point))
544 (mm-display-part handle)
545 (goto-char (point-max)))
546 (mapcar 'mm-display-parts handle))))
548 (defun mm-display-part (handle &optional no-default)
549 "Display the MIME part represented by HANDLE.
550 Returns nil if the part is removed; inline if displayed inline;
551 external if displayed external."
553 (mailcap-parse-mailcaps)
554 (if (mm-handle-displayed-p handle)
555 (mm-remove-part handle)
556 (let* ((type (mm-handle-media-type handle))
557 (method (mailcap-mime-info type)))
558 (if (and (mm-inlinable-p handle)
559 (mm-inlined-p handle))
562 (mm-display-inline handle)
566 (if (and (not method)
567 (equal "text" (car (split-string type))))
570 (mm-insert-inline handle (mm-get-part handle))
573 handle (or method 'mailcap-save-binary-file)))))))))
575 (defun mm-display-external (handle method)
576 "Display HANDLE using METHOD."
577 (let ((outbuf (current-buffer)))
578 (mm-with-unibyte-buffer
579 (if (functionp method)
580 (let ((cur (current-buffer)))
581 (if (eq method 'mailcap-save-binary-file)
583 (set-buffer (generate-new-buffer " *mm*"))
585 (mm-insert-part handle)
586 (let ((win (get-buffer-window cur t)))
588 (select-window win)))
589 (switch-to-buffer (generate-new-buffer " *mm*")))
590 (buffer-disable-undo)
591 (mm-set-buffer-file-coding-system mm-binary-coding-system)
592 (insert-buffer-substring cur)
593 (goto-char (point-min))
594 (message "Viewing with %s" method)
595 (let ((mm (current-buffer))
596 (non-viewer (assq 'non-viewer
598 (mm-handle-media-type handle) t))))
602 (mm-save-part handle))
603 (when (and (not non-viewer)
605 (mm-handle-set-undisplayer handle mm)))))
606 ;; The function is a string to be executed.
607 (mm-insert-part handle)
608 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
609 (filename (mail-content-type-get
610 (mm-handle-disposition handle) 'filename))
611 (mime-info (mailcap-mime-info
612 (mm-handle-media-type handle) t))
613 (needsterm (or (assoc "needsterm" mime-info)
614 (assoc "needsterminal" mime-info)))
615 (copiousoutput (assoc "copiousoutput" mime-info))
617 ;; We create a private sub-directory where we store our files.
619 (set-file-modes dir 448)
621 (setq file (expand-file-name (file-name-nondirectory filename)
623 (setq file (make-temp-name (expand-file-name "mm." dir))))
624 (let ((coding-system-for-write mm-binary-coding-system))
625 (write-region (point-min) (point-max) file nil 'nomesg))
626 (message "Viewing with %s" method)
630 (start-process "*display*" nil
631 mm-external-terminal-program
635 method file (mm-handle-type handle)))
646 (mm-handle-type handle)))))
649 (set-process-sentinel
650 (get-buffer-process buffer)
651 `(lambda (process state)
652 (if (eq 'exit (process-status process))
653 (gnus-configure-windows
654 ',gnus-current-window-configuration))))
655 (gnus-configure-windows 'display-term))
656 (mm-handle-set-external-undisplayer handle (cons file buffer)))
657 (message "Displaying %s..." (format method file))
660 (with-current-buffer outbuf
666 (call-process shell-file-name nil
668 (generate-new-buffer " *mm*"))
672 method file (mm-handle-type handle)))
673 (if (buffer-live-p buffer)
678 (ignore-errors (delete-file file))
679 (ignore-errors (delete-directory
680 (file-name-directory file)))
681 (ignore-errors (kill-buffer buffer))))))
685 (start-process "*display*"
687 (generate-new-buffer " *mm*"))
691 method file (mm-handle-type handle)))
692 (mm-handle-set-external-undisplayer handle (cons file buffer)))
693 (message "Displaying %s..." (format method file))
696 (defun mm-mailcap-command (method file type-list)
697 (let ((ctl (cdr type-list))
701 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
702 (push (substring method beg (match-beginning 0)) out)
703 (setq beg (match-end 0)
704 total (match-string 0 method)
705 sub (match-string 1 method))
707 ((string= total "%%")
709 ((string= total "%s")
710 (setq uses-stdin nil)
711 (push (mm-quote-arg file) out))
712 ((string= total "%t")
713 (push (mm-quote-arg (car type-list)) out))
715 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
716 (push (substring method beg (length method)) out)
720 (push (mm-quote-arg file) out)))
721 (mapconcat 'identity (nreverse out) "")))
723 (defun mm-remove-parts (handles)
724 "Remove the displayed MIME parts represented by HANDLES."
725 (if (and (listp handles)
726 (bufferp (car handles)))
727 (mm-remove-part handles)
729 (while (setq handle (pop handles))
732 (when (buffer-live-p (get-text-property 0 'buffer handle))
733 (kill-buffer (get-text-property 0 'buffer handle))))
735 (stringp (car handle)))
736 (mm-remove-parts (cdr handle)))
738 (mm-remove-part handle)))))))
740 (defun mm-destroy-parts (handles)
741 "Remove the displayed MIME parts represented by HANDLES."
742 (if (and (listp handles)
743 (bufferp (car handles)))
744 (mm-destroy-part handles)
746 (while (setq handle (pop handles))
749 (when (buffer-live-p (get-text-property 0 'buffer handle))
750 (kill-buffer (get-text-property 0 'buffer handle))))
752 (stringp (car handle)))
753 (mm-destroy-parts handle))
755 (mm-destroy-part handle)))))))
757 (defun mm-remove-part (handle)
758 "Remove the displayed MIME part represented by HANDLE."
760 (let ((object (mm-handle-undisplayer handle)))
763 ;; Internally displayed part.
764 ((mm-annotationp object)
765 (delete-annotation object))
766 ((or (functionp object)
768 (eq (car object) 'lambda)))
770 ;; Externally displayed part.
772 (ignore-errors (delete-file (car object)))
773 (ignore-errors (delete-directory (file-name-directory (car object))))
774 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
776 (when (buffer-live-p object)
777 (kill-buffer object)))))
778 (mm-handle-set-undisplayer handle nil))))
780 (defun mm-display-inline (handle)
781 (let* ((type (mm-handle-media-type handle))
782 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
783 (funcall function handle)
784 (goto-char (point-min))))
786 (defun mm-assoc-string-match (alist type)
788 (when (string-match (car elem) type)
791 (defun mm-automatic-display-p (handle)
792 "Say whether the user wants HANDLE to be displayed automatically."
793 (let ((methods mm-automatic-display)
794 (type (mm-handle-media-type handle))
796 (while (setq method (pop methods))
797 (when (and (not (mm-inline-override-p handle))
798 (string-match method type))
803 (defun mm-inlinable-p (handle)
804 "Say whether HANDLE can be displayed inline."
805 (let ((alist mm-inline-media-tests)
806 (type (mm-handle-media-type handle))
809 (when (string-match (caar alist) type)
810 (setq test (caddar alist)
812 (setq test (funcall test handle)))
816 (defun mm-inlined-p (handle)
817 "Say whether the user wants HANDLE to be displayed inline."
818 (let ((methods mm-inlined-types)
819 (type (mm-handle-media-type handle))
821 (while (setq method (pop methods))
822 (when (and (not (mm-inline-override-p handle))
823 (string-match method type))
828 (defun mm-attachment-override-p (handle)
829 "Say whether HANDLE should have attachment behavior overridden."
830 (let ((types mm-attachment-override-types)
831 (type (mm-handle-media-type handle))
834 (while (setq ty (pop types))
835 (when (and (string-match ty type)
836 (mm-inlinable-p handle))
837 (throw 'found t))))))
839 (defun mm-inline-override-p (handle)
840 "Say whether HANDLE should have inline behavior overridden."
841 (let ((types mm-inline-override-types)
842 (type (mm-handle-media-type handle))
845 (while (setq ty (pop types))
846 (when (string-match ty type)
847 (throw 'found t))))))
849 (defun mm-automatic-external-display-p (type)
850 "Return the user-defined method for TYPE."
851 (let ((methods mm-automatic-external-display)
853 (while (setq method (pop methods))
854 (when (string-match method type)
859 (defun mm-destroy-part (handle)
860 "Destroy the data structures connected to HANDLE."
862 (mm-remove-part handle)
863 (when (buffer-live-p (mm-handle-buffer handle))
864 (kill-buffer (mm-handle-buffer handle)))))
866 (defun mm-handle-displayed-p (handle)
867 "Say whether HANDLE is displayed or not."
868 (mm-handle-undisplayer handle))
871 ;;; Functions for outputting parts
874 (defun mm-get-part (handle)
875 "Return the contents of HANDLE as a string."
876 (mm-with-unibyte-buffer
877 (insert (with-current-buffer (mm-handle-buffer handle)
878 (mm-with-unibyte-current-buffer-mule4
880 (mm-decode-content-transfer-encoding
881 (mm-handle-encoding handle)
882 (mm-handle-media-type handle))
885 (defun mm-insert-part (handle)
886 "Insert the contents of HANDLE in the current buffer."
887 (let ((cur (current-buffer)))
889 (if (member (mm-handle-media-supertype handle) '("text" "message"))
891 (insert-buffer-substring (mm-handle-buffer handle))
892 (mm-decode-content-transfer-encoding
893 (mm-handle-encoding handle)
894 (mm-handle-media-type handle))
895 (let ((temp (current-buffer)))
897 (insert-buffer-substring temp)))
898 (mm-with-unibyte-buffer
899 (insert-buffer-substring (mm-handle-buffer handle))
900 (mm-decode-content-transfer-encoding
901 (mm-handle-encoding handle)
902 (mm-handle-media-type handle))
903 (let ((temp (current-buffer)))
905 (insert-buffer-substring temp)))))))
907 (defun mm-file-name-delete-whitespace (file-name)
908 "Remove all whitespace characters from FILE-NAME."
909 (while (string-match "\\s-+" file-name)
910 (setq file-name (replace-match "" t t file-name)))
913 (defun mm-file-name-trim-whitespace (file-name)
914 "Remove leading and trailing whitespace characters from FILE-NAME."
915 (when (string-match "\\`\\s-+" file-name)
916 (setq file-name (substring file-name (match-end 0))))
917 (when (string-match "\\s-+\\'" file-name)
918 (setq file-name (substring file-name 0 (match-beginning 0))))
921 (defun mm-file-name-collapse-whitespace (file-name)
922 "Collapse multiple whitespace characters in FILE-NAME."
923 (while (string-match "\\s-\\s-+" file-name)
924 (setq file-name (replace-match " " t t file-name)))
927 (defun mm-file-name-replace-whitespace (file-name)
928 "Replace whitespace characters in FILE-NAME with underscores.
929 Set `mm-file-name-replace-whitespace' to any other string if you do not
931 (let ((s (or mm-file-name-replace-whitespace "_")))
932 (while (string-match "\\s-" file-name)
933 (setq file-name (replace-match s t t file-name))))
936 (defun mm-save-part (handle)
937 "Write HANDLE to a file."
938 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
939 (filename (mail-content-type-get
940 (mm-handle-disposition handle) 'filename))
943 (setq filename (gnus-map-function mm-file-name-rewrite-functions
944 (file-name-nondirectory filename))))
946 (read-file-name "Save MIME part to: "
948 (or filename name "")
949 (or mm-default-directory default-directory))))
950 (setq mm-default-directory (file-name-directory file))
951 (and (or (not (file-exists-p file))
952 (yes-or-no-p (format "File %s already exists; overwrite? "
955 (mm-save-part-to-file handle file)
958 (defun mm-save-part-to-file (handle file)
959 (mm-with-unibyte-buffer
960 (mm-insert-part handle)
961 (let ((coding-system-for-write 'binary)
962 ;; Don't re-compress .gz & al. Arguably we should make
963 ;; `file-name-handler-alist' nil, but that would chop
964 ;; ange-ftp, which is reasonable to use here.
965 (inhibit-file-name-operation 'write-region)
966 (inhibit-file-name-handlers
967 (cons 'jka-compr-handler inhibit-file-name-handlers)))
968 (write-region (point-min) (point-max) file))))
970 (defun mm-pipe-part (handle)
971 "Pipe HANDLE to a process."
972 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
974 (read-string "Shell command on MIME part: " mm-last-shell-command)))
975 (mm-with-unibyte-buffer
976 (mm-insert-part handle)
977 (let ((coding-system-for-write 'binary))
978 (shell-command-on-region (point-min) (point-max) command nil)))))
980 (defun mm-interactively-view-part (handle)
981 "Display HANDLE using METHOD."
982 (let* ((type (mm-handle-media-type handle))
984 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
985 (mailcap-mime-info type 'all)))
986 (method (let ((minibuffer-local-completion-map
987 mm-viewer-completion-map))
988 (completing-read "Viewer: " methods))))
989 (when (string= method "")
990 (error "No method given"))
991 (if (string-match "^[^% \t]+$" method)
992 (setq method (concat method " %s")))
993 (mm-display-external handle method)))
995 (defun mm-preferred-alternative (handles &optional preferred)
996 "Say which of HANDLES are preferred."
997 (let ((prec (if preferred (list preferred)
998 (mm-preferred-alternative-precedence handles)))
999 p h result type handle)
1000 (while (setq p (pop prec))
1003 (setq handle (car h))
1004 (setq type (mm-handle-media-type handle))
1005 (when (and (equal p type)
1006 (mm-automatic-display-p handle)
1007 (or (stringp (car handle))
1008 (not (mm-handle-disposition handle))
1009 (equal (car (mm-handle-disposition handle))
1017 (defun mm-preferred-alternative-precedence (handles)
1018 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1019 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1021 (dolist (disc (reverse mm-discouraged-alternatives))
1022 (dolist (elem (copy-sequence seq))
1023 (when (string-match disc elem)
1024 (setq seq (nconc (delete elem seq) (list elem))))))
1027 (defun mm-get-content-id (id)
1028 "Return the handle(s) referred to by ID."
1029 (cdr (assoc id mm-content-id-alist)))
1031 (defun mm-get-image (handle)
1032 "Return an image instance based on HANDLE."
1033 (let ((type (mm-handle-media-subtype handle))
1035 ;; Allow some common translations.
1038 ((equal type "x-pixmap")
1040 ((equal type "x-xbitmap")
1042 ((equal type "x-portable-bitmap")
1045 (or (mm-handle-cache handle)
1046 (mm-with-unibyte-buffer
1047 (mm-insert-part handle)
1051 ;; Avoid testing `make-glyph' since W3 may define
1052 ;; a bogus version of it.
1053 (if (fboundp 'create-image)
1055 (create-image (buffer-string) nil 'data-p)
1056 (create-image (buffer-string) (intern type)
1060 ;; xbm images require special handling, since
1061 ;; the only way to create glyphs from these
1062 ;; (without a ton of work) is to write them
1063 ;; out to a file, and then create a file
1065 (let ((file (make-temp-name
1066 (expand-file-name "emm.xbm"
1067 mm-tmp-directory))))
1070 (write-region (point-min) (point-max) file)
1071 (make-glyph (list (cons 'x file))))
1073 (delete-file file)))))
1076 (vector (intern type) :data (buffer-string))))))))
1077 (mm-handle-set-cache handle spec))))))
1079 (defun mm-image-fit-p (handle)
1080 "Say whether the image in HANDLE will fit the current window."
1081 (let ((image (mm-get-image handle)))
1082 (if (fboundp 'glyph-width)
1083 ;; XEmacs' glyphs can actually tell us about their width, so
1084 ;; lets be nice and smart about them.
1085 (or mm-inline-large-images
1086 (and (< (glyph-width image) (window-pixel-width))
1087 (< (glyph-height image) (window-pixel-height))))
1088 (let* ((size (image-size image))
1091 (or mm-inline-large-images
1092 (and (< h (1- (window-height))) ; Don't include mode line.
1093 (< w (window-width))))))))
1095 (defun mm-valid-image-format-p (format)
1096 "Say whether FORMAT can be displayed natively by Emacs."
1099 ((fboundp 'valid-image-instantiator-format-p)
1100 (valid-image-instantiator-format-p format))
1102 ((fboundp 'image-type-available-p)
1103 (and (display-graphic-p)
1104 (image-type-available-p format)))
1105 ;; Nobody else can do images yet.
1109 (defun mm-valid-and-fit-image-p (format handle)
1110 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1111 (and (mm-valid-image-format-p format)
1112 (mm-image-fit-p handle)))
1114 (defun mm-find-part-by-type (handles type &optional notp recursive)
1115 "Search in HANDLES for part with TYPE.
1116 If NOTP, returns first non-matching part.
1117 If RECURSIVE, search recursively."
1120 (if (and recursive (stringp (caar handles)))
1121 (if (setq handle (mm-find-part-by-type (cdar handles) type
1125 (not (equal (mm-handle-media-type (car handles)) type))
1126 (equal (mm-handle-media-type (car handles)) type))
1127 (setq handle (car handles)
1129 (setq handles (cdr handles)))
1132 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1133 (goto-char (point-min))
1134 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1136 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1138 (end (save-excursion
1139 (goto-char (point-max))
1140 (if (re-search-backward close-delimiter nil t)
1144 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1145 (while (and (not result)
1146 (re-search-forward boundary end t))
1147 (goto-char (match-beginning 0))
1151 (narrow-to-region start (1- (point)))
1152 (when (let ((ctl (ignore-errors
1153 (mail-header-parse-content-type
1154 (mail-fetch-field "content-type")))))
1156 (not (equal (car ctl) type))
1157 (equal (car ctl) type)))
1158 (setq result (buffer-substring (point-min) (point-max)))))))
1160 (setq start (point)))
1161 (when (and (not result) start)
1164 (narrow-to-region start end)
1165 (when (let ((ctl (ignore-errors
1166 (mail-header-parse-content-type
1167 (mail-fetch-field "content-type")))))
1169 (not (equal (car ctl) type))
1170 (equal (car ctl) type)))
1171 (setq result (buffer-substring (point-min) (point-max)))))))
1174 (defvar mm-security-handle nil)
1176 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1177 ;; HANDLE could be a CTL.
1179 (put-text-property 0 (length (car handle)) parameter value
1182 (defun mm-possibly-verify-or-decrypt (parts ctl)
1183 (let ((subtype (cadr (split-string (car ctl) "/")))
1184 (mm-security-handle ctl) ;; (car CTL) is the type.
1185 protocol func functest)
1187 ((equal subtype "signed")
1188 (unless (and (setq protocol
1189 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1190 (not (equal protocol "multipart/mixed")))
1191 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1192 (let ((protocols mm-verify-function-alist))
1194 (if (and (or (not (setq functest (nth 3 (car protocols))))
1195 (funcall functest parts ctl))
1196 (mm-find-part-by-type parts (caar protocols) nil t))
1197 (setq protocol (caar protocols)
1199 (setq protocols (cdr protocols))))))
1200 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1202 ((eq mm-verify-option 'never) nil)
1203 ((eq mm-verify-option 'always) t)
1204 ((eq mm-verify-option 'known)
1206 (or (not (setq functest
1207 (nth 3 (assoc protocol
1208 mm-verify-function-alist))))
1209 (funcall functest parts ctl))))
1211 (format "Verify signed (%s) part? "
1212 (or (nth 2 (assoc protocol mm-verify-function-alist))
1213 (format "protocol=%s" protocol))))))
1216 (funcall func parts ctl)
1217 (mm-set-handle-multipart-parameter
1218 mm-security-handle 'gnus-details
1219 (format "Unknown sign protocol (%s)" protocol))))))
1220 ((equal subtype "encrypted")
1221 (unless (setq protocol
1222 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1223 ;; The message is broken.
1224 (let ((parts parts))
1226 (if (assoc (mm-handle-media-type (car parts))
1227 mm-decrypt-function-alist)
1228 (setq protocol (mm-handle-media-type (car parts))
1230 (setq parts (cdr parts))))))
1231 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1233 ((eq mm-decrypt-option 'never) nil)
1234 ((eq mm-decrypt-option 'always) t)
1235 ((eq mm-decrypt-option 'known)
1237 (or (not (setq functest
1238 (nth 3 (assoc protocol
1239 mm-decrypt-function-alist))))
1240 (funcall functest parts ctl))))
1242 (format "Decrypt (%s) part? "
1243 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1244 (format "protocol=%s" protocol))))))
1247 (setq parts (funcall func parts ctl))
1248 (mm-set-handle-multipart-parameter
1249 mm-security-handle 'gnus-details
1250 (format "Unknown encrypt protocol (%s)" protocol))))))
1254 (defun mm-multiple-handles (handles)
1255 (and (listp (car handles))
1256 (> (length handles) 1)))
1258 (defun mm-merge-handles (handles1 handles2)
1260 (if (listp (car handles1))
1263 (if (listp (car handles2))
1267 (defun mm-readable-p (handle)
1268 "Say whether the content of HANDLE is readable."
1269 (and (< (buffer-size (mm-handle-buffer handle)) 10000)
1270 (mm-with-unibyte-buffer
1271 (mm-insert-part handle)
1272 (and (eq (mm-body-7-or-8) '7bit)
1273 (not (mm-long-lines-p 76))))))
1275 (provide 'mm-decode)
1277 ;;; mm-decode.el ends here