<s> import socket , time , sys , thread , ip2country , errno <EOL> import Telnet <EOL> from collections import defaultdict <EOL> class Client : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , root , connection , address , session_id ) : <EOL> '<STR_LIT>' <EOL> self . _root = root <EOL> self . conn = connection <EOL> if address [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> if root . online_ip : <EOL> address = ( root . online_ip , address [ <NUM_LIT:1> ] ) <EOL> elif root . local_ip : <EOL> address = ( root . local_ip , address [ <NUM_LIT:1> ] ) <EOL> self . ip_address = address [ <NUM_LIT:0> ] <EOL> self . local_ip = address [ <NUM_LIT:0> ] <EOL> self . port = address [ <NUM_LIT:1> ] <EOL> if root . randomflags : <EOL> self . country_code = ip2country . randomcc ( ) <EOL> else : <EOL> self . setFlagByIP ( self . ip_address ) <EOL> self . session_id = session_id <EOL> self . db_id = session_id <EOL> self . handler = None <EOL> self . static = False <EOL> self . _protocol = None <EOL> self . removing = False <EOL> self . sendError = False <EOL> self . msg_id = '<STR_LIT>' <EOL> self . sendbuffer = [ ] <EOL> self . sendingmessage = '<STR_LIT>' <EOL> self . logged_in = False <EOL> self . status = '<STR_LIT>' <EOL> self . cpu = <NUM_LIT:0> <EOL> self . access = '<STR_LIT>' <EOL> self . accesslevels = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . channels = [ ] <EOL> self . battle_bots = { } <EOL> self . current_battle = None <EOL> self . battle_bans = [ ] <EOL> self . ingame_time = <NUM_LIT:0> <EOL> self . went_ingame = <NUM_LIT:0> <EOL> self . spectator = False <EOL> self . battlestatus = { '<STR_LIT>' : '<STR_LIT:0>' , '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:0>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> self . teamcolor = '<STR_LIT:0>' <EOL> self . username = '<STR_LIT>' <EOL> self . password = '<STR_LIT>' <EOL> self . hostport = None <EOL> self . udpport = <NUM_LIT:0> <EOL> self . bot = <NUM_LIT:0> <EOL> self . hook = '<STR_LIT>' <EOL> self . floodlimit = { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:2> } , <EOL> '<STR_LIT:user>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:5> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : True } , } <EOL> self . msglengthhistory = { } <EOL> self . lastsaid = { } <EOL> self . nl = '<STR_LIT:\n>' <EOL> self . telnet = False <EOL> self . current_channel = '<STR_LIT>' <EOL> self . blind_channels = [ ] <EOL> self . reverse_ignore = [ ] <EOL> self . tokenized = False <EOL> self . hashpw = False <EOL> self . debug = False <EOL> self . data = '<STR_LIT>' <EOL> self . compat = defaultdict ( lambda : False ) <EOL> self . scriptPassword = None <EOL> now = time . time ( ) <EOL> self . last_login = now <EOL> self . register_date = now <EOL> self . lastdata = now <EOL> self . last_id = <NUM_LIT:0> <EOL> self . users = set ( [ ] ) <EOL> self . battles = set ( [ ] ) <EOL> self . _root . console_write ( '<STR_LIT>' % ( self . ip_address , session_id ) ) <EOL> def setFlagByIP ( self , ip , force = True ) : <EOL> cc = ip2country . lookup ( ip ) <EOL> if force or cc != '<STR_LIT>' : <EOL> self . country_code = cc <EOL> def Bind ( self , handler = None , protocol = None ) : <EOL> if handler : self . handler = handler <EOL> if protocol : <EOL> if not self . _protocol : <EOL> protocol . _new ( self ) <EOL> self . _protocol = protocol <EOL> def Handle ( self , data ) : <EOL> if self . bot and not ( self . access in self . floodlimit and '<STR_LIT>' in self . floodlimit [ self . access ] ) : limit = self . floodlimit [ '<STR_LIT>' ] <EOL> elif self . access in self . floodlimit : limit = self . floodlimit [ self . access ] <EOL> else : limit = self . floodlimit [ '<STR_LIT:user>' ] <EOL> if not '<STR_LIT>' in limit : <EOL> msglength = limit [ '<STR_LIT>' ] <EOL> bytespersecond = limit [ '<STR_LIT>' ] <EOL> seconds = limit [ '<STR_LIT>' ] <EOL> now = int ( time . time ( ) ) <EOL> self . lastdata = now <EOL> if now in self . msglengthhistory : <EOL> self . msglengthhistory [ now ] += len ( data ) <EOL> else : <EOL> self . msglengthhistory [ now ] = len ( data ) <EOL> total = <NUM_LIT:0> <EOL> for iter in dict ( self . msglengthhistory ) : <EOL> if iter < now - ( seconds - <NUM_LIT:1> ) : <EOL> del self . msglengthhistory [ iter ] <EOL> else : <EOL> total += self . msglengthhistory [ iter ] <EOL> if total > ( bytespersecond * seconds ) : <EOL> self . SendNow ( '<STR_LIT>' % ( bytespersecond , seconds ) ) <EOL> self . Remove ( '<STR_LIT>' ) <EOL> return <EOL> self . data += data <EOL> if self . data . count ( '<STR_LIT:\n>' ) > <NUM_LIT:0> : <EOL> data = self . data . split ( '<STR_LIT:\n>' ) <EOL> ( datas , self . data ) = ( data [ : len ( data ) - <NUM_LIT:1> ] , data [ len ( data ) - <NUM_LIT:1> : ] [ <NUM_LIT:0> ] ) <EOL> for data in datas : <EOL> if data . endswith ( '<STR_LIT:\r>' ) and self . telnet : <EOL> self . nl = '<STR_LIT:\r\n>' <EOL> command = data . rstrip ( '<STR_LIT:\r>' ) . lstrip ( '<STR_LIT:U+0020>' ) <EOL> if not '<STR_LIT>' in limit and len ( command ) > msglength : <EOL> self . Send ( '<STR_LIT>' % msglength ) <EOL> else : <EOL> if self . telnet : <EOL> command = Telnet . filter_in ( self , command ) <EOL> if type ( command ) == str : <EOL> command = [ command ] <EOL> for cmd in command : <EOL> self . _protocol . _handle ( self , cmd ) <EOL> def Remove ( self , reason = '<STR_LIT>' ) : <EOL> self . handler . finishRemove ( self , reason ) <EOL> def Send ( self , msg ) : <EOL> if self . telnet : <EOL> msg = Telnet . filter_out ( self , msg ) <EOL> if not msg : return <EOL> if self . handler . thread == thread . get_ident ( ) : <EOL> msg = self . msg_id + msg <EOL> self . sendbuffer . append ( msg + self . nl ) <EOL> self . handler . poller . setoutput ( self . conn , True ) <EOL> def SendNow ( self , msg ) : <EOL> if self . sendError : return <EOL> if self . telnet : <EOL> msg = Telnet . filter_out ( msg ) <EOL> if not msg : return <EOL> try : <EOL> self . conn . send ( msg + self . nl ) <EOL> except socket . error : <EOL> self . sendError = True <EOL> self . Remove ( ) <EOL> def FlushBuffer ( self ) : <EOL> if self . data and self . telnet : <EOL> return <EOL> if not self . sendingmessage : <EOL> message = '<STR_LIT>' <EOL> while not message : <EOL> if not self . sendbuffer : <EOL> self . handler . poller . setoutput ( self . conn , False ) <EOL> return <EOL> message = self . sendbuffer . pop ( <NUM_LIT:0> ) <EOL> self . sendingmessage = message <EOL> senddata = self . sendingmessage <EOL> try : <EOL> sent = self . conn . send ( senddata ) <EOL> self . sendingmessage = self . sendingmessage [ sent : ] <EOL> except UnicodeDecodeError : <EOL> self . sendingmessage = None <EOL> self . _root . console_write ( '<STR_LIT>' ) <EOL> except socket . error , e : <EOL> if e == errno . EAGAIN : <EOL> return <EOL> self . Remove ( ) <EOL> self . handler . poller . setoutput ( self . conn , bool ( self . sendbuffer or self . sendingmessage ) ) <EOL> def AddUser ( self , user ) : <EOL> if type ( user ) in ( str , unicode ) : <EOL> try : user = self . _root . usernames [ user ] <EOL> except : return <EOL> session_id = user . session_id <EOL> if session_id in self . users : return <EOL> self . users . add ( session_id ) <EOL> self . _protocol . client_AddUser ( self , user ) <EOL> def RemoveUser ( self , user ) : <EOL> if type ( user ) in ( str , unicode ) : <EOL> try : user = self . _root . usernames [ user ] <EOL> except : return <EOL> session_id = user . session_id <EOL> if session_id in self . users : <EOL> self . users . remove ( session_id ) <EOL> self . _protocol . client_RemoveUser ( self , user ) <EOL> def SendUser ( self , user , data ) : <EOL> if type ( user ) in ( str , unicode ) : <EOL> try : user = self . _root . usernames [ user ] <EOL> except : return <EOL> session_id = user . session_id <EOL> if session_id in self . users : <EOL> self . Send ( data ) <EOL> def AddBattle ( self , battle ) : <EOL> battle_id = battle . id <EOL> if battle_id in self . battles : return <EOL> self . battles . add ( battle_id ) <EOL> self . _protocol . client_AddBattle ( self , battle ) <EOL> def RemoveBattle ( self , battle ) : <EOL> battle_id = battle . id <EOL> if battle_id in self . battles : <EOL> self . battles . remove ( battle_id ) <EOL> self . _protocol . client_RemoveBattle ( self , battle ) <EOL> def SendBattle ( self , battle , data ) : <EOL> battle_id = battle . id <EOL> if battle_id in self . battles : <EOL> self . Send ( data ) <EOL> def isAdmin ( self ) : <EOL> return ( '<STR_LIT>' in self . accesslevels ) <EOL> def isMod ( self ) : <EOL> return self . isAdmin ( ) or ( '<STR_LIT>' in self . accesslevels ) </s>
