sets up the foreign key and changes how is_admin() works, though it will be going away pretty soon, i just need to do this migration in parts so as to not confuse django too much
72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
"""Library and convenience methods for the IRC bot and plugins."""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import logging
|
|
|
|
import irc.client
|
|
|
|
from ircbot.models import BotAdmin
|
|
|
|
|
|
log = logging.getLogger('ircbot.lib')
|
|
|
|
|
|
class Plugin(object):
|
|
|
|
"""Plugin base class."""
|
|
|
|
def __init__(self, bot, connection, event):
|
|
"""Initialization stuff here --- global handlers, configs from database, so on."""
|
|
|
|
self.bot = bot
|
|
self.connection = connection
|
|
self.event = event
|
|
|
|
log.info(u"initialized %s", self.__class__.__name__)
|
|
|
|
def start(self):
|
|
"""Initialization stuff here --- global handlers, configs from database, so on."""
|
|
|
|
log.info(u"started %s", self.__class__.__name__)
|
|
|
|
def stop(self):
|
|
"""Teardown stuff here --- unregister handlers, for example."""
|
|
|
|
log.info(u"stopped %s", self.__class__.__name__)
|
|
|
|
def _unencode_xml(self, text):
|
|
"""Convert <, >, & to their real entities."""
|
|
|
|
text = text.replace('<', '<')
|
|
text = text.replace('>', '>')
|
|
text = text.replace('&', '&')
|
|
return text
|
|
|
|
|
|
def is_admin(source):
|
|
"""Check if the provided event source is a bot admin."""
|
|
|
|
try:
|
|
bot_user = BotAdmin.objects.get(nickmask=source)
|
|
log.debug("found bot user {0:s}".format(bot_user))
|
|
except BotAdmin.DoesNotExist:
|
|
log.debug("could not find bot user for {0:s}".format(source))
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def reply_destination_for_event(event):
|
|
"""Get the "natural" reply destination for an event.
|
|
|
|
If the event appears to be from a person within a channel, the channel
|
|
is the reply destination. Otherwise, the source (assumed to be the speaker
|
|
in a privmsg)'s nick is the reply destination.
|
|
"""
|
|
|
|
if irc.client.is_channel(event.target):
|
|
return event.target
|
|
else:
|
|
return irc.client.NickMask(event.source).nick
|