mς
EδFc           @   s­   d  Z  d Z d k Z d k Z d k Z d k Z h  d h  d d <<d h  d d <<Z d   Z d	 f  d
     YZ d   Z	 e
 d j o z e	   Wd e i   Xn d S(   s  
Script to log the robot in to a wiki account.

Suggestion is to make a special account to use for robot use only. Make
sure this robot account is well known on your home wikipedia before using.

Parameters:

   -all         Try to log in on all sites where a username is defined in
                user-config.py.

   -pass        Useful in combination with -all when you have accounts for
                several sites and use the same password for all of them.
                Asks you for the password, then logs in on all given sites.

   -pass:XXXX   Uses XXXX as password. Be careful if you use this
                parameter because your password will be shown on your
                screen, and will probably be saved in your command line
                history. This is NOT RECOMMENDED for use on computers
                where others have either physical or remote access.
                Use -pass instead.

   -sysop       Log in with your sysop account.

   -force       Ignores if the user is already logged in, and tries to log in.

If not given as parameter, the script will ask for your username and password
(password entry will be hidden), log in to your home wiki using this
combination, and store the resulting cookies (containing your password hash,
so keep it secured!) in a file in the login-data subdirectory.

All scripts in this library will be looking for this cookie file and will use the
login information if it is present.

To log out, throw away the XX-login.data file that is created in the login-data
subdirectory.
s4   $Id: login.py 4044 2007-08-15 12:56:24Z wikipedian $Nt	   wikipediat   enu   Wikipedia:Registered botst   gentoou	   Help:Botsc         C   si   d k  l } d k l } l } l } l } | | |    } | |  p | |  n | | |    S(   s³   creates missing directories for the given path and
        returns a normalized absolute version of the path.

    - if the given path already exists in the filesystem
      the filesystem is not modified.

    - otherwise makepath creates directories along the given path
      using the dirname() of the path. You may append
      a '/' to the path if you want it to be a directory path.

    from holger@trillke.net 2002/03/18
    (   s   makedirs(   s   normpaths   dirnames   existss   abspathN(	   t   ost   makedirst   os.patht   normpatht   dirnamet   existst   abspatht   patht   dpath(   R
   R   R   R   R   R   R	   (    (    t9   /home/cat/public_html/arm/mrobe/wiki/pywikipedia/login.pyt   makepathA   s      t   LoginManagerc           B   sM   t  Z d  e d  d  Z d   Z e d  Z d   Z d   Z	 e d  Z
 RS(   Nc         C   s+  | p
 t i   |  _  | or y' t i |  i  i i |  i  i	 |  _
 Wqύ t i d |  i  i i |  i  i	 |  i  i i |  i  i	 f   qύ Xno y' t i |  i  i i |  i  i	 |  _
 WnD t i d |  i  i i |  i  i	 |  i  i i |  i  i	 f   n X| |  _ t t d d  o |  i   n d  S(   Nu©   ERROR: Sysop username for %s:%s is undefined.
If you have a sysop account for that site, please add such a line to user-config.py:

sysopnames['%s']['%s'] = 'myUsername'u   ERROR: Username for %s:%s is undefined.
If you have an account for that site, please add such a line to user-config.py:

usernames['%s']['%s'] = 'myUsername't   password_filet    (   t   siteR    t   getSitet   selft   sysopt   configt
   sysopnamest   familyt   namet   langt   usernamet
   NoUsernamet	   usernamest   passwordt   getattrt   readPassword(   R   R   R   R   (    (    R   t   __init__V   s    'E'A	c         C   sΉ   t  i |  i i i  o t  |  i i i i |  i i    or t  |  i i i |  i i   } t i	 |  i |  } x2 | i   D]$ } | i   |  i j o t Sq q Wt Sn t Sd S(   s   
        Checks whether the bot is listed on a specific page to comply with
        the policy on the respective wiki.
        N(   t   botListt   has_keyR   R   R   R   t   languaget   botListPageTitleR    t   Paget   botListPaget   linkedPagest
   linkedPaget   titleWithoutNamespaceR   t   Truet   False(   R   R$   R(   R&   (    (    R   t
   botAllowedf   s     ?  c         C   s  h  d |  i i |  i i    <d |  i <d d <d t t t |    <} |  i i   } |  i i   t i i   j o h  d d <d t i <} |  i i |  } t i t i |  i i   d	 |  i i   | | |   } | i   } t i i t i  d
 Sn|  i i | | d t  \ } } d } t" i# d  } g  } xT | i& i' d  D]@ } | i) |  }	 |	 o$ | d 7} | i+ |	 i, d   qaqaWg  }
 x | D] } t" i/ d |  o |
 i+ |  q²t" i/ d |  o |
 i+ |  q²t" i/ d |  o |
 i+ |  q²t" i/ d |  o |
 i+ |  q²q²Wt0 |
  d j o d i1 |  Sn# d | j o t i2 d  n d Sd S(   s   Login to wikipedia.
    
        remember    Remember login (default: True)
        
        Returns cookie data if succesful, None otherwise.t   wpNamet
   wpPasswordt   wpLoginattempts   Aanmelden & Inschrijvent
   wpRemembers   Content-types!   application/x-www-form-urlencodeds
   User-agents   ://t   Okt	   useCookiei    s   : (.*?);s
   set-cookiei   s   .*_session=.*s   .*UserID=.*s   .*UserName=.*s
   .*Token=.*i   s   
s'   <input type="hidden" name="wpCaptchaId"u   There is a CAPTCHA in the server's response.
Please wait a while or log in using a graphical web browser to reset the request and try again.N(4   R   R   t   encodeR   t   encodingR   t   strt   intt   boolt   remembert   predatat   login_addresst   addresst   hostnameR   t   authenticatet   keysR    t	   useragentt   headerst	   urlEncodet   datat   urllib2t   urlopent   Requestt   protocolt   responset   readt   cjt   savet
   COOKIEFILEt   postFormR+   t   nt   ret   compilet   Reatt   Lt   msgt   getallmatchingheaderst   eatt   searcht   mt   appendt   groupt   log_datat   Ldatat   matcht   lent   joint   outputt   None(   R   R8   RG   RP   RQ   R;   RB   RT   RZ   RV   RY   RM   R@   R9   (    (    R   t	   getCookiev   sH     T<! 
 c         C   sU   d |  i i i |  i i |  i f } t t |  d  } | i
 |  | i   d S(   s~   
        Stores cookie data.

        The argument data is the raw data, as returned by getCookie().

        Returns nothing.s   login-data/%s-%s-%s-login.datat   wN(   R   R   R   R   R   R   t   filenamet   openR   t   ft   writeRB   t   close(   R   RB   Rd   Rb   (    (    R   t   storecookiedata­   s
     %c         C   sχ   t  t i  } xΧ | D]Ο } | i   p q n t |  } t |  d j o) | d |  i
 j o | d |  _ qε q t |  d j oZ | d |  i i j o? | d |  i i i j o% | d |  i
 j o | d |  _ qε q q W| i   d S(   se  
            Reads passwords from a file. DO NOT FORGET TO REMOVE READ 
            ACCESS FOR OTHER USERS!!! Use chmod 600 password-file.
            All lines below should be valid Python tuples in the form 
            (code, family, username, password) or (username, password) 
            to set a default password for an username. Default usernames
            should occur above specific usernames.
		
            Example:
            
            ("my_username", "my_default_password")
            ("my_sysop_user", "my_sysop_password")
            ("en", "wikipedia", "my_en_user", "my_en_pass")
        i   i    i   i   i   N(   Rc   R   R   t   filet   linet   stript   evalt   entryR\   R   R   R   R   R   R   R   Rf   (   R   Rh   Rl   Ri   (    (    R   R   Ή   s        Ec         C   s  |  i p, t i d |  i |  i f d t |  _ n |  i i |  i i    |  _ t i	 d |  i |  i f  |  i
   p, t i	 d t |  i i i |  i i  n |  i   } | o" |  i |  t i	 d  t Sn6 t i	 d  | o d  |  _ |  i d t  Sn t Sd  S(   Nu   Password for user %s on %s:R   u   Logging in to %s as %suz   *** Your username is not listed on [[%s]].
*** Please make sure you are allowed to use the robot before actually using it!u   Should be logged in nowu   Login failed. Wrong password?t   retry(   R   R   R    t   inputR   R   R*   R3   R4   R^   R,   R!   R   R   R   R`   t
   cookiedataRg   Rm   R_   t   loginR+   (   R   Rm   Ro   (    (    R   Rp   Υ   s     
,,	(   t   __name__t
   __module__R_   R+   R    R,   R*   R`   Rg   R   Rp   (    (    (    R   R   U   s   	7		c          C   s½  d  }  }	 t } t } t } x΄ t i   D]¦ } | i
 d  o: t |  d j o t i d d t }	 qΟ | d }	 q) | d j o
 t } q) | d j o
 t } q) | d j o
 t } q) t i d	  d  Sq) W| oΓ | o t i } n
 t i } xΏ | i   D] } x | | i   D]v } t i d
 | d |  } | o. | i d |  d  j o t i d |  qt |	 d | d | }
 |
 i   qWqWn t |	 d | }
 |
 i   d  S(   Ns   -passi   u   Password for all accounts:R   i   s   -sysops   -alls   -forceRp   t   codet   famR   u   Already logged in on %sR   (   R_   R   R   R+   R   t   logallt
   forceLoginR    t
   handleArgst   argt
   startswithR\   Rn   R*   t   showHelpR   R   t   namedictR   t   iterkeyst
   familyNameR   R   R   t
   loggedInAsR^   R   t   loginManRp   (   R   R   R}   Rx   R   Ru   R   R{   Rv   R   R   (    (    R   t   mainξ   sB    
 


	  !t   __main__(   t   __doc__t   __version__RN   RC   R    R   R!   R   R   R   Rq   t   stopme(	   R   R   R   R    RC   RN   R!   R   R   (    (    R   t   ?'   s   		*		% 
