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