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