Separate out into different directories and reshuffle configuration directives

This commit is contained in:
Mark Veidemanis 2018-01-17 19:02:41 +00:00
parent 12b6fe2acb
commit a7599dddfe
4 changed files with 82 additions and 26 deletions

40
conf/example.json Normal file
View File

@ -0,0 +1,40 @@
{
"Listener": {
"Port": 13867,
"Address": "127.0.0.1",
"UseSSL": true,
"Key": "key.pem",
"Certificate": "cert.pem"
},
"UsePassword": true,
"ConnectOnCreate": false,
"HighlightNotifications": true,
"Dist": {
"Enabled": true,
"SendOutput": false,
"File": "conf/dist.sh"
},
"Password": "s",
"Default": {
"host": null,
"port": null,
"protocol": null,
"bind": null,
"timeout": 30,
"nickname": null,
"username": null,
"realname": null,
"userinfo": null,
"finger": null,
"version": null,
"source": null,
"autojoin": [],
"authtype": null,
"password": null,
"authentity": "NickServ",
"key": "key.pem",
"certificate": "cert.pem",
"enabled": true
},
"Master": [null, null]
}

19
conf/help.json Normal file
View File

@ -0,0 +1,19 @@
{
"pass": "pass <password>",
"logout": "logout",
"add": "add <name> [<address>] [<port>] [<ssl|plain>] [<nickname>]",
"del": "del <name>",
"mod": "mod <name> [<key>] [<value>]",
"default": "default [<key>] [<value>]",
"get": "get <name> <variable>",
"key": "key <master|show|add|del|except|unexcept|showexcept|monitor> [<name>] [<target>] [<key>] [<on|off>]",
"who": "who <nick>",
"join": "join <name> <channel> [<key>]",
"enable": "enable <name>",
"disable": "disable <name>",
"list": "list",
"stats": "stats",
"save": "save <config|keyconf|pool|help|wholist|all>",
"load": "load <config|keyconf|pool|help|wholist|all>",
"dist": "dist"
}

6
conf/keyword.json Normal file
View File

@ -0,0 +1,6 @@
{
"Keywords": [
"kek"
],
"KeywordsExcept": {}
}

View File

@ -22,6 +22,9 @@ IRCPool = {}
MonitorPool = [] MonitorPool = []
configPath = "conf/"
certPath = "cert/"
filemap = { filemap = {
"config": ["config.json", "configuration"], "config": ["config.json", "configuration"],
"keyconf": ["keyword.json", "keyword lists"], "keyconf": ["keyword.json", "keyword lists"],
@ -314,20 +317,6 @@ class BaseFactory(Factory):
return return
class Helper(object): class Helper(object):
def getConfig(self):
with open("config.json", "r") as f:
config = load(f)
if set(["Port", "BindAddress", "UseSSL", "UsePassword"]).issubset(set(config.keys())):
if config["UseSSL"] == True:
if not set(["ListenerKey", "ListenerCertificate"]).issubset(set(config.keys())):
error("SSL is on but certificate or key is not defined")
if config["UsePassword"] == True:
if not "Password" in config.keys():
error("Password authentication is on but password is not defined")
return config
else:
error("Mandatory values missing from config")
def setWho(self, network, newObjects): def setWho(self, network, newObjects):
global wholist global wholist
network = "".join([x for x in network if not x in numbers]) network = "".join([x for x in network if not x in numbers])
@ -360,12 +349,12 @@ class Helper(object):
return result return result
def save(self, var): def save(self, var):
with open(filemap[var][0], "w") as f: with open(configPath+filemap[var][0], "w") as f:
dump(globals()[var], f, indent=4) dump(globals()[var], f, indent=4)
return return
def load(self, var): def load(self, var):
with open(filemap[var][0], "r") as f: with open(configPath+filemap[var][0], "r") as f:
globals()[var] = load(f) globals()[var] = load(f)
def incorrectUsage(self, addr, mode): def incorrectUsage(self, addr, mode):
@ -437,7 +426,7 @@ class Helper(object):
helper.sendMaster("MATCH %s %s (U:%s T:%s): (%s/%s) %s" % (actType, name, toSend[1], toSend[2], user, channel, toSend[0])) helper.sendMaster("MATCH %s %s (U:%s T:%s): (%s/%s) %s" % (actType, name, toSend[1], toSend[2], user, channel, toSend[0]))
def addBot(self, name): def addBot(self, name):
global IRCPool global IRCPool, certPath
instance = pool[name] instance = pool[name]
log("Started bot %s to %s:%s protocol %s nickname %s" % (name, instance["host"], instance["port"], instance["protocol"], instance["nickname"])) log("Started bot %s to %s:%s protocol %s nickname %s" % (name, instance["host"], instance["port"], instance["protocol"], instance["nickname"]))
if instance["protocol"] == "plain": if instance["protocol"] == "plain":
@ -454,7 +443,9 @@ class Helper(object):
d = connectProtocol(point, bot) d = connectProtocol(point, bot)
return return
elif instance["protocol"] == "ssl": elif instance["protocol"] == "ssl":
contextFactory = DefaultOpenSSLContextFactory(instance["key"].encode("utf-8", "replace"), instance["certificate"].encode("utf-8", "replace")) keyFN = certPath+instance["key"]
certFN = certPath+instance["certificate"]
contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace"))
if instance["bind"] == None: if instance["bind"] == None:
point = SSL4ClientEndpoint(reactor, instance["host"], int(instance["port"]), contextFactory, timeout=int(instance["timeout"])) point = SSL4ClientEndpoint(reactor, instance["host"], int(instance["port"]), contextFactory, timeout=int(instance["timeout"]))
bot = IRCBot(name) bot = IRCBot(name)
@ -542,9 +533,9 @@ class Helper(object):
return return
elif cmd == "dist": elif cmd == "dist":
if config["DistEnabled"]: if config["Dist"]["Enabled"]:
rtrn = run(["./dist.sh"], shell=True, stdout=PIPE) rtrn = run([config["Dist"]["File"]], shell=True, stdout=PIPE)
if config["SendDistOutput"]: if config["Dist"]["SendOutput"]:
info("Exit code: %s -- Stdout: %s" % (rtrn.returncode, rtrn.stdout)) info("Exit code: %s -- Stdout: %s" % (rtrn.returncode, rtrn.stdout))
else: else:
info("Exit code: %s" % rtrn.returncode) info("Exit code: %s" % rtrn.returncode)
@ -1135,11 +1126,11 @@ if __name__ == "__main__":
helper.addBot(i) helper.addBot(i)
listener = BaseFactory() listener = BaseFactory()
if config["UseSSL"] == True: if config["Listener"]["UseSSL"] == True:
reactor.listenSSL(config["Port"], listener, DefaultOpenSSLContextFactory(config["ListenerKey"], config["ListenerCertificate"]), interface=config["BindAddress"]) 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["BindAddress"], config["Port"])) log("Threshold running with SSL on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
else: else:
reactor.listenTCP(config["Port"], listener, interface=config["BindAddress"]) reactor.listenTCP(config["Listener"]["Port"], listener, interface=config["Listener"]["Address"])
log("Threshold running on %s:%s" % (config["BindAddress"], config["Port"])) log("Threshold running on %s:%s" % (config["Listener"]["Address"], config["Listener"]["Port"]))
reactor.run() reactor.run()