diff --git a/classes.py b/classes.py index 24876a5..dafe795 100644 --- a/classes.py +++ b/classes.py @@ -15,6 +15,7 @@ import ssl import hashlib from copy import deepcopy import inspect +import re try: import ircmatch @@ -1301,6 +1302,35 @@ class Protocol(): return {'target': split_server, 'users': affected_users, 'name': sname, 'uplink': uplink} + def parseCapabilities(self, args): + """ + Parses a string of capabilities in the 005 / RPL_ISUPPORT format. + """ + + if type(args) == str: + args = args.split(' ') + + caps = {} + for cap in args: + try: + # Try to split it as a KEY=VALUE pair. + key, value = cap.split('=', 1) + except ValueError: + key = cap + value = '' + caps[key] = value + + return caps + + @staticmethod + def parsePrefixes(args): + """ + Separates prefixes field like "(qaohv)~&@%+" into a dict mapping mode characters to mode + prefixes. + """ + prefixsearch = re.search(r'\(([A-Za-z]+)\)(.*)', args) + return dict(zip(prefixsearch.group(1), prefixsearch.group(2))) + ### FakeIRC classes, used for test cases class FakeIRC(Irc): diff --git a/protocols/inspircd.py b/protocols/inspircd.py index f5e559f..eb28b4e 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -444,11 +444,12 @@ class InspIRCdProtocol(TS6BaseProtocol): # USERMODES=,,s,BHIRSWcghikorwx GLOBOPS=1 SVSPART=1 # First, turn the arguments into a dict - caps = dict([x.split('=') for x in args[-1].split()]) - + caps = self.parseCapabilities(args[-1]) + log.debug("(%s) capabilities list: %s", self.irc.name, caps) # Check the protocol version protocol_version = int(caps['PROTOCOL']) + if protocol_version < self.min_proto_ver: raise ProtocolError("Remote protocol version is too old! " "At least %s (InspIRCd 2.0.x) is " @@ -474,9 +475,7 @@ class InspIRCdProtocol(TS6BaseProtocol): # Separate the prefixes field (e.g. "(Yqaohv)!~&@%+") into a # dict mapping mode characters to mode prefixes. - prefixsearch = re.search(r'\(([A-Za-z]+)\)(.*)', caps['PREFIX']) - self.irc.prefixmodes = dict(zip(prefixsearch.group(1), - prefixsearch.group(2))) + self.irc.prefixmodes = self.parsePrefixes(caps['PREFIX']) log.debug('(%s) self.irc.prefixmodes set to %r', self.irc.name, self.irc.prefixmodes)