add Alias support
allows stuff like ^!die$ -> !quit no recursion (yet? or maybe i'll never bother), but this should allow the basic aliases
This commit is contained in:
parent
e7de9f840d
commit
0428c30faf
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -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."""
|
||||
|
|
Loading…
Reference in New Issue