86318b412ab1d5b762580492980a07643d016aa0
[gnus] / lisp / mm-decode.el
1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 (require 'mail-parse)
28 (require 'mailcap)
29 (require 'mm-bodies)
30 (eval-when-compile (require 'cl)
31                    (require 'term))
32
33 (eval-and-compile
34   (autoload 'executable-find "executable")
35   (autoload 'mm-inline-partial "mm-partial")
36   (autoload 'mm-inline-external-body "mm-extern")
37   (autoload 'mm-insert-inline "mm-view"))
38
39 (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
40
41 (defgroup mime-display ()
42   "Display of MIME in mail and news articles."
43   :link '(custom-manual "(emacs-mime)Customization")
44   :version "21.1"
45   :group 'mail
46   :group 'news
47   :group 'multimedia)
48
49 (defgroup mime-security ()
50   "MIME security in mail and news articles."
51   :link '(custom-manual "(emacs-mime)Customization")
52   :group 'mail
53   :group 'news
54   :group 'multimedia)
55
56 ;;; Convenience macros.
57
58 (defmacro mm-handle-buffer (handle)
59   `(nth 0 ,handle))
60 (defmacro mm-handle-type (handle)
61   `(nth 1 ,handle))
62 (defsubst mm-handle-media-type (handle)
63   (if (stringp (car handle))
64       (car handle)
65     (car (mm-handle-type handle))))
66 (defsubst mm-handle-media-supertype (handle)
67   (car (split-string (mm-handle-media-type handle) "/")))
68 (defsubst mm-handle-media-subtype (handle)
69   (cadr (split-string (mm-handle-media-type handle) "/")))
70 (defmacro mm-handle-encoding (handle)
71   `(nth 2 ,handle))
72 (defmacro mm-handle-undisplayer (handle)
73   `(nth 3 ,handle))
74 (defmacro mm-handle-set-undisplayer (handle function)
75   `(setcar (nthcdr 3 ,handle) ,function))
76 (defmacro mm-handle-disposition (handle)
77   `(nth 4 ,handle))
78 (defmacro mm-handle-description (handle)
79   `(nth 5 ,handle))
80 (defmacro mm-handle-cache (handle)
81   `(nth 6 ,handle))
82 (defmacro mm-handle-set-cache (handle contents)
83   `(setcar (nthcdr 6 ,handle) ,contents))
84 (defmacro mm-handle-id (handle)
85   `(nth 7 ,handle))
86 (defmacro mm-handle-multipart-original-buffer (handle)
87   `(get-text-property 0 'buffer (car ,handle)))
88 (defmacro mm-handle-multipart-from (handle)
89   `(get-text-property 0 'from (car ,handle)))
90 (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
91   `(get-text-property 0 ,parameter (car ,handle)))
92
93 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
94                                     disposition description cache
95                                     id)
96   `(list ,buffer ,type ,encoding ,undisplayer
97          ,disposition ,description ,cache ,id))
98
99 (defcustom mm-text-html-renderer
100   (cond ((locate-library "w3") 'w3)
101         ((locate-library "w3m") 'w3m)
102         ((executable-find "links") 'links)
103         ((executable-find "lynx") 'lynx)
104         (t 'html2text))
105   "Render of HTML contents.
106 It is one of defined renderer types, or a rendering function.
107 The defined renderer types are:
108 `w3'   : using Emacs/W3;
109 `w3m'  : using emacs-w3m;
110 `links': using links;
111 `lynx' : using lynx;
112 `html2text' : using html2text;
113 `nil'  : using external viewer."
114   :type '(choice (symbol w3)
115                  (symbol w3m)
116                  (symbol links)
117                  (symbol lynx)
118                  (symbol html2text)
119                  (symbol nil)
120                  (function))
121   :version "21.3"
122   :group 'mime-display)
123
124 (defvar mm-inline-text-html-renderer nil
125   "Function used for rendering inline HTML contents.
126 It is suggested to customize `mm-text-html-renderer' instead.")
127
128 (defcustom mm-inline-text-html-with-images nil
129   "If non-nil, Gnus will allow retrieving images in the HTML contents
130 with <img> tags.  It has no effect on Emacs/w3.  For emacs-w3m, the
131 value of the option `w3m-display-inline-images' will be bound with
132 this value.  In addition, the variable `w3m-safe-url-regexp' will be
133 bound with the value nil if it is non-nil to make emacs-w3m show all
134 images, however this behavior may be changed in the future."
135   :type 'boolean
136   :group 'mime-display)
137
138 (defcustom mm-inline-text-html-with-w3m-keymap t
139   "If non-nil, use emacs-w3m command keys in the article buffer."
140   :type 'boolean
141   :group 'mime-display)
142
143 (defcustom mm-inline-media-tests
144   '(("image/jpeg"
145      mm-inline-image
146      (lambda (handle)
147        (mm-valid-and-fit-image-p 'jpeg handle)))
148     ("image/png"
149      mm-inline-image
150      (lambda (handle)
151        (mm-valid-and-fit-image-p 'png handle)))
152     ("image/gif"
153      mm-inline-image
154      (lambda (handle)
155        (mm-valid-and-fit-image-p 'gif handle)))
156     ("image/tiff"
157      mm-inline-image
158      (lambda (handle)
159        (mm-valid-and-fit-image-p 'tiff handle)) )
160     ("image/xbm"
161      mm-inline-image
162      (lambda (handle)
163        (mm-valid-and-fit-image-p 'xbm handle)))
164     ("image/x-xbitmap"
165      mm-inline-image
166      (lambda (handle)
167        (mm-valid-and-fit-image-p 'xbm handle)))
168     ("image/xpm"
169      mm-inline-image
170      (lambda (handle)
171        (mm-valid-and-fit-image-p 'xpm handle)))
172     ("image/x-pixmap"
173      mm-inline-image
174      (lambda (handle)
175        (mm-valid-and-fit-image-p 'xpm handle)))
176     ("image/bmp"
177      mm-inline-image
178      (lambda (handle)
179        (mm-valid-and-fit-image-p 'bmp handle)))
180     ("image/x-portable-bitmap"
181      mm-inline-image
182      (lambda (handle)
183        (mm-valid-and-fit-image-p 'pbm handle)))
184     ("text/plain" mm-inline-text identity)
185     ("text/enriched" mm-inline-text identity)
186     ("text/richtext" mm-inline-text identity)
187     ("text/x-patch" mm-display-patch-inline
188      (lambda (handle)