#!/usr/bin/perl -w

use Net::IRC;
use strict;


my $bot_name = "pocitadlo";
my $channel = "#linux.cz";
#my $channel = "#pepca.test";

my @joke_list = (
	"Co to znamena kdyz ctyri nesou rakev? Bylo nas pet.",
	"Manzelka se v noci nahle probudi a lomcuje s manzelem: \"Milacku, strasne bych se chtela milovat.\" \"A koho ja ti sezenu, ted - ve dve v noci ???\"",
	"Vis z ceho vznikl clovek? Ze svestky. Ze svestky je slivovice ze slivovice je opice a z opice je clovek.",
	"Je rybar na brehu rybnika a chyta ryby. Najednou vykoukne z vody krokodyl a pta se: Berou? Rybar na to: Ne! Krokodyl: Tak kasli na ryby a pojd se vykoupat.",
	"Prijde blazen do obchodu a povida: \"Mate barevne televize?\"; \"Ano, mame. Hodne druhu.\"; \"Tak mi dejte jednu oranzovou.\"",
	"Tak dlouho do lesa volal, az prisel hajny a dal mu na hubu.",
	"Pepicek sedi ve skole a nic se mu nedari. Pan ucitel mu na konci hodiny rekne: \"Bez, Pepicku, a kup si kilo rozumu!\" Pepicek na to: \"A mam rict, ze je to pro vas?\"",
	"Jde mala holcicka Evicka lesem a najednou z krovi vyskoci uchylak a zacne z Evicky strhavat saticky. A Evicka se jenom smala, protoze vedela, ze mu budou male...",
	"Na lavicce v parku sedi parek a liba se. Prisedne chlap a hladi zenu po kolenou. Mladik se na nej obrati s tim, co si to dovoluje. \"Chtel jsem jen manzelce rici, aby mi dala klice.\"",
	"Jak svadi malomocne devce chlapce? Haze po nem oci.",
	"Smrtka stoji u dveri bytu a povida: \"Dobry den, odcitani lidu.\"",
	"Sedi blondynka na posteli drzi vibrator a rika: \"Netres se ja to delam taky poprve!\"",
	"Stojej takhle dva kosmonauti na Marsu a cumej stridave na sebe na Zemi na raketu, na Zemi na sebe na raketu a furt dokola. Az to jeden nevydrzi a vybuchne: \"Co cumis na mne? Ja jsem klicky nezabouchl!\"",
	"\"Milacku, dneska jsem projela tri krizovatky na cervenou, ale nikdo me nezastavil a nepokutoval.
Proto jsem si za usetrene penize koupila kloboucek...\""
);

my @bad_list = (
	"to nevim ;)",
	"oops...",
	"... a tu o cerveny karkulce znas ?"
);

# create the IRC object
my $irc = new Net::IRC;

# Create a connection object.  You can have more than one "connection" per
# IRC object, but we'll just be working with one.
my $conn = $irc->newconn(
	Server 		=> shift || 'irc.felk.cvut.cz',
	# Note: IRC port is normally 6667, but my firewall won't allow it	
	Port		=> shift || '6667', 
	Nick		=> $bot_name,
	Ircname		=> 'Pepcuv pocitajici bot',
	Username	=> 'pocitadlo'
);

# We're going to add this to the conn hash so we know what channel we
# want to operate in.
$conn->{channel} = shift || $channel;

sub on_connect {

	# shift in our connection object that is passed automatically
	my $conn = shift;
  
	# when we connect, join our channel and greet it
	$conn->join($conn->{channel});
	$conn->privmsg($conn->{channel}, 'Dobrej den. ;)');
	$conn->{connected} = 1;

}

sub on_join {

	# get our connection object and the event object, which is passed
	# with this event automatically
	my ($conn, $event) = @_;

	# this is the nick that just joined
	my $nick = $event->{nick};

	if ($nick eq "pepca") {
		$conn->privmsg($conn->{channel}, "$nick: Ahoj!");
	}
}
	
sub on_public {

	my ($conn, $event) = @_;
	my $s = "";
	my $text = $event->{args}[0];
	my $nick = $event->{nick};
	my $result = "";
	my $bad = "";
	my $joke = "";
	glob @bad_list;

	if($text =~ /^$bot_name:.*/) {
		if ($text =~ /.*smut.*/) {
			$joke = @joke_list[rand(@joke_list)];
			$conn->privmsg($conn->{channel}, "$nick: $joke");
		} else {
			$text =~ s/$bot_name:\s*//i;
			$text =~ s/\'//g;
			$text =~ s/while//gi;
			$text =~ s/for//gi;
			$text =~ s/goto//gi;
			$text =~ s/^\s*(.*)\s*$/$1/;
			
			$result = `calc -m 0 -p '$text'`;
			$result =~ s/^\s*(.*)\s*$/$1/;

			if ($text eq "" or $result eq "" or $result =~ /^Error.*/) {
				$bad = @bad_list[rand(@bad_list)];
				$conn->privmsg($conn->{channel}, "$nick: $bad");
			} else {
				$conn->privmsg($conn->{channel}, "$nick: \"$text\" = $result");
			}
		}
	}

}


# add event handlers for join and part events
$conn->add_handler('join', \&on_join);

$conn->add_handler('public', \&on_public);

# The end of MOTD (message of the day), numbered 376 signifies we've connect
$conn->add_handler('376', \&on_connect);

# start IRC
$irc->start();
