# selfgagger - irssi script that prevents you from talking in certain channels
# Copyright (C) 2005 Göran Weinholt <goran@weinholt.se>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# -*- encoding: UTF-8 -*-

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

$VERSION = '1.0';
%IRSSI = (authors         => 'Göran Weinholt',
		  contact         => 'goran@weinholt.se',
		  name            => 'selfgagger',
		  license         => 'GNU GPL v2 or later',
		  description     => 'prevent yourself from talking in certain channels');

sub gagging {
	my ($channel,$network) = @_;
	my $c_nets = Irssi::settings_get_str('selfgagger_channels');
	my @c_nets = split(/ /, $c_nets);
	
	foreach my $c_net (@c_nets) {
		return 1 if (lc($c_net) eq lc("$channel:$network"));
	}
	return 0;
}

sub send_text {
    my ($line, $server, $witem) = @_;
	
	return unless ref $witem;
	if (gagging($witem->{name}, $server->{chatnet})) {
		$witem->print("Gag in place. Type /UNGAG to remove it.",
					  MSGLEVEL_CLIENTCRAP);
		Irssi::signal_stop();
	}
}

sub gag_ungag {
	my ($args, $server, $witem, $engag) = @_;

	unless (ref $witem) {
		print CLIENTCRAP "/GAG and /UNGAG must be typed in a channel!";
		return;
	}
	my $gag_found = gagging($witem->{name}, $server->{chatnet});
	if ($engag and $gag_found) {
		$witem->print("Gag already in place. Type /UNGAG to remove it.",
					  MSGLEVEL_CLIENTCRAP);
		return;
	} elsif (!$engag and !$gag_found) {
		$witem->print("No gag found for this channel.",
					  MSGLEVEL_CLIENTCRAP);
		return;
	}
	my $pair = "$witem->{name}:$server->{chatnet}";
	my @c_nets = split(/ /, Irssi::settings_get_str('selfgagger_channels'));

	@c_nets = grep { !(lc($_) eq lc($pair)) } @c_nets;
	push @c_nets, $pair if $engag;

	Irssi::settings_set_str('selfgagger_channels', join(" ", @c_nets));

	if ($engag) {
		$witem->print("Gag in now in place. Type /UNGAG to remove it.",
					  MSGLEVEL_CLIENTCRAP);
	} else {
		$witem->print("Gag removed. You may now speak.",
					  MSGLEVEL_CLIENTCRAP);
	}
}

sub cmd_gag {
	my ($args, $server, $witem) = @_;
	gag_ungag($args, $server, $witem, 1);
}

sub cmd_ungag {
	my ($args, $server, $witem) = @_;
	gag_ungag($args, $server, $witem, 0);
}

# A space-delimited list of channel:network
Irssi::settings_add_str('selfgagger', 'selfgagger_channels', '');

Irssi::signal_add_first('send text', 'send_text');

Irssi::command_bind('gag', \&cmd_gag);
Irssi::command_bind('ungag', \&cmd_ungag);

print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded.';
print CLIENTCRAP "Use /GAG in a channel to prevent yourself from talking there."

