Article View: news.software.nntp
Article #36713Creating a newsgroups description file (was Re: newsgroups file)
From: Jeremy
Date: Sat, 17 Apr 1999 00:00
Date: Sat, 17 Apr 1999 00:00
156 lines
5843 bytes
5843 bytes
Brad Knowles <brad@shub-internet.org> wrote:
>> Someday I'll get around to that. Hmm, maybe I'll do that this week.
>
> So, where is the code? I want to see some code, dammit! ;-)
Here ya go. It's quick, it's dirty, it's barely tested, but it just
worked for me. :)
#!/usr/local/bin/perl
#
# mkngfile - make a newsgroup description file from multiple sources
#
# Jeremy Nixon <jeremy@exit109.com>
# $Id: mkngfile,v 1.1 1999/04/17 09:19:25 jeremy Exp $
#
# This program may be used and distributed under the same terms
# as Perl itself.
# This program creates a newsgroup description file, using one or
# several input files containing group descriptions. The resulting
# file will contain a description line for each group in your active
# file.
#
# If the input contains multiple different descriptions for a group,
# the program will prompt interactively for which one to use; or, if
# the --noask option is given, one will be chosen arbitrarily. If a
# group has no description, $default_desc (below) will be used.
#
# The output will be sent to stdout, or to the file specified with
# the --output (or -o) option.
#
# Example - to run with your existing newsgroups file, a local copy
# of the ISC newsgroups file, and a directory containing checkgroups
# files with names like *.check, creating the new file as 'newfile':
# mkngfile -o newfile /news/etc/newsgroups newsgroups checkgroups/*.check
#
# You can set the location of your active file below so you don't
# have to specify it on the command line.
use Getopt::Long;
GetOptions (\%opt, qw(h help a=s active=s ask! o=s output=s)) || usage();
usage() if ($opt{'h'} or $opt{'help'});
usage('No input files specified.') unless (scalar @ARGV > 0);
# If a group has no description, use this.
$default_desc = '???';
# Default to asking which of multiple descriptions to use.
# Comment this out if you want the default to be not to ask.
$opt{'ask'} = 1 unless (defined $opt{'ask'});
# Default to system active file; you may want to change this.
$active_file = $opt{'active'} || $opt{'a'} || '/news/etc/active';
usage('No active file.') unless (-r $active_file);
$output_file = $opt{'output'} || $opt{'o'} || undef;
# Read in the active file and create a hash.
open (F, "<$active_file") || die "Can't open $active: $!\n";
%active = map { (split /\s+/, $_)[0,3] } <F>;
close F;
# Read in all the files and create a hash.
%groups = ();
while (defined ($file = shift @ARGV)) {
unless (open (F, "<$file")) {
print STDERR "Can't open $file: $!\n";
next;
}
while (defined ($line = <F>)) {
chomp $line;
($group,$desc) = split /\s+/, $line, 2;
$desc =~ s/\(Moderated\)//gi;
$desc =~ s/^\s+//;
$desc =~ s/\s+$//;
next if ($desc =~ /^\s*$/ or $desc =~ /^\?+$/ or $desc =~ /no description/i);
$groups{$group}{$desc} = 1;
}
close F;
}
# Go through the active file and find each group's description.
@descriptions = ();
foreach $group (sort keys %active) {
$desc = pick_description ($group);
push @descriptions, "$group\t$desc";
}
# If we have an output file, open it; otherwise STDOUT is used.
if ($output_file) {
open (OUT, ">$output_file") or die "Can't open $output_file: $!\n";
open (STDOUT, ">&OUT") or die "Can't redirect stdout: $!\n";
}
# Print the new file.
map { print "$_\n" } @descriptions;
close OUT if ($output_file);
sub pick_description {
# Given a group, return the description.
# If we have more than one description for a group, we ask the
# user which one to use, or just pick one if --noask is specified.
# If we have no description for a group, use $default_desc.
# If the group is moderated, append '(Moderated)' to the description.
my ($description,$n,$c);
my $ng = shift;
my @descs = sort keys %{$groups{$ng}};
if (scalar @descs < 1) {
$description = $default_desc;
} elsif (scalar @descs == 1) {
$description = $descs[0];
} else {
if ($opt{'ask'}) {
print STDERR "$ng - choose description:\n";
map { print STDERR " ". ++$n .". $_\n" } @descs;
until ($c > 0 and $c <= scalar @descs) {
print STDERR "> ";
$c = <STDIN>;
chomp $c;
}
$description = $descs[--$c];
} else {
$description = $descs[0];
}
}
$description .= ' (Moderated)' if ($active{$ng} =~ /^m/i);
return $description;
}
sub usage {
my $message = shift;
print STDERR "$message\n" if ($message);
print STDERR "\nusage:\n";
print STDERR "mkngfile [options] file [file...]\n";
print STDERR " -h, --help display this message\n";
print STDERR " -a, --active path to active file\n";
print STDERR " -o, --output output file to use instead of stdout\n";
print STDERR " --ask ask which of multiple descriptions to use (default)\n";
print STDERR " --noask don't ask, just pick one\n";
print STDERR "\n";
exit 1;
}
Technical Details
Message-ID:
<7f9l34$k0o$1@east44.supernews.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailReferences:
<3714C953.5B0555C1@online.be> <877lresn86.fsf@slip-32-100-202-51.ma.us.ibm.net> <7f3hm5$hfm$1@east44.supernews.com> <brad-1504991106480001@brad.techos.skynet.be>Show Full Headers
Complete Article Headers
Message-ID: <7f9l34$k0o$1@east44.supernews.com> Subject: Creating a newsgroups description file (was Re: newsgroups file) Date: Sat, 17 Apr 1999 00:00:00 +0000 From: Jeremy <jeremy@exit109.com> References: <3714C953.5B0555C1@online.be> <877lresn86.fsf@slip-32-100-202-51.ma.us.ibm.net> <7f3hm5$hfm$1@east44.supernews.com> <brad-1504991106480001@brad.techos.skynet.be> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 467519779 X-Complaints-To: newsabuse@remarq.com X-Trace: 924342180 HGJFWBHQJ1A29CE84 east44.supernews.com Organization: There Is No Organization Newsgroups: news.software.nntp X-Meow: Meow