package NetHirc::BigBrother;

use strict;
use warnings;

use NetHirc::Util;

my @dull_words = qw(an and are as at to the of or on if in is it for from with by);
my $dull_pattern = join('|', @dull_words);

sub new
{
    my $proto = shift;
    my $class = ref $proto || $proto;
    my $self = {
	'verboten'	=>	{ },
	'delete'	=>	2,
	'skip'		=>	2,
	'boring'	=>	qr/$dull_pattern/,
	'minwordlen'	=>	2,
	'announce'	=>	undef,
	'frequency'	=>	19,
	'wakeup'	=>	241,
	'capacity'	=>	7,
    };
    return bless $self, $class;
}

sub check
{
    my $self = shift;
    my $text = shift;
    unless (int(rand($self->{'frequency'})))
    {
	$self->verboten($_[0]);
    }
    unless (int(rand($self->{'wakeup'})))
    {
	$self->apply_restrictions();
    }
}

sub verboten 
{
    debug('b', "verboten");
    my $self = shift;
    my $text = shift;
    $text ||= '';
    for my $i (keys %{$self->{'verboten'}})
    {
	if (int(rand($self->{'delete'})))
	{
	    delete $self->{'verboten'}->{$i};
	}
    }
    $text =~ s/[^a-zA-Z]/ /g;
    return unless $text;
    my @pottymouth = split(' ', $text);
    for my $j (@pottymouth) {
        next unless $j;     ## empty?  ignore.
        ## skip boring words.
	next if ($j =~ $self->{'boring'});
        next if length($j) < $self->{'minwordlen'};
        next unless (int(rand($self->{'skip'})));
        $self->{'verboten'}->{$j} = "VERBOTEN";
    }
    my @tmp = %{$self->{'verboten'}};
    my $limit = 2 * $self->{'capacity'};
    splice(@tmp, $limit) if (@tmp > $limit);
    $self->{'verboten'} = { @tmp };
}

sub apply_restrictions 
{
    debug('b', "apply_restrictions");
    my $self = shift;
    my $announce = $self->{'announce'};
    return unless $announce;
    return unless %{$self->{'verboten'}};
    $announce->(keys %{$self->{'verboten'}});
}

1;
__END__


syntax highlighted by Code2HTML, v. 0.9.1