diff --git a/bot.py b/bot.py index 1b74fca..0577c53 100755 --- a/bot.py +++ b/bot.py @@ -249,6 +249,26 @@ class Sopel(irc.Bot): else: self.write(['JOIN', channel, password]) + def ban(self, banmask, channel): + """ + Attempts to ban the recipient. + """ + banmask = Identifier(banmask) + banmask = tools.configureHostMask(banmask) + if banmask == "": + return + self.write(['MODE', channel, '+b', banmask]) + + def unban(self, banmask, channel): + """ + Attempts to unban the recipient. + """ + banmask = Identifier(banmask) + banmask = tools.configureHostMask(banmask) + if banmask == "": + return + self.write(['MODE', channel, '-b', banmask]) + def say(self, text, recipient, max_messages=1): """Send ``text`` as a PRIVMSG to ``recipient``. diff --git a/modules/adminchannel.py b/modules/adminchannel.py index 6445310..c7c3980 100755 --- a/modules/adminchannel.py +++ b/modules/adminchannel.py @@ -3,7 +3,7 @@ import re import formatting from module import commands, priority, OP, HALFOP, require_privilege, require_chanmsg, example -from tools import Identifier +from tools import Identifier, configureHostMask def default_mask(trigger): @@ -44,28 +44,6 @@ def kick(bot, trigger): bot.write(['KICK', channel, nick], reason) -def configureHostMask(mask): - if mask == '*!*@*': - return mask - if re.match('^[^.@!/]+$', mask) is not None: - return '%s!*@*' % mask - if re.match('^[^@!]+$', mask) is not None: - return '*!*@%s' % mask - - m = re.match('^([^!@]+)@$', mask) - if m is not None: - return '*!%s@*' % m.group(1) - - m = re.match('^([^!@]+)@([^@!]+)$', mask) - if m is not None: - return '*!%s@%s' % (m.group(1), m.group(2)) - - m = re.match('^([^!@]+)!(^[!@]+)@?$', mask) - if m is not None: - return '%s!%s@*' % (m.group(1), m.group(2)) - return '' - - @require_chanmsg @require_privilege(OP, 'You are not a channel operator.') @commands('ban') @@ -90,11 +68,7 @@ def ban(bot, trigger): return channel = opt banmask = text[2] - banmask = configureHostMask(banmask) - if banmask == '': - return - bot.write(['MODE', channel, '+b', banmask]) - + bot.ban(banmask, channel) @require_chanmsg @require_privilege(OP, 'You are not a channel operator.') @@ -118,11 +92,7 @@ def unban(bot, trigger): return channel = opt banmask = text[2] - banmask = configureHostMask(banmask) - if banmask == '': - return - bot.write(['MODE', channel, '-b', banmask]) - + bot.unban(banmask, channel) @require_chanmsg @require_privilege(OP, 'You are not a channel operator.') @@ -153,10 +123,7 @@ def kickban(bot, trigger): mask = text[3] reasonidx = 4 reason = ' '.join(text[reasonidx:]) - mask = configureHostMask(mask) - if mask == '': - return - bot.write(['MODE', channel, '+b', mask]) + bot.ban(mask, channel) bot.write(['KICK', channel, nick], reason) diff --git a/modules/banhe.py b/modules/banhe.py index a9d8a4c..5161703 100755 --- a/modules/banhe.py +++ b/modules/banhe.py @@ -18,7 +18,6 @@ def banhe(bot, trigger): """ print(trigger.group()) banhee, period = trigger.group(3), trigger.group(4) - trigger.set_nick(bot.nick) if not trigger.admin: period = 20 @@ -29,7 +28,7 @@ def banhe(bot, trigger): except (KeyError, ValueError, TypeError): period = 0 - modules.adminchannel.ban(bot, trigger) + bot.ban(banhee, trigger.sender) if period > 2592000: bot.reply("It's too big, Onii-chan.") if not period or period > 2592000: @@ -37,29 +36,32 @@ def banhe(bot, trigger): bot.say(f"Banned \x0304{banhee}\x03 for \x0309{str(period)}\x03 seconds.") time.sleep(period) - modules.adminchannel.unban(bot, trigger) + bot.unban(banhee, trigger.sender) bot.say(f"Unbanned \x0304{banhee}\x03") @require_admin @commands("banheall") -def banheall(bot, trigger): +def banheallCommand(bot, trigger): """ Ban them all, Johnny. """ period = trigger.group(2) - print(bot.nick) - trigger.set_nick(bot.nick) + banheall(bot, period, trigger.sender) + +def banheall(bot, period, channel): + """ + The real banhe. + """ conv = {'s':1, 'm':60, 'h':3600, 'd':86400} try: period = conv[period[-1]] * int(period[:-1]) except (KeyError, ValueError, TypeError): period = 0 - for nick in bot.privileges[trigger.sender].keys(): - trigger.set_group(f".banhe {nick}") - modules.adminchannel.ban(bot, trigger) + for nick in bot.privileges[channel].keys(): + bot.ban(nick, channel) if period > 2592000: bot.reply("It's too big, Onii-chan.") @@ -69,8 +71,7 @@ def banheall(bot, trigger): bot.say(f"Banned \x0304them all\x03 for \x0309{str(period)}\x03 seconds.") time.sleep(period) - for nick in bot.privileges[trigger.sender].keys(): - trigger.set_group(f".banhe {nick}") - modules.adminchannel.unban(bot, trigger) + for nick in bot.privileges[channel].keys(): + bot.unban(nick, channel) bot.say("Unbanned \x0304them all\x03") diff --git a/tools/__init__.py b/tools/__init__.py index b93e8ad..54070af 100755 --- a/tools/__init__.py +++ b/tools/__init__.py @@ -331,3 +331,28 @@ class SopelMemoryWithDefault(defaultdict): def contains(self, key): """Backwards compatability with 3.x, use `in` operator instead.""" return self.__contains__(key) + +def configureHostMask(mask): + """ + Returns a valid hostmask based on user input. + """ + if mask == '*!*@*': + return mask + if re.match('^[^.@!/]+$', mask) is not None: + return '%s!*@*' % mask + if re.match('^[^@!]+$', mask) is not None: + return '*!*@%s' % mask + + m = re.match('^([^!@]+)@$', mask) + if m is not None: + return '*!%s@*' % m.group(1) + + m = re.match('^([^!@]+)@([^@!]+)$', mask) + if m is not None: + return '*!%s@%s' % (m.group(1), m.group(2)) + + m = re.match('^([^!@]+)!(^[!@]+)@?$', mask) + if m is not None: + return '%s!%s@*' % (m.group(1), m.group(2)) + return '' +