Browse Source

Solidify & expand pubsub mode communications

dave/xdcc
dave 5 years ago
parent
commit
2f4ab6cd60
  1. 15
      bin/pubsubbot
  2. 32
      pyircbot/modules/PubSubClient.py

15
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

32
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

Loading…
Cancel
Save