6067b67b7bb51aaaea12efaff141fc48ee9cee7a
[gnus] / lisp / mailcap.el
1 ;;; mailcap.el --- MIME media types configuration
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;       Free Software Foundation, Inc.
4
5 ;; Author: William M. Perry <wmperry@aventail.com>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail, multimedia
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Provides configuration of MIME media types from directly from Lisp
29 ;; and via the usual mailcap mechanism (RFC 1524).  Deals with
30 ;; mime.types similarly.
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (require 'mail-parse)
36 (require 'mm-util)
37
38 (defgroup mailcap nil
39   "Definition of viewers for MIME types."
40   :version "21.1"
41   :group 'mime)
42
43 (defvar mailcap-parse-args-syntax-table
44   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
45     (modify-syntax-entry ?' "\"" table)
46     (modify-syntax-entry ?` "\"" table)
47     (modify-syntax-entry ?{ "(" table)
48     (modify-syntax-entry ?} ")" table)
49     table)
50   "A syntax table for parsing SGML attributes.")
51
52 (eval-and-compile
53   (when (featurep 'xemacs)
54     (condition-case nil
55         (require 'lpr)
56       (error nil))))
57
58 (defvar mailcap-print-command
59   (mapconcat 'identity
60              (cons (if (boundp 'lpr-command)
61                        lpr-command
62                      "lpr")
63                    (when (boundp 'lpr-switches)
64                      (if (stringp lpr-switches)
65                          (list lpr-switches)
66                        lpr-switches)))
67              " ")
68   "Shell command (including switches) used to print Postscript files.")
69
70 ;; Postpone using defcustom for this as it's so big and we essentially
71 ;; have to have two copies of the data around then.  Perhaps just
72 ;; customize the Lisp viewers and rely on the normal configuration
73 ;; files for the rest?  -- fx
74 (defvar mailcap-mime-data
75   `(("application"
76      ("vnd.ms-excel"
77       (viewer . "gnumeric %s")
78       (test   . (getenv "DISPLAY"))
79       (type . "application/vnd.ms-excel"))
80      ("x-x509-ca-cert"
81       (viewer . ssl-view-site-cert)
82       (test . (fboundp 'ssl-view-site-cert))
83       (type . "application/x-x509-ca-cert"))
84      ("x-x509-user-cert"
85       (viewer . ssl-view-user-cert)
86       (test . (fboundp 'ssl-view-user-cert))
87       (type . "application/x-x509-user-cert"))
88      ("octet-stream"
89       (viewer . mailcap-save-binary-file)
90       (non-viewer . t)
91       (type . "application/octet-stream"))
92      ("dvi"
93       (viewer . "xdvi -safer %s")
94       (test   . (eq window-system 'x))
95       ("needsx11")
96       (type   . "application/dvi")
97       ("print" . "dvips -qRP %s"))
98      ("dvi"
99       (viewer . "dvitty %s")
100       (test   . (not (getenv "DISPLAY")))
101       (type   . "application/dvi")
102       ("print" . "dvips -qRP %s"))
103      ("emacs-lisp"
104       (viewer . mailcap-maybe-eval)
105       (type   . "application/emacs-lisp"))
106      ("x-emacs-lisp"
107       (viewer . mailcap-maybe-eval)
108       (type   . "application/x-emacs-lisp"))
109      ("x-tar"
110       (viewer . mailcap-save-binary-file)
111       (non-viewer . t)
112       (type   . "application/x-tar"))
113      ("x-latex"
114       (viewer . tex-mode)
115       (test   . (fboundp 'tex-mode))
116       (type   . "application/x-latex"))
117      ("x-tex"
118       (viewer . tex-mode)
119       (test   . (fboundp 'tex-mode))
120       (type   . "application/x-tex"))
121      ("latex"
122       (viewer . tex-mode)
123       (test   . (fboundp 'tex-mode))
124       (type   . "application/latex"))
125      ("tex"
126       (viewer . tex-mode)
127       (test   . (fboundp 'tex-mode))
128       (type   . "application/tex"))
129      ("texinfo"
130       (viewer . texinfo-mode)
131       (test   . (fboundp 'texinfo-mode))
132       (type   . "application/tex"))
133      ("zip"
134       (viewer . mailcap-save-binary-file)
135       (non-viewer . t)
136       (type   . "application/zip")
137       ("copiousoutput"))
138      ("pdf"
139       (viewer . "gv -safer %s")
140       (type . "application/pdf")
141       (test . window-system)
142       ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
143      ("pdf"
144       (viewer . "gpdf %s")
145       (type . "application/pdf")
146       ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
147       (test . (eq window-system 'x)))
148      ("pdf"
149       (viewer . "xpdf %s")
150       (type . "application/pdf")
151       ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
152       (test . (eq window-system 'x)))
153      ("pdf"
154       (viewer . ,(concat "pdftotext %s -"))
155       (type   . "application/pdf")
156       ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
157       ("copiousoutput"))
158      ("postscript"
159       (viewer . "gv -safer %s")
160       (type . "application/postscript")
161       (test   . window-system)
162       ("print" . ,(concat mailcap-print-command " %s"))
163       ("needsx11"))
164      ("postscript"
165       (viewer . "ghostview -dSAFER %s")
166       (type . "application/postscript")
167       (test   . (eq window-system 'x))
168       ("print" . ,(concat mailcap-print-command " %s"))
169       ("needsx11"))
170      ("postscript"
171       (viewer . "ps2ascii %s")
172       (type . "application/postscript")
173       (test . (not (getenv "DISPLAY")))
174       ("print" . ,(concat mailcap-print-command " %s"))
175       ("copiousoutput"))
176      ("sieve"
177       (viewer . sieve-mode)
178       (test   . (fboundp 'sieve-mode))
179       (type   . "application/sieve"))
180      ("pgp-keys"
181       (viewer . "gpg --import --interactive --verbose")
182       (type   . "application/pgp-keys")
183       ("needsterminal")))
184     ("audio"
185      ("x-mpeg"
186       (viewer . "maplay %s")
187       (type   . "audio/x-mpeg"))
188      (".*"
189       (viewer . "showaudio")
190       (type   . "audio/*")))
191     ("message"
192      ("rfc-*822"
193       (viewer . mm-view-message)
194       (test   . (and (featurep 'gnus)
195                      (gnus-alive-p)))
196       (type   . "message/rfc822"))
197      ("rfc-*822"
198       (viewer . vm-mode)
199       (test   . (fboundp 'vm-mode))
200       (type   . "message/rfc822"))
201      ("rfc-*822"
202       (viewer . w3-mode)
203       (test   . (fboundp 'w3-mode))
204       (type   . "message/rfc822"))
205      ("rfc-*822"
206       (viewer . view-mode)
207       (type   . "message/rfc822")))
208     ("image"
209      ("x-xwd"
210       (viewer  . "xwud -in %s")
211       (type    . "image/x-xwd")
212       ("compose" . "xwd -frame > %s")
213       (test    . (eq window-system 'x))
214       ("needsx11"))
215      ("x11-dump"
216       (viewer . "xwud -in %s")
217       (type . "image/x-xwd")
218       ("compose" . "xwd -frame > %s")
219       (test   . (eq window-system 'x))
220       ("needsx11"))
221      ("windowdump"
222       (viewer . "xwud -in %s")
223       (type . "image/x-xwd")
224       ("compose" . "xwd -frame > %s")
225       (test   . (eq window-system 'x))
226       ("needsx11"))
227      (".*"
228       (viewer . "display %s")
229       (type . "image/*")
230       (test   . (eq window-system 'x))
231       ("needsx11"))
232      (".*"
233       (viewer . "ee %s")
234       (type . "image/*")
235       (test   . (eq window-system 'x))
236       ("needsx11")))
237     ("text"
238      ("plain"
239       (viewer  . w3-mode)
240       (test    . (fboundp 'w3-mode))
241       (type    . "text/plain"))
242      ("plain"
243       (viewer  . view-mode)
244       (test    . (fboundp 'view-mode))
245       (type    . "text/plain"))
246      ("plain"
247       (viewer  . fundamental-mode)
248       (type    . "text/plain"))
249      ("enriched"
250       (viewer . enriched-decode)
251       (test   . (fboundp 'enriched-decode))
252       (type   . "text/enriched"))
253      ("html"
254       (viewer . mm-w3-prepare-buffer)
255       (test   . (fboundp 'w3-prepare-buffer))
256       (type   . "text/html"))
257      ("dns"
258       (viewer . dns-mode)
259       (test   . (fboundp 'dns-mode))
260       (type   . "text/dns")))
261     ("video"
262      ("mpeg"
263       (viewer . "mpeg_play %s")
264       (type   . "video/mpeg")
265       (test   . (eq window-system 'x))
266       ("needsx11")))
267     ("x-world"
268      ("x-vrml"
269       (viewer  . "webspace -remote %s -URL %u")
270       (type    . "x-world/x-vrml")
271       ("description"
272        "VRML document")))
273     ("archive"
274      ("tar"
275       (viewer . tar-mode)
276       (type . "archive/tar")
277       (test . (fboundp 'tar-mode)))))
278   "The mailcap structure is an assoc list of assoc lists.
279 1st assoc list is keyed on the major content-type
280 2nd assoc list is keyed on the minor content-type (which can be a regexp)
281
282 Which looks like:
283 -----------------
284  ((\"application\"
285    (\"postscript\" . <info>))
286   (\"text\"
287    (\"plain\" . <info>)))
288
289 Where <info> is another assoc list of the various information
290 related to the mailcap RFC 1524.  This is keyed on the lowercase
291 attribute name (viewer, test, etc).  This looks like:
292  ((viewer . VIEWERINFO)
293   (test   . TESTINFO)
294   (xxxx   . \"STRING\")
295   FLAG)
296
297 Where VIEWERINFO specifies how the content-type is viewed.  Can be
298 a string, in which case it is run through a shell, with
299 appropriate parameters, or a symbol, in which case the symbol is
300 `funcall'ed, with the buffer as an argument.
301
302 TESTINFO is a test for the viewer's applicability, or nil.  If nil, it
303 means the viewer is always valid.  If it is a Lisp function, it is
304 called with a list of items from any extra fields from the
305 Content-Type header as argument to return a boolean value for the
306 validity.  Otherwise, if it is a non-function Lisp symbol or list
307 whose car is a symbol, it is `eval'led to yield the validity.  If it
308 is a string or list of strings, it represents a shell command to run
309 to return a true or false shell value for the validity.")
310 (put 'mailcap-mime-data 'risky-local-variable t)
311
312 (defcustom mailcap-download-directory nil
313   "*Directory to which `mailcap-save-binary-file' downloads files by default.
314 nil means your home directory."
315   :type '(choice (const :tag "Home directory" nil)
316                  directory)
317   :group 'mailcap)
318
319 (defvar mailcap-poor-system-types
320   '(ms-dos ms-windows windows-nt win32 w32 mswindows)
321   "Systems that don't have a Unix-like directory hierarchy.")
322
323 ;;;
324 ;;; Utility functions
325 ;;;
326
327 (defun mailcap-save-binary-file ()
328   (goto-char (point-min))
329   (unwind-protect
330       (let ((file (read-file-name
331                    "Filename to save as: "
332                    (or mailcap-download-directory "~/")))
333             (require-final-newline nil))
334         (write-region (point-min) (point-max) file))
335     (kill-buffer (current-buffer))))
336
337 (defvar mailcap-maybe-eval-warning
338   "*** WARNING ***
339
340 This MIME part contains untrusted and possibly harmful content.
341 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
342 things can happen.  Please examine the code very carefully before you
343 instruct Emacs to evaluate it.  You can browse the buffer containing
344 the code using \\[scroll-other-window].
345
346 If you are unsure what to do, please answer \"no\"."
347   "Text of warning message displayed by `mailcap-maybe-eval'.
348 Make sure that this text consists only of few text lines.  Otherwise,
349 Gnus might fail to display all of it.")
350
351 (defun mailcap-maybe-eval ()
352   "Maybe evaluate a buffer of Emacs Lisp code."
353   (let ((lisp-buffer (current-buffer)))
354     (goto-char (point-min))
355     (when
356         (save-window-excursion
357           (delete-other-windows)
358           (let ((buffer (get-buffer-create (generate-new-buffer-name
359                                             "*Warning*"))))
360             (unwind-protect
361                 (with-current-buffer buffer
362                   (insert (substitute-command-keys
363                            mailcap-maybe-eval-warning))
364                   (goto-char (point-min))
365                   (display-buffer buffer)
366                   (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
367               (kill-buffer buffer))))
368       (eval-buffer (current-buffer)))
369     (when (buffer-live-p lisp-buffer)
370       (with-current-buffer lisp-buffer
371         (emacs-lisp-mode)))))
372
373
374 ;;;
375 ;;; The mailcap parser
376 ;;;
377
378 (defun mailcap-replace-regexp (regexp to-string)
379   ;; Quiet replace-regexp.
380   (goto-char (point-min))
381   (while (re-search-forward regexp nil t)
382     (replace-match to-string t nil)))
383
384 (defvar mailcap-parsed-p nil)
385
386 (defun mailcap-parse-mailcaps (&optional path force)
387   "Parse out all the mailcaps specified in a path string PATH.
388 Components of PATH are separated by the `path-separator' character
389 appropriate for this system.  If FORCE, re-parse even if already
390 parsed.  If PATH is omitted, use the value of environment variable
391 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
392 /usr/local/etc/mailcap."
393   (interactive (list nil t))
394   (when (or (not mailcap-parsed-p)
395             force)
396     (cond
397      (path nil)
398      ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
399      ((memq system-type mailcap-poor-system-types)
400       (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
401      (t (setq path
402               ;; This is per RFC 1524, specifically
403               ;; with /usr before /usr/local.
404               '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
405                 "/usr/local/etc/mailcap"))))
406     (let ((fnames (reverse
407                    (if (stringp path)
408                        (delete "" (split-string path path-separator))
409                      path)))
410           fname)
411       (while fnames
412         (setq fname (car fnames))
413         (if (and (file-readable-p fname)
414                  (file-regular-p fname))
415             (mailcap-parse-mailcap fname))
416         (setq fnames (cdr fnames))))
417       (setq mailcap-parsed-p t)))
418
419 (defun mailcap-parse-mailcap (fname)
420   "Parse out the mailcap file specified by FNAME."
421   (let (major                           ; The major mime type (image/audio/etc)
422         minor                           ; The minor mime type (gif, basic, etc)
423         save-pos                        ; Misc saved positions used in parsing
424         viewer                          ; How to view this mime type
425         info                            ; Misc info about this mime type
426         )
427     (with-temp-buffer
428       (insert-file-contents fname)
429       (set-syntax-table mailcap-parse-args-syntax-table)
430       (mailcap-replace-regexp "#.*" "") ; Remove all comments
431       (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
432       (mailcap-replace-regexp "\n+" "\n") ; And blank lines
433       (goto-char (point-max))
434       (skip-chars-backward " \t\n")
435       (delete-region (point) (point-max))
436       (while (not (bobp))
437         (skip-chars-backward " \t\n")
438         (beginning-of-line)
439         (setq save-pos (point)
440               info nil)
441         (skip-chars-forward "^/; \t\n")
442         (downcase-region save-pos (point))
443         (setq major (buffer-substring save-pos (point)))
444         (skip-chars-forward " \t")
445         (setq minor "")
446         (when (eq (char-after) ?/)
447           (forward-char)
448           (skip-chars-forward " \t")
449           (setq save-pos (point))
450           (skip-chars-forward "^; \t\n")
451           (downcase-region save-pos (point))
452           (setq minor
453                 (cond
454                  ((eq ?* (or (char-after save-pos) 0)) ".*")
455                  ((= (point) save-pos) ".*")
456                  (t (regexp-quote (buffer-substring save-pos (point)))))))
457         (skip-chars-forward " \t")
458         ;;; Got the major/minor chunks, now for the viewers/etc
459         ;;; The first item _must_ be a viewer, according to the
460         ;;; RFC for mailcap files (#1524)
461         (setq viewer "")
462         (when (eq (char-after) ?\;)
463           (forward-char)
464           (skip-chars-forward " \t")
465           (setq save-pos (point))
466           (skip-chars-forward "^;\n")
467           ;; skip \;
468           (while (eq (char-before) ?\\)
469             (backward-delete-char 1)
470             (forward-char)
471             (skip-chars-forward "^;\n"))
472           (if (eq (or (char-after save-pos) 0) ?')
473               (setq viewer (progn
474                              (narrow-to-region (1+ save-pos) (point))
475                              (goto-char (point-min))
476                              (prog1
477                                  (read (current-buffer))
478                                (goto-char (point-max))
479                                (widen))))
480             (setq viewer (buffer-substring save-pos (point)))))
481         (setq save-pos (point))
482         (end-of-line)
483         (unless (equal viewer "")
484           (setq info (nconc (list (cons 'viewer viewer)
485                                   (cons 'type (concat major "/"
486                                                       (if (string= minor ".*")
487                                                           "*" minor))))
488                             (mailcap-parse-mailcap-extras save-pos (point))))
489           (mailcap-mailcap-entry-passes-test info)
490           (mailcap-add-mailcap-entry major minor info))
491         (beginning-of-line)))))
492
493 (defun mailcap-parse-mailcap-extras (st nd)
494   "Grab all the extra stuff from a mailcap entry."
495   (let (
496         name                            ; From name=
497         value                       &nbs