1 ;;; mm-decode.el --- Functions for decoding MIME things -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998-2018 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 <https://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl-lib
))
30 (autoload 'gnus-map-function
"gnus-util")
32 (autoload 'mm-inline-partial
"mm-partial")
33 (autoload 'mm-inline-external-body
"mm-extern")
34 (autoload 'mm-extern-cache-contents
"mm-extern")
35 (autoload 'mm-insert-inline
"mm-view")
37 (autoload 'mm-archive-decoders
"mm-archive")
38 (autoload 'mm-archive-dissect-and-inline
"mm-archive")
39 (autoload 'mm-dissect-archive
"mm-archive")
41 (defvar gnus-current-window-configuration
)
43 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
44 (add-hook 'gnus-exit-gnus-hook
'mm-temp-files-delete
)
46 (defgroup mime-display
()
47 "Display of MIME in mail and news articles."
48 :link
'(custom-manual "(emacs-mime)Display Customization")
54 (defgroup mime-security
()
55 "MIME security in mail and news articles."
56 :link
'(custom-manual "(emacs-mime)Display Customization")
61 (defface mm-command-output
64 (:foreground
"ForestGreen"))
70 "Face used for displaying output from commands."
73 ;;; Convenience macros.
75 (defmacro mm-handle-buffer
(handle)
77 (defmacro mm-handle-type
(handle)
79 (defsubst mm-handle-media-type
(handle)
80 (if (stringp (car handle
))
82 (car (mm-handle-type handle
))))
83 (defsubst mm-handle-media-supertype
(handle)
84 (car (split-string (mm-handle-media-type handle
) "/")))
85 (defsubst mm-handle-media-subtype
(handle)
86 (cadr (split-string (mm-handle-media-type handle
) "/")))
87 (defmacro mm-handle-encoding
(handle)
89 (defmacro mm-handle-undisplayer
(handle)
91 (defmacro mm-handle-set-undisplayer
(handle function
)
92 `(setcar (nthcdr 3 ,handle
) ,function
))
93 (defmacro mm-handle-disposition
(handle)
95 (defmacro mm-handle-description
(handle)
97 (defmacro mm-handle-cache
(handle)
99 (defmacro mm-handle-set-cache
(handle contents
)
100 `(setcar (nthcdr 6 ,handle
) ,contents
))
101 (defmacro mm-handle-id
(handle)
103 (defmacro mm-handle-multipart-original-buffer
(handle)
104 `(get-text-property 0 'buffer
(car ,handle
)))
105 (defmacro mm-handle-multipart-from
(handle)
106 `(get-text-property 0 'from
(car ,handle
)))
107 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
108 `(get-text-property 0 ,parameter
(car ,handle
)))
110 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
111 disposition description cache
113 `(list ,buffer
,type
,encoding
,undisplayer
114 ,disposition
,description
,cache
,id
))
116 (defcustom mm-text-html-renderer
117 (cond ((fboundp 'libxml-parse-html-region
) 'shr
)
118 ((executable-find "w3m") 'gnus-w3m
)
119 ((executable-find "links") 'links
)
120 ((executable-find "lynx") 'lynx
)
121 ((locate-library "html2text") 'html2text
))
122 "Render of HTML contents.
123 It is one of defined renderer types, or a rendering function.
124 The defined renderer types are:
125 `shr': use the built-in Gnus HTML renderer;
126 `gnus-w3m': use Gnus renderer based on w3m;
127 `w3m': use emacs-w3m;
128 `w3m-standalone': use plain w3m;
131 `html2text': use html2text."
133 :type
'(choice (const shr
)
135 (const w3m
:tag
"emacs-w3m")
136 (const w3m-standalone
:tag
"standalone w3m" )
141 :group
'mime-display
)
143 (defcustom mm-html-inhibit-images nil
144 "Non-nil means inhibit displaying of images inline in the article body."
147 :group
'mime-display
)
149 (defcustom mm-html-blocked-images
""
150 "Regexp matching image URLs to be blocked, or nil meaning not to block.
151 Note that cid images that are embedded in a message won't be blocked."
153 :type
'(choice (const :tag
"Allow all" nil
)
154 (regexp :tag
"Regular expression"))
155 :group
'mime-display
)
157 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
158 "Regexp matching URLs which are considered to be safe.
159 Some HTML mails might contain a nasty trick used by spammers, using
160 the <img> tag which is far more evil than the [Click Here!] button.
161 It is most likely intended to check whether the ominous spam mail has
162 reached your eyes or not, in which case the spammer knows for sure
163 that your email address is valid. It is done by embedding an
164 identifier string into a URL that you might automatically retrieve
165 when displaying the image. The default value is \"\\\\`cid:\" which only
166 matches parts embedded to the Multipart/Related type MIME contents and
167 Gnus will never connect to the spammer's site arbitrarily. You may
168 set this variable to nil if you consider all urls to be safe."
170 :type
'(choice (regexp :tag
"Regexp")
171 (const :tag
"All URLs are safe" nil
))
172 :group
'mime-display
)
174 (defcustom mm-inline-text-html-with-w3m-keymap t
175 "If non-nil, use emacs-w3m command keys in the article buffer."
178 :group
'mime-display
)
180 (defcustom mm-enable-external t
181 "Indicate whether external MIME handlers should be used.
183 If t, all defined external MIME handlers are used. If nil, files are saved by
184 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
185 before the external MIME handler is invoked."
187 :type
'(choice (const :tag
"Always" t
)
188 (const :tag
"Never" nil
)
189 (const :tag
"Ask" ask
))
190 :group
'mime-display
)
192 (defcustom mm-inline-media-tests
196 (mm-valid-and-fit-image-p 'jpeg handle
)))
200 (mm-valid-and-fit-image-p 'png handle
)))
204 (mm-valid-and-fit-image-p 'gif handle
)))
208 (mm-valid-and-fit-image-p 'tiff handle
)))
212 (mm-valid-and-fit-image-p 'xbm handle
)))
216 (mm-valid-and-fit-image-p 'xbm handle
)))
220 (mm-valid-and-fit-image-p 'xpm handle
)))
224 (mm-valid-and-fit-image-p 'xpm handle
)))
228 (mm-valid-and-fit-image-p 'bmp handle
)))
229 ("image/x-portable-bitmap"
232 (mm-valid-and-fit-image-p 'pbm handle
)))
233 ("text/plain" mm-inline-text identity
)
234 ("text/enriched" mm-inline-text identity
)
235 ("text/richtext" mm-inline-text identity
)
236 ("text/x-patch" mm-display-patch-inline identity
)
237 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
238 ("text/x-diff" mm-display-patch-inline identity
)
239 ("application/emacs-lisp" mm-display-elisp-inline identity
)
240 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
241 ("application/x-shellscript" mm-display-shell-script-inline identity
)
242 ("application/x-sh" mm-display-shell-script-inline identity
)
243 ("text/x-sh" mm-display-shell-script-inline identity
)
244 ("application/javascript" mm-display-javascript-inline identity
)
245 ("text/dns" mm-display-dns-inline identity
)
246 ("text/x-org" mm-display-org-inline identity
)
250 mm-text-html-renderer
))
254 (or (featurep 'vcard
)
255 (locate-library "vcard"))))
256 ("message/delivery-status" mm-inline-text identity
)
257 ("message/rfc822" mm-inline-message identity
)
258 ("message/partial" mm-inline-partial identity
)
259 ("message/external-body" mm-inline-external-body identity
)
260 ("text/.*" mm-inline-text identity
)
261 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity
)
262 ("application/zip" mm-archive-dissect-and-inline identity
)
263 ("audio/wav" mm-inline-audio
265 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
266 (device-sound-enabled-p))))
270 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
271 (device-sound-enabled-p))))
272 ("application/pgp-signature" ignore identity
)
273 ("application/x-pkcs7-signature" ignore identity
)
274 ("application/pkcs7-signature" ignore identity
)
275 ("application/x-pkcs7-mime" ignore identity
)
276 ("application/pkcs7-mime" ignore identity
)
277 ("multipart/alternative" ignore identity
)
278 ("multipart/mixed" ignore identity
)
279 ("multipart/related" ignore identity
)
283 (and (mm-valid-image-format-p 'imagemagick
)
284 (mm-with-unibyte-buffer
285 (mm-insert-part handle
)
288 (create-image (buffer-string) 'imagemagick
'data-p
))))
290 (setcar (cdr handle
) (list "image/imagemagick"))
291 (mm-image-fit-p handle
)))))))
292 ;; Disable audio and image
293 ("audio/.*" ignore ignore
)
294 ("image/.*" ignore ignore
)
295 ;; Default to displaying as text
296 (".*" mm-inline-text mm-readable-p
))
297 "Alist of media types/tests saying whether types can be displayed inline."
298 :type
'(repeat (list (regexp :tag
"MIME type")
299 (function :tag
"Display function")
300 (function :tag
"Display test")))
301 :group
'mime-display
)
303 (defcustom mm-inlined-types
304 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
305 "message/partial" "message/external-body" "application/emacs-lisp"
306 "application/x-emacs-lisp"
307 "application/pgp-signature" "application/x-pkcs7-signature"
308 "application/pkcs7-signature" "application/x-pkcs7-mime"
309 "application/pkcs7-mime"
310 "application/x-gtar-compressed"
313 ;; Mutt still uses this even though it has already been withdrawn.
315 "List of media types that are to be displayed inline.
316 See also `mm-inline-media-tests', which says how to display a media
318 :type
'(repeat regexp
)
319 :group
'mime-display
)
321 (defcustom mm-keep-viewer-alive-types
322 '("application/postscript" "application/msword" "application/vnd.ms-excel"
323 "application/pdf" "application/x-dvi"
325 "Media types for viewers not to be killed when selecting a different article.
326 Instead the viewers will be killed on Gnus exit instead. This is
329 :type
'(repeat regexp
)
330 :group
'mime-display
)
332 (defcustom mm-automatic-display
333 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
334 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
335 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
336 "application/emacs-lisp" "application/x-emacs-lisp"
337 "application/x-pkcs7-signature"
338 "application/pkcs7-signature" "application/x-pkcs7-mime"
339 "application/pkcs7-mime"
340 ;; Mutt still uses this even though it has already been withdrawn.
343 "A list of MIME types to be displayed automatically."
344 :type
'(repeat regexp
)
345 :group
'mime-display
)
347 (defcustom mm-attachment-override-types
'("text/x-vcard"
348 "application/pkcs7-mime"
349 "application/x-pkcs7-mime"
350 "application/pkcs7-signature"
351 "application/x-pkcs7-signature")
352 "Types to have \"attachment\" ignored if they can be displayed inline."
353 :type
'(repeat regexp
)
354 :group
'mime-display
)
356 (defcustom mm-inline-override-types nil
357 "Types to be treated as attachments even if they can be displayed inline."
358 :type
'(repeat regexp
)
359 :group
'mime-display
)
361 (defcustom mm-automatic-external-display nil
362 "List of MIME type regexps that will be displayed externally automatically."
363 :type
'(repeat regexp
)
364 :group
'mime-display
)
366 (defcustom mm-discouraged-alternatives nil
367 "List of MIME types that are discouraged when viewing multipart/alternative.
368 Viewing agents are supposed to view the last possible part of a message,
369 as that is supposed to be the richest. However, users may prefer other
370 types instead, and this list says what types are most unwanted. If,
371 for instance, text/html parts are very unwanted, and text/richtext are
372 somewhat unwanted, then the value of this variable should be set
375 (\"text/html\" \"text/richtext\")
377 Adding \"image/.*\" might also be useful. Spammers use it as the
378 preferred part of multipart/alternative messages. See also
379 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
380 enables you to choose manually one of two types those mails include."
381 :type
'(repeat regexp
) ;; See `mm-preferred-alternative-precedence'.
382 :group
'mime-display
)
384 (defcustom mm-tmp-directory temporary-file-directory
385 "Where mm will store its temporary files."
387 :group
'mime-display
)
389 (defcustom mm-inline-large-images nil
390 "If t, then all images fit in the buffer.
391 If `resize', try to resize the images so they fit."
393 (const :tag
"Inline large images as they are." t
)
394 (const :tag
"Resize large images." resize
)
395 (const :tag
"Do not inline large images." nil
))
396 :group
'mime-display
)
398 (defcustom mm-file-name-rewrite-functions
399 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
400 "List of functions used for rewriting file names of MIME parts.
401 Each function takes a file name as input and returns a file name.
403 Ready-made functions include `mm-file-name-delete-control',
404 `mm-file-name-delete-gotchas' (you should not remove these two
405 functions), `mm-file-name-delete-whitespace',
406 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
407 `mm-file-name-replace-whitespace', `capitalize', `downcase',
408 `upcase', and `upcase-initials'."
409 :type
'(list (set :inline t
410 (const mm-file-name-delete-control
)
411 (const mm-file-name-delete-gotchas
)
412 (const mm-file-name-delete-whitespace
)
413 (const mm-file-name-trim-whitespace
)
414 (const mm-file-name-collapse-whitespace
)
415 (const mm-file-name-replace-whitespace
)
419 (const upcase-initials
)
423 :version
"23.1" ;; No Gnus
424 :group
'mime-display
)
427 (defcustom mm-path-name-rewrite-functions nil
428 "List of functions for rewriting the full file names of MIME parts.
429 This is used when viewing parts externally, and is meant for
430 transforming the absolute name so that non-compliant programs can find
431 the file where it's saved.
433 Each function takes a file name as input and returns a file name."
434 :type
'(repeat function
)
435 :group
'mime-display
)
437 (defvar mm-file-name-replace-whitespace nil
438 "String used for replacing whitespace characters; default is `\"_\"'.")
440 (defcustom mm-default-directory nil
441 "The default directory where mm will save files.
442 If not set, `default-directory' will be used."
443 :type
'(choice directory
(const :tag
"Default" nil
))
444 :group
'mime-display
)
446 (defcustom mm-attachment-file-modes
384
447 "Set the mode bits of saved attachments to this integer."
450 :group
'mime-display
)
452 (defcustom mm-external-terminal-program
"xterm"
453 "The program to start an external terminal."
456 :group
'mime-display
)
458 ;;; Internal variables.
460 (defvar mm-last-shell-command
"")
461 (defvar mm-content-id-alist nil
)
462 (defvar mm-postponed-undisplay-list nil
)
463 (defvar mm-inhibit-auto-detect-attachment nil
)
464 (defvar mm-temp-files-to-be-deleted nil
465 "List of temporary files scheduled to be deleted.")
466 (defvar mm-temp-files-cache-file
(concat ".mm-temp-files-" (user-login-name))
467 "Name of a file that caches a list of temporary files to be deleted.
468 The file will be saved in the directory `mm-tmp-directory'.")
470 ;; According to RFC2046, in particular, in a digest, the default
471 ;; Content-Type value for a body part is changed from "text/plain" to
473 (defvar mm-dissect-default-type
"text/plain")
475 (autoload 'mml2015-verify
"mml2015")
476 (autoload 'mml2015-verify-test
"mml2015")
477 (autoload 'mml-smime-verify
"mml-smime")
478 (autoload 'mml-smime-verify-test
"mml-smime")
480 (defvar mm-verify-function-alist
481 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
482 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
483 mm-uu-pgp-signed-test
)
484 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
485 mml-smime-verify-test
)
486 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
487 mml-smime-verify-test
)))
489 (defcustom mm-verify-option
'never
490 "Option of verifying signed parts.
491 `never', not verify; `always', always verify;
492 `known', only verify known protocols. Otherwise, ask user.
494 When set to `always' or `known', you should add
495 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
496 result of the verification."
498 :type
'(choice (item always
)
500 (item :tag
"only known protocols" known
)
501 (item :tag
"ask" nil
))
502 :group
'mime-security
)
504 (autoload 'mml2015-decrypt
"mml2015")
505 (autoload 'mml2015-decrypt-test
"mml2015")
507 (defvar mm-decrypt-function-alist
508 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
509 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
510 mm-uu-pgp-encrypted-test
)))
512 (defcustom mm-decrypt-option nil
513 "Option of decrypting encrypted parts.
514 `never', not decrypt; `always', always decrypt;
515 `known', only decrypt known protocols. Otherwise, ask user."
517 :type
'(choice (item always
)
519 (item :tag
"only known protocols" known
)
520 (item :tag
"ask" nil
))
521 :group
'mime-security
)
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."
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 (defun mm-temp-files-delete ()
578 "Delete temporary files and those parent directories.
579 Note that the deletion may fail if a program is catching hold of a file
580 under Windows or Cygwin. In that case, it schedules the deletion of
581 files left at the next time."
582 (let* ((coding-system-for-read mm-universal-coding-system
)
583 (coding-system-for-write mm-universal-coding-system
)
584 (cache-file (expand-file-name mm-temp-files-cache-file
586 (cache (when (file-exists-p cache-file
)
587 (mm-with-multibyte-buffer
588 (insert-file-contents cache-file
)
589 (split-string (buffer-string) "\n" t
))))
591 (dolist (temp (append cache mm-temp-files-to-be-deleted
))
592 (when (and (file-exists-p temp
)
593 (if (file-directory-p temp
)
594 ;; A parent directory left at the previous time.
596 (ignore-errors (delete-directory temp
))
597 (file-exists-p temp
))
598 ;; Delete a temporary file and its parent directory.
599 (ignore-errors (delete-file temp
))
600 (or (file-exists-p temp
)
602 (setq temp
(file-name-directory temp
))
603 (ignore-errors (delete-directory temp
))
604 (file-exists-p temp
)))))
607 ;; Schedule the deletion of the files left at the next time.
609 (write-region (concat (mapconcat 'identity
(nreverse fails
) "\n")
611 nil cache-file nil
'silent
)
612 (set-file-modes cache-file
#o600
))
613 (when (file-exists-p cache-file
)
614 (ignore-errors (delete-file cache-file
))))
615 (setq mm-temp-files-to-be-deleted nil
)))
617 (autoload 'message-fetch-field
"message")
619 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
620 "Dissect the current buffer and return a list of MIME handles.
621 If NO-STRICT-MIME, don't require the message to have a
622 MIME-Version header before proceeding."
624 (let (ct ctl type subtype cte cd description id result
)
626 (mail-narrow-to-head)
627 (when (or no-strict-mime
629 (mail-fetch-field "mime-version"))
630 (setq ct
(mail-fetch-field "content-type")
631 ctl
(and ct
(mail-header-parse-content-type ct
))
632 cte
(mail-fetch-field "content-transfer-encoding")
633 cd
(or (mail-fetch-field "content-disposition")
636 (cadr (mm-assoc-string-match
637 mm-inline-media-tests
640 ;; Newlines in description should be stripped so as
641 ;; not to break the MIME tag into two or more lines.
642 description
(message-fetch-field "content-description")
643 id
(mail-fetch-field "content-id"))
645 (setq from
(mail-fetch-field "from")))
646 ;; FIXME: In some circumstances, this code is running within
647 ;; a unibyte macro. mail-extract-address-components
648 ;; creates unibyte buffers. This `if', though not a perfect
649 ;; solution, avoids most of them.
651 (setq from
(cadr (mail-extract-address-components from
))))
653 (setq description
(mail-decode-encoded-word-string
656 (not (string-match "/" (car ctl
))))
657 (mm-dissect-singlepart
658 (list mm-dissect-default-type
)
659 (and cte
(intern (downcase (mail-header-strip-cte cte
))))
661 (and cd
(mail-header-parse-content-disposition cd
))
663 (setq type
(split-string (car ctl
) "/"))
664 (setq subtype
(cadr type
)
669 ((equal type
"multipart")
670 (let ((mm-dissect-default-type (if (equal subtype
"digest")
673 (start (cdr (assq 'start
(cdr ctl
)))))
674 (add-text-properties 0 (length (car ctl
))
675 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
677 ;; what really needs to be done here is a way to link a
678 ;; MIME handle back to its parent MIME handle (in a multilevel
679 ;; MIME article). That would probably require changing
680 ;; the mm-handle API so we simply store the multipart buffer
681 ;; name as a text property of the "multipart/whatever" string.
682 (add-text-properties 0 (length (car ctl
))
683 (list 'buffer
(mm-copy-to-buffer)
687 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
689 (mm-possibly-verify-or-decrypt
690 (mm-dissect-singlepart
692 (and cte
(intern (downcase (mail-header-strip-cte cte
))))
694 (and cd
(mail-header-parse-content-disposition cd
))
698 (when (string-match " *<\\(.*\\)> *" id
)
699 (setq id
(match-string 1 id
)))
700 (push (cons id result
) mm-content-id-alist
))
703 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
705 (if (equal "text/plain" (car ctl
))
708 ;; Guess what the type of application/octet-stream parts should
710 (let ((filename (cdr (assq 'filename
(cdr cdl
)))))
711 (when (and (not mm-inhibit-auto-detect-attachment
)
712 (equal (car ctl
) "application/octet-stream")
714 (string-match "\\.\\([^.]+\\)$" filename
))
715 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename
))))
717 (setcar ctl new-type
)))))
720 (mm-copy-to-buffer) ctl cte nil cdl description nil id
))
721 (decoder (assoc (car ctl
) (mm-archive-decoders))))
723 ;; Do automatic decoding
725 (executable-find (caddr decoder
)))
726 (mm-dissect-archive handle
)
729 (defun mm-dissect-multipart (ctl from
)
730 (goto-char (point-min))
731 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
732 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
735 (goto-char (point-max))
736 (if (re-search-backward close-delimiter nil t
)
739 (mm-inhibit-auto-detect-attachment
740 (equal (car ctl
) "multipart/encrypted")))
741 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
742 (while (and (< (point) end
) (re-search-forward boundary end t
))
743 (goto-char (match-beginning 0))
747 (narrow-to-region start
(point))
748 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
750 (or (looking-at boundary
)
752 (setq start
(point)))
753 (when (and start
(< start end
))
756 (narrow-to-region start end
)
757 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
758 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl from
)))
760 (defun mm-copy-to-buffer ()
761 "Copy the contents of the current buffer to a fresh buffer."
762 (let ((obuf (current-buffer))
763 (mb enable-multibyte-characters
)
765 (goto-char (point-min))
766 (search-forward-regexp "^\n" nil t
)
769 (generate-new-buffer " *mm*")
770 ;; Preserve the data's unibyteness (for url-insert-file-contents).
771 (set-buffer-multibyte mb
)
772 (insert-buffer-substring obuf beg
)
775 (defun mm-display-parts (handle)
777 ((stringp (car handle
)) (mapcar #'mm-display-parts
(cdr handle
)))
778 ((bufferp (car handle
))
780 (narrow-to-region (point) (point))
781 (mm-display-part handle
)
782 (goto-char (point-max))))
784 (mapcar #'mm-display-parts handle
))))
786 (autoload 'mailcap-parse-mailcaps
"mailcap")
787 (autoload 'mailcap-mime-info
"mailcap")
789 (defun mm-head-p (&optional point
)
790 "Return non-nil if point is in the article header."
791 (let ((point (or point
(point))))
794 (and (not (re-search-backward "^$" nil t
))
795 (re-search-forward "^$" nil t
)))))
797 (defun mm-display-part (handle &optional no-default force
)
798 "Display the MIME part represented by HANDLE.
799 Returns nil if the part is removed; inline if displayed inline;
800 external if displayed external."
802 (mailcap-parse-mailcaps)
804 (mm-handle-displayed-p handle
))
805 (mm-remove-part handle
)
806 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
807 "message/external-body")
809 (unless (mm-handle-cache handle
)
810 (mm-extern-cache-contents handle
))
811 (mm-handle-cache handle
))
813 (type (mm-handle-media-type ehandle
))
814 (method (mailcap-mime-info type
))
815 (filename (or (mail-content-type-get
816 (mm-handle-disposition handle
) 'filename
)
817 (mail-content-type-get
818 (mm-handle-type handle
) 'name
)
820 (external mm-enable-external
)
821 (decoder (assoc (car (mm-handle-type handle
))
822 (mm-archive-decoders))))
825 (executable-find (caddr decoder
)))
826 (mm-archive-dissect-and-inline handle
)
828 ((and (mm-inlinable-p ehandle
)
829 (mm-inlined-p ehandle
))
832 (re-search-forward "^$" nil t
)
834 (mm-display-inline handle
)
838 (if (and (not method
)
839 (equal "text" (car (split-string type
"/"))))
842 (mm-insert-inline handle
(mm-get-part handle
))
845 (and method
;; If nil, we always use "save".
846 (or (eq mm-enable-external t
)
847 (and (eq mm-enable-external
'ask
)
850 "Display part (" type
854 "using external program \""
855 (format method filename
) "\"")
857 "by calling `%s' on the contents)" method
))
861 handle
(or method
'mailcap-save-binary-file
))
863 handle
'mailcap-save-binary-file
)))))))))
865 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
866 (defvar mailcap-mime-extensions
) ; mailcap-mime-info autoloads
867 (declare-function term-mode
"term" ())
868 (declare-function term-char-mode
"term" ())
870 (defun mm-display-external (handle method
)
871 "Display HANDLE using METHOD."
872 (let ((outbuf (current-buffer)))
873 (mm-with-unibyte-buffer
874 (if (functionp method
)
875 (let ((cur (current-buffer)))
876 (if (eq method
'mailcap-save-binary-file
)
878 (set-buffer (generate-new-buffer " *mm*"))
880 (mm-insert-part handle
)
881 (mm-add-meta-html-tag handle
)
882 (let ((win (get-buffer-window cur t
)))
884 (select-window win
)))
885 (switch-to-buffer (generate-new-buffer " *mm*")))
886 (buffer-disable-undo)
887 (set-buffer-file-coding-system mm-binary-coding-system
)
888 (insert-buffer-substring cur
)
889 (goto-char (point-min))
891 (message "Viewing with %s" method
))
892 (let ((mm (current-buffer))
893 (non-viewer (assq 'non-viewer
895 (mm-handle-media-type handle
) t
))))
899 (when (and (boundp 'gnus-summary-buffer
)
900 (bufferp gnus-summary-buffer
)
901 (buffer-name gnus-summary-buffer
))
902 ;; So that we pop back to the right place, sort of.
903 (switch-to-buffer gnus-summary-buffer
)
904 (switch-to-buffer mm
))
905 (delete-other-windows)
907 (mm-save-part handle
))
908 (when (and (not non-viewer
)
910 (mm-handle-set-undisplayer handle mm
)))))
911 ;; The function is a string to be executed.
912 (mm-insert-part handle
)
913 (mm-add-meta-html-tag handle
)
914 (let* ((dir (make-temp-file
915 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
917 (mail-content-type-get
918 (mm-handle-disposition handle
) 'filename
)
919 (mail-content-type-get
920 (mm-handle-type handle
) 'name
)))
921 (mime-info (mailcap-mime-info
922 (mm-handle-media-type handle
) t
))
923 (needsterm (or (assoc "needsterm" mime-info
)
924 (assoc "needsterminal" mime-info
)))
925 (copiousoutput (assoc "copiousoutput" mime-info
))
927 ;; We create a private sub-directory where we store our files.
928 (set-file-modes dir
#o700
)
930 (setq file
(expand-file-name
931 (gnus-map-function mm-file-name-rewrite-functions
932 (file-name-nondirectory filename
))
934 ;; Use nametemplate (defined in RFC1524) if it is specified
936 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
938 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
939 (setq suffix
(match-string 1 suffix
))
940 ;; Otherwise, use a suffix according to
941 ;; `mailcap-mime-extensions'.
942 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
943 mailcap-mime-extensions
))))
944 (setq file
(make-temp-file (expand-file-name "mm." dir
)
946 (let ((coding-system-for-write mm-binary-coding-system
))
947 (write-region (point-min) (point-max) file nil
'nomesg
))
948 ;; The file is deleted after the viewer exists. If the users edits
949 ;; the file, changes will be lost. Set file to read-only to make it
951 (set-file-modes file
#o400
)
952 (message "Viewing with %s" method
)
955 (let ((command (mm-mailcap-command
956 method file
(mm-handle-type handle
))))
959 (set-process-sentinel
960 (start-process "*display*" nil
961 mm-external-terminal-program
963 shell-command-switch command
)
964 (lambda (process _state
)
965 (if (eq 'exit
(process-status process
))
969 (ignore-errors (delete-file file
))
970 (ignore-errors (delete-directory
980 shell-command-switch command
)))
983 (set-process-sentinel
984 (get-buffer-process buffer
)
985 (let ((wc gnus-current-window-configuration
))
986 (lambda (process _state
)
987 (when (eq 'exit
(process-status process
))
988 (ignore-errors (delete-file file
))
990 (delete-directory (file-name-directory file
)))
991 (gnus-configure-windows wc
)))))
992 (gnus-configure-windows 'display-term
))
993 (mm-handle-set-external-undisplayer handle
(cons file buffer
))
994 (add-to-list 'mm-temp-files-to-be-deleted file t
))
995 (message "Displaying %s..." command
))
998 (with-current-buffer outbuf
1004 (call-process shell-file-name nil
1006 (generate-new-buffer " *mm*"))
1008 shell-command-switch
1010 method file
(mm-handle-type handle
)))
1011 (if (buffer-live-p buffer
)
1012 (with-current-buffer buffer
1015 (ignore-errors (delete-file file
))
1016 (ignore-errors (delete-directory
1017 (file-name-directory file
)))
1018 (ignore-errors (kill-buffer buffer
))))))
1021 ;; Deleting the temp file should be postponed for some wrappers,
1022 ;; shell scripts, and so on, which might exit right after having
1023 ;; started a viewer command as a background job.
1024 (let ((command (mm-mailcap-command
1025 method file
(mm-handle-type handle
))))
1027 (let ((process-connection-type nil
))
1028 (start-process "*display*"
1030 (generate-new-buffer " *mm*"))
1032 shell-command-switch command
)
1033 (set-process-sentinel
1034 (get-buffer-process buffer
)
1035 (lambda (process _state
)
1036 (when (eq (process-status process
) 'exit
)
1040 (ignore-errors (delete-file file
))
1041 (ignore-errors (delete-directory
1042 (file-name-directory file
)))))
1043 (when (buffer-live-p outbuf
)
1044 (with-current-buffer outbuf
1045 (let ((buffer-read-only nil
)
1048 (let ((start (point)))
1050 handle
(with-current-buffer buffer
1052 (put-text-property start
(point)
1053 'face
'mm-command-output
))
1054 (goto-char point
))))
1055 (when (buffer-live-p buffer
)
1056 (kill-buffer buffer
)))
1057 (message "Displaying %s...done" command
))))
1058 (mm-handle-set-external-undisplayer
1059 handle
(cons file buffer
))
1060 (add-to-list 'mm-temp-files-to-be-deleted file t
))
1061 (message "Displaying %s..." command
))
1064 (defun mm-mailcap-command (method file type-list
)
1065 (let ((ctl (cdr type-list
))
1069 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1071 (push (substring method beg
(match-beginning 0)) out
)
1072 (setq beg
(match-end 0)
1073 total
(match-string 0 method
)
1074 sub
(match-string 1 method
))
1076 ((string= total
"%%")
1078 ((or (string= total
"%s")
1079 ;; We do our own quoting.
1080 (string= total
"'%s'")
1081 (string= total
"\"%s\""))
1082 (setq uses-stdin nil
)
1083 (push (shell-quote-argument
1084 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
1085 ((string= total
"%t")
1086 (push (shell-quote-argument (car type-list
)) out
))
1088 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
1089 (push (substring method beg
(length method
)) out
)
1092 (push (shell-quote-argument
1093 (gnus-map-function mm-path-name-rewrite-functions file
))
1095 (mapconcat 'identity
(nreverse out
) "")))
1097 (defun mm-remove-parts (handles)
1098 "Remove the displayed MIME parts represented by HANDLES."
1099 (if (and (listp handles
)
1100 (bufferp (car handles
)))
1101 (mm-remove-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-remove-parts (cdr handle
)))
1112 (mm-remove-part handle
)))))))
1114 (defun mm-destroy-parts (handles)
1115 "Remove the displayed MIME parts represented by HANDLES."
1116 (if (and (listp handles
)
1117 (bufferp (car handles
)))
1118 (mm-destroy-part handles
)
1120 (while (setq handle
(pop handles
))
1123 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1124 (kill-buffer (get-text-property 0 'buffer handle
))))
1125 ((and (listp handle
)
1126 (stringp (car handle
)))
1127 (mm-destroy-parts handle
))
1129 (mm-destroy-part handle
)))))))
1131 (defun mm-remove-part (handle)
1132 "Remove the displayed MIME part represented by HANDLE."
1133 (when (listp handle
)
1134 (let ((object (mm-handle-undisplayer handle
)))
1137 ;; Internally displayed part.
1138 ((or (functionp object
)
1140 (eq (car object
) 'lambda
)))
1142 ;; Externally displayed part.
1145 (while (get-buffer-process (cdr object
))
1146 (interrupt-process (get-buffer-process (cdr object
)))
1147 (message "Waiting for external displayer to die...")
1151 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1152 (message "Waiting for external displayer to die...done")
1153 (ignore-errors (delete-file (car object
)))
1154 (ignore-errors (delete-directory (file-name-directory
1157 (when (buffer-live-p object
)
1158 (kill-buffer object
)))))
1159 (mm-handle-set-undisplayer handle nil
))))
1161 (defun mm-display-inline (handle)
1162 (let* ((type (mm-handle-media-type handle
))
1163 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1164 (funcall function handle
)
1165 (goto-char (point-min))))
1167 (defun mm-assoc-string-match (alist type
)
1168 (cl-dolist (elem alist
)
1169 (when (string-match (car elem
) type
)
1172 (defun mm-automatic-display-p (handle)
1173 "Say whether the user wants HANDLE to be displayed automatically."
1174 (let ((methods mm-automatic-display
)
1175 (type (mm-handle-media-type handle
))
1177 (while (setq method
(pop methods
))
1178 (when (and (not (mm-inline-override-p handle
))
1179 (string-match method type
))
1184 (defun mm-inlinable-p (handle &optional type
)
1185 "Say whether HANDLE can be displayed inline.
1186 TYPE is the mime-type of the object; it defaults to the one given
1188 (unless type
(setq type
(mm-handle-media-type handle
)))
1189 (let ((alist mm-inline-media-tests
)
1192 (when (string-match (caar alist
) type
)
1193 (setq test
(caddar alist
)
1195 (setq test
(funcall test handle
)))
1199 (defun mm-inlined-p (handle)
1200 "Say whether the user wants HANDLE to be displayed inline."
1201 (let ((methods mm-inlined-types
)
1202 (type (mm-handle-media-type handle
))
1204 (while (setq method
(pop methods
))
1205 (when (and (not (mm-inline-override-p handle
))
1206 (string-match method type
))
1211 (defun mm-attachment-override-p (handle)
1212 "Say whether HANDLE should have attachment behavior overridden."
1213 (let ((types mm-attachment-override-types
)
1214 (type (mm-handle-media-type handle
))
1217 (while (setq ty
(pop types
))
1218 (when (and (string-match ty type
)
1219 (mm-inlinable-p handle
))
1220 (throw 'found t
))))))
1222 (defun mm-inline-override-p (handle)
1223 "Say whether HANDLE should have inline behavior overridden."
1224 (let ((types mm-inline-override-types
)
1225 (type (mm-handle-media-type handle
))
1228 (while (setq ty
(pop types
))
1229 (when (string-match ty type
)
1230 (throw 'found t
))))))
1232 (defun mm-automatic-external-display-p (type)
1233 "Return the user-defined method for TYPE."
1234 (let ((methods mm-automatic-external-display
)
1236 (while (setq method
(pop methods
))
1237 (when (string-match method type
)
1242 (defun mm-destroy-part (handle)
1243 "Destroy the data structures connected to HANDLE."
1244 (when (listp handle
)
1245 (mm-remove-part handle
)
1246 (when (buffer-live-p (mm-handle-buffer handle
))
1247 (kill-buffer (mm-handle-buffer handle
)))))
1249 (defun mm-handle-displayed-p (handle)
1250 "Say whether HANDLE is displayed or not."
1251 (mm-handle-undisplayer handle
))
1254 ;;; Functions for outputting parts
1257 (defmacro mm-with-part
(handle &rest forms
)
1258 "Run FORMS in the temp buffer containing the contents of HANDLE."
1259 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1260 ;; chars, so the buffer should be unibyte. It may happen that the
1261 ;; handle-buffer is multibyte for some reason, in which case now is a good
1262 ;; time to adjust it, since we know at this point that it should
1264 `(let* ((handle ,handle
))
1265 (when (and (mm-handle-buffer handle
)
1266 (buffer-name (mm-handle-buffer handle
)))
1268 (mm-disable-multibyte)
1269 (insert-buffer-substring (mm-handle-buffer handle
))
1270 (mm-decode-content-transfer-encoding
1271 (mm-handle-encoding handle
)
1272 (mm-handle-media-type handle
))
1274 (put 'mm-with-part
'lisp-indent-function
1)
1275 (put 'mm-with-part
'edebug-form-spec
'(body))
1277 (defun mm-get-part (handle &optional no-cache
)
1278 "Return the contents of HANDLE as a string.
1279 If NO-CACHE is non-nil, cached contents of a message/external-body part
1281 (if (and (not no-cache
)
1282 (equal (mm-handle-media-type handle
) "message/external-body"))
1284 (unless (mm-handle-cache handle
)
1285 (mm-extern-cache-contents handle
))
1286 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1288 (mm-with-part handle
1291 (defun mm-insert-part (handle &optional no-cache
)
1292 "Insert the contents of HANDLE in the current buffer.
1293 If NO-CACHE is non-nil, cached contents of a message/external-body part
1295 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1298 (with-current-buffer (mm-handle-buffer handle
)
1301 (mm-get-part handle no-cache
)))))
1307 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1309 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1310 ;; Separate the extracted parts that have the same faces.
1314 (defun mm-file-name-delete-whitespace (file-name)
1315 "Remove all whitespace characters from FILE-NAME."
1316 (while (string-match "\\s-+" file-name
)
1317 (setq file-name
(replace-match "" t t file-name
)))
1320 (defun mm-file-name-trim-whitespace (file-name)
1321 "Remove leading and trailing whitespace characters from FILE-NAME."
1322 (when (string-match "\\`\\s-+" file-name
)
1323 (setq file-name
(substring file-name
(match-end 0))))
1324 (when (string-match "\\s-+\\'" file-name
)
1325 (setq file-name
(substring file-name
0 (match-beginning 0))))
1328 (defun mm-file-name-collapse-whitespace (file-name)
1329 "Collapse multiple whitespace characters in FILE-NAME."
1330 (while (string-match "\\s-\\s-+" file-name
)
1331 (setq file-name
(replace-match " " t t file-name
)))
1334 (defun mm-file-name-replace-whitespace (file-name)
1335 "Replace whitespace characters in FILE-NAME with underscores.
1336 Set the option `mm-file-name-replace-whitespace' to any other
1337 string if you do not like underscores."
1338 (let ((s (or mm-file-name-replace-whitespace
"_")))
1339 (while (string-match "\\s-" file-name
)
1340 (setq file-name
(replace-match s t t file-name
))))
1343 (defun mm-file-name-delete-control (filename)
1344 "Delete control characters from FILENAME."
1345 (replace-regexp-in-string "[\x00-\x1f\x7f]" "" filename
))
1347 (defun mm-file-name-delete-gotchas (filename)
1348 "Delete shell gotchas from FILENAME."
1349 (setq filename
(replace-regexp-in-string "[<>|]" "" filename
))
1350 (replace-regexp-in-string "^[.-]+" "" filename
))
1352 (defun mm-save-part (handle &optional prompt
)
1353 "Write HANDLE to a file.
1354 PROMPT overrides the default one used to ask user for a file name."
1355 (let ((filename (or (mail-content-type-get
1356 (mm-handle-disposition handle
) 'filename
)
1357 (mail-content-type-get
1358 (mm-handle-type handle
) 'name
)))
1361 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1362 (file-name-nondirectory filename
))))
1368 (format "Save MIME part to%s: "
1370 (format " (default %s)" filename
)
1372 (or directory mm-default-directory default-directory
)
1375 (or directory mm-default-directory default-directory
))))
1376 (cond ((or (not file
) (equal file
""))
1377 (message "Please enter a file name")
1379 ((and (file-directory-p file
)
1381 (setq directory file
)
1382 (message "Please enter a non-directory file name")
1387 (if (file-directory-p file
)
1388 (setq file
(expand-file-name filename file
))
1389 (setq file
(expand-file-name
1390 file
(or mm-default-directory default-directory
))))
1391 (setq mm-default-directory
(file-name-directory file
))
1392 (and (or (not (file-exists-p file
))
1393 (yes-or-no-p (format "File %s already exists; overwrite? "
1396 (mm-save-part-to-file handle file
)
1399 (defun mm-add-meta-html-tag (handle &optional charset force-charset
)
1400 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1401 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1402 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1403 Return t if meta tag is added or replaced."
1404 (when (equal (mm-handle-media-type handle
) "text/html")
1406 (setq charset
(mail-content-type-get (mm-handle-type handle
)
1408 (setq charset
(format "\
1409 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset
))
1410 (let ((case-fold-search t
))
1411 (goto-char (point-min))
1412 (if (re-search-forward "\
1413 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']?\
1414 text/html\\(?:;\\s-*charset=\\([^\t\n\r \"'>]+\\)\\)?[^>]*>" nil t
)
1415 (if (and (not force-charset
)
1416 (match-beginning 1))
1417 ;; Don't modify existing meta tag.
1419 ;; Replace it with the one specifying charset.
1420 (replace-match charset
)
1422 (if (re-search-forward "<head>\\s-*" nil t
)
1423 (insert charset
"\n")
1424 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t
)
1425 (insert "<head>\n" charset
"\n</head>\n"))
1428 (defun mm-save-part-to-file (handle file
)
1429 (mm-with-unibyte-buffer
1430 (mm-insert-part handle
)
1431 (mm-add-meta-html-tag handle
)
1432 (let ((current-file-modes (default-file-modes)))
1433 (set-default-file-modes mm-attachment-file-modes
)
1435 ;; Don't re-compress .gz & al. Arguably we should make
1436 ;; `file-name-handler-alist' nil, but that would chop
1437 ;; ange-ftp, which is reasonable to use here.
1438 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1439 (set-default-file-modes current-file-modes
)))))
1441 (defun mm-pipe-part (handle &optional cmd
)
1442 "Pipe HANDLE to a process.
1443 Use CMD as the process."
1444 (let ((command (or cmd
1446 "Shell command on MIME part: " mm-last-shell-command
))))
1447 (mm-with-unibyte-buffer
1448 (mm-insert-part handle
)
1449 (mm-add-meta-html-tag handle
)
1450 (let ((coding-system-for-write 'binary
))
1451 (shell-command-on-region (point-min) (point-max) command nil
)))))
1453 (autoload 'gnus-completing-read
"gnus-util")
1455 (defun mm-interactively-view-part (handle)
1456 "Display HANDLE using METHOD."
1457 (let* ((type (mm-handle-media-type handle
))
1459 (mapcar (lambda (i) (cdr (assoc 'viewer i
)))
1460 (mailcap-mime-info type
'all
)))
1461 (method (let ((minibuffer-local-completion-map
1462 mm-viewer-completion-map
))
1463 (completing-read "Viewer: " methods
))))
1464 (when (string= method
"")
1465 (error "No method given"))
1466 (if (string-match "^[^% \t]+$" method
)
1467 (setq method
(concat method
" %s")))
1468 (mm-display-external handle method
)))
1470 (defun mm-preferred-alternative (handles &optional preferred
)
1471 "Say which of HANDLES are preferred."
1472 (let ((prec (if preferred
(list preferred
)
1473 (mm-preferred-alternative-precedence handles
)))
1474 p h result type handle
)
1475 (while (setq p
(pop prec
))
1478 (setq handle
(car h
))
1479 (setq type
(mm-handle-media-type handle
))
1480 (when (and (equal p type
)
1481 (mm-automatic-display-p handle
)
1482 (or (stringp (car handle
))
1483 (not (mm-handle-disposition handle
))
1484 (equal (car (mm-handle-disposition handle
))
1492 (defun mm-preferred-alternative-precedence (handles)
1493 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1494 (setq handles
(reverse handles
))
1495 (dolist (disc (reverse mm-discouraged-alternatives
))
1496 (dolist (handle (copy-sequence handles
))
1497 (when (string-match disc
(mm-handle-media-type handle
))
1498 (setq handles
(nconc (delete handle handles
) (list handle
))))))
1499 ;; Remove empty parts.
1500 (dolist (handle (copy-sequence handles
))
1501 (when (and (bufferp (mm-handle-buffer handle
))
1502 (not (with-current-buffer (mm-handle-buffer handle
)
1503 (goto-char (point-min))
1504 (re-search-forward "[^ \t\n]" nil t
))))
1505 (setq handles
(nconc (delete handle handles
) (list handle
)))))
1506 (mapcar #'mm-handle-media-type handles
))
1508 (defun mm-get-content-id (id)
1509 "Return the handle(s) referred to by ID."
1510 (cdr (assoc id mm-content-id-alist
)))
1512 (defconst mm-image-type-regexps
1513 '(("/\\*.*XPM.\\*/" . xpm
)
1517 ("\211PNG\r\n" . png
)
1519 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1520 ("%!PS" . postscript
))
1521 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1522 When the first bytes of an image file match REGEXP, it is assumed to
1523 be of image type IMAGE-TYPE.")
1525 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1526 (defun mm-image-type-from-buffer ()
1527 "Determine the image type from data in the current buffer.
1528 Value is a symbol specifying the image type or nil if type cannot
1530 (let ((types mm-image-type-regexps
)
1532 (goto-char (point-min))
1533 (while (and types
(null type
))
1534 (let ((regexp (car (car types
)))
1535 (image-type (cdr (car types
))))
1536 (when (looking-at regexp
)
1537 (setq type image-type
))
1538 (setq types
(cdr types
))))
1541 (defun mm-get-image (handle)
1542 "Return an image instance based on HANDLE."
1543 (let ((type (mm-handle-media-subtype handle
))
1545 ;; Allow some common translations.
1548 ((equal type
"x-pixmap")
1550 ((equal type
"x-xbitmap")
1552 ((equal type
"x-portable-bitmap")
1554 ((equal type
"svg+xml")
1557 (or (mm-handle-cache handle
)
1558 (mm-with-unibyte-buffer
1559 (mm-insert-part handle
)
1563 (create-image (buffer-string)
1564 (or (mm-image-type-from-buffer)
1567 (mm-handle-set-cache handle spec
))))))
1569 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1571 (defun mm-image-fit-p (handle)
1572 "Say whether the image in HANDLE will fit the current window."
1573 (let ((image (mm-get-image handle
)))
1575 (let* ((size (image-size image
))
1578 (or mm-inline-large-images
1579 (and (<= h
(1- (window-height))) ; Don't include mode line.
1580 (<= w
(window-width))))))))
1582 (defun mm-valid-image-format-p (format)
1583 "Say whether FORMAT can be displayed natively by Emacs."
1584 (and (display-graphic-p)
1585 (image-type-available-p format
)))
1587 (defun mm-valid-and-fit-image-p (format handle
)
1588 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1589 (and (mm-valid-image-format-p format
)
1590 (mm-image-fit-p handle
)))
1592 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1593 "Search in HANDLES for part with TYPE.
1594 If NOTP, returns first non-matching part.
1595 If RECURSIVE, search recursively."
1598 (if (and recursive
(stringp (caar handles
)))
1599 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1603 (not (equal (mm-handle-media-type (car handles
)) type
))
1604 (equal (mm-handle-media-type (car handles
)) type
))
1605 (setq handle
(car handles
)
1607 (setq handles
(cdr handles
)))
1610 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1611 (goto-char (point-min))
1612 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1614 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1616 (end (save-excursion
1617 (goto-char (point-max))
1618 (if (re-search-backward close-delimiter nil t
)
1622 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1623 (while (and (not result
)
1624 (re-search-forward boundary end t
))
1625 (goto-char (match-beginning 0))
1629 (narrow-to-region start
(1- (point)))
1630 (when (let* ((ct (mail-fetch-field "content-type"))
1631 (ctl (and ct
(mail-header-parse-content-type ct
))))
1633 (not (equal (car ctl
) type
))
1634 (equal (car ctl
) type
)))
1635 (setq result
(buffer-string))))))
1637 (setq start
(point)))
1638 (when (and (not result
) start
)
1641 (narrow-to-region start end
)
1642 (when (let* ((ct (mail-fetch-field "content-type"))
1643 (ctl (and ct
(mail-header-parse-content-type ct
))))
1645 (not (equal (car ctl
) type
))
1646 (equal (car ctl
) type
)))
1647 (setq result
(buffer-string))))))
1650 (defvar mm-security-handle nil
)
1652 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1653 ;; HANDLE could be a CTL.
1655 (put-text-property 0 (length (car handle
)) parameter value
1658 (autoload 'mm-view-pkcs7
"mm-view")
1660 (defun mm-possibly-verify-or-decrypt (parts ctl
&optional from
)
1661 (let ((type (car ctl
))
1662 (subtype (cadr (split-string (car ctl
) "/")))
1663 (mm-security-handle ctl
) ;; (car CTL) is the type.
1664 protocol func functest
)
1666 ((or (equal type
"application/x-pkcs7-mime")
1667 (equal type
"application/pkcs7-mime"))
1670 ((eq mm-decrypt-option
'never
) nil
)
1671 ((eq mm-decrypt-option
'always
) t
)
1672 ((eq mm-decrypt-option
'known
) t
)
1674 (format "Decrypt (S/MIME) part? "))))
1675 (mm-view-pkcs7 parts from
))
1676 (setq parts
(mm-dissect-buffer t
)))))
1677 ((equal subtype
"signed")
1678 (unless (and (setq protocol
1679 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1680 (not (equal protocol
"multipart/mixed")))
1681 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1682 (let ((protocols mm-verify-function-alist
))
1684 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1685 (funcall functest parts ctl
))
1686 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1687 (setq protocol
(caar protocols
)
1689 (setq protocols
(cdr protocols
))))))
1690 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1692 ((eq mm-verify-option
'never
) nil
)
1693 ((eq mm-verify-option
'always
) t
)
1694 ((eq mm-verify-option
'known
)
1696 (or (not (setq functest
1697 (nth 3 (assoc protocol
1698 mm-verify-function-alist
))))
1699 (funcall functest parts ctl
))))
1702 (format "Verify signed (%s) part? "
1703 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1704 (format "protocol=%s" protocol
))))))
1707 (setq parts
(funcall func parts ctl
))
1708 (mm-set-handle-multipart-parameter
1709 mm-security-handle
'gnus-details
1710 (format "Unknown sign protocol (%s)" protocol
))))))
1711 ((equal subtype
"encrypted")
1712 (unless (setq protocol
1713 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1714 ;; The message is broken.
1715 (let ((parts parts
))
1717 (if (assoc (mm-handle-media-type (car parts
))
1718 mm-decrypt-function-alist
)
1719 (setq protocol
(mm-handle-media-type (car parts
))
1721 (setq parts
(cdr parts
))))))
1722 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1724 ((eq mm-decrypt-option
'never
) nil
)
1725 ((eq mm-decrypt-option
'always
) t
)
1726 ((eq mm-decrypt-option
'known
)
1728 (or (not (setq functest
1729 (nth 3 (assoc protocol
1730 mm-decrypt-function-alist
))))
1731 (funcall functest parts ctl
))))
1734 (format "Decrypt (%s) part? "
1735 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1736 (format "protocol=%s" protocol
))))))
1739 (setq parts
(funcall func parts ctl
))
1740 (mm-set-handle-multipart-parameter
1741 mm-security-handle
'gnus-details
1742 (format "Unknown encrypt protocol (%s)" protocol
))))))
1746 (defun mm-multiple-handles (handles)
1747 (and (listp handles
)
1748 (> (length handles
) 1)
1749 (or (listp (car handles
))
1750 (stringp (car handles
)))))
1752 (defun mm-complicated-handles (handles)
1753 (and (listp (car handles
))
1754 (> (length handles
) 1)))
1756 (defun mm-merge-handles (handles1 handles2
)
1758 (if (listp (car handles1
))
1761 (if (listp (car handles2
))
1765 (defun mm-readable-p (handle)
1766 "Say whether the content of HANDLE is readable."
1767 (and (< (with-current-buffer (mm-handle-buffer handle
)
1768 (buffer-size)) 10000)
1769 (mm-with-unibyte-buffer
1770 (mm-insert-part handle
)
1771 (and (eq (mm-body-7-or-8) '7bit
)
1772 (not (mm-long-lines-p 76))))))
1774 (declare-function libxml-parse-html-region
"xml.c"
1775 (start end
&optional base-url discard-comments
))
1776 (declare-function shr-insert-document
"shr" (dom))
1777 (defvar shr-blocked-images
)
1778 (defvar shr-use-fonts
)
1780 (defvar shr-content-function
)
1781 (defvar shr-inhibit-images
)
1783 (defun mm-shr (handle)
1784 ;; Require since we bind its variables.
1786 (let ((shr-width (if shr-use-fonts
1789 (shr-content-function (lambda (id)
1790 (let ((handle (mm-get-content-id id
)))
1792 (mm-with-part handle
1793 (buffer-string))))))
1794 (shr-inhibit-images mm-html-inhibit-images
)
1795 (shr-blocked-images mm-html-blocked-images
)
1796 charset coding char document
)
1797 (mm-with-part (or handle
(setq handle
(mm-dissect-buffer t
)))
1798 (setq case-fold-search t
)
1800 (mail-content-type-get (mm-handle-type handle
) 'charset
))
1802 (goto-char (point-min))
1803 (and (re-search-forward "\
1804 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']?\
1805 text/html;\\s-*charset=\\([^\t\n\r \"'>]+\\)[^>]*>" nil t
)
1806 (setq coding
(mm-charset-to-coding-system (match-string 1)
1808 (setq charset mail-parse-charset
))
1809 (when (and (or coding
1810 (setq coding
(mm-charset-to-coding-system charset nil t
)))
1811 (not (eq coding
'ascii
)))
1813 (decode-coding-string (buffer-string) coding
)
1815 (set-buffer-multibyte t
))))
1816 (goto-char (point-min))
1817 (while (re-search-forward
1818 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t
)
1820 (cdr (assq (if (match-beginning 1)
1821 (string-to-number (match-string 1) 16)
1822 (string-to-number (match-string 2)))
1823 mm-extra-numeric-entities
)))
1824 (replace-match (char-to-string char
))))
1825 ;; Remove "soft hyphens".
1826 (goto-char (point-min))
1827 (while (search-forward "" nil t
)
1828 (replace-match "" t t
))
1829 (setq document
(libxml-parse-html-region (point-min) (point-max))))
1831 (narrow-to-region (point) (point))
1832 (shr-insert-document document
)
1835 (mm-convert-shr-links)
1836 (mm-handle-set-undisplayer
1838 (let ((min (point-min-marker))
1839 (max (point-max-marker)))
1841 (let ((inhibit-read-only t
))
1842 (delete-region min max
))))))))
1844 (defvar shr-image-map
)
1846 (autoload 'widget-convert-button
"wid-edit")
1847 (defvar widget-keymap
)
1849 (defun mm-convert-shr-links ()
1850 (let ((start (point-min))
1853 (< start
(point-max)))
1854 (when (setq start
(text-property-not-all start
(point-max) 'shr-url nil
))
1855 (setq end
(next-single-property-change start
'shr-url nil
(point-max)))
1856 (widget-convert-button
1858 :help-echo
(get-text-property start
'help-echo
)
1859 :keymap
(setq keymap
(copy-keymap
1860 (if (mm-images-in-region-p start end
)
1863 (get-text-property start
'shr-url
))
1864 ;; Mask keys that launch `widget-button-click'.
1865 ;; Those bindings are provided by `widget-keymap'
1866 ;; that is a parent of `gnus-article-mode-map'.
1867 (dolist (key (where-is-internal 'widget-button-click widget-keymap
))
1868 (unless (lookup-key keymap key
)
1869 (define-key keymap key
#'ignore
)))
1870 ;; Avoid `shr-next-link' and `shr-previous-link' in `keymap' so
1871 ;; TAB and M-TAB run `widget-forward' and `widget-backward' instead.
1872 (substitute-key-definition 'shr-next-link nil keymap
)
1873 (substitute-key-definition 'shr-previous-link nil keymap
)
1874 (dolist (overlay (overlays-at start
))
1875 (overlay-put overlay
'face nil
))
1876 (setq start end
)))))
1878 (defun mm-handle-filename (handle)
1879 "Return filename of HANDLE if any."
1880 (or (mail-content-type-get (mm-handle-type handle
)
1882 (mail-content-type-get (mm-handle-disposition handle
)
1885 (provide 'mm-decode
)
1891 ;;; mm-decode.el ends here