From 8a24ca71e74f1794aa3ed66c2217b66043922b2c Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Wed, 26 Jun 2019 10:59:53 -0400 Subject: [PATCH] test multirun disabling --- sopel_modules/SpiceBot/Channels.py | 25 ++++- sopel_modules/SpiceBot/Commands.py | 92 +++++++++++++----- sopel_modules/SpiceBot/Prerun.py | 149 +++++++++++++++++++++++------ sopel_modules/SpiceBot/Users.py | 4 + 4 files changed, 217 insertions(+), 53 deletions(-) diff --git a/sopel_modules/SpiceBot/Channels.py b/sopel_modules/SpiceBot/Channels.py index 22dcc6e..e0b34e3 100644 --- a/sopel_modules/SpiceBot/Channels.py +++ b/sopel_modules/SpiceBot/Channels.py @@ -29,6 +29,7 @@ class BotChannels(): self.lock = threading.Lock() self.channel_lock = False + self.channels = [] self.dict = { "list": {}, "InitialProcess": False @@ -46,7 +47,7 @@ class BotChannels(): if channel.lower() not in list(self.dict["list"].keys()): raise Exception('Channel ' + str(channel.lower()) + " does not seem to be on this server") - if 'users' not in list(self.dict["list"][channel.lower()].keys()): + if 'users' not in list(self.dict["list"][channel.lower()].keys()) or not self.check_channel_bot(channel): raise Exception('Channel ' + str(channel.lower()) + " does not seem to be a channel the bot is in") nick_ids = self.dict["list"][channel.lower()]['users'] @@ -102,6 +103,8 @@ class BotChannels(): self.lock.acquire() del self.dict['list'][channel.lower()] self.lock.release() + if channel.lower() in self.channels: + self.channels.remove(channel.lower()) def join_all_channels(self, bot): if botconfig.SpiceBot_Channels.joinall: @@ -112,6 +115,10 @@ class BotChannels(): if channel not in list(bot.channels.keys()) and botconfig.SpiceBot_Channels.operadmin: bot.write(('SAJOIN', bot.nick, self.dict['list'][channel]['name'])) + if channel in list(bot.channels.keys()): + if channel.lower() not in self.channels: + self.channels.append(channel.lower()) + def chanadmin_all_channels(self, bot): # Chan ADMIN +a for channel in list(bot.channels.keys()): @@ -123,6 +130,10 @@ class BotChannels(): else: bot.part(channel) + if channel not in list(bot.channels.keys()): + if channel.lower() in self.channels: + self.channels.remove(channel.lower()) + def whois_ident(self, nick): nick = Identifier(nick) nick_id = botdb.db.get_nick_id(nick, create=True) @@ -138,6 +149,8 @@ class BotChannels(): self.dict['list'][channel.lower()]['users'] = [] if nick_id not in self.dict['list'][channel.lower()]['users']: self.dict['list'][channel.lower()]['users'].append(int(nick_id)) + if channel.lower() not in self.channels: + self.channels.append(channel.lower()) def remove_from_channel(self, channel, nick, nick_id=None): channel = str(channel) @@ -157,6 +170,8 @@ class BotChannels(): if 'users' not in self.dict['list'][channel.lower()]: self.dict['list'][channel.lower()]['users'] = [] self.dict['list'][channel.lower()]['users'] = [] + if channel.lower() in self.channels: + self.channels.remove(channel.lower()) def channel_scan(self, bot): for channel in list(bot.channels.keys()): @@ -221,5 +236,13 @@ class BotChannels(): def mode(self, bot, trigger): return + def check_channel_bot(self, channel, allchan=False): + if str(channel).lower() in self.channels: + return True + elif allchan and str(channel).lower() in list(self.dict['list'].keys()): + return True + else: + return False + channels = BotChannels() diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index 3271c45..dcb8388 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -19,6 +19,7 @@ from .Logs import logs from .Config import config as botconfig from .Database import db as botdb from .Read import read as botread +from .Channels import channels as botchannels class SpiceBot_Commands_MainSection(StaticSection): @@ -38,7 +39,7 @@ class BotCommands(): 'nickname': {}, 'rule': {} }, - 'disabled': {} + 'disabled': {}, } self.module_files_parse() self.nickrules() @@ -67,17 +68,6 @@ class BotCommands(): return commandstype return None - def get_commands_disabled(self, channel): - if not len(list(self.dict['disabled'])): - self.dict['disabled'] = botdb.get_channel_value(channel, 'commands_disabled') or {} - return self.dict['disabled'] - - def check_commands_disabled(self, command, channel): - if command in list(self.get_commands_disabled(channel).keys()): - return True - else: - return False - def get_realcom(self, command, trigger_command_type): realcom = command @@ -92,18 +82,74 @@ class BotCommands(): return realcom - def set_command_disabled(self, command, channel, timestamp, reason, bywhom): - if not len(list(self.dict['disabled'])): - self.dict['disabled'] = botdb.get_channel_value(channel, 'commands_disabled') or {} - self.dict['disabled'][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} - botdb.set_channel_value(channel, 'commands_disabled', self.dict['disabled']) + def get_commands_disabled(self, target, disabletype="fully"): + disabletype = str(disabletype) + if disabletype not in list(self.dict['disabled'].keys()): + self.dict['disabled'][disabletype] = { + "users": {}, + "channels": {} + } + if botdb.check_nick_id(target): + if not len(list(self.dict['disabled'][disabletype]["users"])): + self.dict['disabled'][disabletype]["users"] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} + return self.dict['disabled'][disabletype]["users"] + elif botchannels.check_channel_bot(target, True): + if not len(list(self.dict['disabled'][disabletype]["channels"])): + self.dict['disabled'][disabletype]["channels"] = botdb.get_channel_value(target, 'commands_disabled') or {} + return self.dict['disabled'][disabletype]["channels"] + else: + raise Exception(str(target.lower()) + " appears to not be a valid user/channel") - def unset_command_disabled(self, command, channel): - if not len(list(self.dict['disabled'])): - self.dict['disabled'] = botdb.get_channel_value(channel, 'commands_disabled') or {} - if command in list(self.dict['disabled'].keys()): - del self.dict['disabled'][command] - botdb.set_channel_value(channel, 'commands_disabled', self.dict['disabled']) + def check_commands_disabled(self, command, target, disabletype="fully"): + if disabletype not in list(self.dict['disabled'].keys()): + self.dict['disabled'][disabletype] = { + "users": {}, + "channels": {} + } + if command in list(self.get_commands_disabled(target, disabletype).keys()): + return True + else: + return False + + def set_command_disabled(self, command, target, timestamp, reason, bywhom, disabletype="fully"): + if disabletype not in list(self.dict['disabled'].keys()): + self.dict['disabled'][disabletype] = { + "users": {}, + "channels": {} + } + if botdb.check_nick_id(target): + if not len(list(self.dict['disabled'][disabletype]["users"])): + self.dict['disabled'][disabletype]["users"] = botdb.get_nick_value(target, 'commands_disabled') or {} + self.dict['disabled'][disabletype]["users"][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} + botdb.set_nick_value(target, 'commands_disabled', self.dict['disabled'][disabletype]["users"]) + elif botchannels.check_channel_bot(target, True): + if not len(list(self.dict['disabled'][disabletype]["channels"])): + self.dict['disabled'][disabletype]["channels"] = botdb.get_channel_value(target, 'commands_disabled') or {} + self.dict['disabled'][disabletype]["channels"][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} + botdb.set_channel_value(target, 'commands_disabled', self.dict['disabled'][disabletype]["channels"]) + else: + raise Exception(str(target.lower()) + " appears to not be a valid user/channel") + + def unset_command_disabled(self, command, target, timestamp, reason, bywhom, disabletype="fully"): + if disabletype not in list(self.dict['disabled'].keys()): + self.dict['disabled'][disabletype] = { + "users": {}, + "channels": {} + } + if botdb.check_nick_id(target): + if not len(list(self.dict['disabled'][disabletype]["users"])): + self.dict['disabled'][disabletype]["users"] = botdb.get_nick_value(target, 'commands_disabled') or {} + if command in list(self.dict['disabled'][disabletype]["users"].keys()): + del self.dict['disabled'][disabletype]["users"][command] + botdb.set_nick_value(target, 'commands_disabled', self.dict['disabled'][disabletype]["users"]) + elif botchannels.check_channel_bot(target, True): + if not len(list(self.dict['disabled'][disabletype]["channels"])): + self.dict['disabled'][disabletype]["channels"] = botdb.get_channel_value(target, 'commands_disabled') or {} + if command in list(self.dict['disabled'][disabletype]["channels"].keys()): + del self.dict['disabled'][disabletype]["channels"][command] + botdb.set_channel_value(target, 'commands_disabled', self.dict['disabled'][disabletype]["channels"]) + else: + raise Exception(str(target.lower()) + " appears to not be a valid user/channel") def register(self, command_dict): diff --git a/sopel_modules/SpiceBot/Prerun.py b/sopel_modules/SpiceBot/Prerun.py index cacccd1..1a4d775 100644 --- a/sopel_modules/SpiceBot/Prerun.py +++ b/sopel_modules/SpiceBot/Prerun.py @@ -15,6 +15,7 @@ import spicemanip from .Tools import command_permissions_check from .Commands import commands as botcommands from .Database import db as botdb +from .Channels import channels as botchannels def prerun(t_command_type='module', t_command_subtype=None): @@ -52,9 +53,13 @@ def prerun(t_command_type='module', t_command_subtype=None): argsdict_list = trigger_argsdict_list(argsdict_default, and_split) # Run the function for all splits + runcount = 0 for argsdict in argsdict_list: + runcount += 1 trigger.sb = copy.deepcopy(argsdict) + trigger.sb["runcount"] = runcount + trigger.sb["args"], trigger.sb["hyphen_arg"] = trigger_hyphen_args(trigger.sb["args"]) if not trigger.sb["hyphen_arg"]: # check if anything prohibits the nick from running the command @@ -127,17 +132,45 @@ def trigger_runstatus(bot, trigger): if trigger.sb["hyphen_arg"]: return False + if trigger.sb["runcount"] > 1: + # check channel multirun blocks + if not trigger.is_privmsg: + channel_disabled_list = botcommands.get_commands_disabled(str(trigger.sender), "multirun") + if trigger.sb["realcom"] in list(channel_disabled_list.keys()): + reason = channel_disabled_list[trigger.sb["realcom"]]["reason"] + timestamp = channel_disabled_list[trigger.sb["realcom"]]["timestamp"] + bywhom = channel_disabled_list[trigger.sb["realcom"]]["disabledby"] + message = "The " + str(trigger.sb["com"]) + " command multirun usage was disabled by " + bywhom + " for " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) + return trigger_cant_run(bot, trigger, message) + + # don't run commands that are disabled for specific users + nick_disabled_list = botcommands.get_commands_disabled(str(trigger.nick), "multirun") + if trigger.sb["realcom"] in list(nick_disabled_list.keys()): + reason = nick_disabled_list[trigger.sb["realcom"]]["reason"] + timestamp = nick_disabled_list[trigger.sb["realcom"]]["timestamp"] + bywhom = nick_disabled_list[trigger.sb["realcom"]]["disabledby"] + message = "The " + str(trigger.sb["com"]) + " command was multirun unsage disabled by " + bywhom + " for " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) + return trigger_cant_run(bot, trigger, message) + # don't run commands that are disabled in channels if not trigger.is_privmsg: - commandused = trigger.sb["realcom"] - disabled_list = botdb.get_channel_value(trigger.sender, 'commands_disabled') or {} - if commandused in list(disabled_list.keys()): - reason = disabled_list[trigger.sb["realcom"]]["reason"] - timestamp = disabled_list[trigger.sb["realcom"]]["timestamp"] - bywhom = disabled_list[trigger.sb["realcom"]]["disabledby"] - message = "The " + str(trigger.sb["com"]) + " command was disabled by " + bywhom + " in " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) + channel_disabled_list = botcommands.get_commands_disabled(str(trigger.sender), "fully") + if trigger.sb["realcom"] in list(channel_disabled_list.keys()): + reason = channel_disabled_list[trigger.sb["realcom"]]["reason"] + timestamp = channel_disabled_list[trigger.sb["realcom"]]["timestamp"] + bywhom = channel_disabled_list[trigger.sb["realcom"]]["disabledby"] + message = "The " + str(trigger.sb["com"]) + " command was disabled by " + bywhom + " for " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) return trigger_cant_run(bot, trigger, message) + # don't run commands that are disabled for specific users + nick_disabled_list = botcommands.get_commands_disabled(str(trigger.nick), "fully") + if trigger.sb["realcom"] in list(nick_disabled_list.keys()): + reason = nick_disabled_list[trigger.sb["realcom"]]["reason"] + timestamp = nick_disabled_list[trigger.sb["realcom"]]["timestamp"] + bywhom = nick_disabled_list[trigger.sb["realcom"]]["disabledby"] + message = "The " + str(trigger.sb["com"]) + " command was disabled by " + bywhom + " for " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) + return trigger_cant_run(bot, trigger, message) + return True @@ -177,44 +210,102 @@ def trigger_hyphen_arg_handler(bot, trigger): else: bot.osd(trigger.sb["com"] + " is a valid command") return + elif trigger.sb["hyphen_arg"] in [ + 'enable', 'disable', + 'block', 'unblock', + "activate", "deactivate", + "on", "off" + ]: - elif trigger.sb["hyphen_arg"] in ['enable']: + target = spicemanip.main(trigger.sb["args"], 1) or None + if not target: + if trigger.sb["hyphen_arg"] in ['enable', 'unblock', "activate", "on"]: + bot.osd("Who/Where am I enabling " + str(trigger.sb["realcom"]) + " usage for?", trigger.sender) + else: + bot.osd("Who/Where am I disabling " + str(trigger.sb["realcom"]) + " usage for?", trigger.sender) + return + + if not botdb.check_nick_id(target) and not botchannels.check_channel_bot(target, True): + bot.osd("I don't know who/what " + str(target) + " is.", trigger.sender) + return if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): - bot.osd("I was unable to disable this command due to privilege issues.") + if trigger.sb["hyphen_arg"] in ['enable', 'unblock', "activate", "on"]: + bot.osd("I was unable to enable this command for " + str(target) + " due to privilege issues.") + else: + bot.osd("I was unable to disable this command for " + str(target) + " due to privilege issues.") return - if trigger.is_privmsg: - bot.osd("This command must be run in a channel you which to enable it in.", trigger.nick, 'notice') + if trigger.sb["hyphen_arg"] in ['enable', 'unblock', "activate", "on"]: + + if not botcommands.check_commands_disabled(trigger.sb["realcom"], target, "fully"): + bot.osd(trigger.sb["com"] + " is already enabled for " + str(target), trigger.nick, 'notice') + return + + botcommands.unset_command_disabled(trigger.sb["realcom"], target, "fully") + bot.osd(trigger.sb["com"] + " is now enabled for " + str(target)) return - if not botcommands.check_commands_disabled(trigger.sb["realcom"], trigger.sender): - bot.osd(trigger.sb["com"] + " is already enabled in " + str(trigger.sender), trigger.nick, 'notice') + else: + + if botcommands.check_commands_disabled(trigger.sb["realcom"], target, "fully"): + bot.osd(trigger.sb["com"] + " is already disabled for " + str(target), trigger.nick, 'notice') + return + + trailingmessage = spicemanip.main(trigger.sb["args"], 0) or "No reason given." + timestamp = str(datetime.datetime.utcnow()) + + botcommands.set_command_disabled(trigger.sb["realcom"], target, timestamp, trailingmessage, trigger.nick, "fully") + bot.osd(trigger.sb["com"] + " is now disabled for " + str(target)) return - botcommands.unset_command_disabled(trigger.sb["realcom"], trigger.sender) - bot.osd(trigger.sb["com"] + " is now enabled in " + str(trigger.sender)) - return + if trigger.sb["hyphen_arg"] in ['multirun', 'multiruns']: - elif trigger.sb["hyphen_arg"] in ['disable']: + onoff = spicemanip.main(trigger.sb["args"], 1) or None + if not onoff or onoff not in ['enable', 'unblock', "activate", "on"]: + bot.osd("Do you want to enable or disable " + str(trigger.sb["realcom"]) + " multirun usage?", trigger.sender) + return + + target = spicemanip.main(trigger.sb["args"], 1) or None + if not target: + if onoff in ['enable', 'unblock', "activate", "on"]: + bot.osd("Who/Where am I enabling " + str(trigger.sb["realcom"]) + " multirun usage for?", trigger.sender) + else: + bot.osd("Who/Where am I disabling " + str(trigger.sb["realcom"]) + " multirun usage for?", trigger.sender) + return + + if not botdb.check_nick_id(target) and not botchannels.check_channel_bot(target, True): + bot.osd("I don't know who/what " + str(target) + " is.", trigger.sender) + return if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): - bot.osd("I was unable to disable this command due to privilege issues.") + if onoff in ['enable', 'unblock', "activate", "on"]: + bot.osd("I was unable to enable multirun usage on " + str(trigger.sb["realcom"]) + " for " + str(target) + " due to privilege issues.") + else: + bot.osd("I was unable to enable multirun usage on " + str(trigger.sb["realcom"]) + " for " + str(target) + " due to privilege issues.") return - if trigger.is_privmsg: - bot.osd("This command must be run in a channel you which to disable it in.", trigger.nick, 'notice') + if onoff in ['enable', 'unblock', "activate", "on"]: + + if not botcommands.check_commands_disabled(trigger.sb["realcom"], target, "multirun"): + bot.osd(trigger.sb["com"] + " multirun is already enabled for " + str(target), trigger.nick, 'notice') + return + + botcommands.unset_command_disabled(trigger.sb["realcom"], target, "multirun") + bot.osd(trigger.sb["com"] + " multirun is now enabled for " + str(target)) return - if botcommands.check_commands_disabled(trigger.sb["realcom"], trigger.sender): - bot.osd(trigger.sb["com"] + " is already disabled in " + str(trigger.sender), trigger.nick, 'notice') + else: + + if botcommands.check_commands_disabled(trigger.sb["realcom"], target, "multirun"): + bot.osd(trigger.sb["com"] + " multirun is already disabled for " + str(target), trigger.nick, 'notice') + return + + trailingmessage = spicemanip.main(trigger.sb["args"], 0) or "No reason given." + timestamp = str(datetime.datetime.utcnow()) + + botcommands.set_command_disabled(trigger.sb["realcom"], target, timestamp, trailingmessage, trigger.nick, "multirun") + bot.osd(trigger.sb["com"] + " multirun is now disabled for " + str(target)) return - trailingmessage = spicemanip.main(trigger.sb["args"], 0) or "No reason given." - timestamp = str(datetime.datetime.utcnow()) - - botcommands.set_command_disabled(trigger.sb["realcom"], trigger.sender, timestamp, trailingmessage, trigger.nick) - bot.osd(trigger.sb["com"] + " is now disabled in " + str(trigger.sender)) - return - return diff --git a/sopel_modules/SpiceBot/Users.py b/sopel_modules/SpiceBot/Users.py index 6a54912..6fe13f4 100644 --- a/sopel_modules/SpiceBot/Users.py +++ b/sopel_modules/SpiceBot/Users.py @@ -231,5 +231,9 @@ class BotUsers(): def targetcheck(self): return True + # idk whois + if not botdb.check_nick_id(posstarget): + return + users = BotUsers()