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)))
41 ;;; Functions for displaying various formats inline
44 (defun mm-inline-image-emacs (handle)
45 (let ((b (point-marker))
48 (put-image (mm-get-image handle) b)
49 (mm-handle-set-undisplayer
51 `(lambda () (remove-images ,b (1+ ,b))))))
53 (defun mm-inline-image-xemacs (handle)
57 (annot (make-annotation (mm-get-image handle) nil 'text))
59 (mm-handle-set-undisplayer
62 (let (buffer-read-only)
63 (delete-annotation ,annot)
64 (delete-region ,(set-marker (make-marker) b)
65 ,(set-marker (make-marker) (point))))))
66 (set-extent-property annot 'mm t)
67 (set-extent-property annot 'duplicable t)))
70 (if (featurep 'xemacs)
71 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
72 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
74 (defvar mm-w3-setup nil)
82 (setq mm-w3-setup t)))
84 (defun mm-inline-text-html-render-with-w3 (handle)
86 (let ((text (mm-get-part handle))
88 (url-standalone-mode t)
89 (url-gateway-unplugged t)
90 (w3-honor-stylesheets nil)
91 (w3-delay-image-loads t)
93 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
94 (width (window-width))
95 (charset (mail-content-type-get
96 (mm-handle-type handle) 'charset)))
100 (narrow-to-region b (point))
101 (goto-char (point-min))
102 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
104 w3-meta-content-type-charset-regexp nil t))
105 (and (boundp 'w3-meta-charset-content-type-regexp)
107 w3-meta-charset-content-type-regexp nil t)))
109 (or (let ((bsubstr (buffer-substring-no-properties
112 (if (fboundp 'w3-coding-system-for-mime-charset)
113 (w3-coding-system-for-mime-charset bsubstr)
114 (mm-charset-to-coding-system bsubstr)))
116 (delete-region (point-min) (point-max))
117 (insert (mm-decode-string text charset))
118 (save-window-excursion
120 (let ((w3-strict-width width)
121 ;; Don't let w3 set the global version of
123 (fill-column fill-column))
125 (w3-region (point-min) (point-max))
127 (delete-region (point-min) (point-max))
129 (charset (mail-content-type-get
130 (mm-handle-type handle) 'charset)))
131 (if (or (eq charset 'gnus-decoded)
132 (eq mail-parse-charset 'gnus-decoded))
134 (narrow-to-region (point) (point))
135 (mm-insert-part handle)
136 (goto-char (point-max)))
137 (insert (mm-decode-string (mm-get-part handle)
140 "Error while rendering html; showing as text/plain"))))))
141 (mm-handle-set-undisplayer
144 (let (buffer-read-only)
145 (if (functionp 'remove-specifier)
146 (mapcar (lambda (prop)
148 (face-property 'default prop)
150 '(background background-pixmap foreground)))
151 (delete-region ,(point-min-marker)
152 ,(point-max-marker)))))))))
154 (defvar mm-w3m-mode-map nil
155 "Local keymap for inlined text/html part rendered by emacs-w3m. It will
156 be different from `w3m-mode-map' to use in the article buffer.")
158 (defvar mm-w3m-mode-command-alist
166 (w3m-antenna-add-current-url)
167 (w3m-bookmark-add-current-url)
168 (w3m-bookmark-add-this-url)
174 (w3m-edit-current-url)
178 (w3m-goto-url-new-session)
180 (w3m-history-restore-position)
181 (w3m-history-store-position)
184 (w3m-previous-buffer)
186 (w3m-redisplay-with-charset)
187 (w3m-reload-this-page)
188 (w3m-scroll-down-or-previous-url)
189 (w3m-scroll-up-or-next-url)
194 (w3m-view-parent-page)
195 (w3m-view-previous-page)
198 "Alist of commands to use for emacs-w3m in the article buffer. Each
199 element looks like (FROM-COMMAND . TO-COMMAND); FROM-COMMAND should be
200 registered in `w3m-mode-map' which will be substituted by TO-COMMAND
201 in `mm-w3m-mode-map'. If TO-COMMAND is nil, an article command key
202 will not be substituted.")
204 (defvar mm-w3m-mode-dont-bind-keys (list [up] [right] [left] [down])
205 "List of keys which should not be bound for the emacs-w3m commands.")
207 (defvar mm-w3m-setup nil
208 "Whether gnus-article-mode has been setup to use emacs-w3m.")
210 (defun mm-setup-w3m ()
211 "Setup gnus-article-mode to use emacs-w3m."
214 (unless mm-w3m-mode-map
215 (setq mm-w3m-mode-map (copy-keymap w3m-mode-map))
216 (dolist (def mm-w3m-mode-command-alist)
218 (substitute-key-definition (car def) (cdr def) mm-w3m-mode-map)
220 (dolist (key mm-w3m-mode-dont-bind-keys)
222 (define-key mm-w3m-mode-map key nil)
224 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
225 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
226 w3m-cid-retrieve-function-alist))
227 (setq mm-w3m-setup t)))
229 (defun mm-w3m-cid-retrieve (url &rest args)
230 "Insert a content pointed by URL if it has the cid: scheme."
231 (when (string-match "\\`cid:" url)
232 (setq url (concat "<" (substring url (match-end 0)) ">"))
234 (dolist (handle (with-current-buffer w3m-current-buffer
235 gnus-article-mime-handles))
236 (when (and (listp handle)
237 (equal url (mm-handle-id handle)))
238 (mm-insert-part handle)
239 (throw 'found-handle (mm-handle-media-type handle)))))))
241 (defun mm-inline-text-html-render-with-w3m (handle)
242 "Render a text/html part using emacs-w3m."
244 (let ((text (mm-get-part handle))
246 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
250 (narrow-to-region b (point))
251 (goto-char (point-min))
252 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
253 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
256 (delete-region (point-min) (point-max))
257 (insert (mm-decode-string text charset)))
258 (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
261 (w3m-display-inline-images mm-inline-text-html-with-images)
263 (w3m-region (point-min) (point-max)))
264 (when mm-inline-text-html-with-w3m-keymap
266 (point-min) (point-max)
267 (append '(mm-inline-text-html-with-w3m t)
268 (gnus-local-map-property mm-w3m-mode-map)))))
269 (mm-handle-set-undisplayer
272 (let (buffer-read-only)
273 (if (functionp 'remove-specifier)
274 (mapcar (lambda (prop)
276 (face-property 'default prop)
278 '(background background-pixmap foreground)))
279 (delete-region ,(point-min-marker)
280 ,(point-max-marker))))))))
282 (defun mm-inline-text (handle)
283 (let ((type (mm-handle-media-subtype handle))
287 (funcall mm-inline-text-html-renderer handle))
288 ((equal type "x-vcard")
293 (if (fboundp 'vcard-pretty-print)
294 (vcard-pretty-print (mm-get-part handle))
296 (vcard-parse-string (mm-get-part handle)
297 'vcard-standard-filter)))))))
300 (charset (mail-content-type-get
301 (mm-handle-type handle) 'charset)))
302 (if (or (eq charset 'gnus-decoded)
303 ;; This is probably not entirely correct, but
304 ;; makes rfc822 parts with embedded multiparts work.
305 (eq mail-parse-charset 'gnus-decoded))
307 (narrow-to-region (point) (point))
308 (mm-insert-part handle)
309 (goto-char (point-max)))
310 (insert (mm-decode-string (mm-get-part handle) charset)))
311 (when (and (equal type "plain")
312 (equal (cdr (assoc 'format (mm-handle-type handle)))
315 (narrow-to-region b (point))
318 (goto-char (point-max))))
320 (narrow-to-region b (point))
321 (set-text-properties (point-min) (point-max) nil)
322 (when (or (equal type "enriched")
323 (equal type "richtext"))
324 (enriched-decode (point-min) (point-max)))
325 (mm-handle-set-undisplayer
328 (let (buffer-read-only)
329 (delete-region ,(point-min-marker)
330 ,(point-max-marker)))))))))))
332 (defun mm-insert-inline (handle text)
333 "Insert TEXT inline from HANDLE."
336 (mm-handle-set-undisplayer
339 (let (buffer-read-only)
340 (delete-region ,(set-marker (make-marker) b)
341 ,(set-marker (make-marker) (point))))))))
343 (defun mm-inline-audio (handle)
344 (message "Not implemented"))
346 (defun mm-view-sound-file ()
347 (message "Not implemented"))
349 (defun mm-w3-prepare-buffer ()
351 (let ((url-standalone-mode t)
352 (url-gateway-unplugged t)
353 (w3-honor-stylesheets nil)
354 (w3-delay-image-loads t))
355 (w3-prepare-buffer)))
357 (defun mm-view-message ()
358 (mm-enable-multibyte)
360 (let (gnus-article-mime-handles)
361 ;; Double decode problem may happen. See mm-inline-message.
362 (run-hooks 'gnus-article-decode-hook)
363 (gnus-article-prepare-display)
364 (setq handles gnus-article-mime-handles))
366 (setq gnus-article-mime-handles
367 (mm-merge-handles gnus-article-mime-handles handles))))
369 (goto-char (point-min)))
371 (defun mm-inline-message (handle)
374 (charset (mail-content-type-get
375 (mm-handle-type handle) 'charset))
376 gnus-displaying-mime handles)
379 (setq charset (intern (downcase charset)))
380 (when (eq charset 'us-ascii)
384 (narrow-to-region b b)
385 (mm-insert-part handle)
386 (let (gnus-article-mime-handles
387 ;; disable prepare hook
388 gnus-article-prepare-hook
389 (gnus-newsgroup-charset
390 (or charset gnus-newsgroup-charset)))
391 (run-hooks 'gnus-article-decode-hook)
392 (gnus-article-prepare-display)
393 (setq handles gnus-article-mime-handles))
394 (goto-char (point-min))
397 (goto-char (point-max))
400 (insert "----------\n\n")
402 (setq gnus-article-mime-handles
403 (mm-merge-handles gnus-article-mime-handles handles)))
404 (mm-handle-set-undisplayer
407 (let (buffer-read-only)
408 (if (fboundp 'remove-specifier)
409 ;; This is only valid on XEmacs.
410 (mapcar (lambda (prop)
412 (face-property 'default prop) (current-buffer)))
413 '(background background-pixmap foreground)))
414 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
416 (defun mm-display-inline-fontify (handle mode)
418 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
419 ;; on for buffers whose name begins with " ". That's why we use
420 ;; save-current-buffer/get-buffer-create rather than
423 (set-buffer (generate-new-buffer "*fontification*"))
426 (buffer-disable-undo)
427 (mm-insert-part handle)
430 (let ((font-lock-verbose nil))
431 ;; I find font-lock a bit too verbose.
432 (font-lock-fontify-buffer))
433 ;; By default, XEmacs font-lock uses non-duplicable text
434 ;; properties. This code forces all the text properties
435 ;; to be copied along with the text.
436 (when (fboundp 'extent-list)
437 (map-extents (lambda (ext ignored)
438 (set-extent-property ext 'duplicable t)
440 nil nil nil nil nil 'text-prop))
441 (setq text (buffer-string)))
442 (kill-buffer (current-buffer))))
443 (mm-insert-inline handle text)))
445 ;; Shouldn't these functions check whether the user even wants to use
446 ;; font-lock? At least under XEmacs, this fontification is pretty
447 ;; much unconditional. Also, it would be nice to change for the size
448 ;; of the fontified region.
450 (defun mm-display-patch-inline (handle)
451 (mm-display-inline-fontify handle 'diff-mode))
453 (defun mm-display-elisp-inline (handle)
454 (mm-display-inline-fontify handle 'emacs-lisp-mode))
456 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
457 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
458 (defvar mm-pkcs7-signed-magic
459 (mm-string-as-unibyte
461 (mapcar 'char-to-string
462 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
463 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
464 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
465 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
467 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
468 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
469 (defvar mm-pkcs7-enveloped-magic
470 (mm-string-as-unibyte
472 (mapcar 'char-to-string
473 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
474 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
475 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
476 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
478 (defun mm-view-pkcs7-get-type (handle)
479 (mm-with-unibyte-buffer
480 (mm-insert-part handle)
481 (cond ((looking-at mm-pkcs7-enveloped-magic)
483 ((looking-at mm-pkcs7-signed-magic)
486 (error "Could not identify PKCS#7 type")))))
488 (defun mm-view-pkcs7 (handle)
489 (case (mm-view-pkcs7-get-type handle)
490 (enveloped (mm-view-pkcs7-decrypt handle))
491 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
493 (defun mm-view-pkcs7-decrypt (handle)
494 (insert-buffer (mm-handle-buffer handle))
495 (goto-char (point-min))
496 (insert "MIME-Version: 1.0\n")
497 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
498 (smime-decrypt-region
499 (point-min) (point-max)
500 (if (= (length smime-keys) 1)
502 (smime-get-key-by-email
503 (completing-read "Decrypt this part with which key? "
505 (and (listp (car-safe smime-keys))
506 (caar smime-keys)))))))
510 ;;; mm-view.el ends here