* mm-decode.el (mm-dissect-buffer): Doc fix.
[gnus.git] / lisp / mm-decode.el
blob8076b2ea06db2a80093d49f25df560db1636fefe
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)
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 (equal (car ctl) "application/octet-stream")
672 filename
673 (string-match "\\.\\([^.]+\\)$" filename))
674 (let ((new-type (mailcap-extension-to-mime (match-string 1 filename))))
675 (when new-type
676 (setcar ctl new-type)))))
677 (let ((handle
678 (mm-make-handle
679 (mm-copy-to-buffer) ctl cte nil cdl description nil id))
680 (decoder (assoc (car ctl) (mm-archive-decoders))))
681 (if (and decoder
682 ;; Do automatic decoding
683 (cadr decoder)
684 (executable-find (caddr decoder)))
685 (mm-dissect-archive handle)
686 handle))))
688 (defun mm-dissect-multipart (ctl from)
689 (goto-char (point-min))
690 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
691 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
692 start parts
693 (end (save-excursion
694 (goto-char (point-max))
695 (if (re-search-backward close-delimiter nil t)
696 (match-beginning 0)
697 (point-max)))))
698 (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
699 (while (and (< (point) end) (re-search-forward boundary end t))
700 (goto-char (match-beginning 0))
701 (when start
702 (save-excursion
703 (save-restriction
704 (narrow-to-region start (point))
705 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
706 (end-of-line 2)
707 (or (looking-at boundary)
708 (forward-line 1))
709 (setq start (point)))
710 (when (and start (< start end))
711 (save-excursion
712 (save-restriction
713 (narrow-to-region start end)
714 (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
715 (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
717 (defun mm-copy-to-buffer ()
718 "Copy the contents of the current buffer to a fresh buffer."
719 (let ((obuf (current-buffer))
720 (mb (mm-multibyte-p))
721 beg)
722 (goto-char (point-min))
723 (search-forward-regexp "^\n" nil t)
724 (setq beg (point))
725 (with-current-buffer
726 (generate-new-buffer " *mm*")
727 ;; Preserve the data's unibyteness (for url-insert-file-contents).
728 (mm-set-buffer-multibyte mb)
729 (insert-buffer-substring obuf beg)
730 (current-buffer))))
732 (defun mm-display-parts (handle &optional no-default)
733 (if (stringp (car handle))
734 (mapcar 'mm-display-parts (cdr handle))
735 (if (bufferp (car handle))
736 (save-restriction
737 (narrow-to-region (point) (point))
738 (mm-display-part handle)
739 (goto-char (point-max)))
740 (mapcar 'mm-display-parts handle))))
742 (autoload 'mailcap-parse-mailcaps "mailcap")
743 (autoload 'mailcap-mime-info "mailcap")
745 (defun mm-display-part (handle &optional no-default force)
746 "Display the MIME part represented by HANDLE.
747 Returns nil if the part is removed; inline if displayed inline;
748 external if displayed external."
749 (save-excursion
750 (mailcap-parse-mailcaps)
751 (if (and (not force)
752 (mm-handle-displayed-p handle))
753 (mm-remove-part handle)
754 (let* ((ehandle (if (equal (mm-handle-media-type handle)
755 "message/external-body")
756 (progn
757 (unless (mm-handle-cache handle)
758 (mm-extern-cache-contents handle))
759 (mm-handle-cache handle))
760 handle))
761 (type (mm-handle-media-type ehandle))
762 (method (mailcap-mime-info type))
763 (filename (or (mail-content-type-get
764 (mm-handle-disposition handle) 'filename)
765 (mail-content-type-get
766 (mm-handle-type handle) 'name)
767 "<file>"))
768 (external mm-enable-external))
769 (if (and (mm-inlinable-p ehandle)
770 (mm-inlined-p ehandle))
771 (progn
772 (forward-line 1)
773 (mm-display-inline handle)
774 'inline)
775 (when (or method
776 (not no-default))
777 (if (and (not method)
778 (equal "text" (car (split-string type "/"))))
779 (progn
780 (forward-line 1)
781 (mm-insert-inline handle (mm-get-part handle))
782 'inline)
783 (setq external
784 (and method ;; If nil, we always use "save".
785 (stringp method) ;; 'mailcap-save-binary-file
786 (or (eq mm-enable-external t)
787 (and (eq mm-enable-external 'ask)
788 (y-or-n-p
789 (concat
790 "Display part (" type
791 ") using external program"
792 ;; Can non-string method ever happen?
793 (if (stringp method)
794 (concat
795 " \"" (format method filename) "\"")
797 "? "))))))
798 (if external
799 (mm-display-external
800 handle (or method 'mailcap-save-binary-file))
801 (mm-display-external
802 handle 'mailcap-save-binary-file)))))))))
804 (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
805 (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
807 (defun mm-display-external (handle method)
808 "Display HANDLE using METHOD."
809 (let ((outbuf (current-buffer)))
810 (mm-with-unibyte-buffer
811 (if (functionp method)
812 (let ((cur (current-buffer)))
813 (if (eq method 'mailcap-save-binary-file)
814 (progn
815 (set-buffer (generate-new-buffer " *mm*"))
816 (setq method nil))
817 (mm-insert-part handle)
818 (mm-add-meta-html-tag handle)
819 (let ((win (get-buffer-window cur t)))
820 (when win
821 (select-window win)))
822 (switch-to-buffer (generate-new-buffer " *mm*")))
823 (buffer-disable-undo)
824 (mm-set-buffer-file-coding-system mm-binary-coding-system)
825 (insert-buffer-substring cur)
826 (goto-char (point-min))
827 (when method
828 (message "Viewing with %s" method))
829 (let ((mm (current-buffer))
830 (non-viewer (assq 'non-viewer
831 (mailcap-mime-info
832 (mm-handle-media-type handle) t))))
833 (unwind-protect
834 (if method
835 (funcall method)
836 (mm-save-part handle))
837 (when (and (not non-viewer)
838 method)
839 (mm-handle-set-undisplayer handle mm)))))
840 ;; The function is a string to be executed.
841 (mm-insert-part handle)
842 (mm-add-meta-html-tag handle)
843 (let* ((dir (mm-make-temp-file
844 (expand-file-name "emm." mm-tmp-directory) 'dir))
845 (filename (or
846 (mail-content-type-get
847 (mm-handle-disposition handle) 'filename)
848 (mail-content-type-get
849 (mm-handle-type handle) 'name)))
850 (mime-info (mailcap-mime-info
851 (mm-handle-media-type handle) t))
852 (needsterm (or (assoc "needsterm" mime-info)
853 (assoc "needsterminal" mime-info)))
854 (copiousoutput (assoc "copiousoutput" mime-info))
855 file buffer)
856 ;; We create a private sub-directory where we store our files.
857 (set-file-modes dir #o700)
858 (if filename
859 (setq file (expand-file-name
860 (gnus-map-function mm-file-name-rewrite-functions
861 (file-name-nondirectory filename))
862 dir))
863 ;; Use nametemplate (defined in RFC1524) if it is specified
864 ;; in mailcap.
865 (let ((suffix (cdr (assoc "nametemplate" mime-info))))
866 (if (and suffix
867 (string-match "\\`%s\\(\\..+\\)\\'" suffix))
868 (setq suffix (match-string 1 suffix))
869 ;; Otherwise, use a suffix according to
870 ;; `mailcap-mime-extensions'.
871 (setq suffix (car (rassoc (mm-handle-media-type handle)
872 mailcap-mime-extensions))))
873 (setq file (mm-make-temp-file (expand-file-name "mm." dir)
874 nil suffix))))
875 (let ((coding-system-for-write mm-binary-coding-system))
876 (write-region (point-min) (point-max) file nil 'nomesg))
877 ;; The file is deleted after the viewer exists. If the users edits
878 ;; the file, changes will be lost. Set file to read-only to make it
879 ;; clear.
880 (set-file-modes file #o400)
881 (message "Viewing with %s" method)
882 (cond
883 (needsterm
884 (let ((command (mm-mailcap-command
885 method file (mm-handle-type handle))))
886 (unwind-protect
887 (if window-system
888 (start-process "*display*" nil
889 mm-external-terminal-program
890 "-e" shell-file-name
891 shell-command-switch command)
892 (require 'term)
893 (require 'gnus-win)
894 (set-buffer
895 (setq buffer
896 (make-term "display"
897 shell-file-name
899 shell-command-switch command)))
900 (term-mode)
901 (term-char-mode)
902 (set-process-sentinel
903 (get-buffer-process buffer)
904 `(lambda (process state)
905 (if (eq 'exit (process-status process))
906 (gnus-configure-windows
907 ',gnus-current-window-configuration))))
908 (gnus-configure-windows 'display-term))
909 (mm-handle-set-external-undisplayer handle (cons file buffer)))
910 (message "Displaying %s..." command))
911 'external)
912 (copiousoutput
913 (with-current-buffer outbuf
914 (forward-line 1)
915 (mm-insert-inline
916 handle
917 (unwind-protect
918 (progn
919 (call-process shell-file-name nil
920 (setq buffer
921 (generate-new-buffer " *mm*"))
923 shell-command-switch
924 (mm-mailcap-command
925 method file (mm-handle-type handle)))
926 (if (buffer-live-p buffer)
927 (with-current-buffer buffer
928 (buffer-string))))
929 (progn
930 (ignore-errors (delete-file file))
931 (ignore-errors (delete-directory
932 (file-name-directory file)))
933 (ignore-errors (kill-buffer buffer))))))
934 'inline)
936 ;; Deleting the temp file should be postponed for some wrappers,
937 ;; shell scripts, and so on, which might exit right after having
938 ;; started a viewer command as a background job.
939 (let ((command (mm-mailcap-command
940 method file (mm-handle-type handle))))
941 (unwind-protect
942 (progn
943 (start-process "*display*"
944 (setq buffer
945 (generate-new-buffer " *mm*"))
946 shell-file-name
947 shell-command-switch command)
948 (set-process-sentinel
949 (get-buffer-process buffer)
950 (lexical-let ((outbuf outbuf)
951 (file file)
952 (buffer buffer)
953 (command command)
954 (handle handle))
955 (run-at-time
956 30.0 nil
957 (lambda ()
958 (ignore-errors
959 (delete-file file))
960 (ignore-errors
961 (delete-directory (file-name-directory file)))))
962 (lambda (process state)
963 (when (eq (process-status process) 'exit)
964 (condition-case nil
965 (delete-file file)
966 (error))
967 (condition-case nil
968 (delete-directory (file-name-directory file))
969 (error))
970 (when (buffer-live-p outbuf)
971 (with-current-buffer outbuf
972 (let ((buffer-read-only nil)
973 (point (point)))
974 (forward-line 2)
975 (mm-insert-inline
976 handle (with-current-buffer buffer
977 (buffer-string)))
978 (goto-char point))))
979 (when (buffer-live-p buffer)
980 (kill-buffer buffer)))
981 (message "Displaying %s...done" command)))))
982 (mm-handle-set-external-undisplayer
983 handle (cons file buffer)))
984 (message "Displaying %s..." command))
985 'external)))))))
987 (defun mm-mailcap-command (method file type-list)
988 (let ((ctl (cdr type-list))
989 (beg 0)
990 (uses-stdin t)
991 out sub total)
992 (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
993 method beg)
994 (push (substring method beg (match-beginning 0)) out)
995 (setq beg (match-end 0)
996 total (match-string 0 method)
997 sub (match-string 1 method))
998 (cond
999 ((string= total "%%")
1000 (push "%" out))
1001 ((or (string= total "%s")
1002 ;; We do our own quoting.
1003 (string= total "'%s'")
1004 (string= total "\"%s\""))
1005 (setq uses-stdin nil)
1006 (push (shell-quote-argument
1007 (gnus-map-function mm-path-name-rewrite-functions file)) out))
1008 ((string= total "%t")
1009 (push (shell-quote-argument (car type-list)) out))
1011 (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
1012 (push (substring method beg (length method)) out)
1013 (when uses-stdin
1014 (push "<" out)
1015 (push (shell-quote-argument
1016 (gnus-map-function mm-path-name-rewrite-functions file))
1017 out))
1018 (mapconcat 'identity (nreverse out) "")))
1020 (defun mm-remove-parts (handles)
1021 "Remove the displayed MIME parts represented by HANDLES."
1022 (if (and (listp handles)
1023 (bufferp (car handles)))
1024 (mm-remove-part handles)
1025 (let (handle)
1026 (while (setq handle (pop handles))
1027 (cond
1028 ((stringp handle)
1029 (when (buffer-live-p (get-text-property 0 'buffer handle))
1030 (kill-buffer (get-text-property 0 'buffer handle))))
1031 ((and (listp handle)
1032 (stringp (car handle)))
1033 (mm-remove-parts (cdr handle)))
1035 (mm-remove-part handle)))))))
1037 (defun mm-destroy-parts (handles)
1038 "Remove the displayed MIME parts represented by HANDLES."
1039 (if (and (listp handles)
1040 (bufferp (car handles)))
1041 (mm-destroy-part handles)
1042 (let (handle)
1043 (while (setq handle (pop handles))
1044 (cond
1045 ((stringp handle)
1046 (when (buffer-live-p (get-text-property 0 'buffer handle))
1047 (kill-buffer (get-text-property 0 'buffer handle))))
1048 ((and (listp handle)
1049 (stringp (car handle)))
1050 (mm-destroy-parts handle))
1052 (mm-destroy-part handle)))))))
1054 (defun mm-remove-part (handle)
1055 "Remove the displayed MIME part represented by HANDLE."
1056 (when (listp handle)
1057 (let ((object (mm-handle-undisplayer handle)))
1058 (ignore-errors
1059 (cond
1060 ;; Internally displayed part.
1061 ((mm-annotationp object)
1062 (if (featurep 'xemacs)
1063 (delete-annotation object)))
1064 ((or (functionp object)
1065 (and (listp object)
1066 (eq (car object) 'lambda)))
1067 (funcall object))
1068 ;; Externally displayed part.
1069 ((consp object)
1070 (condition-case ()
1071 (while (get-buffer-process (cdr object))
1072 (interrupt-process (get-buffer-process (cdr object)))
1073 (message "Waiting for external displayer to die...")
1074 (sit-for 1))
1075 (quit)
1076 (error))
1077 (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
1078 (message "Waiting for external displayer to die...done")
1079 (ignore-errors (delete-file (car object)))
1080 (ignore-errors (delete-directory (file-name-directory
1081 (car object)))))
1082 ((bufferp object)
1083 (when (buffer-live-p object)
1084 (kill-buffer object)))))
1085 (mm-handle-set-undisplayer handle nil))))
1087 (defun mm-display-inline (handle)
1088 (let* ((type (mm-handle-media-type handle))
1089 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
1090 (funcall function handle)
1091 (goto-char (point-min))))
1093 (defun mm-assoc-string-match (alist type)
1094 (dolist (elem alist)
1095 (when (string-match (car elem) type)
1096 (return elem))))
1098 (defun mm-automatic-display-p (handle)
1099 "Say whether the user wants HANDLE to be displayed automatically."
1100 (let ((methods mm-automatic-display)
1101 (type (mm-handle-media-type handle))
1102 method result)
1103 (while (setq method (pop methods))
1104 (when (and (not (mm-inline-override-p handle))
1105 (string-match method type))
1106 (setq result t
1107 methods nil)))
1108 result))
1110 (defun mm-inlinable-p (handle &optional type)
1111 "Say whether HANDLE can be displayed inline.
1112 TYPE is the mime-type of the object; it defaults to the one given
1113 in HANDLE."
1114 (unless type (setq type (mm-handle-media-type handle)))
1115 (let ((alist mm-inline-media-tests)
1116 test)
1117 (while alist
1118 (when (string-match (caar alist) type)
1119 (setq test (caddar alist)
1120 alist nil)
1121 (setq test (funcall test handle)))
1122 (pop alist))
1123 test))
1125 (defun mm-inlined-p (handle)
1126 "Say whether the user wants HANDLE to be displayed inline."
1127 (let ((methods mm-inlined-types)
1128 (type (mm-handle-media-type handle))
1129 method result)
1130 (while (setq method (pop methods))
1131 (when (and (not (mm-inline-override-p handle))
1132 (string-match method type))
1133 (setq result t
1134 methods nil)))
1135 result))
1137 (defun mm-attachment-override-p (handle)
1138 "Say whether HANDLE should have attachment behavior overridden."
1139 (let ((types mm-attachment-override-types)
1140 (type (mm-handle-media-type handle))
1142 (catch 'found
1143 (while (setq ty (pop types))
1144 (when (and (string-match ty type)
1145 (mm-inlinable-p handle))
1146 (throw 'found t))))))
1148 (defun mm-inline-override-p (handle)
1149 "Say whether HANDLE should have inline behavior overridden."
1150 (let ((types mm-inline-override-types)
1151 (type (mm-handle-media-type handle))
1153 (catch 'found
1154 (while (setq ty (pop types))
1155 (when (string-match ty type)
1156 (throw 'found t))))))
1158 (defun mm-automatic-external-display-p (type)
1159 "Return the user-defined method for TYPE."
1160 (let ((methods mm-automatic-external-display)
1161 method result)
1162 (while (setq method (pop methods))
1163 (when (string-match method type)
1164 (setq result t
1165 methods nil)))
1166 result))
1168 (defun mm-destroy-part (handle)
1169 "Destroy the data structures connected to HANDLE."
1170 (when (listp handle)
1171 (mm-remove-part handle)
1172 (when (buffer-live-p (mm-handle-buffer handle))
1173 (kill-buffer (mm-handle-buffer handle)))))
1175 (defun mm-handle-displayed-p (handle)
1176 "Say whether HANDLE is displayed or not."
1177 (mm-handle-undisplayer handle))
1180 ;;; Functions for outputting parts
1183 (defmacro mm-with-part (handle &rest forms)
1184 "Run FORMS in the temp buffer containing the contents of HANDLE."
1185 ;; The handle-buffer's content is a sequence of bytes, not a sequence of
1186 ;; chars, so the buffer should be unibyte. It may happen that the
1187 ;; handle-buffer is multibyte for some reason, in which case now is a good
1188 ;; time to adjust it, since we know at this point that it should
1189 ;; be unibyte.
1190 `(let* ((handle ,handle))
1191 (when (and (mm-handle-buffer handle)
1192 (buffer-name (mm-handle-buffer handle)))
1193 (with-temp-buffer
1194 (mm-disable-multibyte)
1195 (insert-buffer-substring (mm-handle-buffer handle))
1196 (mm-decode-content-transfer-encoding
1197 (mm-handle-encoding handle)
1198 (mm-handle-media-type handle))
1199 ,@forms))))
1200 (put 'mm-with-part 'lisp-indent-function 1)
1201 (put 'mm-with-part 'edebug-form-spec '(body))
1203 (defun mm-get-part (handle &optional no-cache)
1204 "Return the contents of HANDLE as a string.
1205 If NO-CACHE is non-nil, cached contents of a message/external-body part
1206 are ignored."
1207 (if (and (not no-cache)
1208 (equal (mm-handle-media-type handle) "message/external-body"))
1209 (progn
1210 (unless (mm-handle-cache handle)
1211 (mm-extern-cache-contents handle))
1212 (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
1213 (buffer-string)))
1214 (mm-with-part handle
1215 (buffer-string))))
1217 (defun mm-insert-part (handle &optional no-cache)
1218 "Insert the contents of HANDLE in the current buffer.
1219 If NO-CACHE is non-nil, cached contents of a message/external-body part
1220 are ignored."
1221 (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
1222 'charset)
1223 'gnus-decoded)
1224 (with-current-buffer (mm-handle-buffer handle)
1225 (buffer-string)))
1226 ((mm-multibyte-p)
1227 (mm-string-to-multibyte (mm-get-part handle no-cache)))
1229 (mm-get-part handle no-cache)))))
1230 (save-restriction
1231 (widen)
1232 (goto-char
1233 (prog1
1234 (point)
1235 (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
1236 'mm-uu-extract)
1237 (eq (get-char-property 0 'face text) 'mm-uu-extract))
1238 ;; Separate the extracted parts that have the same faces.
1239 (insert "\n" text)
1240 (insert text)))))))
1242 (defun mm-file-name-delete-whitespace (file-name)
1243 "Remove all whitespace characters from FILE-NAME."
1244 (while (string-match "\\s-+" file-name)
1245 (setq file-name (replace-match "" t t file-name)))
1246 file-name)
1248 (defun mm-file-name-trim-whitespace (file-name)
1249 "Remove leading and trailing whitespace characters from FILE-NAME."
1250 (when (string-match "\\`\\s-+" file-name)
1251 (setq file-name (substring file-name (match-end 0))))
1252 (when (string-match "\\s-+\\'" file-name)
1253 (setq file-name (substring file-name 0 (match-beginning 0))))
1254 file-name)
1256 (defun mm-file-name-collapse-whitespace (file-name)
1257 "Collapse multiple whitespace characters in FILE-NAME."
1258 (while (string-match "\\s-\\s-+" file-name)
1259 (setq file-name (replace-match " " t t file-name)))
1260 file-name)
1262 (defun mm-file-name-replace-whitespace (file-name)
1263 "Replace whitespace characters in FILE-NAME with underscores.
1264 Set the option `mm-file-name-replace-whitespace' to any other
1265 string if you do not like underscores."
1266 (let ((s (or mm-file-name-replace-whitespace "_")))
1267 (while (string-match "\\s-" file-name)
1268 (setq file-name (replace-match s t t file-name))))
1269 file-name)
1271 (defun mm-file-name-delete-control (filename)
1272 "Delete control characters from FILENAME."
1273 (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
1275 (defun mm-file-name-delete-gotchas (filename)
1276 "Delete shell gotchas from FILENAME."
1277 (setq filename (gnus-replace-in-string filename "[<>|]" ""))
1278 (gnus-replace-in-string filename "^[.-]+" ""))
1280 (defun mm-save-part (handle &optional prompt)
1281 "Write HANDLE to a file.
1282 PROMPT overrides the default one used to ask user for a file name."
1283 (let ((filename (or (mail-content-type-get
1284 (mm-handle-disposition handle) 'filename)
1285 (mail-content-type-get
1286 (mm-handle-type handle) 'name)))
1287 file)
1288 (when filename
1289 (setq filename (gnus-map-function mm-file-name-rewrite-functions
1290 (file-name-nondirectory filename))))
1291 (setq file
1292 (read-file-name
1293 (or prompt
1294 (format "Save MIME part to (default %s): "
1295 (or filename "")))
1296 (or mm-default-directory default-directory)
1297 (expand-file-name (or filename "")
1298 (or mm-default-directory default-directory))))
1299 (if (file-directory-p file)
1300 (setq file (expand-file-name filename file))
1301 (setq file (expand-file-name
1302 file (or mm-default-directory default-directory))))
1303 (setq mm-default-directory (file-name-directory file))
1304 (and (or (not (file-exists-p file))
1305 (yes-or-no-p (format "File %s already exists; overwrite? "
1306 file)))
1307 (progn
1308 (mm-save-part-to-file handle file)
1309 file))))
1311 (defun mm-add-meta-html-tag (handle &optional charset force-charset)
1312 "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
1313 CHARSET defaults to the one HANDLE specifies. Existing meta tag that
1314 specifies charset will not be modified unless FORCE-CHARSET is non-nil.
1315 Return t if meta tag is added or replaced."
1316 (when (equal (mm-handle-media-type handle) "text/html")
1317 (when (or charset
1318 (setq charset (mail-content-type-get (mm-handle-type handle)
1319 'charset)))
1320 (setq charset (format "\
1321 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
1322 (let ((case-fold-search t))
1323 (goto-char (point-min))
1324 (if (re-search-forward "\
1325 <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
1326 text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
1327 (if (and (not force-charset)
1328 (match-beginning 2)
1329 (string-match "\\`html\\'" (match-string 1)))
1330 ;; Don't modify existing meta tag.
1332 ;; Replace it with the one specifying charset.
1333 (replace-match charset)
1335 (if (re-search-forward "<head>\\s-*" nil t)
1336 (insert charset "\n")
1337 (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
1338 (insert "<head>\n" charset "\n</head>\n"))
1339 t)))))
1341 (defun mm-save-part-to-file (handle file)
1342 (mm-with-unibyte-buffer
1343 (mm-insert-part handle)
1344 (mm-add-meta-html-tag handle)
1345 (let ((current-file-modes (default-file-modes)))
1346 (set-default-file-modes mm-attachment-file-modes)
1347 (unwind-protect
1348 ;; Don't re-compress .gz & al. Arguably we should make
1349 ;; `file-name-handler-alist' nil, but that would chop
1350 ;; ange-ftp, which is reasonable to use here.
1351 (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
1352 (set-default-file-modes current-file-modes)))))
1354 (defun mm-pipe-part (handle &optional cmd)
1355 "Pipe HANDLE to a process.
1356 Use CMD as the process."
1357 (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
1358 (command (or cmd
1359 (gnus-read-shell-command
1360 "Shell command on MIME part: " mm-last-shell-command))))
1361 (mm-with-unibyte-buffer
1362 (mm-insert-part handle)
1363 (mm-add-meta-html-tag handle)
1364 (let ((coding-system-for-write 'binary))
1365 (shell-command-on-region (point-min) (point-max) command nil)))))
1367 (autoload 'gnus-completing-read "gnus-util")
1369 (defun mm-interactively-view-part (handle)
1370 "Display HANDLE using METHOD."
1371 (let* ((type (mm-handle-media-type handle))
1372 (methods
1373 (mapcar (lambda (i) (cdr (assoc 'viewer i)))
1374 (mailcap-mime-info type 'all)))
1375 (method (let ((minibuffer-local-completion-map
1376 mm-viewer-completion-map))
1377 (completing-read "Viewer: " methods))))
1378 (when (string= method "")
1379 (error "No method given"))
1380 (if (string-match "^[^% \t]+$" method)
1381 (setq method (concat method " %s")))
1382 (mm-display-external handle method)))
1384 (defun mm-preferred-alternative (handles &optional preferred)
1385 "Say which of HANDLES are preferred."
1386 (let ((prec (if preferred (list preferred)
1387 (mm-preferred-alternative-precedence handles)))
1388 p h result type handle)
1389 (while (setq p (pop prec))
1390 (setq h handles)
1391 (while h
1392 (setq handle (car h))
1393 (setq type (mm-handle-media-type handle))
1394 (when (and (equal p type)
1395 (mm-automatic-display-p handle)
1396 (or (stringp (car handle))
1397 (not (mm-handle-disposition handle))
1398 (equal (car (mm-handle-disposition handle))
1399 "inline")))
1400 (setq result handle
1401 h nil
1402 prec nil))
1403 (pop h)))
1404 result))
1406 (defun mm-preferred-alternative-precedence (handles)
1407 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1408 (setq handles (reverse handles))
1409 (dolist (disc (reverse mm-discouraged-alternatives))
1410 (dolist (handle (copy-sequence handles))
1411 (when (string-match disc (mm-handle-media-type handle))
1412 (setq handles (nconc (delete handle handles) (list handle))))))
1413 ;; Remove empty parts.
1414 (dolist (handle (copy-sequence handles))
1415 (when (and (bufferp (mm-handle-buffer handle))
1416 (not (with-current-buffer (mm-handle-buffer handle)
1417 (goto-char (point-min))
1418 (re-search-forward "[^ \t\n]" nil t))))
1419 (setq handles (nconc (delete handle handles) (list handle)))))
1420 (mapcar #'mm-handle-media-type handles))
1422 (defun mm-get-content-id (id)
1423 "Return the handle(s) referred to by ID."
1424 (cdr (assoc id mm-content-id-alist)))
1426 (defconst mm-image-type-regexps
1427 '(("/\\*.*XPM.\\*/" . xpm)
1428 ("P[1-6]" . pbm)
1429 ("GIF8" . gif)
1430 ("\377\330" . jpeg)
1431 ("\211PNG\r\n" . png)
1432 ("#define" . xbm)
1433 ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
1434 ("%!PS" . postscript))
1435 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
1436 When the first bytes of an image file match REGEXP, it is assumed to
1437 be of image type IMAGE-TYPE.")
1439 ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
1440 (defun mm-image-type-from-buffer ()
1441 "Determine the image type from data in the current buffer.
1442 Value is a symbol specifying the image type or nil if type cannot
1443 be determined."
1444 (let ((types mm-image-type-regexps)
1445 type)
1446 (goto-char (point-min))
1447 (while (and types (null type))
1448 (let ((regexp (car (car types)))
1449 (image-type (cdr (car types))))
1450 (when (looking-at regexp)
1451 (setq type image-type))
1452 (setq types (cdr types))))
1453 type))
1455 (defun mm-get-image (handle)
1456 "Return an image instance based on HANDLE."
1457 (let ((type (mm-handle-media-subtype handle))
1458 spec)
1459 ;; Allow some common translations.
1460 (setq type
1461 (cond
1462 ((equal type "x-pixmap")
1463 "xpm")
1464 ((equal type "x-xbitmap")
1465 "xbm")
1466 ((equal type "x-portable-bitmap")
1467 "pbm")
1468 (t type)))
1469 (or (mm-handle-cache handle)
1470 (mm-with-unibyte-buffer
1471 (mm-insert-part handle)
1472 (prog1
1473 (setq spec
1474 (ignore-errors
1475 ;; Avoid testing `make-glyph' since W3 may define
1476 ;; a bogus version of it.
1477 (if (fboundp 'create-image)
1478 (create-image (buffer-string)
1479 (or (mm-image-type-from-buffer)
1480 (intern type))
1481 'data-p)
1482 (mm-create-image-xemacs type))))
1483 (mm-handle-set-cache handle spec))))))
1485 (defun mm-create-image-xemacs (type)
1486 (when (featurep 'xemacs)
1487 (cond
1488 ((equal type "xbm")
1489 ;; xbm images require special handling, since
1490 ;; the only way to create glyphs from these
1491 ;; (without a ton of work) is to write them
1492 ;; out to a file, and then create a file
1493 ;; specifier.
1494 (let ((file (mm-make-temp-file
1495 (expand-file-name "emm" mm-tmp-directory)
1496 nil ".xbm")))
1497 (unwind-protect
1498 (progn
1499 (write-region (point-min) (point-max) file)
1500 (make-glyph (list (cons 'x file))))
1501 (ignore-errors
1502 (delete-file file)))))
1504 (make-glyph
1505 (vector
1506 (or (mm-image-type-from-buffer)
1507 (intern type))
1508 :data (buffer-string)))))))
1510 (declare-function image-size "image.c" (spec &optional pixels frame))
1512 (defun mm-image-fit-p (handle)
1513 "Say whether the image in HANDLE will fit the current window."
1514 (let ((image (mm-get-image handle)))
1515 (or (not image)
1516 (if (featurep 'xemacs)
1517 ;; XEmacs's glyphs can actually tell us about their width, so
1518 ;; let's be nice and smart about them.
1519 (or mm-inline-large-images
1520 (and (<= (glyph-width image) (window-pixel-width))
1521 (<= (glyph-height image) (window-pixel-height))))
1522 (let* ((size (image-size image))
1523 (w (car size))
1524 (h (cdr size)))
1525 (or mm-inline-large-images
1526 (and (<= h (1- (window-height))) ; Don't include mode line.
1527 (<= w (window-width)))))))))
1529 (defun mm-valid-image-format-p (format)
1530 "Say whether FORMAT can be displayed natively by Emacs."
1531 (cond
1532 ;; Handle XEmacs
1533 ((fboundp 'valid-image-instantiator-format-p)
1534 (valid-image-instantiator-format-p format))
1535 ;; Handle Emacs
1536 ((fboundp 'image-type-available-p)
1537 (and (display-graphic-p)
1538 (image-type-available-p format)))
1539 ;; Nobody else can do images yet.
1541 nil)))
1543 (defun mm-valid-and-fit-image-p (format handle)
1544 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1545 (and (mm-valid-image-format-p format)
1546 (mm-image-fit-p handle)))
1548 (defun mm-find-part-by-type (handles type &optional notp recursive)
1549 "Search in HANDLES for part with TYPE.
1550 If NOTP, returns first non-matching part.
1551 If RECURSIVE, search recursively."
1552 (let (handle)
1553 (while handles
1554 (if (and recursive (stringp (caar handles)))
1555 (if (setq handle (mm-find-part-by-type (cdar handles) type
1556 notp recursive))
1557 (setq handles nil))
1558 (if (if notp
1559 (not (equal (mm-handle-media-type (car handles)) type))
1560 (equal (mm-handle-media-type (car handles)) type))
1561 (setq handle (car handles)
1562 handles nil)))
1563 (setq handles (cdr handles)))
1564 handle))
1566 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1567 (goto-char (point-min))
1568 (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1569 'boundary)))
1570 (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1571 start
1572 (end (save-excursion
1573 (goto-char (point-max))
1574 (if (re-search-backward close-delimiter nil t)
1575 (match-beginning 0)
1576 (point-max))))
1577 result)
1578 (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1579 (while (and (not result)
1580 (re-search-forward boundary end t))
1581 (goto-char (match-beginning 0))
1582 (when start
1583 (save-excursion
1584 (save-restriction
1585 (narrow-to-region start (1- (point)))
1586 (when (let* ((ct (mail-fetch-field "content-type"))
1587 (ctl (and ct (mail-header-parse-content-type ct))))
1588 (if notp
1589 (not (equal (car ctl) type))
1590 (equal (car ctl) type)))
1591 (setq result (buffer-string))))))
1592 (forward-line 1)
1593 (setq start (point)))
1594 (when (and (not result) start)
1595 (save-excursion
1596 (save-restriction
1597 (narrow-to-region start end)
1598 (when (let* ((ct (mail-fetch-field "content-type"))
1599 (ctl (and ct (mail-header-parse-content-type ct))))
1600 (if notp
1601 (not (equal (car ctl) type))
1602 (equal (car ctl) type)))
1603 (setq result (buffer-string))))))
1604 result))
1606 (defvar mm-security-handle nil)
1608 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1609 ;; HANDLE could be a CTL.
1610 (when handle
1611 (put-text-property 0 (length (car handle)) parameter value
1612 (car handle))))
1614 (autoload 'mm-view-pkcs7 "mm-view")
1616 (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
1617 (let ((type (car ctl))
1618 (subtype (cadr (split-string (car ctl) "/")))
1619 (mm-security-handle ctl) ;; (car CTL) is the type.
1620 protocol func functest)
1621 (cond
1622 ((or (equal type "application/x-pkcs7-mime")
1623 (equal type "application/pkcs7-mime"))
1624 (with-temp-buffer
1625 (when (and (cond
1626 ((eq mm-decrypt-option 'never) nil)
1627 ((eq mm-decrypt-option 'always) t)
1628 ((eq mm-decrypt-option 'known) t)
1629 (t (y-or-n-p
1630 (format "Decrypt (S/MIME) part? "))))
1631 (mm-view-pkcs7 parts from))
1632 (setq parts (mm-dissect-buffer t)))))
1633 ((equal subtype "signed")
1634 (unless (and (setq protocol
1635 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1636 (not (equal protocol "multipart/mixed")))
1637 ;; The message is broken or draft-ietf-openpgp-multsig-01.
1638 (let ((protocols mm-verify-function-alist))
1639 (while protocols
1640 (if (and (or (not (setq functest (nth 3 (car protocols))))
1641 (funcall functest parts ctl))
1642 (mm-find-part-by-type parts (caar protocols) nil t))
1643 (setq protocol (caar protocols)
1644 protocols nil)
1645 (setq protocols (cdr protocols))))))
1646 (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1647 (when (cond
1648 ((eq mm-verify-option 'never) nil)
1649 ((eq mm-verify-option 'always) t)
1650 ((eq mm-verify-option 'known)
1651 (and func
1652 (or (not (setq functest
1653 (nth 3 (assoc protocol
1654 mm-verify-function-alist))))
1655 (funcall functest parts ctl))))
1657 (y-or-n-p
1658 (format "Verify signed (%s) part? "
1659 (or (nth 2 (assoc protocol mm-verify-function-alist))
1660 (format "protocol=%s" protocol))))))
1661 (save-excursion
1662 (if func
1663 (setq parts (funcall func parts ctl))
1664 (mm-set-handle-multipart-parameter
1665 mm-security-handle 'gnus-details
1666 (format "Unknown sign protocol (%s)" protocol))))))
1667 ((equal subtype "encrypted")
1668 (unless (setq protocol
1669 (mm-handle-multipart-ctl-parameter ctl 'protocol))
1670 ;; The message is broken.
1671 (let ((parts parts))
1672 (while parts
1673 (if (assoc (mm-handle-media-type (car parts))
1674 mm-decrypt-function-alist)
1675 (setq protocol (mm-handle-media-type (car parts))
1676 parts nil)
1677 (setq parts (cdr parts))))))
1678 (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1679 (when (cond
1680 ((eq mm-decrypt-option 'never) nil)
1681 ((eq mm-decrypt-option 'always) t)
1682 ((eq mm-decrypt-option 'known)
1683 (and func
1684 (or (not (setq functest
1685 (nth 3 (assoc protocol
1686 mm-decrypt-function-alist))))
1687 (funcall functest parts ctl))))
1689 (y-or-n-p
1690 (format "Decrypt (%s) part? "
1691 (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1692 (format "protocol=%s" protocol))))))
1693 (save-excursion
1694 (if func
1695 (setq parts (funcall func parts ctl))
1696 (mm-set-handle-multipart-parameter
1697 mm-security-handle 'gnus-details
1698 (format "Unknown encrypt protocol (%s)" protocol))))))
1699 (t nil))
1700 parts))
1702 (defun mm-multiple-handles (handles)
1703 (and (listp handles)
1704 (> (length handles) 1)
1705 (or (listp (car handles))
1706 (stringp (car handles)))))
1708 (defun mm-complicated-handles (handles)
1709 (and (listp (car handles))
1710 (> (length handles) 1)))
1712 (defun mm-merge-handles (handles1 handles2)
1713 (append
1714 (if (listp (car handles1))
1715 handles1
1716 (list handles1))
1717 (if (listp (car handles2))
1718 handles2
1719 (list handles2))))
1721 (defun mm-readable-p (handle)
1722 "Say whether the content of HANDLE is readable."
1723 (and (< (with-current-buffer (mm-handle-buffer handle)
1724 (buffer-size)) 10000)
1725 (mm-with-unibyte-buffer
1726 (mm-insert-part handle)
1727 (and (eq (mm-body-7-or-8) '7bit)
1728 (not (mm-long-lines-p 76))))))
1730 (declare-function libxml-parse-html-region "xml.c"
1731 (start end &optional base-url))
1732 (declare-function shr-insert-document "shr" (dom))
1733 (defvar shr-blocked-images)
1734 (defvar gnus-inhibit-images)
1735 (autoload 'gnus-blocked-images "gnus-art")
1737 (defun mm-shr (handle)
1738 ;; Require since we bind its variables.
1739 (require 'shr)
1740 (let ((article-buffer (current-buffer))
1741 (shr-content-function (lambda (id)
1742 (let ((handle (mm-get-content-id id)))
1743 (when handle
1744 (mm-with-part handle
1745 (buffer-string))))))
1746 shr-inhibit-images shr-blocked-images charset char)
1747 (if (and (boundp 'gnus-summary-buffer)
1748 (bufferp gnus-summary-buffer)
1749 (buffer-name gnus-summary-buffer))
1750 (with-current-buffer gnus-summary-buffer
1751 (setq shr-inhibit-images gnus-inhibit-images
1752 shr-blocked-images (gnus-blocked-images)))
1753 (setq shr-inhibit-images gnus-inhibit-images
1754 shr-blocked-images (gnus-blocked-images)))
1755 (unless handle
1756 (setq handle (mm-dissect-buffer t)))
1757 (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
1758 (save-restriction
1759 (narrow-to-region (point) (point))
1760 (shr-insert-document
1761 (mm-with-part handle
1762 (insert (prog1
1763 (if (and charset
1764 (setq charset
1765 (mm-charset-to-coding-system charset))
1766 (not (eq charset 'ascii)))
1767 (mm-decode-coding-string (buffer-string) charset)
1768 (mm-string-as-multibyte (buffer-string)))
1769 (erase-buffer)
1770 (mm-enable-multibyte)))
1771 (goto-char (point-min))
1772 (setq case-fold-search t)
1773 (while (re-search-forward
1774 "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
1775 (when (setq char
1776 (cdr (assq (if (match-beginning 1)
1777 (string-to-number (match-string 1) 16)
1778 (string-to-number (match-string 2)))
1779 mm-extra-numeric-entities)))
1780 (replace-match (char-to-string char))))
1781 ;; Remove "soft hyphens".
1782 (goto-char (point-min))
1783 (while (search-forward "­" nil t)
1784 (replace-match "" t t))
1785 (libxml-parse-html-region (point-min) (point-max))))
1786 (unless (bobp)
1787 (insert "\n"))
1788 (mm-handle-set-undisplayer
1789 handle
1790 `(lambda ()
1791 (let ((inhibit-read-only t))
1792 (delete-region ,(point-min-marker)
1793 ,(point-max-marker))))))))
1795 (defun mm-handle-filename (handle)
1796 "Return filename of HANDLE if any."
1797 (or (mail-content-type-get (mm-handle-type handle)
1798 'name)
1799 (mail-content-type-get (mm-handle-disposition handle)
1800 'filename)))
1802 (provide 'mm-decode)
1804 ;;; mm-decode.el ends here