blob: 2d8fc7efa7c4a35e630ef9826103571146497767 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{rfc822}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-rfc822}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{rfc822}
4
Guido van Rossum86751151995-02-28 17:14:32 +00005
Fred Drakecdea8a31998-03-14 06:17:43 +00006This module defines a class, \class{Message}, which represents a
Guido van Rossuma12ef941995-02-27 17:53:25 +00007collection of ``email headers'' as defined by the Internet standard
Fred Drakec5891241998-02-09 19:16:20 +00008\rfc{822}. It is used in various contexts, usually to read such
Guido van Rossum87294831998-06-16 22:27:40 +00009headers from a file. This module also defines a helper class
10\class{AddressList} for parsing RFC822 addresses.
Guido van Rossuma12ef941995-02-27 17:53:25 +000011
Fred Drake5ca90331997-12-16 15:19:47 +000012Note that there's a separate module to read \UNIX{}, MH, and MMDF
Fred Drakecdea8a31998-03-14 06:17:43 +000013style mailbox files: \module{mailbox}\refstmodindex{mailbox}.
Guido van Rossum067a2ac1997-06-02 17:30:03 +000014
Fred Drakecdea8a31998-03-14 06:17:43 +000015\begin{classdesc}{Message}{file\optional{, seekable}}
Guido van Rossum12991001998-06-10 21:34:27 +000016A \class{Message} instance is instantiated with an input object as
17parameter. Message relies only on the input object having a
18\code{readline} method; in particular, ordinary file objects qualify.
19Instantiation reads headers from the input object up to a delimiter
20line (normally a blank line) and stores them in the instance.
21
Guido van Rossum444d0f81998-06-11 13:50:02 +000022This class can work with any input object that supports a readline
23method. If the input object has seek and tell capability, the
24\code{rewindbody} method will work; also, illegal lines will be pushed back
25onto the input stream. If the input object lacks seek but has an
26\code{unread} method that can push back a line of input, Message will use
27that to push back illegal lines. Thus this class can be used to parse
28messages coming from a buffered stream.
Guido van Rossum12991001998-06-10 21:34:27 +000029
30The optional \code{seekable} argument is provided as a workaround for
31certain stdio libraries in which tell() discards buffered data before
32discovering that the \code{lseek()} system call doesn't work. For
33maximum portability, you should set the seekable argument to zero to
34prevent that initial \code{tell} when passing in an unseekable object
35such as a a file object created from a socket object.
Guido van Rossuma12ef941995-02-27 17:53:25 +000036
37Input lines as read from the file may either be terminated by CR-LF or
38by a single linefeed; a terminating CR-LF is replaced by a single
39linefeed before the line is stored.
40
41All header matching is done independent of upper or lower case;
Fred Drakecdea8a31998-03-14 06:17:43 +000042e.g. \code{\var{m}['From']}, \code{\var{m}['from']} and
43\code{\var{m}['FROM']} all yield the same result.
44\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000045
Guido van Rossum87294831998-06-16 22:27:40 +000046\begin{classdesc}{AddressList}{field}
47You may instantiate the AddresssList helper class using a single
48string parameter, a comma-separated list of RFC822 addresses to be
49parsed. (The parameter None yields an empty list.)
50\end{classdesc}
51
Guido van Rossum843e7121996-12-06 21:23:53 +000052\begin{funcdesc}{parsedate}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000053Attempts to parse a date according to the rules in \rfc{822}.
54however, some mailers don't follow that format as specified, so
55\function{parsedate()} tries to guess correctly in such cases.
Fred Drakec5891241998-02-09 19:16:20 +000056\var{date} is a string containing an \rfc{822} date, such as
Fred Drakecdea8a31998-03-14 06:17:43 +000057\code{'Mon, 20 Nov 1995 19:12:08 -0500'}. If it succeeds in parsing
58the date, \function{parsedate()} returns a 9-tuple that can be passed
59directly to \function{time.mktime()}; otherwise \code{None} will be
Guido van Rossum843e7121996-12-06 21:23:53 +000060returned.
61\end{funcdesc}
62
63\begin{funcdesc}{parsedate_tz}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000064Performs the same function as \function{parsedate()}, but returns
65either \code{None} or a 10-tuple; the first 9 elements make up a tuple
66that can be passed directly to \function{time.mktime()}, and the tenth
67is the offset of the date's timezone from UTC (which is the official
68term for Greenwich Mean Time). (Note that the sign of the timezone
69offset is the opposite of the sign of the \code{time.timezone}
70variable for the same timezone; the latter variable follows the
71\POSIX{} standard while this module follows \rfc{822}.) If the input
72string has no timezone, the last element of the tuple returned is
73\code{None}.
Guido van Rossum843e7121996-12-06 21:23:53 +000074\end{funcdesc}
75
Guido van Rossum8cf94e61998-02-18 05:09:14 +000076\begin{funcdesc}{mktime_tz}{tuple}
Fred Drakecdea8a31998-03-14 06:17:43 +000077Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC
78timestamp. It the timezone item in the tuple is \code{None}, assume
79local time. Minor deficiency: this first interprets the first 8
80elements as a local time and then compensates for the timezone
81difference; this may yield a slight error around daylight savings time
Guido van Rossum8cf94e61998-02-18 05:09:14 +000082switch dates. Not enough to worry about for common use.
83\end{funcdesc}
84
Guido van Rossumecde7811995-03-28 13:35:14 +000085\subsection{Message Objects}
Fred Drakee14dde21998-04-04 06:19:30 +000086\label{message-objects}
Guido van Rossumecde7811995-03-28 13:35:14 +000087
Fred Drakecdea8a31998-03-14 06:17:43 +000088A \class{Message} instance has the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000089
Fred Drakee14dde21998-04-04 06:19:30 +000090\begin{methoddesc}{rewindbody}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000091Seek to the start of the message body. This only works if the file
92object is seekable.
Fred Drakee14dde21998-04-04 06:19:30 +000093\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000094
Guido van Rossum444d0f81998-06-11 13:50:02 +000095\begin{methoddesc}{isheader}{line}
96Returns a line's canonicalized fieldname (the dictionary key that will
97be used to index it) if the line is a legal RFC822 header; otherwise
98returns None (implying that parsing should stop here and the line be
99pushed back on the input stream). It is sometimes useful to override
100this method in a subclass.
101\end{methoddesc}
102
Guido van Rossum12991001998-06-10 21:34:27 +0000103\begin{methoddesc}{islast}{line}
104Return true if the given line is a delimiter on which Message should
Guido van Rossum444d0f81998-06-11 13:50:02 +0000105stop. The delimiter line is consumed, and the file object's read
106location positioned immediately after it. By default this method just
107checks that the line is blank, but you can override it in a subclass.
Guido van Rossum12991001998-06-10 21:34:27 +0000108\end{methoddesc}
109
110\begin{methoddesc}{iscomment}{line}
111Return true if the given line should be ignored entirely, just skipped.
112By default this is a stub that always returns false, but you can
113override it in a subclass.
114\end{methoddesc}
115
Fred Drakee14dde21998-04-04 06:19:30 +0000116\begin{methoddesc}{getallmatchingheaders}{name}
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000117Return a list of lines consisting of all headers matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000118\var{name}, if any. Each physical line, whether it is a continuation
119line or not, is a separate list item. Return the empty list if no
120header matches \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000121\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000122
Fred Drakee14dde21998-04-04 06:19:30 +0000123\begin{methoddesc}{getfirstmatchingheader}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000124Return a list of lines comprising the first header matching
125\var{name}, and its continuation line(s), if any. Return \code{None}
126if there is no header matching \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000127\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000128
Fred Drakee14dde21998-04-04 06:19:30 +0000129\begin{methoddesc}{getrawheader}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000130Return a single string consisting of the text after the colon in the
131first header matching \var{name}. This includes leading whitespace,
132the trailing linefeed, and internal linefeeds and whitespace if there
133any continuation line(s) were present. Return \code{None} if there is
134no header matching \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000135\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000136
Guido van Rossum12991001998-06-10 21:34:27 +0000137\begin{methoddesc}{getheader}{name\optional{, default}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000138Like \code{getrawheader(\var{name})}, but strip leading and trailing
Guido van Rossum12991001998-06-10 21:34:27 +0000139whitespace. Internal whitespace is not stripped. The optional
140\var{default} argument can be used to specify a different default to
141be returned when there is no header matching \var{name}.
142\end{methoddesc}
143
144\begin{methoddesc}{get}{name\optional{, default}}
145An alias for \code{getheader()}, to make the interface more compatible
146with regular dictionaries.
Fred Drakee14dde21998-04-04 06:19:30 +0000147\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000148
Fred Drakee14dde21998-04-04 06:19:30 +0000149\begin{methoddesc}{getaddr}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000150Return a pair \code{(\var{full name}, \var{email address})} parsed
151from the string returned by \code{getheader(\var{name})}. If no
152header matching \var{name} exists, return \code{(None, None)};
153otherwise both the full name and the address are (possibly empty)
154strings.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000155
Fred Drakecdea8a31998-03-14 06:17:43 +0000156Example: If \var{m}'s first \code{From} header contains the string
Guido van Rossum470be141995-03-17 16:07:09 +0000157\code{'jack@cwi.nl (Jack Jansen)'}, then
Guido van Rossuma12ef941995-02-27 17:53:25 +0000158\code{m.getaddr('From')} will yield the pair
Guido van Rossum470be141995-03-17 16:07:09 +0000159\code{('Jack Jansen', 'jack@cwi.nl')}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000160If the header contained
Guido van Rossum470be141995-03-17 16:07:09 +0000161\code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000162exact same result.
Fred Drakee14dde21998-04-04 06:19:30 +0000163\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000164
Fred Drakee14dde21998-04-04 06:19:30 +0000165\begin{methoddesc}{getaddrlist}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000166This is similar to \code{getaddr(\var{list})}, but parses a header
167containing a list of email addresses (e.g. a \code{To} header) and
Fred Drakecdea8a31998-03-14 06:17:43 +0000168returns a list of \code{(\var{full name}, \var{email address})} pairs
169(even if there was only one address in the header). If there is no
170header matching \var{name}, return an empty list.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000171
172XXX The current version of this function is not really correct. It
173yields bogus results if a full name contains a comma.
Fred Drakee14dde21998-04-04 06:19:30 +0000174\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000175
Fred Drakee14dde21998-04-04 06:19:30 +0000176\begin{methoddesc}{getdate}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000177Retrieve a header using \method{getheader()} and parse it into a 9-tuple
178compatible with \function{time.mktime()}. If there is no header matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000179\var{name}, or it is unparsable, return \code{None}.
180
181Date parsing appears to be a black art, and not all mailers adhere to
182the standard. While it has been tested and found correct on a large
183collection of email from many sources, it is still possible that this
184function may occasionally yield an incorrect result.
Fred Drakee14dde21998-04-04 06:19:30 +0000185\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000186
Fred Drakee14dde21998-04-04 06:19:30 +0000187\begin{methoddesc}{getdate_tz}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000188Retrieve a header using \method{getheader()} and parse it into a
18910-tuple; the first 9 elements will make a tuple compatible with
190\function{time.mktime()}, and the 10th is a number giving the offset
191of the date's timezone from UTC. Similarly to \method{getdate()}, if
Guido van Rossum843e7121996-12-06 21:23:53 +0000192there is no header matching \var{name}, or it is unparsable, return
193\code{None}.
Fred Drakee14dde21998-04-04 06:19:30 +0000194\end{methoddesc}
Guido van Rossum843e7121996-12-06 21:23:53 +0000195
Fred Drakecdea8a31998-03-14 06:17:43 +0000196\class{Message} instances also support a read-only mapping interface.
Fred Drakee14dde21998-04-04 06:19:30 +0000197In particular: \code{\var{m}[name]} is like
198\code{\var{m}.getheader(name)} but raises \exception{KeyError} if
199there is no matching header; and \code{len(\var{m})},
Fred Drakecdea8a31998-03-14 06:17:43 +0000200\code{\var{m}.has_key(name)}, \code{\var{m}.keys()},
201\code{\var{m}.values()} and \code{\var{m}.items()} act as expected
202(and consistently).
Guido van Rossuma12ef941995-02-27 17:53:25 +0000203
Fred Drakecdea8a31998-03-14 06:17:43 +0000204Finally, \class{Message} instances have two public instance variables:
Guido van Rossuma12ef941995-02-27 17:53:25 +0000205
Fred Drakee14dde21998-04-04 06:19:30 +0000206\begin{memberdesc}{headers}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000207A list containing the entire set of header lines, in the order in
Guido van Rossum87294831998-06-16 22:27:40 +0000208which they were read (except that setitem calls may disturb this
209order). Each line contains a trailing newline. The
Guido van Rossuma12ef941995-02-27 17:53:25 +0000210blank line terminating the headers is not contained in the list.
Fred Drakee14dde21998-04-04 06:19:30 +0000211\end{memberdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000212
Fred Drakee14dde21998-04-04 06:19:30 +0000213\begin{memberdesc}{fp}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000214The file object passed at instantiation time.
Fred Drakee14dde21998-04-04 06:19:30 +0000215\end{memberdesc}
Guido van Rossum87294831998-06-16 22:27:40 +0000216
217\subsection{AddressList Objects}
218\label{addresslist-objects}
219
220An \class{AddressList} instance has the following methods:
221
222\begin{methoddesc}{__len__}{name}
223Return the number of addresses in the address list.
224\end{methoddesc}
225
226\begin{methoddesc}{__str__}{name}
227Return a canonicalized string representation of the address list.
228Addresses are rendered in "name" <host@domain> form, comma-separated.
229\end{methoddesc}
230
231\begin{methoddesc}{__add__}{name}
232Return an AddressList instance that contains all addresses in both
233AddressList operands, with duplicates removed (set union).
234\end{methoddesc}
235
236\begin{methoddesc}{__sub__}{name}
237Return an AddressList instance that contains every address in the
238left-hand AddressList operand that is not present in the right-hand
239address operand (set difference).
240\end{methoddesc}
241
242
243Finally, \class{AddressList} instances have one public instance variable:
244
245\begin{memberdesc}{addresslist}
246A list of tuple string pairs, one per address. In each member, the
247first is the canonicalized name part of the address, the second is the
248route-address (@-separated host-domain pair).
249\end{memberdesc}