"""Library and convenience methods for the IRC bot and plugins."""

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("initialized %s", self.__class__.__name__)

    def start(self):
        """Initialization stuff here --- global handlers, configs from database, so on."""

        log.info("started %s", self.__class__.__name__)

    def stop(self):
        """Teardown stuff here --- unregister handlers, for example."""

        log.info("stopped %s", self.__class__.__name__)

    def _unencode_xml(self, text):
        """Convert <, >, & to their real entities."""

        text = text.replace('&lt;', '<')
        text = text.replace('&gt;', '>')
        text = text.replace('&amp;', '&')
        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 %s", bot_user)
    except BotUser.DoesNotExist:
        log.debug("could not find bot user for %s", source)
        return False

    try:
        django_user = bot_user.user
        if django_user.has_perm(permission):
            log.debug("bot user %s has requested permission %s", bot_user, permission)
            return True
    except ObjectDoesNotExist:
        log.error("could not find django user for bot user %s", bot_user)
        return False

    log.debug("bot user %s does not have requested permission %s", 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