#This script is under the GNU General Public License version 2
#Script made by Javanx in 2002, tested only on XChat 1.8.8

IRC::register ("Hex Translator", "0.5", "", ""); 
IRC::add_command_handler("hexconv", "hexconv"); 
IRC::add_command_handler("strconv", "strconv");
IRC::add_command_handler("hexsay", "hexsay");
IRC::add_message_handler('PRIVMSG', "hextrans"); 

sub hexconv
{
	@string = unpack("C*", $_[0]);
	$outstr="HEX: ";

	foreach $vowel (@string) {
		$temp = sprintf("\\x%x", $vowel);
		$outstr = $outstr . $temp;
	}
	IRC::print ($outstr);
	return 1;
}

sub hexsay
{
	@string = unpack("C*", $_[0]);
	$outstr="";

	foreach $vowel (@string) {
		$temp = sprintf("\\x%x", $vowel);
		$outstr = $outstr . $temp;
	}
	IRC::command ($outstr);
	return 1;
}

sub strconv
{
	$hexstring = $_[0];
	$hexstring =~ s/\\x/ /g;

	@hexnums = split (/ /, $hexstring);
	$hexnums[0] = "20";
	$outstr="STR:";

	foreach $vowel (@hexnums) {
		$temp = hex($vowel);
		$temp = sprintf("%c", $temp);
		$outstr = $outstr . $temp;
	}

	IRC::print($outstr);
	return 1;
}

sub hextrans
{
	@command = split (/ /, $_[0]);
	$command[3] =~ s/://;
	
	if($command[3]=~/\\x/) {
		$hexstring = $command[3];
		$hexstring =~ s/\\x/ /g;

		@hexnums = split (/ /, $hexstring);
		$hexnums[0] = "20";
		@sourceinfos = split(/!/, $command[0]);
		$sourceinfos[0] =~ s/://;
		$outstr="<" . $sourceinfos[0] . "/" . $command[2] . ">";

		foreach $vowel (@hexnums) {
			$temp = hex($vowel);
			$temp = sprintf("%c", $temp);
			$outstr = $outstr . $temp;
		}

		IRC::print($outstr);
		return 1;
	}
}
