o
    i4                     @  sr  d Z ddlmZ ddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 dgZ6d$ddZ7d%ddZ8G dd dZ9G dd  d Z:d&d!d"Z;G d#d dZ<dS )'z
Telnet server.
    )annotationsN)get_running_loop)	Any	AwaitableCallableListOptionalSetTextIOTuplecast)create_app_sessionget_app)run_in_terminal)Size)AnyFormattedTextto_formatted_text)	PipeInputcreate_pipe_input)Vt100_Output)print_formatted_text)	BaseStyle
DummyStyle   )logger)DOECHOIACLINEMODEMODENAWSSBSESENDSUPPRESS_GO_AHEADTTYPEWILLTelnetProtocolParserTelnetServernumberintreturnbytesc                 C  s
   t | fS N)r,   )r)    r.   _/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/telnet/server.pyint2byte,      
r0   
connectionsocket.socketNonec                 C  s   t d | tt t  | tt t  | tt t t	 t
d t t  | tt t  | tt t  | tt t  | tt t t t t  d S )NzInitializing telnet connectionr   )r   infosendr   r   r   r&   r$   r!   r   r0   r"   r   r    r%   r#   r2   r.   r.   r/   _initialize_telnet0   s   
&"r8   c                   @  s^   e Zd ZdZddd	ZdddZdddZdddZdddZe	dddZ
e	dddZdS )_ConnectionStdoutzk
    Wrapper around socket which provides `write` and `flush` methods for the
    Vt100_Output output.
    r2   r3   encodingstrr+   r4   c                 C  s"   || _ || _d| _g | _d| _d S )NstrictF)	_encoding_connection_errors_buffer_closed)selfr2   r:   r.   r.   r/   __init__S   s
   
z_ConnectionStdout.__init__datac                 C  s2   | dd}| j|j| j| jd |   d S )N
z
)errors)replacer@   appendencoder=   r?   flushrB   rD   r.   r.   r/   writeZ   s   z_ConnectionStdout.writeboolc                 C  s   dS NTr.   rB   r.   r.   r/   isatty_   s   z_ConnectionStdout.isattyc              
   C  s\   z| j s| jd| j W n ty( } ztd|  W Y d }~nd }~ww g | _d S )N    z"Couldn't send data over socket: %s)rA   r>   r6   joinr@   OSErrorr   warning)rB   er.   r.   r/   rJ   b   s   
z_ConnectionStdout.flushc                 C  s
   d| _ d S rN   )rA   rO   r.   r.   r/   closek   r1   z_ConnectionStdout.closec                 C     | j S r-   )r=   rO   r.   r.   r/   r:   n      z_ConnectionStdout.encodingc                 C  rW   r-   )r?   rO   r.   r.   r/   rF   r   rX   z_ConnectionStdout.errorsN)r2   r3   r:   r;   r+   r4   )rD   r;   r+   r4   )r+   rM   r+   r4   )r+   r;   )__name__
__module____qualname____doc__rC   rL   rP   rJ   rV   propertyr:   rF   r.   r.   r.   r/   r9   M   s    




	r9   c                   @  sd   e Zd ZdZ	d,d-ddZd.ddZd/ddZd.ddZd0d!d"Zd0d#d$Z	d1d'd(Z
d.d)d*Zd+S )2TelnetConnectionz6
    Class that represents one Telnet connection.
    Tconnr3   addrtuple[str, int]interact-Callable[[TelnetConnection], Awaitable[None]]serverr(   r:   r;   styleBaseStyle | Nonevt100_inputr   
enable_cprrM   r+   r4   c	                   s   |_ |_|_|_|_|_d_t _	|_
 _d _tddd_t| dfddttt||d	_dfdd}	dfdd}
d fdd}t|	|
|_d _d S )NF(   O   rowscolumnsr+   r   c                     s    j S r-   )sizer.   rO   r.   r/   get_size   s   z+TelnetConnection.__init__.<locals>.get_size)r:   rD   r,   r4   c                   s    j |  dS )z-TelnetProtocolParser 'data_received' callbackN)rh   
send_bytesrD   rO   r.   r/   data_received   s   z0TelnetConnection.__init__.<locals>.data_receivedrm   r*   rn   c                   s:   t | |d _ jdur jr jdd  dS dS dS )z-TelnetProtocolParser 'size_received' callbackrl   Nc                   S  s
   t   S r-   )r   
_on_resizer.   r.   r.   r/   <lambda>      
 zBTelnetConnection.__init__.<locals>.size_received.<locals>.<lambda>)r   ro   vt100_outputcontextrunrl   rO   r.   r/   size_received   s   z0TelnetConnection.__init__.<locals>.size_receivedttyper;   c                   s"   t j|  d_j  dS )z.TelnetProtocolParser 'ttype_received' callback)termri   N)r   stdoutrw   _readyset)r{   ri   rp   rB   r.   r/   ttype_received   s   
z1TelnetConnection.__init__.<locals>.ttype_received)r+   r   rD   r,   r+   r4   )rm   r*   rn   r*   r+   r4   )r{   r;   r+   r4   )r`   ra   rc   re   r:   rf   rA   asyncioEventr~   rh   ri   rw   r   ro   r8   r   r
   r9   r}   r'   parserrx   )rB   r`   ra   rc   re   r:   rf   rh   ri   rs   rz   r   r.   r   r/   rC   |   s(   

zTelnetConnection.__init__c                   s   d fdd}t  }| j| z; j I dH  t j jd t	  _
  I dH  W d   n1 s;w   Y  W    dS W    dS    w )z"
        Run application.
        r+   r4   c                    s:    j d} | r |  d S td j     d S )Ni   z"Connection closed by client. %r %r)r`   recvfeedr   r5   ra   rV   rr   rO   r.   r/   handle_incoming_data   s
   z>TelnetConnection.run_application.<locals>.handle_incoming_dataN)inputoutputrY   )r   
add_readerr`   r~   waitr   rh   rw   contextvarscopy_contextrx   rc   rV   )rB   r   loopr.   rO   r/   run_application   s   

z TelnetConnection.run_applicationrD   r,   c                 C  s   | j | dS )zF
        Handler for incoming data. (Called by TelnetServer.)
        N)r   r   rK   r.   r.   r/   r      s   zTelnetConnection.feedc                 C  s@   | j sd| _ | j  t | j | j  | j  dS dS )z#
        Closed by client.
        TN)rA   rh   rV   r   remove_readerr`   r}   rO   r.   r.   r/   rV      s   

zTelnetConnection.closeformatted_textr   c                 C  s0   | j du rdS t|}t| j || jpt  dS )z*
        Send text to the client.
        N)rw   r   r   rf   r   rB   r   r.   r.   r/   r6      s   
zTelnetConnection.sendc                   s   t    fddS )z\
        Send text to the client.
        This is asynchronous, returns a `Future`.
        c                     s
     S r-   )r6   r.   r   rB   r.   r/   ru      rv   z4TelnetConnection.send_above_prompt.<locals>.<lambda>)r   _run_in_terminalr   r.   r   r/   send_above_prompt   s   z"TelnetConnection.send_above_promptfuncCallable[[], None]c                 C  s    | j r| j t| d S td)Nz2Called _run_in_terminal outside `run_application`.)rx   ry   r   RuntimeError)rB   r   r.   r.   r/   r      s   z!TelnetConnection._run_in_terminalc                 C  s4   | j du rdS | j   | j dd | j   dS )zB
        Erase the screen and move the cursor to the top.
        Nr   )rw   erase_screencursor_gotorJ   rO   r.   r.   r/   r      s
   

zTelnetConnection.erase_screenN)T)r`   r3   ra   rb   rc   rd   re   r(   r:   r;   rf   rg   rh   r   ri   rM   r+   r4   rY   r   )r   r   r+   r4   )r   r   r+   r4   )rZ   r[   r\   r]   rC   r   r   rV   r6   r   r   r   r.   r.   r.   r/   r_   w   s    
7




r_   c                   s   d S r-   r.   r7   r.   r.   r/   _dummy_interact  s   r   c                   @  s`   e Zd ZdZddedddfd$ddZed%ddZd&d'ddZd(ddZ	d(dd Z
d)d"d#ZdS )*r(   a  
    Telnet server implementation.

    Example::

        async def interact(connection):
            connection.send("Welcome")
            session = PromptSession()
            result = await session.prompt_async(message="Say something: ")
            connection.send(f"You said: {result}
")

        async def main():
            server = TelnetServer(interact=interact, port=2323)
            await server.run()
    z	127.0.0.1   zutf-8NThostr;   portr*   rc   rd   r:   rf   rg   ri   rM   r+   r4   c                 C  s<   || _ || _|| _|| _|| _|| _d | _g | _t | _	d S r-   )
r   r   rc   r:   rf   ri   	_run_task_application_tasksr   connections)rB   r   r   rc   r:   rf   ri   r.   r.   r/   rC     s   	zTelnetServer.__init__r3   c                 C  s>   t  t jt j}|t jt jd |||f |d |S )Nr      )socketAF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRbindlisten)clsr   r   sr.   r.   r/   _create_socket0  s
   
zTelnetServer._create_socketready_cbCallable[[], None] | Nonec                   s     j jtd j j t  fdd |r#|  z4t I dH  W t 	 
   jD ]}|  q9t jdkrVtj jdtjdI dH  dS dS t 	 
   jD ]}|  qet jdkrtj jdtjdI dH  w w )z
        Run the telnet server, until this gets cancelled.

        :param ready_cb: Callback that will be called at the point that we're
            actually listening.
        z.Listening for telnet connections on %s port %rc                     s
     S r-   )_acceptr.   rB   r   r.   r/   ru   F  rv   z"TelnetServer.run.<locals>.<lambda>Nr   )timeoutreturn_when)r   r   r   r   r5   r   r   r   Futurer   rV   r   cancellenr   ALL_COMPLETED)rB   r   tr.   r   r/   ry   :  s@   




zTelnetServer.runc                 C  s$   | j durdS t |  | _ dS )z}
        Deprecated: Use `.run()` instead.

        Start the telnet server (stop by calling and awaiting `stop()`).
        N)r   r   create_taskry   rO   r.   r.   r/   starta  s   
zTelnetServer.startc                   sD   | j dur | j   z	| j I dH  W dS  tjy   Y dS w dS )z
        Deprecated: Use `.run()` instead.

        Stop a telnet server that was started using `.start()` and wait for the
        cancellation to complete.
        N)r   r   r   CancelledErrorrO   r.   r.   r/   stopm  s   

zTelnetServer.stoplisten_socketc                   sP   |  \ tjdg R   d fdd}t | j dS )z1
        Accept new incoming connection.
        zNew connection %r %rr+   r4   c                    s  zz]t  P} t jjj| jd}j| tj	dg R   z|
 I d H  W j| tj	dg R   nj| tj	dg R   w W d    n1 sYw   Y  W n> tym   t	d Y n: tyz   t	d Y n6 ty } ztdt|j | dd l}|  W Y d }~n d }~ww W j d S W j d S W j d S W j d S j w )N)r:   rf   rh   ri   zStarting interaction %r %rzStopping interaction %r %rz)Unhandled EOFError in telnet application.z2Unhandled KeyboardInterrupt in telnet application.zGot %sr   )r   r_   rc   r:   rf   ri   r   addr   r5   r   removeEOFErrorKeyboardInterruptBaseExceptionprinttyperZ   	traceback	print_excr   )rh   r2   rU   r   ra   r`   rB   taskr.   r/   ry     sR   

z!TelnetServer._accept.<locals>.runNrY   )acceptr   r5   r   r   r   rH   )rB   r   ry   r.   r   r/   r   {  s
   &zTelnetServer._accept)r   r;   r   r*   rc   rd   r:   r;   rf   rg   ri   rM   r+   r4   )r   r;   r   r*   r+   r3   r-   )r   r   r+   r4   rY   )r   r3   r+   r4   )rZ   r[   r\   r]   r   rC   classmethodr   ry   r   r   r   r.   r.   r.   r/   r(   
  s    	
'
)r)   r*   r+   r,   )r2   r3   r+   r4   )r2   r_   r+   r4   )=r]   
__future__r   r   r   r   sysr   typingr   r   r   r   r   r	   r
   r   r   "prompt_toolkit.application.currentr   r   *prompt_toolkit.application.run_in_terminalr   prompt_toolkit.data_structuresr   prompt_toolkit.formatted_textr   r   prompt_toolkit.inputr   r   prompt_toolkit.output.vt100r   prompt_toolkit.rendererr   prompt_toolkit.stylesr   r   logr   protocolr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   __all__r0   r8   r9   r_   r   r(   r.   r.   r.   r/   <module>   s6    ,<

* 
