#!/usr/bin/perl
#

# Regain nick script 0.1.2
# 2001-2003 Thomas "TommyB" Bauereiß, chef@tommyb-online.de

# Small script to regain a nickname

# Use this to make xchat automatically take back a nickname
# you specify as soon as it is free.
# To do that, add that nick to your notify list, load the script
# and issue /regain <nick>
# Alternatively you can hardcode a default nick to keep by
# editing the line "my $regain_nick = ..." below.
# Don't edit anything below that unless you want to alter the script
# and you know what you're doing.
# You can also send me email at chef@tommyb-online.de

# History
# 2003-09-24   v0.1.2   - Added support for WATCH notify-method
#                         used by DALnet-style servers
# 2002-01-26   v0.1.1   - Small bugfix (see ison_handler)
# 2001-03-18   v0.1.0   - Initial release

# Put something in the quotation marks to set a default nick to regain
# You can also specify a nick at runtime using /regain
my $regain_nick = "";

# -----------------------------------------------------------
# MAGIC BORDER
# Don't cross this this line unless you feel prepared to code
# -----------------------------------------------------------

my $regain_nick_version = "0.1.2";
my $initial_ison_check = 0;

IRC::register("Regain nick", $regain_nick_version, "", "");
IRC::add_message_handler("303", "ison_handler");
IRC::add_message_handler("601", "logoff_handler");
IRC::add_message_handler("605", "logoff_handler");
IRC::add_command_handler("regain", "set_regain_nick");

sub ison_handler
{
	my $return_value = 0;
	if ($initial_ison_check) {
		$initial_ison_check = 0;
		$return_value = 1;
	}

	if ($regain_nick eq "") {
		return $return_value;
	}

	my $line = shift(@_);
	$line =~ /303 .*:([^\r\n]+)/;
	my @on_nicks = split " ", $1;

	my $found = 0;

	foreach $nick (@on_nicks) {
		if (lc($nick) eq lc($regain_nick)) {
			$found = 1;
			last;
		}
	}

	if ($found == 0) {
		take_nick();
	}

	return $return_value;
}

sub logoff_handler {
	my $line = shift(@_);
	my @words = split " ", $line;
	my $nick = $words[3];
	
	if (lc($nick) eq lc($regain_nick)) {
		take_nick();
	}
	return 0;
}

sub take_nick {
	IRC::print "[Regain nick] Changing to nick $regain_nick\n";
	IRC::command("/nick $regain_nick");
}

sub set_regain_nick {
	my $nick = shift(@_);

	if ($nick eq "") {
		IRC::print "Syntax: /REGAIN <nick>   OR   /REGAIN -none\n";
		if ($regain_nick ne "") {
			IRC::print "[Regain nick] Trying to regain nick $regain_nick, put it in your notify list, otherwise things won't work.\003 \n";
		}
	} elsif (lc($nick) eq "-none") {
		$regain_nick = "";
		IRC::print "[Regain nick] Not trying to regain a nick anymore.\n";
	} else {
		$regain_nick = $nick;
		IRC::print "[Regain nick] Now trying to regain nick $regain_nick, put it in your notify list, otherwise things won't work.\n";
		if (IRC::get_info(3)) {
			$initial_ison_check = 1;
			IRC::send_raw("ISON $nick\r\n");
		}
	}
	return 1;
}

IRC::print "::: Regain nick script $regain_nick_version :::\n";
set_regain_nick $regain_nick;

