diff --git a/README.md b/README.md index 56f9713..83fee4e 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ Optional arguments are: ### Creating plugins In your chosen plugins directory ("plugins" by default) make a python file with a function. You can use the `@pinhook.plugin.register` decorator to tell the bot the command to activate the function. +Use the `@pinhook.plugin.register_regex` to activate your functions for all messages, that match a regular expression, you define. + The function will need to be structured as such: ```python import pinhook.plugin diff --git a/pinhook/bot.py b/pinhook/bot.py index 618a9a2..784ca66 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -1,5 +1,6 @@ import imp import os +import re import ssl import time @@ -63,11 +64,15 @@ class Bot(irc.bot.SingleServerIRCBot): plugins.append(p) except Exception as e: print(e) - # gather all commands + # gather all commands and regexes self.cmds = {} + self.regexes = [] for plugin in plugins: for cmd in plugin.pinhook.plugin.cmds: self.cmds[cmd['cmd']] = cmd['func'] + for regex in plugin.pinhook.plugin.regexes: + self.regexes.append(regex) + def on_welcome(self, c, e): if self.ns_pass: @@ -118,6 +123,18 @@ class Bot(irc.bot.SingleServerIRCBot): )) except Exception as e: print(e) + # check if text matches agains any defined regex + for regex in self.regexes: + match = re.match(regex['regex'], text) + if match: + output = regex['func'](Message( + channel=chan, + cmd=regex['regex'], + nick=nick, + arg=text, + botnick=self.bot_nick, + ops=self.ops + )) if output: for msg in output.msg: diff --git a/pinhook/plugin.py b/pinhook/plugin.py index d8be1ce..c43c565 100644 --- a/pinhook/plugin.py +++ b/pinhook/plugin.py @@ -1,4 +1,5 @@ cmds = [] +regexes = [] class Output: def __init__(self, msg_type, msg): @@ -21,9 +22,19 @@ def add_plugin(command, func): cmds.append({'cmd': command, 'func': func}) +def add_regex(regex, func): + regexes.append({'regex': regex, 'func': func}) + + def register(command): def register_for_command(func): add_plugin(command, func) return func return register_for_command + +def register_regex(regex): + def register_for_regex(func): + add_regex(regex, func) + return func + return register_for_regex