o w[eMQ@sdZddlmZddlmZddlmZddlmZGdddZGdd 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)Zid*ed+ed,ed-ed.ed/ed0ed1ed2ed3ed4ed5ed6ed7ed8ed9ed:eeeed;ZGdS)?zRepresenting 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. )MappingProxyType)utils)errors)_header_value_parserc@s^eZdZdddZeddZeddZed d Zed d Zd dZ ddZ ddZ dS)AddressNcCsdtd||||f}d|vsd|vrtd|durC|s |r$tdt|\}}|r5td|||jr=|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/lib/python3.10/email/headerregistry.py__init__s(  zAddress.__init__cC|jSNrrrrrr8zAddress.display_namecCr!r")rr$rrrr<r%zAddress.usernamecCr!r")rr$rrrr@r%zAddress.domaincCs<|j}tj|st|}|jr|d|jS|sdS|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)rlprrrrDs  zAddress.addr_speccCsd|jj|j|j|jS)Nz1{}(display_name={!r}, username={!r}, domain={!r}))r __class____name__rrrr$rrr__repr__Rs zAddress.__repr__cCsF|j}tj|st|}|r |jdkrdn|j}d||S|jS)Nr'rz{} <{}>)rrSPECIALSr)r*rr)rdisprrrr__str__Ws   zAddress.__str__cCs2t|tstS|j|jko|j|jko|j|jkSr") isinstancerNotImplementedrrrrotherrrr__eq__`s    zAddress.__eq__)rrrN) r- __module__ __qualname__r propertyrrrrr.r1r6rrrrr s *      rc@sFeZdZdddZeddZeddZdd Zd d Zd d Z dS)GroupNcCs$||_|r t||_dSt|_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 jszGroup.__init__cCr!r"r#r$rrrr|r%zGroup.display_namecCr!r")r<r$rrrr=r%zGroup.addressescCsd|jj|j|jS)Nz${}(display_name={!r}, addresses={!r})rr,r-rr=r$rrrr.szGroup.__repr__cCs||jdurt|jdkrt|jdS|j}|dur%tj|s%t|}ddd|jD}|r6d|n|}d ||S)Nr, css|]}t|VqdSr"str).0xrrr sz Group.__str__.. z{}:{};) rlenr=rArr/r)r*r r)rr0adrstrrrrr1s  z Group.__str__cCs&t|tstS|j|jko|j|jkSr")r2r:r3rr=r4rrrr6s   z Group.__eq__)NN) r-r7r8r r9rr=r.r1r6rrrrr:hs    r:c@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|drt|d|d<t||d}|d=|j|fi||S)Ndefectsdecoded)parser_has_surrogates _sanitizerA__new__init)clsnamevaluekwdsrrrrrNs zBaseHeader.__new__cCs||_||_||_dSr")_name _parse_tree_defects)rrQ parse_treerIrrrrOs zBaseHeader.initcCr!r")rTr$rrrrQr%zBaseHeader.namecC t|jSr")r;rVr$rrrrI zBaseHeader.defectscCst|jj|jjt|f|jfSr")_reconstruct_headerr,r- __bases__rA__dict__r$rrr __reduce__szBaseHeader.__reduce__cCs t||Sr")rArN)rPrRrrr _reconstructs zBaseHeader._reconstructc Cs`ttt|jdtddgg}|jr$|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-seprEfws)policy) rHeader HeaderLabel ValueTerminalrQrUappendCFWSListWhiteSpaceTerminalfold)rraheaderrrrrhs    zBaseHeader.foldN) r-r7r8__doc__rNrOr9rQrIr] classmethodr^rhrrrrrHs      rHcCst||i|Sr")typer^)cls_namebasesrRrrrrZsrZc@s&eZdZdZeejZeddZ dS)UnstructuredHeaderNcCs"|||d<t|d|d<dS)NrWrJ) value_parserrArPrRrSrrrrK szUnstructuredHeader.parse) r-r7r8 max_count staticmethodrget_unstructuredrprkrKrrrrro  roc@eZdZdZdS)UniqueUnstructuredHeaderr>Nr-r7r8rrrrrrrwrwcsFeZdZdZdZeejZe 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. NcCs|s|dtd|d<d|d<t|d<dSt|trJ||d<zt|}Wnt yI|dt dd|d<t|d<YdSw||d<t |d|d<| |d|d<dS)NrIdatetimerrJrWzInvalid date value or format) rerHeaderMissingRequiredValuer TokenListr2rArparsedate_to_datetimer InvalidDateDefectformat_datetimerprqrrrrK$s&    zDateHeader.parsec"|d|_tj|i|dS)Nr{)pop _datetimesuperrOrargskwr,rrrO9 zDateHeader.initcCr!r")rr$rrrr{=r%zDateHeader.datetime)r-r7r8rjrrrsrrtrprkrKrOr9r{ __classcell__rrrrrzs   rzc@rv)UniqueDateHeaderr>NrxrrrrrBryrcsPeZdZdZeddZeddZfddZe dd Z e d d Z Z S) AddressHeaderNcCst|\}}|r Jd|S)Nzthis should not happen)rget_address_list)rR address_listrrrrpKs zAddressHeader.value_parsercCst|tr+|||d<}g}|jD]}|t|jdd|jDqt|j }nt |ds3|g}dd|D}g}||d<||d<d d d|D|d <d|vr_||d |d<dSdS) NrWcSs*g|]}t|jp d|jp d|jpdqS)r)rrrr)rBmbrrr Zs  z'AddressHeader.parse..__iter__cSs&g|]}t|dstd|gn|qS)r=N)hasattrr:rBitemrrrrcs groupsrIr?cSsg|]}t|qSrr@rrrrrisrJ) r2rArpr=rer:r all_mailboxeslistrrr )rPrRrSrraddrrIrrrrKQs,     zAddressHeader.parsecs,t|d|_d|_tj|i|dS)Nr)r;r_groupsr<rrOrrrrrOmszAddressHeader.initcCr!r")rr$rrrrrr%zAddressHeader.groupscCs&|jdurtdd|jD|_|jS)Ncss |] }|jD]}|VqqdSr")r=)rBgroupaddressrrrrDys z*AddressHeader.addresses..)r<r;rr$rrrr=vs zAddressHeader.addresses) r-r7r8rrrsrprkrKrOr9rr=rrrrrrGs    rc@rv)UniqueAddressHeaderr>Nrxrrrrr~ryrc@seZdZeddZdS)SingleAddressHeadercCs(t|jdkrtd|j|jdS)Nr>z9value of single address header {} is not a single addressr)rFr=r rrQr$rrrrs  zSingleAddressHeader.addressN)r-r7r8r9rrrrrrsrc@rv)UniqueSingleAddressHeaderr>NrxrrrrrryrcsZeZdZdZeejZeddZ fddZ e ddZ e dd Z e d d ZZS) MIMEVersionHeaderr>cCs|||d<}t||d<|d|j|jdurdn|j|d<|j|d<|jdur;d|d|d|d<dSd|d<dS)NrWrJrImajorminorz{}.{}version)rprAextendrrrrrPrRrSrWrrrrKs    zMIMEVersionHeader.parsecs:|d|_|d|_|d|_tj|i|dS)Nrrr)r_version_major_minorrrOrrrrrOs   zMIMEVersionHeader.initcCr!r")rr$rrrrr%zMIMEVersionHeader.majorcCr!r")rr$rrrrr%zMIMEVersionHeader.minorcCr!r")rr$rrrrr%zMIMEVersionHeader.version)r-r7r8rrrsrparse_mime_versionrprkrKrOr9rrrrrrrrrs     rcs8eZdZdZeddZfddZeddZZ S)ParameterizedMIMEHeaderr>cCs\|||d<}t||d<|d|j|jdur"i|d<dSdd|jD|d<dS)NrWrJrIparamscSs&i|]\}}t|t|qSr)rrMlower)rBrQrRrrr s  z1ParameterizedMIMEHeader.parse..)rprArrrrrrrrKs   zParameterizedMIMEHeader.parsecr)Nr)r_paramsrrOrrrrrOrzParameterizedMIMEHeader.initcCrXr")rrr$rrrrrYzParameterizedMIMEHeader.params) r-r7r8rrrkrKrOr9rrrrrrrs  rcsJeZdZeejZfddZeddZ eddZ eddZ Z S) ContentTypeHeadercs6tj|i|t|jj|_t|jj|_dSr") rrOrrMrUmaintype _maintypesubtype_subtyperrrrrOszContentTypeHeader.initcCr!r")rr$rrrrr%zContentTypeHeader.maintypecCr!r")rr$rrrrr%zContentTypeHeader.subtypecCs|jd|jS)N/)rrr$rrr content_typeszContentTypeHeader.content_type) r-r7r8rsrparse_content_type_headerrprOr9rrrrrrrrrs    rcs2eZdZeejZfddZeddZ Z S)ContentDispositionHeadercs<tj|i||jj}|dur||_dSt||_dSr")rrOrUcontent_dispositionrrM_content_disposition)rrrcdrrrrOs"zContentDispositionHeader.initcCr!r")rr$rrrrr%z,ContentDispositionHeader.content_disposition) r-r7r8rsr parse_content_disposition_headerrprOr9rrrrrrrs   rcsBeZdZdZeejZeddZ fddZ e ddZ Z S)ContentTransferEncodingHeaderr>cC2|||d<}t||d<|d|jdSNrWrJrIrprArrrrrrrK z#ContentTransferEncodingHeader.parsecs&tj|i|t|jj|_dSr")rrOrrMrUcte_cterrrrrOsz"ContentTransferEncodingHeader.initcCr!r")rr$rrrrr%z!ContentTransferEncodingHeader.cte)r-r7r8rrrsr&parse_content_transfer_encoding_headerrprkrKrOr9rrrrrrrs   rc@s&eZdZdZeejZeddZ dS)MessageIDHeaderr>cCrrrrrrrrKrzMessageIDHeader.parseN) r-r7r8rrrsrparse_message_idrprkrKrrrrr rursubjectdatez resent-datez orig-datesenderz resent-sendertoz resent-toccz resent-ccbccz resent-bccfromz resent-fromzreply-toz mime-versionz content-type)zcontent-dispositionzcontent-transfer-encodingz message-idc@s8eZdZdZeedfddZddZddZd d Z d S) HeaderRegistryz%A header_factory and header registry.TcCs*i|_||_||_|r|jtdSdS)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 6s  zHeaderRegistry.__init__cCs||j|<dS)zLRegister cls as the specialized class for handling "name" headers. N)rrrrQrPrrr map_to_typeHszHeaderRegistry.map_to_typecCs,|j||j}td|j||jfiS)N_)rgetrrrlr-rrrrr __getitem__NszHeaderRegistry.__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)rrQrRrrr__call__Rs zHeaderRegistry.__call__N) r-r7r8rjrHror rrrrrrrr2s  rN)rjtypesremailrrrrrr:rArHrZrorwrzrrrrrrrrrrrrrrrrrs~    \4d .7 %