#!/usr/bin/perl -w
# VIM: set ts=4, set sw=4
#
# jackpot[at]indivia.net
package IRC::XChat::Match;

my $script_name    = "bin2ascii.pl";
my $sver = '1.3';

IRC::register($script_name,$script_version, "", "");
IRC::print("\cC4Plugin Bin2ascii $sver caricato!\n");

my $pkg = __PACKAGE__;

IRC::add_print_handler("Channel Action",   "${pkg}::handle_text");
IRC::add_print_handler("Channel Action Hilight",   "${pkg}::handle_text");
IRC::add_print_handler("Channel Message",   "${pkg}::handle_text");
IRC::add_print_handler("Channel Msg Hilight",   "${pkg}::handle_text");
IRC::add_print_handler("Channel Notice",   "${pkg}::handle_text");
IRC::add_print_handler("Generic Message",   "${pkg}::handle_text");
IRC::add_print_handler("Your Message",   "${pkg}::handle_text");
IRC::add_print_handler("Kick",   "${pkg}::handle_text");
IRC::add_print_handler("Killed",   "${pkg}::handle_text");
IRC::add_print_handler("Motd",   "${pkg}::handle_text");
IRC::add_print_handler("Notice",   "${pkg}::handle_text");
IRC::add_print_handler("Part with Reason",   "${pkg}::handle_text");
IRC::add_print_handler("Private Message",   "${pkg}::handle_text");
IRC::add_print_handler("Private Message to Dialog",   "${pkg}::handle_text");
IRC::add_print_handler("Quit",   "${pkg}::handle_text");
IRC::add_print_handler("Receive Wallops",   "${pkg}::handle_text");
IRC::add_print_handler("Server Notice",   "${pkg}::handle_text");
IRC::add_print_handler("Server Text",   "${pkg}::handle_text");
IRC::add_print_handler("Topic Change",   "${pkg}::handle_text");
IRC::add_print_handler("Topic",   "${pkg}::handle_text");

Xchat::hook_command("bina", "${pkg}::ascii_bin");

sub checkbin {

	$bb = 0;
	$e = "";
	$c = "";

	for $e (@tx) {
		$c = ord($e);
		if ($c != 49 && $c != 48 && $c != 32 && $c != 126 && $c != 64) { $bb = 1; }
	}
	if ($#tx < 6) { $bb = 1; }
	return $bb;
}

sub handle_text {
	
	@txx = split(/ /,Xchat::strip_code($_[0]));
	$tes = "";
	$testo = "";
	$r = 0;
	$nick = @txx[0];
	for ($r = 1; $r <= $#txx; $r++) {
		$testo = $testo . @txx[$r] . " ";	
	}
	@tx = split(//,$testo);
	$j = 0;
	$a = 0;
	@pot = ( 128, 64, 32, 16, 8, 4, 2, 1 );

	for ($i=0; $i <= $#tx; $i++) {
		if (ord(@tx[$i]) == 49) {
			$j = $j + @pot[$a];
		}
		if (ord(@tx[$i]) == 48) {
		}
		if (ord(@tx[$i]) == 32) {
			$i++;
			if ($j > 0) {
				$tes = $tes . chr($j);
			}
			$j = 0;
			$a = 0;
		}
	$a++;
	}
$tes = $tes . chr($j);
&checkbin;
if ($bb == 0) { IRC::print("\cC3$nick >> $tes"); }
}

sub checkbit {
	$grosby = 0;
	$lilli = 0;
	
	$grosby = length($gobe);
	$lilli = 8 - $grosby;
	while ($lilli > 0) {
		$gobe = "0$gobe";
		$lilli--;
	}
}

sub ascii_bin {

	@bin = split(//,Xchat::strip_code($_[1][1]));
	my $channel = Xchat::get_info("channel");
	$sad = "";
	$grim = 0;
	$lesb = 0;
	$pirro = "";
	$par = 0;
	$gobe = "";
	
	for $sad (@bin) {
		$gobe = "";
		$grim = ord($sad);
		while ($grim > 0) {
			$lesb = $grim;
			$par = $lesb % 2;
			$grim = ($grim - $par) / 2;
			$gobe = "$par$gobe";
		}
	&checkbit;
	$pirro = "$pirro $gobe";
	}
	$pirro =~ s/^\s+//;
	$pirro =~ s/\s+$//;
	Xchat::command("msg $channel $pirro");
}

1;
#end
