def runCmd( self, playerid, cmd, args ): """Runs an admin command.""" ctx = self.mm.getRconContext( playerid ) if not ctx.authedLevel: # Automatically log the player in ctx.autoLogin = "******" % ( playerid, int( host.timer_getWallTime() ) ) self.mm.runRconCommand( playerid, "login %s" % ctx.autoLogin ) ctx.autoLogin = "" # Prevent future logins logout = True else: logout = False if -1 != cmd.find( '%arg' ): # Have replacement params so iterate through replacing args_list = mm_utils.largs( args, None, 0, '', True ) for i in range( len( args_list ) ): arg_name = ( "%%arg%s%%" % ( i + 1 ) ); cmd = cmd.replace( arg_name, args_list[i] ) args = "" ret = self.mm.runRconCommand( playerid, "%s %s" % ( cmd, args ) ) # Automatically log the player in if logout: self.mm.runRconCommand( playerid, "logout" ) return ret
def cmdBanPlayer(self, ctx, cmd): """Ban a player for a specified time period from the server with a message.""" self.mm.debug(2, "cmdBanPlayerBy '%s' by %s" % (cmd, ctx.getName())) (playerid, banPeriod, banReason, bannedBy) = mm_utils.largs(cmd, None, 4, '') playerid = mm_utils.get_int(ctx, playerid, 'playerid') if 0 > playerid: ctx.write('Error: player Id not found\n') return 0 try: player = bf2.playerManager.getPlayerByIndex(playerid) if player is None: ctx.write('Error: player Id %d not found\n' % playerid) else: if banReason is not None: banReason = banReason.strip('" ') if bannedBy is None: bannedBy = ctx.getName() self.banPlayer(player, banReason, banPeriod, None, None, bannedBy) ctx.write("Player '%s' (%d) banned\n" % (player.getName(), playerid)) except: ctx.write('Error: player Id %d not found\n' % playerid) self.mm.error("Failed to get player %d" % playerid, True)
def cmdBanPlayer( self, ctx, cmd ): """Ban a player for a specified time period from the server with a message.""" self.mm.debug( 2, "cmdBanPlayerBy '%s' by %s" % ( cmd, ctx.getName() ) ) ( playerid, banPeriod, banReason, bannedBy ) = mm_utils.largs( cmd, None, 4, '' ) playerid = mm_utils.get_int( ctx, playerid, 'playerid' ) if 0 > playerid: ctx.write( 'Error: player Id not found\n') return 0 try: player = bf2.playerManager.getPlayerByIndex( playerid ) if player is None: ctx.write('Error: player Id %d not found\n' % playerid ) else: if banReason is not None: banReason = banReason.strip( '" ' ) if bannedBy is None: bannedBy = ctx.getName() self.banPlayer( player, banReason, banPeriod, None, None, bannedBy ) ctx.write( "Player '%s' (%d) banned\n" % ( player.getName(), playerid ) ) except: ctx.write('Error: player Id %d not found\n' % playerid ) self.mm.error( "Failed to get player %d" % playerid, True )
def runCmd(self, playerid, cmd, args): """Runs an admin command.""" ctx = self.mm.getRconContext(playerid) if not ctx.authedLevel: # Automatically log the player in ctx.autoLogin = "******" % (playerid, int(host.timer_getWallTime())) self.mm.runRconCommand(playerid, "login %s" % ctx.autoLogin) ctx.autoLogin = "" # Prevent future logins logout = True else: logout = False if -1 != cmd.find('%arg'): # Have replacement params so iterate through replacing args_list = mm_utils.largs(args, None, 0, '', True) for i in range(len(args_list)): arg_name = ("%%arg%s%%" % (i + 1)) cmd = cmd.replace(arg_name, args_list[i]) args = "" ret = self.mm.runRconCommand(playerid, "%s %s" % (cmd, args)) # Automatically log the player in if logout: self.mm.runRconCommand(playerid, "logout") return ret
def cmdRemoveBan( self, ctx, cmd ): """Remove a ban.""" self.mm.debug( 2, "cmdRemoveBan %s" % cmd ) ( ban, reason ) = mm_utils.largs( cmd, None, 2, '' ) if self.__removeBan( ban, reason ): ctx.write( 'Unban successful\n' ) else: ctx.write( 'Unban failed (ban not found)\n' )
def cmdRemoveBan(self, ctx, cmd): """Remove a ban.""" self.mm.debug(2, "cmdRemoveBan %s" % cmd) (ban, reason) = mm_utils.largs(cmd, None, 2, '') if self.__removeBan(ban, reason): ctx.write('Unban successful\n') else: ctx.write('Unban failed (ban not found)\n')
def cmdUpdateBan(self, ctx, cmd): """Update a ban.""" self.mm.debug(2, "cmdUpdateBan %s" % cmd) (bankey, method, nick, period, address, cdkeyhash, profileid, by, reason, datetime) = mm_utils.largs(cmd, None, 10, '') if self.__bans.has_key(bankey): ban = self.__bans[bankey] old_method = ban['method'] # remove as the details may have changed del self.__bans[bankey] # Only update values which where specified if nick: ban['nick'] = self.validatePlayerName(nick) if period: ban['period'] = self.validateBanPeriod(period) if address: ban['address'] = self.validateBanAddress(address) if cdkeyhash: ban['cdkeyhash'] = self.validateBanCdKeyHash(cdkeyhash) if profileid: ban['profileid'] = self.validateBanProfileId(profileid) if by: ban['by'] = self.validateBannedBy(by) if reason: ban['reason'] = self.validateBanReason(reason) if method: ban['method'] = self.validateBanMethod(method, ban['cdkeyhash'], ban['address']) if datetime: ban['datetime'] = datetime # Check and remove the old ban self.__removeBan(bankey, 'Ban update', True) # Add the new one if self.__addBan(ban['method'], ban['nick'], ban['period'], ban['address'], ban['cdkeyhash'], ban['profileid'], ban['by'], ban['reason'], ban['datetime']): ctx.write('Ban updated\n') else: ctx.write("Update failed ( save failed )\n") else: ctx.write("Update failed ( ban '%s' not found )\n" % bankey)
def cmdAddBan( self, ctx, cmd ): """Rcon command to add a ban.""" self.mm.debug( 2, "cmdAddBan %s" % cmd ) ( method, nick, period, address, cdkeyhash, profileid, by, reason, datetime ) = mm_utils.largs( cmd, None, 9, '' ) ban = self.__addBan( method, nick, period, address, cdkeyhash, profileid, by, reason, datetime ) if ban: ctx.write( 'Ban added\n' ) else: ctx.write( "Ban add failed ( save failed )\n" )
def cmdBanPlayer( self, ctx, cmd ): """Ban a player for a specified time period from the server with a message.""" ( playerid, banReason, banPeriod ) = mm_utils.largs( cmd, None, 3, '' ) banReason = banReason.lower() if self.__warnings.has_key( banReason ): # message shortcut replace banReason = self.__warnings[banReason] self.mm.rcon().banPlayer( ctx, ( "'%s' '%s' '%s'" ) % ( playerid, banPeriod, banReason ) , True ) else: self.mm.rcon().banPlayer( ctx, cmd, True )
def onChatMessage(self, playerid, text, channel, flags): """Check for in game commands.""" if 1 != self.__state: return 0 try: # Strip channel prefixes so commands can be used in all channels text = mm_utils.MsgChannels.named[channel].stripPrefix(text) self.mm.debug(2, "Chat[%d]: '%s'" % (playerid, text)) player = bf2.playerManager.getPlayerByIndex(playerid) if player: prefix = self.__config['cmdPrefix'] if text.startswith(prefix): # We have a potential command (cmd, args) = mm_utils.largs(text[len(prefix):], None, 2, '', True) self.mm.debug(2, "CMD?: '%s' '%s'" % (cmd, args)) if cmd in self.__igacmds: # We have a command cdkeyhash = mm_utils.get_cd_key_hash(player) profileid = str(player.getProfileId()) playername = player.getName() admins = self.__admins self.mm.debug( 2, "Looking for: %s, %s, %s" % (cdkeyhash, profileid, playername)) if cdkeyhash in admins: # User is an admin admin = cdkeyhash elif profileid in admins: # User is an admin admin = profileid elif playername in admins: # User is an admin admin = playername else: # User is not an admin mm_utils.msg_player( player.index, self.__config['notAdminMessage'] % (playername)) return # User is an admin details = self.__igacmds[cmd] if details['priv'] in admins[admin] or 'all' in admins[ admin]: self.runCmd(playerid, details['cmd'], args) else: mm_utils.msg_player( player.index, self.__config['notAuthedMessage'] % (playername, cmd)) except: self.mm.error("Oops bad", True)
def cmdKickPlayer( self, ctx, cmd ): """Kick a player from the server with a message.""" self.mm.rcon().kickPlayer( ctx, cmd, True ); ( playerid, reason ) = mm_utils.largs( cmd, None, 2, '' ) reason = reason.lower() if self.__warnings.has_key( reason ): # message shortcut replace reason = self.__warnings[reason] self.mm.rcon().kickPlayer( ctx, ( "'%s' '%s'" ) % ( playerid, reason ) , True ) else: self.mm.rcon().kickPlayer( ctx, cmd, True )
def cmdBanPlayer(self, ctx, cmd): """Ban a player for a specified time period from the server with a message.""" (playerid, banReason, banPeriod) = mm_utils.largs(cmd, None, 3, '') banReason = banReason.lower() if self.__warnings.has_key(banReason): # message shortcut replace banReason = self.__warnings[banReason] self.mm.rcon().banPlayer(ctx, ("'%s' '%s' '%s'") % (playerid, banPeriod, banReason), True) else: self.mm.rcon().banPlayer(ctx, cmd, True)
def cmdAddBan(self, ctx, cmd): """Rcon command to add a ban.""" self.mm.debug(2, "cmdAddBan %s" % cmd) (method, nick, period, address, cdkeyhash, profileid, by, reason, datetime) = mm_utils.largs(cmd, None, 9, '') ban = self.__addBan(method, nick, period, address, cdkeyhash, profileid, by, reason, datetime) if ban: ctx.write('Ban added\n') else: ctx.write("Ban add failed ( save failed )\n")
def cmdKickPlayer(self, ctx, cmd): """Kick a player from the server with a message.""" self.mm.rcon().kickPlayer(ctx, cmd, True) (playerid, reason) = mm_utils.largs(cmd, None, 2, '') reason = reason.lower() if self.__warnings.has_key(reason): # message shortcut replace reason = self.__warnings[reason] self.mm.rcon().kickPlayer(ctx, ("'%s' '%s'") % (playerid, reason), True) else: self.mm.rcon().kickPlayer(ctx, cmd, True)
def onChatMessage( self, playerid, text, channel, flags ): """Check for in game commands.""" if 1 != self.__state: return 0 try: # Strip channel prefixes so commands can be used in all channels text = mm_utils.MsgChannels.named[channel].stripPrefix( text ) self.mm.debug( 2, "Chat[%d]: '%s'" % ( playerid, text ) ) player = bf2.playerManager.getPlayerByIndex( playerid ) if player: prefix = self.__config['cmdPrefix'] if text.startswith( prefix ): # We have a potential command ( cmd, args ) = mm_utils.largs( text[len(prefix):], None, 2, '', True ) self.mm.debug( 2, "CMD?: '%s' '%s'" % ( cmd, args ) ) if cmd in self.__igacmds: # We have a command cdkeyhash = mm_utils.get_cd_key_hash( player ) profileid = str( player.getProfileId() ) playername = player.getName() admins = self.__admins self.mm.debug( 2, "Looking for: %s, %s, %s" % ( cdkeyhash, profileid, playername ) ) if cdkeyhash in admins: # User is an admin admin = cdkeyhash elif profileid in admins: # User is an admin admin = profileid elif playername in admins: # User is an admin admin = playername else: # User is not an admin mm_utils.msg_player( player.index, self.__config['notAdminMessage'] % ( playername ) ) return # User is an admin details = self.__igacmds[cmd] if details['priv'] in admins[admin] or 'all' in admins[admin]: self.runCmd( playerid, details['cmd'], args ) else: mm_utils.msg_player( player.index, self.__config['notAuthedMessage'] % ( playername, cmd ) ) except: self.mm.error( "Oops bad", True )
def cmdWarnPlayer( self, ctx, cmd ): """Send a message to a specific player.""" ( playerid, msg ) = mm_utils.largs( cmd, None, 2, '' ) player = mm_utils.find_player( playerid, True ) if player is None: ctx.write( 'Error: player %s not found\n' % playerid ) self.mm.error( "Failed to find player %s" % playerid ) else: lmsg = msg.lower() if self.__warnings.has_key( lmsg ): # message shortcut replace msg = self.__warnings[lmsg] msg = self.__config['warningPrefix'] + msg msg = msg.replace( '%player%', player.getName().strip( ' ' ) ); msg = msg.replace( '%action%', self.__config['warningAction'] ); msg = msg.replace( '%admin%', ctx.bf2ccClient.adminName ); mm_utils.msg_player( player.index, msg ) ctx.write( 'Chat sent to player %s\n' % playerid )
def cmdWarnPlayer(self, ctx, cmd): """Send a message to a specific player.""" (playerid, msg) = mm_utils.largs(cmd, None, 2, '') player = mm_utils.find_player(playerid, True) if player is None: ctx.write('Error: player %s not found\n' % playerid) self.mm.error("Failed to find player %s" % playerid) else: lmsg = msg.lower() if self.__warnings.has_key(lmsg): # message shortcut replace msg = self.__warnings[lmsg] msg = self.__config['warningPrefix'] + msg msg = msg.replace('%player%', player.getName().strip(' ')) msg = msg.replace('%action%', self.__config['warningAction']) msg = msg.replace('%admin%', ctx.bf2ccClient.adminName) mm_utils.msg_player(player.index, msg) ctx.write('Chat sent to player %s\n' % playerid)
def __parseConfig( self ): """Parse the configuration file.""" self.info( "Loading config '%s'" % ( self.__configFile ) ) add_re = re.compile( '^add([A-Z].*)$' ) set_re = re.compile( '^set([A-Z].*)$' ) try: config = open( self.__configFile, 'r' ) lineNo = 0 for line in config: lineNo += 1 line = line.strip() if 0 != len( line ) and not line.startswith( "#" ): try: ( key, val1, val2 ) = mm_utils.largs( line, ' ', 3, None, True ) val1 = self.__decodeConfigValue( key, val1 ) val2 = self.__decodeConfigValue( key, val2 ) try: oldVal = self.__getattr__( key ) self.warn( "Overriding %s = '%s' with '%s'" % ( key, oldVal, val1 ) ) except KeyError: # ignore pass # load to the relavent place ( module, module_key ) = mm_utils.lsplit( key, '.', 2 ) #self.debug( 2, "%s . %s = %s" % ( module, module_key, value ) ) if __name__ == module: if "loadModule" == module_key: # loadable module self.__addModule( val1 ) else: # core config file self.__setattr__( module_key, val1 ) self.__setParam( module, module_key, val1 ) else: match = add_re.search( module_key ) if match is not None: # user multi setting # matches things like: # <module>.addProfileId 1 # <module>.addProfileId 2 # and sets: # <module>.profileIds = [ 1, 2 ] # or: # <module>.addCmdAlias "k" "kick" # <module>.addCmdAlias "b" "ban" # and sets: # <module>.cmdAliass = { 'k': 'kick', 'b': 'ban' } self.__addParam( module, "%c%ss" % ( module_key[3:4].lower(), module_key[4:] ), val1, val2 ) else: # user single setting self.__setParam( module, module_key, val1 ) except Exception, detail: self.error( 'Syntax error in "%s" on line %d (%s)' % ( self.__configFile, lineNo, detail ), True ) config.close()
def cmdAddCmd( self, ctx, cmd ): """Adds a command into our supported commands set.""" ( command, aliases_str ) = mm_utils.largs( cmd, None, 2, '' ) return self.__addCmd( ctx, command, aliases_str )
def cmdAddCmd(self, ctx, cmd): """Adds a command into our supported commands set.""" (command, aliases_str) = mm_utils.largs(cmd, None, 2, '') return self.__addCmd(ctx, command, aliases_str)
def cmdAddAdmin(self, ctx, cmd): """Adds an admin into our authorised admin set.""" (admin, privs_str) = mm_utils.largs(cmd, None, 2, '') return self.__addAdmin(ctx, admin, privs_str)
def cmdAddAdmin( self, ctx, cmd ): """Adds an admin into our authorised admin set.""" ( admin, privs_str ) = mm_utils.largs( cmd, None, 2, '' ) return self.__addAdmin( ctx, admin, privs_str )
def cmdUpdateBan( self, ctx, cmd ): """Update a ban.""" self.mm.debug( 2, "cmdUpdateBan %s" % cmd ) ( bankey, method, nick, period, address, cdkeyhash, profileid, by, reason, datetime ) = mm_utils.largs( cmd, None, 10, '' ) if self.__bans.has_key( bankey ): ban = self.__bans[bankey] old_method = ban['method'] # remove as the details may have changed del self.__bans[bankey] # Only update values which where specified if nick: ban['nick'] = self.validatePlayerName( nick ) if period: ban['period'] = self.validateBanPeriod( period ) if address: ban['address'] = self.validateBanAddress( address ) if cdkeyhash: ban['cdkeyhash'] = self.validateBanCdKeyHash( cdkeyhash ) if profileid: ban['profileid'] = self.validateBanProfileId( profileid ) if by: ban['by'] = self.validateBannedBy( by ) if reason: ban['reason'] = self.validateBanReason( reason ) if method: ban['method'] = self.validateBanMethod( method, ban['cdkeyhash'], ban['address'] ) if datetime: ban['datetime'] = datetime # Check and remove the old ban self.__removeBan( bankey, 'Ban update', True ) # Add the new one if self.__addBan( ban['method'], ban['nick'], ban['period'], ban['address'], ban['cdkeyhash'], ban['profileid'], ban['by'], ban['reason'], ban['datetime'] ): ctx.write( 'Ban updated\n' ) else: ctx.write( "Update failed ( save failed )\n" ) else: ctx.write( "Update failed ( ban '%s' not found )\n" % bankey )