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