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