add channel model, support autojoining

known channels in the database can be set to autojoin when the bot
starts (when it gets the MOTD, because i couldn't find an "on_connect"
event)
This commit is contained in:
Brian S. Stephan 2015-05-10 10:02:25 -05:00
parent 0ea9c3a164
commit bd3ffd4067
4 changed files with 64 additions and 1 deletions

8
dr_botzo/ircbot/admin.py Normal file
View File

@ -0,0 +1,8 @@
"""Manage ircbot models in the admin interface."""
from django.contrib import admin
from ircbot.models import IrcChannel
admin.site.register(IrcChannel)

View File

@ -1,5 +1,6 @@
"""Provide the base IRC client bot which other code can latch onto.""" """Provide the base IRC client bot which other code can latch onto."""
import logging
import ssl import ssl
import sys import sys
@ -10,6 +11,11 @@ from irc.connection import Factory
from irc.dict import IRCDict from irc.dict import IRCDict
import irc.modes import irc.modes
from ircbot.models import IrcChannel
log = logging.getLogger('ircbot.bot')
class IRCBot(irc.client.SimpleIRCClient): class IRCBot(irc.client.SimpleIRCClient):
"""A single-server IRC bot class.""" """A single-server IRC bot class."""
@ -32,7 +38,7 @@ class IRCBot(irc.client.SimpleIRCClient):
self._realname = settings.IRCBOT_REALNAME self._realname = settings.IRCBOT_REALNAME
# handlers # handlers
for i in ['disconnect', 'join', 'kick', 'mode', 'namreply', 'nick', 'part', 'quit']: for i in ['disconnect', 'join', 'kick', 'mode', 'namreply', 'nick', 'part', 'quit', 'endofmotd']:
self.connection.add_global_handler(i, getattr(self, '_on_' + i), -20) self.connection.add_global_handler(i, getattr(self, '_on_' + i), -20)
def _connected_checker(self): def _connected_checker(self):
@ -91,6 +97,13 @@ class IRCBot(irc.client.SimpleIRCClient):
# Mode on self... XXX # Mode on self... XXX
pass pass
def _on_endofmotd(self, c, e):
"""Join autojoin channels when the MOTD is over."""
for chan in IrcChannel.objects.filter(autojoin=True):
log.info(u"autojoining %s", chan.name)
self.connection.join(chan)
def _on_namreply(self, c, e): def _on_namreply(self, c, e):
"""Get the list of names in a channel. """Get the list of names in a channel.

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='IrcChannel',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(unique=True, max_length=200)),
('autojoin', models.BooleanField(default=False)),
],
),
]

21
dr_botzo/ircbot/models.py Normal file
View File

@ -0,0 +1,21 @@
"""Track basic IRC settings and similar."""
import logging
from django.db import models
log = logging.getLogger('ircbot.models')
class IrcChannel(models.Model):
"""Track channel settings."""
name = models.CharField(max_length=200, unique=True)
autojoin = models.BooleanField(default=False)
def __unicode__(self):
"""String representation."""
return u"{0:s}".format(self.name)