3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-24 03:29:28 +01:00

classes: Move ISUPPORT-style PREFIX and capabilities handling to Protocol

This commit is contained in:
James Lu 2016-07-20 23:47:31 -07:00
parent 75d88224f1
commit eef54cd77d
2 changed files with 34 additions and 5 deletions

View File

@ -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):

View File

@ -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)