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 (autoload 'html2text "html2text")
38 (unless (fboundp 'diff-mode)
39 (autoload 'diff-mode "diff-mode" "" t nil)))
41 (defvar mm-text-html-renderer-alist
42 '((w3 . mm-inline-text-html-render-with-w3)
43 (w3m . mm-inline-text-html-render-with-w3m)
44 (links mm-inline-render-with-file
45 mm-links-remove-leading-blank
47 (lynx mm-inline-render-with-stdin nil
48 "lynx" "-dump" "-force_html" "-stdin")
49 (html2text mm-inline-render-with-function html2text))
50 "The attributes of renderer types for text/html.")
52 (defvar mm-text-html-washer-alist
53 '((w3 . gnus-article-wash-html-with-w3)
54 (w3m . gnus-article-wash-html-with-w3m)
55 (links mm-inline-wash-with-file
56 mm-links-remove-leading-blank
58 (lynx mm-inline-wash-with-stdin nil
59 "lynx" "-dump" "-force_html" "-stdin")
60 (html2text html2text))
61 "The attributes of washer types for text/html.")
63 ;;; Internal variables.
66 ;;; Functions for displaying various formats inline
69 (defun mm-inline-image-emacs (handle)
70 (let ((b (point-marker))
73 (put-image (mm-get-image handle) b)
74 (mm-handle-set-undisplayer
76 `(lambda () (remove-images ,b (1+ ,b))))))
78 (defun mm-inline-image-xemacs (handle)
82 (annot (make-annotation (mm-get-image handle) nil 'text))
84 (mm-handle-set-undisplayer
87 (let (buffer-read-only)
88 (delete-annotation ,annot)
89 (delete-region ,(set-marker (make-marker) b)
90 ,(set-marker (make-marker) (point))))))
91 (set-extent-property annot 'mm t)
92 (set-extent-property annot 'duplicable t)))
95 (if (featurep 'xemacs)
96 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
97 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
99 (defvar mm-w3-setup nil)
100 (defun mm-setup-w3 ()
107 (setq mm-w3-setup t)))
109 (defun mm-inline-text-html-render-with-w3 (handle)
111 (let ((text (mm-get-part handle))
113 (url-standalone-mode t)
114 (url-gateway-unplugged t)
115 (w3-honor-stylesheets nil)
117 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
118 (width (window-width))
119 (charset (mail-content-type-get
120 (mm-handle-type handle) 'charset)))
124 (narrow-to-region b (point))
125 (goto-char (point-min))
126 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
128 w3-meta-content-type-charset-regexp nil t))
129 (and (boundp 'w3-meta-charset-content-type-regexp)
131 w3-meta-charset-content-type-regexp nil t)))
133 (or (let ((bsubstr (buffer-substring-no-properties
136 (if (fboundp 'w3-coding-system-for-mime-charset)
137 (w3-coding-system-for-mime-charset bsubstr)
138 (mm-charset-to-coding-system bsubstr)))
140 (delete-region (point-min) (point-max))
141 (insert (mm-decode-string text charset))
142 (save-window-excursion
144 (let ((w3-strict-width width)
145 ;; Don't let w3 set the global version of
147 (fill-column fill-column))
148 (if (or debug-on-error debug-on-quit)
149 (w3-region (point-min) (point-max))
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-mode-ignored-keys (list [down-mouse-2])
234 "List of keys which should ignore.")
236 (defvar mm-w3m-setup nil
237 "Whether gnus-article-mode has been setup to use emacs-w3m.")
239 (defun mm-setup-w3m ()
240 "Setup gnus-article-mode to use emacs-w3m."
243 (unless mm-w3m-mode-map
244 (setq mm-w3m-mode-map (copy-keymap w3m-mode-map))
245 (dolist (def mm-w3m-mode-command-alist)
247 (substitute-key-definition (car def) (cdr def) mm-w3m-mode-map)
249 (dolist (key mm-w3m-mode-dont-bind-keys)
251 (define-key mm-w3m-mode-map key nil)
253 (dolist (key mm-w3m-mode-ignored-keys)
255 (define-key mm-w3m-mode-map key 'ignore)
257 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
258 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
259 w3m-cid-retrieve-function-alist))
260 (setq mm-w3m-setup t)))
262 (defun mm-w3m-cid-retrieve (url &rest args)
263 "Insert a content pointed by URL if it has the cid: scheme."
264 (when (string-match "\\`cid:" url)
265 (setq url (concat "<" (substring url (match-end 0)) ">"))
267 (dolist (handle (with-current-buffer w3m-current-buffer
268 gnus-article-mime-handles))
269 (when (and (listp handle)
270 (equal url (mm-handle-id handle)))
271 (mm-insert-part handle)
272 (throw 'found-handle (mm-handle-media-type handle)))))))
274 (defun mm-inline-text-html-render-with-w3m (handle)
275 "Render a text/html part using emacs-w3m."
277 (let ((text (mm-get-part handle))
279 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
283 (narrow-to-region b (point))
284 (goto-char (point-min))
285 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
286 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
289 (delete-region (point-min) (point-max))
290 (insert (mm-decode-string text charset)))
291 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
292 (w3m-display-inline-images mm-inline-text-html-with-images)
294 (w3m-region (point-min) (point-max)))
295 (when mm-inline-text-html-with-w3m-keymap
297 (point-min) (point-max)
298 (append '(mm-inline-text-html-with-w3m t)
299 (gnus-local-map-property mm-w3m-mode-map)))))
300 (mm-handle-set-undisplayer
303 (let (buffer-read-only)
304 (if (functionp 'remove-specifier)
305 (mapcar (lambda (prop)
307 (face-property 'default prop)
309 '(background background-pixmap foreground)))
310 (delete-region ,(point-min-marker)
311 ,(point-max-marker))))))))
313 (defun mm-links-remove-leading-blank ()
314 ;; Delete the annoying three spaces preceding each line of links
316 (goto-char (point-min))
317 (while (re-search-forward "^ " nil t)
318 (delete-region (match-beginning 0) (match-end 0))))
320 (defun mm-inline-wash-with-file (post-func cmd &rest args)
321 (let ((file (mm-make-temp-file
322 (expand-file-name "mm" mm-tmp-directory))))
323 (let ((coding-system-for-write 'binary))
324 (write-region (point-min) (point-max) file nil 'silent))
325 (delete-region (point-min) (point-max))
327 (apply 'call-process cmd nil t nil (mapcar 'eval args))
329 (and post-func (funcall post-func))))
331 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
332 (let ((coding-system-for-write 'binary))
333 (apply 'call-process-region (point-min) (point-max)
335 (and post-func (funcall post-func)))
337 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
338 (let ((source (mm-get-part handle)))
341 (mm-with-unibyte-buffer
343 (apply 'mm-inline-wash-with-file post-func cmd args)
346 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
347 (let ((source (mm-get-part handle)))
350 (mm-with-unibyte-buffer
352 (apply 'mm-inline-wash-with-stdin post-func cmd args)
355 (defun mm-inline-render-with-function (handle func &rest args)
356 (let ((source (mm-get-part handle)))
359 (mm-with-unibyte-buffer
364 (defun mm-inline-text-html (handle)
365 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
366 (entry (assq func mm-text-html-renderer-alist))
369 (setq func (cdr entry)))
371 ((gnus-functionp func)
372 (funcall func handle))
374 (apply (car func) handle (cdr func))))))
376 (defun mm-inline-text-vcard (handle)
377 (let (buffer-read-only)
382 (if (fboundp 'vcard-pretty-print)
383 (vcard-pretty-print (mm-get-part handle))
385 (vcard-parse-string (mm-get-part handle)
386 'vcard-standard-filter))))))))
388 (defun mm-inline-text (handle)
390 (type (mm-handle-media-subtype handle))
391 (charset (mail-content-type-get
392 (mm-handle-type handle) 'charset))
394 (if (or (eq charset 'gnus-decoded)
395 ;; This is probably not entirely correct, but
396 ;; makes rfc822 parts with embedded multiparts work.
397 (eq mail-parse-charset 'gnus-decoded))
399 (narrow-to-region (point) (point))
400 (mm-insert-part handle)
401 (goto-char (point-max)))
402 (insert (mm-decode-string (mm-get-part handle) charset)))
403 (when (and (equal type "plain")
404 (equal (cdr (assoc 'format (mm-handle-type handle)))
407 (narrow-to-region b (point))
410 (goto-char (point-max))))
412 (narrow-to-region b (point))
413 (set-text-properties (point-min) (point-max) nil)
414 (when (or (equal type "enriched")
415 (equal type "richtext"))
416 (enriched-decode (point-min) (point-max)))
417 (mm-handle-set-undisplayer
420 (let (buffer-read-only)
421 (delete-region ,(point-min-marker)
422 ,(point-max-marker))))))))
424 (defun mm-insert-inline (handle text)
425 "Insert TEXT inline from HANDLE."
428 (mm-handle-set-undisplayer
431 (let (buffer-read-only)
432 (delete-region ,(set-marker (make-marker) b)
433 ,(set-marker (make-marker) (point))))))))
435 (defun mm-inline-audio (handle)
436 (message "Not implemented"))
438 (defun mm-view-sound-file ()
439 (message "Not implemented"))
441 (defun mm-w3-prepare-buffer ()
443 (let ((url-standalone-mode t)
444 (url-gateway-unplugged t)
445 (w3-honor-stylesheets nil))
446 (w3-prepare-buffer)))
448 (defun mm-view-message ()
449 (mm-enable-multibyte)
451 (let (gnus-article-mime-handles)
452 ;; Double decode problem may happen. See mm-inline-message.
453 (run-hooks 'gnus-article-decode-hook)
454 (gnus-article-prepare-display)
455 (setq handles gnus-article-mime-handles))
457 (setq gnus-article-mime-handles
458 (mm-merge-handles gnus-article-mime-handles handles))))
460 (goto-char (point-min)))
462 (defun mm-inline-message (handle)
465 (charset (mail-content-type-get
466 (mm-handle-type handle) 'charset))
467 gnus-displaying-mime handles)
470 (setq charset (intern (downcase charset)))
471 (when (eq charset 'us-ascii)
475 (narrow-to-region b b)
476 (mm-insert-part handle)
477 (let (gnus-article-mime-handles
478 ;; disable prepare hook
479 gnus-article-prepare-hook
480 (gnus-newsgroup-charset
481 (or charset gnus-newsgroup-charset)))
482 (run-hooks 'gnus-article-decode-hook)
483 (gnus-article-prepare-display)
484 (setq handles gnus-article-mime-handles))
485 (goto-char (point-min))
488 (goto-char (point-max))
491 (insert "----------\n\n")
493 (setq gnus-article-mime-handles
494 (mm-merge-handles gnus-article-mime-handles handles)))
495 (mm-handle-set-undisplayer
498 (let (buffer-read-only)
499 (if (fboundp 'remove-specifier)
500 ;; This is only valid on XEmacs.
501 (mapcar (lambda (prop)
503 (face-property 'default prop) (current-buffer)))
504 '(background background-pixmap foreground)))
505 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
507 (defun mm-display-inline-fontify (handle mode)
509 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
510 ;; on for buffers whose name begins with " ". That's why we use
511 ;; save-current-buffer/get-buffer-create rather than
514 (set-buffer (generate-new-buffer "*fontification*"))
517 (buffer-disable-undo)
518 (mm-insert-part handle)
521 (let ((font-lock-verbose nil))
522 ;; I find font-lock a bit too verbose.
523 (font-lock-fontify-buffer))
524 ;; By default, XEmacs font-lock uses non-duplicable text
525 ;; properties. This code forces all the text properties
526 ;; to be copied along with the text.
527 (when (fboundp 'extent-list)
528 (map-extents (lambda (ext ignored)
529 (set-extent-property ext 'duplicable t)
531 nil nil nil nil nil 'text-prop))
532 (setq text (buffer-string)))
533 (kill-buffer (current-buffer))))
534 (mm-insert-inline handle text)))
536 ;; Shouldn't these functions check whether the user even wants to use
537 ;; font-lock? At least under XEmacs, this fontification is pretty
538 ;; much unconditional. Also, it would be nice to change for the size
539 ;; of the fontified region.
541 (defun mm-display-patch-inline (handle)
542 (mm-display-inline-fontify handle 'diff-mode))
544 (defun mm-display-elisp-inline (handle)
545 (mm-display-inline-fontify handle 'emacs-lisp-mode))
547 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
548 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
549 (defvar mm-pkcs7-signed-magic
550 (mm-string-as-unibyte
552 (mapcar 'char-to-string
553 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
554 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
555 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
556 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
558 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
559 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
560 (defvar mm-pkcs7-enveloped-magic
561 (mm-string-as-unibyte
563 (mapcar 'char-to-string
564 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
565 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
566 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
567 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
569 (defun mm-view-pkcs7-get-type (handle)
570 (mm-with-unibyte-buffer
571 (mm-insert-part handle)
572 (cond ((looking-at mm-pkcs7-enveloped-magic)
574 ((looking-at mm-pkcs7-signed-magic)
577 (error "Could not identify PKCS#7 type")))))
579 (defun mm-view-pkcs7 (handle)
580 (case (mm-view-pkcs7-get-type handle)
581 (enveloped (mm-view-pkcs7-decrypt handle))
582 (signed (mm-view-pkcs7-verify handle))
583 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
585 (defun mm-view-pkcs7-verify (handle)
586 ;; A bogus implementation of PKCS#7. FIXME::
587 (mm-insert-part handle)
588 (goto-char (point-min))
589 (if (search-forward "Content-Type: " nil t)
590 (delete-region (point-min) (match-beginning 0)))
591 (goto-char (point-max))
592 (if (re-search-backward "--\r?\n?" nil t)
593 (delete-region (match-end 0) (point-max)))
594 (goto-char (point-min))
595 (while (search-forward "\r\n" nil t)
596 (replace-match "\n"))
597 (message "Verify signed PKCS#7 message is unimplemented.")
601 (defun mm-view-pkcs7-decrypt (handle)
602 (insert-buffer (mm-handle-buffer handle))
603 (goto-char (point-min))
604 (insert "MIME-Version: 1.0\n")
605 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
606 (smime-decrypt-region
607 (point-min) (point-max)
608 (if (= (length smime-keys) 1)
610 (smime-get-key-by-email
612 (concat "Decipher using which key? "
613 (if smime-keys (concat "(default " (caar smime-keys) ") ")
615 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
616 (goto-char (point-min))
617 (while (search-forward "\r\n" nil t)
618 (replace-match "\n"))
619 (goto-char (point-min)))
623 ;;; mm-view.el ends here