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