From 0428c30faf156f5f97ab76b008701bd0da0b3053 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 14 May 2015 21:39:20 -0500 Subject: [PATCH] add Alias support allows stuff like ^!die$ -> !quit no recursion (yet? or maybe i'll never bother), but this should allow the basic aliases --- dr_botzo/ircbot/admin.py | 3 ++- dr_botzo/ircbot/bot.py | 13 ++++++++++++- dr_botzo/ircbot/migrations/0004_alias.py | 22 ++++++++++++++++++++++ dr_botzo/ircbot/models.py | 24 ++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 dr_botzo/ircbot/migrations/0004_alias.py diff --git a/dr_botzo/ircbot/admin.py b/dr_botzo/ircbot/admin.py index 4738d93..b06ff06 100644 --- a/dr_botzo/ircbot/admin.py +++ b/dr_botzo/ircbot/admin.py @@ -2,9 +2,10 @@ from django.contrib import admin -from ircbot.models import BotAdmin, IrcChannel, IrcPlugin +from ircbot.models import Alias, BotAdmin, IrcChannel, IrcPlugin +admin.site.register(Alias) admin.site.register(BotAdmin) admin.site.register(IrcChannel) admin.site.register(IrcPlugin) diff --git a/dr_botzo/ircbot/bot.py b/dr_botzo/ircbot/bot.py index 5deabb0..4d09922 100644 --- a/dr_botzo/ircbot/bot.py +++ b/dr_botzo/ircbot/bot.py @@ -15,7 +15,7 @@ from irc.dict import IRCDict import irc.modes import ircbot.lib as ircbotlib -from ircbot.models import IrcChannel, IrcPlugin +from ircbot.models import Alias, IrcChannel, IrcPlugin log = logging.getLogger('ircbot.bot') @@ -98,6 +98,17 @@ class DrReactor(irc.client.Reactor): log.debug("EVENT: e[%s] s[%s] t[%s] a[%s]", event.type, event.source, event.target, event.arguments) + # only do aliasing for pubmsg/privmsg + if event.type in ['pubmsg', 'privmsg']: + what = event.arguments[0] + log.debug(u"checking for alias for %s", what) + + for alias in Alias.objects.all(): + repl = alias.replace(what) + if repl: + # we found an alias for our given string, doing a replace + event.arguments[0] = repl + with self.mutex: # doing regex version first as it has the potential to be more specific log.debug(u"checking regex handlers for %s", event.type) diff --git a/dr_botzo/ircbot/migrations/0004_alias.py b/dr_botzo/ircbot/migrations/0004_alias.py new file mode 100644 index 0000000..82a8753 --- /dev/null +++ b/dr_botzo/ircbot/migrations/0004_alias.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ircbot', '0003_auto_20150512_1934'), + ] + + operations = [ + migrations.CreateModel( + name='Alias', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('pattern', models.CharField(unique=True, max_length=200)), + ('replacement', models.CharField(max_length=200)), + ], + ), + ] diff --git a/dr_botzo/ircbot/models.py b/dr_botzo/ircbot/models.py index 46e98e6..8dd58d3 100644 --- a/dr_botzo/ircbot/models.py +++ b/dr_botzo/ircbot/models.py @@ -1,6 +1,7 @@ """Track basic IRC settings and similar.""" import logging +import re from django.db import models @@ -8,6 +9,29 @@ from django.db import models log = logging.getLogger('ircbot.models') +class Alias(models.Model): + + """Allow for aliasing of arbitrary regexes to normal supported commands.""" + + pattern = models.CharField(max_length=200, unique=True) + replacement = models.CharField(max_length=200) + + class Meta: + verbose_name_plural = "aliases" + + def __unicode__(self): + """String representation.""" + + return u"{0:s} -> {1:s}".format(self.pattern, self.replacement) + + def replace(self, what): + command = None + if re.search(self.pattern, what, flags=re.IGNORECASE): + command = re.sub(self.pattern, self.replacement, what, flags=re.IGNORECASE) + + return command + + class BotAdmin(models.Model): """Configure admins, which can do things through the bot that others can't."""