# LiMuBai Bot Events
# Written by Ben Reser < ben@reser.org>
# Adapted from test.pl by dennis taylor <dennis@funkplanet.com>

package LiMuBai::Events;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

require Exporter;
@ISA = qw(Exporter);

@EXPORT_OK = qw(_start _stop _default irc_001 irc_dcc_send 
                irc_disconnected irc_error irc_kick 
                irc_msg irc_public irc_ctcp_ping irc_socketerr);
%EXPORT_TAGS = (all=>[@EXPORT_OK]);
@EXPORT = @EXPORT_OK;

$VERSION = '0.1';

use LiMuBai::Globals;
use LiMuBai::Commands;
use LiMuBai::Utils;
use POE::Kernel;
use POE::Session;
use POE::Component::IRC;

# This gets executed as soon as the kernel sets up this session.
sub _start {
  my ($kernel, $session) = @_[KERNEL, SESSION];

  # Uncomment this to turn on more verbose POE debugging information.
  # $session->option( trace => 1 );

  # Make an alias for our session, to keep it from getting GC'ed.
  $kernel->alias_set( 'smileyninja' );

  # Ask the IRC component to send us all IRC events it receives. This
  # is the easy, indiscriminate way to do it.
  $kernel->post( 'limubai', 'register', 'all');

  # Setting Debug to 1 causes P::C::IRC to print all raw lines of text
  # sent to and received from the IRC server. Very useful for debugging.
  $kernel->post( 'limubai', 'connect', { Debug    => 1,
                                         Nick     => $LiMuBai::Globals::nick,
                                         Server   => $LiMuBai::Globals::server,
                                         Port     => $LiMuBai::Globals::port,
                                         Username => $LiMuBai::Globals::username,
                                         Ircname  => $LiMuBai::Globals::ircname, }
                           );
}

sub _stop {
  my ($kernel) = $_[KERNEL];

  print "Control session stopped.\n";
  $kernel->post( 'limubai', 'quit', 'The poison got me!' );
  $kernel->alias_remove( 'smileyninja' );
}


# After we successfully log into the IRC server, join a channel.
sub irc_001 {
  my ($kernel) = $_[KERNEL];

  $kernel->post( 'limubai', 'mode', $LiMuBai::Globals::nick, '+i' );
  $kernel->post( 'limubai', 'join', '#limubai' );
  $kernel->post( 'limubai', 'away', 'I MUST SAVE THE GREEN DESTINY!' );
}


sub irc_dcc_send {
  my ($nick, $port, $file, $size, $done) = @_[ARG0 .. $#_];

  printf "DCC SEND to $nick ($file): $done bytes of $size sent.   %d%%\n",
	($done / $size) * 100;
}


sub irc_disconnected {
  my ($server) = $_[ARG0];
  my ($kernel) = $_[KERNEL];
  print "Lost connection to server $server.\n";

  unless ($LiMuBai::Globals::quit) {
    # Setting Debug to 1 causes P::C::IRC to print all raw lines of text
    # sent to and received from the IRC server. Very useful for debugging.
    $kernel->post( 'limubai', 'connect', { Debug    => 1,
                                           Nick     => $LiMuBai::Globals::nick,
                                           Server   => $LiMuBai::Globals::server,
                                           Port     => $LiMuBai::Globals::port,
                                           Username => $LiMuBai::Globals::username,
                                           Ircname  => $LiMuBai::Globals::ircname, }
                           );
  }
}

sub irc_socketerr {
  my ($error) = $_[ARG0];
  my ($kernel) = $_[KERNEL];
  print "Socket Error: $error\n";

  unless ($LiMuBai::Globals::quit) {
    # Setting Debug to 1 causes P::C::IRC to print all raw lines of text
    # sent to and received from the IRC server. Very useful for debugging.
    $kernel->post( 'limubai', 'connect', { Debug    => 1,
                                           Nick     => $LiMuBai::Globals::nick,
                                           Server   => $LiMuBai::Globals::server,
                                           Port     => $LiMuBai::Globals::port,
                                           Username => $LiMuBai::Globals::username,
                                           Ircname  => $LiMuBai::Globals::ircname, }
                           );
  }
}


sub irc_error {
  my $err = $_[ARG0];
  print "Server error occurred! $err\n";
}


sub irc_kick {
  my ($who, $where, $isitme, $reason) = @_[ARG0 .. ARG4];

  print "Kicked from $where by $who: $reason\n" if $isitme eq $LiMuBai::Globals::nick;
}


sub irc_msg {
  my ($from, $to, $text) = @_[ARG0 .. $#_];
  my ($kernel) = $_[KERNEL]; 

  my ($fromnick) = LiMuBai::Utils::get_nick($from);
  print "<$fromnick> $text\n";

  LiMuBai::Commands::command_parser(@_);  
}


sub irc_public {
  my ($from, $to, $text) = @_[ARG0 .. $#_];
  my ($kernel) = $_[KERNEL];
  
  my ($fromnick) = LiMuBai::Utils::get_nick($from);
  print "<$fromnick> $text\n";

  LiMuBai::Commands::command_parser(@_);
}


# replies to ctcp pings
sub irc_ctcp_ping {
  my ($from, $arg) = @_[ARG0 .. $#_];
  my ($kernel) = $_[KERNEL];

  my ($nick) = LiMuBai::Utils::get_nick($from);
  print $arg,$/; 
  print "@{$arg}",$/;
  $kernel->post( 'limubai', 'ctcpreply', $nick, 'PING ' . join(' ',@{$arg}) );
}


sub _default {
  my ($state, $event, $args) = @_[STATE, ARG0, ARG1];

  $args ||= [];
  print "$state -- $event @$args\n";
}


1;
