
    6hB                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKJrJrJrJrJr  SSKJr  SSKJrJrJrJrJr  SSKJrJrJr   " S S	\
R>                  5      r  " S
 S\
R>                  5      r!S!S jr"S r#S r$S r%S r&S"S jr'S"S jr(S r)S#S jr*  S$       S%S jjr+        S&S jr,S'S jr-S(S jr.  S)         S*S jjr/ S"         S+S jjr0  S)         S,S jjr1  S)         S-S jjr2  S)         S.S jjr3\Rh                  Rj                  r6S/S jr7S0S jr8S1S  jr9g)2    )annotationsN)AnyListOptionalTupleUnion   )_)encodingerrorpycompatsslutilutil)procutil
stringutilurlutilc                  ,    \ rS rSrSrSS jrSS jrSrg)	STARTTLS-   zDerived class to verify the peer certificate for STARTTLS.

This class allows to pass any keyword arguments to SSL socket creation.
Nc                ^    [         R                  R                  " U 40 UD6  Xl        X l        g N)smtplibSMTP__init___ui_host)selfuihostkwargss       0/usr/lib/python3/dist-packages/mercurial/mail.pyr   STARTTLS.__init__3   s#    d-f-
    c                   U R                  S5      (       d  Sn[        R                  " U5      eU R                  S5      u  pVUS:X  aw  [        R
                  " U R                  UUU R                  U R                  S9U l        U R                  R                  S5      U l
        S U l        S U l        0 U l        SU l        XV4$ )Nstarttlsz*STARTTLS extension not supported by serverr      r   serverhostnamerbr   )has_extnr   SMTPExceptiondocmdr   
wrapsocketsockr   r   makefilefile	helo_resp	ehlo_respesmtp_features
does_esmtp)r   keyfilecertfilecontextmsgrespreplys          r!   r%   STARTTLS.starttls8   s    }}Z((>C'',,

:.3;**		88#zzDI 		**40DI!DN!DN"$DDO}r#   )r   r   r4   r2   r3   r0   r1   r.   r   NNN)__name__
__module____qualname____firstlineno____doc__r   r%   __static_attributes__ r#   r!   r   r   -   s    

r#   r   c                  (    \ rS rSrSrSS jrS rSrg)SMTPSM   zDerived class to verify the peer certificate for SMTPS.

This class allows to pass any keyword arguments to SSL socket creation.
Nc                    X l         X0l        [        R                  R                  " U 40 UD6  X@l        [        R                  U l        Xl        g r   )	r5   r6   r   r   r   r   SMTP_SSL_PORTdefault_portr   )r   r   r5   r6   r   r    s         r!   r   SMTPS.__init__S   s;     d-f-
#11r#   c                B   U R                   S:  a   U R                  R                  SX44-  5        [        R                  " X4U5      n[
        R                  " UU R                  U R                  U R                  U R                  S9nUR                  S5      U l        U$ )Nr   s   connect: %r
r'   r)   )
debuglevelr   debugsocketcreate_connectionr   r-   r5   r6   r   r/   r0   )r   r   porttimeout
new_sockets        r!   _get_socketSMTPS._get_socket[   s    ??QHHNN+|o=>--tlGD
''LLMMxx::

 ''-	r#   )r   r   r6   rI   r0   r5   r<   )r=   r>   r?   r@   rA   r   rS   rB   rC   r#   r!   rE   rE   M   s    
r#   rE   c                 H     SSK n [        U SS5        g! [         a     gf = f)z9Returns true iff Python has TLS support, false otherwise.r   NHAS_TLSFT)sslgetattrImportError)rW   s    r!   	_pyhastlsrZ   j   s+    Y& s    
!!c                  ^
 U R                  SS5      nU R                  SS5      nUS:H  =(       d    [        R                  " U5      nUS:H  nU(       d  U(       a.  [        5       (       d  [        R
                  " [        S5      5      eU R                  SS5      nU(       d  [        R
                  " [        S5      5      eU(       a%  U R                  [        S	5      5        [        XUS
9m
O&U(       a  [        XUS
9m
O[        R                  " US9m
U(       a  SnOSn[        R                  " U R                  SSU5      5      nU R                  [        S5      XW4-  5        T
R                  XWS9  U(       aJ  U R                  [        S5      5        T
R                  5         T
R!                  5         T
R                  5         U(       d  U(       a:  U R                  [        S5      5        ["        R$                  " T
R&                  5         [)        U T
XW5        U
4S jn	U	$ ! [        R*                   a/  n[        R
                  " [        R,                  " U5      5      eSnAff = f)z;build an smtp connection and return a function to send mail   smtps   local_hostnames   tlss   starttlss   smtpss/   can't use TLS: Python SSL support not installed   hosts+   smtp.host not configured - cannot send mails   (using smtps)
)local_hostnamer   )r^   i     s   ports$   sending mail: smtp host %s, port %d
)r   rP   s   (using starttls)
s   (verifying remote certificate)
Nc                  >  TR                  XU5      $ ! [        R                   a_  nUR                  R	                  5        Vs/ s H  oDS   PM	     Os  snf nn[
        R                  " SSR                  U5      -   5      eS nAf[        R                   a/  n[
        R                  " [        R                  " U5      5      eS nAff = f)Nr	      
)sendmailr   SMTPRecipientsRefused
recipientsvaluesr   Abortjoinr+   r   forcebytestr)senderrd   r8   instrss        r!   send_smtp.<locals>.send   s    	=::f#66,, 	>(,(>(>(@A(@1A$(@AJA++eejj&<<==$$ 	=++j55d;<<	=s,    C	BA0BC	*CC	)configr   	parseboolrZ   r   rf   r
   noterE   r   r   r   r   getportconnectehlor%   r   validatesocketr.   _smtp_loginr+   rh   )r   r^   tlsr%   smtpsmailhostdefaultportmailportrj   rm   rl   s             @r!   _smtpr|   u   s   YYw(9:N
))GV
$Ck!>Z%9%9#%>H8OEE9;;kk!NOPPyy'*Hkk!JKLL
$%&"(C	RXFLL7ryy';GHHGGA678:NNOII8I+
'()		

	5
567qvv&9B8.= K    9kk*11$7889s   H" "I%6*I  I%c                   U R                  SS5      nU R                  SS5      nU(       aG  U(       a  [        R                  " U5      nO)U R                  5       nUb  [        R                  " U5      nU(       aM  U(       aE  U R	                  [        S5      U-  5        [        R                  " U5      nUR                  XE5        ggg)af  A 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.
r\   s   usernames   passwordNs&   (authenticating to mail server as %s)
)ro   r   strfromlocalgetpassrq   r
   login)r   smtpry   r{   usernamepasswords         r!   rv   rv      s     yy+.Hyy+.H,,X6Hzz|H##00:H
<=HI((2

8& xr#   c           	         U R                  SS5      nS nSUU" U5      SR                  [        XR5      5      4-  nU R                  [	        S5      U-  5        [
        R                  " US5      nUR                  [        R                  " U5      5        UR                  5       nU(       ac  [        R                  " S[        R                  R                  [
        R                   " U5      S	   5      [
        R"                  " U5      4-  5      eg
)zsend mail using sendmail.   email   methodc                ~    [         R                  " [        R                  " [        R
                  " U 5      5      5      $ r   )r   
shellquoter   emailr   
strtolocal)xs    r!   stremail_sendmail.<locals>.stremail   s)    "":#3#3H4G4G4J#KLLr#   s   %s -f %s %s    s   sending mail: %s
s   wbs   %s %sr   N)ro   rg   maprq   r
   r   popenwriter   tonativeeolcloser   rf   ospathbasename
shellsplitexplainexit)	r   ri   rd   r8   programr   cmdlinefprets	            r!   	_sendmailr      s    ii),GM 		#h+,  G
 GGA#$w./		'BHHTc"#
((*C
kk  !4!4W!=a!@A$$S)
 	
 r#   c                d   [        U S5       n[        R                  " S[        R                  " 5       5      nUR	                  S[
        R                  " U5      [
        R                  " U5      4-  5        UR	                  U5        UR	                  S5        SSS5        g! , (       d  f       g= f)zwrite mails to mboxzab+z%a %b %d %H:%M:%S %Ys   From %s %s
s   

N)opentimestrftime	localtimer   r   r   )mboxri   rd   r8   r   dates         r!   _mboxr      s     
dE	b }}3T^^5EF
""6*H,?,?,EFG	
 	
 
		s   BB!!
B/c                   ^ ^ T(       a   [        TS5      R                  5         U4S j$ T R                  SS5      S:X  a  [        T 5      $ U 4S j$ )zjmake a mail connection. return a function to send mail.
call as sendmail(sender, list-of-recipients, msg).wbc                   > [        TXU5      $ r   )r   )rl   rk   mr   s      r!   <lambda>connect.<locals>.<lambda>   s    uT13r#   r   r   r\   c                   > [        TXU5      $ r   )r   )rl   rk   r   r   s      r!   r   r      s    9Rq1r#   )r   r   ro   r|   )r   r   s   ``r!   rs   rs      sD     T4 33	yy9%0Ry11r#   c                &    [        XS9nU" XU5      $ )N)r   )rs   )r   ri   rd   r8   r   rm   s         r!   rb   rb      s    2!DC((r#   c                r   U R                  SS5      nUS:X  a7  U R                  SS5      (       d  [        R                  " [        S5      5      eg	[        R
                  " U5      nU(       a  US   OSnU(       a  [        R                  " U5      (       d"  [        R                  " [        S5      U-  5      eg	)
z=determine if we have enough config data to try sending email.r   r   r\   r]   s>   smtp specified as email transport, but no smtp host configuredr   r#   s0   %r specified as email transport, but not in PATHN)ro   r   rf   r
   r   r   findexe)r   methodcommands      r!   validateconfigr      s    YYx+Fyy'**++3  + %%f- ''!*SH,,W55++EFP  6r#   c                    [         R                  R                  U 5      R                  R	                  5       n U R                  S5      (       a  U R                  S5      (       d  SU SS -   $ U $ ) isoziso-   N)r   charsetCharsetinput_charsetlower
startswith)css    r!   
codec2ianar     sY    			r	"	0	0	6	6	8B 
}}UBMM&$9$912Ir#   c                D   SS[         R                  " [        R                  5      [         R                  " [        R                  5      /nU(       a  S/nU H*  n U R	                  U5        [        X[        U5      5      s  $    [        XS5      $ ! [         a     MG  f = f)a"  Return 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-asciiutf-8z
iso-8859-1)r   sysstrr   fallbackencodingdecode
mimetextqpr   UnicodeDecodeError)rl   subtypedisplayr   r   s        r!   mimetextpatchr     s     	))*112	
B \	HHWa*W*=>>  a,// " 		s   %B
BBc                X   [         R                  R                  U5      n[         R                  R	                  5       nUR                  SU-   5        U R                  5        H3  n[        U5      S:  d  M  [         R                  R                  Ul	          O   UR                  X5        U$ )zSReturn MIME message.
Quoted-printable transfer encoding will be used if necessary.
ztext/i  )r   r   r   messageMessageset_type
splitlineslenQPbody_encodingset_payload)bodyr   r   r   r8   lines         r!   r   r   4  s     
		w	'B
--


!CLL7"#!t9s?$}}//B " OODJr#   c                   U R                  SS5       Vs/ s H'  n[        R                  " UR                  5       5      PM)     nn[        R                  " [        R
                  R                  5       5      [        R                  " [        R                  R                  5       5      S/nU H  nX;  d  M
  UR                  U5        M     U Vs/ s H  oR                  S5      (       a  M  UPM     sn$ s  snf s  snf )z;Obtains charsets to send mail parts not containing patches.r   s   charsetsr   ascii)
configlistr   r   r   r   r   appendendswith)r   r   charsets	fallbackss       r!   	_charsetsr   O  s     --+66B 	
#6  
 	11779:))//12I
 OOB  ">2W)=B>> ?s   .C5C:,C:c           
     D   U=(       d    [        U 5      n[        U[        5      (       d1  U H  n UR                  U5      U4s  $    UR                  SS5      S4$  UR                  S5        US4$ ! [         a     MR  [
         a5    U R                  [        S5      [        R                  " U5      -  5         M  f = f! [         a    [        R                  [        R                  4 H  n[        R                  " U5      n UR                  U5      nO! [         a     M9  f = fU Hi  n UR                  U5      U4s  s  s $ ! [         a     M*  [
         a5    U R                  [        S5      [        R                  " U5      -  5         Mg  f = f   M      US4$ f = f)a  Returns (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
r   backslashreplacer   )r   
isinstancebytesencodeUnicodeEncodeErrorLookupErrorwarnr
   r   sysbytesr   r   r   r   r   )r   rl   r   sendcharsetsocsicsus          r!   _encoder   `  s    ,y}La  Cxx}c))   88G%78*DD	& j=C &  ;<'',-  %%x'@'@AC//#&CHHSM% #88C=#--) " GG?@"++C01 $ B" j=%s~   A1B? 1
B<>:B<;B<?AFDF
D#F"D##F,E>F
F	F:F	FF		FFc                    U(       d;  [        XU5      u  p[        R                  R                  X5      R	                  5       $ [
        R                  " U5      $ )z4Returns RFC-2047 compliant header from given string.)r   r   headerHeaderr   r   r~   r   rl   r   r   r   s        r!   
headencoder     sD     x(||""1)0022  ##r#   c                   [         R                  " U5      n[        XU5      n UR                  S5      u  pEUR	                  S5        UR	                  [
        R                  " [         R                   5      5      R                  S5      nSXE4-  n[        R                  R                  U[         R                   " U5      45      $ ! [         a#    [        R                  " [        S5      U-  5      e[         aE     UR	                  S5         N~! [         a#    [        R                  " [        S5      U-  5      ef = ff = f)N   @r   idnas   %s@%ss   invalid email address: %ss   invalid local address: %s)r   r   r   splitr   r   r   r   r   r   rf   r
   
ValueErrorr   utils
formataddrr~   )r   nameaddrr   accdoms         r!   _addressencoder     s    t$Db)DF::d#

7jj):):;<CCFK3*$ ;;!!4)>)>t)D"EFF  Bkk!89D@AA F	FKK ! 	F++a <=DEE	F	Fs   A,C 5D?;D-D;;D?c                    U(       d  U(       d  [         R                  " U=(       d    S5      $ [        R                  R	                  [         R                  " U5      5      u  pE[        XXR5      $ )z-Turns address into RFC-2047 compliant header.r#   )r   r~   r   r   	parseaddrr   )r   addressr   r   r   r   s         r!   addressencoder     sL     g$$W^44&&x'<'<W'EFJD"D33r#   c                   / nU HG  n[        U[        5      (       d
   SU-  5       eUR                  [        R                  " U5      5        MI     U(       a7  U Vs/ s H)  oUR                  5       (       d  M  UR                  5       PM+     sn$ / n[        R                  R                  U5       H2  u  pxU(       d	  U(       d  M  [        XX5      n	UR                  U	5        M4     U$ s  snf )zTurns a list of addresses into a list of RFC-2047 compliant headers.
A single element of input list may contain multiple addresses, but output
always has one address per itemz%r unexpectedly not a bytestr)
r   r   r   r   r~   stripr   r   getaddressesr   )
r   addrsr   r   straddrsaresultr   r   rk   s
             r!   addrlistencoder    s     H!U##H%Dq%HH#--a01  #+98awwy		899Fkk..x8
44r8AMM! 9 M :s   C'7C'c                J    SnU(       d  [        XU5      u  p[        USU5      $ )zccreates mime text object, encodes it if needed, and sets
charset and transfer-encoding accordingly.r   plain)r   r   r   s        r!   
mimeencoder	    s)     
Bx(a"%%r#   c                    [         R                  R                  5       n[        R                  " U SS[        S5      S9n  UR                  U 5      U R                  5         $ ! U R                  5         f = f)Nr   surrogateescape
   )r   errorsnewline)r   parserParserioTextIOWrapperchrparsedetach)r   eps     r!   r  r    sW    				B 
		
W%6B
Bxx|
				s    A! !A3c                `    [         R                  R                  5       nUR                  U 5      $ r   )r   r  BytesParser
parsebytes)datar  s     r!   r  r    s#    		!	!	#B==r#   c                   / n[         R                  R                  U 5       Ht  u  p#Ub#   UR                  UR	                  U5      5        M+  [        U[        5      (       a  UR                  U5        MS   UR                  UR	                  S5      5        Mv     [        R                  " SR                  U5      5      $ ! [
        [        4 a     Nf = f! [
         a     Of = fUR                  UR	                  S5      5        M  )zDecodes RFC-2047 headerzUTF-8z
ISO-8859-1 )r   r   decode_headerr   r   r   r   r   strr   
unitolocalrg   )rl   upartspartr   s       r!   
headdecoder"    s    F33A6dkk'23
 dC  MM$	MM$++g./ 7$ sxx/00 '4  " 		dkk,/0s#    B?6 C?CC
C"!C")returnboolr   )r   r  r#  r  )r  F)rl   r   r   r  r   r$  r#  email.message.Message)r   r   r   r  r   r  r#  r%  )r   r   r#  	List[str])r   r   rl   r   r   r&  r#  zTuple[bytes, str])NF)
r   r   rl   zUnion[bytes, str]r   Optional[List[str]]r   r$  r#  r  )
r   r   r   r  r   r  r   r'  r#  r  )
r   r   r   r   r   r'  r   r$  r#  r  )
r   r   r  zList[bytes]r   r'  r   r$  r#  r&  )
r   r   rl   r   r   r'  r   r$  r#  r%  )r   r   r#  r%  )r  r   r#  r%  )rl   z!Union[email.header.Header, bytes]r#  r   ):
__future__r   r   email.charsetemail.generatoremail.headeremail.messageemail.parserr  r   r   rN   r   typingr   r   r   r   r   i18nr
   r   r   r   r   r   r   r   r   r   r   r   r   rE   rZ   r|   rv   r   r   rs   rb   r   r   r   r   r   r   r   r   r   r  r	  	generatorBytesGenerator	Generatorr  r  r"  rC   r#   r!   <module>r3     sD   #       	 	       w|| @GLL :1h'.
42)
( 000 0 	0:
(+6?"/j %)	$$$ "$ 	$
 	$ DHGGG!G-@GG0 %)	
4
4
4 "
4 	
4
 	
4  %)	 " 	
 6 %)	&&& "& 	&
 & OO**	
1r#   