From deab2ff9245af2aa5a26d1adab63826cd700f87a Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 18 Sep 2017 20:12:08 -0700 Subject: [PATCH] add command handler in pubsub module --- pyircbot/modules/PubSubClient.py | 38 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/pyircbot/modules/PubSubClient.py b/pyircbot/modules/PubSubClient.py index 3408645..744d9fb 100644 --- a/pyircbot/modules/PubSubClient.py +++ b/pyircbot/modules/PubSubClient.py @@ -10,6 +10,11 @@ from threading import Thread from json import dumps, loads from time import sleep from zmq.error import Again +from traceback import print_exc +import re + + +COMMAND_RE = re.compile(r'\.(([a-zA-Z0-9]{1,16})(\s|$))(\s.+)?') class PubSubClient(ModuleBase): @@ -32,14 +37,17 @@ class PubSubClient(ModuleBase): except Again: sleep(0.01) continue - print(channel, "--", message) - tag, subcommand, message = message.split(" ", 2) - if tag != "default": - continue + try: + print(channel, "--", message) + tag, subcommand, message = message.split(" ", 2) + if tag != "default": + continue - if subcommand == "privmsg": - dest, message = loads(message) - self.bot.act_PRIVMSG(dest, message) + if subcommand == "privmsg": + dest, message = loads(message) + self.bot.act_PRIVMSG(dest, message) + except: + print_exc() def publish(self, subchannel, message): self.bus.pub(self.config.get("publish").format(subchannel), "{} {}".format("default", message)) @@ -59,6 +67,17 @@ class PubSubClient(ModuleBase): # msg.command msg.args msg.prefix msg.trailing self.publish("part", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) + @hook("PRIVMSG") + def bus_command(self, msg): + # msg.command msg.args msg.prefix msg.trailing + # self.publish("privmsg", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) + match = COMMAND_RE.match(msg.trailing) + if match: + 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}])) + def onenable(self): self.bus = MsgbusSubClient(self.host, int(self.port)) for channel in self.config.get("subscriptions"): @@ -95,6 +114,11 @@ User joins a channel: pyircbot_join default ["dave-irccloud", "#clonebot", {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}] +User uses the command `.seen testbot`: + + pyircbot_command_seen default [["#clonebot"], "dave-irccloud", "testbot", + {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}] + # Client sending a message that the bot will relay pyircbot_send default privmsg ["#clonebot", "asdf1234"]