pluto/handler/irc.py

196 lines
6.2 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
2021-12-27 20:59:24 +00:00
def convert(data):
if isinstance(data, bytes):
return data.decode("ascii")
if isinstance(data, dict):
return dict(map(convert, data.items()))
if isinstance(data, tuple):
return map(convert, data)
return data
2021-12-24 17:24:45 +00:00
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
self.userinfo = None
self.fingerReply = None
self.versionName = None
self.sourceURL = None
2021-12-27 20:59:24 +00:00
self.lineRate = None
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):
spl = msg.split()
# nick = user.split("!")[0]
cmd = spl[0]
# help command
# if cmd == "help":
# self.msg(channel, "Hey it's me")
# addmod command
2021-12-27 13:50:32 +00:00
if cmd == "addmod" and host in self.admins:
2021-12-24 17:24:45 +00:00
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:]))
2021-12-27 13:50:32 +00:00
elif cmd == "trades" and host in self.admins:
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:
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:
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:
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:
rtrn = self.agora.dist_countries()
self.msg(channel, dumps(rtrn))
elif cmd == "find" and host in self.admins and len(spl) == 3:
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"})
elif not rtrn:
return dumps({"success": False, "msg": "Reference not found"})
else:
return dumps(convert(rtrn))
2021-12-24 17:24:45 +00:00
def stopcall(self, call):
call.stop()
def signedOn(self):
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 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):
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):
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
self.agora = None
def set_agora(self, agora):
self.agora = agora
2021-12-24 17:24:45 +00:00
def buildProtocol(self, addr):
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):
self.log.error("Lost connection (%s), reconnecting" % (reason))
connector.connect()
def clientConnectionFailed(self, connector, reason):
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