1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 (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" nil t)
40 (unless (fboundp 'diff-mode)
41 (autoload 'diff-mode "diff-mode" "" t nil)))
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 an 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))
90 (put-image (mm-get-image handle) b)
92 (mm-handle-set-undisplayer
98 (delete-region b (+ b 2)))))))
100 (defun mm-inline-image-xemacs (handle)
103 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
105 (mm-handle-set-undisplayer
108 (let ((b ,(point-marker))
110 (delete-annotation ,annot)
111 (delete-region (- b 2) b))))
112 (set-extent-property annot 'mm t)
113 (set-extent-property annot 'duplicable t)))
116 (if (featurep 'xemacs)
117 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
118 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
120 (defvar mm-w3-setup nil)
121 (defun mm-setup-w3 ()
128 (setq mm-w3-setup t)))
130 (defun mm-inline-text-html-render-with-w3 (handle)
132 (let ((text (mm-get-part handle))
134 (url-standalone-mode t)
135 (url-gateway-unplugged t)
136 (w3-honor-stylesheets nil)
138 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
139 (width (window-width))
140 (charset (mail-content-type-get
141 (mm-handle-type handle) 'charset)))
145 (narrow-to-region b (point))
146 (goto-char (point-min))
147 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
149 w3-meta-content-type-charset-regexp nil t))
150 (and (boundp 'w3-meta-charset-content-type-regexp)
152 w3-meta-charset-content-type-regexp nil t)))
154 (or (let ((bsubstr (buffer-substring-no-properties
157 (if (fboundp 'w3-coding-system-for-mime-charset)
158 (w3-coding-system-for-mime-charset bsubstr)
159 (mm-charset-to-coding-system bsubstr)))
161 (delete-region (point-min) (point-max))
162 (insert (mm-decode-string text charset))
163 (save-window-excursion
165 (let ((w3-strict-width width)
166 ;; Don't let w3 set the global version of
168 (fill-column fill-column))
169 (if (or debug-on-error debug-on-quit)
170 (w3-region (point-min) (point-max))
172 (w3-region (point-min) (point-max))
174 (delete-region (point-min) (point-max))
176 (charset (mail-content-type-get
177 (mm-handle-type handle) 'charset)))
178 (if (or (eq charset 'gnus-decoded)
179 (eq mail-parse-charset 'gnus-decoded))
181 (narrow-to-region (point) (point))
182 (mm-insert-part handle)
183 (goto-char (point-max)))
184 (insert (mm-decode-string (mm-get-part handle)
187 "Error while rendering html; showing as text/plain")))))))
188 (mm-handle-set-undisplayer
191 (let (buffer-read-only)
192 (if (functionp 'remove-specifier)
193 (mapcar (lambda (prop)
195 (face-property 'default prop)
197 '(background background-pixmap foreground)))
198 (delete-region ,(point-min-marker)
199 ,(point-max-marker)))))))))
201 (defvar mm-w3m-setup nil
202 "Whether gnus-article-mode has been setup to use emacs-w3m.")
204 (defun mm-setup-w3m ()
205 "Setup gnus-article-mode to use emacs-w3m."
208 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
209 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
210 w3m-cid-retrieve-function-alist))
211 (setq mm-w3m-setup t))
212 (setq w3m-display-inline-images mm-inline-text-html-with-images))
214 (defun mm-w3m-cid-retrieve-1 (url handle)
215 (dolist (elem handle)
217 (when (equal url (mm-handle-id elem))
218 (mm-insert-part elem)
219 (throw 'found-handle (mm-handle-media-type elem)))
220 (when (and (stringp (car elem))
221 (equal "multipart" (mm-handle-media-supertype elem)))
222 (mm-w3m-cid-retrieve-1 url elem)))))
224 (defun mm-w3m-cid-retrieve (url &rest args)
225 "Insert a content pointed by URL if it has the cid: scheme."
226 (when (string-match "\\`cid:" url)
227 (or (catch 'found-handle
228 (mm-w3m-cid-retrieve-1
229 (setq url (concat "<" (substring url (match-end 0)) ">"))
230 (with-current-buffer w3m-current-buffer
231 gnus-article-mime-handles)))
234 (message "Failed to find \"Content-ID: %s\"" url)))))
236 (defun mm-inline-text-html-render-with-w3m (handle)
237 "Render a text/html part using emacs-w3m."
239 (let ((text (mm-get-part handle))
241 (charset (or (mail-content-type-get (mm-handle-type handle) 'charset)
242 mail-parse-charset)))
244 (insert (if charset (mm-decode-string text charset) text))
246 (narrow-to-region b (point))
248 (goto-char (point-min))
249 (when (setq charset (w3m-detect-meta-charset))
250 (delete-region (point-min) (point-max))
251 (insert (mm-decode-string text charset))))
252 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
254 (w3m-region (point-min) (point-max) nil charset))
255 (when (and mm-inline-text-html-with-w3m-keymap
256 (boundp 'w3m-minor-mode-map)
259 (point-min) (point-max)
260 (list 'keymap w3m-minor-mode-map
261 ;; Put the mark meaning this part was rendered by emacs-w3m.
262 'mm-inline-text-html-with-w3m t)))
263 (mm-handle-set-undisplayer
266 (let (buffer-read-only)
267 (if (functionp 'remove-specifier)
268 (mapcar (lambda (prop)
270 (face-property 'default prop)
272 '(background background-pixmap foreground)))
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#s!!#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))
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 (buffer-read-only)
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))
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))
432 (goto-char (point-max))))
434 (narrow-to-region b (point))
435 (when (or (equal type "enriched")
436 (equal type "richtext"))
437 (set-text-properties (point-min) (point-max) nil)
439 (enriched-decode (point-min) (point-max))))
440 (mm-handle-set-undisplayer
443 (let (buffer-read-only)
444 (delete-region ,(point-min-marker)
445 ,(point-max-marker))))))))
447 (defun mm-insert-inline (handle text)
448 "Insert TEXT inline from HANDLE."
451 (mm-handle-set-undisplayer
454 (let (buffer-read-only)
455 (delete-region ,(set-marker (make-marker) b)
456 ,(set-marker (make-marker) (point))))))))
458 (defun mm-inline-audio (handle)
459 (message "Not implemented"))
461 (defun mm-view-sound-file ()
462 (message "Not implemented"))
464 (defun mm-w3-prepare-buffer ()
466 (let ((url-standalone-mode t)
467 (url-gateway-unplugged t)
468 (w3-honor-stylesheets nil))
469 (w3-prepare-buffer)))
471 (defun mm-view-message ()
472 (mm-enable-multibyte)
474 (let (gnus-article-mime-handles)
475 ;; Double decode problem may happen. See mm-inline-message.
476 (run-hooks 'gnus-article-decode-hook)
477 (gnus-article-prepare-display)
478 (setq handles gnus-article-mime-handles))
480 (setq gnus-article-mime-handles
481 (mm-merge-handles gnus-article-mime-handles handles))))
483 (goto-char (point-min)))
485 (defun mm-inline-message (handle)
488 (charset (mail-content-type-get
489 (mm-handle-type handle) 'charset))
490 gnus-displaying-mime handles)
493 (setq charset (intern (downcase charset)))
494 (when (eq charset 'us-ascii)
498 (narrow-to-region b b)
499 (mm-insert-part handle)
500 (let (gnus-article-mime-handles
501 ;; disable prepare hook
502 gnus-article-prepare-hook
503 (gnus-newsgroup-charset
504 (unless (eq charset 'gnus-decoded) ;; mm-uu might set it.
505 (or charset gnus-newsgroup-charset))))
506 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
507 (run-hooks 'gnus-article-decode-hook))
508 (gnus-article-prepare-display)
509 (setq handles gnus-article-mime-handles))
510 (goto-char (point-min))
513 (goto-char (point-max))
516 (insert "----------\n\n")
518 (setq gnus-article-mime-handles
519 (mm-merge-handles gnus-article-mime-handles handles)))
520 (mm-handle-set-undisplayer
523 (let (buffer-read-only)
524 (if (fboundp 'remove-specifier)
525 ;; This is only valid on XEmacs.
526 (mapcar (lambda (prop)
528 (face-property 'default prop) (current-buffer)))
529 '(background background-pixmap foreground)))
530 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
532 (defun mm-display-inline-fontify (handle mode)
534 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
535 ;; on for buffers whose name begins with " ". That's why we use
536 ;; save-current-buffer/get-buffer-create rather than
539 (set-buffer (generate-new-buffer "*fontification*"))
542 (buffer-disable-undo)
543 (mm-insert-part handle)
545 (let ((font-lock-maximum-size nil)
546 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
547 (font-lock-mode-hook nil)
548 (font-lock-support-mode nil)
549 ;; I find font-lock a bit too verbose.
550 (font-lock-verbose nil))
552 ;; The mode function might have already turned on font-lock.
553 (unless (symbol-value 'font-lock-mode)
554 (font-lock-fontify-buffer)))
555 ;; By default, XEmacs font-lock uses non-duplicable text
556 ;; properties. This code forces all the text properties
557 ;; to be copied along with the text.
558 (when (fboundp 'extent-list)
559 (map-extents (lambda (ext ignored)
560 (set-extent-property ext 'duplicable t)
562 nil nil nil nil nil 'text-prop))
563 (setq text (buffer-string)))
564 (kill-buffer (current-buffer))))
565 (mm-insert-inline handle text)))
567 ;; Shouldn't these functions check whether the user even wants to use
568 ;; font-lock? At least under XEmacs, this fontification is pretty
569 ;; much unconditional. Also, it would be nice to change for the size
570 ;; of the fontified region.
572 (defun mm-display-patch-inline (handle)
573 (mm-display-inline-fontify handle 'diff-mode))
575 (defun mm-display-elisp-inline (handle)
576 (mm-display-inline-fontify handle 'emacs-lisp-mode))
578 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
579 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
580 (defvar mm-pkcs7-signed-magic
581 (mm-string-as-unibyte
583 (mapcar 'char-to-string
584 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
585 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
586 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
587 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
589 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
590 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
591 (defvar mm-pkcs7-enveloped-magic
592 (mm-string-as-unibyte
594 (mapcar '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 ?\x03)))))
600 (defun mm-view-pkcs7-get-type (handle)
601 (mm-with-unibyte-buffer
602 (mm-insert-part handle)
603 (cond ((looking-at mm-pkcs7-enveloped-magic)
605 ((looking-at mm-pkcs7-signed-magic)
608 (error "Could not identify PKCS#7 type")))))
610 (defun mm-view-pkcs7 (handle)
611 (case (mm-view-pkcs7-get-type handle)
612 (enveloped (mm-view-pkcs7-decrypt handle))
613 (signed (mm-view-pkcs7-verify handle))
614 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
616 (defun mm-view-pkcs7-verify (handle)
617 ;; A bogus implementation of PKCS#7. FIXME::
618 (mm-insert-part handle)
619 (goto-char (point-min))
620 (if (search-forward "Content-Type: " nil t)
621 (delete-region (point-min) (match-beginning 0)))
622 (goto-char (point-max))
623 (if (re-search-backward "--\r?\n?" nil t)
624 (delete-region (match-end 0) (point-max)))
625 (goto-char (point-min))
626 (while (search-forward "\r\n" nil t)
627 (replace-match "\n"))
628 (message "Verify signed PKCS#7 message is unimplemented.")
632 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
634 (defun mm-view-pkcs7-decrypt (handle)
635 (insert-buffer-substring (mm-handle-buffer handle))
636 (goto-char (point-min))
637 (insert "MIME-Version: 1.0\n")
638 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
639 (smime-decrypt-region
640 (point-min) (point-max)
641 (if (= (length smime-keys) 1)
643 (smime-get-key-by-email
644 (gnus-completing-read-maybe-default
645 (concat "Decipher using key"
647 (concat " (default " (caar smime-keys) "): ")
649 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
650 (goto-char (point-min))
651 (while (search-forward "\r\n" nil t)
652 (replace-match "\n"))
653 (goto-char (point-min)))
657 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
658 ;;; mm-view.el ends here