1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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 mm-text-html-renderer-alist
42 '((w3 . mm-inline-text-html-render-with-w3)
43 (w3m . mm-inline-text-html-render-with-w3m)
44 (w3m-standalone mm-inline-render-with-stdin nil
45 "w3m" "-dump" "-T" "text/html")
46 (links mm-inline-render-with-file
47 mm-links-remove-leading-blank
49 (lynx mm-inline-render-with-stdin nil
50 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
51 (html2text mm-inline-render-with-function html2text))
52 "The attributes of renderer types for text/html.")
54 (defvar mm-text-html-washer-alist
55 '((w3 . gnus-article-wash-html-with-w3)
56 (w3m . gnus-article-wash-html-with-w3m)
57 (w3m-standalone mm-inline-wash-with-stdin nil
58 "w3m" "-dump" "-T" "text/html")
59 (links mm-inline-wash-with-file
60 mm-links-remove-leading-blank
62 (lynx mm-inline-wash-with-stdin nil
63 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
64 (html2text html2text))
65 "The attributes of washer types for text/html.")
67 (defcustom mm-fill-flowed t
68 "If non-nil a format=flowed article will be displayed flowed."
72 ;;; Internal variables.
75 ;;; Functions for displaying various formats inline
78 (defun mm-inline-image-emacs (handle)
79 (let ((b (point-marker))
81 (put-image (mm-get-image handle) b)
83 (mm-handle-set-undisplayer
89 (delete-region b (+ b 2)))))))
91 (defun mm-inline-image-xemacs (handle)
94 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
96 (mm-handle-set-undisplayer
99 (let ((b ,(point-marker))
101 (delete-annotation ,annot)
102 (delete-region (- b 2) b))))
103 (set-extent-property annot 'mm t)
104 (set-extent-property annot 'duplicable t)))
107 (if (featurep 'xemacs)
108 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
109 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
111 (defvar mm-w3-setup nil)
112 (defun mm-setup-w3 ()
119 (setq mm-w3-setup t)))
121 (defun mm-inline-text-html-render-with-w3 (handle)
123 (let ((text (mm-get-part handle))
125 (url-standalone-mode t)
126 (url-gateway-unplugged t)
127 (w3-honor-stylesheets nil)
129 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
130 (width (window-width))
131 (charset (mail-content-type-get
132 (mm-handle-type handle) 'charset)))
134 (insert (if charset (mm-decode-string text charset) text))
136 (narrow-to-region b (point))
138 (goto-char (point-min))
139 (when (or (and (boundp 'w3-meta-content-type-charset-regexp)
141 w3-meta-content-type-charset-regexp nil t))
142 (and (boundp 'w3-meta-charset-content-type-regexp)
144 w3-meta-charset-content-type-regexp nil t)))
146 (let ((bsubstr (buffer-substring-no-properties
149 (if (fboundp 'w3-coding-system-for-mime-charset)
150 (w3-coding-system-for-mime-charset bsubstr)
151 (mm-charset-to-coding-system bsubstr))))
152 (delete-region (point-min) (point-max))
153 (insert (mm-decode-string text charset))))
154 (save-window-excursion
156 (let ((w3-strict-width width)
157 ;; Don't let w3 set the global version of
159 (fill-column fill-column))
160 (if (or debug-on-error debug-on-quit)
161 (w3-region (point-min) (point-max))
163 (w3-region (point-min) (point-max))
165 (delete-region (point-min) (point-max))
167 (charset (mail-content-type-get
168 (mm-handle-type handle) 'charset)))
169 (if (or (eq charset 'gnus-decoded)
170 (eq mail-parse-charset 'gnus-decoded))
172 (narrow-to-region (point) (point))
173 (mm-insert-part handle)
174 (goto-char (point-max)))
175 (insert (mm-decode-string (mm-get-part handle)
178 "Error while rendering html; showing as text/plain")))))))
179 (mm-handle-set-undisplayer
182 (let (buffer-read-only)
183 (if (functionp 'remove-specifier)
184 (mapcar (lambda (prop)
186 (face-property 'default prop)
188 '(background background-pixmap foreground)))
189 (delete-region ,(point-min-marker)
190 ,(point-max-marker)))))))))
192 (defvar mm-w3m-setup nil
193 "Whether gnus-article-mode has been setup to use emacs-w3m.")
195 (defun mm-setup-w3m ()
196 "Setup gnus-article-mode to use emacs-w3m."
199 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
200 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
201 w3m-cid-retrieve-function-alist))
202 (setq mm-w3m-setup t))
203 (setq w3m-display-inline-images mm-inline-text-html-with-images))
205 (defun mm-w3m-cid-retrieve-1 (url handle)
206 (dolist (elem handle)
208 (if (equal url (mm-handle-id elem))
210 (mm-insert-part elem)
211 (throw 'found-handle (mm-handle-media-type elem))))
212 (if (equal "multipart" (mm-handle-media-supertype elem))
213 (mm-w3m-cid-retrieve-1 url elem)))))
215 (defun mm-w3m-cid-retrieve (url &rest args)
216 "Insert a content pointed by URL if it has the cid: scheme."
217 (when (string-match "\\`cid:" url)
219 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
220 (with-current-buffer w3m-current-buffer
221 gnus-article-mime-handles)))))
223 (defun mm-inline-text-html-render-with-w3m (handle)
224 "Render a text/html part using emacs-w3m."
226 (let ((text (mm-get-part handle))
228 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
230 (insert (if charset (mm-decode-string text charset) text))
232 (narrow-to-region b (point))
234 (goto-char (point-min))
235 (when (setq charset (w3m-detect-meta-charset))
236 (delete-region (point-min) (point-max))
237 (insert (mm-decode-string text charset))))
238 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
240 (w3m-region (point-min) (point-max) nil charset))
241 (when (and mm-inline-text-html-with-w3m-keymap
242 (boundp 'w3m-minor-mode-map)
245 (point-min) (point-max)
246 (list 'keymap w3m-minor-mode-map
247 ;; Put the mark meaning this part was rendered by emacs-w3m.
248 'mm-inline-text-html-with-w3m t))))
249 (mm-handle-set-undisplayer
252 (let (buffer-read-only)
253 (if (functionp 'remove-specifier)
254 (mapcar (lambda (prop)
256 (face-property 'default prop)
258 '(background background-pixmap foreground)))
259 (delete-region ,(point-min-marker)
260 ,(point-max-marker))))))))
262 (defun mm-links-remove-leading-blank ()
263 ;; Delete the annoying three spaces preceding each line of links
265 (goto-char (point-min))
266 (while (re-search-forward "^ " nil t)
267 (delete-region (match-beginning 0) (match-end 0))))
269 (defun mm-inline-wash-with-file (post-func cmd &rest args)
270 (let ((file (mm-make-temp-file
271 (expand-file-name "mm" mm-tmp-directory))))
272 (let ((coding-system-for-write 'binary))
273 (write-region (point-min) (point-max) file nil 'silent))
274 (delete-region (point-min) (point-max))
276 (apply 'call-process cmd nil t nil (mapcar 'eval args))
278 (and post-func (funcall post-func))))
280 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
281 (let ((coding-system-for-write 'binary))
282 (apply 'call-process-region (point-min) (point-max)
284 (and post-func (funcall post-func)))
286 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
287 (let ((source (mm-get-part handle)))
290 (mm-with-unibyte-buffer
292 (apply 'mm-inline-wash-with-file post-func cmd args)
295 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
296 (let ((source (mm-get-part handle)))
299 (mm-with-unibyte-buffer
301 (apply 'mm-inline-wash-with-stdin post-func cmd args)
304 (defun mm-inline-render-with-function (handle func &rest args)
305 (let ((source (mm-get-part handle))
306 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
309 (mm-with-multibyte-buffer
311 (mm-decode-string source charset)
316 (defun mm-inline-text-html (handle)
317 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
318 (entry (assq func mm-text-html-renderer-alist))
321 (setq func (cdr entry)))
324 (funcall func handle))
326 (apply (car func) handle (cdr func))))))
328 (defun mm-inline-text-vcard (handle)
329 (let (buffer-read-only)
334 (if (fboundp 'vcard-pretty-print)
335 (vcard-pretty-print (mm-get-part handle))
337 (vcard-parse-string (mm-get-part handle)
338 'vcard-standard-filter))))))))
340 (defun mm-inline-text (handle)
342 (type (mm-handle-media-subtype handle))
343 (charset (mail-content-type-get
344 (mm-handle-type handle) 'charset))
346 (if (or (eq charset 'gnus-decoded)
347 ;; This is probably not entirely correct, but
348 ;; makes rfc822 parts with embedded multiparts work.
349 (eq mail-parse-charset 'gnus-decoded))
351 (narrow-to-region (point) (point))
352 (mm-insert-part handle)
353 (goto-char (point-max)))
354 (insert (mm-decode-string (mm-get-part handle) charset)))
355 (when (and mm-fill-flowed
357 (equal (cdr (assoc 'format (mm-handle-type handle)))
360 (narrow-to-region b (point))
363 (goto-char (point-max))))
365 (narrow-to-region b (point))
366 (set-text-properties (point-min) (point-max) nil)
367 (when (or (equal type "enriched")
368 (equal type "richtext"))
370 (enriched-decode (point-min) (point-max))))
371 (mm-handle-set-undisplayer
374 (let (buffer-read-only)
375 (delete-region ,(point-min-marker)
376 ,(point-max-marker))))))))
378 (defun mm-insert-inline (handle text)
379 "Insert TEXT inline from HANDLE."
384 (mm-handle-set-undisplayer
387 (let (buffer-read-only)
388 (delete-region ,(set-marker (make-marker) b)
389 ,(set-marker (make-marker) (point))))))))
391 (defun mm-inline-audio (handle)
392 (message "Not implemented"))
394 (defun mm-view-sound-file ()
395 (message "Not implemented"))
397 (defun mm-w3-prepare-buffer ()
399 (let ((url-standalone-mode t)
400 (url-gateway-unplugged t)
401 (w3-honor-stylesheets nil))
402 (w3-prepare-buffer)))
404 (defun mm-view-message ()
405 (mm-enable-multibyte)
407 (let (gnus-article-mime-handles)
408 ;; Double decode problem may happen. See mm-inline-message.
409 (run-hooks 'gnus-article-decode-hook)
410 (gnus-article-prepare-display)
411 (setq handles gnus-article-mime-handles))
413 (setq gnus-article-mime-handles
414 (mm-merge-handles gnus-article-mime-handles handles))))
416 (goto-char (point-min)))
418 (defun mm-inline-message (handle)
421 (charset (mail-content-type-get
422 (mm-handle-type handle) 'charset))
423 gnus-displaying-mime handles)
426 (setq charset (intern (downcase charset)))
427 (when (eq charset 'us-ascii)
431 (narrow-to-region b b)
432 (mm-insert-part handle)
433 (let (gnus-article-mime-handles
434 ;; disable prepare hook
435 gnus-article-prepare-hook
436 (gnus-newsgroup-charset
437 (or charset gnus-newsgroup-charset)))
438 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
439 (run-hooks 'gnus-article-decode-hook))
440 (gnus-article-prepare-display)
441 (setq handles gnus-article-mime-handles))
442 (goto-char (point-min))
445 (goto-char (point-max))
448 (insert "----------\n\n")
450 (setq gnus-article-mime-handles
451 (mm-merge-handles gnus-article-mime-handles handles)))
452 (mm-handle-set-undisplayer
455 (let (buffer-read-only)
456 (if (fboundp 'remove-specifier)
457 ;; This is only valid on XEmacs.
458 (mapcar (lambda (prop)
460 (face-property 'default prop) (current-buffer)))
461 '(background background-pixmap foreground)))
462 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
464 (defun mm-display-inline-fontify (handle mode)
465 (let ((charset (mail-content-type-get (mm-handle-type handle) 'charset))
467 (unless (eq charset 'gnus-decoded)
468 (mm-with-unibyte-buffer
469 (mm-insert-part handle)
470 (mm-decompress-buffer
471 (or (mail-content-type-get (mm-handle-disposition handle) 'name)
472 (mail-content-type-get (mm-handle-disposition handle) 'filename))
475 (setq coding-system (mm-find-buffer-file-coding-system)))
476 (setq text (buffer-string))))
477 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
478 ;; on for buffers whose name begins with " ". That's why we use
479 ;; `with-current-buffer'/`generate-new-buffer' rather than
480 ;; `with-temp-buffer'.
481 (with-current-buffer (generate-new-buffer "*fontification*")
482 (buffer-disable-undo)
483 (mm-enable-multibyte)
484 (insert (cond ((eq charset 'gnus-decoded)
485 (mm-insert-part handle))
487 (mm-decode-coding-string text coding-system))
489 (mm-decode-string text charset))
493 ;; Inhibit font-lock this time (*-mode-hook might run
494 ;; `turn-on-font-lock') so that jit-lock may not turn off
495 ;; font-lock immediately after this.
496 (let ((font-lock-mode t))
498 (let ((font-lock-verbose nil))
499 ;; I find font-lock a bit too verbose.
500 (font-lock-fontify-buffer))
501 ;; By default, XEmacs font-lock uses non-duplicable text
502 ;; properties. This code forces all the text properties
503 ;; to be copied along with the text.
504 (when (fboundp 'extent-list)
505 (map-extents (lambda (ext ignored)
506 (set-extent-property ext 'duplicable t)
508 nil nil nil nil nil 'text-prop))
509 (setq text (buffer-string))
510 (kill-buffer (current-buffer)))
511 (mm-insert-inline handle text)))
513 ;; Shouldn't these functions check whether the user even wants to use
514 ;; font-lock? At least under XEmacs, this fontification is pretty
515 ;; much unconditional. Also, it would be nice to change for the size
516 ;; of the fontified region.
518 (defun mm-display-patch-inline (handle)
519 (mm-display-inline-fontify handle 'diff-mode))
521 (defun mm-display-elisp-inline (handle)
522 (mm-display-inline-fontify handle 'emacs-lisp-mode))
524 (defun mm-display-dns-inline (handle)
525 (mm-display-inline-fontify handle 'dns-mode))
527 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
528 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
529 (defvar mm-pkcs7-signed-magic
530 (mm-string-as-unibyte
531 (mapconcat 'char-to-string
532 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
533 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
534 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
535 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02) "")))
537 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
538 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
539 (defvar mm-pkcs7-enveloped-magic
540 (mm-string-as-unibyte
541 (mapconcat 'char-to-string
542 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
543 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
544 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
545 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03) "")))
547 (defun mm-view-pkcs7-get-type (handle)
548 (mm-with-unibyte-buffer
549 (mm-insert-part handle)
550 (cond ((looking-at mm-pkcs7-enveloped-magic)
552 ((looking-at mm-pkcs7-signed-magic)
555 (error "Could not identify PKCS#7 type")))))
557 (defun mm-view-pkcs7 (handle)
558 (case (mm-view-pkcs7-get-type handle)
559 (enveloped (mm-view-pkcs7-decrypt handle))
560 (signed (mm-view-pkcs7-verify handle))
561 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
563 (defun mm-view-pkcs7-verify (handle)
564 ;; A bogus implementation of PKCS#7. FIXME::
565 (mm-insert-part handle)
566 (goto-char (point-min))
567 (if (search-forward "Content-Type: " nil t)
568 (delete-region (point-min) (match-beginning 0)))
569 (goto-char (point-max))
570 (if (re-search-backward "--\r?\n?" nil t)
571 (delete-region (match-end 0) (point-max)))
572 (goto-char (point-min))
573 (while (search-forward "\r\n" nil t)
574 (replace-match "\n"))
575 (message "Verify signed PKCS#7 message is unimplemented.")
579 (defun mm-view-pkcs7-decrypt (handle)
580 (insert-buffer-substring (mm-handle-buffer handle))
581 (goto-char (point-min))
582 (insert "MIME-Version: 1.0\n")
583 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
584 (smime-decrypt-region
585 (point-min) (point-max)
586 (if (= (length smime-keys) 1)
588 (smime-get-key-by-email
590 (concat "Decipher using which key? "
591 (if smime-keys (concat "(default " (caar smime-keys) ") ")
593 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
594 (goto-char (point-min))
595 (while (search-forward "\r\n" nil t)
596 (replace-match "\n"))
597 (goto-char (point-min)))
601 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
602 ;;; mm-view.el ends here