U e5dÏ¡ã @s¼dZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z e dƒZee d ƒBZe d ƒZeeBZee d ƒZee d ƒZee d ƒBe d ƒZeeBZee dƒBZeeBZee dƒZdd„Ze dejejB¡ZGdd„deƒZGdd„deƒZ Gdd„deƒZ!Gdd„deƒZ"Gdd„deƒZ#Gdd„de ƒZ$Gdd „d eƒZ%Gd!d"„d"eƒZ&Gd#d$„d$eƒZ'Gd%d&„d&eƒZ(Gd'd(„d(e(ƒZ)Gd)d*„d*e ƒZ*Gd+d,„d,eƒZ+Gd-d.„d.eƒZ,Gd/d0„d0eƒZ-Gd1d2„d2eƒZ.Gd3d4„d4eƒZ/Gd5d6„d6eƒZ0Gd7d8„d8eƒZ1Gd9d:„d:eƒZ2Gd;d<„d„d>eƒZ4Gd?d@„d@eƒZ5GdAdB„dBeƒZ6GdCdD„dDeƒZ7GdEdF„dFeƒZ8GdGdH„dHeƒZ9GdIdJ„dJeƒZ:GdKdL„dLe"ƒZ;GdMdN„dNeƒZGdSdT„dTeƒZ?GdUdV„dVe?ƒZ@GdWdX„dXeƒZAGdYdZ„dZeƒZBGd[d\„d\eƒZCGd]d^„d^eƒZDGd_d`„d`eƒZEGdadb„dbeEƒZFGdcdd„ddeEƒZGGdedf„dfeƒZHGdgdh„dheƒZIGdidj„djeƒZJGdkdl„dleJƒZKGdmdn„dneKƒZLGdodp„dpeƒZMGdqdr„dreNƒZOGdsdt„dteOƒZPGdudv„dveOƒZQGdwdx„dxePƒZRGdydz„dze jSƒZTeQd d{ƒZUeQd|d}ƒZVeQd~dƒZWe d€ Xd� Ye¡¡¡jZZ[e d‚ Xe \d� Ye¡¡¡¡j]Z^e dƒ¡j_Z`e d‚ Xe \d� Ye¡¡¡¡j]Zae d‚ Xe \d� Ye¡¡¡¡j]Zbe d‚ Xe \d� Ye¡¡¡¡j]Zcd„d…„Zdd†d‡„Zedˆd‰„ZfdŠd‹„ZgdŒd�„ZhdŽd�„Zid�d‘„Zjd’d“„Zkd”d•„Zld–d—„Zmd˜d™„Zndšd›„Zodœd�„ZpdždŸ„Zqd d¡„Zrd¢d£„Zsd¤d¥„Ztd¦d§„Zud¨d©„Zvdªd«„Zwd¬d­„Zxd®d¯„Zyd°d±„Zzd²d³„Z{d´dµ„Z|d¶d·„Z}d¸d¹„Z~dºd»„Zd¼d½„Z€d¾d¿„Z�dÀdÁ„Z‚dÂdÄZƒdÄdÅ„Z„dÆdÇ„Z…dÈdÉ„Z†dÊdË„Z‡dÌdÍ„ZˆdÎdÏ„Z‰dÐdÑ„ZŠdÒdÓ„Z‹dÔdÕ„ZŒdÖdׄZ�dØdÙ„ZŽdÚdÛ„Z�dÜdÝ„Z�dÞdß„Z‘dàdá„Z’dâdã„Z“dädå„Z”dædç„Z•dèdé„Z–dêdë„Z—dìdí„Z˜dîdï„Z™dðdñ„Zšdòdó„Z›dôdõ„Zœdöd÷„Z�dS)ø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*'%ú%cCs dt|ƒ dd¡ dd¡dS)Nú"ú\ú\\z\")ÚstrÚreplace©Úvalue©rú2/usr/lib64/python3.8/email/_header_value_parser.pyÚ quote_string`srzñ =\? # literal =? [^?]* # charset \? # literal ? [qQbB] # literal 'q' or 'b', case insensitive \? # literal ? .*? # encoded word \?= # literal ?= csšeZdZdZdZdZ‡fdd„Zdd„Z‡fdd„Ze d d „ƒZ e d d „ƒZ d d„Z e dd„ƒZ e dd„ƒZdd„Zddd„Zddd„Zddd„Z‡ZS)Ú TokenListNTcstƒj||Žg|_dS©N)ÚsuperÚ__init__Údefects)ÚselfÚargsÚkw©Ú __class__rrryszTokenList.__init__cCsd dd„|Dƒ¡S)NÚcss|]}t|ƒVqdSr©r ©Ú.0ÚxrrrÚ ~sz$TokenList.__str__..©Újoin©rrrrÚ__str__}szTokenList.__str__csd |jjtƒ ¡¡S©Nz{}({})©ÚformatrÚ__name__rÚ__repr__r&rrrr,€s ÿzTokenList.__repr__cCsd dd„|Dƒ¡S)Nrcss|]}|jr|jVqdSrrr rrrr#†sz"TokenList.value..r$r&rrrr„szTokenList.valuecCstdd„|Dƒ|jƒS)Ncss|] }|jVqdSr)Ú all_defectsr rrrr#Šsz(TokenList.all_defects..)Úsumrr&rrrr-ˆszTokenList.all_defectscCs |d ¡S©Nr)Ústartswith_fwsr&rrrr0ŒszTokenList.startswith_fwscCstdd„|DƒƒS)zATrue if all top level tokens of this part may be RFC2047 encoded.css|] }|jVqdSr)Ú as_ew_allowed)r!Úpartrrrr#’sz*TokenList.as_ew_allowed..)Úallr&rrrr1�szTokenList.as_ew_allowedcCsg}|D]}| |j¡q|Sr)ÚextendÚcomments)rr5Útokenrrrr5”szTokenList.commentscCs t||d�S)N©Úpolicy)Ú_refold_parse_tree©rr8rrrÚfold›szTokenList.foldrcCst|j|d�ƒdS)N©Úindent)ÚprintÚppstr©rr=rrrÚpprintžszTokenList.pprintcCsd |j|d�¡S)NÚ r<)r%Ú_ppr@rrrr?¡szTokenList.ppstrccszd ||jj|j¡V|D]4}t|dƒs:|d |¡Vq| |d¡EdHq|jrdd |j¡}nd}d ||¡VdS)Nz{}{}/{}(rCz* !! invalid element in token list: {!r}z z Defects: {}rz{}){})r*rr+Ú token_typeÚhasattrrCr)rr=r6ZextrarrrrC¤sý ÿ z TokenList._pp)r)r)r)r+Ú __module__Ú __qualname__rDÚsyntactic_breakÚew_combine_allowedrr'r,Úpropertyrr-r0r1r5r;rAr?rCÚ __classcell__rrrrrss&        rc@s$eZdZedd„ƒZedd„ƒZdS)ÚWhiteSpaceTokenListcCsdS©Nú rr&rrrr¸szWhiteSpaceTokenList.valuecCsdd„|DƒS)NcSsg|]}|jdkr|j‘qS)Úcomment)rDÚcontentr rrrÚ ¾s z0WhiteSpaceTokenList.comments..rr&rrrr5¼szWhiteSpaceTokenList.commentsN)r+rFrGrJrr5rrrrrL¶s rLc@seZdZdZdS)ÚUnstructuredTokenListÚ unstructuredN©r+rFrGrDrrrrrRÁsrRc@seZdZdZdS)ÚPhraseÚphraseNrTrrrrrUÅsrUc@seZdZdZdS)ÚWordZwordNrTrrrrrWÈsrWc@seZdZdZdS)ÚCFWSListÚcfwsNrTrrrrrXÌsrXc@seZdZdZdS)ÚAtomÚatomNrTrrrrrZÐsrZc@seZdZdZdZdS)ÚTokenr6FN)r+rFrGrDZ encode_as_ewrrrrr\Ôsr\c@seZdZdZdZdZdZdS)Ú EncodedWordú encoded-wordN)r+rFrGrDÚcteÚcharsetÚlangrrrrr]Ùsr]c@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) Ú QuotedStringú quoted-stringcCs"|D]}|jdkr|jSqdS©Núbare-quoted-string©rDr©rr"rrrrPäs zQuotedString.contentcCs>g}|D]*}|jdkr&| t|ƒ¡q| |j¡qd |¡S)Nrer)rDÚappendr rr%)rÚresr"rrrÚ quoted_valueês  zQuotedString.quoted_valuecCs"|D]}|jdkr|jSqdSrdrf©rr6rrrÚstripped_valueôs zQuotedString.stripped_valueN)r+rFrGrDrJrPrjrlrrrrrbàs  rbc@s$eZdZdZdd„Zedd„ƒZdS)ÚBareQuotedStringrecCstd dd„|Dƒ¡ƒS)Nrcss|]}t|ƒVqdSrrr rrrr#sz+BareQuotedString.__str__..)rr%r&rrrr'ÿszBareQuotedString.__str__cCsd dd„|Dƒ¡S)Nrcss|]}t|ƒVqdSrrr rrrr#sz)BareQuotedString.value..r$r&rrrrszBareQuotedString.valueN)r+rFrGrDr'rJrrrrrrmûsrmc@s8eZdZdZdd„Zdd„Zedd„ƒZedd „ƒZd S) ÚCommentrOcs(d tdg‡fdd„ˆDƒdgggƒ¡S)Nrrcsg|]}ˆ |¡‘qSr)Úquoter r&rrrQsz#Comment.__str__..ú))r%r.r&rr&rr' sýüzComment.__str__cCs2|jdkrt|ƒSt|ƒ dd¡ dd¡ dd¡S)NrOr r rz\(rpz\))rDr r)rrrrrros ÿþz Comment.quotecCsd dd„|Dƒ¡S)Nrcss|]}t|ƒVqdSrrr rrrr#sz"Comment.content..r$r&rrrrPszComment.contentcCs|jgSr)rPr&rrrr5szComment.commentsN) r+rFrGrDr'rorJrPr5rrrrrns rnc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) Ú AddressListz address-listcCsdd„|DƒS)NcSsg|]}|jdkr|‘qS)Úaddress©rDr rrrrQ's z)AddressList.addresses..rr&rrrÚ addresses%szAddressList.addressescCstdd„|DƒgƒS)Ncss|]}|jdkr|jVqdS©rrN©rDÚ mailboxesr rrrr#+s ÿz(AddressList.mailboxes..©r.r&rrrrw)s ÿÿzAddressList.mailboxescCstdd„|DƒgƒS)Ncss|]}|jdkr|jVqdSru©rDÚ all_mailboxesr rrrr#0s ÿz,AddressList.all_mailboxes..rxr&rrrrz.s ÿÿzAddressList.all_mailboxesN)r+rFrGrDrJrtrwrzrrrrrq!s  rqc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) ÚAddressrrcCs|djdkr|djSdS)NrÚgroup©rDÚ display_namer&rrrr~8szAddress.display_namecCs4|djdkr|dgS|djdkr*gS|djS©NrÚmailboxúinvalid-mailboxrvr&rrrrw=s  zAddress.mailboxescCs:|djdkr|dgS|djdkr0|dgS|djSrryr&rrrrzEs   zAddress.all_mailboxesN)r+rFrGrDrJr~rwrzrrrrr{4s  r{c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú MailboxListú mailbox-listcCsdd„|DƒS)NcSsg|]}|jdkr|‘qS)r€rsr rrrrQSs z)MailboxList.mailboxes..rr&rrrrwQszMailboxList.mailboxescCsdd„|DƒS)NcSsg|]}|jdkr|‘qS))r€r�rsr rrrrQWs ÿz-MailboxList.all_mailboxes..rr&rrrrzUszMailboxList.all_mailboxesN©r+rFrGrDrJrwrzrrrrr‚Ms  r‚c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú GroupListú group-listcCs |r|djdkrgS|djS©Nrrƒrvr&rrrrw_szGroupList.mailboxescCs |r|djdkrgS|djSr‡ryr&rrrrzeszGroupList.all_mailboxesNr„rrrrr…[s  r…c@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) ÚGroupr|cCs|djdkrgS|djS©Nér†rvr&rrrrwpszGroup.mailboxescCs|djdkrgS|djSr‰ryr&rrrrzvszGroup.all_mailboxescCs |djSr/)r~r&rrrr~|szGroup.display_nameN)r+rFrGrDrJrwrzr~rrrrrˆls  rˆc@sLeZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ d S) ÚNameAddrú name-addrcCst|ƒdkrdS|djS©Nér)Úlenr~r&rrrr~…s zNameAddr.display_namecCs |djS©Néÿÿÿÿ©Ú local_partr&rrrr“‹szNameAddr.local_partcCs |djSr�©Údomainr&rrrr•�szNameAddr.domaincCs |djSr�)Úrouter&rrrr–“szNameAddr.routecCs |djSr�©Ú addr_specr&rrrr˜—szNameAddr.addr_specN© r+rFrGrDrJr~r“r•r–r˜rrrrr‹�s    r‹c@s@eZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZd S) Ú AngleAddrz angle-addrcCs"|D]}|jdkr|jSqdS©Nú addr-spec)rDr“rgrrrr“ s zAngleAddr.local_partcCs"|D]}|jdkr|jSqdSr›©rDr•rgrrrr•¦s zAngleAddr.domaincCs"|D]}|jdkr|jSqdS)Nú obs-route)rDÚdomainsrgrrrr–¬s zAngleAddr.routecCs<|D]2}|jdkr|jr"|jSt|jƒ|jSqdS)Nrœz<>)rDr“r˜rrgrrrr˜²s   zAngleAddr.addr_specN) r+rFrGrDrJr“r•r–r˜rrrrršœs   ršc@seZdZdZedd„ƒZdS)ÚObsRouteržcCsdd„|DƒS)NcSsg|]}|jdkr|j‘qSr”r�r rrrrQÄs z$ObsRoute.domains..rr&rrrrŸÂszObsRoute.domainsN)r+rFrGrDrJrŸrrrrr ¾sr c@sLeZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ d S) ÚMailboxr€cCs|djdkr|djSdS©NrrŒr}r&rrrr~ËszMailbox.display_namecCs |djSr/r’r&rrrr“ÐszMailbox.local_partcCs |djSr/r”r&rrrr•ÔszMailbox.domaincCs|djdkr|djSdSr¢)rDr–r&rrrr–Øsz Mailbox.routecCs |djSr/r—r&rrrr˜ÝszMailbox.addr_specNr™rrrrr¡Çs    r¡c@s,eZdZdZedd„ƒZeZZZZ dS)ÚInvalidMailboxr�cCsdSrrr&rrrr~æszInvalidMailbox.display_nameNr™rrrrr£âs r£cs(eZdZdZdZe‡fdd„ƒZ‡ZS)ÚDomainr•Fcsd tƒj ¡¡S©Nr©r%rrÚsplitr&rrrr•òsz Domain.domain)r+rFrGrDr1rJr•rKrrrrr¤ísr¤c@seZdZdZdS)ÚDotAtomúdot-atomNrTrrrrr¨÷sr¨c@seZdZdZdZdS)Ú DotAtomTextz dot-atom-textTN©r+rFrGrDr1rrrrrªûsrªc@seZdZdZdZdS)Ú NoFoldLiteralzno-fold-literalFNr«rrrrr¬sr¬c@sDeZdZdZdZedd„ƒZedd„ƒZedd„ƒZed d „ƒZ d S) ÚAddrSpecrœFcCs |djSr/r’r&rrrr“ szAddrSpec.local_partcCst|ƒdkrdS|djS)Nér‘)r�r•r&rrrr•s zAddrSpec.domaincCs<t|ƒdkr|djS|dj ¡|dj|dj ¡S)Nr®rrŽrŠ)r�rÚrstripÚlstripr&rrrrs  zAddrSpec.valuecCsLt|jƒ}t|ƒt|tƒkr*t|jƒ}n|j}|jdk rH|d|jS|S)Nú@)Úsetr“r�Ú DOT_ATOM_ENDSrr•)rZnamesetZlprrrr˜s   zAddrSpec.addr_specN) r+rFrGrDr1rJr“r•rr˜rrrrr­s   r­c@seZdZdZdZdS)Ú ObsLocalPartzobs-local-partFNr«rrrrr´&sr´cs4eZdZdZdZedd„ƒZe‡fdd„ƒZ‡ZS)Ú DisplayNamez display-nameFcCs¦t|ƒ}t|ƒdkr|jS|djdkr4| d¡n*|ddjdkr^t|ddd…ƒ|d<|djdkrv| ¡n*|ddjdkr t|ddd…ƒ|d<|jS)NrrYrŽr‘)rr�rrDÚpop)rrirrrr~1s   zDisplayName.display_namecs¦d}|jrd}n|D]}|jdkrd}qt|ƒdkrš|ršd}}|djdks`|ddjdkrdd}|djdks„|ddjdkrˆd}|t|jƒ|StƒjSdS) NFTrcrrrYrNr‘)rrDr�rr~rr)rror"ZpreZpostrrrrBs   zDisplayName.value) r+rFrGrDrIrJr~rrKrrrrrµ,s  rµc@s,eZdZdZdZedd„ƒZedd„ƒZdS)Ú LocalPartz local-partFcCs&|djdkr|djS|djSdS)Nrrc)rDrjrr&rrrr[s zLocalPart.valuecCsÈtg}t}d}|dtgD]”}|jdkr,q|r\|jdkr\|djdkr\t|dd…ƒ|d<t|tƒ}|rš|jdkrš|djdkrš| t|dd…ƒ¡n | |¡|d}|}qt|dd…ƒ}|jS)NFrrYÚdotr‘rŽ)ÚDOTrDrÚ isinstancerhr)rriZlastZ last_is_tlÚtokZis_tlrrrr“bs(  ÿ  ÿ zLocalPart.local_partN)r+rFrGrDr1rJrr“rrrrr·Vs  r·cs4eZdZdZdZe‡fdd„ƒZedd„ƒZ‡ZS)Ú DomainLiteralzdomain-literalFcsd tƒj ¡¡Sr¥r¦r&rrrr•szDomainLiteral.domaincCs"|D]}|jdkr|jSqdS)NÚptextrfrgrrrÚipƒs zDomainLiteral.ip) r+rFrGrDr1rJr•r¾rKrrrrr¼zs r¼c@seZdZdZdZdZdS)Ú MIMEVersionz mime-versionN)r+rFrGrDÚmajorÚminorrrrrr¿Šsr¿c@s4eZdZdZdZdZdZedd„ƒZedd„ƒZ dS) Ú ParameterÚ parameterFúus-asciicCs|jr|djSdSr�)Ú sectionedÚnumberr&rrrÚsection_number˜szParameter.section_numbercCsf|D]\}|jdkr|jS|jdkr|D]4}|jdkr*|D] }|jdkr<|jSqrr+rDr&rrrrAuszTerminal.pprintcCs t|jƒSr)Úlistrr&rrrr-xszTerminal.all_defectsrc s2d ||jj|jtƒ ¡|js"dn d |j¡¡gS)Nz {}{}/{}({}){}rz {})r*rr+rDrr,rr@rrrrC|sûz Terminal._ppcCsdSrrr&rrrÚpop_trailing_ws…szTerminal.pop_trailing_wscCsgSrrr&rrrr5‰szTerminal.commentscCst|ƒ|jfSr)r rDr&rrrÚ__getnewargs__�szTerminal.__getnewargs__)r)r+rFrGr1rIrHrûr,rArJr-rCrÿr5rrKrrrrrúfs     rúc@s eZdZedd„ƒZdd„ZdS)ÚWhiteSpaceTerminalcCsdSrMrr&rrrr“szWhiteSpaceTerminal.valuecCsdS)NTrr&rrrr0—sz!WhiteSpaceTerminal.startswith_fwsN©r+rFrGrJrr0rrrrr‘s rc@s eZdZedd„ƒZdd„ZdS)Ú ValueTerminalcCs|Srrr&rrrr�szValueTerminal.valuecCsdS)NFrr&rrrr0¡szValueTerminal.startswith_fwsNrrrrrr›s rc@s eZdZedd„ƒZdd„ZdS)ÚEWWhiteSpaceTerminalcCsdSr¥rr&rrrr§szEWWhiteSpaceTerminal.valuecCsdSr¥rr&rrrr'«szEWWhiteSpaceTerminal.__str__N)r+rFrGrJrr'rrrrr¥s rc@seZdZdZdS)Ú_InvalidEwErrorz1Invalid encoded word found while parsing headers.N)r+rFrGÚ__doc__rrrrr¯srr¸ú,úlist-separatorr±zroute-component-markerz([{}]+)rz[^{}]+z[\x00-\x20\x7F]cCs>t|ƒ}|r|j t |¡¡t |¡r:|j t d¡¡dS)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)Ú_non_printable_finderrrhrZNonPrintableDefectrrârã)ÚxtextZnon_printablesrrrÚ_validate_xtextÚs  ÿr cCs t|dƒ^}}g}d}d}tt|ƒƒD]L}||dkrJ|rDd}d}nd}q&|rTd}n|||krdq|| ||¡q&|d}d |¡d ||d…g|¡|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. rŽFr TrN)Ú _wsp_splitterÚranger�rhr%)rÚendcharsZfragmentÚ remainderZvcharsÚescapeÚhad_qpÚposrrrÚ_get_ptext_to_endcharsäs$   rcCs.| ¡}t|dt|ƒt|ƒ…dƒ}||fS)zãFWS = 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. NÚfws)r°rr�)rZnewvaluerrrrÚget_fwssrc CsØtƒ}| d¡s t d |¡¡‚|dd… dd¡^}}||dd…krXt d |¡¡‚d |¡}t|ƒdkr°|dtkr°|dtkr°|  d ¡dkr°| dd¡^}}|d|}t| ¡ƒdkrÒ|j   t  d ¡¡||_ d |¡}zt d|d¡\}}}} Wn*ttfk �r*td  |j ¡ƒ‚YnX||_||_|j  | ¡|�rª|dtk�rrt|ƒ\} }|  | ¡�qDt|dƒ^} }t| d ƒ} t| ƒ|  | ¡d |¡}�qD|�rÐ|dtk�rÐ|j   t  d ¡¡||fS)zE encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" ú=?z"expected encoded word but found {}rŠNz?=rŽrrú?zwhitespace inside encoded wordz!encoded word format invalid: '{}'Úvtextz.missing trailing whitespace after encoded-word)r]Ú startswithrÚHeaderParseErrorr*r§r%r�rÚcountrrhrÜr_Ú_ewràÚ ValueErrorÚKeyErrorrr`rar4ÚWSPrr rr ) rZewr»rZremstrÚrestrír`rarr6ÚcharsrrrrÚget_encoded_wordsd ÿÿ   ÿ þ ý  ÿ  ÿ       ÿr"cCsFtƒ}|�rB|dtkr0t|ƒ\}}| |¡qd}| d¡rìzt|ƒ\}}Wn,tk rfd}Yn†tjk rzYnrXd}t |ƒdkr°|dj dkr°|j  t  d¡¡d}|ràt |ƒdkrà|d j d kràt |ddƒ|d<| |¡qt|dƒ^}}|�rt |¡�r| d¡^}}t|d ƒ}t|ƒ| |¡d  |¡}q|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. rTrFr‘rz&missing whitespace before encoded wordrŽéþÿÿÿr^rr)rRrrrhrr"rrrr�rDrrÜrr Úrfc2047_matcherÚsearchÚ partitionrr r%)rrSr6Úvalid_ewZhave_wsr»rrrrrÚget_unstructured?sJ      ÿÿ    r(cCs*t|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()r½)rrr ©rr½Ú_rrrÚ get_qp_ctext‚s  r+cCs*t|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. r r½)rrr r)rrrÚ get_qcontent“s  r,cCsNt|ƒ}|st d |¡¡‚| ¡}|t|ƒd…}t|dƒ}t|ƒ||fS)z®atext = 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 '{}'NÚatext)Ú_non_atom_end_matcherrrr*r|r�rr )rÚmr-rrrÚ get_atext¢sÿ r0cCsr|ddkrt d |¡¡‚tƒ}|dd…}|rT|ddkrTt|ƒ\}}| |¡|�rB|ddk�rB|dtkr‚t|ƒ\}}n´|dd…dk�r*d}z&t|ƒ\}}|j  t  d ¡¡d }Wn"tjk ràt|ƒ\}}YnX|�r6t |ƒdk�r6|d j d k�r6|d j dk�r6t |d d ƒ|d <n t|ƒ\}}| |¡qT|�sb|j  t  d¡¡||fS||dd…fS)zíbare-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. rr zexpected '"' but found '{}'rŽNrŠrFz!encoded word inside quoted stringTr‘rr#r^z"end of header inside quoted string)rrr*rmr,rhrrr"rrÜr�rDr)rZbare_quoted_stringr6r'rrrÚget_bare_quoted_string²sL ÿ      ÿ ÿÿ    ÿr1cCs¼|r |ddkr t d |¡¡‚tƒ}|dd…}|rŽ|ddkrŽ|dtkr\t|ƒ\}}n&|ddkrvt|ƒ\}}n t|ƒ\}}| |¡q2|s¬|j  t  d¡¡||fS||dd…fS)z¬comment = "(" *([FWS] ccontent) [FWS] ")" ccontent = ctext / quoted-pair / comment We handle nested comments here, and quoted-pair in our qp-ctext routine. rrzexpected '(' but found '{}'rŽNrpzend of header inside comment) rrr*rnrrÚ get_commentr+rhrrÜ)rrOr6rrrr2Ýs&ÿ      ÿr2cCsPtƒ}|rH|dtkrH|dtkr0t|ƒ\}}n t|ƒ\}}| |¡q||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)rXÚ CFWS_LEADERrrr2rh)rrYr6rrrÚget_cfwsös   r4cCsptƒ}|r,|dtkr,t|ƒ\}}| |¡t|ƒ\}}| |¡|rh|dtkrht|ƒ\}}| |¡||fS)zÙquoted-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)rbr3r4rhr1)rZ quoted_stringr6rrrÚget_quoted_strings      r5cCsÐtƒ}|r,|dtkr,t|ƒ\}}| |¡|rL|dtkrLt d |¡¡‚| d¡rŒzt |ƒ\}}Wq˜tjk rˆt |ƒ\}}Yq˜Xn t |ƒ\}}| |¡|rÈ|dtkrÈt|ƒ\}}| |¡||fS)zPatom = [CFWS] 1*atext [CFWS] An atom could be an rfc2047 encoded word. rzexpected atom but found '{}'r) rZr3r4rhÚ ATOM_ENDSrrr*rr"r0)rr[r6rrrÚget_atoms&  ÿ     r7cCsœtƒ}|r|dtkr&t d |¡¡‚|rt|dtkrtt|ƒ\}}| |¡|r&|ddkr&| t¡|dd…}q&|dtkr”t d d|¡¡‚||fS)z( dot-text = 1*atext *("." 1*atext) rz8expected atom at a start of dot-atom-text but found '{}'rrŽNr‘z4expected atom at end of dot-atom-text but found '{}')rªr6rrr*r0rhr¹)rZ dot_atom_textr6rrrÚget_dot_atom_text0s ÿ    ÿr8cCs¬tƒ}|dtkr(t|ƒ\}}| |¡| d¡rhzt|ƒ\}}Wqttjk rdt|ƒ\}}YqtXn t|ƒ\}}| |¡|r¤|dtkr¤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) r¨r3r4rhrr"rrr8)rZdot_atomr6rrrÚ get_dot_atomCs        r9cCsŽ|dtkrt|ƒ\}}nd}|s,t d¡‚|ddkrFt|ƒ\}}n*|dtkrdt d |¡¡‚n t|ƒ\}}|dk r†|g|dd…<||fS)aˆword = 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.r z1Expected 'atom' or 'quoted-string' but found '{}')r3r4rrr5ÚSPECIALSr*r7)rÚleaderr6rrrÚget_word\s" ÿ  ÿ r<cCsútƒ}zt|ƒ\}}| |¡Wn(tjk rH|j t d¡¡YnX|rò|dtkrò|ddkr�| t¡|j t  d¡¡|dd…}qJzt|ƒ\}}WnDtjk rä|dt krÞt |ƒ\}}|j t  d¡¡n‚YnX| |¡qJ||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 wordrrzperiod in 'phrase'rŽNzcomment found without atom) rUr<rhrrrrÜÚ PHRASE_ENDSr¹ÚObsoleteHeaderDefectr3r4)rrVr6rrrÚ get_phrase~s4   ÿ    ÿ   ÿ r?c Csvtƒ}d}|dtkr"t|ƒ\}}|s6t d |¡¡‚zt|ƒ\}}Wn^tjk r¤zt|ƒ\}}Wn6tjk rž|ddkr”|dtkr”‚t ƒ}YnXYnX|dk r¼|g|dd…<|  |¡|�r4|ddksæ|dtk�r4t t |ƒ|ƒ\}}|j dk�r|j  t d¡¡n|j  t d¡¡||d<z|j d¡Wn(tk �rl|j  t d ¡¡YnX||fS) z= local-part = dot-atom / quoted-string / obs-local-part Nrz"expected local-part but found '{}'r úinvalid-obs-local-partzrÚencoderßZNonASCIILocalPartDefect)rr“r;r6Úobs_local_partrrrÚget_local_part¤sJ  ÿ    ÿ ÿ ÿ rEcCsÂtƒ}d}|�r(|ddks*|dtk�r(|ddkrj|rL|j t d¡¡| t¡d}|dd…}q nD|ddkr®| t|dd ƒ¡|dd…}|j t d ¡¡d}q |rÒ|d jd krÒ|j t d ¡¡zt |ƒ\}}d}Wn4tj k �r|dt k�r ‚t |ƒ\}}YnX| |¡q |djd k�sX|djdk�rj|djd k�rj|j t d¡¡|d jd k�sš|d jdk�r¬|djd k�r¬|j t d¡¡|j�rºd|_||fS)z' obs-local-part = word *("." word) Frr rzinvalid repeated '.'TrŽNúmisplaced-specialz/'\' character outside of quoted-string/ccontentr‘r¸zmissing '.' between wordsrYz!Invalid leading '.' in local partr#z"Invalid trailing '.' in local partr@) r´r=rrhrrÜr¹rrDr<rr3r4)rrDZlast_non_ws_was_dotr6rrrrBËsj   ÿ    ÿ  ÿ ÿ   ÿ þ ÿ ÿ þ ÿrBcCs@t|dƒ\}}}t|dƒ}|r0|j t d¡¡t|ƒ||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[]r½z(quoted printable found in domain-literal)rrrrhrr>r )rr½rrrrÚ get_dtextús   ÿrGcCs,|rdS| t d¡¡| tddƒ¡dS)NFz"end of input inside domain-literalú]údomain-literal-endT)rhrrÜr)rÚdomain_literalrrrÚ_check_for_early_dl_endsÿrKcCsjtƒ}|dtkr(t|ƒ\}}| |¡|s6t d¡‚|ddkrRt d |¡¡‚|dd…}t||ƒrp||fS| tddƒ¡|dt kr¢t |ƒ\}}| |¡t |ƒ\}}| |¡t||ƒrÊ||fS|dt krìt |ƒ\}}| |¡t||ƒrþ||fS|ddk�rt d  |¡¡‚| tdd ƒ¡|dd…}|�rb|dtk�rbt|ƒ\}}| |¡||fS) zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] rzexpected domain-literalú[z6expected '[' at start of domain-literal but found '{}'rŽNzdomain-literal-startrHz4expected ']' at end of domain-literal but found '{}'rI) r¼r3r4rhrrr*rKrrrrG)rrJr6rrrÚget_domain_literalsH     ÿ            ÿ   rMcCsrtƒ}d}|dtkr"t|ƒ\}}|s6t d |¡¡‚|ddkrvt|ƒ\}}|dk rd|g|dd…<| |¡||fSzt|ƒ\}}Wn"tjk r¨t |ƒ\}}YnX|rÄ|ddkrÄt d¡‚|dk rÚ|g|dd…<| |¡|�rj|ddk�rj|j  t  d¡¡|dj d k�r*|d|dd…<|�rj|ddk�rj| t ¡t |d d…ƒ\}}| |¡�q*||fS) z] domain = dot-atom / domain-literal / obs-domain obs-domain = atom *("." atom)) Nrzexpected domain but found '{}'rLr±zInvalid Domainrz(domain is not a dot-atom (contains CFWS)r©rŽ)r¤r3r4rrr*rMrhr9r7rr>rDr¹)rr•r;r6rrrÚ get_domain=sD  ÿ      ÿ rNcCs|tƒ}t|ƒ\}}| |¡|r,|ddkrF|j t d¡¡||fS| tddƒ¡t|dd…ƒ\}}| |¡||fS)z( addr-spec = local-part "@" domain rr±z#addr-spec local part with no domainúaddress-at-symbolrŽN)r­rErhrrrÜrrN)rr˜r6rrrÚ get_addr_speccs   ÿ rPcCsŒtƒ}|rj|ddks"|dtkrj|dtkrFt|ƒ\}}| |¡q|ddkr| t¡|dd…}q|rz|ddkrŠt d |¡¡‚| t¡t |dd…ƒ\}}| |¡|�r>|ddk�r>| t¡|dd…}|sä�q>|dtk�rt|ƒ\}}| |¡|ddkr²| t¡t |dd…ƒ\}}| |¡q²|�sNt d¡‚|ddk�rlt d  |¡¡‚| t dd ƒ¡||dd…fS) 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). rrrŽNr±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) r r3r4rhÚ ListSeparatorrrr*ÚRouteComponentMarkerrNr)rZ obs_router6rrrÚ get_obs_routessF     ÿ          ÿrTc Cs²tƒ}|dtkr(t|ƒ\}}| |¡|r8|ddkrHt d |¡¡‚| tddƒ¡|dd…}|ddkr¦| tddƒ¡|j t  d ¡¡|dd…}||fSzt |ƒ\}}Wnztjk �r0z"t |ƒ\}}|j t  d ¡¡Wn(tjk �rt d  |¡¡‚YnX| |¡t |ƒ\}}YnX| |¡|�r^|ddk�r^|dd…}n|j t  d ¡¡| tddƒ¡|�rª|dtk�rªt|ƒ\}}| |¡||fS) z‡ angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS] rúzangle-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) ršr3r4rhrrr*rrrÜrPrTr>)rZ angle_addrr6rrrÚget_angle_addrœsT   ÿ   ÿ   ÿ ÿ    ÿ  rWcCs<tƒ}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)rµr?r4r)rr~r6rrrÚget_display_nameÊs  rXcCsÚtƒ}d}|dtkr6t|ƒ\}}|s6t d |¡¡‚|ddkr¦|dtkr^t d |¡¡‚t|ƒ\}}|s~t d |¡¡‚|dk rœ|g|ddd…<d}| |¡t |ƒ\}}|dk rÈ|g|dd…<| |¡||fS)z, name-addr = [display-name] angle-addr Nrz!expected name-addr but found '{}'rU) r‹r3r4rrr*r=rXrhrW)rZ name_addrr;r6rrrÚ get_name_addrÙs6  ÿ  ÿ ÿ   rYc Cs’tƒ}zt|ƒ\}}WnNtjk rdzt|ƒ\}}Wn&tjk r^t d |¡¡‚YnXYnXtdd„|jDƒƒr€d|_|  |¡||fS)z& mailbox = name-addr / addr-spec zexpected mailbox but found '{}'css|]}t|tjƒVqdSr)rºrrÜr rrrr#sÿzget_mailbox..r�) r¡rYrrrPr*Úanyr-rDrh)rr€r6rrrÚ get_mailbox÷s ÿÿ r[cCsdtƒ}|r\|d|kr\|dtkrD| t|ddƒ¡|dd…}qt|ƒ\}}| |¡q||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. rrFrŽN)r£r=rhrr?)rrZinvalid_mailboxr6rrrÚget_invalid_mailbox s  ÿ  r\cCsºtƒ}|�r²|ddk�r²zt|ƒ\}}| |¡W�ntjk �r<d}|dtkrÖt|ƒ\}}|rv|ddkr”| |¡|j t d¡¡n@t |dƒ\}}|dk r¸|g|dd…<| |¡|j t  d¡¡nb|ddkrö|j t d¡¡nBt |dƒ\}}|dk �r|g|dd…<| |¡|j t  d¡¡YnX|�rŠ|ddk�rŠ|d}d |_ t |dƒ\}}|  |¡|j t  d¡¡|r|ddkr| t ¡|d d…}q||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-listrr‘r�rŽ)r‚r[rhrrr3r4rr>r\rÜrDr4rR)rZ mailbox_listr6r;r€rrrÚget_mailbox_listsX      ÿ  ÿ  ÿ   ÿ   ÿ r^cCstƒ}|s$|j t d¡¡||fSd}|rŠ|dtkrŠt|ƒ\}}|sl|j t d¡¡| |¡||fS|ddkrŠ| |¡||fSt|ƒ\}}t|j ƒdkrÚ|dk r¶| |¡|  |¡|j t  d¡¡||fS|dk rð|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-listr]zgroup-list with empty entries) r…rrhrrÜr3r4r^r�rzr4r>)rZ group_listr;r6rrrÚget_group_listWs> ÿ  ÿ       ÿ r_cCs tƒ}t|ƒ\}}|r"|ddkr2t d |¡¡‚| |¡| tddƒ¡|dd…}|rˆ|ddkrˆ| tddƒ¡||dd…fSt|ƒ\}}| |¡|s¶|j t  d ¡¡n|ddkrÒt d  |¡¡‚| tddƒ¡|dd…}|�r|dt k�rt |ƒ\}}| |¡||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rrQz8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorrŽNr]zgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) rˆrXrrr*rhrr_rrÜr3r4)rr|r6rrrÚ get_group|s8 ÿ     ÿ ÿ   r`c Csxtƒ}zt|ƒ\}}WnNtjk rdzt|ƒ\}}Wn&tjk r^t d |¡¡‚YnXYnX| |¡||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 '{}')r{r`rrr[r*rh)rrrr6rrrÚ get_addressšsÿ rac CsÄtƒ}|�r¼zt|ƒ\}}| |¡W�n tjk �rH}zþd}|dtkrÐt|ƒ\}}|rj|ddkrˆ| |¡|j t d¡¡nFt |dƒ\}}|dk r¬|g|dd…<| t |gƒ¡|j t  d¡¡nh|ddkrð|j t d¡¡nHt |dƒ\}}|dk �r|g|dd…<| t |gƒ¡|j t  d¡¡W5d}~XYnX|�rš|ddk�rš|dd}d|_ t |dƒ\}}|  |¡|j t  d¡¡|r| tdd ƒ¡|d d…}q||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-listr‘r�rrŽ)rqrarhrrr3r4rr>r\r{rÜrDr4r)rZ address_listr6Úerrr;r€rrrÚget_address_list·sX      ÿ ÿ  ÿ  ÿ   ÿrccCs¨tƒ}|st d |¡¡‚|ddkr6t d |¡¡‚| tddƒ¡|dd…}t|ƒ\}}| |¡|rx|ddkrˆt d  |¡¡‚| tdd ƒ¡||dd…fS) z& no-fold-literal = "[" *dtext "]" z'expected no-fold-literal but found '{}'rrLz;expected '[' at the start of no-fold-literal but found '{}'zno-fold-literal-startrŽNrHz9expected ']' at the end of no-fold-literal but found '{}'zno-fold-literal-end)r¬rrr*rhrrG)rZno_fold_literalr6rrrÚget_no_fold_literalîs.ÿ ÿÿ   ÿÿrdcCsŠtƒ}|r,|dtkr,t|ƒ\}}| |¡|r<|ddkrLt d |¡¡‚| tddƒ¡|dd…}zt|ƒ\}}Wn`tjk rØz"t |ƒ\}}|j  t  d¡¡Wn&tjk rÒt d |¡¡‚YnXYnX| |¡|rö|dd k�r@|j  t  d ¡¡|�r8|dd k�r8| td d ƒ¡|dd…}||fS| td d ƒ¡|dd…}zt|ƒ\}}Wnœtjk �rzt |ƒ\}}Wnrtjk �r}zPz"t|ƒ\}}|j  t  d¡¡Wn(tjk �rðt d |¡¡‚YnXW5d}~XYnXYnX| |¡|�r6|dd k�r6|dd…}n|j  t  d¡¡| td d ƒ¡|�r‚|dtk�r‚t|ƒ\}}| |¡||fS)zÏmsg-id = [CFWS] "<" id-left '@' id-right ">" [CFWS] id-left = dot-atom-text / obs-id-left id-right = dot-atom-text / no-fold-literal / obs-id-right no-fold-literal = "[" *dtext "]" rrUzexpected msg-id but found '{}'z msg-id-startrŽNzobsolete id-left in msg-idz4expected dot-atom-text or obs-id-left but found '{}'r±zmsg-id with no id-rightrVz msg-id-endrOzobsolete id-right in msg-idzFexpected dot-atom-text, no-fold-literal or obs-id-right but found '{}'zmissing trailing '>' on msg-id)rôr3r4rhrrr*rr8rBrr>rÜrdrN)rZmsg_idr6ÚerrrÚ get_msg_ids~  ÿ   ÿ ÿÿ  ÿ    ÿ ÿÿ"  ÿ  rfc CsŽtƒ}zt|ƒ\}}| |¡WnLtjk rl}z,t|ƒ}t|ƒ}|j t d  |¡¡¡W5d}~XYnX|rŠ|j t d  |¡¡¡|S)z2message-id = "Message-ID:" msg-id CRLF zInvalid msg-id: {!r}NzUnexpected {!r}) rörfrhrrr(r÷rrÜr*)rZ message_idr6ZexrrrÚparse_message_idIs ÿ ÿrgcCsštƒ}|s |j t d¡¡|S|dtkrXt|ƒ\}}| |¡|sX|j t d¡¡d}|r’|ddkr’|dtkr’||d7}|dd…}q\| ¡sÄ|j t d  |¡¡¡| t |d ƒ¡nt |ƒ|_ | t |d ƒ¡|�r|dtk�rt|ƒ\}}| |¡|�r|ddk�rT|j dk �r:|j t d ¡¡|�rP| t |d ƒ¡|S| t dd ƒ¡|dd…}|�rš|dtk�ršt|ƒ\}}| |¡|�sÂ|j dk �r¾|j t d ¡¡|Sd}|�rö|dtk�rö||d7}|dd…}�qÆ| ¡�s*|j t d   |¡¡¡| t |d ƒ¡nt |ƒ|_ | t |d ƒ¡|�rn|dtk�rnt|ƒ\}}| |¡|�r–|j t d¡¡| 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 CFWSrrrŽNz1Expected MIME major version number but found {!r}r Údigitsz0Incomplete MIME version; found only major numberzversion-separatorz1Expected MIME minor version number but found {!r}z'Excess non-CFWS text after MIME version)r¿rrhrÚHeaderMissingRequiredValuer3r4ÚisdigitrÜr*rÚintrÀrÁ)rZ mime_versionr6rhrrrÚparse_mime_versiones„ ÿ    ÿ  ÿ     ÿ     ÿ   ÿ    ÿrlcCsdtƒ}|r\|ddkr\|dtkrD| t|ddƒ¡|dd…}qt|ƒ\}}| |¡q||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. rr]rFrŽN)rÊr=rhrr?)rZinvalid_parameterr6rrrÚget_invalid_parameter©s  ÿ  rmcCsNt|ƒ}|st d |¡¡‚| ¡}|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 '{}'NÚttext)Ú_non_token_end_matcherrrr*r|r�rr )rr/rnrrrÚ get_ttext»s ÿ rpcCs�tƒ}|r,|dtkr,t|ƒ\}}| |¡|rL|dtkrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|dtkrˆt|ƒ\}}| |¡||fS)zútoken = [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. rúexpected token but found '{}') r\r3r4rhÚ TOKEN_ENDSrrr*rp)rZmtokenr6rrrÚ get_tokenÎs   ÿ    rscCsNt|ƒ}|st d |¡¡‚| ¡}|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_matcherrrr*r|r�rr ©rr/rÎrrrÚ get_attrtextås ÿ rvcCs�tƒ}|r,|dtkr,t|ƒ\}}| |¡|rL|dtkrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|dtkrˆ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. rrq) rÌr3r4rhÚATTRIBUTE_ENDSrrr*rv©rrÍr6rrrÚ get_attributeøs   ÿ    rycCsNt|ƒ}|st d |¡¡‚| ¡}|t|ƒd…}t|dƒ}t|ƒ||fS)zÛattrtext = 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}Núextended-attrtext)Ú#_non_extended_attribute_end_matcherrrr*r|r�rr rurrrÚget_extended_attrtext sÿ r|cCs�tƒ}|r,|dtkr,t|ƒ\}}| |¡|rL|dtkrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|dtkrˆ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. rrq) rÌr3r4rhÚEXTENDED_ATTRIBUTE_ENDSrrr*r|rxrrrÚget_extended_attribute! s  ÿ    r~cCsØtƒ}|r|ddkr&t d |¡¡‚| tddƒ¡|dd…}|rR|d ¡sbt d |¡¡‚d}|r�|d ¡r�||d7}|dd…}qf|dd kr¶|d kr¶|j t d ¡¡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-markerrŽNz$Expected section number but found {}rÚ0z'section number has an invalid leading 0rh) rÑrrr*rhrrjrZInvalidHeaderErrorrkrÆ)rrÒrhrrrÚ get_section7 s, ÿ ÿ  ÿ r�cCs’tƒ}|st d¡‚d}|dtkr0t|ƒ\}}|sDt d |¡¡‚|ddkr^t|ƒ\}}n t|ƒ\}}|dk r€|g|dd…<| |¡||fS)z quoted-string / attribute z&Expected value but found end of stringNrz Expected value but found only {}r ) rÓrrr3r4r*r5r~rh)rÚvr;r6rrrÚ get_valueU s"   ÿ   rƒc Cs�tƒ}t|ƒ\}}| |¡|r,|ddkrL|j t d |¡¡¡||fS|ddkrÌz t|ƒ\}}d|_| |¡Wntj k rŽYnX|sžt  d¡‚|ddkrÌ| t ddƒ¡|dd …}d|_ |dd krât  d ¡‚| t d d ƒ¡|dd …}d }|�r,|dt k�r,t |ƒ\}}| |¡d }|}|j �rF|�rF|dd k�rFt|ƒ\}}|j}d}|jdk�r²|�rŒ|ddk�rŒd}n$t|ƒ\}} | �rÚ| ddk�rÚd}n(zt|ƒ\}} Wn Yn X| �sÚd}|�r0|j t d¡¡| |¡|D](} | jdk�rg| d d …<| }�q*�q|}nd }|j t d¡¡|�r`|ddk�r`d }n t|ƒ\}}|j �r€|jdk�rÆ|�r”|ddk�r´| |¡|d k �r¬|}||fS|j t d¡¡|�sü|j t d¡¡| |¡|d k�rì||fSnð|d k �r@|D]} | jdk�r �q$�q | jdk| | ¡| j|_|ddk�r^t  d |¡¡‚| t ddƒ¡|dd …}|�rÐ|ddk�rÐt|ƒ\}}| |¡|j|_|�rÀ|ddk�rÐt  d |¡¡‚| t ddƒ¡|dd …}|d k �rdtƒ} |�r^|dtk�rt|ƒ\}}n2|dd k�rDt d dƒ}|dd …}n t|ƒ\}}|  |¡�qü| }n t|ƒ\}}| |¡|d k �rˆ|}||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. rr]z)Parameter contains name ({}) but no valuerTzIncomplete parameterzextended-parameter-markerrŽNú=zParameter not followed by '='úparameter-separatorr Fú'z5Quoted string value for extended parameter is invalidrezZParameter 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 delimitersrzrÎz=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {}ZDQUOTE)rÂryrhrrrÜr*r�rÅrrrÉr3r4r5rlrÇrvr|rDrƒrr`rarÓrrr,) rrèr6r;rZappendtoZqstringZ inner_valueZ semi_validr Útr‚rrrÚ get_parameterk sð   ÿ              ÿ     ÿ    ÿ ÿ        ÿ   ÿ       rˆc Csjtƒ}|�rfzt|ƒ\}}| |¡WnÌtjk rò}z¬d}|dtkrVt|ƒ\}}|sp| |¡|WY¢xS|ddkr¢|dk rŽ| |¡|j t d¡¡n@t |ƒ\}}|rÀ|g|dd…<| |¡|j t d  |¡¡¡W5d}~XYnX|�rD|ddk�rD|d}d|_ t |ƒ\}}|  |¡|j t d  |¡¡¡|r| t dd ƒ¡|d d…}q|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. Nrr]zparameter entry with no contentzinvalid parameter {!r}r‘rËz)parameter with invalid trailing text {!r}r…rŽ)rÔrˆrhrrr3r4rrÜrmr*rDr4r)rZmime_parametersr6rbr;rèrrrÚparse_mime_parametersù sJ         ÿ   ÿ   ÿr‰cCsˆ|rV|ddkrV|dtkr>| t|ddƒ¡|dd…}qt|ƒ\}}| |¡q|s^dS| tddƒ¡| t|dd…ƒ¡dS)zBDo our best to find the parameters in an invalid MIME header rr]rFrŽNr…)r=rhrr?r‰)Z tokenlistrr6rrrÚ_find_mime_parameters- s   rŠc Cs¶tƒ}d}|s$|j t d¡¡|Szt|ƒ\}}Wn<tjk rp|j t d |¡¡¡t ||ƒ|YSX| |¡|rŒ|ddkr°|j t d¡¡|r¬t ||ƒ|S|j   ¡  ¡|_ | tddƒ¡|dd …}zt|ƒ\}}Wn>tjk �r*|j t d  |¡¡¡t ||ƒ|YSX| |¡|j   ¡  ¡|_|�sP|S|dd k�rŒ|j t d  |¡¡¡|` |`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}rrýzInvalid content typezcontent-type-separatorrŽNz'Expected content subtype but found {!r}r]zt| ƒ} | jd kr‚t | ƒt @r‚d }z|   |¡|} Wn6t k rÊt d d „| jDƒƒr¾d } nd} d }YnX| jdkræt| |||ƒq>|�r¤|�s¤| j�spd}d}| j�rp| j|d�dt|jƒ …} |j| k�rpt| ƒ|t|dƒk�r^t|ƒ}| |¡|d| 7<q>t| dƒ�sŠt| ƒ| } nt| |||| j| ƒ}d}q>t| ƒ|t|dƒk�rÐ|d| 7<q>| j�rt| ƒd|k�rt|ƒ}|�s|  ¡�r| || ¡d}q>t| dƒ�sNt| ƒ}| j�sD|d7}| |¡|| } q>| j�rn|�sn|  d| ¡d }q>t|ƒ}|�s†|  ¡�r–| || ¡q>|d| 7<q>|j |¡|jS)zLReturn string of contents of parse_tree folded according to RFC rules. úutf-8rÄrNrFÚwrap_as_ew_blockedrŽr½Tcss|]}t|tjƒVqdSr)rºrrãr rrrr#ä sÿz%_refold_parse_tree..ú unknown-8bitrÕr7r‘rC)Zmax_line_lengthÚsysÚmaxsizeÚutf8rúrþr¶r rDr²r:rCrßrZr-Ú_fold_mime_parametersr1rHr;r�rõr�rhrEÚ _fold_as_ewrIr0Úinsertr%)Z parse_treer8Úmaxlenr×r�Úlast_ewr’Z want_encodingZend_ew_not_allowedrær2Útstrr`Z encoded_partÚnewlineZnewpartsrrrr9Ê s˜    ÿ       ÿÿ   r9cCsà|dk r<|rFold 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'. r‘r]ÚstrictFTr“rØr‘r)Zsaferz {}*={}''{}rérŽrNrŠrz''r®éNNz {}*{}*={}{})rär¯rÏrCrßrrârÝrÞror*rr�rhr )r2r�ršr×rårr`Z error_handlerZencoding_requiredZ encoded_valuerœrÒZ extra_chromerŸZ splitpointZmaxcharsÚpartialrrrr—r sn   ÿ  ÿ ÿ r—)žrÚrer”rÝÚstringrÚoperatorrZemailrrrrr²rr3r:r6r³r=Z TSPECIALSrrZ ASPECIALSrwr}rÚcompileÚVERBOSEÚ MULTILINEr$rþrrLrRrUrWrXrZr\r]rbrmrnrqr{r‚r…rˆr‹ršr r¡r£r¤r¨rªr¬r­r´rµr·r¼r¿rÂrÊrÌrÑrÓrÔrêrìrðròrórôrör÷rør rúrrrrrr¹rRrSr*r%r§r rÚmatchr.Úfindallr rortr{r rrr"r(r+r,r0r1r2r4r5r7r8r9r<r?rErBrGrKrMrNrPrTrWrXrYr[r\r^r_r`rarcrdrfrgrlrmrprsrvryr|r~r�rƒrˆr‰rŠrŒr�rŽr�r9r˜r—rrrrÚs.E           øC  "   !*$ V +      ÿ ÿÿÿ  1C+ "&'/'&).9%7ED49/gA