You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.6 KiB
Python

"""
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
_links = {}
_lastlink = {}
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.chat_show(recipient, proc)
elif room:
prof.room_show(room, proc)
elif prof.current_win_is_console():
prof.cons_show(proc)
def _process_message(jid, current_jid, message):
links = re.search(r'[A-Z][A-Z]*',message).string.split('\n')
if links:
if jid not in _links:
_links[jid] = []
for link in links:
if link not in _links[jid]:
_links[jid].append(link)
if current_jid == jid:
prof.completer_add("/banana dec", _links[jid])
_lastlink[jid] = links[len(links)-1]
def prof_post_chat_message_display(jid, resource, message):
current_jid = prof.get_current_recipient()
_process_message(jid, current_jid, message)
def prof_post_room_message_display(room, nick, message):
current_jid = prof.get_current_muc()
_process_message(jid, current_jid, message)
def prof_on_room_history_message(room, nick, message, timestamp):
current_jid = prof.get_current_muc()
_process_message(jid, current_jid, message)
def prof_init(version, status, account_name, fulljid):
synopsis = [
"/banana enc <message>"
"/banana dec <message>"
]
description = "/banana enc: bananify a message.\n/banana dec: debananify a message."
args = [
[ "<message>", "The message to be bananified or debananified" ]
]
examples = [
"/banana enc 7467986 \n/banana dec GESUCANE"
]
prof.register_command("/banana", 0, 2, synopsis, description, args, examples, _cmd_banana)
prof.completer_add("/banana", ["enc","dec"])