Hooks: add global methods to restrict read/write on channels
This commit is contained in:
parent
de2e1d6216
commit
49d7e4ced6
@ -42,19 +42,60 @@ class Abstract:
|
||||
|
||||
"""Abstract class for Hook implementation"""
|
||||
|
||||
def __init__(self, call, data=None, mtimes=-1, end_call=None):
|
||||
def __init__(self, call, data=None, channels=None, servers=None, mtimes=-1,
|
||||
end_call=None):
|
||||
"""Create basis of the hook
|
||||
|
||||
Arguments:
|
||||
call -- function to call to perform the hook
|
||||
|
||||
Keyword arguments:
|
||||
data -- optional datas passed to call
|
||||
"""
|
||||
|
||||
if channels is None: channels = list()
|
||||
if servers is None: servers = list()
|
||||
|
||||
assert callable(call), call
|
||||
assert end_call is None or callable(end_call), end_call
|
||||
assert isinstance(channels, list), channels
|
||||
assert isinstance(servers, list), servers
|
||||
assert type(mtimes) is int, mtimes
|
||||
|
||||
self.call = call
|
||||
self.data = data
|
||||
|
||||
# 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
|
||||
|
||||
self.times = mtimes
|
||||
self.end_call = end_call
|
||||
|
||||
|
||||
def can_read(self, receivers=list(), server=None):
|
||||
assert isinstance(receivers, list), receivers
|
||||
|
||||
if server is None or len(self.servers) == 0 or server in self.servers:
|
||||
if len(self.channels) == 0:
|
||||
return True
|
||||
|
||||
for receiver in receivers:
|
||||
if receiver in self.channels:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def can_write(self, receivers=list(), server=None):
|
||||
return True
|
||||
|
||||
|
||||
def check(self, data1):
|
||||
return True
|
||||
|
||||
|
||||
def match(self, data1, server):
|
||||
def match(self, data1):
|
||||
return True
|
||||
|
||||
|
||||
|
@ -54,7 +54,7 @@ class Message(Abstract):
|
||||
def __str__(self):
|
||||
return "\x03\x02%s\x03\x02%s%s" % (
|
||||
self.name if self.name is not None else "\x03\x1f" + self.regexp + "\x03\x1f" if self.regexp is not None else "",
|
||||
" (restricted to %s)" % (self.server + ":" if self.server is not None else "") + (self.channels if self.channels else "*") if len(self.channels) or self.server else "",
|
||||
" (restricted to %:%s)" % ((",".join(self.servers) if self.server else "*") + (",".join(self.channels) if self.channels else "*")) if len(self.channels) or len(self.server) else "",
|
||||
": %s" % self.help if self.help is not None else ""
|
||||
)
|
||||
|
||||
|
@ -65,7 +65,7 @@ class MessageTreater:
|
||||
"""
|
||||
|
||||
for h in self.hm.get_hooks("pre", type(msg).__name__):
|
||||
if h.match(msg):
|
||||
if h.can_read(msg.to, msg.server) and h.match(msg):
|
||||
res = h.run(msg)
|
||||
|
||||
if isinstance(res, list):
|
||||
@ -91,7 +91,7 @@ class MessageTreater:
|
||||
"""
|
||||
|
||||
for h in self.hm.get_hooks("in", type(msg).__name__):
|
||||
if h.match(msg):
|
||||
if h.can_read(msg.to, msg.server) and h.match(msg):
|
||||
res = h.run(msg)
|
||||
|
||||
if isinstance(res, list):
|
||||
@ -113,7 +113,7 @@ class MessageTreater:
|
||||
"""
|
||||
|
||||
for h in self.hm.get_hooks("post"):
|
||||
if h.match(msg):
|
||||
if h.can_write(msg.to, msg.server) and h.match(msg):
|
||||
res = h.run(msg)
|
||||
|
||||
if isinstance(res, list):
|
||||
|
Loading…
Reference in New Issue
Block a user