usr/lib64/python2.7/rfc822.pyo000064400000076105147204467570011635 0ustar00 zfc@sdZddlZddlmZedddddd d d gZd d fZdfdYZdZdZdZ dfdYZ de fdYZ dZ ddddddddddd d!d"d#d$d%dd&d'd(d)d*d+d,gZ d-d.d/d0d1d2d3gZid4d56d4d66d4d76d4d86d9d:6d;d<6d=d>6d9d?6d@dA6d=dB6dCdD6d@dE6dFdG6dCdH6ZdIZdJZdKZedLZedMkrddlZddlZejjejdNdOZejdPrejdPZneedQZeeZdRGej dSGHdTGej!dUGHdVGej"dWGHdXGej"dYGHej#dYZ$e$dZ%ej&ee$Z$e$rdZGej'e$Ge%Z(e)e(d[\Z*Z+e)e*d[\Z,Z-d\e,e-fGe+rd]e+GnHn dZGeGHej.d4Z/xej0r8e/dP7Z/qWd^Ge/GHd_d`GHdaGe1eGHdbekrvdcGedbGHnddekrndeGej2GHdfGej3GHdgGej4GHndS(hs* 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 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. iN(twarnpy3ks=in 3.x, rfc822 has been removed in favor of the email packaget stacklevelitMessaget AddressListt parsedatet parsedate_tzt mktime_tzs s cBseZdZddZdZdZdZdZdZdZ d Z d Z dd Z e Zd Zd ZdZdZdZdZdZdZdZddZdZdZdZdZdZdZdZRS(s/Represents a single RFC 2822-compliant message.icCs|dkr=y|jWq=ttfk r9d}q=Xn||_||_d|_d|_|jry|jj|_Wqtk rd|_qXn|j|jry|jj|_Wqtk rd|_qXndS(s3Initialize the class instance and read the headers.iiN( ttelltAttributeErrortIOErrortfptseekabletNonetstartofheaderst startofbodyt readheaders(tselfR R ((s/usr/lib64/python2.7/rfc822.pyt__init__Xs(           cCs,|jstdn|jj|jdS(s7Rewind the file to the start of the body (if seekable).sunseekable fileN(R R R tseekR(R((s/usr/lib64/python2.7/rfc822.pyt rewindbodyts  c CsKi|_d|_g|_}d|_d}d}d }}}t|jdrc|jj}n|jr{|jj }nx|ry |}Wqt k rd }}d|_qXn|jj }|sd|_Pn|r|j dr|j||_q~nd}|re|ddkre|j ||j|d|j}|j|j|Delete all occurrences of a specific header, if it is present.NR)ii(R+RR"trangeRR/Rtreversed(RR0R1R#R2R,R'((s/usr/lib64/python2.7/rfc822.pyt __delitem__s$       RcCsw|j}||jkr&|j|S|d|}x+|jdD]}|jj|dqDW||j|<|SdS(Ns: s (R+RRNRR(RR0R8t lowernameRPR'((s/usr/lib64/python2.7/rfc822.pyt setdefaults   cCs|j|jkS(s6Determine whether a message contains the named header.(R+R(RR0((s/usr/lib64/python2.7/rfc822.pythas_keyscCs|j|jkS(s6Determine whether a message contains the named header.(R+R(RR0((s/usr/lib64/python2.7/rfc822.pyt __contains__scCs t|jS(N(titerR(R((s/usr/lib64/python2.7/rfc822.pyt__iter__scCs |jjS(s*Get all of a message's header field names.(Rtkeys(R((s/usr/lib64/python2.7/rfc822.pyR[scCs |jjS(s+Get all of a message's header field values.(Rtvalues(R((s/usr/lib64/python2.7/rfc822.pyR\scCs |jjS(sWGet all of a message's headers. Returns a list of name, value tuples. (Rtitems(R((s/usr/lib64/python2.7/rfc822.pyR]scCsdj|jS(NR(R5R(R((s/usr/lib64/python2.7/rfc822.pyt__str__sN( t__name__t __module__t__doc__RRRR!R RR3R4R6R R9R7R>RAR?RJRKRLRMRQRTRVRWRXRZR[R\R]R^(((s/usr/lib64/python2.7/rfc822.pyRUs:   P                cCst|dkr|jdrS|jdrS|dd!jddjddS|jdr|jdr|dd!Sn|S( sRemove quotes from a string.it"is\\s\s\"t(R"Rtendswithtreplace(R=((s/usr/lib64/python2.7/rfc822.pytunquotes #cCs|jddjddS(sAdd quotes around a string.s\s\\Rbs\"(Rf(R=((s/usr/lib64/python2.7/rfc822.pytquotescCs't|}|j}|sdS|dS(s3Parse an address into a (realname, mailaddr) tuple.iN(NN(RRBR (taddressRGR#((s/usr/lib64/python2.7/rfc822.pyt parseaddrs   t AddrlistClasscBseZdZdZdZdZdZdZdZdZ dd Z d Z d Z d Z dd ZdZRS(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|_|j|j|j|_|jjdd|_||_g|_dS(sInitialize a new instance. `field' is an unparsed address header field, containing one or more addresses. s ()<>@,:;."[]is s t.RN( tspecialstpostLWStCRtatomendsRft phraseendstfieldt commentlist(RRs((s/usr/lib64/python2.7/rfc822.pyRs     cCsx||jt|jkr~|j|j|jdkrK|jd|_q|j|jdkrz|jj|jqPqWdS(s*Parse up to the start of the next address.s it(N(RnR"RsRoRtRt getcomment(R((s/usr/lib64/python2.7/rfc822.pytgotonexts cCs9g}|j}x |r4||7}|j}qW|S(sVParse all addresses. Returns a list containing all of the addresses. (t getaddress(RR:tad((s/usr/lib64/python2.7/rfc822.pyR?s    cCsg|_|j|j}|j}|j}|jg}|jt|jkr|rPdj|j|dfg}qPn|j|jdkr||_||_|j}dj|j|fg}nz|j|jdkrg}t|j}|jd7_x=|jt|jkr|j|j|krm|j|jdkrm|jd7_Pn||j}qWn|j|jdkr|j }|jrdj|ddj|jd |fg}qPdj||fg}nS|r%dj|j|dfg}n+|j|j|j krP|jd7_n|j|jt|jkr|j|jd kr|jd7_n|S( sParse the next address.t is.@R)it;Rcs (t)t,( RtRwRnt getphraselistR"RsR5t getaddrspecRxt getrouteaddrRm(Rtoldpostoldcltplistt returnlisttaddrspectfieldlent routeaddr((s/usr/lib64/python2.7/rfc822.pyRx(sL      %    %  3" .cCs |j|jdkrdSd}|jd7_|jd}x|jt|jkr|rs|jd}n|j|jdkr|jd7_Pnr|j|jdkr|jd7_d}nD|j|jdkr|jd7_n|j}|jd7_P|jqBW|S( sParse a route address (Return-path value). This method just skips all the route stuff and returns the addrspec. RcNiiRRdt@R)(RsRnRwR"t getdomainR(Rt expectroutetadlist((s/usr/lib64/python2.7/rfc822.pyRbs.     cCsFg}|jx|jt|jkr|j|jdkr`|jd|jd7_n`|j|jdkr|jd|jn0|j|j|jkrPn|j|j|jqW|jt|jks|j|jdkr dj|S|jd|jd7_|jdj||j S(sParse an RFC 2822 addr-spec.RliRbs"%s"RR( RwRnR"RsRtgetquoteRqtgetatomR5R(Rtaslist((s/usr/lib64/python2.7/rfc822.pyRs$  .   cCsg}x|jt|jkr|j|j|jkrL|jd7_q |j|jdkr{|jj|jq |j|jdkr|j|jq |j|jdkr|jd7_|jdq |j|j|jkrPq |j|j q Wdj |S(s-Get the complete domain name from an address.iRut[RlR( RnR"RsRoRtRRvtgetdomainliteralRqRR5(Rtsdlist((s/usr/lib64/python2.7/rfc822.pyRsicCs3|j|j|krdSdg}d}|jd7_x|jt|jkr%|dkr|j|j|jd}n|j|j|kr|jd7_Pnk|r|j|jdkr|j|jq;n6|j|jdkrd}n|j|j|j|jd7_q;Wdj|S(sParse 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. RiiRus\(RsRnR"RRvR5(Rt beginchartendcharst allowcommentstslistRh((s/usr/lib64/python2.7/rfc822.pyt getdelimiteds(     cCs|jdddS(s1Get a quote-delimited fragment from self's field.Rbs" i(R(R((s/usr/lib64/python2.7/rfc822.pyRscCs|jdddS(s7Get a parenthesis-delimited fragment from self's field.Rus) i(R(R((s/usr/lib64/python2.7/rfc822.pyRvscCsd|jdddS(s!Parse an RFC 2822 domain-literal.s[%s]Rs] i(R(R((s/usr/lib64/python2.7/rfc822.pyRscCsdg}|dkr!|j}nx\|jt|jkr|j|j|krVPn|j|j|j|jd7_q$Wdj|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 RqRnR"RsRR5(RRqtatomlist((s/usr/lib64/python2.7/rfc822.pyRs   cCsg}x|jt|jkr|j|j|jkrL|jd7_q |j|jdkrx|j|jq |j|jdkr|jj|jq |j|j|jkrPq |j|j |jq W|S(sParse 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. iRbRu( RnR"RsRoRRRtRvRrR(RR((s/usr/lib64/python2.7/rfc822.pyR~sN(R_R`RaRRwR?RxRRRRRRvRR RR~(((s/usr/lib64/python2.7/rfc822.pyRks   :   %    cBsVeZdZdZdZdZdZdZdZdZ dZ RS( s@An AddressList encapsulates a list of parsed RFC 2822 addresses.cCs5tj|||r(|j|_n g|_dS(N(RkRR?RB(RRs((s/usr/lib64/python2.7/rfc822.pyR scCs t|jS(N(R"RB(R((s/usr/lib64/python2.7/rfc822.pyRLscCsdjtt|jS(Ns, (R5tmaptdump_address_pairRB(R((s/usr/lib64/python2.7/rfc822.pyR^scCsStd}|j|_x3|jD](}||jkr#|jj|q#q#W|S(N(RR RBR(RtothertnewaddrR(((s/usr/lib64/python2.7/rfc822.pyt__add__s   cCs:x3|jD](}||jkr |jj|q q W|S(N(RBR(RRR(((s/usr/lib64/python2.7/rfc822.pyt__iadd__scCsFtd}x3|jD](}||jkr|jj|qqW|S(N(RR RBR(RRRR(((s/usr/lib64/python2.7/rfc822.pyt__sub__&s  cCs:x3|jD](}||jkr |jj|q q W|S(N(RBtremove(RRR(((s/usr/lib64/python2.7/rfc822.pyt__isub__.scCs |j|S(N(RB(Rtindex((s/usr/lib64/python2.7/rfc822.pyRM5s( R_R`RaRRLR^RRRRRM(((s/usr/lib64/python2.7/rfc822.pyRs      cCs2|dr&d|dd|ddS|dSdS(s4Dump a (name, address) pair in a canonicalized form.iRbs" Gs     2  U