#!/usr/bin/perl

my $rcsid='$Id: testrep.channel,v 1.2 2000/08/22 23:52:51 sec Exp sec $';
my $len=5; # Max Size in KB of included article.

use POSIX qw(strftime);
use Date::Manip;
use strict;

$|=1;
while(<>){
	chomp;
	if(!open(ARTICLE,"/usr/lib/news/bin/sm '$_'|")){
		print "$0 failed to get article $_\n";
	};
	print "$_\n";
	&reply();
	close(ARTICLE);
};
print "$0 Exiting on eof.\n";
sleep(10);
exit(0);

sub reply(){
my @hdrs;
my ($nor,$date,$from,$reply,$mid,$path,$subject,$ng);
my ($pathe);
my ($nodes,@paths,$paths,$s);

while (<ARTICLE>){
	push @hdrs,$_;
	chomp;
	last if /^$/;
	$nor=1 if (/(ignore|no\s?reply)/i);
	$date=$_ if (s/^date:\s+//i);
	$from=$_ if (s/^from:\s+//i);
	$reply=$_ if (s/^reply-to:\s+//i);
	$mid=$_ if (s/^message-id:\s+//i);
	$path=$_ if (s/^path:\s+//i);
	$subject=$_ if (s/^subject:\s+//i);
	$ng=$_ if (s/^newsgroups:\s+//i);
}

for my $k (1..10){
	$_ = scalar <ARTICLE>;
	push @hdrs,$_;
	$nor=1 if (/(ignore|no\s?reply)/i);
}

return if ($ng =~ /,/); # Ignore cross-postings.
return if ($nor);       # User wanted us to ignore it.
return if ($from =~ /NNTP-Monitor\@hero\.vmc\.private\.ntl\.com/) ;
                        # spammy and unmailable

my $diff=&timediff(time-
UnixDate(ParseDate($date),"%s")
),"\n";

sub timediff(){
	my @n=qw(second minute hour day);
	my @t=(60,60,24,99999);
	my $r=shift;
	my $f=1;
	my $p=0;
	my $s="";
	if ($r == 0) {
	    $s="less than one second";
	}
	else {
	    for my $i (0..$#t){
		$p=$r%$t[$i];$r=int($r/$t[$i]);
		#print "p: $p, r: $r\n";
		if($p){$s=(($f-->0 && $r>0)?"and ":"").$p." ".$n[$i].($p>1?"s":"").($f<-1?",":"").($s?" ".$s:"")}
	    }
	}
	return $s;
}

$from=$reply if ($reply);

$path =~ /([^!]*)!/; $pathe=$1;
$nodes = (($paths = $path)=~ s/!/ /g);
$s=$nodes>1?"s":"";

@paths=split(/!/,$path);
pop(@paths);
$paths=join(" ->_",reverse @paths).". ";
$paths=~s/([^\n]{40,75})[ \n]/$1\n/g;
$paths=~s/_/ /g;

my $rdate=strftime "%a %b %e %H:%M:%S UTC %Y", gmtime;

open(FOO,"|/usr/sbin/sendmail -t -f usenet-autoresponder\@ysolde.ucam.org");
print FOO <<EOF;
Subject: Response to a test message. (was: $subject)
From: $ng autoresponder <usenet\@ysolde.ucam.org>
To: $from
X-Rcs-Id: $rcsid
X-Local-Version: 0.1.0
Precedence: junk

This message has been generated automatically in order to help you track
the circulation in the $ng newsgroup. If you do not wish any reply
from this program, just include the words "ignore" or "no reply" in the
header or in the 10 first lines of the body of your message.

Your Message $mid was received by $pathe
at $rdate.

Assuming your clock was set correctly, this means that your article
travelled the network for $diff
before arriving here.

It had to pass through the following $nodes Usenet node$s starting with
your system:
$paths

Full headers plus the first 10 lines of your original message were:

EOF

my $str=join ("",@hdrs);
print FOO substr($str,0,$len*1024);
if (substr($str,$len*1024,1)){
	print "\n- The above message has been trunctated, because it is longer than  $len KB.\n";
}
close(FOO);

};
