Your IP : 3.139.80.194


Current Path : /usr/lib64/python3.6/email/__pycache__/
Upload File :
Current File : //usr/lib64/python3.6/email/__pycache__/_header_value_parser.cpython-36.pyc

3

�Qg���@szdZddlZddlZddlmZddlmZddlmZddl	m
Zddl	mZddl	m
Z
ed	�Zeed
�BZed�ZeeBZeed�Zeed
�Zeed�Bed�ZeeBZeed�BZeeBZeed�ZddhZeeBZdd�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!Gdd�de�Z"Gdd �d e�Z#Gd!d"�d"e�Z$Gd#d$�d$e�Z%Gd%d&�d&e�Z&Gd'd(�d(e�Z'Gd)d*�d*e'�Z(Gd+d,�d,e�Z)Gd-d.�d.e�Z*Gd/d0�d0e�Z+Gd1d2�d2e�Z,Gd3d4�d4e�Z-Gd5d6�d6e�Z.Gd7d8�d8e�Z/Gd9d:�d:e�Z0Gd;d<�d<e�Z1Gd=d>�d>e�Z2Gd?d@�d@e�Z3GdAdB�dBe�Z4GdCdD�dDe�Z5GdEdF�dFe�Z6GdGdH�dHe�Z7GdIdJ�dJe�Z8GdKdL�dLe!�Z9GdMdN�dNe�Z:GdOdP�dPe�Z;GdQdR�dRe�Z<GdSdT�dTe�Z=GdUdV�dVe=�Z>GdWdX�dXe�Z?GdYdZ�dZe�Z@Gd[d\�d\e�ZAGd]d^�d^e�ZBGd_d`�d`e�ZCGdadb�dbeC�ZDGdcdd�ddeC�ZEGdedf�dfe�ZFGdgdh�dhe�ZGGdidj�dje�ZHGdkdl�dleI�ZJGdmdn�dneJ�ZKGdodp�dpeJ�ZLGdqdr�dreK�ZMeLdds�ZNeLdtdu�ZOeLdvdw�ZPejQdxjRdyjSe���jTZUejQdzjRdyjSe�jVd{d|�jVd}d~���jWZXejQd�jYZZejQdzjRdyjSe�jVd{d|�jVd}d~���jWZ[ejQdzjRdyjSe�jVd{d|�jVd}d~���jWZ\ejQdzjRdyjSe�jVd{d|�jVd}d~���jWZ]d�d��Z^d�d��Z_d�d��Z`d�d��Zad�d��Zbd�d��Zcd�d��Zdd�d��Zed�d��Zfd�d��Zgd�d��Zhd�d��Zid�d��Zjd�d��Zkd�d��Zld�d��Zmd�d��Znd�d��Zod�d��Zpd�d��Zqd�d��Zrd�d��Zsd�d��Ztd�d��Zud�d��Zvd�d��Zwd�d��Zxd�d��Zyd�d��Zzd�d��Z{d�d��Z|d�d��Z}d�d��Z~d�dÄZd�dńZ�d�dDŽZ�d�dɄZ�d�d˄Z�d�d̈́Z�d�dτZ�d�dфZ�d�dӄZ�d�dՄZ�d�dׄZ�d�dلZ�d�dۄZ�d�d݄Z�d�d߄Z�d�d�Z�d�d�Z�d�d�Z�d�d�Z�d�d�Z�d�d�Z�d�d�Z�dS)�alHeader value parser implementing various email-related RFC parsing rules.

The parsing methods defined in this module implement various email related
parsing rules.  Principal among them is RFC 5322, which is the followon
to RFC 2822 and primarily a clarification of the former.  It also implements
RFC 2047 encoded word decoding.

RFC 5322 goes to considerable trouble to maintain backward compatibility with
RFC 822 in the parse phase, while cleaning up the structure on the generation
phase.  This parser supports correct RFC 5322 generation by tagging white space
as folding white space only when folding is allowed in the non-obsolete rule
sets.  Actually, the parser is even more generous when accepting input than RFC
5322 mandates, following the spirit of Postel's Law, which RFC 5322 encourages.
Where possible deviations from the standard are annotated on the 'defects'
attribute of tokens that deviate.

The general structure of the parser follows RFC 5322, and uses its terminology
where there is a direct correspondence.  Where the implementation requires a
somewhat different structure than that used by the formal grammar, new terms
that mimic the closest existing terms are used.  Thus, it really helps to have
a copy of RFC 5322 handy when studying this code.

Input to the parser is a string that has already been unfolded according to
RFC 5322 rules.  According to the RFC this unfolding is the very first step, and
this parser leaves the unfolding step to a higher level message parser, which
will have already detected the line breaks that need unfolding while
determining the beginning and end of each header.

The output of the parser is a TokenList object, which is a list subclass.  A
TokenList is a recursive data structure.  The terminal nodes of the structure
are Terminal objects, which are subclasses of str.  These do not correspond
directly to terminal objects in the formal grammar, but are instead more
practical higher level combinations of true terminals.

All TokenList and Terminal objects have a 'value' attribute, which produces the
semantically meaningful value of that part of the parse subtree.  The value of
all whitespace tokens (no matter how many sub-tokens they may contain) is a
single space, as per the RFC rules.  This includes 'CFWS', which is herein
included in the general class of whitespace tokens.  There is one exception to
the rule that whitespace tokens are collapsed into single spaces in values: in
the value of a 'bare-quoted-string' (a quoted-string with no leading or
trailing whitespace), any whitespace that appeared between the quotation marks
is preserved in the returned value.  Note that in all Terminal strings quoted
pairs are turned into their unquoted values.

All TokenList and Terminal objects also have a string value, which attempts to
be a "canonical" representation of the RFC-compliant form of the substring that
produced the parsed subtree, including minimal use of quoted pair quoting.
Whitespace runs are not collapsed.

Comment tokens also have a 'content' attribute providing the string found
between the parens (including any nested comments) with whitespace preserved.

All TokenList and Terminal objects have a 'defects' attribute which is a
possibly empty list all of the defects found while creating the token.  Defects
may appear on any token in the tree, and a composite list of all defects in the
subtree is available through the 'all_defects' attribute of any node.  (For
Terminal notes x.defects == x.all_defects.)

Each object in a parse tree is called a 'token', and each has a 'token_type'
attribute that gives the name from the RFC 5322 grammar that it represents.
Not all RFC 5322 nodes are produced, and there is one non-RFC 5322 node that
may be produced: 'ptext'.  A 'ptext' is a string of printable ascii characters.
It is returned in place of lists of (ctext/quoted-pair) and
(qtext/quoted-pair).

XXX: provide complete list of token types.
�N)�	hexdigits)�OrderedDict)�
itemgetter)�_encoded_words)�errors)�utilsz 	�(z
()<>@,:;.\"[]�.z."(z/?=z*'%�%�
�
cCs dt|�jdd�jdd�dS)N�"�\z\\z\")�str�replace)�value�r�2/usr/lib64/python3.6/email/_header_value_parser.py�quote_stringbsrcs�eZdZdZdZdZ�fdd�Zdd�Z�fdd�Ze	d	d
��Z
e	dd��Zd
d�Ze	dd��Z
e	dd��Zdd�Zddd�Zddd�Zddd�Z�ZS)�	TokenListNTcst�j||�g|_dS)N)�super�__init__�defects)�self�args�kw)�	__class__rrroszTokenList.__init__cCsdjdd�|D��S)N�css|]}t|�VqdS)N)r)�.0�xrrr�	<genexpr>tsz$TokenList.__str__.<locals>.<genexpr>)�join)rrrr�__str__sszTokenList.__str__csdj|jjt�j��S)Nz{}({}))�formatr�__name__r�__repr__)r)rrrr%vs
zTokenList.__repr__cCsdjdd�|D��S)Nrcss|]}|jr|jVqdS)N)r)rrrrrr |sz"TokenList.value.<locals>.<genexpr>)r!)rrrrrzszTokenList.valuecCstdd�|D�|j�S)Ncss|]}|jVqdS)N)�all_defects)rrrrrr �sz(TokenList.all_defects.<locals>.<genexpr>)�sumr)rrrrr&~szTokenList.all_defectscCs|dj�S)Nr)�startswith_fws)rrrrr(�szTokenList.startswith_fwscCstdd�|D��S)zATrue if all top level tokens of this part may be RFC2047 encoded.css|]}|jVqdS)N)�
as_ew_allowed)r�partrrrr �sz*TokenList.as_ew_allowed.<locals>.<genexpr>)�all)rrrrr)�szTokenList.as_ew_allowedcCs"g}x|D]}|j|j�q
W|S)N)�extend�comments)rr-�tokenrrrr-�s
zTokenList.commentscCst||d�S)N)�policy)�_refold_parse_tree)rr/rrr�fold�szTokenList.foldrcCst|j|d��dS)N)�indent)�print�ppstr)rr2rrr�pprint�szTokenList.pprintcCsdj|j|d��S)Nr)r2)r!�_pp)rr2rrrr4�szTokenList.ppstrccs~dj||jj|j�Vx<|D]4}t|d�s<|dj|�Vq|j|d�EdHqW|jrhdj|j�}nd}dj||�VdS)Nz{}{}/{}(r6z*    !! invalid element in token list: {!r}z    z Defects: {}rz{}){})r#rr$�
token_type�hasattrr6r)rr2r.Zextrarrrr6�s


z
TokenList._pp)r)r)r)r$�
__module__�__qualname__r7�syntactic_break�ew_combine_allowedrr"r%�propertyrr&r(r)r-r1r5r4r6�
__classcell__rr)rrris

rc@s$eZdZedd��Zedd��ZdS)�WhiteSpaceTokenListcCsdS)N� r)rrrrr�szWhiteSpaceTokenList.valuecCsdd�|D�S)NcSsg|]}|jdkr|j�qS)�comment)r7�content)rrrrr�
<listcomp>�sz0WhiteSpaceTokenList.comments.<locals>.<listcomp>r)rrrrr-�szWhiteSpaceTokenList.commentsN)r$r9r:r=rr-rrrrr?�sr?c@seZdZdZdS)�UnstructuredTokenList�unstructuredN)r$r9r:r7rrrrrD�srDc@seZdZdZdS)�Phrase�phraseN)r$r9r:r7rrrrrF�srFc@seZdZdZdS)�WordZwordN)r$r9r:r7rrrrrH�srHc@seZdZdZdS)�CFWSList�cfwsN)r$r9r:r7rrrrrI�srIc@seZdZdZdS)�Atom�atomN)r$r9r:r7rrrrrK�srKc@seZdZdZdZdS)�Tokenr.FN)r$r9r:r7Zencode_as_ewrrrrrM�srMc@seZdZdZdZdZdZdS)�EncodedWordzencoded-wordN)r$r9r:r7�cte�charset�langrrrrrN�srNc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�QuotedStringz
quoted-stringcCs"x|D]}|jdkr|jSqWdS)Nzbare-quoted-string)r7r)rrrrrrB�s

zQuotedString.contentcCsBg}x2|D]*}|jdkr(|jt|��q
|j|j�q
Wdj|�S)Nzbare-quoted-stringr)r7�appendrrr!)r�resrrrr�quoted_value�s

zQuotedString.quoted_valuecCs"x|D]}|jdkr|jSqWdS)Nzbare-quoted-string)r7r)rr.rrr�stripped_value�s

zQuotedString.stripped_valueN)r$r9r:r7r=rBrUrVrrrrrR�s
rRc@s$eZdZdZdd�Zedd��ZdS)�BareQuotedStringzbare-quoted-stringcCstdjdd�|D���S)Nrcss|]}t|�VqdS)N)r)rrrrrr �sz+BareQuotedString.__str__.<locals>.<genexpr>)rr!)rrrrr"�szBareQuotedString.__str__cCsdjdd�|D��S)Nrcss|]}t|�VqdS)N)r)rrrrrr sz)BareQuotedString.value.<locals>.<genexpr>)r!)rrrrr�szBareQuotedString.valueN)r$r9r:r7r"r=rrrrrrW�srWc@s8eZdZdZdd�Zdd�Zedd��Zedd	��Zd
S)�CommentrAcs(djtdg�fdd��D�dggg��S)Nrrcsg|]}�j|��qSr)�quote)rr)rrrrCsz#Comment.__str__.<locals>.<listcomp>�))r!r')rr)rrr"s
zComment.__str__cCs2|jdkrt|�St|�jdd�jdd�jdd�S)NrArz\\rz\(rZz\))r7rr)rrrrrrYs

z
Comment.quotecCsdjdd�|D��S)Nrcss|]}t|�VqdS)N)r)rrrrrr sz"Comment.content.<locals>.<genexpr>)r!)rrrrrBszComment.contentcCs|jgS)N)rB)rrrrr-szComment.commentsN)	r$r9r:r7r"rYr=rBr-rrrrrXs
rXc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�AddressListzaddress-listcCsdd�|D�S)NcSsg|]}|jdkr|�qS)�address)r7)rrrrrrC$sz)AddressList.addresses.<locals>.<listcomp>r)rrrr�	addresses"szAddressList.addressescCstdd�|D�g�S)Ncss|]}|jdkr|jVqdS)r\N)r7�	mailboxes)rrrrrr (sz(AddressList.mailboxes.<locals>.<genexpr>)r')rrrrr^&szAddressList.mailboxescCstdd�|D�g�S)Ncss|]}|jdkr|jVqdS)r\N)r7�
all_mailboxes)rrrrrr -sz,AddressList.all_mailboxes.<locals>.<genexpr>)r')rrrrr_+szAddressList.all_mailboxesN)r$r9r:r7r=r]r^r_rrrrr[sr[c@s4eZdZdZedd��Zedd��Zedd��ZdS)	�Addressr\cCs|djdkr|djSdS)Nr�group)r7�display_name)rrrrrb5szAddress.display_namecCs4|djdkr|dgS|djdkr*gS|djS)Nr�mailboxzinvalid-mailbox)r7r^)rrrrr^:s

zAddress.mailboxescCs:|djdkr|dgS|djdkr0|dgS|djS)Nrrczinvalid-mailbox)r7r_)rrrrr_Bs


zAddress.all_mailboxesN)r$r9r:r7r=rbr^r_rrrrr`1sr`c@s(eZdZdZedd��Zedd��ZdS)�MailboxListzmailbox-listcCsdd�|D�S)NcSsg|]}|jdkr|�qS)rc)r7)rrrrrrCPsz)MailboxList.mailboxes.<locals>.<listcomp>r)rrrrr^NszMailboxList.mailboxescCsdd�|D�S)NcSsg|]}|jdkr|�qS)rc�invalid-mailbox)rcre)r7)rrrrrrCTsz-MailboxList.all_mailboxes.<locals>.<listcomp>r)rrrrr_RszMailboxList.all_mailboxesN)r$r9r:r7r=r^r_rrrrrdJsrdc@s(eZdZdZedd��Zedd��ZdS)�	GroupListz
group-listcCs"|s|djdkrgS|djS)Nrzmailbox-list)r7r^)rrrrr^\szGroupList.mailboxescCs"|s|djdkrgS|djS)Nrzmailbox-list)r7r_)rrrrr_bszGroupList.all_mailboxesN)r$r9r:r7r=r^r_rrrrrfXsrfc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�GroupracCs|djdkrgS|djS)N�z
group-list)r7r^)rrrrr^mszGroup.mailboxescCs|djdkrgS|djS)Nrhz
group-list)r7r_)rrrrr_sszGroup.all_mailboxescCs
|djS)Nr)rb)rrrrrbyszGroup.display_nameN)r$r9r:r7r=r^r_rbrrrrrgisrgc@sLeZdZdZedd��Zedd��Zedd��Zedd	��Zed
d��Z	dS)
�NameAddrz	name-addrcCst|�dkrdS|djS)N�r)�lenrb)rrrrrb�szNameAddr.display_namecCs
|djS)Nrj���)�
local_part)rrrrrm�szNameAddr.local_partcCs
|djS)Nrjrl)�domain)rrrrrn�szNameAddr.domaincCs
|djS)Nrjrl)�route)rrrrro�szNameAddr.routecCs
|djS)Nrjrl)�	addr_spec)rrrrrp�szNameAddr.addr_specN)
r$r9r:r7r=rbrmrnrorprrrrri~sric@s@eZdZdZedd��Zedd��Zedd��Zedd	��Zd
S)�	AngleAddrz
angle-addrcCs"x|D]}|jdkr|jSqWdS)Nz	addr-spec)r7rm)rrrrrrm�s

zAngleAddr.local_partcCs"x|D]}|jdkr|jSqWdS)Nz	addr-spec)r7rn)rrrrrrn�s

zAngleAddr.domaincCs"x|D]}|jdkr|jSqWdS)Nz	obs-route)r7�domains)rrrrrro�s

zAngleAddr.routecCs<x6|D]*}|jdkr|jr |jSt|j�|jSqWdSdS)Nz	addr-specz<>)r7rmrpr)rrrrrrp�s

zAngleAddr.addr_specN)	r$r9r:r7r=rmrnrorprrrrrq�s
rqc@seZdZdZedd��ZdS)�ObsRoutez	obs-routecCsdd�|D�S)NcSsg|]}|jdkr|j�qS)rn)r7rn)rrrrrrC�sz$ObsRoute.domains.<locals>.<listcomp>r)rrrrrr�szObsRoute.domainsN)r$r9r:r7r=rrrrrrrs�srsc@sLeZdZdZedd��Zedd��Zedd��Zedd	��Zed
d��Z	dS)
�MailboxrccCs|djdkr|djSdS)Nrz	name-addr)r7rb)rrrrrb�szMailbox.display_namecCs
|djS)Nr)rm)rrrrrm�szMailbox.local_partcCs
|djS)Nr)rn)rrrrrn�szMailbox.domaincCs|djdkr|djSdS)Nrz	name-addr)r7ro)rrrrro�sz
Mailbox.routecCs
|djS)Nr)rp)rrrrrp�szMailbox.addr_specN)
r$r9r:r7r=rbrmrnrorprrrrrt�srtc@s,eZdZdZedd��ZeZZZZ	dS)�InvalidMailboxzinvalid-mailboxcCsdS)Nr)rrrrrb�szInvalidMailbox.display_nameN)
r$r9r:r7r=rbrmrnrorprrrrru�srucs(eZdZdZdZe�fdd��Z�ZS)�DomainrnFcsdjt�jj��S)Nr)r!rr�split)r)rrrrn�sz
Domain.domain)r$r9r:r7r)r=rnr>rr)rrrv�srvc@seZdZdZdS)�DotAtomzdot-atomN)r$r9r:r7rrrrrx�srxc@seZdZdZdZdS)�DotAtomTextz
dot-atom-textTN)r$r9r:r7r)rrrrry�sryc@sDeZdZdZdZedd��Zedd��Zedd��Zed	d
��Z	dS)�AddrSpecz	addr-specFcCs
|djS)Nr)rm)rrrrrmszAddrSpec.local_partcCst|�dkrdS|djS)N�rjrl)rkrn)rrrrrnszAddrSpec.domaincCs<t|�dkr|djS|djj�|dj|djj�S)Nr{rrjrh)rkr�rstrip�lstrip)rrrrrs
zAddrSpec.valuecCsLt|j�}t|�t|t�kr*t|j�}n|j}|jdk	rH|d|jS|S)N�@)�setrmrk�
DOT_ATOM_ENDSrrn)rZnamesetZlprrrrps

zAddrSpec.addr_specN)
r$r9r:r7r)r=rmrnrrprrrrrz�srzc@seZdZdZdZdS)�ObsLocalPartzobs-local-partFN)r$r9r:r7r)rrrrr� sr�cs4eZdZdZdZedd��Ze�fdd��Z�ZS)�DisplayNamezdisplay-nameFcCs�t|�}|djdkr"|jd�n*|ddjdkrLt|ddd��|d<|djdkrd|j�n*|ddjdkr�t|ddd��|d	<|jS)
NrrJrjrlrlrlrlrlrl)rr7�popr)rrTrrrrb+s
zDisplayName.display_namecs�d}|jrd}nx|D]}|jdkrd}qW|r�d}}|djdksX|ddjdkr\d}|d	jdks||d
djdkr�d}|t|j�|St�jSdS)NFTz
quoted-stringrrrJr@rjrlrlrl)rr7rrbrr)rrYrZpreZpost)rrrr:s

  zDisplayName.value)	r$r9r:r7r<r=rbrr>rr)rrr�&sr�c@s,eZdZdZdZedd��Zedd��ZdS)�	LocalPartz
local-partFcCs&|djdkr|djS|djSdS)Nrz
quoted-string)r7rUr)rrrrrSs
zLocalPart.valuecCs�tg}t}d}x�|dtgD]�}|jdkr.q|r^|jdkr^|djdkr^t|dd��|d<t|t�}|r�|jdkr�|djdkr�|jt|dd���n
|j|�|d	}|}qWt|dd
��}|jS)NFrrJ�dotrjrlrlrlrlrl)�DOTr7r�
isinstancerSr)rrTZlastZ
last_is_tl�tokZis_tlrrrrmZs$


zLocalPart.local_partN)r$r9r:r7r)r=rrmrrrrr�Nsr�cs4eZdZdZdZe�fdd��Zedd��Z�ZS)�
DomainLiteralzdomain-literalFcsdjt�jj��S)Nr)r!rrrw)r)rrrrnwszDomainLiteral.domaincCs"x|D]}|jdkr|jSqWdS)N�ptext)r7r)rrrrr�ip{s

zDomainLiteral.ip)	r$r9r:r7r)r=rnr�r>rr)rrr�rsr�c@seZdZdZdZdZdS)�MIMEVersionzmime-versionN)r$r9r:r7�major�minorrrrrr��sr�c@s4eZdZdZdZdZdZedd��Zedd��Z	dS)	�	Parameter�	parameterFzus-asciicCs|jr|djSdS)Nrjr)�	sectioned�number)rrrr�section_number�szParameter.section_numbercCsbx\|D]T}|jdkr|jS|jdkrx4|D],}|jdkr*x|D]}|jdkr>|jSq>Wq*WqWdS)Nrz
quoted-stringzbare-quoted-stringr)r7rV)rr.rrr�param_value�s






zParameter.param_valueN)
r$r9r:r7r��extendedrPr=r�r�rrrrr��sr�c@seZdZdZdS)�InvalidParameterzinvalid-parameterN)r$r9r:r7rrrrr��sr�c@seZdZdZedd��ZdS)�	Attribute�	attributecCs$x|D]}|jjd�r|jSqWdS)N�attrtext)r7�endswithr)rr.rrrrV�s
zAttribute.stripped_valueN)r$r9r:r7r=rVrrrrr��sr�c@seZdZdZdZdS)�Section�sectionN)r$r9r:r7r�rrrrr��sr�c@seZdZdZedd��ZdS)�ValuercCs2|d}|jdkr|d}|jjd�r,|jS|jS)NrrJrj�
quoted-stringr��extended-attribute)r�r�r�)r7r�rVr)rr.rrrrV�s
zValue.stripped_valueN)r$r9r:r7r=rVrrrrr��sr�c@s(eZdZdZdZedd��Zdd�ZdS)�MimeParameterszmime-parametersFccs�t�}x\|D]T}|jjd�sq|djdkr.q|djj�}||krLg||<||j|j|f�qW�x�|j�D�]�\}}t|t	d�d�}|dd}|j
}|jr�t|�dkr�|dddkr�|ddj
jtjd��|dd�}g}d}x�|D]�\}	}
|	|k�r6|
j�s$|
j
jtjd��q�n|
j
jtjd��|d7}|
j}|
j�r�ytjj|�}Wn&tk
�r�tjj|d	d
�}YnRXy|j|d�}Wn"tk
�r�|jdd�}YnXtj|��r�|
j
jtj��|j|�q�Wd
j|�}||fVqpWdS)Nr�rr�)�keyrjz.duplicate parameter name; duplicate(s) ignoredz+duplicate parameter name; duplicate ignoredz(inconsistent RFC2231 parameter numberingzlatin-1)�encoding�surrogateescapezus-asciir)rr7r�r�striprSr��items�sortedrrPr�rkrr�InvalidHeaderDefectr��urllib�parseZunquote_to_bytes�UnicodeEncodeErrorZunquote�decode�LookupErrorr�_has_surrogates�UndecodableBytesDefectr!)r�paramsr.�name�partsZfirst_paramrPZvalue_parts�ir��paramrrrrr��sZ




zMimeParameters.paramscCsXg}x8|jD].\}}|r0|jdj|t|���q|j|�qWdj|�}|rTd|SdS)Nz{}={}z; r@r)r�rSr#rr!)rr�r�rrrrr"s
zMimeParameters.__str__N)r$r9r:r7r;r=r�r"rrrrr��sFr�c@seZdZdZedd��ZdS)�ParameterizedHeaderValueFcCs&x t|�D]}|jdkr
|jSq
WiS)Nzmime-parameters)�reversedr7r�)rr.rrrr�%s

zParameterizedHeaderValue.paramsN)r$r9r:r;r=r�rrrrr�sr�c@seZdZdZdZdZdZdS)�ContentTypezcontent-typeF�textZplainN)r$r9r:r7r)�maintype�subtyperrrrr�-sr�c@seZdZdZdZdZdS)�ContentDispositionzcontent-dispositionFN)r$r9r:r7r)�content_dispositionrrrrr�5sr�c@seZdZdZdZdZdS)�ContentTransferEncodingzcontent-transfer-encodingFZ7bitN)r$r9r:r7r)rOrrrrr�<sr�c@seZdZdZdZdS)�HeaderLabelzheader-labelFN)r$r9r:r7r)rrrrr�Csr�c@seZdZdZdS)�Header�headerN)r$r9r:r7rrrrr�Isr�csreZdZdZdZdZ�fdd�Z�fdd�Zdd�Ze	dd	��Z
d�fdd�	Zd
d�Ze	dd��Z
dd�Z�ZS)�TerminalTcst�j||�}||_g|_|S)N)r�__new__r7r)�clsrr7r)rrrr�XszTerminal.__new__csdj|jjt�j��S)Nz{}({}))r#rr$rr%)r)rrrr%^szTerminal.__repr__cCst|jjd|j�dS)N�/)r3rr$r7)rrrrr5aszTerminal.pprintcCs
t|j�S)N)�listr)rrrrr&dszTerminal.all_defectsrcs2dj||jj|jt�j�|js"dn
dj|j��gS)Nz
{}{}/{}({}){}rz {})r#rr$r7rr%r)rr2)rrrr6hszTerminal._ppcCsdS)Nr)rrrr�pop_trailing_wsqszTerminal.pop_trailing_wscCsgS)Nr)rrrrr-uszTerminal.commentscCst|�|jfS)N)rr7)rrrr�__getnewargs__yszTerminal.__getnewargs__)r)r$r9r:r)r<r;r�r%r5r=r&r6r�r-r�r>rr)rrr�Rs	r�c@s eZdZedd��Zdd�ZdS)�WhiteSpaceTerminalcCsdS)Nr@r)rrrrrszWhiteSpaceTerminal.valuecCsdS)NTr)rrrrr(�sz!WhiteSpaceTerminal.startswith_fwsN)r$r9r:r=rr(rrrrr�}sr�c@s eZdZedd��Zdd�ZdS)�
ValueTerminalcCs|S)Nr)rrrrr�szValueTerminal.valuecCsdS)NFr)rrrrr(�szValueTerminal.startswith_fwsN)r$r9r:r=rr(rrrrr��sr�c@s eZdZedd��Zdd�ZdS)�EWWhiteSpaceTerminalcCsdS)Nrr)rrrrr�szEWWhiteSpaceTerminal.valuecCsdS)Nrr)rrrrr"�szEWWhiteSpaceTerminal.__str__N)r$r9r:r=rr"rrrrr��sr�r��,zlist-separatorr~zroute-component-markerz([{}]+)rz[^{}]+rz\\�]z\]z[\x00-\x20\x7F]cCs>t|�}|r|jjtj|��tj|�r:|jjtjd��dS)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)�_non_printable_finderrrSrZNonPrintableDefectrr�r�)�xtextZnon_printablesrrr�_validate_xtext�s

r�cCs�t|d�^}}g}d}d}xbtt|��D]J}||dkrL|rFd}d}nd}q(|rVd}n|||krdP|j||�q(W|d}dj|�dj||d�g|�|fS)akScan printables/quoted-pairs until endchars and return unquoted ptext.

    This function turns a run of qcontent, ccontent-without-comments, or
    dtext-with-quoted-printables into a single string by unquoting any
    quoted printables.  It returns the string, the remaining value, and
    a flag that is True iff there were any quoted printables decoded.

    rjFrTrN)�
_wsp_splitter�rangerkrSr!)r�endcharsZfragment�	remainderZvchars�escape�had_qp�posrrr�_get_ptext_to_endchars�s$	r�cCs.|j�}t|dt|�t|��d�}||fS)z�FWS = 1*WSP

    This isn't the RFC definition.  We're using fws to represent tokens where
    folding can be done, but when we are parsing the *un*folding has already
    been done so we don't need to watch out for CRLF.

    N�fws)r}r�rk)rZnewvaluer�rrr�get_fws�sr�c
Cs�t�}|jd�s tjdj|���|dd�jdd�^}}||dd�krXtjdj|���dj|�}t|�dkr�|dtkr�|dtkr�|jdd�^}}|d|}t|j��dkr�|j	j
tjd	��||_dj|�}yt
jd|d�\}}}}	Wn(tk
�rtjd
j|j���YnX||_||_|j	j|	�xh|�r�|dtk�rdt|�\}
}|j
|
��q6t|d�^}}t|d�}t|�|j
|�dj|�}�q6W||fS)zE encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

    z=?z"expected encoded word but found {}rhNz?=rjrrzwhitespace inside encoded wordz!encoded word format invalid: '{}'�vtext)rN�
startswithr�HeaderParseErrorr#rwr!rkrrrSr�rO�_ewr��
ValueErrorrPrQr,�WSPr�r�r�r�)
r�ewr�r�Zremstr�restr�rPrQrr.�charsr�rrr�get_encoded_word�sH

$




r�cCst�}�x|�r|dtkr4t|�\}}|j|�q
|jd�r�yt|�\}}Wntjk
rdYnrXd}t|�dkr�|dj	dkr�|j
jtjd��d}|r�t|�dkr�|d
j	d	kr�t|dd�|d<|j|�q
t
|d�^}}t|d
�}t|�|j|�dj|�}q
W|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.

    rz=?Trjr�z&missing whitespace before encoded wordFrhzencoded-wordr�rrl���rlrl)rDr�r�rSr�r�rr�rkr7rr�r�r�r�r�r!)rrEr.Zhave_wsr�r�r�rrr�get_unstructured!s:






r�cCs*t|d�\}}}t|d�}t|�||fS)actext = <printable ascii except \ ( )>

    This is not the RFC ctext, since we are handling nested comments in comment
    and unquoting quoted-pairs here.  We allow anything except the '()'
    characters, but if we find any ASCII other than the RFC defined printable
    ASCII, a NonPrintableDefect is added to the token's defects list.  Since
    quoted pairs are converted to their unquoted values, what is returned is
    a 'ptext' token.  In this case it is a WhiteSpaceTerminal, so it's value
    is ' '.

    z()r�)r�r�r�)rr��_rrr�get_qp_ctextYs
r�cCs*t|d�\}}}t|d�}t|�||fS)aoqcontent = qtext / quoted-pair

    We allow anything except the DQUOTE character, but if we find any ASCII
    other than the RFC defined printable ASCII, a NonPrintableDefect is
    added to the token's defects list.  Any quoted pairs are converted to their
    unquoted values, so what is returned is a 'ptext' token.  In this case it
    is a ValueTerminal.

    r
r�)r�r�r�)rr�r�rrr�get_qcontentjs

r�cCsNt|�}|stjdj|���|j�}|t|�d�}t|d�}t|�||fS)z�atext = <matches _atext_matcher>

    We allow any non-ATOM_ENDS in atext, but add an InvalidATextDefect to
    the token's defects list if we find non-atext characters.
    zexpected atext but found '{}'N�atext)�_non_atom_end_matcherrr�r#rarkr�r�)r�mr�rrr�	get_atextys
r�cCs|ddkrtjdj|���t�}|dd�}|ddkrPt|�\}}|j|�x�|r�|ddkr�|dtkr|t|�\}}nd|dd�dkr�y"t|�\}}|j	jtj
d��Wq�tjk
r�t|�\}}Yq�Xnt|�\}}|j|�qRW|�s|j	jtj
d	��||fS||dd�fS)
z�bare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE

    A quoted-string without the leading or trailing white space.  Its
    value is the text between the quote marks, with whitespace
    preserved and quoted pairs decoded.
    rr
zexpected '"' but found '{}'rjNrhz=?z!encoded word inside quoted stringz"end of header inside quoted string)rr�r#rWr�rSr�r�r�rr�)rZbare_quoted_stringr.rrr�get_bare_quoted_string�s2


r�cCs�|r |ddkr tjdj|���t�}|dd�}x^|r�|ddkr�|dtkr^t|�\}}n&|ddkrxt|�\}}nt|�\}}|j|�q4W|s�|j	jtj
d��||fS||dd�fS)z�comment = "(" *([FWS] ccontent) [FWS] ")"
       ccontent = ctext / quoted-pair / comment

    We handle nested comments here, and quoted-pair in our qp-ctext routine.
    rrzexpected '(' but found '{}'rjNrZzend of header inside comment)rr�r#rXr�r��get_commentr�rSrr�)rrAr.rrrr��s"
r�cCsTt�}xD|rJ|dtkrJ|dtkr2t|�\}}nt|�\}}|j|�qW||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS

    r)rI�CFWS_LEADERr�r�r�rS)rrJr.rrr�get_cfws�sr�cCspt�}|r,|dtkr,t|�\}}|j|�t|�\}}|j|�|rh|dtkrht|�\}}|j|�||fS)z�quoted-string = [CFWS] <bare-quoted-string> [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)rRr�r�rSr�)rZ
quoted_stringr.rrr�get_quoted_string�s


r�cCs�t�}|r,|dtkr,t|�\}}|j|�|rL|dtkrLtjdj|���|jd�r�yt	|�\}}Wq�tjk
r�t
|�\}}Yq�Xnt
|�\}}|j|�|r�|dtkr�t|�\}}|j|�||fS)zPatom = [CFWS] 1*atext [CFWS]

    An atom could be an rfc2047 encoded word.
    rzexpected atom but found '{}'z=?)rKr�r�rS�	ATOM_ENDSrr�r#r�r�r�)rrLr.rrr�get_atom�s$



r�cCs�t�}|s|dtkr(tjdj|���xP|rx|dtkrxt|�\}}|j|�|r*|ddkr*|jt�|dd�}q*W|dtkr�tjdjd|���||fS)z( dot-text = 1*atext *("." 1*atext)

    rz8expected atom at a start of dot-atom-text but found '{}'r	rjNz4expected atom at end of dot-atom-text but found '{}'rl)ryr�rr�r#r�rSr�)rZ
dot_atom_textr.rrr�get_dot_atom_text�s

r�cCs�t�}|dtkr(t|�\}}|j|�|jd�rhyt|�\}}Wqttjk
rdt|�\}}YqtXnt|�\}}|j|�|r�|dtkr�t|�\}}|j|�||fS)z� dot-atom = [CFWS] dot-atom-text [CFWS]

    Any place we can have a dot atom, we could instead have an rfc2047 encoded
    word.
    rz=?)	rxr�r�rSr�r�rr�r�)rZdot_atomr.rrr�get_dot_atoms



r�cCs�|dtkrt|�\}}nd}|ddkr8t|�\}}n*|dtkrVtjdj|���nt|�\}}|dk	rx|g|dd�<||fS)a�word = atom / quoted-string

    Either atom or quoted-string may start with CFWS.  We have to peel off this
    CFWS first to determine which type of word to parse.  Afterward we splice
    the leading CFWS, if any, into the parsed sub-token.

    If neither an atom or a quoted-string is found before the next special, a
    HeaderParseError is raised.

    The token returned is either an Atom or a QuotedString, as appropriate.
    This means the 'word' level of the formal grammar is not represented in the
    parse tree; this is because having that extra layer when manipulating the
    parse tree is more confusing than it is helpful.

    rNr
z1Expected 'atom' or 'quoted-string' but found '{}')r�r�r��SPECIALSrr�r#r�)r�leaderr.rrr�get_word*s
r�cCs�t�}yt|�\}}|j|�Wn(tjk
rH|jjtjd��YnXx�|r�|dtkr�|ddkr�|jt�|jjtj	d��|dd�}qLyt|�\}}WnDtjk
r�|dt
kr�t|�\}}|jjtj	d��n�YnX|j|�qLW||fS)a� phrase = 1*word / obs-phrase
        obs-phrase = word *(word / "." / CFWS)

    This means a phrase can be a sequence of words, periods, and CFWS in any
    order as long as it starts with at least one word.  If anything other than
    words is detected, an ObsoleteHeaderDefect is added to the token's defect
    list.  We also accept a phrase that starts with CFWS followed by a dot;
    this is registered as an InvalidHeaderDefect, since it is not supported by
    even the obsolete grammar.

    zphrase does not start with wordrr	zperiod in 'phrase'rjNzcomment found without atom)rFr�rSrr�rr��PHRASE_ENDSr��ObsoleteHeaderDefectr�r�)rrGr.rrr�
get_phraseIs.




r�cCstt�}d}|dtkr"t|�\}}|s6tjdj|���yt|�\}}Wn^tjk
r�yt|�\}}Wn6tjk
r�|ddkr�|dtkr��t	�}YnXYnX|dk	r�|g|dd�<|j
|�|o�|ddks�|dtk�r2tt|�|�\}}|j
dk�r|jj
tjd��n|jj
tjd��||d<y|jjd�Wn(tk
�rj|jj
tjd	��YnX||fS)
z= local-part = dot-atom / quoted-string / obs-local-part

    Nrz"expected local-part but found '{}'rzinvalid-obs-local-partz<local-part is not dot-atom, quoted-string, or obs-local-partz,local-part is not a dot-atom (contains CFWS)�asciiz)local-part contains non-ASCII characters))r�r�r�rr�r#r�r�r�rrS�get_obs_local_partrr7rr�r�r�encoder�ZNonASCIILocalPartDefect)rrmr�r.�obs_local_partrrr�get_local_partosB




r�cCs�t�}d}�x|o(|ddks,|dtk�r*|ddkrl|rN|jjtjd��|jt�d}|dd�}qnD|ddkr�|jt|dd	��|dd�}|jjtjd
��d}q|r�|djdkr�|jjtjd��yt	|�\}}d}Wn4tj
k
�r|dtk�r�t|�\}}YnX|j|�qW|djdk�s\|djd
k�rn|djdk�rn|jjtjd��|djdk�s�|djd
k�r�|djdk�r�|jjtjd��|j�r�d|_||fS)z' obs-local-part = word *("." word)
    Frrr	zinvalid repeated '.'TrjNzmisplaced-specialz/'\' character outside of quoted-string/ccontentr�zmissing '.' between wordsrJz!Invalid leading '.' in local partrhz"Invalid trailing '.' in local partzinvalid-obs-local-partrlrlrlr�)
r�r�rrSrr�r�r�r7r�r�r�r�)rr�Zlast_non_ws_was_dotr.rrrr��sV"





r�cCs@t|d�\}}}t|d�}|r0|jjtjd��t|�||fS)a dtext = <printable ascii except \ [ ]> / obs-dtext
        obs-dtext = obs-NO-WS-CTL / quoted-pair

    We allow anything except the excluded characters, but if we find any
    ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is
    added to the token's defects list.  Quoted pairs are converted to their
    unquoted values, so what is returned is a ptext token, in this case a
    ValueTerminal.  If there were quoted-printables, an ObsoleteHeaderDefect is
    added to the returned token's defect list.

    z[]r�z(quoted printable found in domain-literal)r�r�rrSrr�r�)rr�r�rrr�	get_dtext�s

rcCs,|rdS|jtjd��|jtdd��dS)NFz"end of input inside domain-literalr�zdomain-literal-endT)rSrr�r�)r�domain_literalrrr�_check_for_early_dl_end�srcCslt�}|dtkr(t|�\}}|j|�|s6tjd��|ddkrRtjdj|���|dd�}t||�rp||fS|jtdd��|dt	kr�t
|�\}}|j|�t|�\}}|j|�t||�r�||fS|dt	kr�t
|�\}}|j|�t||��r||fS|ddk�rtjd	j|���|jtdd
��|dd�}|�rd|dtk�rdt|�\}}|j|�||fS)zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS]

    rzexpected domain-literal�[z6expected '[' at start of domain-literal but found '{}'rjNzdomain-literal-startr�z4expected ']' at end of domain-literal but found '{}'zdomain-literal-end)r�r�r�rSrr�r#rr�r�r�r)rrr.rrr�get_domain_literal�sD







rcCstt�}d}|dtkr"t|�\}}|s6tjdj|���|ddkrvt|�\}}|dk	rd|g|dd�<|j|�||fSyt|�\}}Wn"tjk
r�t	|�\}}YnX|r�|ddkr�tjd��|dk	r�|g|dd�<|j|�|o�|ddk�rl|j
jtjd��|djd	k�r(|d|dd�<xB|�rj|ddk�rj|jt
�t	|d
d��\}}|j|��q*W||fS)z] domain = dot-atom / domain-literal / obs-domain
        obs-domain = atom *("." atom))

    Nrzexpected domain but found '{}'rr~zInvalid Domainr	z(domain is not a dot-atom (contains CFWS)zdot-atomrj)rvr�r�rr�r#rrSr�r�rr�r7r�)rrnr�r.rrr�
get_domains@




rcCs~t�}t|�\}}|j|�|s.|ddkrH|jjtjd��||fS|jtdd��t|dd��\}}|j|�||fS)z( addr-spec = local-part "@" domain

    rr~z"add-spec local part with no domainzaddress-at-symbolrjN)rzr�rSrrr�r�r)rrpr.rrr�
get_addr_spec.s


rcCs�t�}xf|rl|ddks$|dtkrl|dtkrHt|�\}}|j|�q|ddkr|jt�|dd�}qW|s�|ddkr�tjdj|���|jt�t	|dd��\}}|j|�x�|o�|ddk�rB|jt�|dd�}|s�P|dtk�rt|�\}}|j|�|ddkr�|jt�t	|dd��\}}|j|�q�W|�sTtjd��|ddk�rrtjd	j|���|jt
dd
��||dd�fS)z� obs-route = obs-domain-list ":"
        obs-domain-list = *(CFWS / ",") "@" domain *("," [CFWS] ["@" domain])

        Returns an obs-route token with the appropriate sub-tokens (that is,
        there is no obs-domain-list in the parse tree).
    rr�rjNr~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)rsr�r�rS�
ListSeparatorrr�r#�RouteComponentMarkerrr�)rZ	obs_router.rrr�
get_obs_route>sB






r
cCs�t�}|dtkr(t|�\}}|j|�|s:|ddkrJtjdj|���|jtdd��|dd�}|ddkr�|jtdd��|jjtj	d	��|dd�}||fSyt
|�\}}Wnztjk
�r2y"t|�\}}|jjtjd
��Wn(tjk
�rtjdj|���YnX|j|�t
|�\}}YnX|j|�|�r`|ddk�r`|dd�}n|jjtj	d��|jtdd��|�r�|dtk�r�t|�\}}|j|�||fS)
z� angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr
        obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS]

    r�<z"expected angle-addr but found '{}'zangle-addr-startrjN�>zangle-addr-endznull addr-spec in angle-addrz*obsolete route specification in angle-addrz.expected addr-spec or obs-route but found '{}'z"missing trailing '>' on angle-addr)
rqr�r�rSrr�r#r�rr�rr
r�)rZ
angle_addrr.rrr�get_angle_addrgsJ






r
cCs<t�}t|�\}}|j|dd��|jdd�|_||fS)z� display-name = phrase

    Because this is simply a name-rule, we don't return a display-name
    token containing a phrase, but rather a display-name token with
    the content of the phrase.

    N)r�r�r,r)rrbr.rrr�get_display_name�s
rcCs�t�}d}|dtkr6t|�\}}|s6tjdj|���|ddkr�|dtkr^tjdj|���t|�\}}|s~tjdj|���|dk	r�|g|ddd�<d}|j|�t	|�\}}|dk	r�|g|dd�<|j|�||fS)z, name-addr = [display-name] angle-addr

    Nrz!expected name-addr but found '{}'r)
rir�r�rr�r#r�rrSr
)rZ	name_addrr�r.rrr�
get_name_addr�s0

rcCs�t�}yt|�\}}WnNtjk
rdyt|�\}}Wn&tjk
r^tjdj|���YnXYnXtdd�|jD��r�d|_|j	|�||fS)z& mailbox = name-addr / addr-spec

    zexpected mailbox but found '{}'css|]}t|tj�VqdS)N)r�rr�)rrrrrr �szget_mailbox.<locals>.<genexpr>zinvalid-mailbox)
rtrrr�rr#�anyr&r7rS)rrcr.rrr�get_mailbox�s
rcCsht�}xX|r^|d|kr^|dtkrF|jt|dd��|dd�}qt|�\}}|j|�qW||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.

    rzmisplaced-specialrjN)rur�rSr�r�)rr�Zinvalid_mailboxr.rrr�get_invalid_mailbox�srcCs�t�}�x�|o|ddk�r�yt|�\}}|j|�W�ntjk
�r@d}|dtkr�t|�\}}|sz|ddkr�|j|�|jjtjd��n@t	|d�\}}|dk	r�|g|dd�<|j|�|jjtj
d��nb|ddkr�|jjtjd��nBt	|d�\}}|dk	�r |g|dd�<|j|�|jjtj
d��YnX|�r�|ddk�r�|d
}d	|_t	|d�\}}|j|�|jjtj
d��|r
|ddkr
|jt
�|dd�}q
W||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-listr�rjzinvalid-mailboxrl)rdrrSrr�r�r�rr�rr�r7r,r)rZmailbox_listr.r�rcrrr�get_mailbox_list�sN













rcCst�}|s$|jjtjd��||fSd}|r�|dtkr�t|�\}}|sl|jjtjd��|j|�||fS|ddkr�|j|�||fSt|�\}}t|j	�dkr�|dk	r�|j|�|j
|�|jjtjd��||fS|dk	r�|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-listrzgroup-list with empty entries)rfrrSrr�r�r�rrkr_r,r�)rZ
group_listr�r.rrr�get_group_list"s8







rcCs"t�}t|�\}}|s$|ddkr4tjdj|���|j|�|jtdd��|dd�}|r�|ddkr�|jtdd��||dd�fSt|�\}}|j|�|s�|jjtj	d	��n|ddkr�tjd
j|���|jtdd��|dd�}|�r|dt
k�rt|�\}}|j|�||fS)z7 group = display-name ":" [group-list] ";" [CFWS]

    rrz8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorrjNrzgroup-terminatorzend of header in groupz)expected ';' at end of group but found {})rgrrr�r#rSr�rrr�r�r�)rrar.rrr�	get_groupGs2




rcCsxt�}yt|�\}}WnNtjk
rdyt|�\}}Wn&tjk
r^tjdj|���YnXYnX|j|�||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�rr#rS)rr\r.rrr�get_addresses
rcCs�t�}�x�|�r�yt|�\}}|j|�W�n$tjk
�rP}�zd}|dtkr�t|�\}}|sr|ddkr�|j|�|jjtjd��nFt	|d�\}}|dk	r�|g|dd�<|jt
|g��|jjtjd��nh|ddkr�|jjtjd��nHt	|d�\}}|dk	�r|g|dd�<|jt
|g��|jjtjd��WYdd}~XnX|�r�|ddk�r�|d
d}d|_t	|d�\}}|j
|�|jjtjd��|r
|jtdd	��|dd�}q
W||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.

    Nrr�z"address-list entry with no contentzinvalid address in address-listzempty element in address-listrjzinvalid-mailboxzlist-separatorrl)r[rrSrr�r�r�rr�rr`r�r7r,r�)rZaddress_listr.�errr�rcrrr�get_address_list�sN












rcCs�t�}|s |jjtjd��|S|dtkrXt|�\}}|j|�|sX|jjtjd��d}x8|r�|ddkr�|dtkr�||d7}|dd�}q^W|j�s�|jjtjdj	|���|jt
|d	��nt|�|_|jt
|d
��|o�|dtk�r
t|�\}}|j|�|�s |ddk�rX|jdk	�r>|jjtjd��|�rT|jt
|d	��|S|jt
dd��|dd�}|�r�|dtk�r�t|�\}}|j|�|�s�|jdk	�r�|jjtjd��|Sd}x2|�r�|dtk�r�||d7}|dd�}�q�W|j��s2|jjtjd
j	|���|jt
|d	��nt|�|_
|jt
|d
��|�rv|dtk�rvt|�\}}|j|�|�r�|jjtjd��|jt
|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 CFWSrr	rjNz1Expected MIME major version number but found {!r}r��digitsz0Incomplete MIME version; found only major numberzversion-separatorz1Expected MIME minor version number but found {!r}z'Excess non-CFWS text after MIME version)r�rrSr�HeaderMissingRequiredValuer�r��isdigitr�r#r��intr�r�)rZmime_versionr.rrrr�parse_mime_version�sv













rcCsht�}xX|r^|ddkr^|dtkrF|jt|dd��|dd�}qt|�\}}|j|�qW||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.

    rrzmisplaced-specialrjN)r�r�rSr�r�)rZinvalid_parameterr.rrr�get_invalid_parametersrcCsNt|�}|stjdj|���|j�}|t|�d�}t|d�}t|�||fS)a8ttext = <matches _ttext_matcher>

    We allow any non-TOKEN_ENDS in ttext, but add defects to the token's
    defects list if we find non-ttext characters.  We also register defects for
    *any* non-printables even though the RFC doesn't exclude all of them,
    because we follow the spirit of RFC 5322.

    zexpected ttext but found '{}'N�ttext)�_non_token_end_matcherrr�r#rarkr�r�)rr�r rrr�	get_ttexts	
r"cCs�t�}|r,|dtkr,t|�\}}|j|�|rL|dtkrLtjdj|���t|�\}}|j|�|r�|dtkr�t|�\}}|j|�||fS)z�token = [CFWS] 1*ttext [CFWS]

    The RFC equivalent of ttext is any US-ASCII chars except space, ctls, or
    tspecials.  We also exclude tabs even though the RFC doesn't.

    The RFC implies the CFWS but is not explicit about it in the BNF.

    rzexpected token but found '{}')	rMr�r�rS�
TOKEN_ENDSrr�r#r")rZmtokenr.rrr�	get_token)s	


r$cCsNt|�}|stjdj|���|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}Nr�)�_non_attribute_end_matcherrr�r#rarkr�r�)rr�r�rrr�get_attrtext@s	
r&cCs�t�}|r,|dtkr,t|�\}}|j|�|rL|dtkrLtjdj|���t|�\}}|j|�|r�|dtkr�t|�\}}|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.

    rzexpected token but found '{}')	r�r�r�rS�ATTRIBUTE_ENDSrr�r#r&)rr�r.rrr�
get_attributeSs	


r(cCsNt|�}|stjdj|���|j�}|t|�d�}t|d�}t|�||fS)z�attrtext = 1*(any non-ATTRIBUTE_ENDS character plus '%')

    This is a special parsing routine so that we get a value that
    includes % escapes as a single string (which we decode as a single
    string later).

    z)expected extended attrtext but found {!r}Nzextended-attrtext)�#_non_extended_attribute_end_matcherrr�r#rarkr�r�)rr�r�rrr�get_extended_attrtextjs
r*cCs�t�}|r,|dtkr,t|�\}}|j|�|rL|dtkrLtjdj|���t|�\}}|j|�|r�|dtkr�t|�\}}|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.

    rzexpected token but found '{}')	r�r�r�rS�EXTENDED_ATTRIBUTE_ENDSrr�r#r*)rr�r.rrr�get_extended_attribute|s


r,cCs�t�}|s|ddkr(tjdj|���|jtdd��|dd�}|sX|dj�rhtjdj|���d}x,|r�|dj�r�||d7}|dd�}qnW|dd	kr�|d	kr�|jjtjd
��t	|�|_
|jt|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-markerrjNz$Expected section number but found {}r�0z'section number has an invalid leading 0r)r�rr�r#rSr�rrZInvalidHeaderErrorrr�)rr�rrrr�get_section�s&	

r/cCs�t�}|stjd��d}|dtkr0t|�\}}|sDtjdj|���|ddkr^t|�\}}nt|�\}}|dk	r�|g|dd�<|j|�||fS)z  quoted-string / attribute

    z&Expected value but found end of stringNrz Expected value but found only {}r
)	r�rr�r�r�r#r�r,rS)r�vr�r.rrr�	get_value�s 

r1cCs�t�}t|�\}}|j|�|s.|ddkrN|jjtjdj|���||fS|ddkr�y t|�\}}d|_|j|�Wntj	k
r�YnX|s�tj	d��|ddkr�|jt
dd��|dd	�}d|_|dd
kr�tj	d��|jt
d
d��|dd	�}d	}|�r.|dtk�r.t
|�\}}|j|�d	}|}|j�rH|�rH|dd
k�rHt|�\}}|j}d}|jdk�r�|�r�|ddk�r�d}n$t|�\}}	|	�r�|	ddk�r�d}n(yt|�\}}	WnYnX|	�s�d}|�r2|jjtjd��|j|�x,|D]$}
|
jdk�rg|
d	d	�<|
}P�qW|}nd	}|jjtjd��|�rb|ddk�rbd	}nt|�\}}|j�s�|jdk�r�|�s�|ddk�r�|j|�|d	k	�r�|�s�t|��|}||fS|jjtjd��|�s|jjtjd��|j|�|d	k�r||fSn�|d	k	�rVx|D]}
|
jdk�r"P�q"W|
jdk|j|
�|
j|_|ddk�rttj	dj|���|jt
dd��|dd	�}|�r�|ddk�r�t|�\}}|j|�|j|_|�s�|ddk�r�tj	dj|���|jt
dd��|dd	�}|d	k	�rZt�}x>|�rR|dtk�r8t|�\}}nt|�\}}|j|��qW|}nt|�\}}|j|�|d	k	�r�|�s�t|��|}||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 valuer-TzIncomplete parameterzextended-parameter-markerrjN�=zParameter not followed by '='zparameter-separatorr
F�'z5Quoted string value for extended parameter is invalidzbare-quoted-stringzZParameter 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 delimiterszextended-attrtextr�z=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {})r�r(rSrrr�r#r/r�r�r�r�r�r�r�rVr�r&r*r7r1�AssertionErrorrrPrQr�r�r�r�)rr�r.r�r�ZappendtoZqstringZinner_valueZ
semi_validr��tr0rrr�
get_parameter�s�























r6cCsht�}�xZ|�rbyt|�\}}|j|�Wn�tjk
r�}z�d}|dtkrZt|�\}}|sl|j|�|S|ddkr�|dk	r�|j|�|jjtjd��n@t	|�\}}|r�|g|dd�<|j|�|jjtjdj
|���WYdd}~XnX|�r@|ddk�r@|d
}d|_t	|�\}}|j|�|jjtjdj
|���|r
|jt
dd	��|dd�}q
W|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}rjzinvalid-parameterz)parameter with invalid trailing text {!r}zparameter-separatorrl)r�r6rSrr�r�r�rr�rr#r7r,r�)rZmime_parametersr.rr�r�rrr�parse_mime_parametersQ	sD







 

r7cCs�xX|rX|ddkrX|dtkr@|jt|dd��|dd�}qt|�\}}|j|�qW|sbdS|jtdd��|jt|dd���dS)zBDo our best to find the parameters in an invalid MIME header

    rrzmisplaced-specialrjNzparameter-separator)r�rSr�r�r7)Z	tokenlistrr.rrr�_find_mime_parameters�	sr8cCs�t�}d}|s$|jjtjd��|Syt|�\}}Wn8tjk
rl|jjtjdj|���t	||�|SX|j|�|s�|ddkr�|jjtjd��|r�t	||�|S|j
j�j�|_
|jtdd��|dd	�}yt|�\}}Wn:tjk
�r$|jjtjd
j|���t	||�|SX|j|�|j
j�j�|_|�sJ|S|ddk�r�|jjtjdj|���|`
|`t	||�|S|jtdd
��|jt|dd	���|S)z� maintype "/" subtype *( ";" parameter )

    The maintype and substype are tokens.  Theoretically they could
    be checked against the official IANA list + x-token, but we
    don't do that.
    Fz"Missing content type specificationz(Expected content maintype but found {!r}rr�zInvalid content typezcontent-type-separatorrjNz'Expected content subtype but found {!r}rz<Only parameters are valid after content type, but found {!r}zparameter-separator)r�rrSrrr$r�r�r#r8rr��lowerr�r�r�r7)rZctypeZrecoverr.rrr�parse_content_type_header�	sX











r:c
Cs�t�}|s |jjtjd��|Syt|�\}}Wn8tjk
rh|jjtjdj|���t	||�|SX|j|�|j
j�j�|_
|s�|S|ddkr�|jjtjdj|���t	||�|S|jtdd��|jt|dd���|S)	z* disposition-type *( ";" parameter )

    zMissing content dispositionz+Expected content disposition but found {!r}rrzCOnly parameters are valid after content disposition, but found {!r}zparameter-separatorrjN)r�rrSrrr$r�r�r#r8rr�r9r�r�r7)rZdisp_headerr.rrr� parse_content_disposition_header�	s2






r;cCs�t�}|s |jjtjd��|Syt|�\}}Wn.tjk
r^|jjtjdj|���YnX|j|�|j	j
�j�|_|s�|Sx^|r�|jjtjd��|dt
kr�|jt|dd��|dd�}q�t|�\}}|j|�q�W|S)z mechanism

    z!Missing content transfer encodingz1Expected content transfer encoding but found {!r}z*Extra text after content transfer encodingrzmisplaced-specialrjN)r�rrSrrr$r�r�r#rr�r9rOr�r�r�)rZ
cte_headerr.rrr�&parse_content_transfer_encoding_header�	s.



r<cCsDd}|r@|dr@|ddtkr@|dd}|ddd	�|d
<|S)Nrrjrlrlrlrlrlrlrlrl)r�)�linesZwsprrr�_steal_trailing_WSP_if_exists
s
r>cCs�|jptd�}|jrdnd}dg}d}d}d}tdd�}t|�}	�xr|	�r�|	jd�}
|
|krf|d	8}qDt|
�}|s�|
jd
kr�tj	|�}nt
j	|�}y|j|�|}Wn6tk
r�t
dd�|
jD��r�d
}nd}d}YnX|
jdkr�t|
|||�qD|�r�|�r�|
j�s~d}d}|
j�r~|
j|d�dd�}
|j|
k�r~t|
�|t|d�k�rlt|�}|j|�|d|
7<qDt|
d��s�t|
�|	}	nt|||||
j|�}d}qDt|�|t|d�k�r�|d|7<qD|
j�r t|�d	|k�r t|�}|�s|
j��r |j||�qDt|
d��sXt|
�}|
j�sN|d	7}|j|�||	}	qD|
j�rz|�rz|	jd|
�d}qDt|�}|�s�|
j��r�|j||�qD|d|7<qDW|jj|�|jS)zLReturn string of contents of parse_tree folded according to RFC rules.

    z+infzutf-8zus-asciirNrF�wrap_as_ew_blockedrjr�css|]}t|tj�VqdS)N)r�rr�)rrrrrr @
sz%_refold_parse_tree.<locals>.<genexpr>zunknown-8bitTzmime-parameters)r/r�rlrlrlrlrlrl)Zmax_line_length�float�utf8r�r�r�rr7�
SPECIALSNL�
isdisjoint�NLSETr�r�rr&�_fold_mime_parametersr)r;r1�lineseprkr>rSr8�_fold_as_ewr<r(�insertr!)Z
parse_treer/�maxlenr�r=�last_ewr?Z
want_encodingZend_ew_not_allowedr�r*�tstrrPZencoded_part�newlineZnewpartsrrrr0"
s�










r0cCs�|dk	r<|r<tt|d
|d�|��}|dd|�|d<|dtkr�|d}|dd�}t|d
�|krz|jt|��|d|7<d}|dtkr�|d}|dd�}|dkr�t|d�n|}x�|�r�|t|d�}	|dkr�dn|}
|	t|
�d}|dk�r|jd�q�|d|�}tj||
d	�}
t|
�|	}|dk�r\|d|�}tj|�}
|d|
7<|t|�d�}|r�|jd�t|d�}q�W|d|7<|�r�|SdS)a�Fold string to_encode into lines as encoded word, combining if allowed.
    Return the new value for last_ew, or None if ew_combine_allowed is False.

    If there is already an encoded word in the last line of lines (indicated by
    a non-None value for last_ew) and ew_combine_allowed is true, decode the
    existing ew, combine it with to_encode, and re-encode.  Otherwise, encode
    to_encode.  In either case, split to_encode as necessary so that the
    encoded segments fit within maxlen.

    Nrjrrzus-asciizutf-8�r@)rPrlrlrlrlrlrlrlrlrlrlrlrlrl)rr�r�rkrSr>r�r�)Z	to_encoder=rIrJr<rPZleading_wspZtrailing_wspZnew_last_ewZremaining_spaceZ	encode_asZ
text_spaceZ
first_partr�ZexcessrrrrG�
sF




rGcCs��x�|jD�]�\}}|dj�jd�s6|dd7<|}d}y|j|�d}Wn0tk
r�d}tj|�rxd}d}nd}YnX|r�tjj	|d	|d
�}	dj
|||	�}
ndj
|t|��}
t|d�t|
�d|kr�|dd
|
|d<q
n"t|
�d|k�r|j
d
|
�q
d}|d}x�|�r�t|�tt|��dt|�}
||
dk�rTd}||
d}}x<|d|�}tjj	|d	|d
�}	t|	�|k�r�P|d8}�qfW|j
dj
||||	��d	}|d7}||d�}|�r|dd7<�qWq
WdS)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'.

    rjr�strictFTzunknown-8bitr�zutf-8r)Zsaferz
{}*={}''{}z{}={}r@rhrz''r{�NNz {}*{}*={}{}rlrlrlrlrlrl)r�r|r�r�r�rr�r�r�rYr#rrkrSr)r*r=rIr�r�rrPZ
error_handlerZencoding_requiredZ
encoded_valuerKr�Zextra_chromeZ
chrome_lenZ
splitpointZmaxchars�partialrrrrE�
s\


 rE)��__doc__�rer��stringr�collectionsr�operatorrZemailrr�rrrr�r�r�r�r�r�Z	TSPECIALSr#Z	ASPECIALSr'r+rDrBrr�rr?rDrFrHrIrKrMrNrRrWrXr[r`rdrfrgrirqrsrtrurvrxryrzr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�rr	�compiler#r!rwr�r�matchr��findallr�r!r%r)r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrr
r
rrrrrrrrrrrr"r$r&r(r*r,r/r1r6r7r8r:r;r<r>r0rGrErrrr�<module>DsC"	
!($
V	+




   

*8"
&'/'&).9%>D49/j7

?>