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