* binhex.el: Don't autoload executable-find.
[gnus] / lisp / mm-view.el
1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 ;; 2004 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'mail-parse)
29 (require 'mailcap)
30 (require 'mm-bodies)
31 (require 'mm-decode)
32
33 (eval-and-compile
34   (autoload 'gnus-article-prepare-display "gnus-art")
35   (autoload 'vcard-parse-string "vcard")
36   (autoload 'vcard-format-string "vcard")
37   (autoload 'fill-flowed "flow-fill")
38   (autoload 'html2text "html2text"))
39
40 (defvar mm-text-html-renderer-alist
41   '((w3  . mm-inline-text-html-render-with-w3)
42     (w3m . mm-inline-text-html-render-with-w3m)
43     (w3m-standalone mm-inline-render-with-stdin nil
44                     "w3m" "-dump" "-T" "text/html")
45     (links mm-inline-render-with-file
46            mm-links-remove-leading-blank
47            "links" "-dump" file)
48     (lynx  mm-inline-render-with-stdin nil
49            "lynx" "-dump" "-force_html" "-stdin" "-nolist")
50     (html2text  mm-inline-render-with-function html2text))
51   "The attributes of renderer types for text/html.")
52
53 (defvar mm-text-html-washer-alist
54   '((w3  . gnus-article-wash-html-with-w3)
55     (w3m . gnus-article-wash-html-with-w3m)
56     (w3m-standalone mm-inline-wash-with-stdin nil
57                     "w3m" "-dump" "-T" "text/html")
58     (links mm-inline-wash-with-file
59            mm-links-remove-leading-blank
60            "links" "-dump" file)
61     (lynx  mm-inline-wash-with-stdin nil
62            "lynx" "-dump" "-force_html" "-stdin" "-nolist")
63     (html2text  html2text))
64   "The attributes of washer types for text/html.")
65
66 (defcustom mm-fill-flowed t
67   "If non-nil a format=flowed article will be displayed flowed."
68   :type 'boolean
69   :group 'mime-display)
70
71 ;;; Internal variables.
72
73 ;;;
74 ;;; Functions for displaying various formats inline
75 ;;;
76
77 (defun mm-inline-image-emacs (handle)
78   (let ((b (point-marker))
79         buffer-read-only)
80     (put-image (mm-get-image handle) b)
81     (mm-handle-set-undisplayer
82      handle
83      `(lambda ()
84         (let ((b ,b)
85               buffer-read-only)
86           (remove-images b b)
87           (delete-region b (+ b 2)))))))
88
89 (defun mm-inline-image-xemacs (handle)
90   (insert "\n\n")
91   (forward-char -2)
92   (let ((annot (make-annotation (mm-get-image handle) nil 'text))
93         buffer-read-only)
94     (mm-handle-set-undisplayer
95      handle
96      `(lambda ()
97         (let ((b ,(point-marker))
98               buffer-read-only)
99           (delete-annotation ,annot)
100           (delete-region (- b 2) b))))
101     (set-extent-property annot 'mm t)
102     (set-extent-property annot 'duplicable t)))
103
104 (eval-and-compile
105   (if (featurep 'xemacs)
106       (defalias 'mm-inline-image 'mm-inline-image-xemacs)
107     (defalias 'mm-inline-image 'mm-inline-image-emacs)))
108
109 (defvar mm-w3-setup nil)
110 (defun mm-setup-w3 ()
111   (unless mm-w3-setup
112     (require 'w3)
113     (w3-do-setup)
114     (require 'url)
115     (require 'w3-vars)
116     (require 'url-vars)
117     (setq mm-w3-setup t)))
118
119 (defun mm-inline-text-html-render-with-w3 (handle)
120   (mm-setup-w3)
121   (let ((text (mm-get-part handle))
122         (b (point))
123         (url-standalone-mode t)
124         (url-gateway-unplugged t)
125         (w3-honor-stylesheets nil)
126         (url-current-object
127          (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
128         (width (window-width))
129         (charset (mail-content-type-get
130                   (mm-handle-type handle) 'charset)))
131     (save-excursion
132       (insert text)
133       (save-restriction
134         (narrow-to-region b (point))
135         (goto-char (point-min))
136         (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
137                      (re-search-forward
138                       w3-meta-content-type-charset-regexp nil t))
139                 (and (boundp 'w3-meta-charset-content-type-regexp)
140                      (re-search-forward
141                       w3-meta-charset-content-type-regexp nil t)))
142             (setq charset
143                   (or (let ((bsubstr (buffer-substring-no-properties
144                                       (match-beginning 2)
145                                       (match-end 2))))
146                         (if (fboundp 'w3-coding-system-for-mime-charset)
147                             (w3-coding-system-for-mime-charset bsubstr)
148                           (mm-charset-to-coding-system bsubstr)))
149                       charset)))
150         (delete-region (point-min) (point-max))
151         (insert (mm-decode-string text charset))
152         (save-window-excursion
153           (save-restriction
154             (let ((w3-strict-width width)
155                   ;; Don't let w3 set the global version of
156                   ;; this variable.
157                   (fill-column fill-column))
158               (if (or debug-on-error debug-on-quit)
159                   (w3-region (point-min) (point-max))
160                 (condition-case ()
161                     (w3-region (point-min) (point-max))
162                   (error
163                    (delete-region (point-min) (point-max))
164                    (let ((b (point))
165                          (charset (mail-content-type-get
166                                    (mm-handle-type handle) 'charset)))
167                      (if (or (eq charset 'gnus-decoded)
168                              (eq mail-parse-charset 'gnus-decoded))
169                        (save-restriction
170                          (narrow-to-region (point) (point))
171                          (mm-insert-part handle)
172                          (goto-char (point-max)))
173                        (insert (mm-decode-string (mm-get-part handle)
174                                                  charset))))
175                    (message
176                     "Error while rendering html; showing as text/plain")))))))
177         (mm-handle-set-undisplayer
178          handle
179          `(lambda ()
180             (let (buffer-read-only)
181               (if (functionp 'remove-specifier)
182                   (mapcar (lambda (prop)
183                             (remove-specifier
184                              (face-property 'default prop)
185                              (current-buffer)))
186                           '(background background-pixmap foreground)))
187               (delete-region ,(point-min-marker)
188                              ,(point-max-marker)))))))))
189
190 (defvar mm-w3m-setup nil
191   "Whether gnus-article-mode has been setup to use emacs-w3m.")
192
193 (defun mm-setup-w3m ()
194   "Setup gnus-article-mode to use emacs-w3m."
195   (unless mm-w3m-setup
196     (require 'w3m)
197     (unless (assq 'gnus-article-mode w3m-cid-retrieve-function-alist)
198       (push (cons 'gnus-article-mode 'mm-w3m-cid-retrieve)
199             w3m-cid-retrieve-function-alist))
200     (setq mm-w3m-setup t))
201   (setq w3m-display-inline-images mm-inline-text-html-with-images))
202
203 (defun mm-w3m-cid-retrieve-1 (url handle)
204   (if (mm-multiple-handles handle)
205       (dolist (elem handle)
206         (mm-w3m-cid-retrieve-1 url elem))
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)))))
211
212 (defun mm-w3m-cid-retrieve (url &rest args)
213   "Insert a content pointed by URL if it has the cid: scheme."
214   (when (string-match "\\`cid:" url)
215     (catch 'found-handle
216       (mm-w3m-cid-retrieve-1 (concat "<" (substring url (match-end 0)) ">")
217                              (with-current-buffer w3m-current-buffer
218                                gnus-article-mime-handles)))))
219
220 (defun mm-inline-text-html-render-with-w3m (handle)
221   "Render a text/html part using emacs-w3m."
222   (mm-setup-w3m)
223   (let ((text (mm-get-part handle))
224         (b (point))
225         (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
226     (save-excursion
227       (insert text)
228       (save-restriction
229         (narrow-to-region b (point))
230         (goto-char (point-min))
231         (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
232           (setq charset (or (w3m-charset-to-coding-system (match-string 2))
233                             charset)))
234         (when charset
235           (delete-region (point-min) (point-max))
236           (insert (mm-decode-string text charset)))
237         (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
238               w3m-force-redisplay)
239           (w3m-region (point-min) (point-max)))
240         (when (and mm-inline-text-html-with-w3m-keymap
241                    (boundp 'w3m-minor-mode-map)
242                    w3m-minor-mode-map)
243           (add-text-properties
244            (point-min) (point-max)
245            (list 'keymap w3m-minor-mode-map
246                  ;; Put the mark meaning this part was rendered by emacs-w3m.
247                  'mm-inline-text-html-with-w3m t))))
248       (mm-handle-set-undisplayer
249        handle
250        `(lambda ()
251           (let (buffer-read-only)
252             (if (functionp 'remove-specifier)
253                 (mapcar (lambda (prop)
254                           (remove-specifier
255                            (face-property 'default prop)
256                            (current-buffer)))
257                         '(background background-pixmap foreground)))
258             (delete-region ,(point-min-marker)
259                            ,(point-max-marker))))))))
260
261 (defun mm-links-remove-leading-blank ()
262   ;; Delete the annoying three spaces preceding each line of links
263   ;; output.
264   (goto-char (point-min))
265   (while (re-search-forward "^   " nil t)
266     (delete-region (match-beginning 0) (match-end 0))))
267
268 (defun mm-inline-wash-with-file (post-func cmd &rest args)
269   (let ((file (mm-make-temp-file
270                (expand-file-name "mm" mm-tmp-directory))))
271     (let ((coding-system-for-write 'binary))
272       (write-region (point-min) (point-max) file nil 'silent))
273     (delete-region (point-min) (point-max))
274     (unwind-protect
275         (apply 'call-process cmd nil t nil (mapcar 'eval args))
276       (delete-file file))
277     (and post-func (funcall post-func))))
278
279 (defun mm-inline-wash-with-stdin (post-func cmd &rest args)
280   (let ((coding-system-for-write 'binary))
281     (apply 'call-process-region (point-min) (point-max)
282            cmd t t nil args))
283   (and post-func (funcall post-func)))
284
285 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
286   (let ((source (mm-get-part handle)))
287     (mm-insert-inline
288      handle
289      (mm-with-unibyte-buffer
290        (insert source)
291        (apply 'mm-inline-wash-with-file post-func cmd args)
292        (buffer-string)))))
293
294 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
295   (let ((source (mm-get-part handle)))
296     (mm-insert-inline
297      handle
298      (mm-with-unibyte-buffer
299        (insert source)
300        (apply 'mm-inline-wash-with-stdin post-func cmd args)
301        (buffer-string)))))
302
303 (defun mm-inline-render-with-function (handle func &rest args)
304   (let ((source (mm-get-part handle)))
305     (mm-insert-inline
306      handle
307      (mm-with-unibyte-buffer
308        (insert source)
309        (apply func args)
310        (buffer-string)))))
311
312 (defun mm-inline-text-html (handle)
313   (let* ((func (or mm-inline-text-html-renderer mm-text-html-renderer))
314          (entry (assq func mm-text-html-renderer-alist))
315          buffer-read-only)
316     (if entry
317         (setq func (cdr entry)))
318     (cond
319      ((functionp func)
320       (funcall func handle))
321      (t
322       (apply (car func) handle (cdr func))))))
323
324 (defun mm-inline-text-vcard (handle)
325   (let (buffer-read-only)
326     (mm-insert-inline
327      handle
328      (concat "\n-- \n"
329              (ignore-errors
330                (if (fboundp 'vcard-pretty-print)
331                    (vcard-pretty-print (mm-get-part handle))
332                  (vcard-format-string
333                   (vcard-parse-string (mm-get-part handle)
334                                       'vcard-standard-filter))))))))
335
336 (defun mm-inline-text (handle)
337   (let ((b (point))
338         (type (mm-handle-media-subtype handle))
339         (charset (mail-content-type-get
340                   (mm-handle-type handle) 'charset))
341         buffer-read-only)
342     (if (or (eq charset 'gnus-decoded)
343             ;; This is probably not entirely correct, but
344             ;; makes rfc822 parts with embedded multiparts work.
345             (eq mail-parse-charset 'gnus-decoded))
346         (save-restriction
347           (narrow-to-region (point) (point))
348           (mm-insert-part handle)
349           (goto-char (point-max)))
350       (insert (mm-decode-string (mm-get-part handle) charset)))
351     (when (and mm-fill-flowed
352                (equal type "plain")
353                (equal (cdr (assoc 'format (mm-handle-type handle)))
354                       "flowed"))
355       (save-restriction
356         (narrow-to-region b (point))
357         (goto-char b)
358         (fill-flowed)
359         (goto-char (point-max))))
360     (save-restriction
361       (narrow-to-region b (point))
362       (set-text-properties (point-min) (point-max) nil)
363       (when (or (equal type "enriched")
364                 (equal type "richtext"))
365         (ignore-errors
366           (enriched-decode (point-min) (point-max))))
367       (mm-handle-set-undisplayer
368        handle
369        `(lambda ()
370           (let (buffer-read-only)
371             (delete-region ,(point-min-marker)
372                            ,(point-max-marker))))))))
373
374 (defun mm-insert-inline (handle text)
375   "Insert TEXT inline from HANDLE."
376   (let ((b (point)))
377     (insert text)
378     (mm-handle-set-undisplayer
379      handle
380      `(lambda ()
381         (let (buffer-read-only)
382           (delete-region ,(set-marker (make-marker) b)
383                          ,(set-marker (make-marker) (point))))))))
384
385 (defun mm-inline-audio (handle)
386   (message "Not implemented"))
387
388 (defun mm-view-sound-file ()
389   (message "Not implemented"))
390
391 (defun mm-w3-prepare-buffer ()
392   (require 'w3)
393   (let ((url-standalone-mode t)
394         (url-gateway-unplugged t)
395         (w3-honor-stylesheets nil))
396     (w3-prepare-buffer)))
397
398 (defun mm-view-message ()
399   (mm-enable-multibyte)
400   (let (handles)
401     (let (gnus-article-mime-handles)
402       ;; Double decode problem may happen.  See mm-inline-message.
403       (run-hooks 'gnus-article-decode-hook)
404       (gnus-article-prepare-display)
405       (setq handles gnus-article-mime-handles))
406     (when handles
407       (setq gnus-article-mime-handles
408             (mm-merge-handles gnus-article-mime-handles handles))))
409   (fundamental-mode)
410   (goto-char (point-min)))
411
412 (defun mm-inline-message (handle)
413   (let ((b (point))
414         (bolp (bolp))
415         (charset (mail-content-type-get
416                   (mm-handle-type handle) 'charset))
417         gnus-displaying-mime handles)
418     (when (and charset
419                (stringp charset))
420       (setq charset (intern (downcase charset)))
421       (when (eq charset 'us-ascii)
422         (setq charset nil)))
423     (save-excursion
424       (save-restriction
425         (narrow-to-region b b)
426         (mm-insert-part handle)
427         (let (gnus-article-mime-handles
428               ;; disable prepare hook
429               gnus-article-prepare-hook
430               (gnus-newsgroup-charset
431                (or charset gnus-newsgroup-charset)))
432           (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
433             (run-hooks 'gnus-article-decode-hook))
434           (gnus-article-prepare-display)
435           (setq handles gnus-article-mime-handles))
436         (goto-char (point-min))
437         (unless bolp
438           (insert "\n"))
439         (goto-char (point-max))
440         (unless (bolp)
441           (insert "\n"))
442         (insert "----------\n\n")
443         (when handles
444           (setq gnus-article-mime-handles
445                 (mm-merge-handles gnus-article-mime-handles handles)))
446         (mm-handle-set-undisplayer
447          handle
448          `(lambda ()
449             (let (buffer-read-only)
450               (if (fboundp 'remove-specifier)
451                   ;; This is only valid on XEmacs.
452                   (mapcar (lambda (prop)
453                             (remove-specifier
454                              (face-property 'default prop) (current-buffer)))
455                           '(background background-pixmap foreground)))
456               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
457
458 (defun mm-display-inline-fontify (handle mode)
459   (let (text)
460     ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
461     ;; on for buffers whose name begins with " ".  That's why we use
462     ;; save-current-buffer/get-buffer-create rather than
463     ;; with-temp-buffer.
464     (save-current-buffer
465       (set-buffer (generate-new-buffer "*fontification*"))
466       (unwind-protect
467           (progn
468             (buffer-disable-undo)
469             (mm-insert-part handle)
470             (funcall mode)
471             (require 'font-lock)
472             (let ((font-lock-verbose nil))
473               ;; I find font-lock a bit too verbose.
474               (font-lock-fontify-buffer))
475             ;; By default, XEmacs font-lock uses non-duplicable text
476             ;; properties.  This code forces all the text properties
477             ;; to be copied along with the text.
478             (when (fboundp 'extent-list)
479               (map-extents (lambda (ext ignored)
480                              (set-extent-property ext 'duplicable t)
481                              nil)
482                            nil nil nil nil nil 'text-prop))
483             (setq text (buffer-string)))
484         (kill-buffer (current-buffer))))
485     (mm-insert-inline handle text)))
486
487 ;; Shouldn't these functions check whether the user even wants to use
488 ;; font-lock?  At least under XEmacs, this fontification is pretty
489 ;; much unconditional.  Also, it would be nice to change for the size
490 ;; of the fontified region.
491
492 (defun mm-display-patch-inline (handle)
493   (mm-display-inline-fontify handle 'diff-mode))
494
495 (defun mm-display-elisp-inline (handle)
496   (mm-display-inline-fontify handle 'emacs-lisp-mode))
497
498 ;;      id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
499 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
500 (defvar mm-pkcs7-signed-magic
501   (mm-string-as-unibyte
502    (apply 'concat
503           (mapcar 'char-to-string
504                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
505                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
506                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
507                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
508
509 ;;      id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
510 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
511 (defvar mm-pkcs7-enveloped-magic
512   (mm-string-as-unibyte
513    (apply 'concat
514           (mapcar 'char-to-string
515                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
516                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
517                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
518                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
519
520 (defun mm-view-pkcs7-get-type (handle)
521   (mm-with-unibyte-buffer
522     (mm-insert-part handle)
523     (cond ((looking-at mm-pkcs7-enveloped-magic)
524            'enveloped)
525           ((looking-at mm-pkcs7-signed-magic)
526            'signed)
527           (t
528            (error "Could not identify PKCS#7 type")))))
529
530 (defun mm-view-pkcs7 (handle)
531   (case (mm-view-pkcs7-get-type handle)
532     (enveloped (mm-view-pkcs7-decrypt handle))
533     (signed (mm-view-pkcs7-verify handle))
534     (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
535
536 (defun mm-view-pkcs7-verify (handle)
537   ;; A bogus implementation of PKCS#7. FIXME::
538   (mm-insert-part handle)
539   (goto-char (point-min))
540   (if (search-forward "Content-Type: " nil t)
541       (delete-region (point-min) (match-beginning 0)))
542   (goto-char (point-max))
543   (if (re-search-backward "--\r?\n?" nil t)
544       (delete-region (match-end 0) (point-max)))
545   (goto-char (point-min))
546   (while (search-forward "\r\n" nil t)
547     (replace-match "\n"))
548   (message "Verify signed PKCS#7 message is unimplemented.")
549   (sit-for 1)
550   t)
551
552 (defun mm-view-pkcs7-decrypt (handle)
553   (insert-buffer-substring (mm-handle-buffer handle))
554   (goto-char (point-min))
555   (insert "MIME-Version: 1.0\n")
556   (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
557   (smime-decrypt-region
558    (point-min) (point-max)
559    (if (= (length smime-keys) 1)
560        (cadar smime-keys)
561      (smime-get-key-by-email
562       (completing-read
563        (concat "Decipher using which key? "
564                (if smime-keys (concat "(default " (caar smime-keys) ") ")
565                  ""))
566        smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
567   (goto-char (point-min))
568   (while (search-forward "\r\n" nil t)
569     (replace-match "\n"))
570   (goto-char (point-min)))
571
572 (provide 'mm-view)
573
574 ;;; mm-view.el ends here