pluto/handler/irc.py

147 lines
4.3 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
self.userinfo = None
self.fingerReply = None
self.versionName = None
self.sourceURL = None
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 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()
self.msg(channel, dumps(trades))
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))
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")
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