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