add !lsearch to show what !clear would remove

This commit is contained in:
jesopo 2020-01-23 16:24:08 +00:00
parent 32f047c10e
commit ecfab16fc4

View file

@ -403,12 +403,46 @@ class Module(ModuleManager.BaseModule):
def _cunmute(self, channel):
channel.send_mode("-m")
def _filter_mask(self, mask, list):
def _filter_mask(self, mask, mode_list):
parsed_mask = utils.irc.hostmask_parse(mask)
return list(utils.irc.hostmask_match_many(list, parsed_mask))
return list(utils.irc.hostmask_match_many(mode_list, parsed_mask))
def _filter_prefix(self, prefix, list):
return [l for l in list if prefix in l]
def _type_to_mode(self, server, channel, type):
if type[0] == "+":
if type[1:]:
if type[1] in channel.mode_lists:
return type[1], None
else:
raise utils.EventError("Unknown list mode")
else:
raise utils.EventError("Please provide a list mode")
elif type in ["quiets", "mutes"]:
quiet_method = self._quiet_method(server)
if quiet_method:
return quiet_method[0], quiet_method[1]
else:
raise utils.EventError(NO_QUIETS)
else:
raise utils.EventError("Unknown type '%s'" % type)
def _list_query_event(self, server, channel, args):
list_type = args[0]
list_mode, list_prefix = self._type_to_mode(server, channel, list_type)
list_mask = None
if len(args) > 1:
list_mask = args[1]
mode_list = list(channel.mode_lists[list_mode])
if list_prefix:
mode_list = self._filter_prefix(list_prefix, mode_list)
if list_mask:
mode_list = self._filter_mask(list_mask, mode_list)
return list_mode, mode_list
@utils.hook("received.command.clear")
@utils.kwarg("channel_only", True)
@utils.kwarg("require_mode", "o")
@ -417,39 +451,26 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("usage", "<type> [mask]")
@utils.kwarg("usage", "+<mode> [mask]")
def clear(self, event):
list_type = event["args_split"][0]
list_mode = None
list_filter = lambda l: list(l)
if list_type[0] == "+":
if list_type[1:]:
list_mode = list_type[1]
else:
raise utils.EventError("Please provide a list mode")
elif list_type in ["quiets", "mutes"]:
quiet_method = self._quiet_method(event["server"])
if quiet_method:
list_mode = quiet_method[0]
if quiet_method[1]:
list_filter = lambda list: self._filter_prefix(
quiet_method[1], list)
else:
raise utils.EventError(NO_QUIETS)
else:
raise utils.EventError("Unknown type '%s'" % type)
if list_mode in event["target"].mode_lists:
if len(event["args_split"]) > 1:
old_filter = list_filter
list_filter = lambda list: self._filter_mask(
event["args_split"][1], old_filter(list))
mode_list = list_filter(event["target"].mode_lists[list_mode])
mode, mode_list = self._list_query_event(
event["server"], event["target"], event["args_split"])
chunks = self._chunk(event["server"], mode_list)
for chunk in chunks:
event["target"].send_mode("-%s" % list_mode*len(chunk), chunk)
event["target"].send_mode("-%s" % mode*len(chunk), chunk)
@utils.hook("received.command.lsearch")
@utils.kwarg("channel_only", True)
@utils.kwarg("require_mode", "o")
@utils.kwarg("require_access", "lsearch")
@utils.kwarg("help", "Search a given channel list mode (e.g. +b)")
@utils.kwarg("usage", "<type> [mask]")
@utils.kwarg("usage", "+<mode> [mask]")
def lsearch(self, event):
mode, mode_list = self._list_query_event(
event["server"], event["target"], event["args_split"])
if mode_list:
event["stdout"].write("%s: %s" %
(event["user"].nickname, " ".join(mode_list)))
else:
raise utils.EventError("Unknown list mode")
event["stderr"].write("%s: no matches" % event["user"].nickname)