
    f                       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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      Z$y)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TelnetProtocolParserc                    t        | f      S N)bytes)numbers    a/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/contrib/telnet/protocol.pyint2byter      s    &                "                                             c                      e Zd ZdZ	 	 	 	 	 	 	 	 ddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZddZy)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)
    c                    || _         || _        || _        | j                         | _        | j                  j                  d        y r
   )data_received_callbacksize_received_callbackttype_received_callback_parse_coroutine_parsersend)selfr%   r&   r'   s       r   __init__zTelnetProtocolParser.__init__G   s>     '=#&<#'>$,,.$r   c                &    | j                  |       y r
   )r%   r+   datas     r   received_dataz"TelnetProtocolParser.received_dataT   s    ##D)r   c                0    t        j                  d|       y)zReceived telnet DO command.zDO %rNr   infor.   s     r   do_receivedz TelnetProtocolParser.do_receivedW   s    GT"r   c                0    t        j                  d|       y)zReceived telnet DONT command.zDONT %rNr2   r.   s     r   dont_receivedz"TelnetProtocolParser.dont_received[       It$r   c                0    t        j                  d|       y)zReceived telnet WILL command.zWILL %rNr2   r.   s     r   will_receivedz"TelnetProtocolParser.will_received_   r7   r   c                0    t        j                  d|       y)zReceived telnet WONT command.zWONT %rNr2   r.   s     r   wont_receivedz"TelnetProtocolParser.wont_receivedc   r7   r   c                
   |t         k(  r| j                  |       y |t        k(  r| j                  |       y |t        k(  r| j                  |       y |t        k(  r| j                  |       y t        j                  d||       y )Nzcommand received %r %r)
DOr4   DONTr6   WILLr9   WONTr;   r   r3   )r+   commandr/   s      r   command_receivedz%TelnetProtocolParser.command_receivedg   si    b=T"_t$_t$_t$ KK0'4@r   c                    t        |      dk(  r,t        j                  d|      \  }}| j                  ||       yt	        j
                  d       y)z5
        Received NAWS. (Window dimensions.)
           z!HHzWrong number of NAWS bytesN)lenstructunpackr&   r   warning)r+   r/   columnsrowss       r   nawszTelnetProtocolParser.nawsw   sB     t9> #MM%6MGT''g6NN78r   c                    |dd |dd }}|t         k(  r#|j                  d      }| j                  |       yt        j                  d       y)z)
        Received terminal type.
        r   r   Nasciiz.Received a non-IS terminal type Subnegotiation)ISdecoder'   r   rH   )r+   r/   subcmdttypes       r   rQ   zTelnetProtocolParser.ttype   sI     Aay$qr(R<KK(E((/NNKLr   c                    |dd |dd }}|t         k(  r| j                  |       y|t        k(  r| j                  |       yt	        j
                  dt        |             y)z%
        Got negotiate data.
        r   r   NzNegotiate (%r got bytes))NAWSrK   TTYPErQ   r   r3   rE   )r+   r/   rA   payloads       r   	negotiatezTelnetProtocolParser.negotiate   sS      !9d12hd?IIgJJwKK2CI>r   c           
   #  h  K   	 d}|t        d      k(  rn|t        k(  rd}|t        k(  r| j                  |       n|t        t        t
        t        t        t        t        t        t        f	v r| j                  |d       n|t        t        t        t         fv rd}| j                  ||       n}|t"        k(  rtg }	 d}|t        k(  r d}|t$        k(  rn$|j'                  |       n|j'                  |       ?| j)                  dj+                  |             n| j                  |       .w)z_
        Parser state machine.
        Every 'yield' expression returns the next byte.
        Nr   r   )r   IACr0   NOPDMBRKIPAOAYTECELGArB   r=   r>   r?   r@   SBSEappendrV   join)r+   dd2d3r/   d4s         r   r(   z%TelnetProtocolParser._parse_coroutine   s    
 AHQK c9&&r* CS"b#r2rBB))"c2 BdD11B))"b1 2XD"9!&B!Rx % $B KKO  NN388D>2""1%S s   D0D2c                Z    |D ]&  }| j                   j                  t        |             ( y)z*
        Feed data to the parser.
        N)r)   r*   r   )r+   r/   bs      r   feedzTelnetProtocolParser.feed   s)      	+ALLhqk*	+r   N)r%   zCallable[[bytes], None]r&   zCallable[[int, int], None]r'   zCallable[[str], None]returnNone)r/   r   rm   rn   )rA   r   r/   r   rm   rn   )rm   zGenerator[None, bytes, None])__name__
__module____qualname____doc__r,   r0   r4   r6   r9   r;   rB   rK   rQ   rV   r(   rl    r   r   r   r   8   sl      7  !;  "7	 
 
 *#%%%A 9	M?.&`+r   )r   intrm   r   )%rr   
__future__r   rF   typingr   r   logr   __all__r   rY   SGArX   r=   r>   LINEMODErb   r?   r@   MODErc   ECHOrS   SUPPRESS_GO_AHEADrT   SENDrN   rZ   r[   r\   r]   r^   r_   r`   ra   r   rs   r   r   <module>r      s"   #  &  

 qkqksmc]}B<c]}}{c]{|B<QK {a[c]smc]c]smc]c]c]Y+ Y+r   