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 | |
---|
6 | require 5.0; |
---|
7 | $DEBUG=1; |
---|
8 | |
---|
9 | # Change the path below! |
---|
10 | $dbdir = '/usr/local/news/db'; |
---|
11 | |
---|
12 | chdir($dbdir) |
---|
13 | or die "Can't cd to '$dbdir'\n"; |
---|
14 | |
---|
15 | my %ng; |
---|
16 | my @groups; |
---|
17 | |
---|
18 | print "Reading active ...\n" if $DEBUG; |
---|
19 | open(INFILE,'<active') |
---|
20 | or die "Cant find active file in '$dbdir'\n"; |
---|
21 | while (<INFILE>) { |
---|
22 | my ($group,@rest) = split; |
---|
23 | push @groups,$group; |
---|
24 | } |
---|
25 | |
---|
26 | print "Reading newsgroups ...\n" if $DEBUG; |
---|
27 | open(INFILE,'<newsgroups') |
---|
28 | or die "Can't find newsgroups file in '$dbdir'\n"; |
---|
29 | while (<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 | } |
---|
40 | close INFILE; |
---|
41 | |
---|
42 | print "Reading newsgroups.full ...\n" if $DEBUG; |
---|
43 | if (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. |
---|
63 | print "Sorting groups ...\n" if $DEBUG; |
---|
64 | my @groupss = sort @groups; |
---|
65 | |
---|
66 | print "Writing newsgroups.full.n ...\n" if $DEBUG; |
---|
67 | open(OUTFILE,'>newsgroups.full.n'); |
---|
68 | foreach $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 | } |
---|
72 | close OUTFILE; |
---|
73 | |
---|
74 | print "Writing newsgroups.n ...\n" if $DEBUG; |
---|
75 | open(OUTFILE,'>newsgroups.n'); |
---|
76 | my $numgroups = scalar @groupss; |
---|
77 | for ($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 | } |
---|
83 | close OUTFILE; |
---|
84 | |
---|
85 | print "Done.\n" if $DEBUG; |
---|
86 | |
---|
87 | exit 0; |
---|