bornhack-website/src/ircbot/irc3module.py

100 lines
3.9 KiB
Python
Raw Normal View History

2017-01-30 11:05:11 +00:00
import irc3
from ircbot.models import OutgoingIrcMessage
from django.conf import settings
2017-02-01 00:09:21 +00:00
from django.utils import timezone
2017-03-23 17:32:13 +00:00
import logging
logger = logging.getLogger("bornhack.%s" % __name__)
2017-01-30 11:05:11 +00:00
@irc3.plugin
class Plugin(object):
"""BornHack IRC3 class"""
requires = [
'irc3.plugins.core', # makes the bot able to connect to an irc server and do basic irc stuff
'irc3.plugins.userlist', # maintains a convenient list of the channels the bot is in and their users
'irc3.plugins.command', # what does this do?
]
def __init__(self, bot):
self.bot = bot
###############################################################################################
### builtin irc3 event methods
def server_ready(self, **kwargs):
"""triggered after the server sent the MOTD (require core plugin)"""
2017-03-23 17:32:13 +00:00
logger.debug("inside server_ready(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
def connection_lost(self, **kwargs):
"""triggered when connection is lost"""
2017-03-23 17:32:13 +00:00
logger.debug("inside connection_lost(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
def connection_made(self, **kwargs):
"""triggered when connection is up"""
2017-03-23 17:32:13 +00:00
logger.debug("inside connection_made(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
###############################################################################################
### decorated irc3 event methods
@irc3.event(irc3.rfc.JOIN_PART_QUIT)
def on_join_part_quit(self, **kwargs):
"""triggered when there is a join part or quit on a channel the bot is in"""
2017-03-23 17:32:13 +00:00
logger.debug("inside on_join_part_quit(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
if self.bot.nick == kwargs['mask'].split("!")[0] and kwargs['channel'] == "#tirsdagsfilm":
self.bot.loop.call_later(1, self.bot.get_outgoing_messages)
@irc3.event(irc3.rfc.PRIVMSG)
def on_privmsg(self, **kwargs):
"""triggered when a privmsg is sent to the bot or to a channel the bot is in"""
2017-03-23 17:32:13 +00:00
logger.debug("inside on_privmsg(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
@irc3.event(irc3.rfc.KICK)
def on_kick(self, **kwargs):
2017-03-23 17:32:13 +00:00
logger.debug("inside on_kick(), kwargs: %s" % kwargs)
2017-01-30 11:05:11 +00:00
###############################################################################################
### custom irc3 methods
2017-01-30 11:05:11 +00:00
@irc3.extend
def get_outgoing_messages(self):
"""
This method gets unprocessed OutgoingIrcMessage objects and attempts to send them to
the target channel. Messages are skipped if the bot is not in the channel.
"""
#logger.debug("inside get_outgoing_messages()")
2017-02-01 00:09:21 +00:00
for msg in OutgoingIrcMessage.objects.filter(processed=False).order_by('created'):
2017-07-03 18:53:44 +00:00
logger.info("processing irc message to %s: %s" % (msg.target, msg.message))
# if this message expired mark it as expired and processed without doing anything
if msg.timeout < timezone.now():
2017-07-03 18:53:44 +00:00
logger.info("this message is expired, marking it as such instead of sending it to irc")
msg.expired=True
msg.processed=True
msg.save()
continue
# is this message for a channel or a nick?
2017-01-30 11:05:11 +00:00
if msg.target[0] == "#" and msg.target in self.bot.channels:
2017-07-03 18:53:44 +00:00
logger.info("sending privmsg to %s: %s" % (msg.target, msg.message))
2017-01-30 11:05:11 +00:00
self.bot.privmsg(msg.target, msg.message)
msg.processed=True
msg.save()
elif msg.target:
2017-07-03 18:53:44 +00:00
logger.info("sending privmsg to %s: %s" % (msg.target, msg.message))
self.bot.privmsg(msg.target, msg.message)
msg.processed=True
msg.save()
2017-01-30 11:05:11 +00:00
else:
2017-03-23 17:32:13 +00:00
logger.warning("skipping message to %s" % msg.target)
2017-01-30 11:05:11 +00:00
# call this function again in X seconds
self.bot.loop.call_later(settings.IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS, self.bot.get_outgoing_messages)
2017-01-30 11:05:11 +00:00