"""Library and convenience methods for the IRC bot and plugins.""" from __future__ import unicode_literals import logging import irc.client from django.core.exceptions import ObjectDoesNotExist from ircbot.models import BotUser 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 has_permission(source, permission): """Check if the provided event source is a bot admin.""" try: bot_user = BotUser.objects.get(nickmask=source) log.debug("found bot user {0:s}".format(bot_user)) except BotUser.DoesNotExist: log.debug("could not find bot user for {0:s}".format(source)) return False try: django_user = bot_user.user if django_user.has_perm(permission): log.debug("bot user {0:s} has requested permission {1:s}".format(bot_user, permission)) return True except ObjectDoesNotExist: log.error("could not find django user for bot user {0:s}".format(bot_user)) return False log.debug("bot user {0:s} does not have requested permission {1:s}".format(bot_user, permission)) 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