From 2f4ab6cd604d44d15f9d6cbefcc2f8abbb3d6e17 Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 4 Dec 2017 23:58:24 -0800 Subject: [PATCH] Solidify & expand pubsub mode communications --- bin/pubsubbot | 15 +++++++++------ pyircbot/modules/PubSubClient.py | 32 ++++++++++++-------------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/bin/pubsubbot b/bin/pubsubbot index 63944b3..0c00a64 100755 --- a/bin/pubsubbot +++ b/bin/pubsubbot @@ -30,7 +30,13 @@ class PyIRCBotSub(PrimitiveBot): def run(self): # Connect to msgbus and loop through messages with closing(MsgbusSubClient(self.host, self.port)) as self.client: - self.client.sub("pyircbot_privmsg")#TODO More of these + self.client.prepare_pub() + self.client.sub("pyircbot_privmsg") + self.client.sub("pyircbot_join") + self.client.sub("pyircbot_kick") + self.client.sub("pyircbot_part") + self.client.sub("pyircbot_mode") + self.client.sub("pyircbot_quit") while True: try: channel, body = self.client.recv() @@ -43,12 +49,11 @@ class PyIRCBotSub(PrimitiveBot): if name != self.name: return - # pyircbot_privmsg default - # [["#jesusandhacking"], "xMopxShell", "test", {"prefix": ["xMopxShell", "~xMopxShel", "192.95.23.134"]}] + command = channel.split("_", 1)[1].upper() args, sender, trailing, extras = loads(rest) nick, username, hostname = extras["prefix"] - msg = IRCEvent("PRIVMSG", + msg = IRCEvent(command, args, UserPrefix(nick, username, @@ -61,8 +66,6 @@ class PyIRCBotSub(PrimitiveBot): if validation: hook.method(msg, validation) - # client.pub("pyircbot_send", "default privmsg {}".format(dumps([channel, "{}: pong".format(sender)]))) - " Filesystem Methods " def getConfigPath(self, moduleName): """Return the absolute path for a module's config file diff --git a/pyircbot/modules/PubSubClient.py b/pyircbot/modules/PubSubClient.py index ae409ed..3bf62e8 100644 --- a/pyircbot/modules/PubSubClient.py +++ b/pyircbot/modules/PubSubClient.py @@ -31,7 +31,7 @@ class PubSubClient(ModuleBase): Listen to the bus for send messages and act on recv """ sleep(3) - while True:#TODO clean exit onenable/ondisable etc + while True: if not self.bus: sleep(0.01) continue @@ -61,26 +61,15 @@ class PubSubClient(ModuleBase): """ self.bus.pub(self.config.get("publish").format(subchannel), "{} {}".format("default", message)) - @hook("PRIVMSG") - def bus_privmsg(self, msg, cmd): + @hook("PRIVMSG", "JOIN", "PART", "KICK", "MODE", "QUIT", "NICK", "PING") + def busdriver(self, msg, cmd): """ Relay a privmsg to the event bus """ - self.publish("privmsg", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) - - @hook("JOIN") - def bus_join(self, msg, cmd): - """ - Relay a join message to the event bus - """ - self.publish("join", dumps([msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) - - @hook("PART") - def bus_part(self, msg, cmd): - """ - Relay a part message to the event bus - """ - self.publish("part", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) + self.publish(msg.command.lower(), + dumps([msg.args, + msg.prefix[0], + msg.trailing, {"prefix": msg.prefix}])) @hook("PRIVMSG") def bus_command(self, msg, cmd): @@ -93,7 +82,10 @@ class PubSubClient(ModuleBase): cmd_name = match.groups()[1] cmd_args = msg.trailing[len(cmd_name) + 1:].strip() self.publish("command_{}".format(cmd_name), - dumps([msg.args, msg.prefix[0], cmd_args, {"prefix": msg.prefix}])) + dumps([msg.args, + msg.prefix[0], + cmd_args, + {"prefix": msg.prefix}])) def onenable(self): """ @@ -110,4 +102,4 @@ class PubSubClient(ModuleBase): """ self.log.warning("clean it up") self.publish("sys", "offline") - self.bus.close() + self.bus.close() # This will crash the listener thread