From b8e9efa41ead46fc0be8d72d5d6e749b5e7c8700 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 25 Nov 2017 00:38:06 +0000 Subject: [PATCH] Implement adding and removing keywords, and setting and viewing the keyword master (reporting target) --- help.json | 1 + threshold | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/help.json b/help.json index 98cd1b4..612f2fe 100644 --- a/help.json +++ b/help.json @@ -5,6 +5,7 @@ "del": "del ", "mod": "mod [] []", "get": "get ", + "key": "key {[] []} []", "join": "join []", "enable": "enable ", diff --git a/threshold b/threshold index 8253a77..66af64d 100755 --- a/threshold +++ b/threshold @@ -242,6 +242,12 @@ class Helper(object): else: error("Mandatory values missing from config") + def saveConfig(self): + global config + with open("config.json", "w") as f: + dump(config, f, indent=4) + return + def getPool(self): with open("pool.json", "r") as f: data = f.read() @@ -308,9 +314,27 @@ class Helper(object): d = connectProtocol(point, bot) return + def addKeyword(self, keyword): + if keyword in config["Keywords"]: + return "EXISTS" + else: + for i in config["Keywords"]: + if i in keyword or keyword in i: + return "ISIN" + config["Keywords"].append(keyword) + helper.saveConfig() + return True + + def delKeyword(self, keyword): + if not keyword in config["Keywords"]: + return "NOKEY" + config["Keywords"].remove(keyword) + helper.saveConfig() + return True + def parseCommand(self, addr, authed, data): global pool - data = data.strip() + data = data.strip("\n") spl = data.split() obj = connections[addr] @@ -411,6 +435,7 @@ class Helper(object): return else: incUsage("join") + return elif cmd == "part": if length == 3: @@ -425,6 +450,7 @@ class Helper(object): return else: incUsage("part") + return elif cmd == "get": if length == 3: @@ -438,6 +464,66 @@ class Helper(object): return else: incUsage("get") + return + + elif cmd == "key": + if data.startswith("key add"): + if not data == "key add " and not data == "key add": + keywordsToAdd = data[8:] + keywords = keywordsToAdd.split(",") + for keyword in keywords: + rtrn = self.addKeyword(keyword) + if rtrn == "EXISTS": + failure("Keyword already exists: %s" % keyword) + elif rtrn == "ISIN": + failure("Keyword already matched: %s" % keyword) + elif rtrn == True: + success("Keyword added: %s" % keyword) + return + else: + incUsage("key") + return + elif data.startswith("key del"): + if not data == "key del " and not data == "key del": + keywordsToDel = data[8:] + keywords = keywordsToDel.split(",") + for keyword in keywords: + rtrn = self.delKeyword(keyword) + if rtrn == "NOKEY": + failure("Keyword does not exist: %s" % keyword) + elif rtrn == True: + success("Keyword deleted: %s" % keyword) + return + else: + incUsage("key") + return + + elif spl[1] == "show": + info(", ".join(config["Keywords"])) + return + + elif spl[1] == "master": + if length == 4: + if not spl[2] in pool.keys(): + failure("Name does not exist: %s" % spl[2]) + return + if spl[2] in IRCPool.keys(): + if not spl[3] in IRCPool[spl[2]].channels: + info("Bot not on channel: %s" % spl[3]) + config["Master"] = [spl[2], spl[3]] + helper.saveConfig() + success("Master set to %s on %s" % (spl[3], spl[2])) + return + elif length == 2: + info(" - ".join(config["Master"])) + return + else: + incUsage("key") + return + + else: + incUsage("key") + return elif cmd == "add": if length == 6: @@ -571,6 +657,7 @@ class Helper(object): else: incUsage("mod") + return else: incUsage(None)