use Irssi;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "1.1";
%IRSSI = (
    authors	=> 'Tom Wesley',
    contact	=> 'tom@tomaw.net',
    name	=> 'logs staffing levels',
    license	=> 'GNU GPLv2',
	changed => '2006-07-02',
);

# Changes:
# 1.1 - Added statusbar item.
#
# To do:
# Make settings more user friendly.
# Multiple net support.

my $timeout_tag = Irssi::timeout_add(300000, 'sig_timeout', undef);
my $tag = 'freenode';
my $log = 'log.log';
my $count = 0;

sub sig_timeout {
	my $server = Irssi::server_find_tag($tag);
	return if !$server;
	Irssi::signal_add("event 249", "incoming");
	Irssi::signal_add("event 219", "done");
	if (Irssi::settings_get_bool('statsp_log'))
	{
		open(LOG, ">>$log");
		my ( $SECOND, $MINUTE, $HOUR, $DAY, $MONTH, $YEAR ) = (gmtime)[ 0 .. 5 ];
		$MONTH += 1;
		$YEAR  += 1900;
		printf LOG "%04d-%02d-%02d %02d:%02d:%02d\n", $YEAR, $MONTH, $DAY, $HOUR, $MINUTE, $SECOND;
	}
	$count = 0;
	$server->command("stats p");
}

sub incoming {
	my ($server, $data, $server_name) = @_;
	if ( $data =~ /.* p :(.*?) (\(.*?\))/) {
		$count++;
		print LOG "$1 $2\n" if Irssi::settings_get_bool('statsp_log');
	}
	Irssi::signal_stop();
}

sub done {
	Irssi::signal_remove("event 249", "incoming");
	Irssi::signal_remove("event 219", "incoming");
	close(LOG);
	Irssi::statusbar_items_redraw('staff');
}

sub staff {
	my ($item, $get_size_only) = @_;
	$item->default_handler($get_size_only, "{sb Staff: ".$count."}", undef, 1);
}

Irssi::settings_add_bool('statsp','statsp_log','');
Irssi::statusbar_item_register('staff', '{sb Staff: $0-}', 'staff');
Irssi::command_bind('staff', 'sig_timeout');
# vim:noexpandtab:ts=4
