1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; For Emacs <22.2 and XEmacs.
28 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
32 (eval-when-compile (require 'cl
))
34 (autoload 'gnus-map-function
"gnus-util")
35 (autoload 'gnus-replace-in-string
"gnus-util")
36 (autoload 'gnus-read-shell-command
"gnus-util")
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 (autoload 'mm-archive-decoders
"mm-archive")
44 (autoload 'mm-archive-dissect-and-inline
"mm-archive")
45 (autoload 'mm-dissect-archive
"mm-archive")
47 (defvar gnus-current-window-configuration
)
49 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
51 (defgroup mime-display
()
52 "Display of MIME in mail and news articles."
53 :link
'(custom-manual "(emacs-mime)Display Customization")
59 (defgroup mime-security
()
60 "MIME security in mail and news articles."
61 :link
'(custom-manual "(emacs-mime)Display Customization")
66 ;;; Convenience macros.
68 (defmacro mm-handle-buffer
(handle)
70 (defmacro mm-handle-type
(handle)
72 (defsubst mm-handle-media-type
(handle)
73 (if (stringp (car handle
))
75 (car (mm-handle-type handle
))))
76 (defsubst mm-handle-media-supertype
(handle)
77 (car (split-string (mm-handle-media-type handle
) "/")))
78 (defsubst mm-handle-media-subtype
(handle)
79 (cadr (split-string (mm-handle-media-type handle
) "/")))
80 (defmacro mm-handle-encoding
(handle)
82 (defmacro mm-handle-undisplayer
(handle)
84 (defmacro mm-handle-set-undisplayer
(handle function
)
85 `(setcar (nthcdr 3 ,handle
) ,function
))
86 (defmacro mm-handle-disposition
(handle)
88 (defmacro mm-handle-description
(handle)
90 (defmacro mm-handle-cache
(handle)
92 (defmacro mm-handle-set-cache
(handle contents
)
93 `(setcar (nthcdr 6 ,handle
) ,contents
))
94 (defmacro mm-handle-id
(handle)
96 (defmacro mm-handle-multipart-original-buffer
(handle)
97 `(get-text-property 0 'buffer
(car ,handle
)))
98 (defmacro mm-handle-multipart-from
(handle)
99 `(get-text-property 0 'from
(car ,handle
)))
100 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
101 `(get-text-property 0 ,parameter
(car ,handle
)))
103 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
104 disposition description cache
106 `(list ,buffer
,type
,encoding
,undisplayer
107 ,disposition
,description
,cache
,id
))
109 (defcustom mm-text-html-renderer
110 (cond ((fboundp 'libxml-parse-html-region
) 'shr
)
111 ((executable-find "w3m") 'gnus-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 `shr': use the built-in Gnus HTML renderer;
121 `gnus-w3m': use Gnus renderer based on w3m;
122 `w3m': use emacs-w3m;
123 `w3m-standalone': use plain w3m;
127 `html2text': use html2text;
128 nil : use external viewer (default web browser)."
130 :type
'(choice (const shr
)
133 (const w3m
:tag
"emacs-w3m")
134 (const w3m-standalone
:tag
"standalone w3m" )
138 (const nil
:tag
"External viewer")
140 :group
'mime-display
)
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 identity
)
230 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
231 ("text/x-diff" mm-display-patch-inline identity
)
232 ("application/emacs-lisp" mm-display-elisp-inline identity
)
233 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
234 ("application/x-shellscript" mm-display-shell-script-inline identity
)
235 ("application/x-sh" mm-display-shell-script-inline identity
)
236 ("text/x-sh" mm-display-shell-script-inline identity
)
237 ("application/javascript" mm-display-javascript-inline identity
)
238 ("text/dns" mm-display-dns-inline identity
)
239 ("text/x-org" mm-display-org-inline identity
)
243 mm-text-html-renderer
))
247 (or (featurep 'vcard
)
248 (locate-library "vcard"))))
249 ("message/delivery-status" mm-inline-text identity
)
250 ("message/rfc822" mm-inline-message identity
)
251 ("message/partial" mm-inline-partial identity
)
252 ("message/external-body" mm-inline-external-body identity
)
253 ("text/.*" mm-inline-text identity
)
254 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity
)
255 ("application/zip" mm-archive-dissect-and-inline 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
)
276 (and (mm-valid-image-format-p 'imagemagick
)
277 (mm-with-unibyte-buffer
278 (mm-insert-part handle
)
281 (if (fboundp 'create-image
)
282 (create-image (buffer-string) 'imagemagick
'data-p
)
283 (mm-create-image-xemacs
284 (mm-handle-media-subtype handle
))))))
286 (setcar (cdr handle
) (list "image/imagemagick"))
287 (mm-image-fit-p handle
)))))))
288 ;; Disable audio and image
289 ("audio/.*" ignore ignore
)
290 ("image/.*" ignore ignore
)
291 ;; Default to displaying as text
292 (".*" mm-inline-text mm-readable-p
))
293 "Alist of media types/tests saying whether types can be displayed inline."
294 :type
'(repeat (list (regexp :tag
"MIME type")
295 (function :tag
"Display function")
296 (function :tag
"Display test")))
297 :group
'mime-display
)
299 (defcustom mm-inlined-types
300 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
301 "message/partial" "message/external-body" "application/emacs-lisp"
302 "application/x-emacs-lisp"
303 "application/pgp-signature" "application/x-pkcs7-signature"
304 "application/pkcs7-signature" "application/x-pkcs7-mime"
305 "application/pkcs7-mime"
306 "application/x-gtar-compressed"
309 ;; Mutt still uses this even though it has already been withdrawn.
311 "List of media types that are to be displayed inline.
312 See also `mm-inline-media-tests', which says how to display a media
314 :type
'(repeat regexp
)
315 :group
'mime-display
)
317 (defcustom mm-keep-viewer-alive-types
318 '("application/postscript" "application/msword" "application/vnd.ms-excel"
319 "application/pdf" "application/x-dvi")
320 "List of media types for which the external viewer will not be killed
321 when selecting a different article."
323 :type
'(repeat regexp
)
324 :group
'mime-display
)
326 (defcustom mm-automatic-display
327 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
328 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
329 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
330 "application/emacs-lisp" "application/x-emacs-lisp"
331 "application/x-pkcs7-signature"
332 "application/pkcs7-signature" "application/x-pkcs7-mime"
333 "application/pkcs7-mime"
334 ;; Mutt still uses this even though it has already been withdrawn.
337 "A list of MIME types to be displayed automatically."
338 :type
'(repeat regexp
)
339 :group
'mime-display
)
341 (defcustom mm-attachment-override-types
'("text/x-vcard"
342 "application/pkcs7-mime"
343 "application/x-pkcs7-mime"
344 "application/pkcs7-signature"
345 "application/x-pkcs7-signature")
346 "Types to have \"attachment\" ignored if they can be displayed inline."
347 :type
'(repeat regexp
)
348 :group
'mime-display
)
350 (defcustom mm-inline-override-types nil
351 "Types to be treated as attachments even if they can be displayed inline."
352 :type
'(repeat regexp
)
353 :group
'mime-display
)
355 (defcustom mm-automatic-external-display nil
356 "List of MIME type regexps that will be displayed externally automatically."
357 :type
'(repeat regexp
)
358 :group
'mime-display
)
360 (defcustom mm-discouraged-alternatives nil
361 "List of MIME types that are discouraged when viewing multipart/alternative.
362 Viewing agents are supposed to view the last possible part of a message,
363 as that is supposed to be the richest. However, users may prefer other
364 types instead, and this list says what types are most unwanted. If,
365 for instance, text/html parts are very unwanted, and text/richtext are
366 somewhat unwanted, then the value of this variable should be set
369 (\"text/html\" \"text/richtext\")
371 Adding \"image/.*\" might also be useful. Spammers use it as the
372 preferred part of multipart/alternative messages. See also
373 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
374 enables you to choose manually one of two types those mails include."
375 :type
'(repeat regexp
) ;; See `mm-preferred-alternative-precedence'.
376 :group
'mime-display
)
378 (defcustom mm-tmp-directory
379 (if (fboundp 'temp-directory
)
381 (if (boundp 'temporary-file-directory
)
382 temporary-file-directory
384 "Where mm will store its temporary files."
386 :group
'mime-display
)
388 (defcustom mm-inline-large-images nil
389 "If t, then all images fit in the buffer.
390 If 'resize, try to resize the images so they fit."
392 (const :tag
"Inline large images as they are." t
)
393 (const :tag
"Resize large images." resize
)
394 (const :tag
"Do not inline large images." nil
))
395 :group
'mime-display
)
397 (defcustom mm-file-name-rewrite-functions
398 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
399 "List of functions used for rewriting file names of MIME parts.
400 Each function takes a file name as input and returns a file name.
402 Ready-made functions include `mm-file-name-delete-control',
403 `mm-file-name-delete-gotchas' (you should not remove these two
404 functions), `mm-file-name-delete-whitespace',
405 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
406 `mm-file-name-replace-whitespace', `capitalize', `downcase',
407 `upcase', and `upcase-initials'."
408 :type
'(list (set :inline t
409 (const mm-file-name-delete-control
)
410 (const mm-file-name-delete-gotchas
)
411 (const mm-file-name-delete-whitespace
)
412 (const mm-file-name-trim-whitespace
)
413 (const mm-file-name-collapse-whitespace
)
414 (const mm-file-name-replace-whitespace
)
418 (const upcase-initials
)
422 :version
"23.1" ;; No Gnus
423 :group
'mime-display
)
426 (defvar mm-path-name-rewrite-functions nil
427 "*List of functions for rewriting the full file names of MIME parts.
428 This is used when viewing parts externally, and is meant for
429 transforming the absolute name so that non-compliant programs can find
430 the file where it's saved.
432 Each function takes a file name as input and returns a file name.")
434 (defvar mm-file-name-replace-whitespace nil
435 "String used for replacing whitespace characters; default is `\"_\"'.")
437 (defcustom mm-default-directory nil
438 "The default directory where mm will save files.
439 If not set, `default-directory' will be used."
440 :type
'(choice directory
(const :tag
"Default" nil
))
441 :group
'mime-display
)
443 (defcustom mm-attachment-file-modes
384
444 "Set the mode bits of saved attachments to this integer."
447 :group
'mime-display
)
449 (defcustom mm-external-terminal-program
"xterm"
450 "The program to start an external terminal."
453 :group
'mime-display
)
455 ;;; Internal variables.
457 (defvar mm-last-shell-command
"")
458 (defvar mm-content-id-alist nil
)
459 (defvar mm-postponed-undisplay-list nil
)
460 (defvar mm-inhibit-auto-detect-attachment nil
)
462 ;; According to RFC2046, in particular, in a digest, the default
463 ;; Content-Type value for a body part is changed from "text/plain" to
465 (defvar mm-dissect-default-type
"text/plain")
467 (autoload 'mml2015-verify
"mml2015")
468 (autoload 'mml2015-verify-test
"mml2015")
469 (autoload 'mml-smime-verify
"mml-smime")
470 (autoload 'mml-smime-verify-test
"mml-smime")
472 (defvar mm-verify-function-alist
473 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
474 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
475 mm-uu-pgp-signed-test
)
476 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
477 mml-smime-verify-test
)
478 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
479 mml-smime-verify-test
)))
481 (defcustom mm-verify-option
'never
482 "Option of verifying signed parts.
483 `never', not verify; `always', always verify;
484 `known', only verify known protocols. Otherwise, ask user.
486 When set to `always' or `known', you should add
487 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
488 result of the verification."
490 :type
'(choice (item always
)
492 (item :tag
"only known protocols" known
)
493 (item :tag
"ask" nil
))
494 :group
'mime-security
)
496 (autoload 'mml2015-decrypt
"mml2015")
497 (autoload 'mml2015-decrypt-test
"mml2015")
499 (defvar mm-decrypt-function-alist
500 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
501 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
502 mm-uu-pgp-encrypted-test
)))
504 (defcustom mm-decrypt-option nil
505 "Option of decrypting encrypted parts.
506 `never', not decrypt; `always', always decrypt;
507 `known', only decrypt known protocols. Otherwise, ask user."
509 :type
'(choice (item always
)
511 (item :tag
"only known protocols" known
)
512 (item :tag
"ask" nil
))
513 :group
'mime-security
)
515 (defvar mm-viewer-completion-map
516 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
517 (set-keymap-parent map minibuffer-local-completion-map
)
518 ;; Should we bind other key to minibuffer-complete-word?
519 (define-key map
" " 'self-insert-command
)
521 "Keymap for input viewer with completion.")
523 (defvar mm-viewer-completion-map
524 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
525 (set-keymap-parent map minibuffer-local-completion-map
)
526 ;; Should we bind other key to minibuffer-complete-word?
527 (define-key map
" " 'self-insert-command
)
529 "Keymap for input viewer with completion.")
533 (defun mm-alist-to-plist (alist)
534 "Convert association list ALIST into the equivalent property-list form.
535 The plist is returned. This converts from
537 \((a . 1) (b . 2) (c . 3))
543 The original alist is not modified. See also `destructive-alist-to-plist'."
546 (let ((el (car alist
)))
547 (setq plist
(cons (cdr el
) (cons (car el
) plist
))))
548 (setq alist
(cdr alist
)))
551 (defun mm-keep-viewer-alive-p (handle)
552 "Say whether external viewer for HANDLE should stay alive."
553 (let ((types mm-keep-viewer-alive-types
)
554 (type (mm-handle-media-type handle
))
557 (while (setq ty
(pop types
))
558 (when (string-match ty type
)
559 (throw 'found t
))))))
561 (defun mm-handle-set-external-undisplayer (handle function
)
562 "Set the undisplayer for HANDLE to FUNCTION.
563 Postpone undisplaying of viewers for types in
564 `mm-keep-viewer-alive-types'."
565 (if (mm-keep-viewer-alive-p handle
)
566 (let ((new-handle (copy-sequence handle
)))
567 (mm-handle-set-undisplayer new-handle function
)
568 (mm-handle-set-undisplayer handle nil
)
569 (push new-handle mm-postponed-undisplay-list
))
570 (mm-handle-set-undisplayer handle function
)))
572 (defun mm-destroy-postponed-undisplay-list ()
573 (when mm-postponed-undisplay-list
574 (message "Destroying external MIME viewers")
575 (mm-destroy-parts mm-postponed-undisplay-list
)))
577 (autoload 'message-fetch-field
"message")
579 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
580 "Dissect the current buffer and return a list of MIME handles.
581 If NO-STRICT-MIME, don't require the message to have a
582 MIME-Version header before proceeding."
584 (let (ct ctl type subtype cte cd description id result
)
586 (mail-narrow-to-head)
587 (when (or no-strict-mime
589 (mail-fetch-field "mime-version"))
590 (setq ct
(mail-fetch-field "content-type")
591 ctl
(and ct
(mail-header-parse-content-type ct
))
592 cte
(mail-fetch-field "content-transfer-encoding")
593 cd
(or (mail-fetch-field "content-disposition")
596 (cadr (mm-assoc-string-match
597 mm-inline-media-tests
600 ;; Newlines in description should be stripped so as
601 ;; not to break the MIME tag into two or more lines.
602 description
(message-fetch-field "content-description")
603 id
(mail-fetch-field "content-id"))
605 (setq from
(mail-fetch-field "from")))
606 ;; FIXME: In some circumstances, this code is running within
607 ;; an unibyte macro. mail-extract-address-components
608 ;; creates unibyte buffers. This `if', though not a perfect
609 ;; solution, avoids most of them.
611 (setq from
(cadr (mail-extract-address-components from
))))
613 (setq description
(mail-decode-encoded-word-string
616 (not (string-match "/" (car ctl
))))
617 (mm-dissect-singlepart
618 (list mm-dissect-default-type
)
619 (and cte
(intern (downcase (mail-header-strip cte
))))
621 (and cd
(mail-header-parse-content-disposition cd
))
623 (setq type
(split-string (car ctl
) "/"))
624 (setq subtype
(cadr type
)
629 ((equal type
"multipart")
630 (let ((mm-dissect-default-type (if (equal subtype
"digest")
633 (start (cdr (assq 'start
(cdr ctl
)))))
634 (add-text-properties 0 (length (car ctl
))
635 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
637 ;; what really needs to be done here is a way to link a
638 ;; MIME handle back to it's parent MIME handle (in a multilevel
639 ;; MIME article). That would probably require changing
640 ;; the mm-handle API so we simply store the multipart buffer
641 ;; name as a text property of the "multipart/whatever" string.
642 (add-text-properties 0 (length (car ctl
))
643 (list 'buffer
(mm-copy-to-buffer)
647 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
649 (mm-possibly-verify-or-decrypt
650 (mm-dissect-singlepart
652 (and cte
(intern (downcase (mail-header-strip cte
))))
654 (and cd
(mail-header-parse-content-disposition cd
))
658 (when (string-match " *<\\(.*\\)> *" id
)
659 (setq id
(match-string 1 id
)))
660 (push (cons id result
) mm-content-id-alist
))
663 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
665 (if (equal "text/plain" (car ctl
))
668 ;; Guess what the type of application/octet-stream parts should
670 (let ((filename (cdr (assq 'filename
(cdr cdl
)))))
671 (when (and (not mm-inhibit-auto-detect-attachment
)
672 (equal (car ctl
) "application/octet-stream")
674 (string-match "\\.\\([^.]+\\)$" filename
))
675 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename
))))
677 (setcar ctl new-type
)))))
680 (mm-copy-to-buffer) ctl cte nil cdl description nil id
))
681 (decoder (assoc (car ctl
) (mm-archive-decoders))))
683 ;; Do automatic decoding
685 (executable-find (caddr decoder
)))
686 (mm-dissect-archive handle
)
689 (defun mm-dissect-multipart (ctl from
)
690 (goto-char (point-min))
691 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
692 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
695 (goto-char (point-max))
696 (if (re-search-backward close-delimiter nil t
)
699 (mm-inhibit-auto-detect-attachment
700 (equal (car ctl
) "multipart/encrypted")))
701 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
702 (while (and (< (point) end
) (re-search-forward boundary end t
))
703 (goto-char (match-beginning 0))
707 (narrow-to-region start
(point))
708 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
710 (or (looking-at boundary
)
712 (setq start
(point)))
713 (when (and start
(< start end
))
716 (narrow-to-region start end
)
717 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
718 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl from
)))
720 (defun mm-copy-to-buffer ()
721 "Copy the contents of the current buffer to a fresh buffer."
722 (let ((obuf (current-buffer))
723 (mb (mm-multibyte-p))
725 (goto-char (point-min))
726 (search-forward-regexp "^\n" nil t
)
729 (generate-new-buffer " *mm*")
730 ;; Preserve the data's unibyteness (for url-insert-file-contents).
731 (mm-set-buffer-multibyte mb
)
732 (insert-buffer-substring obuf beg
)
735 (defun mm-display-parts (handle &optional no-default
)
736 (if (stringp (car handle
))
737 (mapcar 'mm-display-parts
(cdr handle
))
738 (if (bufferp (car handle
))
740 (narrow-to-region (point) (point))
741 (mm-display-part handle
)
742 (goto-char (point-max)))
743 (mapcar 'mm-display-parts handle
))))
745 (autoload 'mailcap-parse-mailcaps
"mailcap")
746 (autoload 'mailcap-mime-info
"mailcap")
748 (defun mm-display-part (handle &optional no-default force
)
749 "Display the MIME part represented by HANDLE.
750 Returns nil if the part is removed; inline if displayed inline;
751 external if displayed external."
753 (mailcap-parse-mailcaps)
755 (mm-handle-displayed-p handle
))
756 (mm-remove-part handle
)
757 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
758 "message/external-body")
760 (unless (mm-handle-cache handle
)
761 (mm-extern-cache-contents handle
))
762 (mm-handle-cache handle
))
764 (type (mm-handle-media-type ehandle
))
765 (method (mailcap-mime-info type
))
766 (filename (or (mail-content-type-get
767 (mm-handle-disposition handle
) 'filename
)
768 (mail-content-type-get
769 (mm-handle-type handle
) 'name
)
771 (external mm-enable-external
)
772 (decoder (assoc (car (mm-handle-type handle
))
773 (mm-archive-decoders))))
776 (executable-find (caddr decoder
)))
777 (mm-archive-dissect-and-inline handle
)
779 ((and (mm-inlinable-p ehandle
)
780 (mm-inlined-p ehandle
))
782 (mm-display-inline handle
)
786 (if (and (not method
)
787 (equal "text" (car (split-string type
"/"))))
790 (mm-insert-inline handle
(mm-get-part handle
))
793 (and method
;; If nil, we always use "save".
794 (stringp method
) ;; 'mailcap-save-binary-file
795 (or (eq mm-enable-external t
)
796 (and (eq mm-enable-external
'ask
)
799 "Display part (" type
800 ") using external program"
801 ;; Can non-string method ever happen?
804 " \"" (format method filename
) "\"")
809 handle
(or method
'mailcap-save-binary-file
))
811 handle
'mailcap-save-binary-file
)))))))))
813 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
814 (defvar mailcap-mime-extensions
) ; mailcap-mime-info autoloads
815 (declare-function term-mode
"term" ())
816 (declare-function term-char-mode
"term" ())
818 (defun mm-display-external (handle method
)
819 "Display HANDLE using METHOD."
820 (let ((outbuf (current-buffer)))
821 (mm-with-unibyte-buffer
822 (if (functionp method
)
823 (let ((cur (current-buffer)))
824 (if (eq method
'mailcap-save-binary-file
)
826 (set-buffer (generate-new-buffer " *mm*"))
828 (mm-insert-part handle
)
829 (mm-add-meta-html-tag handle
)
830 (let ((win (get-buffer-window cur t
)))
832 (select-window win
)))
833 (switch-to-buffer (generate-new-buffer " *mm*")))
834 (buffer-disable-undo)
835 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
836 (insert-buffer-substring cur
)
837 (goto-char (point-min))
839 (message "Viewing with %s" method
))
840 (let ((mm (current-buffer))
841 (non-viewer (assq 'non-viewer
843 (mm-handle-media-type handle
) t
))))
847 (mm-save-part handle
))
848 (when (and (not non-viewer
)
850 (mm-handle-set-undisplayer handle mm
)))))
851 ;; The function is a string to be executed.
852 (mm-insert-part handle
)
853 (mm-add-meta-html-tag handle
)
854 (let* ((dir (mm-make-temp-file
855 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
857 (mail-content-type-get
858 (mm-handle-disposition handle
) 'filename
)
859 (mail-content-type-get
860 (mm-handle-type handle
) 'name
)))
861 (mime-info (mailcap-mime-info
862 (mm-handle-media-type handle
) t
))
863 (needsterm (or (assoc "needsterm" mime-info
)
864 (assoc "needsterminal" mime-info
)))
865 (copiousoutput (assoc "copiousoutput" mime-info
))
867 ;; We create a private sub-directory where we store our files.
868 (set-file-modes dir
#o700
)
870 (setq file
(expand-file-name
871 (gnus-map-function mm-file-name-rewrite-functions
872 (file-name-nondirectory filename
))
874 ;; Use nametemplate (defined in RFC1524) if it is specified
876 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
878 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
879 (setq suffix
(match-string 1 suffix
))
880 ;; Otherwise, use a suffix according to
881 ;; `mailcap-mime-extensions'.
882 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
883 mailcap-mime-extensions
))))
884 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
886 (let ((coding-system-for-write mm-binary-coding-system
))
887 (write-region (point-min) (point-max) file nil
'nomesg
))
888 ;; The file is deleted after the viewer exists. If the users edits
889 ;; the file, changes will be lost. Set file to read-only to make it
891 (set-file-modes file
#o400
)
892 (message "Viewing with %s" method
)
895 (let ((command (mm-mailcap-command
896 method file
(mm-handle-type handle
))))
899 (start-process "*display*" nil
900 mm-external-terminal-program
902 shell-command-switch command
)
910 shell-command-switch command
)))
913 (set-process-sentinel
914 (get-buffer-process buffer
)
915 `(lambda (process state
)
916 (if (eq 'exit
(process-status process
))
917 (gnus-configure-windows
918 ',gnus-current-window-configuration
))))
919 (gnus-configure-windows 'display-term
))
920 (mm-handle-set-external-undisplayer handle
(cons file buffer
)))
921 (message "Displaying %s..." command
))
924 (with-current-buffer outbuf
930 (call-process shell-file-name nil
932 (generate-new-buffer " *mm*"))
936 method file
(mm-handle-type handle
)))
937 (if (buffer-live-p buffer
)
938 (with-current-buffer buffer
941 (ignore-errors (delete-file file
))
942 (ignore-errors (delete-directory
943 (file-name-directory file
)))
944 (ignore-errors (kill-buffer buffer
))))))
947 ;; Deleting the temp file should be postponed for some wrappers,
948 ;; shell scripts, and so on, which might exit right after having
949 ;; started a viewer command as a background job.
950 (let ((command (mm-mailcap-command
951 method file
(mm-handle-type handle
))))
954 (start-process "*display*"
956 (generate-new-buffer " *mm*"))
958 shell-command-switch command
)
959 (set-process-sentinel
960 (get-buffer-process buffer
)
961 (lexical-let ((outbuf outbuf
)
972 (delete-directory (file-name-directory file
)))))
973 (lambda (process state
)
974 (when (eq (process-status process
) 'exit
)
979 (delete-directory (file-name-directory file
))
981 (when (buffer-live-p outbuf
)
982 (with-current-buffer outbuf
983 (let ((buffer-read-only nil
)
987 handle
(with-current-buffer buffer
990 (when (buffer-live-p buffer
)
991 (kill-buffer buffer
)))
992 (message "Displaying %s...done" command
)))))
993 (mm-handle-set-external-undisplayer
994 handle
(cons file buffer
)))
995 (message "Displaying %s..." command
))
998 (defun mm-mailcap-command (method file type-list
)
999 (let ((ctl (cdr type-list
))
1003 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1005 (push (substring method beg
(match-beginning 0)) out
)
1006 (setq beg
(match-end 0)
1007 total
(match-string 0 method
)
1008 sub
(match-string 1 method
))
1010 ((string= total
"%%")
1012 ((or (string= total
"%s")
1013 ;; We do our own quoting.
1014 (string= total
"'%s'")
1015 (string= total
"\"%s\""))
1016 (setq uses-stdin nil
)
1017 (push (shell-quote-argument
1018 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
1019 ((string= total
"%t")
1020 (push (shell-quote-argument (car type-list
)) out
))
1022 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
1023 (push (substring method beg
(length method
)) out
)
1026 (push (shell-quote-argument
1027 (gnus-map-function mm-path-name-rewrite-functions file
))
1029 (mapconcat 'identity
(nreverse out
) "")))
1031 (defun mm-remove-parts (handles)
1032 "Remove the displayed MIME parts represented by HANDLES."
1033 (if (and (listp handles
)
1034 (bufferp (car handles
)))
1035 (mm-remove-part handles
)
1037 (while (setq handle
(pop handles
))
1040 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1041 (kill-buffer (get-text-property 0 'buffer handle
))))
1042 ((and (listp handle
)
1043 (stringp (car handle
)))
1044 (mm-remove-parts (cdr handle
)))
1046 (mm-remove-part handle
)))))))
1048 (defun mm-destroy-parts (handles)
1049 "Remove the displayed MIME parts represented by HANDLES."
1050 (if (and (listp handles
)
1051 (bufferp (car handles
)))
1052 (mm-destroy-part handles
)
1054 (while (setq handle
(pop handles
))
1057 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1058 (kill-buffer (get-text-property 0 'buffer handle
))))
1059 ((and (listp handle
)
1060 (stringp (car handle
)))
1061 (mm-destroy-parts handle
))
1063 (mm-destroy-part handle
)))))))
1065 (defun mm-remove-part (handle)
1066 "Remove the displayed MIME part represented by HANDLE."
1067 (when (listp handle
)
1068 (let ((object (mm-handle-undisplayer handle
)))
1071 ;; Internally displayed part.
1072 ((mm-annotationp object
)
1073 (if (featurep 'xemacs
)
1074 (delete-annotation object
)))
1075 ((or (functionp object
)
1077 (eq (car object
) 'lambda
)))
1079 ;; Externally displayed part.
1082 (while (get-buffer-process (cdr object
))
1083 (interrupt-process (get-buffer-process (cdr object
)))
1084 (message "Waiting for external displayer to die...")
1088 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1089 (message "Waiting for external displayer to die...done")
1090 (ignore-errors (delete-file (car object
)))
1091 (ignore-errors (delete-directory (file-name-directory
1094 (when (buffer-live-p object
)
1095 (kill-buffer object
)))))
1096 (mm-handle-set-undisplayer handle nil
))))
1098 (defun mm-display-inline (handle)
1099 (let* ((type (mm-handle-media-type handle
))
1100 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1101 (funcall function handle
)
1102 (goto-char (point-min))))
1104 (defun mm-assoc-string-match (alist type
)
1105 (dolist (elem alist
)
1106 (when (string-match (car elem
) type
)
1109 (defun mm-automatic-display-p (handle)
1110 "Say whether the user wants HANDLE to be displayed automatically."
1111 (let ((methods mm-automatic-display
)
1112 (type (mm-handle-media-type handle
))
1114 (while (setq method
(pop methods
))
1115 (when (and (not (mm-inline-override-p handle
))
1116 (string-match method type
))
1121 (defun mm-inlinable-p (handle &optional type
)
1122 "Say whether HANDLE can be displayed inline.
1123 TYPE is the mime-type of the object; it defaults to the one given
1125 (unless type
(setq type
(mm-handle-media-type handle
)))
1126 (let ((alist mm-inline-media-tests
)
1129 (when (string-match (caar alist
) type
)
1130 (setq test
(caddar alist
)
1132 (setq test
(funcall test handle
)))
1136 (defun mm-inlined-p (handle)
1137 "Say whether the user wants HANDLE to be displayed inline."
1138 (let ((methods mm-inlined-types
)
1139 (type (mm-handle-media-type handle
))
1141 (while (setq method
(pop methods
))
1142 (when (and (not (mm-inline-override-p handle
))
1143 (string-match method type
))
1148 (defun mm-attachment-override-p (handle)
1149 "Say whether HANDLE should have attachment behavior overridden."
1150 (let ((types mm-attachment-override-types
)
1151 (type (mm-handle-media-type handle
))
1154 (while (setq ty
(pop types
))
1155 (when (and (string-match ty type
)
1156 (mm-inlinable-p handle
))
1157 (throw 'found t
))))))
1159 (defun mm-inline-override-p (handle)
1160 "Say whether HANDLE should have inline behavior overridden."
1161 (let ((types mm-inline-override-types
)
1162 (type (mm-handle-media-type handle
))
1165 (while (setq ty
(pop types
))
1166 (when (string-match ty type
)
1167 (throw 'found t
))))))
1169 (defun mm-automatic-external-display-p (type)
1170 "Return the user-defined method for TYPE."
1171 (let ((methods mm-automatic-external-display
)
1173 (while (setq method
(pop methods
))
1174 (when (string-match method type
)
1179 (defun mm-destroy-part (handle)
1180 "Destroy the data structures connected to HANDLE."
1181 (when (listp handle
)
1182 (mm-remove-part handle
)
1183 (when (buffer-live-p (mm-handle-buffer handle
))
1184 (kill-buffer (mm-handle-buffer handle
)))))
1186 (defun mm-handle-displayed-p (handle)
1187 "Say whether HANDLE is displayed or not."
1188 (mm-handle-undisplayer handle
))
1191 ;;; Functions for outputting parts
1194 (defmacro mm-with-part
(handle &rest forms
)
1195 "Run FORMS in the temp buffer containing the contents of HANDLE."
1196 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1197 ;; chars, so the buffer should be unibyte. It may happen that the
1198 ;; handle-buffer is multibyte for some reason, in which case now is a good
1199 ;; time to adjust it, since we know at this point that it should
1201 `(let* ((handle ,handle
))
1202 (when (and (mm-handle-buffer handle
)
1203 (buffer-name (mm-handle-buffer handle
)))
1205 (mm-disable-multibyte)
1206 (insert-buffer-substring (mm-handle-buffer handle
))
1207 (mm-decode-content-transfer-encoding
1208 (mm-handle-encoding handle
)
1209 (mm-handle-media-type handle
))
1211 (put 'mm-with-part
'lisp-indent-function
1)
1212 (put 'mm-with-part
'edebug-form-spec
'(body))
1214 (defun mm-get-part (handle &optional no-cache
)
1215 "Return the contents of HANDLE as a string.
1216 If NO-CACHE is non-nil, cached contents of a message/external-body part
1218 (if (and (not no-cache
)
1219 (equal (mm-handle-media-type handle
) "message/external-body"))
1221 (unless (mm-handle-cache handle
)
1222 (mm-extern-cache-contents handle
))
1223 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1225 (mm-with-part handle
1228 (defun mm-insert-part (handle &optional no-cache
)
1229 "Insert the contents of HANDLE in the current buffer.
1230 If NO-CACHE is non-nil, cached contents of a message/external-body part
1232 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1235 (with-current-buffer (mm-handle-buffer handle
)
1238 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1240 (mm-get-part handle no-cache
)))))
1246 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1248 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1249 ;; Separate the extracted parts that have the same faces.
1253 (defun mm-file-name-delete-whitespace (file-name)
1254 "Remove all whitespace characters from FILE-NAME."
1255 (while (string-match "\\s-+" file-name
)
1256 (setq file-name
(replace-match "" t t file-name
)))
1259 (defun mm-file-name-trim-whitespace (file-name)
1260 "Remove leading and trailing whitespace characters from FILE-NAME."
1261 (when (string-match "\\`\\s-+" file-name
)
1262 (setq file-name
(substring file-name
(match-end 0))))
1263 (when (string-match "\\s-+\\'" file-name
)
1264 (setq file-name
(substring file-name
0 (match-beginning 0))))
1267 (defun mm-file-name-collapse-whitespace (file-name)
1268 "Collapse multiple whitespace characters in FILE-NAME."
1269 (while (string-match "\\s-\\s-+" file-name
)
1270 (setq file-name
(replace-match " " t t file-name
)))
1273 (defun mm-file-name-replace-whitespace (file-name)
1274 "Replace whitespace characters in FILE-NAME with underscores.
1275 Set the option `mm-file-name-replace-whitespace' to any other
1276 string if you do not like underscores."
1277 (let ((s (or mm-file-name-replace-whitespace
"_")))
1278 (while (string-match "\\s-" file-name
)
1279 (setq file-name
(replace-match s t t file-name
))))
1282 (defun mm-file-name-delete-control (filename)
1283 "Delete control characters from FILENAME."
1284 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1286 (defun mm-file-name-delete-gotchas (filename)
1287 "Delete shell gotchas from FILENAME."
1288 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1289 (gnus-replace-in-string filename
"^[.-]+" ""))
1291 (defun mm-save-part (handle &optional prompt
)
1292 "Write HANDLE to a file.
1293 PROMPT overrides the default one used to ask user for a file name."
1294 (let ((filename (or (mail-content-type-get
1295 (mm-handle-disposition handle
) 'filename
)
1296 (mail-content-type-get
1297 (mm-handle-type handle
) 'name
)))
1300 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1301 (file-name-nondirectory filename
))))
1307 (format "Save MIME part to (default %s): "
1309 (or mm-default-directory default-directory
)
1310 (expand-file-name (or filename
"")
1311 (or mm-default-directory default-directory
))))
1312 (cond ((or (not file
) (equal file
""))
1313 (message "Please enter a file name")
1315 ((and (file-directory-p file
)
1317 (message "Please enter a non-directory file name")
1322 (if (file-directory-p file
)
1323 (setq file
(expand-file-name filename file
))
1324 (setq file
(expand-file-name
1325 file
(or mm-default-directory default-directory
))))
1326 (setq mm-default-directory
(file-name-directory file
))
1327 (and (or (not (file-exists-p file
))
1328 (yes-or-no-p (format "File %s already exists; overwrite? "
1331 (mm-save-part-to-file handle file
)
1334 (defun mm-add-meta-html-tag (handle &optional charset force-charset
)
1335 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1336 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1337 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1338 Return t if meta tag is added or replaced."
1339 (when (equal (mm-handle-media-type handle
) "text/html")
1341 (setq charset
(mail-content-type-get (mm-handle-type handle
)
1343 (setq charset
(format "\
1344 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset
))
1345 (let ((case-fold-search t
))
1346 (goto-char (point-min))
1347 (if (re-search-forward "\
1348 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1349 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t
)
1350 (if (and (not force-charset
)
1352 (string-match "\\`html\\'" (match-string 1)))
1353 ;; Don't modify existing meta tag.
1355 ;; Replace it with the one specifying charset.
1356 (replace-match charset
)
1358 (if (re-search-forward "<head>\\s-*" nil t
)
1359 (insert charset
"\n")
1360 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t
)
1361 (insert "<head>\n" charset
"\n</head>\n"))
1364 (defun mm-save-part-to-file (handle file
)
1365 (mm-with-unibyte-buffer
1366 (mm-insert-part handle
)
1367 (mm-add-meta-html-tag handle
)
1368 (let ((current-file-modes (default-file-modes)))
1369 (set-default-file-modes mm-attachment-file-modes
)
1371 ;; Don't re-compress .gz & al. Arguably we should make
1372 ;; `file-name-handler-alist' nil, but that would chop
1373 ;; ange-ftp, which is reasonable to use here.
1374 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1375 (set-default-file-modes current-file-modes
)))))
1377 (defun mm-pipe-part (handle &optional cmd
)
1378 "Pipe HANDLE to a process.
1379 Use CMD as the process."
1380 (let ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
1382 (gnus-read-shell-command
1383 "Shell command on MIME part: " mm-last-shell-command
))))
1384 (mm-with-unibyte-buffer
1385 (mm-insert-part handle
)
1386 (mm-add-meta-html-tag handle
)
1387 (let ((coding-system-for-write 'binary
))
1388 (shell-command-on-region (point-min) (point-max) command nil
)))))
1390 (autoload 'gnus-completing-read
"gnus-util")
1392 (defun mm-interactively-view-part (handle)
1393 "Display HANDLE using METHOD."
1394 (let* ((type (mm-handle-media-type handle
))
1396 (mapcar (lambda (i) (cdr (assoc 'viewer i
)))
1397 (mailcap-mime-info type
'all
)))
1398 (method (let ((minibuffer-local-completion-map
1399 mm-viewer-completion-map
))
1400 (completing-read "Viewer: " methods
))))
1401 (when (string= method
"")
1402 (error "No method given"))
1403 (if (string-match "^[^% \t]+$" method
)
1404 (setq method
(concat method
" %s")))
1405 (mm-display-external handle method
)))
1407 (defun mm-preferred-alternative (handles &optional preferred
)
1408 "Say which of HANDLES are preferred."
1409 (let ((prec (if preferred
(list preferred
)
1410 (mm-preferred-alternative-precedence handles
)))
1411 p h result type handle
)
1412 (while (setq p
(pop prec
))
1415 (setq handle
(car h
))
1416 (setq type
(mm-handle-media-type handle
))
1417 (when (and (equal p type
)
1418 (mm-automatic-display-p handle
)
1419 (or (stringp (car handle
))
1420 (not (mm-handle-disposition handle
))
1421 (equal (car (mm-handle-disposition handle
))
1429 (defun mm-preferred-alternative-precedence (handles)
1430 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1431 (setq handles
(reverse handles
))
1432 (dolist (disc (reverse mm-discouraged-alternatives
))
1433 (dolist (handle (copy-sequence handles
))
1434 (when (string-match disc
(mm-handle-media-type handle
))
1435 (setq handles
(nconc (delete handle handles
) (list handle
))))))
1436 ;; Remove empty parts.
1437 (dolist (handle (copy-sequence handles
))
1438 (when (and (bufferp (mm-handle-buffer handle
))
1439 (not (with-current-buffer (mm-handle-buffer handle
)
1440 (goto-char (point-min))
1441 (re-search-forward "[^ \t\n]" nil t
))))
1442 (setq handles
(nconc (delete handle handles
) (list handle
)))))
1443 (mapcar #'mm-handle-media-type handles
))
1445 (defun mm-get-content-id (id)
1446 "Return the handle(s) referred to by ID."
1447 (cdr (assoc id mm-content-id-alist
)))
1449 (defconst mm-image-type-regexps
1450 '(("/\\*.*XPM.\\*/" . xpm
)
1454 ("\211PNG\r\n" . png
)
1456 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1457 ("%!PS" . postscript
))
1458 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1459 When the first bytes of an image file match REGEXP, it is assumed to
1460 be of image type IMAGE-TYPE.")
1462 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1463 (defun mm-image-type-from-buffer ()
1464 "Determine the image type from data in the current buffer.
1465 Value is a symbol specifying the image type or nil if type cannot
1467 (let ((types mm-image-type-regexps
)
1469 (goto-char (point-min))
1470 (while (and types
(null type
))
1471 (let ((regexp (car (car types
)))
1472 (image-type (cdr (car types
))))
1473 (when (looking-at regexp
)
1474 (setq type image-type
))
1475 (setq types
(cdr types
))))
1478 (defun mm-get-image (handle)
1479 "Return an image instance based on HANDLE."
1480 (let ((type (mm-handle-media-subtype handle
))
1482 ;; Allow some common translations.
1485 ((equal type
"x-pixmap")
1487 ((equal type
"x-xbitmap")
1489 ((equal type
"x-portable-bitmap")
1492 (or (mm-handle-cache handle
)
1493 (mm-with-unibyte-buffer
1494 (mm-insert-part handle
)
1498 ;; Avoid testing `make-glyph' since W3 may define
1499 ;; a bogus version of it.
1500 (if (fboundp 'create-image
)
1501 (create-image (buffer-string)
1502 (or (mm-image-type-from-buffer)
1505 (mm-create-image-xemacs type
))))
1506 (mm-handle-set-cache handle spec
))))))
1508 (defun mm-create-image-xemacs (type)
1509 (when (featurep 'xemacs
)
1512 ;; xbm images require special handling, since
1513 ;; the only way to create glyphs from these
1514 ;; (without a ton of work) is to write them
1515 ;; out to a file, and then create a file
1517 (let ((file (mm-make-temp-file
1518 (expand-file-name "emm" mm-tmp-directory
)
1522 (write-region (point-min) (point-max) file
)
1523 (make-glyph (list (cons 'x file
))))
1525 (delete-file file
)))))
1529 (or (mm-image-type-from-buffer)
1531 :data
(buffer-string)))))))
1533 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1535 (defun mm-image-fit-p (handle)
1536 "Say whether the image in HANDLE will fit the current window."
1537 (let ((image (mm-get-image handle
)))
1539 (if (featurep 'xemacs
)
1540 ;; XEmacs's glyphs can actually tell us about their width, so
1541 ;; let's be nice and smart about them.
1542 (or mm-inline-large-images
1543 (and (<= (glyph-width image
) (window-pixel-width))
1544 (<= (glyph-height image
) (window-pixel-height))))
1545 (let* ((size (image-size image
))
1548 (or mm-inline-large-images
1549 (and (<= h
(1- (window-height))) ; Don't include mode line.
1550 (<= w
(window-width)))))))))
1552 (defun mm-valid-image-format-p (format)
1553 "Say whether FORMAT can be displayed natively by Emacs."
1556 ((fboundp 'valid-image-instantiator-format-p
)
1557 (valid-image-instantiator-format-p format
))
1559 ((fboundp 'image-type-available-p
)
1560 (and (display-graphic-p)
1561 (image-type-available-p format
)))
1562 ;; Nobody else can do images yet.
1566 (defun mm-valid-and-fit-image-p (format handle
)
1567 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1568 (and (mm-valid-image-format-p format
)
1569 (mm-image-fit-p handle
)))
1571 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1572 "Search in HANDLES for part with TYPE.
1573 If NOTP, returns first non-matching part.
1574 If RECURSIVE, search recursively."
1577 (if (and recursive
(stringp (caar handles
)))
1578 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1582 (not (equal (mm-handle-media-type (car handles
)) type
))
1583 (equal (mm-handle-media-type (car handles
)) type
))
1584 (setq handle
(car handles
)
1586 (setq handles
(cdr handles
)))
1589 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1590 (goto-char (point-min))
1591 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1593 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1595 (end (save-excursion
1596 (goto-char (point-max))
1597 (if (re-search-backward close-delimiter nil t
)
1601 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1602 (while (and (not result
)
1603 (re-search-forward boundary end t
))
1604 (goto-char (match-beginning 0))
1608 (narrow-to-region start
(1- (point)))
1609 (when (let* ((ct (mail-fetch-field "content-type"))
1610 (ctl (and ct
(mail-header-parse-content-type ct
))))
1612 (not (equal (car ctl
) type
))
1613 (equal (car ctl
) type
)))
1614 (setq result
(buffer-string))))))
1616 (setq start
(point)))
1617 (when (and (not result
) start
)
1620 (narrow-to-region start end
)
1621 (when (let* ((ct (mail-fetch-field "content-type"))
1622 (ctl (and ct
(mail-header-parse-content-type ct
))))
1624 (not (equal (car ctl
) type
))
1625 (equal (car ctl
) type
)))
1626 (setq result
(buffer-string))))))
1629 (defvar mm-security-handle nil
)
1631 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1632 ;; HANDLE could be a CTL.
1634 (put-text-property 0 (length (car handle
)) parameter value
1637 (autoload 'mm-view-pkcs7
"mm-view")
1639 (defun mm-possibly-verify-or-decrypt (parts ctl
&optional from
)
1640 (let ((type (car ctl
))
1641 (subtype (cadr (split-string (car ctl
) "/")))
1642 (mm-security-handle ctl
) ;; (car CTL) is the type.
1643 protocol func functest
)
1645 ((or (equal type
"application/x-pkcs7-mime")
1646 (equal type
"application/pkcs7-mime"))
1649 ((eq mm-decrypt-option
'never
) nil
)
1650 ((eq mm-decrypt-option
'always
) t
)
1651 ((eq mm-decrypt-option
'known
) t
)
1653 (format "Decrypt (S/MIME) part? "))))
1654 (mm-view-pkcs7 parts from
))
1655 (setq parts
(mm-dissect-buffer t
)))))
1656 ((equal subtype
"signed")
1657 (unless (and (setq protocol
1658 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1659 (not (equal protocol
"multipart/mixed")))
1660 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1661 (let ((protocols mm-verify-function-alist
))
1663 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1664 (funcall functest parts ctl
))
1665 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1666 (setq protocol
(caar protocols
)
1668 (setq protocols
(cdr protocols
))))))
1669 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1671 ((eq mm-verify-option
'never
) nil
)
1672 ((eq mm-verify-option
'always
) t
)
1673 ((eq mm-verify-option
'known
)
1675 (or (not (setq functest
1676 (nth 3 (assoc protocol
1677 mm-verify-function-alist
))))
1678 (funcall functest parts ctl
))))
1681 (format "Verify signed (%s) part? "
1682 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1683 (format "protocol=%s" protocol
))))))
1686 (setq parts
(funcall func parts ctl
))
1687 (mm-set-handle-multipart-parameter
1688 mm-security-handle
'gnus-details
1689 (format "Unknown sign protocol (%s)" protocol
))))))
1690 ((equal subtype
"encrypted")
1691 (unless (setq protocol
1692 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1693 ;; The message is broken.
1694 (let ((parts parts
))
1696 (if (assoc (mm-handle-media-type (car parts
))
1697 mm-decrypt-function-alist
)
1698 (setq protocol
(mm-handle-media-type (car parts
))
1700 (setq parts
(cdr parts
))))))
1701 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1703 ((eq mm-decrypt-option
'never
) nil
)
1704 ((eq mm-decrypt-option
'always
) t
)
1705 ((eq mm-decrypt-option
'known
)
1707 (or (not (setq functest
1708 (nth 3 (assoc protocol
1709 mm-decrypt-function-alist
))))
1710 (funcall functest parts ctl
))))
1713 (format "Decrypt (%s) part? "
1714 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1715 (format "protocol=%s" protocol
))))))
1718 (setq parts
(funcall func parts ctl
))
1719 (mm-set-handle-multipart-parameter
1720 mm-security-handle
'gnus-details
1721 (format "Unknown encrypt protocol (%s)" protocol
))))))
1725 (defun mm-multiple-handles (handles)
1726 (and (listp handles
)
1727 (> (length handles
) 1)
1728 (or (listp (car handles
))
1729 (stringp (car handles
)))))
1731 (defun mm-complicated-handles (handles)
1732 (and (listp (car handles
))
1733 (> (length handles
) 1)))
1735 (defun mm-merge-handles (handles1 handles2
)
1737 (if (listp (car handles1
))
1740 (if (listp (car handles2
))
1744 (defun mm-readable-p (handle)
1745 "Say whether the content of HANDLE is readable."
1746 (and (< (with-current-buffer (mm-handle-buffer handle
)
1747 (buffer-size)) 10000)
1748 (mm-with-unibyte-buffer
1749 (mm-insert-part handle
)
1750 (and (eq (mm-body-7-or-8) '7bit
)
1751 (not (mm-long-lines-p 76))))))
1753 (declare-function libxml-parse-html-region
"xml.c"
1754 (start end
&optional base-url
))
1755 (declare-function shr-insert-document
"shr" (dom))
1756 (defvar shr-blocked-images
)
1757 (defvar gnus-inhibit-images
)
1758 (autoload 'gnus-blocked-images
"gnus-art")
1760 (defun mm-shr (handle)
1761 ;; Require since we bind its variables.
1763 (let ((article-buffer (current-buffer))
1764 (shr-content-function (lambda (id)
1765 (let ((handle (mm-get-content-id id
)))
1767 (mm-with-part handle
1768 (buffer-string))))))
1769 shr-inhibit-images shr-blocked-images charset char
)
1770 (if (and (boundp 'gnus-summary-buffer
)
1771 (bufferp gnus-summary-buffer
)
1772 (buffer-name gnus-summary-buffer
))
1773 (with-current-buffer gnus-summary-buffer
1774 (setq shr-inhibit-images gnus-inhibit-images
1775 shr-blocked-images
(gnus-blocked-images)))
1776 (setq shr-inhibit-images gnus-inhibit-images
1777 shr-blocked-images
(gnus-blocked-images)))
1779 (setq handle
(mm-dissect-buffer t
)))
1780 (setq charset
(mail-content-type-get (mm-handle-type handle
) 'charset
))
1782 (narrow-to-region (point) (point))
1783 (shr-insert-document
1784 (mm-with-part handle
1788 (mm-charset-to-coding-system charset
1790 (not (eq charset
'ascii
)))
1791 (mm-decode-coding-string (buffer-string) charset
)
1792 (mm-string-as-multibyte (buffer-string)))
1794 (mm-enable-multibyte)))
1795 (goto-char (point-min))
1796 (setq case-fold-search t
)
1797 (while (re-search-forward
1798 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t
)
1800 (cdr (assq (if (match-beginning 1)
1801 (string-to-number (match-string 1) 16)
1802 (string-to-number (match-string 2)))
1803 mm-extra-numeric-entities
)))
1804 (replace-match (char-to-string char
))))
1805 ;; Remove "soft hyphens".
1806 (goto-char (point-min))
1807 (while (search-forward "" nil t
)
1808 (replace-match "" t t
))
1809 (libxml-parse-html-region (point-min) (point-max))))
1812 (mm-convert-shr-links)
1813 (mm-handle-set-undisplayer
1816 (let ((inhibit-read-only t
))
1817 (delete-region ,(point-min-marker)
1818 ,(point-max-marker))))))))
1822 (autoload 'widget-convert-button
"wid-edit")
1824 (defun mm-convert-shr-links ()
1825 (let ((start (point-min))
1828 (< start
(point-max)))
1829 (when (setq start
(text-property-not-all start
(point-max) 'shr-url nil
))
1830 (setq end
(next-single-property-change start
'shr-url nil
(point-max)))
1831 (widget-convert-button
1833 :help-echo
(get-text-property start
'help-echo
)
1835 (get-text-property start
'shr-url
))
1836 (put-text-property start end
'local-map nil
)
1837 (setq start end
)))))
1839 (defun mm-handle-filename (handle)
1840 "Return filename of HANDLE if any."
1841 (or (mail-content-type-get (mm-handle-type handle
)
1843 (mail-content-type-get (mm-handle-disposition handle
)
1846 (provide 'mm-decode
)
1852 ;;; mm-decode.el ends here