#!/usr/bin/python """ wtbase - what the base Provides classes to encode text to arbitrary bases (base 2^n supported) and then to textual forms. """ import math import random class DecodingException(Exception): pass class Token(): """ A Token contains a word/sentence and a list of tokenlists which can follow the word. """ def __init__(self, word, nextLists): self.word = word self.nextLists = nextLists def __str__(self): return "Token: word \"%s\"" % self.word class TextGenerator(): """ Basis generator to en- and decode text-bits. """ def __init__(self, base=16): self.lists = {} self.base = base self.baseBit = math.log(base, 2) if self.baseBit != int(self.baseBit): raise ValueError("base must be a power of 2") self.baseBit = int(self.baseBit) self.startList = "initial" def addList(self, identifier, newList): self.lists[identifier] = newList def getList(self, identifier): return self.lists.get(identifier, None) def convToBits(self, data): """ Converts a data string into n-bit parts defined by self.base. Returns a list of integers. """ l = [] bit = 0 rest = 0 for c in data: n = ord(c) if bit != 0: nc = rest | ((n & ((1 << bit) - 1 << 8-bit)) >> 8-bit) l.append(nc) while 8-bit >= self.baseBit: nc = (n & (((1 << self.baseBit)-1) << 8-bit-self.baseBit)) >> (8-bit-self.baseBit) l.append(nc) bit += self.baseBit rest = (n & (1 << 8-bit) - 1) << self.baseBit-(8-bit) bit = (bit+self.baseBit) % 8 % self.baseBit if bit != 0: l.append(rest) return l def convToNums(self, data): """ Reassemble a list of bits back to the original data bytestring. """ l = "" w = 0 rest = 0 bit = 0 for n in data: if bit+self.baseBit >= 8: w |= n >> self.baseBit - (8-bit) bit = self.baseBit - (8-bit) l += chr(w) if bit != 0: w = (n & (1 << bit) - 1) << 8 - bit else: w = 0 else: w |= n << 8-bit-self.baseBit bit = (bit + self.baseBit) % 8 if bit == 0: l += chr(w) w = 0 return l #def convTo4Bits(self, data): # l = [] # for c in data: # n = ord(c) # lo = n & ((1 << 4)-1) # hi = (n & (((1 << 4)-1) << 4)) >> 4 # l.extend([hi, lo]) # return l class SpamGenerator(TextGenerator): """ De- and encode data in base8 spam text. """ def __init__(self): TextGenerator.__init__(self, base=8) self.startList = "greeting" self.addList("greeting", { 0: Token("Hi,\n\n", ["start"]), 1: Token("Hey,\n\n", ["start"]), 2: Token("Greetings,\n\n", ["start"]), 3: Token("Dear Mr. or Mrs.,,\n\n", ["start"]), 4: Token("SPECIAL OFFER!\n", ["start"]), 5: Token("High Quality! Read on!\n", ["start"]), 6: Token("Best buy!\n\n", ["start"]), 7: Token("Dear Valued Customer,\n\n", ["start"]), 8: Token("Well, uhm, ", ["start"]), }) self.addList("start", { 0: Token("we are happy to ", ["inform_them"]), 1: Token("we are glad to ", ["inform_them"]), 2: Token("we gladly ", ["inform_them"]), 3: Token("it happens that we can ",["inform_them"]), 4: Token("we want to ", ["inform_them"]), 5: Token("today ", ["you_have"]), 6: Token("ITS TRUE! ", ["you_have"]), 7: Token("you won! ", ["you_have"]), 8: Token("awesome for you, buddy! ",["leaving"]), }) self.addList("inform_them", { 0: Token("inform you, that ", ["you_have"]), 1: Token("make a remarkt, that ", ["you_have"]), 2: Token("announce, that ", ["you_have"]), 3: Token("celebrate with you! ", ["you_have"]), 4: Token("congratulate you, because ", ["you_have"]), 5: Token("take the extra step: ", ["you_have"]), 6: Token("tell you, that ", ["you_have"]), 7: Token("don't forget about you, ", ["you_have"]), 8: Token("move property! ", ["you_have"]), }) self.addList("you_have", { 0: Token("you won ", ["won_item"]), 1: Token("you have won ", ["won_item"]), 2: Token("you aqcuired ", ["won_item"]), 3: Token("one time offer only: ", ["won_item"]), 4: Token("at your account we found ", ["won_item"]), 5: Token("the prince of nigeria offers to you ", ["won_item"]), 6: Token("off shore accounts brought you ", ["won_item"]), 7: Token("insider traging brought you ", ["won_item"]), 8: Token("you managed to get", ["won_item"]), }) self.addList("won_item", { 0: Token("a sum of ", ["money_sum"]), 1: Token("the priceless diamond of Zalanda. " , ["claim"]), 2: Token("free viagra! ", ["claim"]), 3: Token("an inheritance of ", ["money_sum"]), 4: Token("the opportunity to make money online! ", ["claim"]), 5: Token("a part of an oil pipe line, worth ", ["money_sum"]), 6: Token("free money - ", ["money_sum"]), 7: Token("a rare antique item worth", ["money_sum"]), 8: Token("quiet a bit o' stuff. ", ["claim"]), }) self.addList("money_sum", { 0: Token( "5,000,000 USD. ", ["claim"]), 1: Token("10,000,000 USD. ", ["claim"]), 2: Token( "300,000 USD. ", ["claim"]), 3: Token("13,412,573 USD. ", ["claim"]), 4: Token( "7,555,530 USD. ", ["claim"]), 5: Token( "50,000 USD. ", ["claim"]), 6: Token( "4,500,000 USD. ", ["claim"]), 7: Token("42,000,000 USD. ", ["claim"]), 8: Token("87,000,000 USD. ", ["claim"]), }) self.addList("claim", { 0: Token("To claim ", ["claimable_item"]), 1: Token("To get hold ", ["claimable_item"]), 2: Token("To acquire ", ["claimable_item"]), 3: Token("To receive ", ["claimable_item"]), 4: Token("To obtain ", ["claimable_item"]), 5: Token("To gatherh ", ["claimable_item"]), 6: Token("To take ownership ", ["claimable_item"]), 7: Token("To collect ", ["claimable_item"]), 8: Token("To finally get ", ["claimable_item"]), }) self.addList("claimable_item", { 0: Token("this item, please send ", ["sendables"]), 1: Token("this stuff, please send ", ["sendables"]), 2: Token("your profit, please send ", ["sendables"]), 3: Token("these assets, please send ", ["sendables"]), 4: Token("this price, please send ", ["sendables"]), 5: Token("your earnings, please send ", ["sendables"]), 6: Token("this top-line profit, please send ", ["sendables"]), 7: Token("this treasure, please send ", ["sendables"]), 8: Token("this your winnings, please send ", ["sendables"]), }) self.addList("sendables", { 0: Token("us all your information.\n\n", ["more_stuff", "jibberjabber_start"]), 1: Token("us your account data.\n\n", ["more_stuff", "jibberjabber_start"]), 2: Token("us a transfer-free of 50 USD.\n\n", ["more_stuff", "jibberjabber_start"]), 3: Token("us a list of your passwords.\n\n", ["more_stuff", "jibberjabber_start"]), 4: Token("10 valid TAN Numbers.\n\n", ["more_stuff", "jibberjabber_start"]), 5: Token("us your mothers maiden name.\n\n", ["more_stuff", "jibberjabber_start"]), 6: Token("your birth certificate.\n\n", ["more_stuff", "jibberjabber_start"]), 7: Token("a listing of your incomes.\n\n", ["more_stuff", "jibberjabber_start"]), 8: Token("us your personal information.\n\n", ["jibberjabber_start", "leaving"]), }) self.addList("more_stuff", { 0: Token("But wait, there is more! ", ["you_have"]), 1: Token("But that is not all! ", ["you_have"]), 2: Token("And there is even more! ", ["you_have"]), 3: Token("Also ", ["you_have"]), 4: Token("And because you seem to be the luckiest person alive: ", ["you_have"]), 5: Token("And how does this sound: ", ["you_have"]), 6: Token("In addition ", ["you_have"]), 7: Token("But... what is this? ", ["you_have"]), 8: Token("AND! ", ["you_have"]), }) # loop this. random conversation starter self.addList("jibberjabber_start", { 0: Token("Would you ", ["jj_consider"]), # have you 1: Token("Will you ", ["jj_consider"]), 2: Token("Did you ever ", ["jj_consider"]), 3: Token("Maybe you ", ["jj_consider"]), 4: Token("In ", ["jj_times"]), # in