infinite loop unexpectly dies


V

Vedran Vukotic

Hello

I made the following infinite loop that check's if the bot died and call
it again:

------loadloop.pl-----
#!/usr/bin/perl
use strict;

while (1) {
if (`ps -A |grep top.pl` == ""){
system("./top.pl 2>&1 > /dev/null");
sleep(5);
}
sleep(15);
}
---------------------

The loop successfully reloads the bot (top.pl) if I kill him by calling
the bot's die function or buy killing its process directly.

The problem is that when a "Connection reseted by peer" happens the loop
dies.

Does anyone have an idea why this happens?

Thanks.


bot:
-------top.pl------
#!/usr/bin/perl -w

use strict;

use IO::Socket;

my $server = "irc.freenode.net";
my $nick = "tehbot";
my $login = "tehbot";
my $shutup = 0;
my $op_pass = "blah";

my $channel = "#tehend";
my $countdown = 5;
my $log = 0;
my $outfile = ">>index.html";
my $ofd;

my $sock = new IO::Socket::INET(PeerAddr => $server,PeerPort =>
6667,Proto => 'tcp') or die "Can't connect\n";


print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :bla\r\n";


while (my $input = <$sock>) {
if ($input =~ /004/) {
last;
}
elsif ($input =~ /433/) {
die "Nickname is already in use.";
}
}

print $sock "JOIN $channel\r\n";
print $sock "PRIVMSG nickserv :identify dios801\r\n";
sleep(5);
print $sock "PRIVMSG chanserv :eek:p $channel\r\n";

while (my $input = <$sock>) {
chop $input;
my $second;
my $Minute;
my $Hour;
my $Day;
my $Month;
my $Year;
my $WeekDay;
my $DayOfYear;
my $IsDST;
($second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear,
$IsDST) = localtime(time);
if ($input =~ /^PING(.*)$/i) {
print $sock "PONG $1\r\n";
} elsif ($input =~ /You have no new memos/) {
$log = 1;
}
else {

#=====BEGIN ACTION CODE=============
if ($input =~ /ACTION/){ #Someone does something
my $user = getuser($input);
my $action = getaction($input);
chop $action;
print $sock "PRIVMSG $channel : kewl $user $action :)\r\n";
open ($ofd, $outfile);
print $ofd "<tr>\r\n";
print $ofd "<TH width=\"50px\" bgcolor=\"#c0c0c0\"><FONT
face=Arial size=1><B>$Hour:$Minute:$second</B></FONT></TH>\r\n";
print $ofd "<TH width=\"90px\" bgcolor=\"#c0c0c0\"><FONT
face=Arial size=1><B>$user</B></FONT></TH>\r\n";
print $ofd "<TH bgcolor=\"#ffffff\"
align=\"left\"><FONT face=Arial size=1>$action</FONT></TH>\r\n";
print $ofd "</tr>\r\n";
close($ofd);
}
elsif ($input =~ /JOIN/) { #someone has joined teh channel
my $user = getuser($input);
if ($user ne $nick) {
print $sock "PRIVMSG $channel :Hello $user , welcome to teh end :)\r\n";
}
}
else { #someoneis talking
my $luser = getuser($input);
my $msgx = getmsg($input);
if (($log == 1) && ($msgx !~ /blah/) && ($msgx !~ /Serv/)) {
open ($ofd, $outfile);
print $ofd "<tr>\r\n";
print $ofd "<TH width=\"50px\" bgcolor=\"#C0C0C0\"><FONT
face=Arial size=1><B>$Hour:$Minute:$second</B></FONT></TH>\r\n";
print $ofd "<TH width=\"90px\" bgcolor=\"#C0C0C0\"><FONT
face=Arial size=1><B>$luser</B></FONT></TH>\r\n";
print $ofd "<TH bgcolor=\"#ffffff\"
align=\"left\"><FONT face=Arial size=1>$msgx</FONT></TH>\r\n";
print $ofd "</tr>\r\n";
close($ofd);}
if ($input =~ /:!killself/){
my $user = getuser($input);
print $sock "PRIVMSG $channel :$user killed me!\r\n";
die("!die called");
}
elsif ($input =~ /:!help/){
my $user = getuser($input);
print $sock "PRIVMSG $channel : Hi, $user, u can use teh
following: !help !who !print <some text> !topic <topic> !kick <name> !op
<pass> !nuke Have fun :)\r\n";
}
elsif ($input =~ /:!print /){
my $user = getuser($input);
my $msg = getmsg($input);
$msg = substr($msg,7,length($msg)-7);
print $sock "PRIVMSG $channel :$msg\r\n";
}

elsif ($input =~ /:!snail attack/){
my $user = getuser($input);
my $msg = getmsg($input);
$msg = substr($msg,7,length($msg)-7);
print $sock "PRIVMSG $channel :You have been RAPED by a bunch
of Vicious Snails...\r\n";
}
elsif ($input =~ /:!op/){
$shutup = 1;
my $user = getuser($input);
my $msg = getmsg($input);
my @msg_split = split(/ /,$msg);
my $pass = $msg_split[1]; chop $pass;
if ($pass eq $op_pass) {
print $sock "PRIVMSG $channel :$user told me to op him :) he 's
t3h god\r\n";
print $sock "MODE $channel +o $user\r\n";
} else {
print $sock "PRIVMSG $channel :$user you r a fucking idi0t, you
cna't eaven remember a password, bllaahh\r\n";
}
}
elsif ($input =~ /:!kick /){
$shutup = 1;
my $user = getuser($input);
my $msg = getmsg($input);
$msg = getmsg($input);
$msg = substr($msg,6,length($msg)-6);
print $sock "PRIVMSG $channel :$user told me to kick $msg\r\n";
print $sock "KICK $channel $msg\r\n";
}
elsif ($input =~ /:!nuke/){
print $sock "PRIVMSG $channel :Nuclear Lunch Detected!!!\r\n";
my $i;
for ($i=$countdown;$i>0;$i--){
print $sock "PRIVMSG $channel :$i seconds left\r\n";
sleep(1);
}
sleep(1);
print $sock "WHO $channel\r\n";
while (($input = <$sock>) !~/\/WHO/) {
chop $input;
my @parts = split(/:/,$input);
my @details = split(/ /, $parts[1]);
if ($details[7] ne $nick) {
print $sock "KICK $channel $details[7]\r\n";
}
}
}
elsif ($input =~ /:!topic /){
$shutup = 1;
my $user = getuser($input);
my $msg = getmsg($input);
$msg = getmsg($input);
$msg = substr($msg,7,length($msg)-7);
print $sock "PRIVMSG $channel :$user told me to change topic to
$msg\r\n";
print $sock "TOPIC $channel :$msg\r\n";
#print $sock "TOPIC $channel bla bla bla \r\n";
}
elsif ($input =~ /:!who/){
my $user = getuser($input);
my $n = 0;
my @list;
my $userlist = "";
print $sock "WHO $channel\r\n";
while (($input = <$sock>) !~/\/WHO/) {
chop $input;
my @parts = split(/:/,$input);
my @details = split(/ /, $parts[1]);
$userlist .= $details[7] . " ";
}
print $sock "PRIVMSG $channel :Users on $channel: $userlist\r\n";
}
elsif (($input =~ /$nick/) && ($input =~ /$channel/)){
my $user = getuser($input);
my @temp = split(/ /,$user); my $len = @temp; if (($len eq
1)&&($user ne $nick)){
print $sock "PRIVMSG $channel :Do not $nick me $user\!\r\n";
}
}
}
print "$input\n";
#=====end else code ==============
}
}

sub getuser{
my @splited = split(/:/, $_[0]);
my @temp = split(/!/,$splited[1]);
return $temp[0];
}

sub getmsg{
my @splited = split(/:/, $_[0]);
my $smece = shift(@splited);
$smece = shift(@splited);
my $temp = join(":",@splited);
return $temp;
}

sub getaction{
my @splited = split(/ACTION/, $_[0]);
$splited[1] = substr($splited[1],0,length($splited[1])-1);
return $splited[1];
}

sub write_html_header{
print "WRINTIG 2 log\n";
open ($ofd, $outfile);
print $ofd <<ENDHTML;
<HTML><HEAD><TITLE>tehbot's irc logs</TITLE>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
</HEAD>
<BODY bgColor=white>
<font face="Arial" color="#0000FF" size="4"><B>tehbot's irc
logs<B/></font><br/>
<font face="Arial" color="#0000FF" size="2">tehbot irc bot v. 0.1b
copyright &copy 2006 SoulEdge</font>
<br/><br/>
<B><font face="Arial" color="#0000FF" size="3">Log</font></B></br>
<TABLE width="100%"cellSpacing=1 border=0><TBODY>
<tr>
<TH width="50px" bgcolor="#C0C0C0"><FONT face=Arial
size=2>time</FONT></TH>
<TH width="90px" bgcolor="#C0C0C0"><FONT face=Arial
size=2>user</FONT></TH>
<TH bgcolor="#C0C0C0"><FONT face=Arial size=2>action</FONT></TH>
</tr>
ENDHTML
close ($ofd);
$outfile = ">>index.html";
}
-------------------
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top