Hi, I was looking for a script that would display how many users are in a chat, also OPs. For example "There are currently 12 users and 1 op chatting.
I figure Id probly have to make a cron so the script would connect to the channel every so often to figure out how many users are in there and thats where I got stuck. I would I figure out how to get the number of users in a channel?
I did alot of searching and the closest thing I found was this.
Reads Channel-Listing of an IRC Server and saves it into Database. Listing will be refreshed every x-minutes. (create_table function is just to create an example table for saving data.)
made by: favoo
<?php
/**
* server configuration
*/
define ('IRC_SERVER', 'irc.favo.tv');
define ('IRC_PORT', 6667);
define ('IRC_REFRESH', 30); // minutes
/**
* DB Tables
*/
define ('T_CONFIG', 'irc_config');
define ('T_CHANNEL', 'irc_channel');
/**
* connect to database
*/
mysql_connect ("localhost", "login", "password");
mysql_select_db ("database");
/**
* uncomment to create tables on first run
*/
// create_tables ();
/**
* get channels and output
*/
$arr_channels = db_get_channels ();
foreach ($arr_channels as $arr_channel)
{
echo join ("<br>n", $arr_channel);
echo "-----------------<br>n";
}
/**
* db_get_channels
* - return list of all channels either by asking database or IRC Server
*/
function db_get_channels ()
{
/**
* get last update timestamp
*/
$raw = mysql_query ("SELECT UNIX_TIMESTAMP(lastupdate) FROM ".T_CONFIG);
list ($ts_lastupdate) = mysql_fetch_row ($raw);
mysql_free_result ($raw);
/**
* too old?
*/
if ($ts_lastupdate < (time() - (60*IRC_REFRESH)))
{
/**
* block parallel processing by updating lastupdate to current time
*/
mysql_query ("REPLACE INTO ".T_CONFIG." (lastupdate) VALUES (NOW())");
/**
* get channels
*/
if (($arr_channels = irc_get_channels ()) == false)
{
/**
* failed, return with db-source
*/
return db_get_channels ();
}
/**
* clear existing list
*/
mysql_query ("TRUNCATE ".T_CHANNEL);
/**
* insert channels into database
*/
reset ($arr_channels);
$arr_insert[] = array ();
foreach ($arr_channels as $str_channel => $arr_data)
{
$arr_insert[] = "('".mysql_escape_string($str_channel)."', ".intval($arr_data[0]).", '".mysql_escape_string ($arr_data[1])."')";
if (sizeof ($arr_insert) > 25)
{
mysql_query ("INSERT INTO ".T_CHANNEL." (channel, users, topic) VALUES ".join(',', $arr_insert));
unset ($arr_insert);
}
$arr_return[] = array ($str_channel, $arr_data[0], $arr_data[1]);
}
unset ($arr_channels);
/**
* some entries left?
*/
if (sizeof ($arr_insert))
{
mysql_query ("INSERT INTO ".T_CHANNEL." (channel, users, topic) VALUES ".join(',', $arr_insert));
}
unset ($arr_insert);
}
/**
* get input from DB
*/
else
{
$raw = mysql_query ("SELECT channel, users, topic FROM ".T_CHANNEL);
while ($arr_return[] = mysql_fetch_assoc ($raw))
{ /* hi there :) */ }
mysql_free_result ($raw);
}
/**
* return arary of channels
* [0] = #channel
* [1] = users
* [2] = topic
*/
return $arr_return;
}
/** <- end of db_get_channels () */
/**
* irc_get_channels
* - connects to IRC Server and returns a multidimensional array of all channels
*/
function irc_get_channels ()
{
/**
* open socket to server
*/
$hwnd_irc = fsockopen (IRC_SERVER, IRC_PORT);
/**
* connection failed?
*/
if (!$hwnd_irc)
return false;
/**
* use random nick & identification
*/
$str_nick = chr(rand(65,90)).time().rand(1,10000);
fputs ($hwnd_irc, "USER ".$str_nick." ".IRC_SERVER." ".IRC_SERVER." :Channelreaderrn");
fputs ($hwnd_irc, "NICK ".$str_nick."rn");
/**
* get data until we're finally connected & verified
*/
$arr_channels = array ();
while (!feof($hwnd_irc))
{
$str_line = fgets ($hwnd_irc, 1024);
/**
* response to PING requests
*/
if (substr($str_line,0,5) == 'PING ')
fputs ($hwnd_irc, "PONG ".trim(array_pop(explode(':', $str_line)))."rn");
/**
* switch between response ID's
*/
$arr_line = explode(' ', $str_line);
switch ($arr_line[1])
{
/**
* "End of Motd" (376)
*/
case 376:
/**
* send LIST request
*/
fputs ($hwnd_irc, "LISTrn");
break;
/**
* channel list
*/
case 322:
/**
* 0 = sender
* 1 = ID
* 2 = target
* 3 = channels name
* 4 = users in channel
*/
$arr_channels[$arr_line[3]] = array ();
$arr_channels[$arr_line[3]][0] = $arr_line[4];
$arr_channels[$arr_line[3]][1] = trim (substr ($str_line, strpos($str_line, ':', 5)));
break;
/**
* End of List
*/
case 323:
fputs ($hwnd_irc, "QUITrn");
break 2;
}
}
fclose ($hwnd_irc);
/**
* got no list?
*/
if (!sizeof ($arr_channels))
return false;
/**
* return hash of all channels
* [#channel]
* with an array of users & topic
* [0] = users
* [1] = topic
*/
return $arr_channels;
}
/** <- end of irc_get_channels () */
/**
* create_tables
* - create table structure
*/
function create_tables ()
{
$arr_query[] = "DROP TABLE IF EXISTS `irc_channel`";
$arr_query[] = "CREATE TABLE `irc_channel` (
`channel` varchar(32) NOT NULL default '',
`users` smallint(5) unsigned NOT NULL default '0',
`topic` varchar(255) NOT NULL default '',
PRIMARY KEY (`channel`)
) TYPE=MyISAM";
$arr_query[] = "DROP TABLE IF EXISTS `irc_config`";
$arr_query[] = "CREATE TABLE `irc_config` (
`lastupdate` datetime NOT NULL default '0000-00-00 00:00:00'
) TYPE=MyISAM";
foreach ($arr_query as $str_query)
mysql_query ($str_query);
}
/** <- end of create_tables () */
?>
Sorry if this is a long post but there was no way to shorten this without confusing you guys . Anyway How would I have that just grab the amount of users and ops in a specific channel. If finding the ops to hard then how would I just get the number of users?
Much thanks to whoever helps me.