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():
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)

View File

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

View File

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

View File

@ -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"]}

View File

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

View File

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

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

@ -0,0 +1 @@
{}

View File

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

View File

@ -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"]

View File

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

View File

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

View File

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

View File

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

View File

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