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

Use a WORKING TS6 UID generator, adapted from InspIRCd source

Closes #21.
This commit is contained in:
James Lu 2015-06-19 10:43:42 -07:00
parent 93b1a11651
commit 55a5d08378
2 changed files with 29 additions and 11 deletions

View File

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

View File

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