add command handler in pubsub module

This commit is contained in:
dave 2017-09-18 20:12:08 -07:00
parent c1d4b025f7
commit deab2ff924

View File

@ -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"]