I have quickly knocked this together
PHP Code:
<?php
set_time_limit(0);//so we dont timeout
class IRC
{
const SERVER = 'b0rk.uk.quakenet.org';
const PORT = 6667;
const CHANNEL = '#sketch';
const NICK = 'sketchmedia2';
private static $pSocket;
private static $szBuffer;
public static function getUsers()
{
if(!self::$pSocket = fsockopen(self::SERVER, self::PORT, $iError, $szError, 2))
{
die($iError . ' - ' .$szError);
}
$szUsers = self::mainLoop();
return explode(' ', trim(str_replace(':', '', substr($szUsers, strpos($szUsers, self::CHANNEL) + strlen(self::CHANNEL)))));
}
private static function mainLoop()
{
self::sendCmd('PASS NOPASS');
self::sendCmd('NICK '. self::NICK);
self::sendCmd('USER '. self::NICK . ' USING TALKPHP IRC');
while(!feof(self::$pSocket))
{
self::$szBuffer = fgets(self::$pSocket, 1024);
if(strpos(self::$szBuffer, '376'))
{
self::sendCmd('JOIN ' . self::CHANNEL);
}
if(substr(self::$szBuffer, 0, 6) == 'PING :')
{
self::sendCmd('PONG :'.substr(self::$szBuffer, 6));
}
if(strpos(self::$szBuffer, '353'))
{
fclose(self::$pSocket);
return self::$szBuffer;
}
//echo self::$szBuffer , '<br />';
}
}
private static function sendCmd($szCmd)
{
$szCmd .= "\n\r";
fwrite(self::$pSocket, $szCmd, strlen($szCmd)) or die('Error'); //sends the command to the server
}
}
echo '<pre>', var_dump(IRC::getUsers()), '</pre>';
quite simple, a little explanation though:
PHP Code:
if(strpos(self::$szBuffer, '376'))
{
self::sendCmd('JOIN ' . self::CHANNEL);
}
this checks to see if the MOTD has finished, meaning we really are connected and we can send the JOIN command
PHP Code:
if(substr(self::$szBuffer, 0, 6) == 'PING :')
{
self::sendCmd('PONG :'.substr(self::$szBuffer, 6));
}
this line responds to PING requests sent by the IRC server, this allows each computer to know if the opposite end is still present and available, you will be disconnected if you don't respond to PING with the message sent, this line takes care of that.
PHP Code:
if(strpos(self::$szBuffer, '353'))
{
fclose(self::$pSocket);
return self::$szBuffer;
}
Reads the user list, and returns the line.
You may need to change the numbers or make a regex that does it, but i dont have the time currently, un-comment the echo in the main loop to see the server responses.
The script outputs :
Code:
array(3) { [0]=> string(12) "sketchmedia2" [1]=> string(5) "sam__" [2]=> string(10) "@GBH|Johno" }