__module_name__ = "bin2ascii" 
__module_version__ = "2.5.7" 
__module_description__ = "Decodes binary text sent by other people into ascii"
__module_author__ = "jackpot <jackpot@indivia.net>"

import xchat, binascii

def bin2ascii(num):
	bin = [ 128, 64, 32, 16, 8, 4, 2, 1 ]
	somma = i = 0
	num1=""
	num = num.strip()
	l = len(num)
	asc = ' '
	k = 0
	c = 0
	ss = 0
	cs = 0
	if l > 0:
		num = num + ' '
		add = 0
		while k < l:
			if i > 7:
				i = 0
			if num[k].isdigit() == False:
				ss += 1
				cs -= 1
				cs 
				if add > 0:
					asc += " "
					add = 0
				if num[k+1].isdigit() == False:
					ss = 0
					k += 1
					add += 1
				asc += chr(somma)
				i = 0
				somma = 0
				k += 1
			elif num[k].isdigit() == True:
				ss = 0
				c = int(num[k])
				if c == 1:
					somma += bin[i]
				cs += 1
			i += 1
			k += 1
	asc = asc + chr(somma)			
	i=0
	out=''
	while i < len(asc):
		if ord(asc[i]) > 0:
			out += asc[i]
		i += 1
	asc = out
	asc = asc.strip()
	if asc == "@":
		asc = ""
	if cs < 7:
		asc = ""
	return asc

def decode(word, word_eol, userdata):
   x = 0
   try:
   	x = len(word[1])
   except:
   	x = 0
   else:
	if x > 7:
		output = bin2ascii(word[1])
		if len(output.rstrip()) > 0:
			xchat.prnt(word[0] + ' ==> ' + output)
	
EVENTS = [
  ("Channel Action", 1),
  ("Channel Action Hilight", 1),
  ("Channel Message", 1),
  ("Channel Msg Hilight", 1),
  ("Channel Notice", 2),
  ("Generic Message", (0, 1)),
  ("Your Message", (0, 1)),
  ("Kick", 3),
  ("Killed", 1),
  ("Motd", 0),
  ("Notice", 1),
  ("Part with Reason", 3),
  ("Private Message", 1),
  ("Private Message to Dialog", 1),
  ("Quit", 1),
  ("Receive Wallops", 1),
  ("Server Notice", 0),
  ("Server Text", 0),
  ("Topic Change", 1),
  ("Topic", 1),
]

for event in EVENTS:
    xchat.hook_print(event[0], decode, event)

def bin_command(word, word_eol, userdata):
	s = ''
	out = ''
	text = ''
	dec = 0
	bit = 0
	pr = ''
	try:
		s = word_eol[1]
	except:
		s = ''
	i = 0
	while i < len(s):
		dec = ord(s[i])
		out = ''
		while dec > 0:
			bit = dec - int(dec /2) * 2
			if bit == 1:
				out += '1'
			elif bit == 0:
				out += '0'
			dec = int(dec / 2)
		i += 1
		out = rivolta(out)
		for d in range(8-len(out)):
			out = '0' + out
		text = text + ' ' + out
	if len(text) > 0:
		xchat.command('say ' + text.strip())
	return xchat.EAT_ALL

def rivolta(e):
	l = len(e)-1
	b = ''
	while l > -1:
		b = b + e[l]
		l -= 1
	return b

xchat.hook_command("bin", bin_command, "/BIN <text> Translate into binary this text.")

print "Plugin bin2ascii caricato!"
#print "--------------------------"
#print "  Come si fa a fidarsi di"
#print "  qualcuno che si chiama"
#print "  che si chiama CANCELLI"
#print "  e produce FINESTRE????"
#print "--------------------------"
#print "   Ovviamente se questo"
#print "  plugin lo state usando"
#print "   su un sistema windowz"
#print "  installera' backdoor e"
#print "        spyware."
#print "-------------------------"
#print " C:\>format c: /autotest"
#print "-------------------------"

