3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-01-23 10:44:09 +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,6 +13,34 @@ import utils
from log import log
from classes import *
def p10b64encode(num, length=2):
"""
Encodes a given numeric using P10 Base64 numeric nicks, as documented at
https://github.com/evilnet/nefarious2/blob/a29b63144/doc/p10.txt#L69-L92
"""
c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]'
s = ''
# To accomplish this encoding, we divide the given value into a series of places. Much like
# a number can be divided into hundreds, tens, and digits (e.g. 128 is 1, 2, and 8), the
# places here add up to the value given. In the case of P10 Base64, each place can represent
# 0 to 63. divmod() is used to get the quotient and remainder of a division operation. When
# used on the input number and the length of our allowed characters list, the output becomes
# the values of (the next highest base, the current base).
places = divmod(num, len(c))
print('places:', places)
while places[0] >= len(c):
# If the base one higher than ours is greater than the largest value each base can
# represent, repeat the divmod process on that value,also keeping track of the
# remaining values we've calculated already.
places = divmod(places[0], len(c)) + places[1:]
print('places:', places)
# Expand the place values we've got to the characters list now.
chars = [c[place] for place in places]
s = ''.join(chars)
# Pad up to the required string length using the first character in our list (A).
return s.rjust(length, c[0])
class P10SIDGenerator():
def __init__(self, irc):
self.irc = irc
@ -31,42 +59,13 @@ class P10SIDGenerator():
# 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
https://github.com/evilnet/nefarious2/blob/a29b63144/doc/p10.txt#L69-L92
"""
c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]'
s = ''
# To accomplish this encoding, we divide the given value into a series of places. Much like
# a number can be divided into hundreds, tens, and digits (e.g. 128 is 1, 2, and 8), the
# places here add up to the value given. In the case of P10 Base64, each place can represent
# 0 to 63. divmod() is used to get the quotient and remainder of a division operation. When
# used on the input number and the length of our allowed characters list, the output becomes
# the values of (the next highest base, the current base).
places = divmod(num, len(c))
print('places:', places)
while places[0] >= len(c):
# If the base one higher than ours is greater than the largest value each base can
# represent, repeat the divmod process on that value,also keeping track of the
# remaining values we've calculated already.
places = divmod(places[0], len(c)) + places[1:]
print('places:', places)
# Expand the place values we've got to the characters list now.
chars = [c[place] for place in places]
s = ''.join(chars)
# Pad up to the required string length using the first character in our list (A).
return s.rjust(length, c[0])
def next_sid(self):
"""
Returns the next available SID.
"""
if self.currentnum > self.maxnum:
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
return sid