move mode list (beIq e.g.) to it's own module, mode_lists.py

This commit is contained in:
jesopo 2020-01-22 17:41:20 +00:00
parent 3ddeea0692
commit 811b85d0f9
3 changed files with 63 additions and 55 deletions

View file

@ -401,61 +401,6 @@ class Module(ModuleManager.BaseModule):
def _cunmute(self, channel): def _cunmute(self, channel):
channel.send_mode("-m") channel.send_mode("-m")
@utils.hook("new.channel")
def new_channel_lists(self, event):
event["channel"].mode_lists = {}
# RPL_BANLIST
@utils.hook("received.367")
def on_367(self, event):
self._mode_list_mask(event["server"], event["line"].args[1], "b",
event["line"].args[2])
@utils.hook("received.368")
def on_368(self, event):
self._mode_list_end(event["server"], event["line"].args[1], "b")
# RPL_QUIETLIST
@utils.hook("received.728")
def on_728(self, event):
self._mode_list_mask(event["server"], event["line"].args[1], "q",
event["line"].args[3])
@utils.hook("received.729")
def on_729(self, event):
self._mode_list_end(event["server"], event["line"].args[1], "q")
def _mode_list_mask(self, server, target, mode, mask):
if target in server.channels:
channel = server.channels.get(target)
self._mask_add(channel, "~%s " % mode, mask)
def _mode_list_end(self, server, target, mode):
if target in server.channels:
channel = server.channels.get(target)
temp_key = "~%s" % mode
if temp_key in channel.mode_lists:
channel.mode_lists[mode] = channel.mode_lists.pop(temp_key)
def _mask_add(self, channel, mode, mask):
if not mode in channel.mode_lists:
channel.mode_lists[mode] = set([])
channel.mode_lists[mode].add(mask)
def _mask_remove(self, channel, mode, mask):
if mode in channel.mode_lists:
channel.mode_lists[mode].discard(mask)
@utils.hook("received.mode.channel")
def channel_mode_lists(self, event):
for mode, arg in event["modes"]:
if mode[1] in event["server"].channel_list_modes:
if mode[0] == "+":
self._mask_add(event["channel"], mode[1], arg)
else:
self._mask_remove(event["channel"], mode[1], arg)
@utils.hook("self.join")
def self_join(self, event):
event["channel"].send_mode("+%s" %
"".join(event["server"].channel_list_modes))
@utils.hook("received.command.clear") @utils.hook("received.command.clear")
@utils.kwarg("channel_only", True) @utils.kwarg("channel_only", True)
@utils.kwarg("require_mode", "o") @utils.kwarg("require_mode", "o")

View file

@ -18,6 +18,7 @@ class Channel(IRCObject.Object):
self.topic_time = 0 self.topic_time = 0
self.users = set([]) # type: typing.Set[IRCUser.User] self.users = set([]) # type: typing.Set[IRCUser.User]
self.modes = {} # type: typing.Dict[str, typing.Set] self.modes = {} # type: typing.Dict[str, typing.Set]
self.mode_lists = {}
self.user_modes = {} # type: typing.Dict[IRCUser.User, typing.Set] self.user_modes = {} # type: typing.Dict[IRCUser.User, typing.Set]
self.created_timestamp = None self.created_timestamp = None
self.buffer = IRCBuffer.Buffer(bot, server) self.buffer = IRCBuffer.Buffer(bot, server)

View file

@ -0,0 +1,62 @@
from src import ModuleManager, utils
class Module(ModuleManager.BaseModule):
# RPL_BANLIST
@utils.hook("received.367")
def on_367(self, event):
self._mode_list_mask(event["server"], event["line"].args[1], "b",
event["line"].args[2])
@utils.hook("received.368")
def on_368(self, event):
self._mode_list_end(event["server"], event["line"].args[1], "b")
# RPL_QUIETLIST
@utils.hook("received.728")
def on_728(self, event):
self._mode_list_mask(event["server"], event["line"].args[1], "q",
event["line"].args[3])
@utils.hook("received.729")
def on_729(self, event):
self._mode_list_end(event["server"], event["line"].args[1], "q")
def _mode_list_mask(self, server, target, mode, mask):
if target in server.channels:
channel = server.channels.get(target)
self._mask_add(channel, "~%s " % mode, mask)
def _mode_list_end(self, server, target, mode):
if target in server.channels:
channel = server.channels.get(target)
temp_key = "~%s" % mode
if temp_key in channel.mode_lists:
channel.mode_lists[mode] = channel.mode_lists.pop(temp_key)
else:
channel.mode_lists[mode] = set([])
def _mask_add(self, channel, mode, mask):
if not mode in channel.mode_lists:
channel.mode_lists[mode] = set([])
channel.mode_lists[mode].add(mask)
def _mask_remove(self, channel, mode, mask):
if mode in channel.mode_lists:
channel.mode_lists[mode].discard(mask)
@utils.hook("received.mode.channel")
def channel_mode_lists(self, event):
for mode, arg in event["modes"]:
if mode[1] in event["server"].channel_list_modes:
if mode[0] == "+":
self._mask_add(event["channel"], mode[1], arg)
else:
self._mask_remove(event["channel"], mode[1], arg)
elif mode[1] in dict(event["server"].prefix_modes):
if event["server"].irc_equals(event["server"].nickname, arg):
missed = set(event["server"].channel_list_modes)-set(
event["channel"].mode_lists.keys())
if missed:
event["channel"].send_mode("+%s" % "".join(missed))
@utils.hook("self.join")
def self_join(self, event):
event["channel"].send_mode("+%s" %
"".join(event["server"].channel_list_modes))