(mm-fill-flowed): Add :version.
[gnus] / lisp / mm-view.el
1 ;;; mm-view.el --- functions for viewing MIME objects
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (require 'mail-parse)
30 (require 'mailcap)
31 (require 'mm-bodies)
32 (require 'mm-decode)
33
34 (eval-and-compile
35   (autoload 'gnus-article-prepare-display "gnus-art")
36   (autoload 'vcard-parse-string "vcard")
37   (autoload 'vcard-format-string "vcard")
38   (autoload 'fill-flowed "flow-fill")
39   (autoload 'html2text "html2text"))
40
41 (defvar gnus-article-mime-handles)
42 (defvar gnus-newsgroup-charset)
43 (defvar smime-keys)
44 (defvar w3m-cid-retrieve-function-alist)
45 (defvar w3m-current-buffer)
46 (defvar w3m-display-inline-images)
47 (defvar w3m-minor-mode-map)
48
49 (defvar mm-text-html-renderer-alist
50   '((w3  . mm-inline-text-html-render-with-w3)
51     (w3m . mm-inline-text-html-render-with-w3m)
52     (w3m-standalone . mm-inline-text-html-render-with-w3m-standalone)
53     (links mm-inline-render-with-file
54            mm-links-remove-leading-blank
55            "links" "-dump" file)
56     (lynx  mm-inline-render-with-stdin nil
57            "lynx" "-dump" "-force_html" "-stdin" "-nolist")
58     (html2text  mm-inline-render-with-function html2text))
59   "The attributes of renderer types for text/html.")
60
61 (defvar mm-text-html-washer-alist
62   '((w3  . gnus-article-wash-html-with-w3)
63     (w3m . gnus-article-wash-html-with-w3m)
64     (w3m-standalone . gnus-article-wash-html-with-w3m-standalone)
65     (links mm-inline-wash-with-file
66            mm-links-remove-leading-blank
67            "links" "-dump" file)
68     (lynx  mm-inline-wash-with-stdin nil
69            "lynx" "-dump" "-force_html" "-stdin" "-nolist")
70     (html2text  html2text))
71   "The attributes of washer types for text/html.")
72
73 (defcustom mm-fill-flowed t
74   "If non-nil a format=flowed article will be displayed flowed."
75   :type 'boolean
76   :version "22.1"
77   :group 'mime-display)
78
79 ;;; Internal variables.
80
81 ;;;
82 ;;; Functions for displaying various formats inline
83 ;;;
84
85 (defun mm-inline-image-emacs (handle)
86   (let ((b (point-marker))
87         buffer-read-only)
88     (put-image (mm-get-image handle) b)
89     (insert "\n\n")
90     (mm-handle-set-undisplayer
91      handle
92      `(lambda ()
93         (let ((b ,b)
94               buffer-read-only)
95           (remove-images b b)
96           (delete-region b (+ b 2)))))))
97
98 (defun mm-inline-image-xemacs (handle)
99   (insert "\n\n")
100   (forward-char -2)
101   (let ((annot (make-annotation (mm-get-image handle) nil 'text))
102         buffer-read-only)
103     (mm-handle-set-undisplayer
104      handle
105      `(lambda ()
106         (let ((b ,(point-marker))
107               buffer-read-only)
108           (delete-annotation ,annot)
109           (delete-region (- b 2) b))))
110     (set-extent-property annot 'mm t)
111     (set-extent-property annot 'duplicable t)))
112
113 (eval-and-compile
114   (if (featurep 'xemacs)
115       (defalias 'mm-inline-image 'mm-inline-image-xemacs)
116     (defalias 'mm-inline-image 'mm-inline-image-emacs)))
117
118 (defvar mm-w3-setup nil)
119 (defun mm-setup-w3 ()
120   (unless mm-w3-setup
121     (require 'w3)
122     (w3-do-setup)
123     (require 'url)
124     (require 'w3-vars)
125     (require 'url-vars)
126     (setq mm-w3-setup t)))
127
128 (defun mm-inline-text-html-render-with-w3 (handle)
129   (mm-setup-w3)
130   (let ((text (mm-get-part handle))
131         (b (point))
132         (url-standalone-mode t)
133         (url-gateway-unplugged t)
134         (w3-honor-stylesheets nil)
135         (url-current-object
136          (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
137         (width (window-width))
138         (charset (mail-content-type-get
139                   (mm-handle-type handle) 'charset)))
140     (save-excursion
141       (insert (if charset (mm-decode-string text charset) text))
142       (save-restriction
143         (narrow-to-region b (point))
144         (unless charset
145           (goto-char (point-min))
146           (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
147                          (re-search-forward
148                           w3-meta-content-type-charset-regexp nil t))
149                     (and (boundp 'w3-meta-charset-content-type-regexp)
150                          (re-search-forward
151                           w3-meta-charset-content-type-regexp nil t)))
152             (setq charset
153                   (let ((bsubstr (buffer-substring-no-properties
154                                   (match-beginning 2)
155                                   (match-end 2))))
156                     (if (fboundp 'w3-coding-system-for-mime-charset)
157                         (w3-coding-system-for-mime-charset bsubstr)
158                       (mm-charset-to-coding-system bsubstr))))
159             (delete-region (point-min) (point-max))
160             (insert (mm-decode-string text charset))))
161         (save-window-excursion
162           (save-restriction
163             (let ((w3-strict-width width)
164                   ;; Don't let w3 set the global version of
165                   ;; this variable.
166                   (fill-column fill-column))
167               (if (or debug-on-error debug-on-quit)
168                   (w3-region (point-min) (point-max))
169                 (condition-case ()
170                     (w3-region (point-min) (point-max))
171                   (error
172                    (delete-region (point-min) (point-max))
173                    (let ((b (point))
174                          (charset (mail-content-type-get
175                                    (mm-handle-type handle) 'charset)))
176                      (if (or (eq charset 'gnus-decoded)
177                              (eq mail-parse-charset 'gnus-decoded))
178                        (save-restriction
179                          (narrow-to-region (point) (point))
180                          (mm-insert-part handle)
181                          (goto-char (point-max)))
182                        (insert (mm-decode-string (mm-get-part handle)
183                                                  charset))))
184                    (message
185                     "Error while rendering html; showing as text/plain")))))))
186         (mm-handle-set-undisplayer
187          handle
188          `(lambda ()
189             (let (buffer-read-only)
190               (if (functionp 'remove-specifier)
191                   (mapcar (lambda (prop)
192                             (remove-specifier
193                              (face-property 'default prop)
194                              (current-buffer)))
195                           '(background background-pixmap foreground)))
196               (delete-region ,(point-min-marker)
197                              ,(point-max-marker)))))))))
198
199 (defvar mm-w3m-setup nil
200   "Whether gnus-article-mode has been setup to use emacs-w3m.")
201
202 (defun mm-setup-w3m ()
203   "Setup gnus-article-mode to use emacs-w3m."
204   (unless mm-w3m-setup
205     (require 'w3m)
206     (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
207       (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
208             w3m-cid-retrieve-function-alist))
209     (setq mm-w3m-setup t))
210   (setq w3m-display-inline-images mm-inline-text-html-with-images))
211
212 (defun mm-w3m-cid-retrieve-1 (url handle)
213   (dolist (elem handle)
214     (when (listp elem)
215       (if (equal url (mm-handle-id elem))
216           (progn
217             (mm-insert-part elem)
218             (throw 'found-handle (mm-handle-media-type elem))))
219       (if (equal "multipart" (mm-handle-media-supertype elem))
220           (mm-w3m-cid-retrieve-1 url elem)))))
221
222 (defun mm-w3m-cid-retrieve (url &rest args)
223   "Insert a content pointed by URL if it has the cid: scheme."
224   (when (string-match "\\`cid:" url)
225     (catch 'found-handle
226       (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
227                              (with-current-buffer w3m-current-buffer
228                                gnus-article-mime-handles)))))
229
230 (defun mm-inline-text-html-render-with-w3m (handle)
231   "Render a text/html part using emacs-w3m."
232   (mm-setup-w3m)
233   (let ((text (mm-get-part handle))
234         (b (point))
235         (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
236     (save-excursion
237       (insert (if charset (mm-decode-string text charset) text))
238       (save-restriction
239         (narrow-to-region b (point))
240         (unless charset
241           (goto-char (point-min))
242           (when (setq charset (w3m-detect-meta-charset))
243             (delete-region (point-min) (point-max))
244             (insert (mm-decode-string text charset))))
245         (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
246               w3m-force-redisplay)
247           (w3m-region (point-min) (point-max) nil charset))
248         (when (and mm-inline-text-html-with-w3m-keymap
249                    (boundp 'w3m-minor-mode-map)
250                    w3m-minor-mode-map)
251           (add-text-properties
252            (point-min) (point-max)
253            (list 'keymap w3m-minor-mode-map
254                  ;; Put the mark meaning this part was rendered by emacs-w3m.
255                  'mm-inline-text-html-with-w3m t)))
256         (mm-handle-set-undisplayer
257          handle
258          `(lambda ()
259             (let (buffer-read-only)
260               (if (functionp 'remove-specifier)
261                   (mapcar (lambda (prop)
262                             (remove-specifier
263                              (face-property 'default prop)
264                              (current-buffer)))
265                           '(background background-pixmap foreground)))
266               (delete-region ,(point-min-marker)
267                              ,(point-max-marker)))))))))
268
269 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
270   "*T means the w3m command supports the m17n feature.")
271
272 (defun mm-w3m-standalone-supports-m17n-p ()
273   "Say whether the w3m command supports the m17n feature."
274   (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
275         ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
276         ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
277         ((condition-case nil
278              (let ((coding-system-for-write 'iso-2022-jp)
279                    (coding-system-for-read 'iso-2022-jp)
280                    (str (mm-decode-coding-string "\
281 \e$B#D#o#e#s!!#w#3#m!!#s#u#p#p#o#r#t#s!!#m#1#7#n!)\e(B" 'iso-2022-jp)))
282                (mm-with-multibyte-buffer
283                  (insert str)
284                  (call-process-region
285                   (point-min) (point-max) "w3m" t t nil "-dump"
286                   "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
287                  (goto-char (point-min))
288                  (search-forward str nil t)))
289            (error nil))
290          (setq mm-w3m-standalone-supports-m17n-p t))
291         (t
292          ;;(message "You had better upgrade your w3m command")
293          (setq mm-w3m-standalone-supports-m17n-p nil))))
294
295 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
296   "Render a text/html part using w3m."
297   (if (mm-w3m-standalone-supports-m17n-p)
298       (let ((source (mm-get-part handle))
299             (charset (mail-content-type-get (mm-handle-type handle) 'charset))
300             cs)
301         (unless (and charset
302                      (setq cs (mm-charset-to-coding-system charset))
303                      (not (eq cs 'ascii)))
304           ;; The default.
305           (setq charset "iso-8859-1"
306                 cs 'iso-8859-1))
307         (mm-insert-inline
308          handle
309          (mm-with-unibyte-buffer
310            (insert source)
311            (mm-enable-multibyte)
312            (let ((coding-system-for-write 'binary)
313                  (coding-system-for-read cs))
314              (call-process-region
315               (point-min) (point-max)
316               "w3m" t t nil "-dump" "-T" "text/html"
317               "-I" charset "-O" charset))
318            (buffer-string))))
319     (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
320
321 (defun mm-links-remove-leading-blank ()
322   ;; Delete the annoying three spaces preceding each line of links
323   ;; output.
324   (goto-char (point-min))
325   (while (re-search-forward "^   " nil t)
326     (delete-region (match-beginning 0) (match-end 0))))
327
328 (defun mm-inline-wash-with-file (post-func cmd &rest args)
329   (let ((file (mm-make-temp-file
330                (expand-file-name "mm" mm-tmp-directory))))
331     (let ((coding-system-for-write 'binary))
332       (write-region (point-min) (point-max) file nil 'silent))
333     (delete-region (point-min) (point-max))
334     (unwind-protect
335         (apply 'call-process cmd nil t nil (mapcar 'eval args))
336       (delete-file file))
337     (and post-func (funcall post-func))))
338
339 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
340   (let ((coding-system-for-write 'binary))
341     (apply 'call-process-region (point-min) (point-max)
342            cmd t t nil args))
343   (and post-func (funcall post-func)))
344
345 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
346   (let ((source (mm-get-part handle)))
347     (mm-insert-inline
348      handle
349      (mm-with-unibyte-buffer
350        (insert source)
351        (apply 'mm-inline-wash-with-file post-func cmd args)
352        (buffer-string)))))
353
354 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
355   (let ((source (mm-get-part handle)))
356     (mm-insert-inline
357      handle
358      (mm-with-unibyte-buffer
359        (insert source)
360        (apply 'mm-inline-wash-with-stdin post-func cmd args)
361        (buffer-string)))))
362
363 (defun mm-inline-render-with-function (handle func &rest args)
364   (let ((source (mm-get-part handle))
365         (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
366     (mm-insert-inline
367      handle
368      (mm-with-multibyte-buffer
369        (insert (if charset
370                    (mm-decode-string source charset)
371                  source))
372        (apply func args)
373        (buffer-string)))))
374
375 (defun mm-inline-text-html (handle)
376   (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
377          (entry (assq func mm-text-html-renderer-alist))
378          buffer-read-only)
379     (if entry
380         (setq func (cdr entry)))
381     (cond
382      ((functionp func)
383       (funcall func handle))
384      (t
385       (apply (car func) handle (cdr func))))))
386
387 (defun mm-inline-text-vcard (handle)
388   (let (buffer-read-only)
389     (mm-insert-inline
390      handle
391      (concat "\n-- \n"
392              (ignore-errors
393                (if (fboundp 'vcard-pretty-print)
394                    (vcard-pretty-print (mm-get-part handle))
395                  (vcard-format-string
396                   (vcard-parse-string (mm-get-part handle)
397                                       'vcard-standard-filter))))))))
398
399 (defun mm-inline-text (handle)
400   (let ((b (point))
401         (type (mm-handle-media-subtype handle))
402         (charset (mail-content-type-get
403                   (mm-handle-type handle) 'charset))
404         buffer-read-only)
405     (if (or (eq charset 'gnus-decoded)
406             ;; This is probably not entirely correct, but
407             ;; makes rfc822 parts with embedded multiparts work.
408             (eq mail-parse-charset 'gnus-decoded))
409         (save-restriction
410           (narrow-to-region (point) (point))
411           (mm-insert-part handle)
412           (goto-char (point-max)))
413       (insert (mm-decode-string (mm-get-part handle) charset)))
414     (when (and mm-fill-flowed
415                (equal type "plain")
416                (equal (cdr (assoc 'format (mm-handle-type handle)))
417                       "flowed"))
418       (save-restriction
419         (narrow-to-region b (point))
420         (goto-char b)
421         (fill-flowed)
422         (goto-char (point-max))))
423     (save-restriction
424       (narrow-to-region b (point))
425       (when (or (equal type "enriched")
426                 (equal type "richtext"))
427         (set-text-properties (point-min) (point-max) nil)
428         (ignore-errors
429           (enriched-decode (point-min) (point-max))))
430       (mm-handle-set-undisplayer
431        handle
432        `(lambda ()
433           (let (buffer-read-only)
434             (delete-region ,(point-min-marker)
435                            ,(point-max-marker))))))))
436
437 (defun mm-insert-inline (handle text)
438   "Insert TEXT inline from HANDLE."
439   (let ((b (point)))
440     (insert text)
441     (unless (bolp)
442       (insert "\n"))
443     (mm-handle-set-undisplayer
444      handle
445      `(lambda ()
446         (let (buffer-read-only)
447           (delete-region ,(set-marker (make-marker) b)
448                          ,(set-marker (make-marker) (point))))))))
449
450 (defun mm-inline-audio (handle)
451   (message "Not implemented"))
452
453 (defun mm-view-sound-file ()
454   (message "Not implemented"))
455
456 (defun mm-w3-prepare-buffer ()
457   (require 'w3)
458   (let ((url-standalone-mode t)
459         (url-gateway-unplugged t)
460         (w3-honor-stylesheets nil))
461     (w3-prepare-buffer)))
462
463 (defun mm-view-message ()
464   (mm-enable-multibyte)
465   (let (handles)
466     (let (gnus-article-mime-handles)
467       ;; Double decode problem may happen.  See mm-inline-message.
468       (run-hooks 'gnus-article-decode-hook)
469       (gnus-article-prepare-display)
470       (setq handles gnus-article-mime-handles))
471     (when handles
472       (setq gnus-article-mime-handles
473             (mm-merge-handles gnus-article-mime-handles handles))))
474   (fundamental-mode)
475   (goto-char (point-min)))
476
477 (defun mm-inline-message (handle)
478   (let ((b (point))
479         (bolp (bolp))
480         (charset (mail-content-type-get
481                   (mm-handle-type handle) 'charset))
482         gnus-displaying-mime handles)
483     (when (and charset
484                (stringp charset))
485       (setq charset (intern (downcase charset)))
486       (when (eq charset 'us-ascii)
487         (setq charset nil)))
488     (save-excursion
489       (save-restriction
490         (narrow-to-region b b)
491         (mm-insert-part handle)
492         (let (gnus-article-mime-handles
493               ;; disable prepare hook
494               gnus-article-prepare-hook
495               (gnus-newsgroup-charset
496                (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
497                  (or charset gnus-newsgroup-charset))))
498           (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
499             (run-hooks 'gnus-article-decode-hook))
500           (gnus-article-prepare-display)
501           (setq handles gnus-article-mime-handles))
502         (goto-char (point-min))
503         (unless bolp
504           (insert "\n"))
505         (goto-char (point-max))
506         (unless (bolp)
507           (insert "\n"))
508         (insert "----------\n\n")
509         (when handles
510           (setq gnus-article-mime-handles
511                 (mm-merge-handles gnus-article-mime-handles handles)))
512         (mm-handle-set-undisplayer
513          handle
514          `(lambda ()
515             (let (buffer-read-only)
516               (if (fboundp 'remove-specifier)
517                   ;; This is only valid on XEmacs.
518                   (mapcar (lambda (prop)
519                             (remove-specifier
520                              (face-property 'default prop) (current-buffer)))
521                           '(background background-pixmap foreground)))
522               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
523
524 (defun mm-display-inline-fontify (handle mode)
525   (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
526         text coding-system)
527     (unless (eq charset 'gnus-decoded)
528       (mm-with-unibyte-buffer
529         (mm-insert-part handle)
530         (mm-decompress-buffer
531          (or (mail-content-type-get (mm-handle-disposition handle) 'name)
532              (mail-content-type-get (mm-handle-disposition handle) 'filename))
533          t t)
534         (unless charset
535           (setq coding-system (mm-find-buffer-file-coding-system)))
536         (setq text (buffer-string))))
537     ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
538     ;; on for buffers whose name begins with " ".  That's why we use
539     ;; `with-current-buffer'/`generate-new-buffer' rather than
540     ;; `with-temp-buffer'.
541     (with-current-buffer (generate-new-buffer "*fontification*")
542       (buffer-disable-undo)
543       (mm-enable-multibyte)
544       (insert (cond ((eq charset 'gnus-decoded)
545                      (with-current-buffer (mm-handle-buffer handle)
546                        (buffer-string)))
547                     (coding-system
548                      (mm-decode-coding-string text coding-system))
549                     (charset
550                      (mm-decode-string text charset))
551                     (t
552                      text)))
553       (require 'font-lock)
554       (let ((font-lock-maximum-size nil)
555             ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
556             (font-lock-mode-hook nil)
557             (font-lock-support-mode nil)
558             ;; I find font-lock a bit too verbose.
559             (font-lock-verbose nil))
560         (funcall mode)
561         ;; The mode function might have already turned on font-lock.
562         (unless (symbol-value 'font-lock-mode)
563           (font-lock-fontify-buffer)))
564       ;; By default, XEmacs font-lock uses non-duplicable text
565       ;; properties.  This code forces all the text properties
566       ;; to be copied along with the text.
567       (when (fboundp 'extent-list)
568         (map-extents (lambda (ext ignored)
569                        (set-extent-property ext 'duplicable t)
570                        nil)
571                      nil nil nil nil nil 'text-prop))
572       (setq text (buffer-string))
573       (kill-buffer (current-buffer)))
574     (mm-insert-inline handle text)))
575
576 ;; Shouldn't these functions check whether the user even wants to use
577 ;; font-lock?  At least under XEmacs, this fontification is pretty
578 ;; much unconditional.  Also, it would be nice to change for the size
579 ;; of the fontified region.
580
581 (defun mm-display-patch-inline (handle)
582   (mm-display-inline-fontify handle 'diff-mode))
583
584 (defun mm-display-elisp-inline (handle)
585   (mm-display-inline-fontify handle 'emacs-lisp-mode))
586
587 (defun mm-display-dns-inline (handle)
588   (mm-display-inline-fontify handle 'dns-mode))
589
590 ;;      id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
591 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
592 (defvar mm-pkcs7-signed-magic
593   (mm-string-as-unibyte
594    (mapconcat 'char-to-string
595               (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
596                     ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
597                     ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
598                     ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02) "")))
599
600 ;;      id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
601 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
602 (defvar mm-pkcs7-enveloped-magic
603   (mm-string-as-unibyte
604    (mapconcat 'char-to-string
605               (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
606                     ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
607                     ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
608                     ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03) "")))
609
610 (defun mm-view-pkcs7-get-type (handle)
611   (mm-with-unibyte-buffer
612     (mm-insert-part handle)
613     (cond ((looking-at mm-pkcs7-enveloped-magic)
614            'enveloped)
615           ((looking-at mm-pkcs7-signed-magic)
616            'signed)
617           (t
618            (error "Could not identify PKCS#7 type")))))
619
620 (defun mm-view-pkcs7 (handle)
621   (case (mm-view-pkcs7-get-type handle)
622     (enveloped (mm-view-pkcs7-decrypt handle))
623     (signed (mm-view-pkcs7-verify handle))
624     (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
625
626 (defun mm-view-pkcs7-verify (handle)
627   (let ((verified nil))
628     (with-temp-buffer
629       (insert "MIME-Version: 1.0\n")
630       (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
631       (insert-buffer-substring (mm-handle-buffer handle))
632       (setq verified (smime-verify-region (point-min) (point-max))))
633     (goto-char (point-min))
634     (mm-insert-part handle)
635     (if (search-forward "Content-Type: " nil t)
636         (delete-region (point-min) (match-beginning 0)))
637     (goto-char (point-max))
638     (if (re-search-backward "--\r?\n?" nil t)
639         (delete-region (match-end 0) (point-max)))
640     (unless verified
641       (insert-buffer-substring smime-details-buffer)))
642   (goto-char (point-min))
643   (while (search-forward "\r\n" nil t)
644     (replace-match "\n"))
645   t)
646
647 (defun mm-view-pkcs7-decrypt (handle)
648   (insert-buffer-substring (mm-handle-buffer handle))
649   (goto-char (point-min))
650   (insert "MIME-Version: 1.0\n")
651   (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
652   (smime-decrypt-region
653    (point-min) (point-max)
654    (if (= (length smime-keys) 1)
655        (cadar smime-keys)
656      (smime-get-key-by-email
657       (completing-read
658        (concat "Decipher using key"
659                (if smime-keys (concat "(default " (caar smime-keys) "): ")
660                  ": "))
661        smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
662   (goto-char (point-min))
663   (while (search-forward "\r\n" nil t)
664     (replace-match "\n"))
665   (goto-char (point-min)))
666
667 (provide 'mm-view)
668
669 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
670 ;;; mm-view.el ends here