1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
27 (eval-when-compile (require 'cl))
29 (require 'gnus-mailcap)
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"))
40 (defvar mm-text-html-renderer-alist
41 '((w3 . mm-inline-text-html-render-with-w3)
42 (w3m . mm-inline-text-html-render-with-w3m)
43 (w3m-standalone mm-inline-render-with-stdin nil
44 "w3m" "-dump" "-T" "text/html")
45 (links mm-inline-render-with-file
46 mm-links-remove-leading-blank
48 (lynx mm-inline-render-with-stdin nil
49 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
50 (html2text mm-inline-render-with-function html2text))
51 "The attributes of renderer types for text/html.")
53 (defvar mm-text-html-washer-alist
54 '((w3 . gnus-article-wash-html-with-w3)
55 (w3m . gnus-article-wash-html-with-w3m)
56 (w3m-standalone mm-inline-wash-with-stdin nil
57 "w3m" "-dump" "-T" "text/html")
58 (links mm-inline-wash-with-file
59 mm-links-remove-leading-blank
61 (lynx mm-inline-wash-with-stdin nil
62 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
63 (html2text html2text))
64 "The attributes of washer types for text/html.")
66 (defcustom mm-fill-flowed t
67 "If non-nil a format=flowed article will be displayed flowed."
71 ;;; Internal variables.
74 ;;; Functions for displaying various formats inline
77 (defun mm-inline-image-emacs (handle)
78 (let ((b (point-marker))
80 (put-image (mm-get-image handle) b)
82 (mm-handle-set-undisplayer
88 (delete-region b (+ b 2)))))))
90 (defun mm-inline-image-xemacs (handle)
93 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
95 (mm-handle-set-undisplayer
98 (let ((b ,(point-marker))
100 (delete-annotation ,annot)
101 (delete-region (- b 2) b))))
102 (set-extent-property annot 'mm t)
103 (set-extent-property annot 'duplicable t)))
106 (if (featurep 'xemacs)
107 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
108 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
110 (defvar mm-w3-setup nil)
111 (defun mm-setup-w3 ()
118 (setq mm-w3-setup t)))
120 (defun mm-inline-text-html-render-with-w3 (handle)
122 (let ((text (mm-get-part handle))
124 (url-standalone-mode t)
125 (url-gateway-unplugged t)
126 (w3-honor-stylesheets nil)
128 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
129 (width (window-width))
130 (charset (mail-content-type-get
131 (mm-handle-type handle) 'charset)))
133 (insert (if charset (mm-decode-string text charset) text))
135 (narrow-to-region b (point))
137 (goto-char (point-min))
138 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
140 w3-meta-content-type-charset-regexp nil t))
141 (and (boundp 'w3-meta-charset-content-type-regexp)
143 w3-meta-charset-content-type-regexp nil t)))
145 (let ((bsubstr (buffer-substring-no-properties
148 (if (fboundp 'w3-coding-system-for-mime-charset)
149 (w3-coding-system-for-mime-charset bsubstr)
150 (mm-charset-to-coding-system bsubstr))))
151 (delete-region (point-min) (point-max))
152 (insert (mm-decode-string text charset))))
153 (save-window-excursion
155 (let ((w3-strict-width width)
156 ;; Don't let w3 set the global version of
158 (fill-column fill-column))
159 (if (or debug-on-error debug-on-quit)
160 (w3-region (point-min) (point-max))
162 (w3-region (point-min) (point-max))
164 (delete-region (point-min) (point-max))
166 (charset (mail-content-type-get
167 (mm-handle-type handle) 'charset)))
168 (if (or (eq charset 'gnus-decoded)
169 (eq mail-parse-charset 'gnus-decoded))
171 (narrow-to-region (point) (point))
172 (mm-insert-part handle)
173 (goto-char (point-max)))
174 (insert (mm-decode-string (mm-get-part handle)
177 "Error while rendering html; showing as text/plain")))))))
178 (mm-handle-set-undisplayer
181 (let (buffer-read-only)
182 (if (functionp 'remove-specifier)
183 (mapcar (lambda (prop)
185 (face-property 'default prop)
187 '(background background-pixmap foreground)))
188 (delete-region ,(point-min-marker)
189 ,(point-max-marker)))))))))
191 (defvar mm-w3m-setup nil
192 "Whether gnus-article-mode has been setup to use emacs-w3m.")
194 (defun mm-setup-w3m ()
195 "Setup gnus-article-mode to use emacs-w3m."
198 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
199 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
200 w3m-cid-retrieve-function-alist))
201 (setq mm-w3m-setup t))
202 (setq w3m-display-inline-images mm-inline-text-html-with-images))
204 (defun mm-w3m-cid-retrieve-1 (url handle)
205 (dolist (elem handle)
207 (if (equal url (mm-handle-id elem))
209 (mm-insert-part elem)
210 (throw 'found-handle (mm-handle-media-type elem))))
211 (if (equal "multipart" (mm-handle-media-supertype elem))
212 (mm-w3m-cid-retrieve-1 url elem)))))
214 (defun mm-w3m-cid-retrieve (url &rest args)
215 "Insert a content pointed by URL if it has the cid: scheme."
216 (when (string-match "\\`cid:" url)
218 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
219 (with-current-buffer w3m-current-buffer
220 gnus-article-mime-handles)))))
222 (defun mm-inline-text-html-render-with-w3m (handle)
223 "Render a text/html part using emacs-w3m."
225 (let ((text (mm-get-part handle))
227 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
229 (insert (if charset (mm-decode-string text charset) text))
231 (narrow-to-region b (point))
233 (goto-char (point-min))
234 (when (setq charset (w3m-detect-meta-charset))
235 (delete-region (point-min) (point-max))
236 (insert (mm-decode-string text charset))))
237 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
239 (w3m-region (point-min) (point-max) nil charset))
240 (when (and mm-inline-text-html-with-w3m-keymap
241 (boundp 'w3m-minor-mode-map)
244 (point-min) (point-max)
245 (list 'keymap w3m-minor-mode-map
246 ;; Put the mark meaning this part was rendered by emacs-w3m.
247 'mm-inline-text-html-with-w3m t))))
248 (mm-handle-set-undisplayer
251 (let (buffer-read-only)
252 (if (functionp 'remove-specifier)
253 (mapcar (lambda (prop)
255 (face-property 'default prop)
257 '(background background-pixmap foreground)))
258 (delete-region ,(point-min-marker)
259 ,(point-max-marker))))))))
261 (defun mm-links-remove-leading-blank ()
262 ;; Delete the annoying three spaces preceding each line of links
264 (goto-char (point-min))
265 (while (re-search-forward "^ " nil t)
266 (delete-region (match-beginning 0) (match-end 0))))
268 (defun mm-inline-wash-with-file (post-func cmd &rest args)
269 (let ((file (mm-make-temp-file
270 (expand-file-name "mm" mm-tmp-directory))))
271 (let ((coding-system-for-write 'binary))
272 (write-region (point-min) (point-max) file nil 'silent))
273 (delete-region (point-min) (point-max))
275 (apply 'call-process cmd nil t nil (mapcar 'eval args))
277 (and post-func (funcall post-func))))
279 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
280 (let ((coding-system-for-write 'binary))
281 (apply 'call-process-region (point-min) (point-max)
283 (and post-func (funcall post-func)))
285 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
286 (let ((source (mm-get-part handle)))
289 (mm-with-unibyte-buffer
291 (apply 'mm-inline-wash-with-file post-func cmd args)
294 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
295 (let ((source (mm-get-part handle)))
298 (mm-with-unibyte-buffer
300 (apply 'mm-inline-wash-with-stdin post-func cmd args)
303 (defun mm-inline-render-with-function (handle func &rest args)
304 (let ((source (mm-get-part handle))
305 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
308 (mm-with-multibyte-buffer
310 (mm-decode-string source charset)
315 (defun mm-inline-text-html (handle)
316 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
317 (entry (assq func mm-text-html-renderer-alist))
320 (setq func (cdr entry)))
323 (funcall func handle))
325 (apply (car func) handle (cdr func))))))
327 (defun mm-inline-text-vcard (handle)
328 (let (buffer-read-only)
333 (if (fboundp 'vcard-pretty-print)
334 (vcard-pretty-print (mm-get-part handle))
336 (vcard-parse-string (mm-get-part handle)
337 'vcard-standard-filter))))))))
339 (defun mm-inline-text (handle)
341 (type (mm-handle-media-subtype handle))
342 (charset (mail-content-type-get
343 (mm-handle-type handle) 'charset))
345 (if (or (eq charset 'gnus-decoded)
346 ;; This is probably not entirely correct, but
347 ;; makes rfc822 parts with embedded multiparts work.
348 (eq mail-parse-charset 'gnus-decoded))
350 (narrow-to-region (point) (point))
351 (mm-insert-part handle)
352 (goto-char (point-max)))
353 (insert (mm-decode-string (mm-get-part handle) charset)))
354 (when (and mm-fill-flowed
356 (equal (cdr (assoc 'format (mm-handle-type handle)))
359 (narrow-to-region b (point))
362 (goto-char (point-max))))
364 (narrow-to-region b (point))
365 (set-text-properties (point-min) (point-max) nil)
366 (when (or (equal type "enriched")
367 (equal type "richtext"))
369 (enriched-decode (point-min) (point-max))))
370 (mm-handle-set-undisplayer
373 (let (buffer-read-only)
374 (delete-region ,(point-min-marker)
375 ,(point-max-marker))))))))
377 (defun mm-insert-inline (handle text)
378 "Insert TEXT inline from HANDLE."
381 (mm-handle-set-undisplayer
384 (let (buffer-read-only)
385 (delete-region ,(set-marker (make-marker) b)
386 ,(set-marker (make-marker) (point))))))))
388 (defun mm-inline-audio (handle)
389 (message "Not implemented"))
391 (defun mm-view-sound-file ()
392 (message "Not implemented"))
394 (defun mm-w3-prepare-buffer ()
396 (let ((url-standalone-mode t)
397 (url-gateway-unplugged t)
398 (w3-honor-stylesheets nil))
399 (w3-prepare-buffer)))
401 (defun mm-view-message ()
402 (mm-enable-multibyte)
404 (let (gnus-article-mime-handles)
405 ;; Double decode problem may happen. See mm-inline-message.
406 (run-hooks 'gnus-article-decode-hook)
407 (gnus-article-prepare-display)
408 (setq handles gnus-article-mime-handles))
410 (setq gnus-article-mime-handles
411 (mm-merge-handles gnus-article-mime-handles handles))))
413 (goto-char (point-min)))
415 (defun mm-inline-message (handle)
418 (charset (mail-content-type-get
419 (mm-handle-type handle) 'charset))
420 gnus-displaying-mime handles)
423 (setq charset (intern (downcase charset)))
424 (when (eq charset 'us-ascii)
428 (narrow-to-region b b)
429 (mm-insert-part handle)
430 (let (gnus-article-mime-handles
431 ;; disable prepare hook
432 gnus-article-prepare-hook
433 (gnus-newsgroup-charset
434 (or charset gnus-newsgroup-charset)))
435 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
436 (run-hooks 'gnus-article-decode-hook))
437 (gnus-article-prepare-display)
438 (setq handles gnus-article-mime-handles))
439 (goto-char (point-min))
442 (goto-char (point-max))
445 (insert "----------\n\n")
447 (setq gnus-article-mime-handles
448 (mm-merge-handles gnus-article-mime-handles handles)))
449 (mm-handle-set-undisplayer
452 (let (buffer-read-only)
453 (if (fboundp 'remove-specifier)
454 ;; This is only valid on XEmacs.
455 (mapcar (lambda (prop)
457 (face-property 'default prop) (current-buffer)))
458 '(background background-pixmap foreground)))
459 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
461 (defun mm-display-inline-fontify (handle mode)
463 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
464 ;; on for buffers whose name begins with " ". That's why we use
465 ;; save-current-buffer/get-buffer-create rather than
468 (set-buffer (generate-new-buffer "*fontification*"))
471 (buffer-disable-undo)
472 (mm-insert-part handle)
474 ;; Inhibit font-lock this time (*-mode-hook might run
475 ;; `turn-on-font-lock') so that jit-lock may not turn off
476 ;; font-lock immediately after this.
477 (let ((font-lock-mode t))
479 (let ((font-lock-verbose nil))
480 ;; I find font-lock a bit too verbose.
481 (font-lock-fontify-buffer))
482 ;; By default, XEmacs font-lock uses non-duplicable text
483 ;; properties. This code forces all the text properties
484 ;; to be copied along with the text.
485 (when (fboundp 'extent-list)
486 (map-extents (lambda (ext ignored)
487 (set-extent-property ext 'duplicable t)
489 nil nil nil nil nil 'text-prop))
490 (setq text (buffer-string)))
491 (kill-buffer (current-buffer))))
492 (mm-insert-inline handle text)))
494 ;; Shouldn't these functions check whether the user even wants to use
495 ;; font-lock? At least under XEmacs, this fontification is pretty
496 ;; much unconditional. Also, it would be nice to change for the size
497 ;; of the fontified region.
499 (defun mm-display-patch-inline (handle)
500 (mm-display-inline-fontify handle 'diff-mode))
502 (defun mm-display-elisp-inline (handle)
503 (mm-display-inline-fontify handle 'emacs-lisp-mode))
505 (defun mm-display-dns-inline (handle)
506 (mm-display-inline-fontify handle 'dns-mode))
508 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
509 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
510 (defvar mm-pkcs7-signed-magic
511 (mm-string-as-unibyte
512 (mapconcat 'char-to-string
513 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
514 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
515 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
516 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02) "")))
518 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
519 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
520 (defvar mm-pkcs7-enveloped-magic
521 (mm-string-as-unibyte
522 (mapconcat 'char-to-string
523 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
524 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
525 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
526 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03) "")))
528 (defun mm-view-pkcs7-get-type (handle)
529 (mm-with-unibyte-buffer
530 (mm-insert-part handle)
531 (cond ((looking-at mm-pkcs7-enveloped-magic)
533 ((looking-at mm-pkcs7-signed-magic)
536 (error "Could not identify PKCS#7 type")))))
538 (defun mm-view-pkcs7 (handle)
539 (case (mm-view-pkcs7-get-type handle)
540 (enveloped (mm-view-pkcs7-decrypt handle))
541 (signed (mm-view-pkcs7-verify handle))
542 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
544 (defun mm-view-pkcs7-verify (handle)
545 ;; A bogus implementation of PKCS#7. FIXME::
546 (mm-insert-part handle)
547 (goto-char (point-min))
548 (if (search-forward "Content-Type: " nil t)
549 (delete-region (point-min) (match-beginning 0)))
550 (goto-char (point-max))
551 (if (re-search-backward "--\r?\n?" nil t)
552 (delete-region (match-end 0) (point-max)))
553 (goto-char (point-min))
554 (while (search-forward "\r\n" nil t)
555 (replace-match "\n"))
556 (message "Verify signed PKCS#7 message is unimplemented.")
560 (defun mm-view-pkcs7-decrypt (handle)
561 (insert-buffer-substring (mm-handle-buffer handle))
562 (goto-char (point-min))
563 (insert "MIME-Version: 1.0\n")
564 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
565 (smime-decrypt-region
566 (point-min) (point-max)
567 (if (= (length smime-keys) 1)
569 (smime-get-key-by-email
571 (concat "Decipher using which key? "
572 (if smime-keys (concat "(default " (caar smime-keys) ") ")
574 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
575 (goto-char (point-min))
576 (while (search-forward "\r\n" nil t)
577 (replace-match "\n"))
578 (goto-char (point-min)))
582 ;;; mm-view.el ends here