Change from yield checks to a func in events that pass up EventErrors

This commit is contained in:
jesopo 2019-06-15 18:42:14 +01:00
parent cb2cab627f
commit 8ab4880c5c
4 changed files with 40 additions and 58 deletions

View file

@ -56,7 +56,8 @@ class Module(ModuleManager.BaseModule):
:prefix: Kick :prefix: Kick
""" """
channel = event["server"].channels.get(event["args_split"][0]) channel = event["server"].channels.get(event["args_split"][0])
yield utils.Check("channel-access", channel, "kick")
event["check_assert"](utils.Check("channel-access", channel, "kick"))
self._kick_command(event, channel, event["args_split"][1:]) self._kick_command(event, channel, event["args_split"][1:])
@ -107,7 +108,8 @@ class Module(ModuleManager.BaseModule):
:usage: <channel> <nickname/hostmask> :usage: <channel> <nickname/hostmask>
""" """
channel = event["server"].channels.get(event["args_split"][0]) channel = event["server"].channels.get(event["args_split"][0])
yield utils.Check("channel-access", channel, "ban")
event["check_assert"](utils.Check("channel-access", channel, "ban"))
self._ban(event["server"], channel, True, event["args_split"][1]) self._ban(event["server"], channel, True, event["args_split"][1])
@utils.hook("received.command.ban", channel_only=True, min_args=1) @utils.hook("received.command.ban", channel_only=True, min_args=1)
@ -181,7 +183,8 @@ class Module(ModuleManager.BaseModule):
:usage: <channel> <nickname/hostmask> :usage: <channel> <nickname/hostmask>
""" """
channel = event["server"].channels.get(event["args_split"][0]) channel = event["server"].channels.get(event["args_split"][0])
yield utils.Check("channel-access", channel, "ban")
event["check_assert"](utils.Check("channel-access", channel, "ban"))
self._ban(event["server"], channel, False, event["args_split"][1]) self._ban(event["server"], channel, False, event["args_split"][1])

View file

@ -1,7 +1,7 @@
#--depends-on config #--depends-on config
#--depends-on permissions #--depends-on permissions
import re, string, types import re, string, typing
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
from . import outs from . import outs
@ -159,40 +159,14 @@ class Module(ModuleManager.BaseModule):
return False, None return False, None
return True, None return True, None
def _hook_call_catch(self, func):
try:
return True, func()
except utils.EventError as e:
return False, str(e)
def _hook_call(self, hook, event, check_kwargs):
hook_success, hook_return = self._hook_call_catch(
lambda: hook.call(event))
if hook_success and hook_return and isinstance( def _check_assert(self, check_kwargs,
hook_return, types.GeneratorType): check: typing.Union[utils.Check, utils.MultiCheck]):
while True: checks = check.to_multi() # bot Check and MultiCheck has this func
try: is_success, message = self._check("check", check_kwargs,
next_success, next_return = self._hook_call_catch( checks.requests())
lambda: next(hook_return)) if not is_success:
except StopIteration as e: raise utils.EventError(message)
return True, e.value
if next_success:
multi_check = None
if isinstance(next_return, utils.Check):
multi_check = next_return.to_multi()
elif isinstance(next_return, utils.MultiCheck):
multi_check = next_return
if multi_check:
check_success, check_message = self._check("check",
check_kwargs, multi_check.requests)
if not check_success:
return False, check_message
else:
return False, next_return
return hook_success, hook_return
def command(self, server, target, target_str, is_channel, user, command, def command(self, server, target, target_str, is_channel, user, command,
args_split, tags, hook, **kwargs): args_split, tags, hook, **kwargs):
@ -239,33 +213,32 @@ class Module(ModuleManager.BaseModule):
stderr.write("Not enough arguments (minimum: %d)" % stderr.write("Not enough arguments (minimum: %d)" %
min_args).send(command_method) min_args).send(command_method)
else: else:
check_kwargs = {"hook": hook, "user": user, "server": server, event_kwargs = {"hook": hook, "user": user, "server": server,
"target": target, "is_channel": is_channel, "tags": tags, "target": target, "is_channel": is_channel, "tags": tags,
"args_split": args_split, "command": command} "args_split": args_split, "command": command,
check_kwargs.update(kwargs) "args": " ".join(args_split)}
event_kwargs.update(kwargs)
check_assert = lambda check: self._check_assert(event_kwargs, check)
event_kwargs["check_assert"] = check_assert
check_success, check_message = self._check("preprocess", check_success, check_message = self._check("preprocess",
check_kwargs) event_kwargs)
if not check_success: if not check_success:
if check_message: if check_message:
stderr.write(check_message).send(command_method) stderr.write(check_message).send(command_method)
return True return True
args = " ".join(args_split) new_event = self.events.on(hook.event_name).make_event(
**event_kwargs)
new_event = self.events.on(hook.event_name).make_event(user=user,
server=server, target=target, args=args, tags=tags,
args_split=args_split, stdout=stdout, stderr=stderr,
is_channel=is_channel, command=command, **kwargs)
self.log.trace("calling command '%s': %s", self.log.trace("calling command '%s': %s",
[command, new_event.kwargs]) [command, new_event.kwargs])
hook_success, hook_message = self._hook_call(hook, new_event,
check_kwargs)
if not hook_success: try:
if not hook_message == None: hook.call(new_event)
stderr.write(hook_message).send(command_method) except utils.EventError as e:
stderr.write(str(e)).send(command_method)
return True return True
if not hook.kwargs.get("skip_out", False): if not hook.kwargs.get("skip_out", False):

View file

@ -137,14 +137,15 @@ class Module(ModuleManager.BaseModule):
if context == "set": if context == "set":
if name: if name:
yield utils.Check("self", name)|permission_check event["check_assert"](
utils.Check("self", name)|permission_check)
target = event["server"].get_user(name) target = event["server"].get_user(name)
else: else:
target = event["user"] target = event["user"]
elif context == "channelset": elif context == "channelset":
if name: if name:
yield permission_check event["check_assert"](permission_check)
if name in event["server"].channels: if name in event["server"].channels:
target = event["server"].channels.get(name) target = event["server"].channels.get(name)
else: else:
@ -152,14 +153,15 @@ class Module(ModuleManager.BaseModule):
name) name)
else: else:
if event["is_channel"]: if event["is_channel"]:
yield utils.Check("channel-mode", "o")|permission_check event["check_assert"](
utils.Check("channel-mode", "o")|permission_check)
target = event["target"] target = event["target"]
else: else:
raise utils.EventError( raise utils.EventError(
"Cannot change config for current channel when in " "Cannot change config for current channel when in "
"private message") "private message")
elif context == "serverset" or context == "botset": elif context == "serverset" or context == "botset":
yield permission_check event["check_assert"](permission_check)
export_settings = self._get_export_setting(context) export_settings = self._get_export_setting(context)
if not setting == None: if not setting == None:

View file

@ -188,7 +188,11 @@ def export(setting: str, value: typing.Any):
class MultiCheck(object): class MultiCheck(object):
def __init__(self, def __init__(self,
requests: typing.List[typing.Tuple[str, typing.List[str]]]): requests: typing.List[typing.Tuple[str, typing.List[str]]]):
self.requests = requests self._requests = requests
def to_multi(self):
return self
def requests(self):
return self._requests[:]
class Check(object): class Check(object):
def __init__(self, request: str, *args: str): def __init__(self, request: str, *args: str):
self.request = request self.request = request