101 lines
1.9 KiB
Perl
101 lines
1.9 KiB
Perl
# Auto IRC Bot. An advanced, lightweight and powerful IRC bot.
|
|
# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS)
|
|
# This program is free software; rights to this code are stated in doc/LICENSE.
|
|
|
|
# Subroutines for parsing incoming data from IRC.
|
|
package Parser::IRC;
|
|
use strict;
|
|
use warnings;
|
|
use API::Std qw(conf_get);
|
|
use API::IRC qw(cjoin nick quit);
|
|
|
|
# Raw parsing hash.
|
|
our %RAWC = (
|
|
'001' => \&num001,
|
|
'432' => \&num432,
|
|
'433' => \&num433,
|
|
);
|
|
|
|
# Variables for various functions.
|
|
our (%got_001, %botnick);
|
|
|
|
# Parse raw data.
|
|
sub _parse
|
|
{
|
|
my ($svr, $data) = @_;
|
|
|
|
# Split spaces into @ex.
|
|
my @ex = split(' ', $data);
|
|
|
|
# Make sure there is enough data.
|
|
if (defined $ex[0] and defined $ex[1]) {
|
|
# If it's a ping...
|
|
if ($ex[0] eq 'PING') {
|
|
# send a PONG.
|
|
Auto::socksnd($svr, "PONG ".$ex[1]);
|
|
}
|
|
else {
|
|
# otherwise, check %RAWC for ex[1].
|
|
if (defined $RAWC{$ex[1]}) {
|
|
&{ $RAWC{$ex[1]} }($svr, @ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
###########################
|
|
# Raw parsing subroutines #
|
|
###########################
|
|
|
|
# Parse: Numeric:001
|
|
# Successful connection.
|
|
sub num001
|
|
{
|
|
my ($svr, @ex) = @_;
|
|
|
|
$got_001{$svr} = 1;
|
|
|
|
# Get the auto-join from the config.
|
|
my @cajoin = @{ (conf_get("server:$svr:ajoin"))[0] };
|
|
|
|
# Join the channels.
|
|
unless (defined $cajoin[1]) {
|
|
# For single-line ajoins.
|
|
my @sajoin = split(',', $cajoin[0]);
|
|
|
|
cjoin($svr, $_) foreach (@sajoin);
|
|
}
|
|
else {
|
|
# For multi-line ajoins.
|
|
cjoin($svr, $_) foreach (@cajoin);
|
|
}
|
|
}
|
|
|
|
# Parse: Numeric:432
|
|
# Erroneous nickname.
|
|
sub num432
|
|
{
|
|
my ($svr, undef) = @_;
|
|
|
|
if ($got_001{$svr}) {
|
|
err(3, "Got error from server[".$svr."]: Erroneous nickname.", 0);
|
|
}
|
|
else {
|
|
err(2, "Got error from server[".$svr."] before 001: Erroneous nickname. Closing connection.", 0);
|
|
quit($svr, "An error occurred.");
|
|
}
|
|
}
|
|
|
|
# Parse: Numeric:433
|
|
# Nickname is already in use.
|
|
sub num433
|
|
{
|
|
my ($svr, undef) = @_;
|
|
|
|
if (defined $botnick{$svr}) {
|
|
nick($svr, $botnick{$svr}."_");
|
|
}
|
|
}
|
|
|
|
1;
|