Ticket #25: cleannewsgroups

File cleannewsgroups, 2.3 KB (added by eagle, 14 years ago)

cleannewsgroups from Arthur Hagan

Line 
1#!/bin/perl
2# cleannewsgroups.pl
3# Copyright 1997-1999 Arthur Hagen
4# This program is Freeware.  It's not GPL or Public Domain.
5
6require 5.0;
7$DEBUG=1;
8
9# Change the path below!
10$dbdir = '/usr/local/news/db';
11
12chdir($dbdir)
13  or die "Can't cd to '$dbdir'\n";
14
15my %ng;
16my @groups;
17
18print "Reading active ...\n" if $DEBUG;
19open(INFILE,'<active')
20  or die "Cant find active file in '$dbdir'\n";
21while (<INFILE>) {
22    my ($group,@rest) = split;
23    push @groups,$group;
24}
25
26print "Reading newsgroups ...\n" if $DEBUG;
27open(INFILE,'<newsgroups')
28  or die "Can't find newsgroups file in '$dbdir'\n";
29while (<INFILE>) {
30    chomp;
31    my ($group,@rest) = split /\t/;
32    my $desc = $rest[-1]; # Only the last entry, no need for extra tabs.
33    # Remove duplicate (Moderated) comments
34    while ($desc =~ s/\(Moderated\)(\s)+\(Moderated\)/(Moderated)/g)
35      {};
36    # Strip trailing spaces too
37    $desc =~ s/(\s)+$//;
38    $ng{$group} = $desc;
39}
40close INFILE;
41
42print "Reading newsgroups.full ...\n" if $DEBUG;
43if (open(INFILE,'<newsgroups.full')) {
44    # No harm done if there aren't any newsgroups.full file.  It will
45    # be created later.
46    while (<INFILE>) {
47        chomp;
48        my ($group,@rest) = split /\t/;
49        # Let newsgroups take precedence over newsgroups.full
50        unless ($ng{$group}) {
51            my $desc = $rest[-1];
52            while ($desc =~ s/\(Moderated\)(\s)+\(Moderated\)/(Moderated)/g)
53              {};
54            $desc =~ s/(\s)+$//;
55            $ng{$group} = $desc;
56        }
57    }
58    close INFILE;
59}
60
61# Remove this part if you want your newsgroups file to be in the same
62# order as your active file.  I prefer both to be sorted.
63print "Sorting groups ...\n" if $DEBUG;
64my @groupss = sort @groups;
65
66print "Writing newsgroups.full.n ...\n" if $DEBUG;
67open(OUTFILE,'>newsgroups.full.n');
68foreach $group (sort keys %ng) {
69    # One tab is enough.  Pretty-printing is for humans, not machines.
70    print OUTFILE $group,"\t",$ng{$group},"\n";
71}
72close OUTFILE;
73
74print "Writing newsgroups.n ...\n" if $DEBUG;
75open(OUTFILE,'>newsgroups.n');
76my $numgroups = scalar @groupss;
77for ($i=0;$i<$numgroups;$i++) {
78    my $group = $groupss[$i];
79    if (my $desc = $ng{$group}) {
80        print OUTFILE $group,"\t",$desc,"\n";
81    }
82}
83close OUTFILE;
84
85print "Done.\n" if $DEBUG;
86
87exit 0;