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

# 1.0.0 - Initial version
# 1.0.1 - Add expiring & reasons.
# 1.0.1.1 - Nicks are case insensitive, doh.

# TODO
# timeout failed whois attempts
# exemptions + kill

$VERSION = "1.0.1.1";
%IRSSI = (
	authors => "Tom Wesley <tom\@tomaw.net>",
	contact => "tom\@tomaw.net",
	name    => "kline",
	description => "klines based on IP address reported in whois responses",
	license => "Public Domain"
);

my %kline_params={};

sub kline {
	my ($server, $nick) = @_;

	return if $server->{chat_type} ne "IRC";
	$server->redirect_event( "whois", 1, $nick, 0, "redir autowhois_default",
		{
			"event 327" => "redir autowhois_hostname",
			"" => "event empty"
		}
	);
	$server->send_raw("WHOIS $nick");
}

sub event_whois_hostname {
	my ($server, $data) = @_;
	my ($mynick, $nick, $rdns, $ip) = split(/ +/, $data, 5);
	my ($time, $reason) = split(/ +/, $kline_params{lc $nick}, 2);
	delete $kline_params{lc $nick};
	$server->send_raw("KLINE $time *\@$ip :$reason");
	Irssi::print("KLINE $time *\@$ip :$reason");
}

sub cmd_kline_proper {
	my ($data, $server, $window) = @_;
	if (!$server || !$server->{connected})
	{
		Irssi::print("Not connected.");
		return;
	}
	my ($time, $nick, $params) = split(/ +/, $data, 3);
	$nick = lc $nick;
	$kline_params{$nick} = "$time $params";
	kline($server, $nick);
}

Irssi::signal_add( { 'redir autowhois_hostname'      => \&event_whois_hostname });
Irssi::command_bind("kline_proper", \&cmd_kline_proper);
Irssi::settings_add_str("kline.pl", "kline_exempt", "");

