Add error checking in places, set up automatic relay provisioning and fix starting bots

This commit is contained in:
2019-08-25 21:29:11 +01:00
parent ff74968ff8
commit 2d70d5af11
19 changed files with 242 additions and 262 deletions

29
commands/auto.py Normal file
View File

@@ -0,0 +1,29 @@
import main
from modules import provision
class AutoCommand:
def __init__(self, *args):
self.auto(*args)
def auto(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 3:
if not spl[1] in main.network.keys():
failure("No such network: %s" % spl[1])
return
if not spl[2].isdigit():
failure("Must be integer, not %s" % spl[2])
return
id, alias = main.network[spl[1]].add_relay(int(spl[2]))
success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[1], alias))
main.saveConf("network")
rtrn = provision.provisionRelay(int(spl[2]), spl[1])
success("Started provisioning network %s on relay %s for alias %s" % (spl[1], spl[2], rtrn))
return
else:
incUsage("auto")
return
else:
incUsage(None)

View File

@@ -8,16 +8,12 @@ class CmdCommand:
def cmd(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length > 4:
if not spl[1] in main.network.keys():
failure("No such network: %s" % spl[1])
if not spl[1].isdigit():
failure("Must be integer, not %s" % spl[1])
return
if not int(spl[2]) in main.network[spl[1]].relays.keys():
failure("No such relay: %s on network: %s" % (spl[2], spl[1]))
return
commands = {spl[4]: [" ".join(spl[5:])]}
commands = {spl[3]: [" ".join(spl[4:])]}
success("Sending commands to relay %s as user %s" % (spl[2], spl[3]))
deliverRelayCommands(main.network[spl[1]].relays[spl[2]], commands, user=spl[3]+"/"+spl[1])
deliverRelayCommands(int(spl[1]), commands, user=spl[2])
return
else:
incUsage("cmd")

View File

@@ -11,16 +11,18 @@ class EnableCommand:
if not spl[1] in main.network.keys():
failure("No such network: %s" % spl[1])
return
if not spl[2].isdigit():
failure("Must be a number, not %s" % spl[2])
return
if not int(spl[2]) in main.network[spl[1]].relays.keys():
failure("No such relay: %s in network %s" % (spl[2], spl[1]))
failure("No such relay on %s: %s" % (spl[2], spl[1]))
return
main.network[spl[1]].relays[int(spl[2])]["enabled"] = True
user = main.network[spl[1]].aliases[int(spl[2])]
user = main.network[spl[1]].aliases[int(spl[2])]["nick"]
network = spl[1]
relay = main.network[spl[1]].relays[int(spl[2])]
commands = {"status": ["Connect"]}
deliverRelayCommands(relay, commands, user=user+"/"+network)
deliverRelayCommands(int(spl[2]), commands, user=user+"/"+network)
main.saveConf("network")
if not spl[1]+spl[2] in main.IRCPool.keys():
main.network[spl[1]].start_bot(int(spl[2]))

View File

@@ -1,22 +0,0 @@
import main
class GetCommand:
def __init__(self, *args):
self.get(*args)
def get(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 3:
if not spl[1] in main.pool.keys():
failure("Name does not exist: %s" % spl[1])
return
if not spl[1] in main.IRCPool.keys():
failure("Name has no instance: %s" % spl[1])
return
info(str(main.IRCPool[spl[1]].get(spl[2])))
return
else:
incUsage("get")
return
else:
incUsage(None)

View File

@@ -11,7 +11,7 @@ class JoinCommand:
failure("Network does not exist: %s" % spl[1])
return
if not int(spl[2]) in main.network[spl[1]].relays.keys():
failure("Relay % does not exist on network %", (spl[2], spl[1]))
failure("Relay %s does not exist on network %s" % (spl[2], spl[1]))
return
if not spl[1]+spl[2] in main.IRCPool.keys():
failure("Name has no instance: %s" % spl[1])

View File

@@ -23,7 +23,7 @@ class NetworkCommand:
failure("Auth must be sasl, ns or none, not %s" % spl[5])
return
else:
main.network[spl[2]] = Network(spl[2], spl[3], spl[4], spl[5].lower(), spl[6].lower())
main.network[spl[2]] = Network(spl[2], spl[3], int(spl[4]), spl[5].lower(), spl[6].lower())
success("Successfully created network: %s" % spl[2])
main.saveConf("network")
return

View File

@@ -1,60 +0,0 @@
import main
from modules import provision
class ProvisionCommand:
def __init__(self, *args):
self.provision(*args)
def provision(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 4 or length == 3:
if not spl[1] in main.relay.keys():
failure("No such relay: %s" % spl[1])
return
if not spl[2] in main.alias.keys():
failure("No such alias: %s" % spl[2])
return
if length == 4: # provision for relay, alias and network
if not spl[3] in main.network.keys():
failure("No such network: %s" % spl[3])
return
#if "users" in main.relay[spl[1]]:
# if not spl[2] in main.relay[spl[1]]["users"]:
# failure("Relay %s not provisioned for alias %s" % (spl[1], spl[2]))
# return
#else:
# failure("Relay %s not provisioned for alias %s" % (spl[1], spl[2]))
# return
rtrn = provision.provisionRelayForNetwork(spl[1], spl[2], spl[3])
#if rtrn == "PROVISIONED":
# failure("Relay %s already provisioned for alias %s on network %s" % (spl[1], spl[2], spl[3]))
# return
#elif rtrn == "DUPLICATE":
# failure("Instance with relay %s and alias %s already exists for network %s" % (spl[1], spl[2], spl[3]))
# return
if rtrn:
success("Started provisioning network %s on relay %s for alias %s" % (spl[3], spl[1], spl[2]))
info("Instance name is %s" % rtrn)
return
else:
failure("Failure while provisioning relay %s" % spl[1])
return
if length == 3: # provision for relay and alias only
rtrn = provision.provisionRelayForAlias(spl[1], spl[2])
#if rtrn == "PROVISIONED":
# failure("Relay %s already provisioned for alias %s" % (spl[1], spl[2]))
# return
if rtrn:
success("Started provisioning relay %s for alias %s" % (spl[1], spl[2]))
return
else:
failure("Failure while provisioning relay %s" % spl[1])
return
else:
incUsage("provision")
return
else:
incUsage(None)

View File

@@ -7,29 +7,47 @@ class RelayCommand:
def relay(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 7:
if length == 3:
if spl[1] == "add":
if spl[2] not in main.network.keys():
failure("No such network: %s" % spl[2])
return
if not spl[4].isdigit():
failure("Port must be an integer, not %s" % spl[4])
return
else:
id, alias = main.network[spl[2]].add_relay(spl[3], spl[4], spl[5], spl[6])
if spl[2] in main.network.keys():
id, alias = main.network[spl[2]].add_relay()
success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[2], alias))
main.saveConf("network")
return
else:
failure("No such network: %s" % spl[2])
return
elif spl[1] == "list":
if spl[2] not in main.network.keys():
failure("No such network: %s" % spl[2])
return
info(dump(main.network[spl[2]].relays))
return
else:
incUsage("relay")
return
elif length == 4:
if spl[1] == "del":
if spl[2] not in main.network.keys():
if spl[1] == "add":
if spl[2] in main.network.keys():
if not spl[3].isdigit():
failure("Must be a number, not %s" % spl[3])
return
id, alias = main.network[spl[2]].add_relay(int(spl[3]))
success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[2], alias))
main.saveConf("network")
return
else:
failure("No such network: %s" % spl[2])
return
if int(spl[3]) not in main.network[spl[2]].relays.keys():
elif spl[1] == "del":
if not spl[2] in main.network.keys():
failure("No such network: %s" % spl[2])
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[2]].relays.keys():
failure("No such relay: %s on network %s" % (spl[3], spl[2]))
return
main.network[spl[2]].delete_relay(int(spl[3]))
@@ -39,16 +57,6 @@ class RelayCommand:
else:
incUsage("relay")
return
elif length == 3:
if spl[1] == "list":
if spl[2] not in main.network.keys():
failure("No such network: %s" % spl[2])
return
info(dump(main.network[spl[2]].relays))
return
else:
incUsage("relay")
return
else:
incUsage("relay")
return

40
commands/swho.py Normal file
View File

@@ -0,0 +1,40 @@
import main
class SwhoCommand:
def __init__(self, *args):
self.swho(*args)
def swho(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 2:
if not spl[1] in main.network.keys():
failure("Network does not exist: %s" % spl[1])
return
for i in main.IRCPool.keys():
if spl[1] in i:
for x in main.IRCPool[i].channels:
main.IRCPool[i].who(x)
success("Sent WHO to all channels on all networks on %s" % spl[1])
return
elif length == 3:
if not spl[1] in main.network.keys():
failure("Network does not exist: %s" % spl[1])
return
matches = []
for i in main.IRCPool.keys():
if spl[1] in i:
for x in main.IRCPool[i].channels:
if x == spl[2]:
main.IRCPool[i].who(x)
matches.append(i)
if matches == []:
failure("No matches found for channel %s" % spl[2])
return
success("Sent WHO to %s on: %s" % (spl[2], ", ".join(matches)))
return
else:
incUsage("swho")
return
else:
incUsage(None)