pluto/handler/irc.py

163 lines
5.1 KiB
Python

# Twisted/Klein imports
from twisted.logger import Logger
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor, ssl
# Other library imports
from json import dumps
# 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
self.userinfo = None
self.fingerReply = None
self.versionName = None
self.sourceURL = None
self.prefix = settings.IRC.Prefix
self.admins = (settings.IRC.Admins).split("\n")
self.highlight = (settings.IRC.Highlight).split("\n")
def set_agora(self, agora):
self.agora = agora
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
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()
for trade_id in trades:
fmt = (
f"{trade_id}: {trades[trade_id]['buyer']} "
f"{trades[trade_id]['amount']}{trades[trade_id]['currency']} {trades[trade_id]['amount_xmr']}XMR"
)
self.msg(channel, fmt)
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}")
# self.msg(channel, dumps(messages))
def stopcall(self, call):
call.stop()
def signedOn(self):
self.log.info("Signed on as %s" % (self.nickname))
self.join(settings.IRC.Channel)
def joined(self, channel):
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")
self.agora = None
def set_agora(self, agora):
self.agora = agora
def buildProtocol(self, addr):
prcol = IRCBot(self.log)
self.client = prcol
self.client.set_agora(self.agora)
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))
connector.connect()
def bot():
context = ssl.DefaultOpenSSLContextFactory(settings.IRC.Cert, settings.IRC.Cert)
factory = IRCBotFactory()
reactor.connectSSL(settings.IRC.Host, int(settings.IRC.Port), factory, context)
return factory