Okay, so I'll post it in case you want to see how I did it and in case anyone else wants this script. I made 2 scripts. One does it the easy way using the LIST command on the channel and taking the user list from there. The other does it the long way and JOINs the channel and parses the usernames. I also added a response for the VERSION and PING requests in case the server asks for it (although you're on for such a short time it's not that important).
Take note: These scripts are HEAVILY COMMENTED! I don't actually comment that much, I just wanted to make everything unbelievably clear so that anyone interested can understand. All comments/critiquing will be accepted as these are not perfect. Also understand that because I wasn't planning on doing anything else with these, the requests and responses are all sent plainly. There are no functions made for notices, PMs, etc. I just used raw messages.
KNOWN BUGS:
Sometimes I connect to a server and it checks the hostname/ident, but never does anything past that. Any ideas?
Finally, here's the one that uses LIST #channel for the user list. The number of ops cannot be attained this way.
<?php
/* CONFIGURATIONS! */
$irc_server = 'irc.rizon.net'; // Server
$irc_port = '6667'; // Port
$irc_nick = 'mxRocksMySocks'; // Nick
$irc_channel = '#anime-mx'; // 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_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 "There is 1 user in $irc_channel.";
} else {
echo "There are $users users in $irc_channel.";
}
?>
Here is the other one which uses JOIN #channel and parses the user list looking for ops.
<?php
/* CONFIGURATIONS! */
$irc_server = 'irc.rizon.net'; // Server
$irc_port = '6667'; // Port
$irc_nick = 'mxRocksMySocks'; // Nick
$irc_channel = '#anime-mx'; // Channel
$irc_op = '@'; // The symbol that signifies an op (some servers change it)
/* 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,"JOIN $irc_channel \r\n"); // Join the provided channel.
// $handle = fopen('log.txt',a); // Log if you want.
// Set initial count values.
$op_count = 0;
$total_count = 0;
$user_count = 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_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");
}
}
// 366 is the end user list.
// Close everything so we can display the results.
if ($id == '366') {
// fclose($handle); // Close the log.
fclose($socket); // Close the connection.
break; // End the loop.
}
// 353 is the user list.
// This should get the number of ops and users on the provided channel.
if ($id == '353') {
$users = explode(' ',$message); // The array of users.
$total_count = $total_count + count($users) - 1; // Add the number of new users, subtract the blank one on the end.
foreach($users as $status) { // Run through the users looking for ops.
$pos = strstr($status,$irc_op); // See if it's an op.
if ($pos !== false) { // If we find an op, add 1.
$op_count = $op_count + 1;
}
}
}
}
// The total less the ops is how many other users are present.
// The subtracted 1 is because I think the script is counting itself as well.
$user_count = $total_count - $op_count - 1;
// Display the number of users and ops.
if ($user_count == 1) {
echo "1 user and ";
} else {
echo "$user_count users and ";
}
if ($op_count == 1) {
echo "1 op are in $irc_channel.";
} else {
echo "$op_count ops are in $irc_channel.";
}
?>