diff --git a/protocols/inspircd.py b/protocols/inspircd.py index 1c5f441..d517555 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -8,6 +8,8 @@ from copy import copy import traceback from classes import * +uidgen = TS6UIDGenerator() + def _sendFromServer(irc, msg): irc.send(':%s %s' % (irc.sid, msg)) @@ -15,7 +17,7 @@ def _sendFromUser(irc, numeric, msg): irc.send(':%s %s' % (numeric, msg)) def spawnClient(irc, nick, ident, host, *args): - uid = next_uid(irc.sid) + uid = uidgen.next_uid(irc.sid) ts = int(time.time()) if not isNick(nick): raise ValueError('Invalid nickname %r.' % nick) diff --git a/utils.py b/utils.py index 0514cd0..141c563 100644 --- a/utils.py +++ b/utils.py @@ -5,17 +5,33 @@ global bot_commands # This should be a mapping of command names to functions bot_commands = {} -# From http://www.inspircd.org/wiki/Modules/spanningtree/UUIDs.html -chars = string.ascii_uppercase + string.digits -iters = [iter(chars) for _ in range(6)] -uidchars = [next(char) for char in iters] +class TS6UIDGenerator(): + """TS6 UID Generator module, adapted from InspIRCd source + https://github.com/inspircd/inspircd/blob/f449c6b296ab/src/server.cpp#L85-L156 + """ -def next_uid(sid, level=-1): - try: - uidchars[level] = next(iters[level]) - return sid + ''.join(uidchars) - except StopIteration: - return next_uid(sid, level-1) + def __init__(self): + # TS6 UIDs are 6 characters in length (9 including the SID). + # They wrap from ABCDEFGHIJKLMNOPQRSTUVWXYZ -> 1234567890 -> wrap around: + # (e.g. AAAAAA, AAAAAB ..., AAAAA8, AAAAA9, AAAAB0) + self.allowedchars = string.ascii_uppercase + string.digits + self.uidchars = [self.allowedchars[-1]]*6 + + def increment(self, pos=5): + # 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, sid): + self.increment() + return sid + ''.join(self.uidchars) def msg(irc, target, text, notice=False): command = 'NOTICE' if notice else 'PRIVMSG'