Add `&' `dired-do-async-shell-command'.
[emacs.git] / lisp / gnus / mm-view.el
blobbfadd47200dc89a5e065814696b690fbd1f50f36
1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
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 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
25 (eval-and-compile
26 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
27 (eval-when-compile (require 'cl))
28 (require 'mail-parse)
29 (require 'mailcap)
30 (require 'mm-bodies)
31 (require 'mm-decode)
32 (require 'smime)
34 (autoload 'gnus-article-prepare-display "gnus-art")
35 (autoload 'vcard-parse-string "vcard")
36 (autoload 'vcard-format-string "vcard")
37 (autoload 'fill-flowed "flow-fill")
38 (autoload 'html2text "html2text" nil t)
40 (defvar gnus-article-mime-handles)
41 (defvar gnus-newsgroup-charset)
42 (defvar smime-keys)
43 (defvar w3m-cid-retrieve-function-alist)
44 (defvar w3m-current-buffer)
45 (defvar w3m-display-inline-images)
46 (defvar w3m-minor-mode-map)
48 (defvar mm-text-html-renderer-alist
49 '((w3 . mm-inline-text-html-render-with-w3)
50 (w3m . mm-inline-text-html-render-with-w3m)
51 (w3m-standalone . mm-inline-text-html-render-with-w3m-standalone)
52 (links mm-inline-render-with-file
53 mm-links-remove-leading-blank
54 "links" "-dump" file)
55 (lynx mm-inline-render-with-stdin nil
56 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
57 (html2text mm-inline-render-with-function html2text))
58 "The attributes of renderer types for text/html.")
60 (defvar mm-text-html-washer-alist
61 '((w3 . gnus-article-wash-html-with-w3)
62 (w3m . gnus-article-wash-html-with-w3m)
63 (w3m-standalone . gnus-article-wash-html-with-w3m-standalone)
64 (links mm-inline-wash-with-file
65 mm-links-remove-leading-blank
66 "links" "-dump" file)
67 (lynx mm-inline-wash-with-stdin nil
68 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
69 (html2text html2text))
70 "The attributes of washer types for text/html.")
72 (defcustom mm-fill-flowed t
73 "If non-nil a format=flowed article will be displayed flowed."
74 :type 'boolean
75 :version "22.1"
76 :group 'mime-display)
78 ;;; Internal variables.
80 ;;;
81 ;;; Functions for displaying various formats inline
82 ;;;
84 (defun mm-inline-image-emacs (handle)
85 (let ((b (point-marker))
86 (inhibit-read-only t))
87 (put-image (mm-get-image handle) b)
88 (insert "\n\n")
89 (mm-handle-set-undisplayer
90 handle
91 `(lambda ()
92 (let ((b ,b)
93 (inhibit-read-only t))
94 (remove-images b b)
95 (delete-region b (+ b 2)))))))
97 (defun mm-inline-image-xemacs (handle)
98 (when (featurep 'xemacs)
99 (insert "\n\n")
100 (forward-char -2)
101 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
102 (inhibit-read-only t))
103 (mm-handle-set-undisplayer
104 handle
105 `(lambda ()
106 (let ((b ,(point-marker))
107 (inhibit-read-only t))
108 (delete-annotation ,annot)
109 (delete-region (- b 2) b))))
110 (set-extent-property annot 'mm t)
111 (set-extent-property annot 'duplicable t))))
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)))
118 ;; External.
119 (declare-function w3-do-setup "ext:w3" ())
120 (declare-function w3-region "ext:w3-display" (st nd))
121 (declare-function w3-prepare-buffer "ext:w3-display" (&rest args))
123 (defvar mm-w3-setup nil)
124 (defun mm-setup-w3 ()
125 (unless mm-w3-setup
126 (require 'w3)
127 (w3-do-setup)
128 (require 'url)
129 (require 'w3-vars)
130 (require 'url-vars)
131 (setq mm-w3-setup t)))
133 (defun mm-inline-text-html-render-with-w3 (handle)
134 (mm-setup-w3)
135 (let ((text (mm-get-part handle))
136 (b (point))
137 (url-standalone-mode t)
138 (url-gateway-unplugged t)
139 (w3-honor-stylesheets nil)
140 (url-current-object
141 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
142 (width (window-width))
143 (charset (mail-content-type-get
144 (mm-handle-type handle) 'charset)))
145 (save-excursion
146 (insert (if charset (mm-decode-string text charset) text))
147 (save-restriction
148 (narrow-to-region b (point))
149 (unless charset
150 (goto-char (point-min))
151 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
152 (re-search-forward
153 w3-meta-content-type-charset-regexp nil t))
154 (and (boundp 'w3-meta-charset-content-type-regexp)
155 (re-search-forward
156 w3-meta-charset-content-type-regexp nil t)))
157 (setq charset
158 (let ((bsubstr (buffer-substring-no-properties
159 (match-beginning 2)
160 (match-end 2))))
161 (if (fboundp 'w3-coding-system-for-mime-charset)
162 (w3-coding-system-for-mime-charset bsubstr)
163 (mm-charset-to-coding-system bsubstr))))
164 (delete-region (point-min) (point-max))
165 (insert (mm-decode-string text charset))))
166 (save-window-excursion
167 (save-restriction
168 (let ((w3-strict-width width)
169 ;; Don't let w3 set the global version of
170 ;; this variable.
171 (fill-column fill-column))
172 (if (or debug-on-error debug-on-quit)
173 (w3-region (point-min) (point-max))
174 (condition-case ()
175 (w3-region (point-min) (point-max))
176 (error
177 (delete-region (point-min) (point-max))
178 (let ((b (point))
179 (charset (mail-content-type-get
180 (mm-handle-type handle) 'charset)))
181 (if (or (eq charset 'gnus-decoded)
182 (eq mail-parse-charset 'gnus-decoded))
183 (save-restriction
184 (narrow-to-region (point) (point))
185 (mm-insert-part handle)
186 (goto-char (point-max)))
187 (insert (mm-decode-string (mm-get-part handle)
188 charset))))
189 (message
190 "Error while rendering html; showing as text/plain")))))))
191 (mm-handle-set-undisplayer
192 handle
193 `(lambda ()
194 (let ((inhibit-read-only t))
195 ,@(if (functionp 'remove-specifier)
196 '((dolist (prop '(background background-pixmap foreground))
197 (remove-specifier
198 (face-property 'default prop)
199 (current-buffer)))))
200 (delete-region ,(point-min-marker)
201 ,(point-max-marker)))))))))
203 (defvar mm-w3m-setup nil
204 "Whether gnus-article-mode has been setup to use emacs-w3m.")
206 ;; External.
207 (declare-function w3m-detect-meta-charset "ext:w3m" ())
208 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
210 (defun mm-setup-w3m ()
211 "Setup gnus-article-mode to use emacs-w3m."
212 (unless mm-w3m-setup
213 (require 'w3m)
214 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
215 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
216 w3m-cid-retrieve-function-alist))
217 (setq mm-w3m-setup t))
218 (setq w3m-display-inline-images mm-inline-text-html-with-images))
220 (defun mm-w3m-cid-retrieve-1 (url handle)
221 (dolist (elem handle)
222 (when (consp elem)
223 (when (equal url (mm-handle-id elem))
224 (mm-insert-part elem)
225 (throw 'found-handle (mm-handle-media-type elem)))
226 (when (and (stringp (car elem))
227 (equal "multipart" (mm-handle-media-supertype elem)))
228 (mm-w3m-cid-retrieve-1 url elem)))))
230 (defun mm-w3m-cid-retrieve (url &rest args)
231 "Insert a content pointed by URL if it has the cid: scheme."
232 (when (string-match "\\`cid:" url)
233 (or (catch 'found-handle
234 (mm-w3m-cid-retrieve-1
235 (setq url (concat "<" (substring url (match-end 0)) ">"))
236 (with-current-buffer w3m-current-buffer
237 gnus-article-mime-handles)))
238 (prog1
240 (message "Failed to find \"Content-ID: %s\"" url)))))
242 (defun mm-inline-text-html-render-with-w3m (handle)
243 "Render a text/html part using emacs-w3m."
244 (mm-setup-w3m)
245 (let ((text (mm-get-part handle))
246 (b (point))
247 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
248 mail-parse-charset)))
249 (save-excursion
250 (insert (if charset (mm-decode-string text charset) text))
251 (save-restriction
252 (narrow-to-region b (point))
253 (unless charset
254 (goto-char (point-min))
255 (when (setq charset (w3m-detect-meta-charset))
256 (delete-region (point-min) (point-max))
257 (insert (mm-decode-string text charset))))
258 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
259 w3m-force-redisplay)
260 (w3m-region (point-min) (point-max) nil charset))
261 (when (and mm-inline-text-html-with-w3m-keymap
262 (boundp 'w3m-minor-mode-map)
263 w3m-minor-mode-map)
264 (add-text-properties
265 (point-min) (point-max)
266 (list 'keymap w3m-minor-mode-map
267 ;; Put the mark meaning this part was rendered by emacs-w3m.
268 'mm-inline-text-html-with-w3m t)))
269 (mm-handle-set-undisplayer
270 handle
271 `(lambda ()
272 (let ((inhibit-read-only t))
273 (delete-region ,(point-min-marker)
274 ,(point-max-marker)))))))))
276 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
277 "*T means the w3m command supports the m17n feature.")
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!!#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))
299 ;;(message "You had better upgrade your w3m command")
300 (setq mm-w3m-standalone-supports-m17n-p nil))))
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)))
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")))
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))))
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))))
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)))
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)))))
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)))))
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)))))
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 (inhibit-read-only t))
389 (if entry
390 (setq func (cdr entry)))
391 (cond
392 ((functionp func)
393 (funcall func handle))
395 (apply (car func) handle (cdr func))))))
397 (defun mm-inline-text-vcard (handle)
398 (let ((inhibit-read-only t))
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))))))))
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 (inhibit-read-only t))
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 nil (equal (cdr (assoc 'delsp (mm-handle-type handle)))
432 "yes"))
433 (goto-char (point-max))))
434 (save-restriction
435 (narrow-to-region b (point))
436 (when (member type '("enriched" "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 ((inhibit-read-only t))
444 (delete-region ,(point-min-marker)
445 ,(point-max-marker))))))))
447 (defun mm-insert-inline (handle text)
448 "Insert TEXT inline from HANDLE."
449 (let ((b (point)))
450 (insert text)
451 (unless (bolp)
452 (insert "\n"))
453 (mm-handle-set-undisplayer
454 handle
455 `(lambda ()
456 (let ((inhibit-read-only t))
457 (delete-region ,(copy-marker b)
458 ,(copy-marker (point))))))))
460 (defun mm-inline-audio (handle)
461 (message "Not implemented"))
463 (defun mm-view-sound-file ()
464 (message "Not implemented"))
466 (defun mm-w3-prepare-buffer ()
467 (require 'w3)
468 (let ((url-standalone-mode t)
469 (url-gateway-unplugged t)
470 (w3-honor-stylesheets nil))
471 (w3-prepare-buffer)))
473 (defun mm-view-message ()
474 (mm-enable-multibyte)
475 (let (handles)
476 (let (gnus-article-mime-handles)
477 ;; Double decode problem may happen. See mm-inline-message.
478 (run-hooks 'gnus-article-decode-hook)
479 (gnus-article-prepare-display)
480 (setq handles gnus-article-mime-handles))
481 (when handles
482 (setq gnus-article-mime-handles
483 (mm-merge-handles gnus-article-mime-handles handles))))
484 (fundamental-mode)
485 (goto-char (point-min)))
487 (defun mm-inline-message (handle)
488 (let ((b (point))
489 (bolp (bolp))
490 (charset (mail-content-type-get
491 (mm-handle-type handle) 'charset))
492 gnus-displaying-mime handles)
493 (when (and charset
494 (stringp charset))
495 (setq charset (intern (downcase charset)))
496 (when (eq charset 'us-ascii)
497 (setq charset nil)))
498 (save-excursion
499 (save-restriction
500 (narrow-to-region b b)
501 (mm-insert-part handle)
502 (let (gnus-article-mime-handles
503 ;; disable prepare hook
504 gnus-article-prepare-hook
505 (gnus-newsgroup-charset
506 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
507 (or charset gnus-newsgroup-charset))))
508 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
509 (run-hooks 'gnus-article-decode-hook))
510 (gnus-article-prepare-display)
511 (setq handles gnus-article-mime-handles))
512 (goto-char (point-min))
513 (unless bolp
514 (insert "\n"))
515 (goto-char (point-max))
516 (unless (bolp)
517 (insert "\n"))
518 (insert "----------\n\n")
519 (when handles
520 (setq gnus-article-mime-handles
521 (mm-merge-handles gnus-article-mime-handles handles)))
522 (mm-handle-set-undisplayer
523 handle
524 `(lambda ()
525 (let ((inhibit-read-only t))
526 (if (fboundp 'remove-specifier)
527 ;; This is only valid on XEmacs.
528 (dolist (prop '(background background-pixmap foreground))
529 (remove-specifier
530 (face-property 'default prop) (current-buffer))))
531 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
533 (defun mm-display-inline-fontify (handle mode)
534 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
535 text coding-system)
536 (unless (eq charset 'gnus-decoded)
537 (mm-with-unibyte-buffer
538 (mm-insert-part handle)
539 (mm-decompress-buffer
540 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
541 (mail-content-type-get (mm-handle-disposition handle) 'filename))
542 t t)
543 (unless charset
544 (setq coding-system (mm-find-buffer-file-coding-system)))
545 (setq text (buffer-string))))
546 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
547 ;; on for buffers whose name begins with " ". That's why we use
548 ;; `with-current-buffer'/`generate-new-buffer' rather than
549 ;; `with-temp-buffer'.
550 (with-current-buffer (generate-new-buffer "*fontification*")
551 (buffer-disable-undo)
552 (mm-enable-multibyte)
553 (insert (cond ((eq charset 'gnus-decoded)
554 (with-current-buffer (mm-handle-buffer handle)
555 (buffer-string)))
556 (coding-system
557 (mm-decode-coding-string text coding-system))
558 (charset
559 (mm-decode-string text charset))
561 text)))
562 (require 'font-lock)
563 (let ((font-lock-maximum-size nil)
564 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
565 (font-lock-mode-hook nil)
566 (font-lock-support-mode nil)
567 ;; I find font-lock a bit too verbose.
568 (font-lock-verbose nil))
569 (funcall mode)
570 ;; The mode function might have already turned on font-lock.
571 (unless (symbol-value 'font-lock-mode)
572 (font-lock-fontify-buffer)))
573 ;; By default, XEmacs font-lock uses non-duplicable text
574 ;; properties. This code forces all the text properties
575 ;; to be copied along with the text.
576 (when (featurep 'xemacs)
577 (map-extents (lambda (ext ignored)
578 (set-extent-property ext 'duplicable t)
579 nil)
580 nil nil nil nil nil 'text-prop))
581 (setq text (buffer-string))
582 (kill-buffer (current-buffer)))
583 (mm-insert-inline handle text)))
585 ;; Shouldn't these functions check whether the user even wants to use
586 ;; font-lock? At least under XEmacs, this fontification is pretty
587 ;; much unconditional. Also, it would be nice to change for the size
588 ;; of the fontified region.
590 (defun mm-display-patch-inline (handle)
591 (mm-display-inline-fontify handle 'diff-mode))
593 (defun mm-display-elisp-inline (handle)
594 (mm-display-inline-fontify handle 'emacs-lisp-mode))
596 (defun mm-display-dns-inline (handle)
597 (mm-display-inline-fontify handle 'dns-mode))
599 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
600 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
601 (defvar mm-pkcs7-signed-magic
602 (funcall (if (fboundp 'unibyte-string) 'unibyte-string 'string)
603 ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
604 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
605 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
606 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02))
608 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
609 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
610 (defvar mm-pkcs7-enveloped-magic
611 (funcall (if (fboundp 'unibyte-string) 'unibyte-string 'string)
612 ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
613 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
614 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
615 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03))
617 (defun mm-view-pkcs7-get-type (handle)
618 (mm-with-unibyte-buffer
619 (mm-insert-part handle)
620 (cond ((looking-at mm-pkcs7-enveloped-magic)
621 'enveloped)
622 ((looking-at mm-pkcs7-signed-magic)
623 'signed)
625 (error "Could not identify PKCS#7 type")))))
627 (defun mm-view-pkcs7 (handle)
628 (case (mm-view-pkcs7-get-type handle)
629 (enveloped (mm-view-pkcs7-decrypt handle))
630 (signed (mm-view-pkcs7-verify handle))
631 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
633 (defun mm-view-pkcs7-verify (handle)
634 (let ((verified nil))
635 (with-temp-buffer
636 (insert "MIME-Version: 1.0\n")
637 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
638 (insert-buffer-substring (mm-handle-buffer handle))
639 (setq verified (smime-verify-region (point-min) (point-max))))
640 (goto-char (point-min))
641 (mm-insert-part handle)
642 (if (search-forward "Content-Type: " nil t)
643 (delete-region (point-min) (match-beginning 0)))
644 (goto-char (point-max))
645 (if (re-search-backward "--\r?\n?" nil t)
646 (delete-region (match-end 0) (point-max)))
647 (unless verified
648 (insert-buffer-substring smime-details-buffer)))
649 (goto-char (point-min))
650 (while (search-forward "\r\n" nil t)
651 (replace-match "\n"))
654 (defun mm-view-pkcs7-decrypt (handle)
655 (insert-buffer-substring (mm-handle-buffer handle))
656 (goto-char (point-min))
657 (insert "MIME-Version: 1.0\n")
658 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
659 (smime-decrypt-region
660 (point-min) (point-max)
661 (if (= (length smime-keys) 1)
662 (cadar smime-keys)
663 (smime-get-key-by-email
664 (completing-read
665 (concat "Decipher using key"
666 (if smime-keys (concat "(default " (caar smime-keys) "): ")
667 ": "))
668 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
669 (goto-char (point-min))
670 (while (search-forward "\r\n" nil t)
671 (replace-match "\n"))
672 (goto-char (point-min)))
674 (provide 'mm-view)
676 ;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
677 ;;; mm-view.el ends here