1 ;;; mm-view.el --- functions for viewing MIME objects
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
28 (eval-when-compile (require 'cl))
35 (autoload 'gnus-article-prepare-display "gnus-art")
36 (autoload 'vcard-parse-string "vcard")
37 (autoload 'vcard-format-string "vcard")
38 (autoload 'fill-flowed "flow-fill")
39 (autoload 'html2text "html2text")
40 (unless (fboundp 'diff-mode)
41 (autoload 'diff-mode "diff-mode" "" t nil)))
43 (defvar gnus-article-mime-handles)
44 (defvar gnus-newsgroup-charset)
46 (defvar w3m-cid-retrieve-function-alist)
47 (defvar w3m-current-buffer)
48 (defvar w3m-display-inline-images)
49 (defvar w3m-minor-mode-map)
51 (defvar mm-text-html-renderer-alist
52 '((w3 . mm-inline-text-html-render-with-w3)
53 (w3m . mm-inline-text-html-render-with-w3m)
54 (w3m-standalone mm-inline-render-with-stdin nil
55 "w3m" "-dump" "-T" "text/html")
56 (links mm-inline-render-with-file
57 mm-links-remove-leading-blank
59 (lynx mm-inline-render-with-stdin nil
60 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
61 (html2text mm-inline-render-with-function html2text))
62 "The attributes of renderer types for text/html.")
64 (defvar mm-text-html-washer-alist
65 '((w3 . gnus-article-wash-html-with-w3)
66 (w3m . gnus-article-wash-html-with-w3m)
67 (w3m-standalone mm-inline-wash-with-stdin nil
68 "w3m" "-dump" "-T" "text/html")
69 (links mm-inline-wash-with-file
70 mm-links-remove-leading-blank
72 (lynx mm-inline-wash-with-stdin nil
73 "lynx" "-dump" "-force_html" "-stdin" "-nolist")
74 (html2text html2text))
75 "The attributes of washer types for text/html.")
77 ;;; Internal variables.
80 ;;; Functions for displaying various formats inline
83 (defun mm-inline-image-emacs (handle)
84 (let ((b (point-marker))
86 (put-image (mm-get-image handle) b)
88 (mm-handle-set-undisplayer
94 (delete-region b (+ b 2)))))))
96 (defun mm-inline-image-xemacs (handle)
99 (let ((annot (make-annotation (mm-get-image handle) nil 'text))
101 (mm-handle-set-undisplayer
104 (let ((b ,(point-marker))
106 (delete-annotation ,annot)
107 (delete-region (- b 2) b))))
108 (set-extent-property annot 'mm t)
109 (set-extent-property annot 'duplicable t)))
112 (if (featurep 'xemacs)
113 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
114 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
116 (defvar mm-w3-setup nil)
117 (defun mm-setup-w3 ()
124 (setq mm-w3-setup t)))
126 (defun mm-inline-text-html-render-with-w3 (handle)
128 (let ((text (mm-get-part handle))
130 (url-standalone-mode t)
131 (url-gateway-unplugged t)
132 (w3-honor-stylesheets nil)
134 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
135 (width (window-width))
136 (charset (mail-content-type-get
137 (mm-handle-type handle) 'charset)))
141 (narrow-to-region b (point))
142 (goto-char (point-min))
143 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
145 w3-meta-content-type-charset-regexp nil t))
146 (and (boundp 'w3-meta-charset-content-type-regexp)
148 w3-meta-charset-content-type-regexp nil t)))
150 (or (let ((bsubstr (buffer-substring-no-properties
153 (if (fboundp 'w3-coding-system-for-mime-charset)
154 (w3-coding-system-for-mime-charset bsubstr)
155 (mm-charset-to-coding-system bsubstr)))
157 (delete-region (point-min) (point-max))
158 (insert (mm-decode-string text charset))
159 (save-window-excursion
161 (let ((w3-strict-width width)
162 ;; Don't let w3 set the global version of
164 (fill-column fill-column))
165 (if (or debug-on-error debug-on-quit)
166 (w3-region (point-min) (point-max))
168 (w3-region (point-min) (point-max))
170 (delete-region (point-min) (point-max))
172 (charset (mail-content-type-get
173 (mm-handle-type handle) 'charset)))
174 (if (or (eq charset 'gnus-decoded)
175 (eq mail-parse-charset 'gnus-decoded))
177 (narrow-to-region (point) (point))
178 (mm-insert-part handle)
179 (goto-char (point-max)))
180 (insert (mm-decode-string (mm-get-part handle)
183 "Error while rendering html; showing as text/plain")))))))
184 (mm-handle-set-undisplayer
187 (let (buffer-read-only)
188 (if (functionp 'remove-specifier)
189 (mapcar (lambda (prop)
191 (face-property 'default prop)
193 '(background background-pixmap foreground)))
194 (delete-region ,(point-min-marker)
195 ,(point-max-marker)))))))))
197 (defvar mm-w3m-setup nil
198 "Whether gnus-article-mode has been setup to use emacs-w3m.")
200 (defun mm-setup-w3m ()
201 "Setup gnus-article-mode to use emacs-w3m."
204 (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
205 (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
206 w3m-cid-retrieve-function-alist))
207 (setq mm-w3m-setup t))
208 (setq w3m-display-inline-images mm-inline-text-html-with-images))
210 (defun mm-w3m-cid-retrieve-1 (url handle)
211 (dolist (elem handle)
213 (if (equal url (mm-handle-id elem))
215 (mm-insert-part elem)
216 (throw 'found-handle (mm-handle-media-type elem))))
217 (if (equal "multipart" (mm-handle-media-supertype elem))
218 (mm-w3m-cid-retrieve-1 url elem)))))
220 (defun mm-w3m-cid-retrieve (url &rest args)
221 "Insert a content pointed by URL if it has the cid: scheme."
222 (when (string-match "\\`cid:" url)
224 (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
225 (with-current-buffer w3m-current-buffer
226 gnus-article-mime-handles)))))
228 (defun mm-inline-text-html-render-with-w3m (handle)
229 "Render a text/html part using emacs-w3m."
231 (let ((text (mm-get-part handle))
233 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
235 (insert (if charset (mm-decode-string text charset) text))
237 (narrow-to-region b (point))
239 (goto-char (point-min))
240 (when (setq charset (w3m-detect-meta-charset))
241 (delete-region (point-min) (point-max))
242 (insert (mm-decode-string text charset))))
243 (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
245 (w3m-region (point-min) (point-max) nil charset))
246 (when (and mm-inline-text-html-with-w3m-keymap
247 (boundp 'w3m-minor-mode-map)
250 (point-min) (point-max)
251 (list 'keymap w3m-minor-mode-map
252 ;; Put the mark meaning this part was rendered by emacs-w3m.
253 'mm-inline-text-html-with-w3m t))))
254 (mm-handle-set-undisplayer
257 (let (buffer-read-only)
258 (if (functionp 'remove-specifier)
259 (mapcar (lambda (prop)
261 (face-property 'default prop)
263 '(background background-pixmap foreground)))
264 (delete-region ,(point-min-marker)
265 ,(point-max-marker))))))))
267 (defun mm-links-remove-leading-blank ()
268 ;; Delete the annoying three spaces preceding each line of links
270 (goto-char (point-min))
271 (while (re-search-forward "^ " nil t)
272 (delete-region (match-beginning 0) (match-end 0))))
274 (defun mm-inline-wash-with-file (post-func cmd &rest args)
275 (let ((file (mm-make-temp-file
276 (expand-file-name "mm" mm-tmp-directory))))
277 (let ((coding-system-for-write 'binary))
278 (write-region (point-min) (point-max) file nil 'silent))
279 (delete-region (point-min) (point-max))
281 (apply 'call-process cmd nil t nil (mapcar 'eval args))
283 (and post-func (funcall post-func))))
285 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
286 (let ((coding-system-for-write 'binary))
287 (apply 'call-process-region (point-min) (point-max)
289 (and post-func (funcall post-func)))
291 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
292 (let ((source (mm-get-part handle)))
295 (mm-with-unibyte-buffer
297 (apply 'mm-inline-wash-with-file post-func cmd args)
300 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
301 (let ((source (mm-get-part handle)))
304 (mm-with-unibyte-buffer
306 (apply 'mm-inline-wash-with-stdin post-func cmd args)
309 (defun mm-inline-render-with-function (handle func &rest args)
310 (let ((source (mm-get-part handle))
311 (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
314 (mm-with-multibyte-buffer
316 (mm-decode-string source charset)
321 (defun mm-inline-text-html (handle)
322 (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
323 (entry (assq func mm-text-html-renderer-alist))
326 (setq func (cdr entry)))
329 (funcall func handle))
331 (apply (car func) handle (cdr func))))))
333 (defun mm-inline-text-vcard (handle)
334 (let (buffer-read-only)
339 (if (fboundp 'vcard-pretty-print)
340 (vcard-pretty-print (mm-get-part handle))
342 (vcard-parse-string (mm-get-part handle)
343 'vcard-standard-filter))))))))
345 (defun mm-inline-text (handle)
347 (type (mm-handle-media-subtype handle))
348 (charset (mail-content-type-get
349 (mm-handle-type handle) 'charset))
351 (if (or (eq charset 'gnus-decoded)
352 ;; This is probably not entirely correct, but
353 ;; makes rfc822 parts with embedded multiparts work.
354 (eq mail-parse-charset 'gnus-decoded))
356 (narrow-to-region (point) (point))
357 (mm-insert-part handle)
358 (goto-char (point-max)))
359 (insert (mm-decode-string (mm-get-part handle) charset)))
360 (when (and (equal type "plain")
361 (equal (cdr (assoc 'format (mm-handle-type handle)))
364 (narrow-to-region b (point))
367 (goto-char (point-max))))
369 (narrow-to-region b (point))
370 (set-text-properties (point-min) (point-max) nil)
371 (when (or (equal type "enriched")
372 (equal type "richtext"))
374 (enriched-decode (point-min) (point-max))))
375 (mm-handle-set-undisplayer
378 (let (buffer-read-only)
379 (delete-region ,(point-min-marker)
380 ,(point-max-marker))))))))
382 (defun mm-insert-inline (handle text)
383 "Insert TEXT inline from HANDLE."
386 (mm-handle-set-undisplayer
389 (let (buffer-read-only)
390 (delete-region ,(set-marker (make-marker) b)
391 ,(set-marker (make-marker) (point))))))))
393 (defun mm-inline-audio (handle)
394 (message "Not implemented"))
396 (defun mm-view-sound-file ()
397 (message "Not implemented"))
399 (defun mm-w3-prepare-buffer ()
401 (let ((url-standalone-mode t)
402 (url-gateway-unplugged t)
403 (w3-honor-stylesheets nil))
404 (w3-prepare-buffer)))
406 (defun mm-view-message ()
407 (mm-enable-multibyte)
409 (let (gnus-article-mime-handles)
410 ;; Double decode problem may happen. See mm-inline-message.
411 (run-hooks 'gnus-article-decode-hook)
412 (gnus-article-prepare-display)
413 (setq handles gnus-article-mime-handles))
415 (setq gnus-article-mime-handles
416 (mm-merge-handles gnus-article-mime-handles handles))))
418 (goto-char (point-min)))
420 (defun mm-inline-message (handle)
423 (charset (mail-content-type-get
424 (mm-handle-type handle) 'charset))
425 gnus-displaying-mime handles)
428 (setq charset (intern (downcase charset)))
429 (when (eq charset 'us-ascii)
433 (narrow-to-region b b)
434 (mm-insert-part handle)
435 (let (gnus-article-mime-handles
436 ;; disable prepare hook
437 gnus-article-prepare-hook
438 (gnus-newsgroup-charset
439 (or charset gnus-newsgroup-charset)))
440 (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
441 (run-hooks 'gnus-article-decode-hook))
442 (gnus-article-prepare-display)
443 (setq handles gnus-article-mime-handles))
444 (goto-char (point-min))
447 (goto-char (point-max))
450 (insert "----------\n\n")
452 (setq gnus-article-mime-handles
453 (mm-merge-handles gnus-article-mime-handles handles)))
454 (mm-handle-set-undisplayer
457 (let (buffer-read-only)
458 (if (fboundp 'remove-specifier)
459 ;; This is only valid on XEmacs.
460 (mapcar (lambda (prop)
462 (face-property 'default prop) (current-buffer)))
463 '(background background-pixmap foreground)))
464 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
466 (defun mm-display-inline-fontify (handle mode)
468 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
469 ;; on for buffers whose name begins with " ". That's why we use
470 ;; save-current-buffer/get-buffer-create rather than
473 (set-buffer (generate-new-buffer "*fontification*"))
476 (buffer-disable-undo)
477 (mm-insert-part handle)
479 ;; Inhibit font-lock this time (*-mode-hook might run
480 ;; `turn-on-font-lock') so that jit-lock may not turn off
481 ;; font-lock immediately after this.
482 (let ((font-lock-mode t))
484 (let ((font-lock-verbose nil))
485 ;; I find font-lock a bit too verbose.
486 (font-lock-fontify-buffer))
487 ;; By default, XEmacs font-lock uses non-duplicable text
488 ;; properties. This code forces all the text properties
489 ;; to be copied along with the text.
490 (when (fboundp 'extent-list)
491 (map-extents (lambda (ext ignored)
492 (set-extent-property ext 'duplicable t)
494 nil nil nil nil nil 'text-prop))
495 (setq text (buffer-string)))
496 (kill-buffer (current-buffer))))
497 (mm-insert-inline handle text)))
499 ;; Shouldn't these functions check whether the user even wants to use
500 ;; font-lock? At least under XEmacs, this fontification is pretty
501 ;; much unconditional. Also, it would be nice to change for the size
502 ;; of the fontified region.
504 (defun mm-display-patch-inline (handle)
505 (mm-display-inline-fontify handle 'diff-mode))
507 (defun mm-display-elisp-inline (handle)
508 (mm-display-inline-fontify handle 'emacs-lisp-mode))
510 ;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
511 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
512 (defvar mm-pkcs7-signed-magic
513 (mm-string-as-unibyte
515 (mapcar 'char-to-string
516 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
517 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
518 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
519 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
521 ;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
522 ;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
523 (defvar mm-pkcs7-enveloped-magic
524 (mm-string-as-unibyte
526 (mapcar 'char-to-string
527 (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
528 ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
529 ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
530 ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
532 (defun mm-view-pkcs7-get-type (handle)
533 (mm-with-unibyte-buffer
534 (mm-insert-part handle)
535 (cond ((looking-at mm-pkcs7-enveloped-magic)
537 ((looking-at mm-pkcs7-signed-magic)
540 (error "Could not identify PKCS#7 type")))))
542 (defun mm-view-pkcs7 (handle)
543 (case (mm-view-pkcs7-get-type handle)
544 (enveloped (mm-view-pkcs7-decrypt handle))
545 (signed (mm-view-pkcs7-verify handle))
546 (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
548 (defun mm-view-pkcs7-verify (handle)
549 ;; A bogus implementation of PKCS#7. FIXME::
550 (mm-insert-part handle)
551 (goto-char (point-min))
552 (if (search-forward "Content-Type: " nil t)
553 (delete-region (point-min) (match-beginning 0)))
554 (goto-char (point-max))
555 (if (re-search-backward "--\r?\n?" nil t)
556 (delete-region (match-end 0) (point-max)))
557 (goto-char (point-min))
558 (while (search-forward "\r\n" nil t)
559 (replace-match "\n"))
560 (message "Verify signed PKCS#7 message is unimplemented.")
564 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
566 (defun mm-view-pkcs7-decrypt (handle)
567 (insert-buffer-substring (mm-handle-buffer handle))
568 (goto-char (point-min))
569 (insert "MIME-Version: 1.0\n")
570 (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
571 (smime-decrypt-region
572 (point-min) (point-max)
573 (if (= (length smime-keys) 1)
575 (smime-get-key-by-email
576 (gnus-completing-read-maybe-default
577 (concat "Decipher using which key? "
578 (if smime-keys (concat "(default " (caar smime-keys) ") ")
580 smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
581 (goto-char (point-min))
582 (while (search-forward "\r\n" nil t)
583 (replace-match "\n"))
584 (goto-char (point-min)))
588 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
589 ;;; mm-view.el ends here