

Personal NNTP based news retriever...
Saves news articles in mailbox format...

Fixed minor bug with dup articles pointed out to me by
[email-masked] and [email-masked]...

Thanks,

=====Cut====
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  pgnews
# Wrapped by mcgough@wrdis01 on Thu Jul  9 15:28:04 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'pgnews' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pgnews'\"
else
echo shar: Extracting \"'pgnews'\" \(4501 characters\)
sed "s/^X//" >'pgnews' <<'END_OF_FILE'
X#! /usr/bin/perl
X#/****************************************************
X#*****************************************************
X#**
X#** SOURCE NAME | pgnews, (Perl Get News)
X#**             | 
X#**    SYNOPSIS | pgnews
X#**             | 
X#** DESCRIPTION | pgnews goes to a specified NNTP server
X#**             | and retrieves news articles by newsgroup
X#**             | and saves them to a specified file in
X#**             | mailbox format.
X#**             | Please see the NOTES section.
X#**             | 
X#**     CHANGES | Programmer:         Date:     Reason/Comments
X#**             | Jeffrey B. McGough  09-05-91  initial
X#**             | Jeffrey B. McGough  09-06-91  Added select (see FIXES)
X#**             | Jeffrey B. McGough  10-06-91  Fixed erronious end of article
X#**             | Jeffrey B. McGough  07-09-92  Fixed dup article bug
X#**             | 
X#**       NOTES | Pgnews needs a file named .pgnews to read
X#**             | its newsgroup, last message, and savefile from.
X#**             | .pgnews format is:
X#**             | newsgroup number savefile
X#**             | Example:
X#**             | comp.unix.wizards 7800 cuw
X#**             | comp.unix.shell 3203 cus
X#**             | comp.unix.questions 546 cuq
X#**             | 
X#**             | comp.unix.wizards will be saved to file cuw in
X#**             | mailbox format starting at article 7800 etc.
X#**             | 
X#**       FIXES | 09-06-91: added select on S to keep the client
X#**             | from getting out of sync.
X#**             | Jeffrey B. McGough  [email-masked]
X#**             | 
X#**             | 10-06-91: Fixed an overlooked END of ARTICLE
X#**             | bug... Thanks to a member(s) of the issos
X#**             | group at Ohio State.
X#**             | Jeffrey B. McGough  [email-masked]
X#**             | 
X#**             | 07-09-92: Fixed a duplicate article bug
X#**             | pointed out to me by [email-masked]
X#**             | and [email-masked]. Thanks
X#**             | for the help with the fix.
X#**             | Jeffrey B. McGough  [email-masked]
X#**             | 
X#****************************************************/
X
Xrequire 'sys/socket.ph'; # The way I coded the sockets is this necessary?
X
X$port = 119; # For NNTP
X# HOSTNAME for the server...
X#$host = 'localhost';
X$host = 'emory.mathcs.emory.edu';
X# Pack format...
X$sockaddr = 'S n a4 x8';
X
X$DOMAIN = 2;
X$STYLE = 1;
X
X$newsfile = '.pgnews';
X$nnewsfile = '.pgnews.new';
X
X$rin = $rout = '';
X
X($name, $aliases, $proto) = getprotobyname('tcp');
X($name, $aliases, $type, $len, $hostaddr) = gethostbyname($host);
X
X$sock = pack($sockaddr, $DOMAIN, $port, $hostaddr);
X
Xsocket(S, $DOMAIN, $STYLE, $proto) || die $!;
Xconnect(S, $sock) || die $!;
Xselect(S); $| = 1; select(STDOUT);
X#set up for select
Xvec($rin, fileno(S), 1) = 1;
X#this select will block until the server gives us something.
Xselect($rout=$rin, undef, undef, undef);
X$_ = <S>; #Read one line to see if we got a good connection.
Xif ($_ !~ /^2../)
X{
X	print;
X	die "Service unavailable";
X}
Xopen(GRP, "< $newsfile") || die "Could not open $newsfile: $!";
Xopen(NGRP, "> $nnewsfile") || die "Could not open $nnewsfile: $!";
Xselect(NGRP); $| = 1; select(STDOUT);
Xwhile(<GRP>)
X{
X	chop;
X	($grp, $lgot, $file) = split;
X	print(S "group $grp\n");
X	#this select will block until the server gives us something.
X	select($rout=$rin, undef, undef, undef);
X	$_ = <S>; #Make sure the group change worked...
X	($stat, $num, $first, $last) = split;
X	if( $stat !~ /^2../ )
X	{
X		print;
X		warn "Bad group";
X		print(NGRP "$grp $lgot $file\n");
X		next;
X	}
X	# good group open output file...
X	open(OUTFILE, ">>$file") || die "Could not open $file";
X
X	if ( $first > $lgot )
X	{
X		$lgot = $first;
X	}
X	if ( $lgot <= $last )
X	{
X		foreach $art ($lgot..$last)
X		{
X			print(S "article $art\n");
X			#this select will block until the server gives us something.
X			select($rout=$rin, undef, undef, undef);
X			$_ = <S>; #get error if one exists
X			if($_ !~ /^2../)
X			{
X				print;
X				warn "No article by that number";
X			}
X			else
X			{
X				do
X				{
X					$lgot = $art;
X					$_ = <S>;
X					s/^Path:/From/;
X					s/\r//;
X					if( $_ ne ".\n")
X					{
X						s/^\.//;
X						print OUTFILE;
X						s/^\./../;
X					}	
X					else
X					{
X						print OUTFILE "\n";
X					}
X				} until $_ eq ".\n";
X			}
X		}	
X	}
X	close(OUTFILE);
X	$lgot += 1;
X	print(NGRP "$grp $lgot $file\n");
X}
Xclose(NGRP);
Xclose(GRP);
Xsystem("mv $nnewsfile $newsfile");
Xprint( S "quit\n");
Xclose(S);
END_OF_FILE
if test 4501 -ne `wc -c <'pgnews'`; then
    echo shar: \"'pgnews'\" unpacked with wrong size!
fi
chmod +x 'pgnews'
# end of 'pgnews'
fi
echo shar: End of shell archive.
exit 0
-- 
Lator,                                       We cheat the other guy,
                                             and pass the savings on to you.
Jeffrey B. McGough
WR-ALC UNIX Systems Administrator                    ([email-masked])