From 1127682872c6c6f7a914b53a15b2f99816ee959f Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 18 Apr 2016 16:50:19 +0100 Subject: [PATCH] added a way for a command callback to neatly ask to only be available for people with a rank or above, added a channel_op module to utilise this. --- IRCChannel.py | 7 +++++++ modules/channel_op.py | 44 +++++++++++++++++++++++++++++++++++++++++++ modules/check_mode.py | 15 +++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 modules/channel_op.py create mode 100644 modules/check_mode.py diff --git a/IRCChannel.py b/IRCChannel.py index 54a174db..f48f9d96 100644 --- a/IRCChannel.py +++ b/IRCChannel.py @@ -26,6 +26,8 @@ class Channel(object): self.users.add(user) def remove_user(self, user): self.users.remove(user) + def has_user(self, user): + return user in self.users def add_mode(self, mode, args=None): if not mode in self.modes: self.modes[mode] = set([]) @@ -59,6 +61,11 @@ class Channel(object): self.server.send_message(self.name, text) def send_mode(self, mode=None, target=None): self.server.send_mode(self.name, mode, target) + def send_kick(self, target, reason=None): + self.server.send_kick(self.name, target, reason) + def send_ban(self, hostmask): + self.server.send_mode(self.name, "+b", hostmask) + def mode_or_above(self, nickname, mode): mode_orders = list(self.server.mode_prefixes.values()) mode_index = mode_orders.index(mode) diff --git a/modules/channel_op.py b/modules/channel_op.py new file mode 100644 index 00000000..a454d791 --- /dev/null +++ b/modules/channel_op.py @@ -0,0 +1,44 @@ + + +class Module(object): + _name = "Channel Op" + def __init__(self, bot): + self.bot = bot + bot.events.on("received").on("command").on("kick", "k" + ).hook(self.kick, channel_only=True, require_mode="o", + min_args=1) + bot.events.on("received").on("command").on("ban" + ).hook(self.ban, channel_only=True, require_mode="o", + min_args=1) + bot.events.on("received").on("command").on("kickban", "kb" + ).hook(self.kickban, channel_only=True, require_mode="o", + min_args=1) + + def kick(self, event): + target = event["args_split"][0] + target_user = event["server"].get_user(target) + if event["args_split"][1:]: + reason = " ".join(event["args_split"][1:]) + else: + reason = None + event["stderr"].set_prefix("Kick") + if event["target"].has_user(target_user): + if not event["server"].is_own_nickname(target): + event["target"].send_kick(target, reason) + else: + event["stderr"].write("Nope.") + else: + event["stderr"].write("That user is not in this channel") + + def ban(self, event): + target_user = event["server"].get_user(event["args_split"][0]) + if event["target"].has_user(target_user): + event["target"].send_ban("*!%s@%s" % (target_user.username, + target_user.hostname)) + else: + event["target"].send_ban(event["args_split"][0]) + + def kickban(self, event): + if event["server"].has_user(event["args_split"][0]): + self.ban(event) + self.kick(event) diff --git a/modules/check_mode.py b/modules/check_mode.py new file mode 100644 index 00000000..97da52d7 --- /dev/null +++ b/modules/check_mode.py @@ -0,0 +1,15 @@ + + +class Module(object): + def __init__(self, bot): + self.bot = bot + bot.events.on("preprocess").on("command").hook( + self.preprocess_command) + + def preprocess_command(self, event): + if event["is_channel"] and event["hook"].kwargs.get( + "require_mode"): + required_mode = event["hook"].kwargs.get("require_mode")[0] + if not event["target"].mode_or_above(event["user"].nickname, + required_mode): + return "You do not have permission to do this"