]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-view.el
*** empty log message ***
[gnu-emacs] / 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, 2007 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 3, 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" nil t)
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 (or (mail-content-type-get (mm-handle-type handle) 'charset)
242 mail-parse-charset)))
243 (save-excursion
244 (insert (if charset (mm-decode-string text charset) text))
245 (save-restriction
246 (narrow-to-region b (point))
247 (unless charset
248 (goto-char (point-min))
249 (when (setq charset (w3m-detect-meta-charset))
250 (delete-region (point-min) (point-max))
251 (insert (mm-decode-string text charset))))
252 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
253 w3m-force-redisplay)
254 (w3m-region (point-min) (point-max) nil charset))
255 (when (and mm-inline-text-html-with-w3m-keymap
256 (boundp 'w3m-minor-mode-map)
257 w3m-minor-mode-map)
258 (add-text-properties
259 (point-min) (point-max)
260 (list 'keymap w3m-minor-mode-map
261 ;; Put the mark meaning this part was rendered by emacs-w3m.
262 'mm-inline-text-html-with-w3m t)))
263 (mm-handle-set-undisplayer
264 handle
265 `(lambda ()
266 (let (buffer-read-only)
267 (if (functionp 'remove-specifier)
268 (mapcar (lambda (prop)
269 (remove-specifier
270 (face-property 'default prop)
271 (current-buffer)))
272 '(background background-pixmap foreground)))
273 (delete-region ,(point-min-marker)
274 ,(point-max-marker)))))))))
275
276 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
277 "*T means the w3m command supports the m17n feature.")
278
279 (defun mm-w3m-standalone-supports-m17n-p ()
280 "Say whether the w3m command supports the m17n feature."
281 (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
282 ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
283 ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
284 ((condition-case nil
285 (let ((coding-system-for-write 'iso-2022-jp)
286 (coding-system-for-read 'iso-2022-jp)
287 (str (mm-decode-coding-string "\
288 \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)))
289 (mm-with-multibyte-buffer
290 (insert str)
291 (call-process-region
292 (point-min) (point-max) "w3m" t t nil "-dump"
293 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
294 (goto-char (point-min))
295 (search-forward str nil t)))
296 (error nil))
297 (setq mm-w3m-standalone-supports-m17n-p t))
298 (t
299 ;;(message "You had better upgrade your w3m command")
300 (setq mm-w3m-standalone-supports-m17n-p nil))))
301
302 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
303 "Render a text/html part using w3m."
304 (if (mm-w3m-standalone-supports-m17n-p)
305 (let ((source (mm-get-part handle))
306 (charset (or (mail-content-type-get (mm-handle-type handle)
307 'charset)
308 (symbol-name mail-parse-charset)))
309 cs)
310 (unless (and charset
311 (setq cs (mm-charset-to-coding-system charset))
312 (not (eq cs 'ascii)))
313 ;; The default.
314 (setq charset "iso-8859-1"
315 cs 'iso-8859-1))
316 (mm-insert-inline
317 handle
318 (mm-with-unibyte-buffer
319 (insert source)
320 (mm-enable-multibyte)
321 (let ((coding-system-for-write 'binary)
322 (coding-system-for-read cs))
323 (call-process-region
324 (point-min) (point-max)
325 "w3m" t t nil "-dump" "-T" "text/html"
326 "-I" charset "-O" charset))
327 (buffer-string))))
328 (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
329
330 (defun mm-links-remove-leading-blank ()
331 ;; Delete the annoying three spaces preceding each line of links
332 ;; output.
333 (goto-char (point-min))
334 (while (re-search-forward "^ " nil t)
335 (delete-region (match-beginning 0) (match-end 0))))
336
337 (defun mm-inline-wash-with-file (post-func cmd &rest args)
338 (let ((file (mm-make-temp-file
339 (expand-file-name "mm" mm-tmp-directory))))
340 (let ((coding-system-for-write 'binary))
341 (write-region (point-min) (point-max) file nil 'silent))
342 (delete-region (point-min) (point-max))
343 (unwind-protect
344 (apply 'call-process cmd nil t nil (mapcar 'eval args))
345 (delete-file file))
346 (and post-func (funcall post-func))))
347
348 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
349 (let ((coding-system-for-write 'binary))
350 (apply 'call-process-region (point-min) (point-max)
351 cmd t t nil args))
352 (and post-func (funcall post-func)))
353
354 (defun mm-inline-render-with-file (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-file post-func cmd args)
361 (buffer-string)))))
362
363 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
364 (let ((source (mm-get-part handle)))
365 (mm-insert-inline
366 handle
367 (mm-with-unibyte-buffer
368 (insert source)
369 (apply 'mm-inline-wash-with-stdin post-func cmd args)
370 (buffer-string)))))
371
372 (defun mm-inline-render-with-function (handle func &rest args)
373 (let ((source (mm-get-part handle))
374 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
375 mail-parse-charset)))
376 (mm-insert-inline
377 handle
378 (mm-with-multibyte-buffer
379 (insert (if charset
380 (mm-decode-string source charset)
381 source))
382 (apply func args)
383 (buffer-string)))))
384
385 (defun mm-inline-text-html (handle)
386 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
387 (entry (assq func mm-text-html-renderer-alist))
388 buffer-read-only)
389 (if entry
390 (setq func (cdr entry)))
391 (cond
392 ((functionp func)
393 (funcall func handle))
394 (t
395 (apply (car func) handle (cdr func))))))
396
397 (defun mm-inline-text-vcard (handle)
398 (let (buffer-read-only)
399 (mm-insert-inline
400 handle
401 (concat "\n-- \n"
402 (ignore-errors
403 (if (fboundp 'vcard-pretty-print)
404 (vcard-pretty-print (mm-get-part handle))
405 (vcard-format-string
406 (vcard-parse-string (mm-get-part handle)
407 'vcard-standard-filter))))))))
408
409 (defun mm-inline-text (handle)
410 (let ((b (point))
411 (type (mm-handle-media-subtype handle))
412 (charset (mail-content-type-get
413 (mm-handle-type handle) 'charset))
414 buffer-read-only)
415 (if (or (eq charset 'gnus-decoded)
416 ;; This is probably not entirely correct, but
417 ;; makes rfc822 parts with embedded multiparts work.
418 (eq mail-parse-charset 'gnus-decoded))
419 (save-restriction
420 (narrow-to-region (point) (point))
421 (mm-insert-part handle)
422 (goto-char (point-max)))
423 (insert (mm-decode-string (mm-get-part handle) charset)))
424 (when (and mm-fill-flowed
425 (equal type "plain")
426 (equal (cdr (assoc 'format (mm-handle-type handle)))
427 "flowed"))
428 (save-restriction
429 (narrow-to-region b (point))
430 (goto-char b)
431 (fill-flowed)
432 (goto-char (point-max))))
433 (save-restriction
434 (narrow-to-region b (point))
435 (when (or (equal type "enriched")
436 (equal type "richtext"))
437 (set-text-properties (point-min) (point-max) nil)
438 (ignore-errors
439 (enriched-decode (point-min) (point-max))))
440 (mm-handle-set-undisplayer
441 handle
442 `(lambda ()
443 (let (buffer-read-only)
444 (delete-region ,(point-min-marker)
445 ,(point-max-marker))))))))
446
447 (defun mm-insert-inline (handle text)
448 "Insert TEXT inline from HANDLE."
449 (let ((b (point)))
450 (insert text)
451 (mm-handle-set-undisplayer
452 handle
453 `(lambda ()
454 (let (buffer-read-only)
455 (delete-region ,(set-marker (make-marker) b)
456 ,(set-marker (make-marker) (point))))))))
457
458 (defun mm-inline-audio (handle)
459 (message "Not implemented"))
460
461 (defun mm-view-sound-file ()
462 (message "Not implemented"))
463
464 (defun mm-w3-prepare-buffer ()
465 (require 'w3)
466 (let ((url-standalone-mode t)
467 (url-gateway-unplugged t)
468 (w3-honor-stylesheets nil))
469 (w3-prepare-buffer)))
470
471 (defun mm-view-message ()
472 (mm-enable-multibyte)
473 (let (handles)
474 (let (gnus-article-mime-handles)
475 ;; Double decode problem may happen. See mm-inline-message.
476 (run-hooks 'gnus-article-decode-hook)
477 (gnus-article-prepare-display)
478 (setq handles gnus-article-mime-handles))
479 (when handles
480 (setq gnus-article-mime-handles
481 (mm-merge-handles gnus-article-mime-handles handles))))
482 (fundamental-mode)
483 (goto-char (point-min)))
484
485 (defun mm-inline-message (handle)
486 (let ((b (point))
487 (bolp (bolp))
488 (charset (mail-content-type-get
489 (mm-handle-type handle) 'charset))
490 gnus-displaying-mime handles)
491 (when (and charset
492 (stringp charset))
493 (setq charset (intern (downcase charset)))
494 (when (eq charset 'us-ascii)
495 (setq charset nil)))
496 (save-excursion
497 (save-restriction
498 (narrow-to-region b b)
499 (mm-insert-part handle)
500 (let (gnus-article-mime-handles
501 ;; disable prepare hook
502 gnus-article-prepare-hook
503 (gnus-newsgroup-charset
504 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
505 (or charset gnus-newsgroup-charset))))
506 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
507 (run-hooks 'gnus-article-decode-hook))
508 (gnus-article-prepare-display)
509 (setq handles gnus-article-mime-handles))
510 (goto-char (point-min))
511 (unless bolp
512 (insert "\n"))
513 (goto-char (point-max))
514 (unless (bolp)
515 (insert "\n"))
516 (insert "----------\n\n")
517 (when handles
518 (setq gnus-article-mime-handles
519 (mm-merge-handles gnus-article-mime-handles handles)))
520 (mm-handle-set-undisplayer
521 handle
522 `(lambda ()
523 (let (buffer-read-only)
524 (if (fboundp 'remove-specifier)
525 ;; This is only valid on XEmacs.
526 (mapcar (lambda (prop)
527 (remove-specifier
528 (face-property 'default prop) (current-buffer)))
529 '(background background-pixmap foreground)))
530 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
531
532 (defun mm-display-inline-fontify (handle mode)
533 (let (text)
534 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
535 ;; on for buffers whose name begins with " ". That's why we use
536 ;; save-current-buffer/get-buffer-create rather than
537 ;; with-temp-buffer.
538 (save-current-buffer
539 (set-buffer (generate-new-buffer "*fontification*"))
540 (unwind-protect
541 (progn
542 (buffer-disable-undo)
543 (mm-insert-part handle)
544 (require 'font-lock)
545 (let ((font-lock-maximum-size nil)
546 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
547 (font-lock-mode-hook nil)
548 (font-lock-support-mode nil)
549 ;; I find font-lock a bit too verbose.
550 (font-lock-verbose nil))
551 (funcall mode)
552 ;; The mode function might have already turned on font-lock.
553 (unless (symbol-value 'font-lock-mode)
554 (font-lock-fontify-buffer)))
555 ;; By default, XEmacs font-lock uses non-duplicable text
556 ;; properties. This code forces all the text properties
557 ;; to be copied along with the text.
558 (when (fboundp 'extent-list)
559 (map-extents (lambda (ext ignored)
560 (set-extent-property ext 'duplicable t)
561 nil)
562 nil nil nil nil nil 'text-prop))
563 (setq text (buffer-string)))
564 (kill-buffer (current-buffer))))
565 (mm-insert-inline handle text)))
566
567 ;; Shouldn't these functions check whether the user even wants to use
568 ;; font-lock? At least under XEmacs, this fontification is pretty
569 ;; much unconditional. Also, it would be nice to change for the size
570 ;; of the fontified region.
571
572 (defun mm-display-patch-inline (handle)
573 (mm-display-inline-fontify handle 'diff-mode))
574
575 (defun mm-display-elisp-inline (handle)
576 (mm-display-inline-fontify handle 'emacs-lisp-mode))
577
578 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
579 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
580 (defvar mm-pkcs7-signed-magic
581 (mm-string-as-unibyte
582 (apply 'concat
583 (mapcar 'char-to-string
584 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
585 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
586 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
587 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
588
589 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
590 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
591 (defvar mm-pkcs7-enveloped-magic
592 (mm-string-as-unibyte
593 (apply 'concat
594 (mapcar '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 ?\x03)))))
599
600 (defun mm-view-pkcs7-get-type (handle)
601 (mm-with-unibyte-buffer
602 (mm-insert-part handle)
603 (cond ((looking-at mm-pkcs7-enveloped-magic)
604 'enveloped)
605 ((looking-at mm-pkcs7-signed-magic)
606 'signed)
607 (t
608 (error "Could not identify PKCS#7 type")))))
609
610 (defun mm-view-pkcs7 (handle)
611 (case (mm-view-pkcs7-get-type handle)
612 (enveloped (mm-view-pkcs7-decrypt handle))
613 (signed (mm-view-pkcs7-verify handle))
614 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
615
616 (defun mm-view-pkcs7-verify (handle)
617 ;; A bogus implementation of PKCS#7. FIXME::
618 (mm-insert-part handle)
619 (goto-char (point-min))
620 (if (search-forward "Content-Type: " nil t)
621 (delete-region (point-min) (match-beginning 0)))
622 (goto-char (point-max))
623 (if (re-search-backward "--\r?\n?" nil t)
624 (delete-region (match-end 0) (point-max)))
625 (goto-char (point-min))
626 (while (search-forward "\r\n" nil t)
627 (replace-match "\n"))
628 (message "Verify signed PKCS#7 message is unimplemented.")
629 (sit-for 1)
630 t)
631
632 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
633
634 (defun mm-view-pkcs7-decrypt (handle)
635 (insert-buffer-substring (mm-handle-buffer handle))
636 (goto-char (point-min))
637 (insert "MIME-Version: 1.0\n")
638 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
639 (smime-decrypt-region
640 (point-min) (point-max)
641 (if (= (length smime-keys) 1)
642 (cadar smime-keys)
643 (smime-get-key-by-email
644 (gnus-completing-read-maybe-default
645 (concat "Decipher using key"
646 (if smime-keys
647 (concat " (default " (caar smime-keys) "): ")
648 ": "))
649 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
650 (goto-char (point-min))
651 (while (search-forward "\r\n" nil t)
652 (replace-match "\n"))
653 (goto-char (point-min)))
654
655 (provide 'mm-view)
656
657 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
658 ;;; mm-view.el ends here