^cCddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZddlmZmZm Z mZmZddlmZmZmZe jrddlmZmZmZmZeeeeefsJGdd ej Z!Gd d ej Z"d Z#d Z$dZ%dZ&dZ'd#dZ(d#dZ)dZ*dZ+d$dZ,dZ-dZ.dZ/d%dZ0d#dZ1d%dZ2d%dZ3d%dZ4ej5j6Z7d Z8d!Z9d"Z:dS)&N)_)getattropen)encodingerrorpycompatsslutilutil)procutil stringutilurlutil)AnyListTupleUnionc"eZdZdZddZddZdS)STARTTLSzDerived class to verify the peer certificate for STARTTLS. This class allows to pass any keyword arguments to SSL socket creation. Nc Ptjj|fi|||_||_dSN)smtplibSMTP__init___ui_host)selfuihostkwargss 0/usr/lib/python3/dist-packages/mercurial/mail.pyrzSTARTTLS.__init__4s/ d--f--- cl|dsd}tj||d\}}|dkrht j|j|||j|j|_|j d|_ d|_ d|_ i|_ d|_||fS)Nstarttlss*STARTTLS extension not supported by serverrrserverhostnamerbr)has_extnr SMTPExceptiondocmdr wrapsocketsockrrmakefilefile helo_resp ehlo_respesmtp_features does_esmtp)rkeyfilecertfilemsgrespreplys r r#zSTARTTLS.starttls9s}}Z(( -?C',, , :.. u 3;;* 8#z DI **400DI!DN!DN"$D DOe}r!r)NN)__name__ __module__ __qualname____doc__rr#r!r rr.sF  r!rc eZdZdZddZdZdS)SMTPSzDerived class to verify the peer certificate for SMTPS. This class allows to pass any keyword arguments to SSL socket creation. Nc ||_||_tjj|fi|||_tj|_||_dSr) r3r4rrrr SMTP_SSL_PORT default_portr)rrr3r4rrs r rzSMTPS.__init__TsH    d--f--- #1r!c|jdkr |jd||ffztj||f|}t j||j|j|j|j }| d|_ |S)Nrs connect: %r r%r') debuglevelrdebugsocketcreate_connectionr r+r3r4rr-r.)rrporttimeout new_sockets r _get_socketzSMTPS._get_socket\s ?Q   HNN+d|o= > > >-tTlGDD '  L Mx:    ''-- r!)NNN)r8r9r:r;rrJr<r!r r>r>NsA      r!r>cT ddl}t|dddS#t$rYdSwxYw)z9Returns true iff Python has TLS support, false otherwise.rNHAS_TLSFT)sslr ImportError)rMs r _pyhastlsrOksI Y&&&t uus  ''c |dd}|dd}|dkptj|}|dk}|s|r/ts!t jt d|dd}|s!t jt d|r5|t d t|||  n*|rt|||  ntj |  |rd }nd }tj |dd|}|t d||fz |||r^|t d   |s|r;|t dt#j j t)| ||n=#tj$r+}t jtj|d}~wwxYw fd} | S)z;build an smtp connection and return a function to send mailsmtpslocal_hostnamestlssstarttlsssmtpss/can't use TLS: Python SSL support not installedhosts+smtp.host not configured - cannot send mails(using smtps) )local_hostnamer)rSisports$sending mail: smtp host %s, port %d )rrGs(using starttls) s(verifying remote certificate) Ncl |||S#tj$rR}d|jD}t jdd|zd}~wtj$r+}t jtj |d}~wwxYw)Ncg|] }|d S)rr<).0rs r z'_smtp..send..sAAA1!A$AAAr! ) sendmailrSMTPRecipientsRefused recipientsvaluesrAbortjoinr)r forcebytestr)senderr]r5instss r sendz_smtp..sends =::fj#66 6, > > >AA(>(>(@(@AAAJ+eejj&<&<<== =$ = = =+j5d;;<< < =s!B3A A66B3&B..B3)configr parseboolrOrr_rnoter>rrrrgetportconnectehlor#r validatesocketr, _smtp_loginr)ra) rrStlsr#smtpsmailhost defaultportmailportrcrerds @r _smtprswsYYw(9::N ))GV $ $Ck!>Z%9#%>%>H 8OEQEQ9;;Qk!NOOPPPyy'**H Mk!JKKLLL 8 $%%&&& "^( C C C 8 RX F F F L 7 7 7   ryy';GGHHHGGA6 7 78X:N NOOOII8(I+++ '(()))   '5' 566777qv&&&9B8X....  999k*1$778889===== Ks)H<<I6 &I11I6c|dd}|dd}|rA|rtj|}n*|}|tj|}|rS|rS|t d|ztj|}|||dSdSdS)azA hook for the keyring extension to perform the actual SMTP login. An already connected SMTP object of the proper type is provided, based on the current configuration. The host and port to which the connection was established are provided for accessibility, since the SMTP object doesn't provide an accessor. ``smtplib.SMTPException`` is raised on error. rQsusernamespasswordNs&(authenticating to mail server as %s) )rfr strfromlocalgetpassrhrlogin)rsmtprprrusernamepasswords r rmrmsyy+..Hyy+..H;  ;,X66HHzz||H##0::'H' <==HIII(22 8X&&&&&''''r!c J|dd}d}d|||dt||fz}|t d|zt j|d}|tj || }|r`tj dtjt j|d t j|fzd S) zsend mail using sendmail.emailmethodcrtjtjt j|Sr)r shellquoter emailr strtolocal)xs r stremailz_sendmail..stremails)":#3H4G4J4J#K#KLLLr!s %s -f %s %s ssending mail: %s wbs%s %srN)rfr`maprhrr popenwriter tonativeeolcloserr_ospathbasename shellsplit explainexit) rrbr]r5programrcmdlinefprets r _sendmailrs"ii),,GMMM #h ++,, G GGA# $ $w .///  ' 'BHHT c " "### ((**C  k   !4W!=!=a!@AA$S))      r!ctt|d5}tjdtj}|dt j|t j|fz|||dddddS#1swxYwYdS)zwrite mails to mboxsab+z%a %b %d %H:%M:%S %Ys From %s %s s N)rtimestrftime localtimerrr)mboxrbr]r5rdates r _mboxrs dF   r}3T^5E5EFF  "6**H,?,E,EF G                        sBB--B14B1cr'tdfdSdddkrtSfdS)znmake a mail connection. return a function to send mail. call as sendmail(sender, list-of-recipients, msg).rc(t|||Sr)r)rdrXmrs r zconnect..suT1a33r!r|r}rQc(t|||Sr)r)rdrXrrs r rzconnect..s9RAq11r!)rrrfrs)rrs``r rjrjsm 4 T5!!!33333 yy9%%00Ryy 1 1 1 11r!c>t||}||||S)N)r)rj)rrbr]r5rres r r[r[s( 2D ! ! !D 4 C ( ((r!cd|dd}|dkr9|dds!tjtdd St j|}|r|dnd}|rt j|s$tjtd|zd S) z=determine if we have enough config data to try sending email.r|r}rQrRs>smtp specified as email transport, but no smtp host configuredrr!s0%r specified as email transport, but not in PATHN)rfrr_rr rfindexe)rmethodcommands r validateconfigrs YYx + +F yy'** +3   %f-- '0'!**S H,W55 +EFFP   r!ctj|j}|dr"|ds d|ddzS|S) isoziso-N)rcharsetCharset input_charsetlower startswith)css r codec2ianarsh   r " " 0 6 6 8 8B }}UBMM&$9$9122 Ir!plainFc>ddtjtjtjtjg}|rdg}|D]G} ||t ||t |cS#t$rYDwxYwt ||dS)a2Return MIME message suitable for a patch. Charset will be detected by first trying to decode as us-ascii, then utf-8, and finally the global encodings. If all those fail, fall back to ISO-8859-1, an encoding with that allows all byte sequences. Transfer encodings will be used if necessary.us-asciiutf-8z iso-8859-1)r sysstrrfallbackencodingdecode mimetextqprUnicodeDecodeError)rdsubtypedisplayrrs r mimetextpatchrs )**122  B \  HHW   a*W*=*=>> > > >!    D  a, / //s 2A>> B  B c`tj|}tj}|d|z|D]-}t|dkrtjj|_ n.| |||S)z[Return MIME message. Quoted-printable transfer encoding will be used if necessary. ztext/i) rrrmessageMessageset_type splitlineslenQP body_encoding set_payload)bodyrrrr5lines r rr4s   w ' 'B -   ! !CLL7"###!! t99s??$}/B  E OOD" Jr!cXd|ddD}tjtjtjtjdg}|D]}||vr||d|DS)z;Obtains charsets to send mail parts not containing patches.cZg|](}tj|)Sr<)r rrrWrs r rYz_charsets..Qs:   ##r!r|scharsetsrc<g|]}|d|S)ascii)endswithrs r rYz_charsets..]s) > > >2W)=)= >B > > >r!) configlistr rrrrappend)rcharsets fallbacksrs r _charsetsrNs--+66H 17799::)//1122I    X   OOB    > > > > >>r!c H|pt|}t|ts|D]m} |||fcS#t$rY(t $r:|tdtj |zYjwxYw|dddfS | dn#t$rtj tj fD]}tj|} | |}n#t$rY9wxYw|D]q} |||fcccYS#t$rY,t $r:|tdtj |zYnwxYwYnwxYw|dfS)aReturns (converted) string, charset tuple. Finds out best charset by cycling through sendcharsets in descending order. Tries both encoding and fallbackencoding for input. Only as last resort send as is in fake ascii. Caveat: Do not use for mail parts containing patches!s!ignoring invalid sendcharset: %s rbackslashreplacer)r isinstancebytesencodeUnicodeEncodeError LookupErrorwarnrr sysbytesrrrrr)rrdr sendcharsetsocsicsus r _encoder`sB,y}}L a  E E EC xx}}c))))%      ;<<',,- 88G%788*D D  %x'@A  C/#&&C HHSMM%    #  88C==#--------)D"GG?@@"+C001    $ j=sA BABB0C8F?DF D"F!D""F*EF F FAF FF FFc|sFt|||\}}tj||St j|S)z4Returns RFC-2047 compliant header from given string.)rrheaderHeaderrrrurrdrrrs r headencodersV 3Ax((2|""1b))00222   # ##r!ctj|}t|||} |d\}}|d|t jtjd}d||fz}n#t$r%tj td|zt$rL |dn2#t$r%tj td|zwxYwYnwxYwtj|tj|fS)N@ridnas%s@%ssinvalid email address: %ssinvalid local address: %s)rrrsplitrr rrrrr_r ValueErrorrutils formataddrru)rnameaddrraccdoms r _addressencodersf  t $ $D b$ ) )D F::d##S 7jj):;;<<CCFKK3*$ BBBk!899D@AAA FFF F KK ! F F F+a <==DEE E F ! F ; ! !4)>t)D)D"E F FFs*A8B 7D#C.-D#./DD#"D#c|s|stj|pdStjtj|\}}t ||||S)z-Turns address into RFC-2047 compliant header.r!)rrurr parseaddrr)raddressrrrrs r addressencoders`5g5$W^444&&x'.s-999aqwwyy9 999r!) rrrrrurr getaddressesr) raddrsrrstraddrsrresultrrrXs r addrlistencoders H 22!U##HH%Dq%HHHH-a001111:9989999 Fk..x88 d  4 r4x88A MM!    Mr!cTd}|st|||\}}t|d|S)zgcreates mime text object, encodes it if needed, and sets charset and transfer-encoding accordingly.rr)rrrs r mimeencoders8 B )Ax((2 a" % %%r!ctj}tj|ddt d} |||S#|wxYw)Nrsurrogateescape )rerrorsnewline)rparserParserio TextIOWrapperchrparsedetach)reps r r r sq     B  W%6B   Bxx||  s A..Bchtj}||Sr)rr BytesParser parsebytes)datars r rrs'  ! ! # #B ==  r!cNg}tj|D]\}}|A |||1#t t f$rYnwxYwt|tdr||{ ||d#t $rYnwxYw||dtj d |S)zDecodes RFC-2047 headerNzUTF-8z ISO-8859-1r) rr decode_headerrrrrrtyper unitolocalr`)rdupartspartrs r headdecoders0F33A6611 g    dkk'22333& 4     dDII & &  MM$      MM$++g.. / / / !    D  dkk,//0000  tyy00 1 11s#(AA'&A'(C CCr)rF)NF);r email.charsetemail.generator email.header email.message email.parserr rrrEri18nrr rrrrrr r rr r r TYPE_CHECKINGtypingrrrrallrrr>rOrsrmrrrjr[rrrrrrrrrrr generatorBytesGenerator Generatorr rrr<r!r r'sU     *............ 3T5%( ) ))))w|@GL:   111h'''.   4   2222)))) (000044???$000f$$$$GGGG(4444(&&&& O *     22222r!