From 01192e0ddf80d8b9dcd13d6e472f85561daca033 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Mon, 12 May 2014 22:33:47 -0700 Subject: [PATCH] add kick/ban/unban helper functions --- core/main.py | 13 ++++++++++++- plugins/crowdcontrol.py | 23 +++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/core/main.py b/core/main.py index 38ebfa8..22abb4a 100644 --- a/core/main.py +++ b/core/main.py @@ -35,11 +35,22 @@ class Input(dict): def notice(msg): conn.cmd('NOTICE', [nick, msg]) + def kick(target=None, reason=None): + conn.cmd('KICK', [chan, target or nick, reason or '']) + + def ban(target=None): + conn.cmd('MODE', [chan, '+b', target or host]) + + def unban(target=None): + conn.cmd('MODE', [chan, '-b', target or host]) + + dict.__init__(self, conn=conn, raw=raw, prefix=prefix, command=command, params=params, nick=nick, user=user, host=host, paraml=paraml, msg=msg, server=conn.server, chan=chan, notice=notice, say=say, reply=reply, pm=pm, bot=bot, - me=me, set_nick=set_nick, lastparam=paraml[-1]) + kick=kick, ban=ban, unban=unban, me=me, + set_nick=set_nick, lastparam=paraml[-1]) # make dict keys accessible as attributes def __getattr__(self, key): diff --git a/plugins/crowdcontrol.py b/plugins/crowdcontrol.py index 19937f6..3724b10 100644 --- a/plugins/crowdcontrol.py +++ b/plugins/crowdcontrol.py @@ -15,16 +15,19 @@ from util import hook @hook.regex(r'.*') -def crowdcontrol(inp, nick='', chan='', host='', bot=None, conn=None): +def crowdcontrol(inp, kick=None, ban=None, unban=None, reply=None, bot=None): inp = inp.group(0) for rule in bot.config.get('crowdcontrol', []): if re.search(rule['re'], inp) is not None: - if 'ban_length' in rule and rule['ban_length'] != 0: - conn.cmd("MODE", [chan, "+b", host]) - if rule['kick']: - conn.cmd('KICK', [chan, nick, rule['msg']]) - if 'ban_length' in rule and rule['ban_length'] > 0: - time.sleep(rule['ban_length']) - conn.cmd("MODE", [chan, "-b", host]) - if not rule['kick']: - return rule['msg'] + should_kick = rule.get('kick', 0) + ban_length = rule.get('ban_length', 0) + reason = rule.get('msg') + if ban_length != 0: + ban() + if should_kick: + kick(reason=reason) + elif 'msg' in rule: + reply(reason) + if ban_length > 0: + time.sleep(ban_length) + unban()