1#!/usr/local/bin/perl
2
3use strict;
4use Irssi;
5use Irssi::Irc;
6use vars qw($VERSION %IRSSI);
7
8$VERSION = "0.0.1";
9%IRSSI = (
10    authors     => 'Filippo \'godog\' Giunchedi',
11    contact     => 'filippo\@esaurito.net',
12    name        => 'kblamehost',
13    description => 'Kicks (and bans) people with >= 4 dots in theirs hostname',
14    license     => 'GNU GPLv2 or later',
15    url         => 'http://esaurito.net',
16);
17
18# TODO
19# add ban support
20
21# all settings are space-separated
22Irssi::settings_add_str('misc','kblamehost_channels',''); # channels with kicklamehost enabled
23Irssi::settings_add_str('misc','kblamehost_exclude',''); # regexps with hostnames excluded
24Irssi::settings_add_str('misc','kblamehost_dots','4'); # dots >= an host will be marked as lame
25Irssi::settings_add_str('misc','kblamehost_kickmsg','Lame host detected, change it please!'); # on-kick message
26Irssi::settings_add_str('misc','kblamehost_ban','0'); # should we ban that lame host?
27
28sub event_join
29{
30    my ($channel, $nicksList) = @_;
31    my @nicks = @{$nicksList};
32    my $server = $channel->{'server'};
33    my $channelName = $channel->{name};
34	my $channel_enabled = 0;
35	my @channels = split(/ /,Irssi::settings_get_str('kblamehost_channels'));
36	my @excludes = split(/ /,Irssi::settings_get_str('kblamehost_exclude'));
37
38	foreach (@channels)
39	{
40		$channel_enabled = 1 if($_ eq $channelName);
41	}
42
43	foreach (@nicks)
44	{
45		my $hostname = substr($_->{host},index($_->{host},'@')+1);
46		my @dots = split(/\./,$hostname); # yes i know, it's the number on fields in
47										  # hostname, but array counts from 0 so element's count is number of dots
48		my $is_friend = 0;
49
50		foreach my $exclude (@excludes)
51		{
52			$is_friend = 1 if ($hostname =~ $exclude);
53		}
54
55		if( $#dots >= Irssi::settings_get_str('kblamehost_dots') && $channel_enabled == 1 && $is_friend == 0)
56		{
57			# Irssi::print("lamehost ($hostname) by $_->{nick} detected on $channelName, kicking...");
58			$server->command("kick $channelName $_->{nick} Irssi::settings_get_str('kblamehost_kickmsg')");
59			$server->command("ban $channelName $_->{nick}") if ( Irssi::settings_get_str('kblamehost_ban') );
60		}
61	}
62}
63
64Irssi::signal_add_last("massjoin", "event_join");
65