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