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
2017-07-03 21:02:00 +00:00
# wait 5 secs before starting the loop to check for outgoing messages
self . bot . loop . call_later ( settings . IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS , self . bot . get_outgoing_messages )
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
@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-07-03 21:02:00 +00:00
# nickserv
2017-07-03 19:16:07 +00:00
if kwargs [ ' mask ' ] == " NickServ!NickServ@services.baconsvin.org " and kwargs [ ' event ' ] == " NOTICE " and kwargs [ ' data ' ] == " This nickname is registered. Please choose a different nickname, or identify via \x02 /msg NickServ identify <password> \x02 . " :
logger . info ( " Nickserv identify needed, fixing... " )
2017-07-03 19:19:20 +00:00
self . bot . privmsg ( " NickServ@services.baconsvin.org " , " identify %s %s " % ( settings . IRCBOT_NICK , settings . IRCBOT_NICKSERV_PASSWORD ) )
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
2017-07-03 19:08:47 +00:00
2017-01-30 11:05:11 +00:00
###############################################################################################
### custom irc3 methods
2017-01-31 21:50:28 +00:00
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 .
"""
2017-07-03 17:28:50 +00:00
#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 ) )
2017-01-31 21:50:28 +00:00
# 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 " )
2017-01-31 21:50:28 +00:00
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 ( )
2017-01-31 21:50:28 +00:00
elif msg . target :
2017-07-03 18:53:44 +00:00
logger . info ( " sending privmsg to %s : %s " % ( msg . target , msg . message ) )
2017-01-31 21:50:28 +00:00
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
2017-07-03 17:28:50 +00:00
# call this function again in X seconds
2017-01-31 21:50:28 +00:00
self . bot . loop . call_later ( settings . IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS , self . bot . get_outgoing_messages )
2017-01-30 11:05:11 +00:00