From 2a7594e56e8b82c24b682951e5bed7cb576fa206 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 28 Aug 2017 19:42:10 -0700 Subject: [PATCH] Move PUIDGenerator->classes, IncrementalUIDGenerator->ircs2s_common (#476) --- classes.py | 19 ++++++++++++ protocols/clientbot.py | 4 +-- protocols/ircs2s_common.py | 41 ++++++++++++++++++++++++++ protocols/ngircd.py | 4 +-- protocols/p10.py | 2 +- protocols/ts6_common.py | 2 +- protocols/unreal.py | 2 +- utils.py | 59 -------------------------------------- 8 files changed, 67 insertions(+), 66 deletions(-) diff --git a/classes.py b/classes.py index af4319c..58e3a05 100644 --- a/classes.py +++ b/classes.py @@ -1671,3 +1671,22 @@ class Channel(structures.DeprecatedAttributesObject, structures.CamelCaseToSnake return sorted(result, key=self.sort_prefixes) IrcChannel = Channel + + +class PUIDGenerator(): + """ + Pseudo UID Generator module, using a prefix and a simple counter. + """ + + def __init__(self, prefix, start=0): + self.prefix = prefix + self.counter = start + + def next_uid(self, prefix=''): + """ + Generates the next PUID. + """ + uid = '%s@%s' % (prefix or self.prefix, self.counter) + self.counter += 1 + return uid + next_sid = next_uid diff --git a/protocols/clientbot.py b/protocols/clientbot.py index 9081cb4..b602f1c 100644 --- a/protocols/clientbot.py +++ b/protocols/clientbot.py @@ -61,8 +61,8 @@ class ClientbotWrapperProtocol(IRCCommonProtocol): """Initializes a connection to a server.""" # (Re)initialize counter-based pseudo UID generators super().post_connect() - self.uidgen = utils.PUIDGenerator('PUID') - self.sidgen = utils.PUIDGenerator('ClientbotInternalSID') + self.uidgen = PUIDGenerator('PUID') + self.sidgen = PUIDGenerator('ClientbotInternalSID') self.has_eob = False ts = self.start_ts diff --git a/protocols/ircs2s_common.py b/protocols/ircs2s_common.py index 10a0b14..20c3e38 100644 --- a/protocols/ircs2s_common.py +++ b/protocols/ircs2s_common.py @@ -10,6 +10,47 @@ from pylinkirc.classes import IRCNetwork, ProtocolError from pylinkirc.log import log from pylinkirc import utils, conf +class IncrementalUIDGenerator(): + """ + Incremental UID Generator module, adapted from InspIRCd source: + https://github.com/inspircd/inspircd/blob/f449c6b296ab/src/server.cpp#L85-L156 + """ + + def __init__(self, sid): + if not (hasattr(self, 'allowedchars') and hasattr(self, 'length')): + raise RuntimeError("Allowed characters list not defined. Subclass " + "%s by defining self.allowedchars and self.length " + "and then calling super().__init__()." % self.__class__.__name__) + self.uidchars = [self.allowedchars[0]]*self.length + self.sid = str(sid) + + def increment(self, pos=None): + """ + Increments the UID generator to the next available UID. + """ + # Position starts at 1 less than the UID length. + if pos is None: + pos = self.length - 1 + + # If we're at the last character in the list of allowed ones, reset + # and increment the next level above. + if self.uidchars[pos] == self.allowedchars[-1]: + self.uidchars[pos] = self.allowedchars[0] + self.increment(pos-1) + else: + # Find what position in the allowed characters list we're currently + # on, and add one. + idx = self.allowedchars.find(self.uidchars[pos]) + self.uidchars[pos] = self.allowedchars[idx+1] + + def next_uid(self): + """ + Returns the next unused UID for the server. + """ + uid = self.sid + ''.join(self.uidchars) + self.increment() + return uid + class IRCCommonProtocol(IRCNetwork): COMMON_PREFIXMODES = [('h', 'halfop'), ('a', 'admin'), ('q', 'owner'), ('y', 'owner')] diff --git a/protocols/ngircd.py b/protocols/ngircd.py index 8bde606..3bcd5f3 100644 --- a/protocols/ngircd.py +++ b/protocols/ngircd.py @@ -45,13 +45,13 @@ class NgIRCdProtocol(IRCS2SProtocol): self.send("SERVER %s 1 :%s" % (self.serverdata['hostname'], self.serverdata.get('serverdesc') or conf.conf['pylink']['serverdesc'])) - self._uidgen = utils.PUIDGenerator('PUID') + self._uidgen = PUIDGenerator('PUID') # The first "SID" this generator should return is 2, because server token 1 is implied to be # the main PyLink server. RFC2813 has no official definition of SIDs, but rather uses # integer tokens in the SERVER and NICK (user introduction) commands to keep track of which # user exists on which server. Why did they do it this way? Who knows! - self._sidgen = utils.PUIDGenerator('PSID', start=1) + self._sidgen = PUIDGenerator('PSID', start=1) self.sid = self._sidgen.next_sid(prefix=self.serverdata['hostname']) self._caps.clear() diff --git a/protocols/p10.py b/protocols/p10.py index 22629a7..0bf8af3 100644 --- a/protocols/p10.py +++ b/protocols/p10.py @@ -12,7 +12,7 @@ from pylinkirc.classes import * from pylinkirc.log import log from pylinkirc.protocols.ircs2s_common import * -class P10UIDGenerator(utils.IncrementalUIDGenerator): +class P10UIDGenerator(IncrementalUIDGenerator): """Implements an incremental P10 UID Generator.""" def __init__(self, sid): diff --git a/protocols/ts6_common.py b/protocols/ts6_common.py index f333614..12f3bbc 100644 --- a/protocols/ts6_common.py +++ b/protocols/ts6_common.py @@ -85,7 +85,7 @@ class TS6SIDGenerator(): sid = ''.join(self.output) return sid -class TS6UIDGenerator(utils.IncrementalUIDGenerator): +class TS6UIDGenerator(IncrementalUIDGenerator): """Implements an incremental TS6 UID Generator.""" def __init__(self, sid): diff --git a/protocols/unreal.py b/protocols/unreal.py index 3ffdc5f..aea6d7e 100644 --- a/protocols/unreal.py +++ b/protocols/unreal.py @@ -333,7 +333,7 @@ class UnrealProtocol(TS6BaseProtocol): self.prefixmodes = {'q': '~', 'a': '&', 'o': '@', 'h': '%', 'v': '+'} # Track usages of legacy (Unreal 3.2) nicks. - self.legacy_uidgen = utils.PUIDGenerator('U32user') + self.legacy_uidgen = PUIDGenerator('U32user') self.umodes.update({'deaf': 'd', 'invisible': 'i', 'hidechans': 'p', 'protected': 'q', 'registered': 'r', diff --git a/utils.py b/utils.py index 5837b33..9db2dbf 100644 --- a/utils.py +++ b/utils.py @@ -38,65 +38,6 @@ class ProtocolError(RuntimeError): Exception raised when a network protocol violation is encountered in some way. """ -class IncrementalUIDGenerator(): - """ - Incremental UID Generator module, adapted from InspIRCd source: - https://github.com/inspircd/inspircd/blob/f449c6b296ab/src/server.cpp#L85-L156 - """ - - def __init__(self, sid): - if not (hasattr(self, 'allowedchars') and hasattr(self, 'length')): - raise RuntimeError("Allowed characters list not defined. Subclass " - "%s by defining self.allowedchars and self.length " - "and then calling super().__init__()." % self.__class__.__name__) - self.uidchars = [self.allowedchars[0]]*self.length - self.sid = str(sid) - - def increment(self, pos=None): - """ - Increments the UID generator to the next available UID. - """ - # Position starts at 1 less than the UID length. - if pos is None: - pos = self.length - 1 - - # If we're at the last character in the list of allowed ones, reset - # and increment the next level above. - if self.uidchars[pos] == self.allowedchars[-1]: - self.uidchars[pos] = self.allowedchars[0] - self.increment(pos-1) - else: - # Find what position in the allowed characters list we're currently - # on, and add one. - idx = self.allowedchars.find(self.uidchars[pos]) - self.uidchars[pos] = self.allowedchars[idx+1] - - def next_uid(self): - """ - Returns the next unused UID for the server. - """ - uid = self.sid + ''.join(self.uidchars) - self.increment() - return uid - -class PUIDGenerator(): - """ - Pseudo UID Generator module, using a prefix and a simple counter. - """ - - def __init__(self, prefix, start=0): - self.prefix = prefix - self.counter = start - - def next_uid(self, prefix=''): - """ - Generates the next PUID. - """ - uid = '%s@%s' % (prefix or self.prefix, self.counter) - self.counter += 1 - return uid - next_sid = next_uid - def add_cmd(func, name=None, **kwargs): """Binds an IRC command function to the given command name.""" world.services['pylink'].add_cmd(func, name=name, **kwargs)