Bot-Net
view release on metacpan or search on metacpan
lib/Bot/Net/Mixin/Bot/IRC.pm view on Meta::CPAN
=head1 MIXIN STATES
=head2 on _start
Sets up the IRC client.
=cut
on _start => run {
post irc => register => 'all';
my $auto_connect = recall [ config => 'auto_connect' ];
if (not defined $auto_connect or $auto_connect) {
yield 'bot_connect';
}
};
=head2 on bot connect
Tells the IRC component to connect to the IRC server using the configuration stored in the C<irc_connect> option of the configuration.
=cut
on bot_connect => run {
my $config = recall [ config => 'irc_connect' ] ;
call irc => connect => $config;
};
=head2 on irc_001
Connects to the channels that the bot has been configured to join. This then fires the L</on bot connected> event which indicates that it is now safe to issue IRC commands, if your bot needs to do so.
=cut
on irc_001 => run {
my $self = get OBJECT;
my $log = recall 'log';
my $channels = recall [ config => 'channels' ];
for my $channel (@$channels) {
$log->info("Joining $channel...");
post irc => join => $channel;
}
yield 'bot_connected';
# Report readiness (helpful for testing)
my $config = recall [ config => 'irc_connect' ] ;
recall('log')->info(
"BOT READY : nick $config->{nick} server $config->{server} "
."port $config->{port}");
};
=head2 on irc_disconnected
=head2 on irc_error
=head2 on irc_socketerr
Handles bot disconnections. If the bot becomes disconnected it will automatically attempt to reconnect until the server returns or lets the bot back. It will attempt to do so in a way that will not cause it to be blocked for flooding.
=cut
on [ qw( irc_disconnected irc_error irc_socketerr ) ] => run {
# TODO XXX FIXME Add support for notifying the bot that the connection has
# been lost and then notifying the bot again when the connection is
# re-established.
delay attempt_reconnect => 60;
};
=head2 on attempt_reconnect
This state is invoked by L</on irc_disconnected> and related states. This tells the IRC client to attempt ot reconnect. This will be called repeatedly (on a delay) until a connection is reestablished.
=cut
on attempt_reconnect => run {
post irc => 'connect';
};
=head2 irc_msg USERHOST, ME, MESSAGE
Handles IRC messages sent directly to the bot. This emits:
=over
=item bot message_to_me
See L</bot message_to_me>.
=back
=cut
on irc_msg => run {
my $userhost = get ARG0;
my $me = get ARG1;
my $message = get ARG2;
my ($nick, $host) = split /!/, $userhost;
my $event = Bot::Net::Message->new({
sender_nick => $nick,
sender_host => $host,
recipient_nicks => $me,
message => $message,
private => 1,
});
yield bot_message_to_me => $event;
};
=head2 irc_public USERHOST, CHANNEL, MESSAGE
Handles IRC messages sent to a public channel. This then emits additional bot states:
=over
=item bot message_to_group
( run in 0.489 second using v1.01-cache-2.11-cpan-39bf76dae61 )