mò
ÄµDc           @   sœ   d  k  Z  d  k Z d  k Z d  k Z d  k Z d  k Z d  k Z d „  Z d d „ Z d d „ Z	 d f  d „  ƒ  YZ
 d  d „ Z d e i f d	 „  ƒ  YZ d  S(
   Nc         C   s   t  i d d |  ƒ S(   Ns   ([\r\n\\\"])s   \\\1(   t   ret   subt   s(   R   (    (    t,   /home/brian/downloads/tor/blossom/TorCtl1.pyt   _quote   s    i   c         C   s«   | o t i d |  ƒ } n |  i d ƒ } | o | d o | d =n xD t t | ƒ ƒ D]0 } | | i d ƒ o d | | | | <q] q] W| i	 d ƒ d i
 | ƒ S(   Ns   \r?\ns   
iÿÿÿÿt   .s   .
(   t   translate_nlR    t   splitR   t   linest   xranget   lent   it
   startswitht   appendt   join(   R   R   R   R   (    (    R   t   _escape_dots   s     c         C   s¢   |  i d ƒ } xD t t | ƒ ƒ D]0 } | | i d ƒ o | | d | | <q" q" W| o | d o | i d ƒ n | o d i	 | ƒ Sn d i	 | ƒ Sd  S(   Ns   
R   i   iÿÿÿÿt    s   
(
   R   R   R   R	   R
   R   R   R   R   R   (   R   R   R   R   (    (    R   t   _unescape_dots   s     t   _BufSockc           B   s,   t  Z d „  Z d „  Z d „  Z d „  Z RS(   Nc         C   s   | |  _ g  |  _ d  S(   N(   R   t   selft   _st   _buf(   R   R   (    (    R   t   __init__.   s    	c         C   s)  |  i o` |  i d i d ƒ } | d j o9 |  i d | d  } |  i d | d |  i d <| Sqj n x¸ |  i i d ƒ } | p t i	 ƒ  ‚ n | i d ƒ } | d j o^ |  i i
 | | d  ƒ d i |  i ƒ } | | d } | o | g |  _ n |  i 2| Sqm |  i i
 | ƒ qm Wd  S(   Ni    s   
i   i€   R   (   R   R   t   findt   idxt   resultR   t   recvR   t   TorCtlt   TorCtlClosedR   R   t   rest(   R   R   R   R   R   (    (    R   t   readline2   s*    
  c         C   s   |  i i | ƒ d  S(   N(   R   R   t   sendR   (   R   R   (    (    R   t   writeK   s    c         C   s   |  i i ƒ  d  S(   N(   R   R   t   close(   R   (    (    R   R!   N   s    (   t   __name__t
   __module__R   R   R    R!   (    (    (    R   R   -   s   			c         C   s  g  } xt|  i ƒ  i ƒ  } | o | i d | ƒ n t | ƒ d j  o t i	 d ƒ ‚ n | d  } | d } | d } | d j o | i | | d  f ƒ q	 | d j o | i | | d  f ƒ | Sq	 | d j o t i	 d	 | ƒ ‚ q	 g  } xY |  i ƒ  } | o" | d j o | i d
 | ƒ n | d j o Pn | i | ƒ qú W| i | | t d i | ƒ ƒ f ƒ q	 Wd  S(   Ni   s       %s
i   s%   Badly formatted reply line: Too shorti   t   -t    t   +s+   Badly formatted reply line: unknown type %rs       %ss   .
s   .
R   (   s   .
s   .
(   R   t   fR   t   stript   linet	   debugFileR    R
   R   t   ProtocolErrort   codet   tpR   R   t   Nonet   moreR   R   (   R'   R*   R,   R   R   R-   R)   R/   (    (    R   t   _read_replyQ   s8      


  t
   Connectionc           B   sì   t  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d	 „ Z d d
 „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d f  d „ Z d f  d „ Z d „  Z RS(   s8   A Connection represents a connection to the Tor process.c         C   s,   t  i i |  ƒ t | ƒ |  _ d |  _ d S(   sc   Create a Connection to communicate with the Tor process over the
           socket 'sock'.
        N(	   R   t   _ConnectionBaseR   R   R   t   sockR   R.   t
   _debugFile(   R   R3   (    (    R   R   p   s     c         C   s   | |  _ d S(   t   DOCDOCN(   R'   R   R4   (   R   R'   (    (    R   t   debugx   s     c         C   s   | |  _ | i |  _ d S(   sJ   Cause future events from the Tor process to be sent to 'handler'.
        N(   t   handlerR   t   _handlert   handle1t	   _handleFn(   R   R7   (    (    R   t   set_event_handler|   s     	c         C   s>   t  |  i |  i ƒ } | o | d d d d j } | | f S(   Ni    t   6(   R0   R   R   R4   R   t   isEvent(   R   R   R=   (    (    R   R0   ‚   s    c         C   sy   |  i o[ | } | i d ƒ } t | ƒ d j o d i | d  ƒ d } n |  i i d | ƒ n |  i	 i | ƒ d  S(   Ns   
i   s   >>> %s(
   R   R4   t   msgt   amsgR   R   R
   R   R    R   (   R   R>   R?   R   (    (    R   t   _doSend‡   s    
R   t   250t   251c         C   sÅ   t  | ƒ t i j o d i | ƒ } n | i d ƒ p t ‚ |  i |  i	 | ƒ } xi | D]a \ } } } | d d j o t i d | | f ƒ ‚ n | | j o t i d | ƒ ‚ q\ q\ W| S(   s  Helper: Send a command 'msg' to Tor, and wait for a command
           in response.  If the response type is in expectedTypes,
           return a list of (tp,body,extra) tuples.  If it is an
           error, raise ErrorReply.  Otherwise, raise TorCtl.ProtocolError.
        R   s   
i    t   45s   %s %ss   Unexpectd message type %rN(   t   typeR>   t   typest   ListTypeR   t   endswitht   AssertionErrorR   t	   _sendImplR@   R   R-   t   _R   t
   ErrorReplyt   expectedTypesR+   (   R   R>   RL   R   R-   RJ   (    (    R   t   _sendAndRecv   s      c         C   s$   t  i | ƒ } |  i d | ƒ d S(   sp   Send an authenticating secret to Tor.  You'll need to call this
           method before Tor can start.
        s   AUTHENTICATE %s
N(   t   binasciit   b2a_hext   secrett   hexstrR   RM   (   R   RP   RQ   (    (    R   t   authenticate¤   s     c         C   s²   t  | t ƒ p d i | ƒ } n |  i d | ƒ } g  } xo | D]g \ } } } y/ | i
 d d ƒ \ } } | i | | f ƒ WqC t j
 o | i | d f ƒ qC XqC W| S(   s  Get the value of the configuration option named 'name'.  To
           retrieve multiple values, pass a list for 'name' instead of
           a string.  Returns a list of (key,value) pairs.
           Refer to section 3.3 of control-spec.txt for a list of valid names.
        R%   s   GETCONF %s
t   =i   N(   t
   isinstancet   namet   strR   R   RM   R   t   rRJ   R)   R   t   keyt   valR   t
   ValueErrorR.   (   R   RU   RX   RY   R   RW   R)   RJ   (    (    R   t
   get_option«   s      c         C   s   |  i | | f g ƒ d S(   sN   Set the value of the configuration option 'key' to the value 'value'.
        N(   R   t   set_optionsRX   t   value(   R   RX   R]   (    (    R   t
   set_option¿   s     c         C   sd   | p d Sn d i g  } | D]# \ } } | d | t | ƒ f q  ~ ƒ } |  i d | ƒ d S(   sY   Given a list of (key,value) pairs, set them as configuration
           options.
        NR%   s   %s=%ss   SETCONF %s
(	   t   kvlistR   t   _[1]t   kt   vR   R>   R   RM   (   R   R_   R`   Rb   R>   Ra   (    (    R   R\   Ä   s
     @c         C   s   |  i d d i | ƒ ƒ d S(   sý   Reset the options listed in 'keylist' to their default values.

           Tor started implementing this command in version 0.1.1.7-alpha;
           previous versions wanted you to set configuration keys to "".
           That no longer works.
        s   RESETCONF %s
R%   N(   R   RM   R   t   keylist(   R   Rc   (    (    R   t   reset_optionsÍ   s     c   	      C   sÏ   t  | t ƒ p d i | ƒ } n |  i d | ƒ } h  } xŒ | D]„ \ } } } | d j o Pn y | i d d ƒ \ } } Wn% t j
 o t i d | ƒ ‚ n X| o | | | <qC | | | <qC W| S(   sª   Return the value of the internal information field named 'name'.
           Refer to section 3.9 of control-spec.txt for a list of valid names.
           DOCDOC
        R%   s   GETINFO %s
t   OKRS   i   s   Bad info line %rN(   RT   RU   RV   R   R   RM   R   t   dRJ   R>   R/   R   Ra   R   RZ   R   R+   (	   R   RU   R   R   R/   R>   Ra   RJ   Rf   (    (    R   t   get_infoÖ   s"      c         C   sÉ  g  } x¢| D]š} | d j p | d j o
 d } nd| d j p | d j o
 d } n@| d j p | d j o
 d	 } n| d
 j p | d j o
 d } nø | d j p | d j o t nÖ | d j p | d j o
 d } n² | d j p | d j o q n‘ | d j p | d j o
 d } nm | d j p | d j o
 d } nI | d j p | d j o
 d } n% | d j p | d j o
 d } n | i | ƒ q W|  i d d i | ƒ ƒ d S(   sÛ   Change the list of events that the event handler is interested
           in to those in 'events', which is a list of event names.
           Recognized event names are listed in section 3.3 of the control-spec
        i   t
   CIRCSTATUSt   CIRCi   t   STREAMSTATUSt   STREAMi   t   ORCONNSTATUSt   ORCONNi   t	   BANDWIDTHt   BWi   t   OBSOLETE_LOGi   t   NEWDESCi   t	   DEBUG_MSGi   t   INFO_MSGt   INFOt
   NOTICE_MSGt   NOTICEt   WARN_MSGt   WARNt   ERR_MSGt   ERRs   SETEVENTS %s
R%   N(   t   evst   eventst   et   coneinueR   R   RM   R   (   R   R|   R{   R}   (    (    R   t
   set_eventsì   s8      








c         C   s   |  i d ƒ d S(   s1   Flush all configuration changes to disk.
        s
   SAVECONF
N(   R   RM   (   R   (    (    R   t	   save_conf  s     c         C   sT   h  d d <d d <d d <d d <d	 d
 <i  | | ƒ } |  i d | ƒ d S(   s…   Send the signal 'sig' to the Tor process; The allowed values for
           'sig' are listed in section 3.6 of control-spec.
        i   t   HUPi   t   INTi
   t   USR1i   t   USR2i   t   TERMs   SIGNAL %s
N(   t   gett   sigR   RM   (   R   R‡   (    (    R   t   send_signal  s     ?c         C   sÍ   | p d  Sn d i g  } | D] \ } }	 | d q  ~ ƒ } |  i d | ƒ } g  } xn | D]f \ } }
 } y |
 i d d ƒ \ } } Wn% t j
 o t i d |	 ƒ ‚ n X| i | | f ƒ q_ W| S(   NR%   s   %s=%ss   MAPADDRESS %s
RS   i   s   Bad address line %r(   t   kvListR   R`   Ra   Rb   t   mR   RM   R   RW   RJ   R)   R   RX   RY   RZ   R   R+   R   (   R   R‰   RY   R   Ra   RŠ   R`   RW   RX   Rb   R)   RJ   (    (    R   t   map_address  s    0 c         C   sˆ   | d j o
 d } n |  i d | d i | ƒ f ƒ } | d \ } } } t
 i d | ƒ } | p t i d | ƒ ‚ n | i d ƒ S(	   sw   Tell Tor to extend the circuit identified by 'circid' through the
           servers named in the list 'hops'.
        t   0s   EXTENDCIRCUIT %s %s
t   ,i    s   EXTENDED (\S*)s   Bad extended line %ri   N(   t   circidR.   R   RM   R   t   hopsR   R-   R>   RJ   R    t   matchRŠ   R   R+   t   group(   R   RŽ   R   RŠ   R   R-   R>   RJ   (    (    R   t   extend_circuit-  s     
"c         C   s@   | o |  i d | | | f ƒ n |  i d | | f ƒ d S(   R5   s   REDIRECTSTREAM %s %s %s
s   REDIRECTSTREAM %s %s
N(   t   newportR   RM   t   streamidt   newaddr(   R   R”   R•   R“   (    (    R   t   redirect_stream;  s     c         C   s   |  i d | | f ƒ d S(   R5   s   ATTACHSTREAM %s %s
N(   R   RM   R”   RŽ   (   R   R”   RŽ   (    (    R   t   attach_streamB  s     i    c         C   s'   |  i d | | d i | ƒ f ƒ d S(   R5   s   CLOSESTREAM %s %s %s
R   N(   R   RM   R”   t   reasonR   t   flags(   R   R”   R˜   R™   (    (    R   t   close_streamF  s     c         C   s'   |  i d | | d i | ƒ f ƒ d S(   R5   s   CLOSECIRCUIT %s %s %s
R   N(   R   RM   RŽ   R˜   R   R™   (   R   RŽ   R˜   R™   (    (    R   t   close_circuitK  s     c         C   s   |  i d t | ƒ ƒ d  S(   Ns   +POSTDESCRIPTOR
%s(   R   RM   R   t   desc(   R   Rœ   (    (    R   t   post_descriptorP  s    (   RA   RB   (   R"   R#   t   __doc__R   R6   R;   R0   R@   RM   RR   R[   R^   R\   Rd   Rg   R   R€   Rˆ   R‹   R’   R–   R—   Rš   R›   R   (    (    (    R   R1   n   s.    														#					(   RN   t   osR    t   sockett   sysRE   R   R   R   R   R   R.   R0   R2   R1   (   R   R   R    R   R   RN   R¡   R    R1   R   RŸ   RE   R0   (    (    R   t   ?   s   								$