#!/usr/bin/perl 

#
# this pile of junk is like an emotion bot for the channel
# it is not even close to being done
# -emotion-
# 
#

use Net::IRC;
#use strict;
use Data::Dumper;
use DBI;
use DBD::mysql;
use Class::Date qw(date); 

srand(time ^ %%);

$host = 'localhost';
$db = 'david';
$db_user = 'select';
$db_pwd = 'insert';

$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";

$irc = new Net::IRC;
$conn = $irc->newconn(
	Server 		=> shift || 'irc.efnet.net',
	Port		=> shift || '6667',
	Nick		=> 'herrfreud',
	Ircname		=> 'emoisgay',
	Username	=> 'emoisgay'
);
#$conn->{channel} = shift || '#dillsmack';
$conn->{channel} = shift || '#lagparty';

sub on_connect {
	$conn = shift;
	$conn->join($conn->{channel}, "password");
}

$clock = time() + 60;

sub on_public {
	($conn, $event) = @_;
	$text = $event->{args}[0];
	$nick = $event->{nick};
	if(time() >= $clock) {
	    $clock = time() + 60;
	    if($hour == 60) {
		#	print "do hourly functions\n";
		$hour = 0;
	    } elsif($ten == 10) {
		#	print "print ten-minuteunctions\n";
		$ten = 0;
	    } elsif($five == 5) {
		$stamp = time() - 3600;
		$sql = "SELECT COUNT(emotion) FROM freud WHERE unix_ts>\"$stamp\" AND emotion REGEXP \"fuka|angry|pissed|mad|pms\";";
		$sth = $dbh->prepare($sql) or die "preparing: ",$dbh->errstr;
		if(!$sth->execute) {
		    $dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
		    $sth->execute or die "executing: ", $dbh->errstr;
		}
		($rows) = $sth->fetchrow_array();
		if(!$anger == 1) {
		    if($rows > 3) {
			$conn->privmsg($event->{to}[0], "it seems there is some anger");
		    }
		} else {}
		$anger = 1;
		$five =0 ;
	    } else {
		#	print "print minuteunctions\n";
	    }
	    $hour++;
	    $ten++;
	    $five++;
	} else {	    
	    # tick
	}
	if($text =~ /sex/) {
	    $log["$nick"]["sex"]++;
	}
	if ($text =~ /^\-(\w+)\-/) {
	    $irctime = "00:00";
	    save_emotion($nick,"",$1,$irctime,0);
	    $stamp = $stamp - 21600;
	    $s_nick = $nick =~ tr/A-Z/a-z/;
	    $dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
	    $sql = "SELECT COUNT(emotion) FROM freud WHERE nick=\"$s_nick\" AND emotion=\"$1\" AND unix_ts>\"$stamp\";";
	    $sth = $dbh->prepare($sql) or die "preparing: ",$dbh->errstr;
	    if(!$sth->execute) {
		$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
		$sth->execute or die "executing: ", $dbh->errstr;
	    }
	    ($rows) = $sth->fetchrow_array();
	    if($rows > 1) {
		$conn->privmsg($event->{to}[0], "$nick is getting more $1");
	    }
	} elsif($text =~ /^!freud\s+(.*)/) {
	    $stamp = time();
	    $stamp = $stamp - 21600;
	    $nick = $1;
	    $nick =~ s/\s+$//;
	    $s_nick = $nick;
	    $s_nick =~ tr/A-Z/a-z/;

	    determine_emotion($s_nick,$stamp);

	    if(!$mostly) {
      			$conn->privmsg($event->{to}[0], "$nick is unfeeling");
		    } elsif(!$less) {
      			$conn->privmsg($event->{to}[0], "$nick is $mostly");
		    } else {
			$r = int(rand(4));
			if($r == 0) {
			    $conn->privmsg($event->{to}[0], "apparently $nick is $mostly and a little $less");
			} elsif($r == 1) {
			    $conn->privmsg($event->{to}[0], "well, it seems $nick is quite $mostly and a bit $less");
			} elsif($r == 2) {
			    $conn->privmsg($event->{to}[0], "i believe $nick is a bit $less, but predominantly $mostly");
			} elsif($r == 3) {
			    $conn->privmsg($event->{to}[0], "$nick\'s problems stem from being $mostly and $less");
			} elsif($r == 4) {
			    $conn->privmsg($event->{to}[0], "i guess $nick is $mostly and $less");
			}

		    }
	} elsif($text =~ /die/) {
	    #die;
	}
	undef %emotions;
	undef @emo;
}


sub on_msg {
	($conn, $event) = @_;
	$nick = $event->{nick};
	$text = $event->{args}[0];
	if($text =~ /<-/) {
	    ($target, $emo) = $text =~ /(.+)<-(.+)$/;	
	    $irctime = "00:00";
	    $stamp = time() - 21600;
	    $dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
	    $sql = "SELECT COUNT(emotion) FROM freud WHERE nick=\"$target\" AND emotion=\"$emo\" AND caller=\"$nick\" AND unix_ts>\"$stamp\";";
	    $sth = $dbh->prepare($sql) or die "preparing: ",$dbh->errstr;
	    if(!$sth->execute) {
		$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
		$sth->execute or die "executing: ", $dbh->errstr;
	    }
	    ($rows) = $sth->fetchrow_array();
#	    print "sql: $sql\n";
#	    print "rows: $rows[0]\n";
	    if($rows >= 1) {
		$conn->privmsg($event->{nick}, "herr doctor already knows this");	
	    } else {
		$conn->privmsg($event->{nick}, "$nick says $target is $emo");
		save_emotion($target,$nick,$emo,$irctime,0);
	    }
	}    
}

sub on_notice {
	($conn, $event) = @_;
}


sub default {
	($conn, $event) = @_;
	print Dumper($event);
}
	


# add handlers for our standard events
$conn->add_handler('public', \&on_public);
$conn->add_handler('msg', \&on_msg);
$conn->add_handler('notice', \&on_notice);
$conn->add_handler('376', \&on_connect);

$conn->add_handler('cping', \&default);

$irc->start;

sub determine_emotion {
    ($s_nick, $stamp) = @_;
    $dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
    $sql = "SELECT emotion,decay FROM freud WHERE nick=\"$s_nick\" AND unix_ts>\"$stamp\" ORDER BY eid;";
#    print "sql: $sql\n";
    $sth = $dbh->prepare($sql) or die "preparing: ",$dbh->errstr;
    if(!$sth->execute) {
	$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
	$sth->execute or die "executing: ", $dbh->errstr;
    }	    
    
    while (@row = $sth->fetchrow_array()) {
	if($row[1] == "0" ) {
	    $emotions{"$row[0]"}++;
	} else {
	    $emotions{"$row[0]"}--;
	}
    }
    foreach $value (sort {$emotions{$b} cmp $emotions{$a} }
		    keys %emotions)
    {
	push(@emo, $value);
    }
    $mostly = shift(@emo);
    $less = shift(@emo);    
}

sub save_emotion {
    ($nick,$caller,$emotion,$irctime,$decay) = @_;
    $stamp = time();
    $emotion =~ tr/A-Z/a-z/;
    $nick =~ tr/A-Z/a-z/;
    $caller =~ tr/A-Z/a-z/;
    if(!$caller == "") {
	$sql = "INSERT INTO freud(nick,emotion,caller,decay,irc_ts,unix_ts) values(\"$nick\",\"$emotion\",\"$caller\",\"$decay\",\"00:00\",\"$stamp\");"
    } else {
	$sql = "INSERT INTO freud(nick,emotion,irc_ts,decay,unix_ts) values(\"$nick\",\"$emotion\",\"$decay\",\"00:00\",\"$stamp\");";
    }
$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
    $sth = $dbh->prepare($sql) or die "preparing: ",$dbh->errstr;
    if(!$sth->execute) {
	$dbh = DBI->connect( "DBI:mysql:$db:$host", $db_user, $db_pwd) or die "Connecting : $DBI::errstr\n ";
	$sth->execute or die "executing: ", $dbh->errstr;
	}
    return 1;
}
