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