ff8973949d91df47e7a8bf8bc58f567c0d8f71df
[elisp/gnus.git-] / lisp / mm-decode.el
1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; Jaap-Henk Hoepman (jhh@xs4all.nl): 
26 ;;
27 ;; Added support for delayed destroy of external MIME viewers. All external
28 ;; viewers for mime types in mm-keep-viewer-alive-types will remain active
29 ;; after switching articles or groups, and will only be removed when exiting
30 ;; gnus.
31 ;; 
32
33 ;;; Code:
34
35 (require 'mail-parse)
36 (require 'gnus-mailcap)
37 (require 'mm-bodies)
38 (eval-when-compile (require 'cl)
39                    (require 'term))
40
41 (eval-and-compile
42   (autoload 'mm-inline-partial "mm-partial")
43   (autoload 'mm-inline-external-body "mm-extern")
44   (autoload 'mm-insert-inline "mm-view"))
45
46 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
47
48 (defgroup mime-display ()
49   "Display of MIME in mail and news articles."
50   :link '(custom-manual "(emacs-mime)Customization")
51   :version "21.1"
52   :group 'mail
53   :group 'news
54   :group 'multimedia)
55
56 (defgroup mime-security ()
57   "MIME security in mail and news articles."
58   :link '(custom-manual "(emacs-mime)Customization")
59   :group 'mail
60   :group 'news
61   :group 'multimedia)
62
63 ;;; Convenience macros.
64
65 (defmacro mm-handle-buffer (handle)
66   `(nth 0 ,handle))
67 (defmacro mm-handle-type (handle)
68   `(nth 1 ,handle))
69 (defsubst mm-handle-media-type (handle)
70   (if (stringp (car handle))
71       (car handle)
72     (car (mm-handle-type handle))))
73 (defsubst mm-handle-media-supertype (handle)
74   (car (split-string (mm-handle-media-type handle) "/")))
75 (defsubst mm-handle-media-subtype (handle)
76   (cadr (split-string (mm-handle-media-type handle) "/")))
77 (defmacro mm-handle-encoding (handle)
78   `(nth 2 ,handle))
79 (defmacro mm-handle-undisplayer (handle)
80   `(nth 3 ,handle))
81 (defmacro mm-handle-set-undisplayer (handle function)
82   `(setcar (nthcdr 3 ,handle) ,function))
83 (defmacro mm-handle-disposition (handle)
84   `(nth 4 ,handle))
85 (defmacro mm-handle-description (handle)
86   `(nth 5 ,handle))
87 (defmacro mm-handle-cache (handle)
88   `(nth 6 ,handle))
89 (defmacro mm-handle-set-cache (handle contents)
90   `(setcar (nthcdr 6 ,handle) ,contents))
91 (defmacro mm-handle-id (handle)
92   `(nth 7 ,handle))
93 (defmacro mm-handle-multipart-original-buffer (handle)
94   `(get-text-property 0 'buffer (car ,handle)))
95 (defmacro mm-handle-multipart-from (handle)
96   `(get-text-property 0 'from (car ,handle)))
97 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
98   `(get-text-property 0 ,parameter (car ,handle)))
99
100 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
101                                     disposition description cache
102                                     id)
103   `(list ,buffer ,type ,encoding ,undisplayer
104          ,disposition ,description ,cache ,id))
105
106 (defcustom mm-inline-media-tests
107   '(("image/jpeg"
108      mm-inline-image
109      (lambda (handle)
110        (mm-valid-and-fit-image-p 'jpeg handle)))
111     ("image/png"
112      mm-inline-image
113      (lambda (handle)
114        (mm-valid-and-fit-image-p 'png handle)))
115     ("image/gif"
116      mm-inline-image
117      (lambda (handle)
118        (mm-valid-and-fit-image-p 'gif handle)))
119     ("image/tiff"
120      mm-inline-image
121      (lambda (handle)
122        (mm-valid-and-fit-image-p 'tiff handle)) )
123     ("image/xbm"
124      mm-inline-image
125      (lambda (handle)
126        (mm-valid-and-fit-image-p 'xbm handle)))
127     ("image/x-xbitmap"
128      mm-inline-image
129      (lambda (handle)
130        (mm-valid-and-fit-image-p 'xbm handle)))
131     ("image/xpm"
132      mm-inline-image
133      (lambda (handle)
134        (mm-valid-and-fit-image-p 'xpm handle)))
135     ("image/x-pixmap"
136      mm-inline-image
137      (lambda (handle)
138        (mm-valid-and-fit-image-p 'xpm handle)))
139     ("image/bmp"
140      mm-inline-image
141      (lambda (handle)
142        (mm-valid-and-fit-image-p 'bmp handle)))
143     ("image/x-portable-bitmap"
144      mm-inline-image
145      (lambda (handle)
146        (mm-valid-and-fit-image-p 'pbm handle)))
147     ("text/plain" mm-inline-text identity)
148     ("text/enriched" mm-inline-text identity)
149     ("text/richtext" mm-inline-text identity)
150     ("text/x-patch" mm-display-patch-inline
151      (lambda (handle)
152        (locate-library "diff-mode")))
153     ("application/emacs-lisp" mm-display-elisp-inline identity)
154     ("text/html"
155      mm-inline-text
156      (lambda (handle)
157        (locate-library "w3")))
158     ("text/x-vcard"
159      mm-inline-text
160      (lambda (handle)
161        (or (featurep 'vcard)
162            (locate-library "vcard"))))
163     ("message/delivery-status" mm-inline-text identity)
164     ("message/rfc822" mm-inline-message identity)
165     ("message/partial" mm-inline-partial identity)
166     ("message/external-body" mm-inline-external-body identity)
167     ("text/.*" mm-inline-text identity)
168     ("audio/wav" mm-inline-audio
169      (lambda (handle)
170        (and (or (featurep 'nas-sound) (featurep 'native-sound))
171             (device-sound-enabled-p))))
172     ("audio/au"
173      mm-inline-audio
174      (lambda (handle)
175        (and (or (featurep 'nas-sound) (featurep 'native-sound))
176             (device-sound-enabled-p))))
177     ("application/pgp-signature" ignore identity)
178     ("application/x-pkcs7-signature" ignore identity)
179     ("application/pkcs7-signature" ignore identity)
180     ("multipart/alternative" ignore identity)
181     ("multipart/mixed" ignore identity)
182     ("multipart/related" ignore identity)
183     ;; Default to displaying as text
184     (".*" mm-inline-text identity))
185   "Alist of media types/tests saying whether types can be displayed inline."
186   :type '(repeat (list (string :tag "MIME type")
187                        (function :tag "Display function")
188                        (function :tag "Display test")))
189   :group 'mime-display)
190
191 (defcustom mm-inlined-types
192   '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
193     "message/partial" "message/external-body" "application/emacs-lisp"
194     "application/pgp-signature" "application/x-pkcs7-signature"
195     "application/pkcs7-signature")
196   "List of media types that are to be displayed inline.
197 See also `mm-inline-media-tests', which says how to display a media
198 type inline."
199   :type '(repeat string)
200   :group 'mime-display)
201
202 (defcustom mm-keep-viewer-alive-types
203   '("application/postscript" "application/msword" "application/vnd.ms-excel"
204     "application/pdf" "application/x-dvi")
205   "List of media types for which the external viewer will not be killed
206 when selecting a different article."
207   :type '(repeat string)
208   :group 'mime-display)
209  
210 (defcustom mm-automatic-display
211   '("text/plain" "text/enriched" "text/richtext" "text/html"
212     "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
213     "message/rfc822" "text/x-patch" "application/pgp-signature"
214     "application/emacs-lisp" "application/x-pkcs7-signature"
215     "application/pkcs7-signature")
216   "A list of MIME types to be displayed automatically."
217   :type '(repeat string)
218   :group 'mime-display)
219
220 (defcustom mm-attachment-override-types '("text/x-vcard")
221   "Types to have \"attachment\" ignored if they can be displayed inline."
222   :type '(repeat string)
223   :group 'mime-display)
224
225 (defcustom mm-inline-override-types nil
226   "Types to be treated as attachments even if they can be displayed inline."
227   :type '(repeat string)
228   :group 'mime-display)
229
230 (defcustom mm-automatic-external-display nil
231   "List of MIME type regexps that will be displayed externally automatically."
232   :type '(repeat string)
233   :group 'mime-display)
234
235 (defcustom mm-discouraged-alternatives nil
236   "List of MIME types that are discouraged when viewing multipart/alternative.
237 Viewing agents are supposed to view the last possible part of a message,
238 as that is supposed to be the richest.  However, users may prefer other
239 types instead, and this list says what types are most unwanted.  If,
240 for instance, text/html parts are very unwanted, and text/richtext are
241 somewhat unwanted, then the value of this variable should be set
242 to:
243
244  (\"text/html\" \"text/richtext\")"
245   :type '(repeat string)
246   :group 'mime-display)
247
248 (defcustom mm-tmp-directory
249   (cond ((fboundp 'temp-directory) (temp-directory))
250         ((boundp 'temporary-file-directory) temporary-file-directory)
251         ("/tmp/"))
252   "Where mm will store its temporary files."
253   :type 'directory
254   :group 'mime-display)
255
256 (defcustom mm-inline-large-images nil
257   "If non-nil, then all images fit in the buffer."
258   :type 'boolean
259   :group 'mime-display)
260
261 (defvar mm-file-name-rewrite-functions nil
262   "*List of functions used for rewriting file names of MIME parts.
263 Each function takes a file name as input and returns a file name.
264
265 Ready-made functions include
266 `mm-file-name-delete-whitespace',
267 `mm-file-name-trim-whitespace',
268 `mm-file-name-collapse-whitespace',
269 `mm-file-name-replace-whitespace',
270 `capitalize', `downcase', `upcase', and
271 `upcase-initials'.")
272
273 (defvar mm-file-name-replace-whitespace nil
274   "String used for replacing whitespace characters; default is `\"_\"'.")
275
276 (defcustom mm-default-directory nil
277   "The default directory where mm will save files.
278 If not set, `default-directory' will be used."
279   :type 'directory
280   :group 'mime-display)
281
282 (defcustom mm-external-terminal-program "xterm"
283   "The program to start an external terminal."
284   :type 'string
285   :group 'mime-display)
286
287 ;;; Internal variables.
288
289 (defvar mm-dissection-list nil)
290 (defvar mm-last-shell-command "")
291 (defvar mm-content-id-alist nil)
292 (defvar mm-postponed-undisplay-list nil)
293
294 ;; According to RFC2046, in particular, in a digest, the default
295 ;; Content-Type value for a body part is changed from "text/plain" to
296 ;; "message/rfc822".
297 (defvar mm-dissect-default-type "text/plain")
298
299 (autoload 'mml2015-verify "mml2015")
300 (autoload 'mml2015-verify-test "mml2015")
301 (autoload 'mml-smime-verify "mml-smime")
302 (autoload 'mml-smime-verify-test "mml-smime")
303
304 (defvar mm-verify-function-alist
305   '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
306     ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
307      mm-uu-pgp-signed-test)
308     ("application/pkcs7-signature" mml-smime-verify "S/MIME"
309      mml-smime-verify-test)
310     ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
311      mml-smime-verify-test)))
312
313 (defcustom mm-verify-option 'never
314   "Option of verifying signed parts.
315 `never', not verify; `always', always verify;
316 `known', only verify known protocols. Otherwise, ask user."
317   :type '(choice (item always)
318                  (item never)
319                  (item :tag "only known protocols" known)
320                  (item :tag "ask" nil))
321   :group 'mime-security)
322
323 (autoload 'mml2015-decrypt "mml2015")
324 (autoload 'mml2015-decrypt-test "mml2015")
325
326 (defvar mm-decrypt-function-alist
327   '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
328     ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
329      mm-uu-pgp-encrypted-test)))
330
331 (defcustom mm-decrypt-option nil
332   "Option of decrypting encrypted parts.
333 `never', not decrypt; `always', always decrypt;
334 `known', only decrypt known protocols. Otherwise, ask user."
335   :type '(choice (item always)
336                  (item never)
337                  (item :tag "only known protocols" known)
338                  (item :tag "ask" nil))
339   :group 'mime-security)
340
341 (defvar mm-viewer-completion-map
342   (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
343     (set-keymap-parent map minibuffer-local-completion-map)
344     map)
345   "Keymap for input viewer with completion.")
346
347 ;; Should we bind other key to minibuffer-complete-word?
348 (define-key mm-viewer-completion-map " " 'self-insert-command)
349
350 (defvar mm-viewer-completion-map
351   (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
352     (set-keymap-parent map minibuffer-local-completion-map)
353     map)
354   "Keymap for input viewer with completion.")
355
356 ;; Should we bind other key to minibuffer-complete-word?
357 (define-key mm-viewer-completion-map " " 'self-insert-command)
358
359 ;;; The functions.
360
361 (defun mm-alist-to-plist (alist)
362   "Convert association list ALIST into the equivalent property-list form.
363 The plist is returned.  This converts from
364
365 \((a . 1) (b . 2) (c . 3))
366
367 into
368
369 \(a 1 b 2 c 3)
370
371 The original alist is not modified.  See also `destructive-alist-to-plist'."
372   (let (plist)
373     (while alist
374       (let ((el (car alist)))
375         (setq plist (cons (cdr el) (cons (car el) plist))))
376       (setq alist (cdr alist)))
377     (nreverse plist)))
378
379 (defun mm-keep-viewer-alive-p (handle)
380   "Say whether external viewer for HANDLE should stay alive."
381   (let ((types mm-keep-viewer-alive-types)
382         (type (mm-handle-media-type handle))
383         ty)
384     (catch 'found
385       (while (setq ty (pop types))
386         (when (string-match ty type)
387           (throw 'found t))))))
388
389 (defun mm-handle-set-external-undisplayer (handle function)
390  "Set the undisplayer for this handle; postpone undisplaying of viewers
391 for types in mm-keep-viewer-alive-types."
392  (if (mm-keep-viewer-alive-p handle)
393      (let ((new-handle (copy-sequence handle)))
394        (mm-handle-set-undisplayer new-handle function)
395        (mm-handle-set-undisplayer handle nil)
396        (push new-handle mm-postponed-undisplay-list))
397    (mm-handle-set-undisplayer handle function)))
398
399 (defun mm-destroy-postponed-undisplay-list ()
400   (message "Destroying external MIME viewers")
401   (mm-destroy-parts mm-postponed-undisplay-list))
402
403 (defun mm-dissect-buffer (&optional no-strict-mime)
404   "Dissect the current buffer and return a list of MIME handles."
405   (save-excursion
406     (let (ct ctl type subtype cte cd description id result from)
407       (save-restriction
408         (mail-narrow-to-head)
409         (when (or no-strict-mime
410                   (mail-fetch-field "mime-version"))
411           (setq ct (mail-fetch-field "content-type")
412                 ctl (ignore-errors (mail-header-parse-content-type ct))
413                 cte (mail-fetch-field "content-transfer-encoding")
414                 cd (mail-fetch-field "content-disposition")
415                 description (mail-fetch-field "content-description")
416                 from (mail-fetch-field "from")
417                 id (mail-fetch-field "content-id"))
418           ;; FIXME: In some circumstances, this code is running within
419           ;; an unibyte macro.  mail-extract-address-components
420           ;; creates unibyte buffers. This `if', though not a perfect
421           ;; solution, avoids most of them.
422           (if from
423               (setq from (cadr (mail-extract-address-components from))))))
424       (when cte
425         (setq cte (mail-header-strip cte)))
426       (if (or (not ctl)
427               (not (string-match "/" (car ctl))))
428           (mm-dissect-singlepart
429            (list mm-dissect-default-type)
430            (and cte (intern (downcase (mail-header-remove-whitespace
431                                        (mail-header-remove-comments
432                                         cte)))))
433            no-strict-mime
434            (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
435            description)
436         (setq type (split-string (car ctl) "/"))
437         (setq subtype (cadr type)
438               type (pop type))
439         (setq
440          result
441          (cond
442           ((equal type "multipart")
443            (let ((mm-dissect-default-type (if (equal subtype "digest")
444                                               "message/rfc822"
445                                             "text/plain")))
446              (add-text-properties 0 (length (car ctl))
447                                   (mm-alist-to-plist (cdr ctl)) (car ctl))
448
449              ;; what really needs to be done here is a way to link a
450              ;; MIME handle back to it's parent MIME handle (in a multilevel
451              ;; MIME article).  That would probably require changing
452              ;; the mm-handle API so we simply store the multipart buffert
453              ;; name as a text property of the "multipart/whatever" string.
454              (add-text-properties 0 (length (car ctl))
455                                   (list 'buffer (mm-copy-to-buffer))
456                                   (car ctl))
457              (add-text-properties 0 (length (car ctl))
458                                   (list 'from from)
459                                   (car ctl))
460              (cons (car ctl) (mm-dissect-multipart ctl))))
461           (t
462            (mm-dissect-singlepart
463             ctl
464             (and cte (intern (downcase (mail-header-remove-whitespace
465                                         (mail-header-remove-comments
466                                          cte)))))
467             no-strict-mime
468             (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
469             description id))))
470         (when id
471           (when (string-match " *<\\(.*\\)> *" id)
472             (setq id (match-string 1 id)))
473           (push (cons id result) mm-content-id-alist))
474         result))))
475
476 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
477   (when (or force
478             (if (equal "text/plain" (car ctl))
479                 (assoc 'format ctl)
480               t))
481     (let ((res (mm-make-handle
482                 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
483       (push (car res) mm-dissection-list)
484       res)))
485
486 (defun mm-remove-all-parts ()
487   "Remove all MIME handles."
488   (interactive)
489   (mapcar 'mm-remove-part mm-dissection-list)
490   (setq mm-dissection-list nil))
491
492 (defun mm-dissect-multipart (ctl)
493   (goto-char (point-min))
494   (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
495          (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
496          start parts
497          (end (save-excursion
498                 (goto-char (point-max))
499                 (if (re-search-backward close-delimiter nil t)
500                     (match-beginning 0)
501                   (point-max)))))
502     (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
503     (while (and (< (point) end) (re-search-forward boundary end t))
504       (goto-char (match-beginning 0))
505       (when start
506         (save-excursion
507           (save-restriction
508             (narrow-to-region start (point))
509             (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
510       (forward-line 2)
511       (setq start (point)))
512     (when (and start (< start end))
513       (save-excursion
514         (save-restriction
515           (narrow-to-region start end)
516           (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
517     (mm-possibly-verify-or-decrypt (nreverse parts) ctl)))
518
519 (defun mm-copy-to-buffer ()
520   "Copy the contents of the current buffer to a fresh buffer."
521   (save-excursion
522     (let ((flag enable-multibyte-characters)
523           (new-buffer (generate-new-buffer " *mm*")))
524       (goto-char (point-min))
525       (search-forward-regexp "^\n" nil t)
526       (save-restriction
527         (narrow-to-region (point) (point-max))
528         (when flag
529           (set-buffer-multibyte nil))
530         (copy-to-buffer new-buffer (point-min) (point-max))
531         (when flag
532           (set-buffer-multibyte t)))
533       new-buffer)))
534
535 (defun mm-display-parts (handle &optional no-default)
536   (if (stringp (car handle))
537       (mapcar 'mm-display-parts (cdr handle))
538     (if (bufferp (car handle))
539         (save-restriction
540           (narrow-to-region (point) (point))
541           (mm-display-part handle)
542           (goto-char (point-max)))
543       (mapcar 'mm-display-parts handle))))
544
545 (defun mm-display-part (handle &optional no-default)
546   "Display the MIME part represented by HANDLE.
547 Returns nil if the part is removed; inline if displayed inline;
548 external if displayed external."
549   (save-excursion
550     (mailcap-parse-mailcaps)
551     (if (mm-handle-displayed-p handle)
552         (mm-remove-part handle)
553       (let* ((type (mm-handle-media-type handle))
554              (method (mailcap-mime-info type)))
555         (if (and (mm-inlinable-p handle)
556                  (mm-inlined-p handle))
557             (progn
558               (forward-line 1)
559               (mm-display-inline handle)
560               'inline)
561           (when (or method
562                     (not no-default))
563             (if (and (not method)
564                      (equal "text" (car (split-string type))))
565                 (progn
566                   (forward-line 1)
567                   (mm-insert-inline handle (mm-get-part handle))
568                   'inline)
569               (mm-display-external
570                handle (or method 'mailcap-save-binary-file)))))))))
571
572 (defun mm-display-external (handle method)
573   "Display HANDLE using METHOD."
574   (let ((outbuf (current-buffer)))
575     (mm-with-unibyte-buffer
576       (if (functionp method)
577           (let ((cur (current-buffer)))
578             (if (eq method 'mailcap-save-binary-file)
579                 (progn
580                   (set-buffer (generate-new-buffer " *mm*"))
581                   (setq method nil))
582               (mm-insert-part handle)
583               (let ((win (get-buffer-window cur t)))
584                 (when win
585                   (select-window win)))
586               (switch-to-buffer (generate-new-buffer " *mm*")))
587             (buffer-disable-undo)
588             (mm-set-buffer-file-coding-system mm-binary-coding-system)
589             (insert-buffer-substring cur)
590             (goto-char (point-min))
591             (message "Viewing with %s" method)
592             (let ((mm (current-buffer))
593                   (non-viewer (assq 'non-viewer
594                                     (mailcap-mime-info
595                                      (mm-handle-media-type handle) t))))
596               (unwind-protect
597                   (if method
598                       (funcall method)
599                     (mm-save-part handle))
600                 (when (and (not non-viewer)
601                            method)
602                   (mm-handle-set-undisplayer handle mm)))))
603         ;; The function is a string to be executed.
604         (mm-insert-part handle)
605         (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
606                (filename (mail-content-type-get
607                           (mm-handle-disposition handle) 'filename))
608                (mime-info (mailcap-mime-info
609                            (mm-handle-media-type handle) t))
610                (needsterm (or (assoc "needsterm" mime-info)
611                               (assoc "needsterminal" mime-info)))
612                (copiousoutput (assoc "copiousoutput" mime-info))
613                file buffer)
614           ;; We create a private sub-directory where we store our files.
615           (make-directory dir)
616           (set-file-modes dir 448)
617           (if filename
618               (setq file (expand-file-name (file-name-nondirectory filename)
619                                            dir))
620             (setq file (make-temp-name (expand-file-name "mm." dir))))
621           (let ((coding-system-for-write mm-binary-coding-system))
622             (write-region (point-min) (point-max) file nil 'nomesg))
623           (message "Viewing with %s" method)
624           (cond (needsterm
625                  (unwind-protect
626                      (if window-system
627                          (start-process "*display*" nil
628                                         mm-external-terminal-program
629                                         "-e" shell-file-name
630                                         shell-command-switch
631                                         (mm-mailcap-command
632                                          method file (mm-handle-type handle)))
633                        (require 'term)
634                        (switch-to-buffer 
635                         (make-term "display"
636                                    shell-file-name
637                                    nil
638                                    shell-command-switch
639                                    (mm-mailcap-command
640                                     method file (mm-handle-type handle))))
641                        (term-mode)
642                        (term-char-mode))
643                    (mm-handle-set-external-undisplayer handle (cons file buffer)))
644                  (message "Displaying %s..." (format method file))
645                  'external)
646                 (copiousoutput
647                  (with-current-buffer outbuf
648                    (forward-line 1)
649                    (mm-insert-inline
650                     handle
651                     (unwind-protect
652                         (progn
653                           (call-process shell-file-name nil
654                                         (setq buffer
655                                               (generate-new-buffer " *mm*"))
656                                         nil
657                                         shell-command-switch
658                                         (mm-mailcap-command
659                                          method file (mm-handle-type handle)))
660                           (if (buffer-live-p buffer)
661                               (save-excursion
662                                 (set-buffer buffer)
663                                 (buffer-string))))
664                       (progn
665                         (ignore-errors (delete-file file))
666                         (ignore-errors (delete-directory
667                                         (file-name-directory file)))
668                         (ignore-errors (kill-buffer buffer))))))
669                  'inline)
670                 (t
671                  (unwind-protect
672                      (start-process "*display*"
673                                     (setq buffer
674                                           (generate-new-buffer " *mm*"))
675                                     shell-file-name
676                                     shell-command-switch
677                                     (mm-mailcap-command
678                                      method file (mm-handle-type handle)))
679                    (mm-handle-set-external-undisplayer handle (cons file buffer)))
680                  (message "Displaying %s..." (format method file))
681                  'external)))))))
682
683 (defun mm-mailcap-command (method file type-list)
684   (let ((ctl (cdr type-list))
685         (beg 0)
686         (uses-stdin t)
687         out sub total)
688     (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
689       (push (substring method beg (match-beginning 0)) out)
690       (setq beg (match-end 0)
691             total (match-string 0 method)
692             sub (match-string 1 method))
693       (cond
694        ((string= total "%%")
695         (push "%" out))
696        ((string= total "%s")
697         (setq uses-stdin nil)
698         (push (mm-quote-arg file) out))
699        ((string= total "%t")
700         (push (mm-quote-arg (car type-list)) out))
701        (t
702         (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
703     (push (substring method beg (length method)) out)
704     (if uses-stdin
705         (progn
706           (push "<" out)
707           (push (mm-quote-arg file) out)))
708     (mapconcat 'identity (nreverse out) "")))
709
710 (defun mm-remove-parts (handles)
711   "Remove the displayed MIME parts represented by HANDLES."
712   (if (and (listp handles)
713            (bufferp (car handles)))
714       (mm-remove-part handles)
715     (let (handle)
716       (while (setq handle (pop handles))
717         (cond
718          ((stringp handle)
719           (when (buffer-live-p (get-text-property 0 'buffer handle))
720             (kill-buffer (get-text-property 0 'buffer handle))))
721          ((and (listp handle)
722                (stringp (car handle)))
723           (mm-remove-parts (cdr handle)))
724          (t
725           (mm-remove-part handle)))))))
726
727 (defun mm-destroy-parts (handles)
728   "Remove the displayed MIME parts represented by HANDLES."
729   (if (and (listp handles)
730            (bufferp (car handles)))
731       (mm-destroy-part handles)
732     (let (handle)
733       (while (setq handle (pop handles))
734         (cond
735          ((stringp handle)
736           (when (buffer-live-p (get-text-property 0 'buffer handle))
737             (kill-buffer (get-text-property 0 'buffer handle))))
738          ((and (listp handle)
739                (stringp (car handle)))
740           (mm-destroy-parts handle))
741          (t
742           (mm-destroy-part handle)))))))
743
744 (defun mm-remove-part (handle)
745   "Remove the displayed MIME part represented by HANDLE."
746   (when (listp handle)
747     (let ((object (mm-handle-undisplayer handle)))
748       (ignore-errors
749         (cond
750          ;; Internally displayed part.
751          ((mm-annotationp object)
752           (delete-annotation object))
753          ((or (functionp object)
754               (and (listp object)
755                    (eq (car object) 'lambda)))
756           (funcall object))
757          ;; Externally displayed part.
758          ((consp object)
759           (ignore-errors (delete-file (car object)))
760           (ignore-errors (delete-directory (file-name-directory (car object))))
761           (ignore-errors (kill-buffer (cdr object))))
762          ((bufferp object)
763           (when (buffer-live-p object)
764             (kill-buffer object)))))
765       (mm-handle-set-undisplayer handle nil))))
766
767 (defun mm-display-inline (handle)
768   (let* ((type (mm-handle-media-type handle))
769          (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
770     (funcall function handle)
771     (goto-char (point-min))))
772
773 (defun mm-assoc-string-match (alist type)
774   (dolist (elem alist)
775     (when (string-match (car elem) type)
776       (return elem))))
777
778 (defun mm-automatic-display-p (handle)
779   "Say whether the user wants HANDLE to be displayed automatically."
780   (let ((methods mm-automatic-display)
781         (type (mm-handle-media-type handle))
782         method result)
783     (while (setq method (pop methods))
784       (when (and (not (mm-inline-override-p handle))
785                  (string-match method type))
786         (setq result t
787               methods nil)))
788     result))
789
790 (defun mm-inlinable-p (handle)
791   "Say whether HANDLE can be displayed inline."
792   (let ((alist mm-inline-media-tests)
793         (type (mm-handle-media-type handle))
794         test)
795     (while alist
796       (when (string-match (caar alist) type)
797         (setq test (caddar alist)
798               alist nil)
799         (setq test (funcall test handle)))
800       (pop alist))
801     test))
802
803 (defun mm-inlined-p (handle)
804   "Say whether the user wants HANDLE to be displayed inline."
805   (let ((methods mm-inlined-types)
806         (type (mm-handle-media-type handle))
807         method result)
808     (while (setq method (pop methods))
809       (when (and (not (mm-inline-override-p handle))
810                  (string-match method type))
811         (setq result t
812               methods nil)))
813     result))
814
815 (defun mm-attachment-override-p (handle)
816   "Say whether HANDLE should have attachment behavior overridden."
817   (let ((types mm-attachment-override-types)
818         (type (mm-handle-media-type handle))
819         ty)
820     (catch 'found
821       (while (setq ty (pop types))
822         (when (and (string-match ty type)
823                    (mm-inlinable-p handle))
824           (throw 'found t))))))
825
826 (defun mm-inline-override-p (handle)
827   "Say whether HANDLE should have inline behavior overridden."
828   (let ((types mm-inline-override-types)
829         (type (mm-handle-media-type handle))
830         ty)
831     (catch 'found
832       (while (setq ty (pop types))
833         (when (string-match ty type)
834           (throw 'found t))))))
835
836 (defun mm-automatic-external-display-p (type)
837   "Return the user-defined method for TYPE."
838   (let ((methods mm-automatic-external-display)
839         method result)
840     (while (setq method (pop methods))
841       (when (string-match method type)
842         (setq result t
843               methods nil)))
844     result))
845
846 (defun mm-destroy-part (handle)
847   "Destroy the data structures connected to HANDLE."
848   (when (listp handle)
849     (mm-remove-part handle)
850     (when (buffer-live-p (mm-handle-buffer handle))
851       (kill-buffer (mm-handle-buffer handle)))))
852
853 (defun mm-handle-displayed-p (handle)
854   "Say whether HANDLE is displayed or not."
855   (mm-handle-undisplayer handle))
856
857 ;;;
858 ;;; Functions for outputting parts
859 ;;;
860
861 (defun mm-get-part (handle)
862   "Return the contents of HANDLE as a string."
863   (mm-with-unibyte-buffer
864     (insert (with-current-buffer (mm-handle-buffer handle)
865               (mm-with-unibyte-current-buffer-mule4
866                 (buffer-string))))
867     (mm-decode-content-transfer-encoding
868      (mm-handle-encoding handle)
869      (mm-handle-media-type handle))
870     (buffer-string)))
871
872 (defun mm-insert-part (handle)
873   "Insert the contents of HANDLE in the current buffer."
874   (let ((cur (current-buffer)))
875     (save-excursion
876       (if (member (mm-handle-media-supertype handle) '("text" "message"))
877           (with-temp-buffer
878             (insert-buffer-substring (mm-handle-buffer handle))
879             (mm-decode-content-transfer-encoding
880              (mm-handle-encoding handle)
881              (mm-handle-media-type handle))
882             (let ((temp (current-buffer)))
883               (set-buffer cur)
884               (insert-buffer-substring temp)))
885         (mm-with-unibyte-buffer
886           (insert-buffer-substring (mm-handle-buffer handle))
887           (mm-decode-content-transfer-encoding
888            (mm-handle-encoding handle)
889            (mm-handle-media-type handle))
890           (let ((temp (current-buffer)))
891             (set-buffer cur)
892             (insert-buffer-substring temp)))))))
893
894 (defun mm-file-name-delete-whitespace (file-name)
895   "Remove all whitespace characters from FILE-NAME."
896   (while (string-match "\\s-+" file-name)
897     (setq file-name (replace-match "" t t file-name)))
898   file-name)
899
900 (defun mm-file-name-trim-whitespace (file-name)
901   "Remove leading and trailing whitespace characters from FILE-NAME."
902   (when (string-match "\\`\\s-+" file-name)
903     (setq file-name (substring file-name (match-end 0))))
904   (when (string-match "\\s-+\\'" file-name)
905     (setq file-name (substring file-name 0 (match-beginning 0))))
906   file-name)
907
908 (defun mm-file-name-collapse-whitespace (file-name)
909   "Collapse multiple whitespace characters in FILE-NAME."
910   (while (string-match "\\s-\\s-+" file-name)
911     (setq file-name (replace-match " " t t file-name)))
912   file-name)
913
914 (defun mm-file-name-replace-whitespace (file-name)
915   "Replace whitespace characters in FILE-NAME with underscores.
916 Set `mm-file-name-replace-whitespace' to any other string if you do not
917 like underscores."
918   (let ((s (or mm-file-name-replace-whitespace "_")))
919     (while (string-match "\\s-" file-name)
920       (setq file-name (replace-match s t t file-name))))
921   file-name)
922
923 (defun mm-save-part (handle)
924   "Write HANDLE to a file."
925   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
926          (filename (mail-content-type-get
927                     (mm-handle-disposition handle) 'filename))
928          file)
929     (when filename
930       (setq filename (gnus-map-function mm-file-name-rewrite-functions
931                                         (file-name-nondirectory filename))))
932     (setq file
933           (read-file-name "Save MIME part to: "
934                           (expand-file-name
935                            (or filename name "")
936                            (or mm-default-directory default-directory))))
937     (setq mm-default-directory (file-name-directory file))
938     (and (or (not (file-exists-p file))
939              (yes-or-no-p (format "File %s already exists; overwrite? "
940                                   file)))
941          (progn
942            (mm-save-part-to-file handle file)
943            file))))
944
945 (defun mm-save-part-to-file (handle file)
946   (mm-with-unibyte-buffer
947     (mm-insert-part handle)
948     (let ((coding-system-for-write 'binary)
949           ;; Don't re-compress .gz & al.  Arguably we should make
950           ;; `file-name-handler-alist' nil, but that would chop
951           ;; ange-ftp, which is reasonable to use here.
952           (inhibit-file-name-operation 'write-region)
953           (inhibit-file-name-handlers
954            (cons 'jka-compr-handler inhibit-file-name-handlers)))
955       (write-region (point-min) (point-max) file))))
956
957 (defun mm-pipe-part (handle)
958   "Pipe HANDLE to a process."
959   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
960          (command
961           (read-string "Shell command on MIME part: " mm-last-shell-command)))
962     (mm-with-unibyte-buffer
963       (mm-insert-part handle)
964       (let ((coding-system-for-write 'binary))
965         (shell-command-on-region (point-min) (point-max) command nil)))))
966
967 (defun mm-interactively-view-part (handle)
968   "Display HANDLE using METHOD."
969   (let* ((type (mm-handle-media-type handle))
970          (methods
971           (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
972                   (mailcap-mime-info type 'all)))
973          (method (let ((minibuffer-local-completion-map
974                         mm-viewer-completion-map))
975                    (completing-read "Viewer: " methods))))
976     (when (string= method "")
977       (error "No method given"))
978     (if (string-match "^[^% \t]+$" method)
979         (setq method (concat method " %s")))
980     (mm-display-external handle method)))
981
982 (defun mm-preferred-alternative (handles &optional preferred)
983   "Say which of HANDLES are preferred."
984   (let ((prec (if preferred (list preferred)
985                 (mm-preferred-alternative-precedence handles)))
986         p h result type handle)
987     (while (setq p (pop prec))
988       (setq h handles)
989       (while h
990         (setq handle (car h))
991         (setq type (mm-handle-media-type handle))
992         (when (and (equal p type)
993                    (mm-automatic-display-p handle)
994                    (or (stringp (car handle))
995                        (not (mm-handle-disposition handle))
996                        (equal (car (mm-handle-disposition handle))
997                               "inline")))
998           (setq result handle
999                 h nil
1000                 prec nil))
1001         (pop h)))
1002     result))
1003
1004 (defun mm-preferred-alternative-precedence (handles)
1005   "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1006   (let ((seq (nreverse (mapcar #'mm-handle-media-type
1007                                handles))))
1008     (dolist (disc (reverse mm-discouraged-alternatives))
1009       (dolist (elem (copy-sequence seq))
1010         (when (string-match disc elem)
1011           (setq seq (nconc (delete elem seq) (list elem))))))
1012     seq))
1013
1014 (defun mm-get-content-id (id)
1015   "Return the handle(s) referred to by ID."
1016   (cdr (assoc id mm-content-id-alist)))
1017
1018 (defun mm-get-image (handle)
1019   "Return an image instance based on HANDLE."
1020   (let ((type (mm-handle-media-subtype handle))
1021         spec)
1022     ;; Allow some common translations.
1023     (setq type
1024           (cond
1025            ((equal type "x-pixmap")
1026             "xpm")
1027            ((equal type "x-xbitmap")
1028             "xbm")
1029            ((equal type "x-portable-bitmap")
1030             "pbm")
1031            (t type)))
1032     (or (mm-handle-cache handle)
1033         (mm-with-unibyte-buffer
1034           (mm-insert-part handle)
1035           (prog1
1036               (setq spec
1037                     (ignore-errors
1038                      ;; Avoid testing `make-glyph' since W3 may define
1039                      ;; a bogus version of it.
1040                       (if (fboundp 'create-image)
1041                           (create-image (buffer-string) (intern type) 'data-p)
1042                         (cond
1043                          ((equal type "xbm")
1044                           ;; xbm images require special handling, since
1045                           ;; the only way to create glyphs from these
1046                           ;; (without a ton of work) is to write them
1047                           ;; out to a file, and then create a file
1048                           ;; specifier.
1049                           (let ((file (make-temp-name
1050                                        (expand-file-name "emm.xbm"
1051                                                          mm-tmp-directory))))
1052                             (unwind-protect
1053                                 (progn
1054                                   (write-region (point-min) (point-max) file)
1055                                   (make-glyph (list (cons 'x file))))
1056                               (ignore-errors
1057                                (delete-file file)))))
1058                          (t
1059                           (make-glyph
1060                            (vector (intern type) :data (buffer-string))))))))
1061             (mm-handle-set-cache handle spec))))))
1062
1063 (defun mm-image-fit-p (handle)
1064   "Say whether the image in HANDLE will fit the current window."
1065   (let ((image (mm-get-image handle)))
1066     (if (fboundp 'glyph-width)
1067         ;; XEmacs' glyphs can actually tell us about their width, so
1068         ;; lets be nice and smart about them.
1069         (or mm-inline-large-images
1070             (and (< (glyph-width image) (window-pixel-width))
1071                  (< (glyph-height image) (window-pixel-height))))
1072       (let* ((size (image-size image))
1073              (w (car size))
1074              (h (cdr size)))
1075         (or mm-inline-large-images
1076             (and (< h (1- (window-height))) ; Don't include mode line.
1077                  (< w (window-width))))))))
1078
1079 (defun mm-valid-image-format-p (format)
1080   "Say whether FORMAT can be displayed natively by Emacs."
1081   (cond
1082    ;; Handle XEmacs
1083    ((fboundp 'valid-image-instantiator-format-p)
1084     (valid-image-instantiator-format-p format))
1085    ;; Handle Emacs 21
1086    ((fboundp 'image-type-available-p)
1087     (and (display-graphic-p)
1088          (image-type-available-p format)))
1089    ;; Nobody else can do images yet.
1090    (t
1091     nil)))
1092
1093 (defun mm-valid-and-fit-image-p (format handle)
1094   "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1095   (and (mm-valid-image-format-p format)
1096        (mm-image-fit-p handle)))
1097
1098 (defun mm-find-part-by-type (handles type &optional notp recursive)
1099   "Search in HANDLES for part with TYPE.
1100 If NOTP, returns first non-matching part.
1101 If RECURSIVE, search recursively."
1102   (let (handle)
1103     (while handles
1104       (if (and recursive (stringp (caar handles)))
1105           (if (setq handle (mm-find-part-by-type (cdar handles) type
1106                                                  notp recursive))
1107               (setq handles nil))
1108         (if (if notp
1109                 (not (equal (mm-handle-media-type (car handles)) type))
1110               (equal (mm-handle-media-type (car handles)) type))
1111             (setq handle (car handles)
1112                   handles nil)))
1113       (setq handles (cdr handles)))
1114     handle))
1115
1116 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1117   (goto-char (point-min))
1118   (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1119                                                                    'boundary)))
1120          (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1121          start
1122          (end (save-excursion
1123                 (goto-char (point-max))
1124                 (if (re-search-backward close-delimiter nil t)
1125                     (match-beginning 0)
1126                   (point-max))))
1127          result)
1128     (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1129     (while (and (not result)
1130                 (re-search-forward boundary end t))
1131       (goto-char (match-beginning 0))
1132       (when start
1133         (save-excursion
1134           (save-restriction
1135             (narrow-to-region start (1- (point)))
1136             (when (let ((ctl (ignore-errors
1137                                (mail-header-parse-content-type
1138                                 (mail-fetch-field "content-type")))))
1139                     (if notp
1140                         (not (equal (car ctl) type))
1141                       (equal (car ctl) type)))
1142               (setq result (buffer-substring (point-min) (point-max)))))))
1143       (forward-line 1)
1144       (setq start (point)))
1145     (when (and (not result) start)
1146       (save-excursion
1147         (save-restriction
1148           (narrow-to-region start end)
1149           (when (let ((ctl (ignore-errors
1150                              (mail-header-parse-content-type
1151                               (mail-fetch-field "content-type")))))
1152                   (if notp
1153                       (not (equal (car ctl) type))
1154                     (equal (car ctl) type)))
1155             (setq result (buffer-substring (point-min) (point-max)))))))
1156     result))
1157
1158 (defvar mm-security-handle nil)
1159
1160 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1161   ;; HANDLE could be a CTL.
1162   (if handle
1163       (put-text-property 0 (length (car handle)) parameter value
1164                          (car handle))))
1165
1166 (defun mm-possibly-verify-or-decrypt (parts ctl)
1167   (let ((subtype (cadr (split-string (car ctl) "/")))
1168         (mm-security-handle ctl) ;; (car CTL) is the type.
1169         protocol func functest)
1170     (cond
1171      ((equal subtype "signed")
1172       (unless (and (setq protocol
1173                          (mm-handle-multipart-ctl-parameter ctl 'protocol))
1174                    (not (equal protocol "multipart/mixed")))
1175         ;; The message is broken or draft-ietf-openpgp-multsig-01.
1176         (let ((protocols mm-verify-function-alist))
1177           (while protocols
1178             (if (and (or (not (setq functest (nth 3 (car protocols))))
1179                          (funcall functest parts ctl))
1180                      (mm-find-part-by-type parts (caar protocols) nil t))
1181                 (setq protocol (caar protocols)
1182                       protocols nil)
1183               (setq protocols (cdr protocols))))))
1184       (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1185       (if (cond
1186            ((eq mm-verify-option 'never) nil)
1187            ((eq mm-verify-option 'always) t)
1188            ((eq mm-verify-option 'known)
1189             (and func
1190                  (or (not (setq functest
1191                                 (nth 3 (assoc protocol
1192                                               mm-verify-function-alist))))
1193                      (funcall functest parts ctl))))
1194            (t (y-or-n-p
1195                (format "Verify signed (%s) part? "
1196                        (or (nth 2 (assoc protocol mm-verify-function-alist))
1197                            (format "protocol=%s" protocol))))))
1198           (save-excursion
1199             (if func
1200                 (funcall func parts ctl)
1201               (mm-set-handle-multipart-parameter
1202                mm-security-handle 'gnus-details
1203                (format "Unknown sign protocol (%s)" protocol))))))
1204      ((equal subtype "encrypted")
1205       (unless (setq protocol
1206                     (mm-handle-multipart-ctl-parameter ctl 'protocol))
1207         ;; The message is broken.
1208         (let ((parts parts))
1209           (while parts
1210             (if (assoc (mm-handle-media-type (car parts))
1211                        mm-decrypt-function-alist)
1212                 (setq protocol (mm-handle-media-type (car parts))
1213                       parts nil)
1214               (setq parts (cdr parts))))))
1215       (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1216       (if (cond
1217            ((eq mm-decrypt-option 'never) nil)
1218            ((eq mm-decrypt-option 'always) t)
1219            ((eq mm-decrypt-option 'known)
1220             (and func
1221                  (or (not (setq functest
1222                                 (nth 3 (assoc protocol
1223                                               mm-decrypt-function-alist))))
1224                      (funcall functest parts ctl))))
1225            (t (y-or-n-p
1226                (format "Decrypt (%s) part? "
1227                        (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1228                            (format "protocol=%s" protocol))))))
1229           (save-excursion
1230             (if func
1231                 (setq parts (funcall func parts ctl))
1232               (mm-set-handle-multipart-parameter
1233                mm-security-handle 'gnus-details
1234                (format "Unknown encrypt protocol (%s)" protocol))))))
1235      (t nil))
1236     parts))
1237
1238 (defun mm-multiple-handles (handles)
1239    (and (listp (car handles)) 
1240         (> (length handles) 1)))
1241
1242 (defun mm-merge-handles (handles1 handles2) 
1243   (append
1244    (if (listp (car handles1)) 
1245        handles1
1246      (list handles1))
1247    (if (listp (car handles2))
1248        handles2
1249      (list handles2))))
1250
1251 (provide 'mm-decode)
1252
1253 ;;; mm-decode.el ends here