From 1298c811fbccf5234e1bfd82be5894f0754ee0a9 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 23 May 2019 15:38:43 -0400 Subject: [PATCH] channel disabling prerun --- sopel_modules/SpiceBot/Commands.py | 8 ++ sopel_modules/SpiceBot/Prerun.py | 123 +++++++++++++++++- sopel_modules/SpiceBot_AI/AI.py | 2 +- sopel_modules/SpiceBot_BotPrivs/BotPrivs.py | 4 +- .../Channels_Command.py | 2 +- .../NickQueryCommand.py | 2 +- .../SpiceBot_CommandsQuery/QueryCommand.py | 2 +- .../SpiceBot_GifSearch/Gif_API_Commands.py | 2 +- .../SpiceBot_GifSearch/Gif_Command.py | 2 +- .../SpiceBot_GifSearch/Gif_Search_Commands | 2 +- sopel_modules/SpiceBot_Logs/Logs_Command.py | 2 +- sopel_modules/SpiceBot_Messaging/Messaging.py | 6 +- sopel_modules/SpiceBot_Update/Update.py | 2 +- 13 files changed, 139 insertions(+), 20 deletions(-) diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index dbdc57d..cdaf180 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -27,6 +27,14 @@ class BotCommands(): }, } + def find_command_type(self, command): + for commandstype in self.dict['commands'].keys(): + if commandstype not in ['rule']: + for com in self.dict['commands'][commandstype].keys(): + if com.lower() == command.lower(): + return commandstype + return None + def register(self, bot, command_dict): self.lock.acquire() diff --git a/sopel_modules/SpiceBot/Prerun.py b/sopel_modules/SpiceBot/Prerun.py index e2bbecb..c8ad134 100644 --- a/sopel_modules/SpiceBot/Prerun.py +++ b/sopel_modules/SpiceBot/Prerun.py @@ -4,27 +4,47 @@ from __future__ import unicode_literals, absolute_import, division, print_functi This is the SpiceBot Prerun system. """ +from sopel.tools import Identifier + import functools import copy +import datetime import spicemanip +from .Tools import command_permissions_check +from .Commands import commands +from .Database import db as spicedb + class BotPrerun(): def __init__(self): self.dict = {} - def prerun(self, trigger_command_type='module_command'): + def prerun(self, trigger_command_type='module'): def actual_decorator(function): @functools.wraps(function) def internal_prerun(bot, trigger, *args, **kwargs): - argsdict_default = {} - argsdict_default["type"] = trigger_command_type + # Bots can't run commands + if Identifier(trigger.nick) == bot.nick: + return # Primary command used for trigger, and a list of all words trigger_args, trigger_command = self.trigger_args(trigger.args[1], trigger_command_type) + + command_lookup = commands.find_command_type(trigger_command) + + # Argsdict Defaults + argsdict_default = {} + argsdict_default["type"] = trigger_command_type argsdict_default["com"] = trigger_command + if command_lookup and "aliasfor" in commands.dict["commands"][command_lookup].keys(): + realcom = commands.dict["commands"][command_lookup]["aliasfor"] + else: + realcom = trigger_command + argsdict_default["realcom"] = realcom + # split into && groupings and_split = self.and_split(trigger_args) @@ -34,15 +54,19 @@ class BotPrerun(): # Run the function for all splits for argsdict in argsdict_list: trigger.sb = argsdict - function(bot, trigger, *args, **kwargs) + if len(argsdict["hyphen_args"]): + self.hyphen_arg_handler(bot, trigger, argsdict) + + if self.runstatus(bot, trigger, argsdict): + function(bot, trigger, *args, **kwargs) return return internal_prerun return actual_decorator - def trigger_args(self, triggerargs_one, trigger_command_type='module_command'): + def trigger_args(self, triggerargs_one, trigger_command_type='module'): trigger_args = spicemanip.main(triggerargs_one, 'create') - if trigger_command_type in ['nickname', 'nickname_command', 'nickname_commands']: + if trigger_command_type in ['nickname']: trigger_command = spicemanip.main(trigger_args, 2).lower() trigger_args = spicemanip.main(trigger_args, '3+', 'list') else: @@ -61,9 +85,96 @@ class BotPrerun(): for trigger_args_list in prerun_and_split: argsdict = copy.deepcopy(argsdict_default) trigger_args_part = spicemanip.main(trigger_args_list, 'create') + trigger_args_part, hyphen_args = self.hyphen_args(trigger_args_part) + argsdict["hyphen_args"] = hyphen_args argsdict["args"] = trigger_args_part prerun_split.append(argsdict) return prerun_split + def hyphen_args(self, trigger_args_part): + valid_hyphen_args = [ + 'enable', 'disable' + ] + hyphen_args = [] + trigger_args_unhyphend = [] + for worditem in trigger_args_part: + if str(worditem).startswith("--") and worditem[2:] in valid_hyphen_args: + hyphen_args.append(worditem[2:]) + else: + trigger_args_unhyphend.append(worditem) + return trigger_args_unhyphend, hyphen_args + + def hyphen_arg_handler(self, bot, trigger, argsdict): + + hyphenarg = argsdict["hyphen_args"][0] + + # Commands that cannot run via privmsg + if hyphenarg in ['enable']: + + if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + bot.say("I was unable to disable this command due to privilege issues.") + return + + if trigger.is_privmsg: + bot.notice("This command must be run in a channel you which to enable it in.", trigger.nick) + return + + disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {} + + if argsdict["realcom"] not in disabled_list.keys(): + bot.notice(argsdict["com"] + " is already enabled in " + str(trigger.sender), trigger.nick) + return + + del disabled_list[argsdict["realcom"]] + spicedb.set_channel_value(bot, trigger.sender, 'disabled_commands', disabled_list, 'commands') + bot.say(argsdict["com"] + " is now enabled in " + str(trigger.sender)) + return + + elif hyphenarg in ['disable']: + + if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + bot.say("I was unable to disable this command due to privilege issues.") + return + + if trigger.is_privmsg: + bot.notice("This command must be run in a channel you which to disable it in.", trigger.nick) + return + + disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {} + + if argsdict["realcom"] in disabled_list.keys(): + bot.notice(argsdict["com"] + " is already disabled in " + str(trigger.sender), trigger.nick) + return + + trailingmessage = spicemanip.main(argsdict["args"], 0) or "No reason given." + timestamp = str(datetime.datetime.utcnow()) + + disabled_list[argsdict["realcom"]] = {"reason": trailingmessage, "timestamp": timestamp, "disabledby": trigger.nick} + spicedb.set_channel_value(bot, trigger.sender, 'disabled_commands', disabled_list, 'commands') + + spicedb.adjust_channel_list(bot, trigger.sender, 'disabled_commands', argsdict["realcom"], 'add', 'commands') + bot.say(argsdict["com"] + " is now disabled in " + str(trigger.sender)) + return + + return + + def runstatus(self, bot, trigger, argsdict): + + # Hyphen args can only be used one per && split + if len(argsdict["hyphen_args"]): + return False + + # don't run commands that are disabled in channels + if not trigger.is_privmsg: + disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {} + if argsdict["realcom"] in disabled_list.keys(): + reason = disabled_list["disabled_commands"][argsdict["realcom"]]["reason"] + timestamp = disabled_list["disabled_commands"][argsdict["realcom"]]["timestamp"] + bywhom = disabled_list["disabled_commands"][argsdict["realcom"]]["disabledby"] + bot.notice("The " + str(argsdict["com"]) + " command was disabled by " + bywhom + " in " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason), trigger.nick) + return False + + return True + prerun = BotPrerun() diff --git a/sopel_modules/SpiceBot_AI/AI.py b/sopel_modules/SpiceBot_AI/AI.py index 2712b84..e43eb53 100644 --- a/sopel_modules/SpiceBot_AI/AI.py +++ b/sopel_modules/SpiceBot_AI/AI.py @@ -10,7 +10,7 @@ import sopel_modules.SpiceBot as SpiceBot @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED, SpiceBot.events.BOT_COMMANDSQUERY]) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('(.*)') def bot_command_nick(bot, trigger): diff --git a/sopel_modules/SpiceBot_BotPrivs/BotPrivs.py b/sopel_modules/SpiceBot_BotPrivs/BotPrivs.py index cd9ed10..9927b9b 100644 --- a/sopel_modules/SpiceBot_BotPrivs/BotPrivs.py +++ b/sopel_modules/SpiceBot_BotPrivs/BotPrivs.py @@ -9,13 +9,13 @@ import spicemanip import sopel_modules.SpiceBot as SpiceBot -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('owners', 'owner') def bot_command_owners(bot, trigger): bot_command_process(bot, trigger) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('admins') def bot_command_admins(bot, trigger): bot_command_process(bot, trigger) diff --git a/sopel_modules/SpiceBot_Channels_Command/Channels_Command.py b/sopel_modules/SpiceBot_Channels_Command/Channels_Command.py index 7aab13f..fdc70d7 100644 --- a/sopel_modules/SpiceBot_Channels_Command/Channels_Command.py +++ b/sopel_modules/SpiceBot_Channels_Command/Channels_Command.py @@ -10,7 +10,7 @@ import sopel_modules.SpiceBot as SpiceBot @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED]) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('channels', 'channel') def nickname_comand_channels(bot, trigger): diff --git a/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py index 786dff8..15f3498 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py +++ b/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py @@ -11,7 +11,7 @@ import spicemanip @SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('(.*)') def query_detection_nick(bot, trigger): diff --git a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py index 5754413..f52b37e 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py +++ b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py @@ -11,7 +11,7 @@ import spicemanip @SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) -@SpiceBot.prerun.prerun('query_command') +@SpiceBot.prerun.prerun('query') @sopel.module.rule('^\?(.*)') def query_detection(bot, trigger): diff --git a/sopel_modules/SpiceBot_GifSearch/Gif_API_Commands.py b/sopel_modules/SpiceBot_GifSearch/Gif_API_Commands.py index c19438f..929b1b9 100644 --- a/sopel_modules/SpiceBot_GifSearch/Gif_API_Commands.py +++ b/sopel_modules/SpiceBot_GifSearch/Gif_API_Commands.py @@ -12,7 +12,7 @@ import sopel_modules.SpiceBot as SpiceBot @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) -@SpiceBot.prerun.prerun('prefix_command') +@SpiceBot.prerun.prerun('prefix') @sopel.module.commands('(.*)') def gifapi_triggers(bot, trigger): diff --git a/sopel_modules/SpiceBot_GifSearch/Gif_Command.py b/sopel_modules/SpiceBot_GifSearch/Gif_Command.py index 3ee6481..841f958 100644 --- a/sopel_modules/SpiceBot_GifSearch/Gif_Command.py +++ b/sopel_modules/SpiceBot_GifSearch/Gif_Command.py @@ -12,7 +12,7 @@ import spicemanip @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) -@SpiceBot.prerun.prerun('prefix_command') +@SpiceBot.prerun.prerun('prefix') @module.commands('gif') def gif_trigger(bot, trigger): diff --git a/sopel_modules/SpiceBot_GifSearch/Gif_Search_Commands b/sopel_modules/SpiceBot_GifSearch/Gif_Search_Commands index c19438f..929b1b9 100644 --- a/sopel_modules/SpiceBot_GifSearch/Gif_Search_Commands +++ b/sopel_modules/SpiceBot_GifSearch/Gif_Search_Commands @@ -12,7 +12,7 @@ import sopel_modules.SpiceBot as SpiceBot @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) -@SpiceBot.prerun.prerun('prefix_command') +@SpiceBot.prerun.prerun('prefix') @sopel.module.commands('(.*)') def gifapi_triggers(bot, trigger): diff --git a/sopel_modules/SpiceBot_Logs/Logs_Command.py b/sopel_modules/SpiceBot_Logs/Logs_Command.py index 581860f..0af1032 100644 --- a/sopel_modules/SpiceBot_Logs/Logs_Command.py +++ b/sopel_modules/SpiceBot_Logs/Logs_Command.py @@ -9,7 +9,7 @@ import sopel_modules.SpiceBot as SpiceBot @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOGS]) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('logs', 'debug') def bot_command_logs(bot, trigger): diff --git a/sopel_modules/SpiceBot_Messaging/Messaging.py b/sopel_modules/SpiceBot_Messaging/Messaging.py index 1d5200d..e2af152 100644 --- a/sopel_modules/SpiceBot_Messaging/Messaging.py +++ b/sopel_modules/SpiceBot_Messaging/Messaging.py @@ -9,19 +9,19 @@ import spicemanip import sopel_modules.SpiceBot as SpiceBot -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('action') def bot_command_action(bot, trigger): bot_command_process(bot, trigger) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('notice') def bot_command_notice(bot, trigger): bot_command_process(bot, trigger) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('privmsg', 'say', 'msg') def bot_command_privmsg(bot, trigger): bot_command_process(bot, trigger) diff --git a/sopel_modules/SpiceBot_Update/Update.py b/sopel_modules/SpiceBot_Update/Update.py index 72cbdfe..9f69588 100644 --- a/sopel_modules/SpiceBot_Update/Update.py +++ b/sopel_modules/SpiceBot_Update/Update.py @@ -27,7 +27,7 @@ def setup(bot): bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) -@SpiceBot.prerun.prerun('nickname_command') +@SpiceBot.prerun.prerun('nickname') @sopel.module.nickname_commands('update') def nickname_comand_update(bot, trigger):