#!/usr/local/bin/perl5 -s
#
# This file is part of SnarfNews
# Copyright (C) 1991,1992,1993,1994,1995,1996 Alec Muffett
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#

require "snarf.pl";

$ok = 1;
@patterns = ();
$xpostmax = 0;
$sizemax = 0;

$cf = undef;
$config = shift || "default";
$config2 = "$ENV{SNARFCONF}/filter/$config";

if (-f $config2)
{
    $cf = $config2;
}
elsif (-f $config)
{
    $cf = $config;
}

if (defined($cf) && open(CONFIG, $cf))
{
    while (<CONFIG>)
    {
	next if (/^(\#|\s)/o);
	chomp;

	($key, $value) = split(/\s+/, $_, 2);

	warn "filterart: key='$key' value='$value'\n" if ($debug);

	if ($key eq "header")
	{
	    push(@patterns, $value);
	}
	elsif ($key eq "xpost")
	{
	    $xpostmax = $value;
	}
	elsif ($key eq "size")
	{
	    $value =~ tr/A-Z/a-z/;

	    if ($value =~ /^([0-9]+(\.[0-9]+)?)\s*([km])?/oi)
	    {
		$sizemax = $1;

		if ($3 eq "k")
		{
		    $sizemax *= 1024;
		}
		elsif ($3 eq "m")
		{
		    $sizemax *= 1048576;
		}
		$sizemax = int($sizemax);
	    }
	}
    }
    close(CONFIG);
}
else
{
    $cf = "(none)";
}

warn "filterart: cf=$cf xpost=$xpostmax size=$sizemax\npatterns=@patterns\n\n" if ($debug);

while (<STDIN>)
{
    $debug && warn "| $_";      # debug info

    if (/^\s+/o && ($#header >= 0))
    {
	$header[$#header] .= $_;
    } else
    {
	push(@header, $_);
    }

    last if /^\s*$/o;           # break off header on blank line
}

if ($#header < 0)
{
    warn "filterart: empty header\n" if ($debug);
    exit 0;
}

foreach $pattern (@patterns)
{
    if (@matches = grep(/$pattern/i, @header))
    {
	warn "filterart: matches $pattern\n @matches" if ($debug);
	$ok = 0;
	last;
    }
}

$ngline = (grep(/^newsgroups/oi, @header))[0];
$ngline =~ tr/,//cd;
$xpostnum = length($ngline) + 1;

if ($xpostmax && ($xpostnum > $xpostmax))
{
    warn "filterart: dropped [xpost $xpostnum]\n\n" if ($debug);
    $ok = 0;
}

@body = <STDIN>;

map {$size += length} @header, @body;

if ($sizemax && ($size > $sizemax))
{
    warn "filterart: dropped [size $size]\n\n" if ($debug);
    $ok = 0;
}

print @header, @body if ($ok);

exit 0;
