#!/usr/bin/perl
#
#
# gary
#
#  Alexander Mosley ajm@netlink.co.uk
#
# a modified version of 
#
# aub: assemble usenet binaries
#
#  Mark Stantz, stantz@sgi.com, stantz@sierra.stanford.edu
#
#  aub v1.0 1/92	Basic functionality only.  Buggy.
#      v1.1 3/92	Cleaned up, documented, released, added functionality.
#      v2.0 8/92.	Major rewrite, numerous enhancements, speed hacks.
#			Now requires a formal configuration file.
#  gary v1.0 3/00
#  gary v1.1 7/00       Minor bug fix for files containing spaces
#                       (Thanks to Blake Freeburg)
#
# Most of the documentation for this program is self-contained.
#
# 	gary -m			Prints the short form of the documentation
#	gary -M			Prints the long form
#	gary -C 		Lists changes made since previous version
#
#
use Fcntl;
use SDBM_File;
use FileHandle;
use Socket;

# DEPENDENCIES:
#
# /bin/echo			-- &find_pg will look for these for you if
# /bin/sum			    they aren't in the usual place.  Though
# /bin/cat			    I don't know of any *NIX so bizarre...
# /bin/mv
# which 			-- These had better be in your path;
# whereis			    I'm not looking for them.  But they
#				    only help you if your other stuff isn't
#				    in the standard place.

$ECHO = "/bin/echo";     &find_pg("echo", 0) unless (-x $ECHO);
$SUM = "/usr/bin/sum";   &find_pg("sum", 0)  unless (-x $SUM);
$CAT = "/bin/cat";       &find_pg("cat", 0)  unless (-x $CAT);
$MV = "/bin/mv";         &find_pg("mv", 0)   unless (-x $MV);
$HOSTNAME = "/usr/bin/hostname"; $HOSTNAME = &find_pg("hostname", 0) unless
  (-x $HOSTNAME);

# CONSTANTS: 	most likely you want to leave these alone.  
#
# $gary_lock			A lockfile, used to ensure that only one
#				instance of gary runs at a time.
#
# $general 			Magic cookie for accessing general gary
#				parameters.  Must be an invalid newsgroup
#				name.
#
# $gary_dir			If the GARYDIR environment variable is set, it
#				will override the general (but not specific)
#				gary directory specified in the configuration
#				file.
# 
# $gary_desc			Specifies the general description file.
#
# $gary_hook			Specified the general hook program.
#
# $mini_gary_conf		Unqualified name of our configuration file.
#
# $gary_conf			Qualified name of our configuration file.
#
#
# $timeout_interval		How long we'll wait for a response from an 
# 				NNTP server before deciding it's died on us.
#
# $version			Version number of this program.
#
# $last_version			Version number of previous release.
#
# $glitter_url          URL for the glitter gui program
# 
# $nntpserver_file		A place gary can find the name of the NNTP 
# 				server it's supposed to use.  This is ignored
#				if the user's NNTPSERVER environment variable
#				is set, or if an NNTP server is specified in
#				the configuration file.
#
# @sigs				List of signals that might interrupt our 
#				progress.
#
# @extn_hints			Extensions which help us recognize binary
#				images in subject lines.

$glitter_url = "http://ajm.users.netlink.co.uk/glitter";
$gary_lock   	    = join("/", $ENV{"HOME"}, ".gary_lock");

$general 	    = ".";
$gary_dir{$general}  = $ENV{"GARYDIR"};
$gary_desc{$general} = $ENV{"GARYDESC"};
$gary_hook{$general} = $ENV{"GARYHOOK"};

$mini_gary_conf    = "garyconf";
$gary_conf 	    = join("/", $ENV{"HOME"}, ".glitter", $mini_gary_conf);

$timeout_interval   = 120;
$version	        = "1.0";
$last_version	    = "2.0.5(aub)";
$nntpserver_file    = "/usr/local/lib/rn/nntpserver";

@sigs		    = ('HUP', 'INT', 'QUIT', 'ILL', 'TRAP', 'IOT', 'EMT', 'FPE',
		       'BUS', 'SEGV', 'SYS', 'TERM', 'USR1', 'USR2', 'XCPU', 
		       'XFSZ', 'PIPE');

@extn_hints 	    = (".gif", ".jpg", ".jpeg", ".gl", ".zip", ".au", ".zoo", 
                       ".exe", ".dl", ".snd", ".mpg", ".mpeg", ".tiff", ".lzh",
			".wav", ".mp3", ".png");


#
# GLOBAL variables
#
#
# There are zillions of 'em, and I don't have the energy to document them all
# just now.  If you see something that's not explicitly defined as local(),
# assume it's global.
#

#
# Main program
#

# parse command line options
(&Getopts("cd:nCMmg")) || (exit(1));
# print documentation if requested
&long_manual if ($opt_M);			# None of these subroutines
&short_manual if ($opt_m);			#  will return...
&changes if ($opt_C);

# switch off buffering if in 'glitter' mode
STDOUT->setvbuf($nothing_here, _IONBF, 0) if ($opt_g);

# Make sure we clean up when we catch an unexpected signal
foreach (@sigs) {
  $SIG{$_} = 'handler';
}

# Make a first guess at what file we want to use as our NNTP server.  If the
# user's NNTPSERVER environment variable is defined, we'll use what's in it.
# Otherwise, if $nntpserver_file exists, we'll take a hint from it.

if ($ENV{"NNTPSERVER"}) {
  $server = $ENV{"NNTPSERVER"};
} elsif (-r $nntpserver_file) {
  chop($defserver = `$CAT $nntpserver_file 2>/dev/null`);
  $defserver = $1 if ($defserver =~ m/^([^\n]+)\n/);
}

#Set default single connection to news server
$max_nntp_connections = 1;

# Check that the user has a .glitter directory and attempt to create
# if it doesn't exist
$glitter_directory = join("/", $ENV{"HOME"}, ".glitter");
if (! -e $glitter_directory) {
  if (!mkdir($glitter_directory, 0750)) {
	&abort("Cant create $glitter_directory: $!");
  }
}

# Load the configuration file.
&load_config_file;

&abort("Can't find an NNTP server; please define your NNTPSERVER " .
	   "environment variable\nor specify an NNTP server " .
	   "in your configuration file.") unless ($server);

print "Using NNTP-based news access; server is $server\n"
  if ($opt_d);

# Make sure we're the only instance of gary running for this user (actually,
# for the set of all users having the same home directory as this user.)
&get_lock;

# Fork a process to decode each newsgroup up to a maximum of
# $max_nntp_connections. Continue forking processes as children terminate
# until all newsgroups have been decoded.

$children = 0;
$myproc = "Parent";
foreach (@Groups) {
  $group = $_;
  if ($children >= $max_nntp_connections) {
	wait();
  }
  $pid = fork();
  die "Cannot fork: $!" unless defined($pid);
  $myproc = "Child:".$children if (!$pid);
  last if (!$pid); #child process
  $children++;
  push @children, $pid;
}

# Keep parent process alive until all children have terminated.
if ($pid) {            #parent process
  while (wait() > 0) {}
  &cleanup;
  exit(0);
}

# If we get here, then we are a child process and dealing with
# one group $group.
#
# Set up constants for this group
# DBM file used to track unresolved messages in this group
$group_dbm = join("/", $ENV{"HOME"}, ".glitter", "$group.dbm");
# Temporary file used for decoding binaries
$temp_decode_file   = "/usr/tmp/gary.decode$$";

# Look up port number for nntp service
($name, $junk, $port, $junk) = getservbyname("nntp", "tcp");
&abort("Can't find port number for NNTP service") unless ($name);

# Open up a connection to the remote nntp server.  
&connect_tcp(SOCKET, $server, $port, 1); # Connect or bust
$connected_to_server++;			# For cleanup routines
&setup_socket_io(SOCKET);		# Necessary I/O setup stuff
&get_nntp_header(SOCKET);		# Discard server greeting banner



$garydir = ($gary_dir{$group}) ? $gary_dir{$group} : $gary_dir{$general};
if (!chdir($garydir)) {
  &warn("Could not cd to directory $garydir...skipping group $group");
  &cleanup_group();
}
print "$myproc Assembling binaries from $group\n" if ($opt_d);
print " Unpacking into directory $garydir\n" if ($opt_d > 1);

$gary_desc = ($gary_desc{$group}) ? $gary_desc{$group} : $gary_desc{$general};

$gary_hook = ($gary_hook{$group}) ? $gary_hook{$group} : $gary_hook{$general};

@gary_pp = split(" ", ($postprocessor{$group} ? $postprocessor{$group} 
					   : $postprocessor{$general}));

# Get the first and last article numbers in the group we're interested in.

($first, $last) = &set_group($group);
if ((!$first) && (!$last)) {	# This group is bogus
  $group = "";					# Don't checkpoint this
  &cleanup_group();
} 

# Load the hash of unresolved articles from $group_dbm.
# Any unresolved articles that are no longer stored on
# the nntp server must be discarded.
tie (%subject, 'SDBM_File', $group_dbm, O_RDWR|O_CREAT, 0600)
  || &abort("Unable to tie to $group_dbm: $!");

# If this is the first time we have looked at this group
# and SAMple keyword used (that is defined $max_sample_size)
# then adjust $first so that $first = max($first, $last-$max_sample_size)
($first = (($first>$last-$max_sample_size) ? $first : $last-$max_sample_size))
  if (!$subject{'last_seen'} && defined($max_sample_size));

while (($key, $value) = each %subject) {
  delete $subject{$key} if (($key ne 'last_seen') && ($key < $first));
}

# We have $subject{'last_seen'}, the article number of the last article
# we saw in this group the last time we ran.  If for some reason this has
# wound up undefined (it shouldn't be), or if the first article in the
# newsgroup is now bigger than the last article we've seen (newsgroup has
# been reset), we initialize this value.
#
# Note that when a new group is loaded for the first time, this is set to
# zero, which is most likely smaller than the first article in the group.
#

$subject{'last_seen'} = $first-1
  if ((!$subject{'last_seen'}) || ($subject{'last_seen'} < $first));

print "Last seen $subject{'last_seen'}, first $first, last $last\n"
  if ($opt_d);

# Load all of the new articles in the group into image queues
&get_all_subject_lines($subject{'last_seen'}+1,$last);
while (($key, $value) = each %subject) {
  &load_article($key) unless ($key eq 'last_seen');
}
$subject{'last_seen'} = $last;
print "Downloading binaries...\n" if ($opt_g);

# We use the concept of 'pictures' (ie, binary images) and pieces of 
# pictures while naming our variables, but really we're just putting together
# the pieces of binary files...
#
# Each image queue (image queues are built by &load_article; see the
# discussion there) potentially contains a picture.
#
# For each potential picture, extract the name of the picture and the number
# of pieces that are known to be in it.  Build an array, @pairs, containing
# N items, where N is the number of pieces of the picture that we actually
# have, and where each item contains (encoded) the article number of the ith 
# piece.

foreach $picture (keys %image_queues) {
  print "$group Image $picture..." if ($opt_d);
  ($picture_name, $picture_pieces) = split(":", $picture);
  @pairs = split("%", $image_queues{$picture});
  
  # If we don't yet have all of the pieces of the picture, then this picture
  # is unresolved. 	
  if ($#pairs+1 < $picture_pieces) {		  # Don't have all pieces yet
	print "incomplete\nNoting unresolved article(s) " if ($opt_d); 
	foreach $pair (@pairs) {
	  ($piece, $article_no) = split(";", $pair);
	  print "$article_no " if ($opt_d);
	}
	print "\n" if ($opt_d);
	next;
  }
  
  # We've found a new, complete binary image.  Build a list of all of the
  # article numbers in it, then pass this list to the &assemble_picture code,
  # which creates the binary.
  
  undef @articles_this_picture;
  print "Complete\n" if ($opt_d);
  foreach $pair (@pairs) {
	($piece, @articles_this_picture[++$#articles_this_picture]) = 
	  split(";", $pair);
  }
  
  # If the user has stuck a hook into gary,
  # now's the time to ask it whether or
  # not it really wants us to do the decoding.
  
  if ($gary_hook) {
	$sl = $subject{@articles_this_picture[0]};
	
	$sl =~ s/[`"]/ /g;
      `$ECHO "$sl" 2>/dev/null | $gary_hook >/dev/null 2>/dev/null`;
	  
      $hook_result = $?;
      print (($hook_result) ? "Hook $gary_hook returned FALSE; not decoding\n"
			 : "Hook $gary_hook returned TRUE; decoding\n")
        if ($opt_d);
    } else {
      $hook_result = 0;
    }
	
    if (!$hook_result) {
      &assemble_picture(@articles_this_picture);
	  
	  # Invoke a postprocessor, if necessary.
	  
      @pp = @gary_pp;
      while ($#pp > -1) {
        if ($real_name =~ m/$pp[0]$/i) {
		  &post_process($real_name, $postprocessor_def{$pp[1]});
		  last;
        } else {
		  shift @pp;
		  shift @pp;
        }
      }
    }
	foreach (@articles_this_picture) {
	  delete $subject{$_};
	}
	undef $image_queues{$picture};
  }

  undef %image_queues;	
  &cleanup_group();

#
# Cleanup code -- leave things in an orderly state
#
sub cleanup_group {
  close(DESC) if ($gary_desc);
  &cleanup;
  exit(0);
}




#
# Subroutines -- this bunch handle decoding images (recognition code)
#


sub load_article {
#
# Given an article number, we grab the subject line of the article and
# look to see if it might be something we're interested in.  If it's not,
# we return.
#
#  We are certainly not interested in: 
#
#  -- Articles with no subject line
#  -- Articles beginning with "Re:"
#
#  We are interested in:
#
#  -- Articles containing a string of the form:
#      (all white space is optional, case not significant)
#	- N of N			(N is any number)
#	- N fo N			(bad typist not using posting software)
#	- N / N	
#	- N \ N				(dyslexic poster?)
#	- N | N				(dyslexic poster?)
#
# Given something of this type, we guess that it's a piece of a 
# binary image. 
# 
# Given such a beast, we note how many pieces there are all together and
# which piece of the image this is, then try to deduce the name of the 
# entire image.  
#
# If we have no better guess, we use all the stuff preceding the 
# interesting part (described above) as the name of the image.  However,
# if we find a string containing ".gif", ".jpg" or something like that -- 
# a string containing any one of the extensions in the list of hints -- 
# we use that as the name of the image instead.
#
# We are also interested in:
#
# -- Articles which don't correspond to the types above, but contain a
# a string containing any one of the extensions in the list of hints.
# We assume that the article contains a file of the hinted type and
# guess the name as outlined in the paragraph above. Most
# binary pics seem to be posted in a single article, often without the
# 1/1 expected above. Processing this type of article will probably
# result in more spam, but relying on 1/1 means missing out on many
# binary posts.
# 
# It really doesn't matter what we use as the name, so long as it's the
# same for all of the pieces of the image, and so long as it's not the 
# same as any pieces of other images.  But people reposting articles
# with the same names will screw us up if we're too naive.  See below.
#
# We map colons in the name of the image to "X's", since they can really
# mess us up later in life (we use ":" as one of our delimiters, internally.)
#
# Each identified image piece is inserted into an image queue.  Image
# queues are named "PICTURE_NAME:NUMBER_OF_PIECES_IN_PICTURE".  They are
# priority queues; the priority associated with each piece of an image is
# the piece number, and the data is the image piece article number.
#
# Note that later while scanning all of the pieces in a given image queue,
# this use of priority values will cause us to see the pieces (article 
# numbers) in the correct order.  So we're doing some implicit sorting here.
#
# All numerical values derived from article subject lines are padded with
# a (rather large) number of zeros, so that we never get confused and think
# that piece "02 of 3" should follow piece "1 of 3".
#
# To keep from mixing pieces of reposted binaries up with the original 
# postings, we stick the string "reposted." in front of the name.
#
# There are some more smarts buried in the &insert_q routine.


  local($article) = @_[0];
  local($sl, $hint, $piece, $pieces, $name);

  $sl = $subject{$article};
  print " Got article $sl\n" if ($opt_d > 1);

  if (($sl eq "") || ($sl =~ m/^\s*Re:/i)) {
	delete $subject{$article};
	return;
  }
  $sl =~ y/A-Z/a-z/;
  
# XXXX Changed this to take count of images posted in one article
# without 1/1. This may result in more spam.
  if (($sl =~ m/^(.*\D)(\d+)\s*o\s*f\s*(\d+)/i) ||
	  ($sl =~ m/^(.*\D)(\d+)\s*f\s*o\s*(\d+)/i) ||
	  ($sl =~ m/^(.*\D)(\d+)\s*\/\s*(\d+)/) ||
	  ($sl =~ m/^(.*\D)(\d+)\s*\|\s*(\d+)/) ||
	  ($sl =~ m/^(.*\D)(\d+)\s*\\\s*(\d+)/)) {
	$piece  = &pad($2);
	$pieces = &pad($3);
	$name = $1;
	foreach $hint (@extn_hints) {
	  if ($sl =~ m/\S+$hint/) {
		$name = $&;
		last;
	  }
	} 
	
  } else {
	$name = 0;
	foreach $hint (@extn_hints) {
	  if ($sl =~ m/\S+$hint/) {
		$name = $&;
		last;
	  }
	}
	return unless ($name);
	$piece = &pad(1);
	$pieces = &pad(1);
  }

  $name = "reposted.$name" if ($sl =~ m/repost/i);
  $name =~ s/:/X/g;				# Avoid nasty, subtle bug

  print " Recognized piece $piece of $pieces, binary $name\n" if ($opt_d > 1);
  &insert_q(join(":", $name, $pieces), $piece, $article);
}

  
sub assemble_picture {
#
# Given a list of article numbers which make up a given image, we want to
# produce the complete, uudecoded file.
#
# For each article, we use a state machine to ensure that we only uudecode
# binary data, not news envelope or text or .sig garbage that the poster
# may have included.  
#
# The state machine is discussed a little in the &process_line subroutine.
# How it works is neither immediately obvious nor completely documented.
# You can mess things up really good by tinkering with it, too.  Unless you
# can rewrite uumerge or uucat from memory, better not tinker with this stuff.
#
# This could be made more readable, but it would be a performance trade-off,
# and good performance is highly desirable, since we're doing a lot of 
# interaction with the NNTP server in here.
#
# We take care not to generate images which exactly match previously generated
# images with similar names.

  local($state, $last_piece, $rawfile);

#
# If we're in catch up mode, we don't want to do much.  We will go as far
# as to list the article numbers we would have assembled had we been really
# decoding articles.  This information can be very useful during debugging.
#

  if ($opt_c) {
    return unless ($opt_d);
    print "Catch-up mode...skipping assembly of articles ";
    foreach (@_) {
      print "$_ ";
    } 
    print "\n";
    return;
  }

# Yes, all of these are global...

  $state = 0;					# Initialize state machine
  $last_piece = 0;				# Set for last piece of image
  $desc_text  = "";				# Text describing this image
  $desc_lines = 0;				# Running count $desc_text lines
  $real_name = "";				# Name of binary actually made
  $possible_duplicate = "";			# This is a global

  print " Assembling articles " if ($opt_d > 1);
  $desc_text = "Subject: $subject{@_[0]}\n" if ($gary_desc);
  foreach (@_) {				# For each piece...
	print "$_ " if ($opt_d > 1);
	$state = 2 if ($state);		# This is black magic
	$last_piece++ if ($_ eq $_[$#_]);		 # Set on last piece of image
	
	&putline(SOCKET, "body $_"); # Ask for article text
	$data = &getline(SOCKET);	# Get header response
	
	if ($data !~ m/^222\s/) {
	  &warn("Could not get body of article $_");
	  close(DECODE);			# Abandon ship
	  unlink($temp_decode_file);
	  return;					# Perhaps it just got expired
	}
	
	while (1) {  				# Not infinite, just fast
	  $data = &getline(SOCKET);	# Get a line of the article
	  last if ($last_nntp_line); # Last line this article
	  next if ($state == 3);	# Dump remainder of article
	  $state = &process_line($data, $state); # One step thru state machine
	} 
  }
  
  close(DECODE);				# Temporary file
  
  if ($possible_duplicate) {
    while ($possible_duplicate ne $real_name) {
      if (&identical($temp_decode_file, $possible_duplicate)) {
        print "\n" if ($opt_d > 1);
        print "Suppressing $real_name; would duplicate $possible_duplicate\n"
	  if ($opt_d);
	unlink($temp_decode_file);
        $suppressed++;
        return;
      }
      $possible_duplicate .= "+";
    }
  }

  `$MV '$temp_decode_file' '$real_name' >/dev/null 2>&1`;
  if (($gary_desc) && (!open(DESC, ">> $gary_desc"))) {
    &warn("Could not open (to append) description file $gary_desc...\n" .
          "...Skipping group $group");
    &cleanup_group();
  }
  flock(DESC,LOCK_EX);
  seek(DESC, 0, 2);
  print DESC $desc_text if ($gary_desc);		# Print only if we decode
  flock(DESC,LOCK_UN);
  close DESC;
  print "\nActual image name is $real_name\n" if ($opt_d > 1);
  print "Retrieved $real_name from $group\n" if (($opt_g) && $real_name);
}


sub process_line {
#
# State machine for processing data from articles:
#
# This isn't the best explanation possible, I realize.
#
# We process states in order of frequency of their occurrence, to make things
# go fast...
#
#  STATE		MEANING
#
#    0			Processing first article, "begin" line not yet seen.
#    1			Processing a not-last article, data lines should
#			begin with "M"
#    2			Processing non-first article, scanning for 
#			continuation of binary data (M-lines)
#    3			Through with an article; further data from this
#			article should be discarded.
#    4			Processing last article, data lines should be copied
#			until 'end' seen.
#
#
# There is now a terrible hack in here to make things work for the group
# comp.binaries.os2, where people like to post stuff that contains lines
# beginning with 'Minimum-OS2-Version:' before the uuencoded data continues
# in pieces 2+ of the binary.  gary was seeing the leading 'M' and expecting 
# uuencoded data to immediately follow, then dropping into state 3 when it 
# didn't appear.  Instant lobotomy.
#
# Too bad we can't dump anything beginning with /M\S+: /, but alas, that's 
# perfectly legal...
# 

  local($data, $state) = @_;
  local($begin, $mode, $rest);
  local($dashes) = "-------";

  if ($state == 1) {			# State 1 == most common state
    return 3 unless ($data =~ m/^M/);	# End of binary data this piece
    print DECODE unpack("u", "$data\n");# This is uuencoded data; decode it
    return 1;				# Expect to copy more binary data
  }

  if ($state == 4) {			# State 4 == 2nd most common state
    return 3 if ($data =~ m/^end/);	# Discontinue decoding when 'end' seen
    print DECODE unpack("u", "$data\n");# Decode this data, except for 'end'
    return 4;				# Continue this state until 'end'
  }
    
  if (!$state) {			# State 0 == 3rd most common state
    if ($data !~ m/^begin [0-9]/) {	# Looking for "begin"; not seen yet?
      return 0 if ($data =~ m/\-\-\-/); # HACK -- keep ugliness out of $gary_desc
      return 0 if ($desc_lines++ > 60); # HACK -- some morons post sh archives

      $desc_text = join("\n", $desc_text, $data) if ($gary_desc);
      return 0;				# Copy to desc file if $gary_desc defn.
    }

    ($begin, $mode, $real_name, $rest) = split(/\s+/, $data, 4);
    #Remove spaces from file name (Thanks to Blake Freeburg)
    $real_name = "$real_name $rest";
    $real_name =~ s/ /_/g;
    $real_name =~ s/_$//g; #remove trailing '_' generated on some files.

# Sanity check binary names...we don't like things posted with pathname
# components in the name, weird characters, names beginning with ".", or 
# binaries names "." or ".."

    $real_name = $1 if ($real_name =~ m/\/([^\/]+)$/);
    $real_name = $1 if ($real_name =~ m/^\.(.*)$/);
    $real_name =~ tr/[A-Z][a-z][0-9]+-=_:;.,//dc;
    $real_name = "Mangled" if (($real_name eq ".") || ($real_name eq "..") ||
			       ($real_name eq ""));

    $mode = 644 if (!$mode);		# We don't like mode zero binaries

   if (-e $real_name) {
      $possible_duplicate = $real_name;	# Would collide with this...
      $real_name .= "+" while (-e $real_name);
    }

    
    $desc_text = 			# Stuff group, name into desc info
      "$dashes <$group:$real_name> $dashes\n$desc_text\n\n"
      if ($gary_desc);

    unlink($temp_decode_file);
    (open(DECODE, "> $temp_decode_file")) || 
      &abort("Could not open temporary file $temp_decode_file for writing");
    chmod(oct($mode), $temp_decode_file);

    return 1 if (!$last_piece);		# If 1-N-1, -> state 1, else -> state 4
    return 4;
  }

  if ($state == 2) {			# Looking for beginning of >1st piece
    return 2 unless ($data =~ m/^M/);	# Haven't found it yet.

    return 2 if (length($data) < 61);	# Want uuencoded data, not just anything
    return 2 if ($data =~ m/^Minimum-OS2-Version:/); 	# What next?  
    print DECODE unpack("u", "$data\n");# Found it, need to decode it
    return 1 if (!$last_piece);		# Not last piece -> state 1
    return 4;				# Copy last piece data from state 4.
  }

# We should not be called when in state 3; all other states are undefined.
  &abort("Logic error in state machine");
}


sub insert_q {
#
# Insert item $item with priority $priority into an image queue $queue.
#
# Both the priority and the item are actually stored in the queue as
# the string "$priority;$item".
#
# A queue is just a string; values in the queue are separated by 
# "%" characters.
#
# Since our items are made entirely of numbers, we don't need to worry
# about getting confused by queue data that happens to coincide with the
# characters we use as delimiters.
#
# Sometimes we find ourselves in the position of trying to add an item 
# of priority P to a queue which already contains an item with priority P.
# We know that if we go ahead and do this, it will likely hose us, since
# there should be one and only one piece P of Q in any given binary.  Since
# we are called by code that processes articles in sequential order of 
# subject lines, as a heuristic, we'll assume that the later article should
# supersede the previous article.  This is reasonable; although it is 
# clearly not _defined_ to be the case that article X is more recent than
# article Y when the article number of X is larger than that of Y, such is
# often the case in practice.  Besides, there's no perfect way to decide 
# which of the two articles is to be preferred.  It turns out that this helps
# us a lot more often than it hurts us.


  ($queue, $priority, $item) = @_;
  local(@a_queue) = split("%", $image_queues{$queue});

  for ($i=0; $i <= $#a_queue; $i++) {
    last if $a_queue[$i] >= $priority;
  }

  if ($a_queue[$i] =~ m/^${priority};/) {
    $a_queue[$i] = "$priority;$item";
    print " Replacing piece $priority of $queue with (presumably) newer data\n" 
      if ($opt_d> 1);
  } else {
    splice(@a_queue, $i, 0, "$priority;$item");
  }

  $image_queues{$queue} = join("%", @a_queue);
}


#
# More subroutines -- These deal with accessing news articles.
#

sub get_all_subject_lines {
#
# 
# Tries to fill a global hash %subject, whose keys are
# the specified range of articles and values are the
# respective subject lines.
  
  local($f, $l) = @_;
  
  &putline(SOCKET, "xhdr subject $f-$l");
  $resp = &getline(SOCKET);
  if ($resp =~ m/^221\s/) {		# XHDR is supported!
	$xhdr_supported = 1;
	do {
	  $resp = &getline(SOCKET);
	  #Fill hash with subject lines
	  if (($resp =~ m/^(\d+)\s+(.*)\s*$/) && (length($2) < 500)) {
		# Don't get here if subject line longer than 500.
		# Long values cause problems in db files, but if
		# the subject is that long it is probably rubbish message
		  $subject{$1} = $2;
	  }
	  # Here we set last seen. Relies on subject headers being returned in
	  # order. Only used if we are interrupted here.
	  $subject{'last_seen'} = $1;
	} until ($last_nntp_line);
	return;	
  }
  
  # XHDR not supported
  for($i=$f; $i<=$l; $i++) {
	&putline(SOCKET, "head $i");
	$resp = &getline(SOCKET);
	next unless ($resp =~ m/^221\s/); # Not the expected response 	
	do {
	  $resp = &getline(SOCKET);	  
	  $subject{$i} = $1 if ($resp =~ m/^Subject:\s*(.*)$/);
	  #Set last_seen in case we are interrupted while loading subject lines
	  $subject{'last_seen'} = $i;
	} until ($last_nntp_line);	
  }
}


sub set_group {
#
# Return the first and last article numbers of the group we're interested in.
# We'll use the 'GROUP' command to set the group we're
# talking about and return the data the remote nntpd gives to us.
#
# We return a list (f,l) where f is the first available article in the
# group and l is the last available article.  (0,0) indicates we've run
# into a problem.
#

  local($group_to_examine) = @_[0];
  local($data, $file);
  local($min) = "first_time";
  local($max) = 0;

  &putline(SOCKET,"group $group_to_examine");
  $data = &getline(SOCKET);

  if ($data !~ m/^211\s/) {			# No such group
    &warn("Invalid group: $group_to_examine...ignoring");
    return (0,0);
  }

  $data =~ m/^211\s+\d+\s+(\d+)\s+(\d+)\s/;
  return ($1,$2);
}


sub get_nntp_header {
#
# Grab the nntp banner line from the server, sanity check it, and return.
#

  local($sock) = pop(@_);
  local($line) = &getline($sock);

  if ($line =~ m/INN/) {                        # This code by mwe@dfw.net,
    &putline($sock, "mode reader");             #  looks pretty good to me.
    &getline($sock);                            #   -mfs, 2/95.
  }

  if ($line =~ m/^480\s/) {
	#authentication required
	&abort("Remote nntp service requires logon. Set username and password")
	  if (!(defined $nntp_user));
	&logon($sock);
  }
  if (($line =~ m/^200\s/) || ($line =~ m/^201\s/)) {
	&logon($sock) if (defined $nntp_user);
	return;
  }
  &abort("Remote nntp service is too busy to talk to us now.")
    if ($line =~ m/^400\s/);
  &abort("Remote nntp service doesn't look like nntp service to me.");
}


sub logon {
  my $sock = pop(@_);
  &putline($sock, "AUTHINFO user $nntp_user");
  my $line = &getline($sock);
  if ($line =~ m/^381\s/) {
	#password required
	&putline($sock, "AUTHINFO pass $nntp_pass");
	$line = &getline($sock);
	return if ($line =~ m/^281\s/);
	&abort("Remote NNTP server rejected authentication.");	
  }
  # Authentication not understood or not required
  return;
}
  
sub setup_socket_io {
#
# Make the passed socket unbuffered, and invoke a timeout routine if we 
# don't hear something within finite time.
# 
# I don't really have an 'initialization' section, so this is the next
# best place for this...
#

  local($sockname) = pop(@_);
  local($old_def_fh) = select($sockname);

  $SIG{"ALRM"} = 'getline_timeout';
  $|=1;
  select($old_def_fh);
}


sub getline {
#
# Get a line of data from a socket.  This code is used only if our news
# access is NNTP-based.
#
# Note that $socket_stuff is a global variable
#

  local($sockname) = pop(@_);
  local($data) = "";
  local($inp);

  $timeout_flag = 0;				# Not a local variable
  alarm($timeout_interval);			# Can't wait forever.

  while ($socket_stuff !~ m/\n/) {		# No complete line in buffer
    recv($sockname, $inp, 256, 0);		# Need more data
    $socket_stuff .= $inp;			# Append to our buffer.
  }
  alarm(0);					# Got data.  Shut off alarm.
  
#  if ($socket_stuff !~ m/\r\n/) {  #no preceding carriage return
#    open(BOL, ">>/tmp/gary_errors");
#    print "\n-------------------------------------------START-----------\n";
#    print BOL $socket_stuff;
#    print "\n-------------------------------------------END-------------\n";
#    close BOL;
#    &abort("Line from server was too long. Please send /tmp/gary_errors to alexander\@mews.org.uk");
#  }
  if ($socket_stuff !~ m/\r\n/) {
    ($data, $socket_stuff) = split("\n", $socket_stuff, 2);
  } else {
    ($data, $socket_stuff) = split("\r\n", $socket_stuff, 2);
  }
  $last_nntp_line = ($data eq "\.");		# Can't confuse with ".."
  $data =~ s?\.?? if ($data =~ m/^\.\./);	# NNTP doubles leading "."

  print "  Received: $data\n" if ($opt_d > 2);
  return $data;					
}


sub getline_timeout {
#
# What can we do if our server isn't talking?  Not much.
#

  &abort("NNTP server not responding after $timeout_interval seconds.");
}


sub putline {
#
# Send a line of data over a socket.
#

  local($line) = pop(@_);
  local($sockname) = pop(@_);

  print $sockname "$line\r\n";
  print "  Sent: $line\n" if ($opt_d > 2);
} 


sub connect_tcp {
#
# Connect to a tcp port on some host.  This code is useful in more places
# than just in gary.

  local($e) = pop(@_);	      # 0=return on err, >0 = print error, abort on err
  local($port) = pop(@_);     # port to connect to
  local($server) = pop(@_);   # name of server to connect to
  local($sockname) = pop(@_); # socket to use

  local($packing_template) = "S n a4 x8";
  local($protocol) = "tcp";
  local($thishost, $problem, $junk);

  $thishost = `$HOSTNAME`; chop $thishost;

# Figure out our address...
  ($name, $junk, $junk, $junk, $ouraddr) = gethostbyname($thishost);
  if ($name eq "") {
    $problem="Can't get address of this host (\"$thishost\")";
    &abort($problem) if $e;
    return $e;
  }
 
# And the address of the host we want to connect to
  ($name, $junk, $junk, $junk, $theiraddr) = gethostbyname($server);
  if ($name eq "") {
    $problem = "Can't find address of server $server";
    &abort($problem) if $e;
    return $e;
  }

# Get the number of the protocol we're to use
  ($name, $junk, $proto) = getprotobyname($protocol);
  if ($name eq "") {
    $problem="Unrecognized protocol: $protocol";
    &abort($problem) if $e;
    return $e;
  }

  $us = pack($packing_template, &AF_INET, 0, $ouraddr);
  $them = pack($packing_template, &AF_INET, $port, $theiraddr);

# Get a socket filehandle
  if (!(socket($sockname, &AF_INET, &SOCK_STREAM, $proto))) {
    $problem="Could not create socket";
    &abort($problem) if $e;
    return $e;
  }

  if (!connect($sockname, $them)) {
    $problem="Could not connect to server";
    &abort($problem) if $e;
    return $e;
  }
}


#
# Subroutines -- dedicated to supporting postprocessors
#

sub post_process {
#
# Invoke postprocessor command $cmd on $file.
#
# The error checking here is pretty minimal...
#

  local($file, $cmd) = @_;
  local($head, $tail);

  if ($file =~ m/\./) {
    $head = $`;					# Though $' and $` look similar
    $tail = $';					#  one's forward, one's backward
  } else {
    $head = $file;				# If no "." in image name, 
    $tail = "";					#  there is no tail
  }

  $cmd =~ s/([^\\])\$h/$1$head/g;		# I'm sure there's a more 
  $cmd =~ s/([^\\])\$t/$1$tail/g;		#  graceful way of doing this,
  $cmd =~ s/([^\\])\$f/$1$file/g;		#  but I'm lazy.
  $cmd =~ s/\\\$h/\$h/g;
  $cmd =~ s/\\\$t/\$t/g;
  $cmd =~ s/\\\$f/\$f/g;

  print "Postprocessing: $cmd\n" if ($opt_d);
  
  `$cmd`;
}


#
# Subroutines -- dedicated to loading and parsing configuration files and 
#		 files maintained by gary.
#

sub load_config_file {
#
# Load the configuration file, which is now line-oriented.
#
# We provide the user with some helpful hints if we run into problems opening
# processing his configuration file.
#
# $current_group is a global variable used to keep track of which group 
# current keywords are to be applied to.
#
# &parse_line does most of the work; we also use it to verify that anything
# from the environment we'll be using is legitimate.
#

  local($extn, $cmd, $rest, $grp);

  if (!open(CONFIG, $gary_conf)) {
    &abort("Couldn't open your configuration file \"$gary_conf\".\n" .
           "Execute 'gary -m' (short form) or 'gary -M' (long form) if you " .
           "need help\nfiguring out how to create a configuration file\n\n" .
		   "If you want an easy way to maintain your configuration file\n" .
		   "then get hold of the gui glitter program at\n" .
		   $glitter_url);
  }

# Validate any information which might be given to us via the environment

  $current_group = $general;
  &parse_line("directory", split(/\s+/, $gary_dir{$general}), 
              "environment variable \$GARYDIR") if ($gary_dir{$general});
  &parse_line("desc", split(/\s+/, $gary_desc{$general}), 
              "environment variable \$GARYDESC") if ($gary_desc{$general});
  &parse_line("hook", split(/\s+/, $gary_hook{$general}), 
              "environment variable \$GARYHOOK") if ($gary_hook{$general});

# Load and validate the configuration file

  foreach (<CONFIG>) {
    chop;                                       # Trim <CR>
    s/^#.*$//;                                  # Remove comments
    s/[^\\]#.*$//;
    s/\\#/#/g;					# Unescape escaped comments
    next if m/^\s*$/;                           # Skip blank lines
    s/\s+/ /;                                   # Fold white space into 1 <SP>
    s/^ //;                                     # Drop leading and trailing
    s/ $//;                                     #  white space

    ($cmd, $rest) = split(/\s+/, $_, 2);
    &parse_line($cmd, $rest, "configuration file $gary_conf");
  }

# Make sure that certain things we need to know have been defined

  &conf_err("configuration file $gary_conf", 
            "Configuration file doesn't specify any groups to access")
    if ($#Groups < 0);

  if (!$gary_dir{$general}) {
    foreach $grp (@Groups) {
      next if ($gary_dir{$grp});

      &conf_err("configuration file $gary_conf", 
                "No directory to put decoded binaries in specified.\n Please " .
                "set the GARYDIR environment variable or modify your " .
                "configuration file");
    }
  }

# Use configuration file NNTP server if specified.  Otherwise use NNTP 
# environment variable.  Otherwise use deduced NNTP server, if any.

  $server = $nntpserver if ($nntpserver);
  $server = $defserver unless ($server);

  if ($server eq "") {
    &conf_err("configuration file $gary_conf", 
              "No NNTP server specified or " .
              "locatable.\n Please specify where to access news in the " .
              "configuration file, or by\n setting the NNTPSERVER " . 
	      "environment variable");
  }

  foreach (@extn_hints) {			# dispel regexp magic (Tom
    s/(\W)/\\$1/g;				#  Christiansen's term)
  }
}


sub parse_line {
#
# Handle a given line from the configuration file.
#
# This is pretty straightforward, but tedious.  Almost certainly there's a 
# bug or two in here, but I don't see anything wrong with it at the moment.
#

  local($cmd, $args, $err_type) = @_;
  local(@args) = split(/\s+/, $args);
  local($extn, $group, $pp_name, $pp_cmd);

  if ($cmd =~ m/^dir/i) {			# Directory keyword
    &conf_err($err_type, "No directory specified with DIRectory keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with DIRectory keyword")
      if ($#args > 0);

    &conf_err($err_type, "Supposed directory $args is not a directory") 
      unless (-d $args);
    &conf_err($err_type, "Directory $args is not writable")
      unless (-w $args);
    &conf_err($err_type, "Directory $args is not searchable (executable)")
      unless (-x $args);
	               
    $gary_dir{$current_group} = $args unless ($gary_dir{$current_group});
    return;
  }

  if ($cmd =~ m/^desc/i) {			# Description file argument
    &conf_err($err_type, 
              "No description file specified with DESCription keyword")
      if ($#args < 0);
    &conf_err($err_type, 
	      "Too many arguments specified with DESCription keyword")
      if ($#args > 0);

    &conf_err($err_type, "Could not append to description file $args") 
      unless (open(TEST, ">> $args"));
    close(TEST);

    $gary_desc{$current_group} = $args unless ($gary_desc{$current_group});
    return;
  }

  if ($cmd =~ m/^hook/i) {			# Hook program
    &conf_err($err_type, "No hook program specified with HOOK keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with HOOK keyword")
      if ($#args > 0);

    $args = &find_pg($args, 1);
    &conf_err($err_type, 
             "Supposed hook program $args nonexistent or not a plain file")
      unless (-f $args);

    &conf_err($err_type, "Supposed hook program $args is not executable")
      unless (-x $args);

    $gary_hook{$current_group} = $args unless ($gary_hook{$current_group}); 
    return;
  }

  if ($cmd =~ m/^def/i) {			# Postprocessor definition
    &conf_err($err_type, "No postprocessor named with DEFine keyword")
      if ($#args < 0);

    $args[0] =~ y/[A-Z]/[a-z]/;

    if (!$#args) {
      $args[0] =~ y/[A-Z]/[a-z]/;
      &conf_err($err_type, "No command associated with postprocessor $args[0]");
    }

    ($pp_name, $pp_cmd)  = split(/\s+/, $args, 2);
    $pp_name =~ y/[A-Z]/[a-z]/;
    $postprocessor_def{$pp_name} = $pp_cmd;
    return;
  }

  if ($cmd =~ m/^post/i) {			# Postprocessor usage
    &conf_err($err_type, "No postprocessor named with POSTprocess keyword")
      if ($#args < 0);

    $args[0] =~ y/[A-Z]/[a-z]/;
    &conf_err($err_type, "Postprocessor $args[0] used while undefined")
      unless ($postprocessor_def{$args[0]});
    &conf_err($err_type, "Postprocessor $args[0] applied to no extensions")
      if (!$#args);

    $pp_name = shift(@args);
    foreach $extn (@args) {
      $extn =~ s/(\W)/\\$1/g;
      $postprocessor{$current_group} .= "$extn $pp_name ";
    }
    return;
  }

  if ($cmd =~ m/^rec/i) {			# Recognize an extension
    &conf_err($err_type, "No extensions named with RECognize keyword")
      if ($#args < 0);

    foreach $extn (@args) {			# We'll dispel regexps later
      push(@extn_hints, $extn) unless (grep(($_ eq $extn), @extn_hints));
    }
    return;
  } 

  if ($cmd =~ m/^noxhdr/i) {			# NOXHDR keyword
    &conf_err($err_type, "Argument specified with NOXHDR keyword")
      unless($#args == -1);

    $have_gotten_subj_line_before++;		# Don't bother trying XHDR
    return;
  }

  if ($cmd =~ m/^spool/i) {			# Spool directory
    &conf_err($err_type, "No argument specified with SPOOL keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with SPOOL keyword")
      if ($#args > 0);

    &conf_err($err_type, "Cannot both NNTP and spooled news files as input")
      if ($nntpserver);

    &conf_err($err_type, "Supposed spool directory $args is not a directory")
      unless (-d $args);
    &conf_err($err_type, "Spool directory $args is not searchable (executable)")
      unless (-x $args);

    $spooldir = $args unless ($spooldir);
    return;
   } 

  if ($cmd =~ m/^nntp/i) {			# NNTP server
    &conf_err($err_type, "No argument specified with NNTPserver keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with NNTPserver keyword")
      if ($#args > 0);

    $nntpserver = $args unless ($nntpserver);	# We'll validate this later
    return;
  }

  if ($cmd =~ m/^debug/i) {			# Turn on debugging
    &conf_err($err_type, "No argument specified with DEBUG keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with DEBUG keyword")
      if ($#args > 0);

    &conf_err($err_type, "Debug level is not a number")
      unless ($args =~ m/^\d+$/);

    $opt_d = scalar($args) unless ($opt_d);
    return;
  }

  if ($cmd =~ m/^group/i) {			# A group, or group list
    &conf_err($err_type, "No argument specified with GROUP keyword")
      if ($#args < 0);

    foreach $group (@args) {
      $group =~ y/[A-Z]/[a-z]/;
      push(@Groups, $group) unless (grep(($_ eq $group), @Groups));
    }
    $current_group = $args[$#args];
    return;
  }
  
  if ($cmd =~ m/^con/i) {			# Max number of concurrent NNTP connections
    &conf_err($err_type, "No argument specified with CONnection keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with CONnection keyword")
      if ($#args > 0);

    &conf_err($err_type, "CONnection argument must be a number")
      unless ($args =~ m/^\d+$/);

    $max_nntp_connections = scalar($args);
    return;
  }
  
  if ($cmd =~ m/^sam/i) {
	# Max sample size from newly subscribed group
    &conf_err($err_type, "No argument specified with SAMple keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with SAMple keyword")
      if ($#args > 0);

    &conf_err($err_type, "SAMple argument must be a number")
      unless ($args =~ m/^\d+$/);

    $max_sample_size = scalar($args);
    return;
  }

  if ($cmd =~ m/^user/i) {			# user name for authentication
    &conf_err($err_type, "No argument specified with USER keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with USER keyword")
      if ($#args > 0);

    $nntp_user = $args;
    return;
  }
  
  if ($cmd =~ m/^pass/i) {			# password for authentication
    &conf_err($err_type, "No argument specified with PASS keyword")
      if ($#args < 0);
    &conf_err($err_type, "Too many arguments specified with PASS keyword")
      if ($#args > 0);

    $nntp_pass = $args;
    return;
  }
  
  &conf_err($err_type, "Unrecognized keyword: $cmd");
}


sub conf_err {
#
# Pretty-print configuration file errors.
#

  local($a, $b) = @_;
  print STDERR "Error in $a: $b.\n" if ($opt_g);
  print "Error in $a:\n $b.\n";
  exit(1);
}
    

#
# Subroutines -- Miscellaneous other stuff...
#

sub get_lock {
#
# Primitive (but effective) locking mechanism, used to guarantee that only
# one instance of gary is running at a given time for a given user.
#
# We write out PID to a file using echo/append.  If the first line of the
# file is our PID, we have the lock.
#
# If the first line of the file is someone else's PID, we don't have the
# lock.  We check to see if that process is still around; if it isn't,
# we'll seize the lock.
#

  
  `$ECHO "$$" >> $gary_lock 2>/dev/null`;

  &abort("Can't write $gary_lock") unless (-e $gary_lock);
  if (!open(LOCK, $gary_lock)) {
    unlink($gary_lock);
    &abort("Couldn't open $gary_lock for reading");
  }

  chop($first_line = <LOCK>);
  close(LOCK);

  if ($first_line == $$) {
    $have_lock = 1;
	$lock_process = $$;
    return;
  }

  print "Process $first_line seems to be already running gary.\n";
  &abort("You may not have two instance of gary running at once.  Sorry")
    if (kill(0, $first_line));

  print "That process does not seem to exist any more...\n";
  unlink($gary_lock);
  &get_lock;
}


sub identical {
#
# Return true if two files are identical to one another; false otherwise
# I'm lazy and count on sum being around to do this for me.  
#

  local($f1, $f2) = @_;
  local($s1, $s2) = @_;

  chop($s1 = `$SUM '$f1'`);			# Compute checksums
  chop($s2 = `$SUM '$f2'`);

  $f1 =~ s/(\W)/\\$1/g;				# Dispel regexp magic 
  $f2 =~ s/(\W)/\\$1/g;

  $s1 =~ s/$f1//;				# Remove filenames from strs
  $s2 =~ s/$f2//;

  return ($s1 eq $s2);
}
    

sub find_pg {
#
# find_pg: find the specified executable on this machine, if possible.
#
# We try using which first, assuming that if the desired executable is in
# our path, it's the one we want.
#
# If it's not in our path, we try whereis, returning the first program
# whereis names for us which is executable.
#
# If we can't find what we need, we just return our argument back if our
# second argument is true.  If it's false, we assume that it's crucial that
# we find the program specified, and blow up.
#

  local($pg, $crucial) = @_;
  local($ex) = 1;
  local($try, @found);

  return $pg if ($pg =~ m/^\//);		# Absolute paths know best
  chop($try = `which $pg`);
  return $try if ($try =~ m/^\//);

  chop($try = `whereis $pg`);
  if ($try =~ m/^$pg:\s+\//) {
    @found = split(/\s/, $try);
    $ex++ while (! -x $found[$ex]);
    return $found[$ex] unless ($found[$ex] eq "");
  }

  return $pg if (!$crucial);
  &abort("Could not locate executable \"$pg\"");
}


sub abort {
# 
# Print an error message and exit.  This is for problems we can't recover
# from.
#

  &cleanup;
  print STDERR "@_[0].\n";
  exit(1);
}


sub warn {
# 
# Print an error message and return.  This is for things which really 
# perplex us but which we can recover from.  This might get invoked if
# articles are expired between the time we find out they're around and 
# the time we try to access them, for instance, or if we can't open a 
# file we need to be able to open.
#

  print "@_[0].\n";
}


sub cleanup {
#
# Cleanup code.  This could probably be improved/added-to.
#

  &putline(SOCKET, "quit") if ($connected_to_server);

  untie %subject if defined(tied %subject);

  close(DESC);
  close(DECODE);
  close(SUBJECT);
  unlink($gary_lock) if (($have_lock) && ($lock_process == $$));
  unlink($temp_decode_file);

  close(SOCKET);
}


sub handler {
#
# Die gracefully if interrupted by a signal.
#

  print STDERR "$myproc Dying on signal @_[0]\n";
  #kill all children
  kill 'TERM', @children;
  &cleanup;
  exit(1);
}

sub pad {
#
# Pad a number to six characters (rather excessive, for our needs) with zeros.
#

  local($n) = pop(@_);
  local($i);

  $i = sprintf("%6d", $n); 
  $i =~ s/ /0/g;
  return $i;
}


sub Getopts {
#
# This code was adapted (read that, stolen) from perl's getopt.pl library.
#

    local($argumentative) = @_;
    local(@args,$_,$first,$rest,$errs);
    local($[) = 0;

    @args = split( / */, $argumentative );
    while(($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
        ($first,$rest) = ($1,$2);
        $pos = index($argumentative,$first);
        if($pos >= $[) {
            if($args[$pos+1] eq ':') {
                shift(@ARGV);
                if($rest eq '') {
                  $rest = shift(@ARGV);
                }
                eval "\$opt_$first = \$rest;";
            }
            else {
                eval "\$opt_$first = 1";
                if($rest eq '') {
                    shift(@ARGV);
            }
                else {
                    $ARGV[0] = "-$rest";
                }
            }
        }
        else {
            print STDERR "Unknown option: $first\n";
            ++$errs;
            if($rest ne '') {
                $ARGV[0] = "-$rest";
            }
            else {
                shift(@ARGV);
            }
        }
    }
    $errs == 0;
}


#
# Subroutines -- long, boring subroutines that print out lots of text.
#
# You can't lose the documentation any more.  Beware: variable values get 
# interpolated in here...  It may be occasionally necessary to escape things.
#

sub changes {
#
# Print changes to gary since last version.  We hit only the high points.
#

  print <<"EOF";
    Changes to $last_version to create gary:

    o   Removed support for disk-based (non-NNTP) access to news
    o   Added code to fork child for each group in configuration file
	o   Added dbm code to track unresolved messages in group
	o   Changed subject line retrieval for speed (xhdr)

	Known bugs in this version:

	o	-d0 on command line does not override 'debug N' in 
		 configuration file.

EOF
  exit(0);

}


sub short_manual {
#
# Print out the short form of the manual...memory refresh mode...
#

  print <<"EOF";

  Command line options:

	gary -c		Catch up on all groups, but do not assemble binaries
	gary -g 	Switch off buffering and output info suitable for
	         	glitter program (\$glitter_url)
	gary -dN	Set debugging level to N
	gary -M		Print the gary manual
	gary -m		Print this summary of the manual 
	gary -C		List changes since previous release of gary

  Configuration keywords (for \$HOME/.glitter/garyconf):

	GROUP <GROUP> ...	Causes gary to process the newsgroup(s) listed
	NNTP <HOST>		Use <HOST> as an NNTP news server
	CONnections <N> 	Allow up to N concurrent connections to the NNTP
				 news server
	DIRectory <DIR>		Assemble binaries into the specified 
				 directory (1)
	DESCription <FILE> 	Store descriptions of assembled binaries in the 
				 specified file (1)
	HOOK <PGM>		Decode binaries only when the specified program
				 exits returning status true (zero) (1,2)
	DEFine <NAME> <CMD>	Define a postprocessor called <NAME>, which
				 will invoke the command <CMD> (3)
	POSTprocess <NAME> <EXTN> ...
				Apply postprocessor <NAME> to binaries whose
				 filenames end in any listed <EXTN> suffix (1,3)
	RECognize <EXTN> ...  	Add the suffixes listed to gary's internal 
				 table of common suffixes
	DEBUG <N>		Set the default debugging level to N
	SAMple <N> 	For each group that we have not accessed before
				 examine at most N messages
	USER <name> Username to use for NNTP authentication
	PASS <pass> Password to use for NNTP authentication

  	Notes:

	(1) -- If this keyword appears before any GROUP keyword, it applies to 
	       all groups binaries are assembled from by default.  If the 
	       keyword appears after a given GROUP keyword, it applies to that 
	       group only, and overrides any default which may earlier have 
	       been established with the same keyword.

	(2) -- The hook program will be passed the subject line of the lowest-
	       numbered piece of the binary image on standard input.  It
	       cannot be invoked with arguments (yet).

 	(3) -- If the string "\$h" appears unescaped in the command, it will be 
	       replaced before the command is invoked with the "head" of 
	       the filename of the binary.  Similarly, "\$t" will be replaced 
	       with the "tail", and "\$f" will be replaced with the entire 
	       filename.  The head and tail of "foo.gif" are "foo" and "gif", 
	       respectively.

EOF

  exit(0);
}

sub long_manual {
#
#
#

  print <<"EOF";


			The Introducing gary Document


	1.	What is gary?

	More and more people are posting binary files to usenet these days.
Some of these binaries are executables and audio data; a majority seem to
be pictures of various things, typically landscapes, movie stars and naked
people.  Because of limitations in the type data that usenet can accommodate, 
binaries must be encoded into text, and because binary files are commonly very 
large relative to text files usenet was designed to handle, they frequently 
must be broken up into pieces.  Programs have been developed which take a 
given binary, encode it, and automatically post it in pieces with descriptive 
subject lines.

	When this data arrives at a remote site, users see subject lines
that look something like this:

		12011 roadkill03.gif, part 1/4
		12012 roadkill03.gif, part 3/4
		12013 More pictures of tatooed children, please...
		12014 Re: roadkill02.gif -- I love the way the eyes bulge out
		12015 roadkill03.gif, part 4/4
		12016 roseanne_nude.jpg, part 02 of 02
	   	12017 Only BINARIES should be posted here, GOD DAMMIT	
		12018 roadkill03.gif, part 2/4
		12019 HI, I'M BIFF!!!!  THESE PIX ARE WAY COOL!!!!
		12020 roseanne_nude.jpg, part 01 of 02

	While the process of encoding and splitting up binaries for posting 
to usenet is relatively straightforward, the process of retrieving, sorting,
and decoding the pieces (which do not necessarily arrive in order) at 
receiving sites is less straightforward, tedious, time consuming, and very
prone to human error.  

	gary, which is based on aub, which stands for "assemble usenet
binaries", automates this reassembly process for you.  gary is intended
for use in newsgroups to which binaries are posted exclusively.  When
run, it accesses news articles via an NNTP news server, 
determines whether or not any new binaries have appeared in selected 
newsgroups since the last time it was run, and if so, retrieves, organizes 
and decodes them, depositing them in a configurable location.  This process 
requires no human intervention once gary has been configured.  gary also keeps 
track of binaries which it has seen some, but not all, of the pieces of.  It 
remembers how to find these old pieces, so that when new, previously missing 
pieces arrive at your site, it will build the entire binary the next time it 
is run.  It also remembers which binaries it has already seen all of the 
pieces of already, so that it does not waste time rebuilding the same binaries 
over and over again.

   gary, like aub, was created as a time saver; too many people at too
many sites were spending way too much time manually unpacking binary files.
Its ability to identify and assemble binary images depends on people posting
images with subject lines that observe (loosely) established conventions.
gary's recognition capabilities have been significantly improved since the
earliest release.


	2.	How does gary work?

	gary looks for subject lines containing strings like:

		N of N
		N / N
		N \ N
		N | N

	where N is any number composed of one or more digits, and white
space is optional.  Once it sees such a line, it tries to figure out a
name for the binary by looking at the rest of the subject line.  These names 
are relevant only to gary's internal functioning; when unpacked, binaries are 
named according to the information they were encoded with.  However, it's 
important that, whatever internal name gary decides on for the binary, that 
name be recognizable in the subject lines of all pieces.

	gary also looks for subject lines containing one of the hint extensions.
For example .jpg or .gif. You may specify additional hint extensions to
those specified within gary (see the RECognize keyword below).

	gary ignores all news articles with null subject lines and subject
lines that begin with "Re:" regardless of other content.

	gary uses one file which is maintained in .glitter subdirectory of
each user's home directory. The file is \$HOME/.glitter/garyconf, which
is a configuration file that allows you to customize gary's behavior. See
section 5 for a detailed explanation of the structure of configuration files.
In addition, dbm files are maintained in the .glitter directory. There is one
set of dbm files per newsgroup queried. These keep track of what articles in
which groups gary has resolved already, and what articles gary believes to
be pieces of binaries that it hasn't seen all of the pieces of yet.  


    	3.	What do I need on my system to run gary?

	You will need Larry Wall's perl interpreter.   perl is available via
anonymous FTP from uunet.uu.net, tut.cis.ohio-state.edu, and
jpl-decvax.jpl.nasa.gov.  

	Your machine must also have access to news via the NNTP protocol.


	4.	How do I install gary?

	There's really only one thing that you might need to configure.
gary is a perl script.  The first line of the program looks like this:

		#!/usr/bin/perl

	This appears to tell your shell where to find the perl interpreter.  
If the path of perl on your system is something else, you'll need to change 
this line, or create a link called /usr/bin/perl which points to where
your perl executable actually resides.

	If you need to change this, you'll probably see a message like:
'gary: Bad address.' when you try to run gary.


	5.	How do I configure gary?

	If this section sounds difficult or confusing, try getting hold of
glitter (\$glitter_url). Glitter is a gnome/gtk+
gui application which creates the necessary gary configuration file.

	The configuration file for gary should be called
\$HOME/.glitter/garyconf.

	Configuration files are line-oriented; each line is processed 
separately.  If any line contains the '#' character, gary concludes that 
the character begins a comment, and discards the comment character and 
everything on the line that follows it.  If for some reason you need to
put a '#' character in your configuration file and do not want it to be 
interpreted as beginning a comment, you'll have to escape it by preceding it 
with a backslash character, e.g. '\\#'.

	Each non-blank line in a configuration file must begin with a 
keyword recognized by gary.  The case of keywords is not significant.
As far as gary is concerned, "keyword", "KEYWORD", "Keyword" and "KeYWorD"
all mean the same thing.  Some keywords require arguments; some require no 
arguments appear, and some permit variable numbers of arguments.  If gary 
sees keywords it doesn't understand in your garyconf file, it will complain 
to you about them.

	One of the keywords gary understands is the GROUP keyword.  It's
used to tell gary that you want to decode binaries from the newsgroup(s)
which appear as argument(s) to the keyword.  For example:

		GROUP alt.binaries.pictures.misc
		GROUP alt.binaries.pictures.misc alt.binaries.pictures.fractals

	Every configuration file must contain at least one GROUP keyword to
be correct.  

	In general, gary understands two types of keywords.  One type is 
called 'position insensitive', which means that the keyword will have the
same effect no matter where in the configuration file it appears.  The
other type is called 'position sensitive', which means that the keyword 
means something different when it appears before any GROUP keywords than
it does when it appears after any given GROUP keyword.

	One such position sensitive keyword is the DIRectory keyword.
This keyword is used to tell gary what directory to put binaries it decodes
in.  ("DIRectory" is spelled the way it is because only the 'DIR' part needs 
to appear in a configuration file for gary to recognize it.  In fact, gary will 
interpret any keyword beginning with the letters 'DIR' as being an instance
of the DIRectory keyword.)

	When a position sensitive keyword appears _before_ all GROUP keywords,
the keyword is interpreted as being the default for all groups that appear
later.

	When a position sensitive keyword appears _after_ any GROUP keyword,
it is interpreting as applying *only* to that group, overriding any previous
default which may have been established via use of the same keyword, or
by the value of environment variables (see section 8.)

	Position sensitive keywords appearing after a GROUP keyword which
lists multiple groups are applied only to the last group listed, not to 
all groups appearing on the group line.

	For example, the following three configuration files are equivalent:

	# Sample garyconf file no. 1 -- basic example
	# 
	dir /tmp/gary					# Default directory
	group alt.binaries.pictures.misc		# Process these
	group alt.binaries.pictures.fractals		#  two groups

        # Sample garyconf file no. 2 -- multiple group usage, mixed case
        #
        DiR /tmp/gary                                    # Default directory
        gRoUp alt.binaries.pictures.misc alt.binaries.pictures.fractals

        # Sample garyconf file no. 3 -- does not use defaults
        #
        group alt.binaries.pictures.misc
        directory /tmp/gary                            
        group alt.binaries.pictures.fractals
        direct-to /tmp/gary                           	# 'dir' is all you need

	The following three configuration files are also equivalent, though
not equivalent to the previous three:

        # Sample garyconf file no. 4 -- explicit placement of binaries
        #
        group alt.binaries.pictures.misc
        dir /tmp/gary/misc
        group alt.binaries.pictures.fractals
	    dir /tmp/gary/fractals

        # Sample garyconf file no. 5 -- explicit and default placement 
        #
        dir /tmp/gary/misc   				# Default directory
        group alt.binaries.pictures.misc		# Use default directory
        group alt.binaries.pictures.fractals
	    dir /tmp/gary/fractals				# Override default

        # Sample garyconf file no. 6 -- explicit and default placement revisited
        #
        dir /tmp/gary/fractals 				# Default directory
        group alt.binaries.pictures.misc
	    dir /tmp/gary/fractals				# Override default
        group alt.binaries.pictures.fractals		# Use default directory

	The configuration file:

	# Sample garyconf file no. 7 -- invalid
	#
	group alt.binaries.pictures.misc
	dir /tmp/gary
	group alt.binaries.pictures.fractals		# No good

	is invalid, because no directory for gary to place binaries decoded
from the newsgroup alt.binaries.pictures.fractals is specified.  The 
DIRectory keyword is unique in this regard; there must be some use of the
keyword that enables gary to figure out where to put binaries for every 
group specified, or it will refuse to run.  The easiest way to deal with 
this is to always establish a default directory by using the DIRectory
keyword somewhere before any groups appear.  


	Other position sensitive keywords are available.  


		DESCription <file>

	This keyword causes gary to extract text from what it thinks is the 
text portion of posted articles, and append it to the file you specify.  This
is useful if you're interested in reading the text that describes what all
the binaries gary is unpacking are about.  A maximum of 60 lines per binary
extracted will be put into the file you indicate.  Each description is
prepended with the name of the decoded binary it refers to, and the group
that binary was decoded from.


		HOOK <program>

	This keyword enables you to select which binaries gary decodes
using your own software.  If the HOOK keyword is specified, gary will 
invoke the argument program and supply it with subject line of the first
piece of a binary that it can potentially decode via standard input.  If the 
program returns true (zero), gary will decode the binary.  If the program 
returns false (non-zero), gary will skip decoding the binary, and continue 
processing.

	It is not (yet) possible to specify arguments to the user program.

	For example, the following sample program returns true if standard
input contains the string ".gif" (case insignificant), and false otherwise.

	#!/usr/local/bin/perl
	#
	# /tmp/sample_gary_hook: a simple, sample hook program
	#

	\$sl = <STDIN>;                  # Get standard input
	exit(0) if (\$sl =~ m/\.gif/i);   # Contains ".gif"
	exit(1);			# Didn't see ".gif"

	Suppose this program were attached to gary via the configuration line:

		hook /tmp/sample_gary_hook

	Then gary would only decode binaries containing the string '.gif'.

	You can write hook programs in any language you choose.  


		POSTprocess <postprocessor> <extn> ...

	This keyword enables you to postprocess binaries whose names end
in the string <extn> (you can list any number of these suffixes on a single
line in the configuration file.)  Case is not significant in <extn>.  Before
a POSTprocess keyword can appear, <postprocessor> must first be defined 
using the DEFine keyword, which is position insensitive.  The format of
the DEFine keyword is

		DEFine	<postprocessor> <unix cmd>

	<postprocessor> may be any string.  It's recommended that you
stick to alphanumerics.

	<unix cmd> is any UNIX command, with arguments.  Simple substitutions
are performed on <unix cmd> before it's executed in conjunction with the
existenece of a POSTprocess keyword and the appearance of a binary whose
filename ends in one of the <extn> suffixes listed as arguments to the 
POSTprocess keyword.  This all makes perfect sense but is a little difficult
to explain.  The following example should make things much clearer.

	Consider the following configuration file:

	# Sample gary configuration file demonstrating use of a postprocessor
	#
	dir /tmp/garydir
	define jpg2gif djpeg -G \$f > \$h_.gif
	postprocess jpg2gif .jpg .jpeg
	group alt.binaries.pictures.misc

	The first line tells gary that it should decode binaries into the
directory /tmp/garydir.  The second line defines a postprocessor for gary.  
The name of the postprocessor is specified as "jpg2gif".  The third line 
says that the postprocessor will be invoked whenever a binary with a name 
ending in '.jpg' or '.jpeg' is decoded.  The fourth line specifies the 
group that binaries are to be decoded from.

	Suppose the binary full_moon.jpeg is decoded from 
alt.binaries.pictures.misc.  The binary name "full_moon.jpeg" can be 
thought of as consisting of three parts; the head part -- everything before
the last '.' character --  the '.' character itself, and the tail part --
everything after the last '.' character.  gary uses the abbreviations 
'\$h', '\$t', and '\$f' to refer to the head part, tail part, and entire
filename, respectively.  (If no '.' character appears in the name of a 
decoded binary, \$h equals \$f, the entire name of the binary, and \$t is 
empty.) 

	Because the binary name "full_moon.jpeg" ends in ".jpeg", one of the
arguments specified on line two of the sample configuration file, gary 
invokes the postprocessor "jpg2gif".  gary substitutes the appropriate 
values for '\$f' and '\$h', in this case, "full_moon.jpeg" and "full_moon"
into the postprocessor definition, and executes the resulting UNIX command,
which in this case is 'djpeg -G full_moon.jpeg > full_moon_.gif'  Assuming 
that you have the djpeg program on your machine (this software is available 
via anonymous FTP from ftp.uu.net under the graphics/jpeg directory), this 
command will cause the .jpeg file to be automatically converted into a 
similarly named .gif file when it is decoded.

	A few more examples, again, based on the configuration file above

   Filename of decoded binary        \$h		\$t		\$f
------------------------------------------------------------------------------
	crescent_moon.jpg	crescent_moon	jpg	crescent_moon.jpg
	big.dog.gif		big.dog		gif	big.dog.gif

   Filename of decoded binary	Postprocessed         Reason
------------------------------------------------------------------------------
	crescent_moon.jpg	   yes       \$f ends in '.jpg'
	big.dog.gif		   no	     \$f doesn't end in '.jpg' or in
					      '.jpeg'

    Filename of decoded binary	UNIX command executed
------------------------------------------------------------------------------
	crescent_moon.jpg	djpeg -G crescent_moon.jpg > crescent_moon_.gif
	big.dog.gif		(none executed)


	We could have easily have written:

		define jpg2gif djpeg -G \$f > \$h_.gif ; rm -f \$f 

	to cause gary to remove the old .jpeg version of the binary after
converting it to .gif format.

	I've added the extra underscore character in this example to 
decrease the chance that djpeg, when it runs, will clobber another 
binary which gary already unpacked with the name "full_moon.gif" or
"cresecent_moon.gif". 

	Postprocessor definitions that can't be executed for some reason
may cause you (and gary) some problems at run time.  


	The following keywords are, like DEFine, position independent:


		NNTP <server>

	This tells gary that your news access is NNTP-based, and that it
should use the specified host as an NNTP server. 

	If the NNTP keyword does not appear in your configuration file,
gary will  use your NNTPSERVER environment variable, if it is defined,
to decide what server to connect to.  If your NNTPSERVER environment
variable is not defined, gary will try to figure out where you normally
read news from. If it can't do that, it will ask you to supply the information.

	If you ever change the mechanism by which you access news, or the
server you read news on, you'll need to remove the dbm files in the .glitter
directory that gary maintains to keep track of what groups you have and have
not read.  Otherwise, because articles are numbered differently on different
servers, gary will get hopelessly confused.  This is probably the only time
you'll ever want to tamper with a dbm file. The dbm files are easily
recognized as their titles include the name of a newsgroup.


		CONnection <n>

	Sets the number of concurrent connections allowed to your NNTP server.
Some news servers allow multiple connections from the same host. Set this
to the maximum that your news server allows to get maximum bandwidth. This
especially important if you have a fast connection such as a cable modem
or DSL.

		SAMple <n>

		For each group that we have not accessed before examine at most n messages.
  
		USER <name>

		Username to use for NNTP authentication
  
		PASS <pass>

		Password to use for NNTP authentication
  
		DEBUG <n>

	Sets the default debugging level gary runs at to N.  N must be a 
non-negative integer.  Debugging level 0 is the default; when run at 
debugging level zero, gary produces no output unless it runs into serious
problems.  Setting the debugging level to 1 will tell you about what gary is
doing.  Setting the debugging level to 2 will tell you even more about what
gary is doing.  Setting the debugging level to 3 or higher will show you 
more than you ever wanted to know.


		RECognize <extn> ...

	The recognition code (the part of gary that identifies binaries) 
maintains a list of common suffixes that it uses to recognize binaries 
while it scans subject lines.  For example, many binaries have names ending 
in ".gif", so ".gif" is on gary's internal list of hints.  The RECognize
keyword allows you to add suffixes to this internal list of hints.

	Use this capability sparinging.  You can really give gary a coronary 
by saying something like 'rec a b c d e f g ...'.  Doing something foolish 
like that will cause your gary to lose the ability to assemble things that it 
would otherwise have been able to.  

	The current list of common suffixes gary maintains is:

	".gif", ".jpg", ".jpeg", ".gl", ".zip", ".au", ".zoo", ".exe", ".dl", 
	".snd", ".mpg", ".mpeg", ".tiff", ".lzh", ".wav", ".mp3", ".png"


	If the same keyword appears multiple times, and the second 
appearance is not a position sensitive override of some established default,
then gary ignores the second instance of the keyword.


	7.	How do I use gary?

	After you've built your configuration file, just run 'gary'.  

	If this is the first time you've run any version of gary, you 
may want to use the '-c' command line option.  Or you may not...see section 9.


	8.	Environment variables used by gary.

	\$GARYDIR	Sets the default directory binaries are unpacked into.
			Equivalent to specifying a DIRectory keyword before 
			any GROUP keywords.  Will override any DIRectory 
			keyword appearing before any GROUP keyword, but not 
			those appearing after a GROUP keyword.

	\$GARYDESC	Analogous to \$GARYDIR

	\$GARYHOOK	Analogous to \$GARYDIR

	\$NNTPSERVER	Specifies an NNTP server to use for news access if
			no NNTP keyword appears in the configuration file.
			If an NNTP keyword does appear, \$NNTPSERVER is 
			ignored.

	If gary doesn't seem to be doing what you'd expect it to do based
on your garyconf file, it could be because your environment variables
are causing defaults you've established there to be ignored.


	9.	Command line options supported by gary:

	-c		'Catch-up' mode; gary will bring its internal 
			pointers (and your dbm files) up to date, but will 
			not actually generate any binaries.  This is useful 
			when you run gary for the first time; it keeps it 
			from generating megabytes and megabytes, as it scans 
			old news articles.

	-g		'glitter' mode: Switch off buffering and output info
			suitable for glitter display in program
			(\$glitter_url)

	-dn		'Debug' mode; sets the debugging level to N.  This
			overrides the debugging level set in the configuration
			file, except that 'gary -d0' does not work...this is a 
			bug.

	-M		Causes gary to print the long form of the documentation
			(this document.) 

	-m		Causes gary to print a summary of the documentation.

	-C		Lists significant changes since that last major 
			release of gary.


	10.	What do I do if I have problems installing or configuring gary?

	See if you can figure out what the problem is.  I've only set gary
up on my local system, so it's possible you could have problems I haven't
foreseen.  If you really can't get it to work, try talking to a friend who
knows systems programming and administration type stuff.  Offer your friend
food -- systems people especially like dim sum and Heineken.

	You could also send me mail.  Whether or not I answer your mail will 
depend a lot on how busy I am.  Sorry, but I have an obligation to get work 
done promptly for my client, who's paying me for my time.  I can't really deal 
with supporting gary on the side for the entire net.  Also, if your problem
has to do with peculiarities of your local site, there may not be a lot I 
can do about it.


	11.	What else do I need to know?

	In order to guarantee proper administration of the dbm files,
you can only run one instance of gary at a time.  In this respect gary is
similar to most newsreaders.

	The first time you run gary over a given group, if you choose not to
use the -c option, it may take a long time to run.  This is because it's 
looking at all of the articles in the group, and building lots of binaries.  
After you run it for the first time, it only needs to look at new stuff in 
the group.  Things will go much faster after that.  

	If gary assembles two binaries with the same name, and wants to store
them in the same place, it will compare them to see whether or not they're 
identical.  If they are identical, it will discard the newer copy.  If 
they're not identical, it will append '+' characters as necessary to the 
name of the second binary until the name is unique.

	gary takes liberties with changing around the names of binaries 
that it doesn't particularly like.  It may rename binaries to be called
"Mangled" if people post things that are supposed to be unpacked to "." or 
"..", or something equally obnoxious, for instance.  It will drop the 
leading "." off of binaries called ".something", and relativize pathnames
so that your binaries always wind up in the directories you want them in.

	It's unfriendly to run gary so often that you occupy too much of your
news server's time.

	This software is offered as-is, with no guarantees or promises made 
by me whatsoever.  I disclaim all responsibility for loss or damage caused
by the program.

(Almost all of this work is due to Stantz, but as it is not compatible
with aub, I have changed its name. If anything doesn't work then it
is probably my fault - aub has worked for nearly a decade.)

Alexander Mosley  
ajm\@netlink.co.uk
2/2000
						                     
slightly modified version of work by

Mark Stantz
stantz\@sierra.stanford.edu
stantz\@sgi.com
8/92


EOF
  exit(0);
}
