pluto/handler/irc.py

177 lines
5.8 KiB
Python
Raw Normal View History

2021-12-24 17:24:45 +00:00
# Twisted/Klein imports
from twisted.logger import Logger
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor, ssl
2021-12-27 13:50:32 +00:00
# Other library imports
from json import dumps
2021-12-24 17:24:45 +00:00
# Project imports
from settings import settings
class IRCBot(irc.IRCClient):
def __init__(self, log):
self.log = log
self.nickname = settings.IRC.Nick
self.password = settings.IRC.Pass
self.realname = self.nickname
self.username = self.nickname
2021-12-27 21:13:13 +00:00
# Don't give away information about our client
2021-12-24 17:24:45 +00:00
self.userinfo = None
self.fingerReply = None
self.versionName = None
self.sourceURL = None
2021-12-27 21:13:13 +00:00
self.lineRate = None # Don't throttle messages, we may need to send a lot
2021-12-24 17:24:45 +00:00
2021-12-27 13:50:32 +00:00
self.prefix = settings.IRC.Prefix
2021-12-24 17:24:45 +00:00
self.admins = (settings.IRC.Admins).split("\n")
self.highlight = (settings.IRC.Highlight).split("\n")
2021-12-27 19:30:03 +00:00
self.channel = settings.IRC.Channel
2021-12-27 13:50:32 +00:00
def set_agora(self, agora):
self.agora = agora
2021-12-24 17:24:45 +00:00
def parse(self, user, host, channel, msg):
2021-12-27 21:13:13 +00:00
"""
Simple handler for IRC commands.
"""
2021-12-24 17:24:45 +00:00
spl = msg.split()
# nick = user.split("!")[0]
cmd = spl[0]
2021-12-27 21:13:13 +00:00
if cmd == "trades" and host in self.admins:
# Get details of open trades and post on IRC
2021-12-27 13:50:32 +00:00
trades = self.agora.dashboard()
for trade_id in trades:
2021-12-27 19:30:03 +00:00
self.msg(channel, trade_id)
elif cmd == "create" and host in self.admins and len(spl) == 3:
2021-12-27 21:13:13 +00:00
# Post an ad on AgoraDesk with the given country and currency code
posted = self.agora.create_ad(spl[1], spl[2])
self.msg(channel, dumps(posted))
elif cmd == "messages" and host in self.admins and len(spl) == 1:
2021-12-27 21:13:13 +00:00
# Get all messages for all open trades
messages = self.agora.get_all_messages()
for message_id in messages:
for message in messages[message_id]:
self.msg(channel, f"{message_id}: {message}")
2021-12-27 19:30:03 +00:00
self.msg(channel, "---")
# self.msg(channel, dumps(messages))
2021-12-27 13:50:32 +00:00
2021-12-27 20:59:24 +00:00
elif cmd == "messages" and host in self.admins and len(spl) == 2:
2021-12-27 21:13:13 +00:00
# Get all messages for a given trade
2021-12-27 20:59:24 +00:00
messages = self.agora.get_messages(spl[1])
for message in messages:
self.msg(channel, f"{spl[1]}: {message}")
elif cmd == "dist" and host in self.admins:
2021-12-27 21:13:13 +00:00
# Distribute out our ad to all countries in the config
2021-12-27 20:59:24 +00:00
rtrn = self.agora.dist_countries()
self.msg(channel, dumps(rtrn))
elif cmd == "find" and host in self.admins and len(spl) == 3:
2021-12-27 21:13:13 +00:00
# Find a transaction received by Revolut with the given reference and amount
2021-12-27 20:59:24 +00:00
try:
int(spl[2])
except ValueError:
self.msg(channel, "Amount is not an integer")
rtrn = self.tx.find_tx(spl[1], spl[2])
if rtrn == "AMOUNT_INVALID":
2021-12-27 21:13:13 +00:00
self.msg(channel, "Reference found but amount invalid")
2021-12-27 20:59:24 +00:00
elif not rtrn:
2021-12-27 21:13:13 +00:00
self.msg(channel, "Reference not found")
2021-12-27 20:59:24 +00:00
else:
return dumps(rtrn)
2021-12-27 20:59:24 +00:00
2021-12-24 17:24:45 +00:00
def signedOn(self):
2021-12-27 21:13:13 +00:00
"""
Called when we have signed on to IRC.
Join our channel.
"""
2021-12-24 17:24:45 +00:00
self.log.info("Signed on as %s" % (self.nickname))
2021-12-27 19:30:03 +00:00
self.join(self.channel)
2021-12-24 17:24:45 +00:00
def joined(self, channel):
2021-12-27 21:13:13 +00:00
"""
Called when we have joined a channel.
Setup the Agora LoopingCall to get trades.
This is here to ensure the IRC client is initialised enough to send the trades.
"""
2021-12-27 19:30:03 +00:00
self.agora.setup_loop()
2021-12-24 17:24:45 +00:00
self.log.info("Joined channel %s" % (channel))
def privmsg(self, user, channel, msg):
2021-12-27 21:13:13 +00:00
"""
Called on received PRIVMSGs.
Pass through identified commands to the parse function.
"""
2021-12-24 17:24:45 +00:00
nick = user.split("!")[0]
if channel == self.nickname:
channel = nick
host = user.split("!")[1]
host = host.split("@")[1]
ident = user.split("!")[1]
ident = ident.split("@")[0]
self.log.info("(%s) %s: %s" % (channel, user, msg))
if msg[0] == self.prefix:
if len(msg) > 1:
if msg.split()[0] != "!":
self.parse(user, host, channel, msg[1:])
def noticed(self, user, channel, message):
2021-12-27 21:13:13 +00:00
"""
Called on received NOTICEs.
"""
2021-12-24 17:24:45 +00:00
nick = user.split("!")[0]
if channel == self.nickname:
channel = nick
# self.log.info("[%s] %s: %s" % (channel, user, message))
class IRCBotFactory(protocol.ClientFactory):
def __init__(self):
self.log = Logger("irc")
2021-12-27 13:50:32 +00:00
def set_agora(self, agora):
self.agora = agora
2021-12-24 17:24:45 +00:00
def buildProtocol(self, addr):
2021-12-27 21:13:13 +00:00
"""
Custom override for the Twisted buildProtocol so we can access the Protocol instance.
Passes through the Agora instance to IRC.
:return: IRCBot Protocol instance
"""
2021-12-24 17:24:45 +00:00
prcol = IRCBot(self.log)
self.client = prcol
2021-12-27 13:50:32 +00:00
self.client.set_agora(self.agora)
2021-12-24 17:24:45 +00:00
return prcol
def clientConnectionLost(self, connector, reason):
2021-12-27 21:13:13 +00:00
"""
Called when connection to IRC server lost. Reconnect.
"""
2021-12-24 17:24:45 +00:00
self.log.error("Lost connection (%s), reconnecting" % (reason))
connector.connect()
def clientConnectionFailed(self, connector, reason):
2021-12-27 21:13:13 +00:00
"""
Called when connection to IRC server failed. Reconnect.
"""
2021-12-24 17:24:45 +00:00
self.log.error("Could not connect: %s" % (reason))
2021-12-27 13:50:32 +00:00
connector.connect()
2021-12-24 17:24:45 +00:00
def bot():
2021-12-24 17:35:41 +00:00
context = ssl.DefaultOpenSSLContextFactory(settings.IRC.Cert, settings.IRC.Cert)
2021-12-24 17:24:45 +00:00
factory = IRCBotFactory()
reactor.connectSSL(settings.IRC.Host, int(settings.IRC.Port), factory, context)
return factory