use Purple;

my $PLUGIN;
my $PLUGIN_NAME = "Perl Script";
my $PLUGIN_SETTINGS = "/plugins/undermined/variableparser";

%PLUGIN_INFO = (
	perl_api_version			=>	2,
	name				=>	$PLUGIN_NAME,
	version				=>	"0.0.1 Beta",
	summary				=>	"Allows You To Use Embed Perl In Your Status And Profile Messages",
	description			=>	"",
	author				=>	"NSA",
	url				=>	"http://www.undermined.org/",

	load				=>	"plugin_load",
	unload				=>	"plugin_unload",
	prefs_info			=>	"plugin_preferences",
	plugin_action_sub			=>	"plugin_action_names"
);

%plugin_actions = (
	"Update All Accounts" => \&update_accounts
);

sub plugin_init {
	return %PLUGIN_INFO;
}
sub plugin_load {
	$PLUGIN = shift;

	Purple::Prefs::add_none($PLUGIN_SETTINGS);
	Purple::Prefs::add_none("$PLUGIN_SETTINGS");
	Purple::Prefs::add_bool("$PLUGIN_SETTINGS/parseim", 0);
	Purple::Prefs::add_bool("$PLUGIN_SETTINGS/parseprofile", 0);
	Purple::Prefs::add_bool("$PLUGIN_SETTINGS/parsestatus", 0);
	Purple::Prefs::add_int("$PLUGIN_SETTINGS/refreshrate", 300);

	Purple::Signal::connect(Purple::Conversations::get_handle(), "sending-im-msg", $PLUGIN, \&callback_sending_im_msg);
	Purple::Signal::connect(Purple::Accounts::get_handle(), "account-set-info", $PLUGIN, \&callback_account_set_info);

	update_accounts();
}
sub plugin_unload {
	$PLUGIN = shift;
	
	Purple::Signal::disconnect(Purple::Accounts::get_handle(), "account-set-info", $PLUGIN);
	Purple::Signal::disconnect(Purple::Conversations::get_handle(), "sending-im-msg", $PLUGIN);
}
sub plugin_preferences {
	$frame = new Purple::PluginPref::Frame();

	$ppref = Purple::PluginPref->new_with_name_and_label("$PLUGIN_SETTINGS/parseim", "Parse IM Messages");
	$frame->add($ppref);

	$ppref = Purple::PluginPref->new_with_name_and_label("$PLUGIN_SETTINGS/parseprofile", "Parse Profiles");
	$frame->add($ppref);
	
	$ppref = Purple::PluginPref->new_with_name_and_label("$PLUGIN_SETTINGS/parsestatus", "Parse Status Messages");
	$frame->add($ppref);
	
	$ppref = Purple::PluginPref->new_with_name_and_label("$PLUGIN_SETTINGS/refreshrate", "Refresh Rate (seconds)");
	$ppref->set_bounds(0, 60*60*24);
	$frame->add($ppref);

	return $frame;
}
sub plugin_action_names {
	foreach $key (keys %plugin_actions) {
		push @array, $key;
	}
	return @array;
}

$UPDATING_USERINFO = 0;
sub callback_account_set_info {
	if ($UPDATING_USERINFO == 0) {
		my $account = shift;
		$UPDATING_USERINFO = 1;
		update_account($account);
		$UPDATING_USERINFO = 0;
		Purple::timeout_add($PLUGIN, Purple::Prefs::get_int("$PLUGIN_SETTINGS/refreshrate"), \&callback_account_set_info, $account);
	}
}
sub callback_sending_im_msg {
	my ($account, $reciever, $message) = @_;
	if (Purple::Prefs::get_bool("$PLUGIN_SETTINGS/parseim")) {
		$_[2] = parsescripts($message);
	}
}


$PARSED_CACHE = "";
sub update_accounts {
	@accounts = Purple::Accounts::get_all_active();
	foreach $account (@accounts) {
		update_account($account);
	}
	$PARSED_CACHE = "";
}
sub update_account {
	my $account = shift;
	if ($account->is_connected()) {
		if (Purple::Prefs::get_bool("$PLUGIN_SETTINGS/parseprofile")) {
			my $userinfo = $account->get_user_info();
			my $newinfo = parsescripts($userinfo);
			Purple::Serv::set_info($account->get_connection(), $newinfo);
		}
		if (Purple::Prefs::get_bool("$PLUGIN_SETTINGS/parsestatus")) {
			my $status = Purple::Account::get_active_status($account);
			#my $userinfo = $status->get_message();
			#Purple::Debug::info("blah", "$userinfo");
		}
	}
}


sub parsescripts {
	my $message = shift;
	$message =~ s/&lt;% (.*?) %&gt;/eval Purple::Util::Markup::strip_html($1)/egso;
	return $message;
}