#!/usr/local/bin/perl

# Module that translates the username from hex to a human readable IPv4 address.
# This type of username is commonly used by webbased IRC clients like mibbit and CGI:IRC.

# These are the variables passed to each function call.
#
# $_[0] = name for this module (surely you already know this, but still)
# $_[1] = sending nickname
# $_[2] = sending username
# $_[3] = sending hostmask
# $_[4] = sending channel (or botnick in case of PM)
# $_[5] = current value for $modchan
# $_[6] = current value for $botnick
# $_[7] = first argument given in IRC
# $_[8] = second argument given in IRC etc.

# The package name must match the filename + .pm, so this file should be called "webchat.pm".
package webchat;

# sub public {} is a special name, this function should contain an array of publicly available function names.
# When this subroutine is not implemented, all functions will only be available to bot opers.
sub public {
	@public = ("help", "dehex");
}

# sub help {} is a special name, if you implement this subroutine it will be called when your module is called with no parameters.
# Implementing the mesg subroutine is optional, but highly reckomended.
sub help {
	nanobot::snd("NOTICE $_[1] :Commands: help, dehex");
}

sub dehex {
	if(length ($_[7]) == 8) {
		eval {
			$first = substr($_[7], 0, 2);
			$second = substr($_[7], 2, 2);
			$third = substr($_[7], 4, 2);
			$fourth = substr($_[7], 6, 2);
		};
		
		if($@)
		{
			nanobot::snd("PRIVMSG $_[4] :Error: Could not split data.");	
		}
		
		my $result = "";
		eval {
			$result = hex($first) . ".";
			$result = $result . hex($second) . ".";
			$result = $result . hex($third) . ".";
			$result = $result . hex($fourth);
		};
		
		if($@)
		{
			nanobot::snd("PRIVMSG $_[4] :Error: Could not read hex data.");
		}
		                        
		nanobot::snd("PRIVMSG $_[4] :IP: $result");
	} else {
		nanobot::snd("PRIVMSG $_[4] :Wrong length, expecting 8 characters.");
	}
}

# A module must always end with the line "1;", Perl demands it.
1;
