Fix marker usage in align-areas.
[emacs.git] / lisp / gnus / mm-decode.el
blob7982b745d6600bc2e95ceea84255ab6b60fdf900
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/>.
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)
33 (require 'term))
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")
55 :version "21.1"
56 :group 'mail
57 :group 'news
58 :group 'multimedia)
60 (defgroup mime-security ()
61 "MIME security in mail and news articles."
62 :link '(custom-manual "(emacs-mime)Display Customization")
63 :group 'mail
64 :group 'news
65 :group 'multimedia)
67 ;;; Convenience macros.
69 (defmacro mm-handle-buffer (handle)
70 `(nth 0 ,handle))
71 (defmacro mm-handle-type (handle)
72 `(nth 1 ,handle))
73 (defsubst mm-handle-media-type (handle)
74 (if (stringp (car handle))
75 (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)
82 `(nth 2 ,handle))
83 (defmacro mm-handle-undisplayer (handle)
84 `(nth 3 ,handle))
85 (defmacro mm-handle-set-undisplayer (handle function)
86 `(setcar (nthcdr 3 ,handle) ,function))
87 (defmacro mm-handle-disposition (handle)
88 `(nth 4 ,handle))
89 (defmacro mm-handle-description (handle)
90 `(nth 5 ,handle))
91 (defmacro mm-handle-cache (handle)
92 `(nth 6 ,handle))
93 (defmacro mm-handle-set-cache (handle contents)
94 `(setcar (nthcdr 6 ,handle) ,contents))
95 (defmacro mm-handle-id (handle)
96 `(nth 7 ,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)
117 (t nil))
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;
125 `links': use links;
126 `lynx': use lynx;
127 `w3': use Emacs/W3;
128 `html2text': use html2text;
129 nil : use external viewer (default web browser)."
130 :version "24.1"
131 :type '(choice (const shr)
132 (const gnus-w3m)
133 (const w3)
134 (const w3m :tag "emacs-w3m")
135 (const w3m-standalone :tag "standalone w3m" )
136 (const links)
137 (const lynx)
138 (const html2text)
139 (const nil :tag "External viewer")
140 (function))
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."
147 :version "22.1"
148 :type 'boolean
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."
163 :version "22.1"
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."
170 :version "22.1"
171 :type 'boolean
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."
180 :version "22.1"
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
187 '(("image/p?jpeg"
188 mm-inline-image
189 (lambda (handle)
190 (mm-valid-and-fit-image-p 'jpeg handle)))
191 ("image/png"
192 mm-inline-image
193 (lambda (handle)
194 (mm-valid-and-fit-image-p 'png handle)))
195 ("image/gif"
196 mm-inline-image
197 (lambda (handle)
198 (mm-valid-and-fit-image-p 'gif handle)))
199 ("image/tiff"
200 mm-inline-image
201 (lambda (handle)
202 (mm-valid-and-fit-image-p 'tiff handle)))
203 ("image/xbm"
204 mm-inline-image
205 (lambda (handle)
206 (mm-valid-and-fit-image-p 'xbm handle)))
207 ("image/x-xbitmap"
208 mm-inline-image
209 (lambda (handle)
210 (mm-valid-and-fit-image-p 'xbm handle)))
211 ("image/xpm"
212 mm-inline-image
213 (lambda (handle)
214 (mm-valid-and-fit-image-p 'xpm handle)))
215 ("image/x-xpixmap"
216 mm-inline-image
217 (lambda (handle)
218 (mm-valid-and-fit-image-p 'xpm handle)))
219 ("image/bmp"
220 mm-inline-image
221 (lambda (handle)
222 (mm-valid-and-fit-image-p 'bmp handle)))
223 ("image/x-portable-bitmap"
224 mm-inline-image
225 (lambda (handle)
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)
241 ("text/html"
242 mm-inline-text-html
243 (lambda (handle)
244 mm-text-html-renderer))
245 ("text/x-vcard"
246 mm-inline-text-vcard
247 (lambda (handle)
248 (or (featurep 'vcard)
249 (locate-library "vcard"))))
250 ("message/delivery-status" mm-inline-text identity)
251 ("message/rfc822" mm-inline-message identity)
252 ("message/partial" mm-inline-partial identity)
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
258 (lambda (handle)
259 (and (or (featurep 'nas-sound) (featurep 'native-sound))
260 (device-sound-enabled-p))))
261 ("audio/au"
262 mm-inline-audio
263 (lambda (handle)
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)
274 ("image/.*"
275 mm-inline-image
276 (lambda (handle)
277 (and (mm-valid-image-format-p 'imagemagick)
278 (mm-with-unibyte-buffer
279 (mm-insert-part handle)
280 (let ((image
281 (ignore-errors
282 (if (fboundp 'create-image)
283 (create-image (buffer-string) 'imagemagick 'data-p)
284 (mm-create-image-xemacs
285 (mm-handle-media-subtype handle))))))
286 (when image
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"
308 "application/x-tar"
309 "application/zip"
310 ;; Mutt still uses this even though it has already been withdrawn.
311 "application/pgp")
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
314 type inline."
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."
323 :version "22.1"
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.
336 "application/pgp\\'"
337 "text/x-org")
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)
381 (temp-directory)
382 (if (boundp 'temporary-file-directory)
383 temporary-file-directory
384 "/tmp/"))
385 "Where mm will store its temporary files."
386 :type 'directory
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."
392 :type '(radio
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)
416 (const capitalize)
417 (const downcase)
418 (const upcase)
419 (const upcase-initials)
420 (repeat :inline t
421 :tag "Function"
422 function)))
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."
446 :version "22.1"
447 :type 'integer
448 :group 'mime-display)
450 (defcustom mm-external-terminal-program "xterm"
451 "The program to start an external terminal."
452 :version "22.1"
453 :type 'string
454 :group 'mime-display)
456 ;;; Internal variables.
458 (defvar mm-last-shell-command "")
459 (defvar mm-content-id-alist nil)
460 (defvar mm-postponed-undisplay-list nil)
461 (defvar mm-inhibit-auto-detect-attachment nil)
463 ;; According to RFC2046, in particular, in a digest, the default
464 ;; Content-Type value for a body part is changed from "text/plain" to
465 ;; "message/rfc822".
466 (defvar mm-dissect-default-type "text/plain")
468 (autoload 'mml2015-verify "mml2015")
469 (autoload 'mml2015-verify-test "mml2015")
470 (autoload 'mml-smime-verify "mml-smime")
471 (autoload 'mml-smime-verify-test "mml-smime")
473 (defvar mm-verify-function-alist
474 '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
475 ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
476 mm-uu-pgp-signed-test)
477 ("application/pkcs7-signature" mml-smime-verify "S/MIME"
478 mml-smime-verify-test)
479 ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
480 mml-smime-verify-test)))
482 (defcustom mm-verify-option 'never
483 "Option of verifying signed parts.
484 `never', not verify; `always', always verify;
485 `known', only verify known protocols. Otherwise, ask user.
487 When set to `always' or `known', you should add
488 \"multipart/signed\" to `gnus-buttonized-mime-types' to see
489 result of the verification."
490 :version "22.1"
491 :type '(choice (item always)
492 (item never)
493 (item :tag "only known protocols" known)
494 (item :tag "ask" nil))
495 :group 'mime-security)
497 (autoload 'mml2015-decrypt "mml2015")
498 (autoload 'mml2015-decrypt-test "mml2015")
500 (defvar mm-decrypt-function-alist
501 '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
502 ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
503 mm-uu-pgp-encrypted-test)))
505 (defcustom mm-decrypt-option nil
506 "Option of decrypting encrypted parts.
507 `never', not decrypt; `always', always decrypt;
508 `known', only decrypt known protocols. Otherwise, ask user."
509 :version "22.1"
510 :type '(choice (item always)
511 (item never)
512 (item :tag "only known protocols" known)
513 (item :tag "ask" nil))
514 :group 'mime-security)
516 (defvar mm-viewer-completion-map
517 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
518 (set-keymap-parent map minibuffer-local-completion-map)
519 ;; Should we bind other key to minibuffer-complete-word?
520 (define-key map " " 'self-insert-command)
521 map)
522 "Keymap for input viewer with completion.")
524 (defvar mm-viewer-completion-map
525 (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
526 (set-keymap-parent map minibuffer-local-completion-map)
527 ;; Should we bind other key to minibuffer-complete-word?
528 (define-key map " " 'self-insert-command)
529 map)
530 "Keymap for input viewer with completion.")
532 ;;; The functions.
534 (defun mm-alist-to-plist (alist)
535 "Convert association list ALIST into the equivalent property-list form.
536 The plist is returned. This converts from
538 \((a . 1) (b . 2) (c . 3))
540 into
542 \(a 1 b 2 c 3)
544 The original alist is not modified. See also `destructive-alist-to-plist'."
545 (let (plist)
546 (while alist
547 (let ((el (car alist)))
548 (setq plist (cons (cdr el) (cons (car el) plist))))
549 (setq alist (cdr alist)))
550 (nreverse plist)))
552 (defun mm-keep-viewer-alive-p (handle)
553 "Say whether external viewer for HANDLE should stay alive."
554 (let ((types mm-keep-viewer-alive-types)
555 (type (mm-handle-media-type handle))
557 (catch 'found
558 (while (setq ty (pop types))
559 (when (string-match ty type)
560 (throw 'found t))))))
562 (defun mm-handle-set-external-undisplayer (handle function)
563 "Set the undisplayer for HANDLE to FUNCTION.
564 Postpone undisplaying of viewers for types in
565 `mm-keep-viewer-alive-types'."
566 (if (mm-keep-viewer-alive-p handle)
567 (let ((new-handle (copy-sequence handle)))
568 (mm-handle-set-undisplayer new-handle function)
569 (mm-handle-set-undisplayer handle nil)
570 (push new-handle mm-postponed-undisplay-list))
571 (mm-handle-set-undisplayer handle function)))
573 (defun mm-destroy-postponed-undisplay-list ()
574 (when mm-postponed-undisplay-list
575 (message "Destroying external MIME viewers")
576 (mm-destroy-parts mm-postponed-undisplay-list)))
578 (autoload 'message-fetch-field "message")
580 (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
581 "Dissect the current buffer and return a list of MIME handles.
582 If NO-STRICT-MIME, don't require the message to have a
583 MIME-Version header before proceeding."
584 (save-excursion
585 (let (ct ctl type subtype cte cd description id result)
586 (save-restriction
587 (mail-narrow-to-head)
588 (when (or no-strict-mime
589 loose-mime
590 (mail-fetch-field "mime-version"))
591 (setq ct (mail-fetch-field "content-type")
592 ctl (and ct (mail-header-parse-content-type ct))
593 cte (mail-fetch-field "content-transfer-encoding")
594 cd (or (mail-fetch-field "content-disposition")
595 (when (and ctl
596 (eq 'mm-inline-text
597 (cadr (mm-assoc-string-match
598 mm-inline-media-tests
599 (car ctl)))))
600 "inline"))
601 ;; Newlines in description should be stripped so as
602 ;; not to break the MIME tag into two or more lines.
603 description (message-fetch-field "content-description")
604 id (mail-fetch-field "content-id"))
605 (unless from
606 (setq from (mail-fetch-field "from")))
607 ;; FIXME: In some circumstances, this code is running within
608 ;; an unibyte macro. mail-extract-address-components
609 ;; creates unibyte buffers. This `if', though not a perfect
610 ;; solution, avoids most of them.
611 (if from
612 (setq from (cadr (mail-extract-address-components from))))
613 (if description
614 (setq description (mail-decode-encoded-word-string
615 description)))))
616 (if (or (not ctl)
617 (not (string-match "/" (car ctl))))
618 (mm-dissect-singlepart
619 (list mm-dissect-default-type)
620 (and cte (intern (downcase (mail-header-strip cte))))
621 no-strict-mime
622 (and cd (mail-header-parse-content-disposition cd))
623 description)
624 (setq type (split-string (car ctl) "/"))
625 (setq subtype (cadr type)
626 type (car type))
627 (setq
628 result
629 (cond
630 ((equal type "multipart")
631 (let ((mm-dissect-default-type (if (equal subtype "digest")
632 "message/rfc822"
633 "text/plain"))
634 (start (cdr (assq 'start (cdr ctl)))))
635 (add-text-properties 0 (length (car ctl))
636 (mm-alist-to-plist (cdr ctl)) (car ctl))
638 ;; what really needs to be done here is a way to link a
639 ;; MIME handle back to it's parent MIME handle (in a multilevel
640 ;; MIME article). That would probably require changing
641 ;; the mm-handle API so we simply store the multipart buffer
642 ;; name as a text property of the "multipart/whatever" string.
643 (add-text-properties 0 (length (car ctl))
644 (list 'buffer (mm-copy-to-buffer)
645 'from from
646 'start start)
647 (car ctl))
648 (cons (car ctl) (mm-dissect-multipart ctl from))))
650 (mm-possibly-verify-or-decrypt
651 (mm-dissect-singlepart
653 (and cte (intern (downcase (mail-header-strip cte))))
654 no-strict-mime
655 (and cd (mail-header-parse-content-disposition cd))
656 description id)
657 ctl from))))
658 (when id
659 (when (string-match " *<\\(.*\\)> *" id)
660 (setq id (match-string 1 id)))
661 (push (cons id result) mm-content-id-alist))
662 result))))
664 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
665 (when (or force
666 (if (equal "text/plain" (car ctl))
667 (assoc 'format ctl)
669 ;; Guess what the type of application/octet-stream parts should
670 ;; really be.
671 (let ((filename (cdr (assq 'filename (cdr cdl)))))
672 (when (and (not mm-inhibit-auto-detect-attachment)
673 (equal (car ctl) "application/octet-stream")
674 filename
675 (string-match "\\.\\([^.]+\\)$" filename))
676 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
677 (when new-type
678 (setcar ctl new-type)))))
679 (let ((handle
680 (mm-make-handle
681 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
682 (decoder (assoc (car ctl) (mm-archive-decoders))))
683 (if (and decoder
684 ;; Do automatic decoding
685 (cadr decoder)
686 (executable-find (caddr decoder)))
687 (mm-dissect-archive handle)
688 handle))))
690 (defun mm-dissect-multipart (ctl from)
691 (goto-char (point-min))
692 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
693 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
694 start parts
695 (end (save-excursion
696 (goto-char (point-max))
697 (if (re-search-backward close-delimiter nil t)
698 (match-beginning 0)
699 (point-max))))
700 (mm-inhibit-auto-detect-attachment
701 (equal (car ctl) "multipart/encrypted")))
702 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
703 (while (and (< (point) end) (re-search-forward boundary end t))
704 (goto-char (match-beginning 0))
705 (when start
706 (save-excursion
707 (save-restriction
708 (narrow-to-region start (point))
709 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
710 (end-of-line 2)
711 (or (looking-at boundary)
712 (forward-line 1))
713 (setq start (point)))
714 (when (and start (< start end))
715 (save-excursion
716 (save-restriction
717 (narrow-to-region start end)
718 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
719 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
721 (defun mm-copy-to-buffer ()
722 "Copy the contents of the current buffer to a fresh buffer."
723 (let ((obuf (current-buffer))
724 (mb (mm-multibyte-p))
725 beg)
726 (goto-char (point-min))
727 (search-forward-regexp "^\n" nil t)
728 (setq beg (point))
729 (with-current-buffer
730 (generate-new-buffer " *mm*")
731 ;; Preserve the data's unibyteness (for url-insert-file-contents).
732 (mm-set-buffer-multibyte mb)
733 (insert-buffer-substring obuf beg)
734 (current-buffer))))
736 (defun mm-display-parts (handle &optional no-default)
737 (if (stringp (car handle))
738 (mapcar 'mm-display-parts (cdr handle))
739 (if (bufferp (car handle))
740 (save-restriction
741 (narrow-to-region (point) (point))
742 (mm-display-part handle)
743 (goto-char (point-max)))
744 (mapcar 'mm-display-parts handle))))
746 (autoload 'mailcap-parse-mailcaps "mailcap")
747 (autoload 'mailcap-mime-info "mailcap")
749 (defun mm-display-part (handle &optional no-default force)
750 "Display the MIME part represented by HANDLE.
751 Returns nil if the part is removed; inline if displayed inline;
752 external if displayed external."
753 (save-excursion
754 (mailcap-parse-mailcaps)
755 (if (and (not force)
756 (mm-handle-displayed-p handle))
757 (mm-remove-part handle)
758 (let* ((ehandle (if (equal (mm-handle-media-type handle)
759 "message/external-body")
760 (progn
761 (unless (mm-handle-cache handle)
762 (mm-extern-cache-contents handle))
763 (mm-handle-cache handle))
764 handle))
765 (type (mm-handle-media-type ehandle))
766 (method (mailcap-mime-info type))
767 (filename (or (mail-content-type-get
768 (mm-handle-disposition handle) 'filename)
769 (mail-content-type-get
770 (mm-handle-type handle) 'name)
771 "<file>"))
772 (external mm-enable-external)
773 (decoder (assoc (car (mm-handle-type handle))
774 (mm-archive-decoders))))
775 (cond
776 ((and decoder
777 (executable-find (caddr decoder)))
778 (mm-archive-dissect-and-inline handle)
779 'inline)
780 ((and (mm-inlinable-p ehandle)
781 (mm-inlined-p ehandle))
782 (forward-line 1)
783 (mm-display-inline handle)
784 'inline)
785 ((or method
786 (not no-default))
787 (if (and (not method)
788 (equal "text" (car (split-string type "/"))))
789 (progn
790 (forward-line 1)
791 (mm-insert-inline handle (mm-get-part handle))
792 'inline)
793 (setq external
794 (and method ;; If nil, we always use "save".
795 (stringp method) ;; 'mailcap-save-binary-file
796 (or (eq mm-enable-external t)
797 (and (eq mm-enable-external 'ask)
798 (y-or-n-p
799 (concat
800 "Display part (" type
801 ") using external program"
802 ;; Can non-string method ever happen?
803 (if (stringp method)
804 (concat
805 " \"" (format method filename) "\"")
807 "? "))))))
808 (if external
809 (mm-display-external
810 handle (or method 'mailcap-save-binary-file))
811 (mm-display-external
812 handle 'mailcap-save-binary-file)))))))))
814 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
815 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
817 (defun mm-display-external (handle method)
818 "Display HANDLE using METHOD."
819 (let ((outbuf (current-buffer)))
820 (mm-with-unibyte-buffer
821 (if (functionp method)
822 (let ((cur (current-buffer)))
823 (if (eq method 'mailcap-save-binary-file)
824 (progn
825 (set-buffer (generate-new-buffer " *mm*"))
826 (setq method nil))
827 (mm-insert-part handle)
828 (mm-add-meta-html-tag handle)
829 (let ((win (get-buffer-window cur t)))
830 (when win
831 (select-window win)))
832 (switch-to-buffer (generate-new-buffer " *mm*")))
833 (buffer-disable-undo)
834 (mm-set-buffer-file-coding-system mm-binary-coding-system)
835 (insert-buffer-substring cur)
836 (goto-char (point-min))
837 (when method
838 (message "Viewing with %s" method))
839 (let ((mm (current-buffer))
840 (non-viewer (assq 'non-viewer
841 (mailcap-mime-info
842 (mm-handle-media-type handle) t))))
843 (unwind-protect
844 (if method
845 (funcall method)
846 (mm-save-part handle))
847 (when (and (not non-viewer)
848 method)
849 (mm-handle-set-undisplayer handle mm)))))
850 ;; The function is a string to be executed.
851 (mm-insert-part handle)
852 (mm-add-meta-html-tag handle)
853 (let* ((dir (mm-make-temp-file
854 (expand-file-name "emm." mm-tmp-directory) 'dir))
855 (filename (or
856 (mail-content-type-get
857 (mm-handle-disposition handle) 'filename)
858 (mail-content-type-get
859 (mm-handle-type handle) 'name)))
860 (mime-info (mailcap-mime-info
861 (mm-handle-media-type handle) t))
862 (needsterm (or (assoc "needsterm" mime-info)
863 (assoc "needsterminal" mime-info)))
864 (copiousoutput (assoc "copiousoutput" mime-info))
865 file buffer)
866 ;; We create a private sub-directory where we store our files.
867 (set-file-modes dir #o700)
868 (if filename
869 (setq file (expand-file-name
870 (gnus-map-function mm-file-name-rewrite-functions
871 (file-name-nondirectory filename))
872 dir))
873 ;; Use nametemplate (defined in RFC1524) if it is specified
874 ;; in mailcap.
875 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
876 (if (and suffix
877 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
878 (setq suffix (match-string 1 suffix))
879 ;; Otherwise, use a suffix according to
880 ;; `mailcap-mime-extensions'.
881 (setq suffix (car (rassoc (mm-handle-media-type handle)
882 mailcap-mime-extensions))))
883 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
884 nil suffix))))
885 (let ((coding-system-for-write mm-binary-coding-system))
886 (write-region (point-min) (point-max) file nil 'nomesg))
887 ;; The file is deleted after the viewer exists. If the users edits
888 ;; the file, changes will be lost. Set file to read-only to make it
889 ;; clear.
890 (set-file-modes file #o400)
891 (message "Viewing with %s" method)
892 (cond
893 (needsterm
894 (let ((command (mm-mailcap-command
895 method file (mm-handle-type handle))))
896 (unwind-protect
897 (if window-system
898 (start-process "*display*" nil
899 mm-external-terminal-program
900 "-e" shell-file-name
901 shell-command-switch command)
902 (require 'term)
903 (require 'gnus-win)
904 (set-buffer
905 (setq buffer
906 (make-term "display"
907 shell-file-name
909 shell-command-switch command)))
910 (term-mode)
911 (term-char-mode)
912 (set-process-sentinel
913 (get-buffer-process buffer)
914 `(lambda (process state)
915 (if (eq 'exit (process-status process))
916 (gnus-configure-windows
917 ',gnus-current-window-configuration))))
918 (gnus-configure-windows 'display-term))
919 (mm-handle-set-external-undisplayer handle (cons file buffer)))
920 (message "Displaying %s..." command))
921 'external)
922 (copiousoutput
923 (with-current-buffer outbuf
924 (forward-line 1)
925 (mm-insert-inline
926 handle
927 (unwind-protect
928 (progn
929 (call-process shell-file-name nil
930 (setq buffer
931 (generate-new-buffer " *mm*"))
933 shell-command-switch
934 (mm-mailcap-command
935 method file (mm-handle-type handle)))
936 (if (buffer-live-p buffer)
937 (with-current-buffer buffer
938 (buffer-string))))
939 (progn
940 (ignore-errors (delete-file file))
941 (ignore-errors (delete-directory
942 (file-name-directory file)))
943 (ignore-errors (kill-buffer buffer))))))
944 'inline)
946 ;; Deleting the temp file should be postponed for some wrappers,
947 ;; shell scripts, and so on, which might exit right after having
948 ;; started a viewer command as a background job.
949 (let ((command (mm-mailcap-command
950 method file (mm-handle-type handle))))
951 (unwind-protect
952 (progn
953 (start-process "*display*"
954 (setq buffer
955 (generate-new-buffer " *mm*"))
956 shell-file-name
957 shell-command-switch command)
958 (set-process-sentinel
959 (get-buffer-process buffer)
960 (lexical-let ((outbuf outbuf)
961 (file file)
962 (buffer buffer)
963 (command command)
964 (handle handle))
965 (run-at-time
966 30.0 nil
967 (lambda ()
968 (ignore-errors
969 (delete-file file))
970 (ignore-errors
971 (delete-directory (file-name-directory file)))))
972 (lambda (process state)
973 (when (eq (process-status process) 'exit)
974 (condition-case nil
975 (delete-file file)
976 (error))
977 (condition-case nil
978 (delete-directory (file-name-directory file))
979 (error))
980 (when (buffer-live-p outbuf)
981 (with-current-buffer outbuf
982 (let ((buffer-read-only nil)
983 (point (point)))
984 (forward-line 2)
985 (mm-insert-inline
986 handle (with-current-buffer buffer
987 (buffer-string)))
988 (goto-char point))))
989 (when (buffer-live-p buffer)
990 (kill-buffer buffer)))
991 (message "Displaying %s...done" command)))))
992 (mm-handle-set-external-undisplayer
993 handle (cons file buffer)))
994 (message "Displaying %s..." command))
995 'external)))))))
997 (defun mm-mailcap-command (method file type-list)
998 (let ((ctl (cdr type-list))
999 (beg 0)
1000 (uses-stdin t)
1001 out sub total)
1002 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
1003 method beg)
1004 (push (substring method beg (match-beginning 0)) out)
1005 (setq beg (match-end 0)
1006 total (match-string 0 method)
1007 sub (match-string 1 method))
1008 (cond
1009 ((string= total "%%")
1010 (push "%" out))
1011 ((or (string= total "%s")
1012 ;; We do our own quoting.
1013 (string= total "'%s'")
1014 (string= total "\"%s\""))
1015 (setq uses-stdin nil)
1016 (push (shell-quote-argument
1017 (gnus-map-function mm-path-name-rewrite-functions file)) out))
1018 ((string= total "%t")
1019 (push (shell-quote-argument (car type-list)) out))
1021 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
1022 (push (substring method beg (length method)) out)
1023 (when uses-stdin
1024 (push "<" out)
1025 (push (shell-quote-argument
1026 (gnus-map-function mm-path-name-rewrite-functions file))
1027 out))
1028 (mapconcat 'identity (nreverse out) "")))
1030 (defun mm-remove-parts (handles)
1031 "Remove the displayed MIME parts represented by HANDLES."
1032 (if (and (listp handles)
1033 (bufferp (car handles)))
1034 (mm-remove-part handles)
1035 (let (handle)
1036 (while (setq handle (pop handles))
1037 (cond
1038 ((stringp handle)
1039 (when (buffer-live-p (get-text-property 0 'buffer handle))
1040 (kill-buffer (get-text-property 0 'buffer handle))))
1041 ((and (listp handle)
1042 (stringp (car handle)))
1043 (mm-remove-parts (cdr handle)))
1045 (mm-remove-part handle)))))))
1047 (defun mm-destroy-parts (handles)
1048 "Remove the displayed MIME parts represented by HANDLES."
1049 (if (and (listp handles)
1050 (bufferp (car handles)))
1051 (mm-destroy-part handles)
1052 (let (handle)
1053 (while (setq handle (pop handles))
1054 (cond
1055 ((stringp handle)
1056 (when (buffer-live-p (get-text-property 0 'buffer handle))
1057 (kill-buffer (get-text-property 0 'buffer handle))))
1058 ((and (listp handle)
1059 (stringp (car handle)))
1060 (mm-destroy-parts handle))
1062 (mm-destroy-part handle)))))))
1064 (defun mm-remove-part (handle)
1065 "Remove the displayed MIME part represented by HANDLE."
1066 (when (listp handle)
1067 (let ((object (mm-handle-undisplayer handle)))
1068 (ignore-errors
1069 (cond
1070 ;; Internally displayed part.
1071 ((mm-annotationp object)
1072 (if (featurep 'xemacs)
1073 (delete-annotation object)))
1074 ((or (functionp object)
1075 (and (listp object)
1076 (eq (car object) 'lambda)))
1077 (funcall object))
1078 ;; Externally displayed part.
1079 ((consp object)
1080 (condition-case ()
1081 (while (get-buffer-process (cdr object))
1082 (interrupt-process (get-buffer-process (cdr object)))
1083 (message "Waiting for external displayer to die...")
1084 (sit-for 1))
1085 (quit)
1086 (error))
1087 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1088 (message "Waiting for external displayer to die...done")
1089 (ignore-errors (delete-file (car object)))
1090 (ignore-errors (delete-directory (file-name-directory
1091 (car object)))))
1092 ((bufferp object)
1093 (when (buffer-live-p object)
1094 (kill-buffer object)))))
1095 (mm-handle-set-undisplayer handle nil))))
1097 (defun mm-display-inline (handle)
1098 (let* ((type (mm-handle-media-type handle))
1099 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1100 (funcall function handle)
1101 (goto-char (point-min))))
1103 (defun mm-assoc-string-match (alist type)
1104 (dolist (elem alist)
1105 (when (string-match (car elem) type)
1106 (return elem))))
1108 (defun mm-automatic-display-p (handle)
1109 "Say whether the user wants HANDLE to be displayed automatically."
1110 (let ((methods mm-automatic-display)
1111 (type (mm-handle-media-type handle))
1112 method result)
1113 (while (setq method (pop methods))
1114 (when (and (not (mm-inline-override-p handle))
1115 (string-match method type))
1116 (setq result t
1117 methods nil)))
1118 result))
1120 (defun mm-inlinable-p (handle &optional type)
1121 "Say whether HANDLE can be displayed inline.
1122 TYPE is the mime-type of the object; it defaults to the one given
1123 in HANDLE."
1124 (unless type (setq type (mm-handle-media-type handle)))
1125 (let ((alist mm-inline-media-tests)
1126 test)
1127 (while alist
1128 (when (string-match (caar alist) type)
1129 (setq test (caddar alist)
1130 alist nil)
1131 (setq test (funcall test handle)))
1132 (pop alist))
1133 test))
1135 (defun mm-inlined-p (handle)
1136 "Say whether the user wants HANDLE to be displayed inline."
1137 (let ((methods mm-inlined-types)
1138 (type (mm-handle-media-type handle))
1139 method result)
1140 (while (setq method (pop methods))
1141 (when (and (not (mm-inline-override-p handle))
1142 (string-match method type))
1143 (setq result t
1144 methods nil)))
1145 result))
1147 (defun mm-attachment-override-p (handle)
1148 "Say whether HANDLE should have attachment behavior overridden."
1149 (let ((types mm-attachment-override-types)
1150 (type (mm-handle-media-type handle))
1152 (catch 'found
1153 (while (setq ty (pop types))
1154 (when (and (string-match ty type)
1155 (mm-inlinable-p handle))
1156 (throw 'found t))))))
1158 (defun mm-inline-override-p (handle)
1159 "Say whether HANDLE should have inline behavior overridden."
1160 (let ((types mm-inline-override-types)
1161 (type (mm-handle-media-type handle))
1163 (catch 'found
1164 (while (setq ty (pop types))
1165 (when (string-match ty type)
1166 (throw 'found t))))))
1168 (defun mm-automatic-external-display-p (type)
1169 "Return the user-defined method for TYPE."
1170 (let ((methods mm-automatic-external-display)
1171 method result)
1172 (while (setq method (pop methods))
1173 (when (string-match method type)
1174 (setq result t
1175 methods nil)))
1176 result))
1178 (defun mm-destroy-part (handle)
1179 "Destroy the data structures connected to HANDLE."
1180 (when (listp handle)
1181 (mm-remove-part handle)
1182 (when (buffer-live-p (mm-handle-buffer handle))
1183 (kill-buffer (mm-handle-buffer handle)))))
1185 (defun mm-handle-displayed-p (handle)
1186 "Say whether HANDLE is displayed or not."
1187 (mm-handle-undisplayer handle))
1190 ;;; Functions for outputting parts
1193 (defmacro mm-with-part (handle &rest forms)
1194 "Run FORMS in the temp buffer containing the contents of HANDLE."
1195 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1196 ;; chars, so the buffer should be unibyte. It may happen that the
1197 ;; handle-buffer is multibyte for some reason, in which case now is a good
1198 ;; time to adjust it, since we know at this point that it should
1199 ;; be unibyte.
1200 `(let* ((handle ,handle))
1201 (when (and (mm-handle-buffer handle)
1202 (buffer-name (mm-handle-buffer handle)))
1203 (with-temp-buffer
1204 (mm-disable-multibyte)
1205 (insert-buffer-substring (mm-handle-buffer handle))
1206 (mm-decode-content-transfer-encoding
1207 (mm-handle-encoding handle)
1208 (mm-handle-media-type handle))
1209 ,@forms))))
1210 (put 'mm-with-part 'lisp-indent-function 1)
1211 (put 'mm-with-part 'edebug-form-spec '(body))
1213 (defun mm-get-part (handle &optional no-cache)
1214 "Return the contents of HANDLE as a string.
1215 If NO-CACHE is non-nil, cached contents of a message/external-body part
1216 are ignored."
1217 (if (and (not no-cache)
1218 (equal (mm-handle-media-type handle) "message/external-body"))
1219 (progn
1220 (unless (mm-handle-cache handle)
1221 (mm-extern-cache-contents handle))
1222 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1223 (buffer-string)))
1224 (mm-with-part handle
1225 (buffer-string))))
1227 (defun mm-insert-part (handle &optional no-cache)
1228 "Insert the contents of HANDLE in the current buffer.
1229 If NO-CACHE is non-nil, cached contents of a message/external-body part
1230 are ignored."
1231 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1232 'charset)
1233 'gnus-decoded)
1234 (with-current-buffer (mm-handle-buffer handle)
1235 (buffer-string)))
1236 ((mm-multibyte-p)
1237 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1239 (mm-get-part handle no-cache)))))
1240 (save-restriction
1241 (widen)
1242 (goto-char
1243 (prog1
1244 (point)
1245 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1246 'mm-uu-extract)
1247 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1248 ;; Separate the extracted parts that have the same faces.
1249 (insert "\n" text)
1250 (insert text)))))))
1252 (defun mm-file-name-delete-whitespace (file-name)
1253 "Remove all whitespace characters from FILE-NAME."
1254 (while (string-match "\\s-+" file-name)
1255 (setq file-name (replace-match "" t t file-name)))
1256 file-name)
1258 (defun mm-file-name-trim-whitespace (file-name)
1259 "Remove leading and trailing whitespace characters from FILE-NAME."
1260 (when (string-match "\\`\\s-+" file-name)
1261 (setq file-name (substring file-name (match-end 0))))
1262 (when (string-match "\\s-+\\'" file-name)
1263 (setq file-name (substring file-name 0 (match-beginning 0))))
1264 file-name)
1266 (defun mm-file-name-collapse-whitespace (file-name)
1267 "Collapse multiple whitespace characters in FILE-NAME."
1268 (while (string-match "\\s-\\s-+" file-name)
1269 (setq file-name (replace-match " " t t file-name)))
1270 file-name)
1272 (defun mm-file-name-replace-whitespace (file-name)
1273 "Replace whitespace characters in FILE-NAME with underscores.
1274 Set the option `mm-file-name-replace-whitespace' to any other
1275 string if you do not like underscores."
1276 (let ((s (or mm-file-name-replace-whitespace "_")))
1277 (while (string-match "\\s-" file-name)
1278 (setq file-name (replace-match s t t file-name))))
1279 file-name)
1281 (defun mm-file-name-delete-control (filename)
1282 "Delete control characters from FILENAME."
1283 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1285 (defun mm-file-name-delete-gotchas (filename)
1286 "Delete shell gotchas from FILENAME."
1287 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1288 (gnus-replace-in-string filename "^[.-]+" ""))
1290 (defun mm-save-part (handle &optional prompt)
1291 "Write HANDLE to a file.
1292 PROMPT overrides the default one used to ask user for a file name."
1293 (let ((filename (or (mail-content-type-get
1294 (mm-handle-disposition handle) 'filename)
1295 (mail-content-type-get
1296 (mm-handle-type handle) 'name)))
1297 file)
1298 (when filename
1299 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1300 (file-name-nondirectory filename))))
1301 (setq file
1302 (read-file-name
1303 (or prompt
1304 (format "Save MIME part to (default %s): "
1305 (or filename "")))
1306 (or mm-default-directory default-directory)
1307 (expand-file-name (or filename "")
1308 (or mm-default-directory default-directory))))
1309 (if (file-directory-p file)
1310 (setq file (expand-file-name filename file))
1311 (setq file (expand-file-name
1312 file (or mm-default-directory default-directory))))
1313 (setq mm-default-directory (file-name-directory file))
1314 (and (or (not (file-exists-p file))
1315 (yes-or-no-p (format "File %s already exists; overwrite? "
1316 file)))
1317 (progn
1318 (mm-save-part-to-file handle file)
1319 file))))
1321 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1322 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1323 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1324 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1325 Return t if meta tag is added or replaced."
1326 (when (equal (mm-handle-media-type handle) "text/html")
1327 (when (or charset
1328 (setq charset (mail-content-type-get (mm-handle-type handle)
1329 'charset)))
1330 (setq charset (format "\
1331 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1332 (let ((case-fold-search t))
1333 (goto-char (point-min))
1334 (if (re-search-forward "\
1335 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1336 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
1337 (if (and (not force-charset)
1338 (match-beginning 2)
1339 (string-match "\\`html\\'" (match-string 1)))
1340 ;; Don't modify existing meta tag.
1342 ;; Replace it with the one specifying charset.
1343 (replace-match charset)
1345 (if (re-search-forward "<head>\\s-*" nil t)
1346 (insert charset "\n")
1347 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1348 (insert "<head>\n" charset "\n</head>\n"))
1349 t)))))
1351 (defun mm-save-part-to-file (handle file)
1352 (mm-with-unibyte-buffer
1353 (mm-insert-part handle)
1354 (mm-add-meta-html-tag handle)
1355 (let ((current-file-modes (default-file-modes)))
1356 (set-default-file-modes mm-attachment-file-modes)
1357 (unwind-protect
1358 ;; Don't re-compress .gz & al. Arguably we should make
1359 ;; `file-name-handler-alist' nil, but that would chop
1360 ;; ange-ftp, which is reasonable to use here.
1361 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1362 (set-default-file-modes current-file-modes)))))
1364 (defun mm-pipe-part (handle &optional cmd)
1365 "Pipe HANDLE to a process.
1366 Use CMD as the process."
1367 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1368 (command (or cmd
1369 (gnus-read-shell-command
1370 "Shell command on MIME part: " mm-last-shell-command))))
1371 (mm-with-unibyte-buffer
1372 (mm-insert-part handle)
1373 (mm-add-meta-html-tag handle)
1374 (let ((coding-system-for-write 'binary))
1375 (shell-command-on-region (point-min) (point-max) command nil)))))
1377 (autoload 'gnus-completing-read "gnus-util")
1379 (defun mm-interactively-view-part (handle)
1380 "Display HANDLE using METHOD."
1381 (let* ((type (mm-handle-media-type handle))
1382 (methods
1383 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1384 (mailcap-mime-info type 'all)))
1385 (method (let ((minibuffer-local-completion-map
1386 mm-viewer-completion-map))
1387 (completing-read "Viewer: " methods))))
1388 (when (string= method "")
1389 (error "No method given"))
1390 (if (string-match "^[^% \t]+$" method)
1391 (setq method (concat method " %s")))
1392 (mm-display-external handle method)))
1394 (defun mm-preferred-alternative (handles &optional preferred)
1395 "Say which of HANDLES are preferred."
1396 (let ((prec (if preferred (list preferred)
1397 (mm-preferred-alternative-precedence handles)))
1398 p h result type handle)
1399 (while (setq p (pop prec))
1400 (setq h handles)
1401 (while h
1402 (setq handle (car h))
1403 (setq type (mm-handle-media-type handle))
1404 (when (and (equal p type)
1405 (mm-automatic-display-p handle)
1406 (or (stringp (car handle))
1407 (not (mm-handle-disposition handle))
1408 (equal (car (mm-handle-disposition handle))
1409 "inline")))
1410 (setq result handle
1411 h nil
1412 prec nil))
1413 (pop h)))
1414 result))
1416 (defun mm-preferred-alternative-precedence (handles)
1417 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1418 (setq handles (reverse handles))
1419 (dolist (disc (reverse mm-discouraged-alternatives))
1420 (dolist (handle (copy-sequence handles))
1421 (when (string-match disc (mm-handle-media-type handle))
1422 (setq handles (nconc (delete handle handles) (list handle))))))
1423 ;; Remove empty parts.
1424 (dolist (handle (copy-sequence handles))
1425 (when (and (bufferp (mm-handle-buffer handle))
1426 (not (with-current-buffer (mm-handle-buffer handle)
1427 (goto-char (point-min))
1428 (re-search-forward "[^ \t\n]" nil t))))
1429 (setq handles (nconc (delete handle handles) (list handle)))))
1430 (mapcar #'mm-handle-media-type handles))
1432 (defun mm-get-content-id (id)
1433 "Return the handle(s) referred to by ID."
1434 (cdr (assoc id mm-content-id-alist)))
1436 (defconst mm-image-type-regexps
1437 '(("/\\*.*XPM.\\*/" . xpm)
1438 ("P[1-6]" . pbm)
1439 ("GIF8" . gif)
1440 ("\377\330" . jpeg)
1441 ("\211PNG\r\n" . png)
1442 ("#define" . xbm)
1443 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1444 ("%!PS" . postscript))
1445 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1446 When the first bytes of an image file match REGEXP, it is assumed to
1447 be of image type IMAGE-TYPE.")
1449 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1450 (defun mm-image-type-from-buffer ()
1451 "Determine the image type from data in the current buffer.
1452 Value is a symbol specifying the image type or nil if type cannot
1453 be determined."
1454 (let ((types mm-image-type-regexps)
1455 type)
1456 (goto-char (point-min))
1457 (while (and types (null type))
1458 (let ((regexp (car (car types)))
1459 (image-type (cdr (car types))))
1460 (when (looking-at regexp)
1461 (setq type image-type))
1462 (setq types (cdr types))))
1463 type))
1465 (defun mm-get-image (handle)
1466 "Return an image instance based on HANDLE."
1467 (let ((type (mm-handle-media-subtype handle))
1468 spec)
1469 ;; Allow some common translations.
1470 (setq type
1471 (cond
1472 ((equal type "x-pixmap")
1473 "xpm")
1474 ((equal type "x-xbitmap")
1475 "xbm")
1476 ((equal type "x-portable-bitmap")
1477 "pbm")
1478 (t type)))
1479 (or (mm-handle-cache handle)
1480 (mm-with-unibyte-buffer
1481 (mm-insert-part handle)
1482 (prog1
1483 (setq spec
1484 (ignore-errors
1485 ;; Avoid testing `make-glyph' since W3 may define
1486 ;; a bogus version of it.
1487 (if (fboundp 'create-image)
1488 (create-image (buffer-string)
1489 (or (mm-image-type-from-buffer)
1490 (intern type))
1491 'data-p)
1492 (mm-create-image-xemacs type))))
1493 (mm-handle-set-cache handle spec))))))
1495 (defun mm-create-image-xemacs (type)
1496 (when (featurep 'xemacs)
1497 (cond
1498 ((equal type "xbm")
1499 ;; xbm images require special handling, since
1500 ;; the only way to create glyphs from these
1501 ;; (without a ton of work) is to write them
1502 ;; out to a file, and then create a file
1503 ;; specifier.
1504 (let ((file (mm-make-temp-file
1505 (expand-file-name "emm" mm-tmp-directory)
1506 nil ".xbm")))
1507 (unwind-protect
1508 (progn
1509 (write-region (point-min) (point-max) file)
1510 (make-glyph (list (cons 'x file))))
1511 (ignore-errors
1512 (delete-file file)))))
1514 (make-glyph
1515 (vector
1516 (or (mm-image-type-from-buffer)
1517 (intern type))
1518 :data (buffer-string)))))))
1520 (declare-function image-size "image.c" (spec &optional pixels frame))
1522 (defun mm-image-fit-p (handle)
1523 "Say whether the image in HANDLE will fit the current window."
1524 (let ((image (mm-get-image handle)))
1525 (or (not image)
1526 (if (featurep 'xemacs)
1527 ;; XEmacs's glyphs can actually tell us about their width, so
1528 ;; let's be nice and smart about them.
1529 (or mm-inline-large-images
1530 (and (<= (glyph-width image) (window-pixel-width))
1531 (<= (glyph-height image) (window-pixel-height))))
1532 (let* ((size (image-size image))
1533 (w (car size))
1534 (h (cdr size)))
1535 (or mm-inline-large-images
1536 (and (<= h (1- (window-height))) ; Don't include mode line.
1537 (<= w (window-width)))))))))
1539 (defun mm-valid-image-format-p (format)
1540 "Say whether FORMAT can be displayed natively by Emacs."
1541 (cond
1542 ;; Handle XEmacs
1543 ((fboundp 'valid-image-instantiator-format-p)
1544 (valid-image-instantiator-format-p format))
1545 ;; Handle Emacs
1546 ((fboundp 'image-type-available-p)
1547 (and (display-graphic-p)
1548 (image-type-available-p format)))
1549 ;; Nobody else can do images yet.
1551 nil)))
1553 (defun mm-valid-and-fit-image-p (format handle)
1554 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1555 (and (mm-valid-image-format-p format)
1556 (mm-image-fit-p handle)))
1558 (defun mm-find-part-by-type (handles type &optional notp recursive)
1559 "Search in HANDLES for part with TYPE.
1560 If NOTP, returns first non-matching part.
1561 If RECURSIVE, search recursively."
1562 (let (handle)
1563 (while handles
1564 (if (and recursive (stringp (caar handles)))
1565 (if (setq handle (mm-find-part-by-type (cdar handles) type
1566 notp recursive))
1567 (setq handles nil))
1568 (if (if notp
1569 (not (equal (mm-handle-media-type (car handles)) type))
1570 (equal (mm-handle-media-type (car handles)) type))
1571 (setq handle (car handles)
1572 handles nil)))
1573 (setq handles (cdr handles)))
1574 handle))
1576 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1577 (goto-char (point-min))
1578 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1579 'boundary)))
1580 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1581 start
1582 (end (save-excursion
1583 (goto-char (point-max))
1584 (if (re-search-backward close-delimiter nil t)
1585 (match-beginning 0)
1586 (point-max))))
1587 result)
1588 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1589 (while (and (not result)
1590 (re-search-forward boundary end t))
1591 (goto-char (match-beginning 0))
1592 (when start
1593 (save-excursion
1594 (save-restriction
1595 (narrow-to-region start (1- (point)))
1596 (when (let* ((ct (mail-fetch-field "content-type"))
1597 (ctl (and ct (mail-header-parse-content-type ct))))
1598 (if notp
1599 (not (equal (car ctl) type))
1600 (equal (car ctl) type)))
1601 (setq result (buffer-string))))))
1602 (forward-line 1)
1603 (setq start (point)))
1604 (when (and (not result) start)
1605 (save-excursion
1606 (save-restriction
1607 (narrow-to-region start end)
1608 (when (let* ((ct (mail-fetch-field "content-type"))
1609 (ctl (and ct (mail-header-parse-content-type ct))))
1610 (if notp
1611 (not (equal (car ctl) type))
1612 (equal (car ctl) type)))
1613 (setq result (buffer-string))))))
1614 result))
1616 (defvar mm-security-handle nil)
1618 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1619 ;; HANDLE could be a CTL.
1620 (when handle
1621 (put-text-property 0 (length (car handle)) parameter value
1622 (car handle))))
1624 (autoload 'mm-view-pkcs7 "mm-view")
1626 (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
1627 (let ((type (car ctl))
1628 (subtype (cadr (split-string (car ctl) "/")))
1629 (mm-security-handle ctl) ;; (car CTL) is the type.
1630 protocol func functest)
1631 (cond
1632 ((or (equal type "application/x-pkcs7-mime")
1633 (equal type "application/pkcs7-mime"))
1634 (with-temp-buffer
1635 (when (and (cond
1636 ((eq mm-decrypt-option 'never) nil)
1637 ((eq mm-decrypt-option 'always) t)
1638 ((eq mm-decrypt-option 'known) t)
1639 (t (y-or-n-p
1640 (format "Decrypt (S/MIME) part? "))))
1641 (mm-view-pkcs7 parts from))
1642 (setq parts (mm-dissect-buffer t)))))
1643 ((equal subtype "signed")
1644 (unless (and (setq protocol
1645 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1646 (not (equal protocol "multipart/mixed")))
1647 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1648 (let ((protocols mm-verify-function-alist))
1649 (while protocols
1650 (if (and (or (not (setq functest (nth 3 (car protocols))))
1651 (funcall functest parts ctl))
1652 (mm-find-part-by-type parts (caar protocols) nil t))
1653 (setq protocol (caar protocols)
1654 protocols nil)
1655 (setq protocols (cdr protocols))))))
1656 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1657 (when (cond
1658 ((eq mm-verify-option 'never) nil)
1659 ((eq mm-verify-option 'always) t)
1660 ((eq mm-verify-option 'known)
1661 (and func
1662 (or (not (setq functest
1663 (nth 3 (assoc protocol
1664 mm-verify-function-alist))))
1665 (funcall functest parts ctl))))
1667 (y-or-n-p
1668 (format "Verify signed (%s) part? "
1669 (or (nth 2 (assoc protocol mm-verify-function-alist))
1670 (format "protocol=%s" protocol))))))
1671 (save-excursion
1672 (if func
1673 (setq parts (funcall func parts ctl))
1674 (mm-set-handle-multipart-parameter
1675 mm-security-handle 'gnus-details
1676 (format "Unknown sign protocol (%s)" protocol))))))
1677 ((equal subtype "encrypted")
1678 (unless (setq protocol
1679 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1680 ;; The message is broken.
1681 (let ((parts parts))
1682 (while parts
1683 (if (assoc (mm-handle-media-type (car parts))
1684 mm-decrypt-function-alist)
1685 (setq protocol (mm-handle-media-type (car parts))
1686 parts nil)
1687 (setq parts (cdr parts))))))
1688 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1689 (when (cond
1690 ((eq mm-decrypt-option 'never) nil)
1691 ((eq mm-decrypt-option 'always) t)
1692 ((eq mm-decrypt-option 'known)
1693 (and func
1694 (or (not (setq functest
1695 (nth 3 (assoc protocol
1696 mm-decrypt-function-alist))))
1697 (funcall functest parts ctl))))
1699 (y-or-n-p
1700 (format "Decrypt (%s) part? "
1701 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1702 (format "protocol=%s" protocol))))))
1703 (save-excursion
1704 (if func
1705 (setq parts (funcall func parts ctl))
1706 (mm-set-handle-multipart-parameter
1707 mm-security-handle 'gnus-details
1708 (format "Unknown encrypt protocol (%s)" protocol))))))
1709 (t nil))
1710 parts))
1712 (defun mm-multiple-handles (handles)
1713 (and (listp handles)
1714 (> (length handles) 1)
1715 (or (listp (car handles))
1716 (stringp (car handles)))))
1718 (defun mm-complicated-handles (handles)
1719 (and (listp (car handles))
1720 (> (length handles) 1)))
1722 (defun mm-merge-handles (handles1 handles2)
1723 (append
1724 (if (listp (car handles1))
1725 handles1
1726 (list handles1))
1727 (if (listp (car handles2))
1728 handles2
1729 (list handles2))))
1731 (defun mm-readable-p (handle)
1732 "Say whether the content of HANDLE is readable."
1733 (and (< (with-current-buffer (mm-handle-buffer handle)
1734 (buffer-size)) 10000)
1735 (mm-with-unibyte-buffer
1736 (mm-insert-part handle)
1737 (and (eq (mm-body-7-or-8) '7bit)
1738 (not (mm-long-lines-p 76))))))
1740 (declare-function libxml-parse-html-region "xml.c"
1741 (start end &optional base-url))
1742 (declare-function shr-insert-document "shr" (dom))
1743 (defvar shr-blocked-images)
1744 (defvar gnus-inhibit-images)
1745 (autoload 'gnus-blocked-images "gnus-art")
1747 (defun mm-shr (handle)
1748 ;; Require since we bind its variables.
1749 (require 'shr)
1750 (let ((article-buffer (current-buffer))
1751 (shr-content-function (lambda (id)
1752 (let ((handle (mm-get-content-id id)))
1753 (when handle
1754 (mm-with-part handle
1755 (buffer-string))))))
1756 shr-inhibit-images shr-blocked-images charset char)
1757 (if (and (boundp 'gnus-summary-buffer)
1758 (bufferp gnus-summary-buffer)
1759 (buffer-name gnus-summary-buffer))
1760 (with-current-buffer gnus-summary-buffer
1761 (setq shr-inhibit-images gnus-inhibit-images
1762 shr-blocked-images (gnus-blocked-images)))
1763 (setq shr-inhibit-images gnus-inhibit-images
1764 shr-blocked-images (gnus-blocked-images)))
1765 (unless handle
1766 (setq handle (mm-dissect-buffer t)))
1767 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1768 (save-restriction
1769 (narrow-to-region (point) (point))
1770 (shr-insert-document
1771 (mm-with-part handle
1772 (insert (prog1
1773 (if (and charset
1774 (setq charset
1775 (mm-charset-to-coding-system charset
1776 nil t))
1777 (not (eq charset 'ascii)))
1778 (mm-decode-coding-string (buffer-string) charset)
1779 (mm-string-as-multibyte (buffer-string)))
1780 (erase-buffer)
1781 (mm-enable-multibyte)))
1782 (goto-char (point-min))
1783 (setq case-fold-search t)
1784 (while (re-search-forward
1785 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1786 (when (setq char
1787 (cdr (assq (if (match-beginning 1)
1788 (string-to-number (match-string 1) 16)
1789 (string-to-number (match-string 2)))
1790 mm-extra-numeric-entities)))
1791 (replace-match (char-to-string char))))
1792 ;; Remove "soft hyphens".
1793 (goto-char (point-min))
1794 (while (search-forward "­" nil t)
1795 (replace-match "" t t))
1796 (libxml-parse-html-region (point-min) (point-max))))
1797 (unless (bobp)
1798 (insert "\n"))
1799 (mm-handle-set-undisplayer
1800 handle
1801 `(lambda ()
1802 (let ((inhibit-read-only t))
1803 (delete-region ,(point-min-marker)
1804 ,(point-max-marker))))))))
1806 (defun mm-handle-filename (handle)
1807 "Return filename of HANDLE if any."
1808 (or (mail-content-type-get (mm-handle-type handle)
1809 'name)
1810 (mail-content-type-get (mm-handle-disposition handle)
1811 'filename)))
1813 (provide 'mm-decode)
1815 ;; Local Variables:
1816 ;; coding: iso-8859-1
1817 ;; End:
1819 ;;; mm-decode.el ends here