a dP@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|vs(d|vr0td|dur|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 TypeErrorparser get_addr_specformat all_defects local_partdomain _display_name _username_domain)self display_nameusernamer addr_specinputsa_srestr,/usr/lib64/python3.9/email/headerregistry.py__init__s$ zAddress.__init__cCs|jSNrrrrrr<szAddress.display_namecCs|jSr!)rr#rrrr@szAddress.usernamecCs|jSr!)rr#rrrrDszAddress.domaincCs<|j}tj|st|}|jr0|d|jS|s8dS|S)zThe addr_spec (username@domain) portion of the address, quoted according to RFC 5322 rules, but with no Content Transfer Encoding. @<>)rr DOT_ATOM_ENDS isdisjoint quote_stringr)rlprrrrHs  zAddress.addr_speccCsd|jj|j|j|jS)Nz1{}(display_name={!r}, username={!r}, domain={!r}))r __class____name__rrrr#rrr__repr__Vs zAddress.__repr__cCsF|j}tj|st|}|r@|jdkr.dn|j}d||S|jS)Nr%rz{} <{}>)rrSPECIALSr'r(rr)rdisprrrr__str__[s   zAddress.__str__cCs2t|tstS|j|jko0|j|jko0|j|jkSr!) isinstancerNotImplementedrrrrotherrrr__eq__ds    zAddress.__eq__)rrrN) r+ __module__ __qualname__r propertyrrrrr,r/r4rrrrrs *     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 addressesrrrr nszGroup.__init__cCs|jSr!r"r#rrrrszGroup.display_namecCs|jSr!)r:r#rrrr;szGroup.addressescCsd|jj|j|jS)Nz${}(display_name={!r}, addresses={!r})rr*r+rr;r#rrrr,szGroup.__repr__cCs||jdur&t|jdkr&t|jdS|j}|durJtj|sJt|}ddd|jD}|rld|n|}d ||S)Nr, css|]}t|VqdSr!str).0xrrr z Group.__str__.. z{}:{};) rlenr;r?rr-r'r(r r)rr.adrstrrrrr/s z Group.__str__cCs&t|tstS|j|jko$|j|jkSr!)r0r8r1rr;r2rrrr4s    z Group.__eq__)NN) r+r5r6r r7rr;r,r/r4rrrrr8ls    r8c@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|fi||S)Ndefectsdecoded)parser_has_surrogates _sanitizer?__new__init)clsnamevaluekwdsrrrrrMs zBaseHeader.__new__cCs||_||_||_dSr!)_name _parse_tree_defects)rrP parse_treerHrrrrNszBaseHeader.initcCs|jSr!)rSr#rrrrPszBaseHeader.namecCs t|jSr!)r9rUr#rrrrHszBaseHeader.defectscCst|jj|jjt|f|jfSr!)_reconstruct_headerr*r+ __bases__r?__dict__r#rrr __reduce__szBaseHeader.__reduce__cCs t||Sr!)r?rM)rOrQrrr _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-seprDfws)policy) rHeader HeaderLabel ValueTerminalrPrTappendCFWSListWhiteSpaceTerminalfold)rr^headerrrrres   zBaseHeader.foldN) r+r5r6__doc__rMrNr7rPrHrZ classmethodr[rerrrrrGs      rGcCst||i|Sr!)typer[)cls_namebasesrQrrrrWsrWc@s&eZdZdZeejZeddZ dS)UnstructuredHeaderNcCs"|||d<t|d|d<dS)NrVrI) value_parserr?rOrQrRrrrrJ szUnstructuredHeader.parse) r+r5r6 max_count staticmethodrget_unstructuredrmrhrJrrrrrls rlc@seZdZdZdS)UniqueUnstructuredHeaderr<Nr+r5r6rorrrrrrsrrcsFeZdZdZdZeejZe 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)NrHdatetimerrIrV) rbrHeaderMissingRequiredValuer TokenListr0r?rparsedate_to_datetimeformat_datetimermrnrrrrJ(s   zDateHeader.parsecs"|d|_tj|i|dS)Nru)pop _datetimesuperrNrargskwr*rrrN6s zDateHeader.initcCs|jSr!)r{r#rrrru:szDateHeader.datetime)r+r5r6rgrorprrqrmrhrJrNr7ru __classcell__rrrrrts   rtc@seZdZdZdS)UniqueDateHeaderr<Nrsrrrrr?srcsPeZdZdZeddZeddZfddZe dd Z e d d Z Z S) AddressHeaderNcCst|\}}|rJd|S)Nzthis should not happen)rget_address_list)rQ address_listrrrrmHs 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|vr||d |d<dS) NrVcSs*g|]"}t|jpd|jpd|jp"dqS)r)rrrr)r@mbrrr Ws  z'AddressHeader.parse..__iter__cSs&g|]}t|dstd|gn|qS)r;N)hasattrr8r@itemrrrr`sgroupsrHr=cSsg|] }t|qSrr>rrrrrfrCrI) r0r?rmr;rbr8r all_mailboxeslistrrr )rOrQrRrraddrrHrrrrJNs*     zAddressHeader.parsecs,t|d|_d|_tj|i|dS)Nr)r9rz_groupsr:r|rNr}rrrrNjszAddressHeader.initcCs|jSr!)rr#rrrroszAddressHeader.groupscCs&|jdur tdd|jD|_|jS)Ncss|]}|jD] }|Vq qdSr!)r;)r@groupaddressrrrrBvs z*AddressHeader.addresses..)r:r9rr#rrrr;ss zAddressHeader.addresses) r+r5r6rorprmrhrJrNr7rr;rrrrrrDs    rc@seZdZdZdS)UniqueAddressHeaderr<Nrsrrrrr{src@seZdZeddZdS)SingleAddressHeadercCs(t|jdkrtd|j|jdS)Nr<z9value of single address header {} is not a single addressr)rEr;r rrPr#rrrrs zSingleAddressHeader.addressN)r+r5r6r7rrrrrrsrc@seZdZdZdS)UniqueSingleAddressHeaderr<NrsrrrrrsrcsZeZdZdZeejZeddZ fddZ e ddZ e dd Z e d d ZZS) MIMEVersionHeaderr<cCs|||d<}t||d<|d|j|jdurs z1ParameterizedMIMEHeader.parse..)rmr?rrrrrrrrJs   zParameterizedMIMEHeader.parsecs"|d|_tj|i|dS)Nr)rz_paramsr|rNr}rrrrNs zParameterizedMIMEHeader.initcCs t|jSr!)rrr#rrrrszParameterizedMIMEHeader.params) r+r5r6rorhrJrNr7rrrrrrrs  rcsJeZdZeejZfddZeddZ eddZ eddZ Z S) ContentTypeHeadercs6tj|i|t|jj|_t|jj|_dSr!) r|rNrrLrTmaintype _maintypesubtype_subtyper}rrrrNszContentTypeHeader.initcCs|jSr!)rr#rrrrszContentTypeHeader.maintypecCs|jSr!)rr#rrrrszContentTypeHeader.subtypecCs|jd|jS)N/)rrr#rrr content_typeszContentTypeHeader.content_type) r+r5r6rprparse_content_type_headerrmrNr7rrrrrrrrrs    rcs2eZdZeejZfddZeddZ Z S)ContentDispositionHeadercs6tj|i||jj}|dur&|nt||_dSr!)r|rNrTcontent_dispositionrrL_content_disposition)rr~rcdrrrrNszContentDispositionHeader.initcCs|jSr!)rr#rrrrsz,ContentDispositionHeader.content_disposition) r+r5r6rpr parse_content_disposition_headerrmrNr7rrrrrrrs  rcsBeZdZdZeejZeddZ fddZ e ddZ Z S)ContentTransferEncodingHeaderr<cCs2|||d<}t||d<|d|jdSNrVrIrHrmr?rrrrrrrJs z#ContentTransferEncodingHeader.parsecs&tj|i|t|jj|_dSr!)r|rNrrLrTcte_cter}rrrrNsz"ContentTransferEncodingHeader.initcCs|jSr!)rr#rrrrsz!ContentTransferEncodingHeader.cte)r+r5r6rorpr&parse_content_transfer_encoding_headerrmrhrJrNr7rrrrrrrs   rc@s&eZdZdZeejZeddZ dS)MessageIDHeaderr<cCs2|||d<}t||d<|d|jdSrrrrrrrJs zMessageIDHeader.parseN) r+r5r6rorprparse_message_idrmrhrJrrrrr s r)subjectdatez resent-datez orig-datesenderz resent-sendertoz resent-toccz resent-ccbccz 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)rrruse_default_maprrrr 3s zHeaderRegistry.__init__cCs||j|<dS)zLRegister cls as the specialized class for handling "name" headers. N)rrrrPrOrrr map_to_typeEszHeaderRegistry.map_to_typecCs,|j||j}td|j||jfiS)N_)rgetrrrir+rrrrr __getitem__KszHeaderRegistry.__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)rrPrQrrr__call__Os zHeaderRegistry.__call__N) r+r5r6rgrGrlr rrrrrrrr/s rN)rgtypesremailrrrrrr8r?rGrWrlrrrtrrrrrrrrrrrrrrrrrsX    \4d '7 %