From 2183620070eb6a250307fef6add2fa4b57bb93fa Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Wed, 23 Feb 2022 11:45:53 -0500 Subject: [PATCH] test --- .../SBCore/commands/__init__.py | 37 +++++++++++-------- sopel_SpiceBot_Core_Prerun/__init__.py | 30 +++++++++++---- sopel_SpiceBot_Core_Prerun/comrun.py | 30 +-------------- .../validate_trigger.py | 20 ++++++++++ sopel_SpiceBot_Runtime_Commands/__init__.py | 1 + 5 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 sopel_SpiceBot_Core_Prerun/validate_trigger.py diff --git a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py index dde80f0..6773494 100644 --- a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py +++ b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py @@ -71,35 +71,38 @@ class Commands(): def dispatch(self, trigger_dict): if trigger_dict["trigger_type"] == "command": - return self.dispatch_command(trigger_dict) + pretrigger = self.generate_pretrigger_command(trigger_dict) elif trigger_dict["trigger_type"] == "nickname_command": - return self.dispatch_nickname_command(trigger_dict) + pretrigger = self.generate_pretrigger_nickname_command(trigger_dict) elif trigger_dict["trigger_type"] == "action_command": - return self.dispatch_action_command(trigger_dict) - - def dispatch_command(self, trigger_dict): - pretrigger = PreTrigger( - self.bot.nick, - ":%s %s %s :%s%s %s" % (trigger_dict["trigger_hostmask"], "PRIVMSG", trigger_dict["trigger_sender"], - trigger_dict["trigger_prefix"], trigger_dict["trigger_command"], trigger_dict["trigger_str"]) - ) + pretrigger = self.generate_pretrigger_action_command(trigger_dict) self.bot.dispatch(pretrigger) - def dispatch_nickname_command(self, trigger_dict): + def generate_pretrigger_command(self, trigger_dict): + # @time=2022-02-23T15:04:01.447Z : + pretrigger = PreTrigger( + self.bot.nick, + "%s :%s %s %s :%s%s %s" % ("", + trigger_dict["trigger_hostmask"], "PRIVMSG", trigger_dict["trigger_sender"], + trigger_dict["trigger_prefix"], trigger_dict["trigger_command"], trigger_dict["trigger_str"]) + ) + return pretrigger + + def generate_pretrigger_nickname_command(self, trigger_dict): pretrigger = PreTrigger( self.bot.nick, ":%s %s %s :%s %s %s" % (trigger_dict["trigger_hostmask"], "PRIVMSG", trigger_dict["trigger_sender"], trigger_dict["trigger_prefix"], trigger_dict["trigger_command"], trigger_dict["trigger_str"]) ) - self.bot.dispatch(pretrigger) + return pretrigger - def dispatch_action_command(self, trigger_dict): + def generate_pretrigger_action_command(self, trigger_dict): pretrigger = PreTrigger( self.bot.nick, ":%s %s %s :%s%s %s%s %s" % (trigger_dict["trigger_hostmask"], "PRIVMSG", trigger_dict["trigger_sender"], "\x01", "ACTION", trigger_dict["trigger_command"], trigger_dict["trigger_str"], "\x01") ) - self.bot.dispatch(pretrigger) + return pretrigger def get_commands_split(self, trigger, splitkey): commands = [] @@ -144,7 +147,8 @@ class Commands(): "trigger_str": first_trigger_str, "trigger_command": first_trigger_command, "trigger_hostmask": trigger.hostmask, - "trigger_sender": trigger.sender + "trigger_sender": trigger.sender, + "trigger_time": trigger.time }) for full_trigger_str in triggers: @@ -213,7 +217,8 @@ class Commands(): "trigger_str": trigger_str, "trigger_command": trigger_command, "trigger_hostmask": trigger.hostmask, - "trigger_sender": trigger.sender + "trigger_sender": trigger.sender, + "trigger_time": trigger.time }) return commands diff --git a/sopel_SpiceBot_Core_Prerun/__init__.py b/sopel_SpiceBot_Core_Prerun/__init__.py index 7b717ea..d79de45 100644 --- a/sopel_SpiceBot_Core_Prerun/__init__.py +++ b/sopel_SpiceBot_Core_Prerun/__init__.py @@ -2,7 +2,9 @@ import functools from sopel_SpiceBot_Core_1 import sb -# from .comrun import comrun_create +from .comrun import ComRun +from .validate_trigger import validate_trigger +# from .validate_command import validate_command # from .dispatch_multi import dispatch_multi # from .pipe_split import pipe_split # from .command_args import command_args @@ -14,22 +16,34 @@ def prerun(rulematch=False): def actual_decorator(function): - # @comrun_create(rulematch) - # @dispatch_multi() - # @pipe_split() - # @command_args() - # @rule_match() @functools.wraps(function) def internal_prerun(bot, trigger, comrun, *args, **kwargs): - # Get list of trigger command(s) + # Get list of trigger command(s) by && split commands = sb.commands.get_commands_split(trigger, "&&") + comrun = ComRun(rulematch, 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[1:]: + for trigger_dict in commands: sb.commands.dispatch(trigger_dict) return + # This is where we rebuild trigger + # we validate a few things here + trigger, redispatch = validate_trigger(trigger) + + # 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) # if not comrun.piped: diff --git a/sopel_SpiceBot_Core_Prerun/comrun.py b/sopel_SpiceBot_Core_Prerun/comrun.py index 5e3e79d..0100999 100644 --- a/sopel_SpiceBot_Core_Prerun/comrun.py +++ b/sopel_SpiceBot_Core_Prerun/comrun.py @@ -1,33 +1,7 @@ -import functools class ComRun(): - def __init__(self, rulematch): - self.piped = False + def __init__(self, rulematch, trigger): self.rulematch = rulematch - self.say = "" - self.trigger_dict = { - "trigger_type": None, - "trigger_prefix": None, - "trigger_str": None, - "trigger_command": None, - "trigger_hostmask": None, - "trigger_sender": None - } - - -def comrun_create(rulematch): - """This Detects --arguments to commands.""" - - def actual_decorator(function): - - @functools.wraps(function) - def internal_comrun_create(bot, trigger, *args, **kwargs): - - comrun = ComRun(rulematch) - - function(bot, trigger, comrun, *args, **kwargs) - - return internal_comrun_create - return actual_decorator + self.original_trigger = trigger diff --git a/sopel_SpiceBot_Core_Prerun/validate_trigger.py b/sopel_SpiceBot_Core_Prerun/validate_trigger.py new file mode 100644 index 0000000..e379284 --- /dev/null +++ b/sopel_SpiceBot_Core_Prerun/validate_trigger.py @@ -0,0 +1,20 @@ + + +from sopel_SpiceBot_Core_1 import sb + + +def validate_trigger(trigger, trigger_dict): + redispatch = False + + trigger_time = trigger.time + + if trigger_dict["trigger_type"] == "command": + trigger._pretrigger = sb.commands.generate_pretrigger_command(trigger_dict) + elif trigger_dict["trigger_type"] == "nickname_command": + trigger._pretrigger = sb.commands.generate_pretrigger_nickname_command(trigger_dict) + elif trigger_dict["trigger_type"] == "action_command": + trigger._pretrigger = sb.commands.generate_pretrigger_action_command(trigger_dict) + + trigger._pretrigger.time = trigger_time + + return trigger, redispatch diff --git a/sopel_SpiceBot_Runtime_Commands/__init__.py b/sopel_SpiceBot_Runtime_Commands/__init__.py index 60bf5aa..86a6dfc 100644 --- a/sopel_SpiceBot_Runtime_Commands/__init__.py +++ b/sopel_SpiceBot_Runtime_Commands/__init__.py @@ -15,6 +15,7 @@ def commands_test(bot, trigger, comrun): @plugin.command('testa') def commands_test_a(bot, trigger, comrun): bot.say("test a") + bot.say(str(trigger.tags)) bot.say("%s" % trigger.hostmask)