Thread View: news.software.nntp
1 messages
1 total messages
Started by Mike Williams
Tue, 04 Feb 1997 00:00
perl module for wildmat(3)-style matching
Author: Mike Williams
Date: Tue, 04 Feb 1997 00:00
Date: Tue, 04 Feb 1997 00:00
478 lines
15338 bytes
15338 bytes
In the course of some news admin, I've put together a module to ease
processing of newsgroup patterns, ala INN's wildmat(3) routine.
There are also a couple of demo programs which make use of Wildmat.pm.
One, "sync-active", is an script to synchronise a local INN active file
against a remote server, given a list of newsgroup patterns.
Feedback is welcome ...
--- cut here --------------------------------------------------------------
#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.1).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 173 -rwxr-xr-x News-Wildmat-1.01/Makefile.PL
# 2687 -rw-r--r-- News-Wildmat-1.01/Wildmat.pm
# 603 -rwxr-xr-x News-Wildmat-1.01/wildgrep
# 4508 -rwxr-xr-x News-Wildmat-1.01/sync-active
# 424 -rw-r--r-- News-Wildmat-1.01/README
# 60 -rw-r--r-- News-Wildmat-1.01/MANIFEST
#
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
shar_touch=touch
else
shar_touch=:
echo
echo 'WARNING: not restoring timestamps. Consider getting and'
echo "installing GNU \`touch', distributed in GNU File Utilities..."
echo
fi
rm -f 1231235999 $$.touch
#
# ============= News-Wildmat-1.01/Makefile.PL ==============
if test ! -d 'News-Wildmat-1.01'; then
echo 'x - creating directory News-Wildmat-1.01'
mkdir 'News-Wildmat-1.01'
fi
if test -f 'News-Wildmat-1.01/Makefile.PL' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/Makefile.PL (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/Makefile.PL (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/Makefile.PL' &&
#!/usr/local/bin/perl
use ExtUtils::MakeMaker;
WriteMakefile(
X NAME => "News::Wildmat",
X VERSION_FROM => "Wildmat.pm",
X dist => {
X COMPRESS => "gzip -9",
X },
);
SHAR_EOF
$shar_touch -am 0204151397 'News-Wildmat-1.01/Makefile.PL' &&
chmod 0755 'News-Wildmat-1.01/Makefile.PL' ||
echo 'restore of News-Wildmat-1.01/Makefile.PL failed'
shar_count="`wc -c < 'News-Wildmat-1.01/Makefile.PL'`"
test 173 -eq "$shar_count" ||
echo "News-Wildmat-1.01/Makefile.PL: original size 173, current size $shar_count"
fi
# ============= News-Wildmat-1.01/Wildmat.pm ==============
if test -f 'News-Wildmat-1.01/Wildmat.pm' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/Wildmat.pm (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/Wildmat.pm (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/Wildmat.pm' &&
# Copyright (c) 1996 Mike Williams. All rights reserved.
# This module is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
X
package News::Wildmat;
X
$VERSION = 1.01;
X
require 5.002;
X
=head1 NAME
X
News::Wildmat - match newsgroup patterns
X
=head1 SYNOPSIS
X
X use News::Wildmat;
X
=head1 DESCRIPTION
X
This module implements an object that encapsulates a set of Usenet
newsgroup patterns, allowing a newgroup to be tested for membership.
X
=head2 Creating Wildmat objects
X
Initialisation arguments should reference list of Patterns to match
against.
X
X @wanted = ('comp.*', '!comp.sys.ibm.pc.*');
X $wanted = new News::Wildmat( Patterns => \@wanted );
X
Alternatively, you may specify a list of Files from which to read patterns.
Comments (after a "#") are discarded. Patterns should be separated by
whitespace or commas.
X
X $wanted = new News::Wildmat
X Files => ['/var/news/lib/newsgroups.wanted'];
X
=cut
X
use Carp;
X
sub new
{
X my $class = shift;
X my $self = {};
X bless $self, $class;
X $self->_init(@_);
X return $self;
}
X
sub _init
{
X my $self = shift;
X
X my %params = @_;
X
X # Get patterns from parameters
X @{$self->{Patterns}} = @{$params{Patterns}};
X
X # Additional patterns from files
X $self->_addFilePatterns( @{$params{Files}} );
X
X # Generate anonymous subroutine
X my $matchsub;
X my $src = <<EOF;
\$matchsub = sub {
X local (\$_) = \@_;
X study \$_;
X my (\$matched) = 0;
EOF
X
X for (@{$self->{Patterns}}) {
X my $matchval = 1;
X $matchval = 0 if s/^!//;
X
X # Convert wildmat pattern to regexp
X s/([.+])/\\\1/g; # Quote special characters
X s/\*/.*/g; # Convert asterixs
X
X # Append line to subroutine
X $src .= " (\$matched = $matchval) if /^$_\$/;\n";
X
X }
X
X $src .= " return \$matched;\n};\n";
X
X # Save source of subroutine
X $self->{Src} = $src;
X
X # Evaluate a save compiled version
X eval $src;
X $self->{Match} = $matchsub;
}
X
# Read a pattern file, returning a list of newsgroup patterns
sub _addFilePatterns
{
X my $self = shift;
X
X my $file;
X for $file (@_) {
X open( FILE, $file ) || croak "can't open $file: $!\n";
X
X my @patterns;
X local ($_);
X while (<FILE>) {
X chop;
X s/\#.*\n//; # Remove comments
X s/,/ /; # Replace commas
X # Add patterns
X push( @{$self->{Patterns}}, split( " " ));
X }
X close(FILE);
X }
}
X
=head2 Matching
X
Once a Wildmat object has been created, you can use it's "match" method to
test a newsgroup for membership.
X
X while (<STDIN>) {
X print if $wanted->match($_);
X }
X
=cut
X
sub match
{
X my $self = shift;
X my $matchsub = $self->{Match};
X eval { &$matchsub(@_) };
}
X
1;
SHAR_EOF
$shar_touch -am 0204154697 'News-Wildmat-1.01/Wildmat.pm' &&
chmod 0644 'News-Wildmat-1.01/Wildmat.pm' ||
echo 'restore of News-Wildmat-1.01/Wildmat.pm failed'
shar_count="`wc -c < 'News-Wildmat-1.01/Wildmat.pm'`"
test 2687 -eq "$shar_count" ||
echo "News-Wildmat-1.01/Wildmat.pm: original size 2687, current size $shar_count"
fi
# ============= News-Wildmat-1.01/wildgrep ==============
if test -f 'News-Wildmat-1.01/wildgrep' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/wildgrep (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/wildgrep (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/wildgrep' &&
#!/share/bin/perl
## ========================================================================
## wildgrep -- Grep for newsgroup patterns
## Author : Mike Williams <mikew@pemail.net>
## ========================================================================
X
$usage = <<EOF;
usage: $0 [<pattern>,...] [--file=<pattern_file>,...]
EOF
X
# use lib '/var/news/local/perl';
use Getopt::Long;
use News::Wildmat;
X
&GetOptions (\%opt, "file=s@" );
X
$wanted = new News::Wildmat
X Patterns => \@ARGV,
X Files => \@{$opt{file}};
X
while (<STDIN>) {
X chop;
X print "$_\n" if $wanted->match($_);
}
SHAR_EOF
$shar_touch -am 0204154097 'News-Wildmat-1.01/wildgrep' &&
chmod 0755 'News-Wildmat-1.01/wildgrep' ||
echo 'restore of News-Wildmat-1.01/wildgrep failed'
shar_count="`wc -c < 'News-Wildmat-1.01/wildgrep'`"
test 603 -eq "$shar_count" ||
echo "News-Wildmat-1.01/wildgrep: original size 603, current size $shar_count"
fi
# ============= News-Wildmat-1.01/sync-active ==============
if test -f 'News-Wildmat-1.01/sync-active' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/sync-active (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/sync-active (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/sync-active' &&
#!/usr/local/bin/perl
## ========================================================================
## sync-active -- Synchronise active file
## Author : Mike Williams <mikew@pemail.net>
## ========================================================================
X
# This script makes the easy to update the list of active newsgroups on an
# INN news-server, based on another server's list.
#
# This is basically just an update of Matt Midboe's <matt@garply.com>
# "sync" script to use Wildmat.pm.
X
# Input:
# ------
# - remote active file(s)
# - local active file
# - list of newsgroup patterns we want from the remote site(s)
# - list of local newsgroup patterns (ie. groups we don't want from the
# remote site(s), but still want to keep)
#
# Output:
# -------
# - list of ctlinnd newgroup/rmgroup commands, suitable for feeding into
# /bin/sh
X
#=== I'm a user ===========================================================
X
use Carp;
use Getopt::Long;
X
# use lib '/var/news/local/perl';
use News::Wildmat;
use News::NNTPClient;
X
#=== Parse command-line ===================================================
X
$usage = <<EOF;
usage: $0 \
X --remote=<src> ... --want=<file> ... \
X [--local=<src>] [--keep=<file> ...]
X
X options:
X
X --remote=<src> ... Specifies a file or remote server from which to
X fetch a list of candidate newsgroups. <src> may be
X either a filename, or "\@<host>", indicating that
X the list should be fetched from an NNTP
X server. This option may be given more than once, to
X specify a list of files/servers. There's no
X default: this option is mandatory.
X
X --local=<src> Specifies where to get the list of active newsgroups.
X The default is "\@localhost".
X
X --want=<file> ... Specifies a file containing a list of newsgroup
X patterns, which determine what groups we want.
X
X --keep=<file> ... Specifies a file containing a list of newsgroup
X patterns, which determine what groups we want to
X keep, even if they a not supplied by the remote
X servers. Use this to specify local groups.
X
EOF
X
&GetOptions (\%opt, "remote=s@", "local=s", "want=s@", "keep=s@" );
X
unless ($opt{'want'}) {
X warn "please use --want to specify what newsgroups you want\n";
X die $usage;
}
X
unless ($opt{'remote'}) {
X warn "please use --remote to specify a list of remote newsgroups\n";
X die $usage;
}
X
#=== Generate matchers for want and keep files ============================
X
$want = new News::Wildmat
X Files => \@{$opt{want}};
$keep = new News::Wildmat
X Patterns => [ 'control', 'junk' ],
X Files => \@{$opt{keep}};
X
#=== Subroutine to load an active file ====================================
X
sub readActive
{
X my ($active) = @_;
X
X if ($active =~ s/^@//) {
X my $nntp = new News::NNTPClient( $active );
X $nntp->ok || croak "$0: error fetching active file from $active\n";
X return $nntp->list( 'active' );
X } else {
X open( ACTIVE, $active ) || croak "$0: can't read $active: $!\n";
X my @lines = <ACTIVE>;
X close( ACTIVE );
X return @lines;
X }
X
}
X
#=== Read local active file ==============================================
X
$active = ($opt{local} || '@localhost');
X
for (readActive($active)) {
X chomp;
X ($group, $flag) = (split)[0,3];
X $local{$group} = $flag;
}
X
#=== Read remote active file(s) ===========================================
X
for $remote (@{$opt{'remote'}}) {
X
X for (readActive($remote)) {
X chomp;
X ($group, $flag) = (split)[0,3];
X $remote{$group} = $flag;
X }
X
}
X
#=== Emit a ctlinnd command ===============================================
X
sub ctlinnd
{
X local ($command, $comment) = @_;
X if ($comment) {
X printf( "ctlinnd %-54s \# %s\n", $command, $comment );
X } else {
X print "ctlinnd ", $command, "\n";
X }
}
X
#=== Send newgroups for new/altered groups ================================
X
foreach $group (keys %remote) {
X next unless $want->match($group);
X if (!defined($local{$group})) {
X ctlinnd( "newgroup $group $remote{$group}" );
X } elsif ($local{$group} ne $remote{$group}) {
X ctlinnd( "changegroup $group $remote{$group}" );
X }
}
X
#=== Send rmgroups for bogus/unwanted groups ==============================
X
foreach $group (keys %local) {
X next if $keep->match($group);
X if (!$want->match($group)) {
X ctlinnd( "rmgroup $group", 'unwanted' );
X } elsif (!defined($remote{$group})) {
X ctlinnd( "rmgroup $group", 'bogus' );
X }
}
X
##=== END of sync-active ==================================================
SHAR_EOF
$shar_touch -am 0204154397 'News-Wildmat-1.01/sync-active' &&
chmod 0755 'News-Wildmat-1.01/sync-active' ||
echo 'restore of News-Wildmat-1.01/sync-active failed'
shar_count="`wc -c < 'News-Wildmat-1.01/sync-active'`"
test 4508 -eq "$shar_count" ||
echo "News-Wildmat-1.01/sync-active: original size 4508, current size $shar_count"
fi
# ============= News-Wildmat-1.01/README ==============
if test -f 'News-Wildmat-1.01/README' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/README (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/README (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/README' &&
News::Wildmat.pm makes it easier to test a bunch of newsgroup names against
a bunch of newsgroup patterns (ala INN's wildmat(3) function).
X
As well as the base module, I've included a couple of demo programs:
X
wildgrep A simple utility to filter a list of newsgroups against a
X set of patterns.
X
sync-active A update of Matt Midboe's <matt@garply.com> "sync" script
X to use Wildmat.pm.
X
Mike Williams <mikew@pemail.net>
SHAR_EOF
$shar_touch -am 0204154597 'News-Wildmat-1.01/README' &&
chmod 0644 'News-Wildmat-1.01/README' ||
echo 'restore of News-Wildmat-1.01/README failed'
shar_count="`wc -c < 'News-Wildmat-1.01/README'`"
test 424 -eq "$shar_count" ||
echo "News-Wildmat-1.01/README: original size 424, current size $shar_count"
fi
# ============= News-Wildmat-1.01/MANIFEST ==============
if test -f 'News-Wildmat-1.01/MANIFEST' && test X"$1" != X"-c"; then
echo 'x - skipping News-Wildmat-1.01/MANIFEST (file already exists)'
else
echo 'x - extracting News-Wildmat-1.01/MANIFEST (text)'
sed 's/^X//' << 'SHAR_EOF' > 'News-Wildmat-1.01/MANIFEST' &&
MANIFEST
README
Makefile.PL
Wildmat.pm
sync-active
wildgrep
SHAR_EOF
$shar_touch -am 0204155597 'News-Wildmat-1.01/MANIFEST' &&
chmod 0644 'News-Wildmat-1.01/MANIFEST' ||
echo 'restore of News-Wildmat-1.01/MANIFEST failed'
shar_count="`wc -c < 'News-Wildmat-1.01/MANIFEST'`"
test 60 -eq "$shar_count" ||
echo "News-Wildmat-1.01/MANIFEST: original size 60, current size $shar_count"
fi
exit 0
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads