# script to filter DAUs with alternating Guest nick names
# (C) 2007-08-12 Georg Lukas <georg@boerde.de>

use Irssi;

my $timeout = 90;
my @channels = ("#gbo.idle", "#irpg2");

my %tracker = ();

sub frobnicate {
	my ($newnick, $oldnick) = @_;
	if ($newnick =~ "Guest[0-9]*") {
		$g = $newnick;
		$n = $oldnick;
	} elsif ($oldnick =~ "Guest[0-9]*") {
		$g = $oldnick;
		$n = $newnick;
	} else {
		return 0;
	}
	#Irssi::print("checking $n ($g)");
	if (time < $tracker{$n} + $timeout) {
		# less than $t s... ignoring
		#Irssi::print("ignoring $n ($g)");
		$tracker{$n} = time;
		return 1;
	} else {
		#Irssi::print("updating $n ($g)");
		$tracker{$n} = time;
		return 0;
	}
}
		

sub cleanup {
	while (($key, $value) = each(%tracker)){
		if (time > $value + $timeout) {
			#Irssi::print("removing $key");
			delete($tracker{$key});
		}
	}
}

sub guestfilter {
	my ($server, $newnick, $oldnick, $address) = @_;

	if (frobnicate($newnick, $oldnick)) {
		#Irssi::print("hidden $oldnick -> $newnick");
		Irssi::signal_stop();
	}
	cleanup()
}

Irssi::signal_add('message nick', 'guestfilter');
