From 1164a7c478db121f7a185f8d8a13f5810b53e469 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 19 Sep 2019 08:45:23 -0400 Subject: [PATCH] migrate user privs check --- sopel_modules/SpiceBot/Prerun.py | 12 +++---- sopel_modules/SpiceBot/Tools.py | 36 ------------------- sopel_modules/SpiceBot/Users.py | 36 ++++++++++++++++++- sopel_modules/SpiceBot_Core_Commands/Logs.py | 2 +- .../SpiceBot_Core_Commands/Messaging.py | 2 +- .../Runtime_Controls.py | 2 +- 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/sopel_modules/SpiceBot/Prerun.py b/sopel_modules/SpiceBot/Prerun.py index 8e95fce..feda81a 100644 --- a/sopel_modules/SpiceBot/Prerun.py +++ b/sopel_modules/SpiceBot/Prerun.py @@ -13,7 +13,7 @@ from word2number import w2n from sopel_modules.spicemanip import spicemanip -from .Tools import command_permissions_check, class_create +from .Tools import class_create from .Commands import commands as botcommands from .Database import db as botdb from .Channels import channels as botchannels @@ -282,7 +282,7 @@ def trigger_runstatus_query(bot, trigger, botcom): # Allow permissions for enabling and disabling commands via hyphenargs if botcom.dict["adminswitch"]: - if command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if botusers.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): return True else: botmessagelog.messagelog_error(botcom.dict["log_id"], "The admin switch (-a) is for use by authorized nicks ONLY.") @@ -348,7 +348,7 @@ def trigger_runstatus(bot, trigger, botcom): # Allow permissions for enabling and disabling commands via hyphenargs if botcom.dict["adminswitch"]: - if command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if botusers.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): return True else: botmessagelog.messagelog_error(botcom.dict["log_id"], "The admin switch (-a) is for use by authorized nicks ONLY.") @@ -430,7 +430,7 @@ def trigger_runstatus(bot, trigger, botcom): def trigger_cant_run(bot, trigger, botcom, message=None): if message: botmessagelog.messagelog_error(botcom.dict["log_id"], message) - if command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if botusers.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): botmessagelog.messagelog_error(botcom.dict["log_id"], "You however are authorized to bypass this warning with the (-a) admin switch.") return False @@ -532,7 +532,7 @@ def trigger_hyphen_arg_handler(bot, trigger, botcom): botmessagelog.messagelog_error(botcom.dict["log_id"], "I don't know who/what " + str(target) + " is.") return False - if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if not botusers.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): if target != trigger.nick: if botcom.dict["hyphen_arg"] in ['enable', 'unblock', "activate", "on"]: botmessagelog.messagelog_error(botcom.dict["log_id"], "I was unable to enable this command for " + str(target) + " due to privilege issues.") @@ -583,7 +583,7 @@ def trigger_hyphen_arg_handler(bot, trigger, botcom): botmessagelog.messagelog_error(botcom.dict["log_id"], "I don't know who/what " + str(target) + " is.") return False - if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if not botusers.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): if target != trigger.nick: if onoff in ['enable', 'unblock', "activate", "on"]: botmessagelog.messagelog_error(botcom.dict["log_id"], "I was unable to enable multirun usage on " + str(botcom.dict["comtext"]) + " for " + str(target) + " due to privilege issues.") diff --git a/sopel_modules/SpiceBot/Tools.py b/sopel_modules/SpiceBot/Tools.py index cc71c9a..823356d 100644 --- a/sopel_modules/SpiceBot/Tools.py +++ b/sopel_modules/SpiceBot/Tools.py @@ -18,7 +18,6 @@ from sopel_modules.spicemanip import spicemanip from .Logs import logs from .Config import config as botconfig -from .Users import users as botusers """Variable References""" @@ -57,41 +56,6 @@ def bot_privs(privtype): return botpriveval -def command_permissions_check(bot, trigger, privslist): - - nick_id = botusers.whois_ident(trigger.nick) - if bot.config.SpiceBot_regnick.regnick: - if int(nick_id) not in botusers.dict["identified"]: - return False - - commandrunconsensus = [] - - for botpriv in ["admins", "owner"]: - if botpriv in privslist: - botpriveval = bot_privs(botpriv) - if not inlist(trigger.nick, botpriveval): - commandrunconsensus.append('False') - else: - commandrunconsensus.append('True') - - if not trigger.is_privmsg: - for chanpriv in ['OP', 'HOP', 'VOICE', 'OWNER', 'ADMIN']: - if chanpriv in privslist: - chanpriveval = channel_privs(bot, trigger.sender, chanpriv) - if not inlist(trigger.nick, chanpriveval): - commandrunconsensus.append('False') - else: - commandrunconsensus.append('True') - - if not len(privslist): - commandrunconsensus.append('True') - - if 'True' not in commandrunconsensus: - return False - - return True - - """ Classes """ diff --git a/sopel_modules/SpiceBot/Users.py b/sopel_modules/SpiceBot/Users.py index 1763dd2..4ccf102 100644 --- a/sopel_modules/SpiceBot/Users.py +++ b/sopel_modules/SpiceBot/Users.py @@ -6,7 +6,7 @@ import sopel from sopel.tools import Identifier from .Database import db as botdb -from .Tools import is_number, inlist, similar, array_arrangesort +from .Tools import is_number, inlist, similar, array_arrangesort, bot_privs, channel_privs from sopel_modules.spicemanip import spicemanip @@ -524,5 +524,39 @@ class BotUsers(): elif outputtype == 'random': return spicemanip(validtargs, 'random') + def command_permissions_check(self, bot, trigger, privslist): + + nick_id = self.whois_ident(trigger.nick) + if bot.config.SpiceBot_regnick.regnick: + if int(nick_id) not in self.dict["identified"]: + return False + + commandrunconsensus = [] + + for botpriv in ["admins", "owner"]: + if botpriv in privslist: + botpriveval = bot_privs(botpriv) + if not inlist(trigger.nick, botpriveval): + commandrunconsensus.append('False') + else: + commandrunconsensus.append('True') + + if not trigger.is_privmsg: + for chanpriv in ['OP', 'HOP', 'VOICE', 'OWNER', 'ADMIN']: + if chanpriv in privslist: + chanpriveval = channel_privs(bot, trigger.sender, chanpriv) + if not inlist(trigger.nick, chanpriveval): + commandrunconsensus.append('False') + else: + commandrunconsensus.append('True') + + if not len(privslist): + commandrunconsensus.append('True') + + if 'True' not in commandrunconsensus: + return False + + return True + users = BotUsers() diff --git a/sopel_modules/SpiceBot_Core_Commands/Logs.py b/sopel_modules/SpiceBot_Core_Commands/Logs.py index 2188961..f55241d 100644 --- a/sopel_modules/SpiceBot_Core_Commands/Logs.py +++ b/sopel_modules/SpiceBot_Core_Commands/Logs.py @@ -16,7 +16,7 @@ import sopel_modules.SpiceBot as SpiceBot @sopel.module.nickname_commands('logs', 'debug') def bot_command_logs(bot, trigger, botcom): - if not SpiceBot.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if not SpiceBot.users.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): SpiceBot.messagelog.messagelog_error(botcom.dict["log_id"], "I was unable to process this Bot Nick command due to privilege issues.") return diff --git a/sopel_modules/SpiceBot_Core_Commands/Messaging.py b/sopel_modules/SpiceBot_Core_Commands/Messaging.py index 510a38d..29e59a6 100644 --- a/sopel_modules/SpiceBot_Core_Commands/Messaging.py +++ b/sopel_modules/SpiceBot_Core_Commands/Messaging.py @@ -29,7 +29,7 @@ def bot_command_privmsg(bot, trigger, botcom): def bot_command_process(bot, trigger, botcom): - if not SpiceBot.command_permissions_check(bot, trigger, ['admins', 'owner']): + if not SpiceBot.users.command_permissions_check(bot, trigger, ['admins', 'owner']): SpiceBot.messagelog.messagelog_error(botcom.dict["log_id"], "I was unable to process this Bot Nick command due to privilege issues.") return diff --git a/sopel_modules/SpiceBot_Core_Commands/Runtime_Controls.py b/sopel_modules/SpiceBot_Core_Commands/Runtime_Controls.py index ed7ac5b..3294642 100644 --- a/sopel_modules/SpiceBot_Core_Commands/Runtime_Controls.py +++ b/sopel_modules/SpiceBot_Core_Commands/Runtime_Controls.py @@ -13,7 +13,7 @@ import sopel_modules.SpiceBot as SpiceBot @sopel.module.nickname_commands('update') def nickname_comand_update(bot, trigger, botcom): - if not SpiceBot.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): + if not SpiceBot.users.command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): SpiceBot.messagelog.messagelog_error(botcom.dict["log_id"], "I was unable to process this Bot Nick command due to privilege issues.") return