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