topicmonitor: monitor when channel topic changes

comes with a migration to add topic tracking to IrcChannel
This commit is contained in:
Brian S. Stephan 2015-05-15 21:48:19 -05:00
parent b15afcade8
commit a3484b0d3a
3 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,48 @@
from __future__ import unicode_literals
import logging
from django.utils import timezone
from ircbot.lib import Plugin
from ircbot.models import IrcChannel
log = logging.getLogger('ircbot.ircplugins.topicmonitor')
class TopicMonitor(Plugin):
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
def start(self):
"""Set up the handlers."""
self.connection.reactor.add_global_handler('topic', handle_topic, -20)
super(TopicMonitor, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_handler('topic', handle_topic)
super(TopicMonitor, self).stop()
def handle_topic(connection, event):
"""Store topic changes in the channel model."""
channel = event.target
topic = event.arguments[0]
setter = event.source
log.debug("topic change '%s' by %s in %s", topic, setter, channel)
channel, c = IrcChannel.objects.get_or_create(name=channel)
channel.topic_msg = topic
channel.topic_time = timezone.now()
channel.topic_by = setter
channel.save()
plugin = TopicMonitor

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('ircbot', '0005_auto_20150514_2317'),
]
operations = [
migrations.AddField(
model_name='ircchannel',
name='topic_by',
field=models.CharField(default=b'', max_length=200),
),
migrations.AddField(
model_name='ircchannel',
name='topic_msg',
field=models.TextField(default=b''),
),
migrations.AddField(
model_name='ircchannel',
name='topic_time',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]

View File

@ -4,6 +4,7 @@ import logging
import re
from django.db import models
from django.utils import timezone
log = logging.getLogger('ircbot.models')
@ -51,6 +52,10 @@ class IrcChannel(models.Model):
name = models.CharField(max_length=200, unique=True)
autojoin = models.BooleanField(default=False)
topic_msg = models.TextField(default='')
topic_time = models.DateTimeField(default=timezone.now)
topic_by = models.CharField(max_length=200, default='')
def __unicode__(self):
"""String representation."""