<?
// IRC Connector
// Coder: Seb Renauld (hide@address.com)
//
// Description: This class creates an IRC socket and allows
// The main "script" to interact with any IRC server.
//
// Requirements: a PHP4/PHP5 compiled with --with-sockets.
//
// You are free to use this class whenever and wherever
// you want; however, a small credit (link or something else)
// Are generally accepted :)
//
class IRCSock {
var $info;
var $regexp;
function load_remotes($file) {
// Let's load the system, shall we? :P
$conf = file($file);
foreach ($conf as $lnum => $line) {
$arrayemote = explode(":",$line);
switch ($arrayemote[0]) {
case "p":
$this->regexp[p][] = array("reg" => $arrayemote[1], "f" => $arrayemote[2]);
break;
case "c":
$this->regexp[c][] = array("reg" => $arrayemote[1], "f" => $arrayemote[2]);
break;
}
}
}
// To initiate a server connection
// When it will be over with the protocol sending,
// The function will not return anything...
// it will return something else... Error! :D
function initiate($ip,$port,$nick,$userid) {
$this->info['socket']['id'] = fsockopen($ip,$port,$errno);
// Because the script won't run for ages
// Timeout = 5
if (!$this->info['socket']['id']) {
return($errno);
// The error number is given
}
else {
// Building the basic raw protocol for authentification...
$outprotocol = "NICK $nick\n";
$outprotocol .= "USER $userid 127.0.0.1 $ip :PHP IRC Connector\n";
fwrite($this->info['socket']['id'],$outprotocol);
// The while will be in this function;
// it is because I don't want to send back and forth
// the socket pointer and all its status...
while($hash=fgets($this->info['socket']['id'],1024)){
$hash = str_replace("\r", "", $hash);
$hash = str_replace("\n", "", $hash);
echo $hash."<br>";
// It's time to understand the protocol :)
$arraytemp = explode(" ",$hash);
if(eregi("PING",$arraytemp[0])){
$this->sendraw("PONG ".$arraytemp[1]."\n");
echo "<font color=red>PING? PONG!</font><br>";
}
switch ($arraytemp[1]) {
case "PRIVMSG":
// Privmsgs... check if it's channel or user-based
if (strpos($arraytemp[2],"#") == FALSE) {
// User-based... Starting regexp checks
foreach ($this->regexp['c'] as $line => $array) {
if (eregi($array['reg'],$arraytemp[3])) {
if (function_exists($array['f'])) {
$array['f']($arraytemp[3],$arraytemp[1]);
echo "Called function ".$array['f']." with normal args";
}
}
echo "The regexp matches";
}
}
}
}
}
}
function sendraw($text) {
fputs($this->info['socket']['id'],$text);
}
}
// Declare your stuff like that:
$ircsock = new IRCSock;
$ircsock->load_remotes("remotes.rem");
$ircsock->initiate("webchat.langochat.net",6667,"testBot","lol");
?>