o
    Df                     @  s&  d Z ddlmZ ddlZddlmZmZ ddlmZ dgZ	d"ddZ
e
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZe
dZ e
dZ!e
dZ"e
d Z#G d!d dZ$dS )#z
Parser for the Telnet protocol. (Not a complete implementation of the telnet
specification, but sufficient for a command line interface.)

Inspired by `Twisted.conch.telnet`.
    )annotationsN)Callable	Generator   )loggerTelnetProtocolParsernumberintreturnbytesc                 C  s
   t | fS N)r   )r    r   _/home/ubuntu/webapp/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/telnet/protocol.pyint2byte   s   
r               "                                             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d(ddZ	d)ddZ
d(ddZd(ddZd(dd Zd*d"d#Zd(d$d%Zd&S )+r   a  
    Parser for the Telnet protocol.
    Usage::

        def data_received(data):
            print(data)

        def size_received(rows, columns):
            print(rows, columns)

        p = TelnetProtocolParser(data_received, size_received)
        p.feed(binary_data)
    data_received_callbackCallable[[bytes], None]size_received_callbackCallable[[int, int], None]ttype_received_callbackCallable[[str], None]r
   Nonec                 C  s,   || _ || _|| _|  | _| jd  d S r   )r#   r%   r'   _parse_coroutine_parsersend)selfr#   r%   r'   r   r   r   __init__G   s
   
zTelnetProtocolParser.__init__datar   c                 C  s   |  | d S r   )r#   r-   r/   r   r   r   received_dataT   s   z"TelnetProtocolParser.received_datac                 C     t d| dS )zReceived telnet DO command.zDO %rNr   infor0   r   r   r   do_receivedW      z TelnetProtocolParser.do_receivedc                 C  r2   )zReceived telnet DONT command.zDONT %rNr3   r0   r   r   r   dont_received[   r6   z"TelnetProtocolParser.dont_receivedc                 C  r2   )zReceived telnet WILL command.zWILL %rNr3   r0   r   r   r   will_received_   r6   z"TelnetProtocolParser.will_receivedc                 C  r2   )zReceived telnet WONT command.zWONT %rNr3   r0   r   r   r   wont_receivedc   r6   z"TelnetProtocolParser.wont_receivedcommandc                 C  sj   |t kr| | d S |tkr| | d S |tkr!| | d S |tkr,| | d S t	d|| d S )Nzcommand received %r %r)
DOr5   DONTr7   WILLr8   WONTr9   r   r4   )r-   r:   r/   r   r   r   command_receivedg   s   z%TelnetProtocolParser.command_receivedc                 C  s:   t |dkrtd|\}}| || dS td dS )z5
        Received NAWS. (Window dimensions.)
           z!HHzWrong number of NAWS bytesN)lenstructunpackr%   r   warning)r-   r/   columnsrowsr   r   r   nawsw   s   zTelnetProtocolParser.nawsc                 C  sH   |dd |dd }}|t kr|d}| | dS td dS )z)
        Received terminal type.
        r   r   Nasciiz.Received a non-IS terminal type Subnegotiation)ISdecoder'   r   rD   )r-   r/   subcmdttyper   r   r   rL      s
   
zTelnetProtocolParser.ttypec                 C  sZ   |dd |dd }}|t kr| | dS |tkr#| | dS tdt| dS )z%
        Got negotiate data.
        r   r   NzNegotiate (%r got bytes))NAWSrG   TTYPErL   r   r4   rA   )r-   r/   r:   payloadr   r   r   	negotiate   s   zTelnetProtocolParser.negotiateGenerator[None, bytes, None]c              
   c  s    	 dV }|t dkrng|tkrndV }|tkr| | nV|ttttttt	t
tf	v r1| |d nB|ttttfv rCdV }| || n0|tkrmg }	 dV }|tkr_dV }|tkrYn|| n|| qJ| d| n| | q)z_
        Parser state machine.
        Every 'yield' expression returns the next byte.
        TNr       )r   IACr1   NOPDMBRKIPAOAYTECELGAr?   r;   r<   r=   r>   SBSEappendrP   join)r-   dd2d3r/   d4r   r   r   r*      s:   

z%TelnetProtocolParser._parse_coroutinec                 C  s   |D ]
}| j t| qdS )z*
        Feed data to the parser.
        N)r+   r,   r   )r-   r/   br   r   r   feed   s   zTelnetProtocolParser.feedN)r#   r$   r%   r&   r'   r(   r
   r)   )r/   r   r
   r)   )r:   r   r/   r   r
   r)   )r
   rQ   )__name__
__module____qualname____doc__r.   r1   r5   r7   r8   r9   r?   rG   rL   rP   r*   rf   r   r   r   r   r   8   s    










0)r   r	   r
   r   )%rj   
__future__r   rB   typingr   r   logr   __all__r   rT   SGArS   r;   r<   LINEMODEr]   r=   r>   MODEr^   ECHOrM   SUPPRESS_GO_AHEADrN   SENDrI   rU   rV   rW   rX   rY   rZ   r[   r\   r   r   r   r   r   <module>   sF    
