
    ]2                     j   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)  SSKJ*r*  SSK+J,r,  Sr-S r. " S S\/5      r0 " S S\/5      r1 " S S\5      r2 " S S\/5      r3g)aT  
Telnet server.

Example usage::

    class MyTelnetApplication(TelnetApplication):
        def client_connected(self, telnet_connection):
            # Set CLI with simple prompt.
            telnet_connection.set_application(
                telnet_connection.create_prompt_application(...))

        def handle_command(self, telnet_connection, document):
            # When the client enters a command, just reply.
            telnet_connection.send('You said: %r

' % document.text)

        ...

    a = MyTelnetApplication()
    TelnetServer(application=a, host='127.0.0.1', port=23).run()
    )unicode_literalsN)int2byte	text_typebinary_type)getincrementaldecoder)DEFAULT_BUFFER)	EventLoop)CommandLineInterfaceApplication)Size)create_prompt_application)InputStream)Vt100_Output   )logger)
IACDOLINEMODESBMODESEWILLECHONAWSSUPPRESS_GO_AHEAD)TelnetProtocolParser)TelnetApplication)TelnetServerc                    [         R                  " S5        U R                  [        [        -   [
        -   5        U R                  [        [        -   [        -   5        U R                  [        [        -   [
        -   [        -   [        S5      -   [        -   [        -   5        U R                  [        [        -   [        -   5        U R                  [        [        -   [        -   5        g )NzInitializing telnet connectionr   )r   infosendr   r   r   r   r   r   r   r   r   r   r   )
connections    7lib/third_party/prompt_toolkit/contrib/telnet/server.py_initialize_telnetr$   3   s    
KK01 OOC"Hx'( OOC$J!223 OOC"Hx'$.!<sBRGH OOC$J%& OOC"HtO$    c                   *    \ rS rSrSrS rS rS rSrg)_ConnectionStdoutG   z_
Wrapper around socket which provides `write` and `flush` methods for the
Vt100_Output output.
c                 *    X l         Xl        / U l        g N)	_encoding_connection_buffer)selfr"   encodings      r#   __init___ConnectionStdout.__init__L   s    !%r%   c                     [        U[        5      (       d   eU R                  R                  UR	                  U R
                  5      5        U R                  5         g r*   )
isinstancer   r-   appendencoder+   flushr.   datas     r#   write_ConnectionStdout.writeQ   s=    $	****DKK78

r%   c                      U R                   R                  SR                  U R                  5      5        / U l        g ! [        R
                   a#  n[        R
                  " SU-  5         S nAN:S nAff = f)Nr%   z"Couldn't send data over socket: %s)r,   r!   joinr-   socketerrorr   )r.   es     r#   r6   _ConnectionStdout.flushV   s_    	C!!#((4<<"89  || 	CLL=ABB	Cs   4> A5A00A5)r-   r,   r+   N)	__name__
__module____qualname____firstlineno____doc__r0   r9   r6   __static_attributes__ r%   r#   r'   r'   G   s    

r%   r'   c                   F    \ rS rSrSrS rSS jrS rS rS r	S	 r
S
 rSrg)TelnetConnection_   z.
Class that represents one Telnet connection.
c                 t  ^  [        U[        5      (       d   e[        U[        5      (       d   e[        U[        5      (       d   e[        U[        5      (       d   eUT l        UT l        UT l        ST l        ST l	        UT l
        UT l        S T l        [        SSS9T l        [        U5        U 4S jn[!        XS9T l        [%        T R"                  USS9T l        [)        U5      T l        T R-                  [/        5       5        UR1                  T 5        ST l	        T R2                  R5                  5         g )	NFT(   O   rowscolumnsc                     > T R                   $ r*   )sizer.   s   r#   get_size+TelnetConnection.__init__.<locals>.get_sizey   s    99r%   r/   )write_binary)r3   tupler   r   r   connaddrapplicationclosedhandling_commandserverr/   callbackr   rR   r$   r'   stdoutr   vt100_output_TelnetEventLoopInterface	eventloopset_applicationr   client_connectedcli_redraw)r.   rY   rZ   r[   r^   r/   rT   s   `      r#   r0   TelnetConnection.__init__c   s   $&&&&+'89999&,////(I....		& $  b"-	 	4 	'@(hUS 36: 	689 	$$T* !&r%   Nc                   ^ ^^^^ [        U[        5      (       d   eUb  [        U5      (       d   e[        UT R                  T R
                  S9T l        UT l        T R                  R                  5       m[        TR                  5      m[        T R                  5      mT" 5       /mST R                  l        UUU4S jnUU 4S jn[        X45      T l        g)z
Set ``CommandLineInterface`` instance for this connection.
(This can be replaced any time.)

:param cli: CommandLineInterface instance.
:param callback: Callable that takes the result of the CLI.
N)r[   rc   outputTc                    > [        U [        5      (       d   e TS   R                  U 5      nTR                  U5        g! [         a    T" 5       TS'    gf = f)z.TelnetProtocolParser 'data_received' callback r    N)r3   r   decodefeedUnicodeDecodeError)r8   resultinputstreamstdin_decoderstdin_decoder_clss     r#   data_received7TelnetConnection.set_application.<locals>.data_received   s]    dK0000&q)006  (% #4#6a s   %A   AAc                 B   > [        XS9Tl        TR                  5         g)z.TelnetProtocolParser 'size_received' callback rN   N)r   rR   terminal_size_changed)rO   rP   cbr.   s     r#   size_received7TelnetConnection.set_application.<locals>.size_received   s    $8DI$$&r%   )r3   r   callabler
   rc   ra   rf   r_   create_eventloop_callbacksr   feed_keyr   r/   _is_runningr   parser)	r.   appr_   rt   ry   rx   rq   rr   rs   s	   `    @@@@r#   rd    TelnetConnection.set_application   s     #{++++8H#5#555'nn$$& ! XX002!"++. 2$--@*,-  $			'
 +=Hr%   c                    [        U[        5      (       d   eU R                  R                  U5        U R                  R                  5         U R                  R                  (       a-   U R                  R                  5       nU R                  U5        gg! [        [        4 aD  n[        R                  " S[        U5      R                  5        U R                  5          SnAgSnAff = f)z6
Handler for incoming data. (Called by TelnetServer.)
z%s, closing connection.N)r3   r   r   rn   rf   rg   is_returningreturn_valueEOFErrorKeyboardInterruptr   r    typerA   close_handle_command)r.   r8   r   r?   s       r#   rn   TelnetConnection.feed   s     $,,,, 	 88  #xx446   . ! /0 5tAw7G7GH

	s   )B C*&:C%%C*c                    ^ ^^ [         R                  " ST5        UUU 4S jnU 4S jmT R                  R                  U5        g)z[
Handle command. This will run in a separate thread, in order not
to block the event loop.
zHandle command %rc                     > STl          TR                  b  TR                  TT 5        TR                  R                  T5        g ! TR                  R                  T5        f = f)NT)r]   r_   r^   call_from_executor)commanddoner.   s   r#   in_executor5TelnetConnection._handle_command.<locals>.in_executor   sM    $(D!5==,MM$0..t4..t4s   A A"c                  r  > ST l         T R                  (       d  T R                  R                  5         T R                  R                  [
           R                  5         T R                  R                  R                  5         T R                  R                  5         T R                  R                  5         g g )NF)r]   r\   rf   resetbuffersr   renderer request_absolute_cursor_positionra   r6   rg   rS   s   r#   r   .TelnetConnection._handle_command.<locals>.done   sz    $)D! ;;   0668!!BBD!!'')  " r%   N)r   r    r^   run_in_executor)r.   r   r   r   s   `` @r#   r    TelnetConnection._handle_command   s2    
 	'1	5
	# 	##K0r%   c                     U R                   R                  5         U R                   R                  SS5        U R                   R                  5         g)z
Erase output screen.
r   N)ra   erase_screencursor_gotor6   rS   s    r#   r   TelnetConnection.erase_screen   s>     	&&(%%a+!r%   c                     [        U[        5      (       d   eU R                  R                  UR	                  SS5      5        U R                  R                  5         g)z
Send text to the client.

z
N)r3   r   r`   r9   replacer6   r7   s     r#   r!   TelnetConnection.send   sF     $	****
 	$,,tV45r%   c                 |    U R                   R                  U 5        U R                  R                  5         SU l        g)z
Close the connection.
TN)r[   client_leavingrY   r   r\   rS   s    r#   r   TelnetConnection.close  s-     	''-		r%   )rZ   r[   r_   rf   r\   rY   r/   rc   r]   r   r^   rR   r`   ra   r*   )rA   rB   rC   rD   rE   r0   rd   rn   r   r   r!   r   rF   rG   r%   r#   rI   rI   _   s-    &P.I`/01:"
r%   rI   c                   F    \ rS rSrSrS rS rS rS rSS jr	S	 r
S
 rSrg)rb   i  z<
Eventloop object to be assigned to `CommandLineInterface`.
c                     Xl         g r*   _server)r.   r^   s     r#   r0   "_TelnetEventLoopInterface.__init__  s    r%   c                     gzIgnore. NrG   rS   s    r#   r   _TelnetEventLoopInterface.close      r%   c                     gr   rG   rS   s    r#   stop_TelnetEventLoopInterface.stop  r   r%   c                 :    U R                   R                  U5        g r*   )r   r   r.   r_   s     r#   r   )_TelnetEventLoopInterface.run_in_executor  s    $$X.r%   Nc                 :    U R                   R                  U5        g r*   )r   r   )r.   r_   _max_postpone_untils      r#   r   ,_TelnetEventLoopInterface.call_from_executor  s    ''1r%   c                     [         er*   NotImplementedError)r.   fdr_   s      r#   
add_reader$_TelnetEventLoopInterface.add_reader!      !!r%   c                     [         er*   r   )r.   r   s     r#   remove_reader'_TelnetEventLoopInterface.remove_reader$  r   r%   r   r*   )rA   rB   rC   rD   rE   r0   r   r   r   r   r   r   rF   rG   r%   r#   rb   rb     s*    /2""r%   rb   c                   V    \ rS rSrSrSS jr\S 5       rS rS r	S r
S	 rS
 rS rSrg)r   i(  z
Telnet server implementation.
Nc                    [        U[        5      (       d   e[        U[        5      (       d   e[        U[        5      (       d   e[        U[        5      (       d   eXl        X l        X0l        X@l        [        5       U l	        / U l
        [        R                  " 5       U l        [        R                  " U R                  S   [        R                  [        R                   5        g )Nr   )r3   r   intr   hostportr[   r/   setconnections_calls_from_executorospipe_schedule_pipefcntlF_SETFL
O_NONBLOCK)r.   r   r   r[   r/   s        r#   r0   TelnetServer.__init__,  s    $	****$$$$$+'89999(I....		& 5$&! !ggiD''*EMM2==Ir%   c                    [         R                   " [         R                  [         R                  5      nUR                  [         R                  [         R
                  S5        UR                  X45        UR                  S5        U$ )Nr      )r=   AF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRbindlisten)clsr   r   ss       r#   create_socketTelnetServer.create_socket?  sX     MM&..&*<*<=	V&&(;(;Q?	|	r%   c                 H    [         R                  " US9R                  5         g )N)target)	threadingThreadstartr   s     r#   r   TelnetServer.run_in_executorI  s    )//1r%   c                     U R                   R                  U5        U R                  (       a%  [        R                  " U R                  S   S5        g g )Nr      x)r   r4   r   r   r9   r   s     r#   r   TelnetServer.call_from_executorL  s=    !!((2HHT((+T2 r%   c                     [         R                  " U R                  S   S5        U R                  / sol        U H
  nU" 5         M     g)z;
Process callbacks from `call_from_executor` in eventloop.
r      N)r   readr   r   )r.   calls_from_executorcs      r#   _process_callbacksTelnetServer._process_callbacksR  sC    
 	##A&- :>9R9RTV66$AC %r%   c                     U R                  U R                  U R                  5      n[        R                  " SU R                  U R                  5          [        U R                   Vs/ s H  o"R                  (       a  M  UPM     sn5      U l        [        U R                   Vs/ s H  o"R                  (       a  M  UPM     sn5      nXR                  S   /U Vs/ s H  o"R                  PM     sn-   n[        R                  " U/ / 5      u  n  nU HP  nXq:X  a  U R                  U5        M  XpR                  S   :X  a  U R                  5         M?  U R                  U5        MR     GM  s  snf s  snf s  snf ! UR                  5         f = f)z*
Run the eventloop for the telnet server.
z.Listening for telnet connections on %s port %rr   )r   r   r   r   r    r   r   r\   r]   r   rY   select_acceptr   _handle_incoming_datar   )r.   listen_socketr   r   	read_listr   _r   s           r#   runTelnetServer.run^  sL    **499dii@DdiiQUQZQZ[	"#&43C3C'T3Ca883C'T#U  "d.>.>"Y.>FXFX1.>"YZ #$7$7$:;%01[VV[12  $]]9b"=
aA)]3 11!44//1 2215  'T #Z
 2$ !s=   E; (E, E,E; %E1=E1E; E62B	E; ;Fc                     UR                  5       u  p#[        X#U R                  X R                  S9nU R                  R                  U5        [        R                  " S/UQ76   g)z!
Accept new incoming connection.
rV   zNew connection %r %rN)acceptrI   r[   r/   r   addr   r    )r.   r   rY   rZ   r"   s        r#   r   TelnetServer._accept  sT     #))+
%d$2B2BDS`S`a
Z(*2T2r%   c                     U R                    Vs/ s H  o"R                  U:X  d  M  UPM     snS   nUR                  S5      nU(       a  UR                  U5        gU R                   R	                  U5        gs  snf )z!
Handle incoming data on socket.
r   r   N)r   rY   recvrn   remove)r.   rY   r   r"   r8   s        r#   r   "TelnetServer._handle_incoming_data  sc     "&!1!1D!1AVVt^a!1DQG
yyOOD!##J/ Es
   A8A8)r   r   r[   r   r/   r   r   )z	127.0.0.1   Nzutf-8)rA   rB   rC   rD   rE   r0   classmethodr   r   r   r   r   r   r   rF   rG   r%   r#   r   r   (  s@    J&  23
$"L3	0r%   r   )4rE   
__future__r   r=   r   r   r   r   sixr   r   r   codecsr   prompt_toolkit.enumsr   prompt_toolkit.eventloop.baser	   prompt_toolkit.interfacer
   r   prompt_toolkit.layout.screenr   prompt_toolkit.shortcutsr   #prompt_toolkit.terminal.vt100_inputr   $prompt_toolkit.terminal.vt100_outputr   logr   protocolr   r   r   r   r   r   r   r   r   r   r   r[   r   __all__r$   objectr'   rI   rb   r   rG   r%   r#   <module>r     s   ( (    	  0 0 ( / 3 F - > ; =  Z Z Z * *
%( 0lv l^"	 "4o06 o0r%   