U e5dKQ@szdZddlmZddlmZddlmZddlmZGdddZGdd d Z Gd d d e Z d d Z GdddZ Gddde ZGdddZGdddeZGdddZGdddeZGdddeZGdddeZGdddZGd d!d!ZGd"d#d#eZGd$d%d%eZGd&d'd'ZGd(d)d)Zeeeeeeeeeeeeeeeeeeeed*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^eZdZdddZeddZeddZed d Zed d Zd dZ ddZ ddZ dS)AddressNcCsdtd||||f}d|ks(d|kr0td|dk r|s@|rHtdt|\}}|rjtd|||jrz|jd|j}|j }||_ ||_ ||_ dS) aCreate 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) joinfilter ValueError TypeErrorparserZ get_addr_specformat all_defects local_partdomain _display_name _username_domain)self display_nameusernamer addr_specZinputsZa_srestr,/usr/lib64/python3.8/email/headerregistry.py__init__s& zAddress.__init__cCs|jSNrrrrrr<szAddress.display_namecCs|jSr)rr rrrr@szAddress.usernamecCs|jSr)rr rrrrDszAddress.domaincCsTt|j}t|t|tjkr.t|j}n|j}|jrH|d|jS|sPdS|S)zThe addr_spec (username@domain) portion of the address, quoted according to RFC 5322 rules, but with no Content Transfer Encoding. @<>)setrlenrZ DOT_ATOM_ENDS quote_stringr)rnamesetZlprrrrHs zAddress.addr_speccCsd|jj|j|j|jS)Nz1{}(display_name={!r}, username={!r}, domain={!r}))r __class____name__rrrr rrr__repr__Xs zAddress.__repr__cCs^t|j}t|t|tjkr.t|j}n|j}|rX|jdkrFdn|j}d||S|jS)Nr"rz{} <{}>)r#rr$rSPECIALSr%rr)rr&disprrrr__str__]s  zAddress.__str__cCs8t|t|krdS|j|jko6|j|jko6|j|jkSNF)typerrrrotherrrr__eq__hs   zAddress.__eq__)rrrN) r( __module__ __qualname__rpropertyrrrrr)r,r1rrrrrs *     rc@sFeZdZdddZeddZeddZdd 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)rtuple _addresses)rr addressesrrrrrszGroup.__init__cCs|jSrrr rrrrszGroup.display_namecCs|jSr)r7r rrrr8szGroup.addressescCsd|jj|j|jS)Nz${}(display_name={!r}, addresses={!r})rr'r(rr8r rrrr)s zGroup.__repr__cCs|jdkr&t|jdkr&t|jdS|j}|dk r\t|}t|t|tjkr\t|}ddd|jD}|r~d|n|}d ||S)Nr, css|]}t|VqdSrstr).0xrrr sz Group.__str__.. z{}:{};) rr$r8r<r#rr*r%r r)rr+r&Zadrstrrrrr,s z Group.__str__cCs,t|t|krdS|j|jko*|j|jkSr-)r.rr8r/rrrr1s   z Group.__eq__)NN) r(r2r3rr4rr8r)r,r1rrrrr5ps    r5c@sTeZdZdZddZddZeddZedd Zd d Z e d d Z ddZ 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}|||t|dr4t|d|d<t||d}|d=|j|f||S)Ndefectsdecoded)parserZ_has_surrogates _sanitizer<__new__init)clsnamevaluekwdsrrrrrFs zBaseHeader.__new__cCs||_||_||_dSr)_name _parse_tree_defects)rrI parse_treerBrrrrGszBaseHeader.initcCs|jSr)rLr rrrrIszBaseHeader.namecCs t|jSr)r6rNr rrrrBszBaseHeader.defectscCst|jj|jjt|f|jfSr)_reconstruct_headerr'r( __bases__r<__dict__r rrr __reduce__szBaseHeader.__reduce__cCs t||Sr)r<rF)rHrJrrr _reconstructszBaseHeader._reconstructc Cs`ttt|jdtddgg}|jrH|ttddg||j|j |dS)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-sepr@Zfws)policy) rZHeaderZ HeaderLabelZ ValueTerminalrIrMappendZCFWSListZWhiteSpaceTerminalfold)rrVheaderrrrrXs   zBaseHeader.foldN) r(r2r3__doc__rFrGr4rIrBrS classmethodrTrXrrrrrAs      rAcCst||i|Sr)r.rT)Zcls_namebasesrJrrrrP srPc@s&eZdZdZeejZeddZ dS)UnstructuredHeaderNcCs"|||d<t|d|d<dS)NrOrC) value_parserr<rHrJrKrrrrDszUnstructuredHeader.parse) r(r2r3 max_count staticmethodrget_unstructuredr^r[rDrrrrr]s r]c@seZdZdZdS)UniqueUnstructuredHeaderr9Nr(r2r3r`rrrrrcsrccsFeZdZdZdZeejZe ddZ fddZ e ddZ ZS) DateHeaderaHeader 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|dtd|d<d|d<t|d<dSt|trJt|}||d<t |d|d<| |d|d<dS)NrBdatetimerrCrO) rWrZHeaderMissingRequiredValuerZ TokenList isinstancer<rZparsedate_to_datetimeZformat_datetimer^r_rrrrD.s   zDateHeader.parsecs|d|_tj||dS)Nrf)pop _datetimesuperrGrargskwr'rrrG<s zDateHeader.initcCs|jSr)rir rrrrf@szDateHeader.datetime)r(r2r3rZr`rarrbr^r[rDrGr4rf __classcell__rrrnrres   rec@seZdZdZdS)UniqueDateHeaderr9NrdrrrrrpEsrpcsPeZdZdZeddZeddZfddZe dd Z e d d Z Z S) AddressHeaderNcCst|\}}|rtd|S)Nzthis should not happen)rZget_address_listAssertionError)rJ address_listrrrr^Ns zAddressHeader.value_parsercCst|trV|||d<}g}|jD]"}|t|jdd|jDq&t|j }n"t |dsf|g}dd|D}g}||d<||d<d d d|D|d <d|kr||d |d<dS) NrOcSs*g|]"}t|jpd|jpd|jp"dqS)r)rrrr)r=Zmbrrr ]s  z'AddressHeader.parse..__iter__cSs&g|]}t|dstd|gn|qS)r8N)hasattrr5r=itemrrrrtfsgroupsrBr:cSsg|] }t|qSrr;rwrrrrtlsrC) rgr<r^r8rWr5rZ all_mailboxeslistrrvr )rHrJrKrsryZaddrrBrrrrDTs*     zAddressHeader.parsecs(t|d|_d|_tj||dS)Nry)r6rh_groupsr7rjrGrkrnrrrGpszAddressHeader.initcCs|jSr)r{r rrrryuszAddressHeader.groupscCs&|jdkr tdd|jD|_|jS)Ncss|]}|jD] }|Vq qdSr)r8)r=groupaddressrrrr?|sz*AddressHeader.addresses..)r7r6r{r rrrr8ys zAddressHeader.addresses) r(r2r3r`rar^r[rDrGr4ryr8rorrrnrrqJs    rqc@seZdZdZdS)UniqueAddressHeaderr9Nrdrrrrr~sr~c@seZdZeddZdS)SingleAddressHeadercCs(t|jdkrtd|j|jdS)Nr9z9value of single address header {} is not a single addressr)r$r8r rrIr rrrr}s zSingleAddressHeader.addressN)r(r2r3r4r}rrrrrsrc@seZdZdZdS)UniqueSingleAddressHeaderr9NrdrrrrrsrcsZeZdZdZeejZeddZ fddZ e ddZ e dd Z e d d ZZS) MIMEVersionHeaderr9cCs|||d<}t||d<|d|j|jdkrs z1ParameterizedMIMEHeader.parse..)r^r<rrrrrrrrDs   zParameterizedMIMEHeader.parsecs|d|_tj||dS)Nr)rh_paramsrjrGrkrnrrrGs zParameterizedMIMEHeader.initcCs t|jSr)rrr rrrrszParameterizedMIMEHeader.params) r(r2r3r`r[rDrGr4rrorrrnrrs  rcsJeZdZeejZfddZeddZ eddZ eddZ Z S) ContentTypeHeadercs2tj||t|jj|_t|jj|_dSr) rjrGrrErMmaintype _maintypesubtype_subtyperkrnrrrGszContentTypeHeader.initcCs|jSr)rr rrrrszContentTypeHeader.maintypecCs|jSr)rr rrrrszContentTypeHeader.subtypecCs|jd|jS)N/)rrr rrr content_typeszContentTypeHeader.content_type) r(r2r3rarZparse_content_type_headerr^rGr4rrrrorrrnrrs    rcs2eZdZeejZfddZeddZ Z S)ContentDispositionHeadercs2tj|||jj}|dkr"|nt||_dSr)rjrGrMcontent_dispositionrrE_content_disposition)rrlrmZcdrnrrrGszContentDispositionHeader.initcCs|jSr)rr rrrrsz,ContentDispositionHeader.content_disposition) r(r2r3rarZ parse_content_disposition_headerr^rGr4rrorrrnrrs  rcsBeZdZdZeejZeddZ fddZ e ddZ Z S)ContentTransferEncodingHeaderr9cCs2|||d<}t||d<|d|jdSNrOrCrBr^r<rrrrrrrDs z#ContentTransferEncodingHeader.parsecs"tj||t|jj|_dSr)rjrGrrErMcte_cterkrnrrrGsz"ContentTransferEncodingHeader.initcCs|jSr)rr rrrr sz!ContentTransferEncodingHeader.cte)r(r2r3r`rarZ&parse_content_transfer_encoding_headerr^r[rDrGr4rrorrrnrrs   rc@s&eZdZdZeejZeddZ dS)MessageIDHeaderr9cCs2|||d<}t||d<|d|jdSrrrrrrrDs zMessageIDHeader.parseN) r(r2r3r`rarZparse_message_idr^r[rDrrrrrs r)Zsubjectdatez resent-datez orig-dateZsenderz resent-sendertoz resent-toZccz resent-ccZbccz resent-bccfromz resent-fromzreply-toz mime-versionz content-typezcontent-dispositionzcontent-transfer-encodingz message-idc@s8eZdZdZeedfddZddZddZd d Z d S) HeaderRegistryz%A header_factory and header registry.TcCs&i|_||_||_|r"|jtdS)aCreate 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_classupdate_default_header_map)rrrZuse_default_maprrrr9s zHeaderRegistry.__init__cCs||j|<dS)zLRegister cls as the specialized class for handling "name" headers. N)rrrrIrHrrr map_to_typeKszHeaderRegistry.map_to_typecCs,|j||j}td|j||jfiS)N_)rgetrrr.r(rrrrr __getitem__QszHeaderRegistry.__getitem__cCs||||S)aCreate 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)rrIrJrrr__call__Us zHeaderRegistry.__call__N) r(r2r3rZrAr]rrrrrrrrr5s rN)rZtypesrZemailrrrrrr5r<rArPr]rcrerprqr~rrrrrrrrrrrrrrsX    `6d '7 %