Add ability to ignore users per-command

This commit is contained in:
jesopo 2019-04-27 14:49:45 +01:00
parent 74c16a1bbe
commit df39570d4a

View file

@ -31,6 +31,9 @@ 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}) "validate": utils.bool_or_none})
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:
@ -76,6 +79,13 @@ 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, user, command):
if user.get_setting("ignore", False):
return True
elif user.get_setting("ignore-%s" % command, False):
return True
return False
def message(self, event, command, args_index=1): def message(self, event, command, args_index=1):
args_split = event["message_split"][args_index:] args_split = event["message_split"][args_index:]
if not self.has_command(command): if not self.has_command(command):
@ -89,8 +99,7 @@ class Module(ModuleManager.BaseModule):
return return
if self.has_command(command): if self.has_command(command):
ignore = event["user"].get_setting("ignore", False) if self._is_ignored(event["user"], command):
if ignore:
return return
hook = None hook = None
@ -313,30 +322,47 @@ class Module(ModuleManager.BaseModule):
def ignore(self, event): def ignore(self, event):
""" """
:help: Ignore commands from a given user :help: Ignore commands from a given user
:usage: <nickname> :usage: <nickname> [command]
:permission: ignore :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]) user = event["server"].get_user(event["args_split"][0])
if user.get_setting("ignore", False): if user.get_setting(setting, False):
event["stderr"].write("I'm already ignoring '%s'" % event["stderr"].write("I'm already ignoring '%s'%s" %
user.nickname) (user.nickname, for_str))
else: else:
user.set_setting("ignore", True) user.set_setting(setting, True)
event["stdout"].write("Now ignoring '%s'" % user.nickname) event["stdout"].write("Now ignoring '%s'%s" %
(user.nickname, for_str))
@utils.hook("received.command.unignore", min_args=1) @utils.hook("received.command.unignore", min_args=1)
def unignore(self, event): def unignore(self, event):
""" """
:help: Unignore commands from a given user :help: Unignore commands from a given user
:usage: <nickname> :usage: <nickname> [command]
:permission: unignore :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]) user = event["server"].get_user(event["args_split"][0])
if not user.get_setting("ignore", False): if not user.get_setting(setting, False):
event["stderr"].write("I'm not ignoring '%s'" % user.nickname) event["stderr"].write("I'm not ignoring '%s'%s" %
(user.nickname, for_str))
else: else:
user.del_setting("ignore") user.del_setting(setting)
event["stdout"].write("Removed ignore for '%s'" % user.nickname) event["stdout"].write("Removed ignore for '%s'%s" %
(user.nickname, for_str))
@utils.hook("send.stdout") @utils.hook("send.stdout")
def send_stdout(self, event): def send_stdout(self, event):