#!/usr/local/bin/perl

# - Patrick -
# 
#

#use strict;
use IO::Socket;

#
# config:
#
$nick = "eb";
$username = "eb";
$ircname = "eb, the talking user :-)";

$ircclient = "virginie.toone.org";
$ircserver = "192.168.0.1";

#
# code
#
my $esc = sprintf("%c",1);
my $esca = sprintf("%cACTION",1);

my $dessus = 0;

my $oSocket = new IO::Socket::INET->new (
	'proto' => 'tcp',
	'PeerAddr' => $ircserver,
	'PeerPort' => 6667
	);

sub SendServer { 
	my $sLine = shift;
	print "\e[1;31m-->>".$sLine."\e[1;0m\n";
	print $oSocket $sLine . "\n";
	}

my $parBind = {
	'PING' => sub {
	my $sLine = shift;
	SendServer('PONG '.(split(/\s+/,$sLine,2))[1]);
	},
	'MODE' => sub {
	my $sLine = shift;
	if (! $dessus) {
		SendServer('JOIN #hackers');
		SendServer('PRIVMSG #hackers :plop');
		print "that should be ok\n";
		$dessus = 1;
		}
	},
	'PRIVMSG' => sub {
		my $sLine = shift;
		my $sGtmp = (split "!", $sLine)[0];
		my $sGens = (split ":", $sGtmp)[1];
		my $sContent = (split(':',$sLine,3))[2];
		if ($sContent =~ "plop") {
SendServer ('PRIVMSG #hackers :plop');
SendServer ("PRIVMSG #hackers :sGens: $sGens, sContent: $sContent");
			}

		if ($sContent =~ "uptime") {
			$upt = `/usr/bin/uptime`;
			chop($upt);
SendServer ("PRIVMSG #hackers :Whaou ! mon uptime est de:");
SendServer ("PRIVMSG #hackers :$upt");
			}

		if ($sContent =~ "crypt") {
			$pass = crypt("blah",$sContent);	
			SendServer("PRIVMSG #hackers :$pass");
			}
	}
};

my $parBaseCommand = {
'PING' => 1,
'PONG' => 1
};

SendServer ("USER $username $ircclient $ircserver :$ircname");
SendServer ("NICK $nick");
SendServer ("MODE $nick +k");

while ( my $sLine = <$oSocket> ) {
	print "\e[1;32m<<-- ".$sLine."\e[1;0m";
	chomp ($sLine);
	$sLine =~ s/\r//g;
	my $sCommand = (split (/\s+/, $sLine, 2))[0];
	$sCommand =~ s/^://;
	$sCommand =~ tr/[a-z]/[A-Z]/;
	if (! $parBaseCommand->{$sCommand} ) {
		$sCommand = (split(/\s+/,$sLine,3))[1];
		$sCommand =~ tr/[a-z]/[A-Z]/;
		}
	if (defined ($parBind->{$sCommand})){
		&{$parBind->{$sCommand}}($sLine);
		}
	} 

