Document IRC and cleanup

pull/1/head
Mark Veidemanis 3 years ago
parent b880e9763f
commit 6339e88385
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -18,11 +18,12 @@ class IRCBot(irc.IRCClient):
self.realname = self.nickname
self.username = self.nickname
# Don't give away information about our client
self.userinfo = None
self.fingerReply = None
self.versionName = 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.admins = (settings.IRC.Admins).split("\n")
@ -34,63 +35,27 @@ class IRCBot(irc.IRCClient):
self.agora = agora
def parse(self, user, host, channel, msg):
"""
Simple handler for IRC commands.
"""
spl = msg.split()
# nick = user.split("!")[0]
cmd = spl[0]
# help command
# if cmd == "help":
# 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:
if cmd == "trades" and host in self.admins:
# Get details of open trades and post on IRC
trades = self.agora.dashboard()
for trade_id in trades:
self.msg(channel, trade_id)
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])
self.msg(channel, dumps(posted))
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()
for message_id in messages:
for message in messages[message_id]:
@ -99,39 +64,52 @@ class IRCBot(irc.IRCClient):
# self.msg(channel, dumps(messages))
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])
for message in messages:
self.msg(channel, f"{spl[1]}: {message}")
elif cmd == "dist" and host in self.admins:
# Distribute out our ad to all countries in the config
rtrn = self.agora.dist_countries()
self.msg(channel, dumps(rtrn))
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:
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":
return dumps({"success": False, "msg": "Reference found but amount invalid"})
self.msg(channel, "Reference found but amount invalid")
elif not rtrn:
return dumps({"success": False, "msg": "Reference not found"})
self.msg(channel, "Reference not found")
else:
return dumps(rtrn)
def stopcall(self, call):
call.stop()
def signedOn(self):
"""
Called when we have signed on to IRC.
Join our channel.
"""
self.log.info("Signed on as %s" % (self.nickname))
self.join(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.log.info("Joined channel %s" % (channel))
def privmsg(self, user, channel, msg):
"""
Called on received PRIVMSGs.
Pass through identified commands to the parse function.
"""
nick = user.split("!")[0]
if channel == self.nickname:
@ -149,6 +127,9 @@ class IRCBot(irc.IRCClient):
self.parse(user, host, channel, msg[1:])
def noticed(self, user, channel, message):
"""
Called on received NOTICEs.
"""
nick = user.split("!")[0]
if channel == self.nickname:
channel = nick
@ -158,22 +139,32 @@ class IRCBot(irc.IRCClient):
class IRCBotFactory(protocol.ClientFactory):
def __init__(self):
self.log = Logger("irc")
self.agora = None
def set_agora(self, agora):
self.agora = agora
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)
self.client = prcol
self.client.set_agora(self.agora)
return prcol
def clientConnectionLost(self, connector, reason):
"""
Called when connection to IRC server lost. Reconnect.
"""
self.log.error("Lost connection (%s), reconnecting" % (reason))
connector.connect()
def clientConnectionFailed(self, connector, reason):
"""
Called when connection to IRC server failed. Reconnect.
"""
self.log.error("Could not connect: %s" % (reason))
connector.connect()

Loading…
Cancel
Save