U e5d@sddgZddlZddlZddlZddlmZmZddlmZddlm Z ddl m Z m Z ddlm Zdd lmZejZd Zed Zd d ZdddZddZddZGdddZGdddeZGdddeZdS)Message EmailMessageN)BytesIOStringIO)utils)errors)Policycompat32charset)decode_bz; z[ \(\)<>@,;:\\"/\[\]\?=]cCs4t|d\}}}|s$|dfS||fS)N;)str partitionstrip)paramasepbr%/usr/lib64/python3.8/email/message.py _splitparams rTcCs|dk rt|dkrt|trL|d7}t|d|d|d}d||fSz|dWn6tk r|d7}t|dd}d||fYSX|st|rd |t |fSd||fSn|SdS) Nr*%s=%sasciizutf-8z%s="%s") len isinstancetuplerZencode_rfc2231encodeUnicodeEncodeError tspecialssearchquote)rvaluer%rrr _formatparam's   r'cCsdt|}g}|dddkr|dd}|d}|dkrp|dd||dd|drp|d|d}q6|dkrt|}|d|}d|kr|d}|d|d||dd}||||d}q|S)Nr rr"z\"r=)rfindcountrindexrlowerappend)sZplistendfirrr _parseparamIs    (  ,r3cCs4t|tr&|d|dt|dfSt|SdS)Nrrr)rr runquote)r&rrr _unquotevalue]s r5c@seZdZefddZddZdcdd Zd d Zddd d ZddZ ddZ ddZ ddZ deddZ dfddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zdgd0d1Zd2d3Zd4d5Zdhd6d7Zd8d9Zd:d;Zdd?Z!d@dAZ"dBdCZ#dDdEZ$dFdGZ%didJdKZ&djdLdMZ'dkdPdQZ(dldRdSZ)dmdTdUZ*dndVdWZ+dodXdYZ,dZd[Z-dpd\d]Z.dqd^d_Z/d`daZ0ddbl1m2Z2dS)rrcCs:||_g|_d|_d|_d|_d|_|_g|_d|_dS)N text/plain) policy_headers _unixfrom_payload_charsetZpreambleZepiloguedefects _default_type)selfr7rrr__init__xs zMessage.__init__cCs|SN) as_stringr>rrr__str__szMessage.__str__FrNcCsJddlm}|dkr|jn|}t}||d||d}|j||d|S)Nr) GeneratorF) mangle_from_ maxheaderlenr7unixfrom)email.generatorrDr7rflattengetvalue)r>rHrFr7rDfpgrrrrAs zMessage.as_stringcCs|Sr@)as_bytesrBrrr __bytes__szMessage.__bytes__cCsHddlm}|dkr|jn|}t}||d|d}|j||d|S)Nr)BytesGeneratorF)rEr7rG)rIrPr7rrJrK)r>rHr7rPrLrMrrrrNs  zMessage.as_bytescCs t|jtSr@)rr:listrBrrr is_multipartszMessage.is_multipartcCs ||_dSr@r9)r>rHrrr set_unixfromszMessage.set_unixfromcCs|jSr@rSrBrrr get_unixfromszMessage.get_unixfromcCsF|jdkr|g|_n.z|j|Wntk r@tdYnXdS)Nz=Attach is not valid on a message with a non-multipart payload)r:r.AttributeError TypeError)r>payloadrrrattachs   zMessage.attachc Cs|r(|rdS|dkr|jS|j|S|dk rNt|jtsNtdt|j|j}t|dd}t|trt |r| dd}|sz| | ddd}Wqtk r| dd}YqXn2|rz| d}Wntk r| d}YnX|s|S|d krt|S|d krVtd |\}}|D]}|j||q<|S|d krt|} t} ztj | | d d| WStjk r|YSXt|tr|S|S)NzExpected list, got %szcontent-transfer-encodingrrsurrogateescaper replaceraw-unicode-escapezquoted-printablebase64)z x-uuencodeZuuencodeZuuezx-uueT)quiet)rRr:rrQrWtypergetr-rZ_has_surrogatesr!decode get_param LookupError UnicodeErrorquopriZ decodestringr join splitlinesr7Z handle_defectruurKError) r>r2rbrXcteZbpayloadr&r<ZdefectZin_fileZout_filerrr get_payloadsV"           zMessage.get_payloadcCspt|dr:|dkr||_dSt|ts.t|}||j}t|drT|dd|_n||_|dk rl||dS)Nr!rbrrZ)hasattrr:rCharsetr!output_charsetrb set_charset)r>rXr rrr set_payload/s    zMessage.set_payloadc Cs|dkr|dd|_dSt|ts.t|}||_d|krH|ddd|krf|jdd|dn|d|||kr||j|_d|kr| }z ||Wnjt k r|j}|rz| dd }Wn t k r| |j }YnX|||_|d|YnXdS) Nr MIME-Version1.0 Content-Typer6r zContent-Transfer-EncodingrrZ) del_paramr;rrn add_headerZget_output_charset set_paramZ body_encoder:Zget_body_encodingrWr!rero)r>r rkrXrrrrpCs:       zMessage.set_charsetcCs|jSr@)r;rBrrr get_charsetrszMessage.get_charsetcCs t|jSr@)rr8rBrrr__len__zszMessage.__len__cCs ||Sr@)rar>namerrr __getitem__~s zMessage.__getitem__cCsr|j|}|rX|}d}|jD]4\}}||kr"|d7}||kr"td||q"|j|j||dS)Nrrz/There may be at most {} {} headers in a message)r7Zheader_max_countr-r8 ValueErrorformatr.header_store_parse)r>r{valZ max_countZlnamefoundkvrrr __setitem__s  zMessage.__setitem__cCs@|}g}|jD]"\}}||kr|||fq||_dSr@)r-r8r.)r>r{ newheadersrrrrr __delitem__s  zMessage.__delitem__cCs|dd|jDkS)NcSsg|]\}}|qSr)r-.0rrrrr sz(Message.__contains__..)r-r8rzrrr __contains__szMessage.__contains__ccs|jD]\}}|VqdSr@r8)r>Zfieldr&rrr__iter__szMessage.__iter__cCsdd|jDS)NcSsg|] \}}|qSrrrrrrrsz Message.keys..rrBrrrkeyssz Message.keyscsfddjDS)Ncsg|]\}}j||qSrr7header_fetch_parserrBrrrsz"Message.values..rrBrrBrvaluess zMessage.valuescsfddjDS)Ncs"g|]\}}|j||fqSrrrrBrrrsz!Message.items..rrBrrBritemss z Message.itemscCs:|}|jD]&\}}||kr|j||Sq|Sr@)r-r8r7r)r>r{failobjrrrrrras  z Message.getcCs|j||fdSr@)r8r.)r>r{r&rrrset_rawszMessage.set_rawcCst|jSr@)iterr8copyrBrrr raw_itemsszMessage.raw_itemscCsHg}|}|jD](\}}||kr||j||q|sD|S|Sr@)r-r8r.r7r)r>r{rrrrrrrget_alls  zMessage.get_allcKspg}|D]<\}}|dkr0||ddq |t|dd|q |dk r^|d|t|||<dS)N_-r)rr.r[r'insert SEMISPACErg)r>_name_valueZ_paramspartsrrrrrrvs zMessage.add_headercCs\|}ttt|j|jD]0\}\}}||kr|j|||j|<qXqt|dSr@)r-ziprangerr8r7rKeyError)r>rrr2rrrrrreplace_header!s " zMessage.replace_headercCsHt}|d|}||kr"|St|d}|ddkrDdS|S)N content-typer/rr6)objectraget_default_typerr-r+)r>missingr&ctyperrrget_content_type4s  zMessage.get_content_typecCs|}|ddS)Nrrrsplitr>rrrrget_content_maintypeLszMessage.get_content_maintypecCs|}|ddS)Nrrrrrrrget_content_subtypeUszMessage.get_content_subtypecCs|jSr@r=rBrrrr^szMessage.get_default_typecCs ||_dSr@rrrrrset_default_typegszMessage.set_default_typec Cst}|||}||kr|Sg}t|D]X}z$|dd\}}|}|}Wn tk rr|}d}YnX|||fq*t|}|S)Nr)rr) rrar3rrr}r.rZ decode_params) r>rheaderrr&paramspr{rrrr_get_params_preserveps      zMessage._get_params_preserverTcCs8t}|||}||kr|S|r0dd|DS|SdS)NcSsg|]\}}|t|fqSr)r5rrrrrsz&Message.get_params..)rr)r>rrr4rrrrr get_paramss  zMessage.get_paramscCsN||kr |S|||D]0\}}||kr|r@t|S|Sq|Sr@)rr-r5)r>rrrr4rrrrrrcs  zMessage.get_paramrtrc Cs t|ts|r|||f}||kr2|dkr2d}n ||}|j||dst|s\t|||}qt|t|||g}nbd}|j||dD]N\} } d} | |krt|||} n t| | |} |s| }qt|| g}q|||kr|r| ||n||=|||<dS)Nrr6)rrrr4) rr r-rarcr'rrgrr) r>rr&rrequoter Zlanguager[rZ old_param old_valueZ append_paramrrrrws6   zMessage.set_paramcCs||kr dSd}|j||dD]@\}}||kr|sHt|||}qt|t|||g}q|||kr|||=|||<dS)Nrr)rr-r'rrgra)r>rrrZ new_ctyperrrrrrus zMessage.del_paramcCs|ddkst|dkr,|d=d|d<||kr@|||<dS|j||d}||=|||<|ddD]\}}|||||qhdS)Nrrrz mime-versionrsrrr)r+r}r-rrw)r>r`rrrrrrrrset_types zMessage.set_typecCsDt}|d|d}||kr*|d|d}||kr6|St|S)Nfilenamecontent-dispositionr{r)rrcrcollapse_rfc2231_valuer)r>rrrrrr get_filename&szMessage.get_filenamecCs,t}|d|}||kr|St|S)Nboundary)rrcrrrstrip)r>rrrrrr get_boundary6s  zMessage.get_boundarycCst}||d}||kr$tdg}d}|D]:\}}|dkr\|dd|fd}q0|||fq0|s|dd|fg}|jD]z\} } | dkrg} |D].\} } | dkr| | q| d| | fqt| } ||j | | q|| | fq||_dS) NrzNo Content-Type header foundFrz"%s"Trr) rrrZHeaderParseErrorr-r.r8rrgr7r)r>rrrZ newparamsZfoundpZpkZpvrhrrrrrrr set_boundaryCs2         zMessage.set_boundaryc Cst}|d|}||kr|St|trr|dp2d}z|dd}t||}Wn ttfk rp|d}YnXz|dWntk r|YSX|S)Nr rzus-asciirr\) rrcrr r!rrdrer-)r>rrr ZpcharsetrNrrrget_content_charsetqs     zMessage.get_content_charsetcsfdd|DS)Ncsg|]}|qSr)r)rpartrrrrsz(Message.get_charsets..walk)r>rrrr get_charsetsszMessage.get_charsetscCs*|d}|dkrdSt|d}|S)Nrr)rarr-)r>r&c_drrrget_content_dispositions  zMessage.get_content_dispositionr)FrN)FN)NF)N)N)N)NrT)NrT)rtTNrF)rT)rtT)N)N)N)N)3__name__ __module__ __qualname__r r?rCrArOrNrRrTrUrYrlrqrprxryr|rrrrrrrrarrrrvrrrrrrrrrcrwrurrrrrrrZemail.iteratorsrrrrrrish    Z /            " 3   .   cseZdZd2ddZd3fdd ZddZd d Zd d Zd4ddZddddhZ ddZ ddZ ddddZ ddddZ ddZd5dd Zd6d!d"Zd7d#d$Zdd%d&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1ZZS)8MIMEPartNcCs(|dkrddlm}|}t||dS)Nr)default)Z email.policyrrr?)r>r7rrrrr?s zMIMEPart.__init__Fcs0|dkr|jn|}|dkr |j}tj||dS)N)rFr7)r7Zmax_line_lengthsuperrA)r>rHrFr7 __class__rrrAs zMIMEPart.as_stringcCs|j|jjdddS)NT)utf8r7)rAr7ZclonerBrrrrCszMIMEPart.__str__cCs |d}|dkrdS|jdkS)NrF attachment)raZcontent_disposition)r>rrrr is_attachments zMIMEPart.is_attachmentc cs|r dS|d\}}|dkrB||kr>|||fVdS|dkrNdS|dkrz|D]}|||EdHq^dSd|kr|d|fVd}|d}|r|D]}|d|kr|}qq|dkr|}|r|dnd}|dk r|||EdHdS)Nrtext multipartrelatedstart content-idr)rrrr, iter_parts _find_bodyrcrl) r>rpreferencelistmaintypesubtypeZsubpart candidaterZsubpartsrrrrs6     zMIMEPart._find_bodyrhtmlplaincCsBt|}d}|||D]$\}}||kr|}|}|dkrq>q|S)Nr)rr)r>rZ best_prioZbodyZpriorrrrget_bodys zMIMEPart.get_body)rr)rr)rr)r alternativec cs$|d\}}|dks"|dkr&dS|}z |}Wntk rPYdSX|dkr|dkr|d}|rd}g}|D]"}|d|krd}q|||q||r|EdHdS|d |EdHdSg} |D]L}|d\}}||f|j kr| s|| kr| |q|VqdS) NrrrrrFrTr) rrrlrrVrcrar.pop _body_typesr) r>rrrXrrrZ attachmentsrseenrrriter_attachmentssD        zMIMEPart.iter_attachmentsccs|dkr|EdHdS)Nr)rrlrBrrrr=s zMIMEPart.iter_parts)content_managercOs"|dkr|jj}|j|f||Sr@)r7r get_contentr>rargskwrrrrEszMIMEPart.get_contentcOs&|dkr|jj}|j|f||dSr@)r7r set_contentrrrrrJszMIMEPart.set_contentc Cs|dkr6|}||f}||kr6td||g}g}|jD]4\}}|drj|||fqD|||fqD|rt||j d} || _|j | _ | g|_ ng|_ ||_d||d<|dk r| d|dS)NrzCannot convert {} to {}content-rz multipart/rtr) rrr}r~r8r- startswithr.r`r7r:rw) r>rZdisallowed_subtypesrZexisting_subtypeZ keep_headersZ part_headersr{r&rrrr_make_multipartOs0    zMIMEPart._make_multipartcCs|dd|dS)Nr)rmixedrr>rrrr make_relatedjszMIMEPart.make_relatedcCs|dd|dS)Nr)rrrrrrmake_alternativemszMIMEPart.make_alternativecCs|dd|dS)Nrrrrrrr make_mixedpszMIMEPart.make_mixed)_dispcOsf|dks||kr(t|d|t||jd}|j|||rXd|krX||d<||dS)NrZmake_rrzContent-Disposition)rrgetattrr`r7rrY)r>Z_subtyperrrrrrr_add_multipartss    zMIMEPart._add_multipartcOs|jd|ddi|dS)NrrZinline)rrr>rrrrr add_related}szMIMEPart.add_relatedcOs|jd||dS)Nr)rrrrrradd_alternativeszMIMEPart.add_alternativecOs|jd|ddi|dS)Nrrr)rrrrrradd_attachmentszMIMEPart.add_attachmentcCsg|_d|_dSr@r8r:rBrrrclearszMIMEPart.clearcCsdd|jD|_d|_dS)NcSs&g|]\}}|ds||fqS)r)r-r)rnrrrrrsz*MIMEPart.clear_content..rrBrrr clear_contentszMIMEPart.clear_content)N)FNN)r)N)N)N)rrrr?rArCrrrrrrrrrrrrrrrrrr __classcell__rrrrrs2  7    rcseZdZfddZZS)rcs"tj||d|krd|d<dS)Nrrrs)rrrrrrrszEmailMessage.set_content)rrrrrrrrrrs)NT)__all__rerirfiorrZemailrrZemail._policybaserr r r;Zemail._encoded_wordsr rnrcompiler#rr'r3r5rrrrrrrs4      " N`