1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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.
32 (eval-when-compile (require 'cl
)
36 (autoload 'mm-inline-partial
"mm-partial")
37 (autoload 'mm-inline-external-body
"mm-extern")
38 (autoload 'mm-extern-cache-contents
"mm-extern")
39 (autoload 'mm-insert-inline
"mm-view"))
41 (defvar gnus-current-window-configuration
)
43 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
45 (defgroup mime-display
()
46 "Display of MIME in mail and news articles."
47 :link
'(custom-manual "(emacs-mime)Display Customization")
53 (defgroup mime-security
()
54 "MIME security in mail and news articles."
55 :link
'(custom-manual "(emacs-mime)Display Customization")
60 ;;; Convenience macros.
62 (defmacro mm-handle-buffer
(handle)
64 (defmacro mm-handle-type
(handle)
66 (defsubst mm-handle-media-type
(handle)
67 (if (stringp (car handle
))
69 (car (mm-handle-type handle
))))
70 (defsubst mm-handle-media-supertype
(handle)
71 (car (split-string (mm-handle-media-type handle
) "/")))
72 (defsubst mm-handle-media-subtype
(handle)
73 (cadr (split-string (mm-handle-media-type handle
) "/")))
74 (defmacro mm-handle-encoding
(handle)
76 (defmacro mm-handle-undisplayer
(handle)
78 (defmacro mm-handle-set-undisplayer
(handle function
)
79 `(setcar (nthcdr 3 ,handle
) ,function
))
80 (defmacro mm-handle-disposition
(handle)
82 (defmacro mm-handle-description
(handle)
84 (defmacro mm-handle-cache
(handle)
86 (defmacro mm-handle-set-cache
(handle contents
)
87 `(setcar (nthcdr 6 ,handle
) ,contents
))
88 (defmacro mm-handle-id
(handle)
90 (defmacro mm-handle-multipart-original-buffer
(handle)
91 `(get-text-property 0 'buffer
(car ,handle
)))
92 (defmacro mm-handle-multipart-from
(handle)
93 `(get-text-property 0 'from
(car ,handle
)))
94 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
95 `(get-text-property 0 ,parameter
(car ,handle
)))
97 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
98 disposition description cache
100 `(list ,buffer
,type
,encoding
,undisplayer
101 ,disposition
,description
,cache
,id
))
103 (defcustom mm-text-html-renderer
104 (cond ((locate-library "w3") 'w3
)
105 ((executable-find "w3m") (if (locate-library "w3m")
108 ((executable-find "links") 'links
)
109 ((executable-find "lynx") 'lynx
)
111 "Render of HTML contents.
112 It is one of defined renderer types, or a rendering function.
113 The defined renderer types are:
115 `w3m' : use emacs-w3m;
116 `w3m-standalone': use w3m;
119 `html2text' : use html2text;
120 nil : use external viewer."
122 :type
'(choice (const w3
)
124 (const w3m-standalone
)
130 :group
'mime-display
)
132 (defvar mm-inline-text-html-renderer nil
133 "Function used for rendering inline HTML contents.
134 It is suggested to customize `mm-text-html-renderer' instead.")
136 (defcustom mm-inline-text-html-with-images nil
137 "If non-nil, Gnus will allow retrieving images in HTML contents with
138 the <img> tags. It has no effect on Emacs/w3. See also the
139 documentation for the `mm-w3m-safe-url-regexp' variable."
142 :group
'mime-display
)
144 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
145 "Regexp matching URLs which are considered to be safe.
146 Some HTML mails might contain a nasty trick used by spammers, using
147 the <img> tag which is far more evil than the [Click Here!] button.
148 It is most likely intended to check whether the ominous spam mail has
149 reached your eyes or not, in which case the spammer knows for sure
150 that your email address is valid. It is done by embedding an
151 identifier string into a URL that you might automatically retrieve
152 when displaying the image. The default value is \"\\\\`cid:\" which only
153 matches parts embedded to the Multipart/Related type MIME contents and
154 Gnus will never connect to the spammer's site arbitrarily. You may
155 set this variable to nil if you consider all urls to be safe."
157 :type
'(choice (regexp :tag
"Regexp")
158 (const :tag
"All URLs are safe" nil
))
159 :group
'mime-display
)
161 (defcustom mm-inline-text-html-with-w3m-keymap t
162 "If non-nil, use emacs-w3m command keys in the article buffer."
165 :group
'mime-display
)
167 (defcustom mm-enable-external t
168 "Indicate whether external MIME handlers should be used.
170 If t, all defined external MIME handlers are used. If nil, files are saved by
171 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
172 before the external MIME handler is invoked."
174 :type
'(choice (const :tag
"Always" t
)
175 (const :tag
"Never" nil
)
176 (const :tag
"Ask" ask
))
177 :group
'mime-display
)
179 (defcustom mm-inline-media-tests
183 (mm-valid-and-fit-image-p 'jpeg handle
)))
187 (mm-valid-and-fit-image-p 'png handle
)))
191 (mm-valid-and-fit-image-p 'gif handle
)))
195 (mm-valid-and-fit-image-p 'tiff handle
)) )
199 (mm-valid-and-fit-image-p 'xbm handle
)))
203 (mm-valid-and-fit-image-p 'xbm handle
)))
207 (mm-valid-and-fit-image-p 'xpm handle
)))
211 (mm-valid-and-fit-image-p 'xpm handle
)))
215 (mm-valid-and-fit-image-p 'bmp handle
)))
216 ("image/x-portable-bitmap"
219 (mm-valid-and-fit-image-p 'pbm handle
)))
220 ("text/plain" mm-inline-text identity
)
221 ("text/enriched" mm-inline-text identity
)
222 ("text/richtext" mm-inline-text identity
)
223 ("text/x-patch" mm-display-patch-inline
225 ;; If the diff-mode.el package is installed, the function is
226 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
227 ;; to cater to broken installations. OTOH checking the function
228 ;; makes it possible to install another package which provides an
229 ;; alternative implementation of diff-mode. --Stef
230 (fboundp 'diff-mode
)))
231 ("application/emacs-lisp" mm-display-elisp-inline identity
)
232 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
233 ("text/dns" mm-display-dns-inline identity
)
237 (or mm-inline-text-html-renderer
238 mm-text-html-renderer
)))
242 (or (featurep 'vcard
)
243 (locate-library "vcard"))))
244 ("message/delivery-status" mm-inline-text identity
)
245 ("message/rfc822" mm-inline-message identity
)
246 ("message/partial" mm-inline-partial identity
)
247 ("message/external-body" mm-inline-external-body identity
)
248 ("text/.*" mm-inline-text identity
)
249 ("audio/wav" mm-inline-audio
251 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
252 (device-sound-enabled-p))))
256 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
257 (device-sound-enabled-p))))
258 ("application/pgp-signature" ignore identity
)
259 ("application/x-pkcs7-signature" ignore identity
)
260 ("application/pkcs7-signature" ignore identity
)
261 ("application/x-pkcs7-mime" ignore identity
)
262 ("application/pkcs7-mime" ignore identity
)
263 ("multipart/alternative" ignore identity
)
264 ("multipart/mixed" ignore identity
)
265 ("multipart/related" ignore identity
)
266 ;; Disable audio and image
267 ("audio/.*" ignore ignore
)
268 ("image/.*" ignore ignore
)
269 ;; Default to displaying as text
270 (".*" mm-inline-text mm-readable-p
))
271 "Alist of media types/tests saying whether types can be displayed inline."
272 :type
'(repeat (list (regexp :tag
"MIME type")
273 (function :tag
"Display function")
274 (function :tag
"Display test")))
275 :group
'mime-display
)
277 (defcustom mm-inlined-types
278 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
279 "message/partial" "message/external-body" "application/emacs-lisp"
280 "application/x-emacs-lisp"
281 "application/pgp-signature" "application/x-pkcs7-signature"
282 "application/pkcs7-signature" "application/x-pkcs7-mime"
283 "application/pkcs7-mime"
284 ;; Mutt still uses this even though it has already been withdrawn.
286 "List of media types that are to be displayed inline.
287 See also `mm-inline-media-tests', which says how to display a media
289 :type
'(repeat regexp
)
290 :group
'mime-display
)
292 (defcustom mm-keep-viewer-alive-types
293 '("application/postscript" "application/msword" "application/vnd.ms-excel"
294 "application/pdf" "application/x-dvi")
295 "List of media types for which the external viewer will not be killed
296 when selecting a different article."
298 :type
'(repeat regexp
)
299 :group
'mime-display
)
301 (defcustom mm-automatic-display
302 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
303 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
304 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
305 "application/emacs-lisp" "application/x-emacs-lisp"
306 "application/x-pkcs7-signature"
307 "application/pkcs7-signature" "application/x-pkcs7-mime"
308 "application/pkcs7-mime"
309 ;; Mutt still uses this even though it has already been withdrawn.
310 "application/pgp\\'")
311 "A list of MIME types to be displayed automatically."
312 :type
'(repeat regexp
)
313 :group
'mime-display
)
315 (defcustom mm-attachment-override-types
'("text/x-vcard"
316 "application/pkcs7-mime"
317 "application/x-pkcs7-mime"
318 "application/pkcs7-signature"
319 "application/x-pkcs7-signature")
320 "Types to have \"attachment\" ignored if they can be displayed inline."
321 :type
'(repeat regexp
)
322 :group
'mime-display
)
324 (defcustom mm-inline-override-types nil
325 "Types to be treated as attachments even if they can be displayed inline."
326 :type
'(repeat regexp
)
327 :group
'mime-display
)
329 (defcustom mm-automatic-external-display nil
330 "List of MIME type regexps that will be displayed externally automatically."
331 :type
'(repeat regexp
)
332 :group
'mime-display
)
334 (defcustom mm-discouraged-alternatives nil
335 "List of MIME types that are discouraged when viewing multipart/alternative.
336 Viewing agents are supposed to view the last possible part of a message,
337 as that is supposed to be the richest. However, users may prefer other
338 types instead, and this list says what types are most unwanted. If,
339 for instance, text/html parts are very unwanted, and text/richtext are
340 somewhat unwanted, then the value of this variable should be set
343 (\"text/html\" \"text/richtext\")
345 Adding \"image/.*\" might also be useful. Spammers use it as the
346 prefered part of multipart/alternative messages. See also
347 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
348 enables you to choose manually one of two types those mails include."
349 :type
'(repeat regexp
) ;; See `mm-preferred-alternative-precedence'.
350 :group
'mime-display
)
352 (defcustom mm-tmp-directory
353 (if (fboundp 'temp-directory
)
355 (if (boundp 'temporary-file-directory
)
356 temporary-file-directory
358 "Where mm will store its temporary files."
360 :group
'mime-display
)
362 (defcustom mm-inline-large-images nil
363 "If non-nil, then all images fit in the buffer."
365 :group
'mime-display
)
367 (defcustom mm-file-name-rewrite-functions
368 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
369 "List of functions used for rewriting file names of MIME parts.
370 Each function takes a file name as input and returns a file name.
372 Ready-made functions include `mm-file-name-delete-control',
373 `mm-file-name-delete-gotchas' (you should not remove these two
374 functions), `mm-file-name-delete-whitespace',
375 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
376 `mm-file-name-replace-whitespace', `capitalize', `downcase',
377 `upcase', and `upcase-initials'."
378 :type
'(list (set :inline t
379 (const mm-file-name-delete-control
)
380 (const mm-file-name-delete-gotchas
)
381 (const mm-file-name-delete-whitespace
)
382 (const mm-file-name-trim-whitespace
)
383 (const mm-file-name-collapse-whitespace
)
384 (const mm-file-name-replace-whitespace
)
388 (const upcase-initials
)
392 :version
"23.0" ;; No Gnus
393 :group
'mime-display
)
396 (defvar mm-path-name-rewrite-functions nil
397 "*List of functions for rewriting the full file names of MIME parts.
398 This is used when viewing parts externally, and is meant for
399 transforming the absolute name so that non-compliant programs can find
400 the file where it's saved.
402 Each function takes a file name as input and returns a file name.")
404 (defvar mm-file-name-replace-whitespace nil
405 "String used for replacing whitespace characters; default is `\"_\"'.")
407 (defcustom mm-default-directory nil
408 "The default directory where mm will save files.
409 If not set, `default-directory' will be used."
410 :type
'(choice directory
(const :tag
"Default" nil
))
411 :group
'mime-display
)
413 (defcustom mm-attachment-file-modes
384
414 "Set the mode bits of saved attachments to this integer."
417 :group
'mime-display
)
419 (defcustom mm-external-terminal-program
"xterm"
420 "The program to start an external terminal."
423 :group
'mime-display
)
425 ;;; Internal variables.
427 (defvar mm-last-shell-command
"")
428 (defvar mm-content-id-alist nil
)
429 (defvar mm-postponed-undisplay-list nil
)
431 ;; According to RFC2046, in particular, in a digest, the default
432 ;; Content-Type value for a body part is changed from "text/plain" to
434 (defvar mm-dissect-default-type
"text/plain")
436 (autoload 'mml2015-verify
"mml2015")
437 (autoload 'mml2015-verify-test
"mml2015")
438 (autoload 'mml-smime-verify
"mml-smime")
439 (autoload 'mml-smime-verify-test
"mml-smime")
441 (defvar mm-verify-function-alist
442 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
443 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
444 mm-uu-pgp-signed-test
)
445 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
446 mml-smime-verify-test
)
447 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
448 mml-smime-verify-test
)))
450 (defcustom mm-verify-option
'never
451 "Option of verifying signed parts.
452 `never', not verify; `always', always verify;
453 `known', only verify known protocols. Otherwise, ask user.
455 When set to `always' or `known', you should add
456 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
457 result of the verification."
459 :type
'(choice (item always
)
461 (item :tag
"only known protocols" known
)
462 (item :tag
"ask" nil
))
463 :group
'mime-security
)
465 (autoload 'mml2015-decrypt
"mml2015")
466 (autoload 'mml2015-decrypt-test
"mml2015")
468 (defvar mm-decrypt-function-alist
469 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
470 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
471 mm-uu-pgp-encrypted-test
)))
473 (defcustom mm-decrypt-option nil
474 "Option of decrypting encrypted parts.
475 `never', not decrypt; `always', always decrypt;
476 `known', only decrypt known protocols. Otherwise, ask user."
478 :type
'(choice (item always
)
480 (item :tag
"only known protocols" known
)
481 (item :tag
"ask" nil
))
482 :group
'mime-security
)
484 (defvar mm-viewer-completion-map
485 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
486 (set-keymap-parent map minibuffer-local-completion-map
)
487 ;; Should we bind other key to minibuffer-complete-word?
488 (define-key map
" " 'self-insert-command
)
490 "Keymap for input viewer with completion.")
492 (defvar mm-viewer-completion-map
493 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
494 (set-keymap-parent map minibuffer-local-completion-map
)
495 ;; Should we bind other key to minibuffer-complete-word?
496 (define-key map
" " 'self-insert-command
)
498 "Keymap for input viewer with completion.")
502 (defun mm-alist-to-plist (alist)
503 "Convert association list ALIST into the equivalent property-list form.
504 The plist is returned. This converts from
506 \((a . 1) (b . 2) (c . 3))
512 The original alist is not modified. See also `destructive-alist-to-plist'."
515 (let ((el (car alist
)))
516 (setq plist
(cons (cdr el
) (cons (car el
) plist
))))
517 (setq alist
(cdr alist
)))
520 (defun mm-keep-viewer-alive-p (handle)
521 "Say whether external viewer for HANDLE should stay alive."
522 (let ((types mm-keep-viewer-alive-types
)
523 (type (mm-handle-media-type handle
))
526 (while (setq ty
(pop types
))
527 (when (string-match ty type
)
528 (throw 'found t
))))))
530 (defun mm-handle-set-external-undisplayer (handle function
)
531 "Set the undisplayer for HANDLE to FUNCTION.
532 Postpone undisplaying of viewers for types in
533 `mm-keep-viewer-alive-types'."
534 (if (mm-keep-viewer-alive-p handle
)
535 (let ((new-handle (copy-sequence handle
)))
536 (mm-handle-set-undisplayer new-handle function
)
537 (mm-handle-set-undisplayer handle nil
)
538 (push new-handle mm-postponed-undisplay-list
))
539 (mm-handle-set-undisplayer handle function
)))
541 (defun mm-destroy-postponed-undisplay-list ()
542 (when mm-postponed-undisplay-list
543 (message "Destroying external MIME viewers")
544 (mm-destroy-parts mm-postponed-undisplay-list
)))
546 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
547 "Dissect the current buffer and return a list of MIME handles."
549 (let (ct ctl type subtype cte cd description id result
)
551 (mail-narrow-to-head)
552 (when (or no-strict-mime
554 (mail-fetch-field "mime-version"))
555 (setq ct
(mail-fetch-field "content-type")
556 ctl
(and ct
(mail-header-parse-content-type ct
))
557 cte
(mail-fetch-field "content-transfer-encoding")
558 cd
(mail-fetch-field "content-disposition")
559 description
(mail-fetch-field "content-description")
560 id
(mail-fetch-field "content-id"))
562 (setq from
(mail-fetch-field "from")))
563 ;; FIXME: In some circumstances, this code is running within
564 ;; an unibyte macro. mail-extract-address-components
565 ;; creates unibyte buffers. This `if', though not a perfect
566 ;; solution, avoids most of them.
568 (setq from
(cadr (mail-extract-address-components from
))))))
570 (not (string-match "/" (car ctl
))))
571 (mm-dissect-singlepart
572 (list mm-dissect-default-type
)
573 (and cte
(intern (downcase (mail-header-strip cte
))))
575 (and cd
(mail-header-parse-content-disposition cd
))
577 (setq type
(split-string (car ctl
) "/"))
578 (setq subtype
(cadr type
)
583 ((equal type
"multipart")
584 (let ((mm-dissect-default-type (if (equal subtype
"digest")
587 (start (cdr (assq 'start
(cdr ctl
)))))
588 (add-text-properties 0 (length (car ctl
))
589 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
591 ;; what really needs to be done here is a way to link a
592 ;; MIME handle back to it's parent MIME handle (in a multilevel
593 ;; MIME article). That would probably require changing
594 ;; the mm-handle API so we simply store the multipart buffer
595 ;; name as a text property of the "multipart/whatever" string.
596 (add-text-properties 0 (length (car ctl
))
597 (list 'buffer
(mm-copy-to-buffer)
601 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
603 (mm-possibly-verify-or-decrypt
604 (mm-dissect-singlepart
606 (and cte
(intern (downcase (mail-header-strip cte
))))
608 (and cd
(mail-header-parse-content-disposition cd
))
612 (when (string-match " *<\\(.*\\)> *" id
)
613 (setq id
(match-string 1 id
)))
614 (push (cons id result
) mm-content-id-alist
))
617 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
619 (if (equal "text/plain" (car ctl
))
623 (mm-copy-to-buffer) ctl cte nil cdl description nil id
)))
625 (defun mm-dissect-multipart (ctl from
)
626 (goto-char (point-min))
627 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
628 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
631 (goto-char (point-max))
632 (if (re-search-backward close-delimiter nil t
)
635 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
636 (while (and (< (point) end
) (re-search-forward boundary end t
))
637 (goto-char (match-beginning 0))
641 (narrow-to-region start
(point))
642 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
644 (or (looking-at boundary
)
646 (setq start
(point)))
647 (when (and start
(< start end
))
650 (narrow-to-region start end
)
651 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
652 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl
)))
654 (defun mm-copy-to-buffer ()
655 "Copy the contents of the current buffer to a fresh buffer."
656 (let ((obuf (current-buffer))
658 (goto-char (point-min))
659 (search-forward-regexp "^\n" nil t
)
662 ;; Preserve the data's unibyteness (for url-insert-file-contents).
663 (let ((default-enable-multibyte-characters (mm-multibyte-p)))
664 (generate-new-buffer " *mm*"))
665 (insert-buffer-substring obuf beg
)
668 (defun mm-display-parts (handle &optional no-default
)
669 (if (stringp (car handle
))
670 (mapcar 'mm-display-parts
(cdr handle
))
671 (if (bufferp (car handle
))
673 (narrow-to-region (point) (point))
674 (mm-display-part handle
)
675 (goto-char (point-max)))
676 (mapcar 'mm-display-parts handle
))))
678 (defun mm-display-part (handle &optional no-default
)
679 "Display the MIME part represented by HANDLE.
680 Returns nil if the part is removed; inline if displayed inline;
681 external if displayed external."
683 (mailcap-parse-mailcaps)
684 (if (mm-handle-displayed-p handle
)
685 (mm-remove-part handle
)
686 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
687 "message/external-body")
689 (unless (mm-handle-cache handle
)
690 (mm-extern-cache-contents handle
))
691 (mm-handle-cache handle
))
693 (type (mm-handle-media-type ehandle
))
694 (method (mailcap-mime-info type
))
695 (filename (or (mail-content-type-get
696 (mm-handle-disposition handle
) 'filename
)
697 (mail-content-type-get
698 (mm-handle-type handle
) 'name
)
700 (external mm-enable-external
))
701 (if (and (mm-inlinable-p ehandle
)
702 (mm-inlined-p ehandle
))
705 (mm-display-inline handle
)
709 (if (and (not method
)
710 (equal "text" (car (split-string type
"/"))))
713 (mm-insert-inline handle
(mm-get-part handle
))
716 (and method
;; If nil, we always use "save".
717 (stringp method
) ;; 'mailcap-save-binary-file
718 (or (eq mm-enable-external t
)
719 (and (eq mm-enable-external
'ask
)
722 "Display part (" type
723 ") using external program"
724 ;; Can non-string method ever happen?
727 " \"" (format method filename
) "\"")
732 handle
(or method
'mailcap-save-binary-file
))
734 handle
'mailcap-save-binary-file
)))))))))
736 (defun mm-display-external (handle method
)
737 "Display HANDLE using METHOD."
738 (let ((outbuf (current-buffer)))
739 (mm-with-unibyte-buffer
740 (if (functionp method
)
741 (let ((cur (current-buffer)))
742 (if (eq method
'mailcap-save-binary-file
)
744 (set-buffer (generate-new-buffer " *mm*"))
746 (mm-insert-part handle
)
747 (let ((win (get-buffer-window cur t
)))
749 (select-window win
)))
750 (switch-to-buffer (generate-new-buffer " *mm*")))
751 (buffer-disable-undo)
752 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
753 (insert-buffer-substring cur
)
754 (goto-char (point-min))
756 (message "Viewing with %s" method
))
757 (let ((mm (current-buffer))
758 (non-viewer (assq 'non-viewer
760 (mm-handle-media-type handle
) t
))))
764 (mm-save-part handle
))
765 (when (and (not non-viewer
)
767 (mm-handle-set-undisplayer handle mm
)))))
768 ;; The function is a string to be executed.
769 (mm-insert-part handle
)
770 (let* ((dir (mm-make-temp-file
771 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
773 (mail-content-type-get
774 (mm-handle-disposition handle
) 'filename
)
775 (mail-content-type-get
776 (mm-handle-type handle
) 'name
)))
777 (mime-info (mailcap-mime-info
778 (mm-handle-media-type handle
) t
))
779 (needsterm (or (assoc "needsterm" mime-info
)
780 (assoc "needsterminal" mime-info
)))
781 (copiousoutput (assoc "copiousoutput" mime-info
))
783 ;; We create a private sub-directory where we store our files.
784 (set-file-modes dir
448)
786 (setq file
(expand-file-name
787 (gnus-map-function mm-file-name-rewrite-functions
788 (file-name-nondirectory filename
))
790 ;; Use nametemplate (defined in RFC1524) if it is specified
792 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
794 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
795 (setq suffix
(match-string 1 suffix
))
796 ;; Otherwise, use a suffix according to
797 ;; `mailcap-mime-extensions'.
798 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
799 mailcap-mime-extensions
))))
800 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
802 (let ((coding-system-for-write mm-binary-coding-system
))
803 (write-region (point-min) (point-max) file nil
'nomesg
))
804 (message "Viewing with %s" method
)
807 (let ((command (mm-mailcap-command
808 method file
(mm-handle-type handle
))))
811 (start-process "*display*" nil
812 mm-external-terminal-program
814 shell-command-switch command
)
822 shell-command-switch command
)))
825 (set-process-sentinel
826 (get-buffer-process buffer
)
827 `(lambda (process state
)
828 (if (eq 'exit
(process-status process
))
829 (gnus-configure-windows
830 ',gnus-current-window-configuration
))))
831 (gnus-configure-windows 'display-term
))
832 (mm-handle-set-external-undisplayer handle
(cons file buffer
)))
833 (message "Displaying %s..." command
))
836 (with-current-buffer outbuf
842 (call-process shell-file-name nil
844 (generate-new-buffer " *mm*"))
848 method file
(mm-handle-type handle
)))
849 (if (buffer-live-p buffer
)
850 (with-current-buffer buffer
853 (ignore-errors (delete-file file
))
854 (ignore-errors (delete-directory
855 (file-name-directory file
)))
856 (ignore-errors (kill-buffer buffer
))))))
859 ;; Deleting the temp file should be postponed for some wrappers,
860 ;; shell scripts, and so on, which might exit right after having
861 ;; started a viewer command as a background job.
862 (let ((command (mm-mailcap-command
863 method file
(mm-handle-type handle
))))
866 (start-process "*display*"
868 (generate-new-buffer " *mm*"))
870 shell-command-switch command
)
871 (set-process-sentinel
872 (get-buffer-process buffer
)
873 (lexical-let ;; Don't use `let'.
874 ;; Function used to remove temp file and directory.
876 ;; Don't use `ignore-errors'.
882 ,(file-name-directory file
))
884 ;; Form uses to kill the process buffer and
885 ;; remove the undisplayer.
887 (kill-buffer ,buffer
)
889 (list 'mm-handle-set-undisplayer
892 ;; Message to be issued when the process exits.
893 (done (format "Displaying %s...done" command
))
894 ;; In particular, the timer object (which is
895 ;; a vector in Emacs but is a list in XEmacs)
896 ;; requires that it is lexically scoped.
897 (timer (run-at-time 2.0 nil
'ignore
)))
898 (if (featurep 'xemacs
)
899 (lambda (process state
)
900 (when (eq 'exit
(process-status process
))
901 (if (memq timer itimer-list
)
902 (set-itimer-function timer fn
)
904 (ignore-errors (eval fm
))
905 (message "%s" done
)))
906 (lambda (process state
)
907 (when (eq 'exit
(process-status process
))
908 (if (memq timer timer-list
)
909 (timer-set-function timer fn
)
911 (ignore-errors (eval fm
))
912 (message "%s" done
)))))))
913 (mm-handle-set-external-undisplayer
914 handle
(cons file buffer
)))
915 (message "Displaying %s..." command
))
918 (defun mm-mailcap-command (method file type-list
)
919 (let ((ctl (cdr type-list
))
923 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
925 (push (substring method beg
(match-beginning 0)) out
)
926 (setq beg
(match-end 0)
927 total
(match-string 0 method
)
928 sub
(match-string 1 method
))
930 ((string= total
"%%")
932 ((or (string= total
"%s")
933 ;; We do our own quoting.
934 (string= total
"'%s'")
935 (string= total
"\"%s\""))
936 (setq uses-stdin nil
)
937 (push (shell-quote-argument
938 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
939 ((string= total
"%t")
940 (push (shell-quote-argument (car type-list
)) out
))
942 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
943 (push (substring method beg
(length method
)) out
)
946 (push (shell-quote-argument
947 (gnus-map-function mm-path-name-rewrite-functions file
))
949 (mapconcat 'identity
(nreverse out
) "")))
951 (defun mm-remove-parts (handles)
952 "Remove the displayed MIME parts represented by HANDLES."
953 (if (and (listp handles
)
954 (bufferp (car handles
)))
955 (mm-remove-part handles
)
957 (while (setq handle
(pop handles
))
960 (when (buffer-live-p (get-text-property 0 'buffer handle
))
961 (kill-buffer (get-text-property 0 'buffer handle
))))
963 (stringp (car handle
)))
964 (mm-remove-parts (cdr handle
)))
966 (mm-remove-part handle
)))))))
968 (defun mm-destroy-parts (handles)
969 "Remove the displayed MIME parts represented by HANDLES."
970 (if (and (listp handles
)
971 (bufferp (car handles
)))
972 (mm-destroy-part handles
)
974 (while (setq handle
(pop handles
))
977 (when (buffer-live-p (get-text-property 0 'buffer handle
))
978 (kill-buffer (get-text-property 0 'buffer handle
))))
980 (stringp (car handle
)))
981 (mm-destroy-parts handle
))
983 (mm-destroy-part handle
)))))))
985 (defun mm-remove-part (handle)
986 "Remove the displayed MIME part represented by HANDLE."
988 (let ((object (mm-handle-undisplayer handle
)))
991 ;; Internally displayed part.
992 ((mm-annotationp object
)
993 (delete-annotation object
))
994 ((or (functionp object
)
996 (eq (car object
) 'lambda
)))
998 ;; Externally displayed part.
1001 (while (get-buffer-process (cdr object
))
1002 (interrupt-process (get-buffer-process (cdr object
)))
1003 (message "Waiting for external displayer to die...")
1007 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1008 (message "Waiting for external displayer to die...done")
1009 (ignore-errors (delete-file (car object
)))
1010 (ignore-errors (delete-directory (file-name-directory
1013 (when (buffer-live-p object
)
1014 (kill-buffer object
)))))
1015 (mm-handle-set-undisplayer handle nil
))))
1017 (defun mm-display-inline (handle)
1018 (let* ((type (mm-handle-media-type handle
))
1019 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1020 (funcall function handle
)
1021 (goto-char (point-min))))
1023 (defun mm-assoc-string-match (alist type
)
1024 (dolist (elem alist
)
1025 (when (string-match (car elem
) type
)
1028 (defun mm-automatic-display-p (handle)
1029 "Say whether the user wants HANDLE to be displayed automatically."
1030 (let ((methods mm-automatic-display
)
1031 (type (mm-handle-media-type handle
))
1033 (while (setq method
(pop methods
))
1034 (when (and (not (mm-inline-override-p handle
))
1035 (string-match method type
))
1040 (defun mm-inlinable-p (handle &optional type
)
1041 "Say whether HANDLE can be displayed inline.
1042 TYPE is the mime-type of the object; it defaults to the one given
1044 (unless type
(setq type
(mm-handle-media-type handle
)))
1045 (let ((alist mm-inline-media-tests
)
1048 (when (string-match (caar alist
) type
)
1049 (setq test
(caddar alist
)
1051 (setq test
(funcall test handle
)))
1055 (defun mm-inlined-p (handle)
1056 "Say whether the user wants HANDLE to be displayed inline."
1057 (let ((methods mm-inlined-types
)
1058 (type (mm-handle-media-type handle
))
1060 (while (setq method
(pop methods
))
1061 (when (and (not (mm-inline-override-p handle
))
1062 (string-match method type
))
1067 (defun mm-attachment-override-p (handle)
1068 "Say whether HANDLE should have attachment behavior overridden."
1069 (let ((types mm-attachment-override-types
)
1070 (type (mm-handle-media-type handle
))
1073 (while (setq ty
(pop types
))
1074 (when (and (string-match ty type
)
1075 (mm-inlinable-p handle
))
1076 (throw 'found t
))))))
1078 (defun mm-inline-override-p (handle)
1079 "Say whether HANDLE should have inline behavior overridden."
1080 (let ((types mm-inline-override-types
)
1081 (type (mm-handle-media-type handle
))
1084 (while (setq ty
(pop types
))
1085 (when (string-match ty type
)
1086 (throw 'found t
))))))
1088 (defun mm-automatic-external-display-p (type)
1089 "Return the user-defined method for TYPE."
1090 (let ((methods mm-automatic-external-display
)
1092 (while (setq method
(pop methods
))
1093 (when (string-match method type
)
1098 (defun mm-destroy-part (handle)
1099 "Destroy the data structures connected to HANDLE."
1100 (when (listp handle
)
1101 (mm-remove-part handle
)
1102 (when (buffer-live-p (mm-handle-buffer handle
))
1103 (kill-buffer (mm-handle-buffer handle
)))))
1105 (defun mm-handle-displayed-p (handle)
1106 "Say whether HANDLE is displayed or not."
1107 (mm-handle-undisplayer handle
))
1110 ;;; Functions for outputting parts
1113 (defmacro mm-with-part
(handle &rest forms
)
1114 "Run FORMS in the temp buffer containing the contents of HANDLE."
1115 `(let* ((handle ,handle
)
1116 ;; The multibyteness of the temp buffer should be turned on
1117 ;; if inserting a multibyte string. Contrarily, the buffer's
1118 ;; multibyteness should be off if inserting a unibyte string,
1119 ;; especially if a string contains 8bit data.
1120 (default-enable-multibyte-characters
1121 (with-current-buffer (mm-handle-buffer handle
)
1124 (insert-buffer-substring (mm-handle-buffer handle
))
1125 (mm-disable-multibyte)
1126 (mm-decode-content-transfer-encoding
1127 (mm-handle-encoding handle
)
1128 (mm-handle-media-type handle
))
1130 (put 'mm-with-part
'lisp-indent-function
1)
1131 (put 'mm-with-part
'edebug-form-spec
'(body))
1133 (defun mm-get-part (handle &optional no-cache
)
1134 "Return the contents of HANDLE as a string.
1135 If NO-CACHE is non-nil, cached contents of a message/external-body part
1137 (if (and (not no-cache
)
1138 (equal (mm-handle-media-type handle
) "message/external-body"))
1140 (unless (mm-handle-cache handle
)
1141 (mm-extern-cache-contents handle
))
1142 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1144 (mm-with-part handle
1147 (defun mm-insert-part (handle &optional no-cache
)
1148 "Insert the contents of HANDLE in the current buffer.
1149 If NO-CACHE is non-nil, cached contents of a message/external-body part
1151 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1154 (with-current-buffer (mm-handle-buffer handle
)
1157 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1159 (mm-get-part handle no-cache
)))))
1165 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1167 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1168 ;; Separate the extracted parts that have the same faces.
1172 (defun mm-file-name-delete-whitespace (file-name)
1173 "Remove all whitespace characters from FILE-NAME."
1174 (while (string-match "\\s-+" file-name
)
1175 (setq file-name
(replace-match "" t t file-name
)))
1178 (defun mm-file-name-trim-whitespace (file-name)
1179 "Remove leading and trailing whitespace characters from FILE-NAME."
1180 (when (string-match "\\`\\s-+" file-name
)
1181 (setq file-name
(substring file-name
(match-end 0))))
1182 (when (string-match "\\s-+\\'" file-name
)
1183 (setq file-name
(substring file-name
0 (match-beginning 0))))
1186 (defun mm-file-name-collapse-whitespace (file-name)
1187 "Collapse multiple whitespace characters in FILE-NAME."
1188 (while (string-match "\\s-\\s-+" file-name
)
1189 (setq file-name
(replace-match " " t t file-name
)))
1192 (defun mm-file-name-replace-whitespace (file-name)
1193 "Replace whitespace characters in FILE-NAME with underscores.
1194 Set the option `mm-file-name-replace-whitespace' to any other
1195 string if you do not like underscores."
1196 (let ((s (or mm-file-name-replace-whitespace
"_")))
1197 (while (string-match "\\s-" file-name
)
1198 (setq file-name
(replace-match s t t file-name
))))
1201 (defun mm-file-name-delete-control (filename)
1202 "Delete control characters from FILENAME."
1203 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1205 (defun mm-file-name-delete-gotchas (filename)
1206 "Delete shell gotchas from FILENAME."
1207 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1208 (gnus-replace-in-string filename
"^[.-]+" ""))
1210 (defun mm-save-part (handle &optional prompt
)
1211 "Write HANDLE to a file.
1212 PROMPT overrides the default one used to ask user for a file name."
1213 (let ((filename (or (mail-content-type-get
1214 (mm-handle-disposition handle
) 'filename
)
1215 (mail-content-type-get
1216 (mm-handle-type handle
) 'name
)))
1219 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1220 (file-name-nondirectory filename
))))
1223 (read-file-name (or prompt
"Save MIME part to: ")
1224 (or mm-default-directory default-directory
)
1225 nil nil
(or filename
""))))
1226 (setq mm-default-directory
(file-name-directory file
))
1227 (and (or (not (file-exists-p file
))
1228 (yes-or-no-p (format "File %s already exists; overwrite? "
1231 (mm-save-part-to-file handle file
)
1234 (defun mm-save-part-to-file (handle file
)
1235 (mm-with-unibyte-buffer
1236 (mm-insert-part handle
)
1237 (let ((current-file-modes (default-file-modes)))
1238 (set-default-file-modes mm-attachment-file-modes
)
1240 ;; Don't re-compress .gz & al. Arguably we should make
1241 ;; `file-name-handler-alist' nil, but that would chop
1242 ;; ange-ftp, which is reasonable to use here.
1243 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1244 (set-default-file-modes current-file-modes
)))))
1246 (defun mm-pipe-part (handle)
1247 "Pipe HANDLE to a process."
1248 (let* ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
1250 (read-string "Shell command on MIME part: " mm-last-shell-command
)))
1251 (mm-with-unibyte-buffer
1252 (mm-insert-part handle
)
1253 (let ((coding-system-for-write 'binary
))
1254 (shell-command-on-region (point-min) (point-max) command nil
)))))
1256 (defun mm-interactively-view-part (handle)
1257 "Display HANDLE using METHOD."
1258 (let* ((type (mm-handle-media-type handle
))
1260 (mapcar (lambda (i) (list (cdr (assoc 'viewer i
))))
1261 (mailcap-mime-info type
'all
)))
1262 (method (let ((minibuffer-local-completion-map
1263 mm-viewer-completion-map
))
1264 (completing-read "Viewer: " methods
))))
1265 (when (string= method
"")
1266 (error "No method given"))
1267 (if (string-match "^[^% \t]+$" method
)
1268 (setq method
(concat method
" %s")))
1269 (mm-display-external handle method
)))
1271 (defun mm-preferred-alternative (handles &optional preferred
)
1272 "Say which of HANDLES are preferred."
1273 (let ((prec (if preferred
(list preferred
)
1274 (mm-preferred-alternative-precedence handles
)))
1275 p h result type handle
)
1276 (while (setq p
(pop prec
))
1279 (setq handle
(car h
))
1280 (setq type
(mm-handle-media-type handle
))
1281 (when (and (equal p type
)
1282 (mm-automatic-display-p handle
)
1283 (or (stringp (car handle
))
1284 (not (mm-handle-disposition handle
))
1285 (equal (car (mm-handle-disposition handle
))
1293 (defun mm-preferred-alternative-precedence (handles)
1294 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1295 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1297 (dolist (disc (reverse mm-discouraged-alternatives
))
1298 (dolist (elem (copy-sequence seq
))
1299 (when (string-match disc elem
)
1300 (setq seq
(nconc (delete elem seq
) (list elem
))))))
1303 (defun mm-get-content-id (id)
1304 "Return the handle(s) referred to by ID."
1305 (cdr (assoc id mm-content-id-alist
)))
1307 (defconst mm-image-type-regexps
1308 '(("/\\*.*XPM.\\*/" . xpm
)
1312 ("\211PNG\r\n" . png
)
1314 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1315 ("%!PS" . postscript
))
1316 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1317 When the first bytes of an image file match REGEXP, it is assumed to
1318 be of image type IMAGE-TYPE.")
1320 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1321 (defun mm-image-type-from-buffer ()
1322 "Determine the image type from data in the current buffer.
1323 Value is a symbol specifying the image type or nil if type cannot
1325 (let ((types mm-image-type-regexps
)
1327 (goto-char (point-min))
1328 (while (and types
(null type
))
1329 (let ((regexp (car (car types
)))
1330 (image-type (cdr (car types
))))
1331 (when (looking-at regexp
)
1332 (setq type image-type
))
1333 (setq types
(cdr types
))))
1336 (defun mm-get-image (handle)
1337 "Return an image instance based on HANDLE."
1338 (let ((type (mm-handle-media-subtype handle
))
1340 ;; Allow some common translations.
1343 ((equal type
"x-pixmap")
1345 ((equal type
"x-xbitmap")
1347 ((equal type
"x-portable-bitmap")
1350 (or (mm-handle-cache handle
)
1351 (mm-with-unibyte-buffer
1352 (mm-insert-part handle
)
1356 ;; Avoid testing `make-glyph' since W3 may define
1357 ;; a bogus version of it.
1358 (if (fboundp 'create-image
)
1359 (create-image (buffer-string)
1360 (or (mm-image-type-from-buffer)
1363 (mm-create-image-xemacs type
))))
1364 (mm-handle-set-cache handle spec
))))))
1366 (defun mm-create-image-xemacs (type)
1367 (when (featurep 'xemacs
)
1370 ;; xbm images require special handling, since
1371 ;; the only way to create glyphs from these
1372 ;; (without a ton of work) is to write them
1373 ;; out to a file, and then create a file
1375 (let ((file (mm-make-temp-file
1376 (expand-file-name "emm" mm-tmp-directory
)
1380 (write-region (point-min) (point-max) file
)
1381 (make-glyph (list (cons 'x file
))))
1383 (delete-file file
)))))
1387 (or (mm-image-type-from-buffer)
1389 :data
(buffer-string)))))))
1391 (defun mm-image-fit-p (handle)
1392 "Say whether the image in HANDLE will fit the current window."
1393 (let ((image (mm-get-image handle
)))
1395 (if (featurep 'xemacs
)
1396 ;; XEmacs' glyphs can actually tell us about their width, so
1397 ;; lets be nice and smart about them.
1398 (or mm-inline-large-images
1399 (and (<= (glyph-width image
) (window-pixel-width))
1400 (<= (glyph-height image
) (window-pixel-height))))
1401 (let* ((size (image-size image
))
1404 (or mm-inline-large-images
1405 (and (<= h
(1- (window-height))) ; Don't include mode line.
1406 (<= w
(window-width)))))))))
1408 (defun mm-valid-image-format-p (format)
1409 "Say whether FORMAT can be displayed natively by Emacs."
1412 ((fboundp 'valid-image-instantiator-format-p
)
1413 (valid-image-instantiator-format-p format
))
1415 ((fboundp 'image-type-available-p
)
1416 (and (display-graphic-p)
1417 (image-type-available-p format
)))
1418 ;; Nobody else can do images yet.
1422 (defun mm-valid-and-fit-image-p (format handle
)
1423 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1424 (and (mm-valid-image-format-p format
)
1425 (mm-image-fit-p handle
)))
1427 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1428 "Search in HANDLES for part with TYPE.
1429 If NOTP, returns first non-matching part.
1430 If RECURSIVE, search recursively."
1433 (if (and recursive
(stringp (caar handles
)))
1434 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1438 (not (equal (mm-handle-media-type (car handles
)) type
))
1439 (equal (mm-handle-media-type (car handles
)) type
))
1440 (setq handle
(car handles
)
1442 (setq handles
(cdr handles
)))
1445 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1446 (goto-char (point-min))
1447 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1449 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1451 (end (save-excursion
1452 (goto-char (point-max))
1453 (if (re-search-backward close-delimiter nil t
)
1457 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1458 (while (and (not result
)
1459 (re-search-forward boundary end t
))
1460 (goto-char (match-beginning 0))
1464 (narrow-to-region start
(1- (point)))
1465 (when (let* ((ct (mail-fetch-field "content-type"))
1466 (ctl (and ct
(mail-header-parse-content-type ct
))))
1468 (not (equal (car ctl
) type
))
1469 (equal (car ctl
) type
)))
1470 (setq result
(buffer-string))))))
1472 (setq start
(point)))
1473 (when (and (not result
) start
)
1476 (narrow-to-region start end
)
1477 (when (let* ((ct (mail-fetch-field "content-type"))
1478 (ctl (and ct
(mail-header-parse-content-type ct
))))
1480 (not (equal (car ctl
) type
))
1481 (equal (car ctl
) type
)))
1482 (setq result
(buffer-string))))))
1485 (defvar mm-security-handle nil
)
1487 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1488 ;; HANDLE could be a CTL.
1490 (put-text-property 0 (length (car handle
)) parameter value
1493 (defun mm-possibly-verify-or-decrypt (parts ctl
)
1494 (let ((type (car ctl
))
1495 (subtype (cadr (split-string (car ctl
) "/")))
1496 (mm-security-handle ctl
) ;; (car CTL) is the type.
1497 protocol func functest
)
1499 ((or (equal type
"application/x-pkcs7-mime")
1500 (equal type
"application/pkcs7-mime"))
1503 ((eq mm-decrypt-option
'never
) nil
)
1504 ((eq mm-decrypt-option
'always
) t
)
1505 ((eq mm-decrypt-option
'known
) t
)
1507 (format "Decrypt (S/MIME) part? "))))
1508 (mm-view-pkcs7 parts
))
1509 (setq parts
(mm-dissect-buffer t
)))))
1510 ((equal subtype
"signed")
1511 (unless (and (setq protocol
1512 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1513 (not (equal protocol
"multipart/mixed")))
1514 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1515 (let ((protocols mm-verify-function-alist
))
1517 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1518 (funcall functest parts ctl
))
1519 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1520 (setq protocol
(caar protocols
)
1522 (setq protocols
(cdr protocols
))))))
1523 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1525 ((eq mm-verify-option
'never
) nil
)
1526 ((eq mm-verify-option
'always
) t
)
1527 ((eq mm-verify-option
'known
)
1529 (or (not (setq functest
1530 (nth 3 (assoc protocol
1531 mm-verify-function-alist
))))
1532 (funcall functest parts ctl
))))
1535 (format "Verify signed (%s) part? "
1536 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1537 (format "protocol=%s" protocol
))))))
1540 (setq parts
(funcall func parts ctl
))
1541 (mm-set-handle-multipart-parameter
1542 mm-security-handle
'gnus-details
1543 (format "Unknown sign protocol (%s)" protocol
))))))
1544 ((equal subtype
"encrypted")
1545 (unless (setq protocol
1546 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1547 ;; The message is broken.
1548 (let ((parts parts
))
1550 (if (assoc (mm-handle-media-type (car parts
))
1551 mm-decrypt-function-alist
)
1552 (setq protocol
(mm-handle-media-type (car parts
))
1554 (setq parts
(cdr parts
))))))
1555 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1557 ((eq mm-decrypt-option
'never
) nil
)
1558 ((eq mm-decrypt-option
'always
) t
)
1559 ((eq mm-decrypt-option
'known
)
1561 (or (not (setq functest
1562 (nth 3 (assoc protocol
1563 mm-decrypt-function-alist
))))
1564 (funcall functest parts ctl
))))
1567 (format "Decrypt (%s) part? "
1568 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1569 (format "protocol=%s" protocol
))))))
1572 (setq parts
(funcall func parts ctl
))
1573 (mm-set-handle-multipart-parameter
1574 mm-security-handle
'gnus-details
1575 (format "Unknown encrypt protocol (%s)" protocol
))))))
1579 (defun mm-multiple-handles (handles)
1580 (and (listp handles
)
1581 (> (length handles
) 1)
1582 (or (listp (car handles
))
1583 (stringp (car handles
)))))
1585 (defun mm-complicated-handles (handles)
1586 (and (listp (car handles
))
1587 (> (length handles
) 1)))
1589 (defun mm-merge-handles (handles1 handles2
)
1591 (if (listp (car handles1
))
1594 (if (listp (car handles2
))
1598 (defun mm-readable-p (handle)
1599 "Say whether the content of HANDLE is readable."
1600 (and (< (with-current-buffer (mm-handle-buffer handle
)
1601 (buffer-size)) 10000)
1602 (mm-with-unibyte-buffer
1603 (mm-insert-part handle
)
1604 (and (eq (mm-body-7-or-8) '7bit
)
1605 (not (mm-long-lines-p 76))))))
1607 (provide 'mm-decode
)
1609 ;; arch-tag: 4f35d360-56b8-4030-9388-3ed82d359b9b
1610 ;;; mm-decode.el ends here