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")
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")
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))
77 (put-image (mm-get-image handle) b)
78 (mm-handle-set-undisplayer
80 `(lambda () (remove-images ,b (1+ ,b))))))
82 (defun mm-inline-image-xemacs (handle)
86 (annot (make-annotation (mm-get-image handle) nil 'text))
88 (mm-handle-set-undisplayer
91 (let (buffer-read-only)
92 (delete-annotation ,annot)
93 (delete-region ,(set-marker (make-marker) b)
94 ,(set-marker (make-marker) (point))))))
95 (set-extent-property annot 'mm t)
96 (set-extent-property annot 'duplicable t)))
99 (if (featurep 'xemacs)
100 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
101 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
103 (defvar mm-w3-setup nil)
104 (defun mm-setup-w3 ()
111 (setq mm-w3-setup t)))
113 (defun mm-inline-text-html-render-with-w3 (handle)
115 (let ((text (mm-get-part handle))
117 (url-standalone-mode t)
118 (url-gateway-unplugged t)
119 (w3-honor-stylesheets nil)
121 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
122 (width (window-width))
123 (charset (mail-content-type-get
124 (mm-handle-type handle) 'charset)))
128 (narrow-to-region b (point))
129 (goto-char (point-min))
130 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
132 w3-meta-content-type-charset-regexp nil t))
133 (and (boundp 'w3-meta-charset-content-type-regexp)
135 w3-meta-charset-content-type-regexp nil t)))
137 (or (let ((bsubstr (buffer-substring-no-properties
140 (if (fboundp 'w3-coding-system-for-mime-charset)
141 (w3-coding-system-for-mime-charset bsubstr)
142 (mm-charset-to-coding-system bsubstr)))
144 (delete-region (point-min) (point-max))
145 (insert (mm-decode-string text charset))
146 (save-window-excursion
148 (let ((w3-strict-width width)
149 ;; Don't let w3 set the global version of
151 (fill-column fill-column))
152 (if (or debug-on-error debug-on-quit)
153 (w3-region (point-min) (point-max))
155 (w3-region (point-min) (point-max))
157 (delete-region (point-min) (point-max))
159 (charset (mail-content-type-get
160 (mm-handle-type handle) 'charset)))
161 (if (or (eq charset 'gnus-decoded)
162 (eq mail-parse-charset 'gnus-decoded))
164 (narrow-to-region (point) (point))
165 (mm-insert-part handle)
166 (goto-char (point-max)))
167 (insert (mm-decode-string (mm-get-part handle)
170 "Error while rendering html; showing as text/plain")))))))
171 (mm-handle-set-undisplayer
174 (let (buffer-read-only)
175 (if (functionp 'remove-specifier)
176 (mapcar (lambda (prop)
178 (face-property 'default prop)
180 '(background background-pixmap foreground)))
181 (delete-region ,(point-min-marker)
182 ,(point-max-marker)))))))))
184 (defvar mm-w3m-setup nil
185 "Whether gnus-article-mode has been setup to use emacs-w3m.")
187 (defun mm-setup-w3m ()
188 "Setup gnus-article-mode to use emacs-w3m."
191 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
192 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
193 w3m-cid-retrieve-function-alist))
194 (setq mm-w3m-setup t)))
196 (defun mm-w3m-cid-retrieve (url &rest args)
197 "Insert a content pointed by URL if it has the cid: scheme."
198 (when (string-match "\\`cid:" url)
199 (setq url (concat "<" (substring url (match-end 0)) ">"))
201 (dolist (handle (with-current-buffer w3m-current-buffer
202 gnus-article-mime-handles))
203 (when (and (listp handle)
204 (equal url (mm-handle-id handle)))
205 (mm-insert-part handle)
206 (throw 'found-handle (mm-handle-media-type handle)))))))
209 (unless (or (featurep 'xemacs)
210 (>= emacs-major-version 21))
211 (defvar mm-w3m-mode-map nil
212 "Keymap for text/html part rendered by `mm-w3m-preview-text/html'.
213 This map is overwritten by `mm-w3m-local-map-property' based on the
214 value of `w3m-minor-mode-map'. Therefore, in order to add some
215 commands to this map, add them to `w3m-minor-mode-map' instead of this
218 (defun mm-w3m-local-map-property ()
219 (when (and (boundp 'w3m-minor-mode-map) w3m-minor-mode-map)
220 (if (or (featurep 'xemacs)
221 (>= emacs-major-version 21))
222 (list 'keymap w3m-minor-mode-map)
226 (setq mm-w3m-mode-map (copy-keymap w3m-minor-mode-map))
227 (set-keymap-parent mm-w3m-mode-map gnus-article-mode-map)
228 mm-w3m-mode-map))))))
230 (defun mm-inline-text-html-render-with-w3m (handle)
231 "Render a text/html part using emacs-w3m."
233 (let ((text (mm-get-part handle))
235 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
239 (narrow-to-region b (point))
240 (goto-char (point-min))
241 (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
242 (setq charset (or (w3m-charset-to-coding-system (match-string 2))
245 (delete-region (point-min) (point-max))
246 (insert (mm-decode-string text charset)))
247 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
248 (w3m-display-inline-images mm-inline-text-html-with-images)
250 (w3m-region (point-min) (point-max)))
251 (when mm-inline-text-html-with-w3m-keymap
253 (point-min) (point-max)
254 (nconc (mm-w3m-local-map-property)
255 '(mm-inline-text-html-with-w3m t)))))
256 (mm-handle-set-undisplayer
259 (let (buffer-read-only)
260 (if (functionp 'remove-specifier)
261 (mapcar (lambda (prop)
263 (face-property 'default prop)
265 '(background background-pixmap foreground)))
266 (delete-region ,(point-min-marker)
267 ,(point-max-marker))))))))
269 (defun mm-links-remove-leading-blank ()
270 ;; Delete the annoying three spaces preceding each line of links
272 (goto-char (point-min))
273 (while (re-search-forward "^ " nil t)
274 (delete-region (match-beginning 0) (match-end 0))))
276 (defun mm-inline-wash-with-file (post-func cmd &rest args)
277 (let ((file (mm-make-temp-file
278 (expand-file-name "mm" mm-tmp-directory))))
279 (let ((coding-system-for-write 'binary))
280 (write-region (point-min) (point-max) file nil 'silent))
281 (delete-region (point-min) (point-max))
283 (apply 'call-process cmd nil t nil (mapcar 'eval args))
285 (and post-func (funcall post-func))))
287 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
288 (let ((coding-system-for-write 'binary))
289 (apply 'call-process-region (point-min) (point-max)
291 (and post-func (funcall post-func)))
293 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
294 (let ((source (mm-get-part handle)))
297 (mm-with-unibyte-buffer
299 (apply 'mm-inline-wash-with-file post-func cmd args)
302 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
303 (let ((source (mm-get-part handle)))
306 (mm-with-unibyte-buffer
308 (apply 'mm-inline-wash-with-stdin post-func cmd args)
311 (defun mm-inline-render-with-function (handle func &rest args)
312 (let ((source (mm-get-part handle)))
315 (mm-with-unibyte-buffer
320 (defun mm-inline-text-html (handle)
321 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
322 (entry (assq func mm-text-html-renderer-alist))
325 (setq func (cdr entry)))
327 ((gnus-functionp func)
328 (funcall func handle))
330 (apply (car func) handle (cdr func))))))
332 (defun mm-inline-text-vcard (handle)
333 (let (buffer-read-only)
338 (if (fboundp 'vcard-pretty-print)
339 (vcard-pretty-print (mm-get-part handle))
341 (vcard-parse-string (mm-get-part handle)
342 'vcard-standard-filter))))))))
344 (defun mm-inline-text (handle)
346 (type (mm-handle-media-subtype handle))
347 (charset (mail-content-type-get
348 (mm-handle-type handle) 'charset))
350 (if (or (eq charset 'gnus-decoded)
351 ;; This is probably not entirely correct, but
352 ;; makes rfc822 parts with embedded multiparts work.
353 (eq mail-parse-charset 'gnus-decoded))
355 (narrow-to-region (point) (point))
356 (mm-insert-part handle)
357 (goto-char (point-max)))
358 (insert (mm-decode-string (mm-get-part handle) charset)))
359 (when (and (equal type "plain")
360 (equal (cdr (assoc 'format (mm-handle-type handle)))
363 (narrow-to-region b (point))
366 (goto-char (point-max))))
368 (narrow-to-region b (point))
369 (set-text-properties (point-min) (point-max) nil)
370 (when (or (equal type "enriched")
371 (equal type "richtext"))
373 (enriched-decode (point-min) (point-max))))
374 (mm-handle-set-undisplayer
377 (let (buffer-read-only)
378 (delete-region ,(point-min-marker)
379 ,(point-max-marker))))))))
381 (defun mm-insert-inline (handle text)
382 "Insert TEXT inline from HANDLE."
385 (mm-handle-set-undisplayer
388 (let (buffer-read-only)
389 (delete-region ,(set-marker (make-marker) b)
390 ,(set-marker (make-marker) (point))))))))
392 (defun mm-inline-audio (handle)
393 (message "Not implemented"))
395 (defun mm-view-sound-file ()
396 (message "Not implemented"))
398 (defun mm-w3-prepare-buffer ()
400 (let ((url-standalone-mode t)
401 (url-gateway-unplugged t)
402 (w3-honor-stylesheets nil))
403 (w3-prepare-buffer)))
405 (defun mm-view-message ()
406 (mm-enable-multibyte)
408 (let (gnus-article-mime-handles)
409 ;; Double decode problem may happen. See mm-inline-message.
410 (run-hooks 'gnus-article-decode-hook)
411 (gnus-article-prepare-display)
412 (setq handles gnus-article-mime-handles))
414 (setq gnus-article-mime-handles
415 (mm-merge-handles gnus-article-mime-handles handles))))
417 (goto-char (point-min)))
419 (defun mm-inline-message (handle)
422 (charset (mail-content-type-get
423 (mm-handle-type handle) 'charset))
424 gnus-displaying-mime handles)
427 (setq charset (intern (downcase charset)))
428 (when (eq charset 'us-ascii)
432 (narrow-to-region b b)
433 (mm-insert-part handle)
434 (let (gnus-article-mime-handles
435 ;; disable prepare hook
436 gnus-article-prepare-hook
437 (gnus-newsgroup-charset
438 (or charset gnus-newsgroup-charset)))
439 (run-hooks 'gnus-article-decode-hook)
440 (gnus-article-prepare-display)
441 (setq handles gnus-article-mime-handles))
442 (goto-char (point-min))
445 (goto-char (point-max))
448 (insert "----------\n\n")
450 (setq gnus-article-mime-handles
451 (mm-merge-handles gnus-article-mime-handles handles)))
452 (mm-handle-set-undisplayer
455 (let (buffer-read-only)
456 (if (fboundp 'remove-specifier)
457 ;; This is only valid on XEmacs.
458 (mapcar (lambda (prop)
460 (face-property 'default prop) (current-buffer)))
461 '(background background-pixmap foreground)))
462 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
464 (defun mm-display-inline-fontify (handle mode)
466 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
467 ;; on for buffers whose name begins with " ". That's why we use
468 ;; save-current-buffer/get-buffer-create rather than
471 (set-buffer (generate-new-buffer "*fontification*"))
474 (buffer-disable-undo)
475 (mm-insert-part handle)
478 (let ((font-lock-verbose nil))
479 ;; I find font-lock a bit too verbose.
480 (font-lock-fontify-buffer))
481 ;; By default, XEmacs font-lock uses non-duplicable text
482 ;; properties. This code forces all the text properties
483 ;; to be copied along with the text.
484 (when (fboundp 'extent-list)
485 (map-extents (lambda (ext ignored)
486 (set-extent-property ext 'duplicable t)
488 nil nil nil nil nil 'text-prop))
489 (setq text (buffer-string)))
490 (kill-buffer (current-buffer))))
491 (mm-insert-inline handle text)))
493 ;; Shouldn't these functions check whether the user even wants to use
494 ;; font-lock? At least under XEmacs, this fontification is pretty
495 ;; much unconditional. Also, it would be nice to change for the size
496 ;; of the fontified region.
498 (defun mm-display-patch-inline (handle)
499 (mm-display-inline-fontify handle 'diff-mode))
501 (defun mm-display-elisp-inline (handle)
502 (mm-display-inline-fontify handle 'emacs-lisp-mode))
504 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
505 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
506 (defvar mm-pkcs7-signed-magic
507 (mm-string-as-unibyte
509 (mapcar 'char-to-string
510 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
511 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
512 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
513 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
515 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
516 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
517 (defvar mm-pkcs7-enveloped-magic
518 (mm-string-as-unibyte
520 (mapcar 'char-to-string
521 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
522 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
523 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
524 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
526 (defun mm-view-pkcs7-get-type (handle)
527 (mm-with-unibyte-buffer
528 (mm-insert-part handle)
529 (cond ((looking-at mm-pkcs7-enveloped-magic)
531 ((looking-at mm-pkcs7-signed-magic)
534 (error "Could not identify PKCS#7 type")))))
536 (defun mm-view-pkcs7 (handle)
537 (case (mm-view-pkcs7-get-type handle)
538 (enveloped (mm-view-pkcs7-decrypt handle))
539 (signed (mm-view-pkcs7-verify handle))
540 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
542 (defun mm-view-pkcs7-verify (handle)
543 ;; A bogus implementation of PKCS#7. FIXME::
544 (mm-insert-part handle)
545 (goto-char (point-min))
546 (if (search-forward "Content-Type: " nil t)
547 (delete-region (point-min) (match-beginning 0)))
548 (goto-char (point-max))
549 (if (re-search-backward "--\r?\n?" nil t)
550 (delete-region (match-end 0) (point-max)))
551 (goto-char (point-min))
552 (while (search-forward "\r\n" nil t)
553 (replace-match "\n"))
554 (message "Verify signed PKCS#7 message is unimplemented.")
558 (defun mm-view-pkcs7-decrypt (handle)
559 (insert-buffer (mm-handle-buffer handle))
560 (goto-char (point-min))
561 (insert "MIME-Version: 1.0\n")
562 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
563 (smime-decrypt-region
564 (point-min) (point-max)
565 (if (= (length smime-keys) 1)
567 (smime-get-key-by-email
569 (concat "Decipher using which key? "
570 (if smime-keys (concat "(default " (caar smime-keys) ") ")
572 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
573 (goto-char (point-min))
574 (while (search-forward "\r\n" nil t)
575 (replace-match "\n"))
576 (goto-char (point-min)))
580 ;;; mm-view.el ends here