From 32309ecec26eb4a53fa1bec1704e322bfc2a85fe Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 29 Sep 2019 22:45:16 +0100 Subject: [PATCH] Change alias definitions to be global, so aliases can be reused across different networks --- commands/alias.py | 12 ++++++++++++ commands/all.py | 2 +- commands/allc.py | 7 ++++--- commands/auto.py | 6 +++--- commands/disable.py | 2 +- commands/enable.py | 2 +- commands/mod.py | 31 +++++++++++++++++-------------- conf/example/alias.json | 1 + conf/help.json | 1 + core/bot.py | 4 ++-- main.py | 7 +++++++ modules/alias.py | 5 ++++- modules/network.py | 15 ++++++++++----- modules/provision.py | 4 +++- utils/{getrelay.py => get.py} | 2 +- 15 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 commands/alias.py create mode 100644 conf/example/alias.json rename utils/{getrelay.py => get.py} (93%) diff --git a/commands/alias.py b/commands/alias.py new file mode 100644 index 0000000..1c01f0d --- /dev/null +++ b/commands/alias.py @@ -0,0 +1,12 @@ +import main +from yaml import dump + +class AliasCommand: + def __init__(self, *args): + self.alias(*args) + + def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + if authed: + info(dump(main.alias)) + else: + incUsage(None) diff --git a/commands/all.py b/commands/all.py index a6c377f..e59a9fd 100644 --- a/commands/all.py +++ b/commands/all.py @@ -12,7 +12,7 @@ class AllCommand: for x in main.network[i].relays.keys(): num = main.network[i].relays[x]["id"] net = main.network[i].relays[x]["net"] - alias = main.network[i].aliases[x]["nick"] + alias = main.alias[x]["nick"] commands = {spl[1]: [" ".join(spl[2:])]} success("Sending commands to relay %s as user %s" % (num, alias+"/"+net)) deliverRelayCommands(num, commands, user=alias+"/"+net) diff --git a/commands/allc.py b/commands/allc.py index 41b1a48..5d1d7cb 100644 --- a/commands/allc.py +++ b/commands/allc.py @@ -16,8 +16,9 @@ class AllcCommand: targets.append((i, x)) elif spl[1] == "alias": for i in main.network.keys(): - [targets.append((i, x)) for x in main.network[i].aliases.keys() if - main.network[i].aliases[x]["nick"] == spl[2]] + [targets.append((i, x)) for x in main.alias.keys() if + main.alias[x]["nick"] == spl[2] and + x in main.network[i].aliases.keys()] else: incUsage("allc") return @@ -27,7 +28,7 @@ class AllcCommand: for i in targets: net = i[0] num = i[1] - alias = main.network[net].aliases[num]["nick"] + alias = main.alias[num]["nick"] commands = {spl[3]: [" ".join(spl[4:])]} success("Sending commands to relay %i as user %s" % (num, alias+"/"+net)) deliverRelayCommands(num, commands, user=alias+"/"+net) diff --git a/commands/auto.py b/commands/auto.py index 90a2839..7adf0cb 100644 --- a/commands/auto.py +++ b/commands/auto.py @@ -15,8 +15,8 @@ class AutoCommand: failure("Must be a number, not %s" % spl[2]) return relayNum = int(spl[2]) - id, alias = main.network[spl[1]].add_relay(relayNum) - success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[1], alias)) + num, alias = main.network[spl[1]].add_relay(relayNum) + success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias)) main.saveConf("network") rtrn = provision.provisionRelay(relayNum, spl[1]) success("Started provisioning network %s on relay %s for alias %s" % (spl[1], spl[2], rtrn)) @@ -26,7 +26,7 @@ class AutoCommand: failure("No such network: %s" % spl[1]) return num, alias = main.network[spl[1]].add_relay() - success("Successfully created relay %s on network %s with alias %s" % (str(num), spl[1], alias)) + success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias)) main.saveConf("network") rtrn = provision.provisionRelay(num, spl[1]) success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn)) diff --git a/commands/disable.py b/commands/disable.py index 2da50f3..2e1caeb 100644 --- a/commands/disable.py +++ b/commands/disable.py @@ -22,7 +22,7 @@ class DisableCommand: failure("No such relay: %s in network %s" % (spl[2], spl[1])) return main.network[spl[1]].relays[relayNum]["enabled"] = False - user = main.network[spl[1]].aliases[relayNum]["nick"] + user = main.alias[relayNum]["nick"] network = spl[1] relay = main.network[spl[1]].relays[relayNum] commands = {"status": ["Disconnect"]} diff --git a/commands/enable.py b/commands/enable.py index 0e88dce..af645cd 100644 --- a/commands/enable.py +++ b/commands/enable.py @@ -19,7 +19,7 @@ class EnableCommand: return main.network[spl[1]].relays[int(spl[2])]["enabled"] = True - user = main.network[spl[1]].aliases[int(spl[2])]["nick"] + user = main.alias[int(spl[2])]["nick"] network = spl[1] commands = {"status": ["Connect"]} deliverRelayCommands(int(spl[2]), commands, user=user+"/"+network) diff --git a/commands/mod.py b/commands/mod.py index 611ab9b..0df1ada 100644 --- a/commands/mod.py +++ b/commands/mod.py @@ -22,21 +22,24 @@ class ModCommand: main.saveConf("network") success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1])) return + # Find a better way to do this + #elif length == 6: + # if not spl[1] in main.network.keys(): + # failure("Network does not exist: %s" % spl[1]) + # return + # if not spl[3].isdigit(): + # failure("Must be a number, not %s" % spl[3]) + # return + # if not int(spl[3]) in main.network[spl[1]].relays.keys(): + # failure("Relay/alias does not exist: %s" % spl[3]) + # return - elif length == 6: - if not spl[1] in main.network.keys(): - failure("Network does not exist: %s" % spl[1]) - return - if not int(spl[3]) in main.network[spl[1]].relays.keys(): - failure("Relay/alias does not exist: %s" % spl[3]) - return - - try: - x = getattr(main.network[spl[1]], spl[2]) - x[spl[3]] = spl[4] - except e: - failure("Something went wrong.") - return + # try: + # x = getattr(main.network[spl[1]], spl[2]) + # x[spl[3]] = spl[4] + # except Exception as err: + # failure("Error: %s" % err) + # return else: incUsage("mod") diff --git a/conf/example/alias.json b/conf/example/alias.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/conf/example/alias.json @@ -0,0 +1 @@ +{} diff --git a/conf/help.json b/conf/help.json index 34eca54..0a7ebff 100644 --- a/conf/help.json +++ b/conf/help.json @@ -20,6 +20,7 @@ "users": "users [ ...]", "relay": "relay [] []", "network": "network [
]", + "alias": "alias", "auto": "auto ", "cmd": "cmd ", "token": "token [] []", diff --git a/core/bot.py b/core/bot.py index 952293e..d078ca1 100644 --- a/core/bot.py +++ b/core/bot.py @@ -14,7 +14,7 @@ from modules import monitor from core.relay import sendRelayNotification from utils.dedup import dedup -from utils.getrelay import getRelay +from utils.get import getRelay import main from utils.logging.log import * @@ -99,7 +99,7 @@ class IRCBot(IRCClient): self.num = num self.buffer = "" self.name = net + str(num) - alias = main.network[self.net].aliases[num] + alias = main.alias[num] relay = main.network[self.net].relays[num] self.nickname = alias["nick"] self.realname = alias["realname"] diff --git a/main.py b/main.py index c9e45e3..e7e3a6c 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ filemap = { "monitor": ["monitor.json", "monitoring database", "json"], "tokens": ["tokens.json", "authentication tokens", "json"], "aliasdata": ["aliasdata.json", "data for alias generation", "json"], + "alias": ["alias.json", "provisioned alias data", "json"], # Binary (pickle) configs "network": ["network.dat", "network list", "pickle"] @@ -59,6 +60,12 @@ def loadConf(var): if filemap[var][2] == "json": with open(configPath+filemap[var][0], "r") as f: globals()[var] = json.load(f) + if var == "alias": + # This is a hack to convert all the keys into integers since JSON + # turns them into strings... + # Dammit Jason! + global alias + alias = {int(x):y for x, y in alias.items()} elif filemap[var][2] == "pickle": try: with open(configPath+filemap[var][0], "rb") as f: diff --git a/modules/alias.py b/modules/alias.py index 0248088..e05950d 100644 --- a/modules/alias.py +++ b/modules/alias.py @@ -2,6 +2,9 @@ import main import random import re +def generate_password(): + return "".join([chr(random.randint(0, 74) + 48) for i in range(32)]) + def generate_alias(): nick = random.choice(main.aliasdata["stubs"]) rand = random.randint(1, 2) @@ -60,6 +63,6 @@ def generate_alias(): if rand == 3 or rand == 4: realname = realname.capitalize() - password = "".join([chr(random.randint(0, 74) + 48) for i in range(32)]) + password = generate_password() return {"nick": nick, "altnick": altnick, "ident": ident, "realname": realname, "password": password} diff --git a/modules/network.py b/modules/network.py index 094a0a5..066b41f 100644 --- a/modules/network.py +++ b/modules/network.py @@ -6,7 +6,7 @@ from twisted.internet import reactor from core.bot import IRCBot, IRCBotFactory import main from utils.logging.log import * -from utils.getrelay import getRelay +from utils.get import getRelay class Network: def __init__(self, net, host, port, security, auth): @@ -29,14 +29,19 @@ class Network: "net": self.net, "id": num } - self.aliases[num] = alias.generate_alias() - if main.config["ConnectOnCreate"]: - self.start_bot(num) - return num, self.aliases[num]["nick"] + password = alias.generate_password() + if not num in main.alias.keys(): + main.alias[num] = alias.generate_alias() + main.saveConf("alias") + self.aliases[num] = {"password": password} + #if main.config["ConnectOnCreate"]: -- Done in provision + # self.start_bot(num) + return num, main.alias[num]["nick"] def delete_relay(self, id): del self.relays[id] del self.aliases[id] + #del main.alias[id] - Aliases are global per num, so don't delete them! def start_bot(self, num): # a single name is given to relays in the backend diff --git a/modules/provision.py b/modules/provision.py index d2aad53..af623e1 100644 --- a/modules/provision.py +++ b/modules/provision.py @@ -57,8 +57,10 @@ def provisionRelayForNetwork(num, alias, network): return def provisionRelay(num, network): - aliasObj = main.network[network].aliases[num] + aliasObj = main.alias[num] alias = aliasObj["nick"] provisionUserData(num, *aliasObj.values()) provisionRelayForNetwork(num, alias, network) + if main.config["ConnectOnCreate"]: + main.network[network].start_bot(num) return alias diff --git a/utils/getrelay.py b/utils/get.py similarity index 93% rename from utils/getrelay.py rename to utils/get.py index 043ce44..46e9edf 100644 --- a/utils/getrelay.py +++ b/utils/get.py @@ -9,4 +9,4 @@ def getRelay(num): port = int(port) except ValueError: return False - return [host, port] + return (host, port)