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 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
))))
33 (eval-when-compile (require 'cl
)
36 (autoload 'gnus-map-function
"gnus-util")
37 (autoload 'gnus-replace-in-string
"gnus-util")
38 (autoload 'gnus-read-shell-command
"gnus-util")
40 (autoload 'mm-inline-partial
"mm-partial")
41 (autoload 'mm-inline-external-body
"mm-extern")
42 (autoload 'mm-extern-cache-contents
"mm-extern")
43 (autoload 'mm-insert-inline
"mm-view")
45 (defvar gnus-current-window-configuration
)
47 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
49 (defgroup mime-display
()
50 "Display of MIME in mail and news articles."
51 :link
'(custom-manual "(emacs-mime)Display Customization")
57 (defgroup mime-security
()
58 "MIME security in mail and news articles."
59 :link
'(custom-manual "(emacs-mime)Display Customization")
64 ;;; Convenience macros.
66 (defmacro mm-handle-buffer
(handle)
68 (defmacro mm-handle-type
(handle)
70 (defsubst mm-handle-media-type
(handle)
71 (if (stringp (car handle
))
73 (car (mm-handle-type handle
))))
74 (defsubst mm-handle-media-supertype
(handle)
75 (car (split-string (mm-handle-media-type handle
) "/")))
76 (defsubst mm-handle-media-subtype
(handle)
77 (cadr (split-string (mm-handle-media-type handle
) "/")))
78 (defmacro mm-handle-encoding
(handle)
80 (defmacro mm-handle-undisplayer
(handle)
82 (defmacro mm-handle-set-undisplayer
(handle function
)
83 `(setcar (nthcdr 3 ,handle
) ,function
))
84 (defmacro mm-handle-disposition
(handle)
86 (defmacro mm-handle-description
(handle)
88 (defmacro mm-handle-cache
(handle)
90 (defmacro mm-handle-set-cache
(handle contents
)
91 `(setcar (nthcdr 6 ,handle
) ,contents
))
92 (defmacro mm-handle-id
(handle)
94 (defmacro mm-handle-multipart-original-buffer
(handle)
95 `(get-text-property 0 'buffer
(car ,handle
)))
96 (defmacro mm-handle-multipart-from
(handle)
97 `(get-text-property 0 'from
(car ,handle
)))
98 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
99 `(get-text-property 0 ,parameter
(car ,handle
)))
101 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
102 disposition description cache
104 `(list ,buffer
,type
,encoding
,undisplayer
105 ,disposition
,description
,cache
,id
))
107 (defcustom mm-text-html-renderer
108 (cond ((executable-find "w3m")
109 (if (locate-library "w3m")
112 ((executable-find "links") 'links
)
113 ((executable-find "lynx") 'lynx
)
114 ((locate-library "w3") 'w3
)
115 ((locate-library "html2text") 'html2text
)
117 "Render of HTML contents.
118 It is one of defined renderer types, or a rendering function.
119 The defined renderer types are:
120 `w3m' : use emacs-w3m;
121 `w3m-standalone': use w3m;
125 `html2text' : use html2text;
126 nil : use external viewer (default web browser)."
127 :version
"23.0" ;; No Gnus
128 :type
'(choice (const w3
)
129 (const w3m
:tag
"emacs-w3m")
130 (const w3m-standalone
:tag
"standalone w3m" )
134 (const nil
:tag
"External viewer")
136 :group
'mime-display
)
138 (defvar mm-inline-text-html-renderer nil
139 "Function used for rendering inline HTML contents.
140 It is suggested to customize `mm-text-html-renderer' instead.")
142 (defcustom mm-inline-text-html-with-images nil
143 "If non-nil, Gnus will allow retrieving images in HTML contents with
144 the <img> tags. It has no effect on Emacs/w3. See also the
145 documentation for the `mm-w3m-safe-url-regexp' variable."
148 :group
'mime-display
)
150 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
151 "Regexp matching URLs which are considered to be safe.
152 Some HTML mails might contain a nasty trick used by spammers, using
153 the <img> tag which is far more evil than the [Click Here!] button.
154 It is most likely intended to check whether the ominous spam mail has
155 reached your eyes or not, in which case the spammer knows for sure
156 that your email address is valid. It is done by embedding an
157 identifier string into a URL that you might automatically retrieve
158 when displaying the image. The default value is \"\\\\`cid:\" which only
159 matches parts embedded to the Multipart/Related type MIME contents and
160 Gnus will never connect to the spammer's site arbitrarily. You may
161 set this variable to nil if you consider all urls to be safe."
163 :type
'(choice (regexp :tag
"Regexp")
164 (const :tag
"All URLs are safe" nil
))
165 :group
'mime-display
)
167 (defcustom mm-inline-text-html-with-w3m-keymap t
168 "If non-nil, use emacs-w3m command keys in the article buffer."
171 :group
'mime-display
)
173 (defcustom mm-enable-external t
174 "Indicate whether external MIME handlers should be used.
176 If t, all defined external MIME handlers are used. If nil, files are saved by
177 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
178 before the external MIME handler is invoked."
180 :type
'(choice (const :tag
"Always" t
)
181 (const :tag
"Never" nil
)
182 (const :tag
"Ask" ask
))
183 :group
'mime-display
)
185 (defcustom mm-inline-media-tests
189 (mm-valid-and-fit-image-p 'jpeg handle
)))
193 (mm-valid-and-fit-image-p 'png handle
)))
197 (mm-valid-and-fit-image-p 'gif handle
)))
201 (mm-valid-and-fit-image-p 'tiff handle
)) )
205 (mm-valid-and-fit-image-p 'xbm handle
)))
209 (mm-valid-and-fit-image-p 'xbm handle
)))
213 (mm-valid-and-fit-image-p 'xpm handle
)))
217 (mm-valid-and-fit-image-p 'xpm handle
)))
221 (mm-valid-and-fit-image-p 'bmp handle
)))
222 ("image/x-portable-bitmap"
225 (mm-valid-and-fit-image-p 'pbm handle
)))
226 ("text/plain" mm-inline-text identity
)
227 ("text/enriched" mm-inline-text identity
)
228 ("text/richtext" mm-inline-text identity
)
229 ("text/x-patch" mm-display-patch-inline
231 ;; If the diff-mode.el package is installed, the function is
232 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
233 ;; to cater to broken installations. OTOH checking the function
234 ;; makes it possible to install another package which provides an
235 ;; alternative implementation of diff-mode. --Stef
236 (fboundp 'diff-mode
)))
237 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
238 ("text/x-diff" mm-display-patch-inline
239 (lambda (handle) (fboundp 'diff-mode
)))
240 ("application/emacs-lisp" mm-display-elisp-inline identity
)
241 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
242 ("text/dns" mm-display-dns-inline identity
)
246 (or mm-inline-text-html-renderer
247 mm-text-html-renderer
)))
251 (or (featurep 'vcard
)
252 (locate-library "vcard"))))
253 ("message/delivery-status" mm-inline-text identity
)
254 ("message/rfc822" mm-inline-message identity
)
255 ("message/partial" mm-inline-partial identity
)
256 ("message/external-body" mm-inline-external-body identity
)
257 ("text/.*" mm-inline-text identity
)
258 ("audio/wav" mm-inline-audio
260 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
261 (device-sound-enabled-p))))
265 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
266 (device-sound-enabled-p))))
267 ("application/pgp-signature" ignore identity
)
268 ("application/x-pkcs7-signature" ignore identity
)
269 ("application/pkcs7-signature" ignore identity
)
270 ("application/x-pkcs7-mime" ignore identity
)
271 ("application/pkcs7-mime" ignore identity
)
272 ("multipart/alternative" ignore identity
)
273 ("multipart/mixed" ignore identity
)
274 ("multipart/related" ignore identity
)
275 ;; Disable audio and image
276 ("audio/.*" ignore ignore
)
277 ("image/.*" ignore ignore
)
278 ;; Default to displaying as text
279 (".*" mm-inline-text mm-readable-p
))
280 "Alist of media types/tests saying whether types can be displayed inline."
281 :type
'(repeat (list (regexp :tag
"MIME type")
282 (function :tag
"Display function")
283 (function :tag
"Display test")))
284 :group
'mime-display
)
286 (defcustom mm-inlined-types
287 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
288 "message/partial" "message/external-body" "application/emacs-lisp"
289 "application/x-emacs-lisp"
290 "application/pgp-signature" "application/x-pkcs7-signature"
291 "application/pkcs7-signature" "application/x-pkcs7-mime"
292 "application/pkcs7-mime"
293 ;; Mutt still uses this even though it has already been withdrawn.
295 "List of media types that are to be displayed inline.
296 See also `mm-inline-media-tests', which says how to display a media
298 :type
'(repeat regexp
)
299 :group
'mime-display
)
301 (defcustom mm-keep-viewer-alive-types
302 '("application/postscript" "application/msword" "application/vnd.ms-excel"
303 "application/pdf" "application/x-dvi")
304 "List of media types for which the external viewer will not be killed
305 when selecting a different article."
307 :type
'(repeat regexp
)
308 :group
'mime-display
)
310 (defcustom mm-automatic-display
311 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
312 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
313 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
314 "application/emacs-lisp" "application/x-emacs-lisp"
315 "application/x-pkcs7-signature"
316 "application/pkcs7-signature" "application/x-pkcs7-mime"
317 "application/pkcs7-mime"
318 ;; Mutt still uses this even though it has already been withdrawn.
319 "application/pgp\\'")
320 "A list of MIME types to be displayed automatically."
321 :type
'(repeat regexp
)
322 :group
'mime-display
)
324 (defcustom mm-attachment-override-types
'("text/x-vcard"
325 "application/pkcs7-mime"
326 "application/x-pkcs7-mime"
327 "application/pkcs7-signature"
328 "application/x-pkcs7-signature")
329 "Types to have \"attachment\" ignored if they can be displayed inline."
330 :type
'(repeat regexp
)
331 :group
'mime-display
)
333 (defcustom mm-inline-override-types nil
334 "Types to be treated as attachments even if they can be displayed inline."
335 :type
'(repeat regexp
)
336 :group
'mime-display
)
338 (defcustom mm-automatic-external-display nil
339 "List of MIME type regexps that will be displayed externally automatically."
340 :type
'(repeat regexp
)
341 :group
'mime-display
)
343 (defcustom mm-discouraged-alternatives nil
344 "List of MIME types that are discouraged when viewing multipart/alternative.
345 Viewing agents are supposed to view the last possible part of a message,
346 as that is supposed to be the richest. However, users may prefer other
347 types instead, and this list says what types are most unwanted. If,
348 for instance, text/html parts are very unwanted, and text/richtext are
349 somewhat unwanted, then the value of this variable should be set
352 (\"text/html\" \"text/richtext\")
354 Adding \"image/.*\" might also be useful. Spammers use it as the
355 prefered part of multipart/alternative messages. See also
356 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
357 enables you to choose manually one of two types those mails include."
358 :type
'(repeat regexp
) ;; See `mm-preferred-alternative-precedence'.
359 :group
'mime-display
)
361 (defcustom mm-tmp-directory
362 (if (fboundp 'temp-directory
)
364 (if (boundp 'temporary-file-directory
)
365 temporary-file-directory
367 "Where mm will store its temporary files."
369 :group
'mime-display
)
371 (defcustom mm-inline-large-images nil
372 "If non-nil, then all images fit in the buffer."
374 :group
'mime-display
)
376 (defcustom mm-file-name-rewrite-functions
377 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
378 "List of functions used for rewriting file names of MIME parts.
379 Each function takes a file name as input and returns a file name.
381 Ready-made functions include `mm-file-name-delete-control',
382 `mm-file-name-delete-gotchas' (you should not remove these two
383 functions), `mm-file-name-delete-whitespace',
384 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
385 `mm-file-name-replace-whitespace', `capitalize', `downcase',
386 `upcase', and `upcase-initials'."
387 :type
'(list (set :inline t
388 (const mm-file-name-delete-control
)
389 (const mm-file-name-delete-gotchas
)
390 (const mm-file-name-delete-whitespace
)
391 (const mm-file-name-trim-whitespace
)
392 (const mm-file-name-collapse-whitespace
)
393 (const mm-file-name-replace-whitespace
)
397 (const upcase-initials
)
401 :version
"23.1" ;; No Gnus
402 :group
'mime-display
)
405 (defvar mm-path-name-rewrite-functions nil
406 "*List of functions for rewriting the full file names of MIME parts.
407 This is used when viewing parts externally, and is meant for
408 transforming the absolute name so that non-compliant programs can find
409 the file where it's saved.
411 Each function takes a file name as input and returns a file name.")
413 (defvar mm-file-name-replace-whitespace nil
414 "String used for replacing whitespace characters; default is `\"_\"'.")
416 (defcustom mm-default-directory nil
417 "The default directory where mm will save files.
418 If not set, `default-directory' will be used."
419 :type
'(choice directory
(const :tag
"Default" nil
))
420 :group
'mime-display
)
422 (defcustom mm-attachment-file-modes
384
423 "Set the mode bits of saved attachments to this integer."
426 :group
'mime-display
)
428 (defcustom mm-external-terminal-program
"xterm"
429 "The program to start an external terminal."
432 :group
'mime-display
)
434 ;;; Internal variables.
436 (defvar mm-last-shell-command
"")
437 (defvar mm-content-id-alist nil
)
438 (defvar mm-postponed-undisplay-list nil
)
440 ;; According to RFC2046, in particular, in a digest, the default
441 ;; Content-Type value for a body part is changed from "text/plain" to
443 (defvar mm-dissect-default-type
"text/plain")
445 (autoload 'mml2015-verify
"mml2015")
446 (autoload 'mml2015-verify-test
"mml2015")
447 (autoload 'mml-smime-verify
"mml-smime")
448 (autoload 'mml-smime-verify-test
"mml-smime")
450 (defvar mm-verify-function-alist
451 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
452 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
453 mm-uu-pgp-signed-test
)
454 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
455 mml-smime-verify-test
)
456 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
457 mml-smime-verify-test
)))
459 (defcustom mm-verify-option
'never
460 "Option of verifying signed parts.
461 `never', not verify; `always', always verify;
462 `known', only verify known protocols. Otherwise, ask user.
464 When set to `always' or `known', you should add
465 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
466 result of the verification."
468 :type
'(choice (item always
)
470 (item :tag
"only known protocols" known
)
471 (item :tag
"ask" nil
))
472 :group
'mime-security
)
474 (autoload 'mml2015-decrypt
"mml2015")
475 (autoload 'mml2015-decrypt-test
"mml2015")
477 (defvar mm-decrypt-function-alist
478 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
479 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
480 mm-uu-pgp-encrypted-test
)))
482 (defcustom mm-decrypt-option nil
483 "Option of decrypting encrypted parts.
484 `never', not decrypt; `always', always decrypt;
485 `known', only decrypt known protocols. Otherwise, ask user."
487 :type
'(choice (item always
)
489 (item :tag
"only known protocols" known
)
490 (item :tag
"ask" nil
))
491 :group
'mime-security
)
493 (defvar mm-viewer-completion-map
494 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
495 (set-keymap-parent map minibuffer-local-completion-map
)
496 ;; Should we bind other key to minibuffer-complete-word?
497 (define-key map
" " 'self-insert-command
)
499 "Keymap for input viewer with completion.")
501 (defvar mm-viewer-completion-map
502 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
503 (set-keymap-parent map minibuffer-local-completion-map
)
504 ;; Should we bind other key to minibuffer-complete-word?
505 (define-key map
" " 'self-insert-command
)
507 "Keymap for input viewer with completion.")
511 (defun mm-alist-to-plist (alist)
512 "Convert association list ALIST into the equivalent property-list form.
513 The plist is returned. This converts from
515 \((a . 1) (b . 2) (c . 3))
521 The original alist is not modified. See also `destructive-alist-to-plist'."
524 (let ((el (car alist
)))
525 (setq plist
(cons (cdr el
) (cons (car el
) plist
))))
526 (setq alist
(cdr alist
)))
529 (defun mm-keep-viewer-alive-p (handle)
530 "Say whether external viewer for HANDLE should stay alive."
531 (let ((types mm-keep-viewer-alive-types
)
532 (type (mm-handle-media-type handle
))
535 (while (setq ty
(pop types
))
536 (when (string-match ty type
)
537 (throw 'found t
))))))
539 (defun mm-handle-set-external-undisplayer (handle function
)
540 "Set the undisplayer for HANDLE to FUNCTION.
541 Postpone undisplaying of viewers for types in
542 `mm-keep-viewer-alive-types'."
543 (if (mm-keep-viewer-alive-p handle
)
544 (let ((new-handle (copy-sequence handle
)))
545 (mm-handle-set-undisplayer new-handle function
)
546 (mm-handle-set-undisplayer handle nil
)
547 (push new-handle mm-postponed-undisplay-list
))
548 (mm-handle-set-undisplayer handle function
)))
550 (defun mm-destroy-postponed-undisplay-list ()
551 (when mm-postponed-undisplay-list
552 (message "Destroying external MIME viewers")
553 (mm-destroy-parts mm-postponed-undisplay-list
)))
555 (autoload 'message-fetch-field
"message")
557 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
558 "Dissect the current buffer and return a list of MIME handles."
560 (let (ct ctl type subtype cte cd description id result
)
562 (mail-narrow-to-head)
563 (when (or no-strict-mime
565 (mail-fetch-field "mime-version"))
566 (setq ct
(mail-fetch-field "content-type")
567 ctl
(and ct
(mail-header-parse-content-type ct
))
568 cte
(mail-fetch-field "content-transfer-encoding")
569 cd
(mail-fetch-field "content-disposition")
570 ;; Newlines in description should be stripped so as
571 ;; not to break the MIME tag into two or more lines.
572 description
(message-fetch-field "content-description")
573 id
(mail-fetch-field "content-id"))
575 (setq from
(mail-fetch-field "from")))
576 ;; FIXME: In some circumstances, this code is running within
577 ;; an unibyte macro. mail-extract-address-components
578 ;; creates unibyte buffers. This `if', though not a perfect
579 ;; solution, avoids most of them.
581 (setq from
(cadr (mail-extract-address-components from
))))
583 (setq description
(mail-decode-encoded-word-string
586 (not (string-match "/" (car ctl
))))
587 (mm-dissect-singlepart
588 (list mm-dissect-default-type
)
589 (and cte
(intern (downcase (mail-header-strip cte
))))
591 (and cd
(mail-header-parse-content-disposition cd
))
593 (setq type
(split-string (car ctl
) "/"))
594 (setq subtype
(cadr type
)
599 ((equal type
"multipart")
600 (let ((mm-dissect-default-type (if (equal subtype
"digest")
603 (start (cdr (assq 'start
(cdr ctl
)))))
604 (add-text-properties 0 (length (car ctl
))
605 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
607 ;; what really needs to be done here is a way to link a
608 ;; MIME handle back to it's parent MIME handle (in a multilevel
609 ;; MIME article). That would probably require changing
610 ;; the mm-handle API so we simply store the multipart buffer
611 ;; name as a text property of the "multipart/whatever" string.
612 (add-text-properties 0 (length (car ctl
))
613 (list 'buffer
(mm-copy-to-buffer)
617 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
619 (mm-possibly-verify-or-decrypt
620 (mm-dissect-singlepart
622 (and cte
(intern (downcase (mail-header-strip cte
))))
624 (and cd
(mail-header-parse-content-disposition cd
))
628 (when (string-match " *<\\(.*\\)> *" id
)
629 (setq id
(match-string 1 id
)))
630 (push (cons id result
) mm-content-id-alist
))
633 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
635 (if (equal "text/plain" (car ctl
))
639 (mm-copy-to-buffer) ctl cte nil cdl description nil id
)))
641 (defun mm-dissect-multipart (ctl from
)
642 (goto-char (point-min))
643 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
644 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
647 (goto-char (point-max))
648 (if (re-search-backward close-delimiter nil t
)
651 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
652 (while (and (< (point) end
) (re-search-forward boundary end t
))
653 (goto-char (match-beginning 0))
657 (narrow-to-region start
(point))
658 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
660 (or (looking-at boundary
)
662 (setq start
(point)))
663 (when (and start
(< start end
))
666 (narrow-to-region start end
)
667 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
668 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl
)))
670 (defun mm-copy-to-buffer ()
671 "Copy the contents of the current buffer to a fresh buffer."
672 (let ((obuf (current-buffer))
673 (mb (mm-multibyte-p))
675 (goto-char (point-min))
676 (search-forward-regexp "^\n" nil t
)
679 (generate-new-buffer " *mm*")
680 ;; Preserve the data's unibyteness (for url-insert-file-contents).
681 (mm-set-buffer-multibyte mb
)
682 (insert-buffer-substring obuf beg
)
685 (defun mm-display-parts (handle &optional no-default
)
686 (if (stringp (car handle
))
687 (mapcar 'mm-display-parts
(cdr handle
))
688 (if (bufferp (car handle
))
690 (narrow-to-region (point) (point))
691 (mm-display-part handle
)
692 (goto-char (point-max)))
693 (mapcar 'mm-display-parts handle
))))
695 (autoload 'mailcap-parse-mailcaps
"mailcap")
696 (autoload 'mailcap-mime-info
"mailcap")
698 (defun mm-display-part (handle &optional no-default
)
699 "Display the MIME part represented by HANDLE.
700 Returns nil if the part is removed; inline if displayed inline;
701 external if displayed external."
703 (mailcap-parse-mailcaps)
704 (if (mm-handle-displayed-p handle
)
705 (mm-remove-part handle
)
706 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
707 "message/external-body")
709 (unless (mm-handle-cache handle
)
710 (mm-extern-cache-contents handle
))
711 (mm-handle-cache handle
))
713 (type (mm-handle-media-type ehandle
))
714 (method (mailcap-mime-info type
))
715 (filename (or (mail-content-type-get
716 (mm-handle-disposition handle
) 'filename
)
717 (mail-content-type-get
718 (mm-handle-type handle
) 'name
)
720 (external mm-enable-external
))
721 (if (and (mm-inlinable-p ehandle
)
722 (mm-inlined-p ehandle
))
725 (mm-display-inline handle
)
729 (if (and (not method
)
730 (equal "text" (car (split-string type
"/"))))
733 (mm-insert-inline handle
(mm-get-part handle
))
736 (and method
;; If nil, we always use "save".
737 (stringp method
) ;; 'mailcap-save-binary-file
738 (or (eq mm-enable-external t
)
739 (and (eq mm-enable-external
'ask
)
742 "Display part (" type
743 ") using external program"
744 ;; Can non-string method ever happen?
747 " \"" (format method filename
) "\"")
752 handle
(or method
'mailcap-save-binary-file
))
754 handle
'mailcap-save-binary-file
)))))))))
756 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
757 (defvar mailcap-mime-extensions
) ; mailcap-mime-info autoloads
759 (defun mm-display-external (handle method
)
760 "Display HANDLE using METHOD."
761 (let ((outbuf (current-buffer)))
762 (mm-with-unibyte-buffer
763 (if (functionp method
)
764 (let ((cur (current-buffer)))
765 (if (eq method
'mailcap-save-binary-file
)
767 (set-buffer (generate-new-buffer " *mm*"))
769 (mm-insert-part handle
)
770 (mm-add-meta-html-tag handle
)
771 (let ((win (get-buffer-window cur t
)))
773 (select-window win
)))
774 (switch-to-buffer (generate-new-buffer " *mm*")))
775 (buffer-disable-undo)
776 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
777 (insert-buffer-substring cur
)
778 (goto-char (point-min))
780 (message "Viewing with %s" method
))
781 (let ((mm (current-buffer))
782 (non-viewer (assq 'non-viewer
784 (mm-handle-media-type handle
) t
))))
788 (mm-save-part handle
))
789 (when (and (not non-viewer
)
791 (mm-handle-set-undisplayer handle mm
)))))
792 ;; The function is a string to be executed.
793 (mm-insert-part handle
)
794 (mm-add-meta-html-tag handle
)
795 (let* ((dir (mm-make-temp-file
796 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
798 (mail-content-type-get
799 (mm-handle-disposition handle
) 'filename
)
800 (mail-content-type-get
801 (mm-handle-type handle
) 'name
)))
802 (mime-info (mailcap-mime-info
803 (mm-handle-media-type handle
) t
))
804 (needsterm (or (assoc "needsterm" mime-info
)
805 (assoc "needsterminal" mime-info
)))
806 (copiousoutput (assoc "copiousoutput" mime-info
))
808 ;; We create a private sub-directory where we store our files.
809 (set-file-modes dir
#o700
)
811 (setq file
(expand-file-name
812 (gnus-map-function mm-file-name-rewrite-functions
813 (file-name-nondirectory filename
))
815 ;; Use nametemplate (defined in RFC1524) if it is specified
817 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
819 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
820 (setq suffix
(match-string 1 suffix
))
821 ;; Otherwise, use a suffix according to
822 ;; `mailcap-mime-extensions'.
823 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
824 mailcap-mime-extensions
))))
825 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
827 (let ((coding-system-for-write mm-binary-coding-system
))
828 (write-region (point-min) (point-max) file nil
'nomesg
))
829 ;; The file is deleted after the viewer exists. If the users edits
830 ;; the file, changes will be lost. Set file to read-only to make it
832 (set-file-modes file
#o400
)
833 (message "Viewing with %s" method
)
836 (let ((command (mm-mailcap-command
837 method file
(mm-handle-type handle
))))
840 (start-process "*display*" nil
841 mm-external-terminal-program
843 shell-command-switch command
)
851 shell-command-switch command
)))
854 (set-process-sentinel
855 (get-buffer-process buffer
)
856 `(lambda (process state
)
857 (if (eq 'exit
(process-status process
))
858 (gnus-configure-windows
859 ',gnus-current-window-configuration
))))
860 (gnus-configure-windows 'display-term
))
861 (mm-handle-set-external-undisplayer handle
(cons file buffer
)))
862 (message "Displaying %s..." command
))
865 (with-current-buffer outbuf
871 (call-process shell-file-name nil
873 (generate-new-buffer " *mm*"))
877 method file
(mm-handle-type handle
)))
878 (if (buffer-live-p buffer
)
879 (with-current-buffer buffer
882 (ignore-errors (delete-file file
))
883 (ignore-errors (delete-directory
884 (file-name-directory file
)))
885 (ignore-errors (kill-buffer buffer
))))))
888 ;; Deleting the temp file should be postponed for some wrappers,
889 ;; shell scripts, and so on, which might exit right after having
890 ;; started a viewer command as a background job.
891 (let ((command (mm-mailcap-command
892 method file
(mm-handle-type handle
))))
895 (start-process "*display*"
897 (generate-new-buffer " *mm*"))
899 shell-command-switch command
)
900 (set-process-sentinel
901 (get-buffer-process buffer
)
902 (lexical-let ;; Don't use `let'.
903 ;; Function used to remove temp file and directory.
905 ;; Don't use `ignore-errors'.
911 ,(file-name-directory file
))
913 ;; Form uses to kill the process buffer and
914 ;; remove the undisplayer.
916 (kill-buffer ,buffer
)
918 (list 'mm-handle-set-undisplayer
921 ;; Message to be issued when the process exits.
922 (done (format "Displaying %s...done" command
))
923 ;; In particular, the timer object (which is
924 ;; a vector in Emacs but is a list in XEmacs)
925 ;; requires that it is lexically scoped.
926 (timer (run-at-time 2.0 nil
'ignore
)))
927 (if (featurep 'xemacs
)
928 (lambda (process state
)
929 (when (eq 'exit
(process-status process
))
930 (if (memq timer itimer-list
)
931 (set-itimer-function timer fn
)
933 (ignore-errors (eval fm
))
934 (message "%s" done
)))
935 (lambda (process state
)
936 (when (eq 'exit
(process-status process
))
937 (if (memq timer timer-list
)
938 (timer-set-function timer fn
)
940 (ignore-errors (eval fm
))
941 (message "%s" done
)))))))
942 (mm-handle-set-external-undisplayer
943 handle
(cons file buffer
)))
944 (message "Displaying %s..." command
))
947 (defun mm-mailcap-command (method file type-list
)
948 (let ((ctl (cdr type-list
))
952 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
954 (push (substring method beg
(match-beginning 0)) out
)
955 (setq beg
(match-end 0)
956 total
(match-string 0 method
)
957 sub
(match-string 1 method
))
959 ((string= total
"%%")
961 ((or (string= total
"%s")
962 ;; We do our own quoting.
963 (string= total
"'%s'")
964 (string= total
"\"%s\""))
965 (setq uses-stdin nil
)
966 (push (shell-quote-argument
967 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
968 ((string= total
"%t")
969 (push (shell-quote-argument (car type-list
)) out
))
971 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
972 (push (substring method beg
(length method
)) out
)
975 (push (shell-quote-argument
976 (gnus-map-function mm-path-name-rewrite-functions file
))
978 (mapconcat 'identity
(nreverse out
) "")))
980 (defun mm-remove-parts (handles)
981 "Remove the displayed MIME parts represented by HANDLES."
982 (if (and (listp handles
)
983 (bufferp (car handles
)))
984 (mm-remove-part handles
)
986 (while (setq handle
(pop handles
))
989 (when (buffer-live-p (get-text-property 0 'buffer handle
))
990 (kill-buffer (get-text-property 0 'buffer handle
))))
992 (stringp (car handle
)))
993 (mm-remove-parts (cdr handle
)))
995 (mm-remove-part handle
)))))))
997 (defun mm-destroy-parts (handles)
998 "Remove the displayed MIME parts represented by HANDLES."
999 (if (and (listp handles
)
1000 (bufferp (car handles
)))
1001 (mm-destroy-part handles
)
1003 (while (setq handle
(pop handles
))
1006 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1007 (kill-buffer (get-text-property 0 'buffer handle
))))
1008 ((and (listp handle
)
1009 (stringp (car handle
)))
1010 (mm-destroy-parts handle
))
1012 (mm-destroy-part handle
)))))))
1014 (defun mm-remove-part (handle)
1015 "Remove the displayed MIME part represented by HANDLE."
1016 (when (listp handle
)
1017 (let ((object (mm-handle-undisplayer handle
)))
1020 ;; Internally displayed part.
1021 ((mm-annotationp object
)
1022 (if (featurep 'xemacs
)
1023 (delete-annotation object
)))
1024 ((or (functionp object
)
1026 (eq (car object
) 'lambda
)))
1028 ;; Externally displayed part.
1031 (while (get-buffer-process (cdr object
))
1032 (interrupt-process (get-buffer-process (cdr object
)))
1033 (message "Waiting for external displayer to die...")
1037 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1038 (message "Waiting for external displayer to die...done")
1039 (ignore-errors (delete-file (car object
)))
1040 (ignore-errors (delete-directory (file-name-directory
1043 (when (buffer-live-p object
)
1044 (kill-buffer object
)))))
1045 (mm-handle-set-undisplayer handle nil
))))
1047 (defun mm-display-inline (handle)
1048 (let* ((type (mm-handle-media-type handle
))
1049 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1050 (funcall function handle
)
1051 (goto-char (point-min))))
1053 (defun mm-assoc-string-match (alist type
)
1054 (dolist (elem alist
)
1055 (when (string-match (car elem
) type
)
1058 (defun mm-automatic-display-p (handle)
1059 "Say whether the user wants HANDLE to be displayed automatically."
1060 (let ((methods mm-automatic-display
)
1061 (type (mm-handle-media-type handle
))
1063 (while (setq method
(pop methods
))
1064 (when (and (not (mm-inline-override-p handle
))
1065 (string-match method type
))
1070 (defun mm-inlinable-p (handle &optional type
)
1071 "Say whether HANDLE can be displayed inline.
1072 TYPE is the mime-type of the object; it defaults to the one given
1074 (unless type
(setq type
(mm-handle-media-type handle
)))
1075 (let ((alist mm-inline-media-tests
)
1078 (when (string-match (caar alist
) type
)
1079 (setq test
(caddar alist
)
1081 (setq test
(funcall test handle
)))
1085 (defun mm-inlined-p (handle)
1086 "Say whether the user wants HANDLE to be displayed inline."
1087 (let ((methods mm-inlined-types
)
1088 (type (mm-handle-media-type handle
))
1090 (while (setq method
(pop methods
))
1091 (when (and (not (mm-inline-override-p handle
))
1092 (string-match method type
))
1097 (defun mm-attachment-override-p (handle)
1098 "Say whether HANDLE should have attachment behavior overridden."
1099 (let ((types mm-attachment-override-types
)
1100 (type (mm-handle-media-type handle
))
1103 (while (setq ty
(pop types
))
1104 (when (and (string-match ty type
)
1105 (mm-inlinable-p handle
))
1106 (throw 'found t
))))))
1108 (defun mm-inline-override-p (handle)
1109 "Say whether HANDLE should have inline behavior overridden."
1110 (let ((types mm-inline-override-types
)
1111 (type (mm-handle-media-type handle
))
1114 (while (setq ty
(pop types
))
1115 (when (string-match ty type
)
1116 (throw 'found t
))))))
1118 (defun mm-automatic-external-display-p (type)
1119 "Return the user-defined method for TYPE."
1120 (let ((methods mm-automatic-external-display
)
1122 (while (setq method
(pop methods
))
1123 (when (string-match method type
)
1128 (defun mm-destroy-part (handle)
1129 "Destroy the data structures connected to HANDLE."
1130 (when (listp handle
)
1131 (mm-remove-part handle
)
1132 (when (buffer-live-p (mm-handle-buffer handle
))
1133 (kill-buffer (mm-handle-buffer handle
)))))
1135 (defun mm-handle-displayed-p (handle)
1136 "Say whether HANDLE is displayed or not."
1137 (mm-handle-undisplayer handle
))
1140 ;;; Functions for outputting parts
1143 (defmacro mm-with-part
(handle &rest forms
)
1144 "Run FORMS in the temp buffer containing the contents of HANDLE."
1145 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1146 ;; chars, so the buffer should be unibyte. It may happen that the
1147 ;; handle-buffer is multibyte for some reason, in which case now is a good
1148 ;; time to adjust it, since we know at this point that it should
1150 `(let* ((handle ,handle
))
1152 (mm-disable-multibyte)
1153 (insert-buffer-substring (mm-handle-buffer handle
))
1154 (mm-decode-content-transfer-encoding
1155 (mm-handle-encoding handle
)
1156 (mm-handle-media-type handle
))
1158 (put 'mm-with-part
'lisp-indent-function
1)
1159 (put 'mm-with-part
'edebug-form-spec
'(body))
1161 (defun mm-get-part (handle &optional no-cache
)
1162 "Return the contents of HANDLE as a string.
1163 If NO-CACHE is non-nil, cached contents of a message/external-body part
1165 (if (and (not no-cache
)
1166 (equal (mm-handle-media-type handle
) "message/external-body"))
1168 (unless (mm-handle-cache handle
)
1169 (mm-extern-cache-contents handle
))
1170 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1172 (mm-with-part handle
1175 (defun mm-insert-part (handle &optional no-cache
)
1176 "Insert the contents of HANDLE in the current buffer.
1177 If NO-CACHE is non-nil, cached contents of a message/external-body part
1179 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1182 (with-current-buffer (mm-handle-buffer handle
)
1185 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1187 (mm-get-part handle no-cache
)))))
1193 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1195 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1196 ;; Separate the extracted parts that have the same faces.
1200 (defun mm-file-name-delete-whitespace (file-name)
1201 "Remove all whitespace characters from FILE-NAME."
1202 (while (string-match "\\s-+" file-name
)
1203 (setq file-name
(replace-match "" t t file-name
)))
1206 (defun mm-file-name-trim-whitespace (file-name)
1207 "Remove leading and trailing whitespace characters from FILE-NAME."
1208 (when (string-match "\\`\\s-+" file-name
)
1209 (setq file-name
(substring file-name
(match-end 0))))
1210 (when (string-match "\\s-+\\'" file-name
)
1211 (setq file-name
(substring file-name
0 (match-beginning 0))))
1214 (defun mm-file-name-collapse-whitespace (file-name)
1215 "Collapse multiple whitespace characters in FILE-NAME."
1216 (while (string-match "\\s-\\s-+" file-name
)
1217 (setq file-name
(replace-match " " t t file-name
)))
1220 (defun mm-file-name-replace-whitespace (file-name)
1221 "Replace whitespace characters in FILE-NAME with underscores.
1222 Set the option `mm-file-name-replace-whitespace' to any other
1223 string if you do not like underscores."
1224 (let ((s (or mm-file-name-replace-whitespace
"_")))
1225 (while (string-match "\\s-" file-name
)
1226 (setq file-name
(replace-match s t t file-name
))))
1229 (defun mm-file-name-delete-control (filename)
1230 "Delete control characters from FILENAME."
1231 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1233 (defun mm-file-name-delete-gotchas (filename)
1234 "Delete shell gotchas from FILENAME."
1235 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1236 (gnus-replace-in-string filename
"^[.-]+" ""))
1238 (defun mm-save-part (handle &optional prompt
)
1239 "Write HANDLE to a file.
1240 PROMPT overrides the default one used to ask user for a file name."
1241 (let ((filename (or (mail-content-type-get
1242 (mm-handle-disposition handle
) 'filename
)
1243 (mail-content-type-get
1244 (mm-handle-type handle
) 'name
)))
1247 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1248 (file-name-nondirectory filename
))))
1250 (read-file-name (or prompt
"Save MIME part to: ")
1251 (or mm-default-directory default-directory
)
1252 nil nil
(or filename
"")))
1253 (setq mm-default-directory
(file-name-directory file
))
1254 (and (or (not (file-exists-p file
))
1255 (yes-or-no-p (format "File %s already exists; overwrite? "
1258 (mm-save-part-to-file handle file
)
1261 (defun mm-add-meta-html-tag (handle &optional charset force-charset
)
1262 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1263 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1264 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1265 Return t if meta tag is added or replaced."
1266 (when (equal (mm-handle-media-type handle
) "text/html")
1268 (setq charset
(mail-content-type-get (mm-handle-type handle
)
1270 (setq charset
(format "\
1271 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset
))
1272 (let ((case-fold-search t
))
1273 (goto-char (point-min))
1274 (if (re-search-forward "\
1275 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1276 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t
)
1277 (if (and (not force-charset
)
1279 (string-match "\\`html\\'" (match-string 1)))
1280 ;; Don't modify existing meta tag.
1282 ;; Replace it with the one specifying charset.
1283 (replace-match charset
)
1285 (if (re-search-forward "<head>\\s-*" nil t
)
1286 (insert charset
"\n")
1287 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t
)
1288 (insert "<head>\n" charset
"\n</head>\n"))
1291 (defun mm-save-part-to-file (handle file
)
1292 (mm-with-unibyte-buffer
1293 (mm-insert-part handle
)
1294 (mm-add-meta-html-tag handle
)
1295 (let ((current-file-modes (default-file-modes)))
1296 (set-default-file-modes mm-attachment-file-modes
)
1298 ;; Don't re-compress .gz & al. Arguably we should make
1299 ;; `file-name-handler-alist' nil, but that would chop
1300 ;; ange-ftp, which is reasonable to use here.
1301 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1302 (set-default-file-modes current-file-modes
)))))
1304 (defun mm-pipe-part (handle &optional cmd
)
1305 "Pipe HANDLE to a process.
1306 Use CMD as the process."
1307 (let ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
1309 (gnus-read-shell-command
1310 "Shell command on MIME part: " mm-last-shell-command
))))
1311 (mm-with-unibyte-buffer
1312 (mm-insert-part handle
)
1313 (mm-add-meta-html-tag handle
)
1314 (let ((coding-system-for-write 'binary
))
1315 (shell-command-on-region (point-min) (point-max) command nil
)))))
1317 (defun mm-interactively-view-part (handle)
1318 "Display HANDLE using METHOD."
1319 (let* ((type (mm-handle-media-type handle
))
1321 (mapcar (lambda (i) (list (cdr (assoc 'viewer i
))))
1322 (mailcap-mime-info type
'all
)))
1323 (method (let ((minibuffer-local-completion-map
1324 mm-viewer-completion-map
))
1325 (completing-read "Viewer: " methods
))))
1326 (when (string= method
"")
1327 (error "No method given"))
1328 (if (string-match "^[^% \t]+$" method
)
1329 (setq method
(concat method
" %s")))
1330 (mm-display-external handle method
)))
1332 (defun mm-preferred-alternative (handles &optional preferred
)
1333 "Say which of HANDLES are preferred."
1334 (let ((prec (if preferred
(list preferred
)
1335 (mm-preferred-alternative-precedence handles
)))
1336 p h result type handle
)
1337 (while (setq p
(pop prec
))
1340 (setq handle
(car h
))
1341 (setq type
(mm-handle-media-type handle
))
1342 (when (and (equal p type
)
1343 (mm-automatic-display-p handle
)
1344 (or (stringp (car handle
))
1345 (not (mm-handle-disposition handle
))
1346 (equal (car (mm-handle-disposition handle
))
1354 (defun mm-preferred-alternative-precedence (handles)
1355 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1356 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1358 (dolist (disc (reverse mm-discouraged-alternatives
))
1359 (dolist (elem (copy-sequence seq
))
1360 (when (string-match disc elem
)
1361 (setq seq
(nconc (delete elem seq
) (list elem
))))))
1364 (defun mm-get-content-id (id)
1365 "Return the handle(s) referred to by ID."
1366 (cdr (assoc id mm-content-id-alist
)))
1368 (defconst mm-image-type-regexps
1369 '(("/\\*.*XPM.\\*/" . xpm
)
1373 ("\211PNG\r\n" . png
)
1375 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1376 ("%!PS" . postscript
))
1377 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1378 When the first bytes of an image file match REGEXP, it is assumed to
1379 be of image type IMAGE-TYPE.")
1381 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1382 (defun mm-image-type-from-buffer ()
1383 "Determine the image type from data in the current buffer.
1384 Value is a symbol specifying the image type or nil if type cannot
1386 (let ((types mm-image-type-regexps
)
1388 (goto-char (point-min))
1389 (while (and types
(null type
))
1390 (let ((regexp (car (car types
)))
1391 (image-type (cdr (car types
))))
1392 (when (looking-at regexp
)
1393 (setq type image-type
))
1394 (setq types
(cdr types
))))
1397 (defun mm-get-image (handle)
1398 "Return an image instance based on HANDLE."
1399 (let ((type (mm-handle-media-subtype handle
))
1401 ;; Allow some common translations.
1404 ((equal type
"x-pixmap")
1406 ((equal type
"x-xbitmap")
1408 ((equal type
"x-portable-bitmap")
1411 (or (mm-handle-cache handle
)
1412 (mm-with-unibyte-buffer
1413 (mm-insert-part handle
)
1417 ;; Avoid testing `make-glyph' since W3 may define
1418 ;; a bogus version of it.
1419 (if (fboundp 'create-image
)
1420 (create-image (buffer-string)
1421 (or (mm-image-type-from-buffer)
1424 (mm-create-image-xemacs type
))))
1425 (mm-handle-set-cache handle spec
))))))
1427 (defun mm-create-image-xemacs (type)
1428 (when (featurep 'xemacs
)
1431 ;; xbm images require special handling, since
1432 ;; the only way to create glyphs from these
1433 ;; (without a ton of work) is to write them
1434 ;; out to a file, and then create a file
1436 (let ((file (mm-make-temp-file
1437 (expand-file-name "emm" mm-tmp-directory
)
1441 (write-region (point-min) (point-max) file
)
1442 (make-glyph (list (cons 'x file
))))
1444 (delete-file file
)))))
1448 (or (mm-image-type-from-buffer)
1450 :data
(buffer-string)))))))
1452 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1454 (defun mm-image-fit-p (handle)
1455 "Say whether the image in HANDLE will fit the current window."
1456 (let ((image (mm-get-image handle
)))
1458 (if (featurep 'xemacs
)
1459 ;; XEmacs' glyphs can actually tell us about their width, so
1460 ;; lets be nice and smart about them.
1461 (or mm-inline-large-images
1462 (and (<= (glyph-width image
) (window-pixel-width))
1463 (<= (glyph-height image
) (window-pixel-height))))
1464 (let* ((size (image-size image
))
1467 (or mm-inline-large-images
1468 (and (<= h
(1- (window-height))) ; Don't include mode line.
1469 (<= w
(window-width)))))))))
1471 (defun mm-valid-image-format-p (format)
1472 "Say whether FORMAT can be displayed natively by Emacs."
1475 ((fboundp 'valid-image-instantiator-format-p
)
1476 (valid-image-instantiator-format-p format
))
1478 ((fboundp 'image-type-available-p
)
1479 (and (display-graphic-p)
1480 (image-type-available-p format
)))
1481 ;; Nobody else can do images yet.
1485 (defun mm-valid-and-fit-image-p (format handle
)
1486 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1487 (and (mm-valid-image-format-p format
)
1488 (mm-image-fit-p handle
)))
1490 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1491 "Search in HANDLES for part with TYPE.
1492 If NOTP, returns first non-matching part.
1493 If RECURSIVE, search recursively."
1496 (if (and recursive
(stringp (caar handles
)))
1497 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1501 (not (equal (mm-handle-media-type (car handles
)) type
))
1502 (equal (mm-handle-media-type (car handles
)) type
))
1503 (setq handle
(car handles
)
1505 (setq handles
(cdr handles
)))
1508 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1509 (goto-char (point-min))
1510 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1512 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1514 (end (save-excursion
1515 (goto-char (point-max))
1516 (if (re-search-backward close-delimiter nil t
)
1520 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1521 (while (and (not result
)
1522 (re-search-forward boundary end t
))
1523 (goto-char (match-beginning 0))
1527 (narrow-to-region start
(1- (point)))
1528 (when (let* ((ct (mail-fetch-field "content-type"))
1529 (ctl (and ct
(mail-header-parse-content-type ct
))))
1531 (not (equal (car ctl
) type
))
1532 (equal (car ctl
) type
)))
1533 (setq result
(buffer-string))))))
1535 (setq start
(point)))
1536 (when (and (not result
) start
)
1539 (narrow-to-region start end
)
1540 (when (let* ((ct (mail-fetch-field "content-type"))
1541 (ctl (and ct
(mail-header-parse-content-type ct
))))
1543 (not (equal (car ctl
) type
))
1544 (equal (car ctl
) type
)))
1545 (setq result
(buffer-string))))))
1548 (defvar mm-security-handle nil
)
1550 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1551 ;; HANDLE could be a CTL.
1553 (put-text-property 0 (length (car handle
)) parameter value
1556 (autoload 'mm-view-pkcs7
"mm-view")
1558 (defun mm-possibly-verify-or-decrypt (parts ctl
)
1559 (let ((type (car ctl
))
1560 (subtype (cadr (split-string (car ctl
) "/")))
1561 (mm-security-handle ctl
) ;; (car CTL) is the type.
1562 protocol func functest
)
1564 ((or (equal type
"application/x-pkcs7-mime")
1565 (equal type
"application/pkcs7-mime"))
1568 ((eq mm-decrypt-option
'never
) nil
)
1569 ((eq mm-decrypt-option
'always
) t
)
1570 ((eq mm-decrypt-option
'known
) t
)
1572 (format "Decrypt (S/MIME) part? "))))
1573 (mm-view-pkcs7 parts
))
1574 (setq parts
(mm-dissect-buffer t
)))))
1575 ((equal subtype
"signed")
1576 (unless (and (setq protocol
1577 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1578 (not (equal protocol
"multipart/mixed")))
1579 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1580 (let ((protocols mm-verify-function-alist
))
1582 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1583 (funcall functest parts ctl
))
1584 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1585 (setq protocol
(caar protocols
)
1587 (setq protocols
(cdr protocols
))))))
1588 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1590 ((eq mm-verify-option
'never
) nil
)
1591 ((eq mm-verify-option
'always
) t
)
1592 ((eq mm-verify-option
'known
)
1594 (or (not (setq functest
1595 (nth 3 (assoc protocol
1596 mm-verify-function-alist
))))
1597 (funcall functest parts ctl
))))
1600 (format "Verify signed (%s) part? "
1601 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1602 (format "protocol=%s" protocol
))))))
1605 (setq parts
(funcall func parts ctl
))
1606 (mm-set-handle-multipart-parameter
1607 mm-security-handle
'gnus-details
1608 (format "Unknown sign protocol (%s)" protocol
))))))
1609 ((equal subtype
"encrypted")
1610 (unless (setq protocol
1611 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1612 ;; The message is broken.
1613 (let ((parts parts
))
1615 (if (assoc (mm-handle-media-type (car parts
))
1616 mm-decrypt-function-alist
)
1617 (setq protocol
(mm-handle-media-type (car parts
))
1619 (setq parts
(cdr parts
))))))
1620 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1622 ((eq mm-decrypt-option
'never
) nil
)
1623 ((eq mm-decrypt-option
'always
) t
)
1624 ((eq mm-decrypt-option
'known
)
1626 (or (not (setq functest
1627 (nth 3 (assoc protocol
1628 mm-decrypt-function-alist
))))
1629 (funcall functest parts ctl
))))
1632 (format "Decrypt (%s) part? "
1633 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1634 (format "protocol=%s" protocol
))))))
1637 (setq parts
(funcall func parts ctl
))
1638 (mm-set-handle-multipart-parameter
1639 mm-security-handle
'gnus-details
1640 (format "Unknown encrypt protocol (%s)" protocol
))))))
1644 (defun mm-multiple-handles (handles)
1645 (and (listp handles
)
1646 (> (length handles
) 1)
1647 (or (listp (car handles
))
1648 (stringp (car handles
)))))
1650 (defun mm-complicated-handles (handles)
1651 (and (listp (car handles
))
1652 (> (length handles
) 1)))
1654 (defun mm-merge-handles (handles1 handles2
)
1656 (if (listp (car handles1
))
1659 (if (listp (car handles2
))
1663 (defun mm-readable-p (handle)
1664 "Say whether the content of HANDLE is readable."
1665 (and (< (with-current-buffer (mm-handle-buffer handle
)
1666 (buffer-size)) 10000)
1667 (mm-with-unibyte-buffer
1668 (mm-insert-part handle
)
1669 (and (eq (mm-body-7-or-8) '7bit
)
1670 (not (mm-long-lines-p 76))))))
1672 (provide 'mm-decode
)
1674 ;; arch-tag: 4f35d360-56b8-4030-9388-3ed82d359b9b
1675 ;;; mm-decode.el ends here