use strict;

BEGIN {
	use vars '$irssi', '$xchat';
	eval q {
		use Irssi;
	};
	$irssi = !$@;
	eval q {
		IRC::get_info (1);
	};
	$xchat = !$@;
}

if ($xchat) {
	IRC::register ("XChat Agonizer", "0.1", "Capitalizes the last letter in every word outputted by the user, for extra annoyance.", "");
	IRC::print ("Loading \0034XChat Agonizer\003 by Primer");
	IRC::add_command_handler ('', \&agonize);
} elsif ($irssi) {
	Irssi::print ("Loading \0034IRSSI Agonizer\003 by Primer");
	Irssi::signal_add ('send text', 'agonize');
}

sub agonize {
	if ($xchat) {
		my $msg = shift;
		$msg =~ s/(.)\b/uc $1/eg;
		my $channel = IRC::get_info (2);
		my $nick = IRC::get_info (1);
		IRC::print ("\0036\<\003$nick\0036\>\003\t$msg");
		IRC::command ("/privmsg $channel :$msg");
	} elsif ($irssi) {
		my ($msg, $server, $witem) = @_;
		$msg =~ s/(.)\b/uc $1/eg;
		Irssi::signal_stop ();
		Irssi::signal_remove ('send text', 'agonize');
		Irssi::signal_emit ('send text', $msg, $server, $witem);
		Irssi::signal_add ('send text', 'agonize');
	}
	return 1;
}
