bg, dZddlmZddlmZddlmZddlmZddlmZmZm Z m Z m Z ddl Z ddl mZmZdd lmZmZdd lmZdd lmZdd lmZed ZeedzZedZeezZeedz Zeedz Zeedzedz ZeezZ eedzZ!e!ezZ"e"edz Z#dZ$Gdde%Z&Gdde Z'Gdde'Z(Gdde'Z)Gdde'Z*Gd d!e'Z+Gd"d#e(Z,Gd$d%e'Z-Gd&d'e'Z.Gd(d)e'Z/Gd*d+e'Z0Gd,d-e0Z1Gd.d/e(Z2Gd0d1e'Z3Gd2d3e'Z4Gd4d5e'Z5Gd6d7e'Z6Gd8d9e'Z7Gd:d;e'Z8Gd<d=e'Z9Gd>d?e'Z:Gd@dAe'Z;GdBdCe'Z<GdDdEe'Z=GdFdGe'Z>GdHdIe'Z?GdJdKe'Z@GdLdMe'ZAGdNdOe*ZBGdPdQe'ZCGdRdSe'ZDGdTdUe'ZEGdVdWe'ZFGdXdYeFZGGdZd[e'ZHGd\d]e'ZIGd^d_e'ZJGd`dae'ZKGdbdce'ZLGdddeeLZMGdfdgeLZNGdhdie'ZOGdjdke'ZPGdldme'ZQGdndoe ZRGdpdqeRZSGdrdseRZTGdtdueSZUeTddvZVeTdwdxZWeTdydzZX e jYd{Zd|[ej\Z]e jYd}Zd|[e^d~d^ddj_Z`e jYdjaZbe jYd}Zd|[e ^d~d^ddj_Zce jYd}Zd|[e"^d~d^ddj_Zde jYd}Zd|[e#^d~d^ddj_ZedZfdZgdZhdZidZjdZkdZldZmdZndZodZpdZqdZrdZsdZtdZudZvdZwdZxdZydZzdZ{dZ|dZ}dZ~dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdS)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. )print_function)unicode_literals)division)absolute_import)intrangestrsuperlistN) namedtuple OrderedDict)unquoteunquote_to_bytes)_encoded_words)errors)utilsz (z ()<>@,:;.\"[].z."(z/?=z*'%%c|dt|ddddzdzS)N"\\\z\")r replacevalues |/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/future/backports/email/_header_value_parser.py quote_stringrds8 s5zz!!$//77UCC CC GGc4eZdZdZdZdZdZdZddZdS) _Foldedc||_||_d|_d|_d|_g|_t |_dS)NrT)maxlenpolicylastlen stickyspace firstlinedoner current)selfr#r$s r__init__z_Folded.__init__ms<    vv rc|j|j|j|jj|jd|_dSNr)r(extendr)appendr$linesepclearr%r*s rnewlinez_Folded.newlinevsT &&& ,---  rc@|jr|dSdSN)r)r3r2s rfinalizez_Folded.finalize|s' <  LLNNNNN  rc6d|jSN)joinr(r2s r__str__z_Folded.__str__swwty!!!rc:|j|dSr5)r)r/)r*stokens rr/z_Folded.appends F#####rNc|t|}t|}|j-t|j}|j|z|z|jkri|j|j|xj|z c_|j||xj|z c_d|_d|_dS|jr\| }|/|xjt|z c_|t|z }| |dS|r|dz|jkr|j|z }d|cxkr|krEnnB||z }|j|jd||j|d|_|}| |j|j|j|||z|_d|_d|_dS|js| |j|j|j|d|_d|_dS|j|z|jkr,|j||xj|z c_dS||jkr7| |j|||_dSdS)NFTr) r lenr&r%r#r)r/r'has_fwspop_leading_fws_foldr3)r*tokenr=lstickyspace_lenwsmargintrims rappend_if_fitsz_Folded.append_if_fitss >ZZF KK   '!$"233O|o-1T[@@ ##D$4555 /  ##F+++ ! #' !&t} **,,>$$B/$$#s2ww.O D!!!t 1q5DK#7#7qv/////////*V3DL''(8$(?@@@'+'7'>D$&*O  ##D$4555 ##F+++ ?2 #' !&t>   L   0 1 1 1 L   ' ' '#D "DN4 z$TokenList.__str__..(,,!s1vv,,,,,,rr:r2s rr;zTokenList.__str__s#ww,,t,,,,,,rcd|jjtt|SNz{}({}))formatrUrKr rP__repr__r*rUs rrbzTokenList.__repr__s<t~6$Y55>>@@BB Brc@dd|DS)Nr9c32K|]}|j |jVdSr5rrYs rr\z"TokenList.value..s+8818qw888888rr^r2s rrzTokenList.values#ww88888888rc@td|D|jS)Nc3$K|] }|jV dSr5) all_defectsrYs rr\z(TokenList.all_defects..s$00aAM000000r)sumrRr2s rrhzTokenList.all_defectss#004000$,???rc#K|j}t}|D]}|r>|rt| d|_ | |r |j r||+|||VdS)Nus-asciic3JK|]}t|tjVdSr5 isinstancerUndecodableBytesDefectrYs rr\z"TokenList._fold..NC33"!V%BCC333333r unknown-8bitutf-8r)rqr r@rUnicodeEncodeErroranyrhrr$rJrBrvr&rArCr/r3)r*rr{tstrtlenrrGs rrCzTokenList._foldGsJ  Dt99Dt99D !D   ,,,,% ! ! !33!%!133333&,GG&Gw >>4yy !$$T400 %%''B~&)!%5%5"((..|  6""" MM$    NN    ?  s"A  AB'&B'r9crtd|ddSN r9)indent)printr:_ppr*rs rpprintzTokenList.pprintis0 dii++,,-----rcTd|dSr)r:rrs rppstrzTokenList.ppstrls"yy,,---rc#pKd||jj|jV|D]L}t |ds|d|zV-||dzD]}|VM|jrd|j}nd}d||VdS)Nz{}{}/{}(rz* !! invalid element in token list: {!r}z z Defects: {}r9z{}){})rarUrKruhasattrrrR)r*rrDlineextras rrz TokenList._ppos  N # O     E5%(( !55;VE]]CDDDD"IIfVm44DJJJJ < "))$,77EEEnnVU+++++++rr9)rKrLrMrur+r;rbpropertyrrhrqrlrBrmrAr}rrrrrCrrr __classcell__rUs@rrPrPsJ---BBBBB99X9@@X@T;;X; ((())) *** X ---X       D........,,,,,,,,rrPc:eZdZedZedZdS)WhiteSpaceTokenListcdSN rNr2s rrzWhiteSpaceTokenList.valuesrcd|DS)Nc2g|]}|jdk |jS)comment)rucontentrYs r z0WhiteSpaceTokenList.comments..s&CCCa1<+B+B +B+B+BrrNr2s rrzWhiteSpaceTokenList.commentssCC4CCCCrN)rKrLrMrrrrNrrrrsM XDDXDDDrrceZdZdZdZdZdS)UnstructuredTokenList unstructuredc td|Dr||Sd}|jD] }t|}d} t|dn#t $rtd|jDrd}nd}|td|j |d|gz |}td|j d|D}t|} t| } || z|j kr,|j |d=|| || z|_Y-| |}d }YnwxYw|||r|rt|j d z }{|s|r|||} | +t| |_||r|jr|||||d}"dS) Nc3,K|]}|jdkVdS) encoded-wordNrurYs rr\z.UnstructuredTokenList._fold..s)::q|^+::::::rFrc3JK|]}t|tjVdSr5rrYs rr\z.UnstructuredTokenList._fold..sC22"!V%BCC222222rrrr9c34K|]}t|VdSr5r@rYs rr\z.UnstructuredTokenList._fold..s($N$NSVV$N$N$N$N$N$NrTr?)r _fold_encodedrqr rrrhget_unstructuredr:r)rrir@r#r/r%rJ _fold_as_ewrBr&rArr3) r*rlast_ewr{ris_ewrchunk oldlastlenschunklchunkrGs rrCzUnstructuredTokenList._folds ::T::: : : .%%f-- -J0 0 Dt99DE D   ,,,,%   22 $ 022222&,GG%G&-wxx 8$ ?@@BBBQ/RYBZBZ!$$N$NV^HWH5M$N$N$N!N!NJ ZZF [[F!F*fm;;"N7884 f---)3f)< ++G44) *$$T400 6!&.11A5G    (((%%''B~%(WW"((..|  &!!! MM$    NN   GGa0 0 s "A11C1E?%E?>E?c g}d}|D]}t|} |d||=#t$r|9||||t |}nUt d||d|gz}||YwxYwd|S)Nrr9) r rr/rrr@rr:r)r*rr$rrr{sparttls rrz UnstructuredTokenList.cte_encodes 5 5DIIE 5 Z((( 5!!!!% 5 5 5?JJtw??@@@!#hhGG)"''#ghh-5'2I*J*JKKBJJr1133444  5wws||s*ABC! C!NrKrLrMrurCrrNrrrrs8J666prrceZdZdZdZdZdS)Phrasephrasec d}|jD]=}t|}t|}d} t|dn#t$rt d|jDrd}nd}|6|s!|djdkr|j r| d}nd}t|D]*\} } | jd krt| dd|| <+td|j|d|gz|} t| } t| } || z|jkrE|j|d=|| t'd |jD|_Y||}t|}d }YnwxYw|||r7|r|j st|jd z }n|j s |jd krd}(||?dS)NFrc3JK|]}t|tjVdSr5rrYs rr\zPhrase._fold..rrrrrxryr9bare-quoted-stringc34K|]}t|VdSr5rrYs rr\zPhrase._fold.. s(,L,LSVV,L,L,L,L,L,LrTr? quoted-string)rqr r@rrrrhr}rurrv enumeraterrr:r)rr#r/rir%rJrC)r*rrr{rrhas_ewr remainderirDrrrs rrCz Phrase._foldsJ3 3 Dt99Dt99DF% D   ,,,,%# # # 33!%!133333&,GG%G&t/G/G/I/I&Bx*f444$(HHRLL $& $-dOOFF5 +/CCC&;E!!!H&E&EDG,wxx 8$ ?@@BBBQ/RYBZBZ ZZF [[F'6=88"N7884 f---),,L,LV^,L,L,L)L)L ++G444yyG# H$$T400 #$-#!&.11A5GG]#do&H&H#G JJv    g3 3 s"AE$G"9&G"!G"c "g}d}d}|D]q}t|} |d||n#t$rd}|@|jst |}||||n|s|djdkr|jr| d}nd}t|D]*\} } | jdkrt| dd|| <+td ||d|gz} | |g||d<YnwxYw|js |s |jdkrd}sd |S) NFrTrxryr9rr)r rr/rrr@rr}rurvrrrr:r) r*rr$rrrr{rrrrDrs rrzPhrase.cte_encodes  DIIE B Z((( 5!!!!% B B B?=+"%c((JJtw??@@@@1133 BBx*f444$(HHRLL $& $-dOOFF5 +/CCC&;E!!!H&E&EDG)"''#ghh-5'2I*J*JKKB%'%7%7%@%@$ACM B } U t//Q/Qwws||s*ADE! E!NrrNrrrrs8J<<<|rrceZdZdZdS)WordwordNrKrLrMrurNrrrr:JJJrrceZdZdZdZdS)CFWSListryc*t|jSr5)boolrr2s rr}zCFWSList.has_leading_commentCsDM"""rN)rKrLrMrur}rNrrrr?s(J#####rrceZdZdZdS)AtomatomNrrNrrrrGrrrceZdZdZdS)TokenrDNrrNrrrrLsJJJrrc4eZdZdZdZdZdZedZdS) EncodedWordrNcp|j|jStjt||jdSr5)cterrr rr2s rencodedzEncodedWord.encodedXs1 8 8O 3t99dl+++++r) rKrLrMrurrlangrrrNrrrrQsAJ CG D ,,X,,,rrcTeZdZdZedZedZedZdS) QuotedStringrc8|D]}|jdkr |jcSdSNrrurr*r[s rrzQuotedString.contentds9  A|333w4  rcg}|D]J}|jdkr#|t|0||jKd|S)Nrr9)rur/r rr:)r*rr[s r quoted_valuezQuotedString.quoted_valuejsf $ $A|333 3q66"""" 17####wws||rc8|D]}|jdkr |jcSdSrrr*rDs rstripped_valuezQuotedString.stripped_valuets= # #E#777{"""8 # #rN)rKrLrMrurrrrrNrrrr`sh J X X##X###rrc.eZdZdZdZedZdS)BareQuotedStringrcZtdd|DS)Nr9c34K|]}t|VdSr5rXrYs rr\z+BareQuotedString.__str__..s(#9#9qCFF#9#9#9#9#9#9r)rr:r2s rr;zBareQuotedString.__str__s+BGG#9#9D#9#9#999:::rc@dd|DS)Nr9c34K|]}t|VdSr5rXrYs rr\z)BareQuotedString.value..r]rr^r2s rrzBareQuotedString.value#ww,,t,,,,,,rN)rKrLrMrur;rrrNrrrr{sC%J;;;--X---rrcJeZdZdZdZdZedZedZdS)CommentrcldtdgfdDdgggS)Nr9rc:g|]}|SrN)quote)rZr[r*s rrz#Comment.__str__..s#999qTZZ]]999r))r:rir2s`rr;zComment.__str__sPwws E9999D999 E " ##$$ $rc|jdkrt|St|ddddddS)Nrrrrz\(rz\))rur r)r*rs rrz Comment.quotesc  y ( (u:: 5zz!!$//77"%t---4W"%t.-.- -rc@dd|DS)Nr9c34K|]}t|VdSr5rXrYs rr\z"Comment.content..r]rr^r2s rrzComment.contentrrc|jgSr5)rr2s rrzComment.commentss  ~rN) rKrLrMrur;rrrrrNrrrrsmJ$$$-----X-XrrcTeZdZdZedZedZedZdS) AddressListz address-listcd|DS)Nc(g|]}|jdk |S)addressrrYs rrz)AddressList.addresses..$;;;a1<#:#:#:#:#:rrNr2s r addresseszAddressList.addresses;;4;;;;rc6td|DgS)Nc3:K|]}|jdk |jVdSrNru mailboxesrYs rr\z(AddressList.mailboxes..s<>>Q\9%<%<K%<%<%<%<>>rrir2s rrzAddressList.mailboxes6>>!>>>?ACC Crc6td|DgS)Nc3:K|]}|jdk |jVdSrru all_mailboxesrYs rr\z,AddressList.all_mailboxes..s<>>Q\9%<%<O%<%<%<%<>>rrr2s rrzAddressList.all_mailboxesrrN)rKrLrMrurrrrrNrrrrspJ <<X<CCXCCCXCCCrrcTeZdZdZedZedZedZdS)AddressrcB|djdkr |djSdS)Nrgroupru display_namer2s rr%zAddress.display_names) 7  ( (7' ' ) (rcv|djdkr |dgS|djdkrgS|djSNrmailboxinvalid-mailboxrr2s rrzAddress.mailboxessE 7  * *G9  !W #4 4 4IAw  rc|djdkr |dgS|djdkr |dgS|djSr'rr2s rrzAddress.all_mailboxessL 7  * *G9  !W #4 4 4G9 Aw$$rN)rKrLrMrurr%rrrNrrr!r!shJ ((X(!!X!%%X%%%rr!c>eZdZdZedZedZdS) MailboxList mailbox-listcd|DS)Nc(g|]}|jdk |S)r(rrYs rrz)MailboxList.mailboxes..rrrNr2s rrzMailboxList.mailboxesrrcd|DS)Nc$g|] }|jdv |S))r(r)rrYs rrz-MailboxList.all_mailboxes..s/???a|======rrNr2s rrzMailboxList.all_mailboxess"??4??? ?rNrKrLrMrurrrrNrrr,r,sMJ <<X<??X???rr,c>eZdZdZedZedZdS) GroupList group-listcF|r|djdkrgS|djSNrr-rr2s rrzGroupList.mailboxess- tAw)^;;IAw  rcF|r|djdkrgS|djSr7rr2s rrzGroupList.all_mailboxess- tAw)^;;IAw$$rNr2rNrrr4r4sMJ !!X! %%X%%%rr4cTeZdZdZedZedZedZdS)Groupr#cB|djdkrgS|djSNr5rr2s rrzGroup.mailboxess& 7  - -IAw  rcB|djdkrgS|djSr<rr2s rrzGroup.all_mailboxess& 7  - -IAw$$rc|djSr-)r%r2s rr%zGroup.display_namesAw##rN)rKrLrMrurrrr%rNrrr:r:shJ !!X! %%X% $$X$$$rr:ceZdZdZedZedZedZedZedZ dS)NameAddr name-addrcFt|dkrdS|djSrk)r@r%r2s rr%zNameAddr.display_names# t99>>4Aw##rc|djSNrx local_partr2s rrGzNameAddr.local_part sBx""rc|djSrEdomainr2s rrJzNameAddr.domainsBxrc|djSrE)router2s rrLzNameAddr.routesBx~rc|djSrE addr_specr2s rrOzNameAddr.addr_specsBx!!rN rKrLrMrurr%rGrJrLrOrNrrrArAsJ $$X$ ##X#XX""X"""rrAcjeZdZdZedZedZedZedZdS) AngleAddrz angle-addrc8|D]}|jdkr |jcSdSN addr-spec)rurGrs rrGzAngleAddr.local_part s; $ $A|{**|###+ $ $rc8|D]}|jdkr |jcSdSrTrurJrs rrJzAngleAddr.domain&s8  A|{**x+  rc8|D]}|jdkr |jcSdS)N obs-route)rudomainsrs rrLzAngleAddr.route,s; ! !A|{**y   + ! !rc8|D]}|jdkr |jcSdS)NrUz<>)rurOrs rrOzAngleAddr.addr_spec2s8  A|{**{"""+4rN) rKrLrMrurrGrJrLrOrNrrrRrRsJ $$X$   X !!X! XrrRc(eZdZdZedZdS)ObsRouterYcd|DS)Nc2g|]}|jdk |jSrIrWrYs rrz$ObsRoute.domains..As&CCCQ!,(*B*B*B*B*BrrNr2s rrZzObsRoute.domains?sCC$CCCCrN)rKrLrMrurrZrNrrr]r];s7J DDXDDDrr]ceZdZdZedZedZedZedZedZ dS)Mailboxr(cB|djdkr |djSdSNrrBr$r2s rr%zMailbox.display_nameHs) 7  , ,7' ' - ,rc|djSr-rFr2s rrGzMailbox.local_partMAw!!rc|djSr-rIr2s rrJzMailbox.domainQsAw~rcB|djdkr |djSdSrc)rurLr2s rrLz Mailbox.routeUs( 7  , ,7=  - ,rc|djSr-rNr2s rrOzMailbox.addr_specZsAw  rNrPrNrrraraDsJ ((X(""X"X!!X!!!X!!!rrac8eZdZdZedZexZxZxZZ dS)InvalidMailboxr)cdSr5rNr2s rr%zInvalidMailbox.display_namectrNrPrNrrrjrj_sB"J X/;:J::%)))rrjc2eZdZdZefdZxZS)DomainrJcdtt|jSr8)r:r rnrsplitrcs rrJz Domain.domainns.wwuVT**06688999r)rKrLrMrurrJrrs@rrnrnjsGJ ::::X:::::rrnceZdZdZdS)DotAtomdot-atomNrrNrrrrrrssJJJrrrceZdZdZdS) DotAtomTextz dot-atom-textNrrNrrruruxs JJJrrucjeZdZdZedZedZedZedZdS)AddrSpecrUc|djSr-rFr2s rrGzAddrSpec.local_partrercFt|dkrdS|djS)Nrx)r@rJr2s rrJzAddrSpec.domains" t99q==4Bxrct|dkr |djS|dj|djz|djzS)Nrzrr?r=)r@rrstriplstripr2s rrzAddrSpec.valuesW t99q==7= Aw}##%%d1gm3DGM4H4H4J4JJJrct|j}t|t|tz krt |j}n|j}|j |dz|jzS|S)N@)setrGr@ DOT_ATOM_ENDSrrJ)r*namesetlps rrOzAddrSpec.addr_specsedo&& w<<#gm344 4 4do..BBB ; "8dk) ) rN) rKrLrMrurrGrJrrOrNrrrwrw}sJ ""X"X KKXK XrrwceZdZdZdS) ObsLocalPartzobs-local-partNrrNrrrrs!JJJrrcHeZdZdZedZefdZxZS) DisplayNamez display-namect|}|djdkr|dn7|ddjdkr t|ddd|d<|djdkr|n7|ddjdkr t|ddd|d<|jS)Nrryr?rx)rPrurvr)r*rs rr%zDisplayName.display_namesoo q6  & & GGAJJJJ1vay#v--"3q6!"":..A r7  ' ' GGIIII2wr{%//#CGCRCL11Byrc\d}|jrd}n|D]}|jdkrd}|rrdx}}|djdks|ddjdkrd}|djdks|ddjdkrd}|t|jz|zSt t |jS) NFTrr9rryrrx)rRrurr%r rr)r*rr[prepostrUs rrzDisplayName.values < !EE ! !eZdZdZedZedZdS) LocalPartz local-partcX|djdkr |djS|djS)Nrr)rurrr2s rrzLocalPart.values- 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)NFrrydotrxr?)DOTrurPrr/r)r*rlast last_is_tltokis_tls rrGzLocalPart.local_partse 7cU?  C~'' /s~66H'611#D"I..BsI..E $/U22F%// 9SW--.... 3r7DJJAbD ""yrN)rKrLrMrurrrGrNrrrrsMJ !!X! XrrcHeZdZdZefdZedZxZS) DomainLiteralzdomain-literalcdtt|jSr8)r:r rrrprcs rrJzDomainLiteral.domains.wwu]D117==??@@@rc8|D]}|jdkr |jcSdS)Nptextrrs ripzDomainLiteral.ips8  A|w&&w'  r)rKrLrMrurrJrrrs@rrrsg!J AAAAXAXrrceZdZdZdZdZdS) MIMEVersionz mime-versionN)rKrLrMrumajorminorrNrrrrsJ E EEErrcJeZdZdZdZdZdZedZedZ dS) Parameter parameterFrc.|jr |djndSrk) sectionednumberr2s rsection_numberzParameter.section_number s"&6tAw~~Q6rc|D]N}|jdkr |jcS|jdkr-|D]*}|jdkr|D]}|jdkr |jcccS+OdS)Nrrrr9)rurrs r param_valuezParameter.param_values < ''))D6!!!t 4L  !5u = > > > >!<<>>  KD%5MMEKAhqk)G.7.>.> * ***NE!Q&&M(()CD*F*FGGG)>RR 0 7 7P$)LL:K$L$LEE*PPP %*LL=N$O$OEEE P !077R!M001N1P1PQQQ.CCC!( B B B C ""5))))GGK((E+    A  s$$F14E  E.-E.1GGc g}|jD]S\}}|r7|d|t|>||Td|}|rd|zndS)Nz{}={}z; rr9)rr/rarr:)r*rrrs rr;zMimeParameters.__str__ys; $ $KD% $ gnnT<3F3FGGHHHH d####6""%-sV||2-rN)rKrLrMrurrr;rNrrrrBsB"J 00X0d.....rrc:eZdZedZedZdS)ParameterizedHeaderValuecRt|D]}|jdkr |jcSiS)Nr)reversedrurrs rrzParameterizedHeaderValue.paramss>d^^ $ $E#444|###5 rc|r1|djdkr t|dd|dzSt|jS)Nrxr)rurPrqr2s rrqzParameterizedHeaderValue.partssL  3DH'+<<<T#2#Yb122 2$$rN)rKrLrMrrrqrNrrrrsH X %%X%%%rrceZdZdZdZdZdS) ContentTypez content-typetextplainN)rKrLrMrumaintypesubtyperNrrrrsJHGGGrrceZdZdZdZdS)ContentDispositionzcontent-dispositionN)rKrLrMrucontent_dispositionrNrrrrs&JrrceZdZdZdZdS)ContentTransferEncodingzcontent-transfer-encoding7bitN)rKrLrMrurrNrrrrs,J CCCrrceZdZdZdS) HeaderLabelz header-labelNrrNrrrrsJJJrrceZdZdZdZdS)Headerheaderc|t|dt|jd|_|djdkr"t|dnd|_|d}|rtd| |dS)Nrryr9zMalformed Header token list) r/r rvr@r)r%rur& ValueErrorrC)r*rrests rrCz Header._folds c$((1++&&'''V^A.//26a1Cv1M1MS!---SUxx{{  <:;; ; 6rN)rKrLrMrurCrNrrrrs(Jrrc~eZdZfdZfdZedZd fd ZdZdZ dZ ed Z d Z d Z xZS) Terminalcvtt|||}||_g|_|Sr5)r r__new__rurR)clsrrur*rUs rrzTerminal.__new__s5Xs##++C77$  rcd|jjtt|Sr`)rarUrKr rrbrcs rrbzTerminal.__repr__s2t~6h8M8M8V8V8X8XYYYrc*t|jSr5)r rRr2s rrhzTerminal.all_defectssDL!!!rr9c d||jj|jt t ||jsdnd|jgS)Nz {}{}/{}({}){}r9z {})rarUrKrur rrbrR)r*rrUs rrz Terminal._ppse&&  N # O (D ! ! * * , ,l BBB T\(B(B  rct|} |d|S#t$rtj||cYSwxYw)Nr)r rrr)r*rr$rs rrzTerminal.cte_encodes\D  . LL $ $ $L! . . .:eW-- - - - .s(A  A cdSr5rNr2s rrmzTerminal.pop_trailing_wsrlrcdSr5rNr2s rrBzTerminal.pop_leading_fwsrlrcgSr5rNr2s rrzTerminal.commentss rcdSNFrNr2s rr}zTerminal.has_leading_commenturc.t||jfSr5)r rur2s r__getnewargs__zTerminal.__getnewargs__s4yy$/**rr)rKrLrMrrbrrhrrrmrBrr}rrrs@rrrs ZZZZZ""X"...X+++++++rrc.eZdZedZdZdZdS)WhiteSpaceTerminalcdSrrNr2s rrzWhiteSpaceTerminal.valuerrcdS)NTrNr2s rrlz!WhiteSpaceTerminal.startswith_fwsstrTN)rKrLrMrrrlrArNrrrrs= XGGGrrc4eZdZedZdZdZdZdS) ValueTerminalc|Sr5rNr2s rrzValueTerminal.values rcdSrrNr2s rrlzValueTerminal.startswith_fws rrFcFtjt||Sr5)rrr )r*rs rrzValueTerminal.as_encoded_wordsz#d))W---rN)rKrLrMrrrlrArrNrrrrsR XG.....rrcDeZdZedZedZdZdZdS)EWWhiteSpaceTerminalcdSr8rNr2s rrzEWWhiteSpaceTerminal.valuesrrc|ddSr5rNr2s rrzEWWhiteSpaceTerminal.encodedsAAAwrcdSr8rNr2s rr;zEWWhiteSpaceTerminal.__str__srrTN)rKrLrMrrrr;rArNrrrrsX XXGGGrrr,list-separatorrzroute-component-markerz([{}]+)r9z[^{}]+rr]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_finderrRr/rNonPrintableDefectrrr)xtextnon_printabless r_validate_xtextr Os+511NH V6~FFGGG U##; V: 8:: ; ; ; ; ;;;rctt|d}|dd|ddgz\}}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. r?NFrTr9)r _wsp_splitterrr@r/r:) rendchars _3to2listfragmentrvcharsescapehad_qpposs r_get_ptext_to_endcharsrYs ]5!,,--I$RaR=IabbM?:Hi F F FS]]## C=D    FF c]h & & E hsm$$$$Ag 776??BGGXcdd^$4y$@AA6 IIrcg}g}d}|r t|d\}}}n#t$r |dd}}}YnwxYw|dr|ds!d|||z|z|z|fSt j|\}}}} |||| |}|d|||fS)a" Decode a run of RFC2047 encoded words. _decode_ew_run(value) -> (text, value, defects) Scans the supplied value for a run of tokens that look like they are RFC 2047 encoded words, decodes those words into text according to RFC 2047 rules (whitespace between encoded words is discarded), and returns the text and the remaining value (including any leading whitespace on the remaining value), as well as a list of any defects encountered while decoding. The input value may not have any leading whitespace. r9r?=??=) rr startswithrr:rrr/r.) rrrRlast_wsrrGrrr new_defectss r_decode_ew_runrxs CGG   +*5!44NCUU + + +"BUCCC +t$$ Ed);); E773<<3!3e!;WD D+.:c??(gt[ 4{###   773<<' ))s 22c|}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. Nrt)r}rr@)rnewvaluerts rget_fwsr sF||~~H U# >@@ @eABBioodA..//J !n 122'77OC eABBi% 0 7 7 > >@@ @ WWY  F bqbz &,,tQ//00 %bqb>Z^,<<iDj4 399;;! &4 ,.. / / / BF GGI  E@'*z$*t2C'D'D$gtWW @@@% / 6 6rv > >@@ @@BJBGJg  " 7c>>!$--KE4 IIe    -a0011 &rrNjn-==yeW-- %wwy!!  " u9s 'G7G>cht}|r|dtvr(t|\}}||:|dr t |\}}d}t |dkr?|djdkr.|jtj dd}|r=t |dkr*|d jd krt|dd|d<|| #tj $rYnwxYwtt|d}|d d|dd gz\}}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. rrTrxrtz&missing whitespace before encoded wordFr?rNr"r9)rr%r r/rr,r@rurRrrrr#r rrr r:)rrrDhave_ws _3to2list7rrr"s rrrs.)**L # 8s??"5>>LE5    & & &    D ! !  /66 u|$$q((#B'2e;;$,33F4ND5F5FGGG"'5s<00144#B'2nDD+?(,e,5,5 R(##E****    -q1122 $RaR.JqrrN+;;Yc7++E""" ""9 #: sDD-,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 an 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_s r get_qp_ctextr4s@-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 an 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)rrr r2s r get_qcontentr6s@-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_matcherrr#rar#r@rr )rmr8s r get_atextr;$s e$$A ;% + 2 25 9 9;; ; GGIIE #e**++ E % ) )EE %<rc|ddkr'tjd|t}|dd}|rc|ddkrW|dtvrt |\}}nt |\}}|||r |ddkW|s0|jtj 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 '{}'r?Nz"end of header inside quoted string) rr#rarr%r r6r/rRr)rbare_quoted_stringrDs rget_bare_quoted_stringr>4s Qx3% * 1 1% 8 8:: :)++ !""IE )E!HOO 8s??"5>>LE55'..LE5!!%((( )E!HOO )"))&*D 0+2+2 3 3 3!5(( uQRRy ((rc |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 '{}'r?Nrzend of header inside comment) rr#rarr%r  get_commentr4r/rRr)rrrDs rr@r@Ls2  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_LEADERr%r r@r/)rryrDs rget_cfwsrCes ::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)rrBrCr/r>)r quoted_stringrDs rget_quoted_stringrFrs!NNM $q[(( uU###)%00LE5 $q[(( uU### % rct}|r6|dtvr't|\}}|||r6|dtvr't jd|t|\}}|||r6|dtvr't|\}}||||fS)z"atom = [CFWS] 1*atext [CFWS] rzexpected atom but found '{}') rrBrCr/ ATOM_ENDSrr#rar;)rrrDs rget_atomrIs 66D q[(( u E :qY&&% * 1 1% 8 8:: :U##LE5KK q[(( u E ;rct}|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 '{}'rr?Nrxz4expected atom at end of dot-atom-text but found '{}')rurHrr#rar;r/r)r dot_atom_textrDs rget_dot_atom_textrLs MMM :E!H ))%'++16%==:: : E!HI-- '' uU###  U1X__   % % %!""IE E!HI-- RC%'#VCI..00 0 % rcPt}|dtvr't|\}}||t |\}}|||r6|dtvr't|\}}||||fS)z- dot-atom = [CFWS] dot-atom-text [CFWS] r)rrrBrCr/rL)rdot_atomrDs r get_dot_atomrOsyyH Qx; u$U++LE5 OOE q[(( u U?rc4|dtvrt|\}}nd}|ddkrt|\}}nH|dtvr't jd|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. rNrz1Expected 'atom' or 'quoted-string' but found '{}')rBrCrFSPECIALSrr#rarI)rleaderrDs rget_wordrSs  Qx;   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 wordrrzperiod in 'phrase'r?Nzcomment found without atom) rrSr/rr#rRr PHRASE_ENDSrObsoleteHeaderDefectrBrC)rrrDs r get_phraserWsXXF0 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) Frrrzinvalid repeated '.'Tr?Nmisplaced-specialz/'\' character outside of quoted-string/ccontentrxrzmissing '.' between wordsryz!Invalid leading '.' in local partr.z"Invalid trailing '.' in local partrY) rrUrRr/rrrrrurSr#rBrC)rr]last_non_ws_was_dotrDs rr[r[$s"^^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 an 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[]rz(quoted printable found in domain-literal)rrrRr/rrVr )rrrs r get_dtextrcSsu2%>>E5& % ) )E 9 V8 688 9 9 9E %<rc|rdS|tjd|tdddS)NFz"end of input inside domain-literalrdomain-literal-endT)r/rrr)rdomain_literals r_check_for_early_dl_endrggs] 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 '{}'r?Nzdomain-literal-startrz4expected ']' at end of domain-literal but found '{}'re) rrBrCr/rr#rargrr%r rc)rrfrDs rget_domain_literalrjos6#__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  rcnt}d}|dtvrt|\}}|s'tjd||ddkr5t |\}}||g|dd<||||fS t|\}}n'#tj$rt|\}}YnwxYw||g|dd<|||r|ddkr|j tj d|dj dkr |d|dd<|rc|ddkrW|tt|dd\}}|||r |ddkW||fS) z] domain = dot-atom / domain-literal / obs-domain obs-domain = atom *("." atom)) Nrzexpected domain but found '{}'rirz(domain is not a dot-atom (contains CFWS)rsr?)rnrBrCrr#rarjr/rOrIrRrVrur)rrJrRrDs r get_domainrls XXF F Qx;   <% , 3 3E : :<< < Qx3)%00 u  E"1"I eu}'#E** uu  "''' uuu' 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 rrz"add-spec local part with no domainzaddress-at-symbolr?N)rwr^r/rRrrrrl)rrOrDs r get_addr_specrns I!%((LE5 U  E!HOO  !; 0"2"2 3 3 3% ]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). rrr?Nrz(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]rBrCr/ ListSeparatorrr#raRouteComponentMarkerrlr)r obs_routerDs r get_obs_routerts 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) rRrBrCr/rr#rarrRrrnrtrV)r angle_addrrDs rget_angle_addrrys 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)rrWr.rR)rr%rDs 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 '{}'rv) rArBrCrr#rarUr{r/ry)r name_addrrRrDs r get_name_addrr~0sa 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|tjVdSr5)rrrrYs rr\zget_mailbox..]sC 3 3 a3 4 4 3 3 3 3 3 3rr)) rar~rr#rnrarrhrur/)rr(rDs r get_mailboxrNs  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`r?N)rjrUr/rrW)rrinvalid_mailboxrDs rget_invalid_mailboxrcs%&&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-listrrxr)r?)r,rr/rr#rBrCrRrVrrrur.rq)r mailbox_listrDrRr(s rget_mailbox_listrus==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-listrzgroup-list with empty entries) r4rRr/rrrBrCrr@rr.rV)r group_listrRrDs rget_group_listrs 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 |ddkr'tjd ||t dd|dd}|r6|dtvr't|\}}||||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rrpz8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorr?Nrzgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) r:r{rr#rar/rrrRrrBrC)rr#rDs r get_grouprs GGE#E**LE5 9E!HOO%'**0&--99 9 LL LLs$CDDEEE !""IE  qS ]3(:;;<<<eABBi!%((LE5 LL ' V7 $&& ' ' ' Qx3% 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 '{}')r!rrr#rrar/)rrrDs r get_addressrs"iiGA '' uu  "AAA A&u--LE55& A A A)188??AA A A5A  NN5 E>rct}|rv 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-|tdd |d d}|v||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-listrxr)rr?)rrr/rr#rBrCrRrVrr!rrur.r)r address_listrDerrrRr(s rget_address_listr s==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      c3C D D E E E!""IEQ (R  s'<G FGGct}|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 CFWSr9rr?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)rrRr/rHeaderMissingRequiredValuerBrCr$rrarrrr)r mime_versionrDrs rparse_mime_versionrL s ==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. rrr`r?N)rrUr/rrW)rinvalid_parameterrDs rget_invalid_parameterr s)** ,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_matcherrr#rar#r@rr )rr:rs r get_ttextr s 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 '{}') rrBrCr/ TOKEN_ENDSrr#rar)rmtokenrDs r get_tokenr sWWF 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_matcherrr#rar#r@rr rr:rs 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. rr) rrBrCr/ATTRIBUTE_ENDSrr#rarrrrDs 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_matcherrr#rar#r@rr rs 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. rr) rrBrCr/EXTENDED_ATTRIBUTE_ENDSrr#rarrs 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-markerr?Nz$Expected section number but found {}r90z§ion numberhas an invalid leading 0r) rrr#rar/rr$rRInvalidHeaderErrorrr)rrrs r get_sectionr siiG 0E!HOO%&E&L&L(-'/'/00 0 NN=&677888 !""IE @a((**@%'117@@ @ F E!H$$&&%(abb  E!H$$&&ayCFcMMv8:' ( ( ) ) )[[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) rrr#rBrCrarFrr/)rvrRrDs r get_valuer< s A P%&NOOO F Qx;   @%'006v@@ @ Qx3(// uu-e44 u Hbqb HHUOOO e8Orc4 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 |jdkrc|r |ddkr)||||r J||}||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} |rK|dt2vrt5|\}}nt7|\}}| ||K| }nt)|\}}||||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. rrz)Parameter contains name ({}) but no valuerTzIncomplete parameterzextended-parameter-markerr?N=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 delimitersrrz=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231 delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {})rrr/rRrrrarrr#rrrBrCrFrrrrrurrrrrr%r r6) rrrDrRrappendtoqstring inner_value semi_validrtrs r get_parameterrR 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 " " "$ ''%''y!%<  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~~ uu+E22 u HHUOOO    '' u OOE%y %<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 reminaing 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. Nrrzparameter entry with no contentzinvalid parameter {!r}rxrz)parameter with invalid trailing text {!r}rr?)rrr/rr#rBrCrRrrrarur.r)rmime_parametersrDrrRrs 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 rrr`r?Nr)rUr/rrWr) tokenlistrrDs r_find_mime_parametersr 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}r/zInvalid content typezcontent-type-separatorr?Nz'Expected content subtype but found {!r}rz 022 3 3 3  '' uu  " V7 6 = =e D DFF G G GeU+++   LL E!HOO V7 "$$ % % %  0 !% / / / [&&((..00EN LLs$<==>>> !""IE '' uu  " V7 5 < r@rCrFrIrLrOrSrWr^r[rcrgrjrlrnrtryr{r~rrrrrrrrrrrrrrrrrrrrrrrrNrrrs CCH&%%%%%''''''&&&&&&88888888888888 ////////EEEEEEEE888888))))))((((((  c%jjCCHHn 3  sN CCHH$ U# E "cc#hh . _ E " S(33s883HHHNNNNNfNNNh@,@,@,@,@,@,@,@,FDDDDD)DDDKKKKKIKKK\]]]]]Y]]]~9 #####"###9 I , , , , ,) , , ,#####9###6 - - - - -| - - -!4CCCCC)CCC&%%%%%i%%%2 ? ? ? ? ?) ? ? ?%%%%% %%%"$$$$$I$$$*"""""y"""6 >DDDDDyDDD!!!!!i!!!6;;;;;Y;;;:::::Y:::i !!!!!)!!! y@"""""9""" $2$2$2$2$2&$2$2$2N        F     I   ) 8%%%%%y%%% ##### ###i     I   ?.?.?.?.?.Y?.?.?.D%%%%%y%%%$*1 i      )   Y20+0+0+0+0+s0+0+0+f         . . . . .H . . .     -   &mC c#344 $}S*BCC & :,,RWWS\\::;;A " 9#3#3GGItF++33C==$?$?@@@E" #566>#I$4$4GGJV,,44S>>%@%@AAAF'RZ (8(8GGN##D0088TBB)D)DEEEJ&0bj1A1AGG #$$,,$(1118T1B1B2D2D'E'EEJ$;;;JJJ>***:   ***X555n"    )))02      $$   &   >$$$L%%%N-!-!-!^(%!%!%!N"""H ' ' ' R,,,\   <*"""$666r###J<:444|BBBH$$$$&.&.$,<,IIIV222h777 777r<r