3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

apply_modes: refactor checks for existing modes

This commit is contained in:
James Lu 2019-08-22 23:16:51 -07:00
parent 087ca0947b
commit fe51f71a6e
2 changed files with 17 additions and 10 deletions

View File

@ -1007,12 +1007,16 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
Takes a list of parsed IRC modes, and applies them onto the given target mode list. Takes a list of parsed IRC modes, and applies them onto the given target mode list.
""" """
modelist = set(old_modelist) modelist = set(old_modelist)
mapping = collections.defaultdict(set)
if is_channel: if is_channel:
supported_modes = self.cmodes supported_modes = self.cmodes
else: else:
supported_modes = self.umodes supported_modes = self.umodes
for modepair in modelist: # Make a mapping of mode chars to values
mapping[modepair[0]].add(modepair[1])
for mode in changedmodes: for mode in changedmodes:
# Chop off the +/- part that parse_modes gives; it's meaningless for a mode list. # Chop off the +/- part that parse_modes gives; it's meaningless for a mode list.
try: try:
@ -1040,29 +1044,31 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
'it\'s a prefix mode.', self.name, str(mode)) 'it\'s a prefix mode.', self.name, str(mode))
continue continue
if mode[0][0] != '-': if mode[0][0] != '-': # Adding a mode; assume add if no explicit +/- is given
log.debug('(%s) Adding mode %r on %s', self.name, real_mode, modelist) log.debug('(%s) Adding mode %r on %s', self.name, real_mode, modelist)
# We're adding a mode existing = mapping.get(real_mode[0])
existing = [m for m in modelist if m[0] == real_mode[0] and m[1] != real_mode[1]] if existing and real_mode[0] not in supported_modes['*A']:
if existing and real_mode[1] and real_mode[0] not in supported_modes['*A']:
# The mode we're setting takes a parameter, but is not a list mode (like +beI). # The mode we're setting takes a parameter, but is not a list mode (like +beI).
# Therefore, only one version of it can exist at a time, and we must remove # Therefore, only one version of it can exist at a time, and we must remove
# any old modepairs using the same letter. Otherwise, we'll get duplicates when, # any old modepairs using the same letter. Otherwise, we'll get duplicates when,
# for example, someone sets mode "+l 30" on a channel already set "+l 25". # for example, someone sets mode "+l 30" on a channel already set "+l 25".
log.debug('(%s) Old modes for mode %r exist in %s, removing them: %s', log.debug('(%s) Old modes for mode %r exist in %s, removing them: %s',
self.name, real_mode, modelist, str(existing)) self.name, real_mode, modelist, str(existing))
[modelist.discard(oldmode) for oldmode in existing] for oldvalue in existing:
modelist.discard((real_mode[0], oldvalue))
modelist.add(real_mode) modelist.add(real_mode)
else: else: # Removing a mode
log.debug('(%s) Removing mode %r from %s', self.name, real_mode, modelist) log.debug('(%s) Removing mode %r from %s', self.name, real_mode, modelist)
# We're removing a mode
if real_mode[1] is None: if real_mode[1] is None:
# We're removing a mode that only takes arguments when setting. # We're removing a mode that only takes arguments when setting.
# Remove all mode entries that use the same letter as the one # Remove all mode entries that use the same letter as the one
# we're unsetting. # we're unsetting.
for oldmode in modelist.copy(): existing = mapping.get(real_mode[0])
if oldmode[0] == real_mode[0]: if existing:
modelist.discard(oldmode) for oldvalue in existing:
modelist.discard((real_mode[0], oldvalue))
else: else:
modelist.discard(real_mode) modelist.discard(real_mode)
log.debug('(%s) Final modelist: %s', self.name, modelist) log.debug('(%s) Final modelist: %s', self.name, modelist)

View File

@ -462,6 +462,7 @@ class BaseProtocolTest(unittest.TestCase):
self.assertEqual(c.modes, {('s', None), ('k', '12345')}) self.assertEqual(c.modes, {('s', None), ('k', '12345')})
# TODO: fix last case! # TODO: fix last case!
# TODO: test +b-b combinations et al.
@unittest.skip("Case mismatch not working yet") @unittest.skip("Case mismatch not working yet")
def test_apply_modes_channel_typeA(self): def test_apply_modes_channel_typeA(self):
c = self.p.channels['#Magic'] = Channel(self.p, name='#Magic') c = self.p.channels['#Magic'] = Channel(self.p, name='#Magic')