473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

infinite loop unexpectly dies

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 :op $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";
}
-------------------
Mar 2 '06 #1
0 3663

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.