diff --git a/pinhook/bot.py b/pinhook/bot.py index abb0a05..0bda256 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -11,13 +11,20 @@ irc.client.ServerConnection.buffer_class.errors = 'replace' class Message: - def __init__(self, channel, nick, cmd, arg, botnick, ops): + def __init__(self, channel, nick, botnick, ops, cmd=None, arg=None, text=None, nick_list=None): self.channel = channel self.nick = nick - self.cmd = cmd - self.arg = arg + self.nick_list = nick_list self.botnick = botnick self.ops = ops + if cmd: + self.cmd = cmd + if arg: + self.arg = arg + if text: + self.text = text + if not (cmd or text): + print('Please pass Message a command or text!') class Bot(irc.bot.SingleServerIRCBot): @@ -32,7 +39,6 @@ class Bot(irc.bot.SingleServerIRCBot): self.bot_nick = nickname self.load_plugins() - def set_kwargs(self, **kwargs): kwarguments = { 'port': 6667, @@ -66,11 +72,14 @@ class Bot(irc.bot.SingleServerIRCBot): plugins.append(p) except Exception as e: print(e) - # gather all commands + # gather all commands and listeners self.cmds = {} + self.lstnrs = {} for plugin in plugins: for cmd in plugin.pinhook.plugin.cmds: self.cmds[cmd['cmd']] = cmd['func'] + for lstnr in plugin.pinhook.plugin.lstnrs: + self.lstnrs[lstnr['lstn']] = lstnr['func'] def on_welcome(self, c, e): if self.ns_pass: @@ -79,13 +88,14 @@ class Bot(irc.bot.SingleServerIRCBot): c.join(channel) def on_pubmsg(self, c, e): - self.process_command(c, e, e.arguments[0]) + self.process_command(c, e) def on_privmsg(self, c, e): - self.process_command(c, e, e.arguments[0]) + self.process_command(c, e) - def process_command(self, c, e, text): + def process_command(self, c, e): nick = e.source.nick + text = e.arguments[0] if e.target == self.bot_nick: chan = nick else: @@ -114,19 +124,36 @@ class Bot(irc.bot.SingleServerIRCBot): output = self.cmds[cmd](Message( channel=chan, cmd=cmd, + nick_list=list(self.channels[chan].users()), nick=nick, arg=arg, botnick=self.bot_nick, ops=self.ops )) + if output: + self.process_output(c, chan, output) except Exception as e: print(e) + else: + for lstnr in self.lstnrs: + try: + output = self.lstnrs[lstnr](Message( + channel=chan, + text=text, + nick_list=list(self.channels[chan].users()), + nick=nick, + botnick=self.bot_nick, + ops=self.ops + )) + if output: + self.process_output(c, chan, output) + except Exception as e: + print(e) - if output: - for msg in output.msg: - if output.msg_type == 'message': - c.privmsg(chan, msg) - elif output.msg_type == 'action': - c.action(chan, msg) - time.sleep(.5) - + def process_output(self, c, chan, output): + for msg in output.msg: + if output.msg_type == 'message': + c.privmsg(chan, msg) + elif output.msg_type == 'action': + c.action(chan, msg) + time.sleep(.5) diff --git a/pinhook/plugin.py b/pinhook/plugin.py index 0f85615..8251bfa 100644 --- a/pinhook/plugin.py +++ b/pinhook/plugin.py @@ -1,4 +1,6 @@ cmds = [] +lstnrs = [] + class Output: def __init__(self, msg_type, msg): @@ -6,7 +8,10 @@ class Output: self.msg = self.sanitize(msg) def sanitize(self, msg): - return msg.splitlines() + try: + return msg.splitlines() + except AttributeError: + return msg def action(msg): @@ -25,9 +30,19 @@ def clear_plugins(): cmds.clear() +def add_listener(name, func): + lstnrs.append({'lstn': name, 'func': func}) + + def register(command): def register_for_command(func): add_plugin(command, func) return func return register_for_command + +def listener(name): + def register_as_listener(func): + add_listener(name, func) + return func + return register_as_listener