1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'cl))
33 (autoload 'gnus-article-prepare-display "gnus-art")
34 (autoload 'vcard-parse-string "vcard")
35 (autoload 'vcard-format-string "vcard")
36 (autoload 'fill-flowed "flow-fill")
37 (unless (fboundp 'diff-mode)
38 (autoload 'diff-mode "diff-mode" "" t nil)))
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 (links mm-inline-render-with-file
44 mm-links-remove-leading-blank
46 (lynx mm-inline-render-with-stdin nil
47 "lynx" "-dump" "-force_html" "-stdin"))
48 "The attributes of renderer types for text/html.")
50 (defvar mm-text-html-washer-alist
51 '((w3 . gnus-article-wash-html-with-w3)
52 (w3m . gnus-article-wash-html-with-w3m)
53 (links mm-inline-wash-with-file
54 mm-links-remove-leading-blank
56 (lynx mm-inline-wash-with-stdin nil
57 "lynx" "-dump" "-force_html" "-stdin"))
58 "The attributes of washer types for text/html.")
60 (defvar mm-application-msword-renderer-alist
61 '((catdoc mm-inline-render-with-stdin nil "catdoc"))
62 "The attributes of renderer types.")
64 ;;; Internal variables.
67 ;;; Functions for displaying various formats inline
70 (defun mm-inline-image-emacs (handle)
71 (let ((b (point-marker))
74 (put-image (mm-get-image handle) b)
75 (mm-handle-set-undisplayer
77 `(lambda () (remove-images ,b (1+ ,b))))))
79 (defun mm-inline-image-xemacs (handle)
83 (annot (make-annotation (mm-get-image handle) nil 'text))
85 (mm-handle-set-undisplayer
88 (let (buffer-read-only)
89 (delete-annotation ,annot)
90 (delete-region ,(set-marker (make-marker) b)
91 ,(set-marker (make-marker) (point))))))
92 (set-extent-property annot 'mm t)
93 (set-extent-property annot 'duplicable t)))
96 (if (featurep 'xemacs)
97 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
98 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
100 (defvar mm-w3-setup nil)
101 (defun mm-setup-w3 ()
108 (setq mm-w3-setup t)))
110 (defun mm-inline-text-html-render-with-w3 (handle)
112 (let ((text (mm-get-part handle))
114 (url-standalone-mode t)
115 (url-gateway-unplugged t)
116 (w3-honor-stylesheets nil)
117 (w3-delay-image-loads t)
119 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
120 (width (window-width))
121 (charset (mail-content-type-get
122 (mm-handle-type handle) 'charset)))
126 (narrow-to-region b (point))
127 (goto-char (point-min))
128 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
130 w3-meta-content-type-charset-regexp nil t))
131 (and (boundp 'w3-meta-charset-content-type-regexp)
133 w3-meta-charset-content-type-regexp nil t)))
135 (or (let ((bsubstr (buffer-substring-no-properties
138 (if (fboundp 'w3-coding-system-for-mime-charset)
139 (w3-coding-system-for-mime-charset bsubstr)
140 (mm-charset-to-coding-system bsubstr)))
142 (delete-region (point-min) (point-max))
143 (insert (mm-decode-string text charset))
144 (save-window-excursion
146 (let ((w3-strict-width width)
147 ;; Don't let w3 set the global version of
149 (fill-column fill-column))
151 (w3-region (point-min) (point-max))
153 (delete-region (point-min) (point-max))
155 (charset (mail-content-type-get
156 (mm-handle-type handle) 'charset)))
157 (if (or (eq charset 'gnus-decoded)
158 (eq mail-parse-charset 'gnus-decoded))
160 (narrow-to-region (point) (point))
161 (mm-insert-part handle)
162 (goto-char (point-max)))
163 (insert (mm-decode-string (mm-get-part handle)
166 "Error while rendering html; showing as text/plain"))))))
167 (mm-handle-set-undisplayer
170 (let (buffer-read-only)
171 (if (functionp 'remove-specifier)
172 (mapcar (lambda (prop)
174 (face-property 'default prop)
176 '(background background-pixmap foreground)))
177 (delete-region ,(point-min-marker)
178 ,(point-max-marker)))))))))
180 (defvar mm-w3m-mode-map nil
181 "Local keymap for inlined text/html part rendered by emacs-w3m. It will
182 be different from `w3m-mode-map' to use in the article buffer.")
184 (defvar mm-w3m-mode-command-alist
192 (w3m-antenna-add-current-url)
193 (w3m-bookmark-add-current-url)
194 (w3m-bookmark-add-this-url)
200 (w3m-edit-current-url)
204 (w3m-goto-url-new-session)
206 (w3m-history-restore-position)
207 (w3m-history-store-position)
210 (w3m-previous-buffer)
212 (w3m-redisplay-with-charset)
213 (w3m-reload-this-page)
214 (w3m-scroll-down-or-previous-url)
215 (w3m-scroll-up-or-next-url)
220 (w3m-view-parent-page)
221 (w3m-view-previous-page)
224 "Alist of commands to use for emacs-w3m in the article buffer. Each
225 element looks like (FROM-COMMAND . TO-COMMAND); FROM-COMMAND should be
226 registered in `w3m-mode-map' which will be substituted by TO-COMMAND
227 in `mm-w3m-mode-map'. If TO-COMMAND is nil, an article command key
228 will not be substituted.")
230 (defvar mm-w3m-mode-dont-bind-keys (list [up] [right] [left] [down])
231 "List of keys which should not be bound for the emacs-w3m commands.")
233 (defvar mm-w3m-setup nil
234 "Whether gnus-article-mode has been setup to use emacs-w3m.")
236 (defun mm-setup-w3m ()
237 "Setup gnus-article-mode to use emacs-w3m."
240 (unless mm-w3m-mode-map
241 (setq mm-w3m-mode-map (copy-keymap w3m-mode-map))
242 (dolist (def mm-w3m-mode-command-alist)
244 (substitute-key-definition (car def) (cdr def) mm-w3m-mode-map)
246 (dolist (key mm-w3m-mode-dont-bind-keys)
248 (define-key mm-w3m-mode-map key nil)
250 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
251 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
252 w3m-cid-retrieve-function-alist))
253 (setq mm-w3m-setup t)))
255 (defun mm-w3m-cid-retrieve (url &rest args)
256 "Insert a content pointed by URL if it has the cid: scheme."
257 (when (string-match "\\`cid:" url)
258 (setq url (concat "<" (substring url (match-end 0)) ">"))
260 (dolist (handle (with-current-buffer w3m-current-buffer
261 gnus-article-mime-handles))
262 (when (and (listp handle)
263 (equal url (mm-handle-id handle)))
264 (mm-insert-part handle)
265 (throw 'found-handle (mm-handle-media-type handle)))))))
267 (defun mm-inline-text-html-render-with-w3m (handle)
268 "Render a text/html part using emacs-w3m."
270 (let ((text (mm-get-part handle))
272 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
276 (narrow-to-region b (point))
277 (goto-char (point-min))
278 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
279 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
282 (delete-region (point-min) (point-max))
283 (insert (mm-decode-string text charset)))
284 (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
287 (w3m-display-inline-images mm-inline-text-html-with-images)
289 (w3m-region (point-min) (point-max)))
290 (when mm-inline-text-html-with-w3m-keymap
292 (point-min) (point-max)
293 (append '(mm-inline-text-html-with-w3m t)
294 (gnus-local-map-property mm-w3m-mode-map)))))
295 (mm-handle-set-undisplayer
298 (let (buffer-read-only)
299 (if (functionp 'remove-specifier)
300 (mapcar (lambda (prop)
302 (face-property 'default prop)
304 '(background background-pixmap foreground)))
305 (delete-region ,(point-min-marker)
306 ,(point-max-marker))))))))
308 (defun mm-links-remove-leading-blank ()
309 ;; Delete the annoying three spaces preceding each line of links
311 (goto-char (point-min))
312 (while (re-search-forward "^ " nil t)
313 (delete-region (match-beginning 0) (match-end 0))))
315 (defun mm-inline-wash-with-file (post-func cmd &rest args)
316 (let ((file (make-temp-name
317 (expand-file-name "mm" mm-tmp-directory))))
318 (let ((coding-system-for-write 'binary))
319 (write-region (point-min) (point-max) file nil 'silent))
320 (delete-region (point-min) (point-max))
322 (apply 'call-process cmd nil t nil (mapcar 'eval args))
324 (and post-func (funcall post-func))))
326 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
327 (let ((coding-system-for-write 'binary))
328 (apply 'call-process-region (point-min) (point-max)
330 (and post-func (funcall post-func)))
332 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
333 (let ((source (mm-get-part handle)))
336 (mm-with-unibyte-buffer
338 (apply 'mm-inline-wash-with-file post-func cmd args)
341 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
342 (let ((source (mm-get-part handle)))
345 (mm-with-unibyte-buffer
347 (apply 'mm-inline-wash-with-stdin post-func cmd args)
350 (defun mm-inline-render-with-function (handle func &rest args)
351 (let ((source (mm-get-part handle)))
354 (mm-with-unibyte-buffer
359 (defun mm-inline-text-html (handle)
360 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
361 (entry (assq func mm-text-html-renderer-alist))
364 (setq func (cdr entry)))
366 ((gnus-functionp func)
367 (funcall func handle))
369 (apply (car func) handle (cdr func))))))
371 (defun mm-inline-text-vcard (handle)
372 (let (buffer-read-only)
377 (if (fboundp 'vcard-pretty-print)
378 (vcard-pretty-print (mm-get-part handle))
380 (vcard-parse-string (mm-get-part handle)
381 'vcard-standard-filter))))))))
383 (defun mm-inline-text (handle)
385 (type (mm-handle-media-subtype handle))
386 (charset (mail-content-type-get
387 (mm-handle-type handle) 'charset))
389 (if (or (eq charset 'gnus-decoded)
390 ;; This is probably not entirely correct, but
391 ;; makes rfc822 parts with embedded multiparts work.
392 (eq mail-parse-charset 'gnus-decoded))
394 (narrow-to-region (point) (point))
395 (mm-insert-part handle)
396 (goto-char (point-max)))
397 (insert (mm-decode-string (mm-get-part handle) charset)))
398 (when (and (equal type "plain")
399 (equal (cdr (assoc 'format (mm-handle-type handle)))
402 (narrow-to-region b (point))
405 (goto-char (point-max))))
407 (narrow-to-region b (point))
408 (set-text-properties (point-min) (point-max) nil)
409 (when (or (equal type "enriched")
410 (equal type "richtext"))
411 (enriched-decode (point-min) (point-max)))
412 (mm-handle-set-undisplayer
415 (let (buffer-read-only)
416 (delete-region ,(point-min-marker)
417 ,(point-max-marker))))))))
419 (defun mm-insert-inline (handle text)
420 "Insert TEXT inline from HANDLE."
423 (mm-handle-set-undisplayer
426 (let (buffer-read-only)
427 (delete-region ,(set-marker (make-marker) b)
428 ,(set-marker (make-marker) (point))))))))
430 (defun mm-inline-audio (handle)
431 (message "Not implemented"))
433 (defun mm-view-sound-file ()
434 (message "Not implemented"))
436 (defun mm-w3-prepare-buffer ()
438 (let ((url-standalone-mode t)
439 (url-gateway-unplugged t)
440 (w3-honor-stylesheets nil)
441 (w3-delay-image-loads t))
442 (w3-prepare-buffer)))
444 (defun mm-view-message ()
445 (mm-enable-multibyte)
447 (let (gnus-article-mime-handles)
448 ;; Double decode problem may happen. See mm-inline-message.
449 (run-hooks 'gnus-article-decode-hook)
450 (gnus-article-prepare-display)
451 (setq handles gnus-article-mime-handles))
453 (setq gnus-article-mime-handles
454 (mm-merge-handles gnus-article-mime-handles handles))))
456 (goto-char (point-min)))
458 (defun mm-inline-message (handle)
461 (charset (mail-content-type-get
462 (mm-handle-type handle) 'charset))
463 gnus-displaying-mime handles)
466 (setq charset (intern (downcase charset)))
467 (when (eq charset 'us-ascii)
471 (narrow-to-region b b)
472 (mm-insert-part handle)
473 (let (gnus-article-mime-handles
474 ;; disable prepare hook
475 gnus-article-prepare-hook
476 (gnus-newsgroup-charset
477 (or charset gnus-newsgroup-charset)))
478 (run-hooks 'gnus-article-decode-hook)
479 (gnus-article-prepare-display)
480 (setq handles gnus-article-mime-handles))
481 (goto-char (point-min))
484 (goto-char (point-max))
487 (insert "----------\n\n")
489 (setq gnus-article-mime-handles
490 (mm-merge-handles gnus-article-mime-handles handles)))
491 (mm-handle-set-undisplayer
494 (let (buffer-read-only)
495 (if (fboundp 'remove-specifier)
496 ;; This is only valid on XEmacs.
497 (mapcar (lambda (prop)
499 (face-property 'default prop) (current-buffer)))
500 '(background background-pixmap foreground)))
501 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
503 (defun mm-display-inline-fontify (handle mode)
505 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
506 ;; on for buffers whose name begins with " ". That's why we use
507 ;; save-current-buffer/get-buffer-create rather than
510 (set-buffer (generate-new-buffer "*fontification*"))
513 (buffer-disable-undo)
514 (mm-insert-part handle)
517 (let ((font-lock-verbose nil))
518 ;; I find font-lock a bit too verbose.
519 (font-lock-fontify-buffer))
520 ;; By default, XEmacs font-lock uses non-duplicable text
521 ;; properties. This code forces all the text properties
522 ;; to be copied along with the text.
523 (when (fboundp 'extent-list)
524 (map-extents (lambda (ext ignored)
525 (set-extent-property ext 'duplicable t)
527 nil nil nil nil nil 'text-prop))
528 (setq text (buffer-string)))
529 (kill-buffer (current-buffer))))
530 (mm-insert-inline handle text)))
532 ;; Shouldn't these functions check whether the user even wants to use
533 ;; font-lock? At least under XEmacs, this fontification is pretty
534 ;; much unconditional. Also, it would be nice to change for the size
535 ;; of the fontified region.
537 (defun mm-display-patch-inline (handle)
538 (mm-display-inline-fontify handle 'diff-mode))
540 (defun mm-display-elisp-inline (handle)
541 (mm-display-inline-fontify handle 'emacs-lisp-mode))
543 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
544 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
545 (defvar mm-pkcs7-signed-magic
546 (mm-string-as-unibyte
548 (mapcar 'char-to-string
549 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
550 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
551 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
552 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
554 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
555 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
556 (defvar mm-pkcs7-enveloped-magic
557 (mm-string-as-unibyte
559 (mapcar 'char-to-string
560 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
561 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
562 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
563 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
565 (defun mm-view-pkcs7-get-type (handle)
566 (mm-with-unibyte-buffer
567 (mm-insert-part handle)
568 (cond ((looking-at mm-pkcs7-enveloped-magic)
570 ((looking-at mm-pkcs7-signed-magic)
573 (error "Could not identify PKCS#7 type")))))
575 (defun mm-view-pkcs7 (handle)
576 (case (mm-view-pkcs7-get-type handle)
577 (enveloped (mm-view-pkcs7-decrypt handle))
578 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
580 (defun mm-view-pkcs7-decrypt (handle)
581 (insert-buffer (mm-handle-buffer handle))
582 (goto-char (point-min))
583 (insert "MIME-Version: 1.0\n")
584 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
585 (smime-decrypt-region
586 (point-min) (point-max)
587 (if (= (length smime-keys) 1)
589 (smime-get-key-by-email
590 (completing-read "Decrypt this part with which key? "
592 (and (listp (car-safe smime-keys))
593 (caar smime-keys)))))))
595 (defun mm-inline-application-msword (handle)
596 (let* ((func mm-application-msword-renderer)
597 (entry (assq func mm-application-msword-renderer-alist))
600 (setq func (cdr entry)))
602 ((gnus-functionp func)
603 (funcall func handle))
605 (apply (car func) handle (cdr func))))))
609 ;;; mm-view.el ends here