#
# This file is loaded when nnrpd starts up. If it defines a sub named
# `filter_post', then that function will be called during processing of a
# posting. It has access to the headers of the article via the associative
# array `%hdr'. If it returns a null string then the article is accepted
# for posting. A non-null string rejects it, and the value returned is used
# in the rejection message.
#

use Digest::SHA1 qw(sha1_base64);
my $savefile="/var/log/news/xtracelog/xtrace.log";

my $tracekey = "yourkey";

sub filter_post {
    my $rval = "" ;             # assume we'll accept.

    $modify_headers = 1;
    $hdr{"X-Trace"} = do_encode($hdr{"X-Trace"},$tracekey);

    return $rval;
}

sub do_encode {
        my $string = shift;
        my $key = shift;
        return "" unless defined $string and defined $key;
        if ($string =~ /^(\S+)\s(\d+)\s(\d+)\s((\d{1,3}\.){3}\d)\s/) {
                my($domain, $ts, $pid, $ip) = ($1, $2, $3, $4);
                my $test = $domain."|".sprintf('%lx',$ts)."|".sprintf('%lx',$pid)."|".sprintf('%lx',ip2long($ip));
                my $enc = sha1_base64($test."|".$key);
                my $umask = umask 0077;
                open(SAVE, ">>$savefile") || return "";
                print SAVE "$enc        $string\n";
                close(SAVE);
                umask $umask;
                return $enc;
        } else {
                return $string;
        }
}
sub ip2long {
        my $ip = shift;
        my $long = 0;
        my $i = 0;
        my @sip = split /\./, $ip;
        foreach my $byte (@sip) {
                $long = $long << 8 if $i > 0;
                $long += $byte;
                $i++;
        }
        return $long;
}