use strict;
use warnings;
use Irssi;
use vars qw($VERSION %IRSSI);

$VERSION = "1.0";

%IRSSI = (
    authors     => 'Doug Freed, Unit 193',
    contact     => 'Unit193@OFTC',
    name        => 'holdjoin.pl',
    description => 'Holds join to channels until you\'ve been authenticated with services',
    license     => 'GPLv3+',
);

Irssi::settings_add_str('misc', 'oftc_server_tag', 'OFTC');
Irssi::signal_add('server connected', 'server_connected');

sub server_connected() {
	my ($server) = @_;
	return unless ($server->{tag} eq Irssi::settings_get_str('oftc_server_tag'));
	Irssi::signal_add_first('event connected', 'event_connected');
}

sub event_connected() {
	my ($server) = @_;
	return unless ($server->{tag} eq Irssi::settings_get_str('oftc_server_tag'));
        Irssi::signal_add_first('user mode changed', 'sig_mode_change');
	Irssi::signal_remove('event connected', 'event_connected');
	Irssi::signal_stop();
}

sub sig_mode_change() {
	my ($server) = @_;
	return unless ($server->{tag} eq Irssi::settings_get_str('oftc_server_tag'));
	if ($server->{usermode} =~ /R/) {
		Irssi::signal_remove('user mode changed', 'sig_mode_change');
		Irssi::signal_emit('event connected', $server);
	}
}
