use Gaim;

%PLUGIN_INFO = (
	perl_api_version => 2,
	name             => 'LoginState',
	version          => '1.0',
	summary          => 'Automatically change state after login',
	description      => "To configure initial states create file ~/.gaim/LoginState.conf with lines in format:\n\t'account' => 'state',\n\nFor example:\n\t'105083775' => 'Невидимый',\n\nWARNING: state must be in your locale and in utf8.",
	author           => 'Alex Efros <powerman@powerman.name>',
	url              => 'http://powerman.name/',
	load             => 'plugin_load',
	unload           => 'plugin_unload'
);

%LoginState = eval scalar `cat ~/.gaim/LoginState.conf`;

sub LOG { Gaim::debug_info($PLUGIN_INFO{name}, "$_[0]\n") }

sub plugin_init {
	return %PLUGIN_INFO;
}

sub plugin_load {
	my $plugin = shift;
    Gaim::signal_connect(Gaim::Connections::handle, 'signed-on',
                         $plugin, \&signed_on_cb, q{});
}

sub plugin_unload {
	my $plugin = shift;
}

sub signed_on_cb { 
    my ($gc, $data) = @_;
    my $account = $gc->get_account();
    my $username = $account->get_username();
    LOG("Account $username signed on.");
    if (exists $LoginState{$username}) {
        LOG("Changing $username state to $LoginState{$username}...");
        $account->set_away($LoginState{$username}, q{});
    }
} 

