1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 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, 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 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
29 (eval-when-compile (require 'cl))
37 (autoload 'gnus-article-prepare-display "gnus-art")
38 (autoload 'vcard-parse-string "vcard")
39 (autoload 'vcard-format-string "vcard")
40 (autoload 'fill-flowed "flow-fill")
41 (autoload 'html2text "html2text" nil t))
43 (defvar gnus-article-mime-handles)
44 (defvar gnus-newsgroup-charset)
46 (defvar w3m-cid-retrieve-function-alist)
47 (defvar w3m-current-buffer)
48 (defvar w3m-display-inline-images)
49 (defvar w3m-minor-mode-map)
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
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.")
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
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.")
75 (defcustom mm-fill-flowed t
76 "If non-nil a format=flowed article will be displayed flowed."
81 ;;; Internal variables.
84 ;;; Functions for displaying various formats inline
87 (defun mm-inline-image-emacs (handle)
88 (let ((b (point-marker))
89 (inhibit-read-only t))
90 (put-image (mm-get-image handle) b)
92 (mm-handle-set-undisplayer
96 (inhibit-read-only t))
98 (delete-region b (+ b 2)))))))
100 (defun mm-inline-image-xemacs (handle)
101 (when (featurep 'xemacs)
104 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
105 (inhibit-read-only t))
106 (mm-handle-set-undisplayer
109 (let ((b ,(point-marker))
110 (inhibit-read-only t))
111 (delete-annotation ,annot)
112 (delete-region (- b 2) b))))
113 (set-extent-property annot 'mm t)
114 (set-extent-property annot 'duplicable t))))
117 (if (featurep 'xemacs)
118 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
119 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
122 (declare-function w3-do-setup "ext:w3" ())
123 (declare-function w3-region "ext:w3-display" (st nd))
124 (declare-function w3-prepare-buffer "ext:w3-display" (&rest args))
126 (defvar mm-w3-setup nil)
127 (defun mm-setup-w3 ()
134 (setq mm-w3-setup t)))
136 (defun mm-inline-text-html-render-with-w3 (handle)
138 (let ((text (mm-get-part handle))
140 (url-standalone-mode t)
141 (url-gateway-unplugged t)
142 (w3-honor-stylesheets nil)
144 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
145 (width (window-width))
146 (charset (mail-content-type-get
147 (mm-handle-type handle) 'charset)))
149 (insert (if charset (mm-decode-string text charset) text))
151 (narrow-to-region b (point))
153 (goto-char (point-min))
154 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
156 w3-meta-content-type-charset-regexp nil t))
157 (and (boundp 'w3-meta-charset-content-type-regexp)
159 w3-meta-charset-content-type-regexp nil t)))
161 (let ((bsubstr (buffer-substring-no-properties
164 (if (fboundp 'w3-coding-system-for-mime-charset)
165 (w3-coding-system-for-mime-charset bsubstr)
166 (mm-charset-to-coding-system bsubstr))))
167 (delete-region (point-min) (point-max))
168 (insert (mm-decode-string text charset))))
169 (save-window-excursion
171 (let ((w3-strict-width width)
172 ;; Don't let w3 set the global version of
174 (fill-column fill-column))
175 (if (or debug-on-error debug-on-quit)
176 (w3-region (point-min) (point-max))
178 (w3-region (point-min) (point-max))
180 (delete-region (point-min) (point-max))
182 (charset (mail-content-type-get
183 (mm-handle-type handle) 'charset)))
184 (if (or (eq charset 'gnus-decoded)
185 (eq mail-parse-charset 'gnus-decoded))
187 (narrow-to-region (point) (point))
188 (mm-insert-part handle)
189 (goto-char (point-max)))
190 (insert (mm-decode-string (mm-get-part handle)
193 "Error while rendering html; showing as text/plain")))))))
194 (mm-handle-set-undisplayer
197 (let ((inhibit-read-only t))
198 ,@(if (functionp 'remove-specifier)
199 '((dolist (prop '(background background-pixmap foreground))
201 (face-property 'default prop)
203 (delete-region ,(point-min-marker)
204 ,(point-max-marker)))))))))
206 (defvar mm-w3m-setup nil
207 "Whether gnus-article-mode has been setup to use emacs-w3m.")
210 (declare-function w3m-detect-meta-charset "ext:w3m" ())
211 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
213 (defun mm-setup-w3m ()
214 "Setup gnus-article-mode to use emacs-w3m."
217 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
218 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
219 w3m-cid-retrieve-function-alist))
220 (setq mm-w3m-setup t))
221 (setq w3m-display-inline-images mm-inline-text-html-with-images))
223 (defun mm-w3m-cid-retrieve-1 (url handle)
224 (dolist (elem handle)
226 (when (equal url (mm-handle-id elem))
227 (mm-insert-part elem)
228 (throw 'found-handle (mm-handle-media-type elem)))
229 (when (and (stringp (car elem))
230 (equal "multipart" (mm-handle-media-supertype elem)))
231 (mm-w3m-cid-retrieve-1 url elem)))))
233 (defun mm-w3m-cid-retrieve (url &rest args)
234 "Insert a content pointed by URL if it has the cid: scheme."
235 (when (string-match "\\`cid:" url)
236 (or (catch 'found-handle
237 (mm-w3m-cid-retrieve-1
238 (setq url (concat "<" (substring url (match-end 0)) ">"))
239 (with-current-buffer w3m-current-buffer
240 gnus-article-mime-handles)))
243 (message "Failed to find \"Content-ID: %s\"" url)))))
245 (defun mm-inline-text-html-render-with-w3m (handle)
246 "Render a text/html part using emacs-w3m."
248 (let ((text (mm-get-part handle))
250 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
251 mail-parse-charset)))
253 (insert (if charset (mm-decode-string text charset) text))
255 (narrow-to-region b (point))
257 (goto-char (point-min))
258 (when (setq charset (w3m-detect-meta-charset))
259 (delete-region (point-min) (point-max))
260 (insert (mm-decode-string text charset))))
261 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
263 (w3m-region (point-min) (point-max) nil charset))
264 (when (and mm-inline-text-html-with-w3m-keymap
265 (boundp 'w3m-minor-mode-map)
268 (point-min) (point-max)
269 (list 'keymap w3m-minor-mode-map
270 ;; Put the mark meaning this part was rendered by emacs-w3m.
271 'mm-inline-text-html-with-w3m t)))
272 (mm-handle-set-undisplayer
275 (let ((inhibit-read-only t))
276 (delete-region ,(point-min-marker)
277 ,(point-max-marker)))))))))
279 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
280 "*T means the w3m command supports the m17n feature.")
282 (defun mm-w3m-standalone-supports-m17n-p ()
283 "Say whether the w3m command supports the m17n feature."
284 (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
285 ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
286 ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
288 (let ((coding-system-for-write 'iso-2022-jp)
289 (coding-system-for-read 'iso-2022-jp)
290 (str (mm-decode-coding-string "\
291 \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)))
292 (mm-with-multibyte-buffer
295 (point-min) (point-max) "w3m" t t nil "-dump"
296 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
297 (goto-char (point-min))
298 (search-forward str nil t)))
300 (setq mm-w3m-standalone-supports-m17n-p t))
302 ;;(message "You had better upgrade your w3m command")
303 (setq mm-w3m-standalone-supports-m17n-p nil))))
305 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
306 "Render a text/html part using w3m."
307 (if (mm-w3m-standalone-supports-m17n-p)
308 (let ((source (mm-get-part handle))
309 (charset (or (mail-content-type-get (mm-handle-type handle)
311 (symbol-name mail-parse-charset)))
314 (setq cs (mm-charset-to-coding-system charset))
315 (not (eq cs 'ascii)))
317 (setq charset "iso-8859-1"
321 (mm-with-unibyte-buffer
323 (mm-enable-multibyte)
324 (let ((coding-system-for-write 'binary)
325 (coding-system-for-read cs))
327 (point-min) (point-max)
328 "w3m" t t nil "-dump" "-T" "text/html"
329 "-I" charset "-O" charset))
331 (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
333 (defun mm-links-remove-leading-blank ()
334 ;; Delete the annoying three spaces preceding each line of links
336 (goto-char (point-min))
337 (while (re-search-forward "^ " nil t)
338 (delete-region (match-beginning 0) (match-end 0))))
340 (defun mm-inline-wash-with-file (post-func cmd &rest args)
341 (let ((file (mm-make-temp-file
342 (expand-file-name "mm" mm-tmp-directory))))
343 (let ((coding-system-for-write 'binary))
344 (write-region (point-min) (point-max) file nil 'silent))
345 (delete-region (point-min) (point-max))
347 (apply 'call-process cmd nil t nil (mapcar 'eval args))
349 (and post-func (funcall post-func))))
351 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
352 (let ((coding-system-for-write 'binary))
353 (apply 'call-process-region (point-min) (point-max)
355 (and post-func (funcall post-func)))
357 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
358 (let ((source (mm-get-part handle)))
361 (mm-with-unibyte-buffer
363 (apply 'mm-inline-wash-with-file post-func cmd args)
366 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
367 (let ((source (mm-get-part handle)))
370 (mm-with-unibyte-buffer
372 (apply 'mm-inline-wash-with-stdin post-func cmd args)
375 (defun mm-inline-render-with-function (handle func &rest args)
376 (let ((source (mm-get-part handle))
377 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
378 mail-parse-charset)))
381 (mm-with-multibyte-buffer
383 (mm-decode-string source charset)
388 (defun mm-inline-text-html (handle)
389 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
390 (entry (assq func mm-text-html-renderer-alist))
391 (inhibit-read-only t))
393 (setq func (cdr entry)))
396 (funcall func handle))
398 (apply (car func) handle (cdr func))))))
400 (defun mm-inline-text-vcard (handle)
401 (let ((inhibit-read-only t))
406 (if (fboundp 'vcard-pretty-print)
407 (vcard-pretty-print (mm-get-part handle))
409 (vcard-parse-string (mm-get-part handle)
410 'vcard-standard-filter))))))))
412 (defun mm-inline-text (handle)
414 (type (mm-handle-media-subtype handle))
415 (charset (mail-content-type-get
416 (mm-handle-type handle) 'charset))
417 (inhibit-read-only t))
418 (if (or (eq charset 'gnus-decoded)
419 ;; This is probably not entirely correct, but
420 ;; makes rfc822 parts with embedded multiparts work.
421 (eq mail-parse-charset 'gnus-decoded))
423 (narrow-to-region (point) (point))
424 (mm-insert-part handle)
425 (goto-char (point-max)))
426 (insert (mm-decode-string (mm-get-part handle) charset)))
427 (when (and mm-fill-flowed
429 (equal (cdr (assoc 'format (mm-handle-type handle)))
432 (narrow-to-region b (point))
434 (fill-flowed nil (equal (cdr (assoc 'delsp (mm-handle-type handle)))
436 (goto-char (point-max))))
438 (narrow-to-region b (point))
439 (when (member type '("enriched" "richtext"))
440 (set-text-properties (point-min) (point-max) nil)
442 (enriched-decode (point-min) (point-max))))
443 (mm-handle-set-undisplayer
446 (let ((inhibit-read-only t))
447 (delete-region ,(point-min-marker)
448 ,(point-max-marker))))))))
450 (defun mm-insert-inline (handle text)
451 "Insert TEXT inline from HANDLE."
456 (mm-handle-set-undisplayer
459 (let ((inhibit-read-only t))
460 (delete-region ,(copy-marker b)
461 ,(copy-marker (point))))))))
463 (defun mm-inline-audio (handle)
464 (message "Not implemented"))
466 (defun mm-view-sound-file ()
467 (message "Not implemented"))
469 (defun mm-w3-prepare-buffer ()
471 (let ((url-standalone-mode t)
472 (url-gateway-unplugged t)
473 (w3-honor-stylesheets nil))
474 (w3-prepare-buffer)))
476 (defun mm-view-message ()
477 (mm-enable-multibyte)
479 (let (gnus-article-mime-handles)
480 ;; Double decode problem may happen. See mm-inline-message.
481 (run-hooks 'gnus-article-decode-hook)
482 (gnus-article-prepare-display)
483 (setq handles gnus-article-mime-handles))
485 (setq gnus-article-mime-handles
486 (mm-merge-handles gnus-article-mime-handles handles))))
488 (goto-char (point-min)))
490 (defun mm-inline-message (handle)
493 (charset (mail-content-type-get
494 (mm-handle-type handle) 'charset))
495 gnus-displaying-mime handles)
498 (setq charset (intern (downcase charset)))
499 (when (eq charset 'us-ascii)
503 (narrow-to-region b b)
504 (mm-insert-part handle)
505 (let (gnus-article-mime-handles
506 ;; disable prepare hook
507 gnus-article-prepare-hook
508 (gnus-newsgroup-charset
509 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
510 (or charset gnus-newsgroup-charset))))
511 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
512 (run-hooks 'gnus-article-decode-hook))
513 (gnus-article-prepare-display)
514 (setq handles gnus-article-mime-handles))
515 (goto-char (point-min))
518 (goto-char (point-max))
521 (insert "----------\n\n")
523 (setq gnus-article-mime-handles
524 (mm-merge-handles gnus-article-mime-handles handles)))
525 (mm-handle-set-undisplayer
528 (let ((inhibit-read-only t))
529 (if (fboundp 'remove-specifier)
530 ;; This is only valid on XEmacs.
531 (dolist (prop '(background background-pixmap foreground))
533 (face-property 'default prop) (current-buffer))))
534 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
536 (defun mm-display-inline-fontify (handle mode)
537 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
539 (unless (eq charset 'gnus-decoded)
540 (mm-with-unibyte-buffer
541 (mm-insert-part handle)
542 (mm-decompress-buffer
543 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
544 (mail-content-type-get (mm-handle-disposition handle) 'filename))
547 (setq coding-system (mm-find-buffer-file-coding-system)))
548 (setq text (buffer-string))))
549 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
550 ;; on for buffers whose name begins with " ". That's why we use
551 ;; `with-current-buffer'/`generate-new-buffer' rather than
552 ;; `with-temp-buffer'.
553 (with-current-buffer (generate-new-buffer "*fontification*")
554 (buffer-disable-undo)
555 (mm-enable-multibyte)
556 (insert (cond ((eq charset 'gnus-decoded)
557 (with-current-buffer (mm-handle-buffer handle)
560 (mm-decode-coding-string text coding-system))
562 (mm-decode-string text charset))
566 (let ((font-lock-maximum-size nil)
567 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
568 (font-lock-mode-hook nil)
569 (font-lock-support-mode nil)
570 ;; I find font-lock a bit too verbose.
571 (font-lock-verbose nil))
573 ;; The mode function might have already turned on font-lock.
574 (unless (symbol-value 'font-lock-mode)
575 (font-lock-fontify-buffer)))
576 ;; By default, XEmacs font-lock uses non-duplicable text
577 ;; properties. This code forces all the text properties
578 ;; to be copied along with the text.
579 (when (featurep 'xemacs)
580 (map-extents (lambda (ext ignored)
581 (set-extent-property ext 'duplicable t)
583 nil nil nil nil nil 'text-prop))
584 (setq text (buffer-string))
585 (kill-buffer (current-buffer)))
586 (mm-insert-inline handle text)))
588 ;; Shouldn't these functions check whether the user even wants to use
589 ;; font-lock? At least under XEmacs, this fontification is pretty
590 ;; much unconditional. Also, it would be nice to change for the size
591 ;; of the fontified region.
593 (defun mm-display-patch-inline (handle)
594 (mm-display-inline-fontify handle 'diff-mode))
596 (defun mm-display-elisp-inline (handle)
597 (mm-display-inline-fontify handle 'emacs-lisp-mode))
599 (defun mm-display-dns-inline (handle)
600 (mm-display-inline-fontify handle 'dns-mode))
602 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
603 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
604 (defvar mm-pkcs7-signed-magic
605 (funcall (if (fboundp 'unibyte-string) 'unibyte-string 'string)
606 ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
607 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
608 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
609 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02))
611 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
612 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
613 (defvar mm-pkcs7-enveloped-magic
614 (funcall (if (fboundp 'unibyte-string) 'unibyte-string 'string)
615 ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
616 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
617 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
618 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03))
620 (defun mm-view-pkcs7-get-type (handle)
621 (mm-with-unibyte-buffer
622 (mm-insert-part handle)
623 (cond ((looking-at mm-pkcs7-enveloped-magic)
625 ((looking-at mm-pkcs7-signed-magic)
628 (error "Could not identify PKCS#7 type")))))
630 (defun mm-view-pkcs7 (handle)
631 (case (mm-view-pkcs7-get-type handle)
632 (enveloped (mm-view-pkcs7-decrypt handle))
633 (signed (mm-view-pkcs7-verify handle))
634 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
636 (defun mm-view-pkcs7-verify (handle)
637 (let ((verified nil))
639 (insert "MIME-Version: 1.0\n")
640 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
641 (insert-buffer-substring (mm-handle-buffer handle))
642 (setq verified (smime-verify-region (point-min) (point-max))))
643 (goto-char (point-min))
644 (mm-insert-part handle)
645 (if (search-forward "Content-Type: " nil t)
646 (delete-region (point-min) (match-beginning 0)))
647 (goto-char (point-max))
648 (if (re-search-backward "--\r?\n?" nil t)
649 (delete-region (match-end 0) (point-max)))
651 (insert-buffer-substring smime-details-buffer)))
652 (goto-char (point-min))
653 (while (search-forward "\r\n" nil t)
654 (replace-match "\n"))
657 (defun mm-view-pkcs7-decrypt (handle)
658 (insert-buffer-substring (mm-handle-buffer handle))
659 (goto-char (point-min))
660 (insert "MIME-Version: 1.0\n")
661 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
662 (smime-decrypt-region
663 (point-min) (point-max)
664 (if (= (length smime-keys) 1)
666 (smime-get-key-by-email
668 (concat "Decipher using key"
669 (if smime-keys (concat "(default " (caar smime-keys) "): ")
671 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
672 (goto-char (point-min))
673 (while (search-forward "\r\n" nil t)
674 (replace-match "\n"))
675 (goto-char (point-min)))
679 ;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
680 ;;; mm-view.el ends here