def test_str(self): sender = SenderObj(1, "Test", None) self.assertEqual(SenderObj(1, "Test", None), sender) self.assertEqual( "{'char_id': 1, 'name': 'Test', 'access_level': None}", str(sender)) self.assertEqual( "[{'char_id': 1, 'name': 'Test', 'access_level': None}]", str([sender]))
def process_matches(self, params): val = super().process_matches(params) if val is None: return None else: character_service = Registry.get_instance("character_service") access_service = Registry.get_instance("access_service") char_id = character_service.resolve_char_to_id(val) if char_id is None: return SenderObj(char_id, val.capitalize(), None) else: return SenderObj(char_id, val.capitalize(), access_service.get_access_level(char_id))
def process_command(self, message: str, channel: str, char_id, reply): try: if self.ban_service.get_ban(char_id): # do nothing if character is banned self.logger.info( "ignored banned character %d for command '%s'" % (char_id, message)) return message = html.unescape(message) command_str, command_args = self.get_command_parts(message) # check for command alias command_alias = self.command_alias_service.check_for_alias( command_str) if command_alias: command_str, command_args = self.get_command_parts( command_alias + command_args) cmd_configs = self.get_command_configs(command_str, channel, 1) if cmd_configs: # given a list of cmd_configs that are enabled, see if one has regex that matches incoming command_str cmd_config, matches, handler = self.get_matches( cmd_configs, command_args) if matches: if handler["check_access"](char_id, cmd_config.access_level): sender = SenderObj( char_id, self.character_service.resolve_char_to_name( char_id, "Unknown(%d)" % char_id)) response = handler["callback"]( CommandRequest(channel, sender, reply), *self.process_matches(matches, handler["params"])) if response is not None: reply(response) # record command usage self.usage_service.add_usage( command_str, handler["callback"].__qualname__, char_id, channel) else: self.access_denied_response(char_id, cmd_config, reply) else: # handlers were found, but no handler regex matched help_text = self.get_help_text(char_id, command_str, channel) if help_text: reply(self.format_help_text(command_str, help_text)) else: reply("Error! Invalid syntax.") else: reply("Error! Unknown command <highlight>%s<end>." % command_str) except Exception as e: self.logger.error("error processing command: %s" % message, e) reply("There was an error processing your request.")
def process_matches(self, params): val = super().process_matches(params) if val is None: return None else: character_service = Registry.get_instance("character_service") return SenderObj(character_service.resolve_char_to_id(val), val.capitalize())
def get_account(self, main_id, conn): sql = "SELECT p.char_id, p.points, p.disabled FROM points p WHERE p.char_id = ?" row = self.db.query_single(sql, [main_id]) if not row: self.create_account(main_id, SenderObj(conn.get_char_id(), conn.get_char_name(), None)) row = self.db.query_single(sql, [main_id]) return row
def handle_public_message(self, packet: PublicChannelMessage): extended_message = packet.extended_message if extended_message and extended_message.category_id == 1001 and extended_message.instance_id == 1: char_name = extended_message.params[0] char_id = self.character_service.resolve_char_to_id(char_name) action = extended_message.params[1] access_level = self.access_service.get_access_level(char_id) self.event_service.fire_event( self.CLOAK_EVENT, DictObject({ "sender": SenderObj(char_id, char_name, access_level), "action": action }))
def raid_kick_cmd(self, _1, _2, char: SenderObj, reason: str): if self.raid is None: return self.NO_RAID_RUNNING_RESPONSE main_id = self.alts_service.get_main(char.char_id).char_id in_raid = self.is_in_raid(main_id) try: int(char.name) char.name = self.character_service.resolve_char_to_name(char.name) except ValueError: pass if in_raid is not None: if not in_raid.is_active: return "%s is already set as inactive." % char.name in_raid.is_active = False in_raid.was_kicked = int(time.time()) in_raid.was_kicked_reason = reason return "%s has been kicked from the raid with reason \"%s\"" % (char.name, reason) else: return "%s is not participating." % char.name
def process_command(self, message: str, channel: str, char_id, reply, conn): try: context = DictObject({ "message": message, "char_id": char_id, "channel": channel, "reply": reply }) for pre_processor in self.pre_processors: if pre_processor(context) is False: return for regex in self.ignore_regexes: if regex.search(message): return # message = html.unescape(message) command_str, command_args = self.get_command_parts(message) # check for command alias command_alias_str = self.command_alias_service.get_alias_command_str( command_str, command_args) alias_depth_count = 0 while command_alias_str: alias_depth_count += 1 command_str, command_args = self.get_command_parts( command_alias_str) command_alias_str = self.command_alias_service.get_alias_command_str( command_str, command_args) if alias_depth_count > 20: raise Exception( "Command alias infinite recursion detected for command '%s'" % message) cmd_configs = self.get_command_configs(command_str, channel, 1) access_level = self.access_service.get_access_level(char_id) sender = SenderObj( char_id, self.character_service.resolve_char_to_name( char_id, "Unknown(%d)" % char_id), access_level) if cmd_configs: # given a list of cmd_configs that are enabled, see if one has regex that matches incoming command_str cmd_config, matches, handler = self.get_matches( cmd_configs, command_args) if matches: if handler["check_access"](char_id, cmd_config.access_level): response = handler["callback"]( CommandRequest(conn, channel, sender, reply), *self.process_matches(matches, handler["params"])) if response is not None: reply(response) # record command usage self.usage_service.add_usage( command_str, self.util.get_handler_name(handler["callback"]), char_id, channel) else: self.access_denied_response(message, sender, cmd_config, reply) else: # handlers were found, but no handler regex matched data = self.db.query( "SELECT command, sub_command, access_level FROM command_config " "WHERE command = ? AND channel = ? AND enabled = 1", [command_str, channel]) help_text = self.format_help_text(data, char_id) if help_text: reply( self.format_help_text_blob(command_str, help_text)) else: # the command is known, but no help is returned, therefore character does not have access to command reply("Access denied.") else: self.handle_unknown_command(command_str, command_args, channel, sender, reply) except Exception as e: self.logger.error("error processing command: %s" % message, e) reply("There was an error processing your request.")
def process_command(self, message: str, channel: str, char_id, reply): try: context = DictObject({ "message": message, "char_id": char_id, "channel": channel, "reply": reply }) for pre_processor in self.pre_processors: if pre_processor(context) is False: return for regex in self.ignore_regexes: if regex.search(message): return # message = html.unescape(message) command_str, command_args = self.get_command_parts(message) # check for command alias command_alias = self.command_alias_service.check_for_alias( command_str) alias_depth_count = 0 while command_alias: alias_depth_count += 1 command_str, command_args = self.get_command_parts( command_alias + command_args) command_alias = self.command_alias_service.check_for_alias( command_str) if alias_depth_count > 20: raise Exception( "Command alias infinite recursion detected for command '%s'" % message) cmd_configs = self.get_command_configs(command_str, channel, 1) access_level = self.access_service.get_access_level(char_id) sender = SenderObj( char_id, self.character_service.resolve_char_to_name( char_id, "Unknown(%d)" % char_id), access_level) if cmd_configs: # given a list of cmd_configs that are enabled, see if one has regex that matches incoming command_str cmd_config, matches, handler = self.get_matches( cmd_configs, command_args) if matches: if handler["check_access"](char_id, cmd_config.access_level): def call_command_handler(): try: response = handler["callback"](CommandRequest( channel, sender, reply), *self.process_matches( matches, handler["params"])) if response is not None: reply(response) except Exception as e: self.logger.error( "error processing command: %s" % message, e) reply( self.getresp("global", "error_proccessing")) # FeatureFlags.THREADING self.executor_service.submit_job( 10, call_command_handler) # record command usage self.usage_service.add_usage( command_str, handler["callback"].__qualname__, char_id, channel) else: self.access_denied_response(message, sender, cmd_config, reply) else: # handlers were found, but no handler regex matched help_text = self.get_help_text(char_id, command_str, channel) if help_text: reply(self.format_help_text(command_str, help_text)) else: # the command is known, but no help is returned, therefore user does not have access to command reply(self.getresp("global", "access_denied")) else: self.handle_unknown_command(command_str, command_args, channel, sender, reply) except Exception as e: self.logger.error("error processing command: %s" % message, e) reply(self.getresp("global", "error_proccessing"))