#!/usr/bin/perl -w

# Written by Jeffrey M. Vinocur <jeff@litech.org>
# This work is hereby placed in the public domain by its author.

use strict;

##############################################################################
###      Edit the below for your system and desired nntp//rss config       ###
##############################################################################
my $PREFIX     = 'local.rss.';           # Should end in a dot, unless it's ""
my $PULLNEWSRC = 'rss.pullnews';         # Name of pullnews config file
my $RCPATH     = '/usr/local/news';      # Where to keep $PULLNEWSRC
my $PATHBIN    = '/usr/local/news/bin';  # Where to find `ctlinnd`


##############################################################################
###                  It's safe to ignore these for now                     ###
##############################################################################
my $REST       = 'n';                    # Fourth field documented in access(5)
my $CREATOR    = 'nntp//rss';            # Creator logged to active.times


##############################################################################
###                  No user-servicable parts below :-)                    ###
##############################################################################


$0 =~ s@.*/@@;

die << "END" unless @ARGV;
Usage:  $0 [$PREFIX]group.name.here ...

$0 is a tool for keeping an INN server in sync with nntp//rss.  Each
commandline argument is a newsgroup to create within the \"$PREFIX*\"
hierarchy.  (\"$PREFIX\" will be added to any newsgroup names that
do not already start with it.)  If you want to use a different hierarchy
than \"$PREFIX*\" (or none at all), just edit the configuration options
at the top of this script ($0).

Each newsgroup specified (which should *already* exist on the nntp/rss
server) is first created in INN, using \`ctlinnd newgroup\`.  Then, an
appropriate line is appended to the pullnews configuration file so 
pullnews will begin fetching the group on its next run.


You presumably want to run pullnews out of cron to actually fetch the
articles ($0 only creates the group in INN and configures pullnews, it
doesn't actually run pullnews).  To keep from getting spammed by the
verbose output of pullnews, you might run with -q or try something like:

* * * * * bin/pullnews -c $PULLNEWSRC >& .pullnews.log || cat .pullnews.log

in your crontab.  If you ever delete a group from nntp//rss, pullnews will
probably just fail miserably, so be sure to remove the appropriate line
from $RCPATH/$PULLNEWSRC, and remove the group from INN if desired
(with \`ctlinnd rmgroup\`).

END

die << "END" unless -f "$RCPATH/$PULLNEWSRC";
You need to first create the pullnews configuration file.  Check the
pullnews(8) manpage for details, but a minimal one (to which $0 can
add new groups for you) is a single line containing the hostname or
IP of the news server running nntp//rss, with :port added if it is 
not running on port 119.  If your nntp//rss server is set for "Secure"
NNTP, add at the end of that same line a space, the username, a space,
and then the password.

END



foreach (@ARGV) {
  chomp;
  $_ = "$PREFIX$_" unless /^$PREFIX/;
  print "Adding $_ ... ";
  my $result = `$PATHBIN/ctlinnd newgroup $_ $REST $CREATOR` or die "\n";
  chomp $result;
  unless ($result eq 'Ok') {
    print "\`ctlinnd newgroup\` failed:  $result\n";
    next;
  }
  print "pulling ... ";
  open(PULL, ">> $RCPATH/$PULLNEWSRC") or die "$!";
  print PULL "\t$_\n";
  close(PULL);
  print "done\n";
}

1;
