Synch with Oort Gnus.
[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     ;; 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 ((flag enable-multibyte-characters)
526           (new-buffer (generate-new-buffer " *mm*")))
527       (goto-char (point-min))
528       (search-forward-regexp "^\n" nil t)
529       (save-restriction
530         (narrow-to-region (point) (point-max))
531         (when flag
532           (set-buffer-multibyte nil))
533         (copy-to-buffer new-buffer (point-min) (point-max))
534         (when flag
535           (set-buffer-multibyte t)))
536       new-buffer)))
537
538 (defun mm-display-parts (handle &optional no-default)
539   (if (stringp (car handle))
540       (mapcar 'mm-display-parts (cdr handle))
541     (if (bufferp (car handle))
542         (save-restriction
543           (narrow-to-region (point) (point))
544           (mm-display-part handle)
545           (goto-char (point-max)))
546       (mapcar 'mm-display-parts handle))))
547
548 (defun mm-display-part (handle &optional no-default)
549   "Display the MIME part represented by HANDLE.
550 Returns nil if the part is removed; inline if displayed inline;
551 external if displayed external."
552   (save-excursion
553     (mailcap-parse-mailcaps)
554     (if (mm-handle-displayed-p handle)
555         (mm-remove-part handle)
556       (let* ((type (mm-handle-media-type handle))
557              (method (mailcap-mime-info type)))
558         (if (and (mm-inlinable-p handle)
559                  (mm-inlined-p handle))
560             (progn
561               (forward-line 1)
562               (mm-display-inline handle)
563               'inline)
564           (when (or method
565                     (not no-default))
566             (if (and (not method)
567                      (equal "text" (car (split-string type))))
568                 (progn
569                   (forward-line 1)
570                   (mm-insert-inline handle (mm-get-part handle))
571                   'inline)
572               (mm-display-external
573                handle (or method 'mailcap-save-binary-file)))))))))
574
575 (defun mm-display-external (handle method)
576   "Display HANDLE using METHOD."
577   (let ((outbuf (current-buffer)))
578     (mm-with-unibyte-buffer
579       (if (functionp method)
580           (let ((cur (current-buffer)))
581             (if (eq method 'mailcap-save-binary-file)
582                 (progn
583                   (set-buffer (generate-new-buffer " *mm*"))
584                   (setq method nil))
585               (mm-insert-part handle)
586               (let ((win (get-buffer-window cur t)))
587                 (when win
588                   (select-window win)))
589               (switch-to-buffer (generate-new-buffer " *mm*")))
590             (buffer-disable-undo)
591             (mm-set-buffer-file-coding-system mm-binary-coding-system)
592             (insert-buffer-substring cur)
593             (goto-char (point-min))
594             (message "Viewing with %s" method)
595             (let ((mm (current-buffer))
596                   (non-viewer (assq 'non-viewer
597                                     (mailcap-mime-info
598                                      (mm-handle-media-type handle) t))))
599               (unwind-protect
600                   (if method
601                       (funcall method)
602                     (mm-save-part handle))
603                 (when (and (not non-viewer)
604                            method)
605                   (mm-handle-set-undisplayer handle mm)))))
606         ;; The function is a string to be executed.
607         (mm-insert-part handle)
608         (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
609                (filename (mail-content-type-get
610                           (mm-handle-disposition handle) 'filename))
611                (mime-info (mailcap-mime-info
612                            (mm-handle-media-type handle) t))
613                (needsterm (or (assoc "needsterm" mime-info)
614                               (assoc "needsterminal" mime-info)))
615                (copiousoutput (assoc "copiousoutput" mime-info))
616                file buffer)
617           ;; We create a private sub-directory where we store our files.
618           (make-directory dir)
619           (set-file-modes dir 448)
620           (if filename
621               (setq file (expand-file-name (file-name-nondirectory filename)
622                                            dir))
623             (setq file (make-temp-name (expand-file-name "mm." dir))))
624           (let ((coding-system-for-write mm-binary-coding-system))
625             (write-region (point-min) (point-max) file nil 'nomesg))
626           (message "Viewing with %s" method)
627           (cond (needsterm
628                  (unwind-protect
629                      (if window-system
630                          (start-process "*display*" nil
631                                         mm-external-terminal-program
632                                         "-e" shell-file-name
633                                         shell-command-switch
634                                         (mm-mailcap-command
635                                          method file (mm-handle-type handle)))
636                        (require 'term)
637                        (require 'gnus-win)
638                        (set-buffer
639                         (setq buffer
640                               (make-term "display"
641                                          shell-file-name
642                                          nil
643                                          shell-command-switch
644                                          (mm-mailcap-command
645                                           method file
646                                           (mm-handle-type handle)))))
647                        (term-mode)
648                        (term-char-mode)
649                        (set-process-sentinel
650                         (get-buffer-process buffer)
651                         `(lambda (process state)
652                            (if (eq 'exit (process-status process))
653                                (gnus-configure-windows
654                                 ',gnus-current-window-configuration))))
655                        (gnus-configure-windows 'display-term))
656                    (mm-handle-set-external-undisplayer handle (cons file buffer)))
657                  (message "Displaying %s..." (format method file))
658                  'external)
659                 (copiousoutput
660                  (with-current-buffer outbuf
661                    (forward-line 1)
662                    (mm-insert-inline
663                     handle
664                     (unwind-protect
665                         (progn
666                           (call-process shell-file-name nil
667                                         (setq buffer
668                                               (generate-new-buffer " *mm*"))
669                                         nil
670                                         shell-command-switch
671                                         (mm-mailcap-command
672                                          method file (mm-handle-type handle)))
673                           (if (buffer-live-p buffer)
674                               (save-excursion
675                                 (set-buffer buffer)
676                                 (buffer-string))))
677                       (progn
678                         (ignore-errors (delete-file file))
679                         (ignore-errors (delete-directory
680                                         (file-name-directory file)))
681                         (ignore-errors (kill-buffer buffer))))))
682                  'inline)
683                 (t
684                  (unwind-protect
685                      (start-process "*display*"
686                                     (setq buffer
687                                           (generate-new-buffer " *mm*"))
688                                     shell-file-name
689                                     shell-command-switch
690                                     (mm-mailcap-command
691                                      method file (mm-handle-type handle)))
692                    (mm-handle-set-external-undisplayer handle (cons file buffer)))
693                  (message "Displaying %s..." (format method file))
694                  'external)))))))
695
696 (defun mm-mailcap-command (method file type-list)
697   (let ((ctl (cdr type-list))
698         (beg 0)
699         (uses-stdin t)
700         out sub total)
701     (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
702       (push (substring method beg (match-beginning 0)) out)
703       (setq beg (match-end 0)
704             total (match-string 0 method)
705             sub (match-string 1 method))
706       (cond
707        ((string= total "%%")
708         (push "%" out))
709        ((string= total "%s")
710         (setq uses-stdin nil)
711         (push (mm-quote-arg file) out))
712        ((string= total "%t")
713         (push (mm-quote-arg (car type-list)) out))
714        (t
715         (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
716     (push (substring method beg (length method)) out)
717     (if uses-stdin
718         (progn
719           (push "<" out)
720           (push (mm-quote-arg file) out)))
721     (mapconcat 'identity (nreverse out) "")))
722
723 (defun mm-remove-parts (handles)
724   "Remove the displayed MIME parts represented by HANDLES."
725   (if (and (listp handles)
726            (bufferp (car handles)))
727       (mm-remove-part handles)
728     (let (handle)
729       (while (setq handle (pop handles))
730         (cond
731          ((stringp handle)
732           (when (buffer-live-p (get-text-property 0 'buffer handle))
733             (kill-buffer (get-text-property 0 'buffer handle))))
734          ((and (listp handle)
735                (stringp (car handle)))
736           (mm-remove-parts (cdr handle)))
737          (t
738           (mm-remove-part handle)))))))
739
740 (defun mm-destroy-parts (handles)
741   "Remove the displayed MIME parts represented by HANDLES."
742   (if (and (listp handles)
743            (bufferp (car handles)))
744       (mm-destroy-part handles)
745     (let (handle)
746       (while (setq handle (pop handles))
747         (cond
748          ((stringp handle)
749           (when (buffer-live-p (get-text-property 0 'buffer handle))
750             (kill-buffer (get-text-property 0 'buffer handle))))
751          ((and (listp handle)
752                (stringp (car handle)))
753           (mm-destroy-parts handle))
754          (t
755           (mm-destroy-part handle)))))))
756
757 (defun mm-remove-part (handle)
758   "Remove the displayed MIME part represented by HANDLE."
759   (when (listp handle)
760     (let ((object (mm-handle-undisplayer handle)))
761       (ignore-errors
762         (cond
763          ;; Internally displayed part.
764          ((mm-annotationp object)
765           (delete-annotation object))
766          ((or (functionp object)
767               (and (listp object)
768                    (eq (car object) 'lambda)))
769           (funcall object))
770          ;; Externally displayed part.
771          ((consp object)
772           (ignore-errors (delete-file (car object)))
773           (ignore-errors (delete-directory (file-name-directory (car object))))
774           (ignore-errors (and (cdr object) (kill-buffer (cdr object)))))
775          ((bufferp object)
776           (when (buffer-live-p object)
777             (kill-buffer object)))))
778       (mm-handle-set-undisplayer handle nil))))
779
780 (defun mm-display-inline (handle)
781   (let* ((type (mm-handle-media-type handle))
782          (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
783     (funcall function handle)
784     (goto-char (point-min))))
785
786 (defun mm-assoc-string-match (alist type)
787   (dolist (elem alist)
788     (when (string-match (car elem) type)
789       (return elem))))
790
791 (defun mm-automatic-display-p (handle)
792   "Say whether the user wants HANDLE to be displayed automatically."
793   (let ((methods mm-automatic-display)
794         (type (mm-handle-media-type handle))
795         method result)
796     (while (setq method (pop methods))
797       (when (and (not (mm-inline-override-p handle))
798                  (string-match method type))
799         (setq result t
800               methods nil)))
801     result))
802
803 (defun mm-inlinable-p (handle)
804   "Say whether HANDLE can be displayed inline."
805   (let ((alist mm-inline-media-tests)
806         (type (mm-handle-media-type handle))
807         test)
808     (while alist
809       (when (string-match (caar alist) type)
810         (setq test (caddar alist)
811               alist nil)
812         (setq test (funcall test handle)))
813       (pop alist))
814     test))
815
816 (defun mm-inlined-p (handle)
817   "Say whether the user wants HANDLE to be displayed inline."
818   (let ((methods mm-inlined-types)
819         (type (mm-handle-media-type handle))
820         method result)
821     (while (setq method (pop methods))
822       (when (and (not (mm-inline-override-p handle))
823                  (string-match method type))
824         (setq result t
825               methods nil)))
826     result))
827
828 (defun mm-attachment-override-p (handle)
829   "Say whether HANDLE should have attachment behavior overridden."
830   (let ((types mm-attachment-override-types)
831         (type (mm-handle-media-type handle))
832         ty)
833     (catch 'found
834       (while (setq ty (pop types))
835         (when (and (string-match ty type)
836                    (mm-inlinable-p handle))
837           (throw 'found t))))))
838
839 (defun mm-inline-override-p (handle)
840   "Say whether HANDLE should have inline behavior overridden."
841   (let ((types mm-inline-override-types)
842         (type (mm-handle-media-type handle))
843         ty)
844     (catch 'found
845       (while (setq ty (pop types))
846         (when (string-match ty type)
847           (throw 'found t))))))
848
849 (defun mm-automatic-external-display-p (type)
850   "Return the user-defined method for TYPE."
851   (let ((methods mm-automatic-external-display)
852         method result)
853     (while (setq method (pop methods))
854       (when (string-match method type)
855         (setq result t
856               methods nil)))
857     result))
858
859 (defun mm-destroy-part (handle)
860   "Destroy the data structures connected to HANDLE."
861   (when (listp handle)
862     (mm-remove-part handle)
863     (when (buffer-live-p (mm-handle-buffer handle))
864       (kill-buffer (mm-handle-buffer handle)))))
865
866 (defun mm-handle-displayed-p (handle)
867   "Say whether HANDLE is displayed or not."
868   (mm-handle-undisplayer handle))
869
870 ;;;
871 ;;; Functions for outputting parts
872 ;;;
873
874 (defun mm-get-part (handle)
875   "Return the contents of HANDLE as a string."
876   (mm-with-unibyte-buffer
877     (insert (with-current-buffer (mm-handle-buffer handle)
878               (mm-with-unibyte-current-buffer-mule4
879                 (buffer-string))))
880     (mm-decode-content-transfer-encoding
881      (mm-handle-encoding handle)
882      (mm-handle-media-type handle))
883     (buffer-string)))
884
885 (defun mm-insert-part (handle)
886   "Insert the contents of HANDLE in the current buffer."
887   (let ((cur (current-buffer)))
888     (save-excursion
889       (if (member (mm-handle-media-supertype handle) '("text" "message"))
890           (with-temp-buffer
891             (insert-buffer-substring (mm-handle-buffer handle))
892             (mm-decode-content-transfer-encoding
893              (mm-handle-encoding handle)
894              (mm-handle-media-type handle))
895             (let ((temp (current-buffer)))
896               (set-buffer cur)
897               (insert-buffer-substring temp)))
898         (mm-with-unibyte-buffer
899           (insert-buffer-substring (mm-handle-buffer handle))
900           (mm-decode-content-transfer-encoding
901            (mm-handle-encoding handle)
902            (mm-handle-media-type handle))
903           (let ((temp (current-buffer)))
904             (set-buffer cur)
905             (insert-buffer-substring temp)))))))
906
907 (defun mm-file-name-delete-whitespace (file-name)
908   "Remove all whitespace characters from FILE-NAME."
909   (while (string-match "\\s-+" file-name)
910     (setq file-name (replace-match "" t t file-name)))
911   file-name)
912
913 (defun mm-file-name-trim-whitespace (file-name)
914   "Remove leading and trailing whitespace characters from FILE-NAME."
915   (when (string-match "\\`\\s-+" file-name)
916     (setq file-name (substring file-name (match-end 0))))
917   (when (string-match "\\s-+\\'" file-name)
918     (setq file-name (substring file-name 0 (match-beginning 0))))
919   file-name)
920
921 (defun mm-file-name-collapse-whitespace (file-name)
922   "Collapse multiple whitespace characters in FILE-NAME."
923   (while (string-match "\\s-\\s-+" file-name)
924     (setq file-name (replace-match " " t t file-name)))
925   file-name)
926
927 (defun mm-file-name-replace-whitespace (file-name)
928   "Replace whitespace characters in FILE-NAME with underscores.
929 Set `mm-file-name-replace-whitespace' to any other string if you do not
930 like underscores."
931   (let ((s (or mm-file-name-replace-whitespace "_")))
932     (while (string-match "\\s-" file-name)
933       (setq file-name (replace-match s t t file-name))))
934   file-name)
935
936 (defun mm-save-part (handle)
937   "Write HANDLE to a file."
938   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
939          (filename (mail-content-type-get
940                     (mm-handle-disposition handle) 'filename))
941          file)
942     (when filename
943       (setq filename (gnus-map-function mm-file-name-rewrite-functions
944                                         (file-name-nondirectory filename))))
945     (setq file
946           (read-file-name "Save MIME part to: "
947                           (expand-file-name
948                            (or filename name "")
949                            (or mm-default-directory default-directory))))
950     (setq mm-default-directory (file-name-directory file))
951     (and (or (not (file-exists-p file))
952              (yes-or-no-p (format "File %s already exists; overwrite? "
953                                   file)))
954          (progn
955            (mm-save-part-to-file handle file)
956            file))))
957
958 (defun mm-save-part-to-file (handle file)
959   (mm-with-unibyte-buffer
960     (mm-insert-part handle)
961     (let ((coding-system-for-write 'binary)
962           ;; Don't re-compress .gz & al.  Arguably we should make
963           ;; `file-name-handler-alist' nil, but that would chop
964           ;; ange-ftp, which is reasonable to use here.
965           (inhibit-file-name-operation 'write-region)
966           (inhibit-file-name-handlers
967            (cons 'jka-compr-handler inhibit-file-name-handlers)))
968       (write-region (point-min) (point-max) file))))
969
970 (defun mm-pipe-part (handle)
971   "Pipe HANDLE to a process."
972   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
973          (command
974           (read-string "Shell command on MIME part: " mm-last-shell-command)))
975     (mm-with-unibyte-buffer
976       (mm-insert-part handle)
977       (let ((coding-system-for-write 'binary))
978         (shell-command-on-region (point-min) (point-max) command nil)))))
979
980 (defun mm-interactively-view-part (handle)
981   "Display HANDLE using METHOD."
982   (let* ((type (mm-handle-media-type handle))
983          (methods
984           (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
985                   (mailcap-mime-info type 'all)))
986          (method (let ((minibuffer-local-completion-map
987                         mm-viewer-completion-map))
988                    (completing-read "Viewer: " methods))))
989     (when (string= method "")
990       (error "No method given"))
991     (if (string-match "^[^% \t]+$" method)
992         (setq method (concat method " %s")))
993     (mm-display-external handle method)))
994
995 (defun mm-preferred-alternative (handles &optional preferred)
996   "Say which of HANDLES are preferred."
997   (let ((prec (if preferred (list preferred)
998                 (mm-preferred-alternative-precedence handles)))
999         p h result type handle)
1000     (while (setq p (pop prec))
1001       (setq h handles)
1002       (while h
1003         (setq handle (car h))
1004         (setq type (mm-handle-media-type handle))
1005         (when (and (equal p type)
1006                    (mm-automatic-display-p handle)
1007                    (or (stringp (car handle))
1008                        (not (mm-handle-disposition handle))
1009                        (equal (car (mm-handle-disposition handle))
1010                               "inline")))
1011           (setq result handle
1012                 h nil
1013                 prec nil))
1014         (pop h)))
1015     result))
1016
1017 (defun mm-preferred-alternative-precedence (handles)
1018   "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
1019   (let ((seq (nreverse (mapcar #'mm-handle-media-type
1020                                handles))))
1021     (dolist (disc (reverse mm-discouraged-alternatives))
1022       (dolist (elem (copy-sequence seq))
1023         (when (string-match disc elem)
1024           (setq seq (nconc (delete elem seq) (list elem))))))
1025     seq))
1026
1027 (defun mm-get-content-id (id)
1028   "Return the handle(s) referred to by ID."
1029   (cdr (assoc id mm-content-id-alist)))
1030
1031 (defun mm-get-image (handle)
1032   "Return an image instance based on HANDLE."
1033   (let ((type (mm-handle-media-subtype handle))
1034         spec)
1035     ;; Allow some common translations.
1036     (setq type
1037           (cond
1038            ((equal type "x-pixmap")
1039             "xpm")
1040            ((equal type "x-xbitmap")
1041             "xbm")
1042            ((equal type "x-portable-bitmap")
1043             "pbm")
1044            (t type)))
1045     (or (mm-handle-cache handle)
1046         (mm-with-unibyte-buffer
1047           (mm-insert-part handle)
1048           (prog1
1049               (setq spec
1050                     (ignore-errors
1051                       ;; Avoid testing `make-glyph' since W3 may define
1052                       ;; a bogus version of it.
1053                       (if (fboundp 'create-image)
1054                           (or
1055                            (create-image (buffer-string) nil 'data-p)
1056                            (create-image (buffer-string) (intern type)
1057                                          'data-p))
1058                         (cond
1059                          ((equal type "xbm")
1060                           ;; xbm images require special handling, since
1061                           ;; the only way to create glyphs from these
1062                           ;; (without a ton of work) is to write them
1063                           ;; out to a file, and then create a file
1064                           ;; specifier.
1065                           (let ((file (make-temp-name
1066                                        (expand-file-name "emm.xbm"
1067                                                          mm-tmp-directory))))
1068                             (unwind-protect
1069                                 (progn
1070                                   (write-region (point-min) (point-max) file)
1071                                   (make-glyph (list (cons 'x file))))
1072                               (ignore-errors
1073                                 (delete-file file)))))
1074                          (t
1075                           (make-glyph
1076                            (vector (intern type) :data (buffer-string))))))))
1077             (mm-handle-set-cache handle spec))))))
1078
1079 (defun mm-image-fit-p (handle)
1080   "Say whether the image in HANDLE will fit the current window."
1081   (let ((image (mm-get-image handle)))
1082     (if (fboundp 'glyph-width)
1083         ;; XEmacs' glyphs can actually tell us about their width, so
1084         ;; lets be nice and smart about them.
1085         (or mm-inline-large-images
1086             (and (< (glyph-width image) (window-pixel-width))
1087                  (< (glyph-height image) (window-pixel-height))))
1088       (let* ((size (image-size image))
1089              (w (car size))
1090              (h (cdr size)))
1091         (or mm-inline-large-images
1092             (and (< h (1- (window-height))) ; Don't include mode line.
1093                  (< w (window-width))))))))
1094
1095 (defun mm-valid-image-format-p (format)
1096   "Say whether FORMAT can be displayed natively by Emacs."
1097   (cond
1098    ;; Handle XEmacs
1099    ((fboundp 'valid-image-instantiator-format-p)
1100     (valid-image-instantiator-format-p format))
1101    ;; Handle Emacs 21
1102    ((fboundp 'image-type-available-p)
1103     (and (display-graphic-p)
1104          (image-type-available-p format)))
1105    ;; Nobody else can do images yet.
1106    (t
1107     nil)))
1108
1109 (defun mm-valid-and-fit-image-p (format handle)
1110   "Say whether FORMAT can be displayed natively and HANDLE fits the window."
1111   (and (mm-valid-image-format-p format)
1112        (mm-image-fit-p handle)))
1113
1114 (defun mm-find-part-by-type (handles type &optional notp recursive)
1115   "Search in HANDLES for part with TYPE.
1116 If NOTP, returns first non-matching part.
1117 If RECURSIVE, search recursively."
1118   (let (handle)
1119     (while handles
1120       (if (and recursive (stringp (caar handles)))
1121           (if (setq handle (mm-find-part-by-type (cdar handles) type
1122                                                  notp recursive))
1123               (setq handles nil))
1124         (if (if notp
1125                 (not (equal (mm-handle-media-type (car handles)) type))
1126               (equal (mm-handle-media-type (car handles)) type))
1127             (setq handle (car handles)
1128                   handles nil)))
1129       (setq handles (cdr handles)))
1130     handle))
1131
1132 (defun mm-find-raw-part-by-type (ctl type &optional notp)
1133   (goto-char (point-min))
1134   (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
1135                                                                    'boundary)))
1136          (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
1137          start
1138          (end (save-excursion
1139                 (goto-char (point-max))
1140                 (if (re-search-backward close-delimiter nil t)
1141                     (match-beginning 0)
1142                   (point-max))))
1143          result)
1144     (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
1145     (while (and (not result)
1146                 (re-search-forward boundary end t))
1147       (goto-char (match-beginning 0))
1148       (when start
1149         (save-excursion
1150           (save-restriction
1151             (narrow-to-region start (1- (point)))
1152             (when (let ((ctl (ignore-errors
1153                                (mail-header-parse-content-type
1154                                 (mail-fetch-field "content-type")))))
1155                     (if notp
1156                         (not (equal (car ctl) type))
1157                       (equal (car ctl) type)))
1158               (setq result (buffer-substring (point-min) (point-max)))))))
1159       (forward-line 1)
1160       (setq start (point)))
1161     (when (and (not result) start)
1162       (save-excursion
1163         (save-restriction
1164           (narrow-to-region start end)
1165           (when (let ((ctl (ignore-errors
1166                              (mail-header-parse-content-type
1167                               (mail-fetch-field "content-type")))))
1168                   (if notp
1169                       (not (equal (car ctl) type))
1170                     (equal (car ctl) type)))
1171             (setq result (buffer-substring (point-min) (point-max)))))))
1172     result))
1173
1174 (defvar mm-security-handle nil)
1175
1176 (defsubst mm-set-handle-multipart-parameter (handle parameter value)
1177   ;; HANDLE could be a CTL.
1178   (if handle
1179       (put-text-property 0 (length (car handle)) parameter value
1180                          (car handle))))
1181
1182 (defun mm-possibly-verify-or-decrypt (parts ctl)
1183   (let ((subtype (cadr (split-string (car ctl) "/")))
1184         (mm-security-handle ctl) ;; (car CTL) is the type.
1185         protocol func functest)
1186     (cond
1187      ((equal subtype "signed")
1188       (unless (and (setq protocol
1189                          (mm-handle-multipart-ctl-parameter ctl 'protocol))
1190                    (not (equal protocol "multipart/mixed")))
1191         ;; The message is broken or draft-ietf-openpgp-multsig-01.
1192         (let ((protocols mm-verify-function-alist))
1193           (while protocols
1194             (if (and (or (not (setq functest (nth 3 (car protocols))))
1195                          (funcall functest parts ctl))
1196                      (mm-find-part-by-type parts (caar protocols) nil t))
1197                 (setq protocol (caar protocols)
1198                       protocols nil)
1199               (setq protocols (cdr protocols))))))
1200       (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
1201       (if (cond
1202            ((eq mm-verify-option 'never) nil)
1203            ((eq mm-verify-option 'always) t)
1204            ((eq mm-verify-option 'known)
1205             (and func
1206                  (or (not (setq functest
1207                                 (nth 3 (assoc protocol
1208                                               mm-verify-function-alist))))
1209                      (funcall functest parts ctl))))
1210            (t (y-or-n-p
1211                (format "Verify signed (%s) part? "
1212                        (or (nth 2 (assoc protocol mm-verify-function-alist))
1213                            (format "protocol=%s" protocol))))))
1214           (save-excursion
1215             (if func
1216                 (funcall func parts ctl)
1217               (mm-set-handle-multipart-parameter
1218                mm-security-handle 'gnus-details
1219                (format "Unknown sign protocol (%s)" protocol))))))
1220      ((equal subtype "encrypted")
1221       (unless (setq protocol
1222                     (mm-handle-multipart-ctl-parameter ctl 'protocol))
1223         ;; The message is broken.
1224         (let ((parts parts))
1225           (while parts
1226             (if (assoc (mm-handle-media-type (car parts))
1227                        mm-decrypt-function-alist)
1228                 (setq protocol (mm-handle-media-type (car parts))
1229                       parts nil)
1230               (setq parts (cdr parts))))))
1231       (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
1232       (if (cond
1233            ((eq mm-decrypt-option 'never) nil)
1234            ((eq mm-decrypt-option 'always) t)
1235            ((eq mm-decrypt-option 'known)
1236             (and func
1237                  (or (not (setq functest
1238                                 (nth 3 (assoc protocol
1239                                               mm-decrypt-function-alist))))
1240                      (funcall functest parts ctl))))
1241            (t (y-or-n-p
1242                (format "Decrypt (%s) part? "
1243                        (or (nth 2 (assoc protocol mm-decrypt-function-alist))
1244                            (format "protocol=%s" protocol))))))
1245           (save-excursion
1246             (if func
1247                 (setq parts (funcall func parts ctl))
1248               (mm-set-handle-multipart-parameter
1249                mm-security-handle 'gnus-details
1250                (format "Unknown encrypt protocol (%s)" protocol))))))
1251      (t nil))
1252     parts))
1253
1254 (defun mm-multiple-handles (handles)
1255   (and (listp (car handles))
1256        (> (length handles) 1)))
1257
1258 (defun mm-merge-handles (handles1 handles2)
1259   (append
1260    (if (listp (car handles1))
1261        handles1
1262      (list handles1))
1263    (if (listp (car handles2))
1264        handles2
1265      (list handles2))))
1266
1267 (defun mm-readable-p (handle)
1268   "Say whether the content of HANDLE is readable."
1269   (and (< (buffer-size (mm-handle-buffer handle)) 10000)
1270        (mm-with-unibyte-buffer
1271          (mm-insert-part handle)
1272          (and (eq (mm-body-7-or-8) '7bit)
1273               (not (mm-long-lines-p 76))))))
1274
1275 (provide 'mm-decode)
1276
1277 ;;; mm-decode.el ends here