# ho_easykline.pl
#
# $Id$
#
# Part of the Hybrid Oper Script Collection (I hope)
#
# Easy K-lines.
#

# ======[ Script Header ]===============================================

($VERSION) = '$Revision$' =~ / (\d+\.\d+) /;
%IRSSI = (
    authors     => "Joost Vunderink",
    contact	=> "garion@efnet.nl", 
    name        => "ho_easykline",
    description => "Makes K-lining drones as easy as cake.",
    license	=> "Public Domain",
    url		=> "http://www.garion.org/irssi/hosc.php",
    changed	=> "25/02/2003 19:29",
    changes	=> "v1.0 - initial version"
);

# ======[ Credits ]=====================================================
#
# Thanks to:
# JamesOff - for letting me rip code from ho_operwall shamelessly
#

# ======[ Globals ]=====================================================

# Master switch to prevent accidents.
my $enabled = 0;

my $klineuseronly = 1;

# ======[ Signal hooks ]================================================

# --------[ event_send_text ]-------------------------------------------

# catch a line typed in the easykline window, and process it.
sub event_send_text {
  my ($data, $server, $witem) = @_;
  my $active_window = Irssi::active_win();

  if ($active_window->{name} ne "easykline") {
    return;
  }
  
  if ($data =~ /^on$/i || $data =~ /^enable$/i) {
    Irssi::print("Enabling easy K-lines.");
    $enabled = 1;
    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^off$/i || $data =~ /^disable$/i) {
    Irssi::print("Disabling easy K-lines.");
    $enabled = 0;
    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^help$/i) {
    print_help();
    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^time ([0-9]+)$/i) {
    set_kline_time($1);
    Irssi::print("Setting K-line time to $1.");
    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^reason (.+)$/i) {
    Irssi::print("Setting K-line reason to $1.");
    set_kline_reason($1);
    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^klineuser ?(.*)$/i) {
    if ($1 =~ /on/i || $1 == 1) {
      Irssi::settings_set_bool('ho_easykline_useronly', 1);
      Irssi::print("K-lining *user@host.");
    } else {
      Irssi::settings_set_bool('ho_easykline_useronly', 0);
      Irssi::print("K-lining *@host.");
    }

    Irssi::signal_stop();
    return;
  }

  if ($data =~ /^status$/i) {
    show_status();
    Irssi::signal_stop();
    return;
  }

  if ($enabled == 0) {
    Irssi::print("Easy K-lines disabled. Type 'help' for help and 'on' to enable.");
    Irssi::signal_stop();
    return;
  }

  kline_from_line($server, $data);
}


sub am_i_opered {
  my $umode = Irssi::active_win()->{'active_server'}->{'usermode'};

  if ($umode =~ /o/) {
    return 1;
  }
  
  return 0;
}


sub set_kline_time {
  my ($time) = @_;
  Irssi::settings_set_int('ho_easykline_time', $time);
}

sub set_kline_reason {
  my ($reason) = @_;
  Irssi::settings_set_str('ho_easykline_reason', $reason);
}


sub show_status {
  my $klineuseronly = Irssi::settings_get_bool('ho_easykline_useronly');
  my $klinetime = Irssi::settings_get_int('ho_easykline_time');
  my $klinereason = Irssi::settings_get_str('ho_easykline_reason');
  Irssi::print("Enabled is $enabled. Time is $klinetime.");
  if ($klineuseronly) {
    Irssi::print("K-lining *user\@host.");
  } else {
    Irssi::print("K-lining *\@host.");
  }

  Irssi::print("Reason is $klinereason.");
}

sub print_help {
  Irssi::print("Short help for now.");
  Irssi::print("Anything you paste in this window gets searched for user\@host and those hostnames get K-lined.");
  Irssi::print("Available settings: enable/disable, time, reason, klineuseronly.");
  Irssi::print("Typing the following into this window will change settings:");
  Irssi::print("on|off: turns the script on or off.");
  Irssi::print("time <time>: sets the k-line time. 0 for perm kline.");
  Irssi::print("reason <reason>: sets the k-line reason.");
  Irssi::print("klineuser <on|off>: toggles the k-lining of *user@host and *@host.");
  Irssi::print("Type 'status' to get the current status of easykline.");
}

sub kline_from_line {
  my ($server, $line) = @_;

  if (!am_i_opered()) {
    Irssi::print("Please oper up before using this script.");
    return;
  }

  #Irssi::print("K-lining [$line]");

  if ($line =~ /\b~?(.{1,10})@([a-zA-Z0-9_.-]+)\b/) {
    my ($user, $host) = ($1, $2);
    my $klineuseronly = Irssi::settings_get_bool('ho_easykline_useronly');
    my $klinetime = Irssi::settings_get_int('ho_easykline_time');
    my $klinereason = Irssi::settings_get_str('ho_easykline_reason');
    if ($klineuseronly == 1) {
      $user = "*" . $1;
    } else {
      $user = "*";
    }
    if ($klinetime == 0) {
      Irssi::print("K-lined $user\@$host :$klinereason");
      $server->command("quote kline $user\@$host :$klinereason");
    } else {
      Irssi::print("K-lined $klinetime $user\@$host :$klinereason");
      $server->command("quote kline $klinetime $user\@$host :$klinereason");
    }
  }
}


# ======[ Initialization ]==============================================

# --------[ formats ]--------------------------------------------------

Irssi::theme_register([
	#i like my nicks right-aligned to 9 chars, with overspill
	'operwall', '[{nick $[!-9]0}] $1-',
	'locops', '[{nick $[!-9]0}] $1-'
]);


# ======[ Setup ]=======================================================

# --------[ Register signals ]------------------------------------------

Irssi::signal_add('send text', "event_send_text");

# --------[ Register commands ]-----------------------------------------

# --------[ Register settings ]-----------------------------------------

# Time of K-lines.
Irssi::settings_add_int('ho', 'ho_easykline_time', 1440);

# K-line reason.
Irssi::settings_add_str('ho', 'ho_easykline_reason', 'drones/flooding');

# Kline *user@h instead of *@h
# Set to 0 to kline *@h
Irssi::settings_add_bool('ho', 'ho_easykline_useronly', 1);

# --------[ Intialization ]---------------------------------------------

Irssi::print("%GEasy K-line%n script loaded.", MSGLEVEL_CRAP);

my $win = Irssi::window_find_name("easykline");
if (!defined($win)) {
  Irssi::print("You are missing the %Reasykline%n window. Use /WINDOW ".
		"NEW HIDDEN and /WINDOW NAME easykline to create it.\n".
		"Easy K-lines are only available when typing in that window.",
		MSGLEVEL_CRAP);
}

Irssi::print("For help, switch to the window named easykline and type 'help' there.", MSGLEVEL_CRAP);

if (!am_i_opered()) {
  Irssi::print("You'll need to oper up to use this script.");
}

