开发者

IRC Online Count within channel

Am having problem's with a script I found. Sometimes it works and sometimes it don't... Can anyone see any problems with it? or have a better one?

Many Thanks

<?php 
/* CONFIGURATIONS! */ 
$irc_server = 'xxxxxxxxxx';  // Server 
$irc_port = '6667';             // Port 
//$irc_nick = '';   // Nick 
$irc_channel = '#xxxxxxxx';     // Channel 
/* END CONFIGURATIONS! */ 

$socket = fsockopen($irc_server,$irc_port);            // Connect to the server. 
fputs($socket,"USER SOCKS SOCKS SOCKS :SOCKS\r\n");    // Send the username. 
fputs($socket,"NICK $irc_nick \r\n");                  // Set our nick. 
fputs($socket,"LIST $irc_channel \r\n");               // List the provided channel. 

// $handle = fopen('log.txt',a);  // Log if you want. 

// Set initial value. 
$users = 0; 

// Receive the incoming data. 
while(1) { 
    $sock_data = fgets($socket,1024);   // Get each line of data from the response. 
    // fwrite($handle,"$sock_data\r\n");   // Write the log. 

    // Get the information section of the desired responses. 
    $sep = explode(':',$sock_data);  // Separate by colons. 
    $info = $sep[1];  // After the first colon is the important information. 
    $message = $sep[2]; 

    // Break down the response and get the id and who sent it. 
    $info_sep = explode(' ',$info);  // Separate by spaces. 
    $full_开发者_JS百科who = $info_sep[0];  // The person who sent it is the first item in the list. 
    $id = $info_sep[1];  // The id is the second item in the list. 
    $who_sep = explode('!',$full_who);  // Separate by exclamation points. 
    $who = $who_sep[0];  // The nick of the person is the part before the exclamation point. 

    // I saw some scripts checking for this, so... 
    if ($who == 'PING') { 
        fputs($socket,"PONG $message"); 
    } 

    // PRIVMSG indicates someone is sending you a message. 
    // We just need this to reply to the VERSION and PING requests. 
    if ($id == 'PRIVMSG') { 
        if (substr($message, 0, 8) == 'VERSION') {  // Reply to the version response. 
        fputs($socket,"NOTICE $who :".chr(1)."VERSION getUsers v0.1b".chr(1)."\r\n"); 
        } elseif (strstr($message, 'PING') !== false) {  // Ping them back if needed. 
            fputs($socket,"NOTICE $who :$message"); 
        } 
    } 

    // 322 is the list response. 
    // This should get the number of users on the provided channel. 
    if ($id == '322') { 
        $users = $info_sep[4];  // The number of users. 
        // fclose($handle);  // Close the log. 
        fclose($socket);  // Close the connection. 
        break;  // End the loop. 
    } 

    // 323 is the end list response. 
    // This is in case there is no 322 response (the channel doesn't exist, maybe?) 
    if ($id == '323') { 
        // fclose($handle);  // Close the log. 
        fclose($socket);  // Close the connection. 
        break;  // End the loop. 
    } 

    // 263 is the failed response. 
    // Wait 2 seconds and retry. 
    if ($id == '263') { 
        sleep(2);  // Pause for 2 seconds. 
        fputs($socket,"LIST $irc_channel \r\n");  // List the provided channel. 
    } 
} 

// Display the results on the page. 
if ($users == '1') { 
    echo "1"; 
} else { 
    echo "$users"; 
}
?>


You might have more luck with a tested and stable PHP IRC library like PEAR's Net_SmartIRC - http://pear.php.net/package/Net_SmartIRC

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜