use Purple;

my $PLUGIN;

%PLUGIN_INFO = (
	perl_api_version	=>	2,
	name				=>	"Commands",
	version				=>	"0.0.2 Beta",
	summary				=>	"Random Collection Of Commands",
	description			=>	"Includes:\neval\nexec\nreverse\nhaxor\nrawhtml",
	author				=>	"NSA",
	url					=>	"http://www.undermined.org/",

	load				=>	"plugin_load",
	unload				=>	"plugin_unload"
);


%commands = (
	eval	=>	\&command_eval,
	exec	=>	\&command_exec,
	reverse	=>	\&command_reverse,
	haxor	=>	\&command_haxor,
	rawhtml	=>	\&command_rawhtml
);


sub plugin_init {
	return %PLUGIN_INFO;
}

sub plugin_load {
	$PLUGIN = shift;

	Purple::Signal::connect(Purple::Conversations::get_handle(), "sending-im-msg", $PLUGIN, \&callback_sending_im_msg);

	Purple::Prefs::add_none("/plugins/undermined");
}

sub plugin_unload {
	$PLUGIN = shift;
	Purple::Signal::disconnect(Purple::Conversations::get_handle(), "sending-im-msg", $PLUGIN);
}

sub callback_sending_im_msg {
	my ($account, $reciever, $message) = @_;

	$message = Purple::Util::Markup::strip_html($message);

	if ($message =~ s/^\/([a-z]+)\s?//i) {
		if (exists $commands{$1}) {
			$message = $commands{$1}($account, $reciever, $message);
			$_[2] = "$message";
		}
	}
}



sub command_eval {
	return eval $_[2];
}
sub command_exec {
	return readpipe $_[2];
}

sub command_reverse {
	return reverse $_[2];
}
sub command_haxor {
	my %haxor = (
		'a'	=>	'4',
		'e'	=>	'3',
		'l'	=>	'1',
		'o'	=>	'0',
		's'	=>	'5',
		't'	=>	'7'
	);

	my $message = lc($_[2]);
	my ($key, $value);
	while (($key, $value) = each(%haxor)) {
		$message =~ s/$key/$value/g;
	}
	$message =~ s/([a-z])([a-z])/$1.uc($2)/ge;
	return $message;
}
sub command_rawhtml {
	my $message = $_[2];
	$message = Purple::Util::unescape_html($message);
	return $message;
}
