o
    �õ±jë  ã                   @  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   ú™/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/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  sL   |dd… |dd… }}|t kr|jddd�}|  |¡ dS t d¡ dS )z)
        Received terminal type.
        r   r   NÚasciiÚreplace)Úerrorsz.Received a non-IS terminal type Subnegotiation)ÚISÚdecoder'   r   rD   )r-   r/   ÚsubcmdÚttyper   r   r   rN   „   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   ÚTTYPErN   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ÚappendrR   Ú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
   rS   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r1   r5   r7   r8   r9   r?   rG   rN   rR   r*   rh   r   r   r   r   r   8   s    










0)r   r	   r
   r   )%rl   Ú
__future__r   rB   Úcollections.abcr   r   Úlogr   Ú__all__r   rV   ÚSGArU   r;   r<   ÚLINEMODEr_   r=   r>   ÚMODEr`   ÚECHOrO   ÚSUPPRESS_GO_AHEADrP   ÚSENDrK   rW   rX   rY   rZ   r[   r\   r]   r^   r   r   r   r   r   Ú<module>   sF    ÿ
