Move !ignore logic to it's own file

This commit is contained in:
jesopo 2019-06-20 11:51:04 +01:00
parent 092b7b2874
commit 282bde82c5
2 changed files with 107 additions and 98 deletions

View file

@ -40,9 +40,6 @@ def _command_method_validate(s):
"help": "Disable/enable responding to prefixed commands in-channel", "help": "Disable/enable responding to prefixed commands in-channel",
"validate": utils.bool_or_none, "example": "on"}) "validate": utils.bool_or_none, "example": "on"})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self):
self.exports.add("is-ignored", self._is_ignored)
@utils.hook("new.user|channel") @utils.hook("new.user|channel")
def new(self, event): def new(self, event):
if "user" in event: if "user" in event:
@ -88,15 +85,6 @@ class Module(ModuleManager.BaseModule):
return target.get_setting(COMMAND_METHOD, return target.get_setting(COMMAND_METHOD,
server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper() server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper()
def _is_ignored(self, server, user, command):
if user.get_setting("ignore", False):
return True
elif user.get_setting("ignore-%s" % command, False):
return True
elif server.get_setting("ignore-%s" % command, False):
return True
return False
def _find_command_hook(self, server, command, is_channel, args_split): def _find_command_hook(self, server, command, is_channel, args_split):
if not self.has_command(command): if not self.has_command(command):
aliases = self._get_aliases(server) aliases = self._get_aliases(server)
@ -153,16 +141,17 @@ class Module(ModuleManager.BaseModule):
elif returned: elif returned:
error = returned error = returned
if hard_fail or (not force_success and error): if hard_fail:
if error:
return False, error
return False, None return False, None
elif not force_success and error:
return False, error
else:
return True, None return True, None
def _check_assert(self, check_kwargs, def _check_assert(self, check_kwargs,
check: typing.Union[utils.Check, utils.MultiCheck]): check: typing.Union[utils.Check, utils.MultiCheck]):
checks = check.to_multi() # bot Check and MultiCheck has this func checks = check.to_multi() # both Check and MultiCheck has this func
is_success, message = self._check("check", check_kwargs, is_success, message = self._check("check", check_kwargs,
checks.requests()) checks.requests())
if not is_success: if not is_success:
@ -170,9 +159,6 @@ class Module(ModuleManager.BaseModule):
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):
if self._is_ignored(server, user, command):
return False
message_tags = server.has_capability(MESSAGE_TAGS_CAP) message_tags = server.has_capability(MESSAGE_TAGS_CAP)
expect_output = hook.kwargs.get("expect_output", True) expect_output = hook.kwargs.get("expect_output", True)
@ -362,84 +348,6 @@ class Module(ModuleManager.BaseModule):
event["target"].last_stdout.send( event["target"].last_stdout.send(
self._command_method(event["target"], event["server"])) self._command_method(event["target"], event["server"]))
@utils.hook("received.command.ignore", min_args=1)
def ignore(self, event):
"""
:help: Ignore commands from a given user
:usage: <nickname> [command]
:permission: ignore
"""
setting = "ignore"
for_str = ""
if len(event["args_split"]) > 1:
command = event["args_split"][1].lower()
setting = "ignore-%s" % command
for_str = " for '%s'" % command
user = event["server"].get_user(event["args_split"][0])
if user.get_setting(setting, False):
event["stderr"].write("I'm already ignoring '%s'%s" %
(user.nickname, for_str))
else:
user.set_setting(setting, True)
event["stdout"].write("Now ignoring '%s'%s" %
(user.nickname, for_str))
@utils.hook("received.command.unignore", min_args=1)
def unignore(self, event):
"""
:help: Unignore commands from a given user
:usage: <nickname> [command]
:permission: unignore
"""
setting = "ignore"
for_str = ""
if len(event["args_split"]) > 1:
command = event["args_split"][1].lower()
setting = "ignore-%s" % command
for_str = " for '%s'" % command
user = event["server"].get_user(event["args_split"][0])
if not user.get_setting(setting, False):
event["stderr"].write("I'm not ignoring '%s'%s" %
(user.nickname, for_str))
else:
user.del_setting(setting)
event["stdout"].write("Removed ignore for '%s'%s" %
(user.nickname, for_str))
@utils.hook("received.command.serverignore", in_args=1)
def server_ignore(self, event):
"""
:permission: server-ignore
"""
command = event["args_split"][0].lower()
setting = "ignore-%s" % command
if event["server"].get_setting(setting, False):
event["stderr"].write("I'm already ignoring '%s' for %s" %
(command, str(event["server"])))
else:
event["server"].set_setting(setting, True)
event["stdout"].write("Now ignoring '%s' for %s" %
(command, str(event["server"])))
@utils.hook("received.command.serverunignore", in_args=1)
def server_unignore(self, event):
"""
:permission: server-unignore
"""
command = event["args_split"][0].lower()
setting = "ignore-%s" % command
if not event["server"].get_setting(setting, False):
event["stderr"].write("I'm not ignoring '%s' for %s" %
(command, str(event["server"])))
else:
event["server"].del_setting(setting)
event["stdout"].write("No longer ignoring '%s' for %s" %
(command, str(event["server"])))
@utils.hook("send.stdout") @utils.hook("send.stdout")
def send_stdout(self, event): def send_stdout(self, event):
target = event["target"] target = event["target"]

101
modules/ignore.py Normal file
View file

@ -0,0 +1,101 @@
from src import ModuleManager, utils
class Module(ModuleManager.BaseModule):
def _user_ignored(self, user):
return user.get_setting("ignore", False)
def _user_command_ignored(self, user, command):
return user.get_setting("ignore-%s" % command, False)
def _server_command_ignored(self, server, command):
return server.get_setting("ignore-%s" % command, False)
@utils.hook("received.message.private")
@utils.hook("received.message.channel")
def message(self, event):
if self._user_ignored(event["user"]):
event.eat()
@utils.hook("preprocess.command")
def preprocess_command(self, event):
if self._user_command_ignored(event["user"], event["command"]):
return utils.consts.PERMISSION_HARD_FAIL
elif self._server_command_ignored(event["server"], event["command"]):
return utils.consts.PERMISSION_HARD_FAIL
@utils.hook("received.command.ignore", min_args=1)
def ignore(self, event):
"""
:help: Ignore commands from a given user
:usage: <nickname> [command]
:permission: ignore
"""
setting = "ignore"
for_str = ""
if len(event["args_split"]) > 1:
command = event["args_split"][1].lower()
setting = "ignore-%s" % command
for_str = " for '%s'" % command
user = event["server"].get_user(event["args_split"][0])
if user.get_setting(setting, False):
event["stderr"].write("I'm already ignoring '%s'%s" %
(user.nickname, for_str))
else:
user.set_setting(setting, True)
event["stdout"].write("Now ignoring '%s'%s" %
(user.nickname, for_str))
@utils.hook("received.command.unignore", min_args=1)
def unignore(self, event):
"""
:help: Unignore commands from a given user
:usage: <nickname> [command]
:permission: unignore
"""
setting = "ignore"
for_str = ""
if len(event["args_split"]) > 1:
command = event["args_split"][1].lower()
setting = "ignore-%s" % command
for_str = " for '%s'" % command
user = event["server"].get_user(event["args_split"][0])
if not user.get_setting(setting, False):
event["stderr"].write("I'm not ignoring '%s'%s" %
(user.nickname, for_str))
else:
user.del_setting(setting)
event["stdout"].write("Removed ignore for '%s'%s" %
(user.nickname, for_str))
@utils.hook("received.command.serverignore", in_args=1)
def server_ignore(self, event):
"""
:permission: server-ignore
"""
command = event["args_split"][0].lower()
setting = "ignore-%s" % command
if event["server"].get_setting(setting, False):
event["stderr"].write("I'm already ignoring '%s' for %s" %
(command, str(event["server"])))
else:
event["server"].set_setting(setting, True)
event["stdout"].write("Now ignoring '%s' for %s" %
(command, str(event["server"])))
@utils.hook("received.command.serverunignore", in_args=1)
def server_unignore(self, event):
"""
:permission: server-unignore
"""
command = event["args_split"][0].lower()
setting = "ignore-%s" % command
if not event["server"].get_setting(setting, False):
event["stderr"].write("I'm not ignoring '%s' for %s" %
(command, str(event["server"])))
else:
event["server"].del_setting(setting)
event["stdout"].write("No longer ignoring '%s' for %s" %
(command, str(event["server"])))