1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2015 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/>.
28 (eval-when-compile (require 'cl
))
30 (autoload 'gnus-map-function
"gnus-util")
31 (autoload 'gnus-replace-in-string
"gnus-util")
32 (autoload 'gnus-read-shell-command
"gnus-util")
34 (autoload 'mm-inline-partial
"mm-partial")
35 (autoload 'mm-inline-external-body
"mm-extern")
36 (autoload 'mm-extern-cache-contents
"mm-extern")
37 (autoload 'mm-insert-inline
"mm-view")
39 (autoload 'mm-archive-decoders
"mm-archive")
40 (autoload 'mm-archive-dissect-and-inline
"mm-archive")
41 (autoload 'mm-dissect-archive
"mm-archive")
43 (defvar gnus-current-window-configuration
)
45 (add-hook 'gnus-exit-gnus-hook
'mm-destroy-postponed-undisplay-list
)
46 (add-hook 'gnus-exit-gnus-hook
'mm-temp-files-delete
)
48 (defgroup mime-display
()
49 "Display of MIME in mail and news articles."
50 :link
'(custom-manual "(emacs-mime)Display Customization")
56 (defgroup mime-security
()
57 "MIME security in mail and news articles."
58 :link
'(custom-manual "(emacs-mime)Display Customization")
63 (defface mm-command-output
66 (:foreground
"ForestGreen"))
72 "Face used for displaying output from commands."
75 ;;; Convenience macros.
77 (defmacro mm-handle-buffer
(handle)
79 (defmacro mm-handle-type
(handle)
81 (defsubst mm-handle-media-type
(handle)
82 (if (stringp (car handle
))
84 (car (mm-handle-type handle
))))
85 (defsubst mm-handle-media-supertype
(handle)
86 (car (split-string (mm-handle-media-type handle
) "/")))
87 (defsubst mm-handle-media-subtype
(handle)
88 (cadr (split-string (mm-handle-media-type handle
) "/")))
89 (defmacro mm-handle-encoding
(handle)
91 (defmacro mm-handle-undisplayer
(handle)
93 (defmacro mm-handle-set-undisplayer
(handle function
)
94 `(setcar (nthcdr 3 ,handle
) ,function
))
95 (defmacro mm-handle-disposition
(handle)
97 (defmacro mm-handle-description
(handle)
99 (defmacro mm-handle-cache
(handle)
101 (defmacro mm-handle-set-cache
(handle contents
)
102 `(setcar (nthcdr 6 ,handle
) ,contents
))
103 (defmacro mm-handle-id
(handle)
105 (defmacro mm-handle-multipart-original-buffer
(handle)
106 `(get-text-property 0 'buffer
(car ,handle
)))
107 (defmacro mm-handle-multipart-from
(handle)
108 `(get-text-property 0 'from
(car ,handle
)))
109 (defmacro mm-handle-multipart-ctl-parameter
(handle parameter
)
110 `(get-text-property 0 ,parameter
(car ,handle
)))
112 (defmacro mm-make-handle
(&optional buffer type encoding undisplayer
113 disposition description cache
115 `(list ,buffer
,type
,encoding
,undisplayer
116 ,disposition
,description
,cache
,id
))
118 (defcustom mm-text-html-renderer
119 (cond ((fboundp 'libxml-parse-html-region
) 'shr
)
120 ((executable-find "w3m") 'gnus-w3m
)
121 ((executable-find "links") 'links
)
122 ((executable-find "lynx") 'lynx
)
123 ((locate-library "html2text") 'html2text
)
125 "Render of HTML contents.
126 It is one of defined renderer types, or a rendering function.
127 The defined renderer types are:
128 `shr': use the built-in Gnus HTML renderer;
129 `gnus-w3m': use Gnus renderer based on w3m;
130 `w3m': use emacs-w3m;
131 `w3m-standalone': use plain w3m;
134 `html2text': use html2text;
135 nil : use external viewer (default web browser)."
137 :type
'(choice (const shr
)
139 (const w3m
:tag
"emacs-w3m")
140 (const w3m-standalone
:tag
"standalone w3m" )
144 (const nil
:tag
"External viewer")
146 :group
'mime-display
)
148 (defcustom mm-inline-text-html-with-images nil
149 "If non-nil, Gnus will allow retrieving images in HTML that has <img> tags.
150 See also the documentation for the `mm-w3m-safe-url-regexp'
154 :group
'mime-display
)
156 (defcustom mm-w3m-safe-url-regexp
"\\`cid:"
157 "Regexp matching URLs which are considered to be safe.
158 Some HTML mails might contain a nasty trick used by spammers, using
159 the <img> tag which is far more evil than the [Click Here!] button.
160 It is most likely intended to check whether the ominous spam mail has
161 reached your eyes or not, in which case the spammer knows for sure
162 that your email address is valid. It is done by embedding an
163 identifier string into a URL that you might automatically retrieve
164 when displaying the image. The default value is \"\\\\`cid:\" which only
165 matches parts embedded to the Multipart/Related type MIME contents and
166 Gnus will never connect to the spammer's site arbitrarily. You may
167 set this variable to nil if you consider all urls to be safe."
169 :type
'(choice (regexp :tag
"Regexp")
170 (const :tag
"All URLs are safe" nil
))
171 :group
'mime-display
)
173 (defcustom mm-inline-text-html-with-w3m-keymap t
174 "If non-nil, use emacs-w3m command keys in the article buffer."
177 :group
'mime-display
)
179 (defcustom mm-enable-external t
180 "Indicate whether external MIME handlers should be used.
182 If t, all defined external MIME handlers are used. If nil, files are saved by
183 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
184 before the external MIME handler is invoked."
186 :type
'(choice (const :tag
"Always" t
)
187 (const :tag
"Never" nil
)
188 (const :tag
"Ask" ask
))
189 :group
'mime-display
)
191 (defcustom mm-inline-media-tests
195 (mm-valid-and-fit-image-p 'jpeg handle
)))
199 (mm-valid-and-fit-image-p 'png handle
)))
203 (mm-valid-and-fit-image-p 'gif handle
)))
207 (mm-valid-and-fit-image-p 'tiff handle
)))
211 (mm-valid-and-fit-image-p 'xbm handle
)))
215 (mm-valid-and-fit-image-p 'xbm handle
)))
219 (mm-valid-and-fit-image-p 'xpm handle
)))
223 (mm-valid-and-fit-image-p 'xpm handle
)))
227 (mm-valid-and-fit-image-p 'bmp handle
)))
228 ("image/x-portable-bitmap"
231 (mm-valid-and-fit-image-p 'pbm handle
)))
232 ("text/plain" mm-inline-text identity
)
233 ("text/enriched" mm-inline-text identity
)
234 ("text/richtext" mm-inline-text identity
)
235 ("text/x-patch" mm-display-patch-inline identity
)
236 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
237 ("text/x-diff" mm-display-patch-inline identity
)
238 ("application/emacs-lisp" mm-display-elisp-inline identity
)
239 ("application/x-emacs-lisp" mm-display-elisp-inline identity
)
240 ("application/x-shellscript" mm-display-shell-script-inline identity
)
241 ("application/x-sh" mm-display-shell-script-inline identity
)
242 ("text/x-sh" mm-display-shell-script-inline identity
)
243 ("application/javascript" mm-display-javascript-inline identity
)
244 ("text/dns" mm-display-dns-inline identity
)
245 ("text/x-org" mm-display-org-inline identity
)
249 mm-text-html-renderer
))
253 (or (featurep 'vcard
)
254 (locate-library "vcard"))))
255 ("message/delivery-status" mm-inline-text identity
)
256 ("message/rfc822" mm-inline-message identity
)
257 ("message/partial" mm-inline-partial identity
)
258 ("message/external-body" mm-inline-external-body identity
)
259 ("text/.*" mm-inline-text identity
)
260 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity
)
261 ("application/zip" mm-archive-dissect-and-inline identity
)
262 ("audio/wav" mm-inline-audio
264 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
265 (device-sound-enabled-p))))
269 (and (or (featurep 'nas-sound
) (featurep 'native-sound
))
270 (device-sound-enabled-p))))
271 ("application/pgp-signature" ignore identity
)
272 ("application/x-pkcs7-signature" ignore identity
)
273 ("application/pkcs7-signature" ignore identity
)
274 ("application/x-pkcs7-mime" ignore identity
)
275 ("application/pkcs7-mime" ignore identity
)
276 ("multipart/alternative" ignore identity
)
277 ("multipart/mixed" ignore identity
)
278 ("multipart/related" ignore identity
)
282 (and (mm-valid-image-format-p 'imagemagick
)
283 (mm-with-unibyte-buffer
284 (mm-insert-part handle
)
287 (if (fboundp 'create-image
)
288 (create-image (buffer-string) 'imagemagick
'data-p
)
289 (mm-create-image-xemacs
290 (mm-handle-media-subtype handle
))))))
292 (setcar (cdr handle
) (list "image/imagemagick"))
293 (mm-image-fit-p handle
)))))))
294 ;; Disable audio and image
295 ("audio/.*" ignore ignore
)
296 ("image/.*" ignore ignore
)
297 ;; Default to displaying as text
298 (".*" mm-inline-text mm-readable-p
))
299 "Alist of media types/tests saying whether types can be displayed inline."
300 :type
'(repeat (list (regexp :tag
"MIME type")
301 (function :tag
"Display function")
302 (function :tag
"Display test")))
303 :group
'mime-display
)
305 (defcustom mm-inlined-types
306 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
307 "message/partial" "message/external-body" "application/emacs-lisp"
308 "application/x-emacs-lisp"
309 "application/pgp-signature" "application/x-pkcs7-signature"
310 "application/pkcs7-signature" "application/x-pkcs7-mime"
311 "application/pkcs7-mime"
312 "application/x-gtar-compressed"
315 ;; Mutt still uses this even though it has already been withdrawn.
317 "List of media types that are to be displayed inline.
318 See also `mm-inline-media-tests', which says how to display a media
320 :type
'(repeat regexp
)
321 :group
'mime-display
)
323 (defcustom mm-keep-viewer-alive-types
324 '("application/postscript" "application/msword" "application/vnd.ms-excel"
325 "application/pdf" "application/x-dvi")
326 "List of media types for which the external viewer will not be killed
327 when selecting a different article."
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
385 (if (fboundp 'temp-directory
)
387 (if (boundp 'temporary-file-directory
)
388 temporary-file-directory
390 "Where mm will store its temporary files."
392 :group
'mime-display
)
394 (defcustom mm-inline-large-images nil
395 "If t, then all images fit in the buffer.
396 If 'resize, try to resize the images so they fit."
398 (const :tag
"Inline large images as they are." t
)
399 (const :tag
"Resize large images." resize
)
400 (const :tag
"Do not inline large images." nil
))
401 :group
'mime-display
)
403 (defcustom mm-file-name-rewrite-functions
404 '(mm-file-name-delete-control mm-file-name-delete-gotchas
)
405 "List of functions used for rewriting file names of MIME parts.
406 Each function takes a file name as input and returns a file name.
408 Ready-made functions include `mm-file-name-delete-control',
409 `mm-file-name-delete-gotchas' (you should not remove these two
410 functions), `mm-file-name-delete-whitespace',
411 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
412 `mm-file-name-replace-whitespace', `capitalize', `downcase',
413 `upcase', and `upcase-initials'."
414 :type
'(list (set :inline t
415 (const mm-file-name-delete-control
)
416 (const mm-file-name-delete-gotchas
)
417 (const mm-file-name-delete-whitespace
)
418 (const mm-file-name-trim-whitespace
)
419 (const mm-file-name-collapse-whitespace
)
420 (const mm-file-name-replace-whitespace
)
424 (const upcase-initials
)
428 :version
"23.1" ;; No Gnus
429 :group
'mime-display
)
432 (defvar mm-path-name-rewrite-functions nil
433 "*List of functions for rewriting the full file names of MIME parts.
434 This is used when viewing parts externally, and is meant for
435 transforming the absolute name so that non-compliant programs can find
436 the file where it's saved.
438 Each function takes a file name as input and returns a file name.")
440 (defvar mm-file-name-replace-whitespace nil
441 "String used for replacing whitespace characters; default is `\"_\"'.")
443 (defcustom mm-default-directory nil
444 "The default directory where mm will save files.
445 If not set, `default-directory' will be used."
446 :type
'(choice directory
(const :tag
"Default" nil
))
447 :group
'mime-display
)
449 (defcustom mm-attachment-file-modes
384
450 "Set the mode bits of saved attachments to this integer."
453 :group
'mime-display
)
455 (defcustom mm-external-terminal-program
"xterm"
456 "The program to start an external terminal."
459 :group
'mime-display
)
461 ;;; Internal variables.
463 (defvar mm-last-shell-command
"")
464 (defvar mm-content-id-alist nil
)
465 (defvar mm-postponed-undisplay-list nil
)
466 (defvar mm-inhibit-auto-detect-attachment nil
)
467 (defvar mm-temp-files-to-be-deleted nil
468 "List of temporary files scheduled to be deleted.")
469 (defvar mm-temp-files-cache-file
(concat ".mm-temp-files-" (user-login-name))
470 "Name of a file that caches a list of temporary files to be deleted.
471 The file will be saved in the directory `mm-tmp-directory'.")
473 ;; According to RFC2046, in particular, in a digest, the default
474 ;; Content-Type value for a body part is changed from "text/plain" to
476 (defvar mm-dissect-default-type
"text/plain")
478 (autoload 'mml2015-verify
"mml2015")
479 (autoload 'mml2015-verify-test
"mml2015")
480 (autoload 'mml-smime-verify
"mml-smime")
481 (autoload 'mml-smime-verify-test
"mml-smime")
483 (defvar mm-verify-function-alist
484 '(("application/pgp-signature" mml2015-verify
"PGP" mml2015-verify-test
)
485 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1
"PGP"
486 mm-uu-pgp-signed-test
)
487 ("application/pkcs7-signature" mml-smime-verify
"S/MIME"
488 mml-smime-verify-test
)
489 ("application/x-pkcs7-signature" mml-smime-verify
"S/MIME"
490 mml-smime-verify-test
)))
492 (defcustom mm-verify-option
'never
493 "Option of verifying signed parts.
494 `never', not verify; `always', always verify;
495 `known', only verify known protocols. Otherwise, ask user.
497 When set to `always' or `known', you should add
498 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
499 result of the verification."
501 :type
'(choice (item always
)
503 (item :tag
"only known protocols" known
)
504 (item :tag
"ask" nil
))
505 :group
'mime-security
)
507 (autoload 'mml2015-decrypt
"mml2015")
508 (autoload 'mml2015-decrypt-test
"mml2015")
510 (defvar mm-decrypt-function-alist
511 '(("application/pgp-encrypted" mml2015-decrypt
"PGP" mml2015-decrypt-test
)
512 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1
"PGP"
513 mm-uu-pgp-encrypted-test
)))
515 (defcustom mm-decrypt-option nil
516 "Option of decrypting encrypted parts.
517 `never', not decrypt; `always', always decrypt;
518 `known', only decrypt known protocols. Otherwise, ask user."
520 :type
'(choice (item always
)
522 (item :tag
"only known protocols" known
)
523 (item :tag
"ask" nil
))
524 :group
'mime-security
)
526 (defvar mm-viewer-completion-map
527 (let ((map (make-sparse-keymap 'mm-viewer-completion-map
)))
528 (set-keymap-parent map minibuffer-local-completion-map
)
529 ;; Should we bind other key to minibuffer-complete-word?
530 (define-key map
" " 'self-insert-command
)
532 "Keymap for input viewer with completion.")
536 (defun mm-alist-to-plist (alist)
537 "Convert association list ALIST into the equivalent property-list form.
538 The plist is returned. This converts from
540 \((a . 1) (b . 2) (c . 3))
546 The original alist is not modified. See also `destructive-alist-to-plist'."
549 (let ((el (car alist
)))
550 (setq plist
(cons (cdr el
) (cons (car el
) plist
))))
551 (setq alist
(cdr alist
)))
554 (defun mm-keep-viewer-alive-p (handle)
555 "Say whether external viewer for HANDLE should stay alive."
556 (let ((types mm-keep-viewer-alive-types
)
557 (type (mm-handle-media-type handle
))
560 (while (setq ty
(pop types
))
561 (when (string-match ty type
)
562 (throw 'found t
))))))
564 (defun mm-handle-set-external-undisplayer (handle function
)
565 "Set the undisplayer for HANDLE to FUNCTION.
566 Postpone undisplaying of viewers for types in
567 `mm-keep-viewer-alive-types'."
568 (if (mm-keep-viewer-alive-p handle
)
569 (let ((new-handle (copy-sequence handle
)))
570 (mm-handle-set-undisplayer new-handle function
)
571 (mm-handle-set-undisplayer handle nil
)
572 (push new-handle mm-postponed-undisplay-list
))
573 (mm-handle-set-undisplayer handle function
)))
575 (defun mm-destroy-postponed-undisplay-list ()
576 (when mm-postponed-undisplay-list
577 (message "Destroying external MIME viewers")
578 (mm-destroy-parts mm-postponed-undisplay-list
)))
580 (defun mm-temp-files-delete ()
581 "Delete temporary files and those parent directories.
582 Note that the deletion may fail if a program is catching hold of a file
583 under Windows or Cygwin. In that case, it schedules the deletion of
584 files left at the next time."
585 (let* ((coding-system-for-read mm-universal-coding-system
)
586 (coding-system-for-write mm-universal-coding-system
)
587 (cache-file (expand-file-name mm-temp-files-cache-file
589 (cache (when (file-exists-p cache-file
)
590 (mm-with-multibyte-buffer
591 (insert-file-contents cache-file
)
592 (split-string (buffer-string) "\n" t
))))
594 (dolist (temp (append cache mm-temp-files-to-be-deleted
))
595 (when (and (file-exists-p temp
)
596 (if (file-directory-p temp
)
597 ;; A parent directory left at the previous time.
599 (ignore-errors (delete-directory temp
))
600 (file-exists-p temp
))
601 ;; Delete a temporary file and its parent directory.
602 (ignore-errors (delete-file temp
))
603 (or (file-exists-p temp
)
605 (setq temp
(file-name-directory temp
))
606 (ignore-errors (delete-directory temp
))
607 (file-exists-p temp
)))))
610 ;; Schedule the deletion of the files left at the next time.
612 (write-region (concat (mapconcat 'identity
(nreverse fails
) "\n")
614 nil cache-file nil
'silent
)
615 (set-file-modes cache-file
#o600
))
616 (when (file-exists-p cache-file
)
617 (ignore-errors (delete-file cache-file
))))
618 (setq mm-temp-files-to-be-deleted nil
)))
620 (autoload 'message-fetch-field
"message")
622 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from
)
623 "Dissect the current buffer and return a list of MIME handles.
624 If NO-STRICT-MIME, don't require the message to have a
625 MIME-Version header before proceeding."
627 (let (ct ctl type subtype cte cd description id result
)
629 (mail-narrow-to-head)
630 (when (or no-strict-mime
632 (mail-fetch-field "mime-version"))
633 (setq ct
(mail-fetch-field "content-type")
634 ctl
(and ct
(mail-header-parse-content-type ct
))
635 cte
(mail-fetch-field "content-transfer-encoding")
636 cd
(or (mail-fetch-field "content-disposition")
639 (cadr (mm-assoc-string-match
640 mm-inline-media-tests
643 ;; Newlines in description should be stripped so as
644 ;; not to break the MIME tag into two or more lines.
645 description
(message-fetch-field "content-description")
646 id
(mail-fetch-field "content-id"))
648 (setq from
(mail-fetch-field "from")))
649 ;; FIXME: In some circumstances, this code is running within
650 ;; a unibyte macro. mail-extract-address-components
651 ;; creates unibyte buffers. This `if', though not a perfect
652 ;; solution, avoids most of them.
654 (setq from
(cadr (mail-extract-address-components from
))))
656 (setq description
(mail-decode-encoded-word-string
659 (not (string-match "/" (car ctl
))))
660 (mm-dissect-singlepart
661 (list mm-dissect-default-type
)
662 (and cte
(intern (downcase (mail-header-strip cte
))))
664 (and cd
(mail-header-parse-content-disposition cd
))
666 (setq type
(split-string (car ctl
) "/"))
667 (setq subtype
(cadr type
)
672 ((equal type
"multipart")
673 (let ((mm-dissect-default-type (if (equal subtype
"digest")
676 (start (cdr (assq 'start
(cdr ctl
)))))
677 (add-text-properties 0 (length (car ctl
))
678 (mm-alist-to-plist (cdr ctl
)) (car ctl
))
680 ;; what really needs to be done here is a way to link a
681 ;; MIME handle back to it's parent MIME handle (in a multilevel
682 ;; MIME article). That would probably require changing
683 ;; the mm-handle API so we simply store the multipart buffer
684 ;; name as a text property of the "multipart/whatever" string.
685 (add-text-properties 0 (length (car ctl
))
686 (list 'buffer
(mm-copy-to-buffer)
690 (cons (car ctl
) (mm-dissect-multipart ctl from
))))
692 (mm-possibly-verify-or-decrypt
693 (mm-dissect-singlepart
695 (and cte
(intern (downcase (mail-header-strip cte
))))
697 (and cd
(mail-header-parse-content-disposition cd
))
701 (when (string-match " *<\\(.*\\)> *" id
)
702 (setq id
(match-string 1 id
)))
703 (push (cons id result
) mm-content-id-alist
))
706 (defun mm-dissect-singlepart (ctl cte
&optional force cdl description id
)
708 (if (equal "text/plain" (car ctl
))
711 ;; Guess what the type of application/octet-stream parts should
713 (let ((filename (cdr (assq 'filename
(cdr cdl
)))))
714 (when (and (not mm-inhibit-auto-detect-attachment
)
715 (equal (car ctl
) "application/octet-stream")
717 (string-match "\\.\\([^.]+\\)$" filename
))
718 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename
))))
720 (setcar ctl new-type
)))))
723 (mm-copy-to-buffer) ctl cte nil cdl description nil id
))
724 (decoder (assoc (car ctl
) (mm-archive-decoders))))
726 ;; Do automatic decoding
728 (executable-find (caddr decoder
)))
729 (mm-dissect-archive handle
)
732 (defun mm-dissect-multipart (ctl from
)
733 (goto-char (point-min))
734 (let* ((boundary (concat "\n--" (mail-content-type-get ctl
'boundary
)))
735 (close-delimiter (concat (regexp-quote boundary
) "--[ \t]*$"))
738 (goto-char (point-max))
739 (if (re-search-backward close-delimiter nil t
)
742 (mm-inhibit-auto-detect-attachment
743 (equal (car ctl
) "multipart/encrypted")))
744 (setq boundary
(concat (regexp-quote boundary
) "[ \t]*$"))
745 (while (and (< (point) end
) (re-search-forward boundary end t
))
746 (goto-char (match-beginning 0))
750 (narrow-to-region start
(point))
751 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
753 (or (looking-at boundary
)
755 (setq start
(point)))
756 (when (and start
(< start end
))
759 (narrow-to-region start end
)
760 (setq parts
(nconc (list (mm-dissect-buffer t nil from
)) parts
)))))
761 (mm-possibly-verify-or-decrypt (nreverse parts
) ctl from
)))
763 (defun mm-copy-to-buffer ()
764 "Copy the contents of the current buffer to a fresh buffer."
765 (let ((obuf (current-buffer))
766 (mb (mm-multibyte-p))
768 (goto-char (point-min))
769 (search-forward-regexp "^\n" nil t
)
772 (generate-new-buffer " *mm*")
773 ;; Preserve the data's unibyteness (for url-insert-file-contents).
774 (mm-set-buffer-multibyte mb
)
775 (insert-buffer-substring obuf beg
)
778 (defun mm-display-parts (handle &optional no-default
)
779 (if (stringp (car handle
))
780 (mapcar 'mm-display-parts
(cdr handle
))
781 (if (bufferp (car handle
))
783 (narrow-to-region (point) (point))
784 (mm-display-part handle
)
785 (goto-char (point-max)))
786 (mapcar 'mm-display-parts handle
))))
788 (autoload 'mailcap-parse-mailcaps
"mailcap")
789 (autoload 'mailcap-mime-info
"mailcap")
791 (defun mm-head-p (&optional point
)
792 "Return non-nil if point is in the article header."
793 (let ((point (or point
(point))))
796 (and (not (re-search-backward "^$" nil t
))
797 (re-search-forward "^$" nil t
)))))
799 (defun mm-display-part (handle &optional no-default force
)
800 "Display the MIME part represented by HANDLE.
801 Returns nil if the part is removed; inline if displayed inline;
802 external if displayed external."
804 (mailcap-parse-mailcaps)
806 (mm-handle-displayed-p handle
))
807 (mm-remove-part handle
)
808 (let* ((ehandle (if (equal (mm-handle-media-type handle
)
809 "message/external-body")
811 (unless (mm-handle-cache handle
)
812 (mm-extern-cache-contents handle
))
813 (mm-handle-cache handle
))
815 (type (mm-handle-media-type ehandle
))
816 (method (mailcap-mime-info type
))
817 (filename (or (mail-content-type-get
818 (mm-handle-disposition handle
) 'filename
)
819 (mail-content-type-get
820 (mm-handle-type handle
) 'name
)
822 (external mm-enable-external
)
823 (decoder (assoc (car (mm-handle-type handle
))
824 (mm-archive-decoders))))
827 (executable-find (caddr decoder
)))
828 (mm-archive-dissect-and-inline handle
)
830 ((and (mm-inlinable-p ehandle
)
831 (mm-inlined-p ehandle
))
834 (re-search-forward "^$" nil t
)
836 (mm-display-inline handle
)
840 (if (and (not method
)
841 (equal "text" (car (split-string type
"/"))))
844 (mm-insert-inline handle
(mm-get-part handle
))
847 (and method
;; If nil, we always use "save".
848 (or (eq mm-enable-external t
)
849 (and (eq mm-enable-external
'ask
)
852 "Display part (" type
856 "using external program \""
857 (format method filename
) "\"")
859 "by calling `%s' on the contents)" method
))
863 handle
(or method
'mailcap-save-binary-file
))
865 handle
'mailcap-save-binary-file
)))))))))
867 (declare-function gnus-configure-windows
"gnus-win" (setting &optional force
))
868 (defvar mailcap-mime-extensions
) ; mailcap-mime-info autoloads
869 (declare-function term-mode
"term" ())
870 (declare-function term-char-mode
"term" ())
872 (defun mm-display-external (handle method
)
873 "Display HANDLE using METHOD."
874 (let ((outbuf (current-buffer)))
875 (mm-with-unibyte-buffer
876 (if (functionp method
)
877 (let ((cur (current-buffer)))
878 (if (eq method
'mailcap-save-binary-file
)
880 (set-buffer (generate-new-buffer " *mm*"))
882 (mm-insert-part handle
)
883 (mm-add-meta-html-tag handle
)
884 (let ((win (get-buffer-window cur t
)))
886 (select-window win
)))
887 (switch-to-buffer (generate-new-buffer " *mm*")))
888 (buffer-disable-undo)
889 (mm-set-buffer-file-coding-system mm-binary-coding-system
)
890 (insert-buffer-substring cur
)
891 (goto-char (point-min))
893 (message "Viewing with %s" method
))
894 (let ((mm (current-buffer))
895 (non-viewer (assq 'non-viewer
897 (mm-handle-media-type handle
) t
))))
901 (when (and (boundp 'gnus-summary-buffer
)
902 (bufferp gnus-summary-buffer
)
903 (buffer-name gnus-summary-buffer
))
904 ;; So that we pop back to the right place, sort of.
905 (switch-to-buffer gnus-summary-buffer
)
906 (switch-to-buffer mm
))
907 (delete-other-windows)
909 (mm-save-part handle
))
910 (when (and (not non-viewer
)
912 (mm-handle-set-undisplayer handle mm
)))))
913 ;; The function is a string to be executed.
914 (mm-insert-part handle
)
915 (mm-add-meta-html-tag handle
)
916 (let* ((dir (mm-make-temp-file
917 (expand-file-name "emm." mm-tmp-directory
) 'dir
))
919 (mail-content-type-get
920 (mm-handle-disposition handle
) 'filename
)
921 (mail-content-type-get
922 (mm-handle-type handle
) 'name
)))
923 (mime-info (mailcap-mime-info
924 (mm-handle-media-type handle
) t
))
925 (needsterm (or (assoc "needsterm" mime-info
)
926 (assoc "needsterminal" mime-info
)))
927 (copiousoutput (assoc "copiousoutput" mime-info
))
929 ;; We create a private sub-directory where we store our files.
930 (set-file-modes dir
#o700
)
932 (setq file
(expand-file-name
933 (gnus-map-function mm-file-name-rewrite-functions
934 (file-name-nondirectory filename
))
936 ;; Use nametemplate (defined in RFC1524) if it is specified
938 (let ((suffix (cdr (assoc "nametemplate" mime-info
))))
940 (string-match "\\`%s\\(\\..+\\)\\'" suffix
))
941 (setq suffix
(match-string 1 suffix
))
942 ;; Otherwise, use a suffix according to
943 ;; `mailcap-mime-extensions'.
944 (setq suffix
(car (rassoc (mm-handle-media-type handle
)
945 mailcap-mime-extensions
))))
946 (setq file
(mm-make-temp-file (expand-file-name "mm." dir
)
948 (let ((coding-system-for-write mm-binary-coding-system
))
949 (write-region (point-min) (point-max) file nil
'nomesg
))
950 ;; The file is deleted after the viewer exists. If the users edits
951 ;; the file, changes will be lost. Set file to read-only to make it
953 (set-file-modes file
#o400
)
954 (message "Viewing with %s" method
)
957 (let ((command (mm-mailcap-command
958 method file
(mm-handle-type handle
))))
961 (set-process-sentinel
962 (start-process "*display*" nil
963 mm-external-terminal-program
965 shell-command-switch command
)
966 `(lambda (process state
)
967 (if (eq 'exit
(process-status process
))
971 (ignore-errors (delete-file ,file
))
972 (ignore-errors (delete-directory
973 ,(file-name-directory
982 shell-command-switch command
)))
985 (set-process-sentinel
986 (get-buffer-process buffer
)
987 `(lambda (process state
)
988 (when (eq 'exit
(process-status process
))
989 (ignore-errors (delete-file ,file
))
991 (delete-directory ,(file-name-directory file
)))
992 (gnus-configure-windows
993 ',gnus-current-window-configuration
))))
994 (gnus-configure-windows 'display-term
))
995 (mm-handle-set-external-undisplayer handle
(cons file buffer
))
996 (add-to-list 'mm-temp-files-to-be-deleted file t
))
997 (message "Displaying %s..." command
))
1000 (with-current-buffer outbuf
1006 (call-process shell-file-name nil
1008 (generate-new-buffer " *mm*"))
1010 shell-command-switch
1012 method file
(mm-handle-type handle
)))
1013 (if (buffer-live-p buffer
)
1014 (with-current-buffer buffer
1017 (ignore-errors (delete-file file
))
1018 (ignore-errors (delete-directory
1019 (file-name-directory file
)))
1020 (ignore-errors (kill-buffer buffer
))))))
1023 ;; Deleting the temp file should be postponed for some wrappers,
1024 ;; shell scripts, and so on, which might exit right after having
1025 ;; started a viewer command as a background job.
1026 (let ((command (mm-mailcap-command
1027 method file
(mm-handle-type handle
))))
1029 (let ((process-connection-type nil
))
1030 (start-process "*display*"
1032 (generate-new-buffer " *mm*"))
1034 shell-command-switch command
)
1035 (set-process-sentinel
1036 (get-buffer-process buffer
)
1037 (lexical-let ((outbuf outbuf
)
1042 (lambda (process state
)
1043 (when (eq (process-status process
) 'exit
)
1047 (ignore-errors (delete-file file
))
1048 (ignore-errors (delete-directory
1049 (file-name-directory file
)))))
1050 (when (buffer-live-p outbuf
)
1051 (with-current-buffer outbuf
1052 (let ((buffer-read-only nil
)
1055 (let ((start (point)))
1057 handle
(with-current-buffer buffer
1059 (put-text-property start
(point)
1060 'face
'mm-command-output
))
1061 (goto-char point
))))
1062 (when (buffer-live-p buffer
)
1063 (kill-buffer buffer
)))
1064 (message "Displaying %s...done" command
)))))
1065 (mm-handle-set-external-undisplayer
1066 handle
(cons file buffer
))
1067 (add-to-list 'mm-temp-files-to-be-deleted file t
))
1068 (message "Displaying %s..." command
))
1071 (defun mm-mailcap-command (method file type-list
)
1072 (let ((ctl (cdr type-list
))
1076 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1078 (push (substring method beg
(match-beginning 0)) out
)
1079 (setq beg
(match-end 0)
1080 total
(match-string 0 method
)
1081 sub
(match-string 1 method
))
1083 ((string= total
"%%")
1085 ((or (string= total
"%s")
1086 ;; We do our own quoting.
1087 (string= total
"'%s'")
1088 (string= total
"\"%s\""))
1089 (setq uses-stdin nil
)
1090 (push (shell-quote-argument
1091 (gnus-map-function mm-path-name-rewrite-functions file
)) out
))
1092 ((string= total
"%t")
1093 (push (shell-quote-argument (car type-list
)) out
))
1095 (push (shell-quote-argument (or (cdr (assq (intern sub
) ctl
)) "")) out
))))
1096 (push (substring method beg
(length method
)) out
)
1099 (push (shell-quote-argument
1100 (gnus-map-function mm-path-name-rewrite-functions file
))
1102 (mapconcat 'identity
(nreverse out
) "")))
1104 (defun mm-remove-parts (handles)
1105 "Remove the displayed MIME parts represented by HANDLES."
1106 (if (and (listp handles
)
1107 (bufferp (car handles
)))
1108 (mm-remove-part handles
)
1110 (while (setq handle
(pop handles
))
1113 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1114 (kill-buffer (get-text-property 0 'buffer handle
))))
1115 ((and (listp handle
)
1116 (stringp (car handle
)))
1117 (mm-remove-parts (cdr handle
)))
1119 (mm-remove-part handle
)))))))
1121 (defun mm-destroy-parts (handles)
1122 "Remove the displayed MIME parts represented by HANDLES."
1123 (if (and (listp handles
)
1124 (bufferp (car handles
)))
1125 (mm-destroy-part handles
)
1127 (while (setq handle
(pop handles
))
1130 (when (buffer-live-p (get-text-property 0 'buffer handle
))
1131 (kill-buffer (get-text-property 0 'buffer handle
))))
1132 ((and (listp handle
)
1133 (stringp (car handle
)))
1134 (mm-destroy-parts handle
))
1136 (mm-destroy-part handle
)))))))
1138 (defun mm-remove-part (handle)
1139 "Remove the displayed MIME part represented by HANDLE."
1140 (when (listp handle
)
1141 (let ((object (mm-handle-undisplayer handle
)))
1144 ;; Internally displayed part.
1145 ((mm-annotationp object
)
1146 (if (featurep 'xemacs
)
1147 (delete-annotation object
)))
1148 ((or (functionp object
)
1150 (eq (car object
) 'lambda
)))
1152 ;; Externally displayed part.
1155 (while (get-buffer-process (cdr object
))
1156 (interrupt-process (get-buffer-process (cdr object
)))
1157 (message "Waiting for external displayer to die...")
1161 (ignore-errors (and (cdr object
) (kill-buffer (cdr object
))))
1162 (message "Waiting for external displayer to die...done")
1163 (ignore-errors (delete-file (car object
)))
1164 (ignore-errors (delete-directory (file-name-directory
1167 (when (buffer-live-p object
)
1168 (kill-buffer object
)))))
1169 (mm-handle-set-undisplayer handle nil
))))
1171 (defun mm-display-inline (handle)
1172 (let* ((type (mm-handle-media-type handle
))
1173 (function (cadr (mm-assoc-string-match mm-inline-media-tests type
))))
1174 (funcall function handle
)
1175 (goto-char (point-min))))
1177 (defun mm-assoc-string-match (alist type
)
1178 (dolist (elem alist
)
1179 (when (string-match (car elem
) type
)
1182 (defun mm-automatic-display-p (handle)
1183 "Say whether the user wants HANDLE to be displayed automatically."
1184 (let ((methods mm-automatic-display
)
1185 (type (mm-handle-media-type handle
))
1187 (while (setq method
(pop methods
))
1188 (when (and (not (mm-inline-override-p handle
))
1189 (string-match method type
))
1194 (defun mm-inlinable-p (handle &optional type
)
1195 "Say whether HANDLE can be displayed inline.
1196 TYPE is the mime-type of the object; it defaults to the one given
1198 (unless type
(setq type
(mm-handle-media-type handle
)))
1199 (let ((alist mm-inline-media-tests
)
1202 (when (string-match (caar alist
) type
)
1203 (setq test
(caddar alist
)
1205 (setq test
(funcall test handle
)))
1209 (defun mm-inlined-p (handle)
1210 "Say whether the user wants HANDLE to be displayed inline."
1211 (let ((methods mm-inlined-types
)
1212 (type (mm-handle-media-type handle
))
1214 (while (setq method
(pop methods
))
1215 (when (and (not (mm-inline-override-p handle
))
1216 (string-match method type
))
1221 (defun mm-attachment-override-p (handle)
1222 "Say whether HANDLE should have attachment behavior overridden."
1223 (let ((types mm-attachment-override-types
)
1224 (type (mm-handle-media-type handle
))
1227 (while (setq ty
(pop types
))
1228 (when (and (string-match ty type
)
1229 (mm-inlinable-p handle
))
1230 (throw 'found t
))))))
1232 (defun mm-inline-override-p (handle)
1233 "Say whether HANDLE should have inline behavior overridden."
1234 (let ((types mm-inline-override-types
)
1235 (type (mm-handle-media-type handle
))
1238 (while (setq ty
(pop types
))
1239 (when (string-match ty type
)
1240 (throw 'found t
))))))
1242 (defun mm-automatic-external-display-p (type)
1243 "Return the user-defined method for TYPE."
1244 (let ((methods mm-automatic-external-display
)
1246 (while (setq method
(pop methods
))
1247 (when (string-match method type
)
1252 (defun mm-destroy-part (handle)
1253 "Destroy the data structures connected to HANDLE."
1254 (when (listp handle
)
1255 (mm-remove-part handle
)
1256 (when (buffer-live-p (mm-handle-buffer handle
))
1257 (kill-buffer (mm-handle-buffer handle
)))))
1259 (defun mm-handle-displayed-p (handle)
1260 "Say whether HANDLE is displayed or not."
1261 (mm-handle-undisplayer handle
))
1264 ;;; Functions for outputting parts
1267 (defmacro mm-with-part
(handle &rest forms
)
1268 "Run FORMS in the temp buffer containing the contents of HANDLE."
1269 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1270 ;; chars, so the buffer should be unibyte. It may happen that the
1271 ;; handle-buffer is multibyte for some reason, in which case now is a good
1272 ;; time to adjust it, since we know at this point that it should
1274 `(let* ((handle ,handle
))
1275 (when (and (mm-handle-buffer handle
)
1276 (buffer-name (mm-handle-buffer handle
)))
1278 (mm-disable-multibyte)
1279 (insert-buffer-substring (mm-handle-buffer handle
))
1280 (mm-decode-content-transfer-encoding
1281 (mm-handle-encoding handle
)
1282 (mm-handle-media-type handle
))
1284 (put 'mm-with-part
'lisp-indent-function
1)
1285 (put 'mm-with-part
'edebug-form-spec
'(body))
1287 (defun mm-get-part (handle &optional no-cache
)
1288 "Return the contents of HANDLE as a string.
1289 If NO-CACHE is non-nil, cached contents of a message/external-body part
1291 (if (and (not no-cache
)
1292 (equal (mm-handle-media-type handle
) "message/external-body"))
1294 (unless (mm-handle-cache handle
)
1295 (mm-extern-cache-contents handle
))
1296 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle
))
1298 (mm-with-part handle
1301 (defun mm-insert-part (handle &optional no-cache
)
1302 "Insert the contents of HANDLE in the current buffer.
1303 If NO-CACHE is non-nil, cached contents of a message/external-body part
1305 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle
)
1308 (with-current-buffer (mm-handle-buffer handle
)
1311 (mm-string-to-multibyte (mm-get-part handle no-cache
)))
1313 (mm-get-part handle no-cache
)))))
1319 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face
)
1321 (eq (get-char-property 0 'face text
) 'mm-uu-extract
))
1322 ;; Separate the extracted parts that have the same faces.
1326 (defun mm-file-name-delete-whitespace (file-name)
1327 "Remove all whitespace characters from FILE-NAME."
1328 (while (string-match "\\s-+" file-name
)
1329 (setq file-name
(replace-match "" t t file-name
)))
1332 (defun mm-file-name-trim-whitespace (file-name)
1333 "Remove leading and trailing whitespace characters from FILE-NAME."
1334 (when (string-match "\\`\\s-+" file-name
)
1335 (setq file-name
(substring file-name
(match-end 0))))
1336 (when (string-match "\\s-+\\'" file-name
)
1337 (setq file-name
(substring file-name
0 (match-beginning 0))))
1340 (defun mm-file-name-collapse-whitespace (file-name)
1341 "Collapse multiple whitespace characters in FILE-NAME."
1342 (while (string-match "\\s-\\s-+" file-name
)
1343 (setq file-name
(replace-match " " t t file-name
)))
1346 (defun mm-file-name-replace-whitespace (file-name)
1347 "Replace whitespace characters in FILE-NAME with underscores.
1348 Set the option `mm-file-name-replace-whitespace' to any other
1349 string if you do not like underscores."
1350 (let ((s (or mm-file-name-replace-whitespace
"_")))
1351 (while (string-match "\\s-" file-name
)
1352 (setq file-name
(replace-match s t t file-name
))))
1355 (defun mm-file-name-delete-control (filename)
1356 "Delete control characters from FILENAME."
1357 (gnus-replace-in-string filename
"[\x00-\x1f\x7f]" ""))
1359 (defun mm-file-name-delete-gotchas (filename)
1360 "Delete shell gotchas from FILENAME."
1361 (setq filename
(gnus-replace-in-string filename
"[<>|]" ""))
1362 (gnus-replace-in-string filename
"^[.-]+" ""))
1364 (defun mm-save-part (handle &optional prompt
)
1365 "Write HANDLE to a file.
1366 PROMPT overrides the default one used to ask user for a file name."
1367 (let ((filename (or (mail-content-type-get
1368 (mm-handle-disposition handle
) 'filename
)
1369 (mail-content-type-get
1370 (mm-handle-type handle
) 'name
)))
1373 (setq filename
(gnus-map-function mm-file-name-rewrite-functions
1374 (file-name-nondirectory filename
))))
1380 (format "Save MIME part to (default %s): "
1382 (or mm-default-directory default-directory
)
1383 (expand-file-name (or filename
"")
1384 (or mm-default-directory default-directory
))))
1385 (cond ((or (not file
) (equal file
""))
1386 (message "Please enter a file name")
1388 ((and (file-directory-p file
)
1390 (message "Please enter a non-directory file name")
1395 (if (file-directory-p file
)
1396 (setq file
(expand-file-name filename file
))
1397 (setq file
(expand-file-name
1398 file
(or mm-default-directory default-directory
))))
1399 (setq mm-default-directory
(file-name-directory file
))
1400 (and (or (not (file-exists-p file
))
1401 (yes-or-no-p (format "File %s already exists; overwrite? "
1404 (mm-save-part-to-file handle file
)
1407 (defun mm-add-meta-html-tag (handle &optional charset force-charset
)
1408 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1409 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1410 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1411 Return t if meta tag is added or replaced."
1412 (when (equal (mm-handle-media-type handle
) "text/html")
1414 (setq charset
(mail-content-type-get (mm-handle-type handle
)
1416 (setq charset
(format "\
1417 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset
))
1418 (let ((case-fold-search t
))
1419 (goto-char (point-min))
1420 (if (re-search-forward "\
1421 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1422 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\([^\"'>]+\\)\\)?[^>]*>" nil t
)
1423 (if (and (not force-charset
)
1425 (string-match "\\`html\\'" (match-string 1)))
1426 ;; Don't modify existing meta tag.
1428 ;; Replace it with the one specifying charset.
1429 (replace-match charset
)
1431 (if (re-search-forward "<head>\\s-*" nil t
)
1432 (insert charset
"\n")
1433 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t
)
1434 (insert "<head>\n" charset
"\n</head>\n"))
1437 (defun mm-save-part-to-file (handle file
)
1438 (mm-with-unibyte-buffer
1439 (mm-insert-part handle
)
1440 (mm-add-meta-html-tag handle
)
1441 (let ((current-file-modes (default-file-modes)))
1442 (set-default-file-modes mm-attachment-file-modes
)
1444 ;; Don't re-compress .gz & al. Arguably we should make
1445 ;; `file-name-handler-alist' nil, but that would chop
1446 ;; ange-ftp, which is reasonable to use here.
1447 (mm-write-region (point-min) (point-max) file nil nil nil
'binary t
)
1448 (set-default-file-modes current-file-modes
)))))
1450 (defun mm-pipe-part (handle &optional cmd
)
1451 "Pipe HANDLE to a process.
1452 Use CMD as the process."
1453 (let ((name (mail-content-type-get (mm-handle-type handle
) 'name
))
1455 (gnus-read-shell-command
1456 "Shell command on MIME part: " mm-last-shell-command
))))
1457 (mm-with-unibyte-buffer
1458 (mm-insert-part handle
)
1459 (mm-add-meta-html-tag handle
)
1460 (let ((coding-system-for-write 'binary
))
1461 (shell-command-on-region (point-min) (point-max) command nil
)))))
1463 (autoload 'gnus-completing-read
"gnus-util")
1465 (defun mm-interactively-view-part (handle)
1466 "Display HANDLE using METHOD."
1467 (let* ((type (mm-handle-media-type handle
))
1469 (mapcar (lambda (i) (cdr (assoc 'viewer i
)))
1470 (mailcap-mime-info type
'all
)))
1471 (method (let ((minibuffer-local-completion-map
1472 mm-viewer-completion-map
))
1473 (completing-read "Viewer: " methods
))))
1474 (when (string= method
"")
1475 (error "No method given"))
1476 (if (string-match "^[^% \t]+$" method
)
1477 (setq method
(concat method
" %s")))
1478 (mm-display-external handle method
)))
1480 (defun mm-preferred-alternative (handles &optional preferred
)
1481 "Say which of HANDLES are preferred."
1482 (let ((prec (if preferred
(list preferred
)
1483 (mm-preferred-alternative-precedence handles
)))
1484 p h result type handle
)
1485 (while (setq p
(pop prec
))
1488 (setq handle
(car h
))
1489 (setq type
(mm-handle-media-type handle
))
1490 (when (and (equal p type
)
1491 (mm-automatic-display-p handle
)
1492 (or (stringp (car handle
))
1493 (not (mm-handle-disposition handle
))
1494 (equal (car (mm-handle-disposition handle
))
1502 (defun mm-preferred-alternative-precedence (handles)
1503 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1504 (setq handles
(reverse handles
))
1505 (dolist (disc (reverse mm-discouraged-alternatives
))
1506 (dolist (handle (copy-sequence handles
))
1507 (when (string-match disc
(mm-handle-media-type handle
))
1508 (setq handles
(nconc (delete handle handles
) (list handle
))))))
1509 ;; Remove empty parts.
1510 (dolist (handle (copy-sequence handles
))
1511 (when (and (bufferp (mm-handle-buffer handle
))
1512 (not (with-current-buffer (mm-handle-buffer handle
)
1513 (goto-char (point-min))
1514 (re-search-forward "[^ \t\n]" nil t
))))
1515 (setq handles
(nconc (delete handle handles
) (list handle
)))))
1516 (mapcar #'mm-handle-media-type handles
))
1518 (defun mm-get-content-id (id)
1519 "Return the handle(s) referred to by ID."
1520 (cdr (assoc id mm-content-id-alist
)))
1522 (defconst mm-image-type-regexps
1523 '(("/\\*.*XPM.\\*/" . xpm
)
1527 ("\211PNG\r\n" . png
)
1529 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
1530 ("%!PS" . postscript
))
1531 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1532 When the first bytes of an image file match REGEXP, it is assumed to
1533 be of image type IMAGE-TYPE.")
1535 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1536 (defun mm-image-type-from-buffer ()
1537 "Determine the image type from data in the current buffer.
1538 Value is a symbol specifying the image type or nil if type cannot
1540 (let ((types mm-image-type-regexps
)
1542 (goto-char (point-min))
1543 (while (and types
(null type
))
1544 (let ((regexp (car (car types
)))
1545 (image-type (cdr (car types
))))
1546 (when (looking-at regexp
)
1547 (setq type image-type
))
1548 (setq types
(cdr types
))))
1551 (defun mm-get-image (handle)
1552 "Return an image instance based on HANDLE."
1553 (let ((type (mm-handle-media-subtype handle
))
1555 ;; Allow some common translations.
1558 ((equal type
"x-pixmap")
1560 ((equal type
"x-xbitmap")
1562 ((equal type
"x-portable-bitmap")
1565 (or (mm-handle-cache handle
)
1566 (mm-with-unibyte-buffer
1567 (mm-insert-part handle
)
1571 ;; Avoid testing `make-glyph' since W3 may define
1572 ;; a bogus version of it.
1573 (if (fboundp 'create-image
)
1574 (create-image (buffer-string)
1575 (or (mm-image-type-from-buffer)
1578 (mm-create-image-xemacs type
))))
1579 (mm-handle-set-cache handle spec
))))))
1581 (defun mm-create-image-xemacs (type)
1582 (when (featurep 'xemacs
)
1585 ;; xbm images require special handling, since
1586 ;; the only way to create glyphs from these
1587 ;; (without a ton of work) is to write them
1588 ;; out to a file, and then create a file
1590 (let ((file (mm-make-temp-file
1591 (expand-file-name "emm" mm-tmp-directory
)
1595 (write-region (point-min) (point-max) file
)
1596 (make-glyph (list (cons 'x file
))))
1598 (delete-file file
)))))
1602 (or (mm-image-type-from-buffer)
1604 :data
(buffer-string)))))))
1606 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1608 (defun mm-image-fit-p (handle)
1609 "Say whether the image in HANDLE will fit the current window."
1610 (let ((image (mm-get-image handle
)))
1612 (if (featurep 'xemacs
)
1613 ;; XEmacs's glyphs can actually tell us about their width, so
1614 ;; let's be nice and smart about them.
1615 (or mm-inline-large-images
1616 (and (<= (glyph-width image
) (window-pixel-width))
1617 (<= (glyph-height image
) (window-pixel-height))))
1618 (let* ((size (image-size image
))
1621 (or mm-inline-large-images
1622 (and (<= h
(1- (window-height))) ; Don't include mode line.
1623 (<= w
(window-width)))))))))
1625 (defun mm-valid-image-format-p (format)
1626 "Say whether FORMAT can be displayed natively by Emacs."
1629 ((fboundp 'valid-image-instantiator-format-p
)
1630 (valid-image-instantiator-format-p format
))
1632 ((fboundp 'image-type-available-p
)
1633 (and (display-graphic-p)
1634 (image-type-available-p format
)))
1635 ;; Nobody else can do images yet.
1639 (defun mm-valid-and-fit-image-p (format handle
)
1640 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1641 (and (mm-valid-image-format-p format
)
1642 (mm-image-fit-p handle
)))
1644 (defun mm-find-part-by-type (handles type
&optional notp recursive
)
1645 "Search in HANDLES for part with TYPE.
1646 If NOTP, returns first non-matching part.
1647 If RECURSIVE, search recursively."
1650 (if (and recursive
(stringp (caar handles
)))
1651 (if (setq handle
(mm-find-part-by-type (cdar handles
) type
1655 (not (equal (mm-handle-media-type (car handles
)) type
))
1656 (equal (mm-handle-media-type (car handles
)) type
))
1657 (setq handle
(car handles
)
1659 (setq handles
(cdr handles
)))
1662 (defun mm-find-raw-part-by-type (ctl type
&optional notp
)
1663 (goto-char (point-min))
1664 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1666 (close-delimiter (concat "^" (regexp-quote boundary
) "--[ \t]*$"))
1668 (end (save-excursion
1669 (goto-char (point-max))
1670 (if (re-search-backward close-delimiter nil t
)
1674 (setq boundary
(concat "^" (regexp-quote boundary
) "[ \t]*$"))
1675 (while (and (not result
)
1676 (re-search-forward boundary end t
))
1677 (goto-char (match-beginning 0))
1681 (narrow-to-region start
(1- (point)))
1682 (when (let* ((ct (mail-fetch-field "content-type"))
1683 (ctl (and ct
(mail-header-parse-content-type ct
))))
1685 (not (equal (car ctl
) type
))
1686 (equal (car ctl
) type
)))
1687 (setq result
(buffer-string))))))
1689 (setq start
(point)))
1690 (when (and (not result
) start
)
1693 (narrow-to-region start end
)
1694 (when (let* ((ct (mail-fetch-field "content-type"))
1695 (ctl (and ct
(mail-header-parse-content-type ct
))))
1697 (not (equal (car ctl
) type
))
1698 (equal (car ctl
) type
)))
1699 (setq result
(buffer-string))))))
1702 (defvar mm-security-handle nil
)
1704 (defsubst mm-set-handle-multipart-parameter
(handle parameter value
)
1705 ;; HANDLE could be a CTL.
1707 (put-text-property 0 (length (car handle
)) parameter value
1710 (autoload 'mm-view-pkcs7
"mm-view")
1712 (defun mm-possibly-verify-or-decrypt (parts ctl
&optional from
)
1713 (let ((type (car ctl
))
1714 (subtype (cadr (split-string (car ctl
) "/")))
1715 (mm-security-handle ctl
) ;; (car CTL) is the type.
1716 protocol func functest
)
1718 ((or (equal type
"application/x-pkcs7-mime")
1719 (equal type
"application/pkcs7-mime"))
1722 ((eq mm-decrypt-option
'never
) nil
)
1723 ((eq mm-decrypt-option
'always
) t
)
1724 ((eq mm-decrypt-option
'known
) t
)
1726 (format "Decrypt (S/MIME) part? "))))
1727 (mm-view-pkcs7 parts from
))
1728 (setq parts
(mm-dissect-buffer t
)))))
1729 ((equal subtype
"signed")
1730 (unless (and (setq protocol
1731 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1732 (not (equal protocol
"multipart/mixed")))
1733 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1734 (let ((protocols mm-verify-function-alist
))
1736 (if (and (or (not (setq functest
(nth 3 (car protocols
))))
1737 (funcall functest parts ctl
))
1738 (mm-find-part-by-type parts
(caar protocols
) nil t
))
1739 (setq protocol
(caar protocols
)
1741 (setq protocols
(cdr protocols
))))))
1742 (setq func
(nth 1 (assoc protocol mm-verify-function-alist
)))
1744 ((eq mm-verify-option
'never
) nil
)
1745 ((eq mm-verify-option
'always
) t
)
1746 ((eq mm-verify-option
'known
)
1748 (or (not (setq functest
1749 (nth 3 (assoc protocol
1750 mm-verify-function-alist
))))
1751 (funcall functest parts ctl
))))
1754 (format "Verify signed (%s) part? "
1755 (or (nth 2 (assoc protocol mm-verify-function-alist
))
1756 (format "protocol=%s" protocol
))))))
1759 (setq parts
(funcall func parts ctl
))
1760 (mm-set-handle-multipart-parameter
1761 mm-security-handle
'gnus-details
1762 (format "Unknown sign protocol (%s)" protocol
))))))
1763 ((equal subtype
"encrypted")
1764 (unless (setq protocol
1765 (mm-handle-multipart-ctl-parameter ctl
'protocol
))
1766 ;; The message is broken.
1767 (let ((parts parts
))
1769 (if (assoc (mm-handle-media-type (car parts
))
1770 mm-decrypt-function-alist
)
1771 (setq protocol
(mm-handle-media-type (car parts
))
1773 (setq parts
(cdr parts
))))))
1774 (setq func
(nth 1 (assoc protocol mm-decrypt-function-alist
)))
1776 ((eq mm-decrypt-option
'never
) nil
)
1777 ((eq mm-decrypt-option
'always
) t
)
1778 ((eq mm-decrypt-option
'known
)
1780 (or (not (setq functest
1781 (nth 3 (assoc protocol
1782 mm-decrypt-function-alist
))))
1783 (funcall functest parts ctl
))))
1786 (format "Decrypt (%s) part? "
1787 (or (nth 2 (assoc protocol mm-decrypt-function-alist
))
1788 (format "protocol=%s" protocol
))))))
1791 (setq parts
(funcall func parts ctl
))
1792 (mm-set-handle-multipart-parameter
1793 mm-security-handle
'gnus-details
1794 (format "Unknown encrypt protocol (%s)" protocol
))))))
1798 (defun mm-multiple-handles (handles)
1799 (and (listp handles
)
1800 (> (length handles
) 1)
1801 (or (listp (car handles
))
1802 (stringp (car handles
)))))
1804 (defun mm-complicated-handles (handles)
1805 (and (listp (car handles
))
1806 (> (length handles
) 1)))
1808 (defun mm-merge-handles (handles1 handles2
)
1810 (if (listp (car handles1
))
1813 (if (listp (car handles2
))
1817 (defun mm-readable-p (handle)
1818 "Say whether the content of HANDLE is readable."
1819 (and (< (with-current-buffer (mm-handle-buffer handle
)
1820 (buffer-size)) 10000)
1821 (mm-with-unibyte-buffer
1822 (mm-insert-part handle
)
1823 (and (eq (mm-body-7-or-8) '7bit
)
1824 (not (mm-long-lines-p 76))))))
1826 (declare-function libxml-parse-html-region
"xml.c"
1827 (start end
&optional base-url discard-comments
))
1828 (declare-function shr-insert-document
"shr" (dom))
1829 (defvar shr-blocked-images
)
1830 (defvar shr-use-fonts
)
1831 (defvar gnus-inhibit-images
)
1832 (autoload 'gnus-blocked-images
"gnus-art")
1834 (defun mm-shr (handle)
1835 ;; Require since we bind its variables.
1837 (let ((article-buffer (current-buffer))
1838 (shr-width (if (and (boundp 'shr-use-fonts
)
1842 (shr-content-function (lambda (id)
1843 (let ((handle (mm-get-content-id id
)))
1845 (mm-with-part handle
1846 (buffer-string))))))
1847 shr-inhibit-images shr-blocked-images charset char
)
1848 (if (and (boundp 'gnus-summary-buffer
)
1849 (bufferp gnus-summary-buffer
)
1850 (buffer-name gnus-summary-buffer
))
1851 (with-current-buffer gnus-summary-buffer
1852 (setq shr-inhibit-images gnus-inhibit-images
1853 shr-blocked-images
(gnus-blocked-images)))
1854 (setq shr-inhibit-images gnus-inhibit-images
1855 shr-blocked-images
(gnus-blocked-images)))
1857 (setq handle
(mm-dissect-buffer t
)))
1858 (setq charset
(mail-content-type-get (mm-handle-type handle
) 'charset
))
1860 (narrow-to-region (point) (point))
1861 (shr-insert-document
1862 (mm-with-part handle
1866 (mm-charset-to-coding-system charset
1868 (not (eq charset
'ascii
)))
1869 (mm-decode-coding-string (buffer-string) charset
)
1870 (mm-string-as-multibyte (buffer-string)))
1872 (mm-enable-multibyte)))
1873 (goto-char (point-min))
1874 (setq case-fold-search t
)
1875 (while (re-search-forward
1876 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t
)
1878 (cdr (assq (if (match-beginning 1)
1879 (string-to-number (match-string 1) 16)
1880 (string-to-number (match-string 2)))
1881 mm-extra-numeric-entities
)))
1882 (replace-match (char-to-string char
))))
1883 ;; Remove "soft hyphens".
1884 (goto-char (point-min))
1885 (while (search-forward "" nil t
)
1886 (replace-match "" t t
))
1887 (libxml-parse-html-region (point-min) (point-max))))
1890 (mm-convert-shr-links)
1891 (mm-handle-set-undisplayer
1894 (let ((inhibit-read-only t
))
1895 (delete-region ,(point-min-marker)
1896 ,(point-max-marker))))))))
1900 (autoload 'widget-convert-button
"wid-edit")
1902 (defun mm-convert-shr-links ()
1903 (let ((start (point-min))
1906 (< start
(point-max)))
1907 (when (setq start
(text-property-not-all start
(point-max) 'shr-url nil
))
1908 (setq end
(next-single-property-change start
'shr-url nil
(point-max)))
1909 (widget-convert-button
1911 :help-echo
(get-text-property start
'help-echo
)
1913 (get-text-property start
'shr-url
))
1914 (put-text-property start end
'local-map nil
)
1915 (dolist (overlay (overlays-at start
))
1916 (overlay-put overlay
'face nil
))
1917 (setq start end
)))))
1919 (defun mm-handle-filename (handle)
1920 "Return filename of HANDLE if any."
1921 (or (mail-content-type-get (mm-handle-type handle
)
1923 (mail-content-type-get (mm-handle-disposition handle
)
1926 (provide 'mm-decode
)
1932 ;;; mm-decode.el ends here