o
    ˷e-                     @   s  U d Z ddlZddlZddl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 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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ej4dkrddl5Z5ndZ5ee6d< dgZ7de8de9fddZ:dejddfddZ;G dd dZ<G dd dZ=de=ddfddZ>G d d dZ?dS )!z
Telnet server.
    N)	Any	AwaitableCallableListOptionalSetTextIOTuplecast)create_app_sessionget_app)run_in_terminal)Size)get_event_loop)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)      contextvarsTelnetServernumberreturnc                 C   s
   t | fS N)bytes)r+    r/   [/var/www/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/telnet/server.pyint2byte.      
r1   
connectionc                 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   r1   r!   r   r   r$   r"   r3   r/   r/   r0   _initialize_telnet2   s   
&"r7   c                   @   s   e Zd ZdZdejdeddfddZdeddfd	d
ZdefddZ	dddZ
dddZedefddZedefddZdS )_ConnectionStdoutzk
    Wrapper around socket which provides `write` and `flush` methods for the
    Vt100_Output output.
    r3   encodingr,   Nc                 C   s"   || _ || _d| _g | _d| _d S )NstrictF)	_encoding_connection_errors_buffer_closed)selfr3   r9   r/   r/   r0   __init__U   s
   
z_ConnectionStdout.__init__datac                 C   s2   | dd}| j|j| j| jd |   d S )N
z
)errors)replacer>   appendencoder;   r=   flushr@   rB   r/   r/   r0   write\   s   z_ConnectionStdout.writec                 C   s   dS NTr/   r@   r/   r/   r0   isattya   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)r?   r<   r5   joinr>   OSErrorr   warning)r@   er/   r/   r0   rH   d   s   
z_ConnectionStdout.flushc                 C   s
   d| _ d S rK   )r?   rL   r/   r/   r0   closem   r2   z_ConnectionStdout.closec                 C      | j S r-   )r;   rL   r/   r/   r0   r9   p      z_ConnectionStdout.encodingc                 C   rT   r-   )r=   rL   r/   r/   r0   rD   t   rU   z_ConnectionStdout.errorsr,   N)__name__
__module____qualname____doc__socketstrrA   rJ   boolrM   rH   rS   propertyr9   rD   r/   r/   r/   r0   r8   O   s    

	r8   c                   @   s   e Zd ZdZdejdeeef ded ge	d f ddded	e
e d
eddfddZdddZdeddfddZdddZdeddfddZdeddfddZdeg df ddfddZdddZdS ) TelnetConnectionz6
    Class that represents one Telnet connection.
    connaddrinteractNserverr*   r9   stylevt100_inputr,   c                    s   |_ |_|_|_|_|_d_t _	|_
d _tddd_t| dtffdd ttt||d_d	tdd ffd
d}dtdtdd ffdd}	dtdd f fdd}
t||	|
_d _d S )NF(   O   rowscolumnsr,   c                      s    j S r-   )sizer/   rL   r/   r0   get_size   s   z+TelnetConnection.__init__.<locals>.get_size)r9   rB   c                    s    j |  dS )z-TelnetProtocolParser 'data_received' callbackN)re   
send_bytesrB   rL   r/   r0   data_received   s   z0TelnetConnection.__init__.<locals>.data_receivedri   rj   c                    s:   t | |d _ jdur jr jdd  dS dS dS )z-TelnetProtocolParser 'size_received' callbackrh   Nc                   S   s
   t   S r-   )r   
_on_resizer/   r/   r/   r0   <lambda>      
 zBTelnetConnection.__init__.<locals>.size_received.<locals>.<lambda>)r   rk   vt100_outputcontextrunrh   rL   r/   r0   size_received   s   z0TelnetConnection.__init__.<locals>.size_receivedttypec                    s    t j | d_j  dS )z.TelnetProtocolParser 'ttype_received' callback)termN)r   stdoutrs   _readyset)rw   rl   r@   r/   r0   ttype_received   s   z1TelnetConnection.__init__.<locals>.ttype_received)r`   ra   rb   rc   r9   rd   r?   asyncioEventrz   re   rs   r   rk   r7   r
   r   r8   ry   r.   intr\   r&   parserrt   )r@   r`   ra   rb   rc   r9   rd   re   ro   rv   r}   r/   r|   r0   rA   ~   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,   Nc                     s:    j d} | r |  d S td j     d S )Ni   z"Connection closed by client. %r %r)r`   recvfeedr   r4   ra   rS   rn   rL   r/   r0   handle_incoming_data   s
   z>TelnetConnection.run_application.<locals>.handle_incoming_data)inputoutputrV   )r   
add_readerr`   rz   waitr   re   rs   r)   copy_contextrt   rb   rS   )r@   r   loopr/   rL   r0   run_application   s   

z TelnetConnection.run_applicationrB   c                 C   s   | j | dS )zF
        Handler for incoming data. (Called by TelnetServer.)
        N)r   r   rI   r/   r/   r0   r      s   zTelnetConnection.feedc                 C   s@   | j sd| _ | j  t | j | j  | j  dS dS )z#
        Closed by client.
        TN)r?   re   rS   r   remove_readerr`   ry   rL   r/   r/   r0   rS      s   

zTelnetConnection.closeformatted_textc                 C   s0   | j du rdS t|}t| j || jpt  dS )z*
        Send text to the client.
        N)rs   r   r   rd   r   r@   r   r/   r/   r0   r5      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-   )r5   r/   r   r@   r/   r0   rq      rr   z4TelnetConnection.send_above_prompt.<locals>.<lambda>)r   _run_in_terminalr   r/   r   r0   send_above_prompt   s   z"TelnetConnection.send_above_promptfuncc                 C   s    | j r| j t| d S td)Nz2Called _run_in_terminal outside `run_application`.)rt   ru   r   RuntimeError)r@   r   r/   r/   r0   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   )rs   erase_screencursor_gotorH   rL   r/   r/   r0   r      s
   

zTelnetConnection.erase_screenrV   )rW   rX   rY   rZ   r[   r	   r\   r   r   r   r   r   r   rA   r   r.   r   rS   r   r5   r   r   r   r/   r/   r/   r0   r_   y   s4    
	

4
r_   c                    s   d S r-   r/   r6   r/   r/   r0   _dummy_interact  s   r   c                   @   s   e Zd ZdZddeddfdededeege	d f d	ed
e
e ddfddZedededejfddZdddZdddZdddZdS )r*   z'
    Telnet server implementation.
    z	127.0.0.1   zutf-8Nhostportrb   r9   rd   r,   c                 C   s6   || _ || _|| _|| _|| _g | _t | _d | _d S r-   )	r   r   rb   r9   rd   _application_tasksr{   connections_listen_socket)r@   r   r   rb   r9   rd   r/   r/   r0   rA     s   	
zTelnetServer.__init__c                 C   s>   t  t jt j}|t jt jd |||f |d |S )Nr      )r[   AF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRbindlisten)clsr   r   sr/   r/   r0   _create_socket!  s
   
zTelnetServer._create_socketc                 C   s:   |  | j| j| _td| j| j t | j| j dS )zn
        Start the telnet server.
        Don't forget to call `loop.run_forever()` after doing this.
        z.Listening for telnet connections on %s port %rN)	r   r   r   r   r   r4   r   r   _acceptrL   r/   r/   r0   start+  s
   
zTelnetServer.startc              	      sv   | j rt | j  | j   | jD ]}|  q| jD ]}z|I d H  W q tjy8   t	dt
| Y qw d S )NzTask %s cancelled)r   r   r   rS   r   cancelr~   CancelledErrorr   debugr\   )r@   tr/   r/   r0   stop7  s   



zTelnetServer.stopc                    s`   j du rdS j  \ tjdg R   d fdd}t | j dS )z1
        Accept new incoming connection.
        NzNew connection %r %rr,   c               
      s  zz[t  N} t 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 sWw   Y  W n> tyk   td Y n: tyx   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)r9   rd   re   zStarting interaction %r %rzStopping interaction %r %rz)Unhandled EOFError in telnet application.z2Unhandled KeyboardInterrupt in telnet application.zGot %sr   )r   r_   rb   r9   rd   r   addr   r4   r   removeEOFErrorKeyboardInterruptBaseExceptionprinttyperW   	traceback	print_excr   )re   r3   rR   r   ra   r`   r@   taskr/   r0   ru   Q  sP   	
z!TelnetServer._accept.<locals>.runrV   )r   acceptr   r4   r   create_taskr   rF   )r@   ru   r/   r   r0   r   F  s   
%zTelnetServer._acceptrV   )rW   rX   rY   rZ   r   r\   r   r   r_   r   r   r   rA   classmethodr[   r   r   r   r   r/   r/   r/   r0   r*   	  s2    

	
)@rZ   r~   r[   sys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.eventloopr   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&   version_infor)   __annotations____all__r   r.   r1   r7   r8   r_   r   r*   r/   r/   r/   r0   <module>   s8    ,<

* 