diff --git a/banana.py b/banana.py new file mode 100644 index 0000000..935f585 --- /dev/null +++ b/banana.py @@ -0,0 +1,121 @@ +""" +Import or export from dec to banana or from banana to dec. Thx to itec (https://git.lattuga.net/itec/banana). +""" + +import prof +import re + +def dec2banana(num, dictstart = None, shiftend = None, minlength = None, dictionary = None): + #defaults + if dictstart is None: dictstart = 0 + if shiftend is None: shiftend = 0 + if minlength is None: minlength = 0 + if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] + + numdict = len(dictionary) + v = num + st = "" + l = 0 + + i = (numdict - 1 + dictstart + shiftend) % numdict + while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength): + r = v % len(dictionary[i]) + v = int(v / len(dictionary[i])) + st = dictionary[i][r] + st + i = (i - 1) % numdict + l += 1 + + return(st) + +def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None): + #defaults + if dictstart is None: dictstart = 0 + if shiftend is None: shiftend = 0 + if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456") + + numdict = len(dictionary) + if (len(banana) - shiftend) % numdict != 0: + return("Banana non valida") + v = 0 + for i in range(len(banana)): + r = (numdict + i + dictstart) % numdict + try: + v = v * len(dictionary[r]) + dictionary[r].index(banana[i]) + except: + return("Carattere non valido in posizione", i+1) + + return(v) + +def isbanana(banana, dictstart = None, shiftend = None, dictionary = None): + #defaults + if dictstart is None: dictstart = 0 + if shiftend is None: shiftend = 0 + if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456") + + numdict = len(dictionary) + if (len(banana) - shiftend) % numdict != 0: + return(False) + for i in range(len(banana)): + r = (numdict + i + dictstart) % numdict + if banana[i] not in dictionary[r]: + return(False) + + return(True) + +def callback_dec(x): + b = x[0].strip().lower() + if isbanana(b): + return "%s" % banana2dec(b) + else: + return x[0] + +def text2dec(text): + text = re.sub(r"[A-Z][A-Z]*", callback_dec, text) + return text + +def callback(x): + return " %s " % dec2banana(int(x[0], base=10)).upper() + +def text2banana(text): + text = re.sub(r"[0-9][0-9]*", callback, text) + return text + +def _cmd_banana(cmd=None,text=None): + if not cmd or not text: + prof.cons_show("Try: /help banana") + if cmd == "enc": + proc = text2banana(text) + recipient = prof.get_current_recipient() + room = prof.get_current_muc() + if recipient: + prof.send_line(proc) + elif room: + prof.send_line(proc) + elif prof.current_win_is_console(): + prof.cons_show(proc) + elif cmd == "dec": + proc = text2dec(text) + recipient = prof.get_current_recipient() + room = prof.get_current_muc() + if recipient: + prof.send_line(proc) + elif room: + prof.send_line(proc) + elif prof.current_win_is_console(): + prof.cons_show(proc) + +def prof_init(version, status, account_name, fulljid): + synopsis = [ + "/banana enc " + "/banana dec " + ] + description = "/banana enc: bananify a message.\n/banana dec: debananify a message." + args = [ + [ "", "The message to be bananified or debananified" ] + ] + examples = [ + "/banana dec 7467986 \n/banana enc GESUCANE" + ] + + prof.register_command("/banana", 0, 2, synopsis, description, args, examples, _cmd_banana) + prof.completer_add("/banana", ["enc","dec"])