]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-view.el
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-628
[gnu-emacs] / lisp / gnus / mm-view.el
1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'mail-parse)
29 (require 'mailcap)
30 (require 'mm-bodies)
31 (require 'mm-decode)
32
33 (eval-and-compile
34 (autoload 'gnus-article-prepare-display "gnus-art")
35 (autoload 'vcard-parse-string "vcard")
36 (autoload 'vcard-format-string "vcard")
37 (autoload 'fill-flowed "flow-fill")
38 (autoload 'html2text "html2text")
39 (unless (fboundp 'diff-mode)
40 (autoload 'diff-mode "diff-mode" "" t nil)))
41
42 (defvar mm-text-html-renderer-alist
43 '((w3 . mm-inline-text-html-render-with-w3)
44 (w3m . mm-inline-text-html-render-with-w3m)
45 (w3m-standalone mm-inline-render-with-stdin nil
46 "w3m" "-dump" "-T" "text/html")
47 (links mm-inline-render-with-file
48 mm-links-remove-leading-blank
49 "links" "-dump" file)
50 (lynx mm-inline-render-with-stdin nil
51 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
52 (html2text mm-inline-render-with-function html2text))
53 "The attributes of renderer types for text/html.")
54
55 (defvar mm-text-html-washer-alist
56 '((w3 . gnus-article-wash-html-with-w3)
57 (w3m . gnus-article-wash-html-with-w3m)
58 (w3m-standalone mm-inline-wash-with-stdin nil
59 "w3m" "-dump" "-T" "text/html")
60 (links mm-inline-wash-with-file
61 mm-links-remove-leading-blank
62 "links" "-dump" file)
63 (lynx mm-inline-wash-with-stdin nil
64 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
65 (html2text html2text))
66 "The attributes of washer types for text/html.")
67
68 ;;; Internal variables.
69
70 ;;;
71 ;;; Functions for displaying various formats inline
72 ;;;
73
74 (defun mm-inline-image-emacs (handle)
75 (let ((b (point-marker))
76 buffer-read-only)
77 (put-image (mm-get-image handle) b)
78 (insert "\n\n")
79 (mm-handle-set-undisplayer
80 handle
81 `(lambda ()
82 (let ((b ,b)
83 buffer-read-only)
84 (remove-images b b)
85 (delete-region b (+ b 2)))))))
86
87 (defun mm-inline-image-xemacs (handle)
88 (insert "\n\n")
89 (forward-char -2)
90 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
91 buffer-read-only)
92 (mm-handle-set-undisplayer
93 handle
94 `(lambda ()
95 (let ((b ,(point-marker))
96 buffer-read-only)
97 (delete-annotation ,annot)
98 (delete-region (- b 2) b))))
99 (set-extent-property annot 'mm t)
100 (set-extent-property annot 'duplicable t)))
101
102 (eval-and-compile
103 (if (featurep 'xemacs)
104 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
105 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
106
107 (defvar mm-w3-setup nil)
108 (defun mm-setup-w3 ()
109 (unless mm-w3-setup
110 (require 'w3)
111 (w3-do-setup)
112 (require 'url)
113 (require 'w3-vars)
114 (require 'url-vars)
115 (setq mm-w3-setup t)))
116
117 (defun mm-inline-text-html-render-with-w3 (handle)
118 (mm-setup-w3)
119 (let ((text (mm-get-part handle))
120 (b (point))
121 (url-standalone-mode t)
122 (url-gateway-unplugged t)
123 (w3-honor-stylesheets nil)
124 (url-current-object
125 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
126 (width (window-width))
127 (charset (mail-content-type-get
128 (mm-handle-type handle) 'charset)))
129 (save-excursion
130 (insert text)
131 (save-restriction
132 (narrow-to-region b (point))
133 (goto-char (point-min))
134 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
135 (re-search-forward
136 w3-meta-content-type-charset-regexp nil t))
137 (and (boundp 'w3-meta-charset-content-type-regexp)
138 (re-search-forward
139 w3-meta-charset-content-type-regexp nil t)))
140 (setq charset
141 (or (let ((bsubstr (buffer-substring-no-properties
142 (match-beginning 2)
143 (match-end 2))))
144 (if (fboundp 'w3-coding-system-for-mime-charset)
145 (w3-coding-system-for-mime-charset bsubstr)
146 (mm-charset-to-coding-system bsubstr)))
147 charset)))
148 (delete-region (point-min) (point-max))
149 (insert (mm-decode-string text charset))
150 (save-window-excursion
151 (save-restriction
152 (let ((w3-strict-width width)
153 ;; Don't let w3 set the global version of
154 ;; this variable.
155 (fill-column fill-column))
156 (if (or debug-on-error debug-on-quit)
157 (w3-region (point-min) (point-max))
158 (condition-case ()
159 (w3-region (point-min) (point-max))
160 (error
161 (delete-region (point-min) (point-max))
162 (let ((b (point))
163 (charset (mail-content-type-get
164 (mm-handle-type handle) 'charset)))
165 (if (or (eq charset 'gnus-decoded)
166 (eq mail-parse-charset 'gnus-decoded))
167 (save-restriction
168 (narrow-to-region (point) (point))
169 (mm-insert-part handle)
170 (goto-char (point-max)))
171 (insert (mm-decode-string (mm-get-part handle)
172 charset))))
173 (message
174 "Error while rendering html; showing as text/plain")))))))
175 (mm-handle-set-undisplayer
176 handle
177 `(lambda ()
178 (let (buffer-read-only)
179 (if (functionp 'remove-specifier)
180 (mapcar (lambda (prop)
181 (remove-specifier
182 (face-property 'default prop)
183 (current-buffer)))
184 '(background background-pixmap foreground)))
185 (delete-region ,(point-min-marker)
186 ,(point-max-marker)))))))))
187
188 (defvar mm-w3m-setup nil
189 "Whether gnus-article-mode has been setup to use emacs-w3m.")
190
191 (defun mm-setup-w3m ()
192 "Setup gnus-article-mode to use emacs-w3m."
193 (unless mm-w3m-setup
194 (require 'w3m)
195 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
196 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
197 w3m-cid-retrieve-function-alist))
198 (setq mm-w3m-setup t))
199 (setq w3m-display-inline-images mm-inline-text-html-with-images))
200
201 (defun mm-w3m-cid-retrieve-1 (url handle)
202 (dolist (elem handle)
203 (when (listp elem)
204 (if (equal url (mm-handle-id elem))
205 (progn
206 (mm-insert-part elem)
207 (throw 'found-handle (mm-handle-media-type elem))))
208 (if (equal "multipart" (mm-handle-media-supertype elem))
209 (mm-w3m-cid-retrieve-1 url elem)))))
210
211 (defun mm-w3m-cid-retrieve (url &rest args)
212 "Insert a content pointed by URL if it has the cid: scheme."
213 (when (string-match "\\`cid:" url)
214 (catch 'found-handle
215 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
216 (with-current-buffer w3m-current-buffer
217 gnus-article-mime-handles)))))
218
219 (defun mm-inline-text-html-render-with-w3m (handle)
220 "Render a text/html part using emacs-w3m."
221 (mm-setup-w3m)
222 (let ((text (mm-get-part handle))
223 (b (point))
224 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
225 (save-excursion
226 (insert (if charset (mm-decode-string text charset) text))
227 (save-restriction
228 (narrow-to-region b (point))
229 (unless charset
230 (goto-char (point-min))
231 (when (setq charset (w3m-detect-meta-charset))
232 (delete-region (point-min) (point-max))
233 (insert (mm-decode-string text charset))))
234 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
235 w3m-force-redisplay)
236 (w3m-region (point-min) (point-max) nil charset))
237 (when (and mm-inline-text-html-with-w3m-keymap
238 (boundp 'w3m-minor-mode-map)
239 w3m-minor-mode-map)
240 (add-text-properties
241 (point-min) (point-max)
242 (list 'keymap w3m-minor-mode-map
243 ;; Put the mark meaning this part was rendered by emacs-w3m.
244 'mm-inline-text-html-with-w3m t))))
245 (mm-handle-set-undisplayer
246 handle
247 `(lambda ()
248 (let (buffer-read-only)
249 (if (functionp 'remove-specifier)
250 (mapcar (lambda (prop)
251 (remove-specifier
252 (face-property 'default prop)
253 (current-buffer)))
254 '(background background-pixmap foreground)))
255 (delete-region ,(point-min-marker)
256 ,(point-max-marker))))))))
257
258 (defun mm-links-remove-leading-blank ()
259 ;; Delete the annoying three spaces preceding each line of links
260 ;; output.
261 (goto-char (point-min))
262 (while (re-search-forward "^ " nil t)
263 (delete-region (match-beginning 0) (match-end 0))))
264
265 (defun mm-inline-wash-with-file (post-func cmd &rest args)
266 (let ((file (mm-make-temp-file
267 (expand-file-name "mm" mm-tmp-directory))))
268 (let ((coding-system-for-write 'binary))
269 (write-region (point-min) (point-max) file nil 'silent))
270 (delete-region (point-min) (point-max))
271 (unwind-protect
272 (apply 'call-process cmd nil t nil (mapcar 'eval args))
273 (delete-file file))
274 (and post-func (funcall post-func))))
275
276 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
277 (let ((coding-system-for-write 'binary))
278 (apply 'call-process-region (point-min) (point-max)
279 cmd t t nil args))
280 (and post-func (funcall post-func)))
281
282 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
283 (let ((source (mm-get-part handle)))
284 (mm-insert-inline
285 handle
286 (mm-with-unibyte-buffer
287 (insert source)
288 (apply 'mm-inline-wash-with-file post-func cmd args)
289 (buffer-string)))))
290
291 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
292 (let ((source (mm-get-part handle)))
293 (mm-insert-inline
294 handle
295 (mm-with-unibyte-buffer
296 (insert source)
297 (apply 'mm-inline-wash-with-stdin post-func cmd args)
298 (buffer-string)))))
299
300 (defun mm-inline-render-with-function (handle func &rest args)
301 (let ((source (mm-get-part handle))
302 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
303 (mm-insert-inline
304 handle
305 (mm-with-multibyte-buffer
306 (insert (if charset
307 (mm-decode-string source charset)
308 source))
309 (apply func args)
310 (buffer-string)))))
311
312 (defun mm-inline-text-html (handle)
313 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
314 (entry (assq func mm-text-html-renderer-alist))
315 buffer-read-only)
316 (if entry
317 (setq func (cdr entry)))
318 (cond
319 ((functionp func)
320 (funcall func handle))
321 (t
322 (apply (car func) handle (cdr func))))))
323
324 (defun mm-inline-text-vcard (handle)
325 (let (buffer-read-only)
326 (mm-insert-inline
327 handle
328 (concat "\n-- \n"
329 (ignore-errors
330 (if (fboundp 'vcard-pretty-print)
331 (vcard-pretty-print (mm-get-part handle))
332 (vcard-format-string
333 (vcard-parse-string (mm-get-part handle)
334 'vcard-standard-filter))))))))
335
336 (defun mm-inline-text (handle)
337 (let ((b (point))
338 (type (mm-handle-media-subtype handle))
339 (charset (mail-content-type-get
340 (mm-handle-type handle) 'charset))
341 buffer-read-only)
342 (if (or (eq charset 'gnus-decoded)
343 ;; This is probably not entirely correct, but
344 ;; makes rfc822 parts with embedded multiparts work.
345 (eq mail-parse-charset 'gnus-decoded))
346 (save-restriction
347 (narrow-to-region (point) (point))
348 (mm-insert-part handle)
349 (goto-char (point-max)))
350 (insert (mm-decode-string (mm-get-part handle) charset)))
351 (when (and (equal type "plain")
352 (equal (cdr (assoc 'format (mm-handle-type handle)))
353 "flowed"))
354 (save-restriction
355 (narrow-to-region b (point))
356 (goto-char b)
357 (fill-flowed)
358 (goto-char (point-max))))
359 (save-restriction
360 (narrow-to-region b (point))
361 (set-text-properties (point-min) (point-max) nil)
362 (when (or (equal type "enriched")
363 (equal type "richtext"))
364 (ignore-errors
365 (enriched-decode (point-min) (point-max))))
366 (mm-handle-set-undisplayer
367 handle
368 `(lambda ()
369 (let (buffer-read-only)
370 (delete-region ,(point-min-marker)
371 ,(point-max-marker))))))))
372
373 (defun mm-insert-inline (handle text)
374 "Insert TEXT inline from HANDLE."
375 (let ((b (point)))
376 (insert text)
377 (mm-handle-set-undisplayer
378 handle
379 `(lambda ()
380 (let (buffer-read-only)
381 (delete-region ,(set-marker (make-marker) b)
382 ,(set-marker (make-marker) (point))))))))
383
384 (defun mm-inline-audio (handle)
385 (message "Not implemented"))
386
387 (defun mm-view-sound-file ()
388 (message "Not implemented"))
389
390 (defun mm-w3-prepare-buffer ()
391 (require 'w3)
392 (let ((url-standalone-mode t)
393 (url-gateway-unplugged t)
394 (w3-honor-stylesheets nil))
395 (w3-prepare-buffer)))
396
397 (defun mm-view-message ()
398 (mm-enable-multibyte)
399 (let (handles)
400 (let (gnus-article-mime-handles)
401 ;; Double decode problem may happen. See mm-inline-message.
402 (run-hooks 'gnus-article-decode-hook)
403 (gnus-article-prepare-display)
404 (setq handles gnus-article-mime-handles))
405 (when handles
406 (setq gnus-article-mime-handles
407 (mm-merge-handles gnus-article-mime-handles handles))))
408 (fundamental-mode)
409 (goto-char (point-min)))
410
411 (defun mm-inline-message (handle)
412 (let ((b (point))
413 (bolp (bolp))
414 (charset (mail-content-type-get
415 (mm-handle-type handle) 'charset))
416 gnus-displaying-mime handles)
417 (when (and charset
418 (stringp charset))
419 (setq charset (intern (downcase charset)))
420 (when (eq charset 'us-ascii)
421 (setq charset nil)))
422 (save-excursion
423 (save-restriction
424 (narrow-to-region b b)
425 (mm-insert-part handle)
426 (let (gnus-article-mime-handles
427 ;; disable prepare hook
428 gnus-article-prepare-hook
429 (gnus-newsgroup-charset
430 (or charset gnus-newsgroup-charset)))
431 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
432 (run-hooks 'gnus-article-decode-hook))
433 (gnus-article-prepare-display)
434 (setq handles gnus-article-mime-handles))
435 (goto-char (point-min))
436 (unless bolp
437 (insert "\n"))
438 (goto-char (point-max))
439 (unless (bolp)
440 (insert "\n"))
441 (insert "----------\n\n")
442 (when handles
443 (setq gnus-article-mime-handles
444 (mm-merge-handles gnus-article-mime-handles handles)))
445 (mm-handle-set-undisplayer
446 handle
447 `(lambda ()
448 (let (buffer-read-only)
449 (if (fboundp 'remove-specifier)
450 ;; This is only valid on XEmacs.
451 (mapcar (lambda (prop)
452 (remove-specifier
453 (face-property 'default prop) (current-buffer)))
454 '(background background-pixmap foreground)))
455 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
456
457 (defun mm-display-inline-fontify (handle mode)
458 (let (text)
459 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
460 ;; on for buffers whose name begins with " ". That's why we use
461 ;; save-current-buffer/get-buffer-create rather than
462 ;; with-temp-buffer.
463 (save-current-buffer
464 (set-buffer (generate-new-buffer "*fontification*"))
465 (unwind-protect
466 (progn
467 (buffer-disable-undo)
468 (mm-insert-part handle)
469 (funcall mode)
470 (require 'font-lock)
471 (let ((font-lock-verbose nil))
472 ;; I find font-lock a bit too verbose.
473 (font-lock-fontify-buffer))
474 ;; By default, XEmacs font-lock uses non-duplicable text
475 ;; properties. This code forces all the text properties
476 ;; to be copied along with the text.
477 (when (fboundp 'extent-list)
478 (map-extents (lambda (ext ignored)
479 (set-extent-property ext 'duplicable t)
480 nil)
481 nil nil nil nil nil 'text-prop))
482 (setq text (buffer-string)))
483 (kill-buffer (current-buffer))))
484 (mm-insert-inline handle text)))
485
486 ;; Shouldn't these functions check whether the user even wants to use
487 ;; font-lock? At least under XEmacs, this fontification is pretty
488 ;; much unconditional. Also, it would be nice to change for the size
489 ;; of the fontified region.
490
491 (defun mm-display-patch-inline (handle)
492 (mm-display-inline-fontify handle 'diff-mode))
493
494 (defun mm-display-elisp-inline (handle)
495 (mm-display-inline-fontify handle 'emacs-lisp-mode))
496
497 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
498 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
499 (defvar mm-pkcs7-signed-magic
500 (mm-string-as-unibyte
501 (apply 'concat
502 (mapcar 'char-to-string
503 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
504 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
505 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
506 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
507
508 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
509 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
510 (defvar mm-pkcs7-enveloped-magic
511 (mm-string-as-unibyte
512 (apply 'concat
513 (mapcar 'char-to-string
514 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
515 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
516 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
517 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
518
519 (defun mm-view-pkcs7-get-type (handle)
520 (mm-with-unibyte-buffer
521 (mm-insert-part handle)
522 (cond ((looking-at mm-pkcs7-enveloped-magic)
523 'enveloped)
524 ((looking-at mm-pkcs7-signed-magic)
525 'signed)
526 (t
527 (error "Could not identify PKCS#7 type")))))
528
529 (defun mm-view-pkcs7 (handle)
530 (case (mm-view-pkcs7-get-type handle)
531 (enveloped (mm-view-pkcs7-decrypt handle))
532 (signed (mm-view-pkcs7-verify handle))
533 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
534
535 (defun mm-view-pkcs7-verify (handle)
536 ;; A bogus implementation of PKCS#7. FIXME::
537 (mm-insert-part handle)
538 (goto-char (point-min))
539 (if (search-forward "Content-Type: " nil t)
540 (delete-region (point-min) (match-beginning 0)))
541 (goto-char (point-max))
542 (if (re-search-backward "--\r?\n?" nil t)
543 (delete-region (match-end 0) (point-max)))
544 (goto-char (point-min))
545 (while (search-forward "\r\n" nil t)
546 (replace-match "\n"))
547 (message "Verify signed PKCS#7 message is unimplemented.")
548 (sit-for 1)
549 t)
550
551 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
552
553 (defun mm-view-pkcs7-decrypt (handle)
554 (insert-buffer-substring (mm-handle-buffer handle))
555 (goto-char (point-min))
556 (insert "MIME-Version: 1.0\n")
557 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
558 (smime-decrypt-region
559 (point-min) (point-max)
560 (if (= (length smime-keys) 1)
561 (cadar smime-keys)
562 (smime-get-key-by-email
563 (gnus-completing-read-maybe-default
564 (concat "Decipher using which key? "
565 (if smime-keys (concat "(default " (caar smime-keys) ") ")
566 ""))
567 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
568 (goto-char (point-min))
569 (while (search-forward "\r\n" nil t)
570 (replace-match "\n"))
571 (goto-char (point-min)))
572
573 (provide 'mm-view)
574
575 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
576 ;;; mm-view.el ends here