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