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
))
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 (mail-content-type-get (mm-handle-type handle
) 'charset
)))
243 (insert (if charset
(mm-decode-string text charset
) text
))
245 (narrow-to-region b
(point))
247 (goto-char (point-min))
248 (when (setq charset
(w3m-detect-meta-charset))
249 (delete-region (point-min) (point-max))
250 (insert (mm-decode-string text charset
))))
251 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp
)
253 (w3m-region (point-min) (point-max) nil charset
))
254 (when (and mm-inline-text-html-with-w3m-keymap
255 (boundp 'w3m-minor-mode-map
)
258 (point-min) (point-max)
259 (list 'keymap w3m-minor-mode-map
260 ;; Put the mark meaning this part was rendered by emacs-w3m.
261 'mm-inline-text-html-with-w3m t
)))
262 (mm-handle-set-undisplayer
265 (let (buffer-read-only)
266 (if (functionp 'remove-specifier
)
267 (mapcar (lambda (prop)
269 (face-property 'default prop
)
271 '(background background-pixmap foreground
)))
272 (delete-region ,(point-min-marker)
273 ,(point-max-marker)))))))))
275 (defvar mm-w3m-standalone-supports-m17n-p
(if (featurep 'mule
) 'undecided
)
276 "*T means the w3m command supports the m17n feature.")
278 (defun mm-w3m-standalone-supports-m17n-p ()
279 "Say whether the w3m command supports the m17n feature."
280 (cond ((eq mm-w3m-standalone-supports-m17n-p t
) t
)
281 ((eq mm-w3m-standalone-supports-m17n-p nil
) nil
)
282 ((not (featurep 'mule
)) (setq mm-w3m-standalone-supports-m17n-p nil
))
284 (let ((coding-system-for-write 'iso-2022-jp
)
285 (coding-system-for-read 'iso-2022-jp
)
286 (str (mm-decode-coding-string "\
287 \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
)))
288 (mm-with-multibyte-buffer
291 (point-min) (point-max) "w3m" t t nil
"-dump"
292 "-T" "text/html" "-I" "iso-2022-jp" "-O" "iso-2022-jp")
293 (goto-char (point-min))
294 (search-forward str nil t
)))
296 (setq mm-w3m-standalone-supports-m17n-p t
))
298 ;;(message "You had better upgrade your w3m command")
299 (setq mm-w3m-standalone-supports-m17n-p nil
))))
301 (defun mm-inline-text-html-render-with-w3m-standalone (handle)
302 "Render a text/html part using w3m."
303 (if (mm-w3m-standalone-supports-m17n-p)
304 (let ((source (mm-get-part handle
))
305 (charset (mail-content-type-get (mm-handle-type handle
) 'charset
))
308 (setq cs
(mm-charset-to-coding-system charset
))
309 (not (eq cs
'ascii
)))
311 (setq charset
"iso-8859-1"
315 (mm-with-unibyte-buffer
317 (mm-enable-multibyte)
318 (let ((coding-system-for-write 'binary
)
319 (coding-system-for-read cs
))
321 (point-min) (point-max)
322 "w3m" t t nil
"-dump" "-T" "text/html"
323 "-I" charset
"-O" charset
))
325 (mm-inline-render-with-stdin handle nil
"w3m" "-dump" "-T" "text/html")))
327 (defun mm-links-remove-leading-blank ()
328 ;; Delete the annoying three spaces preceding each line of links
330 (goto-char (point-min))
331 (while (re-search-forward "^ " nil t
)
332 (delete-region (match-beginning 0) (match-end 0))))
334 (defun mm-inline-wash-with-file (post-func cmd
&rest args
)
335 (let ((file (mm-make-temp-file
336 (expand-file-name "mm" mm-tmp-directory
))))
337 (let ((coding-system-for-write 'binary
))
338 (write-region (point-min) (point-max) file nil
'silent
))
339 (delete-region (point-min) (point-max))
341 (apply 'call-process cmd nil t nil
(mapcar 'eval args
))
343 (and post-func
(funcall post-func
))))
345 (defun mm-inline-wash-with-stdin (post-func cmd
&rest args
)
346 (let ((coding-system-for-write 'binary
))
347 (apply 'call-process-region
(point-min) (point-max)
349 (and post-func
(funcall post-func
)))
351 (defun mm-inline-render-with-file (handle post-func cmd
&rest args
)
352 (let ((source (mm-get-part handle
)))
355 (mm-with-unibyte-buffer
357 (apply 'mm-inline-wash-with-file post-func cmd args
)
360 (defun mm-inline-render-with-stdin (handle post-func cmd
&rest args
)
361 (let ((source (mm-get-part handle
)))
364 (mm-with-unibyte-buffer
366 (apply 'mm-inline-wash-with-stdin post-func cmd args
)
369 (defun mm-inline-render-with-function (handle func
&rest args
)
370 (let ((source (mm-get-part handle
))
371 (charset (mail-content-type-get (mm-handle-type handle
) 'charset
)))
374 (mm-with-multibyte-buffer
376 (mm-decode-string source charset
)
381 (defun mm-inline-text-html (handle)
382 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer
))
383 (entry (assq func mm-text-html-renderer-alist
))
386 (setq func
(cdr entry
)))
389 (funcall func handle
))
391 (apply (car func
) handle
(cdr func
))))))
393 (defun mm-inline-text-vcard (handle)
394 (let (buffer-read-only)
399 (if (fboundp 'vcard-pretty-print
)
400 (vcard-pretty-print (mm-get-part handle
))
402 (vcard-parse-string (mm-get-part handle
)
403 'vcard-standard-filter
))))))))
405 (defun mm-inline-text (handle)
407 (type (mm-handle-media-subtype handle
))
408 (charset (mail-content-type-get
409 (mm-handle-type handle
) 'charset
))
411 (if (or (eq charset
'gnus-decoded
)
412 ;; This is probably not entirely correct, but
413 ;; makes rfc822 parts with embedded multiparts work.
414 (eq mail-parse-charset
'gnus-decoded
))
416 (narrow-to-region (point) (point))
417 (mm-insert-part handle
)
418 (goto-char (point-max)))
419 (insert (mm-decode-string (mm-get-part handle
) charset
)))
420 (when (and mm-fill-flowed
422 (equal (cdr (assoc 'format
(mm-handle-type handle
)))
425 (narrow-to-region b
(point))
428 (goto-char (point-max))))
430 (narrow-to-region b
(point))
431 (when (or (equal type
"enriched")
432 (equal type
"richtext"))
433 (set-text-properties (point-min) (point-max) nil
)
435 (enriched-decode (point-min) (point-max))))
436 (mm-handle-set-undisplayer
439 (let (buffer-read-only)
440 (delete-region ,(point-min-marker)
441 ,(point-max-marker))))))))
443 (defun mm-insert-inline (handle text
)
444 "Insert TEXT inline from HANDLE."
447 (mm-handle-set-undisplayer
450 (let (buffer-read-only)
451 (delete-region ,(set-marker (make-marker) b
)
452 ,(set-marker (make-marker) (point))))))))
454 (defun mm-inline-audio (handle)
455 (message "Not implemented"))
457 (defun mm-view-sound-file ()
458 (message "Not implemented"))
460 (defun mm-w3-prepare-buffer ()
462 (let ((url-standalone-mode t
)
463 (url-gateway-unplugged t
)
464 (w3-honor-stylesheets nil
))
465 (w3-prepare-buffer)))
467 (defun mm-view-message ()
468 (mm-enable-multibyte)
470 (let (gnus-article-mime-handles)
471 ;; Double decode problem may happen. See mm-inline-message.
472 (run-hooks 'gnus-article-decode-hook
)
473 (gnus-article-prepare-display)
474 (setq handles gnus-article-mime-handles
))
476 (setq gnus-article-mime-handles
477 (mm-merge-handles gnus-article-mime-handles handles
))))
479 (goto-char (point-min)))
481 (defun mm-inline-message (handle)
484 (charset (mail-content-type-get
485 (mm-handle-type handle
) 'charset
))
486 gnus-displaying-mime handles
)
489 (setq charset
(intern (downcase charset
)))
490 (when (eq charset
'us-ascii
)
494 (narrow-to-region b b
)
495 (mm-insert-part handle
)
496 (let (gnus-article-mime-handles
497 ;; disable prepare hook
498 gnus-article-prepare-hook
499 (gnus-newsgroup-charset
500 (unless (eq charset
'gnus-decoded
) ;; mm-uu might set it.
501 (or charset gnus-newsgroup-charset
))))
502 (let ((gnus-original-article-buffer (mm-handle-buffer handle
)))
503 (run-hooks 'gnus-article-decode-hook
))
504 (gnus-article-prepare-display)
505 (setq handles gnus-article-mime-handles
))
506 (goto-char (point-min))
509 (goto-char (point-max))
512 (insert "----------\n\n")
514 (setq gnus-article-mime-handles
515 (mm-merge-handles gnus-article-mime-handles handles
)))
516 (mm-handle-set-undisplayer
519 (let (buffer-read-only)
520 (if (fboundp 'remove-specifier
)
521 ;; This is only valid on XEmacs.
522 (mapcar (lambda (prop)
524 (face-property 'default prop
) (current-buffer)))
525 '(background background-pixmap foreground
)))
526 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
528 (defun mm-display-inline-fontify (handle mode
)
530 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
531 ;; on for buffers whose name begins with " ". That's why we use
532 ;; save-current-buffer/get-buffer-create rather than
535 (set-buffer (generate-new-buffer "*fontification*"))
538 (buffer-disable-undo)
539 (mm-insert-part handle
)
541 (let ((font-lock-maximum-size nil
)
542 ;; Disable support modes, e.g., jit-lock, lazy-lock, etc.
543 (font-lock-mode-hook nil
)
544 (font-lock-support-mode nil
)
545 ;; I find font-lock a bit too verbose.
546 (font-lock-verbose nil
))
548 ;; The mode function might have already turned on font-lock.
549 (unless (symbol-value 'font-lock-mode
)
550 (font-lock-fontify-buffer)))
551 ;; By default, XEmacs font-lock uses non-duplicable text
552 ;; properties. This code forces all the text properties
553 ;; to be copied along with the text.
554 (when (fboundp 'extent-list
)
555 (map-extents (lambda (ext ignored
)
556 (set-extent-property ext
'duplicable t
)
558 nil nil nil nil nil
'text-prop
))
559 (setq text
(buffer-string)))
560 (kill-buffer (current-buffer))))
561 (mm-insert-inline handle text
)))
563 ;; Shouldn't these functions check whether the user even wants to use
564 ;; font-lock? At least under XEmacs, this fontification is pretty
565 ;; much unconditional. Also, it would be nice to change for the size
566 ;; of the fontified region.
568 (defun mm-display-patch-inline (handle)
569 (mm-display-inline-fontify handle
'diff-mode
))
571 (defun mm-display-elisp-inline (handle)
572 (mm-display-inline-fontify handle
'emacs-lisp-mode
))
574 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
575 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
576 (defvar mm-pkcs7-signed-magic
577 (mm-string-as-unibyte
579 (mapcar 'char-to-string
580 (list ?
\x30 ?
\x5c ?
\x28 ?
\x80 ?
\x5c ?
\x7c ?
\x81 ?
\x2e ?
\x5c
581 ?
\x7c ?
\x82 ?
\x2e ?
\x2e ?
\x5c ?
\x7c ?
\x83 ?
\x2e ?
\x2e
582 ?
\x2e ?
\x5c ?
\x29 ?
\x06 ?
\x09 ?
\x5c ?
\x2a ?
\x86 ?
\x48
583 ?
\x86 ?
\xf7 ?
\x0d ?
\x01 ?
\x07 ?
\x02)))))
585 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
586 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
587 (defvar mm-pkcs7-enveloped-magic
588 (mm-string-as-unibyte
590 (mapcar 'char-to-string
591 (list ?
\x30 ?
\x5c ?
\x28 ?
\x80 ?
\x5c ?
\x7c ?
\x81 ?
\x2e ?
\x5c
592 ?
\x7c ?
\x82 ?
\x2e ?
\x2e ?
\x5c ?
\x7c ?
\x83 ?
\x2e ?
\x2e
593 ?
\x2e ?
\x5c ?
\x29 ?
\x06 ?
\x09 ?
\x5c ?
\x2a ?
\x86 ?
\x48
594 ?
\x86 ?
\xf7 ?
\x0d ?
\x01 ?
\x07 ?
\x03)))))
596 (defun mm-view-pkcs7-get-type (handle)
597 (mm-with-unibyte-buffer
598 (mm-insert-part handle
)
599 (cond ((looking-at mm-pkcs7-enveloped-magic
)
601 ((looking-at mm-pkcs7-signed-magic
)
604 (error "Could not identify PKCS#7 type")))))
606 (defun mm-view-pkcs7 (handle)
607 (case (mm-view-pkcs7-get-type handle
)
608 (enveloped (mm-view-pkcs7-decrypt handle
))
609 (signed (mm-view-pkcs7-verify handle
))
610 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
612 (defun mm-view-pkcs7-verify (handle)
613 ;; A bogus implementation of PKCS#7. FIXME::
614 (mm-insert-part handle
)
615 (goto-char (point-min))
616 (if (search-forward "Content-Type: " nil t
)
617 (delete-region (point-min) (match-beginning 0)))
618 (goto-char (point-max))
619 (if (re-search-backward "--\r?\n?" nil t
)
620 (delete-region (match-end 0) (point-max)))
621 (goto-char (point-min))
622 (while (search-forward "\r\n" nil t
)
623 (replace-match "\n"))
624 (message "Verify signed PKCS#7 message is unimplemented.")
628 (autoload 'gnus-completing-read-maybe-default
"gnus-util" nil nil
'macro
)
630 (defun mm-view-pkcs7-decrypt (handle)
631 (insert-buffer-substring (mm-handle-buffer handle
))
632 (goto-char (point-min))
633 (insert "MIME-Version: 1.0\n")
634 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
635 (smime-decrypt-region
636 (point-min) (point-max)
637 (if (= (length smime-keys
) 1)
639 (smime-get-key-by-email
640 (gnus-completing-read-maybe-default
641 (concat "Decipher using key"
643 (concat " (default " (caar smime-keys
) "): ")
645 smime-keys nil nil nil nil
(car-safe (car-safe smime-keys
))))))
646 (goto-char (point-min))
647 (while (search-forward "\r\n" nil t
)
648 (replace-match "\n"))
649 (goto-char (point-min)))
653 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
654 ;;; mm-view.el ends here