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:
parent
0ea9c3a164
commit
bd3ffd4067
8
dr_botzo/ircbot/admin.py
Normal file
8
dr_botzo/ircbot/admin.py
Normal 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)
|
@ -1,5 +1,6 @@
|
||||
"""Provide the base IRC client bot which other code can latch onto."""
|
||||
|
||||
import logging
|
||||
import ssl
|
||||
import sys
|
||||
|
||||
@ -10,6 +11,11 @@ from irc.connection import Factory
|
||||
from irc.dict import IRCDict
|
||||
import irc.modes
|
||||
|
||||
from ircbot.models import IrcChannel
|
||||
|
||||
|
||||
log = logging.getLogger('ircbot.bot')
|
||||
|
||||
|
||||
class IRCBot(irc.client.SimpleIRCClient):
|
||||
"""A single-server IRC bot class."""
|
||||
@ -32,7 +38,7 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||
self._realname = settings.IRCBOT_REALNAME
|
||||
|
||||
# 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)
|
||||
|
||||
def _connected_checker(self):
|
||||
@ -91,6 +97,13 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||
# Mode on self... XXX
|
||||
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):
|
||||
"""Get the list of names in a channel.
|
||||
|
||||
|
21
dr_botzo/ircbot/migrations/0001_initial.py
Normal file
21
dr_botzo/ircbot/migrations/0001_initial.py
Normal 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
21
dr_botzo/ircbot/models.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user