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
"""
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:])
@ -107,7 +108,8 @@ class Module(ModuleManager.BaseModule):
:usage: <channel> <nickname/hostmask>
"""
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])
@utils.hook("received.command.ban", channel_only=True, min_args=1)
@ -181,7 +183,8 @@ class Module(ModuleManager.BaseModule):
:usage: <channel> <nickname/hostmask>
"""
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])

View file

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

View file

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

View file

@ -188,7 +188,11 @@ def export(setting: str, value: typing.Any):
class MultiCheck(object):
def __init__(self,
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):
def __init__(self, request: str, *args: str):
self.request = request