1
0
Fork 0

Added check and match module defined functions to hooks

This commit is contained in:
nemunaire 2015-11-09 18:57:48 +01:00
parent 11bdf8d0a1
commit 36cfdd8861
3 changed files with 12 additions and 5 deletions

View File

@ -43,7 +43,7 @@ class Abstract:
"""Abstract class for Hook implementation"""
def __init__(self, call, data=None, channels=None, servers=None, mtimes=-1,
end_call=None):
end_call=None, check=None, match=None):
"""Create basis of the hook
Arguments:
@ -58,6 +58,8 @@ class Abstract:
assert callable(call), call
assert end_call is None or callable(end_call), end_call
assert check is None or callable(check), check
assert match is None or callable(match), match
assert isinstance(channels, list), channels
assert isinstance(servers, list), servers
assert type(mtimes) is int, mtimes
@ -65,6 +67,9 @@ class Abstract:
self.call = call
self.data = data
self.mod_check = check
self.mod_match = match
# TODO: find a way to have only one list: a limit is server + channel, not only server or channel
self.channels = channels
self.servers = servers
@ -96,11 +101,11 @@ class Abstract:
def check(self, data1):
return True
return self.mod_check(data1) if self.mod_check is not None else True
def match(self, data1):
return True
return self.mod_match(data1) if self.mod_match is not None else True
def run(self, data1, *args):

View File

@ -17,6 +17,7 @@
import re
from nemubot.hooks.message import Message
from nemubot.hooks.abstract import Abstract
from nemubot.hooks.keywords import NoKeyword
from nemubot.hooks.keywords.abstract import Abstract as AbstractKeywords
from nemubot.hooks.keywords.dict import Dict as DictKeywords
@ -61,5 +62,6 @@ class Command(Message):
else:
return (
(self.name is None or msg.cmd == self.name) and
(self.regexp is None or re.match(self.regexp, msg.cmd))
(self.regexp is None or re.match(self.regexp, msg.cmd)) and
Abstract.match(self, msg)
)

View File

@ -46,4 +46,4 @@ class Message(Abstract):
if not isinstance(msg, nemubot.message.text.Text):
return False
else:
return self.regexp is None or re.match(self.regexp, msg.message)
return (self.regexp is None or re.match(self.regexp, msg.message)) and super().match(msg)