Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > infinite loop unexpectly dies

Reply
Thread Tools

infinite loop unexpectly dies

 
 
Vedran Vukotic
Guest
Posts: n/a
 
      03-02-2006
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 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 ****ing 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 o 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";
}
-------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
aspnet_wp.exe stopped unexpectly error Shirley ASP .Net Web Controls 0 11-28-2007 07:06 PM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Windows 2000 crashes unexpectly May Computer Support 4 05-29-2004 09:00 AM
Perl 'system' Creates Program That Dies When First C Program Dies Christopher M. Lusardi Perl Misc 3 10-19-2003 11:53 AM



Advertisments