use vars qw($VERSION %IRSSI);

use Irssi;
use strict;
$VERSION = "0.04";
%IRSSI = (
    authors	=> "tomaw",
    name	=> "Mass voice using ChanServ",
    description	=> "/mvoice and /mdevoice via ChanServ",
    license	=> "Public Domain",
    changed	=> "Sat 25th Feb 2006",
);

#Changes, (I've forgotten the others...)
#
#0.04
#Make blacklist case insensitive, thanks SportChick

sub cmd_mvoice {
	my ($data, $server, $channel) = @_;
	my %voice;
	my $nick;
	my $list;
	my ($extra, $server, $witem) = @_;
	if ($channel->{type} ne "CHANNEL") {
		Irssi::print("Your are not on a channel");
		return;
	}

	my $i = 0;
	my %blacklist = map { lc($_) => $i++ } split(/,/, Irssi::settings_get_str('mvoice_blacklist'));
	foreach $nick ($channel->nicks()) {
		$voice{$nick->{nick}} = 1 if ! $nick->{voice} && ! $nick->{op} && ! exists($blacklist{lc($nick->{nick})});
	}
	if ($list = join " ", sort keys %voice) {
		$channel->command("cs voice $channel->{name} $list $data ");
		#print("cs voice $channel->{name} $list $data");
	}
}

sub cmd_mdevoice {
	my ($data, $server, $channel) = @_;
	my @nicks;
	my %voice;
	my $nick;
	my $list;

	if ($channel->{type} ne "CHANNEL") {
		Irssi::print("Your are not on a channel");
		return;
	}
	@nicks = $channel->nicks();
	my $i = 0;
	foreach $nick (@nicks) {
		$voice{$nick->{nick}} = 1 if $nick->{voice}
	}
	if ($list = join " -", sort keys %voice) {
		$channel->command("quote cs voice $channel->{name} -$list $data");
		#print("quote cs voice $channel->{name} -$list $data");
	}
}

Irssi::command_bind('mvoice','cmd_mvoice');
Irssi::command_bind('mdevoice','cmd_mdevoice');
Irssi::settings_add_str('mvoice','mvoice_blacklist','');

