From 393771a32a70582c915da2b2b3d98bfedd4567ee Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 14 Apr 2016 16:24:26 -0700 Subject: [PATCH] nefarious: split p10b64encode() away from P10SIDGenerator --- protocols/nefarious.py | 59 +++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/protocols/nefarious.py b/protocols/nefarious.py index 2842749..f28256d 100644 --- a/protocols/nefarious.py +++ b/protocols/nefarious.py @@ -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