3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-02-02 23:54:08 +01:00

nefarious: split p10b64encode() away from P10SIDGenerator

This commit is contained in:
James Lu 2016-04-14 16:24:26 -07:00
parent 3299a871f2
commit 393771a32a

View File

@ -13,26 +13,7 @@ import utils
from log import log from log import log
from classes import * from classes import *
class P10SIDGenerator(): def p10b64encode(num, length=2):
def __init__(self, irc):
self.irc = irc
try:
query = irc.serverdata["sidrange"]
except (KeyError, ValueError):
raise RuntimeError('(%s) "sidrange" is missing from your server configuration block!' % irc.name)
try:
# Query is taken in the format MINNUM-MAXNUM, so we need
# to get the actual number values out of that.
self.minnum, self.maxnum = map(int, query.split('-', 1))
except ValueError:
raise RuntimeError('(%s) Invalid sidrange %r' % (irc.name, query))
else:
# Initialize a counter for the last numeric we've used.
self.currentnum = self.minnum
@staticmethod
def encode(num, length=2):
""" """
Encodes a given numeric using P10 Base64 numeric nicks, as documented at Encodes a given numeric using P10 Base64 numeric nicks, as documented at
https://github.com/evilnet/nefarious2/blob/a29b63144/doc/p10.txt#L69-L92 https://github.com/evilnet/nefarious2/blob/a29b63144/doc/p10.txt#L69-L92
@ -60,13 +41,31 @@ class P10SIDGenerator():
# Pad up to the required string length using the first character in our list (A). # Pad up to the required string length using the first character in our list (A).
return s.rjust(length, c[0]) return s.rjust(length, c[0])
class P10SIDGenerator():
def __init__(self, irc):
self.irc = irc
try:
query = irc.serverdata["sidrange"]
except (KeyError, ValueError):
raise RuntimeError('(%s) "sidrange" is missing from your server configuration block!' % irc.name)
try:
# Query is taken in the format MINNUM-MAXNUM, so we need
# to get the actual number values out of that.
self.minnum, self.maxnum = map(int, query.split('-', 1))
except ValueError:
raise RuntimeError('(%s) Invalid sidrange %r' % (irc.name, query))
else:
# Initialize a counter for the last numeric we've used.
self.currentnum = self.minnum
def next_sid(self): def next_sid(self):
""" """
Returns the next available SID. Returns the next available SID.
""" """
if self.currentnum > self.maxnum: if self.currentnum > self.maxnum:
raise ProtocolError("Ran out of valid SIDs! Check your 'sidrange' setting and try again.") raise ProtocolError("Ran out of valid SIDs! Check your 'sidrange' setting and try again.")
sid = self.encodeSID(self.currentnum) sid = p10b64encode(self.currentnum)
self.currentnum += 1 self.currentnum += 1
return sid return sid