package KikooLol;

use POE::Component::IRC::Plugin qw( :ALL );
use LWP::Simple;
use helpers;

my $annoying = 1;
my $victim = "";

sub new {
    my $package = shift;
    return bless {}, $package;
}

sub PCI_register {
    my ( $self, $irc ) = splice @_, 0, 2;
    $irc->plugin_register( $self, "SERVER", qw(public) );
    return 1;
}

sub PCI_unregister {
    return 1;
}

sub S_public {
    my ( $self, $irc ) = splice @_, 0, 2;
    my $who = ${ $_[0] };
    my $nick = ( split /!/, ${ $_[0] } )[0];
    my $mask = (split /@/, ${ $_[0] } )[1];
    my $channel = ${ $_[1] }->[0];
    my $msg = ${ $_[2] };

    # pour gapz !!!
    if ($msg =~ /^u-bot.\s+!!!/) {
        $irc->yield(privmsg => $channel => "$nick: !!!");
	return PCI_EAT_PLUGIN;
    }

    # repeat what the victim says
    if (($msg =~ /^!cc on /) and helpers::is_admin ($who)) {
        @t = split / /, $msg;
        $victim = @t[2];
        print STDERR "(KikooLol) cc ON: victim => $victim\n";
        if ($victim) { $annoying = 1; }
        return PCI_EAT_PLUGIN;
    }
    if (($msg =~ /^!cc off/) and helpers::is_admin ($who)) {
        print STDERR "(KikooLol) cc OFF: victim => $victim\n";
        $victim = "";
        $annoying = 0;
        return PCI_EAT_PLUGIN;
    }
    if (($annoying) and ($victim eq $nick)) {
        $irc->yield(privmsg => $channel => "$nick: $msg");
        return PCI_EAT_PLUGIN;
    }

    # wtf
    if ($msg =~ /^!wtf /) {
        my $found = 0;
        my @t = split / /, $msg;
        my $acro = @t[1];
        open F, "data/wtf.data";
        while (<F>) {
            @tab = split /\t/, $_, 2;
            if (@tab[0] =~ /^$acro$/i) {
                $irc->yield (privmsg => $channel => "$nick: @tab[0] -> @tab[1]");;
                $found = 1;
                last;
            }
        }
        close (F);
        if (not $found) {
            $irc->yield (privmsg => $channel => "$nick: aucune idée...");
        }
        return PCI_EAT_PLUGIN;
    }
    if (($msg =~ /^!wtfadd /) and (helpers::is_admin ($who))) {
        my $found = 0;
        my @t = split / /, $msg, 3;
        my $acro = @t[1];
        my $def = @t[2];
        chomp $def;
        open F, "data/wtf.data";
        while (<F>) {
            @tab = split /\t/, $_, 2;
            if (@tab[0] =~ /^$acro$/i) { $found = 1; last; }
        }
        close F;
        if (($def) and (not $found)) {
            open F, ">> data/wtf.data";
            print F "@t[1]\t@t[2]\n";
            close F;
            $irc->yield (privmsg => $channel => "$nick: $acro ajouté");
        }
        return PCI_EAT_PLUGIN;
    }
    return PCI_EAT_NONE;
}

1;
