From 21a48907f52a2d14a89af7ddf67929955e0f5275 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 24 Feb 2022 10:11:34 -0500 Subject: [PATCH] test --- .../SBCore/commands/__init__.py | 43 +++++++- sopel_SpiceBot_Core_Prerun/__init__.py | 100 ++++++++++++------ 2 files changed, 112 insertions(+), 31 deletions(-) diff --git a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py index aa35878..986a3bb 100644 --- a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py +++ b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py @@ -159,7 +159,48 @@ class Commands(): rulematch = True return rulematch - def get_commands_split(self, trigger, splitkey): + def get_commands_nosplit(self, trigger): + commands = [] + first_full_trigger_str = trigger.args[1] + + if first_full_trigger_str.startswith(tuple(self.config.prefix_list)): + first_trigger_type = "command" + first_trigger_prefix = first_full_trigger_str[0] + first_trigger_noprefix = first_full_trigger_str[1:] + first_trigger_command = first_trigger_noprefix.split(" ")[0] + first_trigger_str = " ".join([x.strip() for x in first_trigger_noprefix.split(" ")[1:]]) + elif first_full_trigger_str.startswith(self.bot.nick): + first_trigger_type = "nickname_command" + first_trigger_prefix = str(first_full_trigger_str.split(" ")[0]) + first_trigger_noprefix = " ".join(first_full_trigger_str.split(" ")[1:]) + first_trigger_command = first_trigger_noprefix.split(" ")[0] + first_trigger_str = " ".join([x.strip() for x in first_trigger_noprefix.split(" ")[1:]]) + elif "intent" in trigger.tags and trigger.tags["intent"] == "ACTION": + first_trigger_type = "action_command" + first_trigger_prefix = "ACTION" + first_trigger_noprefix = first_full_trigger_str + first_trigger_command = first_trigger_noprefix.split(" ")[0] + first_trigger_str = " ".join([x.strip() for x in first_trigger_noprefix.split(" ")[1:]]) + else: + first_trigger_type = "rule" + first_trigger_prefix = None + first_trigger_noprefix = first_full_trigger_str + first_trigger_command = first_trigger_noprefix.split(" ")[0] + first_trigger_str = " ".join([x.strip() for x in first_trigger_noprefix.split(" ")[1:]]) + + commands.append({ + "trigger_type": first_trigger_type, + "trigger_prefix": first_trigger_prefix, + "trigger_str": first_trigger_str, + "trigger_command": first_trigger_command, + "trigger_hostmask": trigger.hostmask, + "trigger_sender": trigger.sender, + "trigger_time": str(trigger.time) + }) + + return commands + + def get_commands_split(self, trigger, splitkey=None): commands = [] # Get split for multiple commands diff --git a/sopel_SpiceBot_Core_Prerun/__init__.py b/sopel_SpiceBot_Core_Prerun/__init__.py index a69cd26..dafdf5b 100644 --- a/sopel_SpiceBot_Core_Prerun/__init__.py +++ b/sopel_SpiceBot_Core_Prerun/__init__.py @@ -13,14 +13,11 @@ def prerun(): comrun = ComRun(function, trigger) - # Get list of trigger command(s) by && split - commands = sb.commands.get_commands_split(trigger, "&&") - # Since there was more than one command, # we are going to redispatch commands # This will give sopel the appearance of recieving individual commands - if len(commands) > 1: - for trigger_dict in commands: + if len(comrun.commands) > 1: + for trigger_dict in comrun.commands: if comrun.is_rulematch: if not sb.commands.is_real_command(trigger_dict): sb.commands.dispatch(trigger_dict) @@ -28,37 +25,19 @@ def prerun(): sb.commands.dispatch(trigger_dict) return - # If the original trigger is not the same after && split + # If the original trigger is not the same after && or | split # so we will now redispatch to help get the correct function passed - trigger_command = sb.commands.get_command_from_trigger(trigger) - if trigger_command != commands[0]["trigger_command"]: - sb.commands.dispatch(commands[0]) - return - - # Get list of trigger command(s) by | split - commands = sb.commands.get_commands_split(trigger, "|") - - # Validate | split - trigger_command = sb.commands.get_command_from_trigger(trigger) - if trigger_command != commands[0]["trigger_command"]: - trigger_dict = rebuild_pipes(commands) + if comrun.has_command_been_sanitized: + if comrun.is_pipe_command: + trigger_dict = rebuild_pipes(comrun.commands) + else: + trigger_dict = comrun.command sb.commands.dispatch(trigger_dict) return - if comrun.is_rulematch and sb.commands.is_real_command(commands[0]): + if comrun.is_rulematch and comrun.is_real_command(): return - # This is where we rebuild trigger - # we validate a few things here - # trigger = clean_trigger(bot, trigger, commands[0]) - - # Now we have a single valid command - # we are going to validate the command - # as a && or | attached to a command - # could trigger an unmatched command - # if validate_command(rulematch): - # return - # Run function function(bot, trigger, comrun, *args, **kwargs) @@ -90,6 +69,10 @@ class ComRun(): self.function = function self.trigger = trigger + @property + def is_real_command(self): + return sb.commands.is_real_command(self.command) + @property def command_type(self): return sb.commands.what_command_type(self.trigger) @@ -97,3 +80,60 @@ class ComRun(): @property def is_rulematch(self): return sb.commands.is_rulematch(self.function, self.command_type) + + @property + def is_multi_command(self): + if "&&" in self.trigger.args[1]: + return True + return False + + @property + def is_pipe_command(self): + if "|" in self.trigger.args[1]: + return True + return False + + @property + def multi_split_count(self): + if "&&" in self.trigger.args[1]: + return len([x.strip() for x in self.trigger.args[1].split("&&")]) + return "N/A" + + @property + def pipe_split_count(self): + if "|" in self.trigger.args[1]: + return len([x.strip() for x in self.trigger.args[1].split("|")]) + return "N/A" + + @property + def commands(self): + if "&&" in self.trigger.args[1]: + return sb.commands.get_commands_split(self.trigger, "&&") + elif "|" in self.trigger.args[1]: + return sb.commands.get_commands_split(self.trigger, "|") + else: + return sb.commands.get_commands_nosplit(self.trigger) + + @property + def command(self): + return self.commands[0] + + @property + def has_command_been_sanitized(self): + + trigger_command = sb.commands.get_command_from_trigger(self.trigger) + multi_split_count = self.multi_split_count + pipe_split_count = self.pipe_split_count + + if trigger_command != self.command["trigger_command"]: + return True + + elif multi_split_count != "N/A": + if multi_split_count != len(self.commands): + return True + + elif pipe_split_count != "N/A": + if pipe_split_count != len(self.commands): + return True + + return False