#!/usr/local/bin/perl
#
#
# Automagically notify posters of illegal References: Headers
#
# (c) Otmar Lendl (lendl@cosy.sbg.ac.at)
#

#
# Location of the database
#
$offenders = $ENV{"HOME"} . "/News/complaindb";

#
# How long between notifications.
#
$keep_quiet = 3600 * 24 * 64;	# ~ 2 months

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

require("newgetopt.pl");

$is_okay = 1;
$| = 1;

&NGetOpt("to=s") || die "Illegal Option.\n";

die "Specify target with -to !\n" if ($opt_to eq "");

print "This is complain.pl. Examining article from $opt_to.\n";

#
# slurp in Headers
#
%h = ();
$header = "";
while (($_ = <STDIN>) !~ /^$/)
	{
	$header .= $_;
	chop;
	if ( /(^\S*): (.*$)/) 	# header: value
		{
		$h{$1} = $2;
		$last_header = $1;
		}
	elsif ( /^\s*(.*$)/ ) # continuation line
		{
		$h{$last_header} .= $1;
		}
	else 			# Illegal Header
		{
		print "Illegal Header line: \n$_\n";
		}
	}
#
# Now check for missing/wrong References: Header
#
if ( ! defined($h{"Subject"}) )
	{
	print "No Subject Header. What a twink. \n";
	die "Not handled yet.";
	}

if ( $h{"Subject"} =~ /^Re: / )	# Is it a followup ?
	{
	if (!defined($h{"References"})) # No references header ??
		{
		print "No References: Header.\n";
		$no_refs = 1;
		$is_okay = 0;
		}
	 elsif ( $h{"References"} !~ /^<.*>$/ )	# Wrong syntax ?
		{
		print "Malformed References: header.\n";
		$ill_refs = 1;
		$is_okay = 0;
		}
	 else
	 	{
	 	print "The References: header looks okay.\n";
	 	}
	}

#foreach $f ( keys h)
#	{
#	print "$f = $h{$f}\n";
#	}

if ( $is_okay)
	{
	print "No irregularities found. Exiting.\n";
	exit (0);
	}
#
# Open offender database
#
dbmopen(%OFF, $offenders, 0644) || die "Can't open database.";

$lasttime = $OFF{$opt_to};

if ($lasttime + $keep_quiet > time())
	{
	$days = int( (time() - $lasttime) / (3600*24));
	print "Sent last notification just $days days ago. Aborting now.\n";
	exit(0);
	}

open(USER, "/dev/tty") || die "Can't get keyboard input.";

print "Send a notification ? [y]";
$_ = <USER>;

if ( /^n/i )
	{
	print "Okay. No flame sent. Exiting.\n";
	exit (0);
	}

$OFF{$opt_to} = time();
dbmclose(OFF);

#
# Ok, now go for it.
#

open(ELM, "|-") || exec 'elm', '-s', $h{"Subject"}, $opt_to;

print ELM <<"EOM";
Hi,

this is an automated reply to your article $h{"Message-ID"}
sent to $h{"Newsgroups"} on $h{"Date"}.

Your article does not contain a valid References: header and is
therefore in violation to RFC 1036, which is the current
standard for the Usenet message format.

The References: header enables advanced newsreading software 
to reconstruct the discussion tree and thus present the articles 
in a nice crossreferenced fashion. This feature is considered
essential by a lot of people.

Common reasons for problems with the References: headers are:

 * The article was posted using the "post new article" command instead of
   the "post a followup" command.

 * The newsreader doesn't handle References: correctly. (Trumpet, tin,
   TRP, ...)

 * The article passed through a gateway which does not generate the
   appropriate headers. (Typical for BBS systems such as FirstClass,
   or Mail->News gateways.)

I ask you to conform to established Usenet standards. If you are not
in an position to fix the problem yourself, please consult your
system administrator. If that, too, does not leed to a solution,
please consider other means of accessing Usenet.

Questions about this topic should be asked either on news.newusers.questions
or news.software.readers.

Virtually,

	Otmar Lendl
--
/ Otmar Lendl (lendl\@cosy.sbg.ac.at) ## http://www.cosy.sbg.ac.at/~lendl/ \\
\\ Put the scrabble away Arthur, it won't save the human race! # finger4PGP/
EOM

close(ELM);
close(USER);

exit(0);
