From a3b81f8849e9e20816ddc69d47442e76485e26cf Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 2 Oct 2019 20:26:05 +0100 Subject: [PATCH] Implement more automated provisioning of aliases and relays, and remove networks cleanly --- commands/alias.py | 46 ++++++++++++++++++++++++++++++++++++++++++++- commands/auto.py | 20 +++++++++++++++----- commands/disable.py | 2 +- commands/network.py | 1 + conf/help.json | 2 +- modules/network.py | 15 ++++++++++++++- 6 files changed, 77 insertions(+), 9 deletions(-) diff --git a/commands/alias.py b/commands/alias.py index 1c01f0d..b501c36 100644 --- a/commands/alias.py +++ b/commands/alias.py @@ -1,5 +1,6 @@ import main from yaml import dump +from modules import alias class AliasCommand: def __init__(self, *args): @@ -7,6 +8,49 @@ class AliasCommand: def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: - info(dump(main.alias)) + if length == 1: + info(dump(main.alias)) + return + elif length == 2: + if spl[1] == "add": + nextNum = max(main.alias.keys())+1 + main.alias[nextNum] = alias.generate_alias() + success("Generated new alias: %i" % nextNum) + main.saveConf("alias") + return + else: + incUsage("alias") + return + elif length == 3: + if spl[1] == "add": + if not spl[2].isdigit(): + failure("Must be a number, not %s" % spl[2]) + return + num = int(spl[2]) + for i in range(num): + nextNum = max(main.alias.keys())+1 + main.alias[nextNum] = alias.generate_alias() + success("Generated new alias: %i" % nextNum) + main.saveConf("alias") + return + elif spl[1] == "del": + if not spl[2].isdigit(): + failure("Must be a number, not %s" % spl[2]) + return + num = int(spl[2]) + failed = False + for i in main.network.keys(): + if num in main.network[i].aliases.keys(): + failure("Alias in use by %s" % i) + failed = True + if failed: + return + del main.alias[num] + success("Removed alias: %i" % num) + main.saveConf("alias") + return + else: + incUsage("alias") + return else: incUsage(None) diff --git a/commands/auto.py b/commands/auto.py index 7adf0cb..8832bb7 100644 --- a/commands/auto.py +++ b/commands/auto.py @@ -25,13 +25,23 @@ class AutoCommand: if not spl[1] in main.network.keys(): failure("No such network: %s" % spl[1]) return - num, alias = main.network[spl[1]].add_relay() - success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias)) + for i in main.alias.keys(): + print("num", i) + num, alias = main.network[spl[1]].add_relay(i) + success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias)) + rtrn = provision.provisionRelay(num, spl[1]) + success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn)) + main.saveConf("network") + return + elif length == 1: + for i in main.network.keys(): + for x in main.alias.keys(): + num, alias = main.network[i].add_relay(x) + success("Successfully created relay %i on network %s with alias %s" % (num, i, alias)) + rtrn = provision.provisionRelay(num, i) + success("Started provisioning network %s on relay %s for alias %s" % (i, num, rtrn)) main.saveConf("network") - rtrn = provision.provisionRelay(num, spl[1]) - success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn)) return - else: incUsage("auto") return diff --git a/commands/disable.py b/commands/disable.py index 2e1caeb..62b42df 100644 --- a/commands/disable.py +++ b/commands/disable.py @@ -32,7 +32,7 @@ class DisableCommand: if name in main.FactoryPool.keys(): main.FactoryPool[name].stopTrying() main.ReactorPool[name].disconnect() - if spl[1] in main.IRCPool.keys(): + if name in main.IRCPool.keys(): del main.IRCPool[name] del main.ReactorPool[name] del main.FactoryPool[name] diff --git a/commands/network.py b/commands/network.py index 33d404c..e88a905 100644 --- a/commands/network.py +++ b/commands/network.py @@ -38,6 +38,7 @@ class NetworkCommand: elif length == 3: if spl[1] == "del": if spl[2] in main.network.keys(): + main.network[spl[2]].seppuku() # ;( del main.network[spl[2]] success("Successfully removed network: %s" % spl[2]) main.saveConf("network") diff --git a/conf/help.json b/conf/help.json index 37655a9..1e00114 100644 --- a/conf/help.json +++ b/conf/help.json @@ -20,7 +20,7 @@ "users": "users [ ...]", "relay": "relay [] []", "network": "network [
]", - "alias": "alias", + "alias": "alias []", "auto": "auto ", "cmd": "cmd ", "token": "token [] []", diff --git a/modules/network.py b/modules/network.py index 066b41f..e4eefb3 100644 --- a/modules/network.py +++ b/modules/network.py @@ -1,7 +1,7 @@ from twisted.internet.ssl import DefaultOpenSSLContextFactory import json -import modules.alias as alias +from modules import alias from twisted.internet import reactor from core.bot import IRCBot, IRCBotFactory import main @@ -43,6 +43,19 @@ class Network: del self.aliases[id] #del main.alias[id] - Aliases are global per num, so don't delete them! + def seppuku(self): + # Removes all bots in preperation for deletion + for i in self.relays.keys(): + name = self.net+str(i) + if name in main.ReactorPool.keys(): + if name in main.FactoryPool.keys(): + main.FactoryPool[name].stopTrying() + main.ReactorPool[name].disconnect() + if name in main.IRCPool.keys(): + del main.IRCPool[name] + del main.ReactorPool[name] + del main.FactoryPool[name] + def start_bot(self, num): # a single name is given to relays in the backend # e.g. freenode1 for the first relay on freenode network