Make the add command much more flexible and properly implement the defaults system

This commit is contained in:
Mark Veidemanis 2017-12-26 13:57:29 +00:00
parent d144b86452
commit 740fc0034c
3 changed files with 110 additions and 45 deletions

View File

@ -12,9 +12,25 @@
"SendDistOutput": false, "SendDistOutput": false,
"Password": "s", "Password": "s",
"Default": { "Default": {
"password": null, "host": null,
"port": null,
"protocol": null,
"bind": null,
"timeout": 30,
"nickname": null,
"username": null, "username": null,
"authtype": null "realname": null,
"userinfo": null,
"finger": null,
"version": null,
"source": null,
"autojoin": [],
"authtype": null,
"password": null,
"authentity": "NickServ",
"key": "key.pem",
"certificate": "cert.pem",
"enabled": true
}, },
"Master": [null, null] "Master": [null, null]
} }

View File

@ -1,7 +1,7 @@
{ {
"pass": "pass <password>", "pass": "pass <password>",
"logout": "logout", "logout": "logout",
"add": "add <name> <address> <port> <ssl|plain> <nickname>", "add": "add <name> [<address>] [<port>] [<ssl|plain>] [<nickname>]",
"del": "del <name>", "del": "del <name>",
"mod": "mod <name> [<key>] [<value>]", "mod": "mod <name> [<key>] [<value>]",
"get": "get <name> <variable>", "get": "get <name> <variable>",

View File

@ -872,52 +872,101 @@ class Helper(object):
return return
elif cmd == "add": elif cmd == "add":
if length == 6: if length > 6:
failure("Too many arguments")
if spl[1] in pool.keys():
failure("Name already exists: %s" % spl[1])
return return
protocol = spl[4].lower() if length > 1:
name = spl[1]
else:
incUsage("add")
return
if length > 2:
host = spl[2]
if length > 3:
port = spl[3]
if length > 4:
protocol = spl[4]
if length > 5:
nickname = spl[5]
toFail = False
if length < 6:
if config["Default"]["nickname"] == None:
failure("Choose a nickname, or configure one in defaults")
toFail = True
else:
nickname = config["Default"]["nickname"]
if length < 5:
if config["Default"]["protocol"] == None:
failure("Choose a protocol, or configure one in defaults")
toFail = True
else:
protocol = config["Default"]["protocol"]
if length < 4:
if config["Default"]["port"] == None:
failure("Choose a port, or configure one in defaults")
toFail = True
else:
port = config["Default"]["port"]
if length < 3:
if config["Default"]["host"] == None:
failure("Choose a host, or configure one in defaults")
toFail = True
else:
host = config["Default"]["host"]
if toFail:
failure("Stopping due to previous error(s)")
return
if length < 2:
incUsage("add")
return
if name in pool.keys():
failure("Name already exists: %s" % name)
return
protocol = protocol.lower()
if not protocol in ["ssl", "plain"]: if not protocol in ["ssl", "plain"]:
incUsage("connect") failure("Protocol must be ssl or plain, not %s" % protocol)
return return
try: try:
int(spl[3]) int(port)
except: except:
failure("Port must be an integer, not %s" % spl[3]) failure("Port must be an integer, not %s" % port)
return return
pool[spl[1]] = { "host": spl[2], pool[name] = { "host": host,
"port": spl[3], "port": port,
"protocol": protocol, "protocol": protocol,
"bind": None, "bind": config["Default"]["bind"],
"timeout": 30, "timeout": config["Default"]["timeout"],
"nickname": spl[5], "nickname": nickname,
"username": config["Default"]["username"], "username": config["Default"]["username"],
"realname": None, "realname": config["Default"]["realname"],
"userinfo": None, "userinfo": config["Default"]["userinfo"],
"finger": None, "finger": config["Default"]["finger"],
"version": None, "version": config["Default"]["version"],
"source": None, "source": config["Default"]["source"],
"autojoin": [], "autojoin": config["Default"]["autojoin"],
"authtype": config["Default"]["authtype"], "authtype": config["Default"]["authtype"],
"password": config["Default"]["password"], "password": config["Default"]["password"],
"authentity": "NickServ", "authentity": config["Default"]["authentity"],
"key": config["ListenerKey"], "key": config["ListenerKey"],
"certificate": config["ListenerCertificate"], "certificate": config["ListenerCertificate"],
"enabled": config["ConnectOnCreate"], "enabled": config["ConnectOnCreate"],
} }
if config["ConnectOnCreate"] == True: if config["ConnectOnCreate"] == True:
self.addBot(spl[1]) self.addBot(name)
success("Successfully created bot") success("Successfully created bot")
self.savePool() self.savePool()
return return
else:
incUsage("add")
return
elif cmd == "del": elif cmd == "del":
if length == 2: if length == 2: