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))
30 (require 'gnus-mailcap)
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."
78 ;;; Internal variables.
81 ;;; Functions for displaying various formats inline
84 (defun mm-inline-image-emacs (handle)
85 (let ((b (point-marker))
87 (put-image (mm-get-image handle) b)
89 (mm-handle-set-undisplayer
95 (delete-region b (+ b 2)))))))
97 (defun mm-inline-image-xemacs (handle)
100 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
102 (mm-handle-set-undisplayer
105 (let ((b ,(point-marker))
107 (delete-annotation ,annot)
108 (delete-region (- b 2) b))))
109 (set-extent-property annot 'mm t)
110 (set-extent-property annot 'duplicable t)))
113 (if (featurep 'xemacs)
114 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
115 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
117 (defvar mm-w3-setup nil)
118 (defun mm-setup-w3 ()
125 (setq mm-w3-setup t)))
127 (defun mm-inline-text-html-render-with-w3 (handle)
129 (let ((text (mm-get-part handle))
131 (url-standalone-mode t)
132 (url-gateway-unplugged t)
133 (w3-honor-stylesheets nil)
135 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
136 (width (window-width))
137 (charset (mail-content-type-get
138 (mm-handle-type handle) 'charset)))
140 (insert (if charset (mm-decode-string text charset) text))
142 (narrow-to-region b (point))
144 (goto-char (point-min))
145 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
147 w3-meta-content-type-charset-regexp nil t))
148 (and (boundp 'w3-meta-charset-content-type-regexp)
150 w3-meta-charset-content-type-regexp nil t)))
152 (let ((bsubstr (buffer-substring-no-properties
155 (if (fboundp 'w3-coding-system-for-mime-charset)
156 (w3-coding-system-for-mime-charset bsubstr)
157 (mm-charset-to-coding-system bsubstr))))
158 (delete-region (point-min) (point-max))
159 (insert (mm-decode-string text charset))))
160 (save-window-excursion
162 (let ((w3-strict-width width)
163 ;; Don't let w3 set the global version of
165 (fill-column fill-column))
166 (if (or debug-on-error debug-on-quit)
167 (w3-region (point-min) (point-max))
169 (w3-region (point-min) (point-max))
171 (delete-region (point-min) (point-max))
173 (charset (mail-content-type-get
174 (mm-handle-type handle) 'charset)))
175 (if (or (eq charset 'gnus-decoded)
176 (eq mail-parse-charset 'gnus-decoded))
178 (narrow-to-region (point) (point))
179 (mm-insert-part handle)
180 (goto-char (point-max)))
181 (insert (mm-decode-string (mm-get-part handle)
184 "Error while rendering html; showing as text/plain")))))))
185 (mm-handle-set-undisplayer
188 (let (buffer-read-only)
189 (if (functionp 'remove-specifier)
190 (mapcar (lambda (prop)
192 (face-property 'default prop)
194 '(background background-pixmap foreground)))
195 (delete-region ,(point-min-marker)
196 ,(point-max-marker)))))))))
198 (defvar mm-w3m-setup nil
199 "Whether gnus-article-mode has been setup to use emacs-w3m.")
201 (defun mm-setup-w3m ()
202 "Setup gnus-article-mode to use emacs-w3m."
205 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
206 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
207 w3m-cid-retrieve-function-alist))
208 (setq mm-w3m-setup t))
209 (setq w3m-display-inline-images mm-inline-text-html-with-images))
211 (defun mm-w3m-cid-retrieve-1 (url handle)
212 (dolist (elem handle)
214 (if (equal url (mm-handle-id elem))
216 (mm-insert-part elem)
217 (throw 'found-handle (mm-handle-media-type elem))))
218 (if (equal "multipart" (mm-handle-media-supertype elem))
219 (mm-w3m-cid-retrieve-1 url elem)))))
221 (defun mm-w3m-cid-retrieve (url &rest args)
222 "Insert a content pointed by URL if it has the cid: scheme."
223 (when (string-match "\\`cid:" url)
225 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
226 (with-current-buffer w3m-current-buffer
227 gnus-article-mime-handles)))))
229 (defun mm-inline-text-html-render-with-w3m (handle)
230 "Render a text/html part using emacs-w3m."
232 (let ((text (mm-get-part handle))
234 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
236 (insert (if charset (mm-decode-string text charset) text))
238 (narrow-to-region b (point))
240 (goto-char (point-min))
241 (when (setq charset (w3m-detect-meta-charset))
242 (delete-region (point-min) (point-max))
243 (insert (mm-decode-string text charset))))
244 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
246 (w3m-region (point-min) (point-max) nil charset))
247 (when (and mm-inline-text-html-with-w3m-keymap
248 (boundp 'w3m-minor-mode-map)
251 (point-min) (point-max)
252 (list 'keymap w3m-minor-mode-map
253 ;; Put the mark meaning this part was rendered by emacs-w3m.
254 'mm-inline-text-html-with-w3m t)))
255 (mm-handle-set-undisplayer
258 (let (buffer-read-only)
259 (if (functionp 'remove-specifier)
260 (mapcar (lambda (prop)
262 (face-property 'default prop)
264 '(background background-pixmap foreground)))
265 (delete-region ,(point-min-marker)
266 ,(point-max-marker)))))))))
268 (defvar mm-w3m-standalone-supports-m17n-p (if (featurep 'mule) 'undecided)
269 "*T means the w3m command supports the m17n feature.")
271 (defun mm-w3m-standalone-supports-m17n-p ()
272 "Say whether the w3m command supports the m17n feature."
273 (cond ((eq mm-w3m-standalone-supports-m17n-p t) t)
274 ((eq mm-w3m-standalone-supports-m17n-p nil) nil)
275 ((not (featurep 'mule)) (setq mm-w3m-standalone-supports-m17n-p nil))
277 (let ((coding-system-for-write 'iso-2022-jp)
278 (coding-system-for-read 'iso-2022-jp)
279 (str (mm-decode-coding-string "\
280 \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)))
281 (mm-with-multibyte-buffer
284 (point-min) (point-max) "w3m" t t nil "-dump"
285 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
286 (goto-char (point-min))
287 (search-forward str nil t)))
289 (setq mm-w3m-standalone-supports-m17n-p t))
291 ;;(message "You had better upgrade your w3m command")
292 (setq mm-w3m-standalone-supports-m17n-p nil))))
294 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
295 "Render a text/html part using w3m."
296 (if (mm-w3m-standalone-supports-m17n-p)
297 (let ((source (mm-get-part handle))
298 (charset (mail-content-type-get (mm-handle-type handle) 'charset))
301 (setq cs (mm-charset-to-coding-system charset))
302 (not (eq cs 'ascii)))
304 (setq charset "iso-8859-1"
308 (mm-with-unibyte-buffer
310 (mm-enable-multibyte)
311 (let ((coding-system-for-write 'binary)
312 (coding-system-for-read cs))
314 (point-min) (point-max)
315 "w3m" t t nil "-dump" "-T" "text/html"
316 "-I" charset "-O" charset))
318 (mm-inline-render-with-stdin handle nil "w3m" "-dump" "-T" "text/html")))
320 (defun mm-links-remove-leading-blank ()
321 ;; Delete the annoying three spaces preceding each line of links
323 (goto-char (point-min))
324 (while (re-search-forward "^ " nil t)
325 (delete-region (match-beginning 0) (match-end 0))))
327 (defun mm-inline-wash-with-file (post-func cmd &rest args)
328 (let ((file (mm-make-temp-file
329 (expand-file-name "mm" mm-tmp-directory))))
330 (let ((coding-system-for-write 'binary))
331 (write-region (point-min) (point-max) file nil 'silent))
332 (delete-region (point-min) (point-max))
334 (apply 'call-process cmd nil t nil (mapcar 'eval args))
336 (and post-func (funcall post-func))))
338 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
339 (let ((coding-system-for-write 'binary))
340 (apply 'call-process-region (point-min) (point-max)
342 (and post-func (funcall post-func)))
344 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
345 (let ((source (mm-get-part handle)))
348 (mm-with-unibyte-buffer
350 (apply 'mm-inline-wash-with-file post-func cmd args)
353 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
354 (let ((source (mm-get-part handle)))
357 (mm-with-unibyte-buffer
359 (apply 'mm-inline-wash-with-stdin post-func cmd args)
362 (defun mm-inline-render-with-function (handle func &rest args)
363 (let ((source (mm-get-part handle))
364 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
367 (mm-with-multibyte-buffer
369 (mm-decode-string source charset)
374 (defun mm-inline-text-html (handle)
375 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
376 (entry (assq func mm-text-html-renderer-alist))
379 (setq func (cdr entry)))
382 (funcall func handle))
384 (apply (car func) handle (cdr func))))))
386 (defun mm-inline-text-vcard (handle)
387 (let (buffer-read-only)
392 (if (fboundp 'vcard-pretty-print)
393 (vcard-pretty-print (mm-get-part handle))
395 (vcard-parse-string (mm-get-part handle)
396 'vcard-standard-filter))))))))
398 (defun mm-inline-text (handle)
400 (type (mm-handle-media-subtype handle))
401 (charset (mail-content-type-get
402 (mm-handle-type handle) 'charset))
404 (if (or (eq charset 'gnus-decoded)
405 ;; This is probably not entirely correct, but
406 ;; makes rfc822 parts with embedded multiparts work.
407 (eq mail-parse-charset 'gnus-decoded))
409 (narrow-to-region (point) (point))
410 (mm-insert-part handle)
411 (goto-char (point-max)))
412 (insert (mm-decode-string (mm-get-part handle) charset)))
413 (when (and mm-fill-flowed
415 (equal (cdr (assoc 'format (mm-handle-type handle)))
418 (narrow-to-region b (point))
421 (goto-char (point-max))))
423 (narrow-to-region b (point))
424 (when (or (equal type "enriched")
425 (equal type "richtext"))
426 (set-text-properties (point-min) (point-max) nil)
428 (enriched-decode (point-min) (point-max))))
429 (mm-handle-set-undisplayer
432 (let (buffer-read-only)
433 (delete-region ,(point-min-marker)
434 ,(point-max-marker))))))))
436 (defun mm-insert-inline (handle text)
437 "Insert TEXT inline from HANDLE."
442 (mm-handle-set-undisplayer
445 (let (buffer-read-only)
446 (delete-region ,(set-marker (make-marker) b)
447 ,(set-marker (make-marker) (point))))))))
449 (defun mm-inline-audio (handle)
450 (message "Not implemented"))
452 (defun mm-view-sound-file ()
453 (message "Not implemented"))
455 (defun mm-w3-prepare-buffer ()
457 (let ((url-standalone-mode t)
458 (url-gateway-unplugged t)
459 (w3-honor-stylesheets nil))
460 (w3-prepare-buffer)))
462 (defun mm-view-message ()
463 (mm-enable-multibyte)
465 (let (gnus-article-mime-handles)
466 ;; Double decode problem may happen. See mm-inline-message.
467 (run-hooks 'gnus-article-decode-hook)
468 (gnus-article-prepare-display)
469 (setq handles gnus-article-mime-handles))
471 (setq gnus-article-mime-handles
472 (mm-merge-handles gnus-article-mime-handles handles))))
474 (goto-char (point-min)))
476 (defun mm-inline-message (handle)
479 (charset (mail-content-type-get
480 (mm-handle-type handle) 'charset))
481 gnus-displaying-mime handles)
484 (setq charset (intern (downcase charset)))
485 (when (eq charset 'us-ascii)
489 (narrow-to-region b b)
490 (mm-insert-part handle)
491 (let (gnus-article-mime-handles
492 ;; disable prepare hook
493 gnus-article-prepare-hook
494 (gnus-newsgroup-charset
495 (or charset gnus-newsgroup-charset)))
496 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
497 (run-hooks 'gnus-article-decode-hook))
498 (gnus-article-prepare-display)
499 (setq handles gnus-article-mime-handles))
500 (goto-char (point-min))
503 (goto-char (point-max))
506 (insert "----------\n\n")
508 (setq gnus-article-mime-handles
509 (mm-merge-handles gnus-article-mime-handles handles)))
510 (mm-handle-set-undisplayer
513 (let (buffer-read-only)
514 (if (fboundp 'remove-specifier)
515 ;; This is only valid on XEmacs.
516 (mapcar (lambda (prop)
518 (face-property 'default prop) (current-buffer)))
519 '(background background-pixmap foreground)))
520 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
522 (defun mm-display-inline-fontify (handle mode)
523 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
525 (unless (eq charset 'gnus-decoded)
526 (mm-with-unibyte-buffer
527 (mm-insert-part handle)
528 (mm-decompress-buffer
529 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
530 (mail-content-type-get (mm-handle-disposition handle) 'filename))
533 (setq coding-system (mm-find-buffer-file-coding-system)))
534 (setq text (buffer-string))))
535 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
536 ;; on for buffers whose name begins with " ". That's why we use
537 ;; `with-current-buffer'/`generate-new-buffer' rather than
538 ;; `with-temp-buffer'.
539 (with-current-buffer (generate-new-buffer "*fontification*")
540 (buffer-disable-undo)
541 (mm-enable-multibyte)
542 (insert (cond ((eq charset 'gnus-decoded)
543 (with-current-buffer (mm-handle-buffer handle)
546 (mm-decode-coding-string text coding-system))
548 (mm-decode-string text charset))
552 (let ((font-lock-maximum-size nil)
553 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
554 (font-lock-mode-hook nil)
555 (font-lock-support-mode nil)
556 ;; I find font-lock a bit too verbose.
557 (font-lock-verbose nil))
559 ;; The mode function might have already turned on font-lock.
560 (unless (symbol-value 'font-lock-mode)
561 (font-lock-fontify-buffer)))
562 ;; By default, XEmacs font-lock uses non-duplicable text
563 ;; properties. This code forces all the text properties
564 ;; to be copied along with the text.
565 (when (fboundp 'extent-list)
566 (map-extents (lambda (ext ignored)
567 (set-extent-property ext 'duplicable t)
569 nil nil nil nil nil 'text-prop))
570 (setq text (buffer-string))
571 (kill-buffer (current-buffer)))
572 (mm-insert-inline handle text)))
574 ;; Shouldn't these functions check whether the user even wants to use
575 ;; font-lock? At least under XEmacs, this fontification is pretty
576 ;; much unconditional. Also, it would be nice to change for the size
577 ;; of the fontified region.
579 (defun mm-display-patch-inline (handle)
580 (mm-display-inline-fontify handle 'diff-mode))
582 (defun mm-display-elisp-inline (handle)
583 (mm-display-inline-fontify handle 'emacs-lisp-mode))
585 (defun mm-display-dns-inline (handle)
586 (mm-display-inline-fontify handle 'dns-mode))
588 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
589 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
590 (defvar mm-pkcs7-signed-magic
591 (mm-string-as-unibyte
592 (mapconcat 'char-to-string
593 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
594 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
595 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
596 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02) "")))
598 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
599 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
600 (defvar mm-pkcs7-enveloped-magic
601 (mm-string-as-unibyte
602 (mapconcat 'char-to-string
603 (list ?\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 ?\x03) "")))
608 (defun mm-view-pkcs7-get-type (handle)
609 (mm-with-unibyte-buffer
610 (mm-insert-part handle)
611 (cond ((looking-at mm-pkcs7-enveloped-magic)
613 ((looking-at mm-pkcs7-signed-magic)
616 (error "Could not identify PKCS#7 type")))))
618 (defun mm-view-pkcs7 (handle)
619 (case (mm-view-pkcs7-get-type handle)
620 (enveloped (mm-view-pkcs7-decrypt handle))
621 (signed (mm-view-pkcs7-verify handle))
622 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
624 (defun mm-view-pkcs7-verify (handle)
625 ;; A bogus implementation of PKCS#7. FIXME::
626 (mm-insert-part handle)
627 (goto-char (point-min))
628 (if (search-forward "Content-Type: " nil t)
629 (delete-region (point-min) (match-beginning 0)))
630 (goto-char (point-max))
631 (if (re-search-backward "--\r?\n?" nil t)
632 (delete-region (match-end 0) (point-max)))
633 (goto-char (point-min))
634 (while (search-forward "\r\n" nil t)
635 (replace-match "\n"))
636 (message "Verify signed PKCS#7 message is unimplemented.")
640 (defun mm-view-pkcs7-decrypt (handle)
641 (insert-buffer-substring (mm-handle-buffer handle))
642 (goto-char (point-min))
643 (insert "MIME-Version: 1.0\n")
644 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
645 (smime-decrypt-region
646 (point-min) (point-max)
647 (if (= (length smime-keys) 1)
649 (smime-get-key-by-email
651 (concat "Decipher using key"
652 (if smime-keys (concat "(default " (caar smime-keys) "): ")
654 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
655 (goto-char (point-min))
656 (while (search-forward "\r\n" nil t)
657 (replace-match "\n"))
658 (goto-char (point-min)))
662 ;;; mm-view.el ends here