1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
35 (eval-when-compile (require 'cl
)
38 (autoload 'mm-inline-partial
"mm-partial")
39 (autoload 'mm-inline-external-body
"mm-extern")
40 (autoload 'mm-extern-cache-contents
"mm-extern")
41 (autoload 'mm-insert-inline
"mm-view")
43 (defvar gnus-current-window-configuration
)
45 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
47 (defgroup mime-display
()
48 "Display of MIME in mail and news articles."
49 :link
'(custom-manual "(emacs-mime)Display Customization")
55 (defgroup mime-security
()
56 "MIME security in mail and news articles."
57 :link
'(custom-manual "(emacs-mime)Display Customization")
62 ;;; Convenience macros.
64 (defmacro mm-handle-buffer
(handle)
66 (defmacro mm-handle-type
(handle)
68 (defsubst mm-handle-media-type
(handle)
69 (if (stringp (car handle
))
71 (car (mm-handle-type handle
))))
72 (defsubst mm-handle-media-supertype
(handle)
73 (car (split-string (mm-handle-media-type handle
) "/")))
74 (defsubst mm-handle-media-subtype
(handle)
75 (cadr (split-string (mm-handle-media-type handle
) "/")))
76 (defmacro mm-handle-encoding
(handle)
78 (defmacro mm-handle-undisplayer
(handle)
80 (defmacro mm-handle-set-undisplayer
(handle function
)
81 `(setcar (nthcdr 3 ,handle
) ,function
))
82 (defmacro mm-handle-disposition
(handle)
84 (defmacro mm-handle-description
(handle)
86 (defmacro mm-handle-cache
(handle)
88 (defmacro mm-handle-set-cache
(handle contents
)
89 `(setcar (nthcdr 6 ,handle
) ,contents
))
90 (defmacro mm-handle-id
(handle)
92 (defmacro mm-handle-multipart-original-buffer
(handle)
93 `(get-text-property 0 'buffer
(car ,handle
)))
94 (defmacro mm-handle-multipart-from
(handle)
95 `(get-text-property 0 'from
(car ,handle
)))
96 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
97 `(get-text-property 0 ,parameter
(car ,handle
)))
99 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
100 disposition description cache
102 `(list ,buffer
,type
,encoding
,undisplayer
103 ,disposition
,description
,cache
,id
))
105 (defcustom mm-text-html-renderer
106 (cond ((executable-find "w3m")
107 (if (locate-library "w3m")
110 ((executable-find "links") 'links
)
111 ((executable-find "lynx") 'lynx
)
112 ((locate-library "w3") 'w3
)
113 ((locate-library "html2text") 'html2text
)
115 "Render of HTML contents.
116 It is one of defined renderer types, or a rendering function.
117 The defined renderer types are:
118 `w3m' : use emacs-w3m;
119 `w3m-standalone': use w3m;
123 `html2text' : use html2text;
124 nil : use external viewer (default web browser)."
125 :version
"23.0" ;; No Gnus
126 :type
'(choice (const w3
)
127 (const w3m
:tag
"emacs-w3m")
128 (const w3m-standalone
:tag
"standalone w3m" )
132 (const nil
:tag
"External viewer")
134 :group
'mime-display
)
136 (defvar mm-inline-text-html-renderer nil
137 "Function used for rendering inline HTML contents.
138 It is suggested to customize `mm-text-html-renderer' instead.")
140 (defcustom mm-inline-text-html-with-images nil
141 "If non-nil, Gnus will allow retrieving images in HTML contents with
142 the <img> tags. It has no effect on Emacs/w3. See also the
143 documentation for the `mm-w3m-safe-url-regexp' variable."
146 :group
'mime-display
)
148 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
149 "Regexp matching URLs which are considered to be safe.
150 Some HTML mails might contain a nasty trick used by spammers, using
151 the <img> tag which is far more evil than the [Click Here!] button.
152 It is most likely intended to check whether the ominous spam mail has
153 reached your eyes or not, in which case the spammer knows for sure
154 that your email address is valid. It is done by embedding an
155 identifier string into a URL that you might automatically retrieve
156 when displaying the image. The default value is \"\\\\`cid:\" which only
157 matches parts embedded to the Multipart/Related type MIME contents and
158 Gnus will never connect to the spammer's site arbitrarily. You may
159 set this variable to nil if you consider all urls to be safe."
161 :type
'(choice (regexp :tag
"Regexp")
162 (const :tag
"All URLs are safe" nil
))
163 :group
'mime-display
)
165 (defcustom mm-inline-text-html-with-w3m-keymap t
166 "If non-nil, use emacs-w3m command keys in the article buffer."
169 :group
'mime-display
)
171 (defcustom mm-enable-external t
172 "Indicate whether external MIME handlers should be used.
174 If t, all defined external MIME handlers are used. If nil, files are saved by
175 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
176 before the external MIME handler is invoked."
178 :type
'(choice (const :tag
"Always" t
)
179 (const :tag
"Never" nil
)
180 (const :tag
"Ask" ask
))
181 :group
'mime-display
)
183 (defcustom mm-inline-media-tests
187 (mm-valid-and-fit-image-p 'jpeg handle
)))
191 (mm-valid-and-fit-image-p 'png handle
)))
195 (mm-valid-and-fit-image-p 'gif handle
)))
199 (mm-valid-and-fit-image-p 'tiff handle
)) )
203 (mm-valid-and-fit-image-p 'xbm handle
)))
207 (mm-valid-and-fit-image-p 'xbm handle
)))
211 (mm-valid-and-fit-image-p 'xpm handle
)))
215 (mm-valid-and-fit-image-p 'xpm handle
)))
219 (mm-valid-and-fit-image-p 'bmp handle
)))
220 ("image/x-portable-bitmap"
223 (mm-valid-and-fit-image-p 'pbm handle
)))
224 ("text/plain" mm-inline-text identity
)
225 ("text/enriched" mm-inline-text identity
)
226 ("text/richtext" mm-inline-text identity
)
227 ("text/x-patch" mm-display-patch-inline
229 ;; If the diff-mode.el package is installed, the function is
230 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
231 ;; to cater to broken installations. OTOH checking the function
232 ;; makes it possible to install another package which provides an
233 ;; alternative implementation of diff-mode. --Stef
234 (fboundp 'diff-mode
)))
235 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
236 ("text/x-diff" mm-display-patch-inline
237 (lambda (handle) (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 ;; Newlines in description should be stripped so as
567 ;; not to break the MIME tag into two or more lines.
568 description
(message-fetch-field "content-description")
569 id
(mail-fetch-field "content-id"))
571 (setq from
(mail-fetch-field "from")))
572 ;; FIXME: In some circumstances, this code is running within
573 ;; an unibyte macro. mail-extract-address-components
574 ;; creates unibyte buffers. This `if', though not a perfect
575 ;; solution, avoids most of them.
577 (setq from
(cadr (mail-extract-address-components from
))))
579 (setq description
(mail-decode-encoded-word-string
582 (not (string-match "/" (car ctl
))))
583 (mm-dissect-singlepart
584 (list mm-dissect-default-type
)
585 (and cte
(intern (downcase (mail-header-strip cte
))))
587 (and cd
(mail-header-parse-content-disposition cd
))
589 (setq type
(split-string (car ctl
) "/"))
590 (setq subtype
(cadr type
)
595 ((equal type
"multipart")
596 (let ((mm-dissect-default-type (if (equal subtype
"digest")
599 (start (cdr (assq 'start
(cdr ctl
)))))
600 (add-text-properties 0 (length (car ctl
))
601 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
603 ;; what really needs to be done here is a way to link a
604 ;; MIME handle back to it's parent MIME handle (in a multilevel
605 ;; MIME article). That would probably require changing
606 ;; the mm-handle API so we simply store the multipart buffer
607 ;; name as a text property of the "multipart/whatever" string.
608 (add-text-properties 0 (length (car ctl
))
609 (list 'buffer
(mm-copy-to-buffer)
613 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
615 (mm-possibly-verify-or-decrypt
616 (mm-dissect-singlepart
618 (and cte
(intern (downcase (mail-header-strip cte
))))
620 (and cd
(mail-header-parse-content-disposition cd
))
624 (when (string-match " *<\\(.*\\)> *" id
)
625 (setq id
(match-string 1 id
)))
626 (push (cons id result
) mm-content-id-alist
))
629 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
631 (if (equal "text/plain" (car ctl
))
635 (mm-copy-to-buffer) ctl cte nil cdl description nil id
)))
637 (defun mm-dissect-multipart (ctl from
)
638 (goto-char (point-min))
639 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
640 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
643 (goto-char (point-max))
644 (if (re-search-backward close-delimiter nil t
)
647 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
648 (while (and (< (point) end
) (re-search-forward boundary end t
))
649 (goto-char (match-beginning 0))
653 (narrow-to-region start
(point))
654 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
656 (or (looking-at boundary
)
658 (setq start
(point)))
659 (when (and start
(< start end
))
662 (narrow-to-region start end
)
663 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
664 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl
)))
666 (defun mm-copy-to-buffer ()
667 "Copy the contents of the current buffer to a fresh buffer."
668 (let ((obuf (current-buffer))
669 (mb (mm-multibyte-p))
671 (goto-char (point-min))
672 (search-forward-regexp "^\n" nil t
)
675 (generate-new-buffer " *mm*")
676 ;; Preserve the data's unibyteness (for url-insert-file-contents).
677 (mm-set-buffer-multibyte mb
)
678 (insert-buffer-substring obuf beg
)
681 (defun mm-display-parts (handle &optional no-default
)
682 (if (stringp (car handle
))
683 (mapcar 'mm-display-parts
(cdr handle
))
684 (if (bufferp (car handle
))
686 (narrow-to-region (point) (point))
687 (mm-display-part handle
)
688 (goto-char (point-max)))
689 (mapcar 'mm-display-parts handle
))))
691 (defun mm-display-part (handle &optional no-default
)
692 "Display the MIME part represented by HANDLE.
693 Returns nil if the part is removed; inline if displayed inline;
694 external if displayed external."
696 (mailcap-parse-mailcaps)
697 (if (mm-handle-displayed-p handle
)
698 (mm-remove-part handle
)
699 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
700 "message/external-body")
702 (unless (mm-handle-cache handle
)
703 (mm-extern-cache-contents handle
))
704 (mm-handle-cache handle
))
706 (type (mm-handle-media-type ehandle
))
707 (method (mailcap-mime-info type
))
708 (filename (or (mail-content-type-get
709 (mm-handle-disposition handle
) 'filename
)
710 (mail-content-type-get
711 (mm-handle-type handle
) 'name
)
713 (external mm-enable-external
))
714 (if (and (mm-inlinable-p ehandle
)
715 (mm-inlined-p ehandle
))
718 (mm-display-inline handle
)
722 (if (and (not method
)
723 (equal "text" (car (split-string type
"/"))))
726 (mm-insert-inline handle
(mm-get-part handle
))
729 (and method
;; If nil, we always use "save".
730 (stringp method
) ;; 'mailcap-save-binary-file
731 (or (eq mm-enable-external t
)
732 (and (eq mm-enable-external
'ask
)
735 "Display part (" type
736 ") using external program"
737 ;; Can non-string method ever happen?
740 " \"" (format method filename
) "\"")
745 handle
(or method
'mailcap-save-binary-file
))
747 handle
'mailcap-save-binary-file
)))))))))
749 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
751 (defun mm-display-external (handle method
)
752 "Display HANDLE using METHOD."
753 (let ((outbuf (current-buffer)))
754 (mm-with-unibyte-buffer
755 (if (functionp method
)
756 (let ((cur (current-buffer)))
757 (if (eq method
'mailcap-save-binary-file
)
759 (set-buffer (generate-new-buffer " *mm*"))
761 (mm-insert-part handle
)
762 (mm-add-meta-html-tag handle
)
763 (let ((win (get-buffer-window cur t
)))
765 (select-window win
)))
766 (switch-to-buffer (generate-new-buffer " *mm*")))
767 (buffer-disable-undo)
768 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
769 (insert-buffer-substring cur
)
770 (goto-char (point-min))
772 (message "Viewing with %s" method
))
773 (let ((mm (current-buffer))
774 (non-viewer (assq 'non-viewer
776 (mm-handle-media-type handle
) t
))))
780 (mm-save-part handle
))
781 (when (and (not non-viewer
)
783 (mm-handle-set-undisplayer handle mm
)))))
784 ;; The function is a string to be executed.
785 (mm-insert-part handle
)
786 (mm-add-meta-html-tag handle
)
787 (let* ((dir (mm-make-temp-file
788 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
790 (mail-content-type-get
791 (mm-handle-disposition handle
) 'filename
)
792 (mail-content-type-get
793 (mm-handle-type handle
) 'name
)))
794 (mime-info (mailcap-mime-info
795 (mm-handle-media-type handle
) t
))
796 (needsterm (or (assoc "needsterm" mime-info
)
797 (assoc "needsterminal" mime-info
)))
798 (copiousoutput (assoc "copiousoutput" mime-info
))
800 ;; We create a private sub-directory where we store our files.
801 (set-file-modes dir
#o700
)
803 (setq file
(expand-file-name
804 (gnus-map-function mm-file-name-rewrite-functions
805 (file-name-nondirectory filename
))
807 ;; Use nametemplate (defined in RFC1524) if it is specified
809 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
811 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
812 (setq suffix
(match-string 1 suffix
))
813 ;; Otherwise, use a suffix according to
814 ;; `mailcap-mime-extensions'.
815 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
816 mailcap-mime-extensions
))))
817 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
819 (let ((coding-system-for-write mm-binary-coding-system
))
820 (write-region (point-min) (point-max) file nil
'nomesg
))
821 ;; The file is deleted after the viewer exists. If the users edits
822 ;; the file, changes will be lost. Set file to read-only to make it
824 (set-file-modes file
#o400
)
825 (message "Viewing with %s" method
)
828 (let ((command (mm-mailcap-command
829 method file
(mm-handle-type handle
))))
832 (start-process "*display*" nil
833 mm-external-terminal-program
835 shell-command-switch command
)
843 shell-command-switch command
)))
846 (set-process-sentinel
847 (get-buffer-process buffer
)
848 `(lambda (process state
)
849 (if (eq 'exit
(process-status process
))
850 (gnus-configure-windows
851 ',gnus-current-window-configuration
))))
852 (gnus-configure-windows 'display-term
))
853 (mm-handle-set-external-undisplayer handle
(cons file buffer
)))
854 (message "Displaying %s..." command
))
857 (with-current-buffer outbuf
863 (call-process shell-file-name nil
865 (generate-new-buffer " *mm*"))
869 method file
(mm-handle-type handle
)))
870 (if (buffer-live-p buffer
)
871 (with-current-buffer buffer
874 (ignore-errors (delete-file file
))
875 (ignore-errors (delete-directory
876 (file-name-directory file
)))
877 (ignore-errors (kill-buffer buffer
))))))
880 ;; Deleting the temp file should be postponed for some wrappers,
881 ;; shell scripts, and so on, which might exit right after having
882 ;; started a viewer command as a background job.
883 (let ((command (mm-mailcap-command
884 method file
(mm-handle-type handle
))))
887 (start-process "*display*"
889 (generate-new-buffer " *mm*"))
891 shell-command-switch command
)
892 (set-process-sentinel
893 (get-buffer-process buffer
)
894 (lexical-let ;; Don't use `let'.
895 ;; Function used to remove temp file and directory.
897 ;; Don't use `ignore-errors'.
903 ,(file-name-directory file
))
905 ;; Form uses to kill the process buffer and
906 ;; remove the undisplayer.
908 (kill-buffer ,buffer
)
910 (list 'mm-handle-set-undisplayer
913 ;; Message to be issued when the process exits.
914 (done (format "Displaying %s...done" command
))
915 ;; In particular, the timer object (which is
916 ;; a vector in Emacs but is a list in XEmacs)
917 ;; requires that it is lexically scoped.
918 (timer (run-at-time 2.0 nil
'ignore
)))
919 (if (featurep 'xemacs
)
920 (lambda (process state
)
921 (when (eq 'exit
(process-status process
))
922 (if (memq timer itimer-list
)
923 (set-itimer-function timer fn
)
925 (ignore-errors (eval fm
))
926 (message "%s" done
)))
927 (lambda (process state
)
928 (when (eq 'exit
(process-status process
))
929 (if (memq timer timer-list
)
930 (timer-set-function timer fn
)
932 (ignore-errors (eval fm
))
933 (message "%s" done
)))))))
934 (mm-handle-set-external-undisplayer
935 handle
(cons file buffer
)))
936 (message "Displaying %s..." command
))
939 (defun mm-mailcap-command (method file type-list
)
940 (let ((ctl (cdr type-list
))
944 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
946 (push (substring method beg
(match-beginning 0)) out
)
947 (setq beg
(match-end 0)
948 total
(match-string 0 method
)
949 sub
(match-string 1 method
))
951 ((string= total
"%%")
953 ((or (string= total
"%s")
954 ;; We do our own quoting.
955 (string= total
"'%s'")
956 (string= total
"\"%s\""))
957 (setq uses-stdin nil
)
958 (push (shell-quote-argument
959 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
960 ((string= total
"%t")
961 (push (shell-quote-argument (car type-list
)) out
))
963 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
964 (push (substring method beg
(length method
)) out
)
967 (push (shell-quote-argument
968 (gnus-map-function mm-path-name-rewrite-functions file
))
970 (mapconcat 'identity
(nreverse out
) "")))
972 (defun mm-remove-parts (handles)
973 "Remove the displayed MIME parts represented by HANDLES."
974 (if (and (listp handles
)
975 (bufferp (car handles
)))
976 (mm-remove-part handles
)
978 (while (setq handle
(pop handles
))
981 (when (buffer-live-p (get-text-property 0 'buffer handle
))
982 (kill-buffer (get-text-property 0 'buffer handle
))))
984 (stringp (car handle
)))
985 (mm-remove-parts (cdr handle
)))
987 (mm-remove-part handle
)))))))
989 (defun mm-destroy-parts (handles)
990 "Remove the displayed MIME parts represented by HANDLES."
991 (if (and (listp handles
)
992 (bufferp (car handles
)))
993 (mm-destroy-part handles
)
995 (while (setq handle
(pop handles
))
998 (when (buffer-live-p (get-text-property 0 'buffer handle
))
999 (kill-buffer (get-text-property 0 'buffer handle
))))
1000 ((and (listp handle
)
1001 (stringp (car handle
)))
1002 (mm-destroy-parts handle
))
1004 (mm-destroy-part handle
)))))))
1006 (defun mm-remove-part (handle)
1007 "Remove the displayed MIME part represented by HANDLE."
1008 (when (listp handle
)
1009 (let ((object (mm-handle-undisplayer handle
)))
1012 ;; Internally displayed part.
1013 ((mm-annotationp object
)
1014 (if (featurep 'xemacs
)
1015 (delete-annotation object
)))
1016 ((or (functionp object
)
1018 (eq (car object
) 'lambda
)))
1020 ;; Externally displayed part.
1023 (while (get-buffer-process (cdr object
))
1024 (interrupt-process (get-buffer-process (cdr object
)))
1025 (message "Waiting for external displayer to die...")
1029 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1030 (message "Waiting for external displayer to die...done")
1031 (ignore-errors (delete-file (car object
)))
1032 (ignore-errors (delete-directory (file-name-directory
1035 (when (buffer-live-p object
)
1036 (kill-buffer object
)))))
1037 (mm-handle-set-undisplayer handle nil
))))
1039 (defun mm-display-inline (handle)
1040 (let* ((type (mm-handle-media-type handle
))
1041 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1042 (funcall function handle
)
1043 (goto-char (point-min))))
1045 (defun mm-assoc-string-match (alist type
)
1046 (dolist (elem alist
)
1047 (when (string-match (car elem
) type
)
1050 (defun mm-automatic-display-p (handle)
1051 "Say whether the user wants HANDLE to be displayed automatically."
1052 (let ((methods mm-automatic-display
)
1053 (type (mm-handle-media-type handle
))
1055 (while (setq method
(pop methods
))
1056 (when (and (not (mm-inline-override-p handle
))
1057 (string-match method type
))
1062 (defun mm-inlinable-p (handle &optional type
)
1063 "Say whether HANDLE can be displayed inline.
1064 TYPE is the mime-type of the object; it defaults to the one given
1066 (unless type
(setq type
(mm-handle-media-type handle
)))
1067 (let ((alist mm-inline-media-tests
)
1070 (when (string-match (caar alist
) type
)
1071 (setq test
(caddar alist
)
1073 (setq test
(funcall test handle
)))
1077 (defun mm-inlined-p (handle)
1078 "Say whether the user wants HANDLE to be displayed inline."
1079 (let ((methods mm-inlined-types
)
1080 (type (mm-handle-media-type handle
))
1082 (while (setq method
(pop methods
))
1083 (when (and (not (mm-inline-override-p handle
))
1084 (string-match method type
))
1089 (defun mm-attachment-override-p (handle)
1090 "Say whether HANDLE should have attachment behavior overridden."
1091 (let ((types mm-attachment-override-types
)
1092 (type (mm-handle-media-type handle
))
1095 (while (setq ty
(pop types
))
1096 (when (and (string-match ty type
)
1097 (mm-inlinable-p handle
))
1098 (throw 'found t
))))))
1100 (defun mm-inline-override-p (handle)
1101 "Say whether HANDLE should have inline behavior overridden."
1102 (let ((types mm-inline-override-types
)
1103 (type (mm-handle-media-type handle
))
1106 (while (setq ty
(pop types
))
1107 (when (string-match ty type
)
1108 (throw 'found t
))))))
1110 (defun mm-automatic-external-display-p (type)
1111 "Return the user-defined method for TYPE."
1112 (let ((methods mm-automatic-external-display
)
1114 (while (setq method
(pop methods
))
1115 (when (string-match method type
)
1120 (defun mm-destroy-part (handle)
1121 "Destroy the data structures connected to HANDLE."
1122 (when (listp handle
)
1123 (mm-remove-part handle
)
1124 (when (buffer-live-p (mm-handle-buffer handle
))
1125 (kill-buffer (mm-handle-buffer handle
)))))
1127 (defun mm-handle-displayed-p (handle)
1128 "Say whether HANDLE is displayed or not."
1129 (mm-handle-undisplayer handle
))
1132 ;;; Functions for outputting parts
1135 (defmacro mm-with-part
(handle &rest forms
)
1136 "Run FORMS in the temp buffer containing the contents of HANDLE."
1137 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1138 ;; chars, so the buffer should be unibyte. It may happen that the
1139 ;; handle-buffer is multibyte for some reason, in which case now is a good
1140 ;; time to adjust it, since we know at this point that it should
1142 `(let* ((handle ,handle
))
1144 (mm-disable-multibyte)
1145 (insert-buffer-substring (mm-handle-buffer handle
))
1146 (mm-decode-content-transfer-encoding
1147 (mm-handle-encoding handle
)
1148 (mm-handle-media-type handle
))
1150 (put 'mm-with-part
'lisp-indent-function
1)
1151 (put 'mm-with-part
'edebug-form-spec
'(body))
1153 (defun mm-get-part (handle &optional no-cache
)
1154 "Return the contents of HANDLE as a string.
1155 If NO-CACHE is non-nil, cached contents of a message/external-body part
1157 (if (and (not no-cache
)
1158 (equal (mm-handle-media-type handle
) "message/external-body"))
1160 (unless (mm-handle-cache handle
)
1161 (mm-extern-cache-contents handle
))
1162 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1164 (mm-with-part handle
1167 (defun mm-insert-part (handle &optional no-cache
)
1168 "Insert the contents of HANDLE in the current buffer.
1169 If NO-CACHE is non-nil, cached contents of a message/external-body part
1171 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1174 (with-current-buffer (mm-handle-buffer handle
)
1177 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1179 (mm-get-part handle no-cache
)))))
1185 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1187 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1188 ;; Separate the extracted parts that have the same faces.
1192 (defun mm-file-name-delete-whitespace (file-name)
1193 "Remove all whitespace characters from FILE-NAME."
1194 (while (string-match "\\s-+" file-name
)
1195 (setq file-name
(replace-match "" t t file-name
)))
1198 (defun mm-file-name-trim-whitespace (file-name)
1199 "Remove leading and trailing whitespace characters from FILE-NAME."
1200 (when (string-match "\\`\\s-+" file-name
)
1201 (setq file-name
(substring file-name
(match-end 0))))
1202 (when (string-match "\\s-+\\'" file-name
)
1203 (setq file-name
(substring file-name
0 (match-beginning 0))))
1206 (defun mm-file-name-collapse-whitespace (file-name)
1207 "Collapse multiple whitespace characters in FILE-NAME."
1208 (while (string-match "\\s-\\s-+" file-name
)
1209 (setq file-name
(replace-match " " t t file-name
)))
1212 (defun mm-file-name-replace-whitespace (file-name)
1213 "Replace whitespace characters in FILE-NAME with underscores.
1214 Set the option `mm-file-name-replace-whitespace' to any other
1215 string if you do not like underscores."
1216 (let ((s (or mm-file-name-replace-whitespace
"_")))
1217 (while (string-match "\\s-" file-name
)
1218 (setq file-name
(replace-match s t t file-name
))))
1221 (defun mm-file-name-delete-control (filename)
1222 "Delete control characters from FILENAME."
1223 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1225 (defun mm-file-name-delete-gotchas (filename)
1226 "Delete shell gotchas from FILENAME."
1227 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1228 (gnus-replace-in-string filename
"^[.-]+" ""))
1230 (defun mm-save-part (handle &optional prompt
)
1231 "Write HANDLE to a file.
1232 PROMPT overrides the default one used to ask user for a file name."
1233 (let ((filename (or (mail-content-type-get
1234 (mm-handle-disposition handle
) 'filename
)
1235 (mail-content-type-get
1236 (mm-handle-type handle
) 'name
)))
1239 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1240 (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 (gnus-read-shell-command
1300 "Shell command on MIME part: " mm-last-shell-command
)))
1301 (mm-with-unibyte-buffer
1302 (mm-insert-part handle
)
1303 (mm-add-meta-html-tag handle
)
1304 (let ((coding-system-for-write 'binary
))
1305 (shell-command-on-region (point-min) (point-max) command nil
)))))
1307 (defun mm-interactively-view-part (handle)
1308 "Display HANDLE using METHOD."
1309 (let* ((type (mm-handle-media-type handle
))
1311 (mapcar (lambda (i) (list (cdr (assoc 'viewer i
))))
1312 (mailcap-mime-info type
'all
)))
1313 (method (let ((minibuffer-local-completion-map
1314 mm-viewer-completion-map
))
1315 (completing-read "Viewer: " methods
))))
1316 (when (string= method
"")
1317 (error "No method given"))
1318 (if (string-match "^[^% \t]+$" method
)
1319 (setq method
(concat method
" %s")))
1320 (mm-display-external handle method
)))
1322 (defun mm-preferred-alternative (handles &optional preferred
)
1323 "Say which of HANDLES are preferred."
1324 (let ((prec (if preferred
(list preferred
)
1325 (mm-preferred-alternative-precedence handles
)))
1326 p h result type handle
)
1327 (while (setq p
(pop prec
))
1330 (setq handle
(car h
))
1331 (setq type
(mm-handle-media-type handle
))
1332 (when (and (equal p type
)
1333 (mm-automatic-display-p handle
)
1334 (or (stringp (car handle
))
1335 (not (mm-handle-disposition handle
))
1336 (equal (car (mm-handle-disposition handle
))
1344 (defun mm-preferred-alternative-precedence (handles)
1345 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1346 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1348 (dolist (disc (reverse mm-discouraged-alternatives
))
1349 (dolist (elem (copy-sequence seq
))
1350 (when (string-match disc elem
)
1351 (setq seq
(nconc (delete elem seq
) (list elem
))))))
1354 (defun mm-get-content-id (id)
1355 "Return the handle(s) referred to by ID."
1356 (cdr (assoc id mm-content-id-alist
)))
1358 (defconst mm-image-type-regexps
1359 '(("/\\*.*XPM.\\*/" . xpm
)
1363 ("\211PNG\r\n" . png
)
1365 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1366 ("%!PS" . postscript
))
1367 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1368 When the first bytes of an image file match REGEXP, it is assumed to
1369 be of image type IMAGE-TYPE.")
1371 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1372 (defun mm-image-type-from-buffer ()
1373 "Determine the image type from data in the current buffer.
1374 Value is a symbol specifying the image type or nil if type cannot
1376 (let ((types mm-image-type-regexps
)
1378 (goto-char (point-min))
1379 (while (and types
(null type
))
1380 (let ((regexp (car (car types
)))
1381 (image-type (cdr (car types
))))
1382 (when (looking-at regexp
)
1383 (setq type image-type
))
1384 (setq types
(cdr types
))))
1387 (defun mm-get-image (handle)
1388 "Return an image instance based on HANDLE."
1389 (let ((type (mm-handle-media-subtype handle
))
1391 ;; Allow some common translations.
1394 ((equal type
"x-pixmap")
1396 ((equal type
"x-xbitmap")
1398 ((equal type
"x-portable-bitmap")
1401 (or (mm-handle-cache handle
)
1402 (mm-with-unibyte-buffer
1403 (mm-insert-part handle
)
1407 ;; Avoid testing `make-glyph' since W3 may define
1408 ;; a bogus version of it.
1409 (if (fboundp 'create-image
)
1410 (create-image (buffer-string)
1411 (or (mm-image-type-from-buffer)
1414 (mm-create-image-xemacs type
))))
1415 (mm-handle-set-cache handle spec
))))))
1417 (defun mm-create-image-xemacs (type)
1418 (when (featurep 'xemacs
)
1421 ;; xbm images require special handling, since
1422 ;; the only way to create glyphs from these
1423 ;; (without a ton of work) is to write them
1424 ;; out to a file, and then create a file
1426 (let ((file (mm-make-temp-file
1427 (expand-file-name "emm" mm-tmp-directory
)
1431 (write-region (point-min) (point-max) file
)
1432 (make-glyph (list (cons 'x file
))))
1434 (delete-file file
)))))
1438 (or (mm-image-type-from-buffer)
1440 :data
(buffer-string)))))))
1442 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1444 (defun mm-image-fit-p (handle)
1445 "Say whether the image in HANDLE will fit the current window."
1446 (let ((image (mm-get-image handle
)))
1448 (if (featurep 'xemacs
)
1449 ;; XEmacs' glyphs can actually tell us about their width, so
1450 ;; lets be nice and smart about them.
1451 (or mm-inline-large-images
1452 (and (<= (glyph-width image
) (window-pixel-width))
1453 (<= (glyph-height image
) (window-pixel-height))))
1454 (let* ((size (image-size image
))
1457 (or mm-inline-large-images
1458 (and (<= h
(1- (window-height))) ; Don't include mode line.
1459 (<= w
(window-width)))))))))
1461 (defun mm-valid-image-format-p (format)
1462 "Say whether FORMAT can be displayed natively by Emacs."
1465 ((fboundp 'valid-image-instantiator-format-p
)
1466 (valid-image-instantiator-format-p format
))
1468 ((fboundp 'image-type-available-p
)
1469 (and (display-graphic-p)
1470 (image-type-available-p format
)))
1471 ;; Nobody else can do images yet.
1475 (defun mm-valid-and-fit-image-p (format handle
)
1476 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1477 (and (mm-valid-image-format-p format
)
1478 (mm-image-fit-p handle
)))
1480 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1481 "Search in HANDLES for part with TYPE.
1482 If NOTP, returns first non-matching part.
1483 If RECURSIVE, search recursively."
1486 (if (and recursive
(stringp (caar handles
)))
1487 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1491 (not (equal (mm-handle-media-type (car handles
)) type
))
1492 (equal (mm-handle-media-type (car handles
)) type
))
1493 (setq handle
(car handles
)
1495 (setq handles
(cdr handles
)))
1498 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1499 (goto-char (point-min))
1500 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1502 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1504 (end (save-excursion
1505 (goto-char (point-max))
1506 (if (re-search-backward close-delimiter nil t
)
1510 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1511 (while (and (not result
)
1512 (re-search-forward boundary end t
))
1513 (goto-char (match-beginning 0))
1517 (narrow-to-region start
(1- (point)))
1518 (when (let* ((ct (mail-fetch-field "content-type"))
1519 (ctl (and ct
(mail-header-parse-content-type ct
))))
1521 (not (equal (car ctl
) type
))
1522 (equal (car ctl
) type
)))
1523 (setq result
(buffer-string))))))
1525 (setq start
(point)))
1526 (when (and (not result
) start
)
1529 (narrow-to-region start end
)
1530 (when (let* ((ct (mail-fetch-field "content-type"))
1531 (ctl (and ct
(mail-header-parse-content-type ct
))))
1533 (not (equal (car ctl
) type
))
1534 (equal (car ctl
) type
)))
1535 (setq result
(buffer-string))))))
1538 (defvar mm-security-handle nil
)
1540 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1541 ;; HANDLE could be a CTL.
1543 (put-text-property 0 (length (car handle
)) parameter value
1546 (autoload 'mm-view-pkcs7
"mm-view")
1548 (defun mm-possibly-verify-or-decrypt (parts ctl
)
1549 (let ((type (car ctl
))
1550 (subtype (cadr (split-string (car ctl
) "/")))
1551 (mm-security-handle ctl
) ;; (car CTL) is the type.
1552 protocol func functest
)
1554 ((or (equal type
"application/x-pkcs7-mime")
1555 (equal type
"application/pkcs7-mime"))
1558 ((eq mm-decrypt-option
'never
) nil
)
1559 ((eq mm-decrypt-option
'always
) t
)
1560 ((eq mm-decrypt-option
'known
) t
)
1562 (format "Decrypt (S/MIME) part? "))))
1563 (mm-view-pkcs7 parts
))
1564 (setq parts
(mm-dissect-buffer t
)))))
1565 ((equal subtype
"signed")
1566 (unless (and (setq protocol
1567 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1568 (not (equal protocol
"multipart/mixed")))
1569 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1570 (let ((protocols mm-verify-function-alist
))
1572 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1573 (funcall functest parts ctl
))
1574 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1575 (setq protocol
(caar protocols
)
1577 (setq protocols
(cdr protocols
))))))
1578 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1580 ((eq mm-verify-option
'never
) nil
)
1581 ((eq mm-verify-option
'always
) t
)
1582 ((eq mm-verify-option
'known
)
1584 (or (not (setq functest
1585 (nth 3 (assoc protocol
1586 mm-verify-function-alist
))))
1587 (funcall functest parts ctl
))))
1590 (format "Verify signed (%s) part? "
1591 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1592 (format "protocol=%s" protocol
))))))
1595 (setq parts
(funcall func parts ctl
))
1596 (mm-set-handle-multipart-parameter
1597 mm-security-handle
'gnus-details
1598 (format "Unknown sign protocol (%s)" protocol
))))))
1599 ((equal subtype
"encrypted")
1600 (unless (setq protocol
1601 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1602 ;; The message is broken.
1603 (let ((parts parts
))
1605 (if (assoc (mm-handle-media-type (car parts
))
1606 mm-decrypt-function-alist
)
1607 (setq protocol
(mm-handle-media-type (car parts
))
1609 (setq parts
(cdr parts
))))))
1610 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1612 ((eq mm-decrypt-option
'never
) nil
)
1613 ((eq mm-decrypt-option
'always
) t
)
1614 ((eq mm-decrypt-option
'known
)
1616 (or (not (setq functest
1617 (nth 3 (assoc protocol
1618 mm-decrypt-function-alist
))))
1619 (funcall functest parts ctl
))))
1622 (format "Decrypt (%s) part? "
1623 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1624 (format "protocol=%s" protocol
))))))
1627 (setq parts
(funcall func parts ctl
))
1628 (mm-set-handle-multipart-parameter
1629 mm-security-handle
'gnus-details
1630 (format "Unknown encrypt protocol (%s)" protocol
))))))
1634 (defun mm-multiple-handles (handles)
1635 (and (listp handles
)
1636 (> (length handles
) 1)
1637 (or (listp (car handles
))
1638 (stringp (car handles
)))))
1640 (defun mm-complicated-handles (handles)
1641 (and (listp (car handles
))
1642 (> (length handles
) 1)))
1644 (defun mm-merge-handles (handles1 handles2
)
1646 (if (listp (car handles1
))
1649 (if (listp (car handles2
))
1653 (defun mm-readable-p (handle)
1654 "Say whether the content of HANDLE is readable."
1655 (and (< (with-current-buffer (mm-handle-buffer handle
)
1656 (buffer-size)) 10000)
1657 (mm-with-unibyte-buffer
1658 (mm-insert-part handle
)
1659 (and (eq (mm-body-7-or-8) '7bit
)
1660 (not (mm-long-lines-p 76))))))
1662 (provide 'mm-decode
)
1664 ;; arch-tag: 4f35d360-56b8-4030-9388-3ed82d359b9b
1665 ;;; mm-decode.el ends here