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