o
    ˷e                     @   s"  d Z ddlZddlmZmZ ddlmZ dgZdede	f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`.
    N)Callable	Generator   )loggerTelnetProtocolParsernumberreturnc                 C   s
   t | fS N)bytes)r    r   ]/var/www/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/telnet/protocol.pyint2byte   s   
r               "                                             c                   @   s  e Zd ZdZdeegdf deeegdf deegdf ddfddZd	eddfd
dZ	d	eddfddZ
d	eddfddZd	eddfddZd	eddfddZded	eddfddZd	eddfddZd	eddfddZd	eddfddZdededf fddZd	eddf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_callbackNsize_received_callbackttype_received_callbackr   c                 C   s,   || _ || _|| _|  | _| jd  d S r	   )r!   r"   r#   _parse_coroutine_parsersend)selfr!   r"   r#   r   r   r   __init__D   s
   
zTelnetProtocolParser.__init__datac                 C   s   |  | d S r	   )r!   r'   r)   r   r   r   received_dataR   s   z"TelnetProtocolParser.received_datac                 C      t d| dS )zReceived telnet DO command.zDO %rNr   infor*   r   r   r   do_receivedU      z TelnetProtocolParser.do_receivedc                 C   r,   )zReceived telnet DONT command.zDONT %rNr-   r*   r   r   r   dont_receivedY   r0   z"TelnetProtocolParser.dont_receivedc                 C   r,   )zReceived telnet WILL command.zWILL %rNr-   r*   r   r   r   will_received]   r0   z"TelnetProtocolParser.will_receivedc                 C   r,   )zReceived telnet WONT command.zWONT %rNr-   r*   r   r   r   wont_receiveda   r0   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)
DOr/   DONTr1   WILLr2   WONTr3   r   r.   )r'   r4   r)   r   r   r   command_receivede   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   nawsu   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   r>   )r'   r)   subcmdttyper   r   r   rF      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))NAWSrA   TTYPErF   r   r.   r;   )r'   r)   r4   payloadr   r   r   	negotiate   s   zTelnetProtocolParser.negotiatec              
   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   IACr+   NOPDMBRKIPAOAYTECELGAr9   r5   r6   r7   r8   SBSEappendrJ   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.feed)__name__
__module____qualname____doc__r   r
   intstrr(   r+   r/   r1   r2   r3   r9   rA   rF   rJ   r   r$   r_   r   r   r   r   r   5   s,    
0)%rc   r<   typingr   r   logr   __all__rd   r
   r   rM   SGArL   r5   r6   LINEMODErV   r7   r8   MODErW   ECHOrG   SUPPRESS_GO_AHEADrH   SENDrC   rN   rO   rP   rQ   rR   rS   rT   rU   r   r   r   r   r   <module>   sD    