Solidify & expand pubsub mode communications

This commit is contained in:
dave 2017-12-04 23:58:24 -08:00
parent a6fab20270
commit 2f4ab6cd60
2 changed files with 21 additions and 26 deletions

View File

@ -30,7 +30,13 @@ class PyIRCBotSub(PrimitiveBot):
def run(self): def run(self):
# Connect to msgbus and loop through messages # Connect to msgbus and loop through messages
with closing(MsgbusSubClient(self.host, self.port)) as self.client: 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: while True:
try: try:
channel, body = self.client.recv() channel, body = self.client.recv()
@ -43,12 +49,11 @@ class PyIRCBotSub(PrimitiveBot):
if name != self.name: if name != self.name:
return return
# pyircbot_privmsg default command = channel.split("_", 1)[1].upper()
# [["#jesusandhacking"], "xMopxShell", "test", {"prefix": ["xMopxShell", "~xMopxShel", "192.95.23.134"]}]
args, sender, trailing, extras = loads(rest) args, sender, trailing, extras = loads(rest)
nick, username, hostname = extras["prefix"] nick, username, hostname = extras["prefix"]
msg = IRCEvent("PRIVMSG", msg = IRCEvent(command,
args, args,
UserPrefix(nick, UserPrefix(nick,
username, username,
@ -61,8 +66,6 @@ class PyIRCBotSub(PrimitiveBot):
if validation: if validation:
hook.method(msg, validation) hook.method(msg, validation)
# client.pub("pyircbot_send", "default privmsg {}".format(dumps([channel, "{}: pong".format(sender)])))
" Filesystem Methods " " Filesystem Methods "
def getConfigPath(self, moduleName): def getConfigPath(self, moduleName):
"""Return the absolute path for a module's config file """Return the absolute path for a module's config file

View File

@ -31,7 +31,7 @@ class PubSubClient(ModuleBase):
Listen to the bus for send messages and act on recv Listen to the bus for send messages and act on recv
""" """
sleep(3) sleep(3)
while True:#TODO clean exit onenable/ondisable etc while True:
if not self.bus: if not self.bus:
sleep(0.01) sleep(0.01)
continue continue
@ -61,26 +61,15 @@ class PubSubClient(ModuleBase):
""" """
self.bus.pub(self.config.get("publish").format(subchannel), "{} {}".format("default", message)) self.bus.pub(self.config.get("publish").format(subchannel), "{} {}".format("default", message))
@hook("PRIVMSG") @hook("PRIVMSG", "JOIN", "PART", "KICK", "MODE", "QUIT", "NICK", "PING")
def bus_privmsg(self, msg, cmd): def busdriver(self, msg, cmd):
""" """
Relay a privmsg to the event bus Relay a privmsg to the event bus
""" """
self.publish("privmsg", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}])) self.publish(msg.command.lower(),
dumps([msg.args,
@hook("JOIN") msg.prefix[0],
def bus_join(self, msg, cmd): msg.trailing, {"prefix": msg.prefix}]))
"""
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}]))
@hook("PRIVMSG") @hook("PRIVMSG")
def bus_command(self, msg, cmd): def bus_command(self, msg, cmd):
@ -93,7 +82,10 @@ class PubSubClient(ModuleBase):
cmd_name = match.groups()[1] cmd_name = match.groups()[1]
cmd_args = msg.trailing[len(cmd_name) + 1:].strip() cmd_args = msg.trailing[len(cmd_name) + 1:].strip()
self.publish("command_{}".format(cmd_name), 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): def onenable(self):
""" """
@ -110,4 +102,4 @@ class PubSubClient(ModuleBase):
""" """
self.log.warning("clean it up") self.log.warning("clean it up")
self.publish("sys", "offline") self.publish("sys", "offline")
self.bus.close() self.bus.close() # This will crash the listener thread