Document IRC and cleanup

This commit is contained in:
Mark Veidemanis 2021-12-27 21:13:13 +00:00
parent b880e9763f
commit 6339e88385
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 41 additions and 50 deletions

View File

@ -18,11 +18,12 @@ class IRCBot(irc.IRCClient):
self.realname = self.nickname self.realname = self.nickname
self.username = self.nickname self.username = self.nickname
# Don't give away information about our client
self.userinfo = None self.userinfo = None
self.fingerReply = None self.fingerReply = None
self.versionName = None self.versionName = None
self.sourceURL = None self.sourceURL = None
self.lineRate = None self.lineRate = None # Don't throttle messages, we may need to send a lot
self.prefix = settings.IRC.Prefix self.prefix = settings.IRC.Prefix
self.admins = (settings.IRC.Admins).split("\n") self.admins = (settings.IRC.Admins).split("\n")
@ -34,63 +35,27 @@ class IRCBot(irc.IRCClient):
self.agora = agora self.agora = agora
def parse(self, user, host, channel, msg): def parse(self, user, host, channel, msg):
"""
Simple handler for IRC commands.
"""
spl = msg.split() spl = msg.split()
# nick = user.split("!")[0] # nick = user.split("!")[0]
cmd = spl[0] cmd = spl[0]
# help command if cmd == "trades" and host in self.admins:
# if cmd == "help": # Get details of open trades and post on IRC
# self.msg(channel, "Hey it's me")
# addmod command
if cmd == "addmod" and host in self.admins:
if len(spl) == 2:
if not spl[1] in self.admins:
self.admins.append(spl[1])
self.msg(channel, "%s added to admins." % spl[1])
else:
self.msg(channel, "%s is already an admin!" % spl[1])
else:
self.msg(channel, "Incorrect usage")
# delmod command
elif cmd == "delmod" and host in self.admins:
if len(spl) == 2:
if spl[1] in self.admins:
self.admins.remove(spl[1])
self.msg(channel, "%s removed from admins." % spl[1])
else:
self.msg(channel, "%s is not an admin!" % spl[1])
else:
self.msg(channel, "Incorrect usage")
# join command
elif cmd == "join" and host in self.admins:
if len(spl) == 2:
self.join(spl[1])
else:
self.msg(channel, "Incorrect usage")
# part command
elif cmd == "part" and host in self.admins:
if len(spl) == 2:
self.part(spl[1])
else:
self.part(channel)
elif cmd == "raw" and host in self.admins:
self.sendLine(" ".join(spl[1:]))
elif cmd == "trades" and host in self.admins:
trades = self.agora.dashboard() trades = self.agora.dashboard()
for trade_id in trades: for trade_id in trades:
self.msg(channel, trade_id) self.msg(channel, trade_id)
elif cmd == "create" and host in self.admins and len(spl) == 3: elif cmd == "create" and host in self.admins and len(spl) == 3:
# Post an ad on AgoraDesk with the given country and currency code
posted = self.agora.create_ad(spl[1], spl[2]) posted = self.agora.create_ad(spl[1], spl[2])
self.msg(channel, dumps(posted)) self.msg(channel, dumps(posted))
elif cmd == "messages" and host in self.admins and len(spl) == 1: elif cmd == "messages" and host in self.admins and len(spl) == 1:
# Get all messages for all open trades
messages = self.agora.get_all_messages() messages = self.agora.get_all_messages()
for message_id in messages: for message_id in messages:
for message in messages[message_id]: for message in messages[message_id]:
@ -99,39 +64,52 @@ class IRCBot(irc.IRCClient):
# self.msg(channel, dumps(messages)) # self.msg(channel, dumps(messages))
elif cmd == "messages" and host in self.admins and len(spl) == 2: elif cmd == "messages" and host in self.admins and len(spl) == 2:
# Get all messages for a given trade
messages = self.agora.get_messages(spl[1]) messages = self.agora.get_messages(spl[1])
for message in messages: for message in messages:
self.msg(channel, f"{spl[1]}: {message}") self.msg(channel, f"{spl[1]}: {message}")
elif cmd == "dist" and host in self.admins: elif cmd == "dist" and host in self.admins:
# Distribute out our ad to all countries in the config
rtrn = self.agora.dist_countries() rtrn = self.agora.dist_countries()
self.msg(channel, dumps(rtrn)) self.msg(channel, dumps(rtrn))
elif cmd == "find" and host in self.admins and len(spl) == 3: elif cmd == "find" and host in self.admins and len(spl) == 3:
# Find a transaction received by Revolut with the given reference and amount
try: try:
int(spl[2]) int(spl[2])
except ValueError: except ValueError:
self.msg(channel, "Amount is not an integer") self.msg(channel, "Amount is not an integer")
rtrn = self.tx.find_tx(spl[1], spl[2]) rtrn = self.tx.find_tx(spl[1], spl[2])
if rtrn == "AMOUNT_INVALID": if rtrn == "AMOUNT_INVALID":
return dumps({"success": False, "msg": "Reference found but amount invalid"}) self.msg(channel, "Reference found but amount invalid")
elif not rtrn: elif not rtrn:
return dumps({"success": False, "msg": "Reference not found"}) self.msg(channel, "Reference not found")
else: else:
return dumps(rtrn) return dumps(rtrn)
def stopcall(self, call):
call.stop()
def signedOn(self): def signedOn(self):
"""
Called when we have signed on to IRC.
Join our channel.
"""
self.log.info("Signed on as %s" % (self.nickname)) self.log.info("Signed on as %s" % (self.nickname))
self.join(self.channel) self.join(self.channel)
def joined(self, channel): def joined(self, channel):
"""
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.
"""
self.agora.setup_loop() self.agora.setup_loop()
self.log.info("Joined channel %s" % (channel)) self.log.info("Joined channel %s" % (channel))
def privmsg(self, user, channel, msg): def privmsg(self, user, channel, msg):
"""
Called on received PRIVMSGs.
Pass through identified commands to the parse function.
"""
nick = user.split("!")[0] nick = user.split("!")[0]
if channel == self.nickname: if channel == self.nickname:
@ -149,6 +127,9 @@ class IRCBot(irc.IRCClient):
self.parse(user, host, channel, msg[1:]) self.parse(user, host, channel, msg[1:])
def noticed(self, user, channel, message): def noticed(self, user, channel, message):
"""
Called on received NOTICEs.
"""
nick = user.split("!")[0] nick = user.split("!")[0]
if channel == self.nickname: if channel == self.nickname:
channel = nick channel = nick
@ -158,22 +139,32 @@ class IRCBot(irc.IRCClient):
class IRCBotFactory(protocol.ClientFactory): class IRCBotFactory(protocol.ClientFactory):
def __init__(self): def __init__(self):
self.log = Logger("irc") self.log = Logger("irc")
self.agora = None
def set_agora(self, agora): def set_agora(self, agora):
self.agora = agora self.agora = agora
def buildProtocol(self, addr): def buildProtocol(self, addr):
"""
Custom override for the Twisted buildProtocol so we can access the Protocol instance.
Passes through the Agora instance to IRC.
:return: IRCBot Protocol instance
"""
prcol = IRCBot(self.log) prcol = IRCBot(self.log)
self.client = prcol self.client = prcol
self.client.set_agora(self.agora) self.client.set_agora(self.agora)
return prcol return prcol
def clientConnectionLost(self, connector, reason): def clientConnectionLost(self, connector, reason):
"""
Called when connection to IRC server lost. Reconnect.
"""
self.log.error("Lost connection (%s), reconnecting" % (reason)) self.log.error("Lost connection (%s), reconnecting" % (reason))
connector.connect() connector.connect()
def clientConnectionFailed(self, connector, reason): def clientConnectionFailed(self, connector, reason):
"""
Called when connection to IRC server failed. Reconnect.
"""
self.log.error("Could not connect: %s" % (reason)) self.log.error("Could not connect: %s" % (reason))
connector.connect() connector.connect()