From a94d35f0f1e26d1286536783de325b3332cb23c8 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 28 Jan 2019 23:50:43 +0000 Subject: [PATCH] Add `channel_access.py`, to give permissions to users in a channel (similar to chanserv) --- modules/channel_access.py | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 modules/channel_access.py diff --git a/modules/channel_access.py b/modules/channel_access.py new file mode 100644 index 00000000..1eb1c433 --- /dev/null +++ b/modules/channel_access.py @@ -0,0 +1,62 @@ +from src import ModuleManager, utils + +class Module(ModuleManager.BaseModule): + _name = "ChanAccess" + + @utils.hook("preprocess.command") + def preprocess_command(self, event): + require_access = event["hook"].get_kwarg("require_access") + if event["is_channel"] and require_access: + access = event["target"].get_user_setting(event["user"].id, + "access", []) + identified_account = event["user"].get_identified_account() + + if not require_access in access or not identified_account: + return "You do not have permission to do this" + else: + return utils.consts.PERMISSION_FORCE_SUCCESS + + @utils.hook("received.command.access", min_args=1, channel_only=True) + def access(self, event): + """ + :help: Show/modify channel access for a user + :usage: list + :usage: add + :usage: remove + :usage: set + :require_mode: high + """ + subcommand = event["args_split"][0].lower() + target = event["server"].get_user(event["args_split"][1]) + access = event["target"].get_user_setting(target.id, "access", []) + + if subcommand == "list": + event["stdout"].write("Access for %s: %s" % (target.nickname, + " ".join(access))) + elif subcommand == "set": + if not len(event["args_split"]) > 2: + raise utils.EventError("Please provide a list of permissions") + event["target"].set_user_setting(target.id, "access", + event["args_split"][2:]) + elif subcommand == "add": + if not len(event["args_split"]) > 2: + raise utils.EventError("Please provide a list of permissions") + for acc in event["args_split"][2:]: + if acc in access: + raise utils.EventError("%s already has '%s' permission" % ( + target.nickname, acc)) + access.append(acc) + event["target"].set_user_setting(target.id, "access", access) + event["stdout"].write("Added permission to %s: %s" % ( + target.nickname, " ".join(event["args_split"][2:]))) + elif subcommand == "remove": + if not len(event["args_split"]) > 2: + raise utils.EventError("Please provide a list of permissions") + for acc in event["args_split"][2:]: + if not acc in access: + raise utils.EventError("%s does not have '%s' permission" % + (target.nickname, acc)) + access.remove(acc) + event["target"].set_user_setting(target.id, "access", access) + event["stdout"].write("Removed permission from %s: %s" % ( + target.nickname, " ".join(event["args_split"][2:])))