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/>.
26 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
27 (eval-when-compile (require 'cl
))
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
)
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
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
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."
78 ;;; Internal variables.
81 ;;; Functions for displaying various formats inline
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
)
89 (mm-handle-set-undisplayer
93 (inhibit-read-only t
))
95 (delete-region b
(+ b
2)))))))
97 (defun mm-inline-image-xemacs (handle)
98 (when (featurep 'xemacs
)
101 (let ((annot (make-annotation (mm-get-image handle
) nil
'text
))
102 (inhibit-read-only t
))
103 (mm-handle-set-undisplayer
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
))))
114 (if (featurep 'xemacs
)
115 (defalias 'mm-inline-image
'mm-inline-image-xemacs
)
116 (defalias 'mm-inline-image
'mm-inline-image-emacs
)))
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 ()
131 (setq mm-w3-setup t
)))
133 (defun mm-inline-text-html-render-with-w3 (handle)
135 (let ((text (mm-get-part handle
))
137 (url-standalone-mode t
)
138 (url-gateway-unplugged t
)
139 (w3-honor-stylesheets nil
)
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
)))
146 (insert (if charset
(mm-decode-string text charset
) text
))
148 (narrow-to-region b
(point))
150 (goto-char (point-min))
151 (when (or (and (boundp 'w3-meta-content-type-charset-regexp
)
153 w3-meta-content-type-charset-regexp nil t
))
154 (and (boundp 'w3-meta-charset-content-type-regexp
)
156 w3-meta-charset-content-type-regexp nil t
)))
158 (let ((bsubstr (buffer-substring-no-properties
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
168 (let ((w3-strict-width width
)
169 ;; Don't let w3 set the global version of
171 (fill-column fill-column
))
172 (if (or debug-on-error debug-on-quit
)
173 (w3-region (point-min) (point-max))
175 (w3-region (point-min) (point-max))
177 (delete-region (point-min) (point-max))
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
))
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
)
190 "Error while rendering html; showing as text/plain")))))))
191 (mm-handle-set-undisplayer
194 (let ((inhibit-read-only t
))
195 ,@(if (functionp 'remove-specifier
)
196 '((dolist (prop '(background background-pixmap foreground
))
198 (face-property 'default prop
)
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.")
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."
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
)
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
)))
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."
245 (let ((text (mm-get-part handle
))
247 (charset (or (mail-content-type-get (mm-handle-type handle
) 'charset
)
248 mail-parse-charset
)))
250 (insert (if charset
(mm-decode-string text charset
) text
))
252 (narrow-to-region b
(point))
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
)
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
)
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
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
))
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
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
)))
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
)
308 (symbol-name mail-parse-charset
)))
311 (setq cs
(mm-charset-to-coding-system charset
))
312 (not (eq cs
'ascii
)))
314 (setq charset
"iso-8859-1"
318 (mm-with-unibyte-buffer
320 (mm-enable-multibyte)
321 (let ((coding-system-for-write 'binary
)
322 (coding-system-for-read cs
))
324 (point-min) (point-max)
325 "w3m" t t nil
"-dump" "-T" "text/html"
326 "-I" charset
"-O" charset
))
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
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))
344 (apply 'call-process cmd nil t nil
(mapcar 'eval args
))
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)
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
)))
358 (mm-with-unibyte-buffer
360 (apply 'mm-inline-wash-with-file post-func cmd args
)
363 (defun mm-inline-render-with-stdin (handle post-func cmd
&rest args
)
364 (let ((source (mm-get-part handle
)))
367 (mm-with-unibyte-buffer
369 (apply 'mm-inline-wash-with-stdin post-func cmd args
)
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
)))
378 (mm-with-multibyte-buffer
380 (mm-decode-string source charset
)
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
))
390 (setq func
(cdr entry
)))
393 (funcall func handle
))
395 (apply (car func
) handle
(cdr func
))))))
397 (defun mm-inline-text-vcard (handle)
398 (let ((inhibit-read-only t
))
403 (if (fboundp 'vcard-pretty-print
)
404 (vcard-pretty-print (mm-get-part handle
))
406 (vcard-parse-string (mm-get-part handle
)
407 'vcard-standard-filter
))))))))
409 (defun mm-inline-text (handle)
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
))
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
426 (equal (cdr (assoc 'format
(mm-handle-type handle
)))
429 (narrow-to-region b
(point))
431 (fill-flowed nil
(equal (cdr (assoc 'delsp
(mm-handle-type handle
)))
433 (goto-char (point-max))))
435 (narrow-to-region b
(point))
436 (when (member type
'("enriched" "richtext"))
437 (set-text-properties (point-min) (point-max) nil
)
439 (enriched-decode (point-min) (point-max))))
440 (mm-handle-set-undisplayer
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."
453 (mm-handle-set-undisplayer
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 ()
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)
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
))
482 (setq gnus-article-mime-handles
483 (mm-merge-handles gnus-article-mime-handles handles
))))
485 (goto-char (point-min)))
487 (defun mm-inline-message (handle)
490 (charset (mail-content-type-get
491 (mm-handle-type handle
) 'charset
))
492 gnus-displaying-mime handles
)
495 (setq charset
(intern (downcase charset
)))
496 (when (eq charset
'us-ascii
)
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))
515 (goto-char (point-max))
518 (insert "----------\n\n")
520 (setq gnus-article-mime-handles
521 (mm-merge-handles gnus-article-mime-handles handles
)))
522 (mm-handle-set-undisplayer
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
))
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
))
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
))
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
)
557 (mm-decode-coding-string text coding-system
))
559 (mm-decode-string text charset
))
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
))
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
)
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 "\x30\x5c\x28\x80\x5c\x7c\x81\x2e\x5c\x7c\x82\x2e\x2e\x5c\x7c\x83\x2e\x2e\
603 \x2e\x5c\x29\x06\x09\x5c\x2a\x86\x48\x86\xf7\x0d\x01\x07\x02")
605 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
606 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
607 (defvar mm-pkcs7-enveloped-magic
608 "\x30\x5c\x28\x80\x5c\x7c\x81\x2e\x5c\x7c\x82\x2e\x2e\x5c\x7c\x83\x2e\x2e\
609 \x2e\x5c\x29\x06\x09\x5c\x2a\x86\x48\x86\xf7\x0d\x01\x07\x03")
611 (defun mm-view-pkcs7-get-type (handle)
612 (mm-with-unibyte-buffer
613 (mm-insert-part handle
)
614 (cond ((looking-at mm-pkcs7-enveloped-magic
)
616 ((looking-at mm-pkcs7-signed-magic
)
619 (error "Could not identify PKCS#7 type")))))
621 (defun mm-view-pkcs7 (handle)
622 (case (mm-view-pkcs7-get-type handle
)
623 (enveloped (mm-view-pkcs7-decrypt handle
))
624 (signed (mm-view-pkcs7-verify handle
))
625 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
627 (defun mm-view-pkcs7-verify (handle)
628 (let ((verified nil
))
630 (insert "MIME-Version: 1.0\n")
631 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
632 (insert-buffer-substring (mm-handle-buffer handle
))
633 (setq verified
(smime-verify-region (point-min) (point-max))))
634 (goto-char (point-min))
635 (mm-insert-part handle
)
636 (if (search-forward "Content-Type: " nil t
)
637 (delete-region (point-min) (match-beginning 0)))
638 (goto-char (point-max))
639 (if (re-search-backward "--\r?\n?" nil t
)
640 (delete-region (match-end 0) (point-max)))
642 (insert-buffer-substring smime-details-buffer
)))
643 (goto-char (point-min))
644 (while (search-forward "\r\n" nil t
)
645 (replace-match "\n"))
648 (defun mm-view-pkcs7-decrypt (handle)
649 (insert-buffer-substring (mm-handle-buffer handle
))
650 (goto-char (point-min))
651 (insert "MIME-Version: 1.0\n")
652 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
653 (smime-decrypt-region
654 (point-min) (point-max)
655 (if (= (length smime-keys
) 1)
657 (smime-get-key-by-email
659 (concat "Decipher using key"
660 (if smime-keys
(concat "(default " (caar smime-keys
) "): ")
662 smime-keys nil nil nil nil
(car-safe (car-safe smime-keys
))))))
663 (goto-char (point-min))
664 (while (search-forward "\r\n" nil t
)
665 (replace-match "\n"))
666 (goto-char (point-min)))
670 ;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
671 ;;; mm-view.el ends here