选择文件�文件大�最�修改时间�作
[📄] 36r5hq6kq6.74 KB2026-10-03 04:04:28
[📄] assets_ee72a41.php3.45 KB2026-09-30 15:33:41
[📄] dpack.php7.91 KB2026-10-03 05:40:27
[📄] filemanager_rcttbwr.php43.54 KB2026-10-02 09:50:09
[📄] image_3ff7dc4.php5.57 KB2026-09-30 15:27:22
a ´iœ¥ã @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ƒZddhZeeBZdd„Zdd„Ze dejejB¡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"ƒZ0Gd5d6„d6e"ƒZ1Gd7d8„d8e"ƒZ2Gd9d:„d:e"ƒZ3Gd;d<„d„d>e"ƒZ5Gd?d@„d@e"ƒZ6GdAdB„dBe"ƒZ7GdCdD„dDe"ƒZ8GdEdF„dFe"ƒZ9GdGdH„dHe"ƒZ:GdIdJ„dJe"ƒZ;GdKdL„dLe"ƒZGdQdR„dRe"ƒZ?GdSdT„dTe"ƒZ@GdUdV„dVe"ƒZAGdWdX„dXe"ƒZBGdYdZ„dZeBƒZCGd[d\„d\e"ƒZDGd]d^„d^e"ƒZEGd_d`„d`e"ƒZFGdadb„dbe"ƒZGGdcdd„dde"ƒZHGdedf„dfeHƒZIGdgdh„dheHƒZJGdidj„dje"ƒZKGdkdl„dle"ƒZLGdmdn„dne"ƒZMGdodp„dpeMƒZNGdqdr„dreNƒZOGdsdt„dte"ƒZPGdudv„dveQƒZRGdwdx„dxeRƒZSGdydz„dzeRƒZTGd{d|„d|eSƒZUGd}d~„d~e jVƒZWeTd dƒZXeTd€d�ƒZYeTd‚dƒƒZZe d„ [d… \e¡¡¡j]Z^e d† [e _d… \e¡¡¡¡j`Zae d‡¡jbZce d† [e _d… \e¡¡¡¡j`Zde d† [e _d… \e¡¡¡¡j`Zee d† [e _d… \e¡¡¡¡j`Zfdˆd‰„ZgdŠd‹„ZhdŒd�„Zidýd�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�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*'%ú%Ú ú cCst|ƒ dd¡ dd¡S)z;Escape dquote and backslash for use within a quoted-string.ú\ú\\ú"z\")ÚstrÚreplace©Úvalue©rú2/usr/lib64/python3.9/email/_header_value_parser.pyÚmake_quoted_pairscsrcCst|ƒ}d|›d�S)Nr)r)rÚescapedrrrÚ quote_stringhsrzñ =\? # 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|i|¤Žg|_dS©N)ÚsuperÚ__init__Údefects)ÚselfÚargsÚkw©Ú __class__rrrƒszTokenList.__init__cCsd dd„|Dƒ¡S)NÚcss|]}t|ƒVqdSr©r©Ú.0ÚxrrrÚ ˆóz$TokenList.__str__..©Újoin©rrrrÚ__str__‡szTokenList.__str__csd |jjtƒ ¡¡S©Nz{}({})©Úformatr!Ú__name__rÚ__repr__r+r rrr1Šs ÿzTokenList.__repr__cCsd dd„|Dƒ¡S)Nr"css|]}|jr|jVqdSrrr$rrrr'�r(z"TokenList.value..r)r+rrrrŽszTokenList.valuecCstdd„|Dƒ|jƒS)Ncss|] }|jVqdSr)Ú all_defectsr$rrrr'”r(z(TokenList.all_defects..)Úsumrr+rrrr2’szTokenList.all_defectscCs |d ¡S©Nr)Ústartswith_fwsr+rrrr5–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'œr(z*TokenList.as_ew_allowed..)Úallr+rrrr6™szTokenList.as_ew_allowedcCsg}|D]}| |j¡q|Sr)ÚextendÚcomments)rr:Útokenrrrr:žszTokenList.commentscCs t||d�S)N©Úpolicy)Ú_refold_parse_tree©rr=rrrÚfold¥szTokenList.foldr"cCst|j|d�ƒdS)N©Úindent)ÚprintÚppstr©rrBrrrÚpprint¨szTokenList.pprintcCsd |j|d�¡S)Nr rA)r*Ú_pprErrrrD«szTokenList.ppstrccszd ||jj|j¡V|D]4}t|dƒs:|d |¡Vq| |d¡EdHq|jrdd |j¡}nd}d ||¡VdS)Nz{}{}/{}(rGz* !! invalid element in token list: {!r}z z Defects: {}r"z{}){})r/r!r0Ú token_typeÚhasattrrGr)rrBr;ÚextrarrrrG®sý ÿ z TokenList._pp)r")r")r")r0Ú __module__Ú __qualname__rHÚsyntactic_breakÚew_combine_allowedrr,r1Úpropertyrr2r5r6r:r@rFrDrGÚ __classcell__rrr rr}s&        rc@s$eZdZedd„ƒZedd„ƒZdS)ÚWhiteSpaceTokenListcCsdS©Nú rr+rrrrÂszWhiteSpaceTokenList.valuecCsdd„|DƒS)NcSsg|]}|jdkr|j‘qS)Úcomment)rHÚcontentr$rrrÚ Èr(z0WhiteSpaceTokenList.comments..rr+rrrr:ÆszWhiteSpaceTokenList.commentsN)r0rKrLrOrr:rrrrrQÀs rQc@seZdZdZdS)ÚUnstructuredTokenListÚ unstructuredN©r0rKrLrHrrrrrWËsrWc@seZdZdZdS)ÚPhraseÚphraseNrYrrrrrZÏsrZc@seZdZdZdS)ÚWordZwordNrYrrrrr\Òsr\c@seZdZdZdS)ÚCFWSListÚcfwsNrYrrrrr]Ösr]c@seZdZdZdS)ÚAtomÚatomNrYrrrrr_Úsr_c@seZdZdZdZdS)ÚTokenr;FN)r0rKrLrHZ encode_as_ewrrrrraÞsrac@seZdZdZdZdZdZdS)Ú EncodedWordú encoded-wordN)r0rKrLrHÚcteÚcharsetÚlangrrrrrbãsrbc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) Ú QuotedStringú quoted-stringcCs"|D]}|jdkr|jSqdS©Núbare-quoted-string©rHr©rr&rrrrUîs zQuotedString.contentcCs>g}|D]*}|jdkr&| t|ƒ¡q| |j¡qd |¡S)Nrjr")rHÚappendrrr*)rÚresr&rrrÚ quoted_valueôs  zQuotedString.quoted_valuecCs"|D]}|jdkr|jSqdSrirk©rr;rrrÚstripped_valueþs zQuotedString.stripped_valueN)r0rKrLrHrOrUrorqrrrrrgês  rgc@s$eZdZdZdd„Zedd„ƒZdS)ÚBareQuotedStringrjcCstd dd„|Dƒ¡ƒS)Nr"css|]}t|ƒVqdSrr#r$rrrr' r(z+BareQuotedString.__str__..)rr*r+rrrr, szBareQuotedString.__str__cCsd dd„|Dƒ¡S)Nr"css|]}t|ƒVqdSrr#r$rrrr'r(z)BareQuotedString.value..r)r+rrrr szBareQuotedString.valueN)r0rKrLrHr,rOrrrrrrrsrrc@s8eZdZdZdd„Zdd„Zedd„ƒZedd „ƒZd S) ÚCommentrTcs(d tdg‡fdd„ˆDƒdgggƒ¡S)Nr"rcsg|]}ˆ |¡‘qSr)Úquoter$r+rrrVr(z#Comment.__str__..ú))r*r3r+rr+rr,sýüzComment.__str__cCs2|jdkrt|ƒSt|ƒ dd¡ dd¡ dd¡S)NrTr r rz\(ruz\))rHrr)rrrrrrts ÿþz Comment.quotecCsd dd„|Dƒ¡S)Nr"css|]}t|ƒVqdSrr#r$rrrr'%r(z"Comment.content..r)r+rrrrU#szComment.contentcCs|jgSr)rUr+rrrr:'szComment.commentsN) r0rKrLrHr,rtrOrUr:rrrrrss rsc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) Ú AddressListz address-listcCsdd„|DƒS)NcSsg|]}|jdkr|‘qS)Úaddress©rHr$rrrrV1r(z)AddressList.addresses..rr+rrrÚ addresses/szAddressList.addressescCstdd„|DƒgƒS)Ncss|]}|jdkr|jVqdS©rwN©rHÚ mailboxesr$rrrr'5s ÿz(AddressList.mailboxes..©r3r+rrrr|3s ÿÿzAddressList.mailboxescCstdd„|DƒgƒS)Ncss|]}|jdkr|jVqdSrz©rHÚ all_mailboxesr$rrrr':s ÿz,AddressList.all_mailboxes..r}r+rrrr8s ÿÿzAddressList.all_mailboxesN)r0rKrLrHrOryr|rrrrrrv+s  rvc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) ÚAddressrwcCs|djdkr|djSdS)NrÚgroup©rHÚ display_namer+rrrrƒBszAddress.display_namecCs4|djdkr|dgS|djdkr*gS|djS©NrÚmailboxúinvalid-mailboxr{r+rrrr|Gs  zAddress.mailboxescCs:|djdkr|dgS|djdkr0|dgS|djSr„r~r+rrrrOs   zAddress.all_mailboxesN)r0rKrLrHrOrƒr|rrrrrr€>s  r€c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú MailboxListú mailbox-listcCsdd„|DƒS)NcSsg|]}|jdkr|‘qS)r…rxr$rrrrV]r(z)MailboxList.mailboxes..rr+rrrr|[szMailboxList.mailboxescCsdd„|DƒS)NcSsg|]}|jdvr|‘qS))r…r†rxr$rrrrVas ÿz-MailboxList.all_mailboxes..rr+rrrr_szMailboxList.all_mailboxesN©r0rKrLrHrOr|rrrrrr‡Ws  r‡c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú GroupListú group-listcCs |r|djdkrgS|djS©Nrrˆr{r+rrrr|iszGroupList.mailboxescCs |r|djdkrgS|djSrŒr~r+rrrroszGroupList.all_mailboxesNr‰rrrrrŠes  rŠc@s4eZdZdZedd„ƒZedd„ƒZedd„ƒZdS) ÚGroupr�cCs|djdkrgS|djS©Nér‹r{r+rrrr|zszGroup.mailboxescCs|djdkrgS|djSrŽr~r+rrrr€szGroup.all_mailboxescCs |djSr4)rƒr+rrrrƒ†szGroup.display_nameN)r0rKrLrHrOr|rrƒrrrrr�vs  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© r0rKrLrHrOrƒr˜ršr›r�rrrrr�‹s    r�c@s@eZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZd S) Ú AngleAddrz angle-addrcCs"|D]}|jdkr|jSqdS©Nú addr-spec)rHr˜rlrrrr˜ªs zAngleAddr.local_partcCs"|D]}|jdkr|jSqdSr ©rHršrlrrrrš°s zAngleAddr.domaincCs"|D]}|jdkr|jSqdS)Nú obs-route)rHÚdomainsrlrrrr›¶s zAngleAddr.routecCs<|D]2}|jdkr|jr"|jSt|jƒ|jSqdS)Nr¡z<>)rHr˜r�rrlrrrr�¼s   zAngleAddr.addr_specN) r0rKrLrHrOr˜ršr›r�rrrrrŸ¦s   rŸc@seZdZdZedd„ƒZdS)ÚObsRouter£cCsdd„|DƒS)NcSsg|]}|jdkr|j‘qSr™r¢r$rrrrVÎr(z$ObsRoute.domains..rr+rrrr¤ÌszObsRoute.domainsN)r0rKrLrHrOr¤rrrrr¥Èsr¥c@sLeZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ d S) ÚMailboxr…cCs|djdkr|djSdS©Nrr‘r‚r+rrrrƒÕszMailbox.display_namecCs |djSr4r—r+rrrr˜ÚszMailbox.local_partcCs |djSr4r™r+rrrršÞszMailbox.domaincCs|djdkr|djSdSr§)rHr›r+rrrr›âsz Mailbox.routecCs |djSr4rœ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+r rrršüsz Domain.domain)r0rKrLrHr6rOršrPrrr rr©÷sr©c@seZdZdZdS)ÚDotAtomúdot-atomNrYrrrrr­sr­c@seZdZdZdZdS)Ú DotAtomTextz dot-atom-textTN©r0rKrLrHr6rrrrr¯sr¯c@seZdZdZdZdS)Ú NoFoldLiteralzno-fold-literalFNr°rrrrr± sr±c@sDeZdZdZdZedd„ƒZedd„ƒZedd„ƒZed d „ƒZ d S) ÚAddrSpecr¡FcCs |djSr4r—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}|jdurH|d|jS|S)Nú@)Úsetr˜r”Ú DOT_ATOM_ENDSrrš)rZnamesetZlprrrr�$s   zAddrSpec.addr_specN) r0rKrLrHr6rOr˜ršrr�rrrrr²s   r²c@seZdZdZdZdS)Ú ObsLocalPartzobs-local-partFNr°rrrrr¹0sr¹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)Nrr^r“r–)rr”rrHÚpop)rrnrrrrƒ;s   zDisplayName.display_namecs¦d}|jrd}n|D]}|jdkrd}qt|ƒdkrš|ršd}}|djdks`|ddjdkrdd}|djdks„|ddjdkrˆd}|t|jƒ|StƒjSdS) NFTrhrr"r^rSr–)rrHr”rrƒrr)rrtr&ÚpreZpostr rrrLs   zDisplayName.value) r0rKrLrHrNrOrƒrrPrrr rrº6s  rºc@s,eZdZdZdZedd„ƒZedd„ƒZdS)Ú LocalPartz local-partFcCs&|djdkr|djS|djSdS)Nrrh)rHrorr+rrrres 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)NFrr^Údotr–r“)ÚDOTrHrÚ isinstancermr)rrnÚlastZ last_is_tlÚtokZis_tlrrrr˜ls(  ÿ  ÿ zLocalPart.local_partN)r0rKrLrHr6rOrr˜rrrrr½`s  r½cs4eZdZdZdZe‡fdd„ƒZedd„ƒZ‡ZS)Ú DomainLiteralzdomain-literalFcsd tƒj ¡¡Srªr«r+r rrrš‰szDomainLiteral.domaincCs"|D]}|jdkr|jSqdS)NÚptextrkrlrrrÚip�s zDomainLiteral.ip) r0rKrLrHr6rOršrÅrPrrr rrÄs rÃc@seZdZdZdZdZdS)Ú MIMEVersionz mime-versionN)r0rKrLrHÚ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<|jSqt|ƒ}|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_finderrrmrZNonPrintableDefectrrërì)ÚxtextZnon_printablesrrrÚ_validate_xtextäs  ÿrcCs t|dƒ^}}g}d}d}tt|ƒƒD]L}||dkrJ|rDd}d}nd}q&|rTd}n|||vrdq|| ||¡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 Tr"N)Ú _wsp_splitterÚranger”rmr*)rÚendcharsÚfragmentÚ 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µr r”)rZnewvaluerrrrÚget_fws srÚvtextc CsÖtƒ}| d¡s t d |¡¡‚|dd… dd¡^}}||dd…krXt d |¡¡‚d |¡}t|ƒdkr°|dtvr°|dtvr°|  d ¡dkr°| dd¡^}}|d|}t| ¡ƒdkrÒ|j   t  d ¡¡||_ d |¡}zt d|d¡\}}} } Wn(ttf�y(td  |j ¡ƒ‚Yn0||_| |_|j  | ¡|�r¨|dtv�rpt|ƒ\} }|  | ¡�qBt|dƒ^} }t| |ƒ} t| ƒ|  | ¡d |¡}�qB|�rÎ|dtv�rÎ|j   t  d ¡¡||fS) zE encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" ú=?z"expected encoded word but found {}r�Nz?=r“r"rú?zwhitespace inside encoded wordz!encoded word format invalid: '{}'z.missing trailing whitespace after encoded-word)rbÚ startswithrÚHeaderParseErrorr/r¬r*r”rÚcountrrmrãrdÚ_ewréÚ ValueErrorÚKeyErrorrrerfr9ÚWSPrrr r)rZ terminal_typeZewrÂrZremstrÚreströrerfrr;Úcharsr rrrÚget_encoded_wordsd ÿÿ   ÿ þ ý  ÿ  ÿ       ÿr,cCsDtƒ}|�r@|dtvr0t|ƒ\}}| |¡qd}| d¡rêzt|dƒ\}}Wn(tyfd}Yn„tjyxYnr0d}t |ƒdkr®|dj dkr®|j  t  d¡¡d}|rÞt |ƒd krÞ|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. rTr!ZutextFr–rz&missing whitespace before encoded wordr“éþÿÿÿrcr")rWr)rrmr#r,rrr$r”rHrrãr rÚrfc2047_matcherÚsearchÚ partitionr rr*)rrXr;Úvalid_ewZhave_wsrÂrr rrrÚget_unstructuredIsH       ÿÿ    r2cCs*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Ä)rr r©rrÄÚ_rrrÚ get_qp_ctextŒs  r5cCs*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. rrÄ)rr rr3rrrÚ get_qcontent�s  r6cCsNt|ƒ}|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_matcherrr$r/r�r”r r)rÚmr7rrrÚ get_atext¬sÿ r:cCsp|ddkrt d |¡¡‚tƒ}|dd…}|rT|ddkrTt|ƒ\}}| |¡|�r@|ddk�r@|dtvr‚t|ƒ\}}n²|dd…dk�r(d}z&t|ƒ\}}|j  t  d ¡¡d }Wn tjyÞt|ƒ\}}Yn0|�r4t |ƒdk�r4|d j d k�r4|d j dk�r4t |d d ƒ|d <n t|ƒ\}}| |¡qT|�s`|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. rrzexpected '"' but found '{}'r“Nr�r!Fz!encoded word inside quoted stringTr–rr-rcz"end of header inside quoted string)rr$r/rrr6rmr)rr,rrãr”rHr )rZbare_quoted_stringr;r1rrrÚget_bare_quoted_string¼sJ ÿ      ÿ ÿÿ    ÿr;cCs¼|r |ddkr t d |¡¡‚tƒ}|dd…}|rŽ|ddkrŽ|dtvr\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“Nruzend of header inside comment) rr$r/rsr)rÚ get_commentr5rmrrã)rrTr;rrrr<çs&ÿ      ÿr<cCsPtƒ}|rH|dtvrH|dtvr0t|ƒ\}}n t|ƒ\}}| |¡q||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)r]Ú CFWS_LEADERr)rr<rm)rr^r;rrrÚget_cfwss   r>cCsptƒ}|r,|dtvr,t|ƒ\}}| |¡t|ƒ\}}| |¡|rh|dtvrht|ƒ\}}| |¡||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)rgr=r>rmr;)rZ quoted_stringr;rrrÚget_quoted_string s      r?cCsÎtƒ}|r,|dtvr,t|ƒ\}}| |¡|rL|dtvrLt d |¡¡‚| d¡rŠzt |ƒ\}}Wq–tjy†t |ƒ\}}Yq–0n t |ƒ\}}| |¡|rÆ|dtvrÆt|ƒ\}}| |¡||fS)zPatom = [CFWS] 1*atext [CFWS] An atom could be an rfc2047 encoded word. rzexpected atom but found '{}'r!) r_r=r>rmÚ ATOM_ENDSrr$r/r#r,r:)rr`r;rrrÚget_atoms&  ÿ     rAcCsœtƒ}|r|dtvr&t d |¡¡‚|rt|dtvrtt|ƒ\}}| |¡|r&|ddkr&| t¡|dd…}q&|dtur”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¯r@rr$r/r:rmr¿)rZ dot_atom_textr;rrrÚget_dot_atom_text:s ÿ    ÿrBcCsªtƒ}|dtvr(t|ƒ\}}| |¡| d¡rfzt|ƒ\}}Wqrtjybt|ƒ\}}Yqr0n t|ƒ\}}| |¡|r¢|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!) r­r=r>rmr#r,rr$rB)rZdot_atomr;rrrÚ get_dot_atomMs        rCcCsŽ|dtvrt|ƒ\}}nd}|s,t d¡‚|ddkrFt|ƒ\}}n*|dtvrdt d |¡¡‚n t|ƒ\}}|dur†|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.rz1Expected 'atom' or 'quoted-string' but found '{}')r=r>rr$r?ÚSPECIALSr/rA)rÚleaderr;rrrÚget_wordfs" ÿ  ÿ rFcCsötƒ}zt|ƒ\}}| |¡Wn&tjyF|j t d¡¡Yn0|rî|dtvrî|ddkrŽ| t¡|j t  d¡¡|dd…}qHzt|ƒ\}}WnBtjyà|dt vrÚt |ƒ\}}|j t  d¡¡n‚Yn0| |¡qH||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) rZrFrmrr$rrãÚ PHRASE_ENDSr¿ÚObsoleteHeaderDefectr=r>)rr[r;rrrÚ get_phraseˆs4   ÿ    ÿ   ÿ rIc Csptƒ}d}|dtvr"t|ƒ\}}|s6t d |¡¡‚zt|ƒ\}}WnZtjy zt|ƒ\}}Wn4tjyš|ddkr�|dtvr�‚t ƒ}Yn0Yn0|dur¸|g|dd…<|  |¡|�r0|ddksâ|dtv�r0t t |ƒ|ƒ\}}|j dk�r|j  t d¡¡n|j  t d¡¡||d<z|j d¡Wn&t�yf|j  t d ¡¡Yn0||fS) z= local-part = dot-atom / quoted-string / obs-local-part Nrz"expected local-part but found '{}'r úinvalid-obs-local-partzrr$r/rCrFrGrrmÚget_obs_local_partrrHrrãrHrÚencoderçZNonASCIILocalPartDefect)rr˜rEr;Úobs_local_partrrrÚget_local_part®sJ  ÿ    ÿ ÿ ÿ rOcCsÀtƒ}d}|�r&|ddks*|dtv�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}Wn2tj �y|dt v�r‚t |ƒ\}}Yn0| |¡q |djd k�sV|djdk�rh|djd k�rh|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 wordsr^z!Invalid leading '.' in local partr-z"Invalid trailing '.' in local partrJ) r¹rGrrmrrãr¿r rHrFr$r=r>)rrNZlast_non_ws_was_dotr;rrrrLÕsj   ÿ    ÿ  ÿ ÿ   ÿ þ ÿ ÿ þ ÿrLcCs@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)rr rrmrrHr)rrÄrrrrÚ get_dtexts   ÿrQcCs,|rdS| t d¡¡| tddƒ¡dS)NFz"end of input inside domain-literalú]údomain-literal-endT)rmrrãr )rÚdomain_literalrrrÚ_check_for_early_dl_endsÿrUcCsjtƒ}|dtvr(t|ƒ\}}| |¡|s6t d¡‚|ddkrRt d |¡¡‚|dd…}t||ƒrp||fS| tddƒ¡|dt vr¢t |ƒ\}}| |¡t |ƒ\}}| |¡t||ƒrÊ||fS|dt vrìt |ƒ\}}| |¡t||ƒrþ||fS|ddk�rt d  |¡¡‚| tdd ƒ¡|dd…}|�rb|dtv�rbt|ƒ\}}| |¡||fS) zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] rzexpected domain-literalú[z6expected '[' at start of domain-literal but found '{}'r“Nzdomain-literal-startrRz4expected ']' at end of domain-literal but found '{}'rS) rÃr=r>rmrr$r/rUr r)rrQ)rrTr;rrrÚget_domain_literal sH     ÿ            ÿ   rWcCsptƒ}d}|dtvr"t|ƒ\}}|s6t d |¡¡‚|ddkrvt|ƒ\}}|durd|g|dd…<| |¡||fSzt|ƒ\}}Wn tjy¦t |ƒ\}}Yn0|rÂ|ddkrÂt d¡‚|durØ|g|dd…<| |¡|�rh|ddk�rh|j  t  d¡¡|dj d k�r(|d|dd…<|�rh|ddk�rh| t ¡t |d d…ƒ\}}| |¡�q(||fS) z] domain = dot-atom / domain-literal / obs-domain obs-domain = atom *("." atom)) Nrzexpected domain but found '{}'rVr¶zInvalid Domainrz(domain is not a dot-atom (contains CFWS)r®r“)r©r=r>rr$r/rWrmrCrArrHrHr¿)rršrEr;rrrÚ get_domainGsD  ÿ      ÿ rXcCs|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²rOrmrrrãr rX)rr�r;rrrÚ get_addr_specms   ÿ rZcCsŒtƒ}|rj|ddks"|dtvrj|dtvrFt|ƒ\}}| |¡q|ddkr| t¡|dd…}q|rz|ddkrŠt d |¡¡‚| t¡t |dd…ƒ\}}| |¡|�r>|ddk�r>| t¡|dd…}|sä�q>|dtv�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¥r=r>rmÚ ListSeparatorrr$r/ÚRouteComponentMarkerrXr )rZ obs_router;rrrÚ get_obs_route}sF     ÿ          ÿr^c Cs®tƒ}|dtvr(t|ƒ\}}| |¡|r8|ddkrHt d |¡¡‚| tddƒ¡|dd…}|ddkr¦| tddƒ¡|j t  d ¡¡|dd…}||fSzt |ƒ\}}Wnvtj�y,z"t |ƒ\}}|j t  d ¡¡Wn&tj�yt d  |¡¡‚Yn0| |¡t |ƒ\}}Yn0| |¡|�rZ|ddk�rZ|dd…}n|j t  d ¡¡| tddƒ¡|�r¦|dtv�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Ÿr=r>rmrr$r/r rrãrZr^rH)rZ angle_addrr;rrrÚget_angle_addr¦sT   ÿ   ÿ   ÿ ÿ    ÿ  racCs<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ºrIr9r)rrƒr;rrrÚget_display_nameÔs  rbcCsÚtƒ}d}|dtvr6t|ƒ\}}|s6t d |¡¡‚|ddkr¦|dtvr^t d |¡¡‚t|ƒ\}}|s~t d |¡¡‚|durœ|g|ddd…<d}| |¡t |ƒ\}}|durÈ|g|dd…<| |¡||fS)z, name-addr = [display-name] angle-addr Nrz!expected name-addr but found '{}'r_) r�r=r>rr$r/rGrbrmra)rZ name_addrrEr;rrrÚ get_name_addrãs6  ÿ  ÿ ÿ   rcc CsŽtƒ}zt|ƒ\}}WnJtjy`zt|ƒ\}}Wn$tjyZt d |¡¡‚Yn0Yn0tdd„|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¦rcrr$rZr/Úanyr2rHrm)rr…r;rrrÚ get_mailboxs ÿÿ recCsdtƒ}|r\|d|vr\|dtvrD| 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. rrPr“N)r¨rGrmr rI)rrZinvalid_mailboxr;rrrÚget_invalid_mailboxs  ÿ  rfcCs¸tƒ}|�r°|ddk�r°zt|ƒ\}}| |¡W�ntj�y:d}|dtvrÔt|ƒ\}}|rt|ddvr’| |¡|j t d¡¡n@t |dƒ\}}|dur¶|g|dd…<| |¡|j t  d¡¡nb|ddkrô|j t d¡¡nBt |dƒ\}}|du�r|g|dd…<| |¡|j t  d¡¡Yn0|�rˆ|ddv�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‡rermrr$r=r>rrHrfrãrHr9r\)rZ mailbox_listr;rEr…rrrÚget_mailbox_list(sX      ÿ  ÿ  ÿ   ÿ   ÿ rhcCstƒ}|s$|j t d¡¡||fSd}|rŠ|dtvrŠt|ƒ\}}|sl|j t d¡¡| |¡||fS|ddkrŠ| |¡||fSt|ƒ\}}t|j ƒdkrÚ|dur¶| |¡|  |¡|j t  d¡¡||fS|durð|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-listrgzgroup-list with empty entries) rŠrrmrrãr=r>rhr”rr9rH)rZ group_listrEr;rrrÚget_group_listas> ÿ  ÿ       ÿ ricCs tƒ}t|ƒ\}}|r"|ddkr2t d |¡¡‚| |¡| tddƒ¡|dd…}|rˆ|ddkrˆ| tddƒ¡||dd…fSt|ƒ\}}| |¡|s¶|j t  d ¡¡n|ddkrÒt d  |¡¡‚| tddƒ¡|dd…}|�r|dt v�rt |ƒ\}}| |¡||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rr[z8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorr“Nrgzgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) r�rbrr$r/rmr rirrãr=r>)rr�r;rrrÚ get_group†s8 ÿ     ÿ ÿ   rjc Csttƒ}zt|ƒ\}}WnJtjy`zt|ƒ\}}Wn$tjyZt d |¡¡‚Yn0Yn0| |¡||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€rjrr$rer/rm)rrwr;rrrÚ get_address¤sÿ rkc CsÈtƒ}|�rÀzt|ƒ\}}| |¡W�n$tj�yL}�zd}|dtvrÐt|ƒ\}}|rj|ddkrˆ| |¡|j t d¡¡nFt |dƒ\}}|dur¬|g|dd…<| t |gƒ¡|j t  d¡¡nh|ddkrð|j t d¡¡nHt |dƒ\}}|du�r|g|dd…<| t |gƒ¡|j t  d¡¡WYd}~n d}~00|�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“)rvrkrmrr$r=r>rrHrfr€rãrHr9r )rZ address_listr;ÚerrrEr…rrrÚget_address_listÁsX      ÿ ÿ  ÿ  ÿ   ÿrmcCs¨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 '{}'rrVz;expected '[' at the start of no-fold-literal but found '{}'zno-fold-literal-startr“NrRz9expected ']' at the end of no-fold-literal but found '{}'zno-fold-literal-end)r±rr$r/rmr rQ)rZno_fold_literalr;rrrÚget_no_fold_literaløs.ÿ ÿÿ   ÿÿrncCs„tƒ}|r,|dtvr,t|ƒ\}}| |¡|r<|ddkrLt d |¡¡‚| tddƒ¡|dd…}zt|ƒ\}}Wn\tjyÔz"t |ƒ\}}|j  t  d¡¡Wn$tjyÎt d |¡¡‚Yn0Yn0| |¡|rò|dd k�r<|j  t  d ¡¡|�r4|dd k�r4| td d ƒ¡|dd…}||fS| td d ƒ¡|dd…}zt|ƒ\}}Wnštj�yzt |ƒ\}}Wnrtj�yü}zVz"t|ƒ\}}|j  t  d¡¡Wn&tj�yæt d |¡¡‚Yn0WYd}~n d}~00Yn0| |¡|�r0|dd k�r0|dd…}n|j  t  d¡¡| td d ƒ¡|�r||dtv�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 "]" rr_zexpected 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-rightr`z msg-id-endrYzobsolete id-right in msg-idzFexpected dot-atom-text, no-fold-literal or obs-id-right but found '{}'zmissing trailing '>' on msg-id)rýr=r>rmrr$r/r rBrLrrHrãrnrX)rZmsg_idr;ÚerrrÚ get_msg_ids~  ÿ   ÿ ÿÿ  ÿ    ÿ ÿÿ&  ÿ  rpc Cs�tƒ}zt|ƒ\}}| |¡WnNtjyn}z4t|ƒ}t|ƒ}|j t d  |¡¡¡WYd}~n&d}~00|rŒ|j t d  |¡¡¡|S)z2message-id = "Message-ID:" msg-id CRLF zInvalid msg-id: {!r}NzUnexpected {!r}) rÿrprmrr$r2rrrãr/)rZ message_idr;ÚexrrrÚparse_message_idSs ÿ ÿrrcCsštƒ}|s |j t d¡¡|S|dtvrXt|ƒ\}}| |¡|sX|j t d¡¡d}|r’|ddkr’|dtvr’||d7}|dd…}q\| ¡sÄ|j t d  |¡¡¡| t |d ƒ¡nt |ƒ|_ | t |d ƒ¡|�r|dtv�rt|ƒ\}}| |¡|�r|ddk�rT|j du�r:|j t d ¡¡|�rP| t |d ƒ¡|S| t dd ƒ¡|dd…}|�rš|dtv�ršt|ƒ\}}| |¡|�sÂ|j du�r¾|j t d ¡¡|Sd}|�rö|dtv�rö||d7}|dd…}�qÆ| ¡�s*|j t d   |¡¡¡| t |d ƒ¡nt |ƒ|_ | t |d ƒ¡|�rn|dtv�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 CFWSr"rr“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ÆrrmrÚHeaderMissingRequiredValuer=r>Úisdigitrãr/r ÚintrÇrÈ)rZ mime_versionr;rsrrrÚparse_mime_versionos„ ÿ    ÿ  ÿ     ÿ     ÿ   ÿ    ÿrwcCsdtƒ}|r\|ddkr\|dtvrD| 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. rrgrPr“N)rÑrGrmr rI)rZinvalid_parameterr;rrrÚget_invalid_parameter³s  ÿ  rxcCsNt|ƒ}|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_matcherrr$r/r�r”r r)rr9ryrrrÚ get_ttextÅs ÿ r{cCs�tƒ}|r,|dtvr,t|ƒ\}}| |¡|rL|dtvrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|dtvrˆ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 '{}') rar=r>rmÚ TOKEN_ENDSrr$r/r{)rZmtokenr;rrrÚ get_tokenØs   ÿ    r~cCsNt|ƒ}|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_matcherrr$r/r�r”r r©rr9rÕrrrÚ get_attrtextïs ÿ r�cCs�tƒ}|r,|dtvr,t|ƒ\}}| |¡|rL|dtvrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|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. rr|) rÓr=r>rmÚATTRIBUTE_ENDSrr$r/r�©rrÔr;rrrÚ get_attribute s   ÿ    r„cCsNt|ƒ}|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_matcherrr$r/r�r”r rr€rrrÚget_extended_attrtext sÿ r‡cCs�tƒ}|r,|dtvr,t|ƒ\}}| |¡|rL|dtvrLt d |¡¡‚t|ƒ\}}| |¡|rˆ|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. rr|) rÓr=r>rmÚEXTENDED_ATTRIBUTE_ENDSrr$r/r‡rƒrrrÚ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 0rs) rØrr$r/rmr rurZInvalidHeaderErrorrvrÍ)rrÙrsrrrÚ get_sectionA s, ÿ ÿ  ÿ rŒcCs’tƒ}|st d¡‚d}|dtvr0t|ƒ\}}|sDt d |¡¡‚|ddkr^t|ƒ\}}n t|ƒ\}}|dur€|g|dd…<| |¡||fS)z quoted-string / attribute z&Expected value but found end of stringNrz Expected value but found only {}r) rÚrr$r=r>r/r?r‰rm)rÚvrEr;rrrÚ get_value_ s"   ÿ   rŽc Csªtƒ}t|ƒ\}}| |¡|r,|ddkrL|j t d |¡¡¡||fS|ddkrÊz t|ƒ\}}d|_| |¡Wntj yŒYn0|sœt  d¡‚|ddkrÊ| t ddƒ¡|dd …}d|_ |dd kràt  d ¡‚| t d d ƒ¡|dd …}d }|�r*|dt v�r*t |ƒ\}}| |¡d }|}|j �rD|�rD|dd k�rDt|ƒ\}}|j}d}|jdk�r°|�rŠ|ddk�rŠd}n$t|ƒ\}} | �rØ| ddk�rØd}n(zt|ƒ\}} Wn Yn 0| �sØd}|�r.|j t d¡¡| |¡|D](} | jdk�rþg| d d …<| }�q(�qþ|}nd }|j t d¡¡|�r^|ddk�r^d }n t|ƒ\}}|j �r~|jdk�rÒ|�r’|ddk�rÀ| |¡|d u�r¸|�r´J|ƒ‚|}||fS|j t d¡¡|�s|j t d¡¡| |¡|d u�rø||fSnð|d u�rL|D]} | jdk�r�q0�q| jdk| | ¡| j|_|ddk�rjt  d |¡¡‚| t ddƒ¡|dd …}|�rÜ|ddk�rÜt|ƒ\}}| |¡|j|_|�rÌ|ddk�rÜt  d |¡¡‚| t ddƒ¡|dd …}|d u�rptƒ} |�rj|dtv�r*t|ƒ\}}n2|dd k�rPt d dƒ}|dd …}n t|ƒ\}}|  |¡�q| }n t|ƒ\}}| |¡|d u�r¢|�ržJ|ƒ‚|}||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. rrgz)Parameter contains name ({}) but no valuerŠTzIncomplete parameterzextended-parameter-markerr“Nú=zParameter not followed by '='úparameter-separatorrFú'z5Quoted string value for extended parameter is invalidrjzZParameter 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 delimitersr…rÕz=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {}ZDQUOTE)rÉr„rmrrrãr/rŒrÌr$r rÐr=r>r?rqrÎr�r‡rHrŽrrerfrÚr)rr6) rrñr;rErZappendtoZqstringZ inner_valueZ semi_validr*Útr�rrrÚ get_parameteru sô   ÿ              ÿ     ÿ    ÿ ÿ        ÿ   ÿ       r“c Csptƒ}|�rlzt|ƒ\}}| |¡WnÒtjyø}z¸d}|dtvrTt|ƒ\}}|sr| |¡|WYd}~S|ddkr¤|dur�| |¡|j t d¡¡n@t |ƒ\}}|rÂ|g|dd…<| |¡|j t d  |¡¡¡WYd}~n d}~00|�rJ|ddk�rJ|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. Nrrgzparameter entry with no contentzinvalid parameter {!r}r–rÒz)parameter with invalid trailing text {!r}r�r“)rÛr“rmrr$r=r>rrãrxr/rHr9r )rZmime_parametersr;rlrErñrrrÚparse_mime_parameters sJ        ÿ   ÿ   ÿr”cCsˆ|rV|ddkrV|dtvr>| t|ddƒ¡|dd…}qt|ƒ\}}| |¡q|s^dS| tddƒ¡| t|dd…ƒ¡dS)zBDo our best to find the parameters in an invalid MIME header rrgrPr“Nr�)rGrmr rIr”)Z tokenlistrr;rrrÚ_find_mime_parameters7 s   r•c Cs²tƒ}d}|s$|j t d¡¡|Szt|ƒ\}}Wn:tjyn|j t d |¡¡¡t ||ƒ|YS0| |¡|rŠ|ddkr®|j t d¡¡|rªt ||ƒ|S|j   ¡  ¡|_ | tddƒ¡|dd …}zt|ƒ\}}Wn<tj�y&|j t d  |¡¡¡t ||ƒ|YS0| |¡|j   ¡  ¡|_|�sL|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}rrzInvalid content typezcontent-type-separatorr“Nz'Expected content subtype but found {!r}rgzt| ƒ} |s�| jd vr„t   | ¡ }n t   | ¡ }z|   |¡|} Wn4t yÖtd d „| jDƒƒrÊd } nd} d }Yn0| jdkròt| |||ƒq>|�r°|�s°| j�s|d}d}| j�r|| j|d�dt|jƒ …} |j| v�r|t| ƒ|t|dƒk�rjt|ƒ}| |¡|d| 7<q>t| dƒ�s–t| ƒ| } nt| |||| j| ƒ}d}q>t| ƒ|t|dƒk�rÜ|d| 7<q>| j�r"t| ƒd|k�r"t|ƒ}|�s|  ¡�r"| || ¡d}q>t| dƒ�sŒt| ƒ}| jdk�rhtddƒgdd„|Dƒtddƒg}| j�s‚|d7}| |¡|| } q>| j�r¬|�s¬|  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Ër"NrFÚwrap_as_ew_blockedr“)rÄr css|]}t|tjƒVqdSr)rÀrrìr$rrrr'ò sÿz%_refold_parse_tree..ú unknown-8bitTrÜr<r–rMrjrrÄcSsg|]}tt|ƒdƒ‘qS)rÄ)r r)r%ÚprrrrV1 sÿz&_refold_parse_tree..)Zmax_line_lengthÚsysÚmaxsizeÚutf8rrr»rrHÚ SPECIALSNLÚ isdisjointÚNLSETrMrçrdr2Ú_fold_mime_parametersr6rMr@r”rþr›rmrIÚ _fold_as_ewrNr5r Úinsertr*)Z parse_treer=ÚmaxlenrÞršÚlast_ewr�Z want_encodingZend_ew_not_allowedrïr7ÚtstrreZ encoded_partÚnewlineZnewpartsrrrr>Ô s¬       ÿ       ÿÿ   ÿÿ ýÿ  r>cCsà|dur<|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–rgÚstrictFTržrßrœr")Úsaferz {}*={}''{}ròr“rSr�rz''r³éNNz {}*{}*={}{})rír´rÖrMrçrrërärårtr/rr”rmr)r7ršr©rÞrîrreZ error_handlerZencoding_requiredZ encoded_valuer«rÙZ extra_chromer®Z splitpointZmaxcharsÚpartialrrrr¦‰ s`    ÿ  ÿ ÿ r¦)r )¡rÚrer räÚstringrÚoperatorrZemailrr&rrr·r)r=rDr@r¸rGZ TSPECIALSr}Z ASPECIALSr‚rˆr¥r£rrÚcompileÚVERBOSEÚ MULTILINEr.rrrQrWrZr\r]r_rarbrgrrrsrvr€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ÿrrrrr r r r$rr¿r\r]r/r*r¬rrÚmatchr8Úfindallrrzrr†rrrr,r2r5r6r:r;r<r>r?rArBrCrFrIrOrLrQrUrWrXrZr^rarbrcrerfrhrirjrkrmrnrprrrwrxr{r~r�r„r‡r‰rŒrŽr“r”r•r—r˜r™r›r>r§r¦rrrrÚs4E           øC  "   !*$ V +      ÿ ÿÿÿ  1C+ "&'/'&).9%7ED49/tA