#!/usr/local/bin/perl

# This software is Copyright (c) 1996 Jeff Weisberg <jaw@op.net>
# Other than removing this notice, do with this code as you wish...

# snnd (SillyNetNewsDaemon)
# keep all of the alt.nudie.pictures and alt.|<ooL.wArEZ lusers looking
# for open news servers, busy

# run this from inetd


$logfile = "/var/adm/innfake";	# log everything to here
$active  = "/var/tmp/active";	# the active file we give out

$| = 1;
$SIG{'ALRM'} = sub{ exit 0 };

chop( $date = `date` );
$head = <<EOH;
Path: here!there!not-for-real
Newsgroups: a.news.group
From: user\@host
Subject: Welcome to Usenet
Message-Id: <4jcgiv\$8s\@phhhhhhht>
Date: $date
EOH
    ;

$body = <<EOB;

This is a simulated usenet article

----
My sig!
----
My sig!
EOB
    ;

alarm( 300 );
open( LOG, ">> $logfile");

log_info();

print "200 op.net SillyNetNews NNRP server SNN 1.4unoff4 05-Mar-96 ready (posting ok).\r\n";
# change   ^^^^^^ this to your site

while( <> ){
    s/\r//g;
    print LOG "$$: ($host): $_";
    alarm( 300 );
    
    if( /^list/i ){
	print "215 list of newsgroups follows\r\n";
	open( ACTIVE, $active );
	while( <ACTIVE> ){
	    chop;
	    print "$_\r\n";
	}
	close ACTIVE;
	print ".\r\n";
    }elsif( /^quit/i ){
	print "205 Bye Bye!\r\n";
	exit;
    }elsif( /^group/i ){
	split;
	print "211 8723 146926 155649 $_[1]\r\n";
    }elsif( /^article/i ){
	print "220 123456 <S%\$gfc6\@host> article retrieved - head and body follow\r\n$head\r\n$body.\r\n";
    }elsif( /^head/i ){
	print "221 123456 <S%\$gfc6\@host> article retrieved - head follows\r\n$head.\r\n";
    }elsif( /^body/i ){
	print "222 123456 <S%\$gfc6\@host> article retrieved - body follows\r\n$body.\r\n";
    }elsif( /^stat/i ){
	print "223 123456 <S%\$gfc6\@host> article retrieved - request text separately\r\n";
    }elsif( /^help/i ){
	print "100\r\n";
	print "HELP prints this message\r\nAnd nothing more\r\n.\r\n";
    }elsif( /^post/i ){
	print "340 send article to be posted. End with .\r\n";
	while( <> !~ /^\.\s*$/ ){
	    ;
	}
	print "240 article posted ok\r\n";
    }else{
	print "500 error\r\n";
    }
	    
}

sub log_info {
    # get remote address for logfile
    # most connections come from dialup ppp connections
    # so we don't bother trying to get ident info
    
    $rsa = getpeername( STDIN );
    ($family, $rport, $raddr) = unpack('S n a4', $rsa);
    ($name, $aliases, $addrtype, $length, $address) = gethostbyaddr($raddr, 2);
    @octets = unpack ("CCCC", $raddr);
    # use dotted quad if lookup failed
    $name = join ('.', @octets[0..3]) unless $name;
    $host = $name;

    print LOG "\n$$: $host ", `date`;
}

