1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 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)
34 (autoload 'executable-find "executable")
35 (autoload 'mm-inline-partial "mm-partial")
36 (autoload 'mm-inline-external-body "mm-extern")
37 (autoload 'mm-insert-inline "mm-view"))
39 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
41 (defgroup mime-display ()
42 "Display of MIME in mail and news articles."
43 :link '(custom-manual "(emacs-mime)Customization")
49 (defgroup mime-security ()
50 "MIME security in mail and news articles."
51 :link '(custom-manual "(emacs-mime)Customization")
56 ;;; Convenience macros.
58 (defmacro mm-handle-buffer (handle)
60 (defmacro mm-handle-type (handle)
62 (defsubst mm-handle-media-type (handle)
63 (if (stringp (car handle))
65 (car (mm-handle-type handle))))
66 (defsubst mm-handle-media-supertype (handle)
67 (car (split-string (mm-handle-media-type handle) "/")))
68 (defsubst mm-handle-media-subtype (handle)
69 (cadr (split-string (mm-handle-media-type handle) "/")))
70 (defmacro mm-handle-encoding (handle)
72 (defmacro mm-handle-undisplayer (handle)
74 (defmacro mm-handle-set-undisplayer (handle function)
75 `(setcar (nthcdr 3 ,handle) ,function))
76 (defmacro mm-handle-disposition (handle)
78 (defmacro mm-handle-description (handle)
80 (defmacro mm-handle-cache (handle)
82 (defmacro mm-handle-set-cache (handle contents)
83 `(setcar (nthcdr 6 ,handle) ,contents))
84 (defmacro mm-handle-id (handle)
86 (defmacro mm-handle-multipart-original-buffer (handle)
87 `(get-text-property 0 'buffer (car ,handle)))
88 (defmacro mm-handle-multipart-from (handle)
89 `(get-text-property 0 'from (car ,handle)))
90 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
91 `(get-text-property 0 ,parameter (car ,handle)))
93 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
94 disposition description cache
96 `(list ,buffer ,type ,encoding ,undisplayer
97 ,disposition ,description ,cache ,id))
99 (defcustom mm-text-html-renderer
100 (cond ((locate-library "w3") 'w3)
101 ((locate-library "w3m") 'w3m)
102 ((executable-find "links") 'links)
103 ((executable-find "lynx") 'lynx))
104 "Render of HTML contents.
105 It is one of defined renderer types, or a rendering function.
106 The defined renderer types are:
107 `w3' : using Emacs/W3;
108 `w3m' : using emacs-w3m;
109 `links': using links;
111 `nil' : using external viewer."
112 :type '(choice (symbol w3)
119 :group 'mime-display)
121 (defvar mm-inline-text-html-renderer nil
122 "Function used for rendering inline HTML contents.
123 It is suggested to customize `mm-text-html-renderer' instead.")
125 (defcustom mm-inline-text-html-with-images nil
126 "If non-nil, Gnus will allow retrieving images in the HTML contents
127 with <img> tags. It has no effect on Emacs/w3. For emacs-w3m, the
128 value of the option `w3m-display-inline-images' will be bound with
129 this value. In addition, the variable `w3m-safe-url-regexp' will be
130 bound with the value nil if it is non-nil to make emacs-w3m show all
131 images, however this behavior may be changed in the future."
133 :group 'mime-display)
135 (defcustom mm-inline-text-html-with-w3m-keymap t
136 "If non-nil, use emacs-w3m command keys in the article buffer."
138 :group 'mime-display)
140 (defcustom mm-inline-media-tests
144 (mm-valid-and-fit-image-p 'jpeg handle)))
148 (mm-valid-and-fit-image-p 'png handle)))
152 (mm-valid-and-fit-image-p 'gif handle)))
156 (mm-valid-and-fit-image-p 'tiff handle)) )
160 (mm-valid-and-fit-image-p 'xbm handle)))
164 (mm-valid-and-fit-image-p 'xbm handle)))
168 (mm-valid-and-fit-image-p 'xpm handle)))
172 (mm-valid-and-fit-image-p 'xpm handle)))
176 (mm-valid-and-fit-image-p 'bmp handle)))
177 ("image/x-portable-bitmap"
180 (mm-valid-and-fit-image-p 'pbm handle)))
181 ("text/plain" mm-inline-text identity)
182 ("text/enriched" mm-inline-text identity)
183 ("text/richtext" mm-inline-text identity)
184 ("text/x-patch" mm-display-patch-inline
186 (locate-library "diff-mode")))
187 ("application/emacs-lisp" mm-display-elisp-inline identity)
188 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
192 (or mm-inline-text-html-renderer
193 mm-text-html-renderer)))
197 (or (featurep 'vcard)
198 (locate-library "vcard"))))
199 ("message/delivery-status" mm-inline-text identity)
200 ("message/rfc822" mm-inline-message identity)
201 ("message/partial" mm-inline-partial identity)
202 ("message/external-body" mm-inline-external-body identity)
203 ("text/.*" mm-inline-text identity)
204 ("audio/wav" mm-inline-audio
206 (and (or (featurep 'nas-sound) (featurep 'native-sound))
207 (device-sound-enabled-p))))
211 (and (or (featurep 'nas-sound) (featurep 'native-sound))
212 (device-sound-enabled-p))))
213 ("application/pgp-signature" ignore identity)
214 ("application/x-pkcs7-signature" ignore identity)
215 ("application/pkcs7-signature" ignore identity)
216 ("application/x-pkcs7-mime" ignore identity)
217 ("application/pkcs7-mime" ignore identity)
218 ("multipart/alternative" ignore identity)
219 ("multipart/mixed" ignore identity)
220 ("multipart/related" ignore identity)
221 ;; Disable audio and image
222 ("audio/.*" ignore ignore)
223 ("image/.*" ignore ignore)
224 ;; Default to displaying as text
225 (".*" mm-inline-text mm-readable-p))
226 "Alist of media types/tests saying whether types can be displayed inline."
227 :type '(repeat (list (string :tag "MIME type")
228 (function :tag "Display function")
229 (function :tag "Display test")))
230 :group 'mime-display)
232 (defcustom mm-inlined-types
233 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
234 "message/partial" "message/external-body" "application/emacs-lisp"
235 "application/x-emacs-lisp"
236 "application/pgp-signature" "application/x-pkcs7-signature"
237 "application/pkcs7-signature" "application/x-pkcs7-mime"
238 "application/pkcs7-mime")
239 "List of media types that are to be displayed inline.
240 See also `mm-inline-media-tests', which says how to display a media
242 :type '(repeat string)
243 :group 'mime-display)
245 (defcustom mm-keep-viewer-alive-types
246 '("application/postscript" "application/msword" "application/vnd.ms-excel"
247 "application/pdf" "application/x-dvi")
248 "List of media types for which the external viewer will not be killed
249 when selecting a different article."
250 :type '(repeat string)
251 :group 'mime-display)
253 (defcustom mm-automatic-display
254 '("text/plain" "text/enriched" "text/richtext" "text/html"
255 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
256 "message/rfc822" "text/x-patch" "application/pgp-signature"
257 "application/emacs-lisp" "application/x-emacs-lisp"
258 "application/x-pkcs7-signature"
259 "application/pkcs7-signature" "application/x-pkcs7-mime"
260 "application/pkcs7-mime")
261 "A list of MIME types to be displayed automatically."
262 :type '(repeat string)
263 :group 'mime-display)
265 (defcustom mm-attachment-override-types '("text/x-vcard"
266 "application/pkcs7-mime"
267 "application/x-pkcs7-mime"
268 "application/pkcs7-signature"
269 "application/x-pkcs7-signature")
270 "Types to have \"attachment\" ignored if they can be displayed inline."
271 :type '(repeat string)
272 :group 'mime-display)
274 (defcustom mm-inline-override-types nil
275 "Types to be treated as attachments even if they can be displayed inline."
276 :type '(repeat string)
277 :group 'mime-display)
279 (defcustom mm-automatic-external-display nil
280 "List of MIME type regexps that will be displayed externally automatically."
281 :type '(repeat string)
282 :group 'mime-display)
284 (defcustom mm-discouraged-alternatives nil
285 "List of MIME types that are discouraged when viewing multipart/alternative.
286 Viewing agents are supposed to view the last possible part of a message,
287 as that is supposed to be the richest. However, users may prefer other
288 types instead, and this list says what types are most unwanted. If,
289 for instance, text/html parts are very unwanted, and text/richtext are
290 somewhat unwanted, then the value of this variable should be set
293 (\"text/html\" \"text/richtext\")"
294 :type '(repeat string)
295 :group 'mime-display)
297 (defcustom mm-tmp-directory
298 (cond ((fboundp 'temp-directory) (temp-directory))
299 ((boundp 'temporary-file-directory) temporary-file-directory)
301 "Where mm will store its temporary files."
303 :group 'mime-display)
305 (defcustom mm-inline-large-images nil
306 "If non-nil, then all images fit in the buffer."
308 :group 'mime-display)
310 (defvar mm-file-name-rewrite-functions nil
311 "*List of functions used for rewriting file names of MIME parts.
312 Each function takes a file name as input and returns a file name.
314 Ready-made functions include
315 `mm-file-name-delete-whitespace',
316 `mm-file-name-trim-whitespace',
317 `mm-file-name-collapse-whitespace',
318 `mm-file-name-replace-whitespace',
319 `capitalize', `downcase', `upcase', and
322 (defvar mm-path-name-rewrite-functions nil
323 "*List of functions used for rewriting path names of MIME parts.
324 This is used when viewing parts externally , and is meant for
325 transforming the path name so that non-compliant programs can
326 find the file where it's saved.
328 Each function takes a file name as input and returns a file name.")
330 (defvar mm-file-name-replace-whitespace nil
331 "String used for replacing whitespace characters; default is `\"_\"'.")
333 (defcustom mm-default-directory nil
334 "The default directory where mm will save files.
335 If not set, `default-directory' will be used."
336 :type '(choice directory (const :tag "Default" nil))
337 :group 'mime-display)
339 (defcustom mm-external-terminal-program "xterm"
340 "The program to start an external terminal."
342 :group 'mime-display)
344 ;;; Internal variables.
346 (defvar mm-dissection-list nil)
347 (defvar mm-last-shell-command "")
348 (defvar mm-content-id-alist nil)
349 (defvar mm-postponed-undisplay-list nil)
351 ;; According to RFC2046, in particular, in a digest, the default
352 ;; Content-Type value for a body part is changed from "text/plain" to
354 (defvar mm-dissect-default-type "text/plain")
356 (autoload 'mml2015-verify "mml2015")
357 (autoload 'mml2015-verify-test "mml2015")
358 (autoload 'mml-smime-verify "mml-smime")
359 (autoload 'mml-smime-verify-test "mml-smime")
361 (defvar mm-verify-function-alist
362 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
363 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
364 mm-uu-pgp-signed-test)
365 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
366 mml-smime-verify-test)
367 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
368 mml-smime-verify-test)))
370 (defcustom mm-verify-option 'never
371 "Option of verifying signed parts.
372 `never', not verify; `always', always verify;
373 `known', only verify known protocols. Otherwise, ask user."
374 :type '(choice (item always)
376 (item :tag "only known protocols" known)
377 (item :tag "ask" nil))
378 :group 'mime-security)
380 (autoload 'mml2015-decrypt "mml2015")
381 (autoload 'mml2015-decrypt-test "mml2015")
383 (defvar mm-decrypt-function-alist
384 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
385 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
386 mm-uu-pgp-encrypted-test)))
388 (defcustom mm-decrypt-option nil
389 "Option of decrypting encrypted parts.
390 `never', not decrypt; `always', always decrypt;
391 `known', only decrypt known protocols. Otherwise, ask user."
392 :type '(choice (item always)
394 (item :tag "only known protocols" known)
395 (item :tag "ask" nil))
396 :group 'mime-security)
398 (defvar mm-viewer-completion-map
399 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
400 (set-keymap-parent map minibuffer-local-completion-map)
402 "Keymap for input viewer with completion.")
404 ;; Should we bind other key to minibuffer-complete-word?
405 (define-key mm-viewer-completion-map " " 'self-insert-command)
407 (defvar mm-viewer-completion-map
408 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
409 (set-keymap-parent map minibuffer-local-completion-map)
411 "Keymap for input viewer with completion.")
413 ;; Should we bind other key to minibuffer-complete-word?
414 (define-key mm-viewer-completion-map " " 'self-insert-command)
418 (defun mm-alist-to-plist (alist)
419 "Convert association list ALIST into the equivalent property-list form.
420 The plist is returned. This converts from
422 \((a . 1) (b . 2) (c . 3))
428 The original alist is not modified. See also `destructive-alist-to-plist'."
431 (let ((el (car alist)))
432 (setq plist (cons (cdr el) (cons (car el) plist))))
433 (setq alist (cdr alist)))
436 (defun mm-keep-viewer-alive-p (handle)
437 "Say whether external viewer for HANDLE should stay alive."
438 (let ((types mm-keep-viewer-alive-types)
439 (type (mm-handle-media-type handle))
442 (while (setq ty (pop types))
443 (when (string-match ty type)
444 (throw 'found t))))))
446 (defun mm-handle-set-external-undisplayer (handle function)
447 "Set the undisplayer for this handle; postpone undisplaying of viewers
448 for types in mm-keep-viewer-alive-types."
449 (if (mm-keep-viewer-alive-p handle)
450 (let ((new-handle (copy-sequence handle)))
451 (mm-handle-set-undisplayer new-handle function)
452 (mm-handle-set-undisplayer handle nil)
453 (push new-handle mm-postponed-undisplay-list))
454 (mm-handle-set-undisplayer handle function)))
456 (defun mm-destroy-postponed-undisplay-list ()
457 (when mm-postponed-undisplay-list
458 (message "Destroying external MIME viewers")
459 (mm-destroy-parts mm-postponed-undisplay-list)))
461 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime)
462 "Dissect the current buffer and return a list of MIME handles."
464 (let (ct ctl type subtype cte cd description id result from)
466 (mail-narrow-to-head)
467 (when (or no-strict-mime
469 (mail-fetch-field "mime-version"))
470 (setq ct (mail-fetch-field "content-type")
471 ctl (ignore-errors (mail-header-parse-content-type ct))
472 cte (mail-fetch-field "content-transfer-encoding")
473 cd (mail-fetch-field "content-disposition")
474 description (mail-fetch-field "content-description")
475 from (mail-fetch-field "from")
476 id (mail-fetch-field "content-id"))
477 ;; FIXME: In some circumstances, this code is running within
478 ;; an unibyte macro. mail-extract-address-components
479 ;; creates unibyte buffers. This `if', though not a perfect
480 ;; solution, avoids most of them.
482 (setq from (cadr (mail-extract-address-components from))))))
484 (setq cte (mail-header-strip cte)))
486 (not (string-match "/" (car ctl))))
487 (mm-dissect-singlepart
488 (list mm-dissect-default-type)
489 (and cte (intern (downcase (mail-header-remove-whitespace
490 (mail-header-remove-comments
493 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
495 (setq type (split-string (car ctl) "/"))
496 (setq subtype (cadr type)
501 ((equal type "multipart")
502 (let ((mm-dissect-default-type (if (equal subtype "digest")
505 (add-text-properties 0 (length (car ctl))
506 (mm-alist-to-plist (cdr ctl)) (car ctl))
508 ;; what really needs to be done here is a way to link a
509 ;; MIME handle back to it's parent MIME handle (in a multilevel
510 ;; MIME article). That would probably require changing
511 ;; the mm-handle API so we simply store the multipart buffert
512 ;; name as a text property of the "multipart/whatever" string.
513 (add-text-properties 0 (length (car ctl))
514 (list 'buffer (mm-copy-to-buffer))
516 (add-text-properties 0 (length (car ctl))
519 (cons (car ctl) (mm-dissect-multipart ctl))))
521 (mm-possibly-verify-or-decrypt
522 (mm-dissect-singlepart
524 (and cte (intern (downcase (mail-header-remove-whitespace
525 (mail-header-remove-comments
528 (and cd (ignore-errors
529 (mail-header-parse-content-disposition cd)))
533 (when (string-match " *<\\(.*\\)> *" id)
534 (setq id (match-string 1 id)))
535 (push (cons id result) mm-content-id-alist))
538 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
540 (if (equal "text/plain" (car ctl))
543 (let ((res (mm-make-handle
544 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
545 (push (car res) mm-dissection-list)
548 (defun mm-remove-all-parts ()
549 "Remove all MIME handles."
551 (mapcar 'mm-remove-part mm-dissection-list)
552 (setq mm-dissection-list nil))
554 (defun mm-dissect-multipart (ctl)
555 (goto-char (point-min))
556 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
557 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
560 (goto-char (point-max))
561 (if (re-search-backward close-delimiter nil t)
564 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
565 (while (and (< (point) end) (re-search-forward boundary end t))
566 (goto-char (match-beginning 0))
570 (narrow-to-region start (point))
571 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
573 (or (looking-at boundary)
575 (setq start (point)))
576 (when (and start (< start end))
579 (narrow-to-region start end)
580 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
581 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
583 (defun mm-copy-to-buffer ()
584 "Copy the contents of the current buffer to a fresh buffer."
586 (let ((obuf (current-buffer))
588 (goto-char (point-min))
589 (search-forward-regexp "^\n" nil t)
591 (set-buffer (generate-new-buffer " *mm*"))
592 (insert-buffer-substring obuf beg)
595 (defun mm-display-parts (handle &optional no-default)
596 (if (stringp (car handle))
597 (mapcar 'mm-display-parts (cdr handle))
598 (if (bufferp (car handle))
600 (narrow-to-region (point) (point))
601 (mm-display-part handle)
602 (goto-char (point-max)))
603 (mapcar 'mm-display-parts handle))))
605 (defun mm-display-part (handle &optional no-default)
606 "Display the MIME part represented by HANDLE.
607 Returns nil if the part is removed; inline if displayed inline;
608 external if displayed external."
610 (mailcap-parse-mailcaps)
611 (if (mm-handle-displayed-p handle)
612 (mm-remove-part handle)
613 (let* ((type (mm-handle-media-type handle))
614 (method (mailcap-mime-info type)))
615 (if (and (mm-inlinable-p handle)
616 (mm-inlined-p handle))
619 (mm-display-inline handle)
623 (if (and (not method)
624 (equal "text" (car (split-string type))))
627 (mm-insert-inline handle (mm-get-part handle))
630 handle (or method 'mailcap-save-binary-file)))))))))
632 (defun mm-display-external (handle method)
633 "Display HANDLE using METHOD."
634 (let ((outbuf (current-buffer)))
635 (mm-with-unibyte-buffer
636 (if (functionp method)
637 (let ((cur (current-buffer)))
638 (if (eq method 'mailcap-save-binary-file)
640 (set-buffer (generate-new-buffer " *mm*"))
642 (mm-insert-part handle)
643 (let ((win (get-buffer-window cur t)))
645 (select-window win)))
646 (switch-to-buffer (generate-new-buffer " *mm*")))
647 (buffer-disable-undo)
648 (mm-set-buffer-file-coding-system mm-binary-coding-system)
649 (insert-buffer-substring cur)
650 (goto-char (point-min))
651 (message "Viewing with %s" method)
652 (let ((mm (current-buffer))
653 (non-viewer (assq 'non-viewer
655 (mm-handle-media-type handle) t))))
659 (mm-save-part handle))
660 (when (and (not non-viewer)
662 (mm-handle-set-undisplayer handle mm)))))
663 ;; The function is a string to be executed.
664 (mm-insert-part handle)
665 (let* ((dir (make-temp-name
666 (expand-file-name "emm." mm-tmp-directory)))
668 (mail-content-type-get
669 (mm-handle-disposition handle) 'filename)
670 (mail-content-type-get
671 (mm-handle-type handle) 'name)))
672 (mime-info (mailcap-mime-info
673 (mm-handle-media-type handle) t))
674 (needsterm (or (assoc "needsterm" mime-info)
675 (assoc "needsterminal" mime-info)))
676 (copiousoutput (assoc "copiousoutput" mime-info))
678 ;; We create a private sub-directory where we store our files.
680 (set-file-modes dir 448)
682 (setq file (expand-file-name
683 (gnus-map-function mm-file-name-rewrite-functions
684 (file-name-nondirectory filename))
686 (setq file (make-temp-name (expand-file-name "mm." dir))))
687 (let ((coding-system-for-write mm-binary-coding-system))
688 (write-region (point-min) (point-max) file nil 'nomesg))
689 (message "Viewing with %s" method)
694 (start-process "*display*" nil
695 mm-external-terminal-program
699 method file (mm-handle-type handle)))
710 (mm-handle-type handle)))))
713 (set-process-sentinel
714 (get-buffer-process buffer)
715 `(lambda (process state)
716 (if (eq 'exit (process-status process))
717 (gnus-configure-windows
718 ',gnus-current-window-configuration))))
719 (gnus-configure-windows 'display-term))
720 (mm-handle-set-external-undisplayer handle (cons file buffer)))
721 (message "Displaying %s..." (format method file))
724 (with-current-buffer outbuf
730 (call-process shell-file-name nil
732 (generate-new-buffer " *mm*"))
736 method file (mm-handle-type handle)))
737 (if (buffer-live-p buffer)
742 (ignore-errors (delete-file file))
743 (ignore-errors (delete-directory
744 (file-name-directory file)))
745 (ignore-errors (kill-buffer buffer))))))
749 (start-process "*display*"
751 (generate-new-buffer " *mm*"))
755 method file (mm-handle-type handle)))
756 (mm-handle-set-external-undisplayer
757 handle (cons file buffer)))
758 (message "Displaying %s..." (format method file))
761 (defun mm-mailcap-command (method file type-list)
762 (let ((ctl (cdr type-list))
766 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
767 (push (substring method beg (match-beginning 0)) out)
768 (setq beg (match-end 0)
769 total (match-string 0 method)
770 sub (match-string 1 method))
772 ((string= total "%%")
774 ((string= total "%s")
775 (setq uses-stdin nil)
777 (gnus-map-function mm-path-name-rewrite-functions file)) out))
778 ((string= total "%t")
779 (push (mm-quote-arg (car type-list)) out))
781 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
782 (push (substring method beg (length method)) out)
786 (gnus-map-function mm-path-name-rewrite-functions file))
788 (mapconcat 'identity (nreverse out) "")))
790 (defun mm-remove-parts (handles)
791 "Remove the displayed MIME parts represented by HANDLES."
792 (if (and (listp handles)
793 (bufferp (car handles)))
794 (mm-remove-part handles)
796 (while (setq handle (pop handles))
799 (when (buffer-live-p (get-text-property 0 'buffer handle))
800 (kill-buffer (get-text-property 0 'buffer handle))))
802 (stringp (car handle)))
803 (mm-remove-parts (cdr handle)))
805 (mm-remove-part handle)))))))
807 (defun mm-destroy-parts (handles)
808 "Remove the displayed MIME parts represented by HANDLES."
809 (if (and (listp handles)
810 (bufferp (car handles)))
811 (mm-destroy-part handles)
813 (while (setq handle (pop handles))
816 (when (buffer-live-p (get-text-property 0 'buffer handle))
817 (kill-buffer (get-text-property 0 'buffer handle))))
819 (stringp (car handle)))
820 (mm-destroy-parts handle))
822 (mm-destroy-part handle)))))))
824 (defun mm-remove-part (handle)
825 "Remove the displayed MIME part represented by HANDLE."
827 (let ((object (mm-handle-undisplayer handle)))
830 ;; Internally displayed part.
831 ((mm-annotationp object)
832 (delete-annotation object))
833 ((or (functionp object)
835 (eq (car object) 'lambda)))
837 ;; Externally displayed part.
839 (ignore-errors (delete-file (car object)))
840 (ignore-errors (delete-directory (file-name-directory (car object))))
841 (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
843 (when (buffer-live-p object)
844 (kill-buffer object)))))
845 (mm-handle-set-undisplayer handle nil))))
847 (defun mm-display-inline (handle)
848 (let* ((type (mm-handle-media-type handle))
849 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
850 (funcall function handle)
851 (goto-char (point-min))))
853 (defun mm-assoc-string-match (alist type)
855 (when (string-match (car elem) type)
858 (defun mm-automatic-display-p (handle)
859 "Say whether the user wants HANDLE to be displayed automatically."
860 (let ((methods mm-automatic-display)
861 (type (mm-handle-media-type handle))
863 (while (setq method (pop methods))
864 (when (and (not (mm-inline-override-p handle))
865 (string-match method type))
870 (defun mm-inlinable-p (handle)
871 "Say whether HANDLE can be displayed inline."
872 (let ((alist mm-inline-media-tests)
873 (type (mm-handle-media-type handle))
876 (when (string-match (caar alist) type)
877 (setq test (caddar alist)
879 (setq test (funcall test handle)))
883 (defun mm-inlined-p (handle)
884 "Say whether the user wants HANDLE to be displayed inline."
885 (let ((methods mm-inlined-types)
886 (type (mm-handle-media-type handle))
888 (while (setq method (pop methods))
889 (when (and (not (mm-inline-override-p handle))
890 (string-match method type))
895 (defun mm-attachment-override-p (handle)
896 "Say whether HANDLE should have attachment behavior overridden."
897 (let ((types mm-attachment-override-types)
898 (type (mm-handle-media-type handle))
901 (while (setq ty (pop types))
902 (when (and (string-match ty type)
903 (mm-inlinable-p handle))
904 (throw 'found t))))))
906 (defun mm-inline-override-p (handle)
907 "Say whether HANDLE should have inline behavior overridden."
908 (let ((types mm-inline-override-types)
909 (type (mm-handle-media-type handle))
912 (while (setq ty (pop types))
913 (when (string-match ty type)
914 (throw 'found t))))))
916 (defun mm-automatic-external-display-p (type)
917 "Return the user-defined method for TYPE."
918 (let ((methods mm-automatic-external-display)
920 (while (setq method (pop methods))
921 (when (string-match method type)
926 (defun mm-destroy-part (handle)
927 "Destroy the data structures connected to HANDLE."
929 (mm-remove-part handle)
930 (when (buffer-live-p (mm-handle-buffer handle))
931 (kill-buffer (mm-handle-buffer handle)))))
933 (defun mm-handle-displayed-p (handle)
934 "Say whether HANDLE is displayed or not."
935 (mm-handle-undisplayer handle))
938 ;;; Functions for outputting parts
941 (defun mm-get-part (handle)
942 "Return the contents of HANDLE as a string."
943 (mm-with-unibyte-buffer
944 (insert (with-current-buffer (mm-handle-buffer handle)
945 (mm-with-unibyte-current-buffer-mule4
947 (mm-decode-content-transfer-encoding
948 (mm-handle-encoding handle)
949 (mm-handle-media-type handle))
952 (defun mm-insert-part (handle)
953 "Insert the contents of HANDLE in the current buffer."
954 (let ((cur (current-buffer)))
956 (if (member (mm-handle-media-supertype handle) '("text" "message"))
958 (insert-buffer-substring (mm-handle-buffer handle))
960 (mm-decode-content-transfer-encoding
961 (mm-handle-encoding handle)
962 (mm-handle-media-type handle))
963 (let ((temp (current-buffer)))
965 (insert-buffer-substring temp))))
966 (mm-with-unibyte-buffer
967 (insert-buffer-substring (mm-handle-buffer handle))
969 (mm-decode-content-transfer-encoding
970 (mm-handle-encoding handle)
971 (mm-handle-media-type handle))
972 (let ((temp (current-buffer)))
974 (insert-buffer-substring temp))))))))
976 (defun mm-file-name-delete-whitespace (file-name)
977 "Remove all whitespace characters from FILE-NAME."
978 (while (string-match "\\s-+" file-name)
979 (setq file-name (replace-match "" t t file-name)))
982 (defun mm-file-name-trim-whitespace (file-name)
983 "Remove leading and trailing whitespace characters from FILE-NAME."
984 (when (string-match "\\`\\s-+" file-name)
985 (setq file-name (substring file-name (match-end 0))))
986 (when (string-match "\\s-+\\'" file-name)
987 (setq file-name (substring file-name 0 (match-beginning 0))))
990 (defun mm-file-name-collapse-whitespace (file-name)
991 "Collapse multiple whitespace characters in FILE-NAME."
992 (while (string-match "\\s-\\s-+" file-name)
993 (setq file-name (replace-match " " t t file-name)))
996 (defun mm-file-name-replace-whitespace (file-name)
997 "Replace whitespace characters in FILE-NAME with underscores.
998 Set `mm-file-name-replace-whitespace' to any other string if you do not
1000 (let ((s (or mm-file-name-replace-whitespace "_")))
1001 (while (string-match "\\s-" file-name)
1002 (setq file-name (replace-match s t t file-name))))
1005 (defun mm-save-part (handle)
1006 "Write HANDLE to a file."
1007 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1008 (filename (mail-content-type-get
1009 (mm-handle-disposition handle) 'filename))
1012 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1013 (file-name-nondirectory filename))))
1015 (read-file-name "Save MIME part to: "
1017 (or filename name "")
1018 (or mm-default-directory default-directory))))
1019 (setq mm-default-directory (file-name-directory file))
1020 (and (or (not (file-exists-p file))
1021 (yes-or-no-p (format "File %s already exists; overwrite? "
1024 (mm-save-part-to-file handle file)
1027 (defun mm-save-part-to-file (handle file)
1028 (mm-with-unibyte-buffer
1029 (mm-insert-part handle)
1030 (let ((coding-system-for-write 'binary)
1031 ;; Don't re-compress .gz & al. Arguably we should make
1032 ;; `file-name-handler-alist' nil, but that would chop
1033 ;; ange-ftp, which is reasonable to use here.
1034 (inhibit-file-name-operation 'write-region)
1035 (inhibit-file-name-handlers
1036 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1037 (write-region (point-min) (point-max) file))))
1039 (defun mm-pipe-part (handle)
1040 "Pipe HANDLE to a process."
1041 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1043 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1044 (mm-with-unibyte-buffer
1045 (mm-insert-part handle)
1046 (let ((coding-system-for-write 'binary))
1047 (shell-command-on-region (point-min) (point-max) command nil)))))
1049 (defun mm-interactively-view-part (handle)
1050 "Display HANDLE using METHOD."
1051 (let* ((type (mm-handle-media-type handle))
1053 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1054 (mailcap-mime-info type 'all)))
1055 (method (let ((minibuffer-local-completion-map
1056 mm-viewer-completion-map))
1057 (completing-read "Viewer: " methods))))
1058 (when (string= method "")
1059 (error "No method given"))
1060 (if (string-match "^[^% \t]+$" method)
1061 (setq method (concat method " %s")))
1062 (mm-display-external handle method)))
1064 (defun mm-preferred-alternative (handles &optional preferred)
1065 "Say which of HANDLES are preferred."
1066 (let ((prec (if preferred (list preferred)
1067 (mm-preferred-alternative-precedence handles)))
1068 p h result type handle)
1069 (while (setq p (pop prec))
1072 (setq handle (car h))
1073 (setq type (mm-handle-media-type handle))
1074 (when (and (equal p type)
1075 (mm-automatic-display-p handle)
1076 (or (stringp (car handle))
1077 (not (mm-handle-disposition handle))
1078 (equal (car (mm-handle-disposition handle))
1086 (defun mm-preferred-alternative-precedence (handles)
1087 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1088 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1090 (dolist (disc (reverse mm-discouraged-alternatives))
1091 (dolist (elem (copy-sequence seq))
1092 (when (string-match disc elem)
1093 (setq seq (nconc (delete elem seq) (list elem))))))
1096 (defun mm-get-content-id (id)
1097 "Return the handle(s) referred to by ID."
1098 (cdr (assoc id mm-content-id-alist)))
1100 (defconst mm-image-type-regexps
1101 '(("/\\*.*XPM.\\*/" . xpm)
1105 ("\211PNG\r\n" . png)
1107 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1108 ("%!PS" . postscript))
1109 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1110 When the first bytes of an image file match REGEXP, it is assumed to
1111 be of image type IMAGE-TYPE.")
1113 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1114 (defun mm-image-type-from-buffer ()
1115 "Determine the image type from data in the current buffer.
1116 Value is a symbol specifying the image type or nil if type cannot
1118 (let ((types mm-image-type-regexps)
1120 (goto-char (point-min))
1121 (while (and types (null type))
1122 (let ((regexp (car (car types)))
1123 (image-type (cdr (car types))))
1124 (when (looking-at regexp)
1125 (setq type image-type))
1126 (setq types (cdr types))))
1129 (defun mm-get-image (handle)
1130 "Return an image instance based on HANDLE."
1131 (let ((type (mm-handle-media-subtype handle))
1133 ;; Allow some common translations.
1136 ((equal type "x-pixmap")
1138 ((equal type "x-xbitmap")
1140 ((equal type "x-portable-bitmap")
1143 (or (mm-handle-cache handle)
1144 (mm-with-unibyte-buffer
1145 (mm-insert-part handle)
1149 ;; Avoid testing `make-glyph' since W3 may define
1150 ;; a bogus version of it.
1151 (if (fboundp 'create-image)
1152 (create-image (buffer-string)
1153 (or (mm-image-type-from-buffer)
1156 (mm-create-image-xemacs type))))
1157 (mm-handle-set-cache handle spec))))))
1159 (defun mm-create-image-xemacs (type)
1162 ;; xbm images require special handling, since
1163 ;; the only way to create glyphs from these
1164 ;; (without a ton of work) is to write them
1165 ;; out to a file, and then create a file
1167 (let ((file (make-temp-name
1168 (expand-file-name "emm.xbm"
1169 mm-tmp-directory))))
1172 (write-region (point-min) (point-max) file)
1173 (make-glyph (list (cons 'x file))))
1175 (delete-file file)))))
1179 (or (mm-image-type-from-buffer)
1181 :data (buffer-string))))))
1183 (defun mm-image-fit-p (handle)
1184 "Say whether the image in HANDLE will fit the current window."
1185 (let ((image (mm-get-image handle)))
1186 (if (fboundp 'glyph-width)
1187 ;; XEmacs' glyphs can actually tell us about their width, so
1188 ;; lets be nice and smart about them.
1189 (or mm-inline-large-images
1190 (and (< (glyph-width image) (window-pixel-width))
1191 (< (glyph-height image) (window-pixel-height))))
1192 (let* ((size (image-size image))
1195 (or mm-inline-large-images
1196 (and (< h (1- (window-height))) ; Don't include mode line.
1197 (< w (window-width))))))))
1199 (defun mm-valid-image-format-p (format)
1200 "Say whether FORMAT can be displayed natively by Emacs."
1203 ((fboundp 'valid-image-instantiator-format-p)
1204 (valid-image-instantiator-format-p format))
1206 ((fboundp 'image-type-available-p)
1207 (and (display-graphic-p)
1208 (image-type-available-p format)))
1209 ;; Nobody else can do images yet.
1213 (defun mm-valid-and-fit-image-p (format handle)
1214 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1215 (and (mm-valid-image-format-p format)
1216 (mm-image-fit-p handle)))
1218 (defun mm-find-part-by-type (handles type &optional notp recursive)
1219 "Search in HANDLES for part with TYPE.
1220 If NOTP, returns first non-matching part.
1221 If RECURSIVE, search recursively."
1224 (if (and recursive (stringp (caar handles)))
1225 (if (setq handle (mm-find-part-by-type (cdar handles) type
1229 (not (equal (mm-handle-media-type (car handles)) type))
1230 (equal (mm-handle-media-type (car handles)) type))
1231 (setq handle (car handles)
1233 (setq handles (cdr handles)))
1236 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1237 (goto-char (point-min))
1238 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1240 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1242 (end (save-excursion
1243 (goto-char (point-max))
1244 (if (re-search-backward close-delimiter nil t)
1248 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1249 (while (and (not result)
1250 (re-search-forward boundary end t))
1251 (goto-char (match-beginning 0))
1255 (narrow-to-region start (1- (point)))
1256 (when (let ((ctl (ignore-errors
1257 (mail-header-parse-content-type
1258 (mail-fetch-field "content-type")))))
1260 (not (equal (car ctl) type))
1261 (equal (car ctl) type)))
1262 (setq result (buffer-substring (point-min) (point-max)))))))
1264 (setq start (point)))
1265 (when (and (not result) start)
1268 (narrow-to-region start end)
1269 (when (let ((ctl (ignore-errors
1270 (mail-header-parse-content-type
1271 (mail-fetch-field "content-type")))))
1273 (not (equal (car ctl) type))
1274 (equal (car ctl) type)))
1275 (setq result (buffer-substring (point-min) (point-max)))))))
1278 (defvar mm-security-handle nil)
1280 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1281 ;; HANDLE could be a CTL.
1283 (put-text-property 0 (length (car handle)) parameter value
1286 (defun mm-possibly-verify-or-decrypt (parts ctl)
1287 (let ((type (car ctl))
1288 (subtype (cadr (split-string (car ctl) "/")))
1289 (mm-security-handle ctl) ;; (car CTL) is the type.
1290 protocol func functest)
1292 ((or (equal type "application/x-pkcs7-mime")
1293 (equal type "application/pkcs7-mime"))
1296 ((eq mm-decrypt-option 'never) nil)
1297 ((eq mm-decrypt-option 'always) t)
1298 ((eq mm-decrypt-option 'known) t)
1300 (format "Decrypt (S/MIME) part? "))))
1301 (mm-view-pkcs7 parts))
1302 (setq parts (mm-dissect-buffer t)))))
1303 ((equal subtype "signed")
1304 (unless (and (setq protocol
1305 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1306 (not (equal protocol "multipart/mixed")))
1307 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1308 (let ((protocols mm-verify-function-alist))
1310 (if (and (or (not (setq functest (nth 3 (car protocols))))
1311 (funcall functest parts ctl))
1312 (mm-find-part-by-type parts (caar protocols) nil t))
1313 (setq protocol (caar protocols)
1315 (setq protocols (cdr protocols))))))
1316 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1318 ((eq mm-verify-option 'never) nil)
1319 ((eq mm-verify-option 'always) t)
1320 ((eq mm-verify-option 'known)
1322 (or (not (setq functest
1323 (nth 3 (assoc protocol
1324 mm-verify-function-alist))))
1325 (funcall functest parts ctl))))
1327 (format "Verify signed (%s) part? "
1328 (or (nth 2 (assoc protocol mm-verify-function-alist))
1329 (format "protocol=%s" protocol))))))
1332 (funcall func parts ctl)
1333 (mm-set-handle-multipart-parameter
1334 mm-security-handle 'gnus-details
1335 (format "Unknown sign protocol (%s)" protocol))))))
1336 ((equal subtype "encrypted")
1337 (unless (setq protocol
1338 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1339 ;; The message is broken.
1340 (let ((parts parts))
1342 (if (assoc (mm-handle-media-type (car parts))
1343 mm-decrypt-function-alist)
1344 (setq protocol (mm-handle-media-type (car parts))
1346 (setq parts (cdr parts))))))
1347 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1349 ((eq mm-decrypt-option 'never) nil)
1350 ((eq mm-decrypt-option 'always) t)
1351 ((eq mm-decrypt-option 'known)
1353 (or (not (setq functest
1354 (nth 3 (assoc protocol
1355 mm-decrypt-function-alist))))
1356 (funcall functest parts ctl))))
1358 (format "Decrypt (%s) part? "
1359 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1360 (format "protocol=%s" protocol))))))
1363 (setq parts (funcall func parts ctl))
1364 (mm-set-handle-multipart-parameter
1365 mm-security-handle 'gnus-details
1366 (format "Unknown encrypt protocol (%s)" protocol))))))
1370 (defun mm-multiple-handles (handles)
1371 (and (listp (car handles))
1372 (> (length handles) 1)))
1374 (defun mm-merge-handles (handles1 handles2)
1376 (if (listp (car handles1))
1379 (if (listp (car handles2))
1383 (defun mm-readable-p (handle)
1384 "Say whether the content of HANDLE is readable."
1385 (and (< (with-current-buffer (mm-handle-buffer handle)
1386 (buffer-size)) 10000)
1387 (mm-with-unibyte-buffer
1388 (mm-insert-part handle)
1389 (and (eq (mm-body-7-or-8) '7bit)
1390 (not (mm-long-lines-p 76))))))
1392 (provide 'mm-decode)
1394 ;;; mm-decode.el ends here