3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 20:22:45 +01:00

classes: clarify comments on mode-related functions

This commit is contained in:
James Lu 2017-11-03 23:40:11 -07:00 committed by GitHub
parent b366aa8d61
commit 62cef5c3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -592,8 +592,7 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
# We're setting a prefix mode on someone (e.g. +o user1) # We're setting a prefix mode on someone (e.g. +o user1)
log.debug('Mode %s: This mode is a prefix mode.', mode) log.debug('Mode %s: This mode is a prefix mode.', mode)
arg = args.pop(0) arg = args.pop(0)
# Convert nicks to UIDs implicitly; most IRCds will want # Convert nicks to UIDs implicitly
# this already.
arg = self.nick_to_uid(arg) or arg arg = self.nick_to_uid(arg) or arg
if arg not in self.users: # Target doesn't exist, skip it. if arg not in self.users: # Target doesn't exist, skip it.
log.debug('(%s) Skipping setting mode "%s %s"; the ' log.debug('(%s) Skipping setting mode "%s %s"; the '
@ -721,7 +720,7 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
@staticmethod @staticmethod
def _flip(mode): def _flip(mode):
"""Flips a mode character.""" """Flips a mode character."""
# Make it a list first, strings don't support item assignment # Make it a list first; strings don't support item assignment
mode = list(mode) mode = list(mode)
if mode[0] == '-': # Query is something like "-n" if mode[0] == '-': # Query is something like "-n"
mode[0] = '+' # Change it to "+n" mode[0] = '+' # Change it to "+n"
@ -732,7 +731,7 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
return ''.join(mode) return ''.join(mode)
def reverse_modes(self, target, modes, oldobj=None): def reverse_modes(self, target, modes, oldobj=None):
"""Reverses/Inverts the mode string or mode list given. """Reverses/inverts the mode string or mode list given.
Optionally, an oldobj argument can be given to look at an earlier state of Optionally, an oldobj argument can be given to look at an earlier state of
a channel/user object, e.g. for checking the op status of a mode setter a channel/user object, e.g. for checking the op status of a mode setter
@ -776,9 +775,9 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
# D = Mode that changes a setting and never has a parameter. # D = Mode that changes a setting and never has a parameter.
mchar = char[-1] mchar = char[-1]
if mchar in possible_modes['*B'] + possible_modes['*C']: if mchar in possible_modes['*B'] + possible_modes['*C']:
# We need to find the current mode list, so we can reset arguments # We need to look at the current mode list to reset modes that take arguments
# for modes that have arguments. For example, setting +l 30 on a channel # For example, trying to bounce +l 30 on a channel that had +l 50 set should
# that had +l 50 set should give "+l 30", not "-l". # give "+l 50" and not "-l".
oldarg = [m for m in oldmodes if m[0] == mchar] oldarg = [m for m in oldmodes if m[0] == mchar]
if oldarg: # Old mode argument for this mode existed, use that. if oldarg: # Old mode argument for this mode existed, use that.
oldarg = oldarg[0] oldarg = oldarg[0]
@ -797,7 +796,7 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
continue continue
elif char[0] == '-' and (mchar, arg) not in oldmodes and mchar in possible_modes['*A']: elif char[0] == '-' and (mchar, arg) not in oldmodes and mchar in possible_modes['*A']:
# We're unsetting a prefix mode that was never set - don't set it in response! # We're unsetting a prefix mode that was never set - don't set it in response!
# Charybdis lacks verification for this server-side. # TS6 IRCds lacks server-side verification for this and can cause annoying mode floods.
log.debug("(%s) reverse_modes: skipping reversing '%s %s' with %s since it " log.debug("(%s) reverse_modes: skipping reversing '%s %s' with %s since it "
"wasn't previously set.", self.name, char, arg, mpair) "wasn't previously set.", self.name, char, arg, mpair)
continue continue
@ -831,11 +830,11 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
# If the mode has a prefix, use that. # If the mode has a prefix, use that.
curr_prefix, mode = mode curr_prefix, mode = mode
except ValueError: except ValueError:
# If not, the current prefix stays the same; move on to the next # If not, the current prefix stays the same as the last mode pair; move on
# modepair. # to the next one.
pass pass
else: else:
# If the prefix of this mode isn't the same as the last one, add # Only when the prefix of this mode isn't the same as the last one do we add
# the prefix to the mode string. This prevents '+nt-lk' from turning # the prefix to the mode string. This prevents '+nt-lk' from turning
# into '+n+t-l-k' or '+ntlk'. # into '+n+t-l-k' or '+ntlk'.
if prefix != curr_prefix: if prefix != curr_prefix:
@ -869,9 +868,8 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
modes = list(modes) modes = list(modes)
while modes: while modes:
# PyLink mode lists come in the form [('+t', None), ('-b', '*!*@someone'), ('+l', 3)] # PyLink mode lists come in the form [('+t', None), ('-b', '*!*@someone'), ('+l', 3)]
# The +/- part is optional depending on context, and should either: # The +/- part is optional and is treated as the prefix of the last mode if not given,
# 1) The prefix of the last mode. # or + (adding modes) if it is the first mode in the list.
# 2) + (adding modes), if no prefix was ever given
next_mode = modes.pop(0) next_mode = modes.pop(0)
modechar, arg = next_mode modechar, arg = next_mode
@ -879,19 +877,19 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
if prefix not in '+-': if prefix not in '+-':
prefix = last_prefix prefix = last_prefix
# Explicitly add the prefix to the mode character to prevent # Explicitly add the prefix to the mode character to prevent
# ambiguity when passing it to join_modes(). # ambiguity when passing it to e.g. join_modes().
modechar = prefix + modechar modechar = prefix + modechar
# XXX: because tuples are immutable, we have to replace the entire modepair.. # XXX: because tuples are immutable, we have to replace the entire modepair...
next_mode = (modechar, arg) next_mode = (modechar, arg)
# Figure out the length that the next mode will add to the buffer. If we're changing # Figure out the length that the next mode will add to the buffer. If we're changing
# from + to - (setting to removing modes) or vice versa, we'll need two characters # from + to - (setting to removing modes) or vice versa, we'll need two characters:
# ("+" or "-") plus the mode char itself. # the "+" or "-" as well as the actual mode char.
next_length = 1 next_length = 1
if prefix != last_prefix: if prefix != last_prefix:
next_length += 1 next_length += 1
# Replace the last_prefix with the current one for the next iteration. # Replace the last mode prefix with the current one for the next iteration.
last_prefix = prefix last_prefix = prefix
if arg: if arg:
@ -910,8 +908,8 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
queued_modes.append(next_mode) queued_modes.append(next_mode)
log.debug('wrap_modes: queued modes: %s', queued_modes) log.debug('wrap_modes: queued modes: %s', queued_modes)
else: else:
# Otherwise, create a new message by joining the previous queue. # Otherwise, create a new message by joining the previous queued modes into a message.
# Then, add our current mode. # Then, create a new message with our current mode.
strings.append(cls.join_modes(queued_modes)) strings.append(cls.join_modes(queued_modes))
queued_modes.clear() queued_modes.clear()