Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Preventing disconnections of an IRC Bot made with Net::IRC

by DaWolf (Curate)
on Dec 06, 2003 at 21:03 UTC ( [id://312803]=perlquestion: print w/replies, xml ) Need Help??

DaWolf has asked for the wisdom of the Perl Monks concerning the following question:

For a little background take a look at Reasons for memory growth on Win32 IRC Bot app.

I've recently discovered that my brave IRC bot is losing it's connection from time to time. Sometimes this fact is directly related with the fact that his lousy creator likes to play heavy consuming on-line games, but sometimes not.

A while ago it happened because of the online gaming with the "Ping timeout" message. Good thing is that the perl process keeps running, but it seems that I can't make a correct sub for handling disconnections, so I ask for your help, brothers and sisters.

I'll not post the entire code here, it'd be a waste of time, but here are the parts that matters on this:

This is the "start_it" sub wich makes the connection:
sub start_it { my $irc = new Net::IRC; my $conn = $irc->newconn ( Server => shift || $server, Port => shift || $port, Nick => $botnick, Password => $pass, Ircname => 'My IRC Bot', Username => $botnick ); $conn->{channel} = shift || '#'.$canal; $conn->add_handler('376', \&on_connect); $conn->add_handler('quit', \&on_quit); # Lots of other handlers here, and below the one who should do wha +t I want: $conn->add_handler('disconnect', \&on_disconnect); $irc->start() || die("Impossible to start."); }
This is the "on_connect" sub:
sub on_connect { my $conn = shift; $conn->join($conn->{channel}); $conn->{connected} = 1; }
and finally this is the on_disconnect sub:
sub on_disconnect { sleep 30; # Waits for 30 seconds do_runlog(4,""); # Logs that it lost the connection start_it(); # Start over }
It doesn't log anything, so there are two possibilities here:

1) For some reason it's not calling the "on_disconnect" sub
2) Sleep is "sleeping forever"

<UPDATE>
I've passed the do_runlog line to be the first at the on_disconnect sub, so I know if it's being called or not. It's not. So it's not a "sleep bug".
</UPDATE>

I use ActivePerl 5.8.0 on a Windows 2000 Professional machine.

Please any thoughts before I get bald from pulling my hair out would be appreciated.

my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Replies are listed 'Best First'.
Re: Preventing disconnections of an IRC Bot made with Net::IRC
by Anonymous Monk on Dec 07, 2003 at 08:30 UTC
    Unless the IRC server sends a 'disconnect' command (if that is even in the IRC protocol), on_disconnect is not gonna get called.

    In perl, a socket is just a filehandle, and you use fileno and eof to test if its open.

      So, if I've understood you correctly I should use:
      if (eof($irc)) { &on_disconnect; }
      or
      if (!fileno($irc)) { &on_disconnect; }
      Is that correct?

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
        Is $irc a socket?
Re: Preventing disconnections of an IRC Bot made with Net::IRC
by zengargoyle (Deacon) on Dec 07, 2003 at 11:31 UTC

    Untested, but try something like this.

    my $timeout = 20; # seconds my $conn = $irc->newcon ( # ... ); sub check_alivedness { unless ( $conn->connected ) { # do something # $con->connect } $con->schedule( $timeout, \&check_alivedness ); } $con->schedule( $timeout, \&check_alivedness ); $irc->start;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://312803]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found