#!/usr/bin/perl

#    =====================================================================
#    |                   SPALEWARE LICENSE (Revision 0.1)                |
#    |-------------------------------------------------------------------|
#    | This is a little script called "Easy-Bounce" and is               |
#    | licensed under SPALEWARE. You may freely modify and distribute    |
#    | this package or parts of it. But you MUST keep the SPALWARE       |
#    | license in it!                                                    |
#    |                                                                   |
#    =====================================================================


$ircserv = "1.2.3.4";
$ircport = "6667";
$vhost   = "2.3.4.5";
$goconnect = '1';
$nick = "FooBar";
$pass = "I_line_pass";
$user = "user user user :RealName";

use IO::Socket;
$port = "65000"; # listen port
$server = IO::Socket::INET->new(Proto=>'tcp',LocalPort=>$port,Listen=>'1',Reuse=>1);
print "Listening on port $port\n";
while ($client = $server->accept()) {
	while(1) {
		$ruser = $rserv = '';
		vec($rserv,fileno($serv),1) = 1;
		vec($ruser,fileno($client),1) = 1;

		if ( select($rserv, undef, undef, 0.01) ) {
			$touser = <$serv>;
			if($touser) {
				print $client $touser . "\n";
				print "<- $touser";
				if (  $touser =~ /^ERROR :Closing Link: /i ) { exit; }
			}
		}
		if ( select($ruser, undef, undef, 0.01) ) {
			$toserv = <$client>;
			if ( $toserv =~ /^quit /i ) { exit; }
			if ( $toserv =~ /^user /i ) { $toserv =''; }
			if ( $goconnect ) { connectirc($ircserv,$ircport,$vhost); $goconnect = '0'; }
			if ( $connected ) {
				if ( $goconnect eq "0" ) { 
					print $serv "PASS $pass\n";
					print $serv "USER $user\n";
					print $serv "NICK $nick\n";
					$goconnect = '';
					print "-> PASS $pass\n";
					print "-> USER $user\n";
					print "-> NICK $nick\n";
				} elsif ($toserv) {
					print $serv $toserv;
					print "-> $toserv";
				}
			}
		}
	}
}

sub connectirc {
	$remoteip = shift;
	$remoteport = shift;
	$srcip  = shift;
	print "Connecting...\n";
	$serv = IO::Socket::INET->new(PeerAddr=>$remoteip,PeerPort=>$remoteport,Proto=>'tcp',LocalAddr=>$srcip)||die "Unable to connect to IRC";
	print "Connected to $remoteip - $remoteport\n";
	$connected = '1';
}
