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