use Irssi;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "0.0.1";

%IRSSI = (
    authors     =>  "Sebastian Groeneveld",
    contact     =>  "dracuul78\@hotmail.com",
    name        =>  "citaat",
    description =>  "Triggers on !citaat and makes nebot generate a quote",
    license     =>  "GPL v2 and any later",
);

#
#
sub sig_message_ownpublic {
    my ($server, $msg, $target) = @_;

    check_citaat($server, $target, $msg);
}

#
#
sub sig_message_public {
    my ($server, $msg, $nick, $address, $target) = @_;

    check_citaat($server, $target, $msg);
}

#
#
sub check_citaat {
    my ($server, $target, $msg) = @_;
 
    # only trigger on pattern "!citaat" in channel #dingetje 
    return unless $msg =~ '^!citaat$';    
    return unless $target =~ '#dingetje';

    # don't do anything if HaBot is present
    my $channel = $server->channel_find($target);
    return if $channel->nick_find_mask("Ha|Bot*!*ha@*");

    # send da magic command to nebot
    outmsg( $server, "nebot", "quote" );
}   

#
# send message to specified nick
#
sub outmsg {
    my ($server, $target, $msg) = @_;
    
    # method 1 
    $server->send_message($target, $msg, 1);

    # method 2
    # $server->send_raw("PRIVMSG $target :$msg");
    
    # method 3
    # $server->command("MSG $target $msg");
}

#
# Install signal handlers
# 
Irssi::signal_add_first( 'message public',     \&sig_message_public );
Irssi::signal_add_first( 'message own_public', \&sig_message_ownpublic );