Your IP : 3.142.174.8


Current Path : /opt/alt/python38/lib64/python3.8/email/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/email/__pycache__/_header_value_parser.cpython-38.pyc

U

i�f���	@s�dZddlZddlZddlZddlmZddlmZddlm	Z
ddlmZddlmZe
d�Zee
d	�BZe
d
�ZeeBZee
d�Zee
d�Zee
d
�Be
d�ZeeBZee
d�BZeeBZee
d�ZddhZeeBZdd�Ze�dejejB�ZGdd�de �Z!Gdd�de!�Z"Gdd�de!�Z#Gdd�de!�Z$Gdd�de!�Z%Gdd �d e"�Z&Gd!d"�d"e!�Z'Gd#d$�d$e!�Z(Gd%d&�d&e!�Z)Gd'd(�d(e!�Z*Gd)d*�d*e*�Z+Gd+d,�d,e"�Z,Gd-d.�d.e!�Z-Gd/d0�d0e!�Z.Gd1d2�d2e!�Z/Gd3d4�d4e!�Z0Gd5d6�d6e!�Z1Gd7d8�d8e!�Z2Gd9d:�d:e!�Z3Gd;d<�d<e!�Z4Gd=d>�d>e!�Z5Gd?d@�d@e!�Z6GdAdB�dBe!�Z7GdCdD�dDe!�Z8GdEdF�dFe!�Z9GdGdH�dHe!�Z:GdIdJ�dJe!�Z;GdKdL�dLe!�Z<GdMdN�dNe$�Z=GdOdP�dPe!�Z>GdQdR�dRe!�Z?GdSdT�dTe!�Z@GdUdV�dVe!�ZAGdWdX�dXeA�ZBGdYdZ�dZe!�ZCGd[d\�d\e!�ZDGd]d^�d^e!�ZEGd_d`�d`e!�ZFGdadb�dbe!�ZGGdcdd�ddeG�ZHGdedf�dfeG�ZIGdgdh�dhe!�ZJGdidj�dje!�ZKGdkdl�dle!�ZLGdmdn�dneL�ZMGdodp�dpeM�ZNGdqdr�dre!�ZOGdsdt�dteP�ZQGdudv�dveQ�ZRGdwdx�dxeQ�ZSGdydz�dzeR�ZTGd{d|�d|ejU�ZVeSdd}�ZWeSd~d�ZXeSd�d��ZYe�d��Zd��[e���j\Z]e�d��Ze�^d��[e����j_Z`e�d��jaZbe�d��Ze�^d��[e����j_Zce�d��Ze�^d��[e����j_Zde�d��Ze�^d��[e����j_Zed�d��Zfd�d��Zgd�d��Zhd�d��Zid�d��Zjd�d��Zkd�d��Zld�d��Zmd�d��Znd�d��Zod�d��Zpd�d��Zqd�d��Zrd�d��Zsd�d��Ztd�d��Zud�d��Zvd�d��Zwd�d��Zxd�d��Zyd�d��Zzd�d��Z{d�d��Z|d�d��Z}d�d��Z~d�d��Zd�d��Z�d�d��Z�d�d��Z�d�d��Z�d�dÄZ�d�dńZ�d�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�d�d�Z�d�d�Z�d�d�Z�d�d��Z�d�d��Z�d�d��Z�dS)�alHeader value parser implementing various email-related RFC parsing rules.

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

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

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

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

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

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

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

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

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

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

XXX: provide complete list of token types.
�N)�	hexdigits)�
itemgetter)�_encoded_words)�errors)�utilsz 	�(z
()<>@,:;.\"[]�.z."(z/?=z*'%�%�
�
cCs dt|��dd��dd�dS)N�"�\�\\z\")�str�replace��value�r�?/opt/alt/python38/lib64/python3.8/email/_header_value_parser.py�quote_stringbsrz�
   =\?            # literal =?
   [^?]*          # charset
   \?             # literal ?
   [qQbB]         # literal 'q' or 'b', case insensitive
   \?             # literal ?
  .*?             # encoded word
  \?=             # literal ?=
cs�eZdZdZdZdZ�fdd�Zdd�Z�fdd�Ze	d	d
��Z
e	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__rrr{szTokenList.__init__cCsd�dd�|D��S)N�css|]}t|�VqdSr�r��.0�xrrr�	<genexpr>�sz$TokenList.__str__.<locals>.<genexpr>��join�rrrr�__str__szTokenList.__str__csd�|jjt����S�Nz{}({})��formatr�__name__r�__repr__r(rrrr.�s
�zTokenList.__repr__cCsd�dd�|D��S)Nr css|]}|jr|jVqdSrrr"rrrr%�sz"TokenList.value.<locals>.<genexpr>r&r(rrrr�szTokenList.valuecCstdd�|D�|j�S)Ncss|]}|jVqdSr)�all_defectsr"rrrr%�sz(TokenList.all_defects.<locals>.<genexpr>)�sumrr(rrrr/�szTokenList.all_defectscCs|d��S�Nr)�startswith_fwsr(rrrr2�szTokenList.startswith_fwscCstdd�|D��S)zATrue if all top level tokens of this part may be RFC2047 encoded.css|]}|jVqdSr)�
as_ew_allowed)r#�partrrrr%�sz*TokenList.as_ew_allowed.<locals>.<genexpr>)�allr(rrrr3�szTokenList.as_ew_allowedcCsg}|D]}|�|j�q|Sr)�extend�comments)rr7�tokenrrrr7�szTokenList.commentscCst||d�S)N��policy)�_refold_parse_tree�rr:rrr�fold�szTokenList.foldr cCst|j|d��dS)N��indent)�print�ppstr�rr?rrr�pprint�szTokenList.pprintcCsd�|j|d��S)Nr
r>)r'�_pprBrrrrA�szTokenList.ppstrccszd�||jj|j�V|D]4}t|d�s:|d�|�Vq|�|d�EdHq|jrdd�|j�}nd}d�||�VdS)Nz{}{}/{}(rDz*    !! invalid element in token list: {!r}z    z Defects: {}r z{}){})r,rr-�
token_type�hasattrrDr)rr?r8ZextrarrrrD�s�
�
z
TokenList._pp)r )r )r )r-�
__module__�__qualname__rE�syntactic_break�ew_combine_allowedrr)r.�propertyrr/r2r3r7r=rCrArD�
__classcell__rrrrrus&





rc@s$eZdZedd��Zedd��ZdS)�WhiteSpaceTokenListcCsdS�N� rr(rrrr�szWhiteSpaceTokenList.valuecCsdd�|D�S)NcSsg|]}|jdkr|j�qS)�comment)rE�contentr"rrr�
<listcomp>�s
z0WhiteSpaceTokenList.comments.<locals>.<listcomp>rr(rrrr7�szWhiteSpaceTokenList.commentsN)r-rGrHrKrr7rrrrrM�s
rMc@seZdZdZdS)�UnstructuredTokenList�unstructuredN�r-rGrHrErrrrrS�srSc@seZdZdZdS)�Phrase�phraseNrUrrrrrV�srVc@seZdZdZdS)�WordZwordNrUrrrrrX�srXc@seZdZdZdS)�CFWSList�cfwsNrUrrrrrY�srYc@seZdZdZdS)�Atom�atomNrUrrrrr[�sr[c@seZdZdZdZdS)�Tokenr8FN)r-rGrHrEZencode_as_ewrrrrr]�sr]c@seZdZdZdZdZdZdS)�EncodedWord�encoded-wordN)r-rGrHrE�cte�charset�langrrrrr^�sr^c@s4eZdZdZedd��Zedd��Zedd��ZdS)	�QuotedString�
quoted-stringcCs"|D]}|jdkr|jSqdS�N�bare-quoted-string�rEr�rr$rrrrQ�s
zQuotedString.contentcCs>g}|D]*}|jdkr&|�t|��q|�|j�qd�|�S)Nrfr )rE�appendrrr')r�resr$rrr�quoted_value�s
zQuotedString.quoted_valuecCs"|D]}|jdkr|jSqdSrerg�rr8rrr�stripped_value�s
zQuotedString.stripped_valueN)r-rGrHrErKrQrkrmrrrrrc�s

	rcc@s$eZdZdZdd�Zedd��ZdS)�BareQuotedStringrfcCstd�dd�|D���S)Nr css|]}t|�VqdSrr!r"rrrr%sz+BareQuotedString.__str__.<locals>.<genexpr>)rr'r(rrrr)szBareQuotedString.__str__cCsd�dd�|D��S)Nr css|]}t|�VqdSrr!r"rrrr%sz)BareQuotedString.value.<locals>.<genexpr>r&r(rrrrszBareQuotedString.valueN)r-rGrHrEr)rKrrrrrrn�srnc@s8eZdZdZdd�Zdd�Zedd��Zedd	��Zd
S)�CommentrPcs(d�tdg�fdd��D�dggg��S)Nr rcsg|]}��|��qSr)�quoter"r(rrrRsz#Comment.__str__.<locals>.<listcomp>�))r'r0r(rr(rr)
s��zComment.__str__cCs2|jdkrt|�St|��dd��dd��dd�S)NrPr
rrz\(rqz\))rErr)rrrrrrps
��z
Comment.quotecCsd�dd�|D��S)Nr css|]}t|�VqdSrr!r"rrrr%sz"Comment.content.<locals>.<genexpr>r&r(rrrrQszComment.contentcCs|jgSr)rQr(rrrr7szComment.commentsN)	r-rGrHrEr)rprKrQr7rrrrro	s
roc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�AddressListzaddress-listcCsdd�|D�S)NcSsg|]}|jdkr|�qS)�address�rEr"rrrrR)s
z)AddressList.addresses.<locals>.<listcomp>rr(rrr�	addresses'szAddressList.addressescCstdd�|D�g�S)Ncss|]}|jdkr|jVqdS�rsN�rE�	mailboxesr"rrrr%-s
�z(AddressList.mailboxes.<locals>.<genexpr>�r0r(rrrrx+s
��zAddressList.mailboxescCstdd�|D�g�S)Ncss|]}|jdkr|jVqdSrv�rE�
all_mailboxesr"rrrr%2s
�z,AddressList.all_mailboxes.<locals>.<genexpr>ryr(rrrr{0s
��zAddressList.all_mailboxesN)r-rGrHrErKrurxr{rrrrrr#s

rrc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�AddressrscCs|djdkr|djSdS)Nr�group�rE�display_namer(rrrr:szAddress.display_namecCs4|djdkr|dgS|djdkr*gS|djS�Nr�mailbox�invalid-mailboxrwr(rrrrx?s

zAddress.mailboxescCs:|djdkr|dgS|djdkr0|dgS|djSr�rzr(rrrr{Gs


zAddress.all_mailboxesN)r-rGrHrErKrrxr{rrrrr|6s

r|c@s(eZdZdZedd��Zedd��ZdS)�MailboxList�mailbox-listcCsdd�|D�S)NcSsg|]}|jdkr|�qS)r�rtr"rrrrRUs
z)MailboxList.mailboxes.<locals>.<listcomp>rr(rrrrxSszMailboxList.mailboxescCsdd�|D�S)NcSsg|]}|jdkr|�qS))r�r�rtr"rrrrRYs
�z-MailboxList.all_mailboxes.<locals>.<listcomp>rr(rrrr{WszMailboxList.all_mailboxesN�r-rGrHrErKrxr{rrrrr�Os

r�c@s(eZdZdZedd��Zedd��ZdS)�	GroupList�
group-listcCs |r|djdkrgS|djS�Nrr�rwr(rrrrxaszGroupList.mailboxescCs |r|djdkrgS|djSr�rzr(rrrr{gszGroupList.all_mailboxesNr�rrrrr�]s

r�c@s4eZdZdZedd��Zedd��Zedd��ZdS)	�Groupr}cCs|djdkrgS|djS�N�r�rwr(rrrrxrszGroup.mailboxescCs|djdkrgS|djSr�rzr(rrrr{xszGroup.all_mailboxescCs
|djSr1)rr(rrrr~szGroup.display_nameN)r-rGrHrErKrxr{rrrrrr�ns

r�c@sLeZdZdZedd��Zedd��Zedd��Zedd	��Zed
d��Z	dS)
�NameAddr�	name-addrcCst|�dkrdS|djS�N�r)�lenrr(rrrr�szNameAddr.display_namecCs
|djS�N�����
local_partr(rrrr��szNameAddr.local_partcCs
|djSr���domainr(rrrr��szNameAddr.domaincCs
|djSr�)�router(rrrr��szNameAddr.routecCs
|djSr���	addr_specr(rrrr��szNameAddr.addr_specN�
r-rGrHrErKrr�r�r�r�rrrrr��s



r�c@s@eZdZdZedd��Zedd��Zedd��Zedd	��Zd
S)�	AngleAddrz
angle-addrcCs"|D]}|jdkr|jSqdS�N�	addr-spec)rEr�rhrrrr��s
zAngleAddr.local_partcCs"|D]}|jdkr|jSqdSr��rEr�rhrrrr��s
zAngleAddr.domaincCs"|D]}|jdkr|jSqdS)N�	obs-route)rE�domainsrhrrrr��s
zAngleAddr.routecCs<|D]2}|jdkr|jr"|jSt|j�|jSqdS)Nr�z<>)rEr�r�rrhrrrr��s

zAngleAddr.addr_specN)	r-rGrHrErKr�r�r�r�rrrrr��s


r�c@seZdZdZedd��ZdS)�ObsRouter�cCsdd�|D�S)NcSsg|]}|jdkr|j�qSr�r�r"rrrrR�s
z$ObsRoute.domains.<locals>.<listcomp>rr(rrrr��szObsRoute.domainsN)r-rGrHrErKr�rrrrr��sr�c@sLeZdZdZedd��Zedd��Zedd��Zedd	��Zed
d��Z	dS)
�Mailboxr�cCs|djdkr|djSdS�Nrr�r~r(rrrr�szMailbox.display_namecCs
|djSr1r�r(rrrr��szMailbox.local_partcCs
|djSr1r�r(rrrr��szMailbox.domaincCs|djdkr|djSdSr�)rEr�r(rrrr��sz
Mailbox.routecCs
|djSr1r�r(rrrr��szMailbox.addr_specNr�rrrrr��s



r�c@s,eZdZdZedd��ZeZZZZ	dS)�InvalidMailboxr�cCsdSrrr(rrrr�szInvalidMailbox.display_nameNr�rrrrr��s
r�cs(eZdZdZdZe�fdd��Z�ZS)�Domainr�Fcsd�t�j���S�Nr �r'rr�splitr(rrrr��sz
Domain.domain)r-rGrHrEr3rKr�rLrrrrr��sr�c@seZdZdZdS)�DotAtom�dot-atomNrUrrrrr��sr�c@seZdZdZdZdS)�DotAtomTextz
dot-atom-textTN�r-rGrHrEr3rrrrr��sr�c@seZdZdZdZdS)�
NoFoldLiteralzno-fold-literalFNr�rrrrr�sr�c@sDeZdZdZdZedd��Zedd��Zedd��Zed	d
��Z	dS)�AddrSpecr�FcCs
|djSr1r�r(rrrr�szAddrSpec.local_partcCst|�dkrdS|djS)N�r�)r�r�r(rrrr�szAddrSpec.domaincCs<t|�dkr|djS|dj��|dj|dj��S)Nr�rr�r�)r�r�rstrip�lstripr(rrrrs
zAddrSpec.valuecCsLt|j�}t|�t|t�kr*t|j�}n|j}|jdk	rH|d|jS|S)N�@)�setr�r��
DOT_ATOM_ENDSrr�)rZnamesetZlprrrr�s

zAddrSpec.addr_specN)
r-rGrHrEr3rKr�r�rr�rrrrr�s


r�c@seZdZdZdZdS)�ObsLocalPartzobs-local-partFNr�rrrrr�(sr�cs4eZdZdZdZedd��Ze�fdd��Z�ZS)�DisplayNamezdisplay-nameFcCs�t|�}t|�dkr|jS|djdkr4|�d�n*|ddjdkr^t|ddd��|d<|djdkrv|��n*|ddjdkr�t|ddd��|d<|jS)NrrZr�r�)rr�rrE�pop)rrjrrrr3s
zDisplayName.display_namecs�d}|jrd}n|D]}|jdkrd}qt|�dkr�|r�d}}|djdks`|ddjdkrdd}|djdks�|ddjdkr�d}|t|j�|St�jSdS)	NFTrdrr rZrOr�)rrEr�rrrr)rrpr$ZpreZpostrrrrDs
  zDisplayName.value)	r-rGrHrErJrKrrrLrrrrr�.s
r�c@s,eZdZdZdZedd��Zedd��ZdS)�	LocalPartz
local-partFcCs&|djdkr|djS|djSdS)Nrrd)rErkrr(rrrr]s
zLocalPart.valuecCs�tg}t}d}|dtgD]�}|jdkr,q|r\|jdkr\|djdkr\t|dd��|d<t|t�}|r�|jdkr�|djdkr�|�t|dd���n
|�|�|d}|}qt|dd��}|jS)NFrrZ�dotr�r�)�DOTrEr�
isinstancerir)rrjZlastZ
last_is_tl�tokZis_tlrrrr�ds(
�
�
zLocalPart.local_partN)r-rGrHrEr3rKrr�rrrrr�Xs
r�cs4eZdZdZdZe�fdd��Zedd��Z�ZS)�
DomainLiteralzdomain-literalFcsd�t�j���Sr�r�r(rrrr��szDomainLiteral.domaincCs"|D]}|jdkr|jSqdS)N�ptextrgrhrrr�ip�s
zDomainLiteral.ip)	r-rGrHrEr3rKr�r�rLrrrrr�|sr�c@seZdZdZdZdZdS)�MIMEVersionzmime-versionN)r-rGrHrE�major�minorrrrrr��sr�c@s4eZdZdZdZdZdZedd��Zedd��Z	dS)	�	Parameter�	parameterF�us-asciicCs|jr|djSdSr�)�	sectioned�numberr(rrr�section_number�szParameter.section_numbercCsf|D]\}|jdkr|jS|jdkr|D]4}|jdkr*|D] }|jdkr<|jSq<q*qdS)Nrrdrfr )rErmrlrrr�param_value�s




zParameter.param_valueN)
r-rGrHrEr��extendedrarKr�r�rrrrr��s
r�c@seZdZdZdS)�InvalidParameter�invalid-parameterNrUrrrrr��sr�c@seZdZdZedd��ZdS)�	Attribute�	attributecCs$|D]}|j�d�r|jSqdS)N�attrtext)rE�endswithrrlrrrrm�szAttribute.stripped_valueN�r-rGrHrErKrmrrrrr��sr�c@seZdZdZdZdS)�Section�sectionN)r-rGrHrEr�rrrrr��sr�c@seZdZdZedd��ZdS)�ValuercCs2|d}|jdkr|d}|j�d�r,|jS|jS)NrrZr�)rdr�zextended-attribute)rEr�rmrrlrrrrm�s
�zValue.stripped_valueNr�rrrrr��sr�c@s(eZdZdZdZedd��Zdd�ZdS)�MimeParameters�mime-parametersFc
cs�i}|D]T}|j�d�sq|djdkr*q|dj��}||krHg||<||�|j|f�q|��D�]~\}}t|td�d�}|dd}|j	}|j
s�t|�dkr�|dddkr�|ddj�t
�d��|dd�}g}d}|D]�\}	}
|	|k�r(|
j
�s|
j�t
�d��q�n|
j�t
�d��|d7}|
j}|
j
�r�ztj�|�}Wn&tk
�rttjj|d	d
�}YnRXz|�|d�}Wn"tk
�r�|�dd�}YnXt�|��r�|
j�t
���|�|�q�d
�|�}||fVqfdS)Nr�rr�)�keyr�z.duplicate parameter name; duplicate(s) ignoredz+duplicate parameter name; duplicate ignoredz(inconsistent RFC2231 parameter numberingzlatin-1)�encoding�surrogateescaper�r )rEr�r�striprir��items�sortedrrar�r�rr�InvalidHeaderDefectr��urllib�parseZunquote_to_bytes�UnicodeEncodeErrorZunquote�decode�LookupErrorr�_has_surrogates�UndecodableBytesDefectr')r�paramsr8�name�partsZfirst_paramraZvalue_parts�ir��paramrrrrr��s`�

�
�
zMimeParameters.paramscCsTg}|jD].\}}|r.|�d�|t|���q
|�|�q
d�|�}|rPd|SdS)N�{}={}z; rOr )r�rir,rr')rr�r�rrrrr)s
zMimeParameters.__str__N)r-rGrHrErIrKr�r)rrrrr��s

Er�c@seZdZdZedd��ZdS)�ParameterizedHeaderValueFcCs&t|�D]}|jdkr|jSqiS)Nr�)�reversedrEr�rlrrrr�/s
zParameterizedHeaderValue.paramsN)r-rGrHrIrKr�rrrrr�)sr�c@seZdZdZdZdZdZdS)�ContentTypezcontent-typeF�textZplainN)r-rGrHrEr3�maintype�subtyperrrrr�7sr�c@seZdZdZdZdZdS)�ContentDispositionzcontent-dispositionFN)r-rGrHrEr3�content_dispositionrrrrr�>sr�c@seZdZdZdZdZdS)�ContentTransferEncodingzcontent-transfer-encodingFZ7bitN)r-rGrHrEr3r`rrrrr�Dsr�c@seZdZdZdZdS)�HeaderLabelzheader-labelFNr�rrrrr�Jsr�c@seZdZdZdZdd�ZdS)�MsgIDzmsg-idFcCst|�|jSr)r�linesepr<rrrr=Ssz
MsgID.foldN)r-rGrHrEr3r=rrrrr�Osr�c@seZdZdZdS)�	MessageIDz
message-idNrUrrrrr�Xsr�c@seZdZdZdS)�InvalidMessageIDzinvalid-message-idNrUrrrrr�\sr�c@seZdZdZdS)�Header�headerNrUrrrrr�`sr�csreZdZdZdZdZ�fdd�Z�fdd�Zdd�Ze	dd	��Z
d�fdd�	Zd
d�Ze	dd��Z
dd�Z�ZS)�TerminalTcst��||�}||_g|_|Sr)r�__new__rEr)�clsrrErrrrr�nszTerminal.__new__csd�|jjt����Sr*r+r(rrrr.tszTerminal.__repr__cCst|jjd|j�dS)N�/)r@rr-rEr(rrrrCwszTerminal.pprintcCs
t|j�Sr)�listrr(rrrr/zszTerminal.all_defectsr c	s2d�||jj|jt���|js"dn
d�|j��gS)Nz
{}{}/{}({}){}r z {})r,rr-rErr.rrBrrrrD~s�zTerminal._ppcCsdSrrr(rrr�pop_trailing_ws�szTerminal.pop_trailing_wscCsgSrrr(rrrr7�szTerminal.commentscCst|�|jfSr)rrEr(rrr�__getnewargs__�szTerminal.__getnewargs__)r )r-rGrHr3rJrIr�r.rCrKr/rDrr7rrLrrrrr�hs
	
r�c@s eZdZedd��Zdd�ZdS)�WhiteSpaceTerminalcCsdSrNrr(rrrr�szWhiteSpaceTerminal.valuecCsdS)NTrr(rrrr2�sz!WhiteSpaceTerminal.startswith_fwsN�r-rGrHrKrr2rrrrr�s
rc@s eZdZedd��Zdd�ZdS)�
ValueTerminalcCs|Srrr(rrrr�szValueTerminal.valuecCsdS)NFrr(rrrr2�szValueTerminal.startswith_fwsNrrrrrr�s
rc@s eZdZedd��Zdd�ZdS)�EWWhiteSpaceTerminalcCsdSr�rr(rrrr�szEWWhiteSpaceTerminal.valuecCsdSr�rr(rrrr)�szEWWhiteSpaceTerminal.__str__N)r-rGrHrKrr)rrrrr�s
rc@seZdZdZdS)�_InvalidEwErrorz1Invalid encoded word found while parsing headers.N)r-rGrH�__doc__rrrrr�srr��,�list-separatorr�zroute-component-markerz([{}]+)r z[^{}]+z[\x00-\x20\x7F]cCs>t|�}|r|j�t�|��t�|�r:|j�t�d��dS)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)�_non_printable_finderrrirZNonPrintableDefectrr�r�)�xtextZnon_printablesrrr�_validate_xtext�s

�rcCs�t|d�^}}g}d}d}tt|��D]L}||dkrJ|rDd}d}nd}q&|rTd}n|||krdq||�||�q&|d}d�|�d�||d�g|�|fS)akScan printables/quoted-pairs until endchars and return unquoted ptext.

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

    r�Fr
Tr N)�
_wsp_splitter�ranger�rir')r�endcharsZfragment�	remainderZvchars�escape�had_qp�posrrr�_get_ptext_to_endchars�s$	rcCs.|��}t|dt|�t|��d�}||fS)z�FWS = 1*WSP

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

    N�fws)r�rr�)rZnewvaluerrrr�get_fwssrc
	Cs�t�}|�d�s t�d�|���|dd��dd�^}}||dd�krXt�d�|���d�|�}t|�dkr�|dtkr�|dtkr�|�	d	�dkr�|�dd�^}}|d|}t|���dkr�|j
�t�d
��||_
d�|�}zt�d|d�\}}}}	Wn*ttfk
�r*td�|j
���YnX||_||_|j
�|	�|�r�|dtk�rrt|�\}
}|�|
��qDt|d�^}}t|d�}t|�|�|�d�|�}�qD|�r�|dtk�r�|j
�t�d
��||fS)zE encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

    �=?z"expected encoded word but found {}r�Nz?=r�r r�?zwhitespace inside encoded wordz!encoded word format invalid: '{}'�vtextz.missing trailing whitespace after encoded-word)r^�
startswithr�HeaderParseErrorr,r�r'r�r�countrrir�r`�_ewr��
ValueError�KeyErrorrrarbr6�WSPrr
rr)
rZewr�rZremstr�restr�rarbrr8�charsrrrr�get_encoded_wordsd
��

�
��
�

�




�r#cCsFt�}|�rB|dtkr0t|�\}}|�|�qd}|�d�r�zt|�\}}Wn,tk
rfd}Yn�tjk
rzYnrXd}t	|�dkr�|dj
dkr�|j�t�d��d}|r�t	|�dkr�|d	j
d
kr�t
|dd�|d<|�|�qt|d�^}}|�rt�|��r|�d�^}}t|d�}t|�|�|�d�|�}q|S)
aOunstructured = (*([FWS] vchar) *WSP) / obs-unstruct
       obs-unstruct = *((*LF *CR *(obs-utext) *LF *CR)) / FWS)
       obs-utext = %d0 / obs-NO-WS-CTL / LF / CR

       obs-NO-WS-CTL is control characters except WSP/CR/LF.

    So, basically, we have printable runs, plus control characters or nulls in
    the obsolete syntax, separated by whitespace.  Since RFC 2047 uses the
    obsolete syntax in its specification, but requires whitespace on either
    side of the encoded words, I can see no reason to need to separate the
    non-printable-non-whitespace from the printable runs if they occur, so we
    parse this into xtext tokens separated by WSP tokens.

    Because an 'unstructured' value must by definition constitute the entire
    value, this 'get' routine does not return a remaining value, only the
    parsed TokenList.

    rTrFr�rz&missing whitespace before encoded wordr����r_rr )rSr rrirr#rrrr�rErr�rr
�rfc2047_matcher�search�	partitionrrr')rrTr8�valid_ewZhave_wsr�rrrrr�get_unstructuredAsJ


��


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�)rrr�rr��_rrr�get_qp_ctext�s
r,cCs*t|d�\}}}t|d�}t|�||fS)aoqcontent = qtext / quoted-pair

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

    rr�)rrrr*rrr�get_qcontent�s

r-cCsNt|�}|st�d�|���|��}|t|�d�}t|d�}t|�||fS)z�atext = <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_matcherrrr,r}r�rr)r�mr.rrr�	get_atext�s�
r1cCsr|ddkrt�d�|���t�}|dd�}|rT|ddkrTt|�\}}|�|�|�rB|ddk�rB|dtkr�t|�\}}n�|dd�dk�r*d}z&t|�\}}|j	�t�
d	��d
}Wn"tjk
r�t|�\}}YnX|�r6t|�dk�r6|djdk�r6|d
jdk�r6t
|dd�|d<nt|�\}}|�|�qT|�sb|j	�t�
d��||fS||dd�fS)z�bare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE

    A quoted-string without the leading or trailing white space.  Its
    value is the text between the quote marks, with whitespace
    preserved and quoted pairs decoded.
    rrzexpected '"' but found '{}'r�Nr�rFz!encoded word inside quoted stringTr�rr$r_z"end of header inside quoted string)rrr,rnr-rir rr#rr�r�rEr)rZbare_quoted_stringr8r(rrr�get_bare_quoted_string�sL�

���

�r2cCs�|r |ddkr t�d�|���t�}|dd�}|r�|ddkr�|dtkr\t|�\}}n&|ddkrvt|�\}}nt|�\}}|�|�q2|s�|j	�t�
d��||fS||dd�fS)z�comment = "(" *([FWS] ccontent) [FWS] ")"
       ccontent = ctext / quoted-pair / comment

    We handle nested comments here, and quoted-pair in our qp-ctext routine.
    rrzexpected '(' but found '{}'r�Nrqzend of header inside comment)rrr,ror r�get_commentr,rirr�)rrPr8rrrr3�s&�
�r3cCsPt�}|rH|dtkrH|dtkr0t|�\}}nt|�\}}|�|�q||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS

    r)rY�CFWS_LEADERr rr3ri)rrZr8rrr�get_cfws�sr5cCspt�}|r,|dtkr,t|�\}}|�|�t|�\}}|�|�|rh|dtkrht|�\}}|�|�||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)rcr4r5rir2)rZ
quoted_stringr8rrr�get_quoted_strings


r6cCs�t�}|r,|dtkr,t|�\}}|�|�|rL|dtkrLt�d�|���|�d�r�zt	|�\}}Wq�tjk
r�t
|�\}}Yq�Xnt
|�\}}|�|�|r�|dtkr�t|�\}}|�|�||fS)zPatom = [CFWS] 1*atext [CFWS]

    An atom could be an rfc2047 encoded word.
    rzexpected atom but found '{}'r)r[r4r5ri�	ATOM_ENDSrrr,rr#r1)rr\r8rrr�get_atoms&
�


r8cCs�t�}|r|dtkr&t�d�|���|rt|dtkrtt|�\}}|�|�|r&|ddkr&|�t�|dd�}q&|dtkr�t�d�d|���||fS)z( dot-text = 1*atext *("." 1*atext)

    rz8expected atom at a start of dot-atom-text but found '{}'rr�Nr�z4expected atom at end of dot-atom-text but found '{}')r�r7rrr,r1rir�)rZ
dot_atom_textr8rrr�get_dot_atom_text2s �

�r9cCs�t�}|dtkr(t|�\}}|�|�|�d�rhzt|�\}}Wqttjk
rdt|�\}}YqtXnt|�\}}|�|�|r�|dtkr�t|�\}}|�|�||fS)z� dot-atom = [CFWS] dot-atom-text [CFWS]

    Any place we can have a dot atom, we could instead have an rfc2047 encoded
    word.
    rr)	r�r4r5rirr#rrr9)rZdot_atomr8rrr�get_dot_atomEs



r:cCs�|dtkrt|�\}}nd}|s,t�d��|ddkrFt|�\}}n*|dtkrdt�d�|���nt|�\}}|dk	r�|g|dd�<||fS)a�word = atom / quoted-string

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

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

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

    rNz5Expected 'atom' or 'quoted-string' but found nothing.rz1Expected 'atom' or 'quoted-string' but found '{}')r4r5rrr6�SPECIALSr,r8)r�leaderr8rrr�get_word^s"��r=cCs�t�}zt|�\}}|�|�Wn(tjk
rH|j�t�d��YnX|r�|dtkr�|ddkr�|�t�|j�t�	d��|dd�}qJzt|�\}}WnDtjk
r�|dt
kr�t|�\}}|j�t�	d��n�YnX|�|�qJ||fS)a� phrase = 1*word / obs-phrase
        obs-phrase = word *(word / "." / CFWS)

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

    zphrase does not start with wordrrzperiod in 'phrase'r�Nzcomment found without atom)rVr=rirrrr��PHRASE_ENDSr��ObsoleteHeaderDefectr4r5)rrWr8rrr�
get_phrase�s4
�

�
�r@cCsvt�}d}|dtkr"t|�\}}|s6t�d�|���zt|�\}}Wn^tjk
r�zt|�\}}Wn6tjk
r�|ddkr�|dtkr��t	�}YnXYnX|dk	r�|g|dd�<|�
|�|�r4|ddks�|dtk�r4tt|�|�\}}|j
dk�r|j�
t�d��n|j�
t�d��||d<z|j�d�Wn(tk
�rl|j�
t�d	��YnX||fS)
z= local-part = dot-atom / quoted-string / obs-local-part

    Nrz"expected local-part but found '{}'r
�invalid-obs-local-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�r4r5rrr,r:r=r>rri�get_obs_local_partrrErr�r?r�encoder�ZNonASCIILocalPartDefect)rr�r<r8�obs_local_partrrr�get_local_part�sJ�
 
�
�
�rFcCs�t�}d}|�r(|ddks*|dtk�r(|ddkrj|rL|j�t�d��|�t�d}|dd�}q
nD|ddkr�|�t|dd	��|dd�}|j�t�d
��d}q
|r�|djdkr�|j�t�d
��zt	|�\}}d}Wn4tj
k
�r|dtk�r
�t|�\}}YnX|�|�q
|djdk�sX|djdk�rj|djdk�rj|j�t�d��|djdk�s�|djdk�r�|djdk�r�|j�t�d��|j�r�d|_||fS)z' obs-local-part = word *("." word)
    Frr
rzinvalid repeated '.'Tr�N�misplaced-specialz/'\' character outside of quoted-string/ccontentr�r�zmissing '.' between wordsrZz!Invalid leading '.' in local partr$z"Invalid trailing '.' in local partrA)
r�r>rrirr�r�rrEr=rr4r5)rrEZlast_non_ws_was_dotr8rrrrC�sj 
�
�
�
���
���
�rCcCs@t|d�\}}}t|d�}|r0|j�t�d��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)rrrrirr?r)rr�rrrr�	get_dtext�s

�rHcCs,|rdS|�t�d��|�tdd��dS)NFz"end of input inside domain-literal�]�domain-literal-endT)rirr�r)r�domain_literalrrr�_check_for_early_dl_ends�rLcCsjt�}|dtkr(t|�\}}|�|�|s6t�d��|ddkrRt�d�|���|dd�}t||�rp||fS|�tdd��|dt	kr�t
|�\}}|�|�t|�\}}|�|�t||�r�||fS|dt	kr�t
|�\}}|�|�t||�r�||fS|ddk�rt�d	�|���|�tdd
��|dd�}|�rb|dtk�rbt|�\}}|�|�||fS)zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS]

    rzexpected domain-literal�[z6expected '[' at start of domain-literal but found '{}'r�Nzdomain-literal-startrIz4expected ']' at end of domain-literal but found '{}'rJ)r�r4r5rirrr,rLrr rrH)rrKr8rrr�get_domain_literalsH

�





�
rNcCsrt�}d}|dtkr"t|�\}}|s6t�d�|���|ddkrvt|�\}}|dk	rd|g|dd�<|�|�||fSzt|�\}}Wn"tjk
r�t	|�\}}YnX|r�|ddkr�t�d��|dk	r�|g|dd�<|�|�|�rj|ddk�rj|j
�t�d��|djd	k�r*|d|dd�<|�rj|ddk�rj|�t
�t	|d
d��\}}|�|��q*||fS)z] domain = dot-atom / domain-literal / obs-domain
        obs-domain = atom *("." atom))

    Nrzexpected domain but found '{}'rMr�zInvalid Domainrz(domain is not a dot-atom (contains CFWS)r�r�)r�r4r5rrr,rNrir:r8rr?rEr�)rr�r<r8rrr�
get_domain?sD�



�
rOcCs|t�}t|�\}}|�|�|r,|ddkrF|j�t�d��||fS|�tdd��t|dd��\}}|�|�||fS)z( addr-spec = local-part "@" domain

    rr�z#addr-spec local part with no domain�address-at-symbolr�N)r�rFrirrr�rrO)rr�r8rrr�
get_addr_speces

�
rQcCs�t�}|rj|ddks"|dtkrj|dtkrFt|�\}}|�|�q|ddkr|�t�|dd�}q|rz|ddkr�t�d�|���|�t�t	|dd��\}}|�|�|�r>|ddk�r>|�t�|dd�}|s�q>|dtk�rt|�\}}|�|�|ddkr�|�t�t	|dd��\}}|�|�q�|�sNt�d��|ddk�rlt�d	�|���|�t
dd
��||dd�fS)z� obs-route = obs-domain-list ":"
        obs-domain-list = *(CFWS / ",") "@" domain *("," [CFWS] ["@" domain])

        Returns an obs-route token with the appropriate sub-tokens (that is,
        there is no obs-domain-list in the parse tree).
    rrr�Nr�z(expected obs-route domain but found '{}'z%end of header while parsing obs-route�:z4expected ':' marking end of obs-route but found '{}'zend-of-obs-route-marker)r�r4r5ri�
ListSeparatorrrr,�RouteComponentMarkerrOr)rZ	obs_router8rrr�
get_obs_routeusF
�





�rUcCs�t�}|dtkr(t|�\}}|�|�|r8|ddkrHt�d�|���|�tdd��|dd�}|ddkr�|�tdd��|j�t�	d	��|dd�}||fSzt
|�\}}Wnztjk
�r0z"t|�\}}|j�t�d
��Wn(tjk
�rt�d�|���YnX|�|�t
|�\}}YnX|�|�|�r^|ddk�r^|dd�}n|j�t�	d��|�tdd��|�r�|dtk�r�t|�\}}|�|�||fS)
z� angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr
        obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS]

    r�<z"expected angle-addr but found '{}'zangle-addr-startr�N�>zangle-addr-endznull addr-spec in angle-addrz*obsolete route specification in angle-addrz.expected addr-spec or obs-route but found '{}'z"missing trailing '>' on angle-addr)
r�r4r5rirrr,rrr�rQrUr?)rZ
angle_addrr8rrr�get_angle_addr�sT
�
�
�
�



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

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

    N)r�r@r6r)rrr8rrr�get_display_name�s
rYcCs�t�}d}|dtkr6t|�\}}|s6t�d�|���|ddkr�|dtkr^t�d�|���t|�\}}|s~t�d�|���|dk	r�|g|ddd�<d}|�|�t	|�\}}|dk	r�|g|dd�<|�|�||fS)z, name-addr = [display-name] angle-addr

    Nrz!expected name-addr but found '{}'rV)
r�r4r5rrr,r>rYrirX)rZ	name_addrr<r8rrr�
get_name_addr�s6���

rZcCs�t�}zt|�\}}WnNtjk
rdzt|�\}}Wn&tjk
r^t�d�|���YnXYnXtdd�|jD��r�d|_|�	|�||fS)z& mailbox = name-addr / addr-spec

    zexpected mailbox but found '{}'css|]}t|tj�VqdSr)r�rr�r"rrrr%s�zget_mailbox.<locals>.<genexpr>r�)
r�rZrrrQr,�anyr/rEri)rr�r8rrr�get_mailbox�s ��
r\cCsdt�}|r\|d|kr\|dtkrD|�t|dd��|dd�}qt|�\}}|�|�q||fS)z� Read everything up to one of the chars in endchars.

    This is outside the formal grammar.  The InvalidMailbox TokenList that is
    returned acts like a Mailbox, but the data attributes are None.

    rrGr�N)r�r>rirr@)rrZinvalid_mailboxr8rrr�get_invalid_mailboxs�r]cCs�t�}|�r�|ddk�r�zt|�\}}|�|�W�ntjk
�r<d}|dtkr�t|�\}}|rv|ddkr�|�|�|j�t�d��n@t	|d�\}}|dk	r�|g|dd�<|�|�|j�t�
d��nb|ddkr�|j�t�d��nBt	|d�\}}|dk	�r|g|dd�<|�|�|j�t�
d��YnX|�r�|ddk�r�|d}d	|_t	|d�\}}|�|�|j�t�
d��|r|ddkr|�t
�|d
d�}q||fS)aJ mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list
        obs-mbox-list = *([CFWS] ",") mailbox *("," [mailbox / CFWS])

    For this routine we go outside the formal grammar in order to improve error
    handling.  We recognize the end of the mailbox list only at the end of the
    value or at a ';' (the group terminator).  This is so that we can turn
    invalid mailboxes into InvalidMailbox tokens and continue parsing any
    remaining valid mailboxes.  We also allow all mailbox entries to be null,
    and this condition is handled appropriately at a higher level.

    r�;Nz,;zempty element in mailbox-listzinvalid mailbox in mailbox-listrr�r�r�)r�r\rirrr4r5rr?r]r�rEr6rS)rZmailbox_listr8r<r�rrr�get_mailbox_list sX

�

�
�


�

�
r_cCst�}|s$|j�t�d��||fSd}|r�|dtkr�t|�\}}|sl|j�t�d��|�|�||fS|ddkr�|�|�||fSt|�\}}t|j	�dkr�|dk	r�|�|�|�
|�|j�t�d��||fS|dk	r�|g|dd�<|�|�||fS)zg group-list = mailbox-list / CFWS / obs-group-list
        obs-group-list = 1*([CFWS] ",") [CFWS]

    zend of header before group-listNrzend of header in group-listr^zgroup-list with empty entries)r�rrirr�r4r5r_r�r{r6r?)rZ
group_listr<r8rrr�get_group_listYs>
�
�




�
r`cCs t�}t|�\}}|r"|ddkr2t�d�|���|�|�|�tdd��|dd�}|r�|ddkr�|�tdd��||dd�fSt|�\}}|�|�|s�|j�t�	d	��n|ddkr�t�d
�|���|�tdd��|dd�}|�r|dt
k�rt|�\}}|�|�||fS)z7 group = display-name ":" [group-list] ";" [CFWS]

    rrRz8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorr�Nr^zgroup-terminatorzend of header in groupz)expected ';' at end of group but found {})r�rYrrr,rirr`rr�r4r5)rr}r8rrr�	get_group~s8�


��
racCsxt�}zt|�\}}WnNtjk
rdzt|�\}}Wn&tjk
r^t�d�|���YnXYnX|�|�||fS)a� address = mailbox / group

    Note that counter-intuitively, an address can be either a single address or
    a list of addresses (a group).  This is why the returned Address object has
    a 'mailboxes' attribute which treats a single address as a list of length
    one.  When you need to differentiate between to two cases, extract the single
    element, which is either a mailbox or a group token.

    zexpected address but found '{}')r|rarrr\r,ri)rrsr8rrr�get_address�s�
rbc
Cs�t�}|�r�zt|�\}}|�|�W�n tjk
�rH}z�d}|dtkr�t|�\}}|rj|ddkr�|�|�|j�t�d��nFt	|d�\}}|dk	r�|g|dd�<|�t
|g��|j�t�d��nh|ddkr�|j�t�d��nHt	|d�\}}|dk	�r|g|dd�<|�t
|g��|j�t�d��W5d}~XYnX|�r�|ddk�r�|dd}d|_t	|d�\}}|�
|�|j�t�d��|r|�tdd	��|d
d�}q||fS)a� address_list = (address *("," address)) / obs-addr-list
        obs-addr-list = *([CFWS] ",") address *("," [address / CFWS])

    We depart from the formal grammar here by continuing to parse until the end
    of the input, assuming the input to be entirely composed of an
    address-list.  This is always true in email parsing, and allows us
    to skip invalid addresses to parse additional valid ones.

    Nrrz"address-list entry with no contentzinvalid address in address-listzempty element in address-listr�r�r	r�)rrrbrirrr4r5rr?r]r|r�rEr6r)rZaddress_listr8�errr<r�rrr�get_address_list�sX


�
�
�

�

�rdcCs�t�}|st�d�|���|ddkr6t�d�|���|�tdd��|dd�}t|�\}}|�|�|rx|ddkr�t�d	�|���|�tdd
��||dd�fS)z& no-fold-literal = "[" *dtext "]"
    z'expected no-fold-literal but found '{}'rrMz;expected '[' at the start of no-fold-literal but found '{}'zno-fold-literal-startr�NrIz9expected ']' at the end of no-fold-literal but found '{}'zno-fold-literal-end)r�rrr,rirrH)rZno_fold_literalr8rrr�get_no_fold_literal�s.���
��recCs�t�}|r,|dtkr,t|�\}}|�|�|r<|ddkrLt�d�|���|�tdd��|dd�}zt|�\}}Wn`tjk
r�z"t	|�\}}|j
�t�d��Wn&tjk
r�t�d�|���YnXYnX|�|�|r�|dd	k�r@|j
�t�d
��|�r8|ddk�r8|�tdd��|dd�}||fS|�td	d
��|dd�}zt|�\}}Wn�tjk
�rzt
|�\}}Wnrtjk
�r}zPz"t|�\}}|j
�t�d��Wn(tjk
�r�t�d�|���YnXW5d}~XYnXYnX|�|�|�r6|ddk�r6|dd�}n|j
�t�d��|�tdd��|�r�|dtk�r�t|�\}}|�|�||fS)z�msg-id = [CFWS] "<" id-left '@' id-right  ">" [CFWS]
       id-left = dot-atom-text / obs-id-left
       id-right = dot-atom-text / no-fold-literal / obs-id-right
       no-fold-literal = "[" *dtext "]"
    rrVzexpected msg-id but found '{}'zmsg-id-startr�Nzobsolete id-left in msg-idz4expected dot-atom-text or obs-id-left but found '{}'r�zmsg-id with no id-rightrWz
msg-id-endrPzobsolete id-right in msg-idzFexpected dot-atom-text, no-fold-literal or obs-id-right but found '{}'zmissing trailing '>' on msg-id)r�r4r5rirrr,rr9rCrr?r�rerO)rZmsg_idr8�errr�
get_msg_ids~
�
�
��

�
�
��"

�
rgc
Cs�t�}zt|�\}}|�|�WnLtjk
rl}z,t|�}t|�}|j�t�d�	|���W5d}~XYnX|r�|j�t�d�	|���|S)z2message-id      =   "Message-ID:" msg-id CRLF
    zInvalid msg-id: {!r}NzUnexpected {!r})
r�rgrirrr)r�rr�r,)rZ
message_idr8Zexrrr�parse_message_idKs�
�rhcCs�t�}|s |j�t�d��|S|dtkrXt|�\}}|�|�|sX|j�t�d��d}|r�|ddkr�|dtkr�||d7}|dd�}q\|��s�|j�t�d�	|���|�t
|d	��nt|�|_|�t
|d
��|�r|dtk�rt|�\}}|�|�|�r|ddk�rT|jdk	�r:|j�t�d��|�rP|�t
|d	��|S|�t
dd��|dd�}|�r�|dtk�r�t|�\}}|�|�|�s�|jdk	�r�|j�t�d��|Sd}|�r�|dtk�r�||d7}|dd�}�q�|���s*|j�t�d
�	|���|�t
|d	��nt|�|_
|�t
|d
��|�rn|dtk�rnt|�\}}|�|�|�r�|j�t�d��|�t
|d	��|S)zE mime-version = [CFWS] 1*digit [CFWS] "." [CFWS] 1*digit [CFWS]

    z%Missing MIME version number (eg: 1.0)rz0Expected MIME version number but found only CFWSr rr�Nz1Expected MIME major version number but found {!r}r�digitsz0Incomplete MIME version; found only major numberzversion-separatorz1Expected MIME minor version number but found {!r}z'Excess non-CFWS text after MIME version)r�rrir�HeaderMissingRequiredValuer4r5�isdigitr�r,r�intr�r�)rZmime_versionr8rirrr�parse_mime_versiongs�
�

�
�


�

�

�


�rmcCsdt�}|r\|ddkr\|dtkrD|�t|dd��|dd�}qt|�\}}|�|�q||fS)z� Read everything up to the next ';'.

    This is outside the formal grammar.  The InvalidParameter TokenList that is
    returned acts like a Parameter, but the data attributes are None.

    rr^rGr�N)r�r>rirr@)rZinvalid_parameterr8rrr�get_invalid_parameter�s�rncCsNt|�}|st�d�|���|��}|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_matcherrrr,r}r�rr)rr0rorrr�	get_ttext�s	�
rqcCs�t�}|r,|dtkr,t|�\}}|�|�|rL|dtkrLt�d�|���t|�\}}|�|�|r�|dtkr�t|�\}}|�|�||fS)z�token = [CFWS] 1*ttext [CFWS]

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

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

    r�expected token but found '{}')	r]r4r5ri�
TOKEN_ENDSrrr,rq)rZmtokenr8rrr�	get_token�s	
�

rtcCsNt|�}|st�d�|���|��}|t|�d�}t|d�}t|�||fS)aQattrtext = 1*(any non-ATTRIBUTE_ENDS character)

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

    z expected attrtext but found {!r}Nr�)�_non_attribute_end_matcherrrr,r}r�rr�rr0r�rrr�get_attrtext�s	�
rwcCs�t�}|r,|dtkr,t|�\}}|�|�|rL|dtkrLt�d�|���t|�\}}|�|�|r�|dtkr�t|�\}}|�|�||fS)aH [CFWS] 1*attrtext [CFWS]

    This version of the BNF makes the CFWS explicit, and as usual we use a
    value terminal for the actual run of characters.  The RFC equivalent of
    attrtext is the token characters, with the subtraction of '*', "'", and '%'.
    We include tab in the excluded set just as we do for token.

    rrr)	r�r4r5ri�ATTRIBUTE_ENDSrrr,rw�rr�r8rrr�
get_attribute�s	
�

rzcCsNt|�}|st�d�|���|��}|t|�d�}t|d�}t|�||fS)z�attrtext = 1*(any non-ATTRIBUTE_ENDS character plus '%')

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

    z)expected extended attrtext but found {!r}N�extended-attrtext)�#_non_extended_attribute_end_matcherrrr,r}r�rrrvrrr�get_extended_attrtext	s�
r}cCs�t�}|r,|dtkr,t|�\}}|�|�|rL|dtkrLt�d�|���t|�\}}|�|�|r�|dtkr�t|�\}}|�|�||fS)z� [CFWS] 1*extended_attrtext [CFWS]

    This is like the non-extended version except we allow % characters, so that
    we can pick up an encoded value as a single string.

    rrr)	r�r4r5ri�EXTENDED_ATTRIBUTE_ENDSrrr,r}ryrrr�get_extended_attribute#	s
�

rcCs�t�}|r|ddkr&t�d�|���|�tdd��|dd�}|rR|d��sbt�d�|���d}|r�|d��r�||d7}|dd�}qf|dd	kr�|d	kr�|j�t�d
��t	|�|_
|�t|d��||fS)a6 '*' digits

    The formal BNF is more complicated because leading 0s are not allowed.  We
    check for that and add a defect.  We also assume no CFWS is allowed between
    the '*' and the digits, though the RFC is not crystal clear on that.
    The caller should already have dealt with leading CFWS.

    r�*zExpected section but found {}zsection-markerr�Nz$Expected section number but found {}r �0z'section number has an invalid leading 0ri)r�rrr,rirrkrZInvalidHeaderErrorrlr�)rr�rirrr�get_section9	s,	��
�
r�cCs�t�}|st�d��d}|dtkr0t|�\}}|sDt�d�|���|ddkr^t|�\}}nt|�\}}|dk	r�|g|dd�<|�|�||fS)z  quoted-string / attribute

    z&Expected value but found end of stringNrz Expected value but found only {}r)	r�rrr4r5r,r6rri)r�vr<r8rrr�	get_valueW	s"
�
r�cCs�t�}t|�\}}|�|�|r,|ddkrL|j�t�d�|���||fS|ddkr�z t|�\}}d|_|�|�Wntj	k
r�YnX|s�t�	d��|ddkr�|�t
dd��|dd	�}d|_|dd
kr�t�	d��|�t
d
d��|dd	�}d	}|�r,|dtk�r,t
|�\}}|�|�d	}|}|j�rF|�rF|dd
k�rFt|�\}}|j}d}|jdk�r�|�r�|ddk�r�d}n$t|�\}}	|	�r�|	ddk�r�d}n(zt|�\}}	WnYnX|	�s�d}|�r0|j�t�d��|�|�|D](}
|
jdk�rg|
d	d	�<|
}�q*�q|}nd	}|j�t�d��|�r`|ddk�r`d	}nt|�\}}|j�r�|jdk�r�|�r�|ddk�r�|�|�|d	k	�r�|�r�t|��|}||fS|j�t�d��|�s
|j�t�d��|�|�|d	k�r�||fSn�|d	k	�rN|D]}
|
jdk�r�q2�q|
jdk|�|
�|
j|_|ddk�rlt�	d�|���|�t
dd��|dd	�}|�r�|ddk�r�t|�\}}|�|�|j|_|�r�|ddk�r�t�	d�|���|�t
dd��|dd	�}|d	k	�rrt�}|�rl|dtk�r,t|�\}}n2|dd
k�rRt
d
d�}|dd	�}nt|�\}}|�|��q
|}nt|�\}}|�|�|d	k	�r�|�r�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.
    rr^z)Parameter contains name ({}) but no valuer�TzIncomplete parameterzextended-parameter-markerr�N�=zParameter not followed by '='�parameter-separatorrF�'z5Quoted string value for extended parameter is invalidrfzZParameter marked as extended but appears to have a quoted string value that is non-encodedzcApparent initial-extended-value but attribute was not marked as extended or was not initial sectionz(Missing required charset/lang delimitersr{r�z=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231-delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {}ZDQUOTE)r�rzrirrr�r,r�r�rrr�r4r5r6rmr�rwr}rEr��AssertionErrorrrarbr�r rr-)rr�r8r<rZappendtoZqstringZinner_valueZ
semi_validr!�tr�rrr�
get_parameterm	s�
�



�


�


�
�






�
�



r�c
Csjt�}|�rfzt|�\}}|�|�Wn�tjk
r�}z�d}|dtkrVt|�\}}|sp|�|�|WY�xS|ddkr�|dk	r�|�|�|j�t�d��n@t	|�\}}|r�|g|dd�<|�|�|j�t�d�
|���W5d}~XYnX|�rD|ddk�rD|d}d|_t	|�\}}|�|�|j�t�d�
|���|r|�t
dd	��|d
d�}q|S)a! parameter *( ";" parameter )

    That BNF is meant to indicate this routine should only be called after
    finding and handling the leading ';'.  There is no corresponding rule in
    the formal RFC grammar, but it is more convenient for us for the set of
    parameters to be treated as its own TokenList.

    This is 'parse' routine because it consumes the remaining value, but it
    would never be called to parse a full header.  Instead it is called to
    parse everything after the non-parameter value of a specific MIME header.

    Nrr^zparameter entry with no contentzinvalid parameter {!r}r�r�z)parameter with invalid trailing text {!r}r�r�)r�r�rirrr4r5rr�rnr,rEr6r)rZmime_parametersr8rcr<r�rrr�parse_mime_parameters�	sJ



�

�

�r�cCs�|rV|ddkrV|dtkr>|�t|dd��|dd�}qt|�\}}|�|�q|s^dS|�tdd��|�t|dd���dS)zBDo our best to find the parameters in an invalid MIME header

    rr^rGr�Nr�)r>rirr@r�)Z	tokenlistrr8rrr�_find_mime_parameters/
sr�c
Cs�t�}d}|s$|j�t�d��|Szt|�\}}Wn<tjk
rp|j�t�d�|���t	||�|YSX|�|�|r�|ddkr�|j�t�d��|r�t	||�|S|j
����|_
|�tdd��|dd	�}zt|�\}}Wn>tjk
�r*|j�t�d
�|���t	||�|YSX|�|�|j
����|_|�sP|S|ddk�r�|j�t�d�|���|`
|`t	||�|S|�tdd
��|�t|dd	���|S)z� maintype "/" subtype *( ";" parameter )

    The maintype and substype are tokens.  Theoretically they could
    be checked against the official IANA list + x-token, but we
    don't do that.
    Fz"Missing content type specificationz(Expected content maintype but found {!r}rr�zInvalid content typezcontent-type-separatorr�Nz'Expected content subtype but found {!r}r^z<Only parameters are valid after content type, but found {!r}r�)r�rrirrjrtrr�r,r�rr��lowerr�rr�r�)rZctypeZrecoverr8rrr�parse_content_type_header?
sd
�
�



�

�



��
r�c
Cs�t�}|s |j�t�d��|Szt|�\}}Wn<tjk
rl|j�t�d�|���t	||�|YSX|�|�|j
����|_
|s�|S|ddkr�|j�t�d�|���t	||�|S|�tdd��|�t|dd���|S)	z* disposition-type *( ";" parameter )

    zMissing content dispositionz+Expected content disposition but found {!r}rr^zCOnly parameters are valid after content disposition, but found {!r}r�r�N)r�rrirrjrtrr�r,r�rr�r�r�rr�)rZdisp_headerr8rrr� parse_content_disposition_headerx
s:
�
�



��
r�c
Cs�t�}|s |j�t�d��|Szt|�\}}Wn.tjk
r^|j�t�d�|���YnX|�|�|j	�
���|_|s�|S|r�|j�t�d��|dt
kr�|�t|dd��|dd�}q�t|�\}}|�|�q�|S)z mechanism

    z!Missing content transfer encodingz1Expected content transfer encoding but found {!r}z*Extra text after content transfer encodingrrGr�N)r�rrirrjrtrr�r,rr�r�r`r>rr@)rZ
cte_headerr8rrr�&parse_content_transfer_encoding_header�
s4
�
�

�r�cCsDd}|r@|dr@|ddtkr@|dd}|ddd�|d<|S)Nr r�)r )�linesZwsprrr�_steal_trailing_WSP_if_exists�
s
r�cCs�|jp
tj}|jrdnd}dg}d}d}d}tdd�}t|�}	|	�r�|	�d�}
|
|kr`|d8}q>t|
�}|s�|
jd	kr�t	�
|�}nt�
|�}z|�|�|}Wn6t
k
r�td
d�|
jD��r�d}nd}d
}YnX|
jdkr�t|
|||�q>|�r�|�s�|
j�s~d}d}|
j�r~|
j|d�dt|j��}
|j|
k�r~t|
�|t|d�k�rlt|�}|�|�|d|
7<q>t|
d��s�t|
�|	}	nt|||||
j|�}d}q>t|�|t|d�k�r�|d|7<q>|
j�r$t|�d|k�r$t|�}|�s|
���r$|�||�d}q>t|
d��s\t|
�}|
j�sR|d7}|�|�||	}	q>|
j�r||�s||	�d|
�d
}q>t|�}|�s�|
���r�|�||�q>|d|7<q>|j�|�|jS)zLReturn string of contents of parse_tree folded according to RFC rules.

    �utf-8r�r NrF�wrap_as_ew_blockedr�r�css|]}t|tj�VqdSr)r�rr�r"rrrr%�
s�z%_refold_parse_tree.<locals>.<genexpr>�unknown-8bitTr�r9r�rD)Zmax_line_length�sys�maxsize�utf8r�r�r�rrE�
SPECIALSNL�
isdisjoint�NLSETrDr�r[r/�_fold_mime_parametersr3rIr=r�r�r�rirF�_fold_as_ewrJr2�insertr')Z
parse_treer:�maxlenr�r��last_ewr�Z
want_encodingZend_ew_not_allowedr�r4�tstrraZencoded_part�newlineZnewpartsrrrr;�
s�



�



��
r;cCs�|dk	r<|r<tt|d|d�|��}|dd|�|d<|dtkr�|d}|dd�}t|d�|krz|�t|��|d|7<d}|dtkr�|d}|dd�}|dkr�t|d�n|}|dkr�dn|}	t|	�d}
|
d|kr�t�d	��|�r�|t|d�}||
}|dk�r,|�d
�q�|d|�}
tj	|
|	d�}t|�|}|dk�r�|
dd�}
tj	|
|	d�}t|�|}�qR|d|7<|t|
�d�}|r�|�d
�t|d�}q�|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.

    Nr�rr�r r�r��z3max_line_length is too small to fit an encoded wordrO)ra)
rr)r r�rir�rrrrD)Z	to_encoder�r�r�rJraZleading_wspZtrailing_wspZnew_last_ewZ	encode_as�
chrome_lenZremaining_spaceZ
text_spaceZto_encode_wordZencoded_wordZexcessrrrr�7sT��



r�c	Cs�|jD�]�\}}|d���d�s2|dd7<|}d}z|�|�d}Wn0tk
r|d}t�|�rtd}d}nd}YnX|r�tjj	|d	|d
�}	d�
|||	�}
nd�
|t|��}
t|d�t|
�d
|kr�|dd|
|d<qn"t|
�d|k�r
|�
d|
�qd}|d}|rt|�tt|��dt|�}
||
dk�rLd}||
d}}|d|�}tjj	|d	|d
�}	t|	�|k�r��q�|d
8}�q\|�
d�
||||	��d	}|d
7}||d�}|�r|dd7<�qqdS)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'.

    r�r^�strictFTr�r�r�r )Zsaferz
{}*={}''{}r�r�rOr�rz''r��NNz {}*{}*={}{})r�r�r�rDr�rr�r�r�rpr,rr�rir)r4r�r�r�r�rraZ
error_handlerZencoding_requiredZ
encoded_valuer�r�Zextra_chromer�Z
splitpointZmaxchars�partialrrrr�xsn


� ��r�)�r�rer�r��stringr�operatorrZemailrrrrr�r r4r;r7r�r>Z	TSPECIALSrsZ	ASPECIALSrxr~r�r�r�compile�VERBOSE�	MULTILINEr%r�rrMrSrVrXrYr[r]r^rcrnrorrr|r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�rrrrrr�rSrTr,r'r�r
r�matchr/�findallr
rprur|rrrr#r)r,r-r1r2r3r5r6r8r9r:r=r@rFrCrHrLrNrOrQrUrXrYrZr\r]r_r`rarbrdrergrhrmrnrqrtrwrzr}rr�r�r�r�r�r�r�r�r�r;r�r�rrrr�<module>s2E
�C"	
!*$
V	+





����
1C+
"&'/'&).9%7ED49/kA

?>