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))
149 (w3-region (point-min) (point-max))
151 (delete-region (point-min) (point-max))
153 (charset (mail-content-type-get
154 (mm-handle-type handle) 'charset)))
155 (if (or (eq charset 'gnus-decoded)
156 (eq mail-parse-charset 'gnus-decoded))
158 (narrow-to-region (point) (point))
159 (mm-insert-part handle)
160 (goto-char (point-max)))
161 (insert (mm-decode-string (mm-get-part handle)
164 "Error while rendering html; showing as text/plain"))))))
165 (mm-handle-set-undisplayer
168 (let (buffer-read-only)
169 (if (functionp 'remove-specifier)
170 (mapcar (lambda (prop)
172 (face-property 'default prop)
174 '(background background-pixmap foreground)))
175 (delete-region ,(point-min-marker)
176 ,(point-max-marker)))))))))
178 (defvar mm-w3m-mode-map nil
179 "Local keymap for inlined text/html part rendered by emacs-w3m. It will
180 be different from `w3m-mode-map' to use in the article buffer.")
182 (defvar mm-w3m-mode-command-alist
190 (w3m-antenna-add-current-url)
191 (w3m-bookmark-add-current-url)
192 (w3m-bookmark-add-this-url)
198 (w3m-edit-current-url)
202 (w3m-goto-url-new-session)
204 (w3m-history-restore-position)
205 (w3m-history-store-position)
208 (w3m-previous-buffer)
210 (w3m-redisplay-with-charset)
211 (w3m-reload-this-page)
212 (w3m-scroll-down-or-previous-url)
213 (w3m-scroll-up-or-next-url)
218 (w3m-view-parent-page)
219 (w3m-view-previous-page)
222 "Alist of commands to use for emacs-w3m in the article buffer. Each
223 element looks like (FROM-COMMAND . TO-COMMAND); FROM-COMMAND should be
224 registered in `w3m-mode-map' which will be substituted by TO-COMMAND
225 in `mm-w3m-mode-map'. If TO-COMMAND is nil, an article command key
226 will not be substituted.")
228 (defvar mm-w3m-mode-dont-bind-keys (list [up] [right] [left] [down])
229 "List of keys which should not be bound for the emacs-w3m commands.")
231 (defvar mm-w3m-setup nil
232 "Whether gnus-article-mode has been setup to use emacs-w3m.")
234 (defun mm-setup-w3m ()
235 "Setup gnus-article-mode to use emacs-w3m."
238 (unless mm-w3m-mode-map
239 (setq mm-w3m-mode-map (copy-keymap w3m-mode-map))
240 (dolist (def mm-w3m-mode-command-alist)
242 (substitute-key-definition (car def) (cdr def) mm-w3m-mode-map)
244 (dolist (key mm-w3m-mode-dont-bind-keys)
246 (define-key mm-w3m-mode-map key nil)
248 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
249 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
250 w3m-cid-retrieve-function-alist))
251 (setq mm-w3m-setup t)))
253 (defun mm-w3m-cid-retrieve (url &rest args)
254 "Insert a content pointed by URL if it has the cid: scheme."
255 (when (string-match "\\`cid:" url)
256 (setq url (concat "<" (substring url (match-end 0)) ">"))
258 (dolist (handle (with-current-buffer w3m-current-buffer
259 gnus-article-mime-handles))
260 (when (and (listp handle)
261 (equal url (mm-handle-id handle)))
262 (mm-insert-part handle)
263 (throw 'found-handle (mm-handle-media-type handle)))))))
265 (defun mm-inline-text-html-render-with-w3m (handle)
266 "Render a text/html part using emacs-w3m."
268 (let ((text (mm-get-part handle))
270 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
274 (narrow-to-region b (point))
275 (goto-char (point-min))
276 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
277 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
280 (delete-region (point-min) (point-max))
281 (insert (mm-decode-string text charset)))
282 (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
285 (w3m-display-inline-images mm-inline-text-html-with-images)
287 (w3m-region (point-min) (point-max)))
288 (when mm-inline-text-html-with-w3m-keymap
290 (point-min) (point-max)
291 (append '(mm-inline-text-html-with-w3m t)
292 (gnus-local-map-property mm-w3m-mode-map)))))
293 (mm-handle-set-undisplayer
296 (let (buffer-read-only)
297 (if (functionp 'remove-specifier)
298 (mapcar (lambda (prop)
300 (face-property 'default prop)
302 '(background background-pixmap foreground)))
303 (delete-region ,(point-min-marker)
304 ,(point-max-marker))))))))
306 (defun mm-links-remove-leading-blank ()
307 ;; Delete the annoying three spaces preceding each line of links
309 (goto-char (point-min))
310 (while (re-search-forward "^ " nil t)
311 (delete-region (match-beginning 0) (match-end 0))))
313 (defun mm-inline-wash-with-file (post-func cmd &rest args)
314 (let ((file (make-temp-name
315 (expand-file-name "mm" mm-tmp-directory))))
316 (let ((coding-system-for-write 'binary))
317 (write-region (point-min) (point-max) file nil 'silent))
318 (delete-region (point-min) (point-max))
320 (apply 'call-process cmd nil t nil (mapcar 'eval args))
322 (and post-func (funcall post-func))))
324 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
325 (let ((coding-system-for-write 'binary))
326 (apply 'call-process-region (point-min) (point-max)
328 (and post-func (funcall post-func)))
330 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
331 (let ((source (mm-get-part handle)))
334 (mm-with-unibyte-buffer
336 (apply 'mm-inline-wash-with-file post-func cmd args)
339 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
340 (let ((source (mm-get-part handle)))
343 (mm-with-unibyte-buffer
345 (apply 'mm-inline-wash-with-stdin post-func cmd args)
348 (defun mm-inline-render-with-function (handle func &rest args)
349 (let ((source (mm-get-part handle)))
352 (mm-with-unibyte-buffer
357 (defun mm-inline-text-html (handle)
358 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
359 (entry (assq func mm-text-html-renderer-alist))
362 (setq func (cdr entry)))
364 ((gnus-functionp func)
365 (funcall func handle))
367 (apply (car func) handle (cdr func))))))
369 (defun mm-inline-text-vcard (handle)
370 (let (buffer-read-only)
375 (if (fboundp 'vcard-pretty-print)
376 (vcard-pretty-print (mm-get-part handle))
378 (vcard-parse-string (mm-get-part handle)
379 'vcard-standard-filter))))))))
381 (defun mm-inline-text (handle)
383 (type (mm-handle-media-subtype handle))
384 (charset (mail-content-type-get
385 (mm-handle-type handle) 'charset))
387 (if (or (eq charset 'gnus-decoded)
388 ;; This is probably not entirely correct, but
389 ;; makes rfc822 parts with embedded multiparts work.
390 (eq mail-parse-charset 'gnus-decoded))
392 (narrow-to-region (point) (point))
393 (mm-insert-part handle)
394 (goto-char (point-max)))
395 (insert (mm-decode-string (mm-get-part handle) charset)))
396 (when (and (equal type "plain")
397 (equal (cdr (assoc 'format (mm-handle-type handle)))
400 (narrow-to-region b (point))
403 (goto-char (point-max))))
405 (narrow-to-region b (point))
406 (set-text-properties (point-min) (point-max) nil)
407 (when (or (equal type "enriched")
408 (equal type "richtext"))
409 (enriched-decode (point-min) (point-max)))
410 (mm-handle-set-undisplayer
413 (let (buffer-read-only)
414 (delete-region ,(point-min-marker)
415 ,(point-max-marker))))))))
417 (defun mm-insert-inline (handle text)
418 "Insert TEXT inline from HANDLE."
421 (mm-handle-set-undisplayer
424 (let (buffer-read-only)
425 (delete-region ,(set-marker (make-marker) b)
426 ,(set-marker (make-marker) (point))))))))
428 (defun mm-inline-audio (handle)
429 (message "Not implemented"))
431 (defun mm-view-sound-file ()
432 (message "Not implemented"))
434 (defun mm-w3-prepare-buffer ()
436 (let ((url-standalone-mode t)
437 (url-gateway-unplugged t)
438 (w3-honor-stylesheets nil))
439 (w3-prepare-buffer)))
441 (defun mm-view-message ()
442 (mm-enable-multibyte)
444 (let (gnus-article-mime-handles)
445 ;; Double decode problem may happen. See mm-inline-message.
446 (run-hooks 'gnus-article-decode-hook)
447 (gnus-article-prepare-display)
448 (setq handles gnus-article-mime-handles))
450 (setq gnus-article-mime-handles
451 (mm-merge-handles gnus-article-mime-handles handles))))
453 (goto-char (point-min)))
455 (defun mm-inline-message (handle)
458 (charset (mail-content-type-get
459 (mm-handle-type handle) 'charset))
460 gnus-displaying-mime handles)
463 (setq charset (intern (downcase charset)))
464 (when (eq charset 'us-ascii)
468 (narrow-to-region b b)
469 (mm-insert-part handle)
470 (let (gnus-article-mime-handles
471 ;; disable prepare hook
472 gnus-article-prepare-hook
473 (gnus-newsgroup-charset
474 (or charset gnus-newsgroup-charset)))
475 (run-hooks 'gnus-article-decode-hook)
476 (gnus-article-prepare-display)
477 (setq handles gnus-article-mime-handles))
478 (goto-char (point-min))
481 (goto-char (point-max))
484 (insert "----------\n\n")
486 (setq gnus-article-mime-handles
487 (mm-merge-handles gnus-article-mime-handles handles)))
488 (mm-handle-set-undisplayer
491 (let (buffer-read-only)
492 (if (fboundp 'remove-specifier)
493 ;; This is only valid on XEmacs.
494 (mapcar (lambda (prop)
496 (face-property 'default prop) (current-buffer)))
497 '(background background-pixmap foreground)))
498 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
500 (defun mm-display-inline-fontify (handle mode)
502 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
503 ;; on for buffers whose name begins with " ". That's why we use
504 ;; save-current-buffer/get-buffer-create rather than
507 (set-buffer (generate-new-buffer "*fontification*"))
510 (buffer-disable-undo)
511 (mm-insert-part handle)
514 (let ((font-lock-verbose nil))
515 ;; I find font-lock a bit too verbose.
516 (font-lock-fontify-buffer))
517 ;; By default, XEmacs font-lock uses non-duplicable text
518 ;; properties. This code forces all the text properties
519 ;; to be copied along with the text.
520 (when (fboundp 'extent-list)
521 (map-extents (lambda (ext ignored)
522 (set-extent-property ext 'duplicable t)
524 nil nil nil nil nil 'text-prop))
525 (setq text (buffer-string)))
526 (kill-buffer (current-buffer))))
527 (mm-insert-inline handle text)))
529 ;; Shouldn't these functions check whether the user even wants to use
530 ;; font-lock? At least under XEmacs, this fontification is pretty
531 ;; much unconditional. Also, it would be nice to change for the size
532 ;; of the fontified region.
534 (defun mm-display-patch-inline (handle)
535 (mm-display-inline-fontify handle 'diff-mode))
537 (defun mm-display-elisp-inline (handle)
538 (mm-display-inline-fontify handle 'emacs-lisp-mode))
540 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
541 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
542 (defvar mm-pkcs7-signed-magic
543 (mm-string-as-unibyte
545 (mapcar 'char-to-string
546 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
547 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
548 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
549 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
551 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
552 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
553 (defvar mm-pkcs7-enveloped-magic
554 (mm-string-as-unibyte
556 (mapcar 'char-to-string
557 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
558 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
559 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
560 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
562 (defun mm-view-pkcs7-get-type (handle)
563 (mm-with-unibyte-buffer
564 (mm-insert-part handle)
565 (cond ((looking-at mm-pkcs7-enveloped-magic)
567 ((looking-at mm-pkcs7-signed-magic)
570 (error "Could not identify PKCS#7 type")))))
572 (defun mm-view-pkcs7 (handle)
573 (case (mm-view-pkcs7-get-type handle)
574 (enveloped (mm-view-pkcs7-decrypt handle))
575 (signed (mm-view-pkcs7-verify handle))
576 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
578 (defun mm-view-pkcs7-verify (handle)
579 ;; A bogus implementation of PKCS#7. FIXME::
580 (mm-insert-part handle)
581 (goto-char (point-min))
582 (if (search-forward "Content-Type: " nil t)
583 (delete-region (point-min) (match-beginning 0)))
584 (goto-char (point-max))
585 (if (re-search-backward "--\r?\n?" nil t)
586 (delete-region (match-end 0) (point-max)))
587 (goto-char (point-min))
588 (while (search-forward "\r\n" nil t)
589 (replace-match "\n"))
590 (message "Verify signed PKCS#7 message is unimplemented.")
594 (defun mm-view-pkcs7-decrypt (handle)
595 (insert-buffer (mm-handle-buffer handle))
596 (goto-char (point-min))
597 (insert "MIME-Version: 1.0\n")
598 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
599 (smime-decrypt-region
600 (point-min) (point-max)
601 (if (= (length smime-keys) 1)
603 (smime-get-key-by-email
604 (completing-read "Decrypt this part with which key? "
606 (and (listp (car-safe smime-keys))
607 (caar smime-keys)))))))
611 ;;; mm-view.el ends here