1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
37 (eval-when-compile (require 'cl)
41 (autoload 'mm-inline-partial "mm-partial")
42 (autoload 'mm-inline-external-body "mm-extern")
43 (autoload 'mm-extern-cache-contents "mm-extern")
44 (autoload 'mm-insert-inline "mm-view"))
46 (defvar gnus-current-window-configuration)
48 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
50 (defgroup mime-display ()
51 "Display of MIME in mail and news articles."
52 :link '(custom-manual "(emacs-mime)Display Customization")
58 (defgroup mime-security ()
59 "MIME security in mail and news articles."
60 :link '(custom-manual "(emacs-mime)Display Customization")
65 ;;; Convenience macros.
67 (defmacro mm-handle-buffer (handle)
69 (defmacro mm-handle-type (handle)
71 (defsubst mm-handle-media-type (handle)
72 (if (stringp (car handle))
74 (car (mm-handle-type handle))))
75 (defsubst mm-handle-media-supertype (handle)
76 (car (split-string (mm-handle-media-type handle) "/")))
77 (defsubst mm-handle-media-subtype (handle)
78 (cadr (split-string (mm-handle-media-type handle) "/")))
79 (defmacro mm-handle-encoding (handle)
81 (defmacro mm-handle-undisplayer (handle)
83 (defmacro mm-handle-set-undisplayer (handle function)
84 `(setcar (nthcdr 3 ,handle) ,function))
85 (defmacro mm-handle-disposition (handle)
87 (defmacro mm-handle-description (handle)
89 (defmacro mm-handle-cache (handle)
91 (defmacro mm-handle-set-cache (handle contents)
92 `(setcar (nthcdr 6 ,handle) ,contents))
93 (defmacro mm-handle-id (handle)
95 (defmacro mm-handle-multipart-original-buffer (handle)
96 `(get-text-property 0 'buffer (car ,handle)))
97 (defmacro mm-handle-multipart-from (handle)
98 `(get-text-property 0 'from (car ,handle)))
99 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
100 `(get-text-property 0 ,parameter (car ,handle)))
102 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
103 disposition description cache
105 `(list ,buffer ,type ,encoding ,undisplayer
106 ,disposition ,description ,cache ,id))
108 (defcustom mm-text-html-renderer
109 (cond ((executable-find "w3m")
110 (if (locate-library "w3m")
113 ((executable-find "links") 'links)
114 ((executable-find "lynx") 'lynx)
115 ((locate-library "w3") 'w3)
116 ((locate-library "html2text") 'html2text)
118 "Render of HTML contents.
119 It is one of defined renderer types, or a rendering function.
120 The defined renderer types are:
121 `w3m' : use emacs-w3m;
122 `w3m-standalone': use w3m;
126 `html2text' : use html2text;
127 nil : use external viewer (default web browser)."
128 :version "23.0" ;; No Gnus
129 :type '(choice (const w3)
130 (const w3m :tag "emacs-w3m")
131 (const w3m-standalone :tag "standalone w3m" )
135 (const nil :tag "External viewer")
137 :group 'mime-display)
139 (defvar mm-inline-text-html-renderer nil
140 "Function used for rendering inline HTML contents.
141 It is suggested to customize `mm-text-html-renderer' instead.")
143 (defcustom mm-inline-text-html-with-images nil
144 "If non-nil, Gnus will allow retrieving images in HTML contents with
145 the <img> tags. It has no effect on Emacs/w3. See also the
146 documentation for the `mm-w3m-safe-url-regexp' variable."
149 :group 'mime-display)
151 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
152 "Regexp matching URLs which are considered to be safe.
153 Some HTML mails might contain a nasty trick used by spammers, using
154 the <img> tag which is far more evil than the [Click Here!] button.
155 It is most likely intended to check whether the ominous spam mail has
156 reached your eyes or not, in which case the spammer knows for sure
157 that your email address is valid. It is done by embedding an
158 identifier string into a URL that you might automatically retrieve
159 when displaying the image. The default value is \"\\\\`cid:\" which only
160 matches parts embedded to the Multipart/Related type MIME contents and
161 Gnus will never connect to the spammer's site arbitrarily. You may
162 set this variable to nil if you consider all urls to be safe."
164 :type '(choice (regexp :tag "Regexp")
165 (const :tag "All URLs are safe" nil))
166 :group 'mime-display)
168 (defcustom mm-inline-text-html-with-w3m-keymap t
169 "If non-nil, use emacs-w3m command keys in the article buffer."
172 :group 'mime-display)
174 (defcustom mm-enable-external t
175 "Indicate whether external MIME handlers should be used.
177 If t, all defined external MIME handlers are used. If nil, files are saved by
178 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
179 before the external MIME handler is invoked."
181 :type '(choice (const :tag "Always" t)
182 (const :tag "Never" nil)
183 (const :tag "Ask" ask))
184 :group 'mime-display)
186 (defcustom mm-inline-media-tests
190 (mm-valid-and-fit-image-p 'jpeg handle)))
194 (mm-valid-and-fit-image-p 'png handle)))
198 (mm-valid-and-fit-image-p 'gif handle)))
202 (mm-valid-and-fit-image-p 'tiff handle)) )
206 (mm-valid-and-fit-image-p 'xbm handle)))
210 (mm-valid-and-fit-image-p 'xbm handle)))
214 (mm-valid-and-fit-image-p 'xpm handle)))
218 (mm-valid-and-fit-image-p 'xpm handle)))
222 (mm-valid-and-fit-image-p 'bmp handle)))
223 ("image/x-portable-bitmap"
226 (mm-valid-and-fit-image-p 'pbm handle)))
227 ("text/plain" mm-inline-text identity)
228 ("text/enriched" mm-inline-text identity)
229 ("text/richtext" mm-inline-text identity)
230 ("text/x-patch" mm-display-patch-inline
232 ;; If the diff-mode.el package is installed, the function is
233 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
234 ;; to cater to broken installations. OTOH checking the function
235 ;; makes it possible to install another package which provides an
236 ;; alternative implementation of diff-mode. --Stef
237 (fboundp 'diff-mode)))
238 ("application/emacs-lisp" mm-display-elisp-inline identity)
239 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
240 ("text/dns" mm-display-dns-inline identity)
244 (or mm-inline-text-html-renderer
245 mm-text-html-renderer)))
249 (or (featurep 'vcard)
250 (locate-library "vcard"))))
251 ("message/delivery-status" mm-inline-text identity)
252 ("message/rfc822" mm-inline-message identity)
253 ("message/partial" mm-inline-partial identity)
254 ("message/external-body" mm-inline-external-body identity)
255 ("text/.*" mm-inline-text identity)
256 ("audio/wav" mm-inline-audio
258 (and (or (featurep 'nas-sound) (featurep 'native-sound))
259 (device-sound-enabled-p))))
263 (and (or (featurep 'nas-sound) (featurep 'native-sound))
264 (device-sound-enabled-p))))
265 ("application/pgp-signature" ignore identity)
266 ("application/x-pkcs7-signature" ignore identity)
267 ("application/pkcs7-signature" ignore identity)
268 ("application/x-pkcs7-mime" ignore identity)
269 ("application/pkcs7-mime" ignore identity)
270 ("multipart/alternative" ignore identity)
271 ("multipart/mixed" ignore identity)
272 ("multipart/related" ignore identity)
273 ;; Disable audio and image
274 ("audio/.*" ignore ignore)
275 ("image/.*" ignore ignore)
276 ;; Default to displaying as text
277 (".*" mm-inline-text mm-readable-p))
278 "Alist of media types/tests saying whether types can be displayed inline."
279 :type '(repeat (list (regexp :tag "MIME type")
280 (function :tag "Display function")
281 (function :tag "Display test")))
282 :group 'mime-display)
284 (defcustom mm-inlined-types
285 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
286 "message/partial" "message/external-body" "application/emacs-lisp"
287 "application/x-emacs-lisp"
288 "application/pgp-signature" "application/x-pkcs7-signature"
289 "application/pkcs7-signature" "application/x-pkcs7-mime"
290 "application/pkcs7-mime"
291 ;; Mutt still uses this even though it has already been withdrawn.
293 "List of media types that are to be displayed inline.
294 See also `mm-inline-media-tests', which says how to display a media
296 :type '(repeat regexp)
297 :group 'mime-display)
299 (defcustom mm-keep-viewer-alive-types
300 '("application/postscript" "application/msword" "application/vnd.ms-excel"
301 "application/pdf" "application/x-dvi")
302 "List of media types for which the external viewer will not be killed
303 when selecting a different article."
305 :type '(repeat regexp)
306 :group 'mime-display)
308 (defcustom mm-automatic-display
309 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
310 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
311 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
312 "application/emacs-lisp" "application/x-emacs-lisp"
313 "application/x-pkcs7-signature"
314 "application/pkcs7-signature" "application/x-pkcs7-mime"
315 "application/pkcs7-mime"
316 ;; Mutt still uses this even though it has already been withdrawn.
317 "application/pgp\\'")
318 "A list of MIME types to be displayed automatically."
319 :type '(repeat regexp)
320 :group 'mime-display)
322 (defcustom mm-attachment-override-types '("text/x-vcard"
323 "application/pkcs7-mime"
324 "application/x-pkcs7-mime"
325 "application/pkcs7-signature"
326 "application/x-pkcs7-signature")
327 "Types to have \"attachment\" ignored if they can be displayed inline."
328 :type '(repeat regexp)
329 :group 'mime-display)
331 (defcustom mm-inline-override-types nil
332 "Types to be treated as attachments even if they can be displayed inline."
333 :type '(repeat regexp)
334 :group 'mime-display)
336 (defcustom mm-automatic-external-display nil
337 "List of MIME type regexps that will be displayed externally automatically."
338 :type '(repeat regexp)
339 :group 'mime-display)
341 (defcustom mm-discouraged-alternatives nil
342 "List of MIME types that are discouraged when viewing multipart/alternative.
343 Viewing agents are supposed to view the last possible part of a message,
344 as that is supposed to be the richest. However, users may prefer other
345 types instead, and this list says what types are most unwanted. If,
346 for instance, text/html parts are very unwanted, and text/richtext are
347 somewhat unwanted, then the value of this variable should be set
350 (\"text/html\" \"text/richtext\")
352 Adding \"image/.*\" might also be useful. Spammers use it as the
353 prefered part of multipart/alternative messages. See also
354 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
355 enables you to choose manually one of two types those mails include."
356 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
357 :group 'mime-display)
359 (defcustom mm-tmp-directory
360 (if (fboundp 'temp-directory)
362 (if (boundp 'temporary-file-directory)
363 temporary-file-directory
365 "Where mm will store its temporary files."
367 :group 'mime-display)
369 (defcustom mm-inline-large-images nil
370 "If non-nil, then all images fit in the buffer."
372 :group 'mime-display)
374 (defcustom mm-file-name-rewrite-functions
375 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
376 "List of functions used for rewriting file names of MIME parts.
377 Each function takes a file name as input and returns a file name.
379 Ready-made functions include `mm-file-name-delete-control',
380 `mm-file-name-delete-gotchas' (you should not remove these two
381 functions), `mm-file-name-delete-whitespace',
382 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
383 `mm-file-name-replace-whitespace', `capitalize', `downcase',
384 `upcase', and `upcase-initials'."
385 :type '(list (set :inline t
386 (const mm-file-name-delete-control)
387 (const mm-file-name-delete-gotchas)
388 (const mm-file-name-delete-whitespace)
389 (const mm-file-name-trim-whitespace)
390 (const mm-file-name-collapse-whitespace)
391 (const mm-file-name-replace-whitespace)
395 (const upcase-initials)
399 :version "23.1" ;; No Gnus
400 :group 'mime-display)
403 (defvar mm-path-name-rewrite-functions nil
404 "*List of functions for rewriting the full file names of MIME parts.
405 This is used when viewing parts externally, and is meant for
406 transforming the absolute name so that non-compliant programs can find
407 the file where it's saved.
409 Each function takes a file name as input and returns a file name.")
411 (defvar mm-file-name-replace-whitespace nil
412 "String used for replacing whitespace characters; default is `\"_\"'.")
414 (defcustom mm-default-directory nil
415 "The default directory where mm will save files.
416 If not set, `default-directory' will be used."
417 :type '(choice directory (const :tag "Default" nil))
418 :group 'mime-display)
420 (defcustom mm-attachment-file-modes 384
421 "Set the mode bits of saved attachments to this integer."
424 :group 'mime-display)
426 (defcustom mm-external-terminal-program "xterm"
427 "The program to start an external terminal."
430 :group 'mime-display)
432 ;;; Internal variables.
434 (defvar mm-last-shell-command "")
435 (defvar mm-content-id-alist nil)
436 (defvar mm-postponed-undisplay-list nil)
438 ;; According to RFC2046, in particular, in a digest, the default
439 ;; Content-Type value for a body part is changed from "text/plain" to
441 (defvar mm-dissect-default-type "text/plain")
443 (autoload 'mml2015-verify "mml2015")
444 (autoload 'mml2015-verify-test "mml2015")
445 (autoload 'mml-smime-verify "mml-smime")
446 (autoload 'mml-smime-verify-test "mml-smime")
448 (defvar mm-verify-function-alist
449 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
450 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
451 mm-uu-pgp-signed-test)
452 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
453 mml-smime-verify-test)
454 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
455 mml-smime-verify-test)))
457 (defcustom mm-verify-option 'never
458 "Option of verifying signed parts.
459 `never', not verify; `always', always verify;
460 `known', only verify known protocols. Otherwise, ask user.
462 When set to `always' or `known', you should add
463 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
464 result of the verification."
466 :type '(choice (item always)
468 (item :tag "only known protocols" known)
469 (item :tag "ask" nil))
470 :group 'mime-security)
472 (autoload 'mml2015-decrypt "mml2015")
473 (autoload 'mml2015-decrypt-test "mml2015")
475 (defvar mm-decrypt-function-alist
476 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
477 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
478 mm-uu-pgp-encrypted-test)))
480 (defcustom mm-decrypt-option nil
481 "Option of decrypting encrypted parts.
482 `never', not decrypt; `always', always decrypt;
483 `known', only decrypt known protocols. Otherwise, ask user."
485 :type '(choice (item always)
487 (item :tag "only known protocols" known)
488 (item :tag "ask" nil))
489 :group 'mime-security)
491 (defvar mm-viewer-completion-map
492 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
493 (set-keymap-parent map minibuffer-local-completion-map)
494 ;; Should we bind other key to minibuffer-complete-word?
495 (define-key map " " 'self-insert-command)
497 "Keymap for input viewer with completion.")
499 (defvar mm-viewer-completion-map
500 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
501 (set-keymap-parent map minibuffer-local-completion-map)
502 ;; Should we bind other key to minibuffer-complete-word?
503 (define-key map " " 'self-insert-command)
505 "Keymap for input viewer with completion.")
509 (defun mm-alist-to-plist (alist)
510 "Convert association list ALIST into the equivalent property-list form.
511 The plist is returned. This converts from
513 \((a . 1) (b . 2) (c . 3))
519 The original alist is not modified. See also `destructive-alist-to-plist'."
522 (let ((el (car alist)))
523 (setq plist (cons (cdr el) (cons (car el) plist))))
524 (setq alist (cdr alist)))
527 (defun mm-keep-viewer-alive-p (handle)
528 "Say whether external viewer for HANDLE should stay alive."
529 (let ((types mm-keep-viewer-alive-types)
530 (type (mm-handle-media-type handle))
533 (while (setq ty (pop types))
534 (when (string-match ty type)
535 (throw 'found t))))))
537 (defun mm-handle-set-external-undisplayer (handle function)
538 "Set the undisplayer for HANDLE to FUNCTION.
539 Postpone undisplaying of viewers for types in
540 `mm-keep-viewer-alive-types'."
541 (if (mm-keep-viewer-alive-p handle)
542 (let ((new-handle (copy-sequence handle)))
543 (mm-handle-set-undisplayer new-handle function)
544 (mm-handle-set-undisplayer handle nil)
545 (push new-handle mm-postponed-undisplay-list))
546 (mm-handle-set-undisplayer handle function)))
548 (defun mm-destroy-postponed-undisplay-list ()
549 (when mm-postponed-undisplay-list
550 (message "Destroying external MIME viewers")
551 (mm-destroy-parts mm-postponed-undisplay-list)))
553 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
554 "Dissect the current buffer and return a list of MIME handles."
556 (let (ct ctl type subtype cte cd description id result)
558 (mail-narrow-to-head)
559 (when (or no-strict-mime
561 (mail-fetch-field "mime-version"))
562 (setq ct (mail-fetch-field "content-type")
563 ctl (and ct (mail-header-parse-content-type ct))
564 cte (mail-fetch-field "content-transfer-encoding")
565 cd (mail-fetch-field "content-disposition")
566 description (mail-fetch-field "content-description")
567 id (mail-fetch-field "content-id"))
569 (setq from (mail-fetch-field "from")))
570 ;; FIXME: In some circumstances, this code is running within
571 ;; an unibyte macro. mail-extract-address-components
572 ;; creates unibyte buffers. This `if', though not a perfect
573 ;; solution, avoids most of them.
575 (setq from (cadr (mail-extract-address-components from))))
577 (setq description (mail-decode-encoded-word-string
580 (not (string-match "/" (car ctl))))
581 (mm-dissect-singlepart
582 (list mm-dissect-default-type)
583 (and cte (intern (downcase (mail-header-strip cte))))
585 (and cd (mail-header-parse-content-disposition cd))
587 (setq type (split-string (car ctl) "/"))
588 (setq subtype (cadr type)
593 ((equal type "multipart")
594 (let ((mm-dissect-default-type (if (equal subtype "digest")
597 (start (cdr (assq 'start (cdr ctl)))))
598 (add-text-properties 0 (length (car ctl))
599 (mm-alist-to-plist (cdr ctl)) (car ctl))
601 ;; what really needs to be done here is a way to link a
602 ;; MIME handle back to it's parent MIME handle (in a multilevel
603 ;; MIME article). That would probably require changing
604 ;; the mm-handle API so we simply store the multipart buffer
605 ;; name as a text property of the "multipart/whatever" string.
606 (add-text-properties 0 (length (car ctl))
607 (list 'buffer (mm-copy-to-buffer)
611 (cons (car ctl) (mm-dissect-multipart ctl from))))
613 (mm-possibly-verify-or-decrypt
614 (mm-dissect-singlepart
616 (and cte (intern (downcase (mail-header-strip cte))))
618 (and cd (mail-header-parse-content-disposition cd))
622 (when (string-match " *<\\(.*\\)> *" id)
623 (setq id (match-string 1 id)))
624 (push (cons id result) mm-content-id-alist))
627 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
629 (if (equal "text/plain" (car ctl))
633 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
635 (defun mm-dissect-multipart (ctl from)
636 (goto-char (point-min))
637 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
638 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
641 (goto-char (point-max))
642 (if (re-search-backward close-delimiter nil t)
645 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
646 (while (and (< (point) end) (re-search-forward boundary end t))
647 (goto-char (match-beginning 0))
651 (narrow-to-region start (point))
652 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
654 (or (looking-at boundary)
656 (setq start (point)))
657 (when (and start (< start end))
660 (narrow-to-region start end)
661 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
662 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
664 (defun mm-copy-to-buffer ()
665 "Copy the contents of the current buffer to a fresh buffer."
666 (let ((obuf (current-buffer))
668 (goto-char (point-min))
669 (search-forward-regexp "^\n" nil t)
672 ;; Preserve the data's unibyteness (for url-insert-file-contents).
673 (let ((default-enable-multibyte-characters (mm-multibyte-p)))
674 (generate-new-buffer " *mm*"))
675 (insert-buffer-substring obuf beg)
678 (defun mm-display-parts (handle &optional no-default)
679 (if (stringp (car handle))
680 (mapcar 'mm-display-parts (cdr handle))
681 (if (bufferp (car handle))
683 (narrow-to-region (point) (point))
684 (mm-display-part handle)
685 (goto-char (point-max)))
686 (mapcar 'mm-display-parts handle))))
688 (defun mm-display-part (handle &optional no-default)
689 "Display the MIME part represented by HANDLE.
690 Returns nil if the part is removed; inline if displayed inline;
691 external if displayed external."
693 (mailcap-parse-mailcaps)
694 (if (mm-handle-displayed-p handle)
695 (mm-remove-part handle)
696 (let* ((ehandle (if (equal (mm-handle-media-type handle)
697 "message/external-body")
699 (unless (mm-handle-cache handle)
700 (mm-extern-cache-contents handle))
701 (mm-handle-cache handle))
703 (type (mm-handle-media-type ehandle))
704 (method (mailcap-mime-info type))
705 (filename (or (mail-content-type-get
706 (mm-handle-disposition handle) 'filename)
707 (mail-content-type-get
708 (mm-handle-type handle) 'name)
710 (external mm-enable-external))
711 (if (and (mm-inlinable-p ehandle)
712 (mm-inlined-p ehandle))
715 (mm-display-inline handle)
719 (if (and (not method)
720 (equal "text" (car (split-string type "/"))))
723 (mm-insert-inline handle (mm-get-part handle))
726 (and method ;; If nil, we always use "save".
727 (stringp method) ;; 'mailcap-save-binary-file
728 (or (eq mm-enable-external t)
729 (and (eq mm-enable-external 'ask)
732 "Display part (" type
733 ") using external program"
734 ;; Can non-string method ever happen?
737 " \"" (format method filename) "\"")
742 handle (or method 'mailcap-save-binary-file))
744 handle 'mailcap-save-binary-file)))))))))
746 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
748 (defun mm-display-external (handle method)
749 "Display HANDLE using METHOD."
750 (let ((outbuf (current-buffer)))
751 (mm-with-unibyte-buffer
752 (if (functionp method)
753 (let ((cur (current-buffer)))
754 (if (eq method 'mailcap-save-binary-file)
756 (set-buffer (generate-new-buffer " *mm*"))
758 (mm-insert-part handle)
759 (mm-add-meta-html-tag handle)
760 (let ((win (get-buffer-window cur t)))
762 (select-window win)))
763 (switch-to-buffer (generate-new-buffer " *mm*")))
764 (buffer-disable-undo)
765 (mm-set-buffer-file-coding-system mm-binary-coding-system)
766 (insert-buffer-substring cur)
767 (goto-char (point-min))
769 (message "Viewing with %s" method))
770 (let ((mm (current-buffer))
771 (non-viewer (assq 'non-viewer
773 (mm-handle-media-type handle) t))))
777 (mm-save-part handle))
778 (when (and (not non-viewer)
780 (mm-handle-set-undisplayer handle mm)))))
781 ;; The function is a string to be executed.
782 (mm-insert-part handle)
783 (mm-add-meta-html-tag handle)
784 (let* ((dir (mm-make-temp-file
785 (expand-file-name "emm." mm-tmp-directory) 'dir))
787 (mail-content-type-get
788 (mm-handle-disposition handle) 'filename)
789 (mail-content-type-get
790 (mm-handle-type handle) 'name)))
791 (mime-info (mailcap-mime-info
792 (mm-handle-media-type handle) t))
793 (needsterm (or (assoc "needsterm" mime-info)
794 (assoc "needsterminal" mime-info)))
795 (copiousoutput (assoc "copiousoutput" mime-info))
797 ;; We create a private sub-directory where we store our files.
798 (set-file-modes dir #o700)
800 (setq file (expand-file-name
801 (gnus-map-function mm-file-name-rewrite-functions
802 (file-name-nondirectory filename))
804 ;; Use nametemplate (defined in RFC1524) if it is specified
806 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
808 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
809 (setq suffix (match-string 1 suffix))
810 ;; Otherwise, use a suffix according to
811 ;; `mailcap-mime-extensions'.
812 (setq suffix (car (rassoc (mm-handle-media-type handle)
813 mailcap-mime-extensions))))
814 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
816 (let ((coding-system-for-write mm-binary-coding-system))
817 (write-region (point-min) (point-max) file nil 'nomesg))
818 ;; The file is deleted after the viewer exists. If the users edits
819 ;; the file, changes will be lost. Set file to read-only to make it
821 (set-file-modes file #o400)
822 (message "Viewing with %s" method)
825 (let ((command (mm-mailcap-command
826 method file (mm-handle-type handle))))
829 (start-process "*display*" nil
830 mm-external-terminal-program
832 shell-command-switch command)
840 shell-command-switch command)))
843 (set-process-sentinel
844 (get-buffer-process buffer)
845 `(lambda (process state)
846 (if (eq 'exit (process-status process))
847 (gnus-configure-windows
848 ',gnus-current-window-configuration))))
849 (gnus-configure-windows 'display-term))
850 (mm-handle-set-external-undisplayer handle (cons file buffer)))
851 (message "Displaying %s..." command))
854 (with-current-buffer outbuf
860 (call-process shell-file-name nil
862 (generate-new-buffer " *mm*"))
866 method file (mm-handle-type handle)))
867 (if (buffer-live-p buffer)
868 (with-current-buffer buffer
871 (ignore-errors (delete-file file))
872 (ignore-errors (delete-directory
873 (file-name-directory file)))
874 (ignore-errors (kill-buffer buffer))))))
877 ;; Deleting the temp file should be postponed for some wrappers,
878 ;; shell scripts, and so on, which might exit right after having
879 ;; started a viewer command as a background job.
880 (let ((command (mm-mailcap-command
881 method file (mm-handle-type handle))))
884 (start-process "*display*"
886 (generate-new-buffer " *mm*"))
888 shell-command-switch command)
889 (set-process-sentinel
890 (get-buffer-process buffer)
891 (lexical-let ;; Don't use `let'.
892 ;; Function used to remove temp file and directory.
894 ;; Don't use `ignore-errors'.
900 ,(file-name-directory file))
902 ;; Form uses to kill the process buffer and
903 ;; remove the undisplayer.
905 (kill-buffer ,buffer)
907 (list 'mm-handle-set-undisplayer
910 ;; Message to be issued when the process exits.
911 (done (format "Displaying %s...done" command))
912 ;; In particular, the timer object (which is
913 ;; a vector in Emacs but is a list in XEmacs)
914 ;; requires that it is lexically scoped.
915 (timer (run-at-time 2.0 nil 'ignore)))
916 (if (featurep 'xemacs)
917 (lambda (process state)
918 (when (eq 'exit (process-status process))
919 (if (memq timer itimer-list)
920 (set-itimer-function timer fn)
922 (ignore-errors (eval fm))
923 (message "%s" done)))
924 (lambda (process state)
925 (when (eq 'exit (process-status process))
926 (if (memq timer timer-list)
927 (timer-set-function timer fn)
929 (ignore-errors (eval fm))
930 (message "%s" done)))))))
931 (mm-handle-set-external-undisplayer
932 handle (cons file buffer)))
933 (message "Displaying %s..." command))
936 (defun mm-mailcap-command (method file type-list)
937 (let ((ctl (cdr type-list))
941 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
943 (push (substring method beg (match-beginning 0)) out)
944 (setq beg (match-end 0)
945 total (match-string 0 method)
946 sub (match-string 1 method))
948 ((string= total "%%")
950 ((or (string= total "%s")
951 ;; We do our own quoting.
952 (string= total "'%s'")
953 (string= total "\"%s\""))
954 (setq uses-stdin nil)
955 (push (shell-quote-argument
956 (gnus-map-function mm-path-name-rewrite-functions file)) out))
957 ((string= total "%t")
958 (push (shell-quote-argument (car type-list)) out))
960 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
961 (push (substring method beg (length method)) out)
964 (push (shell-quote-argument
965 (gnus-map-function mm-path-name-rewrite-functions file))
967 (mapconcat 'identity (nreverse out) "")))
969 (defun mm-remove-parts (handles)
970 "Remove the displayed MIME parts represented by HANDLES."
971 (if (and (listp handles)
972 (bufferp (car handles)))
973 (mm-remove-part handles)
975 (while (setq handle (pop handles))
978 (when (buffer-live-p (get-text-property 0 'buffer handle))
979 (kill-buffer (get-text-property 0 'buffer handle))))
981 (stringp (car handle)))
982 (mm-remove-parts (cdr handle)))
984 (mm-remove-part handle)))))))
986 (defun mm-destroy-parts (handles)
987 "Remove the displayed MIME parts represented by HANDLES."
988 (if (and (listp handles)
989 (bufferp (car handles)))
990 (mm-destroy-part handles)
992 (while (setq handle (pop handles))
995 (when (buffer-live-p (get-text-property 0 'buffer handle))
996 (kill-buffer (get-text-property 0 'buffer handle))))
998 (stringp (car handle)))
999 (mm-destroy-parts handle))
1001 (mm-destroy-part handle)))))))
1003 (defun mm-remove-part (handle)
1004 "Remove the displayed MIME part represented by HANDLE."
1005 (when (listp handle)
1006 (let ((object (mm-handle-undisplayer handle)))
1009 ;; Internally displayed part.
1010 ((mm-annotationp object)
1011 (if (featurep 'xemacs)
1012 (delete-annotation object)))
1013 ((or (functionp object)
1015 (eq (car object) 'lambda)))
1017 ;; Externally displayed part.
1020 (while (get-buffer-process (cdr object))
1021 (interrupt-process (get-buffer-process (cdr object)))
1022 (message "Waiting for external displayer to die...")
1026 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1027 (message "Waiting for external displayer to die...done")
1028 (ignore-errors (delete-file (car object)))
1029 (ignore-errors (delete-directory (file-name-directory
1032 (when (buffer-live-p object)
1033 (kill-buffer object)))))
1034 (mm-handle-set-undisplayer handle nil))))
1036 (defun mm-display-inline (handle)
1037 (let* ((type (mm-handle-media-type handle))
1038 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1039 (funcall function handle)
1040 (goto-char (point-min))))
1042 (defun mm-assoc-string-match (alist type)
1043 (dolist (elem alist)
1044 (when (string-match (car elem) type)
1047 (defun mm-automatic-display-p (handle)
1048 "Say whether the user wants HANDLE to be displayed automatically."
1049 (let ((methods mm-automatic-display)
1050 (type (mm-handle-media-type handle))
1052 (while (setq method (pop methods))
1053 (when (and (not (mm-inline-override-p handle))
1054 (string-match method type))
1059 (defun mm-inlinable-p (handle &optional type)
1060 "Say whether HANDLE can be displayed inline.
1061 TYPE is the mime-type of the object; it defaults to the one given
1063 (unless type (setq type (mm-handle-media-type handle)))
1064 (let ((alist mm-inline-media-tests)
1067 (when (string-match (caar alist) type)
1068 (setq test (caddar alist)
1070 (setq test (funcall test handle)))
1074 (defun mm-inlined-p (handle)
1075 "Say whether the user wants HANDLE to be displayed inline."
1076 (let ((methods mm-inlined-types)
1077 (type (mm-handle-media-type handle))
1079 (while (setq method (pop methods))
1080 (when (and (not (mm-inline-override-p handle))
1081 (string-match method type))
1086 (defun mm-attachment-override-p (handle)
1087 "Say whether HANDLE should have attachment behavior overridden."
1088 (let ((types mm-attachment-override-types)
1089 (type (mm-handle-media-type handle))
1092 (while (setq ty (pop types))
1093 (when (and (string-match ty type)
1094 (mm-inlinable-p handle))
1095 (throw 'found t))))))
1097 (defun mm-inline-override-p (handle)
1098 "Say whether HANDLE should have inline behavior overridden."
1099 (let ((types mm-inline-override-types)
1100 (type (mm-handle-media-type handle))
1103 (while (setq ty (pop types))
1104 (when (string-match ty type)
1105 (throw 'found t))))))
1107 (defun mm-automatic-external-display-p (type)
1108 "Return the user-defined method for TYPE."
1109 (let ((methods mm-automatic-external-display)
1111 (while (setq method (pop methods))
1112 (when (string-match method type)
1117 (defun mm-destroy-part (handle)
1118 "Destroy the data structures connected to HANDLE."
1119 (when (listp handle)
1120 (mm-remove-part handle)
1121 (when (buffer-live-p (mm-handle-buffer handle))
1122 (kill-buffer (mm-handle-buffer handle)))))
1124 (defun mm-handle-displayed-p (handle)
1125 "Say whether HANDLE is displayed or not."
1126 (mm-handle-undisplayer handle))
1129 ;;; Functions for outputting parts
1132 (defmacro mm-with-part (handle &rest forms)
1133 "Run FORMS in the temp buffer containing the contents of HANDLE."
1134 `(let* ((handle ,handle)
1135 ;; The multibyteness of the temp buffer should be turned on
1136 ;; if inserting a multibyte string. Contrarily, the buffer's
1137 ;; multibyteness should be off if inserting a unibyte string,
1138 ;; especially if a string contains 8bit data.
1139 (default-enable-multibyte-characters
1140 (with-current-buffer (mm-handle-buffer handle)
1143 (insert-buffer-substring (mm-handle-buffer handle))
1144 (mm-disable-multibyte)
1145 (mm-decode-content-transfer-encoding
1146 (mm-handle-encoding handle)
1147 (mm-handle-media-type handle))
1149 (put 'mm-with-part 'lisp-indent-function 1)
1150 (put 'mm-with-part 'edebug-form-spec '(body))
1152 (defun mm-get-part (handle &optional no-cache)
1153 "Return the contents of HANDLE as a string.
1154 If NO-CACHE is non-nil, cached contents of a message/external-body part
1156 (if (and (not no-cache)
1157 (equal (mm-handle-media-type handle) "message/external-body"))
1159 (unless (mm-handle-cache handle)
1160 (mm-extern-cache-contents handle))
1161 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1163 (mm-with-part handle
1166 (defun mm-insert-part (handle &optional no-cache)
1167 "Insert the contents of HANDLE in the current buffer.
1168 If NO-CACHE is non-nil, cached contents of a message/external-body part
1170 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1173 (with-current-buffer (mm-handle-buffer handle)
1176 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1178 (mm-get-part handle no-cache)))))
1184 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1186 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1187 ;; Separate the extracted parts that have the same faces.
1191 (defun mm-file-name-delete-whitespace (file-name)
1192 "Remove all whitespace characters from FILE-NAME."
1193 (while (string-match "\\s-+" file-name)
1194 (setq file-name (replace-match "" t t file-name)))
1197 (defun mm-file-name-trim-whitespace (file-name)
1198 "Remove leading and trailing whitespace characters from FILE-NAME."
1199 (when (string-match "\\`\\s-+" file-name)
1200 (setq file-name (substring file-name (match-end 0))))
1201 (when (string-match "\\s-+\\'" file-name)
1202 (setq file-name (substring file-name 0 (match-beginning 0))))
1205 (defun mm-file-name-collapse-whitespace (file-name)
1206 "Collapse multiple whitespace characters in FILE-NAME."
1207 (while (string-match "\\s-\\s-+" file-name)
1208 (setq file-name (replace-match " " t t file-name)))
1211 (defun mm-file-name-replace-whitespace (file-name)
1212 "Replace whitespace characters in FILE-NAME with underscores.
1213 Set the option `mm-file-name-replace-whitespace' to any other
1214 string if you do not like underscores."
1215 (let ((s (or mm-file-name-replace-whitespace "_")))
1216 (while (string-match "\\s-" file-name)
1217 (setq file-name (replace-match s t t file-name))))
1220 (defun mm-file-name-delete-control (filename)
1221 "Delete control characters from FILENAME."
1222 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1224 (defun mm-file-name-delete-gotchas (filename)
1225 "Delete shell gotchas from FILENAME."
1226 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1227 (gnus-replace-in-string filename "^[.-]+" ""))
1229 (defun mm-save-part (handle &optional prompt)
1230 "Write HANDLE to a file.
1231 PROMPT overrides the default one used to ask user for a file name."
1232 (let ((filename (or (mail-content-type-get
1233 (mm-handle-disposition handle) 'filename)
1234 (mail-content-type-get
1235 (mm-handle-type handle) 'name)))
1238 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1239 (file-name-nondirectory filename))))
1242 (read-file-name (or prompt "Save MIME part to: ")
1243 (or mm-default-directory default-directory)
1244 nil nil (or filename ""))))
1245 (setq mm-default-directory (file-name-directory file))
1246 (and (or (not (file-exists-p file))
1247 (yes-or-no-p (format "File %s already exists; overwrite? "
1250 (mm-save-part-to-file handle file)
1253 (defun mm-add-meta-html-tag (handle &optional charset)
1254 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1255 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1256 specifies charset will not be modified. Return t if meta tag is added
1258 (when (equal (mm-handle-media-type handle) "text/html")
1260 (setq charset (mail-content-type-get (mm-handle-type handle)
1262 (setq charset (format "\
1263 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1264 (let ((case-fold-search t))
1265 (goto-char (point-min))
1266 (if (re-search-forward "\
1267 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1268 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+?\\)\\)?[\"'][^>]*>" nil t)
1269 (if (and (match-beginning 2)
1270 (string-match "\\`html\\'" (match-string 1)))
1271 ;; Don't modify existing meta tag.
1273 ;; Replace it with the one specifying charset.
1274 (replace-match charset)
1276 (if (re-search-forward "<head>\\s-*" nil t)
1277 (insert charset "\n")
1278 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1279 (insert "<head>\n" charset "\n</head>\n"))
1282 (defun mm-save-part-to-file (handle file)
1283 (mm-with-unibyte-buffer
1284 (mm-insert-part handle)
1285 (mm-add-meta-html-tag handle)
1286 (let ((current-file-modes (default-file-modes)))
1287 (set-default-file-modes mm-attachment-file-modes)
1289 ;; Don't re-compress .gz & al. Arguably we should make
1290 ;; `file-name-handler-alist' nil, but that would chop
1291 ;; ange-ftp, which is reasonable to use here.
1292 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1293 (set-default-file-modes current-file-modes)))))
1295 (defun mm-pipe-part (handle)
1296 "Pipe HANDLE to a process."
1297 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1299 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1300 (mm-with-unibyte-buffer
1301 (mm-insert-part handle)
1302 (mm-add-meta-html-tag handle)
1303 (let ((coding-system-for-write 'binary))
1304 (shell-command-on-region (point-min) (point-max) command nil)))))
1306 (defun mm-interactively-view-part (handle)
1307 "Display HANDLE using METHOD."
1308 (let* ((type (mm-handle-media-type handle))
1310 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1311 (mailcap-mime-info type 'all)))
1312 (method (let ((minibuffer-local-completion-map
1313 mm-viewer-completion-map))
1314 (completing-read "Viewer: " methods))))
1315 (when (string= method "")
1316 (error "No method given"))
1317 (if (string-match "^[^% \t]+$" method)
1318 (setq method (concat method " %s")))
1319 (mm-display-external handle method)))
1321 (defun mm-preferred-alternative (handles &optional preferred)
1322 "Say which of HANDLES are preferred."
1323 (let ((prec (if preferred (list preferred)
1324 (mm-preferred-alternative-precedence handles)))
1325 p h result type handle)
1326 (while (setq p (pop prec))
1329 (setq handle (car h))
1330 (setq type (mm-handle-media-type handle))
1331 (when (and (equal p type)
1332 (mm-automatic-display-p handle)
1333 (or (stringp (car handle))
1334 (not (mm-handle-disposition handle))
1335 (equal (car (mm-handle-disposition handle))
1343 (defun mm-preferred-alternative-precedence (handles)
1344 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1345 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1347 (dolist (disc (reverse mm-discouraged-alternatives))
1348 (dolist (elem (copy-sequence seq))
1349 (when (string-match disc elem)
1350 (setq seq (nconc (delete elem seq) (list elem))))))
1353 (defun mm-get-content-id (id)
1354 "Return the handle(s) referred to by ID."
1355 (cdr (assoc id mm-content-id-alist)))
1357 (defconst mm-image-type-regexps
1358 '(("/\\*.*XPM.\\*/" . xpm)
1362 ("\211PNG\r\n" . png)
1364 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1365 ("%!PS" . postscript))
1366 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1367 When the first bytes of an image file match REGEXP, it is assumed to
1368 be of image type IMAGE-TYPE.")
1370 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1371 (defun mm-image-type-from-buffer ()
1372 "Determine the image type from data in the current buffer.
1373 Value is a symbol specifying the image type or nil if type cannot
1375 (let ((types mm-image-type-regexps)
1377 (goto-char (point-min))
1378 (while (and types (null type))
1379 (let ((regexp (car (car types)))
1380 (image-type (cdr (car types))))
1381 (when (looking-at regexp)
1382 (setq type image-type))
1383 (setq types (cdr types))))
1386 (defun mm-get-image (handle)
1387 "Return an image instance based on HANDLE."
1388 (let ((type (mm-handle-media-subtype handle))
1390 ;; Allow some common translations.
1393 ((equal type "x-pixmap")
1395 ((equal type "x-xbitmap")
1397 ((equal type "x-portable-bitmap")
1400 (or (mm-handle-cache handle)
1401 (mm-with-unibyte-buffer
1402 (mm-insert-part handle)
1406 ;; Avoid testing `make-glyph' since W3 may define
1407 ;; a bogus version of it.
1408 (if (fboundp 'create-image)
1409 (create-image (buffer-string)
1410 (or (mm-image-type-from-buffer)
1413 (mm-create-image-xemacs type))))
1414 (mm-handle-set-cache handle spec))))))
1416 (defun mm-create-image-xemacs (type)
1417 (when (featurep 'xemacs)
1420 ;; xbm images require special handling, since
1421 ;; the only way to create glyphs from these
1422 ;; (without a ton of work) is to write them
1423 ;; out to a file, and then create a file
1425 (let ((file (mm-make-temp-file
1426 (expand-file-name "emm" mm-tmp-directory)
1430 (write-region (point-min) (point-max) file)
1431 (make-glyph (list (cons 'x file))))
1433 (delete-file file)))))
1437 (or (mm-image-type-from-buffer)
1439 :data (buffer-string)))))))
1441 (defun mm-image-fit-p (handle)
1442 "Say whether the image in HANDLE will fit the current window."
1443 (let ((image (mm-get-image handle)))
1445 (if (featurep 'xemacs)
1446 ;; XEmacs' glyphs can actually tell us about their width, so
1447 ;; lets be nice and smart about them.
1448 (or mm-inline-large-images
1449 (and (<= (glyph-width image) (window-pixel-width))
1450 (<= (glyph-height image) (window-pixel-height))))
1451 (let* ((size (image-size image))
1454 (or mm-inline-large-images
1455 (and (<= h (1- (window-height))) ; Don't include mode line.
1456 (<= w (window-width)))))))))
1458 (defun mm-valid-image-format-p (format)
1459 "Say whether FORMAT can be displayed natively by Emacs."
1462 ((fboundp 'valid-image-instantiator-format-p)
1463 (valid-image-instantiator-format-p format))
1465 ((fboundp 'image-type-available-p)
1466 (and (display-graphic-p)
1467 (image-type-available-p format)))
1468 ;; Nobody else can do images yet.
1472 (defun mm-valid-and-fit-image-p (format handle)
1473 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1474 (and (mm-valid-image-format-p format)
1475 (mm-image-fit-p handle)))
1477 (defun mm-find-part-by-type (handles type &optional notp recursive)
1478 "Search in HANDLES for part with TYPE.
1479 If NOTP, returns first non-matching part.
1480 If RECURSIVE, search recursively."
1483 (if (and recursive (stringp (caar handles)))
1484 (if (setq handle (mm-find-part-by-type (cdar handles) type
1488 (not (equal (mm-handle-media-type (car handles)) type))
1489 (equal (mm-handle-media-type (car handles)) type))
1490 (setq handle (car handles)
1492 (setq handles (cdr handles)))
1495 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1496 (goto-char (point-min))
1497 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1499 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1501 (end (save-excursion
1502 (goto-char (point-max))
1503 (if (re-search-backward close-delimiter nil t)
1507 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1508 (while (and (not result)
1509 (re-search-forward boundary end t))
1510 (goto-char (match-beginning 0))
1514 (narrow-to-region start (1- (point)))
1515 (when (let* ((ct (mail-fetch-field "content-type"))
1516 (ctl (and ct (mail-header-parse-content-type ct))))
1518 (not (equal (car ctl) type))
1519 (equal (car ctl) type)))
1520 (setq result (buffer-string))))))
1522 (setq start (point)))
1523 (when (and (not result) start)
1526 (narrow-to-region start end)
1527 (when (let* ((ct (mail-fetch-field "content-type"))
1528 (ctl (and ct (mail-header-parse-content-type ct))))
1530 (not (equal (car ctl) type))
1531 (equal (car ctl) type)))
1532 (setq result (buffer-string))))))
1535 (defvar mm-security-handle nil)
1537 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1538 ;; HANDLE could be a CTL.
1540 (put-text-property 0 (length (car handle)) parameter value
1543 (autoload 'mm-view-pkcs7 "mm-view")
1545 (defun mm-possibly-verify-or-decrypt (parts ctl)
1546 (let ((type (car ctl))
1547 (subtype (cadr (split-string (car ctl) "/")))
1548 (mm-security-handle ctl) ;; (car CTL) is the type.
1549 protocol func functest)
1551 ((or (equal type "application/x-pkcs7-mime")
1552 (equal type "application/pkcs7-mime"))
1555 ((eq mm-decrypt-option 'never) nil)
1556 ((eq mm-decrypt-option 'always) t)
1557 ((eq mm-decrypt-option 'known) t)
1559 (format "Decrypt (S/MIME) part? "))))
1560 (mm-view-pkcs7 parts))
1561 (setq parts (mm-dissect-buffer t)))))
1562 ((equal subtype "signed")
1563 (unless (and (setq protocol
1564 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1565 (not (equal protocol "multipart/mixed")))
1566 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1567 (let ((protocols mm-verify-function-alist))
1569 (if (and (or (not (setq functest (nth 3 (car protocols))))
1570 (funcall functest parts ctl))
1571 (mm-find-part-by-type parts (caar protocols) nil t))
1572 (setq protocol (caar protocols)
1574 (setq protocols (cdr protocols))))))
1575 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1577 ((eq mm-verify-option 'never) nil)
1578 ((eq mm-verify-option 'always) t)
1579 ((eq mm-verify-option 'known)
1581 (or (not (setq functest
1582 (nth 3 (assoc protocol
1583 mm-verify-function-alist))))
1584 (funcall functest parts ctl))))
1587 (format "Verify signed (%s) part? "
1588 (or (nth 2 (assoc protocol mm-verify-function-alist))
1589 (format "protocol=%s" protocol))))))
1592 (setq parts (funcall func parts ctl))
1593 (mm-set-handle-multipart-parameter
1594 mm-security-handle 'gnus-details
1595 (format "Unknown sign protocol (%s)" protocol))))))
1596 ((equal subtype "encrypted")
1597 (unless (setq protocol
1598 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1599 ;; The message is broken.
1600 (let ((parts parts))
1602 (if (assoc (mm-handle-media-type (car parts))
1603 mm-decrypt-function-alist)
1604 (setq protocol (mm-handle-media-type (car parts))
1606 (setq parts (cdr parts))))))
1607 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1609 ((eq mm-decrypt-option 'never) nil)
1610 ((eq mm-decrypt-option 'always) t)
1611 ((eq mm-decrypt-option 'known)
1613 (or (not (setq functest
1614 (nth 3 (assoc protocol
1615 mm-decrypt-function-alist))))
1616 (funcall functest parts ctl))))
1619 (format "Decrypt (%s) part? "
1620 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1621 (format "protocol=%s" protocol))))))
1624 (setq parts (funcall func parts ctl))
1625 (mm-set-handle-multipart-parameter
1626 mm-security-handle 'gnus-details
1627 (format "Unknown encrypt protocol (%s)" protocol))))))
1631 (defun mm-multiple-handles (handles)
1632 (and (listp handles)
1633 (> (length handles) 1)
1634 (or (listp (car handles))
1635 (stringp (car handles)))))
1637 (defun mm-complicated-handles (handles)
1638 (and (listp (car handles))
1639 (> (length handles) 1)))
1641 (defun mm-merge-handles (handles1 handles2)
1643 (if (listp (car handles1))
1646 (if (listp (car handles2))
1650 (defun mm-readable-p (handle)
1651 "Say whether the content of HANDLE is readable."
1652 (and (< (with-current-buffer (mm-handle-buffer handle)
1653 (buffer-size)) 10000)
1654 (mm-with-unibyte-buffer
1655 (mm-insert-part handle)
1656 (and (eq (mm-body-7-or-8) '7bit)
1657 (not (mm-long-lines-p 76))))))
1659 (provide 'mm-decode)
1661 ;; arch-tag: 4f35d360-56b8-4030-9388-3ed82d359b9b
1662 ;;; mm-decode.el ends here