3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

applyModes: chop off the +/- part of each mode tuple when storing mode lists

Also update these tests accordingly.
This commit is contained in:
James Lu 2015-07-05 23:00:14 -07:00
parent f1e26b6c55
commit 48c3b4b2e9
2 changed files with 20 additions and 15 deletions

View File

@ -182,19 +182,19 @@ class TestInspIRCdProtocol(unittest.TestCase):
def testHandleFjoinModes(self): def testHandleFjoinModes(self):
self.irc.run(':70M FJOIN #Chat 1423790411 +nt :,10XAAAAAA ,10XAAAAAB') self.irc.run(':70M FJOIN #Chat 1423790411 +nt :,10XAAAAAA ,10XAAAAAB')
self.assertEqual({('+n', None), ('+t', None)}, self.irc.channels['#chat'].modes) self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#chat'].modes)
# Sequential FJOINs must NOT remove existing modes # Sequential FJOINs must NOT remove existing modes
self.irc.run(':70M FJOIN #Chat 1423790412 + :,10XAAAAAC') self.irc.run(':70M FJOIN #Chat 1423790412 + :,10XAAAAAC')
self.assertEqual({('+n', None), ('+t', None)}, self.irc.channels['#chat'].modes) self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#chat'].modes)
def testHandleFjoinModesWithArgs(self): def testHandleFjoinModesWithArgs(self):
self.irc.run(':70M FJOIN #Chat 1423790414 +nlks 10 t0psekrit :,10XAAAAAA ,10XAAAAAB') self.irc.run(':70M FJOIN #Chat 1423790414 +nlks 10 t0psekrit :,10XAAAAAA ,10XAAAAAB')
self.assertEqual({('+n', None), ('+s', None), ('+l', '10'), ('+k', 't0psekrit')}, self.assertEqual({('n', None), ('s', None), ('l', '10'), ('k', 't0psekrit')},
self.irc.channels['#chat'].modes) self.irc.channels['#chat'].modes)
def testHandleFjoinPrefixes(self): def testHandleFjoinPrefixes(self):
self.irc.run(':70M FJOIN #Chat 1423790418 +nt :ov,10XAAAAAA v,10XAAAAAB ,10XAAAAAC') self.irc.run(':70M FJOIN #Chat 1423790418 +nt :ov,10XAAAAAA v,10XAAAAAB ,10XAAAAAC')
self.assertEqual({('+n', None), ('+t', None)}, self.irc.channels['#chat'].modes) self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#chat'].modes)
self.assertEqual({'10XAAAAAA', '10XAAAAAB', '10XAAAAAC'}, self.irc.channels['#chat'].users) self.assertEqual({'10XAAAAAA', '10XAAAAAB', '10XAAAAAC'}, self.irc.channels['#chat'].users)
self.assertIn('10XAAAAAA', self.irc.channels['#chat'].prefixmodes['ops']) self.assertIn('10XAAAAAA', self.irc.channels['#chat'].prefixmodes['ops'])
self.assertEqual({'10XAAAAAA', '10XAAAAAB'}, self.irc.channels['#chat'].prefixmodes['voices']) self.assertEqual({'10XAAAAAA', '10XAAAAAB'}, self.irc.channels['#chat'].prefixmodes['voices'])
@ -202,18 +202,18 @@ class TestInspIRCdProtocol(unittest.TestCase):
def testHandleFmode(self): def testHandleFmode(self):
self.irc.run(':70M FJOIN #pylink 1423790411 +n :o,10XAAAAAA ,10XAAAAAB') self.irc.run(':70M FJOIN #pylink 1423790411 +n :o,10XAAAAAA ,10XAAAAAB')
self.irc.run(':70M FMODE #pylink 1423790412 +ikl herebedragons 100') self.irc.run(':70M FMODE #pylink 1423790412 +ikl herebedragons 100')
self.assertEqual({('+i', None), ('+k', 'herebedragons'), ('+l', '100'), ('+n', None)}, self.irc.channels['#pylink'].modes) self.assertEqual({('i', None), ('k', 'herebedragons'), ('l', '100'), ('n', None)}, self.irc.channels['#pylink'].modes)
self.irc.run(':70M FMODE #pylink 1423790413 -ilk+m herebedragons') self.irc.run(':70M FMODE #pylink 1423790413 -ilk+m herebedragons')
self.assertEqual({('+m', None), ('+n', None)}, self.irc.channels['#pylink'].modes) self.assertEqual({('m', None), ('n', None)}, self.irc.channels['#pylink'].modes)
def testHandleFmodeWithPrefixes(self): def testHandleFmodeWithPrefixes(self):
self.irc.run(':70M FJOIN #pylink 1423790411 +n :o,10XAAAAAA ,10XAAAAAB') self.irc.run(':70M FJOIN #pylink 1423790411 +n :o,10XAAAAAA ,10XAAAAAB')
# Prefix modes are stored separately, so they should never show up in .modes # Prefix modes are stored separately, so they should never show up in .modes
self.assertNotIn(('+o', '10XAAAAAA'), self.irc.channels['#pylink'].modes) self.assertNotIn(('o', '10XAAAAAA'), self.irc.channels['#pylink'].modes)
self.assertEqual({'10XAAAAAA'}, self.irc.channels['#pylink'].prefixmodes['ops']) self.assertEqual({'10XAAAAAA'}, self.irc.channels['#pylink'].prefixmodes['ops'])
self.irc.run(':70M FMODE #pylink 1423790412 +lot 50 %s' % self.u) self.irc.run(':70M FMODE #pylink 1423790412 +lot 50 %s' % self.u)
self.assertIn(self.u, self.irc.channels['#pylink'].prefixmodes['ops']) self.assertIn(self.u, self.irc.channels['#pylink'].prefixmodes['ops'])
modes = {('+l', '50'), ('+n', None), ('+t', None)} modes = {('l', '50'), ('n', None), ('t', None)}
self.assertEqual(modes, self.irc.channels['#pylink'].modes) self.assertEqual(modes, self.irc.channels['#pylink'].modes)
self.irc.run(':70M FMODE #pylink 1423790413 -o %s' % self.u) self.irc.run(':70M FMODE #pylink 1423790413 -o %s' % self.u)
self.assertEqual(modes, self.irc.channels['#pylink'].modes) self.assertEqual(modes, self.irc.channels['#pylink'].modes)

View File

@ -128,6 +128,10 @@ def parseModes(irc, target, args):
return res return res
def applyModes(irc, target, changedmodes): def applyModes(irc, target, changedmodes):
"""<target> <changedmodes>
Takes a list of parsed IRC modes (<changedmodes>, in the format of parseModes()), and applies them on <target>.
<target> can be either a channel or a user; this is handled automatically."""
usermodes = not isChannel(target) usermodes = not isChannel(target)
log.debug('(%s) Using usermodes for this query? %s', irc.name, usermodes) log.debug('(%s) Using usermodes for this query? %s', irc.name, usermodes)
if usermodes: if usermodes:
@ -136,10 +140,12 @@ def applyModes(irc, target, changedmodes):
modelist = irc.channels[target].modes modelist = irc.channels[target].modes
log.debug('(%s) Applying modes %r on %s (initial modelist: %s)', irc.name, changedmodes, target, modelist) log.debug('(%s) Applying modes %r on %s (initial modelist: %s)', irc.name, changedmodes, target, modelist)
for mode in changedmodes: for mode in changedmodes:
# Chop off the +/- part that parseModes gives; it's meaningless for a mode list.
real_mode = (mode[0][1], mode[1])
if not usermodes: if not usermodes:
pmode = '' pmode = ''
for m in ('owner', 'admin', 'op', 'halfop', 'voice'): for m in ('owner', 'admin', 'op', 'halfop', 'voice'):
if m in irc.cmodes and mode[0][1] == irc.cmodes[m]: if m in irc.cmodes and real_mode[0] == irc.cmodes[m]:
pmode = m+'s' pmode = m+'s'
if pmode: if pmode:
pmodelist = irc.channels[target].prefixmodes[pmode] pmodelist = irc.channels[target].prefixmodes[pmode]
@ -149,26 +155,25 @@ def applyModes(irc, target, changedmodes):
else: else:
pmodelist.discard(mode[1]) pmodelist.discard(mode[1])
log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes)
if mode[0][1] in irc.prefixmodes: if real_mode[0] in irc.prefixmodes:
# Ignore other prefix modes such as InspIRCd's +Yy # Ignore other prefix modes such as InspIRCd's +Yy
log.debug('(%s) Not adding mode %s to IrcChannel.modes because it\'s a prefix mode', irc.name, str(mode)) log.debug('(%s) Not adding mode %s to IrcChannel.modes because it\'s a prefix mode', irc.name, str(mode))
continue continue
if mode[0][0] == '+': if mode[0][0] == '+':
# We're adding a mode # We're adding a mode
modelist.add(mode) modelist.add(real_mode)
log.debug('(%s) Adding mode %r on %s', irc.name, mode, target) log.debug('(%s) Adding mode %r on %s', irc.name, mode, target)
else: else:
log.debug('(%s) Removing mode %r on %s', irc.name, mode, target) log.debug('(%s) Removing mode %r on %s', irc.name, mode, target)
# We're removing a mode # We're removing a mode
if 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.
for oldmode in modelist.copy(): for oldmode in modelist.copy():
if oldmode[0][1] == mode[0][1]: if oldmode[0] == real_mode[0]:
modelist.discard(oldmode) modelist.discard(oldmode)
else: else:
# Swap the - for a + and then remove it from the list. # Swap the - for a + and then remove it from the list.
mode = ('+' + mode[0][1], mode[1]) modelist.discard(real_mode)
modelist.discard(mode)
log.debug('(%s) Final modelist: %s', irc.name, modelist) log.debug('(%s) Final modelist: %s', irc.name, modelist)
def joinModes(modes): def joinModes(modes):