3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 13:09:23 +01:00

Move PUIDGenerator->classes, IncrementalUIDGenerator->ircs2s_common (#476)

This commit is contained in:
James Lu 2017-08-28 19:42:10 -07:00
parent b1159400f1
commit 2a7594e56e
8 changed files with 67 additions and 66 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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',

View File

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