2017-11-19 14:46:42 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from twisted.internet import reactor
|
2017-11-24 19:16:08 +00:00
|
|
|
from twisted.internet.defer import Deferred
|
2017-11-19 14:46:42 +00:00
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
2018-02-03 19:03:47 +00:00
|
|
|
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ReconnectingClientFactory
|
2017-11-23 20:37:00 +00:00
|
|
|
from twisted.words.protocols.irc import IRCClient
|
|
|
|
|
2017-12-24 21:04:48 +00:00
|
|
|
#from twisted.python import log
|
|
|
|
#from sys import stdout
|
|
|
|
#log.startLogging(stdout)
|
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
from json import load, dump, loads
|
2017-11-19 14:46:42 +00:00
|
|
|
from sys import exit
|
2017-12-03 18:34:56 +00:00
|
|
|
from subprocess import run, PIPE
|
2017-11-19 14:46:42 +00:00
|
|
|
|
2017-12-24 21:04:48 +00:00
|
|
|
numbers = "0123456789"
|
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
listener = None
|
|
|
|
connections = {}
|
2017-11-23 20:37:00 +00:00
|
|
|
IRCPool = {}
|
2018-02-03 19:03:47 +00:00
|
|
|
ReactorPool = {}
|
|
|
|
FactoryPool = {}
|
2017-11-19 14:46:42 +00:00
|
|
|
|
2017-12-24 21:04:48 +00:00
|
|
|
MonitorPool = []
|
|
|
|
|
2018-01-17 19:02:41 +00:00
|
|
|
configPath = "conf/"
|
|
|
|
certPath = "cert/"
|
|
|
|
|
2018-01-14 19:10:57 +00:00
|
|
|
filemap = {
|
|
|
|
"config": ["config.json", "configuration"],
|
|
|
|
"keyconf": ["keyword.json", "keyword lists"],
|
|
|
|
"pool": ["pool.json", "pool"],
|
|
|
|
"help": ["help.json", "command help"],
|
|
|
|
"wholist": ["wholist.json", "WHO lists"],
|
|
|
|
}
|
2017-12-24 21:04:48 +00:00
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
def log(data):
|
|
|
|
print("[LOG]", data)
|
|
|
|
|
|
|
|
def debug(data):
|
|
|
|
print("[DEBUG]", data)
|
|
|
|
|
2017-11-20 19:15:58 +00:00
|
|
|
def warn(data):
|
|
|
|
print("[WARNING]", data)
|
|
|
|
|
|
|
|
def error(data):
|
|
|
|
print("[ERROR]", data)
|
|
|
|
exit(1)
|
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
def sendData(addr, data):
|
|
|
|
connections[addr].send(data)
|
|
|
|
|
|
|
|
def sendSuccess(addr, data):
|
|
|
|
sendData(addr, "[y] " + data)
|
|
|
|
|
|
|
|
def sendFailure(addr, data):
|
|
|
|
sendData(addr, "[n] " + data)
|
|
|
|
|
|
|
|
def sendInfo(addr, data):
|
|
|
|
sendData(addr, "[i] " + data)
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
class IRCBot(IRCClient):
|
|
|
|
def __init__(self, name):
|
2017-11-24 19:16:08 +00:00
|
|
|
self.connected = False
|
|
|
|
self.channels = []
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
self.name = name
|
|
|
|
instance = pool[name]
|
|
|
|
|
|
|
|
self.nickname = instance["nickname"]
|
|
|
|
self.realname = instance["realname"]
|
|
|
|
self.username = instance["username"]
|
|
|
|
self.userinfo = instance["userinfo"]
|
|
|
|
self.fingerReply = instance["finger"]
|
|
|
|
self.versionName = instance["version"]
|
|
|
|
self.versionNum = None
|
|
|
|
self.versionEnv = None
|
|
|
|
self.sourceURL = instance["source"]
|
2017-11-25 19:48:20 +00:00
|
|
|
self.autojoin = instance["autojoin"]
|
2017-11-23 20:37:00 +00:00
|
|
|
|
|
|
|
self._who = {}
|
|
|
|
self._getWho = {}
|
|
|
|
|
|
|
|
self.authtype = instance["authtype"]
|
|
|
|
if self.authtype == "ns":
|
|
|
|
self.authpass = instance["password"]
|
|
|
|
self.authentity = instance["authentity"]
|
|
|
|
else:
|
|
|
|
self.password = instance["password"]
|
|
|
|
|
|
|
|
def refresh(self):
|
2017-11-24 19:16:08 +00:00
|
|
|
instance = pool[self.name]
|
2017-11-23 20:37:00 +00:00
|
|
|
if not instance["nickname"] == self.nickname:
|
|
|
|
self.nickname = instance["nickname"]
|
|
|
|
self.setNick(self.nickname)
|
|
|
|
|
|
|
|
self.userinfo = instance["userinfo"]
|
|
|
|
self.fingerReply = instance["finger"]
|
|
|
|
self.versionName = instance["version"]
|
|
|
|
self.versionNum = None
|
|
|
|
self.versionEnv = None
|
|
|
|
self.sourceURL = instance["source"]
|
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
def parsen(self, user):
|
|
|
|
step = user.split("!")
|
|
|
|
nick = step[0]
|
|
|
|
if len(step) == 2:
|
|
|
|
step2 = step[1].split("@")
|
|
|
|
ident, host = step2
|
2017-11-30 18:54:08 +00:00
|
|
|
else:
|
2017-12-25 22:09:38 +00:00
|
|
|
ident = nick
|
|
|
|
host = nick
|
|
|
|
|
|
|
|
return [nick, ident, host]
|
|
|
|
|
|
|
|
def privmsg(self, user, channel, msg):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
helper.actKeyword(user, channel, msg, self.nickname, "PRV", self.name)
|
2017-11-25 19:02:13 +00:00
|
|
|
|
|
|
|
def noticed(self, user, channel, msg):
|
2017-12-25 22:09:38 +00:00
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
helper.actKeyword(user, channel, msg, self.nickname, "NOT", self.name)
|
2017-11-25 19:02:13 +00:00
|
|
|
|
|
|
|
def action(self, user, channel, msg):
|
2017-12-25 22:09:38 +00:00
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
helper.actKeyword(user, channel, msg, self.nickname, "ACT", self.name)
|
2017-11-25 19:02:13 +00:00
|
|
|
|
2017-11-24 19:16:08 +00:00
|
|
|
def get(self, var):
|
|
|
|
try:
|
|
|
|
result = getattr(self, var)
|
|
|
|
except AttributeError:
|
|
|
|
result = None
|
|
|
|
return result
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
def setNick(self, nickname):
|
|
|
|
self._attemptedNick = nickname
|
|
|
|
self.sendLine("NICK %s" % nickname)
|
|
|
|
self.nickname = nickname
|
|
|
|
|
|
|
|
def alterCollidedNick(self, nickname):
|
|
|
|
newnick = nickname + "_"
|
|
|
|
return newnick
|
|
|
|
|
|
|
|
def irc_ERR_NICKNAMEINUSE(self, prefix, params):
|
|
|
|
self._attemptedNick = self.alterCollidedNick(self._attemptedNick)
|
|
|
|
self.setNick(self._attemptedNick)
|
|
|
|
|
|
|
|
def irc_ERR_PASSWDMISMATCH(self, prefix, params):
|
2017-11-24 19:16:08 +00:00
|
|
|
log("%s: password mismatch" % self.name)
|
|
|
|
helper.sendAll("%s: password mismatch" % self.name)
|
2017-11-23 20:37:00 +00:00
|
|
|
|
|
|
|
def who(self, channel):
|
|
|
|
channel = channel
|
2017-11-24 19:16:08 +00:00
|
|
|
d = Deferred()
|
2017-11-23 20:37:00 +00:00
|
|
|
if channel not in self._who:
|
|
|
|
self._who[channel] = ([], {})
|
|
|
|
self._who[channel][0].append(d)
|
|
|
|
self.sendLine("WHO %s" % channel)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def irc_RPL_WHOREPLY(self, prefix, params):
|
|
|
|
channel = params[1]
|
|
|
|
user = params[2]
|
|
|
|
host = params[3]
|
|
|
|
server = params[4]
|
|
|
|
nick = params[5]
|
|
|
|
status = params[6]
|
|
|
|
realname = params[7]
|
|
|
|
if channel not in self._who:
|
|
|
|
return
|
|
|
|
n = self._who[channel][1]
|
|
|
|
n[nick] = [nick, user, host, server, status, realname]
|
|
|
|
|
|
|
|
def irc_RPL_ENDOFWHO(self, prefix, params):
|
|
|
|
channel = params[1]
|
|
|
|
if channel not in self._who:
|
|
|
|
return
|
|
|
|
callbacks, info = self._who[channel]
|
|
|
|
for cb in callbacks:
|
|
|
|
cb.callback((channel, info))
|
|
|
|
del self._who[channel]
|
|
|
|
|
|
|
|
def got_who(self, whoinfo):
|
2017-12-24 21:04:48 +00:00
|
|
|
global wholist
|
|
|
|
helper.setWho(self.name, whoinfo[1])
|
2017-11-23 20:37:00 +00:00
|
|
|
|
|
|
|
def signedOn(self):
|
2017-11-24 19:16:08 +00:00
|
|
|
self.connected = True
|
2017-12-25 19:02:17 +00:00
|
|
|
log("signed on: %s" % self.name)
|
2017-11-23 20:37:00 +00:00
|
|
|
if self.authtype == "ns":
|
|
|
|
self.msg(self.authentity, "IDENTIFY %s" % self.nspass)
|
2017-11-25 19:48:20 +00:00
|
|
|
for i in self.autojoin:
|
|
|
|
self.join(i)
|
2017-11-23 20:37:00 +00:00
|
|
|
|
|
|
|
def joined(self, channel):
|
2017-11-24 19:16:08 +00:00
|
|
|
if not channel in self.channels:
|
|
|
|
self.channels.append(channel)
|
2017-11-23 20:37:00 +00:00
|
|
|
self.who(channel).addCallback(self.got_who)
|
|
|
|
|
2017-11-24 19:16:08 +00:00
|
|
|
def left(self, channel):
|
|
|
|
if channel in self.channels:
|
|
|
|
self.channels.remove(channel)
|
|
|
|
|
|
|
|
def kickedFrom(self, channel, kicker, message):
|
|
|
|
if channel in self.channels:
|
|
|
|
self.channels.remove(channel)
|
2017-11-28 19:31:02 +00:00
|
|
|
helper.sendMaster("KICK %s: (%s/%s) %s" % (self.name, kicker, channel, message))
|
2017-11-24 19:16:08 +00:00
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
def userJoined(self, user, channel):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
def userLeft(self, user, channel):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
def userQuit(self, user, quitMessage):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
helper.actKeyword(user, None, quitMessage, self.nickname, "QUT", self.name)
|
|
|
|
|
|
|
|
def userKicked(self, kickee, channel, kicker, message):
|
2017-12-28 18:46:17 +00:00
|
|
|
nick, ident, host = self.parsen(kicker)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
helper.actKeyword(kicker, channel, message, self.nickname, "KCK", self.name)
|
|
|
|
|
2017-12-28 18:46:17 +00:00
|
|
|
def userRenamed(self, oldname, newname):
|
|
|
|
nick, ident, host = self.parsen(oldname)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
helper.setWhoSingle(self.name, newname, ident, host)
|
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
def topicUpdated(self, user, channel, newTopic):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
|
|
|
helper.actKeyword(user, channel, newTopic, self.nickname, "TOP", self.name)
|
|
|
|
|
|
|
|
def modeChanged(self, user, channel, toset, modes, args):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
helper.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
|
2018-02-03 19:03:47 +00:00
|
|
|
class IRCBotFactory(ReconnectingClientFactory):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.instance = pool[name]
|
|
|
|
self.name = name
|
|
|
|
self.maxDelay = self.instance["maxdelay"]
|
|
|
|
self.initialDelay = self.instance["initialdelay"]
|
|
|
|
self.factor = self.instance["factor"]
|
|
|
|
self.jitter = self.instance["jitter"]
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
global IRCPool
|
|
|
|
entry = IRCBot(self.name)
|
|
|
|
IRCPool[self.name] = entry
|
|
|
|
self.client = entry
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
self.client.connected = False
|
|
|
|
self.client.channels = []
|
2017-11-24 19:16:08 +00:00
|
|
|
error = reason.getErrorMessage()
|
|
|
|
log("%s: connection lost: %s" % (self.name, error))
|
|
|
|
helper.sendAll("%s: connection lost: %s" % (self.name, error))
|
2018-02-03 19:03:47 +00:00
|
|
|
self.retry(connector)
|
|
|
|
#ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
2017-11-24 19:16:08 +00:00
|
|
|
|
2018-02-03 19:03:47 +00:00
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
|
|
self.client.connected = False
|
|
|
|
self.client.channels = []
|
2017-11-24 19:16:08 +00:00
|
|
|
error = reason.getErrorMessage()
|
|
|
|
log("%s: connection failed: %s" % (self.name, error))
|
|
|
|
helper.sendAll("%s: connection failed: %s" % (self.name, error))
|
2018-02-03 19:03:47 +00:00
|
|
|
self.retry(connector)
|
|
|
|
#ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
2017-11-24 19:16:08 +00:00
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
class Base(Protocol):
|
|
|
|
def __init__(self, addr):
|
|
|
|
self.addr = addr
|
|
|
|
self.authed = False
|
2017-11-23 18:32:30 +00:00
|
|
|
if config["UsePassword"] == False:
|
|
|
|
self.authed = True
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
data += "\r\n"
|
|
|
|
data = data.encode("utf-8", "replace")
|
|
|
|
self.transport.write(data)
|
|
|
|
|
|
|
|
def dataReceived(self, data):
|
2017-11-20 21:40:04 +00:00
|
|
|
data = data.decode("utf-8", "replace")
|
2017-11-27 19:54:35 +00:00
|
|
|
#log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
2017-12-03 13:10:51 +00:00
|
|
|
if "\n" in data:
|
|
|
|
splitData = [x for x in data.split("\n") if x]
|
|
|
|
if "\n" in data:
|
|
|
|
for i in splitData:
|
|
|
|
helper.parseCommand(self.addr, self.authed, i)
|
|
|
|
return
|
2017-11-20 21:40:04 +00:00
|
|
|
helper.parseCommand(self.addr, self.authed, data)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
def connectionMade(self):
|
|
|
|
log("Connection from %s:%s" % (self.addr.host, self.addr.port))
|
|
|
|
self.send("Hello.")
|
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
2017-12-24 21:04:48 +00:00
|
|
|
global connections, MonitorPool
|
2017-11-21 20:16:14 +00:00
|
|
|
self.authed = False
|
2017-11-20 21:40:04 +00:00
|
|
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
2017-11-19 14:46:42 +00:00
|
|
|
if not listener == None:
|
|
|
|
if self.addr in connections.keys():
|
|
|
|
del connections[self.addr]
|
|
|
|
else:
|
2017-11-20 19:15:58 +00:00
|
|
|
warn("Tried to remove a non-existant connection.")
|
2017-11-19 14:46:42 +00:00
|
|
|
else:
|
2017-11-20 19:15:58 +00:00
|
|
|
warn("Tried to remove a connection from a listener that wasn't running.")
|
2017-12-24 21:04:48 +00:00
|
|
|
if self.addr in MonitorPool:
|
|
|
|
MonitorPool.remove(self.addr)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
class BaseFactory(Factory):
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
global connections
|
|
|
|
entry = Base(addr)
|
|
|
|
connections[addr] = entry
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def send(self, addr, data):
|
|
|
|
global connections
|
|
|
|
if addr in connections.keys():
|
|
|
|
connection = connections[addr]
|
|
|
|
connection.send(data)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2017-11-20 19:53:25 +00:00
|
|
|
class Helper(object):
|
2017-12-24 21:04:48 +00:00
|
|
|
def setWho(self, network, newObjects):
|
|
|
|
global wholist
|
|
|
|
network = "".join([x for x in network if not x in numbers])
|
|
|
|
if not network in wholist.keys():
|
|
|
|
wholist[network] = {}
|
|
|
|
for i in newObjects.keys():
|
|
|
|
wholist[network][i] = newObjects[i]
|
2017-12-25 22:27:09 +00:00
|
|
|
|
2017-12-24 21:04:48 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def setWhoSingle(self, network, nick, ident, host):
|
|
|
|
global wholist
|
|
|
|
network = "".join([x for x in network if not x in numbers])
|
|
|
|
|
|
|
|
if network in wholist.keys():
|
|
|
|
if nick in wholist[network].keys():
|
|
|
|
wholist[network][nick][1] = ident
|
|
|
|
wholist[network][nick][2] = host
|
2017-12-25 19:02:17 +00:00
|
|
|
else:
|
|
|
|
wholist[network][nick] = [nick, ident, host, None, None, None]
|
|
|
|
|
|
|
|
def getWho(self, nick):
|
|
|
|
result = {}
|
|
|
|
for i in wholist.keys():
|
|
|
|
for x in wholist[i].keys():
|
|
|
|
if nick.lower() == x.lower():
|
|
|
|
if not i in result.keys():
|
|
|
|
result[i] = []
|
|
|
|
result[i].append(wholist[i][x])
|
|
|
|
return result
|
2017-12-03 13:10:51 +00:00
|
|
|
|
2018-01-14 19:10:57 +00:00
|
|
|
def save(self, var):
|
2018-01-17 19:02:41 +00:00
|
|
|
with open(configPath+filemap[var][0], "w") as f:
|
2018-01-14 19:10:57 +00:00
|
|
|
dump(globals()[var], f, indent=4)
|
2017-12-25 22:27:09 +00:00
|
|
|
return
|
|
|
|
|
2018-01-14 19:10:57 +00:00
|
|
|
def load(self, var):
|
2018-01-17 19:02:41 +00:00
|
|
|
with open(configPath+filemap[var][0], "r") as f:
|
2018-01-14 19:10:57 +00:00
|
|
|
globals()[var] = load(f)
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
def incorrectUsage(self, addr, mode):
|
|
|
|
if mode == None:
|
|
|
|
sendFailure(addr, "Incorrect usage")
|
|
|
|
return
|
|
|
|
if mode in help.keys():
|
|
|
|
sendFailure(addr, "Usage: " + help[mode])
|
|
|
|
return
|
|
|
|
|
2017-11-24 19:16:08 +00:00
|
|
|
def sendAll(self, data):
|
|
|
|
global connections
|
|
|
|
for i in connections:
|
|
|
|
connections[i].send(data)
|
|
|
|
return
|
|
|
|
|
2017-11-25 18:42:32 +00:00
|
|
|
def sendMaster(self, data):
|
|
|
|
if config["Master"][0] in IRCPool.keys():
|
|
|
|
IRCPool[config["Master"][0]].msg(config["Master"][1], data)
|
2017-12-24 21:09:18 +00:00
|
|
|
else:
|
2018-01-20 21:26:35 +00:00
|
|
|
warn("Master with no IRC instance defined")
|
2017-12-24 21:04:48 +00:00
|
|
|
for i in MonitorPool:
|
|
|
|
connections[i].send(data)
|
2017-11-25 18:42:32 +00:00
|
|
|
|
|
|
|
def isKeyword(self, msg):
|
|
|
|
message = msg.lower()
|
2017-11-28 19:19:27 +00:00
|
|
|
messageDuplicate = message
|
2017-11-27 19:54:35 +00:00
|
|
|
toUndo = False
|
2017-11-25 18:42:32 +00:00
|
|
|
uniqueNum = 0
|
|
|
|
totalNum = 0
|
2017-12-03 13:10:51 +00:00
|
|
|
for i in keyconf["Keywords"]:
|
2017-11-25 18:42:32 +00:00
|
|
|
if i in message:
|
2017-12-03 13:10:51 +00:00
|
|
|
if i in keyconf["KeywordsExcept"].keys():
|
|
|
|
for x in keyconf["KeywordsExcept"][i]:
|
2017-11-27 19:54:35 +00:00
|
|
|
if x in message:
|
|
|
|
toUndo = True
|
2017-11-28 19:19:27 +00:00
|
|
|
messageDuplicate = messageDuplicate.replace(x, "\0\r\n\n\0")
|
2017-12-03 13:10:51 +00:00
|
|
|
for y in keyconf["Keywords"]:
|
2017-11-28 19:19:27 +00:00
|
|
|
if i in messageDuplicate:
|
|
|
|
totalNum += messageDuplicate.count(i)
|
|
|
|
message = messageDuplicate.replace(i, "{"+i+"}")
|
|
|
|
message = message.replace("\0\r\n\n\0", x)
|
|
|
|
uniqueNum += 1
|
|
|
|
|
|
|
|
if toUndo == False:
|
|
|
|
totalNum += message.count(i)
|
|
|
|
message = message.replace(i, "{"+i+"}")
|
|
|
|
uniqueNum += 1
|
2017-11-27 19:54:35 +00:00
|
|
|
|
|
|
|
toUndo = False
|
|
|
|
|
2017-11-25 19:50:57 +00:00
|
|
|
if totalNum == 0:
|
2017-11-25 18:42:32 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return [message, uniqueNum, totalNum]
|
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
def actKeyword(self, user, channel, message, nickname, actType, name):
|
|
|
|
toSend = self.isKeyword(message)
|
|
|
|
if name == config["Master"][0] and channel == config["Master"][1]:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if config["HighlightNotifications"]:
|
|
|
|
msgLower = message.lower()
|
|
|
|
nickLower = nickname.lower()
|
|
|
|
if nickLower in msgLower:
|
|
|
|
msgLower = msgLower.replace(nickLower, "{"+nickLower+"}")
|
|
|
|
self.sendMaster("NICK %s %s (T:%s): (%s/%s) %s" % (actType, name, msgLower.count(nickLower), user, channel, msgLower))
|
|
|
|
if toSend:
|
|
|
|
helper.sendMaster("MATCH %s %s (U:%s T:%s): (%s/%s) %s" % (actType, name, toSend[1], toSend[2], user, channel, toSend[0]))
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
def addBot(self, name):
|
2018-01-17 19:02:41 +00:00
|
|
|
global IRCPool, certPath
|
2017-11-23 20:37:00 +00:00
|
|
|
instance = pool[name]
|
2017-11-24 19:16:08 +00:00
|
|
|
log("Started bot %s to %s:%s protocol %s nickname %s" % (name, instance["host"], instance["port"], instance["protocol"], instance["nickname"]))
|
2017-11-23 20:37:00 +00:00
|
|
|
if instance["protocol"] == "plain":
|
|
|
|
if instance["bind"] == None:
|
2018-02-03 19:03:47 +00:00
|
|
|
bot = IRCBotFactory(name)
|
|
|
|
rct = reactor.connectTCP(instance["host"], instance["port"], bot, timeout=int(instance["timeout"]))
|
|
|
|
|
|
|
|
ReactorPool[name] = rct
|
|
|
|
FactoryPool[name] = bot
|
2017-11-23 20:37:00 +00:00
|
|
|
return
|
|
|
|
else:
|
2018-02-03 19:03:47 +00:00
|
|
|
bot = IRCBotFactory(name)
|
|
|
|
rct = reactor.connectTCP(instance["host"], instance["port"], bot, timeout=int(instance["timeout"]), bindAddress=instance["bind"])
|
|
|
|
|
|
|
|
ReactorPool[name] = rct
|
|
|
|
FactoryPool[name] = bot
|
2017-11-23 20:37:00 +00:00
|
|
|
return
|
|
|
|
elif instance["protocol"] == "ssl":
|
2018-01-17 19:02:41 +00:00
|
|
|
keyFN = certPath+instance["key"]
|
|
|
|
certFN = certPath+instance["certificate"]
|
|
|
|
contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace"))
|
2017-11-23 20:37:00 +00:00
|
|
|
if instance["bind"] == None:
|
2018-02-03 19:03:47 +00:00
|
|
|
bot = IRCBotFactory(name)
|
|
|
|
rct = reactor.connectSSL(instance["host"], int(instance["port"]), bot, contextFactory)
|
|
|
|
|
|
|
|
ReactorPool[name] = rct
|
|
|
|
FactoryPool[name] = bot
|
2017-11-23 20:37:00 +00:00
|
|
|
return
|
|
|
|
else:
|
2018-02-03 19:03:47 +00:00
|
|
|
|
|
|
|
bot = IRCBotFactory(name)
|
|
|
|
rct = reactor.connectSSL(instance["host"], int(instance["port"]), bot, contextFactory, bindAddress=instance["bind"])
|
|
|
|
|
|
|
|
ReactorPool[name] = rct
|
|
|
|
FactoryPool[name] = bot
|
2017-11-23 20:37:00 +00:00
|
|
|
return
|
|
|
|
|
2017-11-25 00:38:06 +00:00
|
|
|
def addKeyword(self, keyword):
|
2017-12-03 13:10:51 +00:00
|
|
|
if keyword in keyconf["Keywords"]:
|
2017-11-25 00:38:06 +00:00
|
|
|
return "EXISTS"
|
|
|
|
else:
|
2017-12-03 13:10:51 +00:00
|
|
|
for i in keyconf["Keywords"]:
|
2017-11-25 00:38:06 +00:00
|
|
|
if i in keyword or keyword in i:
|
|
|
|
return "ISIN"
|
2017-12-03 13:10:51 +00:00
|
|
|
keyconf["Keywords"].append(keyword)
|
2018-01-22 08:01:23 +00:00
|
|
|
self.save("keyconf")
|
2017-11-25 00:38:06 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def delKeyword(self, keyword):
|
2017-12-03 13:10:51 +00:00
|
|
|
if not keyword in keyconf["Keywords"]:
|
2017-11-25 00:38:06 +00:00
|
|
|
return "NOKEY"
|
2017-12-03 13:10:51 +00:00
|
|
|
keyconf["Keywords"].remove(keyword)
|
2018-01-22 08:01:23 +00:00
|
|
|
self.save("keyconf")
|
2017-11-25 00:38:06 +00:00
|
|
|
return True
|
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
def parseCommand(self, addr, authed, data):
|
2017-12-25 22:09:38 +00:00
|
|
|
global pool, MonitorPool, wholist
|
2017-11-20 21:40:04 +00:00
|
|
|
spl = data.split()
|
2017-12-03 13:10:51 +00:00
|
|
|
if addr in connections.keys():
|
|
|
|
obj = connections[addr]
|
|
|
|
else:
|
2018-01-20 21:26:35 +00:00
|
|
|
warn("Got connection object with no instance in the address pool")
|
2017-12-03 13:10:51 +00:00
|
|
|
return
|
2017-11-20 21:40:04 +00:00
|
|
|
|
|
|
|
success = lambda data: sendSuccess(addr, data)
|
|
|
|
failure = lambda data: sendFailure(addr, data)
|
|
|
|
info = lambda data: sendInfo(addr, data)
|
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
incUsage = lambda mode: self.incorrectUsage(addr, mode)
|
2017-11-20 21:40:04 +00:00
|
|
|
length = len(spl)
|
|
|
|
if len(spl) > 0:
|
|
|
|
cmd = spl[0]
|
|
|
|
else:
|
2017-11-21 18:41:18 +00:00
|
|
|
failure("No text was sent")
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
if authed == True:
|
2017-11-21 20:16:14 +00:00
|
|
|
if cmd == "help":
|
|
|
|
helpMap = []
|
|
|
|
for i in help.keys():
|
|
|
|
helpMap.append("%s: %s" % (i, help[i]))
|
|
|
|
info("\n".join(helpMap))
|
|
|
|
return
|
|
|
|
|
2018-01-14 19:10:57 +00:00
|
|
|
elif cmd == "save":
|
|
|
|
if length == 2:
|
|
|
|
if spl[1] in filemap.keys():
|
|
|
|
self.save(spl[1])
|
|
|
|
success("Saved %s to %s" % (spl[1], filemap[spl[1]][0]))
|
|
|
|
return
|
2018-02-02 18:56:50 +00:00
|
|
|
elif spl[1] == "all":
|
|
|
|
for i in filemap.keys():
|
|
|
|
helper.save(i)
|
|
|
|
success("Saved %s from %s" % (i, filemap[i][0]))
|
|
|
|
return
|
2018-01-14 19:10:57 +00:00
|
|
|
else:
|
|
|
|
incUsage("save")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("save")
|
|
|
|
return
|
|
|
|
|
|
|
|
elif cmd == "load":
|
|
|
|
if length == 2:
|
|
|
|
if spl[1] in filemap.keys():
|
|
|
|
self.load(spl[1])
|
|
|
|
success("Loaded %s from %s" % (spl[1], filemap[spl[1]][0]))
|
|
|
|
return
|
2018-02-02 18:56:50 +00:00
|
|
|
elif spl[1] == "all":
|
|
|
|
for i in filemap.keys():
|
|
|
|
helper.load(i)
|
|
|
|
success("Loaded %s from %s" % (i, filemap[i][0]))
|
|
|
|
return
|
2018-01-14 19:10:57 +00:00
|
|
|
else:
|
|
|
|
incUsage("load")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("load")
|
|
|
|
return
|
2017-12-25 22:27:09 +00:00
|
|
|
|
2017-12-03 18:34:56 +00:00
|
|
|
elif cmd == "dist":
|
2018-01-17 19:02:41 +00:00
|
|
|
if config["Dist"]["Enabled"]:
|
|
|
|
rtrn = run([config["Dist"]["File"]], shell=True, stdout=PIPE)
|
|
|
|
if config["Dist"]["SendOutput"]:
|
2017-12-03 19:23:29 +00:00
|
|
|
info("Exit code: %s -- Stdout: %s" % (rtrn.returncode, rtrn.stdout))
|
|
|
|
else:
|
|
|
|
info("Exit code: %s" % rtrn.returncode)
|
2017-12-03 18:34:56 +00:00
|
|
|
else:
|
|
|
|
failure("The dist command is not enabled")
|
|
|
|
return
|
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
elif cmd == "pass":
|
2017-11-20 21:40:04 +00:00
|
|
|
info("You are already authenticated")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-11-20 21:40:04 +00:00
|
|
|
elif cmd == "logout":
|
|
|
|
obj.authed = False
|
|
|
|
success("Logged out")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
|
|
|
elif cmd == "list":
|
|
|
|
poolMap = []
|
|
|
|
for i in pool.keys():
|
2017-11-23 19:11:29 +00:00
|
|
|
poolMap.append("Server: %s" % i)
|
|
|
|
for x in pool[i].keys():
|
|
|
|
poolMap.append("%s: %s" % (x, pool[i][x]))
|
|
|
|
poolMap.append("\n")
|
2017-11-21 20:16:14 +00:00
|
|
|
info("\n".join(poolMap))
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-12-25 22:09:38 +00:00
|
|
|
elif cmd == "stats":
|
|
|
|
if length == 1:
|
|
|
|
stats = []
|
|
|
|
numChannels = 0
|
|
|
|
numWhoEntries = 0
|
|
|
|
for i in IRCPool.keys():
|
|
|
|
numChannels += len(IRCPool[i].channels)
|
|
|
|
for i in wholist.keys():
|
|
|
|
numWhoEntries += len(wholist[i].keys())
|
|
|
|
stats.append("Servers: %s" % len(IRCPool.keys()))
|
|
|
|
stats.append("Channels: %s" % numChannels)
|
|
|
|
stats.append("User records: %s" % numWhoEntries)
|
|
|
|
info("\n".join(stats))
|
|
|
|
return
|
|
|
|
|
|
|
|
elif length == 2:
|
|
|
|
stats = []
|
|
|
|
numChannels = 0
|
|
|
|
numWhoEntries = 0
|
|
|
|
|
|
|
|
failures = 0
|
|
|
|
if spl[1] in IRCPool.keys():
|
|
|
|
numChannels += len(IRCPool[spl[1]].channels)
|
|
|
|
else:
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
failures += 1
|
|
|
|
if spl[1] in wholist.keys():
|
|
|
|
numWhoEntries += len(wholist[spl[1]].keys())
|
|
|
|
else:
|
|
|
|
failure("Who entry does not exist: %s" % spl[1])
|
|
|
|
failures += 1
|
|
|
|
if failures == 2:
|
|
|
|
failure("No information found, aborting")
|
|
|
|
return
|
|
|
|
stats.append("Channels: %s" % numChannels)
|
|
|
|
stats.append("User records: %s" % numWhoEntries)
|
|
|
|
info("\n".join(stats))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("stats")
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
elif cmd == "enable":
|
|
|
|
if length == 2:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
pool[spl[1]]["enabled"] = True
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("pool")
|
2017-11-25 19:44:03 +00:00
|
|
|
if not spl[1] in IRCPool.keys():
|
|
|
|
self.addBot(spl[1])
|
2017-11-25 20:28:49 +00:00
|
|
|
else:
|
2017-11-27 20:18:39 +00:00
|
|
|
pass
|
2017-11-23 20:37:00 +00:00
|
|
|
success("Successfully enabled bot %s" % spl[1])
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("enable")
|
|
|
|
return
|
|
|
|
|
|
|
|
elif cmd == "disable":
|
|
|
|
if length == 2:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
pool[spl[1]]["enabled"] = False
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("pool")
|
2018-02-03 19:03:47 +00:00
|
|
|
if spl[1] in ReactorPool.keys():
|
|
|
|
if spl[1] in FactoryPool.keys():
|
|
|
|
FactoryPool[spl[1]].stopTrying()
|
|
|
|
ReactorPool[spl[1]].disconnect()
|
|
|
|
del IRCPool[spl[1]]
|
|
|
|
del ReactorPool[spl[1]]
|
|
|
|
del FactoryPool[spl[1]]
|
2017-11-23 20:37:00 +00:00
|
|
|
success("Successfully disabled bot %s" % spl[1])
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("disable")
|
|
|
|
return
|
|
|
|
|
2017-11-24 19:16:08 +00:00
|
|
|
elif cmd == "join":
|
|
|
|
if length == 3:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[1] in IRCPool.keys():
|
|
|
|
failure("Name has no instance: %s" % spl[1])
|
|
|
|
return
|
|
|
|
IRCPool[spl[1]].join(spl[2])
|
|
|
|
success("Joined %s" % spl[2])
|
|
|
|
return
|
|
|
|
elif length == 4:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[1] in IRCPool.keys():
|
|
|
|
failure("Name has no instance: %s" % spl[1])
|
|
|
|
return
|
|
|
|
IRCPool[spl[1]].join(spl[2], spl[3])
|
|
|
|
success("Joined %s with key %s" % (spl[2], spl[3]))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("join")
|
2017-11-25 00:38:06 +00:00
|
|
|
return
|
2017-11-24 19:16:08 +00:00
|
|
|
|
|
|
|
elif cmd == "part":
|
|
|
|
if length == 3:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[1] in IRCPool.keys():
|
|
|
|
failure("Name has no instance: %s" % spl[1])
|
|
|
|
return
|
|
|
|
IRCPool[spl[1]].part(spl[2])
|
|
|
|
success("Left %s" % spl[2])
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("part")
|
2017-11-25 00:38:06 +00:00
|
|
|
return
|
2017-11-24 19:16:08 +00:00
|
|
|
|
|
|
|
elif cmd == "get":
|
|
|
|
if length == 3:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[1] in IRCPool.keys():
|
|
|
|
failure("Name has no instance: %s" % spl[1])
|
|
|
|
return
|
|
|
|
info(str(IRCPool[spl[1]].get(spl[2])))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("get")
|
2017-11-25 00:38:06 +00:00
|
|
|
return
|
|
|
|
|
2017-12-25 19:02:17 +00:00
|
|
|
elif cmd == "who":
|
|
|
|
if length == 2:
|
|
|
|
result = self.getWho(spl[1])
|
|
|
|
rtrn = ""
|
|
|
|
for i in result.keys():
|
|
|
|
rtrn += "Matches from: %s" % i
|
|
|
|
rtrn += "\n"
|
|
|
|
for x in result[i]:
|
2017-12-25 22:09:38 +00:00
|
|
|
x = [y for y in x if not y == None]
|
|
|
|
rtrn += str((", ".join(x)))
|
2017-12-25 19:02:17 +00:00
|
|
|
rtrn += "\n"
|
|
|
|
info(rtrn)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("who")
|
|
|
|
return
|
|
|
|
|
2017-11-25 00:38:06 +00:00
|
|
|
elif cmd == "key":
|
|
|
|
if data.startswith("key add"):
|
2017-11-27 19:54:35 +00:00
|
|
|
if not data in ["key add ", "key add"] and data[3] == " ":
|
2017-11-25 00:38:06 +00:00
|
|
|
keywordsToAdd = data[8:]
|
|
|
|
keywords = keywordsToAdd.split(",")
|
|
|
|
for keyword in keywords:
|
|
|
|
rtrn = self.addKeyword(keyword)
|
|
|
|
if rtrn == "EXISTS":
|
|
|
|
failure("Keyword already exists: %s" % keyword)
|
|
|
|
elif rtrn == "ISIN":
|
|
|
|
failure("Keyword already matched: %s" % keyword)
|
|
|
|
elif rtrn == True:
|
|
|
|
success("Keyword added: %s" % keyword)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
|
|
|
elif data.startswith("key del"):
|
2017-11-27 19:54:35 +00:00
|
|
|
if not data in ["key del ", "key del"] and data[3] == " ":
|
2017-11-25 00:38:06 +00:00
|
|
|
keywordsToDel = data[8:]
|
|
|
|
keywords = keywordsToDel.split(",")
|
|
|
|
for keyword in keywords:
|
|
|
|
rtrn = self.delKeyword(keyword)
|
|
|
|
if rtrn == "NOKEY":
|
|
|
|
failure("Keyword does not exist: %s" % keyword)
|
|
|
|
elif rtrn == True:
|
|
|
|
success("Keyword deleted: %s" % keyword)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
2017-11-27 19:54:35 +00:00
|
|
|
if length == 4:
|
|
|
|
if spl[1] == "except":
|
2017-12-03 13:10:51 +00:00
|
|
|
if not spl[2] in keyconf["Keywords"]:
|
2017-11-27 19:54:35 +00:00
|
|
|
failure("No such keyword: %s" % spl[2])
|
|
|
|
return
|
2017-12-03 13:10:51 +00:00
|
|
|
if spl[2] in keyconf["KeywordsExcept"].keys():
|
|
|
|
if spl[3] in keyconf["KeywordsExcept"][spl[2]]:
|
2017-11-27 19:54:35 +00:00
|
|
|
failure("Exception exists: %s" % spl[3])
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if not spl[2] in spl[3]:
|
|
|
|
failure("Keyword %s not in exception %s. This won't work" % (spl[2], spl[3]))
|
|
|
|
return
|
2017-12-03 13:10:51 +00:00
|
|
|
keyconf["KeywordsExcept"][spl[2]] = []
|
2017-11-27 19:54:35 +00:00
|
|
|
|
2017-12-03 13:10:51 +00:00
|
|
|
keyconf["KeywordsExcept"][spl[2]].append(spl[3])
|
2018-01-22 08:01:23 +00:00
|
|
|
self.save("keyconf")
|
2017-11-27 19:54:35 +00:00
|
|
|
success("Successfully added exception %s for keyword %s" % (spl[3], spl[2]))
|
|
|
|
return
|
2017-12-03 19:11:06 +00:00
|
|
|
elif spl[1] == "master":
|
|
|
|
if not spl[2] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[2])
|
|
|
|
return
|
|
|
|
if spl[2] in IRCPool.keys():
|
|
|
|
if not spl[3] in IRCPool[spl[2]].channels:
|
|
|
|
info("Bot not on channel: %s" % spl[3])
|
|
|
|
config["Master"] = [spl[2], spl[3]]
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("config")
|
2017-12-03 19:11:06 +00:00
|
|
|
success("Master set to %s on %s" % (spl[3], spl[2]))
|
|
|
|
return
|
2017-11-27 19:54:35 +00:00
|
|
|
elif spl[1] == "unexcept":
|
2017-12-03 13:10:51 +00:00
|
|
|
if not spl[2] in keyconf["KeywordsExcept"].keys():
|
2017-11-27 19:54:35 +00:00
|
|
|
failure("No such exception: %s" % spl[2])
|
|
|
|
return
|
2017-12-03 13:10:51 +00:00
|
|
|
if not spl[3] in keyconf["KeywordsExcept"][spl[2]]:
|
2017-11-27 19:54:35 +00:00
|
|
|
failure("Exception %s has no attribute %s" % (spl[2], spl[3]))
|
|
|
|
return
|
2017-12-03 13:10:51 +00:00
|
|
|
keyconf["KeywordsExcept"][spl[2]].remove(spl[3])
|
|
|
|
if keyconf["KeywordsExcept"][spl[2]] == []:
|
|
|
|
del keyconf["KeywordsExcept"][spl[2]]
|
2018-01-22 08:01:23 +00:00
|
|
|
self.save("keyconf")
|
2017-11-27 19:54:35 +00:00
|
|
|
success("Successfully removed exception %s for keyword %s" % (spl[3], spl[2]))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
2017-12-03 19:11:06 +00:00
|
|
|
elif length == 3:
|
2017-11-27 19:54:35 +00:00
|
|
|
if spl[1] == "unexcept":
|
2017-12-03 13:10:51 +00:00
|
|
|
if not spl[2] in keyconf["KeywordsExcept"].keys():
|
2017-11-27 19:54:35 +00:00
|
|
|
failure("No such exception: %s" % spl[2])
|
|
|
|
return
|
2017-12-03 13:10:51 +00:00
|
|
|
del keyconf["KeywordsExcept"][spl[2]]
|
2018-01-22 08:01:23 +00:00
|
|
|
self.save("keyconf")
|
2017-11-27 19:54:35 +00:00
|
|
|
success("Successfully removed exception list of %s" % spl[2])
|
|
|
|
return
|
2017-12-24 21:04:48 +00:00
|
|
|
elif spl[1] == "monitor":
|
|
|
|
if spl[2] == "on":
|
|
|
|
if not obj.addr in MonitorPool:
|
|
|
|
MonitorPool.append(obj.addr)
|
|
|
|
success("Keyword monitoring enabled")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
failure("Keyword monitoring is already enabled")
|
|
|
|
return
|
|
|
|
elif spl[2] == "off":
|
|
|
|
if obj.addr in MonitorPool:
|
|
|
|
MonitorPool.remove(obj.addr)
|
|
|
|
success("Keyword monitoring disabled")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
failure("Keyword monitoring is already disabled")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
2017-12-03 19:11:06 +00:00
|
|
|
else:
|
|
|
|
incUsage("key")
|
2017-12-24 21:04:48 +00:00
|
|
|
return
|
2017-12-03 19:11:06 +00:00
|
|
|
elif length == 2:
|
2017-11-27 19:54:35 +00:00
|
|
|
if spl[1] == "show":
|
2017-12-03 13:10:51 +00:00
|
|
|
info(",".join(keyconf["Keywords"]))
|
2017-11-27 19:54:35 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
elif spl[1] == "showexcept":
|
|
|
|
exceptMap = []
|
2017-12-03 13:10:51 +00:00
|
|
|
for i in keyconf["KeywordsExcept"].keys():
|
2017-11-27 19:54:35 +00:00
|
|
|
exceptMap.append("Key: %s" % i)
|
2017-12-03 13:10:51 +00:00
|
|
|
exceptMap.append("%s: %s" % (i, ",".join(keyconf["KeywordsExcept"][i])))
|
2017-11-27 19:54:35 +00:00
|
|
|
exceptMap.append("\n")
|
|
|
|
info("\n".join(exceptMap))
|
|
|
|
return
|
2017-12-03 19:11:06 +00:00
|
|
|
elif spl[1] == "master":
|
2017-11-25 00:38:06 +00:00
|
|
|
info(" - ".join(config["Master"]))
|
|
|
|
return
|
2017-12-24 21:04:48 +00:00
|
|
|
elif spl[1] == "monitor":
|
|
|
|
if obj.addr in MonitorPool:
|
|
|
|
info("Keyword monitoring is enabled on this connection")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
info("Keyword monitoring is disabled on this connection")
|
|
|
|
return
|
2017-11-25 00:38:06 +00:00
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
|
|
|
|
2017-12-25 19:02:17 +00:00
|
|
|
else:
|
|
|
|
incUsage("key")
|
|
|
|
return
|
|
|
|
|
2017-11-23 18:45:18 +00:00
|
|
|
elif cmd == "add":
|
2017-12-26 13:57:29 +00:00
|
|
|
if length > 6:
|
|
|
|
failure("Too many arguments")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-12-26 13:57:29 +00:00
|
|
|
if length > 1:
|
|
|
|
name = spl[1]
|
|
|
|
else:
|
|
|
|
incUsage("add")
|
|
|
|
return
|
|
|
|
if length > 2:
|
|
|
|
host = spl[2]
|
|
|
|
if length > 3:
|
|
|
|
port = spl[3]
|
|
|
|
if length > 4:
|
|
|
|
protocol = spl[4]
|
|
|
|
if length > 5:
|
|
|
|
nickname = spl[5]
|
|
|
|
|
|
|
|
toFail = False
|
|
|
|
if length < 6:
|
|
|
|
if config["Default"]["nickname"] == None:
|
|
|
|
failure("Choose a nickname, or configure one in defaults")
|
|
|
|
toFail = True
|
|
|
|
else:
|
|
|
|
nickname = config["Default"]["nickname"]
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-12-26 13:57:29 +00:00
|
|
|
if length < 5:
|
|
|
|
if config["Default"]["protocol"] == None:
|
|
|
|
failure("Choose a protocol, or configure one in defaults")
|
|
|
|
toFail = True
|
|
|
|
else:
|
|
|
|
protocol = config["Default"]["protocol"]
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-12-26 13:57:29 +00:00
|
|
|
if length < 4:
|
|
|
|
if config["Default"]["port"] == None:
|
|
|
|
failure("Choose a port, or configure one in defaults")
|
|
|
|
toFail = True
|
|
|
|
else:
|
|
|
|
port = config["Default"]["port"]
|
2017-11-23 19:19:48 +00:00
|
|
|
|
2017-12-26 13:57:29 +00:00
|
|
|
if length < 3:
|
|
|
|
if config["Default"]["host"] == None:
|
|
|
|
failure("Choose a host, or configure one in defaults")
|
|
|
|
toFail = True
|
|
|
|
else:
|
|
|
|
host = config["Default"]["host"]
|
|
|
|
if toFail:
|
|
|
|
failure("Stopping due to previous error(s)")
|
2017-11-21 20:16:14 +00:00
|
|
|
return
|
2017-12-26 13:57:29 +00:00
|
|
|
|
|
|
|
if length < 2:
|
2017-11-23 18:45:18 +00:00
|
|
|
incUsage("add")
|
|
|
|
return
|
|
|
|
|
2017-12-26 13:57:29 +00:00
|
|
|
if name in pool.keys():
|
|
|
|
failure("Name already exists: %s" % name)
|
|
|
|
return
|
|
|
|
|
|
|
|
protocol = protocol.lower()
|
|
|
|
|
|
|
|
if not protocol in ["ssl", "plain"]:
|
|
|
|
failure("Protocol must be ssl or plain, not %s" % protocol)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
int(port)
|
|
|
|
except:
|
|
|
|
failure("Port must be an integer, not %s" % port)
|
|
|
|
return
|
|
|
|
|
|
|
|
pool[name] = { "host": host,
|
|
|
|
"port": port,
|
|
|
|
"protocol": protocol,
|
|
|
|
"bind": config["Default"]["bind"],
|
|
|
|
"timeout": config["Default"]["timeout"],
|
|
|
|
"nickname": nickname,
|
|
|
|
"username": config["Default"]["username"],
|
|
|
|
"realname": config["Default"]["realname"],
|
|
|
|
"userinfo": config["Default"]["userinfo"],
|
|
|
|
"finger": config["Default"]["finger"],
|
|
|
|
"version": config["Default"]["version"],
|
|
|
|
"source": config["Default"]["source"],
|
|
|
|
"autojoin": config["Default"]["autojoin"],
|
|
|
|
"authtype": config["Default"]["authtype"],
|
|
|
|
"password": config["Default"]["password"],
|
|
|
|
"authentity": config["Default"]["authentity"],
|
2018-01-20 21:39:43 +00:00
|
|
|
"key": config["Default"]["key"],
|
|
|
|
"certificate": config["Default"]["certificate"],
|
2017-12-26 13:57:29 +00:00
|
|
|
"enabled": config["ConnectOnCreate"],
|
|
|
|
}
|
|
|
|
if config["ConnectOnCreate"] == True:
|
|
|
|
self.addBot(name)
|
|
|
|
success("Successfully created bot")
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("pool")
|
2017-12-26 13:57:29 +00:00
|
|
|
return
|
|
|
|
|
2017-11-23 18:45:18 +00:00
|
|
|
elif cmd == "del":
|
|
|
|
if length == 2:
|
|
|
|
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
del pool[spl[1]]
|
2017-11-23 20:37:00 +00:00
|
|
|
if spl[1] in IRCPool.keys():
|
2017-11-24 19:16:08 +00:00
|
|
|
if IRCPool[spl[1]].connected == True:
|
|
|
|
IRCPool[spl[1]].transport.loseConnection()
|
2017-11-23 20:37:00 +00:00
|
|
|
del IRCPool[spl[1]]
|
2017-11-23 18:45:18 +00:00
|
|
|
success("Successfully removed bot")
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("pool")
|
2017-11-23 18:45:18 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("del")
|
2017-11-21 20:16:14 +00:00
|
|
|
return
|
2017-12-26 14:25:36 +00:00
|
|
|
elif cmd == "default":
|
|
|
|
toUnset = False
|
|
|
|
if length == 1:
|
|
|
|
optionMap = ["Viewing defaults"]
|
|
|
|
for i in config["Default"].keys():
|
|
|
|
optionMap.append("%s: %s" % (i, config["Default"][i]))
|
|
|
|
info("\n".join(optionMap))
|
|
|
|
return
|
|
|
|
elif length == 2:
|
|
|
|
if not spl[1] in config["Default"].keys():
|
|
|
|
failure("No such key: %s" % spl[1])
|
|
|
|
return
|
|
|
|
info("%s: %s" % (spl[1], config["Default"][spl[1]]))
|
|
|
|
return
|
|
|
|
elif length == 3:
|
|
|
|
if not spl[1] in config["Default"].keys():
|
|
|
|
failure("No such key: %s" % spl[1])
|
|
|
|
return
|
|
|
|
|
|
|
|
if spl[2].lower() in ["none", "nil"]:
|
|
|
|
spl[2] = None
|
|
|
|
toUnset = True
|
|
|
|
|
2018-02-03 19:03:47 +00:00
|
|
|
if spl[1] in ["port", "timeout", "maxdelay", "initialdelay", "factor", "jitter"]:
|
2017-12-26 14:25:36 +00:00
|
|
|
try:
|
2018-02-03 19:03:47 +00:00
|
|
|
spl[2] = int(spl[2])
|
2017-12-26 14:25:36 +00:00
|
|
|
except:
|
|
|
|
failure("Value must be an integer, not %s" % spl[2])
|
|
|
|
return
|
|
|
|
|
|
|
|
if spl[1] == "protocol":
|
|
|
|
if not toUnset:
|
|
|
|
if not spl[2] in ["ssl", "plain"]:
|
|
|
|
failure("Protocol must be ssl or plain, not %s" % spl[2])
|
|
|
|
return
|
|
|
|
|
|
|
|
if spl[2] == config["Default"][spl[1]]:
|
|
|
|
failure("Value already exists: %s" % spl[2])
|
|
|
|
return
|
|
|
|
|
|
|
|
if spl[1] == "authtype":
|
|
|
|
if not toUnset:
|
|
|
|
if not spl[2] in ["sp", "ns"]:
|
|
|
|
failure("Authtype must be sp or ns, not %s" % spl[2])
|
|
|
|
return
|
|
|
|
if spl[1] == "enabled":
|
|
|
|
failure("Use the ConnectOnCreate config parameter to set this")
|
|
|
|
return
|
|
|
|
if spl[1] == "autojoin":
|
|
|
|
if not toUnset:
|
|
|
|
spl[2] = spl[2].split(",")
|
|
|
|
else:
|
|
|
|
spl[2] = []
|
|
|
|
|
|
|
|
config["Default"][spl[1]] = spl[2]
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("config")
|
2017-12-26 14:25:36 +00:00
|
|
|
if toUnset:
|
|
|
|
success("Successfully unset key %s" % spl[1])
|
|
|
|
else:
|
|
|
|
success("Successfully set key %s to %s" % (spl[1], spl[2]))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
incUsage("default")
|
|
|
|
return
|
2017-11-21 20:16:14 +00:00
|
|
|
|
2017-11-23 19:11:29 +00:00
|
|
|
elif cmd == "mod":
|
2017-11-23 20:37:00 +00:00
|
|
|
toUnset = False
|
2017-11-23 19:11:29 +00:00
|
|
|
if length == 2:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
optionMap = ["Viewing options for %s" % spl[1]]
|
|
|
|
for i in pool[spl[1]].keys():
|
|
|
|
optionMap.append("%s: %s" % (i, pool[spl[1]][i]))
|
|
|
|
info("\n".join(optionMap))
|
|
|
|
return
|
|
|
|
|
|
|
|
elif length == 3:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[2] in pool[spl[1]].keys():
|
|
|
|
failure("No such key: %s" % spl[2])
|
|
|
|
return
|
|
|
|
info("%s: %s" % (spl[2], pool[spl[1]][spl[2]]))
|
|
|
|
return
|
|
|
|
|
|
|
|
elif length == 4:
|
|
|
|
if not spl[1] in pool.keys():
|
|
|
|
failure("Name does not exist: %s" % spl[1])
|
|
|
|
return
|
|
|
|
if not spl[2] in pool[spl[1]].keys():
|
|
|
|
failure("No such key: %s" % spl[2])
|
|
|
|
return
|
2018-02-03 19:03:47 +00:00
|
|
|
|
2017-11-23 19:19:48 +00:00
|
|
|
if spl[2] == "protocol":
|
|
|
|
if not spl[3] in ["ssl", "plain"]:
|
|
|
|
failure("Protocol must be ssl or plain, not %s" % spl[3])
|
|
|
|
return
|
2017-11-23 19:11:29 +00:00
|
|
|
if spl[3] == pool[spl[1]][spl[2]]:
|
|
|
|
failure("Value already exists: %s" % spl[3])
|
|
|
|
return
|
2017-11-23 20:37:00 +00:00
|
|
|
|
|
|
|
if spl[3].lower() in ["none", "nil"]:
|
|
|
|
spl[3] = None
|
|
|
|
toUnset = True
|
|
|
|
|
2018-02-03 19:03:47 +00:00
|
|
|
if spl[2] in ["port", "timeout", "maxdelay", "initialdelay", "factor", "jitter"]:
|
|
|
|
try:
|
|
|
|
spl[3] = int(spl[3])
|
|
|
|
except:
|
|
|
|
failure("Value must be an integer, not %s" % spl[3])
|
|
|
|
return
|
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
if spl[2] == "authtype":
|
2017-11-25 19:29:48 +00:00
|
|
|
if not toUnset:
|
|
|
|
if not spl[3] in ["sp", "ns"]:
|
|
|
|
failure("Authtype must be sp or ns, not %s" % spl[3])
|
|
|
|
return
|
2017-11-23 20:37:00 +00:00
|
|
|
if spl[2] == "enabled":
|
|
|
|
failure("Use the enable and disable commands to manage this")
|
|
|
|
return
|
2017-11-25 19:48:20 +00:00
|
|
|
if spl[2] == "autojoin":
|
|
|
|
spl[3] = spl[3].split(",")
|
2017-11-23 20:37:00 +00:00
|
|
|
|
2017-11-23 19:11:29 +00:00
|
|
|
pool[spl[1]][spl[2]] = spl[3]
|
2017-11-24 19:16:08 +00:00
|
|
|
if spl[1] in IRCPool.keys():
|
|
|
|
IRCPool[spl[1]].refresh()
|
2018-01-14 19:10:57 +00:00
|
|
|
self.save("pool")
|
2017-11-23 20:37:00 +00:00
|
|
|
if toUnset:
|
2017-11-25 19:26:13 +00:00
|
|
|
success("Successfully unset key %s on %s" % (spl[2], spl[1]))
|
2017-11-23 20:37:00 +00:00
|
|
|
else:
|
|
|
|
success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1]))
|
2017-11-23 19:11:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
incUsage("mod")
|
2017-11-25 00:38:06 +00:00
|
|
|
return
|
2017-11-23 19:11:29 +00:00
|
|
|
|
2017-11-21 20:16:14 +00:00
|
|
|
else:
|
|
|
|
incUsage(None)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if cmd == "pass" and length == 2:
|
2017-11-23 18:32:30 +00:00
|
|
|
if spl[1] == config["Password"]:
|
2017-11-21 20:16:14 +00:00
|
|
|
success("Authenticated successfully")
|
|
|
|
obj.authed = True
|
|
|
|
return
|
2017-11-20 21:40:04 +00:00
|
|
|
else:
|
2017-11-21 20:16:14 +00:00
|
|
|
failure("Password incorrect")
|
|
|
|
obj.transport.loseConnection()
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
|
|
|
else:
|
2017-11-21 20:16:14 +00:00
|
|
|
incUsage(None)
|
2017-11-20 21:40:04 +00:00
|
|
|
return
|
|
|
|
|
2017-11-19 14:46:42 +00:00
|
|
|
if __name__ == "__main__":
|
2017-11-20 19:53:25 +00:00
|
|
|
helper = Helper()
|
2018-01-14 19:10:57 +00:00
|
|
|
for i in filemap.keys():
|
|
|
|
helper.load(i)
|
2017-12-25 22:27:09 +00:00
|
|
|
|
2017-11-23 20:37:00 +00:00
|
|
|
for i in pool.keys():
|
|
|
|
if pool[i]["enabled"] == True:
|
|
|
|
helper.addBot(i)
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
listener = BaseFactory()
|
2018-01-17 19:02:41 +00:00
|
|
|
if config["Listener"]["UseSSL"] == True:
|
|
|
|
reactor.listenSSL(config["Listener"]["Port"], listener, DefaultOpenSSLContextFactory(certPath+config["Listener"]["Key"], certPath+config["Listener"]["Certificate"]), interface=config["Listener"]["Address"])
|
|
|
|
log("Threshold running with SSL on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
else:
|
2018-01-17 19:02:41 +00:00
|
|
|
reactor.listenTCP(config["Listener"]["Port"], listener, interface=config["Listener"]["Address"])
|
|
|
|
log("Threshold running on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
|
2017-11-19 14:46:42 +00:00
|
|
|
|
|
|
|
reactor.run()
|