[Gnus] Silence the byte compiler
[bpt/emacs.git] / lisp / gnus / mm-decode.el
CommitLineData
23f87bed 1;;; mm-decode.el --- Functions for decoding MIME things
e84b4b86 2
ab422c4d 3;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7;; This file is part of GNU Emacs.
8
5e809f55 9;; GNU Emacs is free software: you can redistribute it and/or modify
c113de23 10;; it under the terms of the GNU General Public License as published by
5e809f55
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
c113de23
GM
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c113de23
GM
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
5e809f55 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23
GM
21
22;;; Commentary:
23
24;;; Code:
25
f0b7f5a8 26;; For Emacs <22.2 and XEmacs.
60ef4154
GM
27(eval-and-compile
28 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
29
c113de23 30(require 'mail-parse)
c113de23 31(require 'mm-bodies)
c7641e3c 32(eval-when-compile (require 'cl))
c113de23 33
aa8f8277
GM
34(autoload 'gnus-map-function "gnus-util")
35(autoload 'gnus-replace-in-string "gnus-util")
36(autoload 'gnus-read-shell-command "gnus-util")
37
12c9fab6
GM
38(autoload 'mm-inline-partial "mm-partial")
39(autoload 'mm-inline-external-body "mm-extern")
40(autoload 'mm-extern-cache-contents "mm-extern")
41(autoload 'mm-insert-inline "mm-view")
c113de23 42
89b163db
G
43(autoload 'mm-archive-decoders "mm-archive")
44(autoload 'mm-archive-dissect-and-inline "mm-archive")
45(autoload 'mm-dissect-archive "mm-archive")
46
b7e9d9c3
JB
47(defvar gnus-current-window-configuration)
48
23f87bed 49(add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
a025f7d6 50(add-hook 'gnus-exit-gnus-hook 'mm-temp-files-delete)
23f87bed 51
c113de23
GM
52(defgroup mime-display ()
53 "Display of MIME in mail and news articles."
23f87bed 54 :link '(custom-manual "(emacs-mime)Display Customization")
c113de23
GM
55 :version "21.1"
56 :group 'mail
57 :group 'news
58 :group 'multimedia)
59
23f87bed
MB
60(defgroup mime-security ()
61 "MIME security in mail and news articles."
62 :link '(custom-manual "(emacs-mime)Display Customization")
63 :group 'mail
64 :group 'news
65 :group 'multimedia)
66
89cccc2f
G
67(defface mm-command-output
68 '((((class color)
69 (background dark))
70 (:foreground "ForestGreen"))
71 (((class color)
72 (background light))
73 (:foreground "red3"))
74 (t
75 (:italic t)))
76 "Face used for displaying output from commands."
77 :group 'mime-display)
78
c113de23
GM
79;;; Convenience macros.
80
81(defmacro mm-handle-buffer (handle)
82 `(nth 0 ,handle))
83(defmacro mm-handle-type (handle)
84 `(nth 1 ,handle))
85(defsubst mm-handle-media-type (handle)
86 (if (stringp (car handle))
87 (car handle)
88 (car (mm-handle-type handle))))
89(defsubst mm-handle-media-supertype (handle)
90 (car (split-string (mm-handle-media-type handle) "/")))
91(defsubst mm-handle-media-subtype (handle)
92 (cadr (split-string (mm-handle-media-type handle) "/")))
93(defmacro mm-handle-encoding (handle)
94 `(nth 2 ,handle))
95(defmacro mm-handle-undisplayer (handle)
96 `(nth 3 ,handle))
97(defmacro mm-handle-set-undisplayer (handle function)
98 `(setcar (nthcdr 3 ,handle) ,function))
99(defmacro mm-handle-disposition (handle)
100 `(nth 4 ,handle))
101(defmacro mm-handle-description (handle)
102 `(nth 5 ,handle))
103(defmacro mm-handle-cache (handle)
104 `(nth 6 ,handle))
105(defmacro mm-handle-set-cache (handle contents)
106 `(setcar (nthcdr 6 ,handle) ,contents))
107(defmacro mm-handle-id (handle)
108 `(nth 7 ,handle))
23f87bed
MB
109(defmacro mm-handle-multipart-original-buffer (handle)
110 `(get-text-property 0 'buffer (car ,handle)))
111(defmacro mm-handle-multipart-from (handle)
112 `(get-text-property 0 'from (car ,handle)))
113(defmacro mm-handle-multipart-ctl-parameter (handle parameter)
114 `(get-text-property 0 ,parameter (car ,handle)))
115
c113de23
GM
116(defmacro mm-make-handle (&optional buffer type encoding undisplayer
117 disposition description cache
118 id)
119 `(list ,buffer ,type ,encoding ,undisplayer
120 ,disposition ,description ,cache ,id))
121
23f87bed 122(defcustom mm-text-html-renderer
2526f423
G
123 (cond ((fboundp 'libxml-parse-html-region) 'shr)
124 ((executable-find "w3m") 'gnus-w3m)
23f87bed
MB
125 ((executable-find "links") 'links)
126 ((executable-find "lynx") 'lynx)
8f7abae3
MB
127 ((locate-library "w3") 'w3)
128 ((locate-library "html2text") 'html2text)
129 (t nil))
23f87bed
MB
130 "Render of HTML contents.
131It is one of defined renderer types, or a rendering function.
132The defined renderer types are:
d30dd079
G
133`shr': use the built-in Gnus HTML renderer;
134`gnus-w3m': use Gnus renderer based on w3m;
135`w3m': use emacs-w3m;
136`w3m-standalone': use plain w3m;
23f87bed 137`links': use links;
d30dd079
G
138`lynx': use lynx;
139`w3': use Emacs/W3;
140`html2text': use html2text;
8f7abae3 141nil : use external viewer (default web browser)."
1a10d421 142 :version "24.1"
2526f423
G
143 :type '(choice (const shr)
144 (const gnus-w3m)
20a673b2
KY
145 (const w3)
146 (const w3m :tag "emacs-w3m")
8f7abae3 147 (const w3m-standalone :tag "standalone w3m" )
23f87bed
MB
148 (const links)
149 (const lynx)
150 (const html2text)
8f7abae3 151 (const nil :tag "External viewer")
23f87bed 152 (function))
23f87bed
MB
153 :group 'mime-display)
154
23f87bed
MB
155(defcustom mm-inline-text-html-with-images nil
156 "If non-nil, Gnus will allow retrieving images in HTML contents with
157the <img> tags. It has no effect on Emacs/w3. See also the
158documentation for the `mm-w3m-safe-url-regexp' variable."
bf247b6e 159 :version "22.1"
23f87bed
MB
160 :type 'boolean
161 :group 'mime-display)
162
163(defcustom mm-w3m-safe-url-regexp "\\`cid:"
164 "Regexp matching URLs which are considered to be safe.
165Some HTML mails might contain a nasty trick used by spammers, using
166the <img> tag which is far more evil than the [Click Here!] button.
167It is most likely intended to check whether the ominous spam mail has
168reached your eyes or not, in which case the spammer knows for sure
169that your email address is valid. It is done by embedding an
170identifier string into a URL that you might automatically retrieve
171when displaying the image. The default value is \"\\\\`cid:\" which only
172matches parts embedded to the Multipart/Related type MIME contents and
173Gnus will never connect to the spammer's site arbitrarily. You may
174set this variable to nil if you consider all urls to be safe."
bf247b6e 175 :version "22.1"
23f87bed
MB
176 :type '(choice (regexp :tag "Regexp")
177 (const :tag "All URLs are safe" nil))
178 :group 'mime-display)
179
180(defcustom mm-inline-text-html-with-w3m-keymap t
181 "If non-nil, use emacs-w3m command keys in the article buffer."
bf247b6e 182 :version "22.1"
23f87bed
MB
183 :type 'boolean
184 :group 'mime-display)
185
186(defcustom mm-enable-external t
187 "Indicate whether external MIME handlers should be used.
188
189If t, all defined external MIME handlers are used. If nil, files are saved by
190`mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
191before the external MIME handler is invoked."
bf247b6e 192 :version "22.1"
23f87bed
MB
193 :type '(choice (const :tag "Always" t)
194 (const :tag "Never" nil)
195 (const :tag "Ask" ask))
196 :group 'mime-display)
197
c113de23 198(defcustom mm-inline-media-tests
23f87bed 199 '(("image/p?jpeg"
c113de23
GM
200 mm-inline-image
201 (lambda (handle)
202 (mm-valid-and-fit-image-p 'jpeg handle)))
203 ("image/png"
204 mm-inline-image
205 (lambda (handle)
206 (mm-valid-and-fit-image-p 'png handle)))
207 ("image/gif"
208 mm-inline-image
209 (lambda (handle)
210 (mm-valid-and-fit-image-p 'gif handle)))
211 ("image/tiff"
212 mm-inline-image
213 (lambda (handle)
d1fbe1c3 214 (mm-valid-and-fit-image-p 'tiff handle)))
c113de23
GM
215 ("image/xbm"
216 mm-inline-image
217 (lambda (handle)
218 (mm-valid-and-fit-image-p 'xbm handle)))
219 ("image/x-xbitmap"
220 mm-inline-image
221 (lambda (handle)
222 (mm-valid-and-fit-image-p 'xbm handle)))
223 ("image/xpm"
224 mm-inline-image
225 (lambda (handle)
226 (mm-valid-and-fit-image-p 'xpm handle)))
23f87bed 227 ("image/x-xpixmap"
c113de23
GM
228 mm-inline-image
229 (lambda (handle)
230 (mm-valid-and-fit-image-p 'xpm handle)))
231 ("image/bmp"
232 mm-inline-image
233 (lambda (handle)
234 (mm-valid-and-fit-image-p 'bmp handle)))
5bb548d2
DL
235 ("image/x-portable-bitmap"
236 mm-inline-image
237 (lambda (handle)
238 (mm-valid-and-fit-image-p 'pbm handle)))
c113de23
GM
239 ("text/plain" mm-inline-text identity)
240 ("text/enriched" mm-inline-text identity)
241 ("text/richtext" mm-inline-text identity)
39cde66c 242 ("text/x-patch" mm-display-patch-inline identity)
7ab0253d 243 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
39cde66c 244 ("text/x-diff" mm-display-patch-inline identity)
ce9401f3 245 ("application/emacs-lisp" mm-display-elisp-inline identity)
23f87bed 246 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
6e3165fb
JD
247 ("application/x-shellscript" mm-display-shell-script-inline identity)
248 ("application/x-sh" mm-display-shell-script-inline identity)
249 ("text/x-sh" mm-display-shell-script-inline identity)
008cad90 250 ("application/javascript" mm-display-javascript-inline identity)
01c52d31 251 ("text/dns" mm-display-dns-inline identity)
3f680407 252 ("text/x-org" mm-display-org-inline identity)
c113de23 253 ("text/html"
23f87bed 254 mm-inline-text-html
c113de23 255 (lambda (handle)
2526f423 256 mm-text-html-renderer))
c113de23 257 ("text/x-vcard"
23f87bed 258 mm-inline-text-vcard
c113de23
GM
259 (lambda (handle)
260 (or (featurep 'vcard)
261 (locate-library "vcard"))))
262 ("message/delivery-status" mm-inline-text identity)
263 ("message/rfc822" mm-inline-message identity)
264 ("message/partial" mm-inline-partial identity)
23f87bed 265 ("message/external-body" mm-inline-external-body identity)
c113de23 266 ("text/.*" mm-inline-text identity)
89b163db
G
267 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity)
268 ("application/zip" mm-archive-dissect-and-inline identity)
c113de23
GM
269 ("audio/wav" mm-inline-audio
270 (lambda (handle)
271 (and (or (featurep 'nas-sound) (featurep 'native-sound))
272 (device-sound-enabled-p))))
273 ("audio/au"
274 mm-inline-audio
275 (lambda (handle)
276 (and (or (featurep 'nas-sound) (featurep 'native-sound))
277 (device-sound-enabled-p))))
278 ("application/pgp-signature" ignore identity)
23f87bed
MB
279 ("application/x-pkcs7-signature" ignore identity)
280 ("application/pkcs7-signature" ignore identity)
281 ("application/x-pkcs7-mime" ignore identity)
282 ("application/pkcs7-mime" ignore identity)
c113de23
GM
283 ("multipart/alternative" ignore identity)
284 ("multipart/mixed" ignore identity)
23f87bed 285 ("multipart/related" ignore identity)
a3ddc4d9 286 ("image/.*"
d1fbe1c3
LMI
287 mm-inline-image
288 (lambda (handle)
289 (and (mm-valid-image-format-p 'imagemagick)
290 (mm-with-unibyte-buffer
291 (mm-insert-part handle)
292 (let ((image
293 (ignore-errors
294 (if (fboundp 'create-image)
295 (create-image (buffer-string) 'imagemagick 'data-p)
89b163db
G
296 (mm-create-image-xemacs
297 (mm-handle-media-subtype handle))))))
d1fbe1c3
LMI
298 (when image
299 (setcar (cdr handle) (list "image/imagemagick"))
300 (mm-image-fit-p handle)))))))
23f87bed
MB
301 ;; Disable audio and image
302 ("audio/.*" ignore ignore)
303 ("image/.*" ignore ignore)
304 ;; Default to displaying as text
305 (".*" mm-inline-text mm-readable-p))
c113de23 306 "Alist of media types/tests saying whether types can be displayed inline."
23f87bed 307 :type '(repeat (list (regexp :tag "MIME type")
c113de23
GM
308 (function :tag "Display function")
309 (function :tag "Display test")))
310 :group 'mime-display)
311
312(defcustom mm-inlined-types
313 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
23f87bed
MB
314 "message/partial" "message/external-body" "application/emacs-lisp"
315 "application/x-emacs-lisp"
316 "application/pgp-signature" "application/x-pkcs7-signature"
317 "application/pkcs7-signature" "application/x-pkcs7-mime"
e499bc94 318 "application/pkcs7-mime"
89b163db
G
319 "application/x-gtar-compressed"
320 "application/x-tar"
321 "application/zip"
e499bc94
MB
322 ;; Mutt still uses this even though it has already been withdrawn.
323 "application/pgp")
23f87bed
MB
324 "List of media types that are to be displayed inline.
325See also `mm-inline-media-tests', which says how to display a media
326type inline."
3031d8b0 327 :type '(repeat regexp)
23f87bed
MB
328 :group 'mime-display)
329
330(defcustom mm-keep-viewer-alive-types
331 '("application/postscript" "application/msword" "application/vnd.ms-excel"
332 "application/pdf" "application/x-dvi")
333 "List of media types for which the external viewer will not be killed
334when selecting a different article."
bf247b6e 335 :version "22.1"
3031d8b0 336 :type '(repeat regexp)
c113de23 337 :group 'mime-display)
a1506d29 338
c113de23 339(defcustom mm-automatic-display
01c52d31 340 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
c113de23 341 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
01c52d31 342 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
23f87bed
MB
343 "application/emacs-lisp" "application/x-emacs-lisp"
344 "application/x-pkcs7-signature"
345 "application/pkcs7-signature" "application/x-pkcs7-mime"
e499bc94
MB
346 "application/pkcs7-mime"
347 ;; Mutt still uses this even though it has already been withdrawn.
6e3165fb 348 "application/pgp\\'"
3f680407 349 "text/x-org")
c113de23 350 "A list of MIME types to be displayed automatically."
3031d8b0 351 :type '(repeat regexp)
c113de23
GM
352 :group 'mime-display)
353
23f87bed
MB
354(defcustom mm-attachment-override-types '("text/x-vcard"
355 "application/pkcs7-mime"
356 "application/x-pkcs7-mime"
357 "application/pkcs7-signature"
358 "application/x-pkcs7-signature")
c113de23 359 "Types to have \"attachment\" ignored if they can be displayed inline."
3031d8b0 360 :type '(repeat regexp)
c113de23
GM
361 :group 'mime-display)
362
363(defcustom mm-inline-override-types nil
364 "Types to be treated as attachments even if they can be displayed inline."
3031d8b0 365 :type '(repeat regexp)
c113de23
GM
366 :group 'mime-display)
367
368(defcustom mm-automatic-external-display nil
369 "List of MIME type regexps that will be displayed externally automatically."
3031d8b0 370 :type '(repeat regexp)
c113de23
GM
371 :group 'mime-display)
372
373(defcustom mm-discouraged-alternatives nil
374 "List of MIME types that are discouraged when viewing multipart/alternative.
375Viewing agents are supposed to view the last possible part of a message,
376as that is supposed to be the richest. However, users may prefer other
377types instead, and this list says what types are most unwanted. If,
378for instance, text/html parts are very unwanted, and text/richtext are
379somewhat unwanted, then the value of this variable should be set
380to:
381
58090a8d
MB
382 (\"text/html\" \"text/richtext\")
383
384Adding \"image/.*\" might also be useful. Spammers use it as the
e4920bc9 385preferred part of multipart/alternative messages. See also
3031d8b0
MB
386`gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
387enables you to choose manually one of two types those mails include."
58090a8d 388 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
c113de23
GM
389 :group 'mime-display)
390
23f87bed
MB
391(defcustom mm-tmp-directory
392 (if (fboundp 'temp-directory)
393 (temp-directory)
394 (if (boundp 'temporary-file-directory)
395 temporary-file-directory
396 "/tmp/"))
397 "Where mm will store its temporary files."
398 :type 'directory
399 :group 'mime-display)
c113de23
GM
400
401(defcustom mm-inline-large-images nil
a41c2e6d
G
402 "If t, then all images fit in the buffer.
403If 'resize, try to resize the images so they fit."
404 :type '(radio
405 (const :tag "Inline large images as they are." t)
406 (const :tag "Resize large images." resize)
407 (const :tag "Do not inline large images." nil))
c113de23
GM
408 :group 'mime-display)
409
01c52d31 410(defcustom mm-file-name-rewrite-functions
23f87bed 411 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
01c52d31 412 "List of functions used for rewriting file names of MIME parts.
23f87bed
MB
413Each function takes a file name as input and returns a file name.
414
01c52d31
MB
415Ready-made functions include `mm-file-name-delete-control',
416`mm-file-name-delete-gotchas' (you should not remove these two
417functions), `mm-file-name-delete-whitespace',
418`mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
419`mm-file-name-replace-whitespace', `capitalize', `downcase',
420`upcase', and `upcase-initials'."
421 :type '(list (set :inline t
422 (const mm-file-name-delete-control)
423 (const mm-file-name-delete-gotchas)
424 (const mm-file-name-delete-whitespace)
425 (const mm-file-name-trim-whitespace)
426 (const mm-file-name-collapse-whitespace)
427 (const mm-file-name-replace-whitespace)
428 (const capitalize)
429 (const downcase)
430 (const upcase)
431 (const upcase-initials)
432 (repeat :inline t
433 :tag "Function"
434 function)))
330f707b 435 :version "23.1" ;; No Gnus
01c52d31
MB
436 :group 'mime-display)
437
23f87bed
MB
438
439(defvar mm-path-name-rewrite-functions nil
440 "*List of functions for rewriting the full file names of MIME parts.
441This is used when viewing parts externally, and is meant for
442transforming the absolute name so that non-compliant programs can find
443the file where it's saved.
444
445Each function takes a file name as input and returns a file name.")
446
447(defvar mm-file-name-replace-whitespace nil
448 "String used for replacing whitespace characters; default is `\"_\"'.")
449
450(defcustom mm-default-directory nil
451 "The default directory where mm will save files.
452If not set, `default-directory' will be used."
453 :type '(choice directory (const :tag "Default" nil))
454 :group 'mime-display)
455
456(defcustom mm-attachment-file-modes 384
457 "Set the mode bits of saved attachments to this integer."
bf247b6e 458 :version "22.1"
23f87bed
MB
459 :type 'integer
460 :group 'mime-display)
461
462(defcustom mm-external-terminal-program "xterm"
463 "The program to start an external terminal."
bf247b6e 464 :version "22.1"
23f87bed
MB
465 :type 'string
466 :group 'mime-display)
467
c113de23
GM
468;;; Internal variables.
469
c113de23
GM
470(defvar mm-last-shell-command "")
471(defvar mm-content-id-alist nil)
23f87bed 472(defvar mm-postponed-undisplay-list nil)
89b163db 473(defvar mm-inhibit-auto-detect-attachment nil)
a025f7d6
KY
474(defvar mm-temp-files-to-be-deleted nil
475 "List of temporary files scheduled to be deleted.")
476(defvar mm-temp-files-cache-file (concat ".mm-temp-files-" (user-login-name))
477 "Name of a file that caches a list of temporary files to be deleted.
478The file will be saved in the directory `mm-tmp-directory'.")
c113de23
GM
479
480;; According to RFC2046, in particular, in a digest, the default
481;; Content-Type value for a body part is changed from "text/plain" to
482;; "message/rfc822".
483(defvar mm-dissect-default-type "text/plain")
484
23f87bed
MB
485(autoload 'mml2015-verify "mml2015")
486(autoload 'mml2015-verify-test "mml2015")
487(autoload 'mml-smime-verify "mml-smime")
488(autoload 'mml-smime-verify-test "mml-smime")
489
490(defvar mm-verify-function-alist
491 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
492 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
493 mm-uu-pgp-signed-test)
494 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
495 mml-smime-verify-test)
496 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
497 mml-smime-verify-test)))
498
499(defcustom mm-verify-option 'never
500 "Option of verifying signed parts.
501`never', not verify; `always', always verify;
01c52d31
MB
502`known', only verify known protocols. Otherwise, ask user.
503
504When set to `always' or `known', you should add
505\"multipart/signed\" to `gnus-buttonized-mime-types' to see
506result of the verification."
bf247b6e 507 :version "22.1"
23f87bed
MB
508 :type '(choice (item always)
509 (item never)
510 (item :tag "only known protocols" known)
511 (item :tag "ask" nil))
512 :group 'mime-security)
513
514(autoload 'mml2015-decrypt "mml2015")
515(autoload 'mml2015-decrypt-test "mml2015")
516
517(defvar mm-decrypt-function-alist
518 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
519 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
520 mm-uu-pgp-encrypted-test)))
521
522(defcustom mm-decrypt-option nil
523 "Option of decrypting encrypted parts.
524`never', not decrypt; `always', always decrypt;
525`known', only decrypt known protocols. Otherwise, ask user."
bf247b6e 526 :version "22.1"
23f87bed
MB
527 :type '(choice (item always)
528 (item never)
529 (item :tag "only known protocols" known)
530 (item :tag "ask" nil))
531 :group 'mime-security)
532
533(defvar mm-viewer-completion-map
534 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
535 (set-keymap-parent map minibuffer-local-completion-map)
c615a00c
SM
536 ;; Should we bind other key to minibuffer-complete-word?
537 (define-key map " " 'self-insert-command)
23f87bed
MB
538 map)
539 "Keymap for input viewer with completion.")
540
62a27ccf
DL
541(defvar mm-viewer-completion-map
542 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
543 (set-keymap-parent map minibuffer-local-completion-map)
c615a00c
SM
544 ;; Should we bind other key to minibuffer-complete-word?
545 (define-key map " " 'self-insert-command)
62a27ccf
DL
546 map)
547 "Keymap for input viewer with completion.")
548
c113de23
GM
549;;; The functions.
550
23f87bed
MB
551(defun mm-alist-to-plist (alist)
552 "Convert association list ALIST into the equivalent property-list form.
553The plist is returned. This converts from
554
555\((a . 1) (b . 2) (c . 3))
556
557into
558
559\(a 1 b 2 c 3)
560
561The original alist is not modified. See also `destructive-alist-to-plist'."
562 (let (plist)
563 (while alist
564 (let ((el (car alist)))
565 (setq plist (cons (cdr el) (cons (car el) plist))))
566 (setq alist (cdr alist)))
567 (nreverse plist)))
568
569(defun mm-keep-viewer-alive-p (handle)
570 "Say whether external viewer for HANDLE should stay alive."
571 (let ((types mm-keep-viewer-alive-types)
572 (type (mm-handle-media-type handle))
573 ty)
574 (catch 'found
575 (while (setq ty (pop types))
576 (when (string-match ty type)
577 (throw 'found t))))))
578
579(defun mm-handle-set-external-undisplayer (handle function)
580 "Set the undisplayer for HANDLE to FUNCTION.
581Postpone undisplaying of viewers for types in
582`mm-keep-viewer-alive-types'."
583 (if (mm-keep-viewer-alive-p handle)
584 (let ((new-handle (copy-sequence handle)))
585 (mm-handle-set-undisplayer new-handle function)
586 (mm-handle-set-undisplayer handle nil)
587 (push new-handle mm-postponed-undisplay-list))
588 (mm-handle-set-undisplayer handle function)))
589
590(defun mm-destroy-postponed-undisplay-list ()
591 (when mm-postponed-undisplay-list
592 (message "Destroying external MIME viewers")
593 (mm-destroy-parts mm-postponed-undisplay-list)))
594
a025f7d6
KY
595(defun mm-temp-files-delete ()
596 "Delete temporary files and those parent directories.
597Note that the deletion may fail if a program is catching hold of a file
598under Windows or Cygwin. In that case, it schedules the deletion of
599files left at the next time."
600 (let* ((coding-system-for-read mm-universal-coding-system)
601 (coding-system-for-write mm-universal-coding-system)
602 (cache-file (expand-file-name mm-temp-files-cache-file
603 mm-tmp-directory))
604 (cache (when (file-exists-p cache-file)
605 (mm-with-multibyte-buffer
606 (insert-file-contents cache-file)
607 (split-string (buffer-string) "\n" t))))
608 fails)
609 (dolist (temp (append cache mm-temp-files-to-be-deleted))
efa44122
KY
610 (when (and (file-exists-p temp)
611 (if (file-directory-p temp)
612 ;; A parent directory left at the previous time.
613 (progn
614 (ignore-errors (delete-directory temp))
615 (file-exists-p temp))
616 ;; Delete a temporary file and its parent directory.
617 (ignore-errors (delete-file temp))
618 (or (file-exists-p temp)
a025f7d6 619 (progn
efa44122 620 (setq temp (file-name-directory temp))
a025f7d6 621 (ignore-errors (delete-directory temp))
efa44122 622 (file-exists-p temp)))))
a025f7d6
KY
623 (push temp fails)))
624 (if fails
625 ;; Schedule the deletion of the files left at the next time.
626 (progn
627 (write-region (concat (mapconcat 'identity (nreverse fails) "\n")
628 "\n")
629 nil cache-file nil 'silent)
630 (set-file-modes cache-file #o600))
631 (when (file-exists-p cache-file)
6e164c21
KY
632 (ignore-errors (delete-file cache-file))))
633 (setq mm-temp-files-to-be-deleted nil)))
a025f7d6 634
aa8f8277
GM
635(autoload 'message-fetch-field "message")
636
ee7d3cc0 637(defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
89b163db
G
638 "Dissect the current buffer and return a list of MIME handles.
639If NO-STRICT-MIME, don't require the message to have a
640MIME-Version header before proceeding."
c113de23 641 (save-excursion
ee7d3cc0 642 (let (ct ctl type subtype cte cd description id result)
c113de23
GM
643 (save-restriction
644 (mail-narrow-to-head)
645 (when (or no-strict-mime
23f87bed 646 loose-mime
c113de23
GM
647 (mail-fetch-field "mime-version"))
648 (setq ct (mail-fetch-field "content-type")
c96ec15a 649 ctl (and ct (mail-header-parse-content-type ct))
c113de23 650 cte (mail-fetch-field "content-transfer-encoding")
c8435226
DD
651 cd (or (mail-fetch-field "content-disposition")
652 (when (and ctl
653 (eq 'mm-inline-text
654 (cadr (mm-assoc-string-match
655 mm-inline-media-tests
656 (car ctl)))))
657 "inline"))
aa227ba5
KY
658 ;; Newlines in description should be stripped so as
659 ;; not to break the MIME tag into two or more lines.
660 description (message-fetch-field "content-description")
23f87bed 661 id (mail-fetch-field "content-id"))
ee7d3cc0 662 (unless from
c96ec15a 663 (setq from (mail-fetch-field "from")))
23f87bed
MB
664 ;; FIXME: In some circumstances, this code is running within
665 ;; an unibyte macro. mail-extract-address-components
666 ;; creates unibyte buffers. This `if', though not a perfect
667 ;; solution, avoids most of them.
668 (if from
4599d0ec
MB
669 (setq from (cadr (mail-extract-address-components from))))
670 (if description
671 (setq description (mail-decode-encoded-word-string
672 description)))))
c113de23
GM
673 (if (or (not ctl)
674 (not (string-match "/" (car ctl))))
675 (mm-dissect-singlepart
676 (list mm-dissect-default-type)
01c52d31 677 (and cte (intern (downcase (mail-header-strip cte))))
c113de23 678 no-strict-mime
c96ec15a 679 (and cd (mail-header-parse-content-disposition cd))
c113de23
GM
680 description)
681 (setq type (split-string (car ctl) "/"))
682 (setq subtype (cadr type)
54e573e6 683 type (car type))
c113de23
GM
684 (setq
685 result
686 (cond
687 ((equal type "multipart")
688 (let ((mm-dissect-default-type (if (equal subtype "digest")
689 "message/rfc822"
23f87bed
MB
690 "text/plain"))
691 (start (cdr (assq 'start (cdr ctl)))))
692 (add-text-properties 0 (length (car ctl))
693 (mm-alist-to-plist (cdr ctl)) (car ctl))
694
695 ;; what really needs to be done here is a way to link a
696 ;; MIME handle back to it's parent MIME handle (in a multilevel
697 ;; MIME article). That would probably require changing
c615a00c 698 ;; the mm-handle API so we simply store the multipart buffer
23f87bed
MB
699 ;; name as a text property of the "multipart/whatever" string.
700 (add-text-properties 0 (length (car ctl))
701 (list 'buffer (mm-copy-to-buffer)
702 'from from
703 'start start)
704 (car ctl))
ee7d3cc0 705 (cons (car ctl) (mm-dissect-multipart ctl from))))
c113de23 706 (t
23f87bed
MB
707 (mm-possibly-verify-or-decrypt
708 (mm-dissect-singlepart
709 ctl
01c52d31 710 (and cte (intern (downcase (mail-header-strip cte))))
23f87bed 711 no-strict-mime
c96ec15a 712 (and cd (mail-header-parse-content-disposition cd))
23f87bed 713 description id)
b0feab7d 714 ctl from))))
c113de23
GM
715 (when id
716 (when (string-match " *<\\(.*\\)> *" id)
717 (setq id (match-string 1 id)))
718 (push (cons id result) mm-content-id-alist))
719 result))))
720
721(defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
722 (when (or force
723 (if (equal "text/plain" (car ctl))
724 (assoc 'format ctl)
725 t))
89b163db
G
726 ;; Guess what the type of application/octet-stream parts should
727 ;; really be.
728 (let ((filename (cdr (assq 'filename (cdr cdl)))))
729 (when (and (not mm-inhibit-auto-detect-attachment)
730 (equal (car ctl) "application/octet-stream")
731 filename
732 (string-match "\\.\\([^.]+\\)$" filename))
733 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
734 (when new-type
735 (setcar ctl new-type)))))
736 (let ((handle
737 (mm-make-handle
738 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
739 (decoder (assoc (car ctl) (mm-archive-decoders))))
740 (if (and decoder
741 ;; Do automatic decoding
742 (cadr decoder)
743 (executable-find (caddr decoder)))
744 (mm-dissect-archive handle)
745 handle))))
c113de23 746
ee7d3cc0 747(defun mm-dissect-multipart (ctl from)
c113de23
GM
748 (goto-char (point-min))
749 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
750 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
751 start parts
752 (end (save-excursion
753 (goto-char (point-max))
754 (if (re-search-backward close-delimiter nil t)
755 (match-beginning 0)
89b163db
G
756 (point-max))))
757 (mm-inhibit-auto-detect-attachment
758 (equal (car ctl) "multipart/encrypted")))
c113de23 759 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
4481aa98 760 (while (and (< (point) end) (re-search-forward boundary end t))
c113de23
GM
761 (goto-char (match-beginning 0))
762 (when start
763 (save-excursion
764 (save-restriction
765 (narrow-to-region start (point))
ee7d3cc0 766 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
23f87bed
MB
767 (end-of-line 2)
768 (or (looking-at boundary)
769 (forward-line 1))
c113de23 770 (setq start (point)))
4481aa98 771 (when (and start (< start end))
c113de23
GM
772 (save-excursion
773 (save-restriction
774 (narrow-to-region start end)
ee7d3cc0 775 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
b0feab7d 776 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
c113de23
GM
777
778(defun mm-copy-to-buffer ()
779 "Copy the contents of the current buffer to a fresh buffer."
d4eb2b7e 780 (let ((obuf (current-buffer))
61dc89b0 781 (mb (mm-multibyte-p))
d4eb2b7e
SM
782 beg)
783 (goto-char (point-min))
784 (search-forward-regexp "^\n" nil t)
785 (setq beg (point))
54e573e6 786 (with-current-buffer
61dc89b0
SM
787 (generate-new-buffer " *mm*")
788 ;; Preserve the data's unibyteness (for url-insert-file-contents).
789 (mm-set-buffer-multibyte mb)
c113de23
GM
790 (insert-buffer-substring obuf beg)
791 (current-buffer))))
792
23f87bed
MB
793(defun mm-display-parts (handle &optional no-default)
794 (if (stringp (car handle))
795 (mapcar 'mm-display-parts (cdr handle))
796 (if (bufferp (car handle))
797 (save-restriction
798 (narrow-to-region (point) (point))
799 (mm-display-part handle)
800 (goto-char (point-max)))
801 (mapcar 'mm-display-parts handle))))
802
aa8f8277
GM
803(autoload 'mailcap-parse-mailcaps "mailcap")
804(autoload 'mailcap-mime-info "mailcap")
805
a87ee50b 806(defun mm-display-part (handle &optional no-default force)
c113de23
GM
807 "Display the MIME part represented by HANDLE.
808Returns nil if the part is removed; inline if displayed inline;
809external if displayed external."
810 (save-excursion
811 (mailcap-parse-mailcaps)
a87ee50b
KY
812 (if (and (not force)
813 (mm-handle-displayed-p handle))
c113de23 814 (mm-remove-part handle)
163cb72d
MB
815 (let* ((ehandle (if (equal (mm-handle-media-type handle)
816 "message/external-body")
817 (progn
818 (unless (mm-handle-cache handle)
819 (mm-extern-cache-contents handle))
820 (mm-handle-cache handle))
821 handle))
822 (type (mm-handle-media-type ehandle))
23f87bed
MB
823 (method (mailcap-mime-info type))
824 (filename (or (mail-content-type-get
825 (mm-handle-disposition handle) 'filename)
826 (mail-content-type-get
827 (mm-handle-type handle) 'name)
828 "<file>"))
89b163db
G
829 (external mm-enable-external)
830 (decoder (assoc (car (mm-handle-type handle))
831 (mm-archive-decoders))))
832 (cond
833 ((and decoder
834 (executable-find (caddr decoder)))
835 (mm-archive-dissect-and-inline handle)
836 'inline)
837 ((and (mm-inlinable-p ehandle)
838 (mm-inlined-p ehandle))
839 (forward-line 1)
840 (mm-display-inline handle)
841 'inline)
842 ((or method
843 (not no-default))
844 (if (and (not method)
845 (equal "text" (car (split-string type "/"))))
846 (progn
847 (forward-line 1)
848 (mm-insert-inline handle (mm-get-part handle))
849 'inline)
850 (setq external
851 (and method ;; If nil, we always use "save".
23f87bed
MB
852 (stringp method) ;; 'mailcap-save-binary-file
853 (or (eq mm-enable-external t)
854 (and (eq mm-enable-external 'ask)
855 (y-or-n-p
856 (concat
857 "Display part (" type
858 ") using external program"
859 ;; Can non-string method ever happen?
860 (if (stringp method)
861 (concat
862 " \"" (format method filename) "\"")
863 "")
89b163db
G
864 "? "))))))
865 (if external
23f87bed 866 (mm-display-external
89b163db
G
867 handle (or method 'mailcap-save-binary-file))
868 (mm-display-external
869 handle 'mailcap-save-binary-file)))))))))
c113de23 870
60ef4154 871(declare-function gnus-configure-windows "gnus-win" (setting &optional force))
aa8f8277 872(defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
c7641e3c
GM
873(declare-function term-mode "term" ())
874(declare-function term-char-mode "term" ())
60ef4154 875
c113de23
GM
876(defun mm-display-external (handle method)
877 "Display HANDLE using METHOD."
878 (let ((outbuf (current-buffer)))
879 (mm-with-unibyte-buffer
880 (if (functionp method)
881 (let ((cur (current-buffer)))
882 (if (eq method 'mailcap-save-binary-file)
883 (progn
6eb681a3 884 (set-buffer (generate-new-buffer " *mm*"))
c113de23
GM
885 (setq method nil))
886 (mm-insert-part handle)
87035689 887 (mm-add-meta-html-tag handle)
c113de23
GM
888 (let ((win (get-buffer-window cur t)))
889 (when win
890 (select-window win)))
6eb681a3 891 (switch-to-buffer (generate-new-buffer " *mm*")))
23f87bed 892 (buffer-disable-undo)
c113de23
GM
893 (mm-set-buffer-file-coding-system mm-binary-coding-system)
894 (insert-buffer-substring cur)
895 (goto-char (point-min))
23f87bed
MB
896 (when method
897 (message "Viewing with %s" method))
c113de23
GM
898 (let ((mm (current-buffer))
899 (non-viewer (assq 'non-viewer
900 (mailcap-mime-info
901 (mm-handle-media-type handle) t))))
902 (unwind-protect
903 (if method
904 (funcall method)
905 (mm-save-part handle))
906 (when (and (not non-viewer)
907 method)
908 (mm-handle-set-undisplayer handle mm)))))
909 ;; The function is a string to be executed.
910 (mm-insert-part handle)
87035689 911 (mm-add-meta-html-tag handle)
23f87bed 912 (let* ((dir (mm-make-temp-file
3efe5554 913 (expand-file-name "emm." mm-tmp-directory) 'dir))
23f87bed
MB
914 (filename (or
915 (mail-content-type-get
916 (mm-handle-disposition handle) 'filename)
917 (mail-content-type-get
918 (mm-handle-type handle) 'name)))
c113de23
GM
919 (mime-info (mailcap-mime-info
920 (mm-handle-media-type handle) t))
921 (needsterm (or (assoc "needsterm" mime-info)
922 (assoc "needsterminal" mime-info)))
923 (copiousoutput (assoc "copiousoutput" mime-info))
924 file buffer)
925 ;; We create a private sub-directory where we store our files.
d55fe5bb 926 (set-file-modes dir #o700)
c113de23 927 (if filename
23f87bed
MB
928 (setq file (expand-file-name
929 (gnus-map-function mm-file-name-rewrite-functions
930 (file-name-nondirectory filename))
931 dir))
cf5a5c38
MB
932 ;; Use nametemplate (defined in RFC1524) if it is specified
933 ;; in mailcap.
934 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
935 (if (and suffix
936 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
937 (setq suffix (match-string 1 suffix))
938 ;; Otherwise, use a suffix according to
939 ;; `mailcap-mime-extensions'.
940 (setq suffix (car (rassoc (mm-handle-media-type handle)
941 mailcap-mime-extensions))))
942 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
943 nil suffix))))
c113de23
GM
944 (let ((coding-system-for-write mm-binary-coding-system))
945 (write-region (point-min) (point-max) file nil 'nomesg))
d55fe5bb
MB
946 ;; The file is deleted after the viewer exists. If the users edits
947 ;; the file, changes will be lost. Set file to read-only to make it
948 ;; clear.
949 (set-file-modes file #o400)
c113de23 950 (message "Viewing with %s" method)
23f87bed
MB
951 (cond
952 (needsterm
953 (let ((command (mm-mailcap-command
954 method file (mm-handle-type handle))))
955 (unwind-protect
956 (if window-system
7cfa5cec
KY
957 (set-process-sentinel
958 (start-process "*display*" nil
959 mm-external-terminal-program
960 "-e" shell-file-name
961 shell-command-switch command)
962 `(lambda (process state)
963 (if (eq 'exit (process-status process))
964 (run-at-time
965 60.0 nil
966 (lambda ()
967 (ignore-errors (delete-file ,file))
968 (ignore-errors (delete-directory
969 ,(file-name-directory
970 file))))))))
23f87bed
MB
971 (require 'term)
972 (require 'gnus-win)
973 (set-buffer
974 (setq buffer
975 (make-term "display"
976 shell-file-name
977 nil
978 shell-command-switch command)))
979 (term-mode)
980 (term-char-mode)
981 (set-process-sentinel
982 (get-buffer-process buffer)
983 `(lambda (process state)
7cfa5cec
KY
984 (when (eq 'exit (process-status process))
985 (ignore-errors (delete-file ,file))
986 (ignore-errors
987 (delete-directory ,(file-name-directory file)))
988 (gnus-configure-windows
989 ',gnus-current-window-configuration))))
23f87bed 990 (gnus-configure-windows 'display-term))
7cfa5cec
KY
991 (mm-handle-set-external-undisplayer handle (cons file buffer))
992 (add-to-list 'mm-temp-files-to-be-deleted file t))
23f87bed
MB
993 (message "Displaying %s..." command))
994 'external)
995 (copiousoutput
996 (with-current-buffer outbuf
997 (forward-line 1)
998 (mm-insert-inline
999 handle
1000 (unwind-protect
1001 (progn
1002 (call-process shell-file-name nil
1003 (setq buffer
1004 (generate-new-buffer " *mm*"))
1005 nil
1006 shell-command-switch
1007 (mm-mailcap-command
1008 method file (mm-handle-type handle)))
1009 (if (buffer-live-p buffer)
c615a00c 1010 (with-current-buffer buffer
23f87bed
MB
1011 (buffer-string))))
1012 (progn
1013 (ignore-errors (delete-file file))
1014 (ignore-errors (delete-directory
1015 (file-name-directory file)))
1016 (ignore-errors (kill-buffer buffer))))))
1017 'inline)
1018 (t
58090a8d
MB
1019 ;; Deleting the temp file should be postponed for some wrappers,
1020 ;; shell scripts, and so on, which might exit right after having
1021 ;; started a viewer command as a background job.
23f87bed
MB
1022 (let ((command (mm-mailcap-command
1023 method file (mm-handle-type handle))))
1024 (unwind-protect
a6933dcc 1025 (let ((process-connection-type nil))
4b91459a
MB
1026 (start-process "*display*"
1027 (setq buffer
1028 (generate-new-buffer " *mm*"))
1029 shell-file-name
1030 shell-command-switch command)
1031 (set-process-sentinel
1032 (get-buffer-process buffer)
89b163db
G
1033 (lexical-let ((outbuf outbuf)
1034 (file file)
1035 (buffer buffer)
1036 (command command)
1037 (handle handle))
89b163db
G
1038 (lambda (process state)
1039 (when (eq (process-status process) 'exit)
7cfa5cec
KY
1040 (run-at-time
1041 60.0 nil
1042 (lambda ()
1043 (ignore-errors (delete-file file))
1044 (ignore-errors (delete-directory
1045 (file-name-directory file)))))
89b163db
G
1046 (when (buffer-live-p outbuf)
1047 (with-current-buffer outbuf
1048 (let ((buffer-read-only nil)
1049 (point (point)))
1050 (forward-line 2)
89cccc2f
G
1051 (let ((start (point)))
1052 (mm-insert-inline
1053 handle (with-current-buffer buffer
1054 (buffer-string)))
1055 (put-text-property start (point)
1056 'face 'mm-command-output))
89b163db
G
1057 (goto-char point))))
1058 (when (buffer-live-p buffer)
1059 (kill-buffer buffer)))
1060 (message "Displaying %s...done" command)))))
23f87bed 1061 (mm-handle-set-external-undisplayer
a025f7d6
KY
1062 handle (cons file buffer))
1063 (add-to-list 'mm-temp-files-to-be-deleted file t))
23f87bed
MB
1064 (message "Displaying %s..." command))
1065 'external)))))))
a1506d29 1066
c113de23
GM
1067(defun mm-mailcap-command (method file type-list)
1068 (let ((ctl (cdr type-list))
1069 (beg 0)
1070 (uses-stdin t)
1071 out sub total)
23f87bed
MB
1072 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1073 method beg)
c113de23
GM
1074 (push (substring method beg (match-beginning 0)) out)
1075 (setq beg (match-end 0)
1076 total (match-string 0 method)
1077 sub (match-string 1 method))
1078 (cond
1079 ((string= total "%%")
1080 (push "%" out))
23f87bed
MB
1081 ((or (string= total "%s")
1082 ;; We do our own quoting.
1083 (string= total "'%s'")
1084 (string= total "\"%s\""))
c113de23 1085 (setq uses-stdin nil)
01c52d31 1086 (push (shell-quote-argument
23f87bed 1087 (gnus-map-function mm-path-name-rewrite-functions file)) out))
c113de23 1088 ((string= total "%t")
01c52d31 1089 (push (shell-quote-argument (car type-list)) out))
c113de23 1090 (t
01c52d31 1091 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
c113de23 1092 (push (substring method beg (length method)) out)
23f87bed
MB
1093 (when uses-stdin
1094 (push "<" out)
01c52d31 1095 (push (shell-quote-argument
23f87bed
MB
1096 (gnus-map-function mm-path-name-rewrite-functions file))
1097 out))
c113de23 1098 (mapconcat 'identity (nreverse out) "")))
a1506d29 1099
c113de23
GM
1100(defun mm-remove-parts (handles)
1101 "Remove the displayed MIME parts represented by HANDLES."
1102 (if (and (listp handles)
1103 (bufferp (car handles)))
1104 (mm-remove-part handles)
1105 (let (handle)
1106 (while (setq handle (pop handles))
1107 (cond
1108 ((stringp handle)
23f87bed
MB
1109 (when (buffer-live-p (get-text-property 0 'buffer handle))
1110 (kill-buffer (get-text-property 0 'buffer handle))))
c113de23
GM
1111 ((and (listp handle)
1112 (stringp (car handle)))
1113 (mm-remove-parts (cdr handle)))
1114 (t
1115 (mm-remove-part handle)))))))
1116
1117(defun mm-destroy-parts (handles)
1118 "Remove the displayed MIME parts represented by HANDLES."
1119 (if (and (listp handles)
1120 (bufferp (car handles)))
1121 (mm-destroy-part handles)
1122 (let (handle)
1123 (while (setq handle (pop handles))
1124 (cond
1125 ((stringp handle)
23f87bed
MB
1126 (when (buffer-live-p (get-text-property 0 'buffer handle))
1127 (kill-buffer (get-text-property 0 'buffer handle))))
c113de23
GM
1128 ((and (listp handle)
1129 (stringp (car handle)))
23f87bed 1130 (mm-destroy-parts handle))
c113de23
GM
1131 (t
1132 (mm-destroy-part handle)))))))
1133
1134(defun mm-remove-part (handle)
1135 "Remove the displayed MIME part represented by HANDLE."
1136 (when (listp handle)
1137 (let ((object (mm-handle-undisplayer handle)))
1138 (ignore-errors
1139 (cond
1140 ;; Internally displayed part.
1141 ((mm-annotationp object)
60ef4154
GM
1142 (if (featurep 'xemacs)
1143 (delete-annotation object)))
c113de23
GM
1144 ((or (functionp object)
1145 (and (listp object)
1146 (eq (car object) 'lambda)))
1147 (funcall object))
1148 ;; Externally displayed part.
1149 ((consp object)
23f87bed
MB
1150 (condition-case ()
1151 (while (get-buffer-process (cdr object))
1152 (interrupt-process (get-buffer-process (cdr object)))
1153 (message "Waiting for external displayer to die...")
1154 (sit-for 1))
1155 (quit)
1156 (error))
1157 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1158 (message "Waiting for external displayer to die...done")
c113de23 1159 (ignore-errors (delete-file (car object)))
23f87bed
MB
1160 (ignore-errors (delete-directory (file-name-directory
1161 (car object)))))
c113de23
GM
1162 ((bufferp object)
1163 (when (buffer-live-p object)
1164 (kill-buffer object)))))
1165 (mm-handle-set-undisplayer handle nil))))
1166
1167(defun mm-display-inline (handle)
1168 (let* ((type (mm-handle-media-type handle))
1169 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1170 (funcall function handle)
1171 (goto-char (point-min))))
1172
1173(defun mm-assoc-string-match (alist type)
1174 (dolist (elem alist)
1175 (when (string-match (car elem) type)
1176 (return elem))))
1177
23f87bed
MB
1178(defun mm-automatic-display-p (handle)
1179 "Say whether the user wants HANDLE to be displayed automatically."
1180 (let ((methods mm-automatic-display)
1181 (type (mm-handle-media-type handle))
1182 method result)
1183 (while (setq method (pop methods))
1184 (when (and (not (mm-inline-override-p handle))
1185 (string-match method type))
1186 (setq result t
1187 methods nil)))
1188 result))
1189
54e573e6
MB
1190(defun mm-inlinable-p (handle &optional type)
1191 "Say whether HANDLE can be displayed inline.
1192TYPE is the mime-type of the object; it defaults to the one given
1193in HANDLE."
1194 (unless type (setq type (mm-handle-media-type handle)))
c113de23 1195 (let ((alist mm-inline-media-tests)
c113de23
GM
1196 test)
1197 (while alist
1198 (when (string-match (caar alist) type)
1199 (setq test (caddar alist)
1200 alist nil)
1201 (setq test (funcall test handle)))
1202 (pop alist))
1203 test))
1204
c113de23 1205(defun mm-inlined-p (handle)
23f87bed 1206 "Say whether the user wants HANDLE to be displayed inline."
c113de23
GM
1207 (let ((methods mm-inlined-types)
1208 (type (mm-handle-media-type handle))
1209 method result)
1210 (while (setq method (pop methods))
1211 (when (and (not (mm-inline-override-p handle))
23f87bed 1212 (string-match method type))
c113de23
GM
1213 (setq result t
1214 methods nil)))
1215 result))
1216
1217(defun mm-attachment-override-p (handle)
1218 "Say whether HANDLE should have attachment behavior overridden."
1219 (let ((types mm-attachment-override-types)
1220 (type (mm-handle-media-type handle))
1221 ty)
1222 (catch 'found
1223 (while (setq ty (pop types))
1224 (when (and (string-match ty type)
1225 (mm-inlinable-p handle))
1226 (throw 'found t))))))
1227
1228(defun mm-inline-override-p (handle)
1229 "Say whether HANDLE should have inline behavior overridden."
1230 (let ((types mm-inline-override-types)
1231 (type (mm-handle-media-type handle))
1232 ty)
1233 (catch 'found
1234 (while (setq ty (pop types))
1235 (when (string-match ty type)
1236 (throw 'found t))))))
1237
1238(defun mm-automatic-external-display-p (type)
1239 "Return the user-defined method for TYPE."
1240 (let ((methods mm-automatic-external-display)
1241 method result)
1242 (while (setq method (pop methods))
1243 (when (string-match method type)
1244 (setq result t
1245 methods nil)))
1246 result))
1247
1248(defun mm-destroy-part (handle)
1249 "Destroy the data structures connected to HANDLE."
1250 (when (listp handle)
1251 (mm-remove-part handle)
1252 (when (buffer-live-p (mm-handle-buffer handle))
1253 (kill-buffer (mm-handle-buffer handle)))))
1254
1255(defun mm-handle-displayed-p (handle)
1256 "Say whether HANDLE is displayed or not."
1257 (mm-handle-undisplayer handle))
1258
1259;;;
1260;;; Functions for outputting parts
1261;;;
1262
531bedc3
MB
1263(defmacro mm-with-part (handle &rest forms)
1264 "Run FORMS in the temp buffer containing the contents of HANDLE."
d4eb2b7e
SM
1265 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1266 ;; chars, so the buffer should be unibyte. It may happen that the
1267 ;; handle-buffer is multibyte for some reason, in which case now is a good
1268 ;; time to adjust it, since we know at this point that it should
1269 ;; be unibyte.
1270 `(let* ((handle ,handle))
8ccbef23
G
1271 (when (and (mm-handle-buffer handle)
1272 (buffer-name (mm-handle-buffer handle)))
1273 (with-temp-buffer
1274 (mm-disable-multibyte)
1275 (insert-buffer-substring (mm-handle-buffer handle))
1276 (mm-decode-content-transfer-encoding
1277 (mm-handle-encoding handle)
1278 (mm-handle-media-type handle))
1279 ,@forms))))
531bedc3
MB
1280(put 'mm-with-part 'lisp-indent-function 1)
1281(put 'mm-with-part 'edebug-form-spec '(body))
1282
77218834
MB
1283(defun mm-get-part (handle &optional no-cache)
1284 "Return the contents of HANDLE as a string.
1285If NO-CACHE is non-nil, cached contents of a message/external-body part
1286are ignored."
1287 (if (and (not no-cache)
1288 (equal (mm-handle-media-type handle) "message/external-body"))
531bedc3
MB
1289 (progn
1290 (unless (mm-handle-cache handle)
1291 (mm-extern-cache-contents handle))
1292 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1293 (buffer-string)))
1294 (mm-with-part handle
719120ef 1295 (buffer-string))))
c113de23 1296
77218834
MB
1297(defun mm-insert-part (handle &optional no-cache)
1298 "Insert the contents of HANDLE in the current buffer.
1299If NO-CACHE is non-nil, cached contents of a message/external-body part
1300are ignored."
01c52d31
MB
1301 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1302 'charset)
1303 'gnus-decoded)
1304 (with-current-buffer (mm-handle-buffer handle)
1305 (buffer-string)))
1306 ((mm-multibyte-p)
1307 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1308 (t
1309 (mm-get-part handle no-cache)))))
1310 (save-restriction
1311 (widen)
1312 (goto-char
1313 (prog1
1314 (point)
1315 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1316 'mm-uu-extract)
1317 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1318 ;; Separate the extracted parts that have the same faces.
1319 (insert "\n" text)
1320 (insert text)))))))
23f87bed
MB
1321
1322(defun mm-file-name-delete-whitespace (file-name)
1323 "Remove all whitespace characters from FILE-NAME."
1324 (while (string-match "\\s-+" file-name)
1325 (setq file-name (replace-match "" t t file-name)))
1326 file-name)
1327
1328(defun mm-file-name-trim-whitespace (file-name)
1329 "Remove leading and trailing whitespace characters from FILE-NAME."
1330 (when (string-match "\\`\\s-+" file-name)
1331 (setq file-name (substring file-name (match-end 0))))
1332 (when (string-match "\\s-+\\'" file-name)
1333 (setq file-name (substring file-name 0 (match-beginning 0))))
1334 file-name)
1335
1336(defun mm-file-name-collapse-whitespace (file-name)
1337 "Collapse multiple whitespace characters in FILE-NAME."
1338 (while (string-match "\\s-\\s-+" file-name)
1339 (setq file-name (replace-match " " t t file-name)))
1340 file-name)
1341
1342(defun mm-file-name-replace-whitespace (file-name)
1343 "Replace whitespace characters in FILE-NAME with underscores.
1344Set the option `mm-file-name-replace-whitespace' to any other
1345string if you do not like underscores."
1346 (let ((s (or mm-file-name-replace-whitespace "_")))
1347 (while (string-match "\\s-" file-name)
1348 (setq file-name (replace-match s t t file-name))))
1349 file-name)
1350
1351(defun mm-file-name-delete-control (filename)
1352 "Delete control characters from FILENAME."
1353 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1354
1355(defun mm-file-name-delete-gotchas (filename)
1356 "Delete shell gotchas from FILENAME."
1357 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1358 (gnus-replace-in-string filename "^[.-]+" ""))
c113de23 1359
01c52d31
MB
1360(defun mm-save-part (handle &optional prompt)
1361 "Write HANDLE to a file.
1362PROMPT overrides the default one used to ask user for a file name."
531bedc3
MB
1363 (let ((filename (or (mail-content-type-get
1364 (mm-handle-disposition handle) 'filename)
1365 (mail-content-type-get
1366 (mm-handle-type handle) 'name)))
1367 file)
c113de23 1368 (when filename
23f87bed
MB
1369 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1370 (file-name-nondirectory filename))))
dc39b96a
CS
1371 (while
1372 (progn
1373 (setq file
1374 (read-file-name
1375 (or prompt
1376 (format "Save MIME part to (default %s): "
1377 (or filename "")))
1378 (or mm-default-directory default-directory)
1379 (expand-file-name (or filename "")
1380 (or mm-default-directory default-directory))))
1381 (cond ((or (not file) (equal file ""))
1382 (message "Please enter a file name")
1383 t)
1384 ((and (file-directory-p file)
1385 (not filename))
1386 (message "Please enter a non-directory file name")
1387 t)
1388 (t nil)))
1389 (sit-for 2)
1390 (discard-input))
3d319c8f
LMI
1391 (if (file-directory-p file)
1392 (setq file (expand-file-name filename file))
1393 (setq file (expand-file-name
1394 file (or mm-default-directory default-directory))))
c113de23 1395 (setq mm-default-directory (file-name-directory file))
23f87bed
MB
1396 (and (or (not (file-exists-p file))
1397 (yes-or-no-p (format "File %s already exists; overwrite? "
1398 file)))
1399 (progn
1400 (mm-save-part-to-file handle file)
1401 file))))
c113de23 1402
82fc7980 1403(defun mm-add-meta-html-tag (handle &optional charset force-charset)
bbbe940b
MB
1404 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1405CHARSET defaults to the one HANDLE specifies. Existing meta tag that
82fc7980
KY
1406specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1407Return t if meta tag is added or replaced."
bbbe940b
MB
1408 (when (equal (mm-handle-media-type handle) "text/html")
1409 (when (or charset
1410 (setq charset (mail-content-type-get (mm-handle-type handle)
1411 'charset)))
1412 (setq charset (format "\
1413<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1414 (let ((case-fold-search t))
1415 (goto-char (point-min))
1416 (if (re-search-forward "\
1417<meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
07dd2ea4 1418text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\([^\"'>]+\\)\\)?" nil t)
82fc7980
KY
1419 (if (and (not force-charset)
1420 (match-beginning 2)
bbbe940b
MB
1421 (string-match "\\`html\\'" (match-string 1)))
1422 ;; Don't modify existing meta tag.
1423 nil
1424 ;; Replace it with the one specifying charset.
1425 (replace-match charset)
1426 t)
1427 (if (re-search-forward "<head>\\s-*" nil t)
1428 (insert charset "\n")
1429 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1430 (insert "<head>\n" charset "\n</head>\n"))
1431 t)))))
1432
c113de23
GM
1433(defun mm-save-part-to-file (handle file)
1434 (mm-with-unibyte-buffer
1435 (mm-insert-part handle)
bbbe940b 1436 (mm-add-meta-html-tag handle)
01c52d31
MB
1437 (let ((current-file-modes (default-file-modes)))
1438 (set-default-file-modes mm-attachment-file-modes)
1439 (unwind-protect
c113de23
GM
1440 ;; Don't re-compress .gz & al. Arguably we should make
1441 ;; `file-name-handler-alist' nil, but that would chop
1442 ;; ange-ftp, which is reasonable to use here.
01c52d31 1443 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
23f87bed 1444 (set-default-file-modes current-file-modes)))))
c113de23 1445
9581ba4d
KY
1446(defun mm-pipe-part (handle &optional cmd)
1447 "Pipe HANDLE to a process.
1448Use CMD as the process."
1449 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1450 (command (or cmd
1451 (gnus-read-shell-command
1452 "Shell command on MIME part: " mm-last-shell-command))))
c113de23
GM
1453 (mm-with-unibyte-buffer
1454 (mm-insert-part handle)
bbbe940b 1455 (mm-add-meta-html-tag handle)
23f87bed
MB
1456 (let ((coding-system-for-write 'binary))
1457 (shell-command-on-region (point-min) (point-max) command nil)))))
c113de23 1458
e116c1eb
GM
1459(autoload 'gnus-completing-read "gnus-util")
1460
c113de23
GM
1461(defun mm-interactively-view-part (handle)
1462 "Display HANDLE using METHOD."
1463 (let* ((type (mm-handle-media-type handle))
1464 (methods
229b59da 1465 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
c113de23 1466 (mailcap-mime-info type 'all)))
62a27ccf
DL
1467 (method (let ((minibuffer-local-completion-map
1468 mm-viewer-completion-map))
0dc42289 1469 (completing-read "Viewer: " methods))))
c113de23
GM
1470 (when (string= method "")
1471 (error "No method given"))
5bb548d2 1472 (if (string-match "^[^% \t]+$" method)
c113de23 1473 (setq method (concat method " %s")))
619ac84f 1474 (mm-display-external handle method)))
c113de23
GM
1475
1476(defun mm-preferred-alternative (handles &optional preferred)
1477 "Say which of HANDLES are preferred."
1478 (let ((prec (if preferred (list preferred)
1479 (mm-preferred-alternative-precedence handles)))
1480 p h result type handle)
1481 (while (setq p (pop prec))
1482 (setq h handles)
1483 (while h
1484 (setq handle (car h))
1485 (setq type (mm-handle-media-type handle))
1486 (when (and (equal p type)
1487 (mm-automatic-display-p handle)
1488 (or (stringp (car handle))
1489 (not (mm-handle-disposition handle))
1490 (equal (car (mm-handle-disposition handle))
1491 "inline")))
1492 (setq result handle
1493 h nil
1494 prec nil))
1495 (pop h)))
1496 result))
1497
1498(defun mm-preferred-alternative-precedence (handles)
1499 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
c516cd6d
LMI
1500 (setq handles (reverse handles))
1501 (dolist (disc (reverse mm-discouraged-alternatives))
1502 (dolist (handle (copy-sequence handles))
1503 (when (string-match disc (mm-handle-media-type handle))
1504 (setq handles (nconc (delete handle handles) (list handle))))))
1505 ;; Remove empty parts.
1506 (dolist (handle (copy-sequence handles))
acf151a8
LI
1507 (when (and (bufferp (mm-handle-buffer handle))
1508 (not (with-current-buffer (mm-handle-buffer handle)
1509 (goto-char (point-min))
1510 (re-search-forward "[^ \t\n]" nil t))))
c516cd6d
LMI
1511 (setq handles (nconc (delete handle handles) (list handle)))))
1512 (mapcar #'mm-handle-media-type handles))
c113de23
GM
1513
1514(defun mm-get-content-id (id)
1515 "Return the handle(s) referred to by ID."
1516 (cdr (assoc id mm-content-id-alist)))
1517
23f87bed
MB
1518(defconst mm-image-type-regexps
1519 '(("/\\*.*XPM.\\*/" . xpm)
1520 ("P[1-6]" . pbm)
1521 ("GIF8" . gif)
1522 ("\377\330" . jpeg)
1523 ("\211PNG\r\n" . png)
1524 ("#define" . xbm)
1525 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1526 ("%!PS" . postscript))
1527 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1528When the first bytes of an image file match REGEXP, it is assumed to
1529be of image type IMAGE-TYPE.")
1530
1531;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1532(defun mm-image-type-from-buffer ()
1533 "Determine the image type from data in the current buffer.
1534Value is a symbol specifying the image type or nil if type cannot
1535be determined."
1536 (let ((types mm-image-type-regexps)
1537 type)
1538 (goto-char (point-min))
1539 (while (and types (null type))
1540 (let ((regexp (car (car types)))
1541 (image-type (cdr (car types))))
1542 (when (looking-at regexp)
1543 (setq type image-type))
1544 (setq types (cdr types))))
1545 type))
1546
c113de23
GM
1547(defun mm-get-image (handle)
1548 "Return an image instance based on HANDLE."
1549 (let ((type (mm-handle-media-subtype handle))
1550 spec)
1551 ;; Allow some common translations.
1552 (setq type
1553 (cond
1554 ((equal type "x-pixmap")
1555 "xpm")
1556 ((equal type "x-xbitmap")
1557 "xbm")
5bb548d2
DL
1558 ((equal type "x-portable-bitmap")
1559 "pbm")
c113de23
GM
1560 (t type)))
1561 (or (mm-handle-cache handle)
1562 (mm-with-unibyte-buffer
1563 (mm-insert-part handle)
1564 (prog1
1565 (setq spec
1566 (ignore-errors
23f87bed
MB
1567 ;; Avoid testing `make-glyph' since W3 may define
1568 ;; a bogus version of it.
c113de23 1569 (if (fboundp 'create-image)
23f87bed
MB
1570 (create-image (buffer-string)
1571 (or (mm-image-type-from-buffer)
1572 (intern type))
1573 'data-p)
1574 (mm-create-image-xemacs type))))
c113de23
GM
1575 (mm-handle-set-cache handle spec))))))
1576
23f87bed 1577(defun mm-create-image-xemacs (type)
9efa445f
DN
1578 (when (featurep 'xemacs)
1579 (cond
1580 ((equal type "xbm")
1581 ;; xbm images require special handling, since
1582 ;; the only way to create glyphs from these
1583 ;; (without a ton of work) is to write them
1584 ;; out to a file, and then create a file
1585 ;; specifier.
1586 (let ((file (mm-make-temp-file
1587 (expand-file-name "emm" mm-tmp-directory)
1588 nil ".xbm")))
1589 (unwind-protect
1590 (progn
1591 (write-region (point-min) (point-max) file)
1592 (make-glyph (list (cons 'x file))))
1593 (ignore-errors
1594 (delete-file file)))))
1595 (t
1596 (make-glyph
1597 (vector
1598 (or (mm-image-type-from-buffer)
1599 (intern type))
1600 :data (buffer-string)))))))
23f87bed 1601
12c9fab6
GM
1602(declare-function image-size "image.c" (spec &optional pixels frame))
1603
c113de23
GM
1604(defun mm-image-fit-p (handle)
1605 "Say whether the image in HANDLE will fit the current window."
1606 (let ((image (mm-get-image handle)))
524705ae 1607 (or (not image)
9efa445f 1608 (if (featurep 'xemacs)
44e97401 1609 ;; XEmacs's glyphs can actually tell us about their width, so
c7015153 1610 ;; let's be nice and smart about them.
524705ae
MB
1611 (or mm-inline-large-images
1612 (and (<= (glyph-width image) (window-pixel-width))
1613 (<= (glyph-height image) (window-pixel-height))))
1614 (let* ((size (image-size image))
1615 (w (car size))
1616 (h (cdr size)))
1617 (or mm-inline-large-images
1618 (and (<= h (1- (window-height))) ; Don't include mode line.
1619 (<= w (window-width)))))))))
c113de23
GM
1620
1621(defun mm-valid-image-format-p (format)
1622 "Say whether FORMAT can be displayed natively by Emacs."
1623 (cond
1624 ;; Handle XEmacs
1625 ((fboundp 'valid-image-instantiator-format-p)
1626 (valid-image-instantiator-format-p format))
5b5dafd2 1627 ;; Handle Emacs
c113de23
GM
1628 ((fboundp 'image-type-available-p)
1629 (and (display-graphic-p)
1630 (image-type-available-p format)))
1631 ;; Nobody else can do images yet.
1632 (t
1633 nil)))
1634
1635(defun mm-valid-and-fit-image-p (format handle)
1636 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
62a27ccf 1637 (and (mm-valid-image-format-p format)
c113de23
GM
1638 (mm-image-fit-p handle)))
1639
23f87bed
MB
1640(defun mm-find-part-by-type (handles type &optional notp recursive)
1641 "Search in HANDLES for part with TYPE.
1642If NOTP, returns first non-matching part.
1643If RECURSIVE, search recursively."
1644 (let (handle)
1645 (while handles
1646 (if (and recursive (stringp (caar handles)))
1647 (if (setq handle (mm-find-part-by-type (cdar handles) type
1648 notp recursive))
1649 (setq handles nil))
1650 (if (if notp
1651 (not (equal (mm-handle-media-type (car handles)) type))
1652 (equal (mm-handle-media-type (car handles)) type))
1653 (setq handle (car handles)
1654 handles nil)))
1655 (setq handles (cdr handles)))
1656 handle))
1657
1658(defun mm-find-raw-part-by-type (ctl type &optional notp)
1659 (goto-char (point-min))
1660 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1661 'boundary)))
1662 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1663 start
1664 (end (save-excursion
1665 (goto-char (point-max))
1666 (if (re-search-backward close-delimiter nil t)
1667 (match-beginning 0)
1668 (point-max))))
1669 result)
1670 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1671 (while (and (not result)
1672 (re-search-forward boundary end t))
1673 (goto-char (match-beginning 0))
1674 (when start
1675 (save-excursion
1676 (save-restriction
1677 (narrow-to-region start (1- (point)))
c96ec15a
MB
1678 (when (let* ((ct (mail-fetch-field "content-type"))
1679 (ctl (and ct (mail-header-parse-content-type ct))))
23f87bed
MB
1680 (if notp
1681 (not (equal (car ctl) type))
1682 (equal (car ctl) type)))
1683 (setq result (buffer-string))))))
1684 (forward-line 1)
1685 (setq start (point)))
1686 (when (and (not result) start)
1687 (save-excursion
1688 (save-restriction
1689 (narrow-to-region start end)
c96ec15a
MB
1690 (when (let* ((ct (mail-fetch-field "content-type"))
1691 (ctl (and ct (mail-header-parse-content-type ct))))
23f87bed
MB
1692 (if notp
1693 (not (equal (car ctl) type))
1694 (equal (car ctl) type)))
1695 (setq result (buffer-string))))))
1696 result))
1697
1698(defvar mm-security-handle nil)
1699
1700(defsubst mm-set-handle-multipart-parameter (handle parameter value)
1701 ;; HANDLE could be a CTL.
1702 (when handle
1703 (put-text-property 0 (length (car handle)) parameter value
1704 (car handle))))
1705
60ef4154
GM
1706(autoload 'mm-view-pkcs7 "mm-view")
1707
b0feab7d 1708(defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
23f87bed
MB
1709 (let ((type (car ctl))
1710 (subtype (cadr (split-string (car ctl) "/")))
1711 (mm-security-handle ctl) ;; (car CTL) is the type.
1712 protocol func functest)
1713 (cond
1714 ((or (equal type "application/x-pkcs7-mime")
1715 (equal type "application/pkcs7-mime"))
1716 (with-temp-buffer
1717 (when (and (cond
1718 ((eq mm-decrypt-option 'never) nil)
1719 ((eq mm-decrypt-option 'always) t)
1720 ((eq mm-decrypt-option 'known) t)
1721 (t (y-or-n-p
1722 (format "Decrypt (S/MIME) part? "))))
b0feab7d 1723 (mm-view-pkcs7 parts from))
23f87bed
MB
1724 (setq parts (mm-dissect-buffer t)))))
1725 ((equal subtype "signed")
1726 (unless (and (setq protocol
1727 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1728 (not (equal protocol "multipart/mixed")))
1729 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1730 (let ((protocols mm-verify-function-alist))
1731 (while protocols
1732 (if (and (or (not (setq functest (nth 3 (car protocols))))
1733 (funcall functest parts ctl))
1734 (mm-find-part-by-type parts (caar protocols) nil t))
1735 (setq protocol (caar protocols)
1736 protocols nil)
1737 (setq protocols (cdr protocols))))))
1738 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1739 (when (cond
1740 ((eq mm-verify-option 'never) nil)
1741 ((eq mm-verify-option 'always) t)
1742 ((eq mm-verify-option 'known)
1743 (and func
1744 (or (not (setq functest
1745 (nth 3 (assoc protocol
1746 mm-verify-function-alist))))
1747 (funcall functest parts ctl))))
1748 (t
1749 (y-or-n-p
1750 (format "Verify signed (%s) part? "
1751 (or (nth 2 (assoc protocol mm-verify-function-alist))
1752 (format "protocol=%s" protocol))))))
1753 (save-excursion
1754 (if func
01c52d31 1755 (setq parts (funcall func parts ctl))
23f87bed
MB
1756 (mm-set-handle-multipart-parameter
1757 mm-security-handle 'gnus-details
1758 (format "Unknown sign protocol (%s)" protocol))))))
1759 ((equal subtype "encrypted")
1760 (unless (setq protocol
1761 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1762 ;; The message is broken.
1763 (let ((parts parts))
1764 (while parts
1765 (if (assoc (mm-handle-media-type (car parts))
1766 mm-decrypt-function-alist)
1767 (setq protocol (mm-handle-media-type (car parts))
1768 parts nil)
1769 (setq parts (cdr parts))))))
1770 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1771 (when (cond
1772 ((eq mm-decrypt-option 'never) nil)
1773 ((eq mm-decrypt-option 'always) t)
1774 ((eq mm-decrypt-option 'known)
1775 (and func
1776 (or (not (setq functest
1777 (nth 3 (assoc protocol
1778 mm-decrypt-function-alist))))
1779 (funcall functest parts ctl))))
1780 (t
1781 (y-or-n-p
1782 (format "Decrypt (%s) part? "
1783 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1784 (format "protocol=%s" protocol))))))
1785 (save-excursion
1786 (if func
1787 (setq parts (funcall func parts ctl))
1788 (mm-set-handle-multipart-parameter
1789 mm-security-handle 'gnus-details
1790 (format "Unknown encrypt protocol (%s)" protocol))))))
1791 (t nil))
1792 parts))
1793
1794(defun mm-multiple-handles (handles)
1795 (and (listp handles)
1796 (> (length handles) 1)
1797 (or (listp (car handles))
1798 (stringp (car handles)))))
1799
1800(defun mm-complicated-handles (handles)
1801 (and (listp (car handles))
1802 (> (length handles) 1)))
1803
1804(defun mm-merge-handles (handles1 handles2)
1805 (append
1806 (if (listp (car handles1))
1807 handles1
1808 (list handles1))
1809 (if (listp (car handles2))
1810 handles2
1811 (list handles2))))
1812
1813(defun mm-readable-p (handle)
1814 "Say whether the content of HANDLE is readable."
1815 (and (< (with-current-buffer (mm-handle-buffer handle)
1816 (buffer-size)) 10000)
1817 (mm-with-unibyte-buffer
1818 (mm-insert-part handle)
1819 (and (eq (mm-body-7-or-8) '7bit)
1820 (not (mm-long-lines-p 76))))))
1821
cfadea45
KY
1822(declare-function libxml-parse-html-region "xml.c"
1823 (start end &optional base-url))
1824(declare-function shr-insert-document "shr" (dom))
63698bec 1825(defvar shr-blocked-images)
40de2c6d 1826(defvar gnus-inhibit-images)
e116c1eb 1827(autoload 'gnus-blocked-images "gnus-art")
cfadea45 1828
870409d4 1829(defun mm-shr (handle)
130e977f
LMI
1830 ;; Require since we bind its variables.
1831 (require 'shr)
cfadea45 1832 (let ((article-buffer (current-buffer))
130e977f
LMI
1833 (shr-content-function (lambda (id)
1834 (let ((handle (mm-get-content-id id)))
1835 (when handle
1836 (mm-with-part handle
1837 (buffer-string))))))
c912b478 1838 shr-inhibit-images shr-blocked-images charset char)
b59a9eef 1839 (if (and (boundp 'gnus-summary-buffer)
9937bef4 1840 (bufferp gnus-summary-buffer)
b59a9eef
KY
1841 (buffer-name gnus-summary-buffer))
1842 (with-current-buffer gnus-summary-buffer
1843 (setq shr-inhibit-images gnus-inhibit-images
1844 shr-blocked-images (gnus-blocked-images)))
1845 (setq shr-inhibit-images gnus-inhibit-images
1846 shr-blocked-images (gnus-blocked-images)))
870409d4
G
1847 (unless handle
1848 (setq handle (mm-dissect-buffer t)))
cfadea45 1849 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
a41c2e6d
G
1850 (save-restriction
1851 (narrow-to-region (point) (point))
1852 (shr-insert-document
1853 (mm-with-part handle
c912b478
KY
1854 (insert (prog1
1855 (if (and charset
1856 (setq charset
b50e5112
KY
1857 (mm-charset-to-coding-system charset
1858 nil t))
c912b478
KY
1859 (not (eq charset 'ascii)))
1860 (mm-decode-coding-string (buffer-string) charset)
1861 (mm-string-as-multibyte (buffer-string)))
1862 (erase-buffer)
1863 (mm-enable-multibyte)))
1864 (goto-char (point-min))
1865 (setq case-fold-search t)
1866 (while (re-search-forward
1867 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1868 (when (setq char
1869 (cdr (assq (if (match-beginning 1)
1870 (string-to-number (match-string 1) 16)
1871 (string-to-number (match-string 2)))
1872 mm-extra-numeric-entities)))
1873 (replace-match (char-to-string char))))
a212a18a
LI
1874 ;; Remove "soft hyphens".
1875 (goto-char (point-min))
c38e0c97 1876 (while (search-forward "­" nil t)
a212a18a 1877 (replace-match "" t t))
3444dc61 1878 (libxml-parse-html-region (point-min) (point-max))))
89b163db
G
1879 (unless (bobp)
1880 (insert "\n"))
7304e4dd 1881 (mm-convert-shr-links)
3444dc61
KY
1882 (mm-handle-set-undisplayer
1883 handle
1884 `(lambda ()
1885 (let ((inhibit-read-only t))
1886 (delete-region ,(point-min-marker)
1887 ,(point-max-marker))))))))
870409d4 1888
c220cb68
KY
1889(defvar shr-map)
1890
97989677
GM
1891(autoload 'widget-convert-button "wid-edit")
1892
7304e4dd
LMI
1893(defun mm-convert-shr-links ()
1894 (let ((start (point-min))
1895 end)
1896 (while (and start
1897 (< start (point-max)))
1898 (when (setq start (text-property-not-all start (point-max) 'shr-url nil))
1899 (setq end (next-single-property-change start 'shr-url nil (point-max)))
1900 (widget-convert-button
1901 'url-link start end
1902 :help-echo (get-text-property start 'help-echo)
1903 :keymap shr-map
1904 (get-text-property start 'shr-url))
544d4594 1905 (put-text-property start end 'local-map nil)
7304e4dd
LMI
1906 (setq start end)))))
1907
dc802687
TZ
1908(defun mm-handle-filename (handle)
1909 "Return filename of HANDLE if any."
1910 (or (mail-content-type-get (mm-handle-type handle)
1911 'name)
1912 (mail-content-type-get (mm-handle-disposition handle)
1913 'filename)))
1914
c113de23
GM
1915(provide 'mm-decode)
1916
89b163db 1917;; Local Variables:
c38e0c97 1918;; coding: utf-8
89b163db
G
1919;; End:
1920
c113de23 1921;;; mm-decode.el ends here