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