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
)
50 (add-hook 'gnus-exit-gnus-hook
'mm-temp-files-delete
)
52 (defgroup mime-display
()
53 "Display of MIME in mail and news articles."
54 :link
'(custom-manual "(emacs-mime)Display Customization")
60 (defgroup mime-security
()
61 "MIME security in mail and news articles."
62 :link
'(custom-manual "(emacs-mime)Display Customization")
67 (defface mm-command-output
70 (:foreground
"ForestGreen"))
76 "Face used for displaying output from commands."
79 ;;; Convenience macros.
81 (defmacro mm-handle-buffer
(handle)
83 (defmacro mm-handle-type
(handle)
85 (defsubst mm-handle-media-type
(handle)
86 (if (stringp (car handle
))
88 (car (mm-handle-type handle
))))
89 (defsubst mm-handle-media-supertype
(handle)
90 (car (split-string (mm-handle-media-type handle
) "/")))
91 (defsubst mm-handle-media-subtype
(handle)
92 (cadr (split-string (mm-handle-media-type handle
) "/")))
93 (defmacro mm-handle-encoding
(handle)
95 (defmacro mm-handle-undisplayer
(handle)
97 (defmacro mm-handle-set-undisplayer
(handle function
)
98 `(setcar (nthcdr 3 ,handle
) ,function
))
99 (defmacro mm-handle-disposition
(handle)
101 (defmacro mm-handle-description
(handle)
103 (defmacro mm-handle-cache
(handle)
105 (defmacro mm-handle-set-cache
(handle contents
)
106 `(setcar (nthcdr 6 ,handle
) ,contents
))
107 (defmacro mm-handle-id
(handle)
109 (defmacro mm-handle-multipart-original-buffer
(handle)
110 `(get-text-property 0 'buffer
(car ,handle
)))
111 (defmacro mm-handle-multipart-from
(handle)
112 `(get-text-property 0 'from
(car ,handle
)))
113 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
114 `(get-text-property 0 ,parameter
(car ,handle
)))
116 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
117 disposition description cache
119 `(list ,buffer
,type
,encoding
,undisplayer
120 ,disposition
,description
,cache
,id
))
122 (defcustom mm-text-html-renderer
123 (cond ((fboundp 'libxml-parse-html-region
) 'shr
)
124 ((executable-find "w3m") 'gnus-w3m
)
125 ((executable-find "links") 'links
)
126 ((executable-find "lynx") 'lynx
)
127 ((locate-library "w3") 'w3
)
128 ((locate-library "html2text") 'html2text
)
130 "Render of HTML contents.
131 It is one of defined renderer types, or a rendering function.
132 The defined renderer types are:
133 `shr': use the built-in Gnus HTML renderer;
134 `gnus-w3m': use Gnus renderer based on w3m;
135 `w3m': use emacs-w3m;
136 `w3m-standalone': use plain w3m;
140 `html2text': use html2text;
141 nil : use external viewer (default web browser)."
143 :type
'(choice (const shr
)
146 (const w3m
:tag
"emacs-w3m")
147 (const w3m-standalone
:tag
"standalone w3m" )
151 (const nil
:tag
"External viewer")
153 :group
'mime-display
)
155 (defcustom mm-inline-text-html-with-images nil
156 "If non-nil, Gnus will allow retrieving images in HTML contents with
157 the <img> tags. It has no effect on Emacs/w3. See also the
158 documentation for the `mm-w3m-safe-url-regexp' variable."
161 :group
'mime-display
)
163 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
164 "Regexp matching URLs which are considered to be safe.
165 Some HTML mails might contain a nasty trick used by spammers, using
166 the <img> tag which is far more evil than the [Click Here!] button.
167 It is most likely intended to check whether the ominous spam mail has
168 reached your eyes or not, in which case the spammer knows for sure
169 that your email address is valid. It is done by embedding an
170 identifier string into a URL that you might automatically retrieve
171 when displaying the image. The default value is \"\\\\`cid:\" which only
172 matches parts embedded to the Multipart/Related type MIME contents and
173 Gnus will never connect to the spammer's site arbitrarily. You may
174 set this variable to nil if you consider all urls to be safe."
176 :type
'(choice (regexp :tag
"Regexp")
177 (const :tag
"All URLs are safe" nil
))
178 :group
'mime-display
)
180 (defcustom mm-inline-text-html-with-w3m-keymap t
181 "If non-nil, use emacs-w3m command keys in the article buffer."
184 :group
'mime-display
)
186 (defcustom mm-enable-external t
187 "Indicate whether external MIME handlers should be used.
189 If t, all defined external MIME handlers are used. If nil, files are saved by
190 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
191 before the external MIME handler is invoked."
193 :type
'(choice (const :tag
"Always" t
)
194 (const :tag
"Never" nil
)
195 (const :tag
"Ask" ask
))
196 :group
'mime-display
)
198 (defcustom mm-inline-media-tests
202 (mm-valid-and-fit-image-p 'jpeg handle
)))
206 (mm-valid-and-fit-image-p 'png handle
)))
210 (mm-valid-and-fit-image-p 'gif handle
)))
214 (mm-valid-and-fit-image-p 'tiff handle
)))
218 (mm-valid-and-fit-image-p 'xbm handle
)))
222 (mm-valid-and-fit-image-p 'xbm handle
)))
226 (mm-valid-and-fit-image-p 'xpm handle
)))
230 (mm-valid-and-fit-image-p 'xpm handle
)))
234 (mm-valid-and-fit-image-p 'bmp handle
)))
235 ("image/x-portable-bitmap"
238 (mm-valid-and-fit-image-p 'pbm handle
)))
239 ("text/plain" mm-inline-text identity
)
240 ("text/enriched" mm-inline-text identity
)
241 ("text/richtext" mm-inline-text identity
)
242 ("text/x-patch" mm-display-patch-inline identity
)
243 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
244 ("text/x-diff" mm-display-patch-inline identity
)
245 ("application/emacs-lisp" mm-display-elisp-inline identity
)
246 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
247 ("application/x-shellscript" mm-display-shell-script-inline identity
)
248 ("application/x-sh" mm-display-shell-script-inline identity
)
249 ("text/x-sh" mm-display-shell-script-inline identity
)
250 ("application/javascript" mm-display-javascript-inline identity
)
251 ("text/dns" mm-display-dns-inline identity
)
252 ("text/x-org" mm-display-org-inline identity
)
256 mm-text-html-renderer
))
260 (or (featurep 'vcard
)
261 (locate-library "vcard"))))
262 ("message/delivery-status" mm-inline-text identity
)
263 ("message/rfc822" mm-inline-message identity
)
264 ("message/partial" mm-inline-partial identity
)
265 ("message/external-body" mm-inline-external-body identity
)
266 ("text/.*" mm-inline-text identity
)
267 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity
)
268 ("application/zip" mm-archive-dissect-and-inline identity
)
269 ("audio/wav" mm-inline-audio
271 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
272 (device-sound-enabled-p))))
276 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
277 (device-sound-enabled-p))))
278 ("application/pgp-signature" ignore identity
)
279 ("application/x-pkcs7-signature" ignore identity
)
280 ("application/pkcs7-signature" ignore identity
)
281 ("application/x-pkcs7-mime" ignore identity
)
282 ("application/pkcs7-mime" ignore identity
)
283 ("multipart/alternative" ignore identity
)
284 ("multipart/mixed" ignore identity
)
285 ("multipart/related" ignore identity
)
289 (and (mm-valid-image-format-p 'imagemagick
)
290 (mm-with-unibyte-buffer
291 (mm-insert-part handle
)
294 (if (fboundp 'create-image
)
295 (create-image (buffer-string) 'imagemagick
'data-p
)
296 (mm-create-image-xemacs
297 (mm-handle-media-subtype handle
))))))
299 (setcar (cdr handle
) (list "image/imagemagick"))
300 (mm-image-fit-p handle
)))))))
301 ;; Disable audio and image
302 ("audio/.*" ignore ignore
)
303 ("image/.*" ignore ignore
)
304 ;; Default to displaying as text
305 (".*" mm-inline-text mm-readable-p
))
306 "Alist of media types/tests saying whether types can be displayed inline."
307 :type
'(repeat (list (regexp :tag
"MIME type")
308 (function :tag
"Display function")
309 (function :tag
"Display test")))
310 :group
'mime-display
)
312 (defcustom mm-inlined-types
313 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
314 "message/partial" "message/external-body" "application/emacs-lisp"
315 "application/x-emacs-lisp"
316 "application/pgp-signature" "application/x-pkcs7-signature"
317 "application/pkcs7-signature" "application/x-pkcs7-mime"
318 "application/pkcs7-mime"
319 "application/x-gtar-compressed"
322 ;; Mutt still uses this even though it has already been withdrawn.
324 "List of media types that are to be displayed inline.
325 See also `mm-inline-media-tests', which says how to display a media
327 :type
'(repeat regexp
)
328 :group
'mime-display
)
330 (defcustom mm-keep-viewer-alive-types
331 '("application/postscript" "application/msword" "application/vnd.ms-excel"
332 "application/pdf" "application/x-dvi")
333 "List of media types for which the external viewer will not be killed
334 when selecting a different article."
336 :type
'(repeat regexp
)
337 :group
'mime-display
)
339 (defcustom mm-automatic-display
340 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
341 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
342 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
343 "application/emacs-lisp" "application/x-emacs-lisp"
344 "application/x-pkcs7-signature"
345 "application/pkcs7-signature" "application/x-pkcs7-mime"
346 "application/pkcs7-mime"
347 ;; Mutt still uses this even though it has already been withdrawn.
350 "A list of MIME types to be displayed automatically."
351 :type
'(repeat regexp
)
352 :group
'mime-display
)
354 (defcustom mm-attachment-override-types
'("text/x-vcard"
355 "application/pkcs7-mime"
356 "application/x-pkcs7-mime"
357 "application/pkcs7-signature"
358 "application/x-pkcs7-signature")
359 "Types to have \"attachment\" ignored if they can be displayed inline."
360 :type
'(repeat regexp
)
361 :group
'mime-display
)
363 (defcustom mm-inline-override-types nil
364 "Types to be treated as attachments even if they can be displayed inline."
365 :type
'(repeat regexp
)
366 :group
'mime-display
)
368 (defcustom mm-automatic-external-display nil
369 "List of MIME type regexps that will be displayed externally automatically."
370 :type
'(repeat regexp
)
371 :group
'mime-display
)
373 (defcustom mm-discouraged-alternatives nil
374 "List of MIME types that are discouraged when viewing multipart/alternative.
375 Viewing agents are supposed to view the last possible part of a message,
376 as that is supposed to be the richest. However, users may prefer other
377 types instead, and this list says what types are most unwanted. If,
378 for instance, text/html parts are very unwanted, and text/richtext are
379 somewhat unwanted, then the value of this variable should be set
382 (\"text/html\" \"text/richtext\")
384 Adding \"image/.*\" might also be useful. Spammers use it as the
385 preferred part of multipart/alternative messages. See also
386 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
387 enables you to choose manually one of two types those mails include."
388 :type
'(repeat regexp
) ;; See `mm-preferred-alternative-precedence'.
389 :group
'mime-display
)
391 (defcustom mm-tmp-directory
392 (if (fboundp 'temp-directory
)
394 (if (boundp 'temporary-file-directory
)
395 temporary-file-directory
397 "Where mm will store its temporary files."
399 :group
'mime-display
)
401 (defcustom mm-inline-large-images nil
402 "If t, then all images fit in the buffer.
403 If 'resize, try to resize the images so they fit."
405 (const :tag
"Inline large images as they are." t
)
406 (const :tag
"Resize large images." resize
)
407 (const :tag
"Do not inline large images." nil
))
408 :group
'mime-display
)
410 (defcustom mm-file-name-rewrite-functions
411 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
412 "List of functions used for rewriting file names of MIME parts.
413 Each function takes a file name as input and returns a file name.
415 Ready-made functions include `mm-file-name-delete-control',
416 `mm-file-name-delete-gotchas' (you should not remove these two
417 functions), `mm-file-name-delete-whitespace',
418 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
419 `mm-file-name-replace-whitespace', `capitalize', `downcase',
420 `upcase', and `upcase-initials'."
421 :type
'(list (set :inline t
422 (const mm-file-name-delete-control
)
423 (const mm-file-name-delete-gotchas
)
424 (const mm-file-name-delete-whitespace
)
425 (const mm-file-name-trim-whitespace
)
426 (const mm-file-name-collapse-whitespace
)
427 (const mm-file-name-replace-whitespace
)
431 (const upcase-initials
)
435 :version
"23.1" ;; No Gnus
436 :group
'mime-display
)
439 (defvar mm-path-name-rewrite-functions nil
440 "*List of functions for rewriting the full file names of MIME parts.
441 This is used when viewing parts externally, and is meant for
442 transforming the absolute name so that non-compliant programs can find
443 the file where it's saved.
445 Each function takes a file name as input and returns a file name.")
447 (defvar mm-file-name-replace-whitespace nil
448 "String used for replacing whitespace characters; default is `\"_\"'.")
450 (defcustom mm-default-directory nil
451 "The default directory where mm will save files.
452 If not set, `default-directory' will be used."
453 :type
'(choice directory
(const :tag
"Default" nil
))
454 :group
'mime-display
)
456 (defcustom mm-attachment-file-modes
384
457 "Set the mode bits of saved attachments to this integer."
460 :group
'mime-display
)
462 (defcustom mm-external-terminal-program
"xterm"
463 "The program to start an external terminal."
466 :group
'mime-display
)
468 ;;; Internal variables.
470 (defvar mm-last-shell-command
"")
471 (defvar mm-content-id-alist nil
)
472 (defvar mm-postponed-undisplay-list nil
)
473 (defvar mm-inhibit-auto-detect-attachment nil
)
474 (defvar mm-temp-files-to-be-deleted nil
475 "List of temporary files scheduled to be deleted.")
476 (defvar mm-temp-files-cache-file
(concat ".mm-temp-files-" (user-login-name))
477 "Name of a file that caches a list of temporary files to be deleted.
478 The file will be saved in the directory `mm-tmp-directory'.")
480 ;; According to RFC2046, in particular, in a digest, the default
481 ;; Content-Type value for a body part is changed from "text/plain" to
483 (defvar mm-dissect-default-type
"text/plain")
485 (autoload 'mml2015-verify
"mml2015")
486 (autoload 'mml2015-verify-test
"mml2015")
487 (autoload 'mml-smime-verify
"mml-smime")
488 (autoload 'mml-smime-verify-test
"mml-smime")
490 (defvar mm-verify-function-alist
491 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
492 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
493 mm-uu-pgp-signed-test
)
494 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
495 mml-smime-verify-test
)
496 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
497 mml-smime-verify-test
)))
499 (defcustom mm-verify-option
'never
500 "Option of verifying signed parts.
501 `never', not verify; `always', always verify;
502 `known', only verify known protocols. Otherwise, ask user.
504 When set to `always' or `known', you should add
505 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
506 result of the verification."
508 :type
'(choice (item always
)
510 (item :tag
"only known protocols" known
)
511 (item :tag
"ask" nil
))
512 :group
'mime-security
)
514 (autoload 'mml2015-decrypt
"mml2015")
515 (autoload 'mml2015-decrypt-test
"mml2015")
517 (defvar mm-decrypt-function-alist
518 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
519 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
520 mm-uu-pgp-encrypted-test
)))
522 (defcustom mm-decrypt-option nil
523 "Option of decrypting encrypted parts.
524 `never', not decrypt; `always', always decrypt;
525 `known', only decrypt known protocols. Otherwise, ask user."
527 :type
'(choice (item always
)
529 (item :tag
"only known protocols" known
)
530 (item :tag
"ask" nil
))
531 :group
'mime-security
)
533 (defvar mm-viewer-completion-map
534 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
535 (set-keymap-parent map minibuffer-local-completion-map
)
536 ;; Should we bind other key to minibuffer-complete-word?
537 (define-key map
" " 'self-insert-command
)
539 "Keymap for input viewer with completion.")
541 (defvar mm-viewer-completion-map
542 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
543 (set-keymap-parent map minibuffer-local-completion-map
)
544 ;; Should we bind other key to minibuffer-complete-word?
545 (define-key map
" " 'self-insert-command
)
547 "Keymap for input viewer with completion.")
551 (defun mm-alist-to-plist (alist)
552 "Convert association list ALIST into the equivalent property-list form.
553 The plist is returned. This converts from
555 \((a . 1) (b . 2) (c . 3))
561 The original alist is not modified. See also `destructive-alist-to-plist'."
564 (let ((el (car alist
)))
565 (setq plist
(cons (cdr el
) (cons (car el
) plist
))))
566 (setq alist
(cdr alist
)))
569 (defun mm-keep-viewer-alive-p (handle)
570 "Say whether external viewer for HANDLE should stay alive."
571 (let ((types mm-keep-viewer-alive-types
)
572 (type (mm-handle-media-type handle
))
575 (while (setq ty
(pop types
))
576 (when (string-match ty type
)
577 (throw 'found t
))))))
579 (defun mm-handle-set-external-undisplayer (handle function
)
580 "Set the undisplayer for HANDLE to FUNCTION.
581 Postpone undisplaying of viewers for types in
582 `mm-keep-viewer-alive-types'."
583 (if (mm-keep-viewer-alive-p handle
)
584 (let ((new-handle (copy-sequence handle
)))
585 (mm-handle-set-undisplayer new-handle function
)
586 (mm-handle-set-undisplayer handle nil
)
587 (push new-handle mm-postponed-undisplay-list
))
588 (mm-handle-set-undisplayer handle function
)))
590 (defun mm-destroy-postponed-undisplay-list ()
591 (when mm-postponed-undisplay-list
592 (message "Destroying external MIME viewers")
593 (mm-destroy-parts mm-postponed-undisplay-list
)))
595 (defun mm-temp-files-delete ()
596 "Delete temporary files and those parent directories.
597 Note that the deletion may fail if a program is catching hold of a file
598 under Windows or Cygwin. In that case, it schedules the deletion of
599 files left at the next time."
600 (let* ((coding-system-for-read mm-universal-coding-system
)
601 (coding-system-for-write mm-universal-coding-system
)
602 (cache-file (expand-file-name mm-temp-files-cache-file
604 (cache (when (file-exists-p cache-file
)
605 (mm-with-multibyte-buffer
606 (insert-file-contents cache-file
)
607 (split-string (buffer-string) "\n" t
))))
609 (dolist (temp (append cache mm-temp-files-to-be-deleted
))
610 (unless (and (file-exists-p temp
)
611 (if (file-directory-p temp
)
612 ;; A parent directory left at the previous time.
614 (ignore-errors (delete-directory temp
))
615 (not (file-exists-p temp
)))
616 ;; Delete a temporary file and its parent directory.
617 (ignore-errors (delete-file temp
))
618 (and (not (file-exists-p temp
))
620 (setq temp
(file-name-directory temp
))
621 (ignore-errors (delete-directory temp
))
622 (not (file-exists-p temp
))))))
625 ;; Schedule the deletion of the files left at the next time.
627 (write-region (concat (mapconcat 'identity
(nreverse fails
) "\n")
629 nil cache-file nil
'silent
)
630 (set-file-modes cache-file
#o600
))
631 (when (file-exists-p cache-file
)
632 (ignore-errors (delete-file cache-file
))))
633 (setq mm-temp-files-to-be-deleted nil
)))
635 (autoload 'message-fetch-field
"message")
637 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
638 "Dissect the current buffer and return a list of MIME handles.
639 If NO-STRICT-MIME, don't require the message to have a
640 MIME-Version header before proceeding."
642 (let (ct ctl type subtype cte cd description id result
)
644 (mail-narrow-to-head)
645 (when (or no-strict-mime
647 (mail-fetch-field "mime-version"))
648 (setq ct
(mail-fetch-field "content-type")
649 ctl
(and ct
(mail-header-parse-content-type ct
))
650 cte
(mail-fetch-field "content-transfer-encoding")
651 cd
(or (mail-fetch-field "content-disposition")
654 (cadr (mm-assoc-string-match
655 mm-inline-media-tests
658 ;; Newlines in description should be stripped so as
659 ;; not to break the MIME tag into two or more lines.
660 description
(message-fetch-field "content-description")
661 id
(mail-fetch-field "content-id"))
663 (setq from
(mail-fetch-field "from")))
664 ;; FIXME: In some circumstances, this code is running within
665 ;; an unibyte macro. mail-extract-address-components
666 ;; creates unibyte buffers. This `if', though not a perfect
667 ;; solution, avoids most of them.
669 (setq from
(cadr (mail-extract-address-components from
))))
671 (setq description
(mail-decode-encoded-word-string
674 (not (string-match "/" (car ctl
))))
675 (mm-dissect-singlepart
676 (list mm-dissect-default-type
)
677 (and cte
(intern (downcase (mail-header-strip cte
))))
679 (and cd
(mail-header-parse-content-disposition cd
))
681 (setq type
(split-string (car ctl
) "/"))
682 (setq subtype
(cadr type
)
687 ((equal type
"multipart")
688 (let ((mm-dissect-default-type (if (equal subtype
"digest")
691 (start (cdr (assq 'start
(cdr ctl
)))))
692 (add-text-properties 0 (length (car ctl
))
693 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
695 ;; what really needs to be done here is a way to link a
696 ;; MIME handle back to it's parent MIME handle (in a multilevel
697 ;; MIME article). That would probably require changing
698 ;; the mm-handle API so we simply store the multipart buffer
699 ;; name as a text property of the "multipart/whatever" string.
700 (add-text-properties 0 (length (car ctl
))
701 (list 'buffer
(mm-copy-to-buffer)
705 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
707 (mm-possibly-verify-or-decrypt
708 (mm-dissect-singlepart
710 (and cte
(intern (downcase (mail-header-strip cte
))))
712 (and cd
(mail-header-parse-content-disposition cd
))
716 (when (string-match " *<\\(.*\\)> *" id
)
717 (setq id
(match-string 1 id
)))
718 (push (cons id result
) mm-content-id-alist
))
721 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
723 (if (equal "text/plain" (car ctl
))
726 ;; Guess what the type of application/octet-stream parts should
728 (let ((filename (cdr (assq 'filename
(cdr cdl
)))))
729 (when (and (not mm-inhibit-auto-detect-attachment
)
730 (equal (car ctl
) "application/octet-stream")
732 (string-match "\\.\\([^.]+\\)$" filename
))
733 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename
))))
735 (setcar ctl new-type
)))))
738 (mm-copy-to-buffer) ctl cte nil cdl description nil id
))
739 (decoder (assoc (car ctl
) (mm-archive-decoders))))
741 ;; Do automatic decoding
743 (executable-find (caddr decoder
)))
744 (mm-dissect-archive handle
)
747 (defun mm-dissect-multipart (ctl from
)
748 (goto-char (point-min))
749 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
750 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
753 (goto-char (point-max))
754 (if (re-search-backward close-delimiter nil t
)
757 (mm-inhibit-auto-detect-attachment
758 (equal (car ctl
) "multipart/encrypted")))
759 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
760 (while (and (< (point) end
) (re-search-forward boundary end t
))
761 (goto-char (match-beginning 0))
765 (narrow-to-region start
(point))
766 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
768 (or (looking-at boundary
)
770 (setq start
(point)))
771 (when (and start
(< start end
))
774 (narrow-to-region start end
)
775 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
776 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl from
)))
778 (defun mm-copy-to-buffer ()
779 "Copy the contents of the current buffer to a fresh buffer."
780 (let ((obuf (current-buffer))
781 (mb (mm-multibyte-p))
783 (goto-char (point-min))
784 (search-forward-regexp "^\n" nil t
)
787 (generate-new-buffer " *mm*")
788 ;; Preserve the data's unibyteness (for url-insert-file-contents).
789 (mm-set-buffer-multibyte mb
)
790 (insert-buffer-substring obuf beg
)
793 (defun mm-display-parts (handle &optional no-default
)
794 (if (stringp (car handle
))
795 (mapcar 'mm-display-parts
(cdr handle
))
796 (if (bufferp (car handle
))
798 (narrow-to-region (point) (point))
799 (mm-display-part handle
)
800 (goto-char (point-max)))
801 (mapcar 'mm-display-parts handle
))))
803 (autoload 'mailcap-parse-mailcaps
"mailcap")
804 (autoload 'mailcap-mime-info
"mailcap")
806 (defun mm-display-part (handle &optional no-default force
)
807 "Display the MIME part represented by HANDLE.
808 Returns nil if the part is removed; inline if displayed inline;
809 external if displayed external."
811 (mailcap-parse-mailcaps)
813 (mm-handle-displayed-p handle
))
814 (mm-remove-part handle
)
815 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
816 "message/external-body")
818 (unless (mm-handle-cache handle
)
819 (mm-extern-cache-contents handle
))
820 (mm-handle-cache handle
))
822 (type (mm-handle-media-type ehandle
))
823 (method (mailcap-mime-info type
))
824 (filename (or (mail-content-type-get
825 (mm-handle-disposition handle
) 'filename
)
826 (mail-content-type-get
827 (mm-handle-type handle
) 'name
)
829 (external mm-enable-external
)
830 (decoder (assoc (car (mm-handle-type handle
))
831 (mm-archive-decoders))))
834 (executable-find (caddr decoder
)))
835 (mm-archive-dissect-and-inline handle
)
837 ((and (mm-inlinable-p ehandle
)
838 (mm-inlined-p ehandle
))
840 (mm-display-inline handle
)
844 (if (and (not method
)
845 (equal "text" (car (split-string type
"/"))))
848 (mm-insert-inline handle
(mm-get-part handle
))
851 (and method
;; If nil, we always use "save".
852 (stringp method
) ;; 'mailcap-save-binary-file
853 (or (eq mm-enable-external t
)
854 (and (eq mm-enable-external
'ask
)
857 "Display part (" type
858 ") using external program"
859 ;; Can non-string method ever happen?
862 " \"" (format method filename
) "\"")
867 handle
(or method
'mailcap-save-binary-file
))
869 handle
'mailcap-save-binary-file
)))))))))
871 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
872 (defvar mailcap-mime-extensions
) ; mailcap-mime-info autoloads
873 (declare-function term-mode
"term" ())
874 (declare-function term-char-mode
"term" ())
876 (defun mm-display-external (handle method
)
877 "Display HANDLE using METHOD."
878 (let ((outbuf (current-buffer)))
879 (mm-with-unibyte-buffer
880 (if (functionp method
)
881 (let ((cur (current-buffer)))
882 (if (eq method
'mailcap-save-binary-file
)
884 (set-buffer (generate-new-buffer " *mm*"))
886 (mm-insert-part handle
)
887 (mm-add-meta-html-tag handle
)
888 (let ((win (get-buffer-window cur t
)))
890 (select-window win
)))
891 (switch-to-buffer (generate-new-buffer " *mm*")))
892 (buffer-disable-undo)
893 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
894 (insert-buffer-substring cur
)
895 (goto-char (point-min))
897 (message "Viewing with %s" method
))
898 (let ((mm (current-buffer))
899 (non-viewer (assq 'non-viewer
901 (mm-handle-media-type handle
) t
))))
905 (mm-save-part handle
))
906 (when (and (not non-viewer
)
908 (mm-handle-set-undisplayer handle mm
)))))
909 ;; The function is a string to be executed.
910 (mm-insert-part handle
)
911 (mm-add-meta-html-tag handle
)
912 (let* ((dir (mm-make-temp-file
913 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
915 (mail-content-type-get
916 (mm-handle-disposition handle
) 'filename
)
917 (mail-content-type-get
918 (mm-handle-type handle
) 'name
)))
919 (mime-info (mailcap-mime-info
920 (mm-handle-media-type handle
) t
))
921 (needsterm (or (assoc "needsterm" mime-info
)
922 (assoc "needsterminal" mime-info
)))
923 (copiousoutput (assoc "copiousoutput" mime-info
))
925 ;; We create a private sub-directory where we store our files.
926 (set-file-modes dir
#o700
)
928 (setq file
(expand-file-name
929 (gnus-map-function mm-file-name-rewrite-functions
930 (file-name-nondirectory filename
))
932 ;; Use nametemplate (defined in RFC1524) if it is specified
934 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
936 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
937 (setq suffix
(match-string 1 suffix
))
938 ;; Otherwise, use a suffix according to
939 ;; `mailcap-mime-extensions'.
940 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
941 mailcap-mime-extensions
))))
942 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
944 (let ((coding-system-for-write mm-binary-coding-system
))
945 (write-region (point-min) (point-max) file nil
'nomesg
))
946 ;; The file is deleted after the viewer exists. If the users edits
947 ;; the file, changes will be lost. Set file to read-only to make it
949 (set-file-modes file
#o400
)
950 (message "Viewing with %s" method
)
953 (let ((command (mm-mailcap-command
954 method file
(mm-handle-type handle
))))
957 (start-process "*display*" nil
958 mm-external-terminal-program
960 shell-command-switch command
)
968 shell-command-switch command
)))
971 (set-process-sentinel
972 (get-buffer-process buffer
)
973 `(lambda (process state
)
974 (if (eq 'exit
(process-status process
))
975 (gnus-configure-windows
976 ',gnus-current-window-configuration
))))
977 (gnus-configure-windows 'display-term
))
978 (mm-handle-set-external-undisplayer handle
(cons file buffer
)))
979 (message "Displaying %s..." command
))
982 (with-current-buffer outbuf
988 (call-process shell-file-name nil
990 (generate-new-buffer " *mm*"))
994 method file
(mm-handle-type handle
)))
995 (if (buffer-live-p buffer
)
996 (with-current-buffer buffer
999 (ignore-errors (delete-file file
))
1000 (ignore-errors (delete-directory
1001 (file-name-directory file
)))
1002 (ignore-errors (kill-buffer buffer
))))))
1005 ;; Deleting the temp file should be postponed for some wrappers,
1006 ;; shell scripts, and so on, which might exit right after having
1007 ;; started a viewer command as a background job.
1008 (let ((command (mm-mailcap-command
1009 method file
(mm-handle-type handle
))))
1011 (let ((process-connection-type nil
))
1012 (start-process "*display*"
1014 (generate-new-buffer " *mm*"))
1016 shell-command-switch command
)
1017 (set-process-sentinel
1018 (get-buffer-process buffer
)
1019 (lexical-let ((outbuf outbuf
)
1024 (lambda (process state
)
1025 (when (eq (process-status process
) 'exit
)
1026 (when (buffer-live-p outbuf
)
1027 (with-current-buffer outbuf
1028 (let ((buffer-read-only nil
)
1031 (let ((start (point)))
1033 handle
(with-current-buffer buffer
1035 (put-text-property start
(point)
1036 'face
'mm-command-output
))
1037 (goto-char point
))))
1038 (when (buffer-live-p buffer
)
1039 (kill-buffer buffer
)))
1040 (message "Displaying %s...done" command
)))))
1041 (mm-handle-set-external-undisplayer
1042 handle
(cons file buffer
))
1043 (add-to-list 'mm-temp-files-to-be-deleted file t
))
1044 (message "Displaying %s..." command
))
1047 (defun mm-mailcap-command (method file type-list
)
1048 (let ((ctl (cdr type-list
))
1052 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1054 (push (substring method beg
(match-beginning 0)) out
)
1055 (setq beg
(match-end 0)
1056 total
(match-string 0 method
)
1057 sub
(match-string 1 method
))
1059 ((string= total
"%%")
1061 ((or (string= total
"%s")
1062 ;; We do our own quoting.
1063 (string= total
"'%s'")
1064 (string= total
"\"%s\""))
1065 (setq uses-stdin nil
)
1066 (push (shell-quote-argument
1067 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
1068 ((string= total
"%t")
1069 (push (shell-quote-argument (car type-list
)) out
))
1071 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
1072 (push (substring method beg
(length method
)) out
)
1075 (push (shell-quote-argument
1076 (gnus-map-function mm-path-name-rewrite-functions file
))
1078 (mapconcat 'identity
(nreverse out
) "")))
1080 (defun mm-remove-parts (handles)
1081 "Remove the displayed MIME parts represented by HANDLES."
1082 (if (and (listp handles
)
1083 (bufferp (car handles
)))
1084 (mm-remove-part handles
)
1086 (while (setq handle
(pop handles
))
1089 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1090 (kill-buffer (get-text-property 0 'buffer handle
))))
1091 ((and (listp handle
)
1092 (stringp (car handle
)))
1093 (mm-remove-parts (cdr handle
)))
1095 (mm-remove-part handle
)))))))
1097 (defun mm-destroy-parts (handles)
1098 "Remove the displayed MIME parts represented by HANDLES."
1099 (if (and (listp handles
)
1100 (bufferp (car handles
)))
1101 (mm-destroy-part handles
)
1103 (while (setq handle
(pop handles
))
1106 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1107 (kill-buffer (get-text-property 0 'buffer handle
))))
1108 ((and (listp handle
)
1109 (stringp (car handle
)))
1110 (mm-destroy-parts handle
))
1112 (mm-destroy-part handle
)))))))
1114 (defun mm-remove-part (handle)
1115 "Remove the displayed MIME part represented by HANDLE."
1116 (when (listp handle
)
1117 (let ((object (mm-handle-undisplayer handle
)))
1120 ;; Internally displayed part.
1121 ((mm-annotationp object
)
1122 (if (featurep 'xemacs
)
1123 (delete-annotation object
)))
1124 ((or (functionp object
)
1126 (eq (car object
) 'lambda
)))
1128 ;; Externally displayed part.
1131 (while (get-buffer-process (cdr object
))
1132 (interrupt-process (get-buffer-process (cdr object
)))
1133 (message "Waiting for external displayer to die...")
1137 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1138 (message "Waiting for external displayer to die...done")
1139 (ignore-errors (delete-file (car object
)))
1140 (ignore-errors (delete-directory (file-name-directory
1143 (when (buffer-live-p object
)
1144 (kill-buffer object
)))))
1145 (mm-handle-set-undisplayer handle nil
))))
1147 (defun mm-display-inline (handle)
1148 (let* ((type (mm-handle-media-type handle
))
1149 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1150 (funcall function handle
)
1151 (goto-char (point-min))))
1153 (defun mm-assoc-string-match (alist type
)
1154 (dolist (elem alist
)
1155 (when (string-match (car elem
) type
)
1158 (defun mm-automatic-display-p (handle)
1159 "Say whether the user wants HANDLE to be displayed automatically."
1160 (let ((methods mm-automatic-display
)
1161 (type (mm-handle-media-type handle
))
1163 (while (setq method
(pop methods
))
1164 (when (and (not (mm-inline-override-p handle
))
1165 (string-match method type
))
1170 (defun mm-inlinable-p (handle &optional type
)
1171 "Say whether HANDLE can be displayed inline.
1172 TYPE is the mime-type of the object; it defaults to the one given
1174 (unless type
(setq type
(mm-handle-media-type handle
)))
1175 (let ((alist mm-inline-media-tests
)
1178 (when (string-match (caar alist
) type
)
1179 (setq test
(caddar alist
)
1181 (setq test
(funcall test handle
)))
1185 (defun mm-inlined-p (handle)
1186 "Say whether the user wants HANDLE to be displayed inline."
1187 (let ((methods mm-inlined-types
)
1188 (type (mm-handle-media-type handle
))
1190 (while (setq method
(pop methods
))
1191 (when (and (not (mm-inline-override-p handle
))
1192 (string-match method type
))
1197 (defun mm-attachment-override-p (handle)
1198 "Say whether HANDLE should have attachment behavior overridden."
1199 (let ((types mm-attachment-override-types
)
1200 (type (mm-handle-media-type handle
))
1203 (while (setq ty
(pop types
))
1204 (when (and (string-match ty type
)
1205 (mm-inlinable-p handle
))
1206 (throw 'found t
))))))
1208 (defun mm-inline-override-p (handle)
1209 "Say whether HANDLE should have inline behavior overridden."
1210 (let ((types mm-inline-override-types
)
1211 (type (mm-handle-media-type handle
))
1214 (while (setq ty
(pop types
))
1215 (when (string-match ty type
)
1216 (throw 'found t
))))))
1218 (defun mm-automatic-external-display-p (type)
1219 "Return the user-defined method for TYPE."
1220 (let ((methods mm-automatic-external-display
)
1222 (while (setq method
(pop methods
))
1223 (when (string-match method type
)
1228 (defun mm-destroy-part (handle)
1229 "Destroy the data structures connected to HANDLE."
1230 (when (listp handle
)
1231 (mm-remove-part handle
)
1232 (when (buffer-live-p (mm-handle-buffer handle
))
1233 (kill-buffer (mm-handle-buffer handle
)))))
1235 (defun mm-handle-displayed-p (handle)
1236 "Say whether HANDLE is displayed or not."
1237 (mm-handle-undisplayer handle
))
1240 ;;; Functions for outputting parts
1243 (defmacro mm-with-part
(handle &rest forms
)
1244 "Run FORMS in the temp buffer containing the contents of HANDLE."
1245 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1246 ;; chars, so the buffer should be unibyte. It may happen that the
1247 ;; handle-buffer is multibyte for some reason, in which case now is a good
1248 ;; time to adjust it, since we know at this point that it should
1250 `(let* ((handle ,handle
))
1251 (when (and (mm-handle-buffer handle
)
1252 (buffer-name (mm-handle-buffer handle
)))
1254 (mm-disable-multibyte)
1255 (insert-buffer-substring (mm-handle-buffer handle
))
1256 (mm-decode-content-transfer-encoding
1257 (mm-handle-encoding handle
)
1258 (mm-handle-media-type handle
))
1260 (put 'mm-with-part
'lisp-indent-function
1)
1261 (put 'mm-with-part
'edebug-form-spec
'(body))
1263 (defun mm-get-part (handle &optional no-cache
)
1264 "Return the contents of HANDLE as a string.
1265 If NO-CACHE is non-nil, cached contents of a message/external-body part
1267 (if (and (not no-cache
)
1268 (equal (mm-handle-media-type handle
) "message/external-body"))
1270 (unless (mm-handle-cache handle
)
1271 (mm-extern-cache-contents handle
))
1272 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1274 (mm-with-part handle
1277 (defun mm-insert-part (handle &optional no-cache
)
1278 "Insert the contents of HANDLE in the current buffer.
1279 If NO-CACHE is non-nil, cached contents of a message/external-body part
1281 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1284 (with-current-buffer (mm-handle-buffer handle
)
1287 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1289 (mm-get-part handle no-cache
)))))
1295 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1297 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1298 ;; Separate the extracted parts that have the same faces.
1302 (defun mm-file-name-delete-whitespace (file-name)
1303 "Remove all whitespace characters from FILE-NAME."
1304 (while (string-match "\\s-+" file-name
)
1305 (setq file-name
(replace-match "" t t file-name
)))
1308 (defun mm-file-name-trim-whitespace (file-name)
1309 "Remove leading and trailing whitespace characters from FILE-NAME."
1310 (when (string-match "\\`\\s-+" file-name
)
1311 (setq file-name
(substring file-name
(match-end 0))))
1312 (when (string-match "\\s-+\\'" file-name
)
1313 (setq file-name
(substring file-name
0 (match-beginning 0))))
1316 (defun mm-file-name-collapse-whitespace (file-name)
1317 "Collapse multiple whitespace characters in FILE-NAME."
1318 (while (string-match "\\s-\\s-+" file-name
)
1319 (setq file-name
(replace-match " " t t file-name
)))
1322 (defun mm-file-name-replace-whitespace (file-name)
1323 "Replace whitespace characters in FILE-NAME with underscores.
1324 Set the option `mm-file-name-replace-whitespace' to any other
1325 string if you do not like underscores."
1326 (let ((s (or mm-file-name-replace-whitespace
"_")))
1327 (while (string-match "\\s-" file-name
)
1328 (setq file-name
(replace-match s t t file-name
))))
1331 (defun mm-file-name-delete-control (filename)
1332 "Delete control characters from FILENAME."
1333 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1335 (defun mm-file-name-delete-gotchas (filename)
1336 "Delete shell gotchas from FILENAME."
1337 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1338 (gnus-replace-in-string filename
"^[.-]+" ""))
1340 (defun mm-save-part (handle &optional prompt
)
1341 "Write HANDLE to a file.
1342 PROMPT overrides the default one used to ask user for a file name."
1343 (let ((filename (or (mail-content-type-get
1344 (mm-handle-disposition handle
) 'filename
)
1345 (mail-content-type-get
1346 (mm-handle-type handle
) 'name
)))
1349 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1350 (file-name-nondirectory filename
))))
1356 (format "Save MIME part to (default %s): "
1358 (or mm-default-directory default-directory
)
1359 (expand-file-name (or filename
"")
1360 (or mm-default-directory default-directory
))))
1361 (cond ((or (not file
) (equal file
""))
1362 (message "Please enter a file name")
1364 ((and (file-directory-p file
)
1366 (message "Please enter a non-directory file name")
1371 (if (file-directory-p file
)
1372 (setq file
(expand-file-name filename file
))
1373 (setq file
(expand-file-name
1374 file
(or mm-default-directory default-directory
))))
1375 (setq mm-default-directory
(file-name-directory file
))
1376 (and (or (not (file-exists-p file
))
1377 (yes-or-no-p (format "File %s already exists; overwrite? "
1380 (mm-save-part-to-file handle file
)
1383 (defun mm-add-meta-html-tag (handle &optional charset force-charset
)
1384 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1385 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1386 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1387 Return t if meta tag is added or replaced."
1388 (when (equal (mm-handle-media-type handle
) "text/html")
1390 (setq charset
(mail-content-type-get (mm-handle-type handle
)
1392 (setq charset
(format "\
1393 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset
))
1394 (let ((case-fold-search t
))
1395 (goto-char (point-min))
1396 (if (re-search-forward "\
1397 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1398 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t
)
1399 (if (and (not force-charset
)
1401 (string-match "\\`html\\'" (match-string 1)))
1402 ;; Don't modify existing meta tag.
1404 ;; Replace it with the one specifying charset.
1405 (replace-match charset
)
1407 (if (re-search-forward "<head>\\s-*" nil t
)
1408 (insert charset
"\n")
1409 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t
)
1410 (insert "<head>\n" charset
"\n</head>\n"))
1413 (defun mm-save-part-to-file (handle file
)
1414 (mm-with-unibyte-buffer
1415 (mm-insert-part handle
)
1416 (mm-add-meta-html-tag handle
)
1417 (let ((current-file-modes (default-file-modes)))
1418 (set-default-file-modes mm-attachment-file-modes
)
1420 ;; Don't re-compress .gz & al. Arguably we should make
1421 ;; `file-name-handler-alist' nil, but that would chop
1422 ;; ange-ftp, which is reasonable to use here.
1423 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1424 (set-default-file-modes current-file-modes
)))))
1426 (defun mm-pipe-part (handle &optional cmd
)
1427 "Pipe HANDLE to a process.
1428 Use CMD as the process."
1429 (let ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
1431 (gnus-read-shell-command
1432 "Shell command on MIME part: " mm-last-shell-command
))))
1433 (mm-with-unibyte-buffer
1434 (mm-insert-part handle
)
1435 (mm-add-meta-html-tag handle
)
1436 (let ((coding-system-for-write 'binary
))
1437 (shell-command-on-region (point-min) (point-max) command nil
)))))
1439 (autoload 'gnus-completing-read
"gnus-util")
1441 (defun mm-interactively-view-part (handle)
1442 "Display HANDLE using METHOD."
1443 (let* ((type (mm-handle-media-type handle
))
1445 (mapcar (lambda (i) (cdr (assoc 'viewer i
)))
1446 (mailcap-mime-info type
'all
)))
1447 (method (let ((minibuffer-local-completion-map
1448 mm-viewer-completion-map
))
1449 (completing-read "Viewer: " methods
))))
1450 (when (string= method
"")
1451 (error "No method given"))
1452 (if (string-match "^[^% \t]+$" method
)
1453 (setq method
(concat method
" %s")))
1454 (mm-display-external handle method
)))
1456 (defun mm-preferred-alternative (handles &optional preferred
)
1457 "Say which of HANDLES are preferred."
1458 (let ((prec (if preferred
(list preferred
)
1459 (mm-preferred-alternative-precedence handles
)))
1460 p h result type handle
)
1461 (while (setq p
(pop prec
))
1464 (setq handle
(car h
))
1465 (setq type
(mm-handle-media-type handle
))
1466 (when (and (equal p type
)
1467 (mm-automatic-display-p handle
)
1468 (or (stringp (car handle
))
1469 (not (mm-handle-disposition handle
))
1470 (equal (car (mm-handle-disposition handle
))
1478 (defun mm-preferred-alternative-precedence (handles)
1479 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1480 (setq handles
(reverse handles
))
1481 (dolist (disc (reverse mm-discouraged-alternatives
))
1482 (dolist (handle (copy-sequence handles
))
1483 (when (string-match disc
(mm-handle-media-type handle
))
1484 (setq handles
(nconc (delete handle handles
) (list handle
))))))
1485 ;; Remove empty parts.
1486 (dolist (handle (copy-sequence handles
))
1487 (when (and (bufferp (mm-handle-buffer handle
))
1488 (not (with-current-buffer (mm-handle-buffer handle
)
1489 (goto-char (point-min))
1490 (re-search-forward "[^ \t\n]" nil t
))))
1491 (setq handles
(nconc (delete handle handles
) (list handle
)))))
1492 (mapcar #'mm-handle-media-type handles
))
1494 (defun mm-get-content-id (id)
1495 "Return the handle(s) referred to by ID."
1496 (cdr (assoc id mm-content-id-alist
)))
1498 (defconst mm-image-type-regexps
1499 '(("/\\*.*XPM.\\*/" . xpm
)
1503 ("\211PNG\r\n" . png
)
1505 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1506 ("%!PS" . postscript
))
1507 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1508 When the first bytes of an image file match REGEXP, it is assumed to
1509 be of image type IMAGE-TYPE.")
1511 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1512 (defun mm-image-type-from-buffer ()
1513 "Determine the image type from data in the current buffer.
1514 Value is a symbol specifying the image type or nil if type cannot
1516 (let ((types mm-image-type-regexps
)
1518 (goto-char (point-min))
1519 (while (and types
(null type
))
1520 (let ((regexp (car (car types
)))
1521 (image-type (cdr (car types
))))
1522 (when (looking-at regexp
)
1523 (setq type image-type
))
1524 (setq types
(cdr types
))))
1527 (defun mm-get-image (handle)
1528 "Return an image instance based on HANDLE."
1529 (let ((type (mm-handle-media-subtype handle
))
1531 ;; Allow some common translations.
1534 ((equal type
"x-pixmap")
1536 ((equal type
"x-xbitmap")
1538 ((equal type
"x-portable-bitmap")
1541 (or (mm-handle-cache handle
)
1542 (mm-with-unibyte-buffer
1543 (mm-insert-part handle
)
1547 ;; Avoid testing `make-glyph' since W3 may define
1548 ;; a bogus version of it.
1549 (if (fboundp 'create-image
)
1550 (create-image (buffer-string)
1551 (or (mm-image-type-from-buffer)
1554 (mm-create-image-xemacs type
))))
1555 (mm-handle-set-cache handle spec
))))))
1557 (defun mm-create-image-xemacs (type)
1558 (when (featurep 'xemacs
)
1561 ;; xbm images require special handling, since
1562 ;; the only way to create glyphs from these
1563 ;; (without a ton of work) is to write them
1564 ;; out to a file, and then create a file
1566 (let ((file (mm-make-temp-file
1567 (expand-file-name "emm" mm-tmp-directory
)
1571 (write-region (point-min) (point-max) file
)
1572 (make-glyph (list (cons 'x file
))))
1574 (delete-file file
)))))
1578 (or (mm-image-type-from-buffer)
1580 :data
(buffer-string)))))))
1582 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1584 (defun mm-image-fit-p (handle)
1585 "Say whether the image in HANDLE will fit the current window."
1586 (let ((image (mm-get-image handle
)))
1588 (if (featurep 'xemacs
)
1589 ;; XEmacs's glyphs can actually tell us about their width, so
1590 ;; let's be nice and smart about them.
1591 (or mm-inline-large-images
1592 (and (<= (glyph-width image
) (window-pixel-width))
1593 (<= (glyph-height image
) (window-pixel-height))))
1594 (let* ((size (image-size image
))
1597 (or mm-inline-large-images
1598 (and (<= h
(1- (window-height))) ; Don't include mode line.
1599 (<= w
(window-width)))))))))
1601 (defun mm-valid-image-format-p (format)
1602 "Say whether FORMAT can be displayed natively by Emacs."
1605 ((fboundp 'valid-image-instantiator-format-p
)
1606 (valid-image-instantiator-format-p format
))
1608 ((fboundp 'image-type-available-p
)
1609 (and (display-graphic-p)
1610 (image-type-available-p format
)))
1611 ;; Nobody else can do images yet.
1615 (defun mm-valid-and-fit-image-p (format handle
)
1616 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1617 (and (mm-valid-image-format-p format
)
1618 (mm-image-fit-p handle
)))
1620 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1621 "Search in HANDLES for part with TYPE.
1622 If NOTP, returns first non-matching part.
1623 If RECURSIVE, search recursively."
1626 (if (and recursive
(stringp (caar handles
)))
1627 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1631 (not (equal (mm-handle-media-type (car handles
)) type
))
1632 (equal (mm-handle-media-type (car handles
)) type
))
1633 (setq handle
(car handles
)
1635 (setq handles
(cdr handles
)))
1638 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1639 (goto-char (point-min))
1640 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1642 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1644 (end (save-excursion
1645 (goto-char (point-max))
1646 (if (re-search-backward close-delimiter nil t
)
1650 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1651 (while (and (not result
)
1652 (re-search-forward boundary end t
))
1653 (goto-char (match-beginning 0))
1657 (narrow-to-region start
(1- (point)))
1658 (when (let* ((ct (mail-fetch-field "content-type"))
1659 (ctl (and ct
(mail-header-parse-content-type ct
))))
1661 (not (equal (car ctl
) type
))
1662 (equal (car ctl
) type
)))
1663 (setq result
(buffer-string))))))
1665 (setq start
(point)))
1666 (when (and (not result
) start
)
1669 (narrow-to-region start end
)
1670 (when (let* ((ct (mail-fetch-field "content-type"))
1671 (ctl (and ct
(mail-header-parse-content-type ct
))))
1673 (not (equal (car ctl
) type
))
1674 (equal (car ctl
) type
)))
1675 (setq result
(buffer-string))))))
1678 (defvar mm-security-handle nil
)
1680 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1681 ;; HANDLE could be a CTL.
1683 (put-text-property 0 (length (car handle
)) parameter value
1686 (autoload 'mm-view-pkcs7
"mm-view")
1688 (defun mm-possibly-verify-or-decrypt (parts ctl
&optional from
)
1689 (let ((type (car ctl
))
1690 (subtype (cadr (split-string (car ctl
) "/")))
1691 (mm-security-handle ctl
) ;; (car CTL) is the type.
1692 protocol func functest
)
1694 ((or (equal type
"application/x-pkcs7-mime")
1695 (equal type
"application/pkcs7-mime"))
1698 ((eq mm-decrypt-option
'never
) nil
)
1699 ((eq mm-decrypt-option
'always
) t
)
1700 ((eq mm-decrypt-option
'known
) t
)
1702 (format "Decrypt (S/MIME) part? "))))
1703 (mm-view-pkcs7 parts from
))
1704 (setq parts
(mm-dissect-buffer t
)))))
1705 ((equal subtype
"signed")
1706 (unless (and (setq protocol
1707 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1708 (not (equal protocol
"multipart/mixed")))
1709 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1710 (let ((protocols mm-verify-function-alist
))
1712 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1713 (funcall functest parts ctl
))
1714 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1715 (setq protocol
(caar protocols
)
1717 (setq protocols
(cdr protocols
))))))
1718 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1720 ((eq mm-verify-option
'never
) nil
)
1721 ((eq mm-verify-option
'always
) t
)
1722 ((eq mm-verify-option
'known
)
1724 (or (not (setq functest
1725 (nth 3 (assoc protocol
1726 mm-verify-function-alist
))))
1727 (funcall functest parts ctl
))))
1730 (format "Verify signed (%s) part? "
1731 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1732 (format "protocol=%s" protocol
))))))
1735 (setq parts
(funcall func parts ctl
))
1736 (mm-set-handle-multipart-parameter
1737 mm-security-handle
'gnus-details
1738 (format "Unknown sign protocol (%s)" protocol
))))))
1739 ((equal subtype
"encrypted")
1740 (unless (setq protocol
1741 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1742 ;; The message is broken.
1743 (let ((parts parts
))
1745 (if (assoc (mm-handle-media-type (car parts
))
1746 mm-decrypt-function-alist
)
1747 (setq protocol
(mm-handle-media-type (car parts
))
1749 (setq parts
(cdr parts
))))))
1750 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1752 ((eq mm-decrypt-option
'never
) nil
)
1753 ((eq mm-decrypt-option
'always
) t
)
1754 ((eq mm-decrypt-option
'known
)
1756 (or (not (setq functest
1757 (nth 3 (assoc protocol
1758 mm-decrypt-function-alist
))))
1759 (funcall functest parts ctl
))))
1762 (format "Decrypt (%s) part? "
1763 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1764 (format "protocol=%s" protocol
))))))
1767 (setq parts
(funcall func parts ctl
))
1768 (mm-set-handle-multipart-parameter
1769 mm-security-handle
'gnus-details
1770 (format "Unknown encrypt protocol (%s)" protocol
))))))
1774 (defun mm-multiple-handles (handles)
1775 (and (listp handles
)
1776 (> (length handles
) 1)
1777 (or (listp (car handles
))
1778 (stringp (car handles
)))))
1780 (defun mm-complicated-handles (handles)
1781 (and (listp (car handles
))
1782 (> (length handles
) 1)))
1784 (defun mm-merge-handles (handles1 handles2
)
1786 (if (listp (car handles1
))
1789 (if (listp (car handles2
))
1793 (defun mm-readable-p (handle)
1794 "Say whether the content of HANDLE is readable."
1795 (and (< (with-current-buffer (mm-handle-buffer handle
)
1796 (buffer-size)) 10000)
1797 (mm-with-unibyte-buffer
1798 (mm-insert-part handle
)
1799 (and (eq (mm-body-7-or-8) '7bit
)
1800 (not (mm-long-lines-p 76))))))
1802 (declare-function libxml-parse-html-region
"xml.c"
1803 (start end
&optional base-url
))
1804 (declare-function shr-insert-document
"shr" (dom))
1805 (defvar shr-blocked-images
)
1806 (defvar gnus-inhibit-images
)
1807 (autoload 'gnus-blocked-images
"gnus-art")
1809 (defun mm-shr (handle)
1810 ;; Require since we bind its variables.
1812 (let ((article-buffer (current-buffer))
1813 (shr-content-function (lambda (id)
1814 (let ((handle (mm-get-content-id id
)))
1816 (mm-with-part handle
1817 (buffer-string))))))
1818 shr-inhibit-images shr-blocked-images charset char
)
1819 (if (and (boundp 'gnus-summary-buffer
)
1820 (bufferp gnus-summary-buffer
)
1821 (buffer-name gnus-summary-buffer
))
1822 (with-current-buffer gnus-summary-buffer
1823 (setq shr-inhibit-images gnus-inhibit-images
1824 shr-blocked-images
(gnus-blocked-images)))
1825 (setq shr-inhibit-images gnus-inhibit-images
1826 shr-blocked-images
(gnus-blocked-images)))
1828 (setq handle
(mm-dissect-buffer t
)))
1829 (setq charset
(mail-content-type-get (mm-handle-type handle
) 'charset
))
1831 (narrow-to-region (point) (point))
1832 (shr-insert-document
1833 (mm-with-part handle
1837 (mm-charset-to-coding-system charset
1839 (not (eq charset
'ascii
)))
1840 (mm-decode-coding-string (buffer-string) charset
)
1841 (mm-string-as-multibyte (buffer-string)))
1843 (mm-enable-multibyte)))
1844 (goto-char (point-min))
1845 (setq case-fold-search t
)
1846 (while (re-search-forward
1847 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t
)
1849 (cdr (assq (if (match-beginning 1)
1850 (string-to-number (match-string 1) 16)
1851 (string-to-number (match-string 2)))
1852 mm-extra-numeric-entities
)))
1853 (replace-match (char-to-string char
))))
1854 ;; Remove "soft hyphens".
1855 (goto-char (point-min))
1856 (while (search-forward "" nil t
)
1857 (replace-match "" t t
))
1858 (libxml-parse-html-region (point-min) (point-max))))
1861 (mm-convert-shr-links)
1862 (mm-handle-set-undisplayer
1865 (let ((inhibit-read-only t
))
1866 (delete-region ,(point-min-marker)
1867 ,(point-max-marker))))))))
1871 (autoload 'widget-convert-button
"wid-edit")
1873 (defun mm-convert-shr-links ()
1874 (let ((start (point-min))
1877 (< start
(point-max)))
1878 (when (setq start
(text-property-not-all start
(point-max) 'shr-url nil
))
1879 (setq end
(next-single-property-change start
'shr-url nil
(point-max)))
1880 (widget-convert-button
1882 :help-echo
(get-text-property start
'help-echo
)
1884 (get-text-property start
'shr-url
))
1885 (put-text-property start end
'local-map nil
)
1886 (setq start end
)))))
1888 (defun mm-handle-filename (handle)
1889 "Return filename of HANDLE if any."
1890 (or (mail-content-type-get (mm-handle-type handle
)
1892 (mail-content-type-get (mm-handle-disposition handle
)
1895 (provide 'mm-decode
)
1901 ;;; mm-decode.el ends here