#!/usr/bin/perl -w
#
# $Id: syncgroups.pl,v 1.1 2001/08/29 14:18:26 cord Exp $
#
# DESCRIPTION: syncgroups.pl - connect to a newsserver and generate a
#   checkgroups-list
#
# Copyright (C) 2001 Cord Beermann
#
# URL: http://Cord.de/tools/news/
#
# AUTHOR: Cord Beermann (Cord@Wunder-Nett.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., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

# A Perl script is "correct" if it gets the job done before your boss fires
# you.
#   -- 'Programming Perl Second Edition'
#       by Larry Wall, Tom Christiansen & Randal L. Schwartz

# If you have to remove this, read the README!
require 5.002;
use vars qw($opt_C $opt_h $opt_p $opt_V);
use Net::NNTP;
use Getopt::Std;

getopts('Chp:V');

$VERSION='syncgroups.pl $Revision: 1.1 $';

$COPYRIGHT='Copyright (C) 2001 Cord Beermann.
syncgroups.pl comes with ABSOLUTELY NO WARRANTY. It is free software, and you
are welcome to redistribute it under certain conditions. See source for
details.  Homepage: http://cord.de/tools/news/';

$USAGE='Usage: syncgroups.pl news.host.name [-ChV]

-C         copyright (prints the copyright)
-h         help    (prints out this message)
-V         Version (prints version-info)
';

print("$USAGE\n\n$COPYRIGHT\n\n") if ($opt_h);
print("$COPYRIGHT\n\n") if ($opt_C);
print("$VERSION\n\n$COPYRIGHT\n\n") if ($opt_V);

exit 0 if (($opt_h) or ($opt_C) or ($opt_V));

die("$0: please tell me which host i should connect to\n")
  if not defined($ARGV[0]);

$nntp = Net::NNTP->new($ARGV[0]) or die "$0: Can't connect to server '$ARGV[0]'\n";
$nntp->reader() or die "$0: can't set mode reader\n";
$active =  $nntp->list or die "$0: can't read active-file\n";
$newsgroups = $nntp->newsgroups or die "$0: can't read newsgroups-file\n";;
$nntp->quit;

foreach $group (sort keys %$active) {
  if (exists $newsgroups->{$group} and $newsgroups->{$group} !~ /^\s*$/) {
    print "$group\t" . $newsgroups->{$group} . "\n";
  } else {
    if ($active->{$group}[2] eq 'm') {
      print "$group\tNo description. (Moderated)\n";
    } else {
      print "$group\tNo description.\n";
    }
  }
}

foreach $group (sort keys %$newsgroups) {
  unless (defined $active->{$group}) {
    print "!$group\n";
  }
}
