Change alias definitions to be global, so aliases can be reused across different networks

This commit is contained in:
Mark Veidemanis 2019-09-29 22:45:16 +01:00
parent 355a80b19b
commit 32309ecec2
15 changed files with 68 additions and 33 deletions

12
commands/alias.py Normal file
View File

@ -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)

View File

@ -12,7 +12,7 @@ class AllCommand:
for x in main.network[i].relays.keys(): for x in main.network[i].relays.keys():
num = main.network[i].relays[x]["id"] num = main.network[i].relays[x]["id"]
net = main.network[i].relays[x]["net"] 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:])]} commands = {spl[1]: [" ".join(spl[2:])]}
success("Sending commands to relay %s as user %s" % (num, alias+"/"+net)) success("Sending commands to relay %s as user %s" % (num, alias+"/"+net))
deliverRelayCommands(num, commands, user=alias+"/"+net) deliverRelayCommands(num, commands, user=alias+"/"+net)

View File

@ -16,8 +16,9 @@ class AllcCommand:
targets.append((i, x)) targets.append((i, x))
elif spl[1] == "alias": elif spl[1] == "alias":
for i in main.network.keys(): for i in main.network.keys():
[targets.append((i, x)) for x in main.network[i].aliases.keys() if [targets.append((i, x)) for x in main.alias.keys() if
main.network[i].aliases[x]["nick"] == spl[2]] main.alias[x]["nick"] == spl[2] and
x in main.network[i].aliases.keys()]
else: else:
incUsage("allc") incUsage("allc")
return return
@ -27,7 +28,7 @@ class AllcCommand:
for i in targets: for i in targets:
net = i[0] net = i[0]
num = i[1] num = i[1]
alias = main.network[net].aliases[num]["nick"] alias = main.alias[num]["nick"]
commands = {spl[3]: [" ".join(spl[4:])]} commands = {spl[3]: [" ".join(spl[4:])]}
success("Sending commands to relay %i as user %s" % (num, alias+"/"+net)) success("Sending commands to relay %i as user %s" % (num, alias+"/"+net))
deliverRelayCommands(num, commands, user=alias+"/"+net) deliverRelayCommands(num, commands, user=alias+"/"+net)

View File

@ -15,8 +15,8 @@ class AutoCommand:
failure("Must be a number, not %s" % spl[2]) failure("Must be a number, not %s" % spl[2])
return return
relayNum = int(spl[2]) relayNum = int(spl[2])
id, alias = main.network[spl[1]].add_relay(relayNum) num, alias = main.network[spl[1]].add_relay(relayNum)
success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[1], alias)) success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias))
main.saveConf("network") main.saveConf("network")
rtrn = provision.provisionRelay(relayNum, spl[1]) rtrn = provision.provisionRelay(relayNum, spl[1])
success("Started provisioning network %s on relay %s for alias %s" % (spl[1], spl[2], rtrn)) 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]) failure("No such network: %s" % spl[1])
return return
num, alias = main.network[spl[1]].add_relay() 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") main.saveConf("network")
rtrn = provision.provisionRelay(num, spl[1]) rtrn = provision.provisionRelay(num, spl[1])
success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn)) success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn))

View File

@ -22,7 +22,7 @@ class DisableCommand:
failure("No such relay: %s in network %s" % (spl[2], spl[1])) failure("No such relay: %s in network %s" % (spl[2], spl[1]))
return return
main.network[spl[1]].relays[relayNum]["enabled"] = False main.network[spl[1]].relays[relayNum]["enabled"] = False
user = main.network[spl[1]].aliases[relayNum]["nick"] user = main.alias[relayNum]["nick"]
network = spl[1] network = spl[1]
relay = main.network[spl[1]].relays[relayNum] relay = main.network[spl[1]].relays[relayNum]
commands = {"status": ["Disconnect"]} commands = {"status": ["Disconnect"]}

View File

@ -19,7 +19,7 @@ class EnableCommand:
return return
main.network[spl[1]].relays[int(spl[2])]["enabled"] = True 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] network = spl[1]
commands = {"status": ["Connect"]} commands = {"status": ["Connect"]}
deliverRelayCommands(int(spl[2]), commands, user=user+"/"+network) deliverRelayCommands(int(spl[2]), commands, user=user+"/"+network)

View File

@ -22,21 +22,24 @@ class ModCommand:
main.saveConf("network") main.saveConf("network")
success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1])) success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1]))
return 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: # try:
if not spl[1] in main.network.keys(): # x = getattr(main.network[spl[1]], spl[2])
failure("Network does not exist: %s" % spl[1]) # x[spl[3]] = spl[4]
return # except Exception as err:
if not int(spl[3]) in main.network[spl[1]].relays.keys(): # failure("Error: %s" % err)
failure("Relay/alias does not exist: %s" % spl[3]) # return
return
try:
x = getattr(main.network[spl[1]], spl[2])
x[spl[3]] = spl[4]
except e:
failure("Something went wrong.")
return
else: else:
incUsage("mod") incUsage("mod")

1
conf/example/alias.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -20,6 +20,7 @@
"users": "users <channel> [<channel> ...]", "users": "users <channel> [<channel> ...]",
"relay": "relay <add|del|list> [<network>] [<num>]", "relay": "relay <add|del|list> [<network>] [<num>]",
"network": "network <add|del|list> [<name> <address> <port> <ssl|plain> <sasl|ns|none>]", "network": "network <add|del|list> [<name> <address> <port> <ssl|plain> <sasl|ns|none>]",
"alias": "alias",
"auto": "auto <network> <relay>", "auto": "auto <network> <relay>",
"cmd": "cmd <relay> <user> <entity> <text ...>", "cmd": "cmd <relay> <user> <entity> <text ...>",
"token": "token <add|del|list> [<key>] [<relay>]", "token": "token <add|del|list> [<key>] [<relay>]",

View File

@ -14,7 +14,7 @@ from modules import monitor
from core.relay import sendRelayNotification from core.relay import sendRelayNotification
from utils.dedup import dedup from utils.dedup import dedup
from utils.getrelay import getRelay from utils.get import getRelay
import main import main
from utils.logging.log import * from utils.logging.log import *
@ -99,7 +99,7 @@ class IRCBot(IRCClient):
self.num = num self.num = num
self.buffer = "" self.buffer = ""
self.name = net + str(num) self.name = net + str(num)
alias = main.network[self.net].aliases[num] alias = main.alias[num]
relay = main.network[self.net].relays[num] relay = main.network[self.net].relays[num]
self.nickname = alias["nick"] self.nickname = alias["nick"]
self.realname = alias["realname"] self.realname = alias["realname"]

View File

@ -17,6 +17,7 @@ filemap = {
"monitor": ["monitor.json", "monitoring database", "json"], "monitor": ["monitor.json", "monitoring database", "json"],
"tokens": ["tokens.json", "authentication tokens", "json"], "tokens": ["tokens.json", "authentication tokens", "json"],
"aliasdata": ["aliasdata.json", "data for alias generation", "json"], "aliasdata": ["aliasdata.json", "data for alias generation", "json"],
"alias": ["alias.json", "provisioned alias data", "json"],
# Binary (pickle) configs # Binary (pickle) configs
"network": ["network.dat", "network list", "pickle"] "network": ["network.dat", "network list", "pickle"]
@ -59,6 +60,12 @@ def loadConf(var):
if filemap[var][2] == "json": if filemap[var][2] == "json":
with open(configPath+filemap[var][0], "r") as f: with open(configPath+filemap[var][0], "r") as f:
globals()[var] = json.load(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": elif filemap[var][2] == "pickle":
try: try:
with open(configPath+filemap[var][0], "rb") as f: with open(configPath+filemap[var][0], "rb") as f:

View File

@ -2,6 +2,9 @@ import main
import random import random
import re import re
def generate_password():
return "".join([chr(random.randint(0, 74) + 48) for i in range(32)])
def generate_alias(): def generate_alias():
nick = random.choice(main.aliasdata["stubs"]) nick = random.choice(main.aliasdata["stubs"])
rand = random.randint(1, 2) rand = random.randint(1, 2)
@ -60,6 +63,6 @@ def generate_alias():
if rand == 3 or rand == 4: if rand == 3 or rand == 4:
realname = realname.capitalize() 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} return {"nick": nick, "altnick": altnick, "ident": ident, "realname": realname, "password": password}

View File

@ -6,7 +6,7 @@ from twisted.internet import reactor
from core.bot import IRCBot, IRCBotFactory from core.bot import IRCBot, IRCBotFactory
import main import main
from utils.logging.log import * from utils.logging.log import *
from utils.getrelay import getRelay from utils.get import getRelay
class Network: class Network:
def __init__(self, net, host, port, security, auth): def __init__(self, net, host, port, security, auth):
@ -29,14 +29,19 @@ class Network:
"net": self.net, "net": self.net,
"id": num "id": num
} }
self.aliases[num] = alias.generate_alias() password = alias.generate_password()
if main.config["ConnectOnCreate"]: if not num in main.alias.keys():
self.start_bot(num) main.alias[num] = alias.generate_alias()
return num, self.aliases[num]["nick"] 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): def delete_relay(self, id):
del self.relays[id] del self.relays[id]
del self.aliases[id] del self.aliases[id]
#del main.alias[id] - Aliases are global per num, so don't delete them!
def start_bot(self, num): def start_bot(self, num):
# a single name is given to relays in the backend # a single name is given to relays in the backend

View File

@ -57,8 +57,10 @@ def provisionRelayForNetwork(num, alias, network):
return return
def provisionRelay(num, network): def provisionRelay(num, network):
aliasObj = main.network[network].aliases[num] aliasObj = main.alias[num]
alias = aliasObj["nick"] alias = aliasObj["nick"]
provisionUserData(num, *aliasObj.values()) provisionUserData(num, *aliasObj.values())
provisionRelayForNetwork(num, alias, network) provisionRelayForNetwork(num, alias, network)
if main.config["ConnectOnCreate"]:
main.network[network].start_bot(num)
return alias return alias

View File

@ -9,4 +9,4 @@ def getRelay(num):
port = int(port) port = int(port)
except ValueError: except ValueError:
return False return False
return [host, port] return (host, port)