🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #36580

Re: newsgroups file

#36580
From: Russ Allbery
Date: Wed, 14 Apr 1999 00:00
186 lines
7972 bytes
Jeremy <jeremy@exit109.com> writes:

> I keep meaning to write up a nice little script that'll take all the
> files in my directory-o-checkgroups, my existing newsgroups file, and
> the ISC newsgroups file, and spit out a nice complete newsgroups file.
> The trick is when you get two descriptions for the same group in the
> input and have to decide which to use.  Someday I'll get around to that.
> Hmm, maybe I'll do that this week.

Attached is my cleaning script in case it may be of some help to someone.
A couple of things to note about this:  It doesn't do locking like it
should, mostly because back when I wrote it originally I didn't understand
the locking mechanisms (the comments on this subject are wrong).  It also
strictly enforces an 80-character limit, *including* the (Moderated) tag.
Yes, I actually invent new descriptions for those Big Eight, de.*, it.*,
etc. groups that have descriptions that are too long.  Yes, this means
that my newsgroups file probably has a few things that are absolutely
hilarious Italian, since I don't speak it.  :)

(I do that because a copy of the newsgroups file is stuck in a public
directory every night for people to read through and search, and I didn't
want weird wrapping in 80 column screens.)

-- 
Russ Allbery (rra@stanford.edu)         <URL:http://www.eyrie.org/~eagle/>


#!/usr/bin/perl
$version = q$Id: ngclean,v 0.5 1998/06/27 09:46:51 eagle Exp $;
#
# ngclean -- Clean up the newsgroups file based on the active file.
#
# Written by Russ Allbery <rra@stanford.edu>
# Copyright 1997, 1998 Board of Trustees, Leland Stanford Jr. University

############################################################################
# Site configuration
############################################################################

# The full path to the active file.
$active = '/var/news/data/active';

# The full path to the newsgroups file.
$newsgroups = '/var/news/data/newsgroups';

# The directory to use for temporary files.
$tempdir = $ENV{TMPDIR} || '/tmp';


############################################################################
# Modules and delcarations
############################################################################

require 5.001;

use strict;
use vars qw($active $newsgroups $tempdir $version);


############################################################################
# File parsing and format
############################################################################

# Read the active file into the passed hash, storing the group names as the
# keys and the moderation status as the values.
sub read_active (\%) {
    my $groups = shift;
    open (ACTIVE, $active) or die "$0: cannot open $active: $!\n";
    print 'Scanning active file...';
    %$groups = map { (split)[0,3] } <ACTIVE>;
    close ACTIVE;
    print "done\n";
}

# Takes in a newsgroup name and adds the right number of tabs after it for
# an entry in a newsgroups file.
sub add_tabs {
    my $newsgroup = shift;
    my $extra = int ((23 - length $newsgroup) / 8);
    $extra = $extra > 0 ? $extra : 0;
    return $newsgroup . ("\t" x (1 + $extra));
}

# Prompts for and reads in a newsgroup description from STDIN.
sub get_description {
    my $newsgroup = shift;
    print "Enter description:\n";
    print add_tabs ($newsgroup);
    my $description = <STDIN>;
    $description =~ s/\s+$//;
    return $description;
}


############################################################################
# Main routine
############################################################################

# Clean up $0 for error messages.
$0 =~ s%.*/%%;
$| = 1;

# Read in the active file.
my (%active, %description);
read_active (%active);

# Now, we're going to do editing of the newsgroups file, which inherently
# involves a race condition since the server, when creating a new group,
# just does the equivalent of cat >> to the file.  Unfortunately, there's no
# way of avoiding that entirely, but we can at least make the window small
# by checking the file size before and afterwards.
my $size = -s $newsgroups;
unless ($size) { die "$0: newsgroups file is empty?\n" }

# Open the newsgroups file to grab the current descriptions.
open (NEWSGROUPS, $newsgroups) or die "$0: cannot open $newsgroups: $!\n";

# Walk the newsgroups file, inspecting each line.  We're checking for lines
# that are too long, fixing tabbing, removing lines for groups that no
# longer exist, and looking for groups without newsgroup lines.  In order to
# find the latter, we delete a group out of the active hash as we find its
# description and then will look at what's left in the hash when we're done.
# We store the descriptions in a parallel hash, and in order to let later
# descriptions for the same group override earlier ones, we check to see if
# there's already an entry in the hash for a group.
while (<NEWSGROUPS>) {
    my ($group, $description) = split (/\s+/, $_, 2);
    $description =~ s/\s+$//;
    if ($description{$group}) {
        print "Using newer description for $group\n";
    } elsif (!$active{$group}) {
        print "Discarding nonexistant group $group\n";
        next;
    }
    if ($description =~ s/( ?\(Moderated\)){2,}$/ (Moderated)/) {
        print "$group had duplicate (Moderated) tags, removed\n";
    }
    my $moderated = /\(Moderated\)$/;
    if (!$moderated && $active{$group} eq 'm') {
        print "$group is moderated, adding (Moderated) to description\n";
        $description .= ' (Moderated)';
    } elsif ($moderated && $active{$group} ne 'm') {
        print "$group is not moderated, removing (Moderated) from "
            . "description\n";
        $description =~ s/ ?\(Moderated\)$//;
    }
    my $maxlength = 80 - length $group;
    $maxlength -= 8 - ((length $group) % 8);
    $maxlength -= 8 if (length $group < 16);
    $maxlength -= 8 if (length $group < 8);
    if (length $description > $maxlength) {
        print "Line too long (", length $description, "> $maxlength):\n";
        print $_;
        $description = get_description ($group);
    }
    $description{$group} = add_tabs ($group) . $description;
    delete $active{$group};
}

# See what's left in the active hash and get descriptions for those groups.
for (sort keys %active) {
    $description{$_} = add_tabs ($_) . get_description ($_);
}

# Okay, we're done.  Sort our descriptions and print them out.
open (NEW, "> $tempdir/ngclean.$$") or die "$0: can't open temp file: $!\n";
for (sort keys %description) {
    print NEW $description{$_}, "\n";
}
close NEW;

# Check to make sure the newsgroups file is still the same size as when we
# started, and if so, replace it in place.  If not, tell the user where we
# left the temporary copy.
if (-s $newsgroups == $size) {
    rename $newsgroups, "$newsgroups.old"
        or die "$0: unable to save copy of old newsgroups file: $!\n";
    system 'mv', '-f', "$tempdir/ngclean.$$", $newsgroups
        and die "$0: unable to mv newsgroups file: status $?\n";
} else {
    warn "$newsgroups appears to have changed.\n";
    warn "Leaving the new copy in $tempdir/ngclean.$$\n";
    exit 1;
}

Technical Details
Message-ID: <yl90bu1vb8.fsf@windlord.stanford.edu>
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
References: <3714C953.5B0555C1@online.be> <ylemlm3hf3.fsf@windlord.stanford.edu> <7f39iv$efo$1@east44.supernews.com> <877lresn86.fsf@slip-32-100-202-51.ma.us.ibm.net> <7f3hm5$hfm$1@east44.supernews.com>

Show Full Headers
Complete Article Headers
Message-ID: <yl90bu1vb8.fsf@windlord.stanford.edu>
Subject: Re: newsgroups file
Date: Wed, 14 Apr 1999 00:00:00 +0000
From: Russ Allbery <rra@stanford.edu>
References: <3714C953.5B0555C1@online.be> <ylemlm3hf3.fsf@windlord.stanford.edu> <7f39iv$efo$1@east44.supernews.com> <877lresn86.fsf@slip-32-100-202-51.ma.us.ibm.net> <7f3hm5$hfm$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-mail
X-Deja-AN: 466547984
Organization: The Eyrie
Newsgroups: news.software.nntp