use strict;
use warnings;
use Xchat qw(:all);
use Data::Dumper ();

my $window_name = "-RAW LOG-";

register( "Raw Inbound", "1.0", "Print out inbound messages in hex" );

hook_server( "RAW LINE", sub {
	unless( find_context $window_name, get_info "server" ) {
		commandf 'QUERY -nofocus "%s"', $window_name;
	}
	set_context $window_name;
	
	my $index;

	for my $i ( 1 .. $#{$_[0]} ) {
		if( $_[0][$i] =~ /^:/ ) {
			$index = $i;
			$_[1][$i] = Data::Dumper->new( [ $_[1][$i] ] )
				->Terse(1)
				->Useqq(1)
				->Indent(0)
				->Dump;
			last;
		}
	}

	if( $index ) {
		prnt join " ", @{$_[0]}[ 0 .. $index - 1], $_[1][$index];
	} else {
		prnt join " ", @{$_[0]};
	}


	return EAT_NONE;
});
