#!/afs/athena/contrib/perl/p
#
# uucollector - retrieve uuencoded postings and uudecode them
#
# Here is how I run uucollector from cron:
#
#   35 21 * * * bin/uucollector -d /usr/tmp/incoming all.binaries.all 2>&1
#
# I also have a file named .archive in my home directory with the
# binary news groups that I am interested in.

# this cruft is to give a reasonable path when cron runs the job
$home = $ENV{"HOME"};
$oldpath = $ENV{"PATH"};
$ENV{"PATH"} = join(":", "$home/bin", "/usr/local/bin", "/usr/ucb",
		    "/usr/new", "/bsd43/bin", "/usr2/tools/bin/mips",
		    "/usr/new/mh", $oldpath);

unshift(@INC, "/afs/sipb.mit.edu/contrib/perl");
unshift(@INC, "/afs/sipb.mit.edu/contrib/perl/nntp");

require('getopts.pl');

# defaults
$opt_d = ".";			# directory to mess up
$opt_F =  "$home/.archive";	# newsrc-style file

# spank the user for giving bad args
sub usage
{
    die <<"EndoUsage"
usage: $0 [-<options>] <newsgroup> ...
    options:	-d <directory>: collect files in directory
		-F <newsrc>:	use alternate newsrc file
		-S <server>:	use alternate news server
EndoUsage
    ;
}

# process command line arguments
&usage if ! &Getopts("d:F:S:");

&usage if ! @ARGV;		# must supply news groups

# the rest of these are news groups
$args = join(" ", @ARGV[0..$#ARGV]);

# create the collection directory if needed
mkdir($opt_d, 0700) if ! -d $opt_d;
chdir($opt_d);

# use a different nntp server host from the default
@ENV{"NNTPSERVER"} = $opt_S if $opt_S;

# tell the user what he's already seen
print "Original archive contents of $opt_F:\n\n";
print `cat $opt_F`;
print "================\n";

# snuff all the followup articles in the news group
print "Marking the following articles read:\n\n";
print `nntpfetch -UF $opt_F "^R[eE]: " $args`, "\n"; # snuff garbage
@files = <xxyy.*>;
$mergers = join(' ', @files);
print "\n", `grep ^Subject: $mergers` if @files;
print "================\n";

# get list of topics
@list = `nntpgrep -F $opt_F . $args`;

# smush data into a list of topics that we can use to hunt articles
foreach $line (@list)
{
    @nginfo = split(/: /, $line);
    $nginfo[$#nginfo] =~ /[a-zA-Z0-9_-]+/;
    $patterns{"$&"} ++ if $&;
}

#tell user what we're going to look for
print "    Pattern             Occurances\n";
print "    ------------------  ----------\n";

foreach $pattern (reverse(sort keys(%patterns)))
{
    printf("    %-20s  %3d\n", $pattern, $patterns{$pattern});
}
print "================\n";

# now snag the articles and uudecode 'em
foreach $pattern (reverse(sort keys(%patterns)))
{
    # blow away junk from previous time thru loop.
    unlink <xxyy.*>;
    print "Matching pattern \"$pattern\":\n\n";
    print `nntpfetch -UF $opt_F \"^[^a-zA-Z0-9_-]*$pattern|: [^a-zA-Z0-9_-]*$pattern\" $args`;
    @files = <xxyy.*>;

    # check to see if retrieved articles are legit uuencoded stuff
    foreach $FILE (@files)
    {
	$isuudecoded = 0;
	open(FILE);

	# skip header
	while(<FILE>)
	{
	    last if ! /^[A-Z][a-z0-9_-]*:/;
	}

	while (<FILE>)
	{
	    $isuudecoded = 1 if /^M/ && length($_) >= 60;
	    last if $isuudecoded;
	}
	close(FILE);
        unlink $FILE if ! $isuudecoded;	# not uuencoded!
    }

    # see if any files remain after the uuencoding test
    @files = <xxyy.*>;
    next if ! @files;

    # merge the remaining files
    $mergers = join(' ', @files);
    print "\n", `grep ^Subject: $mergers`;
    print "\nuumerge $mergers:\n", `uumerge $mergers 2>&1`;
}
continue
{
    print "================\n";
}

unlink <xxyy.*>;
