Track user channel modes by user object, not nickname

This commit is contained in:
jesopo 2018-07-02 16:15:26 +01:00
parent ae9d715e55
commit 84cc65526f
4 changed files with 24 additions and 9 deletions

View file

@ -28,18 +28,33 @@ class Channel(object):
self.users.add(user) self.users.add(user)
def remove_user(self, user): def remove_user(self, user):
self.users.remove(user) self.users.remove(user)
for mode in self.modes[:]:
if mode in self.sever.mode_prefixes and user in self.modes[mode]:
self.modes[mode].discard(user)
if not len(self.modes[mode]):
del self.modes[mode]
def has_user(self, user): def has_user(self, user):
return user in self.users return user in self.users
def add_mode(self, mode, args=None): def add_mode(self, mode, args=None):
if not mode in self.modes: if not mode in self.modes:
self.modes[mode] = set([]) self.modes[mode] = set([])
if args: if args:
if mode in self.server.mode_prefixes:
user = self.server.get_user(args)
if user:
self.modes[mode].add(user)
else:
self.modes[mode].add(args.lower()) self.modes[mode].add(args.lower())
self.bot.events.on("mode").on("channel").call( self.bot.events.on("mode").on("channel").call(
channel=self, mode=mode, args=args, remove=False) channel=self, mode=mode, args=args, remove=False)
def remove_mode(self, mode, args=None): def remove_mode(self, mode, args=None):
if not args: if not args:
del self.modes[mode] del self.modes[mode]
else:
if mode in self.server.mode_prefixes:
user = self.server.get_user(args)
if user:
self.modes[mode].discard(user)
else: else:
self.modes[mode].discard(args.lower()) self.modes[mode].discard(args.lower())
if not len(self.modes[mode]): if not len(self.modes[mode]):
@ -68,17 +83,17 @@ class Channel(object):
def send_ban(self, hostmask): def send_ban(self, hostmask):
self.server.send_mode(self.name, "+b", hostmask) self.server.send_mode(self.name, "+b", hostmask)
def mode_or_above(self, nickname, mode): def mode_or_above(self, user, mode):
mode_orders = list(self.server.mode_prefixes.values()) mode_orders = list(self.server.mode_prefixes.values())
mode_index = mode_orders.index(mode) mode_index = mode_orders.index(mode)
for mode in mode_orders[:mode_index+1]: for mode in mode_orders[:mode_index+1]:
if nickname.lower() in self.modes.get(mode, []): if user in self.modes.get(mode, []):
return True return True
return False return False
def get_user_status(self, nickname): def get_user_status(self, user):
modes = "" modes = ""
for mode in self.server.mode_prefixes.values(): for mode in self.server.mode_prefixes.values():
if nickname.lower() in self.modes.get(mode, []): if user in self.modes.get(mode, []):
modes += mode modes += mode
return modes return modes

View file

@ -90,7 +90,7 @@ class Module(object):
if len(set(nicknames) & set(event["message_split"])) >= event["channel"].get_setting( if len(set(nicknames) & set(event["message_split"])) >= event["channel"].get_setting(
"highlight-spam-threshold", 10): "highlight-spam-threshold", 10):
protection_enabled = event["channel"].get_setting("highlight-spam-protection", False) protection_enabled = event["channel"].get_setting("highlight-spam-protection", False)
has_mode = event["channel"].mode_or_above(event["user"].nickname, "v") has_mode = event["channel"].mode_or_above(event["user"], "v")
should_ban = event["channel"].get_setting("highlight-spam-ban", False) should_ban = event["channel"].get_setting("highlight-spam-ban", False)
if protection_enabled and not has_mode: if protection_enabled and not has_mode:
if should_ban: if should_ban:

View file

@ -10,6 +10,6 @@ class Module(object):
if event["is_channel"] and event["hook"].kwargs.get( if event["is_channel"] and event["hook"].kwargs.get(
"require_mode"): "require_mode"):
required_mode = event["hook"].kwargs.get("require_mode")[0] required_mode = event["hook"].kwargs.get("require_mode")[0]
if not event["target"].mode_or_above(event["user"].nickname, if not event["target"].mode_or_above(event["user"],
required_mode): required_mode):
return "You do not have permission to do this" return "You do not have permission to do this"

View file

@ -50,7 +50,7 @@ class Module(object):
self._set(self.settings, event, event["user"]) self._set(self.settings, event, event["user"])
def channel_set(self, event): def channel_set(self, event):
if event["target"].mode_or_above(event["user"].nickname, if event["target"].mode_or_above(event["user"],
"o"): "o"):
self._set(self.channel_settings, event, event["target"]) self._set(self.channel_settings, event, event["target"])
else: else: