#!/usr/bin/perl -w

# Edit the line below!!!
# do NOT use the tilde (~), always state the full pathname.
# In windows, include the drive letter (e.g. c:/foo/bar/reasons )
#
my $kreasonfile = "/home/apollon/.xchat2/reasons";


#
# Do not edit anything below this line :p


IRC::register('LucyScript','0.7','','');
IRC::add_command_handler('KICK','kick');
IRC::add_command_handler('KB', 'kickban');
IRC::add_command_handler('OPME', 'opme');
IRC::add_command_handler("RANDOM", "randkick");
IRC::print("Lucy script loaded");
IRC::print("Commands: /kick          Random reason kick");
IRC::print("          /kb            Random reason kickban");
IRC::print("          /opme          Request an op from chanserv");
IRC::print("          /random        kick a random user");

$vers = IRC::get_info(0);

sub kick {
        my ($data, $server) = @_;
        my ($nick, $reason) = split(/ /,$data,2);
	my $channel = IRC::get_info(2);
	if ($reason ne "") {
		IRC::send_raw("KICK $channel $nick :$reason\n");
                } else {
		my $randreason=rand_reason();
		IRC::send_raw("KICK $channel $nick :$randreason\n");
                }
        close (KICK);
        return 1;
}

sub rand_reason {
        if (! (open (f, $kreasonfile)) ) {
                IRC::print("Malakia, de briskw to arxeio $kreasonfile.");
                return;
        }
        my $lines = 0; while(<f>) { $lines++; };
        my $line = int(rand($lines))+1;
        my $kreason;
        seek(f, 0, 0); $. = 0;
        while(<f>) {
                next if ($. != $line);
                chomp;
                $kreason = $_;
                last;
        }
        close(f);
        return $kreason;
}

sub kickban {
        my ($data, $server) = @_;
        my ($nick, $reason) = split(/ /,$data,2);
	my $channel = IRC::get_info(2);
if ($reason ne "") {
                IRC::command("/ban $nick 1");
		IRC::send_raw("KICK $channel $nick :$reason\n");
                } else {
		my $randreason=rand_reason();
		IRC::command("/ban $nick 1");
		IRC::send_raw("KICK $channel $nick :$randreason\n");
                }
        return 1;
}

sub opme {
	my $nick = IRC::get_info(1);
	my $channel = IRC::get_info(2);
	IRC::command("/cs op $channel $nick");
	return 1;
}

sub randkick {
	$nick = IRC::get_info(1);
	$chan = IRC::get_info(2);
	$serv = IRC::get_info(3);
	@users = IRC::user_list($chan, $serv);
	$pop = int( ( scalar @users ) / 5) ;
	#IRC::print( "Channel $chan has $pop users." ) ;
	$whom = @users[int( rand( $pop ) ) * 5];
	$pc = int(1000/$pop)/10;
	IRC::command( "/kick $whom YARK [Yet Another Random Kick]: $pc% probability" );
	return 1;
}
