common idiom that'll only get used more and more, so might as well make a library method of it
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
"""Library and convenience methods for the IRC bot and plugins."""
|
|
|
|
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 is_admin(source):
|
|
"""Check if the provided event source is a bot admin."""
|
|
|
|
if source in BotAdmin.objects.values_list('nickmask', flat=True):
|
|
log.debug(u"in is_admin; True")
|
|
return True
|
|
log.debug(u"in is_admin; False")
|
|
return False
|
|
|
|
|
|
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
|