Revert autoload removal.
[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-render-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 (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)) ">"))
204     (catch 'found-handle
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)))))))
211
212 (eval-and-compile
213   (unless (or (featurep 'xemacs)
214               (>= emacs-major-version 21))
215     (defvar mm-w3m-mode-map nil
216       "Keymap for text/html parts rendered by emacs-w3m.
217 This keymap will be bound only when Emacs 20 is running and overwritten
218 by the value of `w3m-minor-mode-map'.  In order to add some commands to
219 this keymap, add them to `w3m-minor-mode-map' instead of this keymap.")))
220
221 (defun mm-w3m-local-map-property ()
222   (when (and (boundp 'w3m-minor-mode-map) w3m-minor-mode-map)
223     (if (or (featurep 'xemacs)
224             (>= emacs-major-version 21))
225         (list 'keymap w3m-minor-mode-map)
226       (list 'local-map
227             (or mm-w3m-mode-map
228                 (progn
229                   (setq mm-w3m-mode-map (copy-keymap w3m-minor-mode-map))
230                   (set-keymap-parent mm-w3m-mode-map gnus-article-mode-map)
231                   mm-w3m-mode-map))))))
232
233 (defun mm-inline-text-html-render-with-w3m (handle)
234   "Render a text/html part using emacs-w3m."
235   (mm-setup-w3m)
236   (let ((text (mm-get-part handle))
237         (b (point))
238         (charset (mail-content-type-get (mm-handle-type handle) 'charset)))
239     (save-excursion
240       (insert text)
241       (save-restriction
242         (narrow-to-region b (point))
243         (goto-char (point-min))
244         (when (re-search-forward w3m-meta-content-type-charset-regexp nil t)
245           (setq charset (or (w3m-charset-to-coding-system (match-string 2))
246                             charset)))
247         (when charset
248           (delete-region (point-min) (point-max))
249           (insert (mm-decode-string text charset)))
250         (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
251               w3m-force-redisplay)
252           (w3m-region (point-min) (point-max)))
253         (when mm-inline-text-html-with-w3m-keymap
254           (add-text-properties
255            (point-min) (point-max)
256            (nconc (mm-w3m-local-map-property)
257                   ;; Put the mark meaning this part was rendered by emacs-w3m.
258                   '(mm-inline-text-html-with-w3m t)))))
259       (mm-handle-set-undisplayer
260        handle
261        `(lambda ()
262           (let (buffer-read-only)
263             (if (functionp 'remove-specifier)
264                 (mapcar (lambda (prop)
265                           (remove-specifier
266                            (face-property 'default prop)
267                            (current-buffer)))
268                         '(background background-pixmap foreground)))
269             (delete-region ,(point-min-marker)
270                            ,(point-max-marker))))))))
271
272 (defun mm-links-remove-leading-blank ()
273   ;; Delete the annoying three spaces preceding each line of links
274   ;; output.
275   (goto-char (point-min))
276   (while (re-search-forward "^   " nil t)
277     (delete-region (match-beginning 0) (match-end 0))))
278
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))
285     (unwind-protect
286         (apply 'call-process cmd nil t nil (mapcar 'eval args))
287       (delete-file file))
288     (and post-func (funcall post-func))))
289
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)
293            cmd t t nil args))
294   (and post-func (funcall post-func)))
295
296 (defun mm-inline-render-with-file (handle post-func cmd &rest args)
297   (let ((source (mm-get-part handle)))
298     (mm-insert-inline
299      handle
300      (mm-with-unibyte-buffer
301        (insert source)
302        (apply 'mm-inline-wash-with-file post-func cmd args)
303        (buffer-string)))))
304
305 (defun mm-inline-render-with-stdin (handle post-func cmd &rest args)
306   (let ((source (mm-get-part handle)))
307     (mm-insert-inline
308      handle
309      (mm-with-unibyte-buffer
310        (insert source)
311        (apply 'mm-inline-wash-with-stdin post-func cmd args)
312        (buffer-string)))))
313
314 (defun mm-inline-render-with-function (handle func &rest args)
315   (let ((source (mm-get-part handle)))
316     (mm-insert-inline
317      handle
318      (mm-with-unibyte-buffer
319        (insert source)
320        (apply func args)
321        (buffer-string)))))
322
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))
326          buffer-read-only)
327     (if entry
328         (setq func (cdr entry)))
329     (cond
330      ((functionp func)
331       (funcall func handle))
332      (t
333       (apply (car func) handle (cdr func))))))
334
335 (defun mm-inline-text-vcard (handle)
336   (let (buffer-read-only)
337     (mm-insert-inline
338      handle
339      (concat "\n-- \n"
340              (ignore-errors
341                (if (fboundp 'vcard-pretty-print)
342                    (vcard-pretty-print (mm-get-part handle))
343                  (vcard-format-string
344                   (vcard-parse-string (mm-get-part handle)
345                                       'vcard-standard-filter))))))))
346
347 (defun mm-inline-text (handle)
348   (let ((b (point))
349         (type (mm-handle-media-subtype handle))
350         (charset (mail-content-type-get
351                   (mm-handle-type handle) 'charset))
352         buffer-read-only)
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))
357         (save-restriction
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)))
364                       "flowed"))
365       (save-restriction
366         (narrow-to-region b (point))
367         (goto-char b)
368         (fill-flowed)
369         (goto-char (point-max))))
370     (save-restriction
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"))
375         (ignore-errors
376           (enriched-decode (point-min) (point-max))))
377       (mm-handle-set-undisplayer
378        handle
379        `(lambda ()
380           (let (buffer-read-only)
381             (delete-region ,(point-min-marker)
382                            ,(point-max-marker))))))))
383
384 (defun mm-insert-inline (handle text)
385   "Insert TEXT inline from HANDLE."
386   (let ((b (point)))
387     (insert text)
388     (mm-handle-set-undisplayer
389      handle
390      `(lambda ()
391         (let (buffer-read-only)
392           (delete-region ,(set-marker (make-marker) b)
393                          ,(set-marker (make-marker) (point))))))))
394
395 (defun mm-inline-audio (handle)
396   (message "Not implemented"))
397
398 (defun mm-view-sound-file ()
399   (message "Not implemented"))
400
401 (defun mm-w3-prepare-buffer ()
402   (require 'w3)
403   (let ((url-standalone-mode t)
404         (url-gateway-unplugged t)
405         (w3-honor-stylesheets nil))
406     (w3-prepare-buffer)))
407
408 (defun mm-view-message ()
409   (mm-enable-multibyte)
410   (let (handles)
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))
416     (when handles
417       (setq gnus-article-mime-handles
418             (mm-merge-handles gnus-article-mime-handles handles))))
419   (fundamental-mode)
420   (goto-char (point-min)))
421
422 (defun mm-inline-message (handle)
423   (let ((b (point))
424         (bolp (bolp))
425         (charset (mail-content-type-get
426                   (mm-handle-type handle) 'charset))
427         gnus-displaying-mime handles)
428     (when (and charset
429                (stringp charset))
430       (setq charset (intern (downcase charset)))
431       (when (eq charset 'us-ascii)
432         (setq charset nil)))
433     (save-excursion
434       (save-restriction
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           (let ((gnus-original-article-buffer (mm-handle-buffer handle)))
443             (run-hooks 'gnus-article-decode-hook))
444           (gnus-article-prepare-display)
445           (setq handles gnus-article-mime-handles))
446         (goto-char (point-min))
447         (unless bolp
448           (insert "\n"))
449         (goto-char (point-max))
450         (unless (bolp)
451           (insert "\n"))
452         (insert "----------\n\n")
453         (when handles
454           (setq gnus-article-mime-handles
455                 (mm-merge-handles gnus-article-mime-handles handles)))
456         (mm-handle-set-undisplayer
457          handle
458          `(lambda ()
459             (let (buffer-read-only)
460               (if (fboundp 'remove-specifier)
461                   ;; This is only valid on XEmacs.
462                   (mapcar (lambda (prop)
463                             (remove-specifier
464                              (face-property 'default prop) (current-buffer)))
465                           '(background background-pixmap foreground)))
466               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
467
468 (defun mm-display-inline-fontify (handle mode)
469   (let (text)
470     ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
471     ;; on for buffers whose name begins with " ".  That's why we use
472     ;; save-current-buffer/get-buffer-create rather than
473     ;; with-temp-buffer.
474     (save-current-buffer
475       (set-buffer (generate-new-buffer "*fontification*"))
476       (unwind-protect
477           (progn
478             (buffer-disable-undo)
479             (mm-insert-part handle)
480             (funcall mode)
481             (require 'font-lock)
482             (let ((font-lock-verbose nil))
483               ;; I find font-lock a bit too verbose.
484               (font-lock-fontify-buffer))
485             ;; By default, XEmacs font-lock uses non-duplicable text
486             ;; properties.  This code forces all the text properties
487             ;; to be copied along with the text.
488             (when (fboundp 'extent-list)
489               (map-extents (lambda (ext ignored)
490                              (set-extent-property ext 'duplicable t)
491                              nil)
492                            nil nil nil nil nil 'text-prop))
493             (setq text (buffer-string)))
494         (kill-buffer (current-buffer))))
495     (mm-insert-inline handle text)))
496
497 ;; Shouldn't these functions check whether the user even wants to use
498 ;; font-lock?  At least under XEmacs, this fontification is pretty
499 ;; much unconditional.  Also, it would be nice to change for the size
500 ;; of the fontified region.
501
502 (defun mm-display-patch-inline (handle)
503   (mm-display-inline-fontify handle 'diff-mode))
504
505 (defun mm-display-elisp-inline (handle)
506   (mm-display-inline-fontify handle 'emacs-lisp-mode))
507
508 ;;      id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
509 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
510 (defvar mm-pkcs7-signed-magic
511   (mm-string-as-unibyte
512    (apply 'concat
513           (mapcar 'char-to-string
514                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
515                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
516                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
517                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02)))))
518
519 ;;      id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
520 ;;          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
521 (defvar mm-pkcs7-enveloped-magic
522   (mm-string-as-unibyte
523    (apply 'concat
524           (mapcar 'char-to-string
525                   (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
526                         ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
527                         ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
528                         ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03)))))
529
530 (defun mm-view-pkcs7-get-type (handle)
531   (mm-with-unibyte-buffer
532     (mm-insert-part handle)
533     (cond ((looking-at mm-pkcs7-enveloped-magic)
534            'enveloped)
535           ((looking-at mm-pkcs7-signed-magic)
536            'signed)
537           (t
538            (error "Could not identify PKCS#7 type")))))
539
540 (defun mm-view-pkcs7 (handle)
541   (case (mm-view-pkcs7-get-type handle)
542     (enveloped (mm-view-pkcs7-decrypt handle))
543     (signed (mm-view-pkcs7-verify handle))
544     (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
545
546 (defun mm-view-pkcs7-verify (handle)
547   ;; A bogus implementation of PKCS#7. FIXME::
548   (mm-insert-part handle)
549   (goto-char (point-min))
550   (if (search-forward "Content-Type: " nil t)
551       (delete-region (point-min) (match-beginning 0)))
552   (goto-char (point-max))
553   (if (re-search-backward "--\r?\n?" nil t)
554       (delete-region (match-end 0) (point-max)))
555   (goto-char (point-min))
556   (while (search-forward "\r\n" nil t)
557     (replace-match "\n"))
558   (message "Verify signed PKCS#7 message is unimplemented.")
559   (sit-for 1)
560   t)
561
562 (autoload 'gnus-completing-read-maybe-default "gnus-util" nil nil 'macro)
563
564 (defun mm-view-pkcs7-decrypt (handle)
565   (insert-buffer-substring (mm-handle-buffer handle))
566   (goto-char (point-min))
567   (insert "MIME-Version: 1.0\n")
568   (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
569   (smime-decrypt-region
570    (point-min) (point-max)
571    (if (= (length smime-keys) 1)
572        (cadar smime-keys)
573      (smime-get-key-by-email
574       (gnus-completing-read-maybe-default
575        (concat "Decipher using which key? "
576                (if smime-keys (concat "(default " (caar smime-keys) ") ")
577                  ""))
578        smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
579   (goto-char (point-min))
580   (while (search-forward "\r\n" nil t)
581     (replace-match "\n"))
582   (goto-char (point-min)))
583
584 (provide 'mm-view)
585
586 ;;; mm-view.el ends here