(picture-mode-exit): Run picture-mode-exit-hook.
[emacs.git] / lisp / gnus / mm-decode.el
blob60cbb413473d5cb46469f5a878d2831570a44906
1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;;; Code:
29 (require 'mail-parse)
30 (require 'mailcap)
31 (require 'mm-bodies)
32 (eval-when-compile (require 'cl)
33 (require 'term))
35 (eval-and-compile
36 (autoload 'executable-find "executable")
37 (autoload 'mm-inline-partial "mm-partial")
38 (autoload 'mm-inline-external-body "mm-extern")
39 (autoload 'mm-extern-cache-contents "mm-extern")
40 (autoload 'mm-insert-inline "mm-view"))
42 (defvar gnus-current-window-configuration)
44 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
46 (defgroup mime-display ()
47 "Display of MIME in mail and news articles."
48 :link '(custom-manual "(emacs-mime)Display Customization")
49 :version "21.1"
50 :group 'mail
51 :group 'news
52 :group 'multimedia)
54 (defgroup mime-security ()
55 "MIME security in mail and news articles."
56 :link '(custom-manual "(emacs-mime)Display Customization")
57 :group 'mail
58 :group 'news
59 :group 'multimedia)
61 ;;; Convenience macros.
63 (defmacro mm-handle-buffer (handle)
64 `(nth 0 ,handle))
65 (defmacro mm-handle-type (handle)
66 `(nth 1 ,handle))
67 (defsubst mm-handle-media-type (handle)
68 (if (stringp (car handle))
69 (car handle)
70 (car (mm-handle-type handle))))
71 (defsubst mm-handle-media-supertype (handle)
72 (car (split-string (mm-handle-media-type handle) "/")))
73 (defsubst mm-handle-media-subtype (handle)
74 (cadr (split-string (mm-handle-media-type handle) "/")))
75 (defmacro mm-handle-encoding (handle)
76 `(nth 2 ,handle))
77 (defmacro mm-handle-undisplayer (handle)
78 `(nth 3 ,handle))
79 (defmacro mm-handle-set-undisplayer (handle function)
80 `(setcar (nthcdr 3 ,handle) ,function))
81 (defmacro mm-handle-disposition (handle)
82 `(nth 4 ,handle))
83 (defmacro mm-handle-description (handle)
84 `(nth 5 ,handle))
85 (defmacro mm-handle-cache (handle)
86 `(nth 6 ,handle))
87 (defmacro mm-handle-set-cache (handle contents)
88 `(setcar (nthcdr 6 ,handle) ,contents))
89 (defmacro mm-handle-id (handle)
90 `(nth 7 ,handle))
91 (defmacro mm-handle-multipart-original-buffer (handle)
92 `(get-text-property 0 'buffer (car ,handle)))
93 (defmacro mm-handle-multipart-from (handle)
94 `(get-text-property 0 'from (car ,handle)))
95 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
96 `(get-text-property 0 ,parameter (car ,handle)))
98 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
99 disposition description cache
101 `(list ,buffer ,type ,encoding ,undisplayer
102 ,disposition ,description ,cache ,id))
104 (defcustom mm-text-html-renderer
105 (cond ((locate-library "w3") 'w3)
106 ((executable-find "w3m") (if (locate-library "w3m")
107 'w3m
108 'w3m-standalone))
109 ((executable-find "links") 'links)
110 ((executable-find "lynx") 'lynx)
111 (t 'html2text))
112 "Render of HTML contents.
113 It is one of defined renderer types, or a rendering function.
114 The defined renderer types are:
115 `w3' : use Emacs/W3;
116 `w3m' : use emacs-w3m;
117 `w3m-standalone': use w3m;
118 `links': use links;
119 `lynx' : use lynx;
120 `html2text' : use html2text;
121 nil : use external viewer."
122 :version "22.1"
123 :type '(choice (const w3)
124 (const w3m)
125 (const w3m-standalone)
126 (const links)
127 (const lynx)
128 (const html2text)
129 (const nil)
130 (function))
131 :group 'mime-display)
133 (defvar mm-inline-text-html-renderer nil
134 "Function used for rendering inline HTML contents.
135 It is suggested to customize `mm-text-html-renderer' instead.")
137 (defcustom mm-inline-text-html-with-images nil
138 "If non-nil, Gnus will allow retrieving images in HTML contents with
139 the <img> tags. It has no effect on Emacs/w3. See also the
140 documentation for the `mm-w3m-safe-url-regexp' variable."
141 :version "22.1"
142 :type 'boolean
143 :group 'mime-display)
145 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
146 "Regexp matching URLs which are considered to be safe.
147 Some HTML mails might contain a nasty trick used by spammers, using
148 the <img> tag which is far more evil than the [Click Here!] button.
149 It is most likely intended to check whether the ominous spam mail has
150 reached your eyes or not, in which case the spammer knows for sure
151 that your email address is valid. It is done by embedding an
152 identifier string into a URL that you might automatically retrieve
153 when displaying the image. The default value is \"\\\\`cid:\" which only
154 matches parts embedded to the Multipart/Related type MIME contents and
155 Gnus will never connect to the spammer's site arbitrarily. You may
156 set this variable to nil if you consider all urls to be safe."
157 :version "22.1"
158 :type '(choice (regexp :tag "Regexp")
159 (const :tag "All URLs are safe" nil))
160 :group 'mime-display)
162 (defcustom mm-inline-text-html-with-w3m-keymap t
163 "If non-nil, use emacs-w3m command keys in the article buffer."
164 :version "22.1"
165 :type 'boolean
166 :group 'mime-display)
168 (defcustom mm-enable-external t
169 "Indicate whether external MIME handlers should be used.
171 If t, all defined external MIME handlers are used. If nil, files are saved by
172 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
173 before the external MIME handler is invoked."
174 :version "22.1"
175 :type '(choice (const :tag "Always" t)
176 (const :tag "Never" nil)
177 (const :tag "Ask" ask))
178 :group 'mime-display)
180 (defcustom mm-inline-media-tests
181 '(("image/p?jpeg"
182 mm-inline-image
183 (lambda (handle)
184 (mm-valid-and-fit-image-p 'jpeg handle)))
185 ("image/png"
186 mm-inline-image
187 (lambda (handle)
188 (mm-valid-and-fit-image-p 'png handle)))
189 ("image/gif"
190 mm-inline-image
191 (lambda (handle)
192 (mm-valid-and-fit-image-p 'gif handle)))
193 ("image/tiff"
194 mm-inline-image
195 (lambda (handle)
196 (mm-valid-and-fit-image-p 'tiff handle)) )
197 ("image/xbm"
198 mm-inline-image
199 (lambda (handle)
200 (mm-valid-and-fit-image-p 'xbm handle)))
201 ("image/x-xbitmap"
202 mm-inline-image
203 (lambda (handle)
204 (mm-valid-and-fit-image-p 'xbm handle)))
205 ("image/xpm"
206 mm-inline-image
207 (lambda (handle)
208 (mm-valid-and-fit-image-p 'xpm handle)))
209 ("image/x-xpixmap"
210 mm-inline-image
211 (lambda (handle)
212 (mm-valid-and-fit-image-p 'xpm handle)))
213 ("image/bmp"
214 mm-inline-image
215 (lambda (handle)
216 (mm-valid-and-fit-image-p 'bmp handle)))
217 ("image/x-portable-bitmap"
218 mm-inline-image
219 (lambda (handle)
220 (mm-valid-and-fit-image-p 'pbm handle)))
221 ("text/plain" mm-inline-text identity)
222 ("text/enriched" mm-inline-text identity)
223 ("text/richtext" mm-inline-text identity)
224 ("text/x-patch" mm-display-patch-inline
225 (lambda (handle)
226 ;; If the diff-mode.el package is installed, the function is
227 ;; autoloaded. Checking (locate-library "diff-mode") would be trying
228 ;; to cater to broken installations. OTOH checking the function
229 ;; makes it possible to install another package which provides an
230 ;; alternative implementation of diff-mode. --Stef
231 (fboundp 'diff-mode)))
232 ("application/emacs-lisp" mm-display-elisp-inline identity)
233 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
234 ("text/html"
235 mm-inline-text-html
236 (lambda (handle)
237 (or mm-inline-text-html-renderer
238 mm-text-html-renderer)))
239 ("text/x-vcard"
240 mm-inline-text-vcard
241 (lambda (handle)
242 (or (featurep 'vcard)
243 (locate-library "vcard"))))
244 ("message/delivery-status" mm-inline-text identity)
245 ("message/rfc822" mm-inline-message identity)
246 ("message/partial" mm-inline-partial identity)
247 ("message/external-body" mm-inline-external-body identity)
248 ("text/.*" mm-inline-text identity)
249 ("audio/wav" mm-inline-audio
250 (lambda (handle)
251 (and (or (featurep 'nas-sound) (featurep 'native-sound))
252 (device-sound-enabled-p))))
253 ("audio/au"
254 mm-inline-audio
255 (lambda (handle)
256 (and (or (featurep 'nas-sound) (featurep 'native-sound))
257 (device-sound-enabled-p))))
258 ("application/pgp-signature" ignore identity)
259 ("application/x-pkcs7-signature" ignore identity)
260 ("application/pkcs7-signature" ignore identity)
261 ("application/x-pkcs7-mime" ignore identity)
262 ("application/pkcs7-mime" ignore identity)
263 ("multipart/alternative" ignore identity)
264 ("multipart/mixed" ignore identity)
265 ("multipart/related" ignore identity)
266 ;; Disable audio and image
267 ("audio/.*" ignore ignore)
268 ("image/.*" ignore ignore)
269 ;; Default to displaying as text
270 (".*" mm-inline-text mm-readable-p))
271 "Alist of media types/tests saying whether types can be displayed inline."
272 :type '(repeat (list (regexp :tag "MIME type")
273 (function :tag "Display function")
274 (function :tag "Display test")))
275 :group 'mime-display)
277 (defcustom mm-inlined-types
278 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
279 "message/partial" "message/external-body" "application/emacs-lisp"
280 "application/x-emacs-lisp"
281 "application/pgp-signature" "application/x-pkcs7-signature"
282 "application/pkcs7-signature" "application/x-pkcs7-mime"
283 "application/pkcs7-mime"
284 ;; Mutt still uses this even though it has already been withdrawn.
285 "application/pgp")
286 "List of media types that are to be displayed inline.
287 See also `mm-inline-media-tests', which says how to display a media
288 type inline."
289 :type '(repeat regexp)
290 :group 'mime-display)
292 (defcustom mm-keep-viewer-alive-types
293 '("application/postscript" "application/msword" "application/vnd.ms-excel"
294 "application/pdf" "application/x-dvi")
295 "List of media types for which the external viewer will not be killed
296 when selecting a different article."
297 :version "22.1"
298 :type '(repeat regexp)
299 :group 'mime-display)
301 (defcustom mm-automatic-display
302 '("text/plain" "text/enriched" "text/richtext" "text/html"
303 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
304 "message/rfc822" "text/x-patch" "application/pgp-signature"
305 "application/emacs-lisp" "application/x-emacs-lisp"
306 "application/x-pkcs7-signature"
307 "application/pkcs7-signature" "application/x-pkcs7-mime"
308 "application/pkcs7-mime"
309 ;; Mutt still uses this even though it has already been withdrawn.
310 "application/pgp")
311 "A list of MIME types to be displayed automatically."
312 :type '(repeat regexp)
313 :group 'mime-display)
315 (defcustom mm-attachment-override-types '("text/x-vcard"
316 "application/pkcs7-mime"
317 "application/x-pkcs7-mime"
318 "application/pkcs7-signature"
319 "application/x-pkcs7-signature")
320 "Types to have \"attachment\" ignored if they can be displayed inline."
321 :type '(repeat regexp)
322 :group 'mime-display)
324 (defcustom mm-inline-override-types nil
325 "Types to be treated as attachments even if they can be displayed inline."
326 :type '(repeat regexp)
327 :group 'mime-display)
329 (defcustom mm-automatic-external-display nil
330 "List of MIME type regexps that will be displayed externally automatically."
331 :type '(repeat regexp)
332 :group 'mime-display)
334 (defcustom mm-discouraged-alternatives nil
335 "List of MIME types that are discouraged when viewing multipart/alternative.
336 Viewing agents are supposed to view the last possible part of a message,
337 as that is supposed to be the richest. However, users may prefer other
338 types instead, and this list says what types are most unwanted. If,
339 for instance, text/html parts are very unwanted, and text/richtext are
340 somewhat unwanted, then the value of this variable should be set
343 (\"text/html\" \"text/richtext\")
345 Adding \"image/.*\" might also be useful. Spammers use it as the
346 prefered part of multipart/alternative messages. See also
347 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
348 enables you to choose manually one of two types those mails include."
349 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
350 :group 'mime-display)
352 (defcustom mm-tmp-directory
353 (if (fboundp 'temp-directory)
354 (temp-directory)
355 (if (boundp 'temporary-file-directory)
356 temporary-file-directory
357 "/tmp/"))
358 "Where mm will store its temporary files."
359 :type 'directory
360 :group 'mime-display)
362 (defcustom mm-inline-large-images nil
363 "If non-nil, then all images fit in the buffer."
364 :type 'boolean
365 :group 'mime-display)
367 (defvar mm-file-name-rewrite-functions
368 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
369 "*List of functions used for rewriting file names of MIME parts.
370 Each function takes a file name as input and returns a file name.
372 Ready-made functions include
373 `mm-file-name-delete-control'
374 `mm-file-name-delete-gotchas'
375 `mm-file-name-delete-whitespace',
376 `mm-file-name-trim-whitespace',
377 `mm-file-name-collapse-whitespace',
378 `mm-file-name-replace-whitespace',
379 `capitalize', `downcase', `upcase', and
380 `upcase-initials'.")
382 (defvar mm-path-name-rewrite-functions nil
383 "*List of functions for rewriting the full file names of MIME parts.
384 This is used when viewing parts externally, and is meant for
385 transforming the absolute name so that non-compliant programs can find
386 the file where it's saved.
388 Each function takes a file name as input and returns a file name.")
390 (defvar mm-file-name-replace-whitespace nil
391 "String used for replacing whitespace characters; default is `\"_\"'.")
393 (defcustom mm-default-directory nil
394 "The default directory where mm will save files.
395 If not set, `default-directory' will be used."
396 :type '(choice directory (const :tag "Default" nil))
397 :group 'mime-display)
399 (defcustom mm-attachment-file-modes 384
400 "Set the mode bits of saved attachments to this integer."
401 :version "22.1"
402 :type 'integer
403 :group 'mime-display)
405 (defcustom mm-external-terminal-program "xterm"
406 "The program to start an external terminal."
407 :version "22.1"
408 :type 'string
409 :group 'mime-display)
411 ;;; Internal variables.
413 (defvar mm-last-shell-command "")
414 (defvar mm-content-id-alist nil)
415 (defvar mm-postponed-undisplay-list nil)
417 ;; According to RFC2046, in particular, in a digest, the default
418 ;; Content-Type value for a body part is changed from "text/plain" to
419 ;; "message/rfc822".
420 (defvar mm-dissect-default-type "text/plain")
422 (autoload 'mml2015-verify "mml2015")
423 (autoload 'mml2015-verify-test "mml2015")
424 (autoload 'mml-smime-verify "mml-smime")
425 (autoload 'mml-smime-verify-test "mml-smime")
427 (defvar mm-verify-function-alist
428 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
429 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
430 mm-uu-pgp-signed-test)
431 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
432 mml-smime-verify-test)
433 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
434 mml-smime-verify-test)))
436 (defcustom mm-verify-option 'never
437 "Option of verifying signed parts.
438 `never', not verify; `always', always verify;
439 `known', only verify known protocols. Otherwise, ask user."
440 :version "22.1"
441 :type '(choice (item always)
442 (item never)
443 (item :tag "only known protocols" known)
444 (item :tag "ask" nil))
445 :group 'mime-security)
447 (autoload 'mml2015-decrypt "mml2015")
448 (autoload 'mml2015-decrypt-test "mml2015")
450 (defvar mm-decrypt-function-alist
451 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
452 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
453 mm-uu-pgp-encrypted-test)))
455 (defcustom mm-decrypt-option nil
456 "Option of decrypting encrypted parts.
457 `never', not decrypt; `always', always decrypt;
458 `known', only decrypt known protocols. Otherwise, ask user."
459 :version "22.1"
460 :type '(choice (item always)
461 (item never)
462 (item :tag "only known protocols" known)
463 (item :tag "ask" nil))
464 :group 'mime-security)
466 (defvar mm-viewer-completion-map
467 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
468 (set-keymap-parent map minibuffer-local-completion-map)
469 ;; Should we bind other key to minibuffer-complete-word?
470 (define-key map " " 'self-insert-command)
471 map)
472 "Keymap for input viewer with completion.")
474 (defvar mm-viewer-completion-map
475 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
476 (set-keymap-parent map minibuffer-local-completion-map)
477 ;; Should we bind other key to minibuffer-complete-word?
478 (define-key map " " 'self-insert-command)
479 map)
480 "Keymap for input viewer with completion.")
482 ;;; The functions.
484 (defun mm-alist-to-plist (alist)
485 "Convert association list ALIST into the equivalent property-list form.
486 The plist is returned. This converts from
488 \((a . 1) (b . 2) (c . 3))
490 into
492 \(a 1 b 2 c 3)
494 The original alist is not modified. See also `destructive-alist-to-plist'."
495 (let (plist)
496 (while alist
497 (let ((el (car alist)))
498 (setq plist (cons (cdr el) (cons (car el) plist))))
499 (setq alist (cdr alist)))
500 (nreverse plist)))
502 (defun mm-keep-viewer-alive-p (handle)
503 "Say whether external viewer for HANDLE should stay alive."
504 (let ((types mm-keep-viewer-alive-types)
505 (type (mm-handle-media-type handle))
507 (catch 'found
508 (while (setq ty (pop types))
509 (when (string-match ty type)
510 (throw 'found t))))))
512 (defun mm-handle-set-external-undisplayer (handle function)
513 "Set the undisplayer for HANDLE to FUNCTION.
514 Postpone undisplaying of viewers for types in
515 `mm-keep-viewer-alive-types'."
516 (if (mm-keep-viewer-alive-p handle)
517 (let ((new-handle (copy-sequence handle)))
518 (mm-handle-set-undisplayer new-handle function)
519 (mm-handle-set-undisplayer handle nil)
520 (push new-handle mm-postponed-undisplay-list))
521 (mm-handle-set-undisplayer handle function)))
523 (defun mm-destroy-postponed-undisplay-list ()
524 (when mm-postponed-undisplay-list
525 (message "Destroying external MIME viewers")
526 (mm-destroy-parts mm-postponed-undisplay-list)))
528 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
529 "Dissect the current buffer and return a list of MIME handles."
530 (save-excursion
531 (let (ct ctl type subtype cte cd description id result)
532 (save-restriction
533 (mail-narrow-to-head)
534 (when (or no-strict-mime
535 loose-mime
536 (mail-fetch-field "mime-version"))
537 (setq ct (mail-fetch-field "content-type")
538 ctl (and ct (mail-header-parse-content-type ct))
539 cte (mail-fetch-field "content-transfer-encoding")
540 cd (mail-fetch-field "content-disposition")
541 description (mail-fetch-field "content-description")
542 id (mail-fetch-field "content-id"))
543 (unless from
544 (setq from (mail-fetch-field "from")))
545 ;; FIXME: In some circumstances, this code is running within
546 ;; an unibyte macro. mail-extract-address-components
547 ;; creates unibyte buffers. This `if', though not a perfect
548 ;; solution, avoids most of them.
549 (if from
550 (setq from (cadr (mail-extract-address-components from))))))
551 (when cte
552 (setq cte (mail-header-strip cte)))
553 (if (or (not ctl)
554 (not (string-match "/" (car ctl))))
555 (mm-dissect-singlepart
556 (list mm-dissect-default-type)
557 (and cte (intern (downcase (mail-header-remove-whitespace
558 (mail-header-remove-comments
559 cte)))))
560 no-strict-mime
561 (and cd (mail-header-parse-content-disposition cd))
562 description)
563 (setq type (split-string (car ctl) "/"))
564 (setq subtype (cadr type)
565 type (pop type))
566 (setq
567 result
568 (cond
569 ((equal type "multipart")
570 (let ((mm-dissect-default-type (if (equal subtype "digest")
571 "message/rfc822"
572 "text/plain"))
573 (start (cdr (assq 'start (cdr ctl)))))
574 (add-text-properties 0 (length (car ctl))
575 (mm-alist-to-plist (cdr ctl)) (car ctl))
577 ;; what really needs to be done here is a way to link a
578 ;; MIME handle back to it's parent MIME handle (in a multilevel
579 ;; MIME article). That would probably require changing
580 ;; the mm-handle API so we simply store the multipart buffer
581 ;; name as a text property of the "multipart/whatever" string.
582 (add-text-properties 0 (length (car ctl))
583 (list 'buffer (mm-copy-to-buffer)
584 'from from
585 'start start)
586 (car ctl))
587 (cons (car ctl) (mm-dissect-multipart ctl from))))
589 (mm-possibly-verify-or-decrypt
590 (mm-dissect-singlepart
592 (and cte (intern (downcase (mail-header-remove-whitespace
593 (mail-header-remove-comments
594 cte)))))
595 no-strict-mime
596 (and cd (mail-header-parse-content-disposition cd))
597 description id)
598 ctl))))
599 (when id
600 (when (string-match " *<\\(.*\\)> *" id)
601 (setq id (match-string 1 id)))
602 (push (cons id result) mm-content-id-alist))
603 result))))
605 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
606 (when (or force
607 (if (equal "text/plain" (car ctl))
608 (assoc 'format ctl)
610 (mm-make-handle
611 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
613 (defun mm-dissect-multipart (ctl from)
614 (goto-char (point-min))
615 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
616 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
617 start parts
618 (end (save-excursion
619 (goto-char (point-max))
620 (if (re-search-backward close-delimiter nil t)
621 (match-beginning 0)
622 (point-max)))))
623 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
624 (while (and (< (point) end) (re-search-forward boundary end t))
625 (goto-char (match-beginning 0))
626 (when start
627 (save-excursion
628 (save-restriction
629 (narrow-to-region start (point))
630 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
631 (end-of-line 2)
632 (or (looking-at boundary)
633 (forward-line 1))
634 (setq start (point)))
635 (when (and start (< start end))
636 (save-excursion
637 (save-restriction
638 (narrow-to-region start end)
639 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
640 (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
642 (defun mm-copy-to-buffer ()
643 "Copy the contents of the current buffer to a fresh buffer."
644 (save-excursion
645 (let ((obuf (current-buffer))
646 beg)
647 (goto-char (point-min))
648 (search-forward-regexp "^\n" nil t)
649 (setq beg (point))
650 (set-buffer
651 ;; Preserve the data's unibyteness (for url-insert-file-contents).
652 (let ((default-enable-multibyte-characters (mm-multibyte-p)))
653 (generate-new-buffer " *mm*")))
654 (insert-buffer-substring obuf beg)
655 (current-buffer))))
657 (defun mm-display-parts (handle &optional no-default)
658 (if (stringp (car handle))
659 (mapcar 'mm-display-parts (cdr handle))
660 (if (bufferp (car handle))
661 (save-restriction
662 (narrow-to-region (point) (point))
663 (mm-display-part handle)
664 (goto-char (point-max)))
665 (mapcar 'mm-display-parts handle))))
667 (defun mm-display-part (handle &optional no-default)
668 "Display the MIME part represented by HANDLE.
669 Returns nil if the part is removed; inline if displayed inline;
670 external if displayed external."
671 (save-excursion
672 (mailcap-parse-mailcaps)
673 (if (mm-handle-displayed-p handle)
674 (mm-remove-part handle)
675 (let* ((type (mm-handle-media-type handle))
676 (method (mailcap-mime-info type))
677 (filename (or (mail-content-type-get
678 (mm-handle-disposition handle) 'filename)
679 (mail-content-type-get
680 (mm-handle-type handle) 'name)
681 "<file>"))
682 (external mm-enable-external))
683 (if (and (mm-inlinable-p handle)
684 (mm-inlined-p handle))
685 (progn
686 (forward-line 1)
687 (mm-display-inline handle)
688 'inline)
689 (when (or method
690 (not no-default))
691 (if (and (not method)
692 (equal "text" (car (split-string type))))
693 (progn
694 (forward-line 1)
695 (mm-insert-inline handle (mm-get-part handle))
696 'inline)
697 (if (and method ;; If nil, we always use "save".
698 (stringp method) ;; 'mailcap-save-binary-file
699 (or (eq mm-enable-external t)
700 (and (eq mm-enable-external 'ask)
701 (y-or-n-p
702 (concat
703 "Display part (" type
704 ") using external program"
705 ;; Can non-string method ever happen?
706 (if (stringp method)
707 (concat
708 " \"" (format method filename) "\"")
710 "? ")))))
711 (setq external t)
712 (setq external nil))
713 (if external
714 (mm-display-external
715 handle (or method 'mailcap-save-binary-file))
716 (mm-display-external
717 handle 'mailcap-save-binary-file)))))))))
719 (defun mm-display-external (handle method)
720 "Display HANDLE using METHOD."
721 (let ((outbuf (current-buffer)))
722 (mm-with-unibyte-buffer
723 (if (functionp method)
724 (let ((cur (current-buffer)))
725 (if (eq method 'mailcap-save-binary-file)
726 (progn
727 (set-buffer (generate-new-buffer " *mm*"))
728 (setq method nil))
729 (mm-insert-part handle)
730 (let ((win (get-buffer-window cur t)))
731 (when win
732 (select-window win)))
733 (switch-to-buffer (generate-new-buffer " *mm*")))
734 (buffer-disable-undo)
735 (mm-set-buffer-file-coding-system mm-binary-coding-system)
736 (insert-buffer-substring cur)
737 (goto-char (point-min))
738 (when method
739 (message "Viewing with %s" method))
740 (let ((mm (current-buffer))
741 (non-viewer (assq 'non-viewer
742 (mailcap-mime-info
743 (mm-handle-media-type handle) t))))
744 (unwind-protect
745 (if method
746 (funcall method)
747 (mm-save-part handle))
748 (when (and (not non-viewer)
749 method)
750 (mm-handle-set-undisplayer handle mm)))))
751 ;; The function is a string to be executed.
752 (mm-insert-part handle)
753 (let* ((dir (mm-make-temp-file
754 (expand-file-name "emm." mm-tmp-directory) 'dir))
755 (filename (or
756 (mail-content-type-get
757 (mm-handle-disposition handle) 'filename)
758 (mail-content-type-get
759 (mm-handle-type handle) 'name)))
760 (mime-info (mailcap-mime-info
761 (mm-handle-media-type handle) t))
762 (needsterm (or (assoc "needsterm" mime-info)
763 (assoc "needsterminal" mime-info)))
764 (copiousoutput (assoc "copiousoutput" mime-info))
765 file buffer)
766 ;; We create a private sub-directory where we store our files.
767 (set-file-modes dir 448)
768 (if filename
769 (setq file (expand-file-name
770 (gnus-map-function mm-file-name-rewrite-functions
771 (file-name-nondirectory filename))
772 dir))
773 ;; Use nametemplate (defined in RFC1524) if it is specified
774 ;; in mailcap.
775 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
776 (if (and suffix
777 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
778 (setq suffix (match-string 1 suffix))
779 ;; Otherwise, use a suffix according to
780 ;; `mailcap-mime-extensions'.
781 (setq suffix (car (rassoc (mm-handle-media-type handle)
782 mailcap-mime-extensions))))
783 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
784 nil suffix))))
785 (let ((coding-system-for-write mm-binary-coding-system))
786 (write-region (point-min) (point-max) file nil 'nomesg))
787 (message "Viewing with %s" method)
788 (cond
789 (needsterm
790 (let ((command (mm-mailcap-command
791 method file (mm-handle-type handle))))
792 (unwind-protect
793 (if window-system
794 (start-process "*display*" nil
795 mm-external-terminal-program
796 "-e" shell-file-name
797 shell-command-switch command)
798 (require 'term)
799 (require 'gnus-win)
800 (set-buffer
801 (setq buffer
802 (make-term "display"
803 shell-file-name
805 shell-command-switch command)))
806 (term-mode)
807 (term-char-mode)
808 (set-process-sentinel
809 (get-buffer-process buffer)
810 `(lambda (process state)
811 (if (eq 'exit (process-status process))
812 (gnus-configure-windows
813 ',gnus-current-window-configuration))))
814 (gnus-configure-windows 'display-term))
815 (mm-handle-set-external-undisplayer handle (cons file buffer)))
816 (message "Displaying %s..." command))
817 'external)
818 (copiousoutput
819 (with-current-buffer outbuf
820 (forward-line 1)
821 (mm-insert-inline
822 handle
823 (unwind-protect
824 (progn
825 (call-process shell-file-name nil
826 (setq buffer
827 (generate-new-buffer " *mm*"))
829 shell-command-switch
830 (mm-mailcap-command
831 method file (mm-handle-type handle)))
832 (if (buffer-live-p buffer)
833 (with-current-buffer buffer
834 (buffer-string))))
835 (progn
836 (ignore-errors (delete-file file))
837 (ignore-errors (delete-directory
838 (file-name-directory file)))
839 (ignore-errors (kill-buffer buffer))))))
840 'inline)
842 ;; Deleting the temp file should be postponed for some wrappers,
843 ;; shell scripts, and so on, which might exit right after having
844 ;; started a viewer command as a background job.
845 (let ((command (mm-mailcap-command
846 method file (mm-handle-type handle))))
847 (unwind-protect
848 (progn
849 (start-process "*display*"
850 (setq buffer
851 (generate-new-buffer " *mm*"))
852 shell-file-name
853 shell-command-switch command)
854 (set-process-sentinel
855 (get-buffer-process buffer)
856 (lexical-let ;; Don't use `let'.
857 ;; Function used to remove temp file and directory.
858 ((fn `(lambda nil
859 ;; Don't use `ignore-errors'.
860 (condition-case nil
861 (delete-file ,file)
862 (error))
863 (condition-case nil
864 (delete-directory
865 ,(file-name-directory file))
866 (error))))
867 ;; Form uses to kill the process buffer and
868 ;; remove the undisplayer.
869 (fm `(progn
870 (kill-buffer ,buffer)
871 ,(macroexpand
872 (list 'mm-handle-set-undisplayer
873 (list 'quote handle)
874 nil))))
875 ;; Message to be issued when the process exits.
876 (done (format "Displaying %s...done" command))
877 ;; In particular, the timer object (which is
878 ;; a vector in Emacs but is a list in XEmacs)
879 ;; requires that it is lexically scoped.
880 (timer (run-at-time 2.0 nil 'ignore)))
881 (lambda (process state)
882 (when (eq 'exit (process-status process))
883 (if (memq timer timer-list)
884 (timer-set-function timer fn)
885 (funcall fn))
886 (ignore-errors (eval fm))
887 (message "%s" done))))))
888 (mm-handle-set-external-undisplayer
889 handle (cons file buffer)))
890 (message "Displaying %s..." command))
891 'external)))))))
893 (defun mm-mailcap-command (method file type-list)
894 (let ((ctl (cdr type-list))
895 (beg 0)
896 (uses-stdin t)
897 out sub total)
898 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
899 method beg)
900 (push (substring method beg (match-beginning 0)) out)
901 (setq beg (match-end 0)
902 total (match-string 0 method)
903 sub (match-string 1 method))
904 (cond
905 ((string= total "%%")
906 (push "%" out))
907 ((or (string= total "%s")
908 ;; We do our own quoting.
909 (string= total "'%s'")
910 (string= total "\"%s\""))
911 (setq uses-stdin nil)
912 (push (mm-quote-arg
913 (gnus-map-function mm-path-name-rewrite-functions file)) out))
914 ((string= total "%t")
915 (push (mm-quote-arg (car type-list)) out))
917 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
918 (push (substring method beg (length method)) out)
919 (when uses-stdin
920 (push "<" out)
921 (push (mm-quote-arg
922 (gnus-map-function mm-path-name-rewrite-functions file))
923 out))
924 (mapconcat 'identity (nreverse out) "")))
926 (defun mm-remove-parts (handles)
927 "Remove the displayed MIME parts represented by HANDLES."
928 (if (and (listp handles)
929 (bufferp (car handles)))
930 (mm-remove-part handles)
931 (let (handle)
932 (while (setq handle (pop handles))
933 (cond
934 ((stringp handle)
935 (when (buffer-live-p (get-text-property 0 'buffer handle))
936 (kill-buffer (get-text-property 0 'buffer handle))))
937 ((and (listp handle)
938 (stringp (car handle)))
939 (mm-remove-parts (cdr handle)))
941 (mm-remove-part handle)))))))
943 (defun mm-destroy-parts (handles)
944 "Remove the displayed MIME parts represented by HANDLES."
945 (if (and (listp handles)
946 (bufferp (car handles)))
947 (mm-destroy-part handles)
948 (let (handle)
949 (while (setq handle (pop handles))
950 (cond
951 ((stringp handle)
952 (when (buffer-live-p (get-text-property 0 'buffer handle))
953 (kill-buffer (get-text-property 0 'buffer handle))))
954 ((and (listp handle)
955 (stringp (car handle)))
956 (mm-destroy-parts handle))
958 (mm-destroy-part handle)))))))
960 (defun mm-remove-part (handle)
961 "Remove the displayed MIME part represented by HANDLE."
962 (when (listp handle)
963 (let ((object (mm-handle-undisplayer handle)))
964 (ignore-errors
965 (cond
966 ;; Internally displayed part.
967 ((mm-annotationp object)
968 (delete-annotation object))
969 ((or (functionp object)
970 (and (listp object)
971 (eq (car object) 'lambda)))
972 (funcall object))
973 ;; Externally displayed part.
974 ((consp object)
975 (condition-case ()
976 (while (get-buffer-process (cdr object))
977 (interrupt-process (get-buffer-process (cdr object)))
978 (message "Waiting for external displayer to die...")
979 (sit-for 1))
980 (quit)
981 (error))
982 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
983 (message "Waiting for external displayer to die...done")
984 (ignore-errors (delete-file (car object)))
985 (ignore-errors (delete-directory (file-name-directory
986 (car object)))))
987 ((bufferp object)
988 (when (buffer-live-p object)
989 (kill-buffer object)))))
990 (mm-handle-set-undisplayer handle nil))))
992 (defun mm-display-inline (handle)
993 (let* ((type (mm-handle-media-type handle))
994 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
995 (funcall function handle)
996 (goto-char (point-min))))
998 (defun mm-assoc-string-match (alist type)
999 (dolist (elem alist)
1000 (when (string-match (car elem) type)
1001 (return elem))))
1003 (defun mm-automatic-display-p (handle)
1004 "Say whether the user wants HANDLE to be displayed automatically."
1005 (let ((methods mm-automatic-display)
1006 (type (mm-handle-media-type handle))
1007 method result)
1008 (while (setq method (pop methods))
1009 (when (and (not (mm-inline-override-p handle))
1010 (string-match method type))
1011 (setq result t
1012 methods nil)))
1013 result))
1015 (defun mm-inlinable-p (handle)
1016 "Say whether HANDLE can be displayed inline."
1017 (let ((alist mm-inline-media-tests)
1018 (type (mm-handle-media-type handle))
1019 test)
1020 (while alist
1021 (when (string-match (caar alist) type)
1022 (setq test (caddar alist)
1023 alist nil)
1024 (setq test (funcall test handle)))
1025 (pop alist))
1026 test))
1028 (defun mm-inlined-p (handle)
1029 "Say whether the user wants HANDLE to be displayed inline."
1030 (let ((methods mm-inlined-types)
1031 (type (mm-handle-media-type handle))
1032 method result)
1033 (while (setq method (pop methods))
1034 (when (and (not (mm-inline-override-p handle))
1035 (string-match method type))
1036 (setq result t
1037 methods nil)))
1038 result))
1040 (defun mm-attachment-override-p (handle)
1041 "Say whether HANDLE should have attachment behavior overridden."
1042 (let ((types mm-attachment-override-types)
1043 (type (mm-handle-media-type handle))
1045 (catch 'found
1046 (while (setq ty (pop types))
1047 (when (and (string-match ty type)
1048 (mm-inlinable-p handle))
1049 (throw 'found t))))))
1051 (defun mm-inline-override-p (handle)
1052 "Say whether HANDLE should have inline behavior overridden."
1053 (let ((types mm-inline-override-types)
1054 (type (mm-handle-media-type handle))
1056 (catch 'found
1057 (while (setq ty (pop types))
1058 (when (string-match ty type)
1059 (throw 'found t))))))
1061 (defun mm-automatic-external-display-p (type)
1062 "Return the user-defined method for TYPE."
1063 (let ((methods mm-automatic-external-display)
1064 method result)
1065 (while (setq method (pop methods))
1066 (when (string-match method type)
1067 (setq result t
1068 methods nil)))
1069 result))
1071 (defun mm-destroy-part (handle)
1072 "Destroy the data structures connected to HANDLE."
1073 (when (listp handle)
1074 (mm-remove-part handle)
1075 (when (buffer-live-p (mm-handle-buffer handle))
1076 (kill-buffer (mm-handle-buffer handle)))))
1078 (defun mm-handle-displayed-p (handle)
1079 "Say whether HANDLE is displayed or not."
1080 (mm-handle-undisplayer handle))
1083 ;;; Functions for outputting parts
1086 (defmacro mm-with-part (handle &rest forms)
1087 "Run FORMS in the temp buffer containing the contents of HANDLE."
1088 `(let* ((handle ,handle)
1089 ;; The multibyteness of the temp buffer should be turned on
1090 ;; if inserting a multibyte string. Contrarily, the buffer's
1091 ;; multibyteness should be off if inserting a unibyte string,
1092 ;; especially if a string contains 8bit data.
1093 (default-enable-multibyte-characters
1094 (with-current-buffer (mm-handle-buffer handle)
1095 (mm-multibyte-p))))
1096 (with-temp-buffer
1097 (insert-buffer-substring (mm-handle-buffer handle))
1098 (mm-disable-multibyte)
1099 (mm-decode-content-transfer-encoding
1100 (mm-handle-encoding handle)
1101 (mm-handle-media-type handle))
1102 ,@forms)))
1103 (put 'mm-with-part 'lisp-indent-function 1)
1104 (put 'mm-with-part 'edebug-form-spec '(body))
1106 (defun mm-get-part (handle &optional no-cache)
1107 "Return the contents of HANDLE as a string.
1108 If NO-CACHE is non-nil, cached contents of a message/external-body part
1109 are ignored."
1110 (if (and (not no-cache)
1111 (equal (mm-handle-media-type handle) "message/external-body"))
1112 (progn
1113 (unless (mm-handle-cache handle)
1114 (mm-extern-cache-contents handle))
1115 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1116 (buffer-string)))
1117 (mm-with-part handle
1118 (buffer-string))))
1120 (defun mm-insert-part (handle &optional no-cache)
1121 "Insert the contents of HANDLE in the current buffer.
1122 If NO-CACHE is non-nil, cached contents of a message/external-body part
1123 are ignored."
1124 (save-excursion
1125 (insert
1126 (cond ((eq (mail-content-type-get (mm-handle-type handle) 'charset)
1127 'gnus-decoded)
1128 (with-current-buffer (mm-handle-buffer handle)
1129 (buffer-string)))
1130 ((mm-multibyte-p)
1131 (mm-string-as-multibyte (mm-get-part handle no-cache)))
1133 (mm-get-part handle no-cache))))))
1135 (defun mm-file-name-delete-whitespace (file-name)
1136 "Remove all whitespace characters from FILE-NAME."
1137 (while (string-match "\\s-+" file-name)
1138 (setq file-name (replace-match "" t t file-name)))
1139 file-name)
1141 (defun mm-file-name-trim-whitespace (file-name)
1142 "Remove leading and trailing whitespace characters from FILE-NAME."
1143 (when (string-match "\\`\\s-+" file-name)
1144 (setq file-name (substring file-name (match-end 0))))
1145 (when (string-match "\\s-+\\'" file-name)
1146 (setq file-name (substring file-name 0 (match-beginning 0))))
1147 file-name)
1149 (defun mm-file-name-collapse-whitespace (file-name)
1150 "Collapse multiple whitespace characters in FILE-NAME."
1151 (while (string-match "\\s-\\s-+" file-name)
1152 (setq file-name (replace-match " " t t file-name)))
1153 file-name)
1155 (defun mm-file-name-replace-whitespace (file-name)
1156 "Replace whitespace characters in FILE-NAME with underscores.
1157 Set the option `mm-file-name-replace-whitespace' to any other
1158 string if you do not like underscores."
1159 (let ((s (or mm-file-name-replace-whitespace "_")))
1160 (while (string-match "\\s-" file-name)
1161 (setq file-name (replace-match s t t file-name))))
1162 file-name)
1164 (defun mm-file-name-delete-control (filename)
1165 "Delete control characters from FILENAME."
1166 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1168 (defun mm-file-name-delete-gotchas (filename)
1169 "Delete shell gotchas from FILENAME."
1170 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1171 (gnus-replace-in-string filename "^[.-]+" ""))
1173 (defun mm-save-part (handle)
1174 "Write HANDLE to a file."
1175 (let ((filename (or (mail-content-type-get
1176 (mm-handle-disposition handle) 'filename)
1177 (mail-content-type-get
1178 (mm-handle-type handle) 'name)))
1179 file)
1180 (when filename
1181 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1182 (file-name-nondirectory filename))))
1183 (setq file
1184 (mm-with-multibyte
1185 (read-file-name "Save MIME part to: "
1186 (or mm-default-directory default-directory)
1187 nil nil (or filename ""))))
1188 (setq mm-default-directory (file-name-directory file))
1189 (and (or (not (file-exists-p file))
1190 (yes-or-no-p (format "File %s already exists; overwrite? "
1191 file)))
1192 (progn
1193 (mm-save-part-to-file handle file)
1194 file))))
1196 (defun mm-save-part-to-file (handle file)
1197 (mm-with-unibyte-buffer
1198 (mm-insert-part handle)
1199 (let ((coding-system-for-write 'binary)
1200 (current-file-modes (default-file-modes))
1201 ;; Don't re-compress .gz & al. Arguably we should make
1202 ;; `file-name-handler-alist' nil, but that would chop
1203 ;; ange-ftp, which is reasonable to use here.
1204 (inhibit-file-name-operation 'write-region)
1205 (inhibit-file-name-handlers
1206 (cons 'jka-compr-handler inhibit-file-name-handlers)))
1207 (set-default-file-modes mm-attachment-file-modes)
1208 (unwind-protect
1209 (write-region (point-min) (point-max) file)
1210 (set-default-file-modes current-file-modes)))))
1212 (defun mm-pipe-part (handle)
1213 "Pipe HANDLE to a process."
1214 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
1215 (command
1216 (read-string "Shell command on MIME part: " mm-last-shell-command)))
1217 (mm-with-unibyte-buffer
1218 (mm-insert-part handle)
1219 (let ((coding-system-for-write 'binary))
1220 (shell-command-on-region (point-min) (point-max) command nil)))))
1222 (defun mm-interactively-view-part (handle)
1223 "Display HANDLE using METHOD."
1224 (let* ((type (mm-handle-media-type handle))
1225 (methods
1226 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
1227 (mailcap-mime-info type 'all)))
1228 (method (let ((minibuffer-local-completion-map
1229 mm-viewer-completion-map))
1230 (completing-read "Viewer: " methods))))
1231 (when (string= method "")
1232 (error "No method given"))
1233 (if (string-match "^[^% \t]+$" method)
1234 (setq method (concat method " %s")))
1235 (mm-display-external handle method)))
1237 (defun mm-preferred-alternative (handles &optional preferred)
1238 "Say which of HANDLES are preferred."
1239 (let ((prec (if preferred (list preferred)
1240 (mm-preferred-alternative-precedence handles)))
1241 p h result type handle)
1242 (while (setq p (pop prec))
1243 (setq h handles)
1244 (while h
1245 (setq handle (car h))
1246 (setq type (mm-handle-media-type handle))
1247 (when (and (equal p type)
1248 (mm-automatic-display-p handle)
1249 (or (stringp (car handle))
1250 (not (mm-handle-disposition handle))
1251 (equal (car (mm-handle-disposition handle))
1252 "inline")))
1253 (setq result handle
1254 h nil
1255 prec nil))
1256 (pop h)))
1257 result))
1259 (defun mm-preferred-alternative-precedence (handles)
1260 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1261 (let ((seq (nreverse (mapcar #'mm-handle-media-type
1262 handles))))
1263 (dolist (disc (reverse mm-discouraged-alternatives))
1264 (dolist (elem (copy-sequence seq))
1265 (when (string-match disc elem)
1266 (setq seq (nconc (delete elem seq) (list elem))))))
1267 seq))
1269 (defun mm-get-content-id (id)
1270 "Return the handle(s) referred to by ID."
1271 (cdr (assoc id mm-content-id-alist)))
1273 (defconst mm-image-type-regexps
1274 '(("/\\*.*XPM.\\*/" . xpm)
1275 ("P[1-6]" . pbm)
1276 ("GIF8" . gif)
1277 ("\377\330" . jpeg)
1278 ("\211PNG\r\n" . png)
1279 ("#define" . xbm)
1280 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1281 ("%!PS" . postscript))
1282 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1283 When the first bytes of an image file match REGEXP, it is assumed to
1284 be of image type IMAGE-TYPE.")
1286 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1287 (defun mm-image-type-from-buffer ()
1288 "Determine the image type from data in the current buffer.
1289 Value is a symbol specifying the image type or nil if type cannot
1290 be determined."
1291 (let ((types mm-image-type-regexps)
1292 type)
1293 (goto-char (point-min))
1294 (while (and types (null type))
1295 (let ((regexp (car (car types)))
1296 (image-type (cdr (car types))))
1297 (when (looking-at regexp)
1298 (setq type image-type))
1299 (setq types (cdr types))))
1300 type))
1302 (defun mm-get-image (handle)
1303 "Return an image instance based on HANDLE."
1304 (let ((type (mm-handle-media-subtype handle))
1305 spec)
1306 ;; Allow some common translations.
1307 (setq type
1308 (cond
1309 ((equal type "x-pixmap")
1310 "xpm")
1311 ((equal type "x-xbitmap")
1312 "xbm")
1313 ((equal type "x-portable-bitmap")
1314 "pbm")
1315 (t type)))
1316 (or (mm-handle-cache handle)
1317 (mm-with-unibyte-buffer
1318 (mm-insert-part handle)
1319 (prog1
1320 (setq spec
1321 (ignore-errors
1322 ;; Avoid testing `make-glyph' since W3 may define
1323 ;; a bogus version of it.
1324 (if (fboundp 'create-image)
1325 (create-image (buffer-string)
1326 (or (mm-image-type-from-buffer)
1327 (intern type))
1328 'data-p)
1329 (mm-create-image-xemacs type))))
1330 (mm-handle-set-cache handle spec))))))
1332 (defun mm-create-image-xemacs (type)
1333 (cond
1334 ((equal type "xbm")
1335 ;; xbm images require special handling, since
1336 ;; the only way to create glyphs from these
1337 ;; (without a ton of work) is to write them
1338 ;; out to a file, and then create a file
1339 ;; specifier.
1340 (let ((file (mm-make-temp-file
1341 (expand-file-name "emm" mm-tmp-directory)
1342 nil ".xbm")))
1343 (unwind-protect
1344 (progn
1345 (write-region (point-min) (point-max) file)
1346 (make-glyph (list (cons 'x file))))
1347 (ignore-errors
1348 (delete-file file)))))
1350 (make-glyph
1351 (vector
1352 (or (mm-image-type-from-buffer)
1353 (intern type))
1354 :data (buffer-string))))))
1356 (defun mm-image-fit-p (handle)
1357 "Say whether the image in HANDLE will fit the current window."
1358 (let ((image (mm-get-image handle)))
1359 (if (fboundp 'glyph-width)
1360 ;; XEmacs' glyphs can actually tell us about their width, so
1361 ;; lets be nice and smart about them.
1362 (or mm-inline-large-images
1363 (and (< (glyph-width image) (window-pixel-width))
1364 (< (glyph-height image) (window-pixel-height))))
1365 (let* ((size (image-size image))
1366 (w (car size))
1367 (h (cdr size)))
1368 (or mm-inline-large-images
1369 (and (< h (1- (window-height))) ; Don't include mode line.
1370 (< w (window-width))))))))
1372 (defun mm-valid-image-format-p (format)
1373 "Say whether FORMAT can be displayed natively by Emacs."
1374 (cond
1375 ;; Handle XEmacs
1376 ((fboundp 'valid-image-instantiator-format-p)
1377 (valid-image-instantiator-format-p format))
1378 ;; Handle Emacs 21
1379 ((fboundp 'image-type-available-p)
1380 (and (display-graphic-p)
1381 (image-type-available-p format)))
1382 ;; Nobody else can do images yet.
1384 nil)))
1386 (defun mm-valid-and-fit-image-p (format handle)
1387 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1388 (and (mm-valid-image-format-p format)
1389 (mm-image-fit-p handle)))
1391 (defun mm-find-part-by-type (handles type &optional notp recursive)
1392 "Search in HANDLES for part with TYPE.
1393 If NOTP, returns first non-matching part.
1394 If RECURSIVE, search recursively."
1395 (let (handle)
1396 (while handles
1397 (if (and recursive (stringp (caar handles)))
1398 (if (setq handle (mm-find-part-by-type (cdar handles) type
1399 notp recursive))
1400 (setq handles nil))
1401 (if (if notp
1402 (not (equal (mm-handle-media-type (car handles)) type))
1403 (equal (mm-handle-media-type (car handles)) type))
1404 (setq handle (car handles)
1405 handles nil)))
1406 (setq handles (cdr handles)))
1407 handle))
1409 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1410 (goto-char (point-min))
1411 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1412 'boundary)))
1413 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1414 start
1415 (end (save-excursion
1416 (goto-char (point-max))
1417 (if (re-search-backward close-delimiter nil t)
1418 (match-beginning 0)
1419 (point-max))))
1420 result)
1421 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1422 (while (and (not result)
1423 (re-search-forward boundary end t))
1424 (goto-char (match-beginning 0))
1425 (when start
1426 (save-excursion
1427 (save-restriction
1428 (narrow-to-region start (1- (point)))
1429 (when (let* ((ct (mail-fetch-field "content-type"))
1430 (ctl (and ct (mail-header-parse-content-type ct))))
1431 (if notp
1432 (not (equal (car ctl) type))
1433 (equal (car ctl) type)))
1434 (setq result (buffer-string))))))
1435 (forward-line 1)
1436 (setq start (point)))
1437 (when (and (not result) start)
1438 (save-excursion
1439 (save-restriction
1440 (narrow-to-region start end)
1441 (when (let* ((ct (mail-fetch-field "content-type"))
1442 (ctl (and ct (mail-header-parse-content-type ct))))
1443 (if notp
1444 (not (equal (car ctl) type))
1445 (equal (car ctl) type)))
1446 (setq result (buffer-string))))))
1447 result))
1449 (defvar mm-security-handle nil)
1451 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1452 ;; HANDLE could be a CTL.
1453 (when handle
1454 (put-text-property 0 (length (car handle)) parameter value
1455 (car handle))))
1457 (defun mm-possibly-verify-or-decrypt (parts ctl)
1458 (let ((type (car ctl))
1459 (subtype (cadr (split-string (car ctl) "/")))
1460 (mm-security-handle ctl) ;; (car CTL) is the type.
1461 protocol func functest)
1462 (cond
1463 ((or (equal type "application/x-pkcs7-mime")
1464 (equal type "application/pkcs7-mime"))
1465 (with-temp-buffer
1466 (when (and (cond
1467 ((eq mm-decrypt-option 'never) nil)
1468 ((eq mm-decrypt-option 'always) t)
1469 ((eq mm-decrypt-option 'known) t)
1470 (t (y-or-n-p
1471 (format "Decrypt (S/MIME) part? "))))
1472 (mm-view-pkcs7 parts))
1473 (setq parts (mm-dissect-buffer t)))))
1474 ((equal subtype "signed")
1475 (unless (and (setq protocol
1476 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1477 (not (equal protocol "multipart/mixed")))
1478 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1479 (let ((protocols mm-verify-function-alist))
1480 (while protocols
1481 (if (and (or (not (setq functest (nth 3 (car protocols))))
1482 (funcall functest parts ctl))
1483 (mm-find-part-by-type parts (caar protocols) nil t))
1484 (setq protocol (caar protocols)
1485 protocols nil)
1486 (setq protocols (cdr protocols))))))
1487 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1488 (when (cond
1489 ((eq mm-verify-option 'never) nil)
1490 ((eq mm-verify-option 'always) t)
1491 ((eq mm-verify-option 'known)
1492 (and func
1493 (or (not (setq functest
1494 (nth 3 (assoc protocol
1495 mm-verify-function-alist))))
1496 (funcall functest parts ctl))))
1498 (y-or-n-p
1499 (format "Verify signed (%s) part? "
1500 (or (nth 2 (assoc protocol mm-verify-function-alist))
1501 (format "protocol=%s" protocol))))))
1502 (save-excursion
1503 (if func
1504 (funcall func parts ctl)
1505 (mm-set-handle-multipart-parameter
1506 mm-security-handle 'gnus-details
1507 (format "Unknown sign protocol (%s)" protocol))))))
1508 ((equal subtype "encrypted")
1509 (unless (setq protocol
1510 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1511 ;; The message is broken.
1512 (let ((parts parts))
1513 (while parts
1514 (if (assoc (mm-handle-media-type (car parts))
1515 mm-decrypt-function-alist)
1516 (setq protocol (mm-handle-media-type (car parts))
1517 parts nil)
1518 (setq parts (cdr parts))))))
1519 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1520 (when (cond
1521 ((eq mm-decrypt-option 'never) nil)
1522 ((eq mm-decrypt-option 'always) t)
1523 ((eq mm-decrypt-option 'known)
1524 (and func
1525 (or (not (setq functest
1526 (nth 3 (assoc protocol
1527 mm-decrypt-function-alist))))
1528 (funcall functest parts ctl))))
1530 (y-or-n-p
1531 (format "Decrypt (%s) part? "
1532 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1533 (format "protocol=%s" protocol))))))
1534 (save-excursion
1535 (if func
1536 (setq parts (funcall func parts ctl))
1537 (mm-set-handle-multipart-parameter
1538 mm-security-handle 'gnus-details
1539 (format "Unknown encrypt protocol (%s)" protocol))))))
1540 (t nil))
1541 parts))
1543 (defun mm-multiple-handles (handles)
1544 (and (listp handles)
1545 (> (length handles) 1)
1546 (or (listp (car handles))
1547 (stringp (car handles)))))
1549 (defun mm-complicated-handles (handles)
1550 (and (listp (car handles))
1551 (> (length handles) 1)))
1553 (defun mm-merge-handles (handles1 handles2)
1554 (append
1555 (if (listp (car handles1))
1556 handles1
1557 (list handles1))
1558 (if (listp (car handles2))
1559 handles2
1560 (list handles2))))
1562 (defun mm-readable-p (handle)
1563 "Say whether the content of HANDLE is readable."
1564 (and (< (with-current-buffer (mm-handle-buffer handle)
1565 (buffer-size)) 10000)
1566 (mm-with-unibyte-buffer
1567 (mm-insert-part handle)
1568 (and (eq (mm-body-7-or-8) '7bit)
1569 (not (mm-long-lines-p 76))))))
1571 (provide 'mm-decode)
1573 ;; arch-tag: 4f35d360-56b8-4030-9388-3ed82d359b9b
1574 ;;; mm-decode.el ends here