From fc8a115e179223de69abd6e068f3823d11ccd9c8 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 24 Dec 2017 21:04:48 +0000 Subject: [PATCH] Implement alternative keyword notifications and move the per-instance wholist into the global namespace --- example.json | 1 + help.json | 2 +- threshold | 70 ++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/example.json b/example.json index fcc89a3..d22a183 100644 --- a/example.json +++ b/example.json @@ -7,6 +7,7 @@ "UsePassword": true, "ConnectOnCreate": false, "HighlightNotifications": true, + "Debugger": false, "DistEnabled": true, "SendDistOutput": false, "Password": "s", diff --git a/help.json b/help.json index 1e77914..d87f86f 100644 --- a/help.json +++ b/help.json @@ -5,7 +5,7 @@ "del": "del ", "mod": "mod [] []", "get": "get ", - "key": "key [] [] []", + "key": "key [] [] [] []", "join": "join []", "enable": "enable ", diff --git a/threshold b/threshold index ad9faa8..7371efb 100755 --- a/threshold +++ b/threshold @@ -6,14 +6,24 @@ from twisted.internet.protocol import Protocol, Factory from twisted.internet.endpoints import SSL4ClientEndpoint, TCP4ClientEndpoint, connectProtocol from twisted.words.protocols.irc import IRCClient +#from twisted.python import log +#from sys import stdout +#log.startLogging(stdout) + from json import load, dump, loads from sys import exit from subprocess import run, PIPE +numbers = "0123456789" + listener = None connections = {} IRCPool = {} +MonitorPool = [] + +wholist = {} + def log(data): print("[LOG]", data) @@ -60,7 +70,6 @@ class IRCBot(IRCClient): self._who = {} self._getWho = {} - self.wholist = {} self.authtype = instance["authtype"] if self.authtype == "ns": @@ -180,7 +189,8 @@ class IRCBot(IRCClient): del self._who[channel] def got_who(self, whoinfo): - self.wholist[whoinfo[0]] = whoinfo[1] + global wholist + helper.setWho(self.name, whoinfo[1]) def signedOn(self): self.connected = True @@ -235,11 +245,8 @@ class Base(Protocol): if "\n" in data: splitData = [x for x in data.split("\n") if x] if "\n" in data: - #timePlus = 0.0 for i in splitData: helper.parseCommand(self.addr, self.authed, i) - #reactor.callLater(timePlus, lambda: helper.parseCommand(self.addr, self.authed, i)) - #timePlus += 0.01 return helper.parseCommand(self.addr, self.authed, data) @@ -248,7 +255,7 @@ class Base(Protocol): self.send("Hello.") def connectionLost(self, reason): - global connections + global connections, MonitorPool self.authed = False log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage())) if not listener == None: @@ -258,6 +265,8 @@ class Base(Protocol): warn("Tried to remove a non-existant connection.") else: warn("Tried to remove a connection from a listener that wasn't running.") + if self.addr in MonitorPool: + MonitorPool.remove(self.addr) class BaseFactory(Factory): def buildProtocol(self, addr): @@ -289,6 +298,23 @@ class Helper(object): else: error("Mandatory values missing from config") + def setWho(self, network, newObjects): + global wholist + network = "".join([x for x in network if not x in numbers]) + if not network in wholist.keys(): + wholist[network] = {} + for i in newObjects.keys(): + wholist[network][i] = newObjects[i] + return + + def setWhoSingle(self, network, nick, ident, host): + global wholist + network = "".join([x for x in network if not x in numbers]) + + if network in wholist.keys(): + if nick in wholist[network].keys(): + wholist[network][nick][1] = ident + wholist[network][nick][2] = host def saveConfig(self): global config @@ -344,6 +370,8 @@ class Helper(object): def sendMaster(self, data): if config["Master"][0] in IRCPool.keys(): IRCPool[config["Master"][0]].msg(config["Master"][1], data) + for i in MonitorPool: + connections[i].send(data) def isKeyword(self, msg): message = msg.lower() @@ -428,7 +456,7 @@ class Helper(object): return True def parseCommand(self, addr, authed, data): - global pool + global pool, MonitorPool spl = data.split() if addr in connections.keys(): obj = connections[addr] @@ -672,8 +700,29 @@ class Helper(object): helper.saveKeywordConfig() success("Successfully removed exception list of %s" % spl[2]) return + elif spl[1] == "monitor": + if spl[2] == "on": + if not obj.addr in MonitorPool: + MonitorPool.append(obj.addr) + success("Keyword monitoring enabled") + return + else: + failure("Keyword monitoring is already enabled") + return + elif spl[2] == "off": + if obj.addr in MonitorPool: + MonitorPool.remove(obj.addr) + success("Keyword monitoring disabled") + return + else: + failure("Keyword monitoring is already disabled") + return + else: + incUsage("key") + return else: incUsage("key") + return elif length == 2: if spl[1] == "show": info(",".join(keyconf["Keywords"])) @@ -690,6 +739,13 @@ class Helper(object): elif spl[1] == "master": info(" - ".join(config["Master"])) return + elif spl[1] == "monitor": + if obj.addr in MonitorPool: + info("Keyword monitoring is enabled on this connection") + return + else: + info("Keyword monitoring is disabled on this connection") + return else: incUsage("key") return