def __init__(self, configuration): super(RotcBot,self).__init__(configuration) Watcher.__init__(self) logging.Handler.__init__(self) # Logging stuff chandler = logging.StreamHandler() chandler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) self.log.addHandler(chandler) self.setLevel(logging.INFO) # Only send INFO messages to the control room self.setFormatter(logging.Formatter("%(levelname)s - %(message)s")) self.log.addHandler(self) # The messages that go to the console self.log.setLevel(logging.DEBUG) ##### self.server_ids = [] # enumerate keys of server_list self.last_rotc_update = 0 self.EVENTS = { 'new_game': 'The first player joins a server.', 'game_close': 'The last player leaves a server, or a server which had players is closed.', 'player_change': 'A change in player numbers on a server.', 'new_server': 'A new server was created (any number of players, even 0).', 'server_close': 'A server was closed (even if it had no players)' } # Build the server list self._build_server_list() # Connect the events self.callback_new_server = self._callback_new_server self.callback_detail_change = self._callback_detail_change self.callback_server_close = self._callback_server_close
def __init__(self, jid, password, res="main", debug=False, json_output=None): JabberBot.__init__(self, jid, password, res, debug) self.w = Watcher() self.server_ids = [] # enumerate keys of server_list self.last_rotc_update = 0 self.roster = None self.json_output = json_output self.EVENTS = { 'new_game': 'The first player joins a server.', 'game_close': 'The last player leaves a server, or a server which had players is closed.', 'player_change': 'A change in player numbers on a server.', 'new_server': 'A new server was created (any number of players, even 0).', 'server_close': 'A server was closed (even if it had no players)' }
class RotcBot(JabberBot): def __init__(self, jid, password, res="main", debug=False, json_output=None): JabberBot.__init__(self, jid, password, res, debug) self.w = Watcher() self.server_ids = [] # enumerate keys of server_list self.last_rotc_update = 0 self.roster = None self.json_output = json_output self.EVENTS = { 'new_game': 'The first player joins a server.', 'game_close': 'The last player leaves a server, or a server which had players is closed.', 'player_change': 'A change in player numbers on a server.', 'new_server': 'A new server was created (any number of players, even 0).', 'server_close': 'A server was closed (even if it had no players)' } @botcmd(hidden=True) def l(self, mess, args): return self.list(mess, args) @botcmd def list(self, mess, args): """Displays the list of open rotc servers. Alias: l""" if len(self.server_ids) == 0: return "There are no open rotc servers. Maybe you want to host one?" retn = ["List of open rotc ethernet servers:"] for i in range(len(self.server_ids)): addr = self.server_ids[i] try: name = self.w.server_info[addr]['server_name'] players = self.w.server_info[addr]['player_count'] map = self.w.server_info[addr]['mission_name'] retn.append("[%i] %s has %s players" % (i, name, players)) except KeyError: retn.append("[%i] %s is in an unknown state" % (i, addr)) return '\n'.join(retn) @botcmd(hidden=True) def d(self, mess, args): return self.details(mess, args) @botcmd def details(self, mess, args): """Displays the details of a given rotc server. Alias: d""" if args in self.server_ids: addr = args det = self.w.get_server_details(addr) else: try: i = int(args) except ValueError: return "Unknown id. Enter either id from list or IP" if i < len(self.server_ids) and self.server_ids[i] != None: addr = self.server_ids[i] det = self.w.get_server_details(addr) else: return "There is no server with this id." retn = ["Details for rotc ethernet server with address %s:" % addr] for key, value in det.items(): retn.append("%s: %s" % (key, value)) return '\n'.join(retn) @botcmd(hidden=True) def broadcast(self, mess, args): """ADMIN - broadcast a message to all users of rotcbot.""" jid = str(mess.getFrom()).split('/')[0] if jid == control_room: self._broadcast(''.join(args)) else: self.error("User %s wanted to broadcast, but he's not in the control room" % jid) @botcmd(hidden=True) def msg(self, mess, args): """sends a message to a jid.""" jid = str(mess.getFrom()).split('/')[0] if jid == control_room: try: target, msg = args.split(' ', 1) self.send(target, msg) except ValueError: self.error("The correct format is: send jid message") else: self.error("User %s wanted to use msg, but he's not in the control room" % jid) @botcmd(hidden=True) def restart(self, mess, args): jid = str(mess.getFrom()).split('/')[0] if jid == control_room: self.quit() # we will be restarted by the supervisor else: self.error("User %s wanted to use restart, but he's not in the control room" % jid) @botcmd(hidden=True) def shutdown(self, mess=None, args=None): if mess is None and args is None: JabberBot.shutdown(self) return jid = str(mess.getFrom()).split('/')[0] if jid == control_room: self.log("Bang, Bang, %s shot me down, bang bang..." % jid) raise Exception("Shutdown rotcbot") else: self.error("User %s wanted to shutdown rotbot, but he's not in the control room" % jid) @botcmd(hidden=True) def show_contacts(self, mess, args): jid = str(mess.getFrom()).split('/')[0] if jid == control_room: contacts = "List of all users:\n" for contact in self.roster.getItems(): try: groups = self.roster.getGroups(contact) except e: groups = "ERR " + str(e) contacts += '> %s - %s\n' % (str(contact), groups) #self.send(control_room, contacts, message_type='groupchat') self.log(contacts) else: self.error("User %s wanted to use show_contacts, but he's not in the control room" % jid) #@botcmd #def raise_error(self, mess, args): # """Tells the bot to die""" # # raise Exception('killed', mess) def idle_proc(self): if self.last_rotc_update + 7 < int(time.time()): self._build_server_list() self.last_rotc_update = int(time.time()) if self.json_output: self.json_output() def json_output(self): try: with open(self.json_output, 'w') as out: data = {'time':self.last_rotc_update, 'server_info':self.w.server_info} json.dump(data, out) except IOError, e: self.error('Failed to write to json file: %s' % str(e)) return False return True