monolith/commands/alias.py

57 lines
2.1 KiB
Python

import main
from yaml import dump
from modules import alias
class AliasCommand:
def __init__(self, *args):
self.alias(*args)
def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
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)