PHP Bot timeout

Talk about pretty much anything here, but DO NOT USE FOR SUPPORT.

Moderator: Supporters

Locked
Matridom
Posts: 296
Joined: Fri Jan 07, 2005 3:28 am

PHP Bot timeout

Post by Matridom »

I've written several PHP bots that work well on my server (where i can play with ping times and such) I'm having an issue connecting to a remote IRC server. The script keeps timing out during periods of low traffic.

I've set my "set_time_limit" to 0 to prevent PHP from timing out the session, wich it does, however after 60 seconds, my browser times out the connection causing the bot to disconnect. I Fixed this at home by creating a class with a 50 second ping time. I do not have this option available to me on this remote server.

What work arounds do i have available?

here is the start of my connection code

Code: Select all

//setting server side stuff.
set_time_limit(0);

//ICR server settings
$server = "someremote.host";
$room = "#room";
$port = "6667";

//Nickname stuff
$botname = "PHPBot";
$nickservpass = "XXXXXX";


############################### Start of script ################################
$serv = fsockopen($server,$port);

//Part of hte IRC protocol requires certain steps in order to connect.
//We are doing these manualy here
fputs($serv,"USER ".$botname." localhost localhost :".$botname."\r\n");
fputs($serv,"NICK ".$botname."\r\n");

while ($line = fgets($serv,1024)) {
    $a = explode(" ", $line);
    $a[2] = strtolower($a[2]);
    //This echo is to debug the program. since the script is run with output to
    // /dev/null, there is no need to comment this out.
    echo "<= $line <br />";
    flush();

    switch ($a[0]) {

    //the ping/pong reponse is a requirement, if the bot idles too long
    //the server will ping the client, a pong needs to be sent back to keep
    //the connection alive. this is done here.
    case "PING":
        fputs($serv, "PONG ".$a[1]." \r\n");
	echo "=> PONG ".$a[1]."<br>";
        break;

        default:
        break;
      }
Never argue with an idiot. They will bring you down to their level, then beat you with experience.
w00t
Posts: 1136
Joined: Thu Mar 25, 2004 3:31 am
Location: Nowra, Australia

Post by w00t »

I'd advise using Net::Smart_IRC from PEAR. Makes life *a lot* easier. :P

Code: Select all

<?php
	require_once('Net/SmartIRC.php');

	define('KARMABOT_GLOBAL_HOST', 'irc.chatspike.net');
	define('KARMABOT_GLOBAL_PORT', 6667);
	define('KARMABOT_GLOBAL_NICK', 'K');
	
	define('DB_HOST', 'localhost');	
 	define('DB_USER', 'root');
 	define('DB_PASS', '');
 	define('DB_NAME', 'botster');

	class KarmaBot
	{
		var $irc;

		function KarmaBot()
		{
			/* now do IRC initialisation */
			$this->irc = &new Net_SmartIRC();
			$this->irc->setUseSockets(TRUE);
			$this->irc->connect(KARMABOT_GLOBAL_HOST, KARMABOT_GLOBAL_PORT);
			$this->irc->registerTimehandler(10000, $this, 'timer_pingmysql');
			$this->irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^(\+\+.+|.+\+\+)', $this, 'command_karmaup');
			$this->irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^(--.+|.+--)', $this, 'command_karmadown');
			$this->irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '^karma .+', $this, 'command_karma');
			$this->irc->login(KARMABOT_GLOBAL_NICK, 'Karma Chameleon.', 0, KARMABOT_GLOBAL_NICK);
			//$this->irc->join("#chatspike");
			//$this->irc->join("#nintendo");
			$this->irc->join("#coding");

			$this->irc->listen();
			$this->irc->disconnect();
		}

		function debug($message)
		{
			$this->irc->message(SMARTIRC_TYPE_CHANNEL, "#coding", $message);
		}
		
		function command_karmaup(&$irc, &$data)
		{
			$nick = mysql_real_escape_string(substr($data->messageex[0], 0, strlen($data->messageex[0]) - 2));
			
			$query = "SELECT * FROM karma WHERE `name` = '" . $nick . "'";
			$res = mysql_query($query);
			if (mysql_num_rows($res) == 0)
			{
				/* not already there, insert a record */
				$query = "INSERT INTO karma (name, karma) VALUES ('" . $nick . "', 1)";
				$res = mysql_query($query);
			}
			else
			{
				/* already taken */	
				$query = "UPDATE karma SET `karma` = `karma` + 1 WHERE `name` = '" . $nick . "'";
				$res = mysql_query($query);
			}
		}
		
		function command_karmadown(&$irc, &$data)
		{
			$nick = mysql_real_escape_string(substr($data->messageex[0], 0, strlen($data->messageex[0]) - 2));
			
			$query = "SELECT * FROM karma WHERE `name` = '" . $nick . "'";
			$res = mysql_query($query);
			if (mysql_num_rows($res) == 0)
			{
				/* not already there, insert a record */
				$query = "INSERT INTO karma (name, karma) VALUES ('" . $nick . "', -1)";
				$res = mysql_query($query);
			}
			else
			{
				/* already taken */	
				$query = "UPDATE karma SET `karma` = `karma` - 1 WHERE `name` = '" . $nick . "'";
				$res = mysql_query($query);
			}
		}
		
		function command_karma(&$irc, &$data)
		{
			if (!isset($data->messageex[1]))
				return;
				
			$nick = mysql_real_escape_string($data->messageex[1]);
			
			$query = "SELECT * FROM karma WHERE `name` = '" . $nick . "'";
			$res = mysql_query($query);
			$row = mysql_fetch_assoc($res);
			
			if ($row)
			{
				$this->irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Karma for " . $data->messageex[1] . " is " . $row['karma']);
			}
		}
		
		/*
		 * SmartIRC calls this every 10 seconds. Pings the mysql connection to keep it alive.
		 */
		function timer_pingmysql(&$irc)
		{
			mysql_ping();
		}
	}

	/* init db connection */
	if (!@mysql_connect(DB_HOST, DB_USER, DB_PASS))
	{
		echo("Could not connect to mysql server: " . mysql_error());
		die();
	}
	else if (!@mysql_select_db(DB_NAME))
	{
		echo("Could not select database: " . mysql_error());
		die();
	}
	
	$KarmaBot = &new KarmaBot();
?>
-ChatSpike IRC Network [http://www.chatspike.net]
-Denora Stats [http://denora.nomadirc.net]
-Omerta [http://www.barafranca.com]
Matridom
Posts: 296
Joined: Fri Jan 07, 2005 3:28 am

Post by Matridom »

I'll look into it. Thanx
Never argue with an idiot. They will bring you down to their level, then beat you with experience.
Locked