#!/usr/bin/perl
#
# quick'n'dirty, versions uncounted 
#

###############################################################################
#
# Defaults:
#
## Connection
$srva = "irc.striked.org";
#$srva = "irc.efnet.net";
$srvb = "irc.i.cz";
$port = "6667";
$targchan = "#iraqlive.fox";

###############################################################################
#
# If you are not interested in code itself, you should stop here.
#
###############################################################################
#
# Details:
use Socket;
use IO::Handle;
$debug=0;

my $SOCKA = IO::Handle->new();
$SOCKA->autoflush(1);
my $SOCKB = IO::Handle->new();
$SOCKB->autoflush(1);
$|=1;

$iaddra   = inet_aton($srva) || die "Server not found: $srva";
$paddra   = sockaddr_in($port, $iaddra);
$iaddrb   = inet_aton($srvb) || die "Server not found: $srvb";
$paddrb   = sockaddr_in($port, $iaddrb);
$proto   = getprotobyname('tcp');

print "connect to $srva\n";
socket($SOCKA, PF_INET, SOCK_STREAM, $proto) || die "Cannot create socket: $!";
connect($SOCKA, $paddra) || die "Cannoct connect: $!";

print "connect to $srvb\n";
socket($SOCKB, PF_INET, SOCK_STREAM, $proto) || die "Cannot create socket: $!";
connect($SOCKB, $paddrb) || die "Cannoct connect: $!";

sub docon {
	my ($SOCK,$ornick) = @_;
	my $nick = $ornick;
	print "logging in..\n";
	print $SOCK "USER forw forw forw :FoxNews relay from #livenews\@irc.striked.org\015\012";
	print $SOCK "NICK $nick\015\012";

	while (defined($IN=<$SOCK>)) {
		chomp($IN);
		print $IN."\n";
		@input=split(/ /, $IN);
		if ($input[0] eq "PING") {
			print $SOCK "PONG $input[1]\015\012";
		} elsif ($input[1] =~ /^\d{3}$/) {
			if ($input[1] == 433) { $nick=rand_nick($ornick); print $SOCK "NICK $nick\015\012";
			} elsif ($input[1] == 376) {
				last;
			}
		} elsif ($input[1] eq "QUIT") {
			print "failed (@input)\n"; exit;
		}
	}
}
print "$srva ";
docon($SOCKA,"ct"); print $SOCKA "JOIN #livenews\n";
#docon($SOCKA,"ct"); print $SOCKA "JOIN #fox-live\n";
print "$srvb ";
docon($SOCKB,"ct"); print $SOCKB "JOIN $targchan\n";

use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);

$flags = fcntl($SOCKB, F_GETFL, 0)
	or die "Can't get flags for the socket: $!\n";

$flags = fcntl($SOCKB, F_SETFL, $flags | O_NONBLOCK)
	or die "Can't set flags for the socket: $!\n";


print "up and running...\n";
$bf = "";
while (1) {
  $IN=<$SOCKA>;
 $crap=<$SOCKB>;
  chomp($crap);
  if (! $IN) { last; }
  chomp($IN);
  if (! $IN) { next; }
  @input=split(/ /, $IN);
  @crap=split(/ /, $crap);
  if ($input[0] =~ /^:/) { $src=shift(@input); }
  if ($input[0] eq "PING") { $input[0] = "PONG"; $o=join(" ", @input); print $SOCKA "$o\015\012"; }
  if ($crap[0] eq "PING") { $crap[0] = "PONG"; $o=join(" ", @crap); print $SOCKB "$o\015\012"; }
  if ($input[0] ne "PRIVMSG")  {next; }
#  if ($src !~ /cnn-live/) { print "src is ::$src::\n"; next; }
  $o=join(" ", @input);
  $o=~s/^[^:]*:(.*)$/$1/;
  $o=lc($o);
  #$bf = $o.".";
  # XXX this is foolish, but i was hopeless ;)
  $bf=~s/\13//g;
  $bf=~s/\xD//g;
  $bf=~s/\015//g;
  $bf=~s/\012//g;
  $bf=~s/\xA//g;
  $bf=~s/\10//g;
  if ($bf=~/\.([ "].*)?$/ or $o=~/^>>/ or length($bf)>200) {
    $bf=~s/^ *//; $bf=~s/^([^.]{2-}.?)(.*)$/$1/; $bf=ucfirst($bf); print "$bf\r\n";
    print $SOCKB "PRIVMSG $targchan :$bf\015\012";
    $bf=$2;
  }
  $bf.=$o.' ';
}
print localtime()."\n";

sub debugmsg {
#  my($msg);
#  $msg=join(" ", @_);
#  print $SOCK "PRIVMSG #debug :$msg\015\012";
#  print ">>> PRIVMSG #debug :$msg\015\012";
}

###############################################################################
#
# Generate a random nick
# @_: -nothing-
# uses (== writes to) the global variable $nick
#

sub rand_nick {
  my ($nick) = @_;
#for ($i=1; $i<9; $i++)
  { $nick.=chr(65+rand(31)); }
  return $nick;
}


###############################################################################
#
# member of an array?
#

sub member {
  my($pos, $f);
  $f=shift;
  for ($pos=0; $pos<@_; $pos++) { if (uc( $_[$pos] )eq uc $f) { return $pos; } }
  debugmsg("-- $f not a member of @_");
  return -1;
}

###############################################################################
#
# regexp member of an array?
#

sub rmember {
  my($pos, $f);
  $f=shift;
  for ($pos=0; $pos<@_; $pos++) { if ($_[$pos]=~/$f/i) { return $pos; } }
  debugmsg("-- $f not a member of @_");
  return -1;
}

###############################################################################
#
# No more interesting stuff :-(r)
#

