
    Zj9                     .   d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ dZd	Zed
z  Z ej        d          Z ej        d          Z ej        d          Z G d dej                  Z G d deej                  ZdS )z?
An ``asyncio.Protocol`` subclass for lower level IO handling.
    N)Callablecast   )SMTPDataErrorSMTPReadTimeoutErrorSMTPResponseExceptionSMTPServerDisconnectedSMTPTimeoutError)SMTPResponse)
SMTPStatus)SMTPProtocoli       s   (?:\r\n|\n|\r(?!\n))s   (?m)^\.s   [\x00-\x1f\x7f]c                   |    e Zd ZdZddej        dz  ddfdZddZddZde	dz  ddfd	Z
dd
Zdej        ddfdZdS )FlowControlMixina  
    Reusable flow control logic for StreamWriter.drain().
    This implements the protocol methods pause_writing(),
    resume_writing() and connection_lost().  If the subclass overrides
    these it must call the super methods.
    StreamWriter.drain() must wait for _drain_helper() coroutine.

    Copied from stdlib as per recommendation: https://bugs.python.org/msg343685.
    Logging and asserts removed, type annotations added.
    Nloopreturnc                     |t          j                    | _        n|| _        d| _        t	          j                    | _        d| _        d S NF)asyncioget_event_loop_loop_pausedcollectionsdeque_drain_waiters_connection_lost)selfr   s     W/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/aiosmtplib/protocol.py__init__zFlowControlMixin.__init__0   sK    < /11DJJDJ 	 !&    c                     d| _         d S NT)r   r   s    r   pause_writingzFlowControlMixin.pause_writing<   s    r    c                 z    d| _         | j        D ]+}|                                s|                    d            ,d S r   )r   r   done
set_resultr   waiters     r   resume_writingzFlowControlMixin.resume_writing?   sJ    ) 	( 	(F;;== (!!$'''	( 	(r    excc                     d| _         | j        sd S | j        D ]C}|                                s-||                    d            .|                    |           Dd S r"   )r   r   r   r&   r'   set_exception)r   r+   r)   s      r   connection_lostz FlowControlMixin.connection_lostF   sv     $| 	F) 	. 	.F;;== .;%%d++++((---	. 	.r    c                 2  K   | j         rt          d          | j        sd S | j                                        }| j                            |           	 | d {V  | j                            |           d S # | j                            |           w xY w)NConnection lost)r   ConnectionResetErrorr   r   create_futurer   appendremover(   s     r   _drain_helperzFlowControlMixin._drain_helperS   s        	:&'8999| 	F))++""6***	/LLLLLLL&&v.....D&&v....s   A: :Bstreamasyncio.Future[None]c                     t           N)NotImplementedErrorr   r6   s     r   _get_close_waiterz"FlowControlMixin._get_close_waiter_   s    !!r    r9   r   N)__name__
__module____qualname____doc__r   AbstractEventLoopr   r$   r*   	Exceptionr.   r5   StreamWriterr<    r    r   r   r   $   s        	 	
& 
&W6= 
& 
& 
& 
& 
&   ( ( ( (.9t#3 . . . . .
/ 
/ 
/ 
/"(< "AW " " " " " "r    r   c            	           e Zd Z	 	 ddej        dz  deg df         dz  ddf fdZdej        ddfdZd d	Z	e
defd
            Zdej        ddfdZdedz  ddf fdZdeddfdZdefdZdedz  fdZdedz  fdZd!dedz  defdZdeddfdZdddededz  defdZ	 d!dededz  defdZ	 	 ddej        dedz  dedz  defdZ  xZ!S )"r   Nr   connection_lost_callbackr   c                    t                                          |           d| _        t                      | _        d | _        d | _        d | _        | j        	                                | _
        d| _        || _        d S )N)r   F)superr   	_over_ssl	bytearray_buffer_response_waiter	transport_command_lockr   r2   _closed_future
_quit_sent_connection_lost_callback)r   r   rG   	__class__s      r   r   zSMTPProtocol.__init__d   st    
 	d### {{EI7;266:j6N6N6P6P)A&&&r    r6   r7   c                     | j         S r9   )rP   r;   s     r   r<   zSMTPProtocol._get_close_waitert   s    ""r    c                 .    |                                   d S r9   )_retrieve_response_exceptionr#   s    r   __del__zSMTPProtocol.__del__w   s     	))+++++r    c                 b    t          | j        duo| j                                                   S )z<
        Check if our transport is still connected.
        N)boolrN   
is_closingr#   s    r   is_connectedzSMTPProtocol.is_connected|   s/    
 DN$.Rt~7P7P7R7R3RSSSr    rN   c                     t          t          j        |          | _        |                    d          d u| _        | j                                        | _        t          j	                    | _
        d| _        d S )N
sslcontextF)r   r   	TransportrN   get_extra_inforJ   r   r2   rM   LockrO   rQ   )r   rN   s     r   connection_madezSMTPProtocol.connection_made   s_    g/;;"11,??tK $
 8 8 : :$\^^r    r+   c                    t                                          |           | j        r| j                                        sq| j        r8| j                            t          t          j        j	        d                     n2t          d          }|r||_        | j                            |           d | _        d | _        | j        r|                                  d S d S )N r0   )rI   r.   rM   r&   rQ   r'   r   r   closingvaluer	   	__cause__r-   rN   rO   rR   )r   r+   smtp_excrS   s      r   r.   zSMTPProtocol.connection_lost   s    $$$  		>)>)C)C)E)E 		> >%00 !3!92>>    22CDD -),H&%33H===!) 	-**,,,,,	- 	-r    datac                    | j         t          d|          | j                                         rd S | j                            |           t          | j                  t          k    rC| j        d d = | j                             t          t          j
        j        d                     d S |                    d          }|dk    s||dz   |dz            dk    rd S 	 |                                 }|| j                             |           d S d S # t          $ r%}| j                             |           Y d }~d S d }~ww xY w)Nz4data_received called without a response waiter set: Response too long   
   r      -)rM   RuntimeErrorr&   rL   extendlenMAX_RESPONSE_LENGTHr-   r   r   invalid_responsere   rfind_read_response_from_bufferr'   rC   )r   rh   last_linebreakresponser+   s        r   data_receivedzSMTPProtocol.data_received   s    (OtOO   "'')) 	FD!!!t|222QQQ!//%/57J   
 F E**b  NQ&!);;<DDF	;6688H #%00::::: $#  	5 	5 	5!//444444444	5s   !D 
ED??Ec                     t          d          }| j        r3| j                                        s| j                            |           dS )NzUnexpected EOF receivedF)r	   rM   r&   r-   )r   r+   s     r   eof_receivedzSMTPProtocol.eof_received   sO    $%>??  	5)>)C)C)E)E 	5!//444 ur    c                     | j         rK| j                                         r2| j                                         s| j                                         S dS )z
        Return any exception that has been set on the response waiter.

        Used to avoid 'Future exception was never retrieved' warnings
        N)rM   r&   	cancelled	exceptionr#   s    r   rV   z)SMTPProtocol._retrieve_response_exception   s[     !	5%**,,	5 )3355	5
 (22444tr    c                 T   d}t                      }d}d}	 | j                            d|          }|dk    rn0t          | j        ||dz                      }t	          |          t
          k    rt          t          j        j	        d          	 t          |dd	                   }nH# t          $ r; |                    d
d          }t          t          j        j	        d|           dw xY w|t	          |          z  }t	          |          r|                    d           |                    |dd                             d                     |d	d         dk    rd}nS|r=t          |t          |                              d
d                    }| j        d|= |S dS )z7Parse the actual response (if any) from the data bufferrl   r   FTrk   r   rj   Nrm   zutf-8ignore)errorszMalformed SMTP response line: r   s    	
rn   surrogateescape)rK   rL   findbytesrq   MAX_LINE_LENGTHr   r   rs   re   int
ValueErrordecoderp   stripr   )	r   codemessageoffsetmessage_completeline_end_indexline
error_textrw   s	            r   ru   z'SMTPProtocol._read_response_from_buffer   s   ++ 	!\..uf==N##f~/A&ABCCD4yy?**+/57J  48}}   ![[[BB
+/5AZAA   c$iiF7|| &u%%%NN48>>*55666AaCyD  #' 7	:  	#eGnn++G5FGG H WfW%O4s   B( (AC-timeoutc                   K   | j         t          d          	 t          j        | j         |           d{V }n.# t          t          j        f$ r}t          d          |d}~ww xY w	 | j        d| _         nQ| j                                        | _         n2# | j        d| _         n| j                                        | _         w xY w|S )a  
        Get a status response from the server.

        This method must be awaited once per command sent; if multiple commands
        are written to the transport without awaiting, response data will be lost.

        Returns an :class:`.response.SMTPResponse` namedtuple consisting of:
          - server response code (e.g. 250, or such, if all goes well)
          - server response string (multiline responses are converted to a
            single, multiline string).
        Nr0   z%Timed out waiting for server response)	rM   r	   r   wait_forTimeoutErrorr   rN   r   r2   )r   r   resultr+   s       r   read_responsezSMTPProtocol.read_response  s        (():;;;		C"+D,A7KKKKKKKKFFg23 	Y 	Y 	Y&'NOOUXX	Y 
 ~%(,%%(,
(@(@(B(B%% ~%(,%%(,
(@(@(B(B%BBBBs&    ; B A&A!!A&&B /Cc                 &   | j         | j                                         rt          d          	 t          t          j        | j                                       |           d S # t          t          f$ r t          d| j         d          d w xY w)Nr0   z
Transport z does not support writing.)
rN   rZ   r	   r   r   WriteTransportwriteAttributeErrorr:   ro   )r   rh   s     r   r   zSMTPProtocol.write"  s    >!T^%>%>%@%@!():;;;	'88>>tDDDDD 34 	 	 	IT^III 	s   2A% %+Br   argsc                  K   |D ]+}t                               |          rt          d          ,| j        t	          d          d                    |          dz   }| j        4 d{V  |                     |           |dk    rd| _        |                     |           d{V }ddd          d{V  n# 1 d{V swxY w Y   |S )	zj
        Sends an SMTP command along with any args to the server, and returns
        a response.
        z3Command arg contains a prohibited control characterNServer not connected       
s   QUIT
Tr   )	COMMAND_INJECTION_REGEXsearchr   rO   r	   joinr   rQ   r   )r   r   r   argcommandrw   s         r   execute_commandzSMTPProtocol.execute_command.  s       	X 	XC&--c22 X !VWWWX%()?@@@))D//G+% 	A 	A 	A 	A 	A 	A 	A 	AJJw+%%"&!///@@@@@@@@H	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A s   ,?B==
C
Cr   c                   K   | j         t          d          t                              d|          }t                              d|          }|                    d          s|dz  }|dz  }| j         4 d{V  |                     d           |                     |           d{V }|j        t          j
        k    rt          |j        |j                  |                     |           |                     |           d{V }|j        t          j        k    rt          |j        |j                  	 ddd          d{V  n# 1 d{V swxY w Y   |S )z
        Sends an SMTP DATA command to the server, followed by encoded message content.

        Automatically quotes lines beginning with a period per RFC821.
        Lone \\r and \\n characters are converted to \\r\\n
        characters.
        Nr   r   s   ..s   .
s   DATA
r   )rO   r	   LINE_ENDINGS_REGEXsubPERIOD_REGEXendswithr   r   r   r   start_inputr   r   	completed)r   r   r   start_responserw   s        r   execute_data_commandz!SMTPProtocol.execute_data_commandF  s      %()?@@@$(('::""5'22(( 	wG8% 		E 		E 		E 		E 		E 		E 		E 		EJJ{####'#5#5g#5#F#FFFFFFFN"j&<<<#N$79OPPPJJw!///@@@@@@@@H}
 444#HM83CDDDD		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E s   ;CE
EEtls_contextserver_hostnamec           	      ^  K   | j         rt          d          | j        t          d          | j        4 d{V  |                     d           |                     |           d{V }|j        t          j        k    rt          |j        |j
                  | j        | j                                        rt          d          | j        dd= 	 | j                            t!          t"          j        | j                  | |d||           d{V }nh# t&          t"          j        f$ r}t)          d	          |d}~wt*          $ r}t)          d
          |d}~wt,          $ r}t          d          |d}~ww xY w|| _        ddd          d{V  n# 1 d{V swxY w Y   |S )zG
        Puts the connection to the SMTP server into TLS mode.
        zAlready using TLS.Nr   s
   STARTTLS
r   r0   F)server_sider   ssl_handshake_timeoutz#Timed out while upgrading transportz,Connection aborted while upgrading transportz*Connection reset while upgrading transport)rJ   ro   rO   r	   r   r   r   r   readyr   r   rN   rZ   rL   r   	start_tlsr   r   r   r   r
   ConnectionAbortedErrorConnectionError)r   r   r   r   rw   tls_transportr+   s          r   r   zSMTPProtocol.start_tlsf  s      > 	53444%()?@@@% &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+JJ'''!///@@@@@@@@H}
 000+HM8;KLLL ~%)B)B)D)D%,->??? QQQ&*j&:&:/@@ %$3*1 '; ' ' ! ! ! ! ! ! !'"67 W W W&'LMMSVV)   &B  #   ,@ 
 +DNM&	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+ &	+P sO   BFADFF 1EF EF +E;;F  
F
F&)F&)NNr=   r9   )"r>   r?   r@   r   rB   r   r   rD   r<   rW   propertyrY   r[   BaseTransportra   rC   r.   r   rx   rz   BaseExceptionrV   r   ru   floatr   r   r   r   ssl
SSLContextstrr   __classcell__)rS   s   @r   r   r   c   s        26>BB B'$.B #+2t8"4t";B 
	B B B B B B #(< #AW # # # #, , , ,
 Td T T T XT )>  4        -9t#3 - - - - - - -(";% ";D "; "; "; ";Hd    md.B    +L4,? + + + +Z 54< <    8
% 
D 
 
 
 
 59  %*T\	   2 7; ',t|	   F '+ $	6 6^6 t6 	6
 
6 6 6 6 6 6 6 6r    r   )rA   r   r   rer   typingr   r   r   r   r   r   r	   r
   rw   r   r   __all__r   rr   compiler   r   r   Protocolr   BaseProtocolr   rE   r    r   <module>r      s         				 



 ! ! ! ! ! ! ! !              # " " " " "         &) RZ 899 rz+&&$"*%899 <" <" <" <" <"w' <" <" <"~y y y y y#W%9 y y y y yr    