use strict;
use Irssi 20020101.0250 ();
use vars qw($VERSION %IRSSI);
$VERSION = "0.1";

#
# ezbounce auto login/connect
# Copyright (c) 2004 Benoit Bourdin (bennyben)
#
# usage:
#  1) Be sure to set bouncer login and password:
#    /set ezbounce_login <my_login>
#    /set ezbounce_pass <my_pass>
#  2) put as password the server to connect to
#    for example:
#      /ircnet add freenode
#      /server add -ircnet freenode ircgateway.mydomain.org 5555 irc.freenode.net
#     and to connect: /server freenode
#

%IRSSI = (
    authors     => "Benoit Bourdin (bennyben)",
    contact     => "bennyben\@rezosup.net",
    name        => "ezbounce auto login/connect",
    description => "automatically login and connect via ezbounce (irc bouncer)",
    license     => "GPL",
    url         => "http://irssi.org/"
);

sub on_notice {
	my ($server, $data, $nick, $address) = @_;
	my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;

		# bouncer notice
	if ($nick eq "(ezbounce)") {
	
		if ($text eq "[awaiting login/pass command]") {
			$server->command("^QUOTE login ".Irssi::settings_get_str('ezbounce_login')." ".Irssi::settings_get_str('ezbounce_pass'));
			Irssi::print "bouncer: logging in...";
		
		} elsif ($text eq "[use /quote CONN <server> to connect]") {
			my $serv = $server->{password};
			if($serv ne "") {
				$server->command("^QUOTE CONN ".$serv);
				Irssi::print "bouncer: connecting to ".$serv."...";
			}
		}
	}
};

Irssi::settings_add_str($IRSSI{'name'}, 'ezbounce_login', 'default_login');
Irssi::settings_add_str($IRSSI{'name'}, 'ezbounce_pass', 'default_pass');

Irssi::signal_add('event notice', 'on_notice');
