<s> import time , traceback <EOL> from Client import Client <EOL> from CryptoHandler import UNICODE_ENCODING <EOL> class ChanServ : <EOL> def __init__ ( self , client , root ) : <EOL> self . client = client <EOL> self . _root = root <EOL> self . channeldb = root . channeldb <EOL> def onLogin ( self ) : <EOL> self . _root . protocol . _calc_status ( self . client , <NUM_LIT:0> ) <EOL> for channel in self . _root . channels . values ( ) : <EOL> self . Send ( '<STR_LIT>' % str ( channel . name ) ) <EOL> def Handle ( self , msg ) : <EOL> try : <EOL> if not msg . count ( '<STR_LIT:U+0020>' ) : <EOL> return <EOL> cmd , args = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if cmd == '<STR_LIT>' : <EOL> chan , user , msg = args . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> self . HandleMessage ( chan , user , msg ) <EOL> if cmd == '<STR_LIT>' : <EOL> user , msg = args . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> self . HandleMessage ( None , user , msg ) <EOL> except : <EOL> self . _root . error ( traceback . format_exc ( ) ) <EOL> def HandleMessage ( self , chan , user , msg ) : <EOL> if len ( msg ) <= <NUM_LIT:0> : <EOL> return <EOL> if msg [ <NUM_LIT:0> ] != "<STR_LIT:!>" : <EOL> return <EOL> msg = msg . lstrip ( '<STR_LIT:!>' ) <EOL> args = None <EOL> if user == '<STR_LIT>' : return <EOL> if msg . count ( '<STR_LIT:U+0020>' ) >= <NUM_LIT:2> : <EOL> splitmsg = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> if splitmsg [ <NUM_LIT:1> ] . startswith ( '<STR_LIT:#>' ) : <EOL> cmd , chan , args = splitmsg <EOL> chan = chan . lstrip ( '<STR_LIT:#>' ) <EOL> else : <EOL> cmd , args = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> elif msg . count ( '<STR_LIT:U+0020>' ) == <NUM_LIT:1> : <EOL> splitmsg = msg . split ( '<STR_LIT:U+0020>' ) <EOL> if splitmsg [ <NUM_LIT:1> ] . startswith ( '<STR_LIT:#>' ) : <EOL> cmd , chan = splitmsg <EOL> chan = chan . lstrip ( '<STR_LIT:#>' ) <EOL> else : <EOL> cmd , args = splitmsg <EOL> else : <EOL> cmd = msg <EOL> response = self . HandleCommand ( chan , user , cmd , args ) <EOL> if response : <EOL> self . Send ( [ '<STR_LIT>' % ( user , s ) for s in response . split ( '<STR_LIT:\n>' ) ] ) <EOL> def HandleCommand ( self , chan , user , cmd , args = None ) : <EOL> client = self . _root . protocol . clientFromUsername ( user ) <EOL> cmd = cmd . lower ( ) <EOL> if cmd == '<STR_LIT>' : <EOL> return '<STR_LIT>' % user <EOL> if chan in self . _root . channels : <EOL> channel = self . _root . channels [ chan ] <EOL> access = channel . getAccess ( client ) <EOL> if cmd == '<STR_LIT:info>' : <EOL> founder = self . _root . clientFromID ( channel . owner , True ) <EOL> if founder : founder = '<STR_LIT>' % founder . username <EOL> else : founder = '<STR_LIT>' <EOL> admins = [ ] <EOL> for admin in channel . admins : <EOL> client = self . _root . clientFromID ( admin ) <EOL> if client : admins . append ( client . username ) <EOL> users = channel . users <EOL> antispam = '<STR_LIT>' if channel . antispam else '<STR_LIT>' <EOL> if not admins : mods = '<STR_LIT>' <EOL> else : mods = '<STR_LIT>' % ( len ( admins ) , '<STR_LIT>' . join ( admins ) ) <EOL> if len ( users ) == <NUM_LIT:1> : users = '<STR_LIT>' <EOL> else : users = '<STR_LIT>' % len ( users ) <EOL> return '<STR_LIT>' % ( chan , antispam , founder , mods , users ) <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> args = args or '<STR_LIT>' <EOL> channel . setTopic ( client , args ) <EOL> self . channeldb . setTopic ( client . username , channel , args ) <EOL> return '<STR_LIT>' % chan <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> channel . owner = '<STR_LIT>' <EOL> channel . channelMessage ( '<STR_LIT>' % chan ) <EOL> self . Send ( '<STR_LIT>' % chan ) <EOL> self . channeldb . unRegister ( client , channel ) <EOL> return '<STR_LIT>' % chan <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> target = self . _root . clientFromUsername ( args ) <EOL> if not target : return '<STR_LIT>' <EOL> channel . setFounder ( client , target ) <EOL> channel . channelMessage ( '<STR_LIT>' % ( chan , args ) ) <EOL> return '<STR_LIT>' % ( chan , args ) <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if args == '<STR_LIT>' : <EOL> channel . antispam = True <EOL> channel . channelMessage ( '<STR_LIT>' % ( chan , user ) ) <EOL> return '<STR_LIT>' % chan <EOL> elif args == '<STR_LIT>' : <EOL> channel . antispam = False <EOL> channel . channelMessage ( '<STR_LIT>' % ( chan , user ) ) <EOL> return '<STR_LIT>' % chan <EOL> status = '<STR_LIT>' <EOL> if channel . antispam : status = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( chan , status ) <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> target = self . _root . clientFromUsername ( args ) <EOL> if target and channel . isOp ( target ) : return '<STR_LIT>' % ( chan , args ) <EOL> channel . opUser ( client , target ) <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> target = self . _root . clientFromUsername ( args ) <EOL> if target and not channel . isOp ( target ) : return '<STR_LIT>' % ( chan , args ) <EOL> channel . deopUser ( client , target ) <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> target = self . _root . clientFromUsername ( args ) <EOL> if target and channel . isOp ( target ) : args = '<STR_LIT>' % ( user , args ) <EOL> channel . channelMessage ( args ) <EOL> return <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> channel . setKey ( client , args ) <EOL> self . channeldb . setKey ( channel , args ) <EOL> return '<STR_LIT>' % chan <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> channel . setKey ( client , '<STR_LIT:*>' ) <EOL> self . channeldb . setKey ( channel , '<STR_LIT:*>' ) <EOL> return '<STR_LIT>' % chan <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not args : return '<STR_LIT>' % chan <EOL> if args . count ( '<STR_LIT:U+0020>' ) : <EOL> target , reason = args . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> else : <EOL> target = args <EOL> reason = None <EOL> if target in channel . users : <EOL> target = self . _root . clientFromUsername ( target ) <EOL> channel . kickUser ( client , target , reason ) <EOL> return '<STR_LIT>' % ( chan , target . username ) <EOL> else : return '<STR_LIT>' % ( chan , target ) <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> elif cmd == '<STR_LIT>' : <EOL> if access in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> channel . store_history = not channel . store_history <EOL> self . channeldb . setHistory ( channel ) <EOL> msg = '<STR_LIT>' % ( chan , str ( channel . store_history ) ) <EOL> channel . channelMessage ( msg ) <EOL> return msg <EOL> else : <EOL> return '<STR_LIT>' % chan <EOL> if cmd == '<STR_LIT>' : <EOL> if client . isMod ( ) : <EOL> if not args : args = user <EOL> self . Send ( '<STR_LIT>' % chan ) <EOL> if not chan in self . _root . channels : <EOL> return '<STR_LIT>' % ( chan ) <EOL> channel = self . _root . channels [ chan ] <EOL> target = self . _root . clientFromUsername ( args ) <EOL> if target : <EOL> channel . setFounder ( client , target ) <EOL> self . channeldb . register ( channel , target ) <EOL> return '<STR_LIT>' % ( chan , args . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return '<STR_LIT>' % ( chan , args ) <EOL> elif not chan in self . _root . channels : <EOL> return '<STR_LIT>' % chan <EOL> if not chan : <EOL> return '<STR_LIT>' % ( cmd ) <EOL> def Send ( self , msg ) : <EOL> if type ( msg ) == list or type ( msg ) == tuple : <EOL> for s in msg : <EOL> self . client . HandleProtocolCommand ( s . encode ( UNICODE_ENCODING ) ) <EOL> else : <EOL> self . client . HandleProtocolCommand ( msg . encode ( UNICODE_ENCODING ) ) <EOL> class ChanServClient ( Client ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , root , address , session_id ) : <EOL> '<STR_LIT>' <EOL> Client . __init__ ( self , root , address , session_id ) <EOL> self . static = True <EOL> self . logged_in = True <EOL> self . access = '<STR_LIT>' <EOL> self . accesslevels = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' , '<STR_LIT>' ] <EOL> self . bot = <NUM_LIT:1> <EOL> self . username = '<STR_LIT>' <EOL> self . password = '<STR_LIT>' <EOL> self . cpu = '<STR_LIT>' <EOL> self . lobby_id = '<STR_LIT>' <EOL> self . _root . usernames [ self . username ] = self <EOL> self . _root . clients [ session_id ] = self <EOL> self . _root . console_write ( '<STR_LIT>' % ( self . session_id , self . username ) ) <EOL> self . reload ( ) <EOL> def reload ( self ) : <EOL> self . ChanServ = ChanServ ( self , self . _root ) <EOL> def Handle ( self , data ) : <EOL> pass <EOL> def Remove ( self , reason = None ) : <EOL> pass <EOL> def Send ( self , msg , binary = False ) : <EOL> if not msg : return <EOL> self . ChanServ . Handle ( msg ) <EOL> def FlushBuffer ( self ) : <EOL> pass </s>
