From bef1875186052e3333f71ea7f86a4e3316760b83 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 25 Dec 2017 22:09:38 +0000 Subject: [PATCH] Implement statistics and shorten some functions --- help.json | 3 +- threshold | 148 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 111 insertions(+), 40 deletions(-) diff --git a/help.json b/help.json index 7322907..99ff659 100644 --- a/help.json +++ b/help.json @@ -8,9 +8,10 @@ "key": "key [] [] [] []", "who": "who ", "join": "join []", - "enable": "enable ", "disable": "disable ", "list": "list", + "stats": "stats", "rehash": "rehash", "rekey": "rekey", "dist": "dist" diff --git a/threshold b/threshold index cd5d205..574c5ec 100755 --- a/threshold +++ b/threshold @@ -91,47 +91,35 @@ class IRCBot(IRCClient): self.versionEnv = None self.sourceURL = instance["source"] - def privmsg(self, user, channel, msg): - toSend = helper.isKeyword(msg) - if self.name == config["Master"][0] and channel == config["Master"][1]: - pass + def parsen(self, user): + step = user.split("!") + nick = step[0] + if len(step) == 2: + step2 = step[1].split("@") + ident, host = step2 else: - if config["HighlightNotifications"]: - msgLower = msg.lower() - nickLower = self.nickname.lower() - if nickLower in msgLower: - msgLower = msgLower.replace(nickLower, "{"+nickLower+"}") - helper.sendMaster("NICK PRV %s (T:%s): (%s/%s) %s" % (self.name, msgLower.count(nickLower), user, channel, msgLower)) - if toSend: - helper.sendMaster("MATCH PRV %s (U:%s T:%s): (%s/%s) %s" % (self.name, toSend[1], toSend[2], user, channel, toSend[0])) + ident = nick + host = nick + + return [nick, ident, host] + + def privmsg(self, user, channel, msg): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + helper.actKeyword(user, channel, msg, self.nickname, "PRV", self.name) def noticed(self, user, channel, msg): - toSend = helper.isKeyword(msg) - if self.name == config["Master"][0] and channel == config["Master"][1]: - pass - else: - if config["HighlightNotifications"]: - msgLower = msg.lower() - nickLower = self.nickname.lower() - if nickLower in msgLower: - msgLower = msgLower.replace(nickLower, "{"+nickLower+"}") - helper.sendMaster("NICK NOT %s (T:%s): (%s/%s) %s" % (self.name, msgLower.count(nickLower), user, channel, msgLower)) - if toSend: - helper.sendMaster("MATCH NOT %s (U:%s T:%s): (%s/%s) %s" % (self.name, toSend[1], toSend[2], user, channel, toSend[0])) + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + helper.actKeyword(user, channel, msg, self.nickname, "NOT", self.name) def action(self, user, channel, msg): - toSend = helper.isKeyword(msg) - if self.name == config["Master"][0] and channel == config["Master"][1]: - pass - else: - if config["HighlightNotifications"]: - msgLower = msg.lower() - nickLower = self.nickname.lower() - if nickLower in msgLower: - msgLower = msgLower.replace(nickLower, "{"+nickLower+"}") - helper.sendMaster("NICK ACT %s (T:%s): (%s/%s) %s" % (self.name, msgLower.count(nickLower), user, channel, msgLower)) - if toSend: - helper.sendMaster("MATCH ACT %s (U:%s T:%s): (%s/%s) %s" % (self.name, toSend[1], toSend[2], user, channel, toSend[0])) + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + helper.actKeyword(user, channel, msg, self.nickname, "ACT", self.name) def get(self, var): try: @@ -214,6 +202,33 @@ class IRCBot(IRCClient): self.channels.remove(channel) helper.sendMaster("KICK %s: (%s/%s) %s" % (self.name, kicker, channel, message)) + def userJoined(self, user, channel): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + def userLeft(self, user, channel): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + def userQuit(self, user, quitMessage): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + helper.actKeyword(user, None, quitMessage, self.nickname, "QUT", self.name) + + def userKicked(self, kickee, channel, kicker, message): + helper.actKeyword(kicker, channel, message, self.nickname, "KCK", self.name) + + def topicUpdated(self, user, channel, newTopic): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + + helper.actKeyword(user, channel, newTopic, self.nickname, "TOP", self.name) + + def modeChanged(self, user, channel, toset, modes, args): + nick, ident, host = self.parsen(user) + helper.setWhoSingle(self.name, nick, ident, host) + def connectionLost(self, reason): self.connected = False self.channels = [] @@ -420,6 +435,20 @@ class Helper(object): else: return [message, uniqueNum, totalNum] + def actKeyword(self, user, channel, message, nickname, actType, name): + toSend = self.isKeyword(message) + if name == config["Master"][0] and channel == config["Master"][1]: + pass + else: + if config["HighlightNotifications"]: + msgLower = message.lower() + nickLower = nickname.lower() + if nickLower in msgLower: + msgLower = msgLower.replace(nickLower, "{"+nickLower+"}") + self.sendMaster("NICK %s %s (T:%s): (%s/%s) %s" % (actType, name, msgLower.count(nickLower), user, channel, msgLower)) + if toSend: + helper.sendMaster("MATCH %s %s (U:%s T:%s): (%s/%s) %s" % (actType, name, toSend[1], toSend[2], user, channel, toSend[0])) + def addBot(self, name): global IRCPool instance = pool[name] @@ -471,7 +500,7 @@ class Helper(object): return True def parseCommand(self, addr, authed, data): - global pool, MonitorPool + global pool, MonitorPool, wholist spl = data.split() if addr in connections.keys(): obj = connections[addr] @@ -541,6 +570,47 @@ class Helper(object): info("\n".join(poolMap)) return + elif cmd == "stats": + if length == 1: + stats = [] + numChannels = 0 + numWhoEntries = 0 + for i in IRCPool.keys(): + numChannels += len(IRCPool[i].channels) + for i in wholist.keys(): + numWhoEntries += len(wholist[i].keys()) + stats.append("Servers: %s" % len(IRCPool.keys())) + stats.append("Channels: %s" % numChannels) + stats.append("User records: %s" % numWhoEntries) + info("\n".join(stats)) + return + + elif length == 2: + stats = [] + numChannels = 0 + numWhoEntries = 0 + + failures = 0 + if spl[1] in IRCPool.keys(): + numChannels += len(IRCPool[spl[1]].channels) + else: + failure("Name does not exist: %s" % spl[1]) + failures += 1 + if spl[1] in wholist.keys(): + numWhoEntries += len(wholist[spl[1]].keys()) + else: + failure("Who entry does not exist: %s" % spl[1]) + failures += 1 + if failures == 2: + failure("No information found, aborting") + return + stats.append("Channels: %s" % numChannels) + stats.append("User records: %s" % numWhoEntries) + info("\n".join(stats)) + return + else: + incUsage("stats") + elif cmd == "enable": if length == 2: if not spl[1] in pool.keys(): @@ -630,7 +700,6 @@ class Helper(object): return elif cmd == "who": - global wholist if length == 2: result = self.getWho(spl[1]) rtrn = "" @@ -638,7 +707,8 @@ class Helper(object): rtrn += "Matches from: %s" % i rtrn += "\n" for x in result[i]: - rtrn += (", ".join(x)) + x = [y for y in x if not y == None] + rtrn += str((", ".join(x))) rtrn += "\n" info(rtrn) return