Hello, I'm currently working on my IRC bot and it was all thanks to
this script that I started this project.
After managing to connect to the server using that script, I converted it to OOP, because I need training in that department as well and now I'm stuck. At first I wanted to know how I could get the bot to send commands to the server when it was in an infinite loop, but I put that sort of on hold after a guy on Daniweb gave me a new piece of code to try out in order to read output from the server and do actions based on that.
The thread is here:
http://www.daniweb.com/forums/thread243489.html
If you read that thread you'd be able to understand more of my problem I think. So, as of now, I'm stuck with two issues at hand. After trying the new code the guy over at Daniweb gave me, I can't seem to connect to the server and I don't really know how to use the code he gave me, I tried to, but I don't know whether failed or not.
Here is my current script:
PHP Code:
<?php
class irc
{
// server connect
public $host = "irc.freenode.net";
public $port = 6665;
public $channel = "#dinksmallwood";
public $greetName;
// client-side
public $username = "Dink_Smallwood";
public $hostname = "ti0184a340-0784.bb.online.no";
public $server = array();
public $crap = 0;
public function __construct()
{
error_reporting(E_ALL);
ignore_user_abort(true);
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['CMD_BUFFER'] = explode (" ", trim(fgets($this->server['SOCKET'], 1024)));
$this->server['RAW_BUFFER'] = fgets($this->server['SOCKET'], 1024);
echo "[RECEIVE] ".$this->server['RAW_BUFFER']."\n\r";
if (strpos($this->server['RAW_BUFFER'], "End of /MODT command"))
{
$this->sendCmd("JOIN ".$this->channel." \n\r", true);
$this->sendCmd("PRIVMSG" . " #dinksmallwood :" . "Hello, I'm Dink Smallwood. I defeated Seth!" . "\n\r", true);
}
switch ($this->server["CMD_BUFFER"][2])
{
case ":JOIN":
preg_match("(:([^!]+)!)", $this->server['READ_BUFFER'], $this->greetName);
$this->sendCmd("PRIVMSG" . " #dinksmallwood :" . "Welcome ".$this->greetName[1]." to the Dink Smallwood IRC chat" . "\n\r", true);
break;
}
if(substr($this->server['CMD_BUFFER'][3], 0, 6) == "PING :")
{
$this->sendCmd("PONG :".substr($this->server['READ_BUFFER'][0], 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>";
}
}
}
?>
Any help on this topic at hand will be appreciated!