2015-05-12 20:45:18 -05:00
|
|
|
"""Library and convenience methods for the IRC bot and plugins."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2015-05-12 22:33:09 -05:00
|
|
|
import irc.client
|
|
|
|
|
2015-06-20 10:08:51 -05:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
2015-06-20 09:31:28 -05:00
|
|
|
from ircbot.models import BotUser
|
2015-05-12 20:45:18 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-01-16 17:58:11 -06:00
|
|
|
log.info("initialized %s", self.__class__.__name__)
|
2015-05-12 20:45:18 -05:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Initialization stuff here --- global handlers, configs from database, so on."""
|
2016-01-16 17:58:11 -06:00
|
|
|
log.info("started %s", self.__class__.__name__)
|
2015-05-12 20:45:18 -05:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Teardown stuff here --- unregister handlers, for example."""
|
2016-01-16 17:58:11 -06:00
|
|
|
log.info("stopped %s", self.__class__.__name__)
|
2015-05-12 20:45:18 -05:00
|
|
|
|
2015-06-16 20:27:44 -05:00
|
|
|
def _unencode_xml(self, text):
|
|
|
|
"""Convert <, >, & to their real entities."""
|
|
|
|
text = text.replace('<', '<')
|
|
|
|
text = text.replace('>', '>')
|
|
|
|
text = text.replace('&', '&')
|
|
|
|
return text
|
|
|
|
|
2015-05-12 20:45:18 -05:00
|
|
|
|
2015-06-20 10:08:51 -05:00
|
|
|
def has_permission(source, permission):
|
2015-05-12 20:45:18 -05:00
|
|
|
"""Check if the provided event source is a bot admin."""
|
2015-06-20 09:27:50 -05:00
|
|
|
try:
|
2015-06-20 09:31:28 -05:00
|
|
|
bot_user = BotUser.objects.get(nickmask=source)
|
2016-01-16 19:36:10 -06:00
|
|
|
log.debug("found bot user %s", bot_user)
|
2015-06-20 09:31:28 -05:00
|
|
|
except BotUser.DoesNotExist:
|
2016-01-16 19:36:10 -06:00
|
|
|
log.debug("could not find bot user for %s", source)
|
2015-06-20 09:27:50 -05:00
|
|
|
return False
|
|
|
|
|
2015-06-20 10:08:51 -05:00
|
|
|
try:
|
|
|
|
django_user = bot_user.user
|
|
|
|
if django_user.has_perm(permission):
|
2016-01-16 19:36:10 -06:00
|
|
|
log.debug("bot user %s has requested permission %s", bot_user, permission)
|
2015-06-20 10:08:51 -05:00
|
|
|
return True
|
|
|
|
except ObjectDoesNotExist:
|
2016-01-16 19:36:10 -06:00
|
|
|
log.error("could not find django user for bot user %s", bot_user)
|
2015-06-20 10:08:51 -05:00
|
|
|
return False
|
|
|
|
|
2016-01-16 19:36:10 -06:00
|
|
|
log.debug("bot user %s does not have requested permission %s", bot_user, permission)
|
2015-06-20 10:08:51 -05:00
|
|
|
return False
|
2015-05-12 22:33:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2017-03-10 18:16:35 -06:00
|
|
|
|
|
|
|
|
|
|
|
def most_specific_message(event):
|
|
|
|
"""Provides the "most specific" message for a pubmsg/privmsg.
|
|
|
|
|
|
|
|
Specificity is defined as the raw message, or the message after a "bot: blah" address.
|
|
|
|
"""
|
|
|
|
if event.addressed:
|
|
|
|
return event.addressed_msg
|
|
|
|
else:
|
|
|
|
return event.original_msg
|