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
43 (defun mm-inline-image-emacs (handle)
44 (let ((b (point-marker))
47 (put-image (mm-get-image handle) b)
48 (mm-handle-set-undisplayer
50 `(lambda () (remove-images ,b (1+ ,b))))))
52 (defun mm-inline-image-xemacs (handle)
56 (annot (make-annotation (mm-get-image handle) nil 'text))
58 (mm-handle-set-undisplayer
61 (let (buffer-read-only)
62 (delete-annotation ,annot)
63 (delete-region ,(set-marker (make-marker) b)
64 ,(set-marker (make-marker) (point))))))
65 (set-extent-property annot 'mm t)
66 (set-extent-property annot 'duplicable t)))
69 (if (featurep 'xemacs)
70 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
71 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
73 (defvar mm-w3-setup nil)
81 (setq mm-w3-setup t)))
83 (defun mm-inline-text (handle)
84 (let ((type (mm-handle-media-subtype handle))
85 text buffer-read-only)
89 (setq text (mm-get-part handle))
91 (url-standalone-mode 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)
124 (url-standalone-mode t))
126 (w3-region (point-min) (point-max))
128 (delete-region (point-min) (point-max))
130 (charset (mail-content-type-get
131 (mm-handle-type handle) 'charset)))
132 (if (or (eq charset 'gnus-decoded)
133 (eq mail-parse-charset 'gnus-decoded))
135 (narrow-to-region (point) (point))
136 (mm-insert-part handle)
137 (goto-char (point-max)))
138 (insert (mm-decode-string (mm-get-part handle)
141 "Error while rendering html; showing as text/plain"))))))
142 (mm-handle-set-undisplayer
145 (let (buffer-read-only)
146 (if (functionp 'remove-specifier)
147 (mapcar (lambda (prop)
149 (face-property 'default prop)
151 '(background background-pixmap foreground)))
152 (delete-region ,(point-min-marker)
153 ,(point-max-marker)))))))))
154 ((or (equal type "enriched")
155 (equal type "richtext"))
157 (mm-with-unibyte-buffer
158 (mm-insert-part handle)
159 (save-window-excursion
160 (enriched-decode (point-min) (point-max))
161 (setq text (buffer-string)))))
162 (mm-insert-inline handle text))
163 ((equal type "x-vcard")
167 (if (fboundp 'vcard-pretty-print)
168 (vcard-pretty-print (mm-get-part handle))
170 (vcard-parse-string (mm-get-part handle)
171 'vcard-standard-filter))))))
174 (charset (mail-content-type-get
175 (mm-handle-type handle) 'charset)))
176 (if (or (eq charset 'gnus-decoded)
177 ;; This is probably not entirely correct, but
178 ;; makes rfc822 parts with embedded multiparts work.
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) charset)))
185 (when (and (equal type "plain")
186 (equal (cdr (assoc 'format (mm-handle-type handle)))
189 (narrow-to-region b (point))
192 (goto-char (point-max))))
194 (narrow-to-region b (point))
195 (set-text-properties (point-min) (point-max) nil)
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-prepare-buffer)))
225 (defun mm-view-message ()
226 (mm-enable-multibyte)
228 (let (gnus-article-mime-handles)
229 ;; Double decode problem may happen. See mm-inline-message.
230 (run-hooks 'gnus-article-decode-hook)
231 (gnus-article-prepare-display)
232 (setq handles gnus-article-mime-handles))
234 (setq gnus-article-mime-handles
235 (mm-merge-handles gnus-article-mime-handles handles))))
237 (goto-char (point-min)))
239 (defun mm-inline-message (handle)
242 (charset (mail-content-type-get
243 (mm-handle-type handle) 'charset))
244 gnus-displaying-mime handles)
247 (setq charset (intern (downcase charset)))
248 (when (eq charset 'us-ascii)
252 (narrow-to-region b b)
253 (mm-insert-part handle)
254 (let (gnus-article-mime-handles
255 ;; disable prepare hook
256 gnus-article-prepare-hook
257 (gnus-newsgroup-charset
258 (or charset gnus-newsgroup-charset)))
259 (run-hooks 'gnus-article-decode-hook)
260 (gnus-article-prepare-display)
261 (setq handles gnus-article-mime-handles))
262 (goto-char (point-min))
265 (goto-char (point-max))
268 (insert "----------\n\n")
270 (setq gnus-article-mime-handles
271 (mm-merge-handles gnus-article-mime-handles handles)))
272 (mm-handle-set-undisplayer
275 (let (buffer-read-only)
276 (if (fboundp 'remove-specifier)
277 ;; This is only valid on XEmacs.
278 (mapcar (lambda (prop)
280 (face-property 'default prop) (current-buffer)))
281 '(background background-pixmap foreground)))
282 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
284 (defun mm-display-inline-fontify (handle mode)
286 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
287 ;; on for buffers whose name begins with " ". That's why we use
288 ;; save-current-buffer/get-buffer-create rather than
291 (set-buffer (generate-new-buffer "*fontification*"))
294 (buffer-disable-undo)
295 (mm-insert-part handle)
297 (let ((font-lock-verbose nil))
298 ;; I find font-lock a bit too verbose.
299 (font-lock-fontify-buffer))
300 ;; By default, XEmacs font-lock uses non-duplicable text
301 ;; properties. This code forces all the text properties
302 ;; to be copied along with the text.
303 (when (fboundp 'extent-list)
304 (map-extents (lambda (ext ignored)
305 (set-extent-property ext 'duplicable t)
307 nil nil nil nil nil 'text-prop))
308 (setq text (buffer-string)))
309 (kill-buffer (current-buffer))))
310 (mm-insert-inline handle text)))
312 ;; Shouldn't these functions check whether the user even wants to use
313 ;; font-lock? At least under XEmacs, this fontification is pretty
314 ;; much unconditional. Also, it would be nice to change for the size
315 ;; of the fontified region.
317 (defun mm-display-patch-inline (handle)
318 (mm-display-inline-fontify handle 'diff-mode))
320 (defun mm-display-elisp-inline (handle)
321 (mm-display-inline-fontify handle 'emacs-lisp-mode))
323 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
324 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
325 (defvar mm-pkcs7-signed-magic
326 (mm-string-as-unibyte
328 (mapcar 'char-to-string
329 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
330 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
331 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
332 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
334 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
335 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
336 (defvar mm-pkcs7-enveloped-magic
337 (mm-string-as-unibyte
339 (mapcar 'char-to-string
340 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
341 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
342 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
343 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
345 (defun mm-view-pkcs7-get-type (handle)
346 (mm-with-unibyte-buffer
347 (mm-insert-part handle)
348 (cond ((looking-at mm-pkcs7-enveloped-magic)
350 ((looking-at mm-pkcs7-signed-magic)
353 (error "Could not identify PKCS#7 type")))))
355 (defun mm-view-pkcs7 (handle)
356 (case (mm-view-pkcs7-get-type handle)
357 (enveloped (mm-view-pkcs7-decrypt handle))
358 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
360 (defun mm-view-pkcs7-decrypt (handle)
362 ((eq mm-decrypt-option 'never) nil)
363 ((eq mm-decrypt-option 'always) t)
364 ((eq mm-decrypt-option 'known) t)
366 (format "Decrypt (S/MIME) part? "))))
369 (insert-buffer (mm-handle-buffer handle))
370 (goto-char (point-min))
371 (insert "MIME-Version: 1.0\n")
372 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
373 (smime-decrypt-region
374 (point-min) (point-max)
375 (if (= (length smime-keys) 1)
377 (smime-get-key-by-email
378 (completing-read "Decrypt this part with which key? "
380 (and (listp (car-safe smime-keys))
381 (caar smime-keys))))))
382 (setq res (buffer-string)))
383 (mm-insert-inline handle res))))
387 ;;; mm-view.el ends here