#!/usr/bin/perl -wT
use strict;
use diagnostics;
use Festival::Client;
use Local::Irclog::Bitchx;
use vars qw/$line
			$Festival
			%trans
			%emotes
			/;

#----
my $debug = "0";

my $irclog = $ENV{HOME} . '/IrcLog';
$irclog =~ /([\w\/\.-_]*)/;			# untaint %ENV
$irclog = $1;
print $irclog;
my $confdir = $ENV{HOME} . '/.bx2fest/';
$confdir =~ /([\w\/\.-_]*)/;
$confdir = $1;
my $transfile = "$confdir" . "trans.def";
if ($debug)	{ $irclog = 'IrcLog.sample'; }
my $real_irclog = $irclog . '.real';
my $host = "localhost";
my $port = "1314";
my $ascii_art = undef;
my $lastspeaker = "J_Random_Hacker";
my $newspeaker  = "J_Random_Hacker";
my $lastime = 64;
my $spydie = 0;
my $delay = 10;
unless ($debug)	{ $Festival = Festival::Client->new("$host" . ":" . "$port"); }
my $emote = 0;
#----

&load_hash("$transfile", \%trans, "\003");
&load_hash("$transfile", \%emotes, "\004");

open (IRC, "<$irclog") || die "couldn't open $irclog for reading: $!";
while ($line = <IRC>) {
	my $text = "";
	open (IRC_R, ">>$real_irclog") || warn "erp! couldn't open $real_irclog for appending: $!";
	print IRC_R "$line";
	close(IRC_R);
	&gethash($line);
	if(! %bx)	{ next }	
		if ($bx{'name'})	{ 
			$newspeaker = $bx{'name'};
		}
		if ($debug)	{
			print "Last --> $lastspeaker\n";
			print "New  --> $newspeaker\n";
		}
		if($bx{'type'} eq "regular")	{
			$bx{'content'} = &emotes($bx{'content'});
			$bx{'content'} = &trans($bx{'content'});
			if ("$bx{'content'}" =~ m/^[-\'\`_\|\/\\\s\)\(\.(?:escape)]+$/)	{
				unless (($ascii_art) and ($newspeaker eq $lastspeaker))	{
					$text = "$bx{'name'} is posting ascii art.";
					$ascii_art = "1";
				} 
			}
			else { 
				undef $ascii_art;
				if ($emote  == "1")	{
					$text = "$bx{'name'} $bx{'content'}";
					$emote = "0";
				}
				else	{
					&spydie;
					if ($spydie)	{
						$text = "$bx{'content'}";
						undef $spydie;
					}
					else	{
						$text = "$bx{'name'} says $bx{'content'}";
					}
				}
			}
			if ($debug)	{ print "$text\n"; }
		}
		elsif($bx{'type'} eq "msg_from")	{
			$text = "private from $bx{'name'}: $bx{'content'}";
			if ($debug)	{ print "$text \n"; }
		}		
		elsif($bx{'type'} eq "msg_to")	{
			$text = "private to $bx{'name'}: $bx{'content'}";
			if ($debug)	{ print "$text \n"; }
		}	
		elsif($bx{'type'} eq "me")	{
			$text = "$bx{'name'} $bx{'content'}";
			if ($debug)	{ print "$text \n"; }
		}
		elsif($bx{'type'} eq "joined")	{
			$text = "$bx{'name'} has joined $bx{'chan'}";
			if ($debug)	{ print "$text\n"; }
		}
		elsif($bx{'type'} eq "signoff")	{
			$text = "$bx{'name'} has left $bx{'chan'}\. $bx{'content'}";
			if ($debug)	{ print "$text\n"; 
			}
		}
		elsif($bx{'type'} eq "topic")	{
			$text = "the topic for $bx{'chan'} is $bx{'content'}";
			if ($debug)	{ print "$text\n"; }
		}
		$text = &garbage($text);
		if ($bx{'name'})	{
			$lastspeaker = $bx{'name'};
		}
		if ($debug)	{ 
			print "Final==> $text\n<|===================================|>\n";
		}
		else	{
			$Festival->say($text);
		}
}

sub load_hash	{
	my $filename = $_[0];
	my $hashref = $_[1];
	my $slash = $_[2];
	my ($key, $val);
	if ($debug) { 
		print "Filename ==> $filename\nHash ==> $hashref\n";
	}
	open (FILE, "$filename") or die "Can't open $filename: $!\n";
	while (<FILE>)	{
		chomp;
		if ($_ !~ /^[\#]/)	{
			if ($_ =~ /$slash/)	{
				($key, $val) = split(/$slash/);
				$$hashref{$key} = $val;
				if ($debug)	{
					print "Key ==> $key\nVar ==> $$hashref{$key}\n";
				}
			}
		}
	}
	return %$hashref;
	close (FILE);
}

sub emotes	{
	my $content = $_[0];
	my ($key, $val);
	while (($key, $val) = each %emotes)	{
		if ($content =~ s/^\s+?$key/$val/)	{
			$emote = "1";
		} 
		$content =~ s/$key/$val/g;
	}
	return $content;
}

sub trans	{
	my $content = $_[0];
	my ($key, $val);
	while (($key, $val) = each %trans)	{
		$content =~ s/\b$key\b/$val/ig;
	}
	return $content;
}

sub spydie	{
	my $time = time;
	my $delaytime = $lastime + $delay;
	if ($newspeaker =~ /$lastspeaker/ and $time < $delaytime)	{
		if ($debug)	{
			print "--DUP--";
		}
		$spydie = "1"
	}
	$lastime = $time;
}	
		
sub garbage {
	my $text = $_[0];
	$text =~ s/\|\|/or/g;
	$text =~ s/\|/pipe/g;
	$text =~ s/\>+/greater than/g;
	$text =~ s/\<+/less than/g;
	$text =~ s/[\"\'\[\]\_\-]//g;
	$text =~ /([\w\d ]*)/;
	$text =~ $1;
	return $text;	
}


