#-------------------------------------------------------------------------------
# $Id: usercount2file.pl 1770 2007-11-14 09:13:06Z jnieuwen $
#-------------------------------------------------------------------------------
# (c) 2007 Jeroen van Nieuwenhuizen
#-------------------------------------------------------------------------------
# The use clauses
#-------------------------------------------------------------------------------
use strict ;

use POSIX qw(strftime);
use Irssi;
use Irssi::Irc;

my $VERSION = '$Id: usercount2file.pl 1770 2007-11-14 09:13:06Z jnieuwen $';
my %IRSSI = (
        "authors"       => "Jeroen van Nieuwenhuizen",
        "contact"       => "contact [at] jeroen [dot] se",
        "name"          => "Puts the number of bitlbee online users in the statusbar",
        "description"   => "Writes usercounts to a file",
        "url"           => "http://www.jeroen.se",
        "license"       => "GNU GPL v2",
        "changed"       => "2007-11-14"
);


my $g_timer ;
my $g_file = '' ;
my $ircops ;
my $ops ;
my $halfops ;
my $voices ;
my $normal ;

#-------------------------------------------------------------------------------
sub calc_users {
	my $channel = shift;
	my $server = $channel->{server};

	$ircops = $ops = $halfops = $voices = $normal = 0;
	for ($channel->nicks()) {
		if ($_->{serverop}) {
			$ircops++;
		}
		if ($_->{op}) {
			$ops++;
		} elsif ($_->{halfop}) {
			$halfops++;
		} elsif ($_->{voice}) {
			$voices++;
		} else {
			$normal++;
		}
	}
	my $total = $ops+$halfops+$voices+$normal;
	Irssi::statusbar_items_redraw('jeroen_se_bitlbee');
}

#-------------------------------------------------------------------------------
sub do_run {
	foreach my $channel (Irssi::channels()) {
		calc_users($channel) if ($channel->{name} eq '&bitlbee') ;
	}
	if (defined $g_timer) {
		Irssi::timeout_remove($g_timer) ;
		$g_timer = undef ;
	}

	$g_timer = Irssi::timeout_add(60000, "do_run", "");
}

sub jeroen_se_bitlbee {
	my ($item, $get_size_only) = @_;
	$item->default_handler($get_size_only, "{sb bitlbee: +$voices $normal}", undef, 1);
}

#-------------------------------------------------------------------------------
# MAIN LOOP
#-------------------------------------------------------------------------------
Irssi::statusbar_item_register('jeroen_se_bitlbee', undef, 'jeroen_se_bitlbee');
Irssi::statusbars_recreate_items();
do_run() ;

