From 740fc0034ceb541e20c893ae2837677f0d6f6f3d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 26 Dec 2017 13:57:29 +0000 Subject: [PATCH] Make the add command much more flexible and properly implement the defaults system --- example.json | 20 +++++++- help.json | 2 +- threshold | 127 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 107 insertions(+), 42 deletions(-) diff --git a/example.json b/example.json index 5faaaaf..8244110 100644 --- a/example.json +++ b/example.json @@ -12,9 +12,25 @@ "SendDistOutput": false, "Password": "s", "Default": { - "password": null, + "host": null, + "port": null, + "protocol": null, + "bind": null, + "timeout": 30, + "nickname": 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] } diff --git a/help.json b/help.json index 99ff659..4473d75 100644 --- a/help.json +++ b/help.json @@ -1,7 +1,7 @@ { "pass": "pass ", "logout": "logout", - "add": "add
", + "add": "add [
] [] [] []", "del": "del ", "mod": "mod [] []", "get": "get ", diff --git a/threshold b/threshold index f36ac69..728b038 100755 --- a/threshold +++ b/threshold @@ -872,53 +872,102 @@ class Helper(object): return elif cmd == "add": - if length == 6: - - if spl[1] in pool.keys(): - failure("Name already exists: %s" % spl[1]) - return + if length > 6: + failure("Too many arguments") + 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 not protocol in ["ssl", "plain"]: - incUsage("connect") - return + if length < 5: + if config["Default"]["protocol"] == None: + failure("Choose a protocol, or configure one in defaults") + toFail = True + else: + protocol = config["Default"]["protocol"] - try: - int(spl[3]) - except: - failure("Port must be an integer, not %s" % spl[3]) - return + if length < 4: + if config["Default"]["port"] == None: + failure("Choose a port, or configure one in defaults") + toFail = True + else: + port = config["Default"]["port"] - pool[spl[1]] = { "host": spl[2], - "port": spl[3], - "protocol": protocol, - "bind": None, - "timeout": 30, - "nickname": spl[5], - "username": config["Default"]["username"], - "realname": None, - "userinfo": None, - "finger": None, - "version": None, - "source": None, - "autojoin": [], - "authtype": config["Default"]["authtype"], - "password": config["Default"]["password"], - "authentity": "NickServ", - "key": config["ListenerKey"], - "certificate": config["ListenerCertificate"], - "enabled": config["ConnectOnCreate"], - } - if config["ConnectOnCreate"] == True: - self.addBot(spl[1]) - success("Successfully created bot") - self.savePool() + 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 - else: + + 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"]: + failure("Protocol must be ssl or plain, not %s" % protocol) + return + + try: + int(port) + except: + failure("Port must be an integer, not %s" % port) + return + + pool[name] = { "host": host, + "port": port, + "protocol": protocol, + "bind": config["Default"]["bind"], + "timeout": config["Default"]["timeout"], + "nickname": nickname, + "username": config["Default"]["username"], + "realname": config["Default"]["realname"], + "userinfo": config["Default"]["userinfo"], + "finger": config["Default"]["finger"], + "version": config["Default"]["version"], + "source": config["Default"]["source"], + "autojoin": config["Default"]["autojoin"], + "authtype": config["Default"]["authtype"], + "password": config["Default"]["password"], + "authentity": config["Default"]["authentity"], + "key": config["ListenerKey"], + "certificate": config["ListenerCertificate"], + "enabled": config["ConnectOnCreate"], + } + if config["ConnectOnCreate"] == True: + self.addBot(name) + success("Successfully created bot") + self.savePool() + return + elif cmd == "del": if length == 2: