![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 9
Rep Power: 0
![]() |
Bot Reconnection problem
I am trying to get the bot to reconnect when it gets disconnect. I have included two pieces of code. The first being the bot the second being the reconnecting code. I have taken the reconnecting code and tryed to plus it inside the bot. This is a bot for irc.
Bot Code #!/usr/bin/perl
# This is a simple IRC bot that just rot13 encrypts public messages.
# It responds to "rot13 <text to encrypt>".
use warnings;
use strict;
use POE;
use POE::Component::IRC;
sub CHANNEL () { "#Freedom" }
# Create the component that will represent an IRC network.
POE::Component::IRC->new("magnet");
# Create the bot session. The new() call specifies the events the bot
# knows about and the functions that will handle those events.
POE::Session->create(
inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
irc_public => \&on_public,
},
);
# The bot session has started. Register this bot with the "magnet"
# IRC component. Select a nickname. Connect to a server.
sub bot_start {
my $kernel = $_[KERNEL];
my $heap = $_[HEAP];
my $session = $_[SESSION];
$kernel->post( magnet => register => "all" );
my $nick = 'usepoe' . $$ % 1000;
$kernel->post( magnet => connect =>
{ Nick => 'JPPLAY-bot',
Username => 'cookbottest',
Ircname => 'POE::Component::IRC cookbook bot',
Server => 'irc.radio-freedom.com',
Port => '6667',
}
);
}
# The bot has successfully connected to a server. Join a channel.
sub on_connect {
$_[KERNEL]->post( magnet => join => CHANNEL );
}
# Once connected, start a periodic timer to ping ourselves. This
# ensures that the IRC connection is still alive. Otherwise the TCP
# socket may stall, and you won't receive a disconnect notice for
# up to several hours.
sub bot_connected {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
$heap->{seen_traffic} = 1;
$kernel->delay( autoping => 300 );
}
# Ping ourselves, but only if we haven't seen any traffic since the
# last ping. This prevents us from pinging ourselves more than
# necessary (which tends to get noticed by server operators).
sub bot_do_autoping {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
$kernel->post( poco_irc => userhost => "my-nickname" )
unless $heap->{seen_traffic};
$heap->{seen_traffic} = 0;
$kernel->delay( autoping => 300 );
}
# The bot has received a public message. Parse it for commands, and
# respond to interesting things.
sub on_public {
my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
my $ts = scalar localtime;
print " [$ts] <$nick:$channel> $msg\n";
if ( my ($rot13) = $msg =~ /^rot13 (.+)/ ) {
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
# Send a response back to the server.
$kernel->post( magnet => privmsg => CHANNEL, $rot13 );
}
}
# Reconnect in 60 seconds. Don't ping while we're disconnected. It's
# important to wait between connection attempts or the server may
# detect "abuse". In that case, you may be prohibited from connecting
# at all.
sub bot_reconnect {
my $kernel = $_[KERNEL];
$kernel->delay( autoping => undef );
$kernel->delay( connect => 60 );
}
# Run the bot until it is done.
$poe_kernel->run();
exit 0;Know the reconnecting code. POE::Session->create(
inline_states => {
irc_disconnected => \&bot_reconnect,
irc_error => \&bot_reconnect,
irc_socketerr => \&bot_reconnect,
connect => \&bot_connect,
_start => \&bot_start,
autoping => \&bot_do_autoping,
irc_001 => \&bot_connected,
},
);
# Initialize the bot, and connect to a server.
sub bot_start {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
# Set up the bot here.
...;
# Connect.
$kernel->yield("connect");
}
# The connect code is in its own handler so it can be reached from two
# places: _start, and bot_reconnect.
sub bot_connect {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
# Consider performing server rotation here. A server may not be
# back right away, and having alternatives means your bot will be
# back in business sooner.
...;
# Do the connection.
$kernel->post( poco_irc => connect => \%parameters );
}
# Once connected, start a periodic timer to ping ourselves. This
# ensures that the IRC connection is still alive. Otherwise the TCP
# socket may stall, and you won't receive a disconnect notice for
# up to several hours.
sub bot_connected {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
# Join channel(s), set user modes, etc.
...;
$heap->{seen_traffic} = 1;
$kernel->delay( autoping => 300 );
}
# Ping ourselves, but only if we haven't seen any traffic since the
# last ping. This prevents us from pinging ourselves more than
# necessary (which tends to get noticed by server operators).
sub bot_do_autoping {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
$kernel->post( poco_irc => userhost => "my-nickname" )
unless $heap->{seen_traffic};
$heap->{seen_traffic} = 0;
$kernel->delay( autoping => 300 );
}
# Reconnect in 60 seconds. Don't ping while we're disconnected. It's
# important to wait between connection attempts or the server may
# detect "abuse". In that case, you may be prohibited from connecting
# at all.
sub bot_reconnect {
my $kernel = $_[KERNEL];
$kernel->delay( autoping => undef );
$kernel->delay( connect => 60 );
}I think my problem is I forgot to put the follow code inside the bot. Where does it go? POE::Session->create(
inline_states => {
irc_disconnected => \&bot_reconnect,
irc_error => \&bot_reconnect,
irc_socketerr => \&bot_reconnect,
connect => \&bot_connect,
_start => \&bot_start,
autoping => \&bot_do_autoping,
irc_001 => \&bot_connected,
},
); |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Oct 2004
Posts: 9
Rep Power: 0
![]() |
When I try to add:
irc_disconnected => \&bot_reconnect,
irc_error => \&bot_reconnect,
irc_socketerr => \&bot_reconnect,
connect => \&bot_connect,
_start => \&bot_start,
autoping => \&bot_do_autoping,
irc_001 => \&bot_connected,I am putting it right after. inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
irc_public => \&on_public, |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|