1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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 (handle)
85 (let ((type (mm-handle-media-subtype handle))
86 text buffer-read-only)
90 (setq text (mm-get-part handle))
92 (url-standalone-mode t)
93 (w3-honor-stylesheets nil)
94 (w3-delay-image-loads t)
96 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
97 (width (window-width))
98 (charset (mail-content-type-get
99 (mm-handle-type handle) 'charset)))
103 (narrow-to-region b (point))
104 (goto-char (point-min))
105 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
107 w3-meta-content-type-charset-regexp nil t))
108 (and (boundp 'w3-meta-charset-content-type-regexp)
110 w3-meta-charset-content-type-regexp nil t)))
112 (or (let ((bsubstr (buffer-substring-no-properties
115 (if (fboundp 'w3-coding-system-for-mime-charset)
116 (w3-coding-system-for-mime-charset bsubstr)
117 (mm-charset-to-coding-system bsubstr)))
119 (delete-region (point-min) (point-max))
120 (insert (mm-decode-string text charset))
121 (save-window-excursion
123 (let ((w3-strict-width width)
124 ;; Don't let w3 set the global version of
126 (fill-column fill-column)
127 (w3-honor-stylesheets nil)
128 (w3-delay-image-loads t)
129 (url-standalone-mode t))
131 (w3-region (point-min) (point-max))
133 (delete-region (point-min) (point-max))
135 (charset (mail-content-type-get
136 (mm-handle-type handle) 'charset)))
137 (if (or (eq charset 'gnus-decoded)
138 (eq mail-parse-charset 'gnus-decoded))
140 (narrow-to-region (point) (point))
141 (mm-insert-part handle)
142 (goto-char (point-max)))
143 (insert (mm-decode-string (mm-get-part handle)
146 "Error while rendering html; showing as text/plain"))))))
147 (mm-handle-set-undisplayer
150 (let (buffer-read-only)
151 (if (functionp 'remove-specifier)
152 (mapcar (lambda (prop)
154 (face-property 'default prop)
156 '(background background-pixmap foreground)))
157 (delete-region ,(point-min-marker)
158 ,(point-max-marker)))))))))
159 ((equal type "x-vcard")
164 (if (fboundp 'vcard-pretty-print)
165 (vcard-pretty-print (mm-get-part handle))
167 (vcard-parse-string (mm-get-part handle)
168 'vcard-standard-filter)))))))
171 (charset (mail-content-type-get
172 (mm-handle-type handle) 'charset)))
173 (if (or (eq charset 'gnus-decoded)
174 ;; This is probably not entirely correct, but
175 ;; makes rfc822 parts with embedded multiparts work.
176 (eq mail-parse-charset 'gnus-decoded))
178 (narrow-to-region (point) (point))
179 (mm-insert-part handle)
180 (goto-char (point-max)))
181 (insert (mm-decode-string (mm-get-part handle) charset)))
182 (when (and (equal type "plain")
183 (equal (cdr (assoc 'format (mm-handle-type handle)))
186 (narrow-to-region b (point))
189 (goto-char (point-max))))
191 (narrow-to-region b (point))
192 (set-text-properties (point-min) (point-max) nil)
193 (when (or (equal type "enriched")
194 (equal type "richtext"))
195 (enriched-decode (point-min) (point-max)))
196 (mm-handle-set-undisplayer
199 (let (buffer-read-only)
200 (delete-region ,(point-min-marker)
201 ,(point-max-marker)))))))))))
203 (defun mm-insert-inline (handle text)
204 "Insert TEXT inline from HANDLE."
207 (mm-handle-set-undisplayer
210 (let (buffer-read-only)
211 (delete-region ,(set-marker (make-marker) b)
212 ,(set-marker (make-marker) (point))))))))
214 (defun mm-inline-audio (handle)
215 (message "Not implemented"))
217 (defun mm-view-sound-file ()
218 (message "Not implemented"))
220 (defun mm-w3-prepare-buffer ()
222 (let ((url-standalone-mode t)
223 (w3-honor-stylesheets nil)
224 (w3-delay-image-loads t))
225 (w3-prepare-buffer)))
227 (defun mm-view-message ()
228 (mm-enable-multibyte)
230 (let (gnus-article-mime-handles)
231 ;; Double decode problem may happen. See mm-inline-message.
232 (run-hooks 'gnus-article-decode-hook)
233 (gnus-article-prepare-display)
234 (setq handles gnus-article-mime-handles))
236 (setq gnus-article-mime-handles
237 (mm-merge-handles gnus-article-mime-handles handles))))
239 (goto-char (point-min)))
241 (defun mm-inline-message (handle)
244 (charset (mail-content-type-get
245 (mm-handle-type handle) 'charset))
246 gnus-displaying-mime handles)
249 (setq charset (intern (downcase charset)))
250 (when (eq charset 'us-ascii)
254 (narrow-to-region b b)
255 (mm-insert-part handle)
256 (let (gnus-article-mime-handles
257 ;; disable prepare hook
258 gnus-article-prepare-hook
259 (gnus-newsgroup-charset
260 (or charset gnus-newsgroup-charset)))
261 (run-hooks 'gnus-article-decode-hook)
262 (gnus-article-prepare-display)
263 (setq handles gnus-article-mime-handles))
264 (goto-char (point-min))
267 (goto-char (point-max))
270 (insert "----------\n\n")
272 (setq gnus-article-mime-handles
273 (mm-merge-handles gnus-article-mime-handles handles)))
274 (mm-handle-set-undisplayer
277 (let (buffer-read-only)
278 (if (fboundp 'remove-specifier)
279 ;; This is only valid on XEmacs.
280 (mapcar (lambda (prop)
282 (face-property 'default prop) (current-buffer)))
283 '(background background-pixmap foreground)))
284 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
286 (defun mm-display-inline-fontify (handle mode)
288 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
289 ;; on for buffers whose name begins with " ". That's why we use
290 ;; save-current-buffer/get-buffer-create rather than
293 (set-buffer (generate-new-buffer "*fontification*"))
296 (buffer-disable-undo)
297 (mm-insert-part handle)
299 (let ((font-lock-verbose nil))
300 ;; I find font-lock a bit too verbose.
301 (font-lock-fontify-buffer))
302 ;; By default, XEmacs font-lock uses non-duplicable text
303 ;; properties. This code forces all the text properties
304 ;; to be copied along with the text.
305 (when (fboundp 'extent-list)
306 (map-extents (lambda (ext ignored)
307 (set-extent-property ext 'duplicable t)
309 nil nil nil nil nil 'text-prop))
310 (setq text (buffer-string)))
311 (kill-buffer (current-buffer))))
312 (mm-insert-inline handle text)))
314 ;; Shouldn't these functions check whether the user even wants to use
315 ;; font-lock? At least under XEmacs, this fontification is pretty
316 ;; much unconditional. Also, it would be nice to change for the size
317 ;; of the fontified region.
319 (defun mm-display-patch-inline (handle)
320 (mm-display-inline-fontify handle 'diff-mode))
322 (defun mm-display-elisp-inline (handle)
323 (mm-display-inline-fontify handle 'emacs-lisp-mode))
325 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
326 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
327 (defvar mm-pkcs7-signed-magic
328 (mm-string-as-unibyte
330 (mapcar 'char-to-string
331 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
332 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
333 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
334 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
336 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
337 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
338 (defvar mm-pkcs7-enveloped-magic
339 (mm-string-as-unibyte
341 (mapcar 'char-to-string
342 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
343 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
344 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
345 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
347 (defun mm-view-pkcs7-get-type (handle)
348 (mm-with-unibyte-buffer
349 (mm-insert-part handle)
350 (cond ((looking-at mm-pkcs7-enveloped-magic)
352 ((looking-at mm-pkcs7-signed-magic)
355 (error "Could not identify PKCS#7 type")))))
357 (defun mm-view-pkcs7 (handle)
358 (case (mm-view-pkcs7-get-type handle)
359 (enveloped (mm-view-pkcs7-decrypt handle))
360 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
362 (defun mm-view-pkcs7-decrypt (handle)
363 (insert-buffer (mm-handle-buffer handle))
364 (goto-char (point-min))
365 (insert "MIME-Version: 1.0\n")
366 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
367 (smime-decrypt-region
368 (point-min) (point-max)
369 (if (= (length smime-keys) 1)
371 (smime-get-key-by-email
372 (completing-read "Decrypt this part with which key? "
374 (and (listp (car-safe smime-keys))
375 (caar smime-keys)))))))
379 ;;; mm-view.el ends here