Hello all!
I'm trying to make an IRCbot, but I have an issue I just can't seem to get through.
<?php
class irc
{
// server connect
public $host = "irc.freenode.net";
public $port = 6665;
public $channel = "#dinksmallwood";
// client-side
public $username = "GlennBOT";
public $hostname = "ti0184a340-0784.bb.online.no";
public $server = array();
public $crap = 0;
public function __construct()
{
error_reporting(E_ALL);
set_time_limit(0);
}
public function ircConnect()
{
// Connect to IRC server
$this->server['SOCKET'] = fsockopen($this->host, $this->port, $errno, $errstr, 2);
// Needed headers in order to connect, otherwise we recieve error.
$this->server['HEADERS'] .= "NICK " . $this->username . "\n\r";
$this->server['HEADERS'] .= "USER " . $this->username . ' ' . $this->hostname . ' ' . $this->host . ' :' . $this->username . "\n\r";
// Sending headers.
$this->sendCmd($this->server['HEADERS'], true);
}
public function irc_loop()
{
if($this->server['SOCKET'])
{
while(!feof($this->server['SOCKET']))
{
$this->server['READ_BUFFER'] = fgets($this->server['SOCKET'], 1024);
echo "[RECEIVE] ".$this->server['READ_BUFFER']."\n\r";
if (strpos($this->server['READ_BUFFER'], "376"))
{
$this->sendCmd("JOIN ".$this->channel." \n\r", true);
$this->sendCmd("PRIVMSG ".$this->channel." Hello, I'm a baby \n\r", true);
}
if(substr($this->server['READ_BUFFER'], 0, 6) == "PING :")
{
$this->sendCmd("PONG :".substr($this->server['READ_BUFFER'], 6)."\n\r", true);
}
flush();
}
}
}
public function sendCmd($command, $showConsole = false)
{
@fputs($this->server['SOCKET'], $command, strlen($command));
if ($showConsole == true)
{
echo "[SEND] ".$command." <br>";
}
}
}
?>
It really does connect and talks on startup, but because this script is stuck in the while(!feof($this->server)) loop, I can't seem to send commands to the server, since it's just checking after new responses from the server all the time.
Could anyone guide me here and tell me how I go about sending commands t the server while still looping and checkingfor server responses?
Thanks in advance!