!A?hM & dZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z e dZee d zZe d ZeezZee d z Zee d z Zee d ze d z ZeezZee dzZeezZee dz ZddhZeezZdZdZejdejejzZ Gdde!Z"Gdde"Z#Gdde"Z$Gdde"Z%Gdde"Z&Gdd e#Z'Gd!d"e"Z(Gd#d$e"Z)Gd%d&e"Z*Gd'd(e"Z+Gd)d*e+Z,Gd+d,e#Z-Gd-d.e"Z.Gd/d0e"Z/Gd1d2e"Z0Gd3d4e"Z1Gd5d6e"Z2Gd7d8e"Z3Gd9d:e"Z4Gd;de"Z6Gd?d@e"Z7GdAdBe"Z8GdCdDe"Z9GdEdFe"Z:GdGdHe"Z;GdIdJe"Z<GdKdLe"Z=GdMdNe%Z>GdOdPe"Z?GdQdRe"Z@GdSdTe"ZAGdUdVe"ZBGdWdXeBZCGdYdZe"ZDGd[d\e"ZEGd]d^e"ZFGd_d`e"ZGGdadbe"ZHGdcddeHZIGdedfeHZJGdgdhe"ZKGdidje"ZLGdkdle"ZMGdmdneMZNGdodpeNZOGdqdre"ZPGdsdteQZRGdudveRZSGdwdxeRZTGdydzeSZUGd{d|e jVZWeTd d}ZXeTd~dZYdeY_ZdeY_[eTddZ\ejd]d^ej_Z`ejd]ejad^ejbZcejdjdZeejd]ejad^ejbZfejd]ejad^ejbZgejd]ejad^ejbZhdZidZjdZkddZldZmdZndZodZpdZqdZrdZsdZtdZudZvdZwdZxdZydZzdZ{dZ|dZ}dZ~dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdS)alHeader value parser implementing various email-related RFC parsing rules. The parsing methods defined in this module implement various email related parsing rules. Principal among them is RFC 5322, which is the followon to RFC 2822 and primarily a clarification of the former. It also implements RFC 2047 encoded word decoding. RFC 5322 goes to considerable trouble to maintain backward compatibility with RFC 822 in the parse phase, while cleaning up the structure on the generation phase. This parser supports correct RFC 5322 generation by tagging white space as folding white space only when folding is allowed in the non-obsolete rule sets. Actually, the parser is even more generous when accepting input than RFC 5322 mandates, following the spirit of Postel's Law, which RFC 5322 encourages. Where possible deviations from the standard are annotated on the 'defects' attribute of tokens that deviate. The general structure of the parser follows RFC 5322, and uses its terminology where there is a direct correspondence. Where the implementation requires a somewhat different structure than that used by the formal grammar, new terms that mimic the closest existing terms are used. Thus, it really helps to have a copy of RFC 5322 handy when studying this code. Input to the parser is a string that has already been unfolded according to RFC 5322 rules. According to the RFC this unfolding is the very first step, and this parser leaves the unfolding step to a higher level message parser, which will have already detected the line breaks that need unfolding while determining the beginning and end of each header. The output of the parser is a TokenList object, which is a list subclass. A TokenList is a recursive data structure. The terminal nodes of the structure are Terminal objects, which are subclasses of str. These do not correspond directly to terminal objects in the formal grammar, but are instead more practical higher level combinations of true terminals. All TokenList and Terminal objects have a 'value' attribute, which produces the semantically meaningful value of that part of the parse subtree. The value of all whitespace tokens (no matter how many sub-tokens they may contain) is a single space, as per the RFC rules. This includes 'CFWS', which is herein included in the general class of whitespace tokens. There is one exception to the rule that whitespace tokens are collapsed into single spaces in values: in the value of a 'bare-quoted-string' (a quoted-string with no leading or trailing whitespace), any whitespace that appeared between the quotation marks is preserved in the returned value. Note that in all Terminal strings quoted pairs are turned into their unquoted values. All TokenList and Terminal objects also have a string value, which attempts to be a "canonical" representation of the RFC-compliant form of the substring that produced the parsed subtree, including minimal use of quoted pair quoting. Whitespace runs are not collapsed. Comment tokens also have a 'content' attribute providing the string found between the parens (including any nested comments) with whitespace preserved. All TokenList and Terminal objects have a 'defects' attribute which is a possibly empty list all of the defects found while creating the token. Defects may appear on any token in the tree, and a composite list of all defects in the subtree is available through the 'all_defects' attribute of any node. (For Terminal notes x.defects == x.all_defects.) Each object in a parse tree is called a 'token', and each has a 'token_type' attribute that gives the name from the RFC 5322 grammar that it represents. Not all RFC 5322 nodes are produced, and there is one non-RFC 5322 node that may be produced: 'ptext'. A 'ptext' is a string of printable ascii characters. It is returned in place of lists of (ctext/quoted-pair) and (qtext/quoted-pair). XXX: provide complete list of token types. N) hexdigits) itemgetter)_encoded_words)errors)utilsz (z ()<>@,:;.\"[].z."(z/?=z*'%%  cpt|ddddS)z;Escape dquote and backslash for use within a quoted-string.\\\"z\")strreplacevalues G/opt/alt/python-internal/lib64/python3.11/email/_header_value_parser.pymake_quoted_pairsrcs. u::  dF + + 3 3C ? ??c,t|}d|dS)Nr)r)rescapeds r quote_stringrhs&&G w>>>rz =\? # literal =? [^?]* # charset \? # literal ? [qQbB] # literal 'q' or 'b', case insensitive \? # literal ? .*? # encoded word \?= # literal ?= ceZdZdZdZdZfdZdZfdZe dZ e dZ dZ e d Z e d Zd Zdd ZddZddZxZS) TokenListNTcHtj|i|g|_dSN)super__init__defects)selfargskw __class__s rr zTokenList.__init__s*$%"%%% rc@dd|DS)Nc34K|]}t|VdSrr.0xs r z$TokenList.__str__..(,,!s1vv,,,,,,rjoinr"s r__str__zTokenList.__str__s#ww,,t,,,,,,rcd|jjtSNz{}({})formatr%__name__r__repr__r"r%s rr8zTokenList.__repr__s3t~6"WW--//11 1rc@dd|DS)Nr'c32K|]}|j |jVdSrrr*s rr-z"TokenList.value..s+8818qw888888rr/r1s rrzTokenList.values#ww88888888rc@td|D|jS)Nc3$K|] }|jV dSr) all_defectsr*s rr-z(TokenList.all_defects..s$00aAM000000r)sumr!r1s rr>zTokenList.all_defectss#004000$,???rc6|dSNr)startswith_fwsr1s rrBzTokenList.startswith_fwssAw%%'''rc4td|DS)zATrue if all top level tokens of this part may be RFC2047 encoded.c3$K|] }|jV dSr) as_ew_allowed)r+parts rr-z*TokenList.as_ew_allowed..s%77$4%777777r)allr1s rrEzTokenList.as_ew_alloweds!77$777777rcHg}|D]}||j|Sr)extendcomments)r"rJtokens rrJzTokenList.commentss2 , ,E OOEN + + + +rc$t||S)Npolicy)_refold_parse_treer"rNs rfoldzTokenList.folds!$v6666rr'cLt||dS)Nindent)printppstrr"rTs rpprintzTokenList.pprints$ djjj''(((((rcTd||S)Nr rS)r0_pprWs rrVzTokenList.ppstrs"yy00111rc#nKd||jj|jV|D]K}t |ds|d|zV-||dzEd{VL|jrd|j}nd}d||VdS)Nz{}{}/{}(rZz* !! invalid element in token list: {!r}z z Defects: {}r'z{}){})r6r%r7 token_typehasattrrZr!)r"rTrKextras rrZz TokenList._pps  N # O    4 4E5%(( 4!55;VE]]CDDDD!99VF]3333333333 < "))$,77EEEnnVU+++++++rr')r7 __module__ __qualname__r\syntactic_breakew_combine_allowedr r2r8propertyrr>rBrErJrQrXrVrZ __classcell__r%s@rrr}s9JO---1111199X9@@X@(((88X8X 777))))2222,,,,,,,,rrc:eZdZedZedZdS)WhiteSpaceTokenListcdSN r1s rrzWhiteSpaceTokenList.valuesrcd|DS)Nc2g|]}|jdk |jS)comment)r\contentr*s r z0WhiteSpaceTokenList.comments..s&CCCa1<+B+B +B+B+Brrlr1s rrJzWhiteSpaceTokenList.commentssCC4CCCCrN)r7r`rardrrJrlrrrhrhsM XDDXDDDrrhceZdZdZdS)UnstructuredTokenList unstructuredNr7r`rar\rlrrrtrtsJJJrrtceZdZdZdS)PhrasephraseNrvrlrrrxrxJJJrrxceZdZdZdS)WordwordNrvrlrrr|r|JJJrr|ceZdZdZdS)CFWSListcfwsNrvrlrrrrr~rrceZdZdZdS)AtomatomNrvrlrrrrr~rrceZdZdZdZdS)TokenrKFN)r7r`rar\ encode_as_ewrlrrrrsJLLLrrceZdZdZdZdZdZdS) EncodedWord encoded-wordN)r7r`rar\ctecharsetlangrlrrrrs"J CG DDDrrcTeZdZdZedZedZedZdS) QuotedString quoted-stringc8|D]}|jdkr |jcSdSNbare-quoted-stringr\rr"r,s rrqzQuotedString.contents9  A|333w4  rcg}|D]J}|jdkr#|t|0||jKd|S)Nrr')r\appendrrr0)r"resr,s r quoted_valuezQuotedString.quoted_valuesf $ $A|333 3q66"""" 17####wws||rc8|D]}|jdkr |jcSdSrrr"rKs rstripped_valuezQuotedString.stripped_values= # #E#777{"""8 # #rN)r7r`rar\rdrqrrrlrrrrsh J X X##X###rrc.eZdZdZdZedZdS)BareQuotedStringrcZtdd|DS)Nr'c34K|]}t|VdSrr)r*s rr-z+BareQuotedString.__str__.. s(#9#9qCFF#9#9#9#9#9#9r)rr0r1s rr2zBareQuotedString.__str__ s+BGG#9#9D#9#9#999:::rc@dd|DS)Nr'c34K|]}t|VdSrr)r*s rr-z)BareQuotedString.value..r.rr/r1s rrzBareQuotedString.value #ww,,t,,,,,,rN)r7r`rar\r2rdrrlrrrrsC%J;;;--X---rrcJeZdZdZdZdZedZedZdS)CommentrpcldtdgfdDdgggS)Nr'rc:g|]}|Srl)quote)r+r,r"s rrrz#Comment.__str__..s#999qTZZ]]999r))r0r?r1s`rr2zComment.__str__sPwws E9999D999 E " ##$$ $rc|jdkrt|St|ddddddS)Nrprrrz\(rz\))r\rr)r"rs rrz Comment.quotesc  y ( (u:: 5zz!!$//77"%u...5g"%u/./. .rc@dd|DS)Nr'c34K|]}t|VdSrr)r*s rr-z"Comment.content..%r.rr/r1s rrqzComment.content#rrc|jgSr)rqr1s rrJzComment.comments's  ~rN) r7r`rar\r2rrdrqrJrlrrrrsmJ$$$...--X-XrrcTeZdZdZedZedZedZdS) AddressListz address-listcd|DS)Nc(g|]}|jdk |S)addressr\r*s rrrz)AddressList.addresses..1$;;;a1<#:#:#:#:#:rrlr1s r addresseszAddressList.addresses/;;4;;;;rc6td|DgS)Nc3:K|]}|jdk |jVdSrNr\ mailboxesr*s rr-z(AddressList.mailboxes..5s<>>Q\9%<%<K%<%<%<%<>>rr?r1s rrzAddressList.mailboxes36>>!>>>?ACC Crc6td|DgS)Nc3:K|]}|jdk |jVdSrr\ all_mailboxesr*s rr-z,AddressList.all_mailboxes..:s<>>Q\9%<%<O%<%<%<%<>>rrr1s rrzAddressList.all_mailboxes8rrN)r7r`rar\rdrrrrlrrrr+spJ <<X<CCXCCCXCCCrrcTeZdZdZedZedZedZdS)AddressrcB|djdkr |djSdS)Nrgroupr\ display_namer1s rrzAddress.display_nameBs) 7  ( (7' ' ) (rcv|djdkr |dgS|djdkrgS|djSNrmailboxinvalid-mailboxrr1s rrzAddress.mailboxesGsE 7  * *G9  !W #4 4 4IAw  rc|djdkr |dgS|djdkr |dgS|djSrrr1s rrzAddress.all_mailboxesOsL 7  * *G9  !W #4 4 4G9 Aw$$rN)r7r`rar\rdrrrrlrrrr>shJ ((X(!!X!%%X%%%rrc>eZdZdZedZedZdS) MailboxList mailbox-listcd|DS)Nc(g|]}|jdk |S)rrr*s rrrz)MailboxList.mailboxes..]rrrlr1s rrzMailboxList.mailboxes[rrcd|DS)Nc$g|] }|jdv |S))rrrr*s rrrz-MailboxList.all_mailboxes..as/???a|======rrlr1s rrzMailboxList.all_mailboxes_s"??4??? ?rNr7r`rar\rdrrrlrrrrWsMJ <<X<??X???rrc>eZdZdZedZedZdS) GroupList group-listcF|r|djdkrgS|djSNrrrr1s rrzGroupList.mailboxesis- tAw)^;;IAw  rcF|r|djdkrgS|djSrrr1s rrzGroupList.all_mailboxesos- tAw)^;;IAw$$rNrrlrrrresMJ !!X! %%X%%%rrcTeZdZdZedZedZedZdS)GrouprcB|djdkrgS|djSNrrr1s rrzGroup.mailboxeszs& 7  - -IAw  rcB|djdkrgS|djSrrr1s rrzGroup.all_mailboxess& 7  - -IAw$$rc|djSrA)rr1s rrzGroup.display_namesAw##rN)r7r`rar\rdrrrrlrrrrvshJ !!X! %%X% $$X$$$rrceZdZdZedZedZedZedZedZ dS)NameAddr name-addrcFt|dkrdS|djSNr)lenrr1s rrzNameAddr.display_names# t99>>4Aw##rc|djSN local_partr1s rrzNameAddr.local_partsBx""rc|djSrdomainr1s rrzNameAddr.domainsBxrc|djSr)router1s rrzNameAddr.routesBx~rc|djSr addr_specr1s rrzNameAddr.addr_specsBx!!rN r7r`rar\rdrrrrrrlrrrrsJ $$X$ ##X#XX""X"""rrcjeZdZdZedZedZedZedZdS) AngleAddrz angle-addrc8|D]}|jdkr |jcSdSN addr-spec)r\rrs rrzAngleAddr.local_parts; $ $A|{**|###+ $ $rc8|D]}|jdkr |jcSdSrr\rrs rrzAngleAddr.domains8  A|{**x+  rc8|D]}|jdkr |jcSdS)N obs-route)r\domainsrs rrzAngleAddr.routes; ! !A|{**y   + ! !rc|D];}|jdkr.|jr |jcSt|j|jzcS)r\rrrrs rrzAngleAddr.addr_specs`  A|{**<D;&&&' 55 CCCC + 4rN) r7r`rar\rdrrrrrlrrrrsJ $$X$   X !!X! Xrrc(eZdZdZedZdS)ObsRoutercd|DS)Nc2g|]}|jdk |jSrrr*s rrrz$ObsRoute.domains..s&CCCQ!,(*B*B*B*B*Brrlr1s rrzObsRoute.domainssCC$CCCCrN)r7r`rar\rdrrlrrrrs7J DDXDDDrrceZdZdZedZedZedZedZedZ dS)MailboxrcB|djdkr |djSdSNrrrr1s rrzMailbox.display_names) 7  , ,7' ' - ,rc|djSrArr1s rrzMailbox.local_partAw!!rc|djSrArr1s rrzMailbox.domainsAw~rcB|djdkr |djSdSr )r\rr1s rrz Mailbox.routes( 7  , ,7=  - ,rc|djSrArr1s rrzMailbox.addr_specsAw  rNrrlrrrrsJ ((X(""X"X!!X!!!X!!!rrc8eZdZdZedZexZxZxZZ dS)InvalidMailboxrcdSrrlr1s rrzInvalidMailbox.display_nametrNrrlrrrrsB"J X/;:J::%)))rrc6eZdZdZdZefdZxZS)DomainrFctdtjSNr'r0rrsplitr9s rrz Domain.domain'wwuww}**,,---r)r7r`rar\rErdrrerfs@rrrsLJM ....X.....rrceZdZdZdS)DotAtomdot-atomNrvrlrrrrsJJJrrceZdZdZdZdS) DotAtomTextz dot-atom-textTNr7r`rar\rErlrrrrs JMMMrrceZdZdZdZdS) NoFoldLiteralzno-fold-literalFNrrlrrr!r! s"JMMMrr!cneZdZdZdZedZedZedZedZ dS)AddrSpecrFc|djSrArr1s rrzAddrSpec.local_partr rcFt|dkrdS|djS)Nr)rrr1s rrzAddrSpec.domains" t99q==4Bxrct|dkr |djS|dj|djz|djzS)Nr&rrr)rrrstriplstripr1s rrzAddrSpec.valuesW t99q==7= Aw}##%%d1gm3DGM4H4H4J4JJJrct|j}t|t|tz krt |j}n|j}|j |dz|jzS|S)N@)setrr DOT_ATOM_ENDSrr)r"namesetlps rrzAddrSpec.addr_spec$sedo&& w<<#gm344 4 4do..BBB ; "8dk) ) rN) r7r`rar\rErdrrrrrlrrr#r#sJM ""X"X KKXK Xrr#ceZdZdZdZdS) ObsLocalPartzobs-local-partFNrrlrrr1r10s!JMMMrr1cLeZdZdZdZedZefdZxZS) DisplayNamez display-nameFct|}t|dkr|jS|djdkr|dn7|ddjdkr t|ddd|d<|djdkr|n7|ddjdkr t|ddd|d<|jS)Nrrrr)rrrr\pop)r"rs rrzDisplayName.display_name;soo s88q==9  q6  & & GGAJJJJ1vay#v--"3q6!"":..A r7  ' ' GGIIII2wr{%//#CGCRCL11Byrctd}|jrd}n|D]}|jdkrd}t|dkrt|rrdx}}|djdks|ddjdkrd}|djdks|ddjdkrd}|t|jz|zSt jS) NFTrrr'rrkr)r!r\rrrrr)r"rr,prepostr%s rrzDisplayName.valueLs < !EE ! !>e>OC$Aw!6))T!WQZ-BF-J-JBx"F**d2hrl.Ev.M.M|D$5666t; ;77= r) r7r`rar\rcrdrrrerfs@rr3r36shJ X !!!!X!!!!!rr3cBeZdZdZdZedZedZdS) LocalPartz local-partFcX|djdkr |djS|djS)Nrr)r\rrr1s rrzLocalPart.valuees- 7  0 07' '7= rc tg}t}d}|dtgzD]}|jdkr|r6|jdkr+|djdkrt|dd|d<t|t}|rG|jdkr<|djdkr+|t|ddn|||d}|}t|dd}|jS)NFrrdotrr)DOTr\r isinstancerr)r"rlast last_is_tltokis_tls rrzLocalPart.local_partlse 7cU?  C~'' /s~66H'611#D"I..BsI..E $/U22F%// 9SW--.... 3r7DJJAbD ""yrN)r7r`rar\rErdrrrlrrr:r:`sRJM !!X! Xrr:cLeZdZdZdZefdZedZxZS) DomainLiteralzdomain-literalFctdtjSrrr9s rrzDomainLiteral.domainrrc8|D]}|jdkr |jcSdS)Nptextrrs ripzDomainLiteral.ips8  A|w&&w'  r) r7r`rar\rErdrrIrerfs@rrErEsg!JM ....X.XrrEceZdZdZdZdZdS) MIMEVersionz mime-versionN)r7r`rar\majorminorrlrrrKrKsJ E EEErrKcJeZdZdZdZdZdZedZedZ dS) Parameter parameterFus-asciic.|jr |djndSr) sectionednumberr1s rsection_numberzParameter.section_numbers"&6tAw~~Q6rc|D]N}|jdkr |jcS|jdkr-|D]*}|jdkr|D]}|jdkr |jcccS+OdS)Nrrrr')r\rrs r param_valuezParameter.param_values < ''))D6!!!t 4L  !5u = > > > >!<<>>3 3 KD%5jmm444E(1+K!)G' &CJJNN8A;!##!HQK'..v/IH0J0JKKK!"1"IEKA).! *! *%!Q&&!>I ,,V-GI.K.KLLL  ,,V-GF.H.HIIIQ)>RR & = =e D DP$)LL:K$L$LEE +-?@PPP %*LL=N$O$OEEE P !077R!M001N1P1PQQQ.PPP!' 4 4UY 4 O O P ""5))))GGK((E+    g3 3 s$I&"G99'H#"H#&+JJc g}|jD]S\}}|r7|d|t|>||Td|}|rd|zndS)N{}={}z; rkr')r}rr6rr0)r"r}r~rs rr2zMimeParameters.__str__&s; $ $KD% $ gnnT<3F3FGGHHHH d####6""%-sV||2-rN)r7r`rar\rbrdr}r2rlrrrjrjsJ"JO CCXCJ.....rrjc(eZdZdZedZdS)ParameterizedHeaderValueFcRt|D]}|jdkr |jcSiS)Nrk)reversedr\r}rs rr}zParameterizedHeaderValue.params7s>d^^ $ $E#444|###5 rN)r7r`rarbrdr}rlrrrr1s4O XrrceZdZdZdZdZdZdS) ContentTypez content-typeFtextplainN)r7r`rar\rEmaintypesubtyperlrrrr?s"JMHGGGrrceZdZdZdZdZdS)ContentDispositionzcontent-dispositionFN)r7r`rar\rEcontent_dispositionrlrrrrFs &JMrrceZdZdZdZdZdS)ContentTransferEncodingzcontent-transfer-encodingF7bitN)r7r`rar\rErrlrrrrLs,JM CCCrrceZdZdZdZdS) HeaderLabelz header-labelFNrrlrrrrRsJMMMrrceZdZdZdZdZdS)MsgIDzmsg-idFc0t||jzSr)rlineseprPs rrQz MsgID.fold[s4yy6>))rN)r7r`rar\rErQrlrrrrWs-JM*****rrceZdZdZdS) MessageIDz message-idNrvrlrrrr`sJJJrrceZdZdZdS)InvalidMessageIDzinvalid-message-idNrvrlrrrrds%JJJrrceZdZdZdS)HeaderheaderNrvrlrrrrhrzrrc~eZdZdZdZdZfdZfdZdZe dZ d fd Z dZ e d Z d ZxZS) TerminalTcht||}||_g|_|Sr)r__new__r\r!)clsrr\r"r%s rrzTerminal.__new__vs-wwsE**$  rcd|jjtSr4r5r9s rr8zTerminal.__repr__|s,t~68H8H8J8JKKKrcNt|jjdz|jzdS)N/)rUr%r7r\r1s rrXzTerminal.pprints' dn%+do=>>>>>rc*t|jSr)listr!r1s rr>zTerminal.all_defectssDL!!!rr'c d||jj|jt |jsdnd|jgS)Nz {}{}/{}({}){}r'z {})r6r%r7r\rr8r!)r"rTr%s rrZz Terminal._pps_&&  N # O GG    l BBB T\(B(B  rcdSrrlr1s rpop_trailing_wszTerminal.pop_trailing_wsrrcgSrrlr1s rrJzTerminal.commentss rc.t||jfSr)rr\r1s r__getnewargs__zTerminal.__getnewargs__s4yy$/**rr_)r7r`rarErcrbrr8rXrdr>rZrrJrrerfs@rrrpsMO LLLLL???""X"X+++++++rrc*eZdZedZdZdS)WhiteSpaceTerminalcdSrjrlr1s rrzWhiteSpaceTerminal.valuermrcdS)NTrlr1s rrBz!WhiteSpaceTerminal.startswith_fwsstrNr7r`rardrrBrlrrrrs< Xrrc*eZdZedZdZdS) ValueTerminalc|Srrlr1s rrzValueTerminal.values rcdS)NFrlr1s rrBzValueTerminal.startswith_fwssurNrrlrrrrs< Xrrc*eZdZedZdZdS)EWWhiteSpaceTerminalcdSrrlr1s rrzEWWhiteSpaceTerminal.valuesrrcdSrrlr1s rr2zEWWhiteSpaceTerminal.__str__srrN)r7r`rardrr2rlrrrrs< XrrceZdZdZdS)_InvalidEwErrorz1Invalid encoded word found while parsing headers.N)r7r`ra__doc__rlrrrrs;;;;rrr=,zlist-separatorFr+zroute-component-markerz([{}]+)r'z[^{}]+z[\x00-\x20\x7F]ct|}|r,|jtj|t j|r.|jtjddSdS)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)_non_printable_finderr!rrNonPrintableDefectrrzr{)xtextnon_printabless r_validate_xtextrs+511NH V6~FFGGG U##; V: 8:: ; ; ; ; ;;;rclt|d^}}g}d}d}tt|D]D}||dkr |rd}d}nd}|rd}n |||vrn!|||E|dz}d|d||dg|z|fS)akScan printables/quoted-pairs until endchars and return unquoted ptext. This function turns a run of qcontent, ccontent-without-comments, or dtext-with-quoted-printables into a single string by unquoting any quoted printables. It returns the string, the remaining value, and a flag that is True iff there were any quoted printables decoded. rFrTr'N) _wsp_splitterrangerrr0)rendcharsfragment remaindervcharsescapehad_qpposs r_get_ptext_to_endcharsrs)22Hy F F FS]]## C=D    FF c]h & & E hsm$$$$Ag 776??BGGXcdd^$4y$@AA6 IIrc|}t|dt|t|z d}||fS)zFWS = 1*WSP This isn't the RFC definition. We're using fws to represent tokens where folding can be done, but when we are parsing the *un*folding has already been done so we don't need to watch out for CRLF. Nfws)r)rr)rnewvaluers rget_fwsrsF||~~H U# >@@ @ABBioodA..OC) eABBi% 0 7 7 > >@@ @ WWY  F F aq Yq Y #!<<a00yDj4 399;;! &4 ,.. / / / BF GGI  E@'*z$*t2C'D'D$gtWW  !@@@ / 6 6rv > >@@ @@BJBGJg  " 7c>>!$--KE4 IIe    )$22 e]33 %wwy!!  " ?q$$ &4 <>> ? ? ? u9s F--9G&ct}|r|dtvr(t|\}}||:d}|dr t |d\}}d}t |dkr?|djdkr.|jtj dd}|r=t |d kr*|d jd krt|dd|d<||#t$rd}Yntj $rYnwxYwt|d ^}}|r1t|r|d^}}t%|d}t'|||d |}||S) aOunstructured = (*([FWS] vchar) *WSP) / obs-unstruct obs-unstruct = *((*LF *CR *(obs-utext) *LF *CR)) / FWS) obs-utext = %d0 / obs-NO-WS-CTL / LF / CR obs-NO-WS-CTL is control characters except WSP/CR/LF. So, basically, we have printable runs, plus control characters or nulls in the obsolete syntax, separated by whitespace. Since RFC 2047 uses the obsolete syntax in its specification, but requires whitespace on either side of the encoded words, I can see no reason to need to separate the non-printable-non-whitespace from the printable runs if they occur, so we parse this into xtext tokens separated by WSP tokens. Because an 'unstructured' value must by definition constitute the entire value, this 'get' routine does not return a remaining value, only the parsed TokenList. rTrutextrrz&missing whitespace before encoded wordFrrr')rtrrrrrrr\r!rrsrrrrrfc2047_matchersearch partitionrrr0)rrurKvalid_ewhave_wsrBrrs rget_unstructuredrKs$.)**L (# 8s??"5>>LE5    & & &    D ! !  /w?? u|$$q((#B'2e;;$,33F4ND5F5FGGG"'5s<00144#B'2nDD+?(,e,5,5 R(##E***%# ! ! ! *    "(q11i  4..s33 4#ood33OC)c7++E""" ""Q (#R s!D D=,D=<D=cpt|d\}}}t|d}t|||fS)actext = This is not the RFC ctext, since we are handling nested comments in comment and unquoting quoted-pairs here. We allow anything except the '()' characters, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Since quoted pairs are converted to their unquoted values, what is returned is a 'ptext' token. In this case it is a WhiteSpaceTerminal, so it's value is ' '. z()rH)rrrrrH_s r get_qp_ctextrs@-UD99OE5! ug . .EE %<rcpt|d\}}}t|d}t|||fS)aoqcontent = qtext / quoted-pair We allow anything except the DQUOTE character, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Any quoted pairs are converted to their unquoted values, so what is returned is a 'ptext' token. In this case it is a ValueTerminal. rrH)rrrrs r get_qcontentrs@-UC88OE5! % ) )EE %<rct|}|s'tjd||}|t |d}t |d}t|||fS)zatext = We allow any non-ATOM_ENDS in atext, but add an InvalidATextDefect to the token's defects list if we find non-atext characters. zexpected atext but found '{}'Natext)_non_atom_end_matcherrrr6rrrr)rmrs r get_atextrs e$$A ;% + 2 25 9 9;; ; GGIIE #e**++ E % ) )EE %<rc|ddkr'tjd|t}|dd}|r3|ddkr't |\}}|||r/|ddkr"|dt vrt|\}}n|dddkrd} t|\}}|j tj d d }n'#tj$rt |\}}YnwxYw|rNt|dkr;|d j d kr*|d j dkrt|d d |d <nt |\}}|||r |ddk"|s0|j tj d||fS||ddfS)zbare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE A quoted-string without the leading or trailing white space. Its value is the text between the quote marks, with whitespace preserved and quoted pairs decoded. rrzexpected '"' but found '{}'rNrrFz!encoded word inside quoted stringTrrrrz"end of header inside quoted string)rrr6rrrrrrr!rsrr\r)rbare_quoted_stringrKrs rget_bare_quoted_stringrsE Qx3% * 1 1% 8 8:: :)++ !""IE )qS#E** u!!%((( )E!HOO 8s??"5>>LE55 2A2Y$  H 3/66 u"*11&2L73939:::* 3 3 3+E22 uuu 3 7C 233a77&r*5>>*2.9^KK-A*2..7.7&r*(..LE5!!%(((+ )E!HOO, )"))&*D 0+2+2 3 3 3!5(( uQRRy ((sAD!D)(D)c |r3|ddkr'tjd|t}|dd}|r|ddkrv|dtvrt |\}}n1|ddkrt |\}}nt|\}}|||r |ddkv|s0|j tj d||fS||ddfS)zcomment = "(" *([FWS] ccontent) [FWS] ")" ccontent = ctext / quoted-pair / comment We handle nested comments here, and quoted-pair in our qp-ctext routine. rrzexpected '(' but found '{}'rNrzend of header inside comment) rrr6rrr get_commentrrr!rs)rrprKs rrrs2  9qS% ) 0 0 7 799 9iiG !""IE E!HOO 8s??"5>>LE55 1X__&u--LE55'..LE5u E!HOO v9 * , , - - -~ E!""I rct}|ri|dtvrZ|dtvrt|\}}nt |\}}|||r|dtvZ||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)r CFWS_LEADERrrrr)rrrKs rget_cfwsrs ::D E!H ++ 8s??"5>>LE55&u--LE5 E E!H ++ ;rcTt}|r6|dtvr't|\}}||t |\}}|||r6|dtvr't|\}}||||fS)zquoted-string = [CFWS] [CFWS] 'bare-quoted-string' is an intermediate class defined by this parser and not by the RFC grammar. It is the quoted string without any attached CFWS. r)rrrrr)r quoted_stringrKs rget_quoted_stringrs!NNM $q[(( uU###)%00LE5 $q[(( uU### % rcdt}|r6|dtvr't|\}}|||r6|dtvr't jd||dr; t|\}}n9#t j$rt|\}}YnwxYwt|\}}|||r6|dtvr't|\}}||||fS)zPatom = [CFWS] 1*atext [CFWS] An atom could be an rfc2047 encoded word. rzexpected atom but found '{}'r) rrrr ATOM_ENDSrrr6rrr)rrrKs rget_atomr !sI 66D q[(( u E :qY&&% * 1 1% 8 8:: : ( ,+E22LE55& , , ,%U++LE555 , !'' uKK q[(( u E ;sB((!C  C ct}|r|dtvr'tjd||ry|dtvrjt |\}}|||r0|ddkr$|t|dd}|r|dtvj|dtur*tjdd|z||fS)z( dot-text = 1*atext *("." 1*atext) rz8expected atom at a start of dot-atom-text but found '{}'r rNrz4expected atom at end of dot-atom-text but found '{}')rr rrr6rrr>)r dot_atom_textrKs rget_dot_atom_textr<s MMM :E!H ))%'++16%==:: : E!HI-- '' uU###  U1X__   % % %!""IE E!HI-- RC%'#VCI..00 0 % rct}|dtvr't|\}}|||dr; t |\}}n9#t j$rt|\}}YnwxYwt|\}}|||r6|dtvr't|\}}||||fS)z dot-atom = [CFWS] dot-atom-text [CFWS] Any place we can have a dot atom, we could instead have an rfc2047 encoded word. rr) rrrrrrrrr)rdot_atomrKs r get_dot_atomrOs yyH Qx; u 0 4+E22LE55& 4 4 4-U33LE555 4 )// u OOE q[(( u U?sA..!BBc`|dtvrt|\}}nd}|stjd|ddkrt |\}}nH|dt vr'tjd|t|\}}||g|dd<||fS)aword = atom / quoted-string Either atom or quoted-string may start with CFWS. We have to peel off this CFWS first to determine which type of word to parse. Afterward we splice the leading CFWS, if any, into the parsed sub-token. If neither an atom or a quoted-string is found before the next special, a HeaderParseError is raised. The token returned is either an Atom or a QuotedString, as appropriate. This means the 'word' level of the formal grammar is not represented in the parse tree; this is because having that extra layer when manipulating the parse tree is more confusing than it is helpful. rNz5Expected 'atom' or 'quoted-string' but found nothing.rz1Expected 'atom' or 'quoted-string' but found '{}')rrrrrSPECIALSr6r )rleaderrKs rget_wordrhs  Qx;   E% CEE E Qx}}(// uu qX  %'77=ve}}FF F  u Hbqb %<rct} t|\}}||nA#tj$r/|jtjdYnwxYw|r |dtvr|ddkrQ|t|jtj d|dd}n t|\}}nd#tj$rR|dtvr?t|\}}|jtj dnYnwxYw|||r|dtv||fS)a phrase = 1*word / obs-phrase obs-phrase = word *(word / "." / CFWS) This means a phrase can be a sequence of words, periods, and CFWS in any order as long as it starts with at least one word. If anything other than words is detected, an ObsoleteHeaderDefect is added to the token's defect list. We also accept a phrase that starts with CFWS followed by a dot; this is registered as an InvalidHeaderDefect, since it is not supported by even the obsolete grammar. zphrase does not start with wordrr zperiod in 'phrase'rNzcomment found without atom) rxrrrrr!rs PHRASE_ENDSr>ObsoleteHeaderDefectrr)rryrKs r get_phrasersXXF0 u e  "000f8 -// 0 0 0 0 00 !E!HK// 8S== MM#    N ! !&"=$#&#& ' ' '!""IEE ' uu*   8{**#+E??LE5N))&*E4+6+6777777  MM% ! !E!HK//" 5=s"'8;A65A6)C< >@@ @ #E** uu  "    #E??LE55&   Qx4E!H $;$;KKEEE  Hbqb e '%(D..E!HK$?$? 23z??U3J K K  $(@ @ @   % %f&@N'P'P Q Q Q Q   % %f&A>'@'@ A A A& 1 >(((( >>>!!&"@;#=#= > > > > >> u sHA//C?BC9C C CCCF::6G32G3ct}d}|r|ddks|dtvr|ddkrU|r,|jt jd|t d}|dd}|ddkrb|t|dd |dd}|jt jd d}|r=|d jd kr,|jt jd  t|\}}d}n7#tj $r%|dtvrt|\}}YnwxYw|||r|ddk|dtv|djd ks"|djdkr=|djd kr,|jt jd|d jd ks"|d jdkr=|djd kr,|jt jd|jrd|_||fS)z' obs-local-part = word *("." word) Frrr zinvalid repeated '.'TrNmisplaced-specialz/'\' character outside of quoted-string/ccontentrr=zmissing '.' between wordsrz!Invalid leading '.' in local partrz"Invalid trailing '.' in local partr) r1rr!rrrsr>rr\rrrr)rr last_non_ws_was_dotrKs rrrs"^^N %U1Xt^^uQx{'B'B 8s??" -&--f.H*/,/,---  ! !# & & &"& !""IE  1Xt^^  ! !-a0C#E#E F F F!""IE  " ) )&*DB+D+D E E E"'    .nR0;uDD  " ) )&*D++-+- . . . +#E??LE5"'  & + + +Qx{**#E??LE555 + e$$$7 %U1Xt^^uQx{'B'B8 q$-- 1  (& 0 0 1  (% / /%%f&@ /'1'1 2 2 2r%.. 2  )6 1 1 2  )5 0 0%%f&@ 0'2'2 3 3 3=$<! 5  s?E1FFct|d\}}}t|d}|r,|jt jdt |||fS)a dtext = / obs-dtext obs-dtext = obs-NO-WS-CTL / quoted-pair We allow anything except the excluded characters, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Quoted pairs are converted to their unquoted values, so what is returned is a ptext token, in this case a ValueTerminal. If there were quoted-printables, an ObsoleteHeaderDefect is added to the returned token's defect list. z[]rHz(quoted printable found in domain-literal)rrr!rrrr)rrHrs r get_dtextr&su2%>>E5& % ) )E 9 V8 688 9 9 9E %<rc|rdS|tjd|tdddS)NFz"end of input inside domain-literal]domain-literal-endT)rrrsr)rdomain_literals r_check_for_early_dl_endr+s] u&4,..///--ABBCCC 4rcLt}|dtvr't|\}}|||st jd|ddkr't jd||dd}t||r||fS|tdd|dtvr't|\}}||t|\}}||t||r||fS|dtvr't|\}}||t||r||fS|ddkr't jd ||tdd |dd}|r6|dtvr't|\}}||||fS) zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] rzexpected domain-literal[z6expected '[' at start of domain-literal but found '{}'rNzdomain-literal-startr(z4expected ']' at end of domain-literal but found '{}'r)) rErrrrrr6r+rrrr&)rr*rKs rget_domain_literalr."s6#__N Qx; ue$$$ A%&?@@@ Qx3%'!!'00 0 !""IEun55%u$$--CDDEEE Qx3u~~ ue$$$U##LE5%   un55%u$$ Qx3u~~ ue$$$un55%u$$ Qx3%'!!'00 0--ABBCCC !""IE %q[(( ue$$$ 5  rct}d}|dtvrt|\}}|s'tjd||ddkr5t |\}}||g|dd<||||fS t|\}}n'#tj$rt|\}}YnwxYw|r |ddkrtjd||g|dd<|||r|ddkr|j tj d|dj d kr |d|dd<|rc|ddkrW|tt|d d\}}|||r |ddkW||fS) z] domain = dot-atom / domain-literal / obs-domain obs-domain = atom *("." atom)) Nrzexpected domain but found '{}'r-r+zInvalid Domainr z(domain is not a dot-atom (contains CFWS)rr)rrrrrr6r.rrr r!rr\r>)rrrrKs r get_domainr0Is XXF F Qx;   <% , 3 3E : :<< < Qx3)%00 u  E"1"I eu}'#E** uu  "''' uuu' 8qS%&6777 Hbqb  MM% !qSf9 688 9 9 9 !9 : - -q F111I !aC MM#   #E!""I..LE5 MM%  !aC 5=sB00!CCct}t|\}}|||r |ddkr0|jt jd||fS|t ddt|dd\}}||||fS)z( addr-spec = local-part "@" domain rr+z#addr-spec local part with no domainaddress-at-symbolrN)r#r!rr!rrsrr0)rrrKs r get_addr_specr3os I!%((LE5 U  E!HOO  !; 1"3"3 4 4 4% ]3(;<<===eABBi((LE5 U e rct}|r|ddks|dtvr|dtvr(t|\}}||n0|ddkr$|t|dd}|r|ddku|dtv|r |ddkr't jd||tt|dd\}}|||r|ddkr|t|dd}|sn|dtvr't|\}}|||ddkrI|tt|dd\}}|||r |ddk|st jd|ddkr't jd ||tdd ||ddfS) z obs-route = obs-domain-list ":" obs-domain-list = *(CFWS / ",") "@" domain *("," [CFWS] ["@" domain]) Returns an obs-route token with the appropriate sub-tokens (that is, there is no obs-domain-list in the parse tree). rrrNr+z(expected obs-route domain but found '{}'z%end of header while parsing obs-route:z4expected ':' marking end of obs-route but found '{}'zend-of-obs-route-marker) rrrr ListSeparatorrrr6RouteComponentMarkerr0r)r obs_routerKs r get_obs_router9s I U1Xs]]eAh+&=&= 8{ " "#E??LE5   U # # # # 1X__   ] + + +!""IE U1Xs]]eAh+&=&= FE!HOO% 6 = =e D DFF F )***eABBi((LE5 U  $E!HcMM'''abb    8{ " "#E??LE5   U # # # 8s??   1 2 2 2%eABBi00LE5   U # # #  $E!HcMM O%&MNNN Qx3%(''-ve}}66 6 ]3(ABBCCC eABBi rc&t}|dtvr't|\}}|||r |ddkr't jd||tdd|dd}|ddkr]|tdd|jt j d |dd}||fS t|\}}n#tj$r t|\}}|jt j d n:#tj$r(t jd |wxYw||t|\}}YnwxYw|||r|ddkr |dd}n,|jt j d |tdd|r6|dtvr't|\}}||||fS) z angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS] rzangle-addr-endznull addr-spec in angle-addrz*obsolete route specification in angle-addrz.expected addr-spec or obs-route but found '{}'z"missing trailing '>' on angle-addr) rrrrrrr6rr!rsr3r9r)r angle_addrrKs rget_angle_addrr>s J Qx; u%   @E!HOO% 0 7 7 > >@@ @mC);<<=== !""IE Qx3--=>>???!!&"< *#,#, - - -abb 5   ,$U++ uu  " , , , P(//LE5   % %f&A<'>'> ? ? ? ?& P P P)@GGNNPP P P %   $U++ uuu ,e 3qSabb !!&"< 0#2#2 3 3 3mC)9::;;; !q[(( u%   u s*D$$G4>E32G37F***GGct}t|\}}||dd|jdd|_||fS)z display-name = phrase Because this is simply a name-rule, we don't return a display-name token containing a phrase, but rather a display-name token with the content of the phrase. N)r3rrIr!)rrrKs rget_display_namer@sX==Le$$LE5aaa!!! =+L  rcht}d}|dtvr;t|\}}|s'tjd||ddkr|dt vr'tjd|t|\}}|s'tjd|||g|ddd<d}||t|\}}||g|dd<||||fS)z, name-addr = [display-name] angle-addr Nrz!expected name-addr but found '{}'r;) rrrrrr6rr@rr>)r name_addrrrKs r get_name_addrrCsa I F Qx;   D)3::6BBDD D Qx3 8{ " ")3::5AACC C'.. u C)3::5AACC C  "8E!HRaRLF!%((LE5 Hbqb  U e rct} t|\}}nc#tj$rQ t |\}}n:#tj$r(tjd|wxYwYnwxYwt d|jDrd|_| |||fS)z& mailbox = name-addr / addr-spec zexpected mailbox but found '{}'c3JK|]}t|tjVdSr)r?rrsr*s rr-zget_mailbox..sC 3 3 a3 4 4 3 3 3 3 3 3rr) rrCrrr3r6anyr>r\r)rrrKs r get_mailboxrGs  iiGA$U++ uu  "AAA A(//LE55& A A A)188??AA A A5A   3 3 % 1 3 3 333/. NN5 E>&#BAB7A==BBc*t}|r|d|vrv|dtvr4|t|dd|dd}n't |\}}|||r |d|vv||fS)z Read everything up to one of the chars in endchars. This is outside the formal grammar. The InvalidMailbox TokenList that is returned acts like a Mailbox, but the data attributes are None. rr#rN)rrrrr)rrinvalid_mailboxrKs rget_invalid_mailboxrKs%&&O *E!HH,, 8{ " "  " "=q1D$F$F G G G!""IEE%e,,LE5  " "5 ) ) ) *E!HH,, E !!rct}|rd|ddkrW t|\}}||n}#tj$rjd}|dt vrt |\}}|r |ddvrB|||jtjdnt|d\}}||g|dd<|||jtj dn|ddkr-|jtjdn^t|d\}}||g|dd<|||jtj dYnwxYw|rm|ddvrc|d}d |_ t|d\}}| ||jtj d|r0|ddkr$|t|d d}|r |ddkW||fS) aJ mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list obs-mbox-list = *([CFWS] ",") mailbox *("," [mailbox / CFWS]) For this routine we go outside the formal grammar in order to improve error handling. We recognize the end of the mailbox list only at the end of the value or at a ';' (the group terminator). This is so that we can turn invalid mailboxes into InvalidMailbox tokens and continue parsing any remaining valid mailboxes. We also allow all mailbox entries to be null, and this condition is handled appropriately at a higher level. r;Nz,;zempty element in mailbox-listzinvalid mailbox in mailbox-listrrrr)rrGrrrrrr!rrKrsr\rIr6)r mailbox_listrKrrs rget_mailbox_listrO*s==L (E!HOO 8&u--LE5    & & & && 8 8 8FQx;&& (  <aD 0 0 ''/// (//0K71919::::$7ud#C#CLE5)%+Hbqb  ''... (//0J91;1;<<<<qS$++F,G3-5-56666 35$?? u%!'E"1"I##E***$++F,F5-7-7888/ 82  4U1XT))#2&G!2G .ud;;LE5 NN5 ! ! !  ' '(B1)3)3 4 4 4  U1X__    . . .!""IEQ (E!HOOR  s'A E7GGct}|s0|jtjd||fSd}|r|dt vr~t |\}}|sE|jtjd||||fS|ddkr||||fSt|\}}t|j dkr\|||| ||jtj d||fS||g|dd<||||fS)zg group-list = mailbox-list / CFWS / obs-group-list obs-group-list = 1*([CFWS] ",") [CFWS] zend of header before group-listNrzend of header in group-listrMzgroup-list with empty entries) rr!rrrsrrrOrrrIr)r group_listrrKs rget_group_listrRcs J !!!&"< -#/#/ 0 0 05  F %q[((   %   % %f&@-'/'/ 0 0 0   f % % %u$ $ 8s??   f % % %u$ $#E**LE5 5 ""     f % % %%   !!&"= +#-#- . . .5   Hbqb e u rct}t|\}}|r |ddkr'tjd||||t dd|dd}|r;|ddkr/|t dd||ddfSt|\}}|||s-|jtj d n3|ddkr'tjd ||t dd|dd}|r6|dtvr't|\}}||||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rr5z8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorrNrMzgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) rr@rrr6rrrRr!rsrr)rrrKs r get_grouprTs GGE#E**LE5 9E!HOO%'**0&--99 9 LL LLs$CDDEEE !""IE  qS ]3(:;;<<<eABBi!%((LE5 LL G V7 $&& ' ' ' ' qS% 7 > >u E EGG G LLs$677888 !""IE q[(( u U %<rc>t} t|\}}nc#tj$rQ t |\}}n:#tj$r(tjd|wxYwYnwxYw||||fS)a address = mailbox / group Note that counter-intuitively, an address can be either a single address or a list of addresses (a group). This is why the returned Address object has a 'mailboxes' attribute which treats a single address as a list of length one. When you need to differentiate between to two cases, extract the single element, which is either a mailbox or a group token. zexpected address but found '{}')rrTrrrGr6r)rrrKs r get_addressrVs"iiGA '' uu  "AAA A&u--LE55& A A A)188??AA A A5A  NN5 E>rHct}|rm t|\}}||n#tj$r}d}|dt vrt |\}}|r |ddkrC|||jtjdnt|d\}}||g|dd<|t|g|jtj dn|ddkr-|jtjdnlt|d\}}||g|dd<|t|g|jtj dYd}~nd}~wwxYw|ru|ddkri|dd}d|_ t|d\}}| ||jtj d|r$|t|d d}|m||fS) a address_list = (address *("," address)) / obs-addr-list obs-addr-list = *([CFWS] ",") address *("," [address / CFWS]) We depart from the formal grammar here by continuing to parse until the end of the input, assuming the input to be entirely composed of an address-list. This is always true in email parsing, and allows us to skip invalid addresses to parse additional valid ones. Nrrz"address-list entry with no contentzinvalid address in address-listzempty element in address-listrrr)rrVrrrrrr!rrKrrsr\rIr6)r address_listrKerrrrs rget_address_listrZs==L ( 8&u--LE5    & & & && 8 8 8FQx;&& (  <aC ''/// (//0K<1>1>????$7uc#B#BLE5)%+Hbqb  ''(8(8999 (//0J91;1;<<<<qS$++F,G3-5-56666 35#>> u%!'E"1"I##GUG$4$4555$++F,F5-7-7888/ 82  4U1X__#2&q)G!2G .uc::LE5 NN5 ! ! !  ' '(B1)3)3 4 4 4      . . .!""IEQ (R  s'<G FGGcFt}|s'tjd||ddkr'tjd||t dd|dd}t |\}}|||r |ddkr'tjd ||t dd ||ddfS) z& no-fold-literal = "[" *dtext "]" z'expected no-fold-literal but found '{}'rr-z;expected '[' at the start of no-fold-literal but found '{}'zno-fold-literal-startrNr(z9expected ']' at the end of no-fold-literal but found '{}'zno-fold-literal-end)r!rrr6rrr&)rno_fold_literalrKs rget_no_fold_literalr]s1$ooO E% 5 < " [CFWS] id-left = dot-atom-text / obs-id-left id-right = dot-atom-text / no-fold-literal / obs-id-right no-fold-literal = "[" *dtext "]" rr;zexpected msg-id but found '{}'z msg-id-startrNzobsolete id-left in msg-idz4expected dot-atom-text or obs-id-left but found '{}'r+zmsg-id with no id-rightr<z msg-id-endr2zobsolete id-right in msg-idzFexpected dot-atom-text, no-fold-literal or obs-id-right but found '{}'zmissing trailing '>' on msg-id)rrrrrrr6rrrr!rrsr]r0)rmsg_idrKes r get_msg_idras  WWF q[(( u e D  E  7EE E HK+H>=K>K>J  K  7KK K KKKct} t|\}}|||r?|jt jd|ny#tj$rg}t|}t|}|jt jd|Yd}~nd}~wwxYw|S)z2message-id = "Message-ID:" msg-id CRLF zUnexpected {!r}zInvalid msg-id: {!r}N) rrarr!rrsr6rrr)r message_idrKexs rparse_message_idreUs J 2!%(( u%     2   % %f&@!((//'1'1 2 2 2  "KKK ''%e,, !!  &'='D'DR'H'H I I K K K K K K K KK s'A99C/AC**C/ct}|s.|jtjd|S|dt vrUt |\}}|||s,|jtjdd}|rM|ddkrA|dt vr2||dz }|dd}|r|ddkr|dt v2|sc|jtjd ||t|d n7t||_ |t|d |r6|dt vr't |\}}|||r |ddkrZ|j ,|jtjd |r#|t|d |S|tdd |dd}|r6|dt vr't |\}}|||s5|j ,|jtjd |Sd}|r5|dt vr&||dz }|dd}|r|dt v&|sc|jtjd ||t|d n7t||_ |t|d |r6|dt vr't |\}}|||rO|jtjd|t|d |S)zE mime-version = [CFWS] 1*digit [CFWS] "." [CFWS] 1*digit [CFWS] z%Missing MIME version number (eg: 1.0)rz0Expected MIME version number but found only CFWSr'r rNz1Expected MIME major version number but found {!r}rdigitsz0Incomplete MIME version; found only major numberzversion-separatorz1Expected MIME minor version number but found {!r}z'Excess non-CFWS text after MIME version)rKr!rrHeaderMissingRequiredValuerrisdigitrsr6rintrLrM)r mime_versionrKrgs rparse_mime_versionrlqs ==L ##F$E 3%5%5 6 6 6 Qx; uE""" E  ' '(IB)D)D E E E F E!HOOa (C(C%(abb  E!HOOa (C(C >>  =##F$> ? F Fv N N%P%P Q Q QM&'::;;;; [[ M&(;;<<< #q[(( uE""" E!HOO   )  ' '(BB)D)D E E E  ?    eW = = > > > c+>??@@@ !""IE #q[(( uE"""    )  ' '(BB)D)D E E E F E!HK//%(abb  E!HK// >>  =##F$> ? F Fv N N%P%P Q Q QM&'::;;;; [[ M&(;;<<< #q[(( uE""" ;##F$> 5%7%7 8 8 8M%99::: rc2t}|r|ddkrx|dtvr4|t|dd|dd}n't |\}}|||r |ddkx||fS)z Read everything up to the next ';'. This is outside the formal grammar. The InvalidParameter TokenList that is returned acts like a Parameter, but the data attributes are None. rrMr#rN)rZrrrr)rinvalid_parameterrKs rget_invalid_parameterros)** ,E!HOO 8{ " "  $ $]583F&H&H I I I!""IEE%e,,LE5  $ $U + + + ,E!HOO e ##rct|}|s'tjd||}|t |d}t |d}t|||fS)a8ttext = We allow any non-TOKEN_ENDS in ttext, but add defects to the token's defects list if we find non-ttext characters. We also register defects for *any* non-printables even though the RFC doesn't exclude all of them, because we follow the spirit of RFC 5322. zexpected ttext but found '{}'Nttext)_non_token_end_matcherrrr6rrrr)rrrqs r get_ttextrss u%%A ;% + 2 25 9 9;; ; GGIIE #e**++ E % ) )EE %<rct}|r6|dtvr't|\}}|||r6|dtvr't jd|t|\}}|||r6|dtvr't|\}}||||fS)ztoken = [CFWS] 1*ttext [CFWS] The RFC equivalent of ttext is any US-ASCII chars except space, ctls, or tspecials. We also exclude tabs even though the RFC doesn't. The RFC implies the CFWS but is not explicit about it in the BNF. rexpected token but found '{}') rrrr TOKEN_ENDSrrr6rs)rmtokenrKs r get_tokenrxsWWF q[(( u e ;qZ''% + 2 25 9 9;; ;U##LE5 MM% q[(( u e 5=rct|}|s'tjd||}|t |d}t |d}t|||fS)aQattrtext = 1*(any non-ATTRIBUTE_ENDS character) We allow any non-ATTRIBUTE_ENDS in attrtext, but add defects to the token's defects list if we find non-attrtext characters. We also register defects for *any* non-printables even though the RFC doesn't exclude all of them, because we follow the spirit of RFC 5322. z expected attrtext but found {!r}Nr`)_non_attribute_end_matcherrrr6rrrrrrr`s r get_attrtextr|s #5))A >% . 5 5e < <>> >wwyyH #h--.. !EXz22HH U?rct}|r6|dtvr't|\}}|||r6|dtvr't jd|t|\}}|||r6|dtvr't|\}}||||fS)aH [CFWS] 1*attrtext [CFWS] This version of the BNF makes the CFWS explicit, and as usual we use a value terminal for the actual run of characters. The RFC equivalent of attrtext is the token characters, with the subtraction of '*', "'", and '%'. We include tab in the excluded set just as we do for token. rru) r]rrrATTRIBUTE_ENDSrrr6r|rr^rKs r get_attributer s I  q[(( u ;q^++% + 2 25 9 9;; ;&&LE5 U  q[(( u e rct|}|s'tjd||}|t |d}t |d}t|||fS)zattrtext = 1*(any non-ATTRIBUTE_ENDS character plus '%') This is a special parsing routine so that we get a value that includes % escapes as a single string (which we decode as a single string later). z)expected extended attrtext but found {!r}Nextended-attrtext)#_non_extended_attribute_end_matcherrrr6rrrrr{s rget_extended_attrtextr s ,E22A G% 7 > >u E EGG GwwyyH #h--.. !EX':;;HH U?rct}|r6|dtvr't|\}}|||r6|dtvr't jd|t|\}}|||r6|dtvr't|\}}||||fS)z [CFWS] 1*extended_attrtext [CFWS] This is like the non-extended version except we allow % characters, so that we can pick up an encoded value as a single string. rru) r]rrrEXTENDED_ATTRIBUTE_ENDSrrr6rrs rget_extended_attributer- s I  q[(( u ;q444% + 2 25 9 9;; ;(//LE5 U  q[(( u e rct}|r |ddkr'tjd||t dd|dd}|r|ds'tjd|d}|rK|dr1||dz }|dd}|r|d1|dd kr2|d kr,|jtjd t||_ |t |d ||fS) a6 '*' digits The formal BNF is more complicated because leading 0s are not allowed. We check for that and add a defect. We also assume no CFWS is allowed between the '*' and the digits, though the RFC is not crystal clear on that. The caller should already have dealt with leading CFWS. r*zExpected section but found {}zsection-markerrNz$Expected section number but found {}r'0z'section number has an invalid leading 0rg) rdrrr6rrrir!rsrjrT)rrergs r get_sectionrC siiG 0E!HOO%&E&L&L(-'/'/00 0 NN=&677888 !""IE @a((**@%'117@@ @ F E!H$$&&%(abb  E!H$$&&ayCFcMMv99 ; ; < < <[[GN NN=22333 E>rct}|stjdd}|dtvrt |\}}|s'tjd||ddkrt |\}}nt|\}}||g|dd<||||fS)z quoted-string / attribute z&Expected value but found end of stringNrz Expected value but found only {}r) rgrrrrr6rrr)rvrrKs r get_valuera s A P%&NOOO F Qx;   @%'006v@@ @ Qx3(// uu-e44 u Hbqb HHUOOO e8OrcR t}t|\}}|||r |ddkrC|jt jd|||fS|ddkr t|\}}d|_||n#tj $rYnwxYw|st j d|ddkr4|tdd|dd }d|_ |dd krt j d |td d |dd }d }|r6|dtvr't|\}}||d }|}|j r|r|dd krt|\}}|j}d}|jdkr4|r|ddkrd}nBt#|\}} | r| ddkrd}n t%|\}} | sd}n#YnxYw|r_|jt jd|||D]} | jdkr g| d d <| }n|}n.d }|jt jd|r|ddkrd }nt)|\}}|j r |jdkrW|r |ddkr||||}||fS|jt jd|sI|jt jd|||||fSnC|>|D]} | jdkrn| jdk|| | j|_|ddkr't j d||tdd|dd }|rt|ddkrht#|\}}|||j|_|r |ddkr't j d||tdd|dd }|t1} |rr|dt2vrt5|\}}n9|dd krtd d}|dd }nt7|\}}| ||r| }nt)|\}}||||}||fS)aY attribute [section] ["*"] [CFWS] "=" value The CFWS is implied by the RFC but not made explicit in the BNF. This simplified form of the BNF from the RFC is made to conform with the RFC BNF through some extra checks. We do it this way because it makes both error recovery and working with the resulting parse tree easier. rrMz)Parameter contains name ({}) but no valuerTzIncomplete parameterzextended-parameter-markerrN=zParameter not followed by '='parameter-separatorrF'z5Quoted string value for extended parameter is invalidrzZParameter marked as extended but appears to have a quoted string value that is non-encodedzcApparent initial-extended-value but attribute was not marked as extended or was not initial sectionz(Missing required charset/lang delimitersrr`z=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {}DQUOTE)rOrrr!rrsr6rrSrrrXrrrrrUr|rr\rrrrrgrrr) rrrKrrappendtoqstring inner_value semi_validrtrs r get_parameterrw s KKE ''LE5 LL E!HOO V79%%+VE]]44 5 5 5e| Qx3 &u--LE5"EO LL    &    D  B)*@AA A 8s?? LLs,GHH I I I!""IE!EN Qx3%&EFFF LLs$9::;;; !""IE F q[(( u UIH ~$<%$ FU1A55 aC OOE " " "$!%<  V7 DEE F F F  V7 688 9 9 9  %<      <#666E7 LJ & & OOA   GEM 8s??)+FFLfUmmUU U c+>??@@@abb   KU1X__'..LE5 OOE " " "EJ KE!HOO-/<??@@@abb  GG Qx3&u~~ uuqS%c844abb +E22 u HHUOOO  '' u OOE %<s.CCC,IIc"t}|r t|\}}||n#tj$r }d}|dt vrt |\}}|s|||cYd}~S|ddkrD||||jtjdnpt|\}}|r|g|dd<|||jtjd |Yd}~nd}~wwxYw|r|ddkru|d}d|_ t|\}}| ||jtjd ||r-|tdd |d d}||S) a! parameter *( ";" parameter ) That BNF is meant to indicate this routine should only be called after finding and handling the leading ';'. There is no corresponding rule in the formal RFC grammar, but it is more convenient for us for the set of parameters to be treated as its own TokenList. This is 'parse' routine because it consumes the remaining value, but it would never be called to parse a full header. Instead it is called to parse everything after the non-parameter value of a specific MIME header. NrrMzparameter entry with no contentzinvalid parameter {!r}rr[z)parameter with invalid trailing text {!r}rr)rjrrrrrrr!rsror6r\rIr)rmime_parametersrKrYrrs rparse_mime_parametersr sg%&&O # =(//LE5  " "5 ) ) ) )& = = =FQx;&& (  '&&v...&&&&&&&Qx3%#**6222'..v/I507078888 5U;; u)!'E"1"I&&u---'..v/I,33E::0<0<===# =&  LU1X__$B'E2E 077LE5 LL     # * *6+E;BB5II,K,K L L L    " "=6K#L#L M M M!""IEG #H s"'<E ;EE CEEc|r|ddkrx|dtvr4|t|dd|dd}n't|\}}|||r |ddkx|sdS|tdd|t |dddS)zBDo our best to find the parameters in an invalid MIME header rrMr#rNr)rrrrr) tokenlistrrKs r_find_mime_parametersr9 s $E!HOO 8{ " "   ]585HII J J J!""IEE%e,,LE5   U # # # $E!HOO  ]3(=>>??? *595566666rct}d}|s.|jtjd|S t |\}}nf#tj$rT|jtjd|t|||cYSwxYw|||r |ddkr@|jtjd|rt|||S|j |_ |tdd|dd } t |\}}nf#tj$rT|jtjd |t|||cYSwxYw|||j |_|s|S|dd krU|jtjd ||` |`t|||S|td d |t!|dd |S)z maintype "/" subtype *( ";" parameter ) The maintype and substype are tokens. Theoretically they could be checked against the official IANA list + x-token, but we don't do that. Fz"Missing content type specificationz(Expected content maintype but found {!r}rrzInvalid content typezcontent-type-separatorrNz'Expected content subtype but found {!r}rMz 022 3 3 3  '' uu  " V7 6 = =e D DFF G G GeU+++   LL E!HOO V7 "$$ % % %  0 !% / / / [&&((..00EN LLs$<==>>> !""IE '' uu  " V7 5 < |dr6|ddtvr!|dd}|ddd|d<|S)Nr'r)r)lineswsps r_steal_trailing_WSP_if_existsr sS C #r#uRy}33Bim"IcrcNb Jrc|jp tj}|jrdnd}dg}d}d}d}d}t dd} t |} | r| d} | | ur|dz}"t| } |s@| jd vrt |  }nt |  } | ||} n5#t$r(td | jDrd } nd} d }YnwxYw| jd krt!| ||||r|s| jsd}d}| jr| |dt)|j }|j|vr_t)||t)|dz kr$t-|}|||dxx|z cc<t1| dst | | z} d}| jr;|| |kr|d ks |dkr| dkrd}t3| |||| j| }| }d}d}d}t)| |t)|dz kr|dxx| z cc<7| jrWt)| dz|krAt-|}|s| r||| zd}t1| dsrt | }| jdkr0t9ddgd|Dzt9ddgz}| js|dz }|| || z} | jr|s| d| d }:t-|}|s| r||| zn|dxx| z cc<| |j||jzS)zLReturn string of contents of parse_tree folded according to RFC rules. utf-8rQr'NrFwrap_as_ew_blockedr)rHrc3JK|]}t|tjVdSr)r?rr{r*s rr-z%_refold_parse_tree.. sC..a!>??......r unknown-8bitTrkrMrrrrrHcHg|]}tt|d S)rH)rr)r+ps rrrz&_refold_parse_tree..C s;(((##4Q#7#7AA(((r)max_line_lengthsysmaxsizeutf8rrr5rr\ SPECIALSNL isdisjointNLSETrryrFr>_fold_mime_parametersrErbrQrrrrr] _fold_as_ewrcrBrinsertr0) parse_treerNmaxlenrorlast_ew last_charsetr want_encodingend_ew_not_allowedrrFtstrr encoded_partnewlinenewpartss rrOrO s  # 2s{F +5ww:H DEGLM!"&:;;   E syy|| % % % ! #  4yy ;"444$.$9$9$$?$? ? %*$4$4T$:$: :  ! KK ! ! !GG! ! ! !.. ,..... "(" MMM ! ?/ / / !$vx @ @ @  ) &!3) &% ! % ' !#'99F9#;#;Q>Q=Q  u % % 66s=C/DDcx|B|r@tt|d|d|z}|dd||d<|dtvr]|d}|dd}t|d|kr"|t ||dxx|z cc<d}|dtvr|d}|dd}|t|dn|}|dkrdn|} t| dz} | dz|krt jd |r|t|dz } | | z } | dkr|d ;|d| } tj | | }t|| z }|dkr8| dd} tj | | }t|| z }|dk8|dxx|z cc<|t| d}|r*|d t|d}||dxx|z cc<|r|ndS) aFold string to_encode into lines as encoded word, combining if allowed. Return the new value for last_ew, or None if ew_combine_allowed is False. If there is already an encoded word in the last line of lines (indicated by a non-None value for last_ew) and ew_combine_allowed is true, decode the existing ew, combine it with to_encode, and re-encode. Otherwise, encode to_encode. In either case, split to_encode as necessary so that the encoded segments fit within maxlen. Nrrrr'rQrz3max_line_length is too small to fit an encoded wordrk)r) rrrrrrrrrr) to_encoderrrrcr leading_wsp trailing_wsp new_last_ew encode_as chrome_lenremaining_space text_spaceto_encode_word encoded_wordexcesss rrrZ s1 U2Ywxx09< = =?? "Ihwh'b |s l abbM b NNf $ $ LL6u== > > > b [ L} } crcN $+O#eBi...K"j00gIY!#JQ6!!% ACC C ) 3uRy>>1$z1 ?? LL    ";J;/z.)DDD \""_4qjj,CRC0N:niHHHL&&8F qjj b \! c.11223  ) LL   eBi..K+ ), "IIIIII, 6;;$6rc |jD]O\}}|dds|dxxdz cc<|}d} ||d}n-#t$r d}t j|rd}d}nd}YnwxYw|r:tj |d | } d ||| } n#d |t|} t|dt| zd z|kr|ddz| z|d<+t| dz|kr| d| z[d} |dz} |rt|tt| zdzt| z} || dzkrd}|| z dz x}} |d|}tj |d | } t| |krn|d z}F| d || | | d } | d z } ||d}|r|dxxdz cc<|QdS)a>Fold TokenList 'part' into the 'lines' list as mime parameters. Using the decoded list of parameters and values, format them according to the RFC rules, including using RFC2231 encoding if the value cannot be expressed in 'encoding' and/or the parameter+value is too long to fit within 'maxlen'. rrMstrictFTrrnrr')saferz {}*={}''{}rrrkrrz''r&NNz {}*{}*={}{})r}r(rarryrrzrtrurr6rrrr)rFrrror~rr error_handlerencoding_required encoded_valuerre extra_chromer splitpointmaxcharspartials rrr s{:!:! e Ry!!**3//  "III III  " LL " " " %  ! " " " $ $U++ "( 1 !  "  ="L..B}/66M&&tWmDDDD>>$ U(;(;<>CII % )F 2 2b C$.E"I  YY]f $ $ LLt $ $ $ ~  !TSW%6%66:S=N=NNJa'' $*Z$7!$; ;J   , & 2 2"]!3!<!< }%%11a   LL..g|]<< = = =L qLG*++&E !b S - !I:!:!sA(('BB)r)rrerrtstringroperatorremailrrrrr,rrrr r-r TSPECIALSrv ASPECIALSr~rrrrrcompileVERBOSE MULTILINErrrrhrtrxr|rrrrrrrrrrrrrrrrrrrrr!r#r1r3r:rErKrOrZr]rdrgrjrrrrrrrrrrrrrrrrr>r6rErbr7r6r0rrrmatchrfindallrrrrzrrrrrrrrrrrrrr rrrrr!rr&r+r.r0r3r9r>r@rCrGrKrOrRrTrVrZr]rarerlrorsrxr|rrrrrrrrrrrrrOrrrlrrrsCCJ ''''''  c%jjCCHHn 3  sN CCHH$ U# E "cc#hh . _ E " S(33s883 t    @@@  "*Z",  @,@,@,@,@,@,@,@,FDDDDD)DDD     I   Y9"9I )#####9###6 - - - - -| - - -!4CCCCC)CCC&%%%%%i%%%2 ? ? ? ? ?) ? ? ?%%%%% %%%"$$$$$I$$$*"""""y"""6 DDDDDDyDDD!!!!!i!!!6;;;;;Y;;;.....Y...i) I yB9 '!'!'!'!'!&'!'!'!T!!!!! !!!H     I    ) 8%%%%%y%%% ##### ###i     I   S.S.S.S.S.YS.S.S.l     y   *1 i ) *****I***&&&&&y&&&Y(+(+(+(+(+s(+(+(+VH-<<<<#I$4$4 BIbggj!!""%$%$%%%*'RZ (8(8 BIbggn%%&&)()())).&0bj1A1A BIbgg-..//2121'2'227$;;;JJJ<   ////bAAAF"    ))))))V2      $6   &2   D$$$L%%%N-!-!-!^(%!%!%!N$$$L ' ' ' R,,,\   <*"""$666r###J<:444n&&&,BBBJ8BBBH$$$$&.&.$,<,LLL\222h777 777r<^B7B7B7H?7?7?7BI!I!I!I!I!r