#!/usr/bin/perl -w
use strict;
use Net::AIM;
use Chatbot::Eliza;
   
my $nick  = "motherofperl";
my $aim   = new Net::AIM;
my $mybot = new Chatbot::Eliza;

$aim->debug(1);
my $conn = $aim->newconn(Screenname   => 'motherofperlbot',
			 Password     => 'motherofperl')
    or die "Can't connect to AIM server.\n";

$conn->add_handler('config',    \&on_config);
$conn->add_handler('im_in',    \&on_im);
$conn->add_handler('error',    \&on_error);

$aim->start;

sub on_config {
    my ($self, $event) = @_;
    my ($str) = $event->args;
    $self->set_config($str);
}

sub on_error {
	my ($self, $event) = @_;
	my $error;
	my @stuff;
	($error, @stuff) = $event->args;
	my $errstr = $event->trans($error);
	$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
	print STDERR "ERROR: $errstr\n";
}

sub on_im {
    my ($self, $event) = @_;
    my ($nick) = $event->from;
    print $event->dump;
    my @args = $event->args;
    $self->send_im($nick, $mybot->transform($args[2]));
}
