#!/usr/bin/perl -w
#============================================================================
#
# touch_newsgroup
#
#   Utility to force leafnode to download news from very-low traffic
#   newsgroups by reading the last article from the newsgroup.
# 
#   Copyright (c) 1999 Jim Nicholson <j.nicholson@computer.org>.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#============================================================================

use strict;
use Getopt::Std;
use Net::NNTP;

my %option = ();
getopts("f:h:v", \%option);
die "No groups specified\n" if (($#ARGV == -1) && (!$option{f}));
my $nntp_host = $option{h} || "127.0.0.1";

my $server = Net::NNTP->new($nntp_host)
  or die "Can't connect to news server: $@\n";
print "touching newsgroups on server $nntp_host\n" if ($option{v});

if ($option{f}) {
  open GROUPFILE, $option{f} or die "Cannot open $option{f}\n";
  while (<GROUPFILE>) {
    chomp;
    read_article($_);
  }
  close GROUPFILE;
}

foreach (@ARGV) {
  read_article($_);
}

sub read_article {
  my ($groupname) = @_;
  my ($narticles, $first, $last, $name) = $server->group($groupname)
    or die "Can't select $groupname\n";
  if ($narticles > 0) {
    my $lines = $server->article() #fetch current/first article
      or die "Can't get first article in $name\n";
    print "read first article from $name\n" if ($option{v});
  } else {
    print "no articles in $name\n";
  }
}
