3 ¦ fßOã@sjdZddlmZddlmZddlmZddlmZGdd„dƒZGdd „d ƒZ Gd d „d e ƒZ d d „Z Gdd„dƒZ Gdd„de ƒZGdd„dƒZGdd„deƒZGdd„dƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„dƒZGd d!„d!ƒZGd"d#„d#eƒZGd$d%„d%eƒZGd&d'„d'ƒZeeeeeeeeeeeeeeeeeeed(œZGd)d*„d*ƒZd+S),a;Representing and manipulating email headers via custom objects. This module provides an implementation of the HeaderRegistry API. The implementation is designed to flexibly follow RFC5322 rules. Eventually HeaderRegistry will be a public API, but it isn't yet, and will probably change some before that happens. é)ÚMappingProxyType)Úutils)Úerrors)Ú_header_value_parserc@s^eZdZddd„Zedd„ƒZedd„ƒZed d „ƒZed d „ƒZd d„Z dd„Z dd„Z dS)ÚAddressÚNcCsœdjtd||||fƒƒ}d|ks(d|kr0tdƒ‚|dk r†|s@|rHtdƒ‚tj|ƒ\}}|rjtdj||ƒƒ‚|jrz|jd‚|j}|j }||_ ||_ ||_ dS) a÷Create an object representing a full email address. An address can have a 'display_name', a 'username', and a 'domain'. In addition to specifying the username and domain separately, they may be specified together by using the addr_spec keyword *instead of* the username and domain keywords. If an addr_spec string is specified it must be properly quoted according to RFC 5322 rules; an error will be raised if it is not. An Address object has display_name, username, domain, and addr_spec attributes, all of which are read-only. The addr_spec and the string value of the object are both quoted according to RFC5322 rules, but without any Content Transfer Encoding. rNú Ú z8invalid arguments; address parts cannot contain CR or LFz=addrspec specified when username and/or domain also specifiedz6Invalid addr_spec; only '{}' could be parsed from '{}'r) ÚjoinÚfilterÚ ValueErrorÚ TypeErrorÚparserZ get_addr_specÚformatÚ all_defectsÚ local_partÚdomainÚ _display_nameÚ _usernameÚ_domain)ÚselfÚ display_nameÚusernamerÚ addr_specZinputsZa_sÚrest©rú9/opt/alt/python36/lib64/python3.6/email/headerregistry.pyÚ__init__s"  zAddress.__init__cCs|jS)N)r)rrrrr<szAddress.display_namecCs|jS)N)r)rrrrr@szAddress.usernamecCs|jS)N)r)rrrrrDszAddress.domaincCsTt|jƒ}t|ƒt|tjƒkr.tj|jƒ}n|j}|jrH|d|jS|sPdS|S)z“The addr_spec (username@domain) portion of the address, quoted according to RFC 5322 rules, but with no Content Transfer Encoding. ú@z<>)ÚsetrÚlenrZ DOT_ATOM_ENDSÚ quote_stringr)rÚnamesetZlprrrrHs zAddress.addr_speccCsdj|jj|j|j|jƒS)Nz1{}(display_name={!r}, username={!r}, domain={!r}))rÚ __class__Ú__name__rrr)rrrrÚ__repr__XszAddress.__repr__cCs^t|jƒ}t|ƒt|tjƒkr.tj|jƒ}n|j}|rX|jdkrFdn|j}dj||ƒS|jS)Nz<>rz{} <{}>)rrr rÚSPECIALSr!rr)rr"ÚdisprrrrÚ__str__]s  zAddress.__str__cCs8t|ƒt|ƒkrdS|j|jko6|j|jko6|j|jkS)NF)Útyperrr)rÚotherrrrÚ__eq__hs   zAddress.__eq__)rrrN) r$Ú __module__Ú __qualname__rÚpropertyrrrrr%r(r+rrrrrs *     rc@sFeZdZddd„Zedd„ƒZedd„ƒZdd „Zd d „Zd d „Z dS)ÚGroupNcCs||_|rt|ƒntƒ|_dS)aCreate an object representing an address group. An address group consists of a display_name followed by colon and a list of addresses (see Address) terminated by a semi-colon. The Group is created by specifying a display_name and a possibly empty list of Address objects. A Group can also be used to represent a single address that is not in a group, which is convenient when manipulating lists that are a combination of Groups and individual Addresses. In this case the display_name should be set to None. In particular, the string representation of a Group whose display_name is None is the same as the Address object, if there is one and only one Address object in the addresses list. N)rÚtupleÚ _addresses)rrÚ addressesrrrrrszGroup.__init__cCs|jS)N)r)rrrrr„szGroup.display_namecCs|jS)N)r1)rrrrr2ˆszGroup.addressescCsdj|jj|j|jƒS)Nz${}(display_name={!r}, addresses={!r})rr#r$rr2)rrrrr%ŒszGroup.__repr__cCsŽ|jdkr&t|jƒdkr&t|jdƒS|j}|dk r\t|ƒ}t|ƒt|tjƒkr\tj|ƒ}djdd„|jDƒƒ}|r~d|n|}dj ||ƒS)Nérz, css|]}t|ƒVqdS)N)Ústr)Ú.0Úxrrrú ™sz Group.__str__..ú z{}:{};) rr r2r4rrr&r!r r)rr'r"Zadrstrrrrr(‘s z Group.__str__cCs,t|ƒt|ƒkrdS|j|jko*|j|jkS)NF)r)rr2)rr*rrrr+�s z Group.__eq__)NN) r$r,r-rr.rr2r%r(r+rrrrr/ps     r/c@sTeZdZdZdd„Zdd„Zedd„ƒZedd „ƒZd d „Z e d d „ƒZ dd„Z dS)Ú BaseHeadera|Base class for message headers. Implements generic behavior and provides tools for subclasses. A subclass must define a classmethod named 'parse' that takes an unfolded value string and a dictionary as its arguments. The dictionary will contain one key, 'defects', initialized to an empty list. After the call the dictionary must contain two additional keys: parse_tree, set to the parse tree obtained from parsing the header, and 'decoded', set to the string value of the idealized representation of the data from the value. (That is, encoded words are decoded, and values that have canonical representations are so represented.) The defects key is intended to collect parsing defects, which the message parser will subsequently dispose of as appropriate. The parser should not, insofar as practical, raise any errors. Defects should be added to the list instead. The standard header parsers register defects for RFC compliance issues, for obsolete RFC syntax, and for unrecoverable parsing errors. The parse method may add additional keys to the dictionary. In this case the subclass must define an 'init' method, which will be passed the dictionary as its keyword arguments. The method should use (usually by setting them as the value of similarly named attributes) and remove all the extra keys added by its parse method, and then use super to call its parent class with the remaining arguments and keywords. The subclass should also make sure that a 'max_count' attribute is defined that is either None or 1. XXX: need to better define this API. cCs\dgi}|j||ƒtj|dƒr4tj|dƒ|d<tj||dƒ}|d=|j|f|Ž|S)NÚdefectsÚdecoded)ÚparserZ_has_surrogatesÚ _sanitizer4Ú__new__Úinit)ÚclsÚnameÚvalueÚkwdsrrrrr>Ès zBaseHeader.__new__cCs||_||_||_dS)N)Ú_nameÚ _parse_treeÚ_defects)rrAÚ parse_treer:rrrr?ÒszBaseHeader.initcCs|jS)N)rD)rrrrrA×szBaseHeader.namecCs t|jƒS)N)r0rF)rrrrr:ÛszBaseHeader.defectscCst|jj|jjt|ƒf|jfS)N)Ú_reconstruct_headerr#r$Ú __bases__r4Ú__dict__)rrrrÚ __reduce__ßs zBaseHeader.__reduce__cCs tj||ƒS)N)r4r>)r@rBrrrÚ _reconstructészBaseHeader._reconstructcCs`tjtjtj|jdƒtjddƒgƒgƒ}|jrH|jtjtjddƒgƒƒ|j|jƒ|j |d�S)atFold header according to policy. The parsed representation of the header is folded according to RFC5322 rules, as modified by the policy. If the parse tree contains surrogateescaped bytes, the bytes are CTE encoded using the charset 'unknown-8bit". Any non-ASCII characters in the parse tree are CTE encoded using charset utf-8. XXX: make this a policy setting. The returned value is an ASCII-only string possibly containing linesep characters, and ending with a linesep character. The string includes the header name and the ': ' separator. z header-nameú:z header-sepr8Zfws)Úpolicy) rZHeaderZ HeaderLabelZ ValueTerminalrArEÚappendZCFWSListZWhiteSpaceTerminalÚfold)rrNÚheaderrrrrPís  zBaseHeader.foldN) r$r,r-Ú__doc__r>r?r.rAr:rKÚ classmethodrLrPrrrrr9¦s     r9cCst||iƒj|ƒS)N)r)rL)Zcls_nameÚbasesrBrrrrH srHc@s&eZdZdZeejƒZedd„ƒZ dS)ÚUnstructuredHeaderNcCs"|j|ƒ|d<t|dƒ|d<dS)NrGr;)Ú value_parserr4)r@rBrCrrrr<szUnstructuredHeader.parse) r$r,r-Ú max_countÚ staticmethodrÚget_unstructuredrVrSr<rrrrrUs rUc@seZdZdZdS)ÚUniqueUnstructuredHeaderr3N)r$r,r-rWrrrrrZsrZcsFeZdZdZdZeejƒZe dd„ƒZ ‡fdd„Z e dd„ƒZ ‡ZS) Ú DateHeadera‘Header whose value consists of a single timestamp. Provides an additional attribute, datetime, which is either an aware datetime using a timezone, or a naive datetime if the timezone in the input string is -0000. Also accepts a datetime as input. The 'value' attribute is the normalized form of the timestamp, which means it is the output of format_datetime on the datetime. NcCsz|s6|djtjƒƒd|d<d|d<tjƒ|d<dSt|tƒrJtj|ƒ}||d<tj |dƒ|d<|j |dƒ|d<dS)Nr:Údatetimerr;rG) rOrZHeaderMissingRequiredValuerZ TokenListÚ isinstancer4rZparsedate_to_datetimeZformat_datetimerV)r@rBrCrrrr<.s   zDateHeader.parsecs|jdƒ|_tƒj||ŽdS)Nr\)ÚpopÚ _datetimeÚsuperr?)rÚargsÚkw)r#rrr?<s zDateHeader.initcCs|jS)N)r_)rrrrr\@szDateHeader.datetime)r$r,r-rRrWrXrrYrVrSr<r?r.r\Ú __classcell__rr)r#rr[s     r[c@seZdZdZdS)ÚUniqueDateHeaderr3N)r$r,r-rWrrrrrdEsrdcsPeZdZdZedd„ƒZedd„ƒZ‡fdd„Ze dd „ƒZ e d d „ƒZ ‡Z S) Ú AddressHeaderNcCs tj|ƒ\}}| stdƒ‚|S)Nzthis should not happen)rZget_address_listÚAssertionError)rBÚ address_listrrrrVNszAddressHeader.value_parsercCsÂt|tƒrZ|j|ƒ|d<}g}x,|jD]"}|jt|jdd„|jDƒƒƒq(Wt|j ƒ}n"t |dƒsj|g}dd„|Dƒ}g}||d<||d<dj d d„|Dƒƒ|d <d|kr¾|j|d ƒ|d<dS) NrGcSs*g|]"}t|jpd|jpd|jp"dƒ‘qS)r)rrrr)r5Zmbrrrú ]sz'AddressHeader.parse..Ú__iter__cSs&g|]}t|dƒstd|gƒn|‘qS)r2N)Úhasattrr/)r5ÚitemrrrrhfsÚgroupsr:z, cSsg|] }t|ƒ‘qSr)r4)r5rkrrrrhlsr;) r]r4rVr2rOr/rZ all_mailboxesÚlistrrjr )r@rBrCrgrlZaddrr:rrrr<Ts$     zAddressHeader.parsecs(t|jdƒƒ|_d|_tƒj||ŽdS)Nrl)r0r^Ú_groupsr1r`r?)rrarb)r#rrr?pszAddressHeader.initcCs|jS)N)rn)rrrrrluszAddressHeader.groupscCs&|jdkr tdd„|jDƒƒ|_|jS)NcSsg|]}|jD]}|‘qqSr)r2)r5ÚgroupÚaddressrrrrh|sz+AddressHeader.addresses..)r1r0rn)rrrrr2ys zAddressHeader.addresses) r$r,r-rWrXrVrSr<r?r.rlr2rcrr)r#rreJs     rec@seZdZdZdS)ÚUniqueAddressHeaderr3N)r$r,r-rWrrrrrq�srqc@seZdZedd„ƒZdS)ÚSingleAddressHeadercCs(t|jƒdkrtdj|jƒƒ‚|jdS)Nr3z9value of single address header {} is not a single addressr)r r2r rrA)rrrrrpˆs zSingleAddressHeader.addressN)r$r,r-r.rprrrrrr†srrc@seZdZdZdS)ÚUniqueSingleAddressHeaderr3N)r$r,r-rWrrrrrs�srscsZeZdZdZeejƒZedd„ƒZ ‡fdd„Z e dd„ƒZ e dd „ƒZ e d d „ƒZ‡ZS) ÚMIMEVersionHeaderr3cCs€|j|ƒ|d<}t|ƒ|d<|dj|jƒ|jdkrÊsz1ParameterizedMIMEHeader.parse..)rVr4rxrr})r@rBrCrGrrrr<Ás   zParameterizedMIMEHeader.parsecs|jdƒ|_tƒj||ŽdS)Nr})r^Ú_paramsr`r?)rrarb)r#rrr?Îs zParameterizedMIMEHeader.initcCs t|jƒS)N)rr€)rrrrr}ÒszParameterizedMIMEHeader.params) r$r,r-rWrSr<r?r.r}rcrr)r#rr|ºs r|csJeZdZeejƒZ‡fdd„Zedd„ƒZ edd„ƒZ edd„ƒZ ‡Z S) ÚContentTypeHeadercs2tƒj||Žtj|jjƒ|_tj|jjƒ|_dS)N) r`r?rr=rEÚmaintypeÚ _maintypeÚsubtypeÚ_subtype)rrarb)r#rrr?ÛszContentTypeHeader.initcCs|jS)N)rƒ)rrrrr‚àszContentTypeHeader.maintypecCs|jS)N)r…)rrrrr„äszContentTypeHeader.subtypecCs|jd|jS)Nú/)r‚r„)rrrrÚ content_typeèszContentTypeHeader.content_type) r$r,r-rXrZparse_content_type_headerrVr?r.r‚r„r‡rcrr)r#rr�×s     r�cs2eZdZeejƒZ‡fdd„Zedd„ƒZ ‡Z S)ÚContentDispositionHeadercs2tƒj||Ž|jj}|dkr"|ntj|ƒ|_dS)N)r`r?rEÚcontent_dispositionrr=Ú_content_disposition)rrarbZcd)r#rrr?ñszContentDispositionHeader.initcCs|jS)N)rŠ)rrrrr‰ösz,ContentDispositionHeader.content_disposition) r$r,r-rXrZ parse_content_disposition_headerrVr?r.r‰rcrr)r#rrˆís  rˆcsBeZdZdZeejƒZedd„ƒZ ‡fdd„Z e dd„ƒZ ‡Z S)ÚContentTransferEncodingHeaderr3cCs2|j|ƒ|d<}t|ƒ|d<|dj|jƒdS)NrGr;r:)rVr4rxr)r@rBrCrGrrrr<s z#ContentTransferEncodingHeader.parsecs"tƒj||Žtj|jjƒ|_dS)N)r`r?rr=rEÚcteÚ_cte)rrarb)r#rrr?sz"ContentTransferEncodingHeader.initcCs|jS)N)r�)rrrrrŒ sz!ContentTransferEncodingHeader.cte)r$r,r-rWrXrZ&parse_content_transfer_encoding_headerrVrSr<r?r.rŒrcrr)r#rr‹ûs    r‹)ZsubjectZdatez resent-datez orig-dateZsenderz resent-senderZtoz resent-toZccz resent-ccZbccz resent-bccÚfromz resent-fromzreply-toz mime-versionz content-typezcontent-dispositionzcontent-transfer-encodingc@s8eZdZdZeedfdd„Zdd„Zdd„Zd d „Z d S) ÚHeaderRegistryz%A header_factory and header registry.TcCs&i|_||_||_|r"|jjtƒdS)aèCreate a header_factory that works with the Policy API. base_class is the class that will be the last class in the created header class's __bases__ list. default_class is the class that will be used if "name" (see __call__) does not appear in the registry. use_default_map controls whether or not the default mapping of names to specialized classes is copied in to the registry when the factory is created. The default is True. N)ÚregistryÚ base_classÚ default_classÚupdateÚ_default_header_map)rr‘r’Zuse_default_maprrrr,s zHeaderRegistry.__init__cCs||j|jƒ<dS)zLRegister cls as the specialized class for handling "name" headers. N)r�r~)rrAr@rrrÚ map_to_type>szHeaderRegistry.map_to_typecCs,|jj|jƒ|jƒ}td|j||jfiƒS)NÚ_)r�Úgetr~r’r)r$r‘)rrAr@rrrÚ __getitem__DszHeaderRegistry.__getitem__cCs||||ƒS)aƒCreate a header instance for header 'name' from 'value'. Creates a header instance by creating a specialized class for parsing and representing the specified header by combining the factory base_class with a specialized class from the registry or the default_class, and passing the name and value to the constructed class's constructor. r)rrArBrrrÚ__call__Hs zHeaderRegistry.__call__N) r$r,r-rRr9rUrr•r˜r™rrrrr�(s  r�N)rRÚtypesrZemailrrrrrr/r4r9rHrUrZr[rdrerqrrrsrtr|r�rˆr‹r”r�rrrrÚ sR    `6d '7 %