ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ???????????????????????????????????? ???????????????????????????????????? ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ???????????????????????????????????? ???????????????????????????????????? ֦iE dZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z e dZee d zZe d ZeezZee d z Zee d z Zee d ze d z ZeezZee dzZeezZee dz ZddhZeezZdZdZdZej<dej>ej@zZ!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,Z-Gd,d-e$Z.Gd.d/e#Z/Gd0d1e#Z0Gd2d3e#Z1Gd4d5e#Z2Gd6d7e#Z3Gd8d9e#Z4Gd:d;e#Z5Gd<d=e#Z6Gd>d?e#Z7Gd@dAe#Z8GdBdCe#Z9GdDdEe#Z:GdFdGe#Z;GdHdIe#Z<GdJdKe#Z=GdLdMe#Z>GdNdOe&Z?GdPdQe#Z@GdRdSe#ZAGdTdUe#ZBGdVdWe#ZCGdXdYeCZDGdZd[e#ZEGd\d]e#ZFGd^d_e#ZGGd`dae#ZHGdbdce#ZIGdddeeIZJGdfdgeIZKGdhdie#ZLGdjdke#ZMGdldme#ZNGdndoeNZOGdpdqeOZPGdrdse#ZQGdtdueRZSGdvdweSZTGdxdyeSZUGdzd{eTZVGd|d}e jZXeUd d~ZYeUddZZdeZ_[deZ_\eUddZ]ej<djdjejZaej<djejdjejZdej<djZfej<djejdjejZgej<djejdjejZhej<djejdjejZidZjdZkdZlddZmdZndZodZpdZqdZrdZsdZtdZudZvdZwdZxdZydZzdZ{dZ|dZ}dZ~dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZy)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*'%%  cXt|jddjddS)z;Escape dquote and backslash for use within a quoted-string.\\\"z\"strreplacevalues 3/usr/lib64/python3.12/email/_header_value_parser.pymake_quoted_pairsrcs& u:  dF + 3 3C ??cxt|jddjddjddS)z:Escape parenthesis and backslash for use within a comment.rrr\()\)rrs rmake_parenthesis_pairsrhs2 u:  dF + e WWS%01rc$t|}d|dS)Nr)r)rescapeds r quote_stringr ns&G wiq>rz =\? # literal =? [^?]* # charset \? # literal ? [qQbB] # literal 'q' or 'b', case insensitive \? # literal ? .*? # encoded word \?= # literal ?= ceZdZdZdZdZfdZdZfdZe dZ e dZ dZ e d Z e d Zd Zdd Zdd ZddZxZS) TokenListNTc2t||i|g|_yN)super__init__defects)selfargskw __class__s rr&zTokenList.__init__s $%"% rc2djd|DS)Nc32K|]}t|ywr$r.0xs r z$TokenList.__str__..,t!s1vtjoinr(s r__str__zTokenList.__str__sww,t,,,rchdj|jjt|SNz{}({})formatr+__name__r%__repr__r(r+s rr?zTokenList.__repr__s+t~~66"W-/1 1rc2djd|DS)Nr-c3NK|]}|js|jywr$rr0s rr3z"TokenList.value..s81qwws%%r6r8s rrzTokenList.valuesww8888rc<td|D|jS)Nc34K|]}|jywr$) all_defectsr0s rr3z(TokenList.all_defects..s04aAMM4)sumr'r8s rrEzTokenList.all_defectss040$,,??rc(|djSNr)startswith_fwsr8s rrJzTokenList.startswith_fwssAw%%''rc&td|DS)zATrue if all top level tokens of this part may be RFC2047 encoded.c34K|]}|jywr$) as_ew_allowed)r1parts rr3z*TokenList.as_ew_allowed..s7$$4%%$rF)allr8s rrMzTokenList.as_ew_alloweds7$777rcNg}|D]}|j|j|Sr$)extendcomments)r(rRtokens rrRzTokenList.commentss&E OOENN +rct||S)Npolicy)_refold_parse_treer(rVs rfoldzTokenList.folds!$v66rc:t|j|y)Nindent)printppstrr(r\s rpprintzTokenList.pprints djjj'(rcDdj|j|S)Nr r[)r7_ppr_s rr^zTokenList.ppstrsyy011rc#~Kdj||jj|j|D]A}t |ds|dj|z&|j |dzEd{C|j rdj|j }nd}dj||y7Ew)Nz{}{}/{}(rbz* !! invalid element in token list: {!r}z z Defects: {}r-z{}){})r=r+r> token_typehasattrrbr')r(r\rSextras rrbz TokenList._pps  NN # # OO E5%(!55;VE]CD!99VF]333  <<"))$,,7EEnnVU++ 4sA3B=5B;6AB=r-)r> __module__ __qualname__rdsyntactic_breakew_combine_allowedr&r9r?propertyrrErJrMrRrYr`r^rb __classcell__r+s@rr"r"sJO-199@@(88 7)2,rr"c,eZdZedZedZy)WhiteSpaceTokenListcyN r8s rrzWhiteSpaceTokenList.valuerc`|Dcgc]}|jdk(s|j c}Scc}w)Ncomment)rdcontentr(r2s rrRzWhiteSpaceTokenList.commentss)#'C4a1<<+B 4CCC++N)r>rhrirlrrRrtrrrprps* DDrrpceZdZdZy)UnstructuredTokenList unstructuredNr>rhrirdrtrrr|r|sJrr|ceZdZdZy)PhrasephraseNr~rtrrrrJrrceZdZdZy)WordwordNr~rtrrrrJrrceZdZdZy)CFWSListcfwsNr~rtrrrrrrrceZdZdZy)AtomatomNr~rtrrrrrrrceZdZdZdZy)TokenrSFN)r>rhrird encode_as_ewrtrrrrs JLrrceZdZdZdZdZdZy) EncodedWord encoded-wordN)r>rhrirdctecharsetlangrtrrrrsJ CG Drrc@eZdZdZedZedZedZy) QuotedString quoted-stringcL|D]}|jdk(s|jcSyNbare-quoted-stringrdrrys rrxzQuotedString.contents"A||33wwrcg}|D]G}|jdk(r|jt|-|j|jIdj |S)Nrr-)rdappendrrr7)r(resr2s r quoted_valuezQuotedString.quoted_valuesNA||33 3q6" 177#  wws|rcL|D]}|jdk(s|jcSyrrr(rSs rstripped_valuezQuotedString.stripped_values%E#77{{"rN)r>rhrirdrlrxrrrtrrrrsA J  ##rrc&eZdZdZdZedZy)BareQuotedStringrcDtdjd|DS)Nr-c32K|]}t|ywr$r/r0s rr3z+BareQuotedString.__str__..s#9DqCFDr5)r r7r8s rr9zBareQuotedString.__str__sBGG#9D#99::rc2djd|DS)Nr-c32K|]}t|ywr$r/r0s rr3z)BareQuotedString.value..r4r5r6r8s rrzBareQuotedString.valueww,t,,,rN)r>rhrirdr9rlrrtrrrr s %J;--rrc<eZdZdZdZdZedZedZy)Commentrwc djtdg|Dcgc]}|j|c}dgggScc}w)Nr-rr)r7rGquoterys rr9zComment.__str__sKwws E489DqTZZ]D9 E " #$ $9s>c|jdk(r t|St|jddjddjddS)Nrwrrrrrr)rdrr)r(rs rrz Comment.quote"sR   y (u: 5z!!$/77"%u..5g"%u/. .rc2djd|DS)Nr-c32K|]}t|ywr$r/r0s rr3z"Comment.content..+r4r5r6r8s rrxzComment.content)rrc|jgSr$)rxr8s rrRzComment.comments-s ~rN) r>rhrirdr9rrlrxrRrtrrrrs9J$.--rrc@eZdZdZedZedZedZy) AddressListz address-listcL|Dcgc]}|jdk(s|c}Scc}w)Naddressrdrys r addresseszAddressList.addresses5%;4a1<<#:4;;;!!c(td|DgS)Nc3RK|]}|jdk(r|j!ywrNrd mailboxesr0s rr3z(AddressList.mailboxes..;s'>!Q\\9%<KK!%'rGr8s rrzAddressList.mailboxes9!>!>?AC Crc(td|DgS)Nc3RK|]}|jdk(r|j!ywrrd all_mailboxesr0s rr3z,AddressList.all_mailboxes..@s'>!Q\\9%<OO!rrr8s rrzAddressList.all_mailboxes>rrN)r>rhrirdrlrrrrtrrrr1sEJ <<CCCCrrc@eZdZdZedZedZedZy)AddressrcF|djdk(r|djSy)Nrgrouprd display_namer8s rrzAddress.display_nameHs) 7   (7'' ' )rcx|djdk(r|dgS|djdk(rgS|djSNrmailboxinvalid-mailboxrr8s rrzAddress.mailboxesMsH 7   *G9  !W  #4 4IAw   rc|djdk(r|dgS|djdk(r|dgS|djSrrr8s rrzAddress.all_mailboxesUsO 7   *G9  !W  #4 4G9 Aw$$$rN)r>rhrirdrlrrrrtrrrrDsAJ ((!!%%rrc0eZdZdZedZedZy) MailboxList mailbox-listcL|Dcgc]}|jdk(s|c}Scc}w)Nrrrys rrzMailboxList.mailboxesarrcH|Dcgc]}|jdvr|c}Scc}w)N)rrrrys rrzMailboxList.all_mailboxeses2?4a||==4? ??sNr>rhrirdrlrrrtrrrr]s-J <<??rrc0eZdZdZedZedZy) GroupList group-listcL|r|djdk7rgS|djSNrrrr8s rrzGroupList.mailboxesos+tAw))^;IAw   rcL|r|djdk7rgS|djSrrr8s rrzGroupList.all_mailboxesus+tAw))^;IAw$$$rNrrtrrrrks-J !! %%rrc@eZdZdZedZedZedZy)GrouprcH|djdk7rgS|djSNrrr8s rrzGroup.mailboxess) 7   -IAw   rcH|djdk7rgS|djSrrr8s rrzGroup.all_mailboxess) 7   -IAw$$$rc |djSrI)rr8s rrzGroup.display_namesAw###rN)r>rhrirdrlrrrrtrrrr|sAJ !! %% $$rrc`eZdZdZedZedZedZedZedZ y)NameAddr name-addrc>t|dk(ry|djSNr)lenrr8s rrzNameAddr.display_names t9>Aw###rc |djSN local_partr8s rrzNameAddr.local_partsBx"""rc |djSrdomainr8s rrzNameAddr.domainsBxrc |djSr)router8s rrzNameAddr.routesBx~~rc |djSr addr_specr8s rrzNameAddr.addr_specsBx!!!rN r>rhrirdrlrrrrrrtrrrrsiJ $$ ##""rrcPeZdZdZedZedZedZedZy) AngleAddrz angle-addrcL|D]}|jdk(s|jcSyN addr-spec)rdrrys rrzAngleAddr.local_parts"A||{*||#rcL|D]}|jdk(s|jcSyrrdrrys rrzAngleAddr.domains!A||{*xxrcL|D]}|jdk(s|jcSy)N obs-route)rddomainsrys rrzAngleAddr.routes"A||{*yy rc|D]O}|jdk(s|jr|jcSt|j|jzcSy)Nrz<>)rdrrr rys rrzAngleAddr.addr_specsFA||{*<<;;&' 5 CC rN) r>rhrirdrlrrrrrtrrrrsUJ $$   !! rrc eZdZdZedZy)ObsRouterc`|Dcgc]}|jdk(s|j c}Scc}w)Nrrrys rrzObsRoute.domainss)"&C$Q!,,(*B$CCCrzN)r>rhrirdrlrrtrrrrsJ DDrrc`eZdZdZedZedZedZedZedZ y)MailboxrcF|djdk(r|djSyNrrrr8s rrzMailbox.display_names) 7   ,7'' ' -rc |djSrIrr8s rrzMailbox.local_partAw!!!rc |djSrIrr8s rrzMailbox.domainsAw~~rcF|djdk(r|djSyr )rdrr8s rrz Mailbox.routes' 7   ,7==  -rc |djSrIrr8s rrzMailbox.addr_specsAw   rNrrtrrr r siJ ((""!!!!rr c0eZdZdZedZexZxZxZZ y)InvalidMailboxrcyr$rtr8s rrzInvalidMailbox.display_namerNrrtrrrrs/"J /;:J::%)rrc0eZdZdZdZefdZxZS)DomainrFcRdjt|jSNr-r7r%rsplitr@s rrz Domain.domainwwuw}**,--r)r>rhrirdrMrlrrmrns@rrrsJM ..rrceZdZdZy)DotAtomdot-atomNr~rtrrrrsJrrceZdZdZdZy) DotAtomTextz dot-atom-textTNr>rhrirdrMrtrrr r  s  JMrr ceZdZdZdZy) NoFoldLiteralzno-fold-literalFNr!rtrrr#r#s "JMrr#cTeZdZdZdZedZedZedZedZ y)AddrSpecrFc |djSrIrr8s rrzAddrSpec.local_partr rc>t|dkry|djS)Nr)rrr8s rrzAddrSpec.domains t9q=Bxrct|dkr|djS|djj|djz|djjzS)Nr(rrr)rrrstriplstripr8s rrzAddrSpec.value$sU t9q=7== Aw}}##%d1gmm3DGMM4H4H4JJJrct|j}t|t|tz kDrt |j}n |j}|j |dz|j zS|S)N@)setrr DOT_ATOM_ENDSr r)r(namesetlps rrzAddrSpec.addr_spec*s_doo& w<#gm34 4doo.BB ;; "8dkk) ) rN) r>rhrirdrMrlrrrrrtrrr%r%s\JM "" KK rr%ceZdZdZdZy) ObsLocalPartzobs-local-partFNr!rtrrr3r36s !JMrr3c@eZdZdZdZedZefdZxZS) DisplayNamez display-nameFct|}t|dk(r |jS|djdk(r|j dneOC$Q""f,47I.Q %%/R##v-48Y/R ''61|D$5$566t; ;7= r) r>rhrirdrkrlrrrmrns@rr5r5<s4J $!!rr5c4eZdZdZdZedZedZy) LocalPartz local-partFcb|djdk(r|djS|djS)Nrr)rdrrr8s rrzLocalPart.valueqs2 7   07'' '7== rctg}t}d}|dtgzD]}|jdk(r|r2|jdk(r#|djdk(rt|dd|d<t|t}|r?|jdk(r0|djdk(r|j t|ddn|j ||d}|}t|dd}|j S)NFrrdotrr)DOTrdr"r8rr)r(rlast last_is_tltokis_tls rrzLocalPart.local_partxse 7cU?C~~'s~~6H''61#D"I.BsI.E$//U2F%%/ 9SW-. 3r7DJ#Ab "yyrN)r>rhrirdrMrlrrrtrrr=r=ls2JM !! rr=c@eZdZdZdZefdZedZxZS) DomainLiteralzdomain-literalFcRdjt|jSrrr@s rrzDomainLiteral.domainrrcL|D]}|jdk(s|jcSy)Nptextrrys ripzDomainLiteral.ips!A||w&wwr) r>rhrirdrMrlrrKrmrns@rrGrGs3!JM ..rrGceZdZdZdZdZy) MIMEVersionz mime-versionN)r>rhrirdmajorminorrtrrrMrMsJ E ErrMc<eZdZdZdZdZdZedZedZ y) Parameter parameterFus-asciic<|jr|djSdSr) sectionednumberr8s rsection_numberzParameter.section_numbers"&tAw~~6Q6rc|D]n}|jdk(r|jcS|jdk(s0|D]:}|jdk(s|D]#}|jdk(s|jcccS<py)Nrrrr-)rdrrs r param_valuezParameter.param_valuesxE7*+++?2"E''+??%*E$//7:',';'; ;&+# rN) r>rhrirdrUextendedrrlrWrYrtrrrQrQs<JIHG 77   rrQceZdZdZy)InvalidParameterinvalid-parameterNr~rtrrr\r\s$Jrr\c eZdZdZedZy) Attribute attributecd|D]+}|jjds|jcSy)Nattrtext)rdendswithrrs rrzAttribute.stripped_values*E((4{{"rNr>rhrirdrlrrtrrr_r_sJ ##rr_ceZdZdZdZy)SectionsectionN)r>rhrirdrVrtrrrfrfs J Frrfc eZdZdZedZy)Valuerc|d}|jdk(r|d}|jjdr |jS|jS)Nrrr)rr`zextended-attribute)rdrcrrrs rrzValue.stripped_valuesPQ   v %GE    $ $D F'' 'zzrNrdrtrrririsJ rric*eZdZdZdZedZdZy)MimeParametersmime-parametersFc#lKi}|D]w}|jjds|djdk7r2|djj}||vrg||<||j |j |fy|j D]\}}t|td}|dd}|j}|jsRt|dkDrD|dddk(r9|ddjj tjd|dd}g}d}|D]\} } | |k7ri| js/| jj tjdG| jj tjd|dz }| j} | jrv t j"j%| } | j'|d } t-j.| r.| jj tj0 |j | d j5|} || fy#t(t*f$r| j'd d } YwxYw#t*$r$t j"j3| d } YwxYww)NrRrr`)keyrz.duplicate parameter name; duplicate(s) ignoredz+duplicate parameter name; duplicate ignoredz(inconsistent RFC2231 parameter numberingsurrogateescaperSzlatin-1)encodingr-)rdrcrstriprrWitemssortedrrrZrr'rInvalidHeaderDefectrYurllibparseunquote_to_bytesdecode LookupErrorUnicodeEncodeErrorr_has_surrogatesUndecodableBytesDefectunquoter7) r(paramsrSnameparts first_paramr value_partsirWparamrs rrzMimeParameters.paramssE##,,[9Qx""k18>>'')D6!!t 4L  !5!5u = >"<<>KD%5jm4E(1+K!))G''CJN8A;!#!HQK''..v/I/IH0JK!"1IEKA).%!Q&!>> ,,V-G-GI.KL  ,,V-G-GF.HIQ))>>R & = =e DP$)LL:K$LE!007!MM001N1N1PQ""5)C*/DGGK(E+ g*R!,-?@P %*LL=N$OE P.P!' 4 4UY 4 O PsIF6J49JI+A2J4!J>J4JJ4*J1.J40J11J4c g}|jD]C\}}|r+|jdj|t|3|j|Edj |}|rd|zSdS)N{}={}z; rsr-)rrr=r r7)r(rrrs rr9zMimeParameters.__str__2se;;KD% gnnT<3FGH d# ' 6"%sV|-2-rN)r>rhrirdrjrlrr9rtrrrlrls&"JO CCJ.rrlc eZdZdZedZy)ParameterizedHeaderValueFc`t|D]}|jdk(s|jcSiS)Nrm)reversedrdrrs rrzParameterizedHeaderValue.paramsCs0d^E#44||#$ rN)r>rhrirjrlrrtrrrr=sO rrceZdZdZdZdZdZy) ContentTypez content-typeFtextplainN)r>rhrirdrMmaintypesubtypertrrrrKsJMHGrrceZdZdZdZdZy)ContentDispositionzcontent-dispositionFN)r>rhrirdrMcontent_dispositionrtrrrrRs&JMrrceZdZdZdZdZy)ContentTransferEncodingzcontent-transfer-encodingF7bitN)r>rhrirdrMrrtrrrrXs,JM CrrceZdZdZdZy) HeaderLabelz header-labelFNr!rtrrrr^s JMrrceZdZdZdZdZy)MsgIDzmsg-idFc2t||jzSr$)rlineseprXs rrYz MsgID.foldgs4y6>>))rN)r>rhrirdrMrYrtrrrrcsJM*rrceZdZdZy) MessageIDz message-idNr~rtrrrrlsJrrceZdZdZy)InvalidMessageIDzinvalid-message-idNr~rtrrrrps%JrrceZdZdZy)HeaderheaderNr~rtrrrrtrrrcreZdZdZdZdZfdZfdZdZe dZ d fd Z dZ e dZ d ZxZS) TerminalTcDt|||}||_g|_|Sr$)r%__new__rdr')clsrrdr(r+s rrzTerminal.__new__s&wsE*$  rchdj|jjt|Sr;r<r@s rr?zTerminal.__repr__s&t~~668H8JKKrcbt|jjdz|jzy)N/)r]r+r>rdr8s rr`zTerminal.pprints" dnn%%+doo=>rc,t|jSr$)listr'r8s rrEzTerminal.all_defectssDLL!!rc dj||jj|jt||j sdgSdj|j gS)Nz {}{}/{}({}){}r-z {})r=r+r>rdr%r?r')r(r\r+s rrbz Terminal._ppsg&&  NN # # OO G  llB   ). T\\(B  rcyr$rtr8s rpop_trailing_wszTerminal.pop_trailing_wsrrcgSr$rtr8s rrRzTerminal.commentss rc0t||jfSr$)rrdr8s r__getnewargs__zTerminal.__getnewargs__s4y$//**rrg)r>rhrirMrkrjrr?r`rlrErbrrRrrmrns@rrr|sZMO L?""+rrc"eZdZedZdZy)WhiteSpaceTerminalcyrrrtr8s rrzWhiteSpaceTerminal.valuerurc |xr |dtvSrIWSPr8s rrJz!WhiteSpaceTerminal.startswith_fwss&Q3&rNr>rhrirlrrJrtrrrrs 'rrc"eZdZedZdZy) ValueTerminalc|Sr$rtr8s rrzValueTerminal.values rcy)NFrtr8s rrJzValueTerminal.startswith_fwssrNrrtrrrrs rrc"eZdZedZdZy)EWWhiteSpaceTerminalcyrrtr8s rrzEWWhiteSpaceTerminal.valuesrcyrrtr8s rr9zEWWhiteSpaceTerminal.__str__srN)r>rhrirlrr9rtrrrrs rrceZdZdZy)_InvalidEwErrorz1Invalid encoded word found while parsing headers.N)r>rhri__doc__rtrrrrs;rrr@,zlist-separatorFr-zroute-component-markerz([{}]+)r-z[^{}]+z[\x00-\x20\x7F]ct|}|r.|jjtj|t j |r/|jjtjdyy)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)_non_printable_finderr'rrNonPrintableDefectrr|r})xtextnon_printabless r_validate_xtextrsc+51N V66~FG U# V:: 8: ;$rc"t|d^}}g}d}d}tt|D]6}||dk(r |rd}d}nd}|rd}n |||vrn|j||8dz}dj |dj ||dg|z|fS)akScan printables/quoted-pairs until endchars and return unquoted ptext. This function turns a run of qcontent, ccontent-without-comments, or dtext-with-quoted-printables into a single string by unquoting any quoted printables. It returns the string, the remaining value, and a flag that is True iff there were any quoted printables decoded. rFrTr-N) _wsp_splitterrangerrr7)rendcharsfragment remaindervcharsescapehad_qpposs r_get_ptext_to_endcharsrs)2Hy F F FS]# C=D  F c]h &  hsm$$Ag 776?BGGXcd^$4y$@A6 IIrcr|j}t|dt|t|z d}||fS)zFWS = 1*WSP This isn't the RFC definition. We're using fws to represent tokens where folding can be done, but when we are parsing the *un*folding has already been done so we don't need to watch out for CRLF. Nfws)r+rr)rnewvaluers rget_fwsrs:||~H U#@ @ABioodA.OC) eABi%% 0 7 7 >@ @ WWY F F aq Yq Y #!<<a0yDj4 399;! &44 ,. / BF GGI E@'*zz$*t2C'D$gtWBJBGJJg  7c>!$-KE4 IIe  )$2 e]3 %wwy!  q$ &44 <> ? u9)  !@ / 6 6rvv >@ @@s I 4I>c t}|r[|dtvr t|\}}|j|.d}|j dr t |d\}}d}t |dkDrB|djdk7r0|jjtjdd}|r2t |d kDr$|d jd k(rt|dd|d<|j|t|d ^}}|r(tj!|r|j#d^}}t%|d}t'||j|d j)|}|r[|S#t$rd}Ytj$rYwxYw) aOunstructured = (*([FWS] vchar) *WSP) / obs-unstruct obs-unstruct = *((*LF *CR *(obs-utext) *LF *CR)) / FWS) obs-utext = %d0 / obs-NO-WS-CTL / LF / CR obs-NO-WS-CTL is control characters except WSP/CR/LF. So, basically, we have printable runs, plus control characters or nulls in the obsolete syntax, separated by whitespace. Since RFC 2047 uses the obsolete syntax in its specification, but requires whitespace on either side of the encoded words, I can see no reason to need to separate the non-printable-non-whitespace from the printable runs if they occur, so we parse this into xtext tokens separated by WSP tokens. Because an 'unstructured' value must by definition constitute the entire value, this 'get' routine does not return a remaining value, only the parsed TokenList. rTrutextrrz&missing whitespace before encoded wordFrrr-)r|rrrrrrrdr'rrurrrrrfc2047_matchersearch partitionrrr7)rr}rSvalid_ewhave_wsrDrrs rget_unstructuredrWs.)*L  8s?"5>LE5    &    D ! /w? u|$q(#B'22e;$,,33F4N4ND5FG"'s<014#B'22nD+?(,e,5 R(##E*'q1i ..s3#ood3OC)c7+E" "Q R A# ! **  s E++ F 8F  F cXt|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()rJ)rrrrrJ_s r get_qp_ctextrs4-UD9OE5! ug .EE %<rcXt|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. rrJ)rrrrs r get_qcontentrs4-UC8OE5! % )EE %<rct|}|s$tjdj||j }|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_matcherrrr=rrrr)rmrs r get_atextrsk e$A %% + 2 25 9; ; GGIE #e*+ E % )EE %<rcN|r|ddk7r$tjdj|t}|dd}|r'|ddk(rt |\}}|j ||r|ddk7r|dt vrt|\}}n|dddk(rd} t|\}}|jj tjd d }|rSt|dkDrE|d jd k(r3|d jdk(r!t|d d |d <nt |\}}|j ||r |ddk7r|s2|jj tjd||fS||ddfS#tj$rt |\}}YwxYw)zbare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE A quoted-string without the leading or trailing white space. Its value is the text between the quote marks, with whitespace preserved and quoted pairs decoded. rrzexpected '"' but found '{}'rNrrFz!encoded word inside quoted stringTrrrrz"end of header inside quoted string)rrr=rrrrrrr'rurrdr)rbare_quoted_stringrSrs rget_bare_quoted_stringrs E!HO%% * 1 1% 8: :)+ !"IE qS#E* u!!%( E!HO 8s?"5>LE5 2AY$ H 3/6 u"**11&2L2L739: C 23a7&r*55>*2.99^K-A*2..7&r*(.LE5!!%(+ E!HO, ""))&*D*D 0+2 3!5(( uQRy ((!** 3+E2 u 3s>F!F$#F$c|r,|ddk7r$tjdj|t}|dd}|rc|ddk7r[|dtvrt |\}}n%|ddk(rt |\}}nt|\}}|j||r |ddk7r[|s2|jjtjd||fS||ddfS)zcomment = "(" *([FWS] ccontent) [FWS] ")" ccontent = ctext / quoted-pair / comment We handle nested comments here, and quoted-pair in our qp-ctext routine. rrzexpected '(' but found '{}'rNrzend of header inside comment) rrr=rrr get_commentrrr'ru)rrwrSs rrrs  qS%% ) 0 0 79 9iG !"IE E!HO 8s?"5>LE5 1X_&u-LE5'.LE5u E!HO v99 * , -~ E!"I rct}|rR|dtvrG|dtvrt|\}}nt |\}}|j ||r |dtvrG||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)r CFWS_LEADERrrrr)rrrSs rget_cfwsrsg :D E!H + 8s?"5>LE5&u-LE5 E E!H + ;rc t}|r*|dtvrt|\}}|j|t |\}}|j||r*|dtvrt|\}}|j|||fS)zquoted-string = [CFWS] [CFWS] 'bare-quoted-string' is an intermediate class defined by this parser and not by the RFC grammar. It is the quoted string without any attached CFWS. r)rrrrr)r quoted_stringrSs rget_quoted_stringr s!NM q[( uU#)%0LE5 q[( uU# % rct}|r*|dtvrt|\}}|j||r/|dtvr$t j dj||jdr t|\}}nt|\}}|j||r*|dtvrt|\}}|j|||fS#t j $rt|\}}YdwxYw)zPatom = [CFWS] 1*atext [CFWS] An atom could be an rfc2047 encoded word. rzexpected atom but found '{}'r) rrrr ATOM_ENDSrrr=rrr)rrrSs rget_atomr-s 6D q[( u E qY&%% * 1 1% 8: :  ,+E2LE5 !' uKK q[( u E ;&& ,%U+LE5 ,s:C!C<;C<ct}|r |dtvr$tjdj ||r\|dtvrQt |\}}|j ||r"|ddk(r|j t|dd}|r |dtvrQ|dtur'tjdj d|z||fS)z( dot-text = 1*atext *("." 1*atext) rz8expected atom at a start of dot-atom-text but found '{}'r rNrz4expected atom at end of dot-atom-text but found '{}')r r rrr=rrrA)r dot_atom_textrSs rget_dot_atom_textrHs MM E!H )%%'++16%=: : E!HI- ' uU# U1X_   %!"IE E!HI- RC%%'#VCI.0 0 % rct}|dtvrt|\}}|j||j dr t |\}}nt|\}}|j||r*|dtvrt|\}}|j|||fS#t j$rt|\}}YdwxYw)z dot-atom = [CFWS] dot-atom-text [CFWS] Any place we can have a dot atom, we could instead have an rfc2047 encoded word. rr) rrrrrrrrr)rdot_atomrSs r get_dot_atomr[s yH Qx; u  4+E2LE5 )/ u OOE q[( u U?&& 4-U3LE5 4sB%%!C C c(|dtvrt|\}}nd}|stjd|ddk(rt |\}}n=|dt vr$tjdj |t|\}}||g|dd||fS)aword = atom / quoted-string Either atom or quoted-string may start with CFWS. We have to peel off this CFWS first to determine which type of word to parse. Afterward we splice the leading CFWS, if any, into the parsed sub-token. If neither an atom or a quoted-string is found before the next special, a HeaderParseError is raised. The token returned is either an Atom or a QuotedString, as appropriate. This means the 'word' level of the formal grammar is not represented in the parse tree; this is because having that extra layer when manipulating the parse tree is more confusing than it is helpful. rNz5Expected 'atom' or 'quoted-string' but found nothing.rz1Expected 'atom' or 'quoted-string' but found '{}')rrrrr SPECIALSr=r)rleaderrSs rget_wordrts  Qx;   %% CE E Qx}(/ u qX %%'77=ve}F F  u Hbq %<rct} t|\}}|j||r|dtvr|ddk(rI|jt|j jtjd|dd}n t|\}}|j||r |dtvr||fS#tj$r1|j jtj dYwxYw#tj$rL|dtvr=t|\}}|j jtjdnYwxYw)a phrase = 1*word / obs-phrase obs-phrase = word *(word / "." / CFWS) This means a phrase can be a sequence of words, periods, and CFWS in any order as long as it starts with at least one word. If anything other than words is detected, an ObsoleteHeaderDefect is added to the token's defect list. We also accept a phrase that starts with CFWS followed by a dot; this is registered as an InvalidHeaderDefect, since it is not supported by even the obsolete grammar. zphrase does not start with wordrr zperiod in 'phrase'rNzcomment found without atom) rrrrrr'ru PHRASE_ENDSrAObsoleteHeaderDefectrr)rrrSs r get_phrasersMXF0 u e E!HK/ 8S= MM#  NN ! !&"="=$#& '!"IE ' u MM% ! E!HK/" 5=)  " "0f88 -/ 00** 8{*#+E?LE5NN))&*E*E4+677 s%B; D;AC?>C?AE! E!ct}d}|r|dtvrt|\}}|s$tjdj | t |\}}||g|dd|j||r|ddk(s |dtvrtt||z\}}|jdk(r/|jjtjdn.|jjtj d||d< |j"j%d||fS#tj$rK t|\}}n7#tj$r!|ddk7r |dtvrt}YnwxYwY6wxYw#t&$r4|jjtj(d Y||fSwxYw) z= local-part = dot-atom / quoted-string / obs-local-part Nrz"expected local-part but found '{}'rinvalid-obs-local-partz@ @ #E* uHbq e %(D.E!HK$? 23z?U3J K  $ $(@ @    % %f&@&@N'P Q    % %f&A&A>'@ A& 1 >( u 1  " "  #E?LE5&& Qx4E!H $;KE  * >!!&"@"@;#= > u >sHD6F6F EF1F  F F  FF7GGcNt}d}|rB|ddk(s |dtvr.|ddk(rM|r.|jjt j d|jt d}|dd}l|ddk(rT|jt|dd |dd}|jjt j d d}|r@|d jd k7r.|jjt j d  t|\}}d}|j||r|ddk(r!|dtvr.|s$t jdj||djd k(s2|djdk(rNt|dkDr@|djd k(r.|jjt j d|d jd k(s2|d jdk(rNt|dkDr@|djd k(r.|jjt j d|jrd|_||fS#tj$r|dtvrt|\}}Y{wxYw)z' obs-local-part = word *("." word) Frrr zinvalid repeated '.'TrNmisplaced-specialz/'\' character outside of quoted-string/ccontentrr@zmissing '.' between wordsz&expected obs-local-part but found '{}'rz!Invalid leading '.' in local partrz"Invalid trailing '.' in local partr)r3rr'rrrurArrdrrrrr=r)rr#last_non_ws_was_dotrSs rr r s"^N U1Xt^uQx{'B 8s?"&&--f.H.H*/,-  ! !# &"& !"IE  1Xt^  ! !-a0C#E F!"IE  " " ) )&*D*DB+D E"'   nR0;;uD  " " ) )&*D*D++- . +#E?LE5"'  e$7 U1Xt^uQx{'B8 %% 4 ; ;E BD Dq$$- 1  ( (& 0  ! # 1  ( (% /%%f&@&@ /'1 2r%%. 2  ) )6 1  ! # 2  ) )5 0%%f&@&@ 0'2 3$<! 5  -&& +Qx{*#E?LE5 +sI33-J$#J$ct|d\}}}t|d}|r.|jjt j dt |||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[]rJz(quoted printable found in domain-literal)rrr'rrrr)rrJrs r get_dtextr)sZ2%>E5& % )E  V88 68 9E %<rc|ry|jtjd|jtddy)NFz"end of input inside domain-literal]domain-literal-endT)rrrur)rdomain_literals r_check_for_early_dl_endr.+s? &44,./--ABC rcnt}|dtvrt|\}}|j||st j d|ddk7r$t j dj ||dd}t||r||fS|jtdd|dtvrt|\}}|j|t|\}}|j|t||r||fS|dtvrt|\}}|j|t||r||fS|ddk7r$t j d j ||jtdd |dd}|r*|dtvrt|\}}|j|||fS) zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] rzexpected domain-literal[z6expected '[' at start of domain-literal but found '{}'rNzdomain-literal-startr+z4expected ']' at end of domain-literal but found '{}'r,) rGrrrrrr=r.rrrr))rr-rSs rget_domain_literalr13s#_N Qx; ue$ %%&?@@ Qx3%%'!!'0 0 !"IEun5u$$--CDE Qx3u~ ue$U#LE5% un5u$$ Qx3u~ ue$un5u$$ Qx3%%'!!'0 0--ABC !"IE q[( ue$ 5  rc"t}d}|r|dtvrt|\}}|s$tjdj ||ddk(r+t |\}}||g|dd|j|||fS t|\}}|r|ddk(rtjd||g|dd|j||r|ddk(r|jjtjd|djd k(r|d|dd|rJ|ddk(rB|jtt|d d\}}|j||r |ddk(rB||fS#tj$rt|\}}YwxYw) z] domain = dot-atom / domain-literal / obs-domain obs-domain = atom *("." atom)) Nrzexpected domain but found '{}'r0r-zInvalid Domainr z(domain is not a dot-atom (contains CFWS)rr)rrrrrr=r1rrrr'rrdrA)rrrrSs r get_domainr3Zs XF F q[(   %% , 3 3E :< < Qx3)%0 u  E"1I eu}'#E* u qS%%&677 Hbq  MM% qSf99 68 9 !9  : -q F1IaC MM# #E!"I.LE5 MM% aC 5=!  " "' u'sE**!F FcNt}t|\}}|j||r|ddk7r2|jjt j d||fS|jt ddt|dd\}}|j|||fS)z( addr-spec = local-part "@" domain rr-z#addr-spec local part with no domainaddress-at-symbolrN)r%r$rr'rrurr3)rrrSs r get_addr_specr6s I!%(LE5 U E!HO  !;!; 1"3 4% ]3(;<=eABi(LE5 U e rct}|rw|ddk(s |dtvrd|dtvr t|\}}|j|n"|ddk(r|jt|dd}|r|ddk(rX|dtvrd|r|ddk7r$t j dj||jtt|dd\}}|j||r|ddk(r|jt|dd}|snw|dtvrt|\}}|j||snJ|ddk(r7|jtt|dd\}}|j||r |ddk(r|st j d|ddk7r$t j d j||jtdd ||ddfS) z obs-route = obs-domain-list ":" obs-domain-list = *(CFWS / ",") "@" domain *("," [CFWS] ["@" domain]) Returns an obs-route token with the appropriate sub-tokens (that is, there is no obs-domain-list in the parse tree). rrrNr-z(expected obs-route domain but found '{}'z%end of header while parsing obs-route:z4expected ':' marking end of obs-route but found '{}'zend-of-obs-route-marker) rrrr ListSeparatorrrr=RouteComponentMarkerr3r)r obs_routerSs r get_obs_router<s I U1Xs]eAh+&= 8{ "#E?LE5   U # 1X_   ] +!"IE U1Xs]eAh+&= E!HO%% 6 = =e DF F )*eABi(LE5 U E!HcM'ab   8{ "#E?LE5   U #  8s?   1 2%eABi0LE5   U # E!HcM %%&MNN Qx3%%(''-ve}6 6 ]3(ABC eABi rcxt}|r*|dtvrt|\}}|j||r|ddk7r$t j dj ||jtdd|dd}|rZ|ddk(rR|jtdd|jjt jd |dd}||fS t|\}}|j||r|ddk(r|dd}n.|jjt jd |jtdd|r*|dtvrt|\}}|j|||fS#tj $r t|\}}|jjt jd n;#tj $r%t j d j |wxYw|j|t|\}}YHwxYw) 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) rrrrrrr=rr'rur6r<r)r angle_addrrSs rget_angle_addrrAs4 J q[( u% E!HO%% 0 7 7 >@ @mC);<= !"IE qS--=>?!!&"<"< *#, -ab 5   ,$U+ ue qSab !!&"<"< 0#2 3mC)9:; q[( u% u )  " " , P(/LE5    % %f&A&A<'> ?&& P))@GGNP P P % $U+ u ,s*"F H9) rrrr=rrrrCr8r"rrA)r name_addrrrSs r get_name_addrrFs` I F %% / 6 6u =? ? Qx;  ))3::6BD D Qx3 8{ "))3::5AC C'. u))3::5AC C  %(I. &xa! #Hbq F!%(LE5 Hbq  U e rclt} t|\}}t d|jDrd|_|j|||fS#tj$rN t |\}}n;#tj$r%tjdj |wxYwYwxYw)z& mailbox = name-addr / addr-spec zexpected mailbox but found '{}'c3PK|]}t|tj ywr$)r8rrur0s rr3zget_mailbox..+s% 3 11 a33 4 1$&r) r rFrrr6r=anyrErdr)rrrSs r get_mailboxrKs iGA$U+ u  3 % 1 1 33. NN5 E>  " "A A(/LE5&& A))188?A A AAs)AB3&A54B358B--B32B3ct}|r_|d|vrX|dtvr$|jt|dd|dd}nt |\}}|j||r|d|vrX||fS)z Read everything up to one of the chars in endchars. This is outside the formal grammar. The InvalidMailbox TokenList that is returned acts like a Mailbox, but the data attributes are None. rr&rN)rrrrr)rrinvalid_mailboxrSs rget_invalid_mailboxrN1s%&O E!HH, 8{ "  " "=q1D$F G!"IE%e,LE5  " "5 ) E!HH, E !!rc\t}|r|ddk7r t|\}}|j||ra|ddvrZ|d}d |_ t|d\}}|j||jjtjd|r"|ddk(r|jt|d d}|r |ddk7r||fS#tj$rLd}|dt vrt |\}}|r|ddvr@|j||jjtjdnt|d\}}||g|dd|j||jjtjdn|ddk(r/|jjtjdnVt|d\}}||g|dd|j||jjtjdYwxYw) aJ mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list obs-mbox-list = *([CFWS] ",") mailbox *("," [mailbox / CFWS]) For this routine we go outside the formal grammar in order to improve error handling. We recognize the end of the mailbox list only at the end of the value or at a ';' (the group terminator). This is so that we can turn invalid mailboxes into InvalidMailbox tokens and continue parsing any remaining valid mailboxes. We also allow all mailbox entries to be null, and this condition is handled appropriately at a higher level. r;Nz,;zempty element in mailbox-listzinvalid mailbox in mailbox-listrrrr)rrKrrrrrr'rrNrurdrQr9)r mailbox_listrSrrs rget_mailbox_listrRCs5=L E!HO 8&u-LE5    &4 U1XT)#2&G!2G .ud;LE5 NN5 !  ' '(B(B1)3 4 U1X_    .!"IEQ E!HOR  K&& 8FQx;& ( aD 0 ''/ ((//0K0K719:$7ud#CLE5)%+Hbq  ''. ((//0J0J91;<qS$$++F,G,G3-56 35$? u%!'E"1I##E*$$++F,F,F5-78/ 8sC EH+*H+ct}|s2|jjtjd||fSd}|r{|dt vrpt |\}}|sC|jjtjd|j|||fS|ddk(r|j|||fSt|\}}t|jdk(rV||j||j||jjtjd||fS||g|dd|j|||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-listrPzgroup-list with empty entries) rr'rrrurrrRrrrQr)r group_listrrSs rget_group_listrU|sa J !!&"<"< -#/ 05  F q[(      % %f&@&@-'/ 0   f %u$ $ 8s?   f %u$ $#E*LE5 5  "     f %% !!&"="= +#- .5   Hbq e u rct}t|\}}|r|ddk7r$tjdj ||j ||j t dd|dd}|r*|ddk(r"|j t dd||ddfSt|\}}|j ||s/|jj tjd n,|ddk7r$tjd j ||j t dd|dd}|r*|dtvrt|\}}|j |||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rr8z8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorrNrPzgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) rrCrrr=rrrUr'rurr)rrrSs r get_grouprWsa GE#E*LE5 E!HO%%'**0&-9 9 LL LLs$CDE !"IE qS ]3(:;<eABi!%(LE5 LL  V77 $& ' qS%% 7 > >u EG G LLs$678 !"IE q[( u U %<rc&t} t|\}}|j |||fS#tj$rN t |\}}n;#tj$r%tjdj |wxYwYuwxYw)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 '{}')rrWrrrKr=r)rrrSs r get_addressrYs"iGA ' u NN5 E>  " "A A&u-LE5&& A))188?A A AAs'/BAB8B  BBc^t}|r t|\}}|j||re|ddk7r]|dd}d|_ t|d\}}|j||jjtjd|r|jt|d d}|r||fS#tj$rad}|dt vrt |\}}|r|ddk(r@|j||jjtjdnt|d\}}||g|dd|jt|g|jjtjdn|ddk(r/|jjtjdn`t|d\}}||g|dd|jt|g|jjtjdYwxYw) a address_list = (address *("," address)) / obs-addr-list obs-addr-list = *([CFWS] ",") address *("," [address / CFWS]) We depart from the formal grammar here by continuing to parse until the end of the input, assuming the input to be entirely composed of an address-list. This is always true in email parsing, and allows us to skip invalid addresses to parse additional valid ones. Nrrz"address-list entry with no contentzinvalid address in address-listzempty element in address-listrrr)rrYrrrrrr'rrNrrurdrQr9)r address_listrSrrs rget_address_listr\s(=L  8&u-LE5    &4 U1X_#2&q)G!2G .uc:LE5 NN5 !  ' '(B(B1)3 4     .!"IEQ R  K&& 8FQx;& ( aC ''/ ((//0K0K<1>?$7uc#BLE5)%+Hbq  ''(89 ((//0J0J91;<qS$$++F,G,G3-56 35#> u%!'E"1I##GUG$45$$++F,F,F5-78/ 8sB77E1H,+H,ct}|s$tjdj||ddk7r$tjdj||j t dd|dd}t |\}}|j ||r|ddk7r$tjd j||j t dd ||ddfS) z& no-fold-literal = "[" *dtext "]" z'expected no-fold-literal but found '{}'rr0z;expected '[' at the start of no-fold-literal but found '{}'zno-fold-literal-startrNr+z9expected ']' at the end of no-fold-literal but found '{}'zno-fold-literal-end)r#rrr=rrr))rno_fold_literalrSs rget_no_fold_literalr_s$oO %% 5 < " [CFWS] id-left = dot-atom-text / obs-id-left id-right = dot-atom-text / no-fold-literal / obs-id-right no-fold-literal = "[" *dtext "]" rr>zexpected msg-id but found '{}'z msg-id-startrNzobsolete id-left in msg-idz4expected dot-atom-text or obs-id-left but found '{}'r-zmsg-id with no id-rightr?z msg-id-endr5zobsolete id-right in msg-idzFexpected dot-atom-text, no-fold-literal or obs-id-right but found '{}'zmissing trailing '>' on msg-id)rrrrrrr=rrr r'rrur_r3)rmsg_idrSs r get_msg_idrb)s WF q[( u e E!HO%% , 3 3E :< < MM-^45 !"IE 1(/ u MM% E!HOf88 %' ( U1X_ MM-\: ;!"IEu} MM-%89: !"IE 5(/ u MM% qSab f88 ,. / MM-\23 q[( u e 5=a  " " 1 1-e4LE5 NN ! !&"="=,#. /&& 1))""(&-1 1 1 / 14  " " 5 5.u5LE5&& 5 5)%0 u%%f&A&A1'34** 5--&&,fUm55 54 5 5sxG,I'I$(> ##F$>$> ? F Fv N%P QM&':; [ M&(;< q[( uE" E!HO    )  ' '(B(BB)D E     eW = > c+>?@ !"IE q[( uE"     )  ' '(B(BB)D E F E!HK/%(ab  E!HK/ >> ##F$>$> ? F Fv N%P QM&':; [ M&(;< q[( uE" ##F$>$> 5%7 8M%9: rct}|ra|ddk7rY|dtvr$|jt|dd|dd}nt |\}}|j||r |ddk7rY||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. rrPr&rN)r\rrrr)rinvalid_parameterrSs rget_invalid_parameterrps)* E!HO 8{ "  $ $]583F&H I!"IE%e,LE5  $ $U + E!HO e ##rct|}|s$tjdj||j }|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_matcherrrr=rrrr)rrrrs r get_ttextrtsk u%A %% + 2 25 9; ; GGIE #e*+ E % )EE %<rcnt}|r*|dtvrt|\}}|j||r/|dtvr$t j dj|t|\}}|j||r*|dtvrt|\}}|j|||fS)ztoken = [CFWS] 1*ttext [CFWS] The RFC equivalent of ttext is any US-ASCII chars except space, ctls, or tspecials. We also exclude tabs even though the RFC doesn't. The RFC implies the CFWS but is not explicit about it in the BNF. rexpected token but found '{}') rrrr TOKEN_ENDSrrr=rt)rmtokenrSs r get_tokenrysWF q[( u e qZ'%% + 2 25 9; ;U#LE5 MM% q[( u e 5=rct|}|s$tjdj||j }|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}Nrb)_non_attribute_end_matcherrrr=rrrrrrrbs r get_attrtextr} sk #5)A %% . 5 5e <> >wwyH #h-. !EXz2HH U?rcnt}|r*|dtvrt|\}}|j||r/|dtvr$t j dj|t|\}}|j||r*|dtvrt|\}}|j|||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. rrv) r_rrrATTRIBUTE_ENDSrrr=r}rr`rSs r get_attributer s I q[( u q^+%% + 2 25 9; ;&LE5 U q[( u e rct|}|s$tjdj||j }|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_matcherrrr=rrrrr|s rget_extended_attrtextr4 sn ,E2A %% 7 > >u EG GwwyH #h-. !EX':;HH U?rcnt}|r*|dtvrt|\}}|j||r/|dtvr$t j dj|t|\}}|j||r*|dtvrt|\}}|j|||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. rrv) r_rrrEXTENDED_ATTRIBUTE_ENDSrrr=rrs rget_extended_attributerF s I q[( u q44%% + 2 25 9; ;(/LE5 U q[( u e rclt}|r|ddk7r$tjdj||j t dd|dd}|r|dj s$tjdj|d}|r6|dj r#||dz }|dd}|r|dj r#|dd k(r3|d k7r.|jj tjd t||_ |j t |d ||fS) a6 '*' digits The formal BNF is more complicated because leading 0s are not allowed. We check for that and add a defect. We also assume no CFWS is allowed between the '*' and the digits, though the RFC is not crystal clear on that. The caller should already have dealt with leading CFWS. r*zExpected section but found {}zsection-markerrNz$Expected section number but found {}r-0z'section number has an invalid leading 0rh) rfrrr=rrrjr'rurkrV)rrgrhs r get_sectionr\ s2iG E!HO%%&E&L&L(-'/0 0 NN=&678 !"IE a((*%%'117@ @ F E!H$$&%(ab  E!H$$&ayCFcMv999 ; <[GN NN=23 E>rcJt}|stjdd}|dtvrt |\}}|s$tjdj ||ddk(rt |\}}nt|\}}||g|dd|j|||fS)z quoted-string / attribute z&Expected value but found end of stringNrz Expected value but found only {}r) rirrrrr=r rr)rvrrSs r get_valuerz s A %%&NOO F Qx;   %%'006v@ @ Qx3(/ u-e4 u Hbq HHUO e8Orc2 t}t|\}}|j||r|ddk(rA|jjt j dj |||fS|ddk(rm t|\}}d|_|j||st jd|ddk(r'|jtdd|dd }d|_ |dd k7rt jd |jtd d |dd }|r*|dtvrt|\}}|j|d }|}|jr|r|dd k(rt|\}}|j}d}|j dk(r(|r |ddk(rd}n/t#|\}}|r|ddk(rd}n t%|\}}|sd} |ra|jjt j d|j||D]} | j&dk(sg| d d | }n|}n0d }|jjt j d|r |ddk(rd }nt)|\}}|jr|j dkDrZ|r|ddk7r"|j|| |rJ||}||fS|jjt j d|sF|jjt j d|j||#||fS|I|D]} | j&dk(sn j&dk(|j| | j*|_|ddk7r$t jdj ||jtdd|dd }|rf|ddk7r^t#|\}}|j||j*|_|r|ddk7r$t jdj ||jtdd|dd }|et1} |rV|dt2vrt5|\}}n(|dd k(rtd d}|dd }nt7|\}}| j||rV| }nt)|\}}|j|| |rJ||}||fS#tj$rYiwxYw#Y@xYw)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. rrPz)Parameter contains name ({}) but no valuerTzIncomplete parameterzextended-parameter-markerrN=zParameter not followed by '='parameter-separatorrF'z5Quoted string value for extended parameter is invalidrzZParameter marked as extended but appears to have a quoted string value that is non-encodedzcApparent initial-extended-value but attribute was not marked as extended or was not initial sectionz(Missing required charset/lang delimitersrrbz=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {}DQUOTE)rQrrr'rrur=rrUrrrZrrr rrWr}rrdrrrrrirrr) rrrSrappendtoqstring inner_value semi_validrtrs r get_parameterr s+ KE 'LE5 LL E!HO V779%%+VE]4 5e| Qx3 &u-LE5"EO LL ))*@A A 8s? LLs,GH I!"IE!EN Qx3%%&EFF LLs$9:; !"IE q[( u UIH ~~%E!HO/u5,,    1 ${1~4! *;7 tDGsN!%J &3K@ t!%J  MM !;!;G"I J LL !<<#77AaD H   EI MM !;!;:"; < qS ' u >>U11A5aC OOE "$ '%'y!%<  V77 DE F  V77 68 9  %<   <<#66 LLJ & OOA GGEM 8s?))+FFLfUmU U c+>?@ab  U1X_'.LE5 OOE "EJE!HO--/<?@ab  GQx3&u~ uqS%c84ab +E2 u HHUO ' u OOE%y %<i&&   D s>&S7T7T TTct}|r t|\}}|j||rp|ddk7rh|d}d|_ t|\}}|j||jjtjdj||r |jtdd |d d}|r|S#tj$rd}|dt vrt |\}}|s|j||cYS|ddk(rB||j||jjtjdndt|\}}|r|g|dd|j||jjtjdj|YwxYw) 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. NrrPzparameter entry with no contentzinvalid parameter {!r}rr]z)parameter with invalid trailing text {!r}rr)rlrrrrrrr'rurpr=rdrQr)rmime_parametersrSrrs rparse_mime_parametersr s%&O  =(/LE5  " "5 )( U1X_$B'E2E 07LE5 LL   # # * *6+E+E;BB5I,K L   " "=6K#L M!"IEG H A&& =FQx;& ( &&v.&&Qx3%#**62''..v/I/I5078 5U; u!'E"1I&&u-''..v/I/I,33E:0<=# =sCAF= B.F=<F=c@|ra|ddk7rY|dtvr$|jt|dd|dd}nt|\}}|j||r |ddk7rY|sy|jtdd|jt |ddy)zBDo our best to find the parameters in an invalid MIME header rrPr&rNr)rrrrr) tokenlistrrSs r_find_mime_parametersrQ s E!HO 8{ "   ]585HI J!"IE%e,LE5   U # E!HO  ]3(=>? *5956rc8t}|s0|jjtjd|S t |\}}|j||r|ddk7r>|jjtjd|r t|||S|jjj|_ |jtdd|dd} t |\}}|j||jjj|_|s|S|dd k7rO|jjtjd j||` |`t|||S|jtd d |jt!|dd|S#tj $rN|jjtjdj|t|||cYSwxYw#tj $rN|jjtjd j|t|||cYSwxYw) 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. z"Missing content type specificationz(Expected content maintype but found {!r}rrzInvalid content typezcontent-type-separatorrNz'Expected content subtype but found {!r}rPz> 02 3  ' u  LL E!HO V77 "$ %  !% / [[&&(..0EN LLs$<=> !"IE ' u  LLKK%%'--/EM   Qx3 V77 ( ) NEMeU+  LLs$9:; LL&uQRy12 LQ  " " V77 6 = =e DF GeU+ &  " " V77 5 < . s%.,qa!>!>?,rI unknown-8bitTrmrUrr!rrrJrwrr)"max_line_lengthsysmaxsizeutf8rrr7rrd SPECIALSNL isdisjointNLSETr!r{rJrE_fold_mime_parametersrMrjrYrrrrre _fold_as_ewrkrJrr7rrrinsert) parse_treerVmaxlenrqrleading_whitespacelast_ew last_charsetr want_encodingend_ew_not_allowedrrNtstrr encoded_partnewlinewhitespace_accumulatorcharnewpartsps rrWrW s  # # 2s{{F ++w:H DEGLM!"&:;  E yy| % % ! #  4y"44$.$9$9$$? ? %*$4$4T$: :  ! KK !G ??/ / !$vx @  !3%% % ''#'99F9#;Q=Q#RL~~\9|,vE"I/FF&CE&JG!LL1b \1  4*T U* % ##'|+!^3!W,J1F"G%dE67&*&=&=wHZ\&("& %  %  t9U2Y/ / "I I     D A '3E:G$--/  Wt^,)+&!"ID3*11$7&&(WW-C%D"tX&DzH"66 #301&(&##4Q#7A&((#3012 )+"301'(& /##9!#>  u % 66}" !. ,,..(" M !B( (s:P)#Q1-Q!),QQc|+|r)tt|d|d|z}|dd||d<nM|dtvrB|d}|dd}t|d|k(r|j t ||dxx|z cc<d}|dtvr |d}|dd}|t|dn|} |dk(rdn|} t| dz} | dz|k\rt jd |r!|t|dz } | | z t|z } | dkr|j d  b [ L} } crN $+O#eBi.K"j0gIY!#JQ6!%% AC C  3uRy>1$z1C8J4KK ? LL   u:>c%)n16H::&8)LL "I %I!# ";J/zz.)D \"_4qj,CR0N::niHL&8F qj b \! c.123   LL eBi.K? @ "II,;6$6rc |jD]\}}|djjds |dxxdz cc<|}d} |j|d}|r6tjj|d | } d j||| } nd j|t|} t|dt| zd z|kr|ddz| z|d<t| dz|kr|jd| zd} |dz} |st|tt| zdzt| z} || dzkrd}|| z dz x}} |d|}tjj|d | } t| |krn|d z}<|jdj|| | | d } | d z } ||d}|r |dxxdz cc<|ry#t$r"d}t j |rd}d}nd}YwxYw)a>Fold TokenList 'part' into the 'lines' list as mime parameters. Using the decoded list of parameters and values, format them according to the RFC rules, including using RFC2231 encoding if the value cannot be expressed in 'encoding' and/or the parameter+value is too long to fit within 'maxlen'. rrPstrictFTrrprr-)saferz {}*={}''{}rrrsrrz''r(NNz {}*{}*={}{})rr*rcr!r{rr|rvrwrr=r rrr)rNrrrqrrr error_handlerencoding_required encoded_valuerrg extra_chromer splitpointmaxcharspartials rrr sA{{ e Ry!**3/ "I I  " LL " %  "LL..B}/6M&&tWmDD>>$ U(;CI % )F 2b C$.E"I  Y]f $ LLt $ ~ TSW%66:S=NNJa' $*Z$7!$; ;J , & 2 2"]!3!< }%1a  LL..g|]< =L qLG*+&Eb S -I#" " $ $$U+( 1 !  "sG'G>=G>)r)rrerrvstringroperatorremailrrrrr.rrrr r/r TSPECIALSrw ASPECIALSrrrrrrr compileVERBOSE MULTILINErrr"rpr|rrrrrrrrrrrrrrrrrr rrrr r#r%r3r5r=rGrMrQr\r_rfrirlrrrrrrrrrrrrrrrrrAr9rMrjr:r=r7rrrmatchrfindallrrsr{rrrrrrrrrrrrr rrrrrr$r r)r.r1r3r6r<rArCrFrKrNrRrUrWrYr\r_rbrfrmrprtryr}rrrrrrrrrrrrrWrrrtrrrs:CJ '  %jCHn   sN CH$ U# E "c#h . _ E " S(3s83 t    @ 1  "**ZZ",, @,@,FD)D I Y9"9I )#9#6 -| -!4C)C&%i%2 ?) ?% %"$I$*"y"6 DDyD!i!6;Y;.Y.i) I yB9 -!&-!`! !H I  ) 8%y% # #i I S.YS.l y *1 i ) *I*&y&Y(+s(+V''H->#I$4$4 BIIbggj!"%$%%*U'RZZ (8(8 BIIbggn%&)()).&0bjj1A1A BIIbgg-./21'227%$;J< /bAF"  ))V2  $6 &2 D$L%N2!h(%!N$L ) V,\ "H*"$6r#J<:4n&,BJ8BH$$&.&.$,<,KZ2h7 6p<^`7DJ7XI!r