From 8135f3a735cf1bd15e3805eed7b29c4694ba1ab2 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 19 Mar 2016 17:25:04 -0700 Subject: [PATCH] core: Depluralize prefixmodes mappings (#168) This is an API breaking change. I really don't know why I made these keys plural to start, since it only made things more complicated. Prefix modes are not pluralized in the list of supported modes of each IRCd. --- classes.py | 4 ++-- coreplugin.py | 2 +- plugins/commands.py | 2 +- plugins/relay.py | 6 +++--- utils.py | 37 +++++++++++++++++++++---------------- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/classes.py b/classes.py index 84c18d7..61c3b3b 100644 --- a/classes.py +++ b/classes.py @@ -573,8 +573,8 @@ class IrcChannel(): self.modes = {('n', None), ('t', None)} self.topic = '' self.ts = int(time.time()) - self.prefixmodes = {'ops': set(), 'halfops': set(), 'voices': set(), - 'owners': set(), 'admins': set()} + self.prefixmodes = {'op': set(), 'halfop': set(), 'voice': set(), + 'owner': set(), 'admin': set()} # Determines whether a topic has been set here or not. Protocol modules # should set this. diff --git a/coreplugin.py b/coreplugin.py index cd922d4..b8ba63e 100644 --- a/coreplugin.py +++ b/coreplugin.py @@ -62,7 +62,7 @@ def handle_whois(irc, source, command, args): # Show prefix modes like a regular IRCd does. for prefixmode, prefixchar in irc.prefixmodes.items(): modename = [mname for mname, char in irc.cmodes.items() if char == prefixmode] - if modename and target in c.prefixmodes[modename[0]+'s']: + if modename and target in c.prefixmodes[modename[0]]: chan = prefixchar + chan public_chans.append(chan) if public_chans: # Only send the line if the person is in any visible channels... diff --git a/plugins/commands.py b/plugins/commands.py index ec402ba..443743a 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -151,7 +151,7 @@ def showchan(irc, source, args): for user, nick in sorted(zip(c.users, nicks), key=lambda userpair: userpair[1].lower()): prefixmodes = [irc.prefixmodes.get(irc.cmodes.get(pmode, ''), '') - for pmode in pmodes if user in c.prefixmodes[pmode+'s']] + for pmode in pmodes if user in c.prefixmodes[pmode]] nicklist.append(''.join(prefixmodes) + nick) while nicklist[:20]: # 20 nicks per line to prevent message cutoff. diff --git a/plugins/relay.py b/plugins/relay.py index a3821bd..f3ba3de 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -203,8 +203,8 @@ def getPrefixModes(irc, remoteirc, channel, user, mlist=None): if pmode in remoteirc.cmodes: # If the mode supported by IRCd # Check if the caller is in the prefix modes list for that # prefix.prefixmodes mapping looks like: - # {'ops': ['user1', 'user2'], 'voices': ['user3', 'user4'], ...} - userlist = mlist[pmode+'s'] + # {'op': ['user1', 'user2'], 'voice': ['user3', 'user4'], ...} + userlist = mlist[pmode] log.debug('(%s) relay.getPrefixModes: checking if %r is in %s list: %r', irc.name, user, pmode, userlist) if user in userlist: @@ -683,7 +683,7 @@ def relayModes(irc, remoteirc, sender, channel, modes=None): log.debug("(%s) relay.relayModes: argument found as (%r, %r) " "for network %r.", irc.name, modechar, arg, remoteirc.name) - oplist = remoteirc.channels[remotechan].prefixmodes[name+'s'] + oplist = remoteirc.channels[remotechan].prefixmodes[name] log.debug("(%s) relay.relayModes: list of %ss on %r is: %s", irc.name, name, remotechan, oplist) diff --git a/utils.py b/utils.py index 2cb43ff..b99b427 100644 --- a/utils.py +++ b/utils.py @@ -291,25 +291,30 @@ def applyModes(irc, target, changedmodes): real_mode = (mode[0][1], mode[1]) except IndexError: real_mode = mode + if not usermodes: - pmode = '' - for m in ('owner', 'admin', 'op', 'halfop', 'voice'): - if m in irc.cmodes and real_mode[0] == irc.cmodes[m]: - pmode = m+'s' - if pmode: - pmodelist = irc.channels[target].prefixmodes[pmode] - log.debug('(%s) Initial prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) - if mode[0][0] == '+': - pmodelist.add(mode[1]) - else: - pmodelist.discard(mode[1]) - irc.channels[target].prefixmodes[pmode] = pmodelist - log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) + # We only handle +qaohv for now. Iterate over every supported mode: + # if the IRCd supports this mode and it is the one being set, add/remove + # the person from the corresponding prefix mode list (e.g. c.prefixmodes['op'] + # for ops). + for pmode in ('owner', 'admin', 'op', 'halfop', 'voice'): + if pmode in irc.cmodes and real_mode[0] == irc.cmodes[pmode]: + pmodelist = irc.channels[target].prefixmodes[pmode] + log.debug('(%s) Initial prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) + if mode[0][0] == '+': + pmodelist.add(mode[1]) + else: + pmodelist.discard(mode[1]) + + log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) + if real_mode[0] in irc.prefixmodes: - # Ignore other prefix modes such as InspIRCd's +Yy + # Don't add prefix modes to IrcChannel.modes; they belong in the + # prefixmodes mapping handled above. log.debug('(%s) Not adding mode %s to IrcChannel.modes because ' - 'it\'s a prefix mode we don\'t care about.', irc.name, str(mode)) + 'it\'s a prefix mode.', irc.name, str(mode)) continue + if mode[0][0] == '+': # We're adding a mode existing = [m for m in modelist if m[0] == real_mode[0] and m[1] != real_mode[1]] @@ -417,7 +422,7 @@ def reverseModes(irc, target, modes, oldobj=None): possible_modes['*A'] += ''.join(irc.prefixmodes) for name, userlist in c.prefixmodes.items(): try: - oldmodes.update([(irc.cmodes[name[:-1]], u) for u in userlist]) + oldmodes.update([(irc.cmodes[name], u) for u in userlist]) except KeyError: continue else: