Jump to content

PHP IRC keeps leaving


konnwat

Recommended Posts

okay i have a PHP irc bot hosted on a server (not my localhost) and it keeps on leaving at times. theres no pattern, sometimes it leaves after 5 mins of starting, sometimes hours after. but i need it to stay on  >:(

i thought it was there 3 things but i checked:
PHP safe mode is on
set_time_limit(0); is in the script and the timeout is at a year  :P
there are no unknown characters anywhere

what is wrong? someone please help me.
Link to comment
Share on other sites

okay its a bit long though, its about 550 lines :s and it uses a lot of includes to make it simple.

[code]<?PHP
//add sripos
if (!function_exists("stripos")) {
  function stripos($str,$needle) {
  return strpos(strtolower($str),strtolower($needle));
  }
}
//connect to IRC
set_time_limit(0);

//set vars
$host = 'irc.barafranca.com';
$port = '6667';
$nickname = "bot";
$channel = file('../bot/channels.txt');

//open data.txt
$savedline = file('../image/data.txt');
$savedline[3] = $savedline[2];
$savedline[2] = $savedline[1];
$savedline[1] = $savedline[0];

$killmode['#chan'] = 'on';
$ks = array();

//connect
$socket = fsockopen($host, $port);
stream_set_timeout($socket, 60*60*24*365);
fwrite($socket, "NICK ".$nickname."\r\n");
fwrite($socket, "USER ".$nickname." 8 * :nick's Bot\r\n");
fwrite($socket, "MODE ".$nickname." +B\r\n");
while ($line=fgets($socket)) {
  if (strpos($line, "433")>0) die();
  if (strpos($line, "004")>0) {
      $chancount = count($channel);
      fwrite($socket, "PRIVMSG nickserv :IDENTIFY bot hello123\r\n");
      for ($i = 0; $i < $chancount; $i++) {
            fwrite($socket, "JOIN ".$channel[$i]."\r\n");
      }
      break;
  }
}

//fuctions
while ($line=fgets($socket)) {
$chan = explode("PRIVMSG ", $line);
$chan = explode(" :", $chan[1]);
$chan = explode("#", $chan[0]);
$chan = "#".$chan[1];

$nick = explode(":", $line);
$nick = explode("!", $nick[1]);
$nick = $nick[0];

$tempstuff = explode("\r\n", $line);
if (stripos($line, ":@")>0) {
  $tempstuff = explode(":@", $tempstuff[0]);
} else {
  $tempstuff = explode(":!", $tempstuff[0]);
}
$tempstuff = explode(" ", $tempstuff[1]);
$two = $tempstuff[1];
$three = $tempstuff[2];
$four = $tempstuff[3];
?>[/code]

and then the scripts are like these

[code]if (stripos($line, ":hello bot")>0) {
fwrite($socket, "PRIVMSG $chan :hello $nick\r\n");
}[/code]
Link to comment
Share on other sites

Here is my working bot.. He don't have many features bot it works fine.. You may want to look at it as a general reference.

[code]
<?php
class irc_commands{
  function message($type, $destination, $message) {
      switch ($type) {
  case "ME":
      return 'PRIVMSG ' . $destination . ' :' . chr(1) . 'ACTION ' . $message . chr(1);
  break;
  case "REG":
    return 'PRIVMSG ' . $destination . ' :' . $message;
  break;
  }
  }
 
  function joinChannel($channels) {
      if (is_array($channels)) {
      $commands = array();
      foreach ($channels as $channel) {
      $commands[] = 'JOIN' . $channel;
  }
  return $commands;
  }else{
    return 'JOIN ' . $channels;
  }
  }

  function partChannel($channels) {
      if (is_array($channels)) {
      $commands = array();
      foreach($channels as $channel) {
    $commands[] = 'PART ' . $channel;
  }
  return $commands;
  } else {
      return 'PART ' . $channels;
  }
  }
 
  function kick($channel, $nicks, $reason) {
      if (is_array($nicks)) {
      $commands = array();
  foreach ($nicks as $nick) {
      $commands[] = 'KICK ' . $channel . ' ' . $nick . ' :' . $reason;
  }
  return $commands;
  } else {
      return 'KICK' . $channel . ' ' . $nicks . ' :' . $reason;
  }
  }
 
  function ident($password) {
      return 'NICKSERV IDENTIFY ' . $password;
  }
 
  function quit($message) {
      return 'QUIT ' . $message;
  }    
}

class ircBot extends irc_commands{
  var $config = array('server' => 'irc.freenode.net',
                      'port' => 6667,
                      'host' => '',
                      'channel' => '#phpfreaks',
                      'nick' => '',
                      'name' => '',
                      'password' => '');

  var $badWords = array('fuck','shit','damn','damm','bitch',
                      'cunt','whore','hoe','slut','ass');

  var $keyPhrases = array('you suck');
var $keyCommands = array('quit','end','shut up','stfu','terminate','talk',
                      'time','version');

  var $con = array();

  function cmd_send($command, $con) {
      $put = fputs($con['sock'], "$command\r\n");
      if (!$put) {
      $error = 'Error : Unable to write to the socket';
          print $error . '\n\r';
  fwrite($con['fp'], $error, 4096);
        } else {
          print "$command\n\r";
  fwrite($con['fp'], $command . "\n", 4096);
      }
  }
 
  function startBot() {
      $this->con['fp'] = fopen('path/to/log/', 'r+b');
      $this->con['sock'] = fsockopen($this->config['server'], $this->config['port']);
  if (!$this->con['sock']) {
      $error = "Error: unable to connect to " . $this->config['server'] . '\n\r';
  print $error;
  fwrite($this->fp, $error, 4096);
  }else{
      $this->cmd_send('NICK ' . $this->config['nick'], $this->con);
  $this->cmd_send('USER ' . $this->config['nick'] . ' ' . $this->config['host'] . ' ' . $this->config['server'] . ' :' . $this->config['name'], $this->con);
      while (!feof($this->con['sock'])) {
      $this->con['buffer']['all'] = trim(fgets($this->con['sock'], 4096));
  if (strpos($this->con['buffer']['all'], 'NOTICE AUTH :*** Checking ident')) {
      $this->cmd_send('NICK ' . $this->config['nick'], $this->con);
      $this->cmd_send('USER ' . $this->config['nick'] . ' ' . $this->config['host'] . ' ' . $this->config['server'] . ' :' . $this->config['name'], $this->con);
  } else if ($this->con['buffer']['all'] == ':NickServ!NickServ@services. NOTICE ' . $this->config['nick'] . ' :If this is your nickname, type /msg NickServ IDENTIFY <password>') {
      $command = parent::ident($this->config['password']);
      $this->cmd_send($command, $this->con);
  } else if ($this->con['buffer']['all'] == ':services. MODE ' . $this->config['nick'] . ' :+e') {
      $command = parent::joinChannel($this->config['channel']);
  $this->cmd_send($command, $this->con);
  } else if (strpos($this->con['buffer']['all'], $this->config['nick'] . ' !quit')) {
      $command = parent::quit("Don't hate me because I am bad ass");
  $this->cmd_send($command, $this->con);
  } else if (strpos($this->con['buffer']['all'], $this->config['nick'] . ' you suck')) {
      $command = parent::message('ME', $this->config['channel'], 'thinks you suck!!');
  $this->cmd_send($command, $this->con);
  }
  print date("[d/m @ H:i]")."<- ". $this->con['buffer']['all'] . "\n";
  fwrite($this->con['fp'], $this->con['buffer']['all'] . "\n", 4096);
  if (substr($this->con['buffer']['all'], 0, 6) == 'PING :') {
      $this->cmd_send('PONG :'.substr($this->con['buffer']['all'], 0, 6), $this->con);
  }  
  }
  }    
  }
}

set_time_limit(0);
$bot = new ircBot;
$bot->startBot(); 
?>
[/code]

Now you may want to pay special attention to this part

[code=php:0]
f (substr($this->con['buffer']['all'], 0, 6) == 'PING :') {
  $this->cmd_send('PONG :'.substr($this->con['buffer']['all'], 0, 6), $this->con);
}  
[/code]

Well it don't matter what you pong back with but you have to pong something back.. In this case it will be like this PONG :PING.. That works fine..

Good Luck,
Tom
Link to comment
Share on other sites

yeah it used to run on my localhost but

1. i needed it to run after i close my laptop :P
2. i made a php gd pic that get info from the bot that needs to be on also, all the time.
and 3. my php is now broken :( dunno what i did but i go to tick personal web sharing and it take forever to load. i might post that in the other forum though :P

i cant remember what it was like on my computer. also can i try copy and paste your code just to see if your bot stays on, then we will know its my script and not the server.
Link to comment
Share on other sites

right. so, if I do strpos($line,"PING :") it would return the first instance of "PING :" in your string.  If it finds nothing, it will return false, or 0.  So basically, the only way your condition will be true is if 0 === 0, or in other words, the condition is true if it is not found.

edit:

yes, you should either change it to != 0 just simply  if (substr($line,"PING :")) { ... }
Link to comment
Share on other sites

[23:48] IgBot joined the chat room.
[23:48] IgBot was promoted to operator by ChanServ.
[23:56] IgBot left the chat room. (Ping timeout: 241 seconds)

no that broke it :P

[code]
<?PHP
//pong at pings
if (strpos($line,"PING :") != 0) {
fwrite($socket, "PONG :irc.barafranca.com\r\n");
}
?>[/code]
Link to comment
Share on other sites

I don't really think that's the problem here. I think you fixed something that needed to be fixed...but let's find out for sure that that's not the problem, by commenting out your condition and doing this instead:

[code]
// if (strpos($line,"PING :") != 0) {
if (substr($line, 0, 6) == 'PING :') {
  fwrite($socket, "PONG :irc.barafranca.com\r\n");
}
[/code]

Link to comment
Share on other sites

I don't think that's because of something on your end... I mean, the principle here is that the target server keeps spitting out lines, and while it keeps spitting out lines, the loop continues.  It would only stop if the server stops spitting out lines. 

For example, if you have a quit command for your bot that sends a disconnection command: this would disconnect from the server, therefore the server would be sending no more lines, therefore the loop would end. 

I would first take a look at all of your "event" conditions to see if something isn't causing the server to drop the connection.  Like for instance, if you DO have a quit command for your bot, make sure your condition is setup right, because we have already determined one condition of yours that wasn't..

Failing that, I can't think of anything else, except for that maybe the server has some kind of anti-bot mechanism in place that drops you if it determines you're a bot...
Link to comment
Share on other sites

I like the sound of the "server stop spitting lines" that sounds true. thank you :D, how can i stop this?

theres no errors in my ping script by the way, every time i change it from what it was the bot leaves with a ping time out. but its i have it like it was original it leaves with EOF.

and also my server is made for php bots, no safe mode, fsockopen is allowed, and i posted a ticket about CPU usage before and the admins didn't seem to ban me for it ^^
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.