<s> import time , os <EOL> import traceback <EOL> try : <EOL> from LegacyBans import BanHandler <EOL> except : <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> print traceback . format_exc ( ) <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> print '<STR_LIT>' <EOL> BanHandler = None <EOL> def dec2bin ( i , bits = None ) : <EOL> i = int ( i ) <EOL> b = '<STR_LIT>' <EOL> while i > <NUM_LIT:0> : <EOL> j = i & <NUM_LIT:1> <EOL> b = str ( j ) + b <EOL> i >>= <NUM_LIT:1> <EOL> if bits : <EOL> b = b . rjust ( bits , '<STR_LIT:0>' ) <EOL> return b <EOL> class User ( object ) : <EOL> levels = { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:user>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> @ classmethod <EOL> def fromAccountLine ( cls , line ) : <EOL> line = line . split ( ) <EOL> if len ( line ) < <NUM_LIT:8> : return <EOL> try : <EOL> username = line [ <NUM_LIT:0> ] <EOL> password = line [ <NUM_LIT:1> ] <EOL> access = line [ <NUM_LIT:2> ] <EOL> uid = line [ <NUM_LIT:3> ] <EOL> last_login = int ( line [ <NUM_LIT:4> ] ) <EOL> last_ip = line [ <NUM_LIT:5> ] <EOL> register_date = int ( line [ <NUM_LIT:6> ] ) <EOL> country = line [ <NUM_LIT:7> ] <EOL> account_id = line [ <NUM_LIT:8> ] <EOL> permissions = int ( ( access [ - <NUM_LIT:3> : ] or '<STR_LIT:0>' ) , <NUM_LIT:2> ) <EOL> ingame_time = int ( ( access [ - <NUM_LIT> : - <NUM_LIT:3> ] or '<STR_LIT:0>' ) , <NUM_LIT:2> ) <EOL> agreement = ( len ( access ) >= <NUM_LIT> and access [ - <NUM_LIT> ] == '<STR_LIT:1>' ) <EOL> bot = ( len ( access ) >= <NUM_LIT> and access [ - <NUM_LIT> ] == '<STR_LIT:1>' ) <EOL> if permissions and not agreement : access = '<STR_LIT>' <EOL> elif permissions == <NUM_LIT:3> : access = '<STR_LIT>' <EOL> elif permissions == <NUM_LIT:2> : access = '<STR_LIT>' <EOL> elif permissions == <NUM_LIT:1> : access = '<STR_LIT:user>' <EOL> else : access = '<STR_LIT>' <EOL> return cls ( username , password , ingame_time , bot , access , uid , last_login , last_ip , register_date , country , account_id ) <EOL> except : <EOL> print '<STR_LIT>' , line <EOL> def __init__ ( self , username , password , ingame_time , bot , access , uid , last_login , last_ip , register_date , country , account_id ) : <EOL> self . lowername = username . lower ( ) <EOL> self . username = username <EOL> self . password = password <EOL> self . ingame_time = ingame_time <EOL> self . bot = bot <EOL> self . access = access <EOL> self . last_id = uid <EOL> self . last_login = last_login <EOL> self . last_ip = last_ip <EOL> self . register_date = register_date <EOL> self . country = country <EOL> self . id = account_id <EOL> self . hook_chars = '<STR_LIT>' <EOL> def getAccess ( self ) : <EOL> if self . access in self . levels : <EOL> access = self . levels [ self . access ] <EOL> else : access = <NUM_LIT:0> <EOL> agreement = ( '<STR_LIT:0>' if ( self . access in ( '<STR_LIT>' , '<STR_LIT>' ) ) else '<STR_LIT:1>' ) <EOL> bot = ( '<STR_LIT:1>' if self . bot else '<STR_LIT:0>' ) <EOL> return ( bot + agreement + dec2bin ( self . ingame_time , <NUM_LIT:20> ) + dec2bin ( access , <NUM_LIT:3> ) ) . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' <EOL> def toAccountLine ( self ) : <EOL> return '<STR_LIT:U+0020>' . join ( ( self . username , self . password , self . getAccess ( ) , str ( self . last_id ) , str ( int ( self . last_login ) ) , self . last_ip , str ( int ( self . register_date ) ) , self . country , str ( self . id ) ) ) <EOL> class UsersHandler : <EOL> def __init__ ( self , root , accountstxt ) : <EOL> self . _root = root <EOL> self . accountstxt = accountstxt <EOL> if root . tsbanurl and BanHandler : <EOL> self . bandb = BanHandler ( root , root . tsbanurl ) <EOL> else : self . bandb = None <EOL> self . accounts = { } <EOL> self . idToAccount = { } <EOL> self . lock = None <EOL> self . last_id = <NUM_LIT:0> <EOL> def readAccounts ( self ) : <EOL> f = open ( self . accountstxt , '<STR_LIT:r>' ) <EOL> last_id = <NUM_LIT:0> <EOL> line = f . readline ( ) . rstrip ( ) <EOL> while line : <EOL> user = User . fromAccountLine ( line ) <EOL> if user : <EOL> self . accounts [ user . lowername ] = user <EOL> last_id = max ( last_id , int ( user . id ) ) <EOL> self . idToAccount [ user . id ] = user <EOL> line = f . readline ( ) . rstrip ( ) <EOL> self . last_id = last_id <EOL> f . close ( ) <EOL> def writeAccounts ( self ) : <EOL> tmpname = self . accountstxt + '<STR_LIT>' <EOL> f = open ( tmpname , '<STR_LIT:w>' ) <EOL> for user in self . accounts . values ( ) : <EOL> f . write ( user . toAccountLine ( ) + '<STR_LIT:\n>' ) <EOL> f . close ( ) <EOL> if os . path . exists ( self . accountstxt ) and os . path . exists ( tmpname ) : <EOL> os . remove ( self . accountstxt ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> os . rename ( tmpname , self . accountstxt ) <EOL> def clientFromID ( self , db_id ) : <EOL> if db_id in self . idToAccount : <EOL> return self . idToAccount [ db_id ] <EOL> def clientFromUsername ( self , username ) : <EOL> name = username . lower ( ) <EOL> if name in self . accounts : <EOL> return self . accounts [ name ] <EOL> def check_ban ( self , username = None , ip = None , userid = None ) : <EOL> if self . bandb : <EOL> return self . bandb . check_ban ( username , ip , userid ) <EOL> else : return True , None <EOL> def login_user ( self , username , password , ip , lobby_id , user_id , cpu , local_ip , country ) : <EOL> name = username . lower ( ) <EOL> lanadmin = self . _root . lanadmin <EOL> now = int ( time . time ( ) ) <EOL> if name == lanadmin [ '<STR_LIT:username>' ] . lower ( ) and password == lanadmin [ '<STR_LIT:password>' ] : <EOL> user = User ( lanadmin [ '<STR_LIT:username>' ] , password , <NUM_LIT:0> , False , '<STR_LIT>' , None , now , ip , now , country , <NUM_LIT:0> ) <EOL> return True , user <EOL> elif name == lanadmin [ '<STR_LIT:username>' ] . lower ( ) : <EOL> return False , '<STR_LIT>' <EOL> user = self . clientFromUsername ( name ) <EOL> if not user : <EOL> return False , '<STR_LIT>' <EOL> good , reason = self . check_ban ( user . username , ip , user_id ) <EOL> if not good : <EOL> return False , '<STR_LIT>' % reason <EOL> if not password == user . password : <EOL> return False , '<STR_LIT>' <EOL> user . last_login = now <EOL> user . last_ip = ip <EOL> user . last_id = user_id <EOL> return True , user <EOL> def end_session ( self , db_id ) : pass <EOL> def register_user ( self , username , password , ip , country ) : <EOL> good , reason = self . check_ban ( ip = ip ) <EOL> if not good : return False , '<STR_LIT>' % reason <EOL> if len ( username ) > <NUM_LIT:20> : return False , '<STR_LIT>' <EOL> if self . _root . censor : <EOL> if not self . _root . SayHooks . _nasty_word_censor ( username ) : <EOL> return False , '<STR_LIT>' <EOL> name = username . lower ( ) <EOL> lanadmin = self . _root . lanadmin <EOL> if name == lanadmin [ '<STR_LIT:username>' ] . lower ( ) or name in self . accounts : <EOL> return False , '<STR_LIT>' <EOL> now = int ( time . time ( ) ) <EOL> user = User ( username , password , <NUM_LIT:0> , False , '<STR_LIT>' , None , now , ip , now , country , self . last_id + <NUM_LIT:1> ) <EOL> self . last_id += <NUM_LIT:1> <EOL> self . accounts [ name ] = user <EOL> self . idToAccount [ user . id ] = user <EOL> return True , '<STR_LIT>' <EOL> def ban_user ( self , owner , username , duration , reason ) : <EOL> if self . bandb : <EOL> user = self . clientFromUsername ( username ) <EOL> client_id = None <EOL> if user : <EOL> client_id = user . last_id <EOL> self . bandb . ban_user ( owner , username , client_id , duration , reason ) <EOL> def unban_user ( self , username ) : <EOL> if self . bandb : <EOL> self . bandb . unban_user ( username ) <EOL> def ban_ip ( self , owner , ip , duration , reason ) : <EOL> if self . bandb : <EOL> self . bandb . ban_ip ( owner , ip , duration , reason ) <EOL> def unban_ip ( self , ip ) : <EOL> if self . bandb : <EOL> self . bandb . unban_ip ( ip ) <EOL> def banlist ( self ) : <EOL> if self . bandb : <EOL> return self . bandb . banlist ( ) <EOL> def rename_user ( self , username , newname ) : <EOL> if self . _root . censor and not self . _root . SayHooks . _nasty_word_censor ( newname ) : <EOL> return False , '<STR_LIT>' <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> name = newname . lower ( ) <EOL> old = username . lower ( ) <EOL> if name != old and name in self . accounts : <EOL> return False , '<STR_LIT>' <EOL> else : <EOL> user . lowername = name <EOL> user . username = newname <EOL> self . accounts [ name ] = user <EOL> if name != old : <EOL> del self . accounts [ old ] <EOL> return True , '<STR_LIT>' <EOL> def change_password ( self , username , oldpass , newpass ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> if user . password == oldpass : <EOL> user . password = newpass <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def save_user ( self , client ) : <EOL> user = self . clientFromUsername ( client . username ) <EOL> if user : <EOL> user . ingame_time = client . ingame_time <EOL> user . access = client . access <EOL> user . bot = client . bot <EOL> def confirm_agreement ( self , client ) : <EOL> user = self . clientFromUsername ( client . username ) <EOL> if user : <EOL> self . accounts [ user . lowername ] . access = '<STR_LIT:user>' <EOL> def get_lastlogin ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return True , user . last_login <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def get_registration_date ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return True , user . register_date <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def get_ingame_time ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return True , user . ingame_time <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def get_account_info ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return True , user . toAccountLine ( ) <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def get_account_access ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return True , '<STR_LIT>' % ( int ( user . getAccess ( ) , <NUM_LIT:2> ) , user . access ) <EOL> else : <EOL> return False , '<STR_LIT>' <EOL> def find_ip ( self , ip ) : <EOL> users = [ ] <EOL> if '<STR_LIT::>' in ip : <EOL> return [ '<STR_LIT>' ] <EOL> else : <EOL> ip = ip . split ( '<STR_LIT:.>' , <NUM_LIT:3> ) <EOL> for user in self . accounts . values ( ) : <EOL> user_ip = user . last_ip . split ( '<STR_LIT:.>' , <NUM_LIT:3> ) <EOL> for i in xrange ( len ( ip ) ) : <EOL> if ip [ i ] != '<STR_LIT:*>' and not ip [ i ] == user_ip [ i ] : <EOL> break <EOL> else : <EOL> users . append ( user ) <EOL> return users <EOL> def get_ip ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> return user . last_ip <EOL> def remove_user ( self , username ) : <EOL> user = self . clientFromUsername ( username ) <EOL> if user : <EOL> del self . accounts [ user ] <EOL> if user . id : <EOL> del self . idToAccount [ user . id ] </s>
