�
��c@s�dZddkZdddddgZdd fZdfd
��YZd�Zd�Zd
�Zdfd��YZdefd��YZ d�Z
ddddddddddddddd d!dd"d#d$d%d&d'd(gZd)d*d+d,d-d.d/gZhd0d1<d0d2<d0d3<d0d4<d5d6<d7d8<d9d:<d5d;<d<d=<d9d><d?d@<d<dA<dBdC<d?dD<Z
dE�ZdF�ZdG�ZedH�ZedIjoddkZddkZeiieidJdK�ZeidLoeidLZneedM�Zee�ZdNGeidO�GHdPGeidQ�GHdRGei dS�GHdTGei dU�GHei!dU�Z"e"dZ#ei$ee"��Z"e"ogdVGei%e"�Ge#Z&e'e&dW�\Z(Z)e'e(dW�\Z*Z+dXe*e+fGe)odYe)GnHn
dVGeGHei,�d0Z-xei.�oe-dL7Z-q"WdZGe-GHd[d\GHd]Ge/e�GHd^ejod_Ged^GHnd`ejondaGei0�GHdbGei1�GHdcGei2�GHndS(ds,RFC 2822 message manipulation.
Note: This is only a very rough sketch of a full RFC-822 parser; in particular
the tokenizing of addresses does not adhere to all the quoting rules.
Note: RFC 2822 is a long awaited update to RFC 822. This module should
conform to RFC 2822, and is thus mis-named (it's not worth renaming it). Some
effort at RFC 2822 updates have been made, but a thorough audit has not been
performed. Consider any RFC 2822 non-conformance to be a bug.
RFC 2822: http://www.faqs.org/rfcs/rfc2822.html
RFC 822 : http://www.faqs.org/rfcs/rfc822.html (obsolete)
Directions for use:
To create a Message object: first open a file, e.g.:
fp = open(file, 'r')
You can use any other legal way of getting an open file object, e.g. use
sys.stdin or call os.popen(). Then pass the open file object to the Message()
constructor:
m = Message(fp)
This class can work with any input object that supports a readline method. If
the input object has seek and tell capability, the rewindbody method will
work; also illegal lines will be pushed back onto the input stream. If the
input object lacks seek but has an `unread' method that can push back a line
of input, Message will use that to push back illegal lines. Thus this class
can be used to parse messages coming from a buffered stream.
The optional `seekable' argument is provided as a workaround for certain stdio
libraries in which tell() discards buffered data before discovering that the
lseek() system call doesn't work. For maximum portability, you should set the
seekable argument to zero to prevent that initial \code{tell} when passing in
an unseekable object such as a a file object created from a socket object. If
it is 1 on entry -- which it is by default -- the tell() method of the open
file object is called once; if this raises an exception, seekable is reset to
0. For other nonzero values of seekable, this test is not made.
To get the text of a particular header there are several methods:
str = m.getheader(name)
str = m.getrawheader(name)
where name is the name of the header, e.g. 'Subject'. The difference is that
getheader() strips the leading and trailing whitespace, while getrawheader()
doesn't. Both functions retain embedded whitespace (including newlines)
exactly as they are specified in the header, and leave the case of the text
unchanged.
For addresses and address lists there are functions
realname, mailaddress = m.getaddr(name)
list = m.getaddrlist(name)
where the latter returns a list of (realname, mailaddr) tuples.
There is also a method
time = m.getdate(name)
which parses a Date-like field and returns a time-compatible tuple,
i.e. a tuple such as returned by time.localtime() or accepted by
time.mktime().
See the class definition for lower level access methods.
There are also some utility functions here.
i�NtMessagetAddressListt parsedatetparsedate_tzt mktime_tzs
s
cBseZdZdd�Zd�Zd�Zd�Zd�Zd�Zd�Z d �Z
d
�Zdd�Z
e
Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zdd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZRS(s/Represents a single RFC 2822-compliant message.icCs�|djo4y|i�WqAttfj
o
d}qAXn||_||_d|_d|_|io9y|ii�|_Wq�tj
od|_q�Xn|i�|io9y|ii�|_Wq�j
od|_q�ndS(s3Initialize the class instance and read the headers.iiN( ttelltAttributeErrortIOErrortfptseekabletNonetstartofheaderststartofbodytreadheaders(tselfRR ((s/sys/lib/python/rfc822.pyt__init__Ts(
cCs.|ip
td�n|ii|i�dS(s7Rewind the file to the start of the body (if seekable).sunseekable fileN(R RRtseekR(R((s/sys/lib/python/rfc822.pyt
rewindbodyps
c CsTh|_d|_g|_}d|_d}d}d}}}t|id�o|ii}n|io|ii }nx�|o:y
|�}Wq�t
j
od}}d|_q�Xn|ii�}|pd|_Pn|o'|id�o|i||_q�nd}|oS|ddjoB|i
|�|i|d|i�}|i�|i|<q�n-|i|�oq�n|i|�oPn|i|�}|o5|i
|�|t|�di�|i|<q�q�|ip
d |_n
d
|_|o||�n,|o|ii|�n|id|_Pq�dS(
s�Read header lines.
Read header lines up to the entirely blank line that terminates them.
The (normally blank) line that ends the headers is skipped, but not
included in the returned list. If a non-header line ends the headers,
(which is an error), an attempt is made to backspace over it; it is
never included in the returned list.
The variable self.status is set to the empty string if all went well,
otherwise it is an error message. The variable self.headers is a
completely uninterpreted list of lines contained in the header (so
printing them will reproduce the header exactly as it appears in the
file).
titunreadisEOF in headerssFrom s s
s
No headerss%Non-header line where header expecteds
; bad seekN(tdicttunixfromtheaderststatusR
thasattrRRR RRtreadlinet
startswithtappendtstript iscommenttislasttisheadertlenR( Rtlstt
headerseent firstlinetstartoflineRRtlinetx((s/sys/lib/python/rfc822.pyR
vsb
!
cCs2|id�}|djo|| i�SndS(sDetermine whether a given line is a legal header.
This method should return the header name, suitably canonicalized.
You may override this method in order to use Message parsing on tagged
data in RFC 2822-like formats with special header formats.
t:iN(tfindtlowerR
(RR%ti((s/sys/lib/python/rfc822.pyR�s
cCs
|tjS(sdDetermine whether a line is a legal end of RFC 2822 headers.
You may override this method if your application wants to bend the
rules, e.g. to strip trailing whitespace, or to recognize MH template
separators ('--------'). For convenience (e.g. for code reading from
sockets) a line consisting of
also matches.
(t_blanklines(RR%((s/sys/lib/python/rfc822.pyR�scCstS(s�Determine whether a line should be skipped entirely.
You may override this method in order to use Message parsing on tagged
data in RFC 2822-like formats that support embedded comments or
free-text data.
(tFalse(RR%((s/sys/lib/python/rfc822.pyR�scCs�|i�d}t|�}g}d}xe|iD]Z}|| i�|jo
d}n|d i�p
d}n|o|i|�q2q2W|S(s�Find all header lines matching a given header name.
Look through the list of headers and find all lines matching a given
header name (and their continuation lines). A list of the lines is
returned, without interpretation. If the header does not occur, an
empty list is returned. If the header occurs multiple times, all
occurrences are returned. Case is not important in the header name.
R'ii(R)R RtisspaceR(RtnametnR!thitR%((s/sys/lib/python/rfc822.pytgetallmatchingheaders�s
cCs�|i�d}t|�}g}d}xk|iD]`}|o|d i�pPqzn"|| i�|jo
d}n|o|i|�q2q2W|S(s�Get the first header line matching name.
This is similar to getallmatchingheaders, but it returns only the
first matching header (and its continuation lines).
R'ii(R)R RR-R(RR.R/R!R0R%((s/sys/lib/python/rfc822.pytgetfirstmatchingheader�s
cCsG|i|�}|pdSn|dt|�d|d<di|�S(s2A higher-level interface to getfirstmatchingheader().
Return a string containing the literal text of the header but with the
keyword stripped. All leading, trailing and embedded whitespace is
kept in the string, however. Return None if the header does not
occur.
iiRN(R2R
R tjoin(RR.R!((s/sys/lib/python/rfc822.pytgetrawheader
s
cCs|ii|i�|�S(s
Get the header value for a name.
This is the normal interface: it returns a stripped version of the
header value for a given header name, or None if it doesn't exist.
This uses the dictionary version which finds the *last* such header.
(RtgetR)(RR.tdefault((s/sys/lib/python/rfc822.pyt getheaderscCs�g}d}d}x�|i|�D]�}|di�o1|od||i�f}q�|i�}q"|o|i|�n||id�di�}d}q"W|o|i|�n|S(s Get all values for a header.
This returns a list of values for headers given more than once; each
value in the result list is stripped in the same way as the result of
getheader(). If the header is not given, return an empty list.
Ris%s
%sR'i(R1R-RRR((RR.tresulttcurrentthave_headerts((s/sys/lib/python/rfc822.pyt
getheaders#s
cCs*|i|�}|o|dSndSdS(s�Get a single address from a header, as a tuple.
An example return value:
('Guido van Rossum', 'guido@cwi.nl')
iN(NN(tgetaddrlistR
(RR.talist((s/sys/lib/python/rfc822.pytgetaddr<scCs�g}x�|i|�D]{}|ddjo|i|�q|o|id�n|id�}|djo||d}n|i|�qWdi|�}t|�}|iS(s
Get a list of addresses from a header.
Retrieves a list of addresses from a header, where each address is a
tuple as returned by getaddr(). Scans all named headers, so it works
properly with multiple To: or Cc: headers for example.
is s, R'iR(R1RR(R3Rtaddresslist(RR.trawthR*taddrtalladdrsta((s/sys/lib/python/rfc822.pyR=Is
cCs2y||}Wntj
odSnXt|�S(s�Retrieve a date field from a header.
Retrieves a date field from the named header, returning a tuple
compatible with time.mktime().
N(tKeyErrorR
R(RR.tdata((s/sys/lib/python/rfc822.pytgetdate_s
cCs2y||}Wntj
odSnXt|�S(s�Retrieve a date field from a header as a 10-tuple.
The first 9 elements make up a tuple compatible with time.mktime(),
and the 10th is the offset of the poster's time zone from GMT/UTC.
N(RFR
R(RR.RG((s/sys/lib/python/rfc822.pyt
getdate_tzks
cCs
t|i�S(s'Get the number of headers in a message.(R R(R((s/sys/lib/python/rfc822.pyt__len__zscCs|i|i�S(s,Get a specific header, as from a dictionary.(RR)(RR.((s/sys/lib/python/rfc822.pyt__getitem__~scCsZ||=||i|i�<|d|}x+|id�D]}|ii|d�q8WdS(s�Set the value of a header.
Note: This is not a perfect inversion of __getitem__, because any
changed headers get stuck at the end of the raw-headers list rather
than where the altered header was.
s: s
N(RR)tsplitRR(RR.tvaluettextR%((s/sys/lib/python/rfc822.pyt__setitem__�scCs�|i�}||ijodSn|i|=|d}t|�}g}d}x~tt|i��D]g}|i|}|| i�|jo
d}n|d i�p
d}n|o|i|�qfqfWxt|�D]}|i|=q�WdS(s>Delete all occurrences of a specific header, if it is present.NR'ii(R)RR trangeRR-Rtreversed(RR.R/R!R0R*R%((s/sys/lib/python/rfc822.pyt__delitem__�s(
RcCs||i�}||ijo|i|SnN|d|}x+|id�D]}|ii|d�qIW||i|<|SdS(Ns: s
(R)RRLRR(RR.R6t lowernameRNR%((s/sys/lib/python/rfc822.pyt
setdefault�s
cCs|i�|ijS(s6Determine whether a message contains the named header.(R)R(RR.((s/sys/lib/python/rfc822.pythas_key�scCs|i�|ijS(s6Determine whether a message contains the named header.(R)R(RR.((s/sys/lib/python/rfc822.pyt__contains__�scCs
t|i�S(N(titerR(R((s/sys/lib/python/rfc822.pyt__iter__�scCs
|ii�S(s*Get all of a message's header field names.(Rtkeys(R((s/sys/lib/python/rfc822.pyRY�scCs
|ii�S(s+Get all of a message's header field values.(Rtvalues(R((s/sys/lib/python/rfc822.pyRZ�scCs
|ii�S(sWGet all of a message's headers.
Returns a list of name, value tuples.
(Rtitems(R((s/sys/lib/python/rfc822.pyR[�scCsdi|i�S(NR(R3R(R((s/sys/lib/python/rfc822.pyt__str__�sN( t__name__t
__module__t__doc__RRR
RRRR1R2R4R
R7R5R<R?R=RHRIRJRKRORRRTRURVRXRYRZR[R\(((s/sys/lib/python/rfc822.pyRQs: K
cCs�t|�djoz|id�o7|id�o'|dd!idd�idd�Sn|id�o|id�o|dd!Sq�n|S( sRemove quotes from a string.it"i�s\\s\s\"t<t>(R Rtendswithtreplace(R;((s/sys/lib/python/rfc822.pytunquote�s ' cCs|idd�idd�S(sAdd quotes around a string.s\s\\R`s\"(Rd(R;((s/sys/lib/python/rfc822.pytquote�scCs,t|�}|i}|pdSn|dS(s3Parse an address into a (realname, mailaddr) tuple.iN(NN(RR@R
(taddressRER!((s/sys/lib/python/rfc822.pyt parseaddr�s
t
AddrlistClasscBs�eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z dd �Z
d
�Zd�Zd�Z
dd
�Zd�ZRS(s)Address parser class by Ben Escoto.
To understand what this class does, it helps to have a copy of
RFC 2822 in front of you.
http://www.faqs.org/rfcs/rfc2822.html
Note: this class interface is deprecated and may be removed in the future.
Use rfc822.AddressList instead.
cCsld|_d|_d|_d|_|i|i|i|_|iidd�|_||_g|_dS(s�Initialize a new instance.
`field' is an unparsed address header field, containing one or more
addresses.
s()<>@,:;."[]is s
t.RN( tspecialstpostLWStCRtatomendsRdt
phraseendstfieldtcommentlist(RRq((s/sys/lib/python/rfc822.pyR�s cCs�x�|it|i�joh|i|i|idjo|id|_q|i|idjo|ii|i��qPqWdS(s*Parse up to the start of the next address.s
it(N(RlR RqRmRrRt
getcomment(R((s/sys/lib/python/rfc822.pytgotonext
scCs;g}|i�}x"|o||7}|i�}qW|S(sVParse all addresses.
Returns a list containing all of the addresses.
(t
getaddress(RR8tad((s/sys/lib/python/rfc822.pyR=s
cCs�g|_|i�|i}|i}|i�}|i�g}|it|i�jo.|o#di|i�|dfg}qen�|i|idjo=||_||_|i�}di|i�|fg}n�|i|idjo�g}t|i�}|id7_xK|it|i�joY|i�|i|jo+|i|idjo|id7_Pn||i�}qWn�|i|idjof|i �}|io4di|�ddi|i�d |fg}qedi|�|fg}nX|o#di|i�|dfg}n.|i|i|i
jo|id7_n|i�|it|i�jo*|i|id
jo|id7_n|S(sParse the next address.t is.@R'it;Ras (t)t,(RrRuRlt
getphraselistR RqR3tgetaddrspecRvtgetrouteaddrRk(Rtoldpostoldcltplistt
returnlisttaddrspectfieldlent routeaddr((s/sys/lib/python/rfc822.pyRvsL
'
'
4#
0cCs/|i|idjodSnd}|id7_|i�d}x�it|i�jo�|o|i�d}n�|i|idjo|id7_Pnw|i|idjo|id7_d}nG|i|idjo|id7_n|i�}|id7_P|i�qGW|S( s�Parse a route address (Return-path value).
This method just skips all the route stuff and returns the addrspec.
RaNiiRRbt@R'(RqRlRuR t getdomainR}(Rtexpectroutetadlist((s/sys/lib/python/rfc822.pyR~Ys.
cCsTg}|i�x�|it|i�jo�|i|idjo |id�|id7_ne|i|idjo|id|i��n3|i|i|ijoPn|i|i��|i�qW|it|i�jp|i|idjodi|�Sn|id�|id7_|i�di|�|i �S(sParse an RFC 2822 addr-spec.RjiR`s"%s"R�R(
RuRlR RqRtgetquoteRotgetatomR3R�(Rtaslist((s/sys/lib/python/rfc822.pyR}ys$
0
cCs)g}x|it|i�jo�i|i|ijo|id7_q |i|idjo|ii|i��q |i|idjo|i|i��q |i|idjo |id7_|id�q |i|i|ijoPq |i|i ��q Wdi
|�S(s-Get the complete domain name from an address.iRst[RjR(RlR RqRmRrRRttgetdomainliteralRoR�R3(Rtsdlist((s/sys/lib/python/rfc822.pyR��sicCsC|i|i|jodSndg}d}|id7_x�it|i�jo�|djo!|i|i|i�d}n�|i|i|jo|id7_Pnq|o1|i|idjo|i|i��q@n9|i|idjo
d}n|i|i|i�|id7_q@Wdi|�S(s�Parse a header fragment delimited by special characters.
`beginchar' is the start character for the fragment. If self is not
looking at an instance of `beginchar' then getdelimited returns the
empty string.
`endchars' is a sequence of allowable end-delimiting characters.
Parsing stops when one of these is encountered.
If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
within the parsed fragment.
RiiRss\(RqRlR RRtR3(Rt beginchartendcharst
allowcommentstslistRf((s/sys/lib/python/rfc822.pytgetdelimited�s(
cCs|iddd�S(s1Get a quote-delimited fragment from self's field.R`s"
i(R�(R((s/sys/lib/python/rfc822.pyR��scCs|iddd�S(s7Get a parenthesis-delimited fragment from self's field.Rss)
i(R�(R((s/sys/lib/python/rfc822.pyRt�scCsd|iddd�S(s!Parse an RFC 2822 domain-literal.s[%s]R�s]
i(R�(R((s/sys/lib/python/rfc822.pyR��scCs�dg}|djo
|i}nx`|it|i�joF|i|i|joPn|i|i|i�|id7_q&Wdi|�S(sParse an RFC 2822 atom.
Optional atomends specifies a different set of end token delimiters
(the default is to use self.atomends). This is used e.g. in
getphraselist() since phrase endings must not include the `.' (which
is legal in phrases).RiN(R
RoRlR RqRR3(RRotatomlist((s/sys/lib/python/rfc822.pyR��s
cCs�g}x�it|i�jo�|i|i|ijo|id7_q |i|idjo|i|i��q |i|idjo|ii|i��q |i|i|ijoPq |i|i |i��q W|S(s�Parse a sequence of RFC 2822 phrases.
A phrase is a sequence of words, which are in turn either RFC 2822
atoms or quoted-strings. Phrases are canonicalized by squeezing all
runs of continuous whitespace into one space.
iR`Rs(
RlR RqRmRR�RrRtRpR�(RR�((s/sys/lib/python/rfc822.pyR|�sN(R]R^R_RRuR=RvR~R}R�R�R�RtR�R
R�R|(((s/sys/lib/python/rfc822.pyRi�s
: % cBsVeZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
RS( s@An AddressList encapsulates a list of parsed RFC 2822 addresses.cCs7ti||�|o|i�|_n
g|_dS(N(RiRR=R@(RRq((s/sys/lib/python/rfc822.pyRscCs
t|i�S(N(R R@(R((s/sys/lib/python/rfc822.pyRJscCsditt|i��S(Ns, (R3tmaptdump_address_pairR@(R((s/sys/lib/python/rfc822.pyR\
scCsUtd�}|i|_x5|iD]*}||ijo|ii|�q#q#W|S(N(RR
R@R(RtothertnewaddrR&((s/sys/lib/python/rfc822.pyt__add__
s
cCs<x5|iD]*}||ijo|ii|�q
q
W|S(N(R@R(RR�R&((s/sys/lib/python/rfc822.pyt__iadd__s
cCsHtd�}x5|iD]*}||ijo|ii|�qqW|S(N(RR
R@R(RR�R�R&((s/sys/lib/python/rfc822.pyt__sub__s
cCs<x5|iD]*}||ijo|ii|�q
q
W|S(N(R@tremove(RR�R&((s/sys/lib/python/rfc822.pyt__isub__%s
cCs|i|S(N(R@(Rtindex((s/sys/lib/python/rfc822.pyRK,s(R]R^R_RRJR\R�R�R�R�RK(((s/sys/lib/python/rfc822.pyR�s cCs7|do d|dd|ddSn |dSdS(s4Dump a (name, address) pair in a canonicalized form.iR`s" <iRbN((tpair((s/sys/lib/python/rfc822.pyR�0s tjantfebtmartaprtmaytjuntjultaugtseptocttnovtdectjanuarytfebruarytmarchtapriltjunetjulytaugustt septembertoctobertnovembertdecembertmonttuetwedtthutfritsattsunitUTtUTCtGMTtZip�ASTi��ADTi�ESTtEDTi��CSTtCDTiD�MSTtMDTi��PSTtPDTcCs|pdSn|i�}|dddjp|di�tjo|d=n;|did�}|djo|d|d|d<nt|�djo<|did�}t|�djo||d}q�nt|�djoW|d}|id �}|djo || ||dg|d)qE|id
�nt|�djodSn|d }|\}}}}}|i�}|tjo,||i�}}|tjodSq�nti |�d}|djo|d}n|ddjo|d }n|id
�}|djo||}}n|ddjo|d }n|di
�p||}}n|ddjo|d }n|id
�}t|�djo|\} }
d}n+t|�djo|\} }
}ndSy@t|�}t|�}t| �} t|
�}
t|�}Wntj
odSnXd}|i
�}|tjot|}n'yt|�}Wntj
onX|oF|djod}
|}nd}
|
|dd|dd}n|||| |
|ddd|f
S(sQConvert a date string to a time tuple.
Accounts for military timezones.
ii�R{Rjiit-it+RiiR'it0idii<N(R{Rj(R
RLR)t _daynamestrfindR R(Rt_monthnamesR�tisdigittintt
ValueErrortuppert
_timezones(RGR*tstuffR;tddtmmtyyttmttztthhttmmttssttzoffsetttzsign((s/sys/lib/python/rfc822.pyRNs�,
"cCs)t|�}|djo|Sn|d S(s&Convert a time string to a time tuple.i N(RR
(RGtt((s/sys/lib/python/rfc822.pyR�s
cCsX|ddjoti|d d�Sn+ti|d d�}||dtiSdS(sCTurn a 10-tuple as returned by parsedate_tz() into a UTC timestamp.i ii�iN(i�(i(R
ttimetmktimettimezone(RGR�((s/sys/lib/python/rfc822.pyR�scCsq|djoti�}nti|�}dd|d |d
d|dd|d|d|d|dfS(s\Returns time format preferred for Internet standards.
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
According to RFC 1123, day and month names must always be in
English. If not for that, this code could use strftime(). It
can't because strftime() honors the locale and could generated
non-English names.
s#%s, %02d %s %04d %02d:%02d:%02d GMTtMontTuetWedtThutFritSattSuniitJantFebtMartAprtMaytJuntJultAugtSeptOcttNovtDeciiiiiN(R�R�R�R�R�R�R�(R�R�R�R�R�R�R�R�R�R�R�R�(R
R�tgmtime(ttimeval((s/sys/lib/python/rfc822.pyt
formatdate�s
t__main__tHOMEsMail/inbox/1itrsFrom:tfromsTo:ttosSubject:tsubjectsDate:tdatesParsedDate:i<s %+03d%02ds.%02dsLines:R�iFslen =tDatesDate =s
X-Nonsenseskeys =svalues =sitems =(3R_R�t__all__R+RReRfRhRiRR�R�R�R�RRRR
R�R]tsystostpathR3tenvirontfiletargvtopentftmR?R=R7RIRR�t localtimetasctimethhmmsstdivmodthhmmtssthhR�RR/RR RYRZR[(((s/sys/lib/python/rfc822.pys<module>Gsz�
�2 ( U
|