Thread: mirc php script
View Single Post
Old 09-07-2008, 01:12 PM   #8 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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::SERVERself::PORT$iError$szError2))
        {
            die(
$iError ' - ' .$szError);
        }
        
$szUsers self::mainLoop();
        return 
explode(' 'trim(str_replace(':'''substr($szUsersstrpos($szUsersself::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::$pSocket1024);
            if(
strpos(self::$szBuffer'376'))
            {
                
self::sendCmd('JOIN ' self::CHANNEL);
            }
            if(
substr(self::$szBuffer06) == 'PING :')
            {
                
self::sendCmd('PONG :'.substr(self::$szBuffer6));
            }
            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$szCmdstrlen($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::$szBuffer06) == 'PING :')
{
     
self::sendCmd('PONG :'.substr(self::$szBuffer6));

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" }
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Wildhoney (09-07-2008)