blob: 7fea9b5abbd87ac7c377ca374632f3a29bc4d922 [file] [log] [blame]
Guido van Rossum20af95b1997-03-25 22:01:35 +00001\section{Standard Module \sectcode{mailcap}}
2\stmodindex{mailcap}
3\renewcommand{\indexsubitem}{(in module mailcap)}
4
5Mailcap files are used to configure how MIME-aware applications such
6as mail readers and Web browsers react to files with different MIME
7types. (The name ``mailcap'' is derived from the phrase ``mail
8capability''.) For example, a mailcap file might contain a line like
9\verb\video/mpeg; xmpeg %s\. Then, if the user encounters an email
10message or Web document with the MIME type video/mpeg, \verb\%s\ will be
11replaced by a filename (usually one belonging to a temporary file) and
12the xmpeg program can be automatically started to view the file.
13
14The mailcap format is documented in RFC 1524, ``A User Agent
15Configuration Mechanism For Multimedia Mail Format Information'', but
16is not an Internet standard. However, mailcap files are supported on
17most Unix systems.
18
19\begin{funcdesc}{findmatch}{caps\, MIMEtype\, key\, filename\, plist}
20Return a 2-tuple; the first element is a string containing the command
21line to be executed
22(which can be passed to \code{os.system()}), and the second element is
23the mailcap entry for a given MIME type. If no matching MIME
24type can be found, \code{(None, None)} is returned.
25
26\var{key} is the name of the field desired, which represents the type of
27activity to be performed; the default value is 'view', since in the
28most common case you simply want to view the body of the MIME-typed
29data. Other possible values might be 'compose' and 'edit', if you
30wanted to create a new body of the given MIME type or alter the
31existing body data. See RFC1524 for a complete list of these fields.
32
33\var{filename} is the filename to be substituted for \%s in the
34command line; the default value is
35\file{/dev/null} which is almost certainly not what you want, so
36usually you'll override it by specifying a filename.
37
38\var{plist} can be a list containing named parameters; the default
39value is simply an empty list. Each entry in the list must be a
40string containing the parameter name, an equals sign (=), and the
41parameter's value. Mailcap entries can contain
42named parameters like \verb\%{foo}\, which will be replaced by the
43value of the parameter named 'foo'. For example, if the command line
44\verb\showpartial %{id} %{number} %{total}\
45was in a mailcap file, and \var{plist} was set to \code{['id=1',
46'number=2', 'total=3']}, the resulting command line would be
47\code{"showpartial 1 2 3"}.
48
49In a mailcap file, the "test" field can optionally be specified to
50test some external condition (e.g., the machine architecture, or the
51window system in use) to determine whether or not the mailcap line
52applies. \code{findmatch()} will automatically check such conditions
53and skip the entry if the check fails.
54\end{funcdesc}
55
56\begin{funcdesc}{getcaps}{}
57Returns a dictionary mapping MIME types to a list of mailcap file
58entries. This dictionary must be passed to the \code{findmatch}
59function. An entry is stored as a list of dictionaries, but it
60shouldn't be necessary to know the details of this representation.
61
62The information is derived from all of the mailcap files found on the
63system. Settings in the user's mailcap file \file{\$HOME/.mailcap}
64will override settings in the system mailcap files
65\file{/etc/mailcap}, \file{/usr/etc/mailcap}, and
66\file{/usr/local/etc/mailcap}.
67\end{funcdesc}
68
69An example usage:
70\begin{verbatim}
71>>> import mailcap
72>>> d=mailcap.getcaps()
73>>> mailcap.findmatch(d, 'video/mpeg', filename='/tmp/tmp1223')
74('xmpeg /tmp/tmp1223', {'view': 'xmpeg %s'})
75\end{verbatim}