add command handler in pubsub module
This commit is contained in:
parent
c1d4b025f7
commit
deab2ff924
|
@ -10,6 +10,11 @@ from threading import Thread
|
||||||
from json import dumps, loads
|
from json import dumps, loads
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from zmq.error import Again
|
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):
|
class PubSubClient(ModuleBase):
|
||||||
|
@ -32,6 +37,7 @@ class PubSubClient(ModuleBase):
|
||||||
except Again:
|
except Again:
|
||||||
sleep(0.01)
|
sleep(0.01)
|
||||||
continue
|
continue
|
||||||
|
try:
|
||||||
print(channel, "--", message)
|
print(channel, "--", message)
|
||||||
tag, subcommand, message = message.split(" ", 2)
|
tag, subcommand, message = message.split(" ", 2)
|
||||||
if tag != "default":
|
if tag != "default":
|
||||||
|
@ -40,6 +46,8 @@ class PubSubClient(ModuleBase):
|
||||||
if subcommand == "privmsg":
|
if subcommand == "privmsg":
|
||||||
dest, message = loads(message)
|
dest, message = loads(message)
|
||||||
self.bot.act_PRIVMSG(dest, message)
|
self.bot.act_PRIVMSG(dest, message)
|
||||||
|
except:
|
||||||
|
print_exc()
|
||||||
|
|
||||||
def publish(self, subchannel, message):
|
def publish(self, subchannel, message):
|
||||||
self.bus.pub(self.config.get("publish").format(subchannel), "{} {}".format("default", 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
|
# msg.command msg.args msg.prefix msg.trailing
|
||||||
self.publish("part", dumps([msg.args, msg.prefix[0], msg.trailing, {"prefix": msg.prefix}]))
|
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):
|
def onenable(self):
|
||||||
self.bus = MsgbusSubClient(self.host, int(self.port))
|
self.bus = MsgbusSubClient(self.host, int(self.port))
|
||||||
for channel in self.config.get("subscriptions"):
|
for channel in self.config.get("subscriptions"):
|
||||||
|
@ -95,6 +114,11 @@ User joins a channel:
|
||||||
pyircbot_join default ["dave-irccloud", "#clonebot",
|
pyircbot_join default ["dave-irccloud", "#clonebot",
|
||||||
{"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}]
|
{"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
|
# Client sending a message that the bot will relay
|
||||||
|
|
||||||
pyircbot_send default privmsg ["#clonebot", "asdf1234"]
|
pyircbot_send default privmsg ["#clonebot", "asdf1234"]
|
||||||
|
|
Loading…
Reference in New Issue