PDA

View Full Version : Users in IRC Channel(s)


FASherman
28 Nov 2002, 05:23
Okay, I'm not sure if this qualifies as a "true" hack or not. Feel free to move this to an appropriate area, if not.

At any rate, I have 18 IRC channels supporting a range of topics. What I needed was something to keep track of how many users where in each channel.

1. Create a tables named ircstat in your vbulletin database. The table consists of two fields, channelname (type = text) and channelusers (type = int, length = 3, Not Null, Default = 0).

2. Insert a row for each IRC channel you want to monitor:
INSERT INTO ircstat(channelname,channelusers) VALUES ('#channel','0')

Case IS important. It must match your IRC channel name EXACTLY.

3. Create a perl script called ircstat.pl. Cut and paste below into it and change the values that need changing:

---START CUT&PASTE NEXT LINE
#!/usr/bin/perl
$server = "change.irc.server";
$port = 6667;
$nick = "PickANick";
$realname = "Just passing through...";
use IO::Socket;
use DBI;
$dbname = "yourdbname";
$dbhost = "yourhost";
$dbuser = "yourdbuser";
$dbpasswd = "yourdbpassword";
$vbdb=DBI->connect("dbi:mysql:".$dbname.":".$dbhost,$dbuser,$dbpasswd);
@channels = ('#channel1','#channel2','#channel3');
$irc=IO::Socket::INET->new( PeerAddr=>$server, PeerPort=>$port, Proto=>'tcp')or die "$server: $@\n";
print $irc "USER $nick $nick $nick :$realname\n";
print $irc "NICK $nick\n";
while(@channels) {
$channel = shift @channels;
print $irc "LIST $channel\n";
while(defined($in=<$irc>)){
chop($in);
if($in=~/PING :(.*)/){
print $irc "PONG $1\n";
}
@field = split(/ /,$in);
if ($channel eq $field[3]) {
$sql = $vbdb->prepare("UPDATE ircstat SET channelusers=$field[4] WHERE channelname=\"$field[3]\"");
$sql->execute;
last;
}
if ($field[1] eq "263") {
print $irc "LIST $channel\n";
}
}
}
close($irc);
-----END CUT&PASTE PREVIOUS LINE----

4. Run this script out of cron. Every 5 minutes if you have 1 or 2 channels, every 10 minutes if you have more than 2 channels.

Remember, all this does is keeps the database updated. with the number of users in your channels. You'll have to decide where you want to display this information and make the appropriate php/template hacks.

KelteN
28 Nov 2002, 05:31
Cool, if I ever setup a IRC channel for my forum. Ill be sure to use this hack/mod ;)

Thx :D

JulianD
28 Nov 2002, 06:46
OK thanks for the script :) All the script to get users in IRC made with PHP doesn't work on my host... I hope this one works :)

futureal
28 Nov 2002, 08:50
Wow, this is something I have been looking for, for a long time. Thanks!

Any idea what the server load might be like? It seems negligible, which is great. Hopefully the IRC servers my forums use will allow it to connect. :)

FASherman
28 Nov 2002, 13:28
Originally posted by futureal
Wow, this is something I have been looking for, for a long time. Thanks!

Any idea what the server load might be like? It seems negligible, which is great. Hopefully the IRC servers my forums use will allow it to connect. :)

They will. They see it as just another client. Server load is practically nothing. Most of the load is on the IRC server, hence the check for "263" messages, which means the server responded that load is too high, please try again.

With 18 channels on a heavily used IRC server, it still completes in just under 3 minutes.

Velocd
28 Nov 2002, 15:42
Some questions:

#1. Does this bog down the server if you are NOT using a dedicated server? This was an issue with the egg-drop method.

#2. How might you store the nick as well for each person in the database? You could probably modify the table to be:

create table ircstat
( channelname text not null primary key,
channelusers smallint(5) not null default'0',
usernicks text not null
);

But within your perl script, how might with the following query:

$sql = $vbdb->prepare("UPDATE ircstat SET channelusers=$field[4] WHERE channelname=\"$field[3]\"");


I could insert as well the nick each person (seperated by commas, maybe using variable $nick or $username?) It would be great if the nick corresponded with the persons actual username on the forum, but that would be much more difficult to achieve.

Anyway, if this is possible I'll defiantly consider installing the hack. ;)

FASherman
28 Nov 2002, 18:00
You do NOT want to gather the nicks of everyone in a channel. Trust me. To do that, you would have to have the script (really a lightweight bot) JOIN each channel, run NAMES in each channel, parse the results, update the database and exit.

Imagine how disruptive it would be for this bot to bounce in and out every 5 minutes or so...

As for the server load, its minimal. The real load is on the IRC server.

FASherman
29 Nov 2002, 17:10
To get it to do what you request, change the script to this:

---START CUT&PASTE NEXT LINE
#!/usr/bin/perl
$server = "change.irc.server";
$port = 6667;
$nick = "PickANick";
$realname = "Just passing through...";
use IO::Socket;
use DBI;
$dbname = "yourdbname";
$dbhost = "yourhost";
$dbuser = "yourdbuser";
$dbpasswd = "yourdbpassword";
$vbdb=DBI->connect("dbi:mysql:".$dbname.":".$dbhost,$dbuser,$dbpasswd);
@channels = ('#Newsdesk','#Education','#Politics-&-Govt','#Military-&-Vets','#DallasMavericks','#TexasRangers','#HoustonTexans','#YouthSports','#bits-&-bytes','#Daily-Web.Info','#technology','#religion-&-faith','#business-&-finance','#health','#SportsPage','#DallasStars','#DallasCowboys','#entertainment ');
$irc=IO::Socket::INET->new( PeerAddr=>$server, PeerPort=>$port, Proto=>'tcp')or die "$server: $@\n";
print $irc "USER $nick $nick $nick :$realname\n";
print $irc "NICK $nick\n";
while(@channels) {
$channel = shift @channels;
print $irc "JOIN $channel\n";
while(defined($in=<$irc>)){
chop($in);
if($in=~/PING :(.*)/){
print $irc "PONG $1\n";
}
@field = split(/ /,$in);
if ($field[1] eq "353") {
@users = split(/:/,$in);
$users[2] =~ s/@//g;
$users[2] =~ s/\+//g;
$users[2] =~ s/ PutNickHere//; # <----Don't forget!
@nicks = split(/ +/,$users[2]);
$sql = $vbdb->prepare("UPDATE ircstat SET channelusers=$#nicks, channelnicks=\"$users[2]\" WHERE channelname=\"$field[4]\"");

$sql->execute;
print $irc "PART $channel\n";
last;
}
}
}
close($irc);
-----END CUT&PASTE PREVIOUS LINE----

This runs considerably faster, 20 seconds for 18 channels instead of 3 minutes, but it does have drawbacks.

First, is whatever time interval you set the script to run via cron, thats how often this user is going to join/leave the channel. The more often you do it, the more annoying it is. The less often you do it, the more stale your data becomes.

If you have channel bots, make sure they do not do an on_join greet to this nick. It just increases the annoyance factor. Also make sure this nick is exempy from excessive join/part bans.

This script is an example of the axiom, "Be careful what you wish for, you may get it".

GoTTi
29 Nov 2002, 19:08
im confused as hell...ive never used cron jobs, but this would be of some good for me...

can you put the instructions in simpl form with more detailed instructions...it is kinda throwing me off...

example:
4. Run this script out of cron. Every 5 minutes if you have 1 or 2 channels, every 10 minutes if you have more than 2 channels.


I dont know how to use cron, so what would be the commands that I would use?

Velocd
29 Nov 2002, 19:55
@da_gotti: http://www.aota.net/Script_Installation_Tips/cronhelp.php4

@FASherman: thanks for the script, I'll try it out and see how much of an annoyance it might be ;)

FASherman
29 Nov 2002, 21:06
Originally posted by Velocd
@FASherman: thanks for the script, I'll try it out and see how much of an annoyance it might be ;)

I've go it running every 5 minutes and gave it the nick Census. With a nick that reflected what it was doing, it became less annoying.

This page uses its output: http://www.daily-web.info/modules.php?s=&name=Live

Open two browsers to this page. Leave one there. In the second, enter one of the discussion rooms. Doesn't matter which one. Wait until you see the census bot join and leave.

Refresh the first browser and verify that the info updated and that your nick in listed in the room.

Velocd
29 Nov 2002, 21:39
Hmm, well I waited for about 10 minutes in a channel yet no census ever showed. Nor the page ever update. :ermm:

FASherman
29 Nov 2002, 22:43
You're correct. As I said, the script is really an lightweight IRCbot and, like many bots, it got temporarily k-lined. I switched it over to a new bot-friendly server and its fine.

Scrooge
01 Dec 2002, 03:08
First of all, I've been begging for this for months....thank you!

I could use a little help though. I'm not sure exactly how to fill out some of the values.

INSERT INTO ircstat(channelname,channelusers) VALUES ('#channel','0')

Okay, you stated that it is case sensitive, so I assume something should be edited here. I only have one channel, #reefaquariumguide, do I enter this into the #channel spot? And do I leave a 0 after it?


In this line from the perl script:

@channels = ('#channel1','#channel2','#channel3');

Do I edit that with the name of my channel as well?

I've got it setup now but get nothing. I am monitoring the chat and the bot never shows up. Just in case I setup the cron wrong I also tried running it in the command prompt. I checked the error logs and I don't see any errors it caused, but nothing shows up in the chat.

Thanks for any help :)

Scrooge
01 Dec 2002, 04:29
Okay, solved my own question through trial and error I think. For others who might be as dumb as me.....put your channel in the first part I asked about....the second part stays as is.

The database is updating for me now. Could you give me a quick clue on what to edit in the php/templates? I tried putting $channelusers into the forumhome template but think I need to do something else because nothing displays.

Scrooge
01 Dec 2002, 23:02
Any help? Or maybe at least point me to a place for the answer?

Scrooge
02 Dec 2002, 18:15
I'm feeling soooo unloved :( :(

Come on guys, someone help me out? I have the hack basically working, but being a PHP/SQL dummy I don't know how to make the variables work so I can display it. I know how to edit a template and put the variable where I want it.....but it is empty. I think I need to edit index.php maybe? I've been waiting for a who's in chat hack that didn't require eggdrop since January.......now I'm so close but not quite there.

FASherman
03 Dec 2002, 02:51
You're on the right track. You have a variable in a template, but it contains nothing because it hasn't been loaded. Go into index.php and add the sql commands to load the variable.

Sorry for the late response, but I've been working on a tcl script for an eggdrop bot that does the database inserts directly via Mytcl on JOINs PARTs KICKs, QUICKs and server pings. That way the data will always be current since it'll be even driven, not time driven.

deepdark
11 Apr 2003, 22:13
any help about mysql query please how to add can any body send to me a copmleted copy paste query please