#!/afs/athena/contrib/perl/perl

# This is the 'getnews' perl script. 
# You feed it a Message-ID as its only argument.
# The enclosing <angle-brackets> are optional.
# Output goes to STDOUT.
# This script assumes that either you have an NNTPSERVER environment
#   variable, or else your NNTP server is called "news".
  
require 'sys/socket.ph';
require 'flush.pl';

$sockaddr = 'S n a4 x8';

$proto = (getprotobyname('tcp'))[2];
$port = (getservbyname('nntp', 'tcp'))[2];
$hostname = $ENV{'NNTPSERVER'} || 'news';
$hostaddr = (gethostbyname($hostname))[4] || die "No such host: $hostname";

$port = pack($sockaddr, &AF_INET, $port, $hostaddr);
socket(S, &AF_INET, &SOCK_STREAM, $proto) || die "Can't open socket";
connect(S, $port) || die "Can't connect socket";

(($prompt = <S>) =~ /^20[01] /) || die "Bad NNTP status: $prompt";
$msgid = $ARGV[0];
$msgid = "<$msgid>" unless ($msgid =~ /^<.*>$/);
&printflush (S, "article $msgid \r\n");
(($reply = <S>) =~ /^220 /) || die "Bad NNTP status: $reply";
while (<S>)
   {s/\r//;  # get rid of annoying CR's before each newline
    last if /^\.$/;  # article ends with a period all by itself on line
    print;}
