1 ;;; mm-decode.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; For Emacs <22.2 and XEmacs.
28 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
32 (eval-when-compile (require 'cl)
35 (autoload 'gnus-map-function "gnus-util")
36 (autoload 'gnus-replace-in-string "gnus-util")
37 (autoload 'gnus-read-shell-command "gnus-util")
39 (autoload 'mm-inline-partial "mm-partial")
40 (autoload 'mm-inline-external-body "mm-extern")
41 (autoload 'mm-extern-cache-contents "mm-extern")
42 (autoload 'mm-insert-inline "mm-view")
44 (autoload 'mm-archive-decoders "mm-archive")
45 (autoload 'mm-archive-dissect-and-inline "mm-archive")
46 (autoload 'mm-dissect-archive "mm-archive")
48 (defvar gnus-current-window-configuration)
50 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
52 (defgroup mime-display ()
53 "Display of MIME in mail and news articles."
54 :link '(custom-manual "(emacs-mime)Display Customization")
60 (defgroup mime-security ()
61 "MIME security in mail and news articles."
62 :link '(custom-manual "(emacs-mime)Display Customization")
67 ;;; Convenience macros.
69 (defmacro mm-handle-buffer (handle)
71 (defmacro mm-handle-type (handle)
73 (defsubst mm-handle-media-type (handle)
74 (if (stringp (car handle))
76 (car (mm-handle-type handle))))
77 (defsubst mm-handle-media-supertype (handle)
78 (car (split-string (mm-handle-media-type handle) "/")))
79 (defsubst mm-handle-media-subtype (handle)
80 (cadr (split-string (mm-handle-media-type handle) "/")))
81 (defmacro mm-handle-encoding (handle)
83 (defmacro mm-handle-undisplayer (handle)
85 (defmacro mm-handle-set-undisplayer (handle function)
86 `(setcar (nthcdr 3 ,handle) ,function))
87 (defmacro mm-handle-disposition (handle)
89 (defmacro mm-handle-description (handle)
91 (defmacro mm-handle-cache (handle)
93 (defmacro mm-handle-set-cache (handle contents)
94 `(setcar (nthcdr 6 ,handle) ,contents))
95 (defmacro mm-handle-id (handle)
97 (defmacro mm-handle-multipart-original-buffer (handle)
98 `(get-text-property 0 'buffer (car ,handle)))
99 (defmacro mm-handle-multipart-from (handle)
100 `(get-text-property 0 'from (car ,handle)))
101 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
102 `(get-text-property 0 ,parameter (car ,handle)))
104 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
105 disposition description cache
107 `(list ,buffer ,type ,encoding ,undisplayer
108 ,disposition ,description ,cache ,id))
110 (defcustom mm-text-html-renderer
111 (cond ((fboundp 'libxml-parse-html-region) 'shr)
112 ((executable-find "w3m") 'gnus-w3m)
113 ((executable-find "links") 'links)
114 ((executable-find "lynx") 'lynx)
115 ((locate-library "w3") 'w3)
116 ((locate-library "html2text") 'html2text)
118 "Render of HTML contents.
119 It is one of defined renderer types, or a rendering function.
120 The defined renderer types are:
121 `shr': use the built-in Gnus HTML renderer;
122 `gnus-w3m': use Gnus renderer based on w3m;
123 `w3m': use emacs-w3m;
124 `w3m-standalone': use plain w3m;
128 `html2text': use html2text;
129 nil : use external viewer (default web browser)."
131 :type '(choice (const shr)
134 (const w3m :tag "emacs-w3m")
135 (const w3m-standalone :tag "standalone w3m" )
139 (const nil :tag "External viewer")
141 :group 'mime-display)
143 (defcustom mm-inline-text-html-with-images nil
144 "If non-nil, Gnus will allow retrieving images in HTML contents with
145 the <img> tags. It has no effect on Emacs/w3. See also the
146 documentation for the `mm-w3m-safe-url-regexp' variable."
149 :group 'mime-display)
151 (defcustom mm-w3m-safe-url-regexp "\\`cid:"
152 "Regexp matching URLs which are considered to be safe.
153 Some HTML mails might contain a nasty trick used by spammers, using
154 the <img> tag which is far more evil than the [Click Here!] button.
155 It is most likely intended to check whether the ominous spam mail has
156 reached your eyes or not, in which case the spammer knows for sure
157 that your email address is valid. It is done by embedding an
158 identifier string into a URL that you might automatically retrieve
159 when displaying the image. The default value is \"\\\\`cid:\" which only
160 matches parts embedded to the Multipart/Related type MIME contents and
161 Gnus will never connect to the spammer's site arbitrarily. You may
162 set this variable to nil if you consider all urls to be safe."
164 :type '(choice (regexp :tag "Regexp")
165 (const :tag "All URLs are safe" nil))
166 :group 'mime-display)
168 (defcustom mm-inline-text-html-with-w3m-keymap t
169 "If non-nil, use emacs-w3m command keys in the article buffer."
172 :group 'mime-display)
174 (defcustom mm-enable-external t
175 "Indicate whether external MIME handlers should be used.
177 If t, all defined external MIME handlers are used. If nil, files are saved by
178 `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
179 before the external MIME handler is invoked."
181 :type '(choice (const :tag "Always" t)
182 (const :tag "Never" nil)
183 (const :tag "Ask" ask))
184 :group 'mime-display)
186 (defcustom mm-inline-media-tests
190 (mm-valid-and-fit-image-p 'jpeg handle)))
194 (mm-valid-and-fit-image-p 'png handle)))
198 (mm-valid-and-fit-image-p 'gif handle)))
202 (mm-valid-and-fit-image-p 'tiff handle)))
206 (mm-valid-and-fit-image-p 'xbm handle)))
210 (mm-valid-and-fit-image-p 'xbm handle)))
214 (mm-valid-and-fit-image-p 'xpm handle)))
218 (mm-valid-and-fit-image-p 'xpm handle)))
222 (mm-valid-and-fit-image-p 'bmp handle)))
223 ("image/x-portable-bitmap"
226 (mm-valid-and-fit-image-p 'pbm handle)))
227 ("text/plain" mm-inline-text identity)
228 ("text/enriched" mm-inline-text identity)
229 ("text/richtext" mm-inline-text identity)
230 ("text/x-patch" mm-display-patch-inline identity)
231 ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
232 ("text/x-diff" mm-display-patch-inline identity)
233 ("application/emacs-lisp" mm-display-elisp-inline identity)
234 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
235 ("application/x-shellscript" mm-display-shell-script-inline identity)
236 ("application/x-sh" mm-display-shell-script-inline identity)
237 ("text/x-sh" mm-display-shell-script-inline identity)
238 ("application/javascript" mm-display-javascript-inline identity)
239 ("text/dns" mm-display-dns-inline identity)
240 ("text/x-org" mm-display-org-inline identity)
244 mm-text-html-renderer))
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)
253 ("message/external-body" mm-inline-external-body identity)
254 ("text/.*" mm-inline-text identity)
255 ("application/x-.?tar\\(-.*\\)?" mm-archive-dissect-and-inline identity)
256 ("application/zip" mm-archive-dissect-and-inline identity)
257 ("audio/wav" mm-inline-audio
259 (and (or (featurep 'nas-sound) (featurep 'native-sound))
260 (device-sound-enabled-p))))
264 (and (or (featurep 'nas-sound) (featurep 'native-sound))
265 (device-sound-enabled-p))))
266 ("application/pgp-signature" ignore identity)
267 ("application/x-pkcs7-signature" ignore identity)
268 ("application/pkcs7-signature" ignore identity)
269 ("application/x-pkcs7-mime" ignore identity)
270 ("application/pkcs7-mime" ignore identity)
271 ("multipart/alternative" ignore identity)
272 ("multipart/mixed" ignore identity)
273 ("multipart/related" ignore identity)
277 (and (mm-valid-image-format-p 'imagemagick)
278 (mm-with-unibyte-buffer
279 (mm-insert-part handle)
282 (if (fboundp 'create-image)
283 (create-image (buffer-string) 'imagemagick 'data-p)
284 (mm-create-image-xemacs
285 (mm-handle-media-subtype handle))))))
287 (setcar (cdr handle) (list "image/imagemagick"))
288 (mm-image-fit-p handle)))))))
289 ;; Disable audio and image
290 ("audio/.*" ignore ignore)
291 ("image/.*" ignore ignore)
292 ;; Default to displaying as text
293 (".*" mm-inline-text mm-readable-p))
294 "Alist of media types/tests saying whether types can be displayed inline."
295 :type '(repeat (list (regexp :tag "MIME type")
296 (function :tag "Display function")
297 (function :tag "Display test")))
298 :group 'mime-display)
300 (defcustom mm-inlined-types
301 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
302 "message/partial" "message/external-body" "application/emacs-lisp"
303 "application/x-emacs-lisp"
304 "application/pgp-signature" "application/x-pkcs7-signature"
305 "application/pkcs7-signature" "application/x-pkcs7-mime"
306 "application/pkcs7-mime"
307 "application/x-gtar-compressed"
310 ;; Mutt still uses this even though it has already been withdrawn.
312 "List of media types that are to be displayed inline.
313 See also `mm-inline-media-tests', which says how to display a media
315 :type '(repeat regexp)
316 :group 'mime-display)
318 (defcustom mm-keep-viewer-alive-types
319 '("application/postscript" "application/msword" "application/vnd.ms-excel"
320 "application/pdf" "application/x-dvi")
321 "List of media types for which the external viewer will not be killed
322 when selecting a different article."
324 :type '(repeat regexp)
325 :group 'mime-display)
327 (defcustom mm-automatic-display
328 '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
329 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
330 "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
331 "application/emacs-lisp" "application/x-emacs-lisp"
332 "application/x-pkcs7-signature"
333 "application/pkcs7-signature" "application/x-pkcs7-mime"
334 "application/pkcs7-mime"
335 ;; Mutt still uses this even though it has already been withdrawn.
338 "A list of MIME types to be displayed automatically."
339 :type '(repeat regexp)
340 :group 'mime-display)
342 (defcustom mm-attachment-override-types '("text/x-vcard"
343 "application/pkcs7-mime"
344 "application/x-pkcs7-mime"
345 "application/pkcs7-signature"
346 "application/x-pkcs7-signature")
347 "Types to have \"attachment\" ignored if they can be displayed inline."
348 :type '(repeat regexp)
349 :group 'mime-display)
351 (defcustom mm-inline-override-types nil
352 "Types to be treated as attachments even if they can be displayed inline."
353 :type '(repeat regexp)
354 :group 'mime-display)
356 (defcustom mm-automatic-external-display nil
357 "List of MIME type regexps that will be displayed externally automatically."
358 :type '(repeat regexp)
359 :group 'mime-display)
361 (defcustom mm-discouraged-alternatives nil
362 "List of MIME types that are discouraged when viewing multipart/alternative.
363 Viewing agents are supposed to view the last possible part of a message,
364 as that is supposed to be the richest. However, users may prefer other
365 types instead, and this list says what types are most unwanted. If,
366 for instance, text/html parts are very unwanted, and text/richtext are
367 somewhat unwanted, then the value of this variable should be set
370 (\"text/html\" \"text/richtext\")
372 Adding \"image/.*\" might also be useful. Spammers use it as the
373 preferred part of multipart/alternative messages. See also
374 `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
375 enables you to choose manually one of two types those mails include."
376 :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
377 :group 'mime-display)
379 (defcustom mm-tmp-directory
380 (if (fboundp 'temp-directory)
382 (if (boundp 'temporary-file-directory)
383 temporary-file-directory
385 "Where mm will store its temporary files."
387 :group 'mime-display)
389 (defcustom mm-inline-large-images nil
390 "If t, then all images fit in the buffer.
391 If 'resize, try to resize the images so they fit."
393 (const :tag "Inline large images as they are." t)
394 (const :tag "Resize large images." resize)
395 (const :tag "Do not inline large images." nil))
396 :group 'mime-display)
398 (defcustom mm-file-name-rewrite-functions
399 '(mm-file-name-delete-control mm-file-name-delete-gotchas)
400 "List of functions used for rewriting file names of MIME parts.
401 Each function takes a file name as input and returns a file name.
403 Ready-made functions include `mm-file-name-delete-control',
404 `mm-file-name-delete-gotchas' (you should not remove these two
405 functions), `mm-file-name-delete-whitespace',
406 `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
407 `mm-file-name-replace-whitespace', `capitalize', `downcase',
408 `upcase', and `upcase-initials'."
409 :type '(list (set :inline t
410 (const mm-file-name-delete-control)
411 (const mm-file-name-delete-gotchas)
412 (const mm-file-name-delete-whitespace)
413 (const mm-file-name-trim-whitespace)
414 (const mm-file-name-collapse-whitespace)
415 (const mm-file-name-replace-whitespace)
419 (const upcase-initials)
423 :version "23.1" ;; No Gnus
424 :group 'mime-display)
427 (defvar mm-path-name-rewrite-functions nil
428 "*List of functions for rewriting the full file names of MIME parts.
429 This is used when viewing parts externally, and is meant for
430 transforming the absolute name so that non-compliant programs can find
431 the file where it's saved.
433 Each function takes a file name as input and returns a file name.")
435 (defvar mm-file-name-replace-whitespace nil
436 "String used for replacing whitespace characters; default is `\"_\"'.")
438 (defcustom mm-default-directory nil
439 "The default directory where mm will save files.
440 If not set, `default-directory' will be used."
441 :type '(choice directory (const :tag "Default" nil))
442 :group 'mime-display)
444 (defcustom mm-attachment-file-modes 384
445 "Set the mode bits of saved attachments to this integer."
448 :group 'mime-display)
450 (defcustom mm-external-terminal-program "xterm"
451 "The program to start an external terminal."
454 :group 'mime-display)
456 ;;; Internal variables.
458 (defvar mm-last-shell-command "")
459 (defvar mm-content-id-alist nil)