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 (w3m-standalone mm-inline-render-with-stdin nil
45 "w3m" "-dump" "-T" "text/html")
46 (links mm-inline-render-with-file
47 mm-links-remove-leading-blank
49 (lynx mm-inline-render-with-stdin nil
50 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
51 (html2text mm-inline-render-with-function html2text))
52 "The attributes of renderer types for text/html.")
54 (defvar mm-text-html-washer-alist
55 '((w3 . gnus-article-wash-html-with-w3)
56 (w3m . gnus-article-wash-html-with-w3m)
57 (w3m-standalone mm-inline-render-with-stdin nil
58 "w3m" "-dump" "-T" "text/html")
59 (links mm-inline-wash-with-file
60 mm-links-remove-leading-blank
62 (lynx mm-inline-wash-with-stdin nil
63 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
64 (html2text html2text))
65 "The attributes of washer types for text/html.")
67 ;;; Internal variables.
70 ;;; Functions for displaying various formats inline
73 (defun mm-inline-image-emacs (handle)
74 (let ((b (point-marker))
76 (put-image (mm-get-image handle) b)
78 (mm-handle-set-undisplayer
84 (delete-region b (+ b 2)))))))
86 (defun mm-inline-image-xemacs (handle)
89 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
91 (mm-handle-set-undisplayer
94 (let ((b ,(point-marker))
96 (delete-annotation ,annot)
97 (delete-region (- b 2) b))))
98 (set-extent-property annot 'mm t)
99 (set-extent-property annot 'duplicable t)))
102 (if (featurep 'xemacs)
103 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
104 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
106 (defvar mm-w3-setup nil)
107 (defun mm-setup-w3 ()
114 (setq mm-w3-setup t)))
116 (defun mm-inline-text-html-render-with-w3 (handle)
118 (let ((text (mm-get-part handle))
120 (url-standalone-mode t)
121 (url-gateway-unplugged t)
122 (w3-honor-stylesheets nil)
124 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
125 (width (window-width))
126 (charset (mail-content-type-get
127 (mm-handle-type handle) 'charset)))
131 (narrow-to-region b (point))
132 (goto-char (point-min))
133 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
135 w3-meta-content-type-charset-regexp nil t))
136 (and (boundp 'w3-meta-charset-content-type-regexp)
138 w3-meta-charset-content-type-regexp nil t)))
140 (or (let ((bsubstr (buffer-substring-no-properties
143 (if (fboundp 'w3-coding-system-for-mime-charset)
144 (w3-coding-system-for-mime-charset bsubstr)
145 (mm-charset-to-coding-system bsubstr)))
147 (delete-region (point-min) (point-max))
148 (insert (mm-decode-string text charset))
149 (save-window-excursion
151 (let ((w3-strict-width width)
152 ;; Don't let w3 set the global version of
154 (fill-column fill-column))
155 (if (or debug-on-error debug-on-quit)
156 (w3-region (point-min) (point-max))
158 (w3-region (point-min) (point-max))
160 (delete-region (point-min) (point-max))
162 (charset (mail-content-type-get
163 (mm-handle-type handle) 'charset)))
164 (if (or (eq charset 'gnus-decoded)
165 (eq mail-parse-charset 'gnus-decoded))
167 (narrow-to-region (point) (point))
168 (mm-insert-part handle)
169 (goto-char (point-max)))
170 (insert (mm-decode-string (mm-get-part handle)
173 "Error while rendering html; showing as text/plain")))))))
174 (mm-handle-set-undisplayer
177 (let (buffer-read-only)
178 (if (functionp 'remove-specifier)
179 (mapcar (lambda (prop)
181 (face-property 'default prop)
183 '(background background-pixmap foreground)))
184 (delete-region ,(point-min-marker)
185 ,(point-max-marker)))))))))
187 (defvar mm-w3m-setup nil
188 "Whether gnus-article-mode has been setup to use emacs-w3m.")
190 (defun mm-setup-w3m ()
191 "Setup gnus-article-mode to use emacs-w3m."
194 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
195 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
196 w3m-cid-retrieve-function-alist))
197 (setq mm-w3m-setup t))
198 (setq w3m-display-inline-images mm-inline-text-html-with-images))
200 (defun mm-w3m-cid-retrieve (url &rest args)
201 "Insert a content pointed by URL if it has the cid: scheme."
202 (when (string-match "\\`cid:" url)
203 (setq url (concat "<" (substring url (match-end 0)) ">"))
205 (dolist (handle (with-current-buffer w3m-current-buffer
206 gnus-article-mime-handles))
207 (when (and (listp handle)
208 (equal url (mm-handle-id handle)))
209 (mm-insert-part handle)
210 (throw 'found-handle (mm-handle-media-type handle)))))))
213 (unless (or (featurep 'xemacs)
214 (>= emacs-major-version 21))
215 (defvar mm-w3m-mode-map nil
216 "Keymap for text/html part rendered by `mm-w3m-preview-text/html'.
217 This map is overwritten by `mm-w3m-local-map-property' based on the
218 value of `w3m-minor-mode-map'. Therefore, in order to add some
219 commands to this map, add them to `w3m-minor-mode-map' instead of this
222 (defun mm-w3m-local-map-property ()
223 (when (and (boundp 'w3m-minor-mode-map) w3m-minor-mode-map)
224 (if (or (featurep 'xemacs)
225 (>= emacs-major-version 21))
226 (list 'keymap w3m-minor-mode-map)
230 (setq mm-w3m-mode-map (copy-keymap w3m-minor-mode-map))
231 (set-keymap-parent mm-w3m-mode-map gnus-article-mode-map)
232 mm-w3m-mode-map))))))
234 (defun mm-inline-text-html-render-with-w3m (handle)
235 "Render a text/html part using emacs-w3m."
237 (let ((text (mm-get-part handle))
239 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
243 (narrow-to-region b (point))
244 (goto-char (point-min))
245 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
246 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
249 (delete-region (point-min) (point-max))
250 (insert (mm-decode-string text charset)))
251 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
253 (w3m-region (point-min) (point-max)))
254 (when mm-inline-text-html-with-w3m-keymap
256 (point-min) (point-max)
257 (nconc (mm-w3m-local-map-property)
258 '(mm-inline-text-html-with-w3m t)))))
259 (mm-handle-set-undisplayer
262 (let (buffer-read-only)
263 (if (functionp 'remove-specifier)
264 (mapcar (lambda (prop)
266 (face-property 'default prop)
268 '(background background-pixmap foreground)))
269 (delete-region ,(point-min-marker)
270 ,(point-max-marker))))))))
272 (defun mm-links-remove-leading-blank ()
273 ;; Delete the annoying three spaces preceding each line of links
275 (goto-char (point-min))
276 (while (re-search-forward "^ " nil t)
277 (delete-region (match-beginning 0) (match-end 0))))
279 (defun mm-inline-wash-with-file (post-func cmd &rest args)
280 (let ((file (mm-make-temp-file
281 (expand-file-name "mm" mm-tmp-directory))))
282 (let ((coding-system-for-write 'binary))
283 (write-region (point-min) (point-max) file nil 'silent))
284 (delete-region (point-min) (point-max))
286 (apply 'call-process cmd nil t nil (mapcar 'eval args))
288 (and post-func (funcall post-func))))
290 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
291 (let ((coding-system-for-write 'binary))
292 (apply 'call-process-region (point-min) (point-max)
294 (and post-func (funcall post-func)))
296 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
297 (let ((source (mm-get-part handle)))
300 (mm-with-unibyte-buffer
302 (apply 'mm-inline-wash-with-file post-func cmd args)
305 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
306 (let ((source (mm-get-part handle)))
309 (mm-with-unibyte-buffer
311 (apply 'mm-inline-wash-with-stdin post-func cmd args)
314 (defun mm-inline-render-with-function (handle func &rest args)
315 (let ((source (mm-get-part handle)))
318 (mm-with-unibyte-buffer
323 (defun mm-inline-text-html (handle)
324 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
325 (entry (assq func mm-text-html-renderer-alist))
328 (setq func (cdr entry)))
331 (funcall func handle))
333 (apply (car func) handle (cdr func))))))
335 (defun mm-inline-text-vcard (handle)
336 (let (buffer-read-only)
341 (if (fboundp 'vcard-pretty-print)
342 (vcard-pretty-print (mm-get-part handle))
344 (vcard-parse-string (mm-get-part handle)
345 'vcard-standard-filter))))))))
347 (defun mm-inline-text (handle)
349 (type (mm-handle-media-subtype handle))
350 (charset (mail-content-type-get
351 (mm-handle-type handle) 'charset))
353 (if (or (eq charset 'gnus-decoded)
354 ;; This is probably not entirely correct, but
355 ;; makes rfc822 parts with embedded multiparts work.
356 (eq mail-parse-charset 'gnus-decoded))
358 (narrow-to-region (point) (point))
359 (mm-insert-part handle)
360 (goto-char (point-max)))
361 (insert (mm-decode-string (mm-get-part handle) charset)))
362 (when (and (equal type "plain")
363 (equal (cdr (assoc 'format (mm-handle-type handle)))
366 (narrow-to-region b (point))
369 (goto-char (point-max))))
371 (narrow-to-region b (point))
372 (set-text-properties (point-min) (point-max) nil)
373 (when (or (equal type "enriched")
374 (equal type "richtext"))
376 (enriched-decode (point-min) (point-max))))
377 (mm-handle-set-undisplayer
380 (let (buffer-read-only)
381 (delete-region ,(point-min-marker)
382 ,(point-max-marker))))))))
384 (defun mm-insert-inline (handle text)
385 "Insert TEXT inline from HANDLE."
388 (mm-handle-set-undisplayer
391 (let (buffer-read-only)
392 (delete-region ,(set-marker (make-marker) b)
393 ,(set-marker (make-marker) (point))))))))
395 (defun mm-inline-audio (handle)
396 (message "Not implemented"))
398 (defun mm-view-sound-file ()
399 (message "Not implemented"))
401 (defun mm-w3-prepare-buffer ()
403 (let ((url-standalone-mode t)
404 (url-gateway-unplugged t)
405 (w3-honor-stylesheets nil))
406 (w3-prepare-buffer)))
408 (defun mm-view-message ()
409 (mm-enable-multibyte)
411 (let (gnus-article-mime-handles)
412 ;; Double decode problem may happen. See mm-inline-message.
413 (run-hooks 'gnus-article-decode-hook)
414 (gnus-article-prepare-display)
415 (setq handles gnus-article-mime-handles))
417 (setq gnus-article-mime-handles
418 (mm-merge-handles gnus-article-mime-handles handles))))
420 (goto-char (point-min)))
422 (defun mm-inline-message (handle)
425 (charset (mail-content-type-get
426 (mm-handle-type handle) 'charset))
427 gnus-displaying-mime handles)
430 (setq charset (intern (downcase charset)))
431 (when (eq charset 'us-ascii)
435 (narrow-to-region b b)
436 (mm-insert-part handle)
437 (let (gnus-article-mime-handles
438 ;; disable prepare hook
439 gnus-article-prepare-hook
440 (gnus-newsgroup-charset
441 (or charset gnus-newsgroup-charset)))
442 (run-hooks 'gnus-article-decode-hook)
443 (gnus-article-prepare-display)
444 (setq handles gnus-article-mime-handles))
445 (goto-char (point-min))
448 (goto-char (point-max))
451 (insert "----------\n\n")
453 (setq gnus-article-mime-handles
454 (mm-merge-handles gnus-article-mime-handles handles)))
455 (mm-handle-set-undisplayer
458 (let (buffer-read-only)
459 (if (fboundp 'remove-specifier)
460 ;; This is only valid on XEmacs.
461 (mapcar (lambda (prop)
463 (face-property 'default prop) (current-buffer)))
464 '(background background-pixmap foreground)))
465 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
467 (defun mm-display-inline-fontify (handle mode)
469 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
470 ;; on for buffers whose name begins with " ". That's why we use
471 ;; save-current-buffer/get-buffer-create rather than
474 (set-buffer (generate-new-buffer "*fontification*"))
477 (buffer-disable-undo)
478 (mm-insert-part handle)
481 (let ((font-lock-verbose nil))
482 ;; I find font-lock a bit too verbose.
483 (font-lock-fontify-buffer))
484 ;; By default, XEmacs font-lock uses non-duplicable text
485 ;; properties. This code forces all the text properties
486 ;; to be copied along with the text.
487 (when (fboundp 'extent-list)
488 (map-extents (lambda (ext ignored)
489 (set-extent-property ext 'duplicable t)
491 nil nil nil nil nil 'text-prop))
492 (setq text (buffer-string)))
493 (kill-buffer (current-buffer))))
494 (mm-insert-inline handle text)))
496 ;; Shouldn't these functions check whether the user even wants to use
497 ;; font-lock? At least under XEmacs, this fontification is pretty
498 ;; much unconditional. Also, it would be nice to change for the size
499 ;; of the fontified region.
501 (defun mm-display-patch-inline (handle)
502 (mm-display-inline-fontify handle 'diff-mode))
504 (defun mm-display-elisp-inline (handle)
505 (mm-display-inline-fontify handle 'emacs-lisp-mode))
507 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
508 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
509 (defvar mm-pkcs7-signed-magic
510 (mm-string-as-unibyte
512 (mapcar 'char-to-string
513 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
514 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
515 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
516 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
518 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
519 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
520 (defvar mm-pkcs7-enveloped-magic
521 (mm-string-as-unibyte
523 (mapcar 'char-to-string
524 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
525 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
526 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
527 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
529 (defun mm-view-pkcs7-get-type (handle)
530 (mm-with-unibyte-buffer
531 (mm-insert-part handle)
532 (cond ((looking-at mm-pkcs7-enveloped-magic)
534 ((looking-at mm-pkcs7-signed-magic)
537 (error "Could not identify PKCS#7 type")))))
539 (defun mm-view-pkcs7 (handle)
540 (case (mm-view-pkcs7-get-type handle)
541 (enveloped (mm-view-pkcs7-decrypt handle))
542 (signed (mm-view-pkcs7-verify handle))
543 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
545 (defun mm-view-pkcs7-verify (handle)
546 ;; A bogus implementation of PKCS#7. FIXME::
547 (mm-insert-part handle)
548 (goto-char (point-min))
549 (if (search-forward "Content-Type: " nil t)
550 (delete-region (point-min) (match-beginning 0)))
551 (goto-char (point-max))
552 (if (re-search-backward "--\r?\n?" nil t)
553 (delete-region (match-end 0) (point-max)))
554 (goto-char (point-min))
555 (while (search-forward "\r\n" nil t)
556 (replace-match "\n"))
557 (message "Verify signed PKCS#7 message is unimplemented.")
561 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
563 (defun mm-view-pkcs7-decrypt (handle)
564 (insert-buffer-substring (mm-handle-buffer handle))
565 (goto-char (point-min))
566 (insert "MIME-Version: 1.0\n")
567 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
568 (smime-decrypt-region
569 (point-min) (point-max)
570 (if (= (length smime-keys) 1)
572 (smime-get-key-by-email
573 (gnus-completing-read-maybe-default
574 (concat "Decipher using which key? "
575 (if smime-keys (concat "(default " (caar smime-keys) ") ")
577 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
578 (goto-char (point-min))
579 (while (search-forward "\r\n" nil t)
580 (replace-match "\n"))
581 (goto-char (point-min)))
585 ;;; mm-view.el ends here