Awesome IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.3 KiB

.. module::PubSubClient
:synopsis: connect to a message bus and act as a message relay
.. moduleauthor::Dave Pedu <>
from pyircbot.modulebase import ModuleBase, hook
from msgbus.client import MsgbusSubClient # see
from threading import Thread
from json import dumps, loads
from time import sleep
from zmq.error import Again
class PubSubClient(ModuleBase):
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName), self.port = self.config.get("servers")[0].split(":")
self.bus = None
self.bus_listener_thread = Thread(target=self.bus_listener)
self.bus_listener_thread.daemon = True
def bus_listener(self):
while True:#TODO clean exit onenable/ondisable etc
if not self.bus:
channel, message = self.bus.recv(block=False)
except Again:
print(channel, "--", message)
tag, subcommand, message = message.split(" ", 2)
if tag != "default":
if subcommand == "privmsg":
dest, message = loads(message), message)
def publish(self, subchannel, message):"publish").format(subchannel), "{} {}".format("default", message))
def bus_privmsg(self, msg):
# msg.command msg.args msg.prefix msg.trailing
self.publish("privmsg", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}]))
def bus_join(self, msg):
# msg.command msg.args msg.prefix msg.trailing
self.publish("join", dumps([msg.prefix[0], msg.trailing, {"prefix": msg.prefix}]))
def bus_part(self, msg):
# msg.command msg.args msg.prefix msg.trailing
self.publish("part", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}]))
def onenable(self):
self.bus = MsgbusSubClient(, int(self.port))
for channel in self.config.get("subscriptions"):
self.publish("sys", "online")
def ondisable(self):
self.log.warning("clean it up")
self.publish("sys", "offline")
Bot connects to the bus:
pyircbot_sys default online
Bot disconnects from the bus:
pyircbot_sys default offline
Bot relaying a privmsg:
pyircbot_privmsg default [["#clonebot"], "dave-irccloud", "message text",
{"prefix": ["dave-irccloud", "sid36094", ""]}]
User parts a channel:
pyircbot_part default [["#clonebot"], "dave-irccloud", "part msg",
{"prefix": ["dave-irccloud", "sid36094", ""]}]
User joins a channel:
pyircbot_join default ["dave-irccloud", "#clonebot",
{"prefix": ["dave-irccloud", "sid36094", ""]}]
# Client sending a message that the bot will relay
pyircbot_send default privmsg ["#clonebot", "asdf1234"]