1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 2, or (at your option)
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; 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.
28 (eval-when-compile (require 'cl))
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"))
41 (defvar gnus-article-mime-handles)
42 (defvar gnus-newsgroup-charset)
44 (defvar w3m-cid-retrieve-function-alist)
45 (defvar w3m-current-buffer)
46 (defvar w3m-display-inline-images)
47 (defvar w3m-minor-mode-map)
49 (defvar mm-text-html-renderer-alist
50 '((w3 . mm-inline-text-html-render-with-w3)
51 (w3m . mm-inline-text-html-render-with-w3m)
52 (w3m-standalone . mm-inline-text-html-render-with-w3m-standalone)
53 (links mm-inline-render-with-file
54 mm-links-remove-leading-blank
56 (lynx mm-inline-render-with-stdin nil
57 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
58 (html2text mm-inline-render-with-function html2text))
59 "The attributes of renderer types for text/html.")
61 (defvar mm-text-html-washer-alist
62 '((w3 . gnus-article-wash-html-with-w3)
63 (w3m . gnus-article-wash-html-with-w3m)
64 (w3m-standalone . gnus-article-wash-html-with-w3m-standalone)
65 (links mm-inline-wash-with-file
66 mm-links-remove-leading-blank
68 (lynx mm-inline-wash-with-stdin nil
69 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
70 (html2text html2text))
71 "The attributes of washer types for text/html.")
73 (defcustom mm-fill-flowed t
74 "If non-nil a format=flowed article will be displayed flowed."
79 ;;; Internal variables.
82 ;;; Functions for displaying various formats inline
85 (defun mm-inline-image-emacs (handle)
86 (let ((b (point-marker))
88 (put-image (mm-get-image handle) b)
90 (mm-handle-set-undisplayer
96 (delete-region b (+ b 2)))))))
98 (defun mm-inline-image-xemacs (handle)
101 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
103 (mm-handle-set-undisplayer
106 (let ((b ,(point-marker))
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)))
118 (defvar mm-w3-setup nil)
119 (defun mm-setup-w3 ()
126 (setq mm-w3-setup t)))
128 (defun mm-inline-text-html-render-with-w3 (handle)
130 (let ((text (mm-get-part handle))
132 (url-standalone-mode t)
133 (url-gateway-unplugged t)
134 (w3-honor-stylesheets nil)
136 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
137 (width (window-width))
138 (charset (mail-content-type-get
139 (mm-handle-type handle) 'charset)))
141 (insert (if charset (mm-decode-string text charset) text))
143 (narrow-to-region b (point))
145 (goto-char (point-min))
146 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
148 w3-meta-content-type-charset-regexp nil t))
149 (and (boundp 'w3-meta-charset-content-type-regexp)
151 w3-meta-charset-content-type-regexp nil t)))
153 (let ((bsubstr (buffer-substring-no-properties
156 (if (fboundp 'w3-coding-system-for-mime-charset)
157 (w3-coding-system-for-mime-charset bsubstr)
158 (mm-charset-to-coding-system bsubstr))))
159 (delete-region (point-min) (point-max))
160 (insert (mm-decode-string text charset))))
161 (save-window-excursion
163 (let ((w3-strict-width width)
164 ;; Don't let w3 set the global version of
166 (fill-column fill-column))
167 (if (or debug-on-error debug-on-quit)
168 (w3-region (point-min) (point-max))
170 (w3-region (point-min) (point-max))
172 (delete-region (point-min) (point-max))
174 (charset (mail-content-type-get
175 (mm-handle-type handle) 'charset)))
176 (if (or (eq charset 'gnus-decoded)
177 (eq mail-parse-charset 'gnus-decoded))
179 (narrow-to-region (point) (point))
180 (mm-insert-part handle)
181 (goto-char (point-max)))
182 (insert (mm-decode-string (mm-get-part handle)
185 "Error while rendering html; showing as text/plain")))))))
186 (mm-handle-set-undisplayer
189 (let (buffer-read-only)
190 (if (functionp 'remove-specifier)
191 (mapcar (lambda (prop)
193 (face-property 'default prop)
195 '(background background-pixmap foreground)))
196 (delete-region ,(point-min-marker)
197 ,(point-max-marker)))))))))
199 (defvar mm-w3m-setup nil
200 "Whether gnus-article-mode has been setup to use emacs-w3m.")
202 (defun mm-setup-w3m ()
203 "Setup gnus-article-mode to use emacs-w3m."
206 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
207 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
208 w3m-cid-retrieve-function-alist))
209 (setq mm-w3m-setup t))
210 (setq w3m-display-inline-images mm-inline-text-html-with-images))
212 (defun mm-w3m-cid-retrieve-1 (url handle)
213 (dolist (elem handle)
215 (if (equal url (mm-handle-id elem))
217 (mm-insert-part elem)
218 (throw 'found-handle (mm-handle-media-type elem))))
219 (if (equal "multipart" (mm-handle-media-supertype elem))
220 (mm-w3m-cid-retrieve-1 url elem)))))
222 (defun mm-w3m-cid-retrieve (url &rest args)
223 "Insert a content pointed by URL if it has the cid: scheme."
224 (when (string-match "\\`cid:" url)
226 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
227 (with-current-buffer w3m-current-buffer
228 gnus-article-mime-handles)))))
230 (defun mm-inline-text-html-render-with-w3m (handle)
231 "Render a text/html part using emacs-w3m."
233 (let ((text (mm-get-part handle))
235 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
237 (insert (if charset (mm-decode-string text charset) text))
239 (narrow-to-region b (point))
241 (goto-char (point-min))
242 (when (setq charset (w3m-detect-meta-charset))
243 (delete-region (point-min) (point-max))
244 (insert (mm-decode-string text charset))))
245 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
247 (w3m-region (point-min) (point-max) nil charset))
248 (when (and mm-inline-text-html-with-w3m-keymap
249 (boundp 'w3m-minor-mode-map)
252 (point-min) (point-max)
253 (list 'keymap w3m-minor-mode-map
254 ;; Put the mark meaning this part was rendered by emacs-w3m.
255 'mm-inline-text-html-with-w3m t)))
256 (mm-handle-set-undisplayer
259 (let (buffer-read-only)
260 (if (functionp 'remove-specifier)
261 (mapcar (lambda (prop)
263 (face-property 'default prop)
265 '(background background-pixmap foreground)))
266 (delete-region ,(point-min-marker)
267 ,(point-max-marker)))))))))
269 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
270 "*T means the w3m command supports the m17n feature.")
272 (defun mm-w3m-standalone-supports-m17n-p ()
273 "Say whether the w3m command supports the m17n feature."
274 (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
275 ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
276 ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
278 (let ((coding-system-for-write 'iso-2022-jp)
279 (coding-system-for-read 'iso-2022-jp)
280 (str (mm-decode-coding-string "\
281 \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)))
282 (mm-with-multibyte-buffer
285 (point-min) (point-max) "w3m" t t nil "-dump"
286 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
287 (goto-char (point-min))
288 (search-forward str nil t)))
290 (setq mm-w3m-standalone-supports-m17n-p t))
292 ;;(message "You had better upgrade your w3m command")
293 (setq mm-w3m-standalone-supports-m17n-p nil))))
295 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
296 "Render a text/html part using w3m."
297 (if (mm-w3m-standalone-supports-m17n-p)
298 (let ((source (mm-get-part handle))
299 (charset (mail-content-type-get (mm-handle-type handle) 'charset))
302 (setq cs (mm-charset-to-coding-system charset))
303 (not (eq cs 'ascii)))
305 (setq charset "iso-8859-1"
309 (mm-with-unibyte-buffer
311 (mm-enable-multibyte)
312 (let ((coding-system-for-write 'binary)
313 (coding-system-for-read cs))
315 (point-min) (point-max)
316 "w3m" t t nil "-dump" "-T" "text/html"
317 "-I" charset "-O" charset))
319 (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
321 (defun mm-links-remove-leading-blank ()
322 ;; Delete the annoying three spaces preceding each line of links
324 (goto-char (point-min))
325 (while (re-search-forward "^ " nil t)
326 (delete-region (match-beginning 0) (match-end 0))))
328 (defun mm-inline-wash-with-file (post-func cmd &rest args)
329 (let ((file (mm-make-temp-file
330 (expand-file-name "mm" mm-tmp-directory))))
331 (let ((coding-system-for-write 'binary))
332 (write-region (point-min) (point-max) file nil 'silent))
333 (delete-region (point-min) (point-max))
335 (apply 'call-process cmd nil t nil (mapcar 'eval args))
337 (and post-func (funcall post-func))))
339 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
340 (let ((coding-system-for-write 'binary))
341 (apply 'call-process-region (point-min) (point-max)
343 (and post-func (funcall post-func)))
345 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
346 (let ((source (mm-get-part handle)))
349 (mm-with-unibyte-buffer
351 (apply 'mm-inline-wash-with-file post-func cmd args)
354 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
355 (let ((source (mm-get-part handle)))
358 (mm-with-unibyte-buffer
360 (apply 'mm-inline-wash-with-stdin post-func cmd args)
363 (defun mm-inline-render-with-function (handle func &rest args)
364 (let ((source (mm-get-part handle))
365 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
368 (mm-with-multibyte-buffer
370 (mm-decode-string source charset)
375 (defun mm-inline-text-html (handle)
376 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
377 (entry (assq func mm-text-html-renderer-alist))
380 (setq func (cdr entry)))
383 (funcall func handle))
385 (apply (car func) handle (cdr func))))))
387 (defun mm-inline-text-vcard (handle)
388 (let (buffer-read-only)
393 (if (fboundp 'vcard-pretty-print)
394 (vcard-pretty-print (mm-get-part handle))
396 (vcard-parse-string (mm-get-part handle)
397 'vcard-standard-filter))))))))
399 (defun mm-inline-text (handle)
401 (type (mm-handle-media-subtype handle))
402 (charset (mail-content-type-get
403 (mm-handle-type handle) 'charset))
405 (if (or (eq charset 'gnus-decoded)
406 ;; This is probably not entirely correct, but
407 ;; makes rfc822 parts with embedded multiparts work.
408 (eq mail-parse-charset 'gnus-decoded))
410 (narrow-to-region (point) (point))
411 (mm-insert-part handle)
412 (goto-char (point-max)))
413 (insert (mm-decode-string (mm-get-part handle) charset)))
414 (when (and mm-fill-flowed
416 (equal (cdr (assoc 'format (mm-handle-type handle)))
419 (narrow-to-region b (point))
422 (goto-char (point-max))))
424 (narrow-to-region b (point))
425 (when (or (equal type "enriched")
426 (equal type "richtext"))
427 (set-text-properties (point-min) (point-max) nil)
429 (enriched-decode (point-min) (point-max))))
430 (mm-handle-set-undisplayer
433 (let (buffer-read-only)
434 (delete-region ,(point-min-marker)
435 ,(point-max-marker))))))))
437 (defun mm-insert-inline (handle text)
438 "Insert TEXT inline from HANDLE."
443 (mm-handle-set-undisplayer
446 (let (buffer-read-only)
447 (delete-region ,(set-marker (make-marker) b)
448 ,(set-marker (make-marker) (point))))))))
450 (defun mm-inline-audio (handle)
451 (message "Not implemented"))
453 (defun mm-view-sound-file ()
454 (message "Not implemented"))
456 (defun mm-w3-prepare-buffer ()
458 (let ((url-standalone-mode t)
459 (url-gateway-unplugged t)
460 (w3-honor-stylesheets nil))
461 (w3-prepare-buffer)))
463 (defun mm-view-message ()
464 (mm-enable-multibyte)
466 (let (gnus-article-mime-handles)
467 ;; Double decode problem may happen. See mm-inline-message.
468 (run-hooks 'gnus-article-decode-hook)
469 (gnus-article-prepare-display)
470 (setq handles gnus-article-mime-handles))
472 (setq gnus-article-mime-handles
473 (mm-merge-handles gnus-article-mime-handles handles))))
475 (goto-char (point-min)))
477 (defun mm-inline-message (handle)
480 (charset (mail-content-type-get
481 (mm-handle-type handle) 'charset))
482 gnus-displaying-mime handles)
485 (setq charset (intern (downcase charset)))
486 (when (eq charset 'us-ascii)
490 (narrow-to-region b b)
491 (mm-insert-part handle)
492 (let (gnus-article-mime-handles
493 ;; disable prepare hook
494 gnus-article-prepare-hook
495 (gnus-newsgroup-charset
496 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
497 (or charset gnus-newsgroup-charset))))
498 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
499 (run-hooks 'gnus-article-decode-hook))
500 (gnus-article-prepare-display)
501 (setq handles gnus-article-mime-handles))
502 (goto-char (point-min))
505 (goto-char (point-max))
508 (insert "----------\n\n")
510 (setq gnus-article-mime-handles
511 (mm-merge-handles gnus-article-mime-handles handles)))
512 (mm-handle-set-undisplayer
515 (let (buffer-read-only)
516 (if (fboundp 'remove-specifier)
517 ;; This is only valid on XEmacs.
518 (mapcar (lambda (prop)
520 (face-property 'default prop) (current-buffer)))
521 '(background background-pixmap foreground)))
522 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
524 (defun mm-display-inline-fontify (handle mode)
525 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
527 (unless (eq charset 'gnus-decoded)
528 (mm-with-unibyte-buffer
529 (mm-insert-part handle)
530 (mm-decompress-buffer
531 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
532 (mail-content-type-get (mm-handle-disposition handle) 'filename))
535 (setq coding-system (mm-find-buffer-file-coding-system)))
536 (setq text (buffer-string))))
537 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
538 ;; on for buffers whose name begins with " ". That's why we use
539 ;; `with-current-buffer'/`generate-new-buffer' rather than
540 ;; `with-temp-buffer'.
541 (with-current-buffer (generate-new-buffer "*fontification*")
542 (buffer-disable-undo)
543 (mm-enable-multibyte)
544 (insert (cond ((eq charset 'gnus-decoded)
545 (with-current-buffer (mm-handle-buffer handle)
548 (mm-decode-coding-string text coding-system))
550 (mm-decode-string text charset))
554 (let ((font-lock-maximum-size nil)
555 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
556 (font-lock-mode-hook nil)
557 (font-lock-support-mode nil)
558 ;; I find font-lock a bit too verbose.
559 (font-lock-verbose nil))
561 ;; The mode function might have already turned on font-lock.
562 (unless (symbol-value 'font-lock-mode)
563 (font-lock-fontify-buffer)))
564 ;; By default, XEmacs font-lock uses non-duplicable text
565 ;; properties. This code forces all the text properties
566 ;; to be copied along with the text.
567 (when (fboundp 'extent-list)
568 (map-extents (lambda (ext ignored)
569 (set-extent-property ext 'duplicable t)
571 nil nil nil nil nil 'text-prop))
572 (setq text (buffer-string))
573 (kill-buffer (current-buffer)))
574 (mm-insert-inline handle text)))
576 ;; Shouldn't these functions check whether the user even wants to use
577 ;; font-lock? At least under XEmacs, this fontification is pretty
578 ;; much unconditional. Also, it would be nice to change for the size
579 ;; of the fontified region.
581 (defun mm-display-patch-inline (handle)
582 (mm-display-inline-fontify handle 'diff-mode))
584 (defun mm-display-elisp-inline (handle)
585 (mm-display-inline-fontify handle 'emacs-lisp-mode))
587 (defun mm-display-dns-inline (handle)
588 (mm-display-inline-fontify handle 'dns-mode))
590 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
591 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
592 (defvar mm-pkcs7-signed-magic
593 (mm-string-as-unibyte
594 (mapconcat '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 ?\x02) "")))
600 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
601 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
602 (defvar mm-pkcs7-enveloped-magic
603 (mm-string-as-unibyte
604 (mapconcat 'char-to-string
605 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
606 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
607 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
608 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03) "")))
610 (defun mm-view-pkcs7-get-type (handle)
611 (mm-with-unibyte-buffer
612 (mm-insert-part handle)
613 (cond ((looking-at mm-pkcs7-enveloped-magic)
615 ((looking-at mm-pkcs7-signed-magic)
618 (error "Could not identify PKCS#7 type")))))
620 (defun mm-view-pkcs7 (handle)
621 (case (mm-view-pkcs7-get-type handle)
622 (enveloped (mm-view-pkcs7-decrypt handle))
623 (signed (mm-view-pkcs7-verify handle))
624 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
626 (defun mm-view-pkcs7-verify (handle)
627 (let ((verified nil))
629 (insert "MIME-Version: 1.0\n")
630 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
631 (insert-buffer-substring (mm-handle-buffer handle))
632 (setq verified (smime-verify-region (point-min) (point-max))))
633 (goto-char (point-min))
634 (mm-insert-part handle)
635 (if (search-forward "Content-Type: " nil t)
636 (delete-region (point-min) (match-beginning 0)))
637 (goto-char (point-max))
638 (if (re-search-backward "--\r?\n?" nil t)
639 (delete-region (match-end 0) (point-max)))
641 (insert-buffer-substring smime-details-buffer)))
642 (goto-char (point-min))
643 (while (search-forward "\r\n" nil t)
644 (replace-match "\n"))
647 (defun mm-view-pkcs7-decrypt (handle)
648 (insert-buffer-substring (mm-handle-buffer handle))
649 (goto-char (point-min))
650 (insert "MIME-Version: 1.0\n")
651 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
652 (smime-decrypt-region
653 (point-min) (point-max)
654 (if (= (length smime-keys) 1)
656 (smime-get-key-by-email
658 (concat "Decipher using key"
659 (if smime-keys (concat "(default " (caar smime-keys) "): ")
661 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
662 (goto-char (point-min))
663 (while (search-forward "\r\n" nil t)
664 (replace-match "\n"))
665 (goto-char (point-min)))
669 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
670 ;;; mm-view.el ends here