#!/usr/bin/perl 

# Simple IRC Bot # 
# Coded By Kiki - federico.alice@tiscali.it 
# http://geekmind.altervista.org
# Greatz to all channerl #perl.it on irc.freenode.net 
# Thx to dree

use strict; 
use warnings; 
use Net::IRC; 
use IO::Socket; 
use Socket; 

print "--------------------------------\n"; 
print "Simple IRC Bot\n"; 
print "Coded by Kiki\n"; 
print "federico.alice[at]tiscali[dot]it\n"; 
print "http://geekmind.altervista.org\n";
print "--------------------------------\n"; 

my $irc = new Net::IRC; 

my $conn = $irc->newconn( 
Server =>  shift ||'irc.azzurra.org', 
Port => '6667', 
Nick => 'KikiBot',                #change that for the bot's Name 
Ircname => 'Little Kiki',         #change that for Real Name 
Username => "bot",                #change that for Username 
);

my @frasi = (
   
   "Eccomi",
   "Sono qui",
   "Che c'Ã¨?",
   "Cosa vuoi?",
   "Ti serve qualcosa?",
   "Hai bisogno?",
   "Mi hai chiamato?",
   "Et voilÃ ",
);

$conn->{channel} = shift || '#0x90';

$conn->add_handler('join', \&on_join); 
$conn->add_handler('part', \&on_part); 
$conn->add_handler('public', \&on_public);
$conn->add_handler('376', \&on_connect); 

$irc->start();

sub on_connect { 

    my $conn = shift; 
   
        $conn->join($conn->{channel});
	$conn->privmsg($conn->{channel}, 'Hello everyone!');
	$conn->{connected} = 1;
} 

sub on_join { 

    my ($conn, $event) = @_; 

    my $nick = $event->{nick}; 
    $conn->privmsg($conn->{channel}, "Ciao a tutti!"); 
} 

# Le frasi a cui risponde

sub on_public {
    my ($self, $event) = @_;
    my @to = $event->to;
    my ($nick, $mynick) = ($event->nick, $self->nick);
    my ($arg) = ($event->args);

    if ($arg =~ /Ciao $mynick/i) {     
	$self->privmsg([ @to ], "Ciao $nick!!");     
    }

    elsif ($arg =~ /come stai $mynick/i) {
	$self->privmsg([ @to ], "Bene $nick, e tu?");
    }

    elsif ($arg =~ /che fai $mynick/i) {
	$self->privmsg([ @to ], "Niente, lavoro per Kiki");
    }

    elsif ($arg =~ /di dove sei $mynick/i) {
	$self->privmsg([ @to ], "Abito al 127.0.0.1");
    }

    elsif ($arg =~ /warez/i) {
          $self->privmsg([ @to ], "Niente warez qua");
    }
    # quit

    elsif ($arg =~ /Esci $mynick/i) {      
	$self->quit("Born to be r00t!!"); 
	exit 0;
    }

    elsif ($arg =~ /$mynick/i) {
	$self->privmsg([ @to ], fraserandom());
    }

}


sub on_part { 
     
    my ($conn, $event) = @_; 

    my $nick = $event->{nick}; 
    $conn->privmsg($conn->{channel}, "Goodbye, $nick!"); 
}

sub fraserandom {

	my $elemento = $frasi[ rand @frasi ];
	return $elemento;
}
