Thread View: news.software.nn
1 messages
1 total messages
Started by Erik E. Rantapaa
Fri, 06 Mar 1992 00:09
nnbiff - report interesting news
Author: Erik E. Rantapaa
Date: Fri, 06 Mar 1992 00:09
Date: Fri, 06 Mar 1992 00:09
774 lines
21141 bytes
21141 bytes
Here is a set of Perl routines to help search the data base files
used by `nn'. You can use them to write programs that periodically
search for `interesting' news.
To install, unpack the shar archive and install as directed by the
README file. You may have to refer the source to `nn', esp. `config.h'
for some of the settings.
As a test, the sample program `nnbiff' is set up to match on the
subject of this article.
I'd be interested in ideas, bugs and suggestions for improvement.
Please pass them along to rantapaa@math.umn.edu.
--
Erik Rantapaa rantapaa@math.umn.edu
---- Cut Here and feed the following to sh ----
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 03/06/1992 00:08 UTC by rantapaa@s6.math.umn.edu
# Source directory /home/math-s6/grad/rantapaa/nnbiff
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 3072 -rw-rw-r-- README
# 1929 -rw-rw-r-- nn.ph
# 10603 -rw-rw-r-- nn.pl
# 659 -rwxrwxr-x nnbiff
#
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
Introduction
============
X
`nnbiff' is a set of Perl routines and macro definitions to help you
write programs to check for `interesting' news.
X
`nnbiff' requires that you have a nn database on your system.
X
Installation
============
X
X 1) Edit nn.ph to reflect how nn is configured.
X
X `$NETWORK_DATABASE', `$DB_DIRECTORY', `$DB_DATA_DIRECTORY' and
X `$DB_LONG_NAMES' can be taken directly from nn's `config.h'.
X
X You'll have to set $NATIVE_HEADER_{FMT,SIZE} if you are not using
X a network database. The format is used to read a `data_header'
X structure. Look in `db.h' for this declaration.
X
X If your DATA directory contains subdirectories instead of the
X the actual data files, set `$DB_HAS_SUBDIRS'. (This is not
X the normal case, but some implementations index the data files
X by group_number/100, so that group 2345 is in .../DATA/23/2345.d,
X etc.)
X
X 2) Copy `nn.pl' and `nn.ph' to your system's perl library (usually
X /usr/local/lib/perl) or to your own private perl library.
X
X You can tell perl where your own private library is by setting your
X PERLLIB environment variable. See the Perl manual for more info.
X
X 3) Edit `nnbiff' to look for stuff that interests you.
X
Examples
========
X
The sample program `nnbiff' takes the following command line arguments:
X
X nnbiff [-N] [ labels ...]
X
If present, the -N option means not to read the .nnbiffrc file.
Labels, if present, inform nnbiff which queries should be carried
out. No labels means to perform all queries.
X
The general format of a nnbiff query is
X
X QUERY(label)
X GROUP('news.group')
X LOOP
X ... patterns ...
X ENDLOOP
X ENDQUERY
X
`label' is simply an identifier made of letters and digits to identify
the query. It should be unquoted. The `GROUP(...)' declares which
news group is to be searched.
X
You can use the `SENDER(...)' and `SUBJECT(...)' macros in the `...
patterns ...' area to select articles whose sender/subject match given
regular expressions. The patterns are concatenated by logical ors, so
that
X
X SUBJECT(/nnbiff/)
X SENDER(/Erik E. Rantapaa/)
X
will match articles whose subject contains 'nnbiff' OR whose sender
matches /Erik E. Rantapaa/.
X
.nnbiffrc
=========
X
nnbiff keeps track of the status of each query in your `.nnbiffrc' file.
X
The .nnbiffrc file is formatted as follows:
X
X queryid:lastcheck:lastart:reported
X
where
X
X queryid = label identifying the query
X lastcheck = last time this query was checked
X lastart = highest article number checked so far
X reported = list of articles already reported for this query
X
`queryid' may be any combination of letters and digits.
X
nnbiff will short-circut scanning nn's database if the relevent
files haven't changed since `lastcheck'.
X
To do
=====
X
X * Set exit status based on query results.
X * Add more command line options.
X * Make a real package out of nn.pl
X * Add a convenient way of checking for any unread news
X in a news group.
X * ...
X
Please send any suggestions/improvements/bug reports
to rantapaa@math.umn.edu.
SHAR_EOF
chmod 0664 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 3072 -eq "$Wc_c" ||
echo 'README: original size 3072, current size' "$Wc_c"
fi
# ============= nn.ph ==============
if test -f 'nn.ph' -a X"$1" != X"-c"; then
echo 'x - skipping nn.ph (File already exists)'
else
echo 'x - extracting nn.ph (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'nn.ph' &&
#define YES 1
#define NO 0
$NETWORK_DATABASE = NO;
$NATIVE_HEADER_FMT = "lLllssCCCC";
$NATIVE_HEADER_SIZE = 24;
$DB_DIRECTORY = '/usr/local/lib/nn/db';
$DB_DATA_DIRECTORY = '/usr/local/lib/nn/db/DATA';
$DB_LONG_NAMES = NO;
$DB_HAS_SUBDIRS = NO;
$RCFILE = '.nnbiffrc';
X
#define QUERY(x) \
X _query_x: { \
X last unless ($_all_labels || $_labels{'x'}); \
X $_time = time; \
X undef $_maxart; \
X $_query = 'x';
X
#define GROUP(g) \
X $_group = g;
X
#define LOOP \
X ($_lastcheck,$_reported,$_lastart) = &RcStatus($_query);\
X { \
X last unless &ModifiedAfter($_group,$_lastcheck); \
X &InitCollect; \
X &OpenDataFile($_group); \
X while (&ReadDataEntry) {
X
#define SUBJECT(re) \
X &Collect, next if ($dh_subject =~ re);
X
#define SENDER(re) \
X &Collect, next if ($dh_sender =~ re);
X
#define COLLECT \
X &Collect, next
X
#define ENDLOOP \
X } \
X &CloseDataFile;
X
#define ENDQUERY \
X } \
X &UpdateQuery if $_updaterc; \
X }
X
require 'nn.pl';
$_checkrc = 1;
$_updaterc = 1;
SHAR_EOF
chmod 0664 nn.ph ||
echo 'restore of nn.ph failed'
Wc_c="`wc -c < 'nn.ph'`"
test 1929 -eq "$Wc_c" ||
echo 'nn.ph: original size 1929, current size' "$Wc_c"
fi
# ============= nn.pl ==============
if test -f 'nn.pl' -a X"$1" != X"-c"; then
echo 'x - skipping nn.pl (File already exists)'
else
echo 'x - extracting nn.pl (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'nn.pl' &&
#
# nn.pl -- Routines to read the nn news database.
#
X
$MASTER_FIELDS = 5;
$ARTICLE_FIELDS = 10;
$GROUP_FIELDS = 9;
$DB_LOCK_MESSAGE = 80;
X
if ($NETWORK_DATABASE) {
X $HEADER_FMT = "N$ARTICLE_FIELDS";
X $HEADER_SIZE = 4*$ARTICLE_FIELDS;
} else {
X $HEADER_FMT = $NATIVE_HEADER_FMT;
X $HEADER_SIZE = $NATIVE_HEADER_SIZE;
}
X
sub InitCollect {
X undef @_rclows;
X undef @_rchighs;
}
X
sub Collect {
X if ($_checkrc) {
X if (!defined(@_rclows)) {
X &SplitRanges($_reported,*_rclows,*_rchighs);
X }
X return if &InList($dh_number, *_rclows, *_rchighs);
X }
X push(@Articles,
X join("\n",
X ($_query,
X $_group,
X $dh_number,
X $dh_sender,
X $dh_subject,
X $dh_date,
X $dh_lines,
X $dh_re,
X $dh_replies)));
}
X
sub Report {
X local($cgroup, $out);
X local($query,$group,$number,$sender,$subject,$date,$lines,$re,$replies);
X $cgroup = '';
X return unless (@Articles);
X for $x (@Articles) {
X ($query,
X $group,
X $number,
X $sender,
X $subject,
X $date,
X $lines,
X $re,
X $replies)
X = split(/\n/, $x);
X if ($group ne $cgroup) {
X $cgroup = $group;
X print "Interesting news in $cgroup:\n";
X }
X $subject = 'Re: ' . $subject if $re;
X $subject = substr($subject,0,60);
X $sender = substr($sender,0,16);
X printf " %-16s %s\n", $sender, $subject;
X if ($_updaterc) {
X $ReportedModified{$query} .= ",$number";
X }
X }
}
X
# The user's rc file contains the following information about
# each query:
#
# query:time:art:reported
#
# query - the query id
# time - last time this query was check
# art - highest article checked
# reported - list of articles reported for this query
#
# The above information is read into the following variables:
#
# %LastCheck
# %LastArticle
# %Reported
X
sub InitRcVars {
X undef %LastCheck;
X undef %LastArticle;
X undef %Reported;
X undef %ReportedModified;
X $_RcModified = 0;
}
X
sub RcStatus {
X local($query) = @_;
X local($time,$reported,$article);
X $time = $LastCheck{$query};
X $reported = $Reported{$query};
X $article = $LastArticle{$query};
X
X $time = 0 unless defined($time);
X $article = 0 unless defined($article);
X return ($time,$reported,$article);
}
X
sub UpdateQuery {
X $LastCheck{$_query} = $_time;
X $LastArticle{$_query} = $_maxart if defined($_maxart);
X $_RcModified = 1;
}
X
X
sub UpdateReported {
X local($query, $mods);
X return if !defined(%ReportedModified);
X while (($query, $mods) = each %ReportedModified) {
X $new = join(',', &SortRanges($Reported{$query} . $mods));
X $Reported{$query} = $new;
X }
X $_RcModified = 1;
X undef %ReportedModified;
}
X
sub ReadRcFile {
X local($now) = time;
X local($query, $time, $article, $reported);
X local($rcfile) = "$ENV{'HOME'}/$RCFILE";
X
X return unless open(RCFILE, "<$rcfile");
X while (<RCFILE>) {
X chop;
X ($query, $time, $article, $reported) = split(/:\s*/,$_,4);
X if ($time > $now || $time < 0) {
X print STDERR "Bad time ($time) found in $RCFILE\n";
X $time = $now;
X $_RcModified = 1;
X }
X $LastCheck{$query} = $time;
X $LastArticle{$query} = $article;
X $Reported{$query} = $reported;
X }
X close(RCFILE);
}
X
sub WriteRcFile {
X &UpdateReported;
X return if !$_RcModified;
X if (!open(RCFILE, ">$ENV{'HOME'}/$RCFILE")) {
X print STDERR "Cannot open ~/$RCFILE: $!\n";
X return;
X }
X for $query (sort keys %LastCheck) {
X print RCFILE
X join(':',
X $query,
X $LastCheck{$query},
X $LastArticle{$query},
X $Reported{$query}
X ), "\n";
X }
X close(RCFILE);
}
X
#
# Range stuff...
#
X
#
# SortRanges
#
X
sub SortRanges {
X local($ranges) = @_;
X local(@lows, @highs, @index, @sindex, @nranges, $i, $l, $h);
X &SplitRanges($ranges,*lows,*highs);
X return @nranges if ($#lows < $[);
X @index = ($[..$#lows);
X @sindex = sort RangeSort @index;
X $i = shift(@sindex);
X $l = $lows[$i];
X $h = $highs[$i];
X for $i (@sindex) {
X if ($h >= $lows[$i]-1) {
X $h = $highs[$i] if ($highs[$i] > $h);
X next;
X }
X if ($h > $l) {
X push(@nranges, "$l-$h");
X } else {
X push(@nranges, "$l");
X }
X $l = $lows[$i];
X $h = $highs[$i];
X }
X if ($h > $l) {
X push(@nranges, "$l-$h");
X } else {
X push(@nranges, "$l");
X }
X return @nranges;
}
X
sub RangeSort {
X return $lows[$a] <=> $lows[$b];
}
X
#
# SplitRanges
#
sub SplitRanges {
X local($ranges,*lows,*highs) = @_;
X local(@ranges) = split(/\s*,\s*/,$ranges);
X local($r, $l, $h);
X undef @lows;
X undef @highs;
X for $r (@ranges) {
X next if $r =~ /^\s*$/;
X if ($r =~ /^[0-9]+$/) {
X push(@lows, $r);
X push(@highs, $r);
X } else {
X ($l, $h) = split(/-/,$r,2);
X push(@lows, $l);
X push(@highs, $h);
X }
X }
}
X
#
# InList
#
sub InList {
X local($n,*lows,*highs) = @_;
X local($length) = $#lows;
X local($i);
X for ($i = $[; $i <= $length; $i++) {
X return 1 if ($lows[$i] <= $n && $n <= $highs[$i]);
X }
X 0;
}
X
#
# ReadGroups
#
sub ReadGroups {
X local($id) = 0;
X open(G, "<$DB_DIRECTORY/GROUPS") || die "Cannot open GROUPS file $!";
X while (<G>) {
X next if /^#/;
X next if /^\s*$/;
X ($name) = split(/\s+/);
X $GroupId{$name} = $id;
X $GroupName[$id] = $name;
X $id++;
X }
X close(G);
}
X
#
# GroupId - return id associated with a group.
#
sub GroupId {
X local($g) = @_;
X if ($g =~ /[0-9]+/) {
X $g;
X } else {
X $GroupId{$g};
X }
}
X
#
# GroupName - return the group name associated with an id.
#
sub GroupName {
X local($g) = @_;
X if ($g =~ /[0-9]+/) {
X $GroupName[$g];
X } else {
X $g;
X }
}
X
#
# DbFileName - return the name of a nn database name.
#
sub DbFileName {
X local($group, $type) = @_;
X local($id, $mod);
X local($isnumeric) = ($group =~ /^[0-9]+$/);
X if ($DB_LONG_NAMES) {
X $id = ($isnumeric ? $GroupName[$group] : $group);
X "$DB_DATA_DIRECTORY/$id.$type";
X } else {
X $id = ($isnumeric ? $group : $GroupId{$group});
X if ($DB_HAS_SUBDIRS) {
X $mod = $id / 100;
X "$DB_DATA_DIRECTORY/$mod/$id.$type";
X } else {
X "$DB_DATA_DIRECTORY/$id.$type";
X }
X }
}
X
#
# Read an entire index file into an array.
#
sub ReadIndexFile {
X local($group, *index) = @_;
X local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
X $ctime,$blksize,$blocks);
X local($bytes, $buffer, @index);
X open(INDEX, "<" . &DbFileName($group, 'x'))
X || die "Cannot open index: $!";
X ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
X $ctime,$blksize,$blocks) = stat(INDEX);
X die "Bad file size for index file" unless $size % 4 == 0;
X if (($bytes = read(INDEX,$buffer,$size)) != $size) {
X die "Bad read from index file\n";
X }
X @index = unpack("N*", $buffer);
X close(INDEX);
X return @index;
}
X
#
# ModifiedAfter
#
X
sub ModifiedAfter {
X local($group,$time) = @_;
X local($file);
X local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
X $atime,$mtime,$ctime,$blksize,$blocks);
X
X $file = &DbFileName($group,'d');
X if (-r $file) {
X ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
X $atime,$mtime,$ctime,$blksize,$blocks)
X = stat($file);
X if ($mtime >= $time) {
X return 1;
X }
X }
X return 0;
}
X
#
# OpenDataFile
#
X
sub OpenDataFile {
X local($group) = &GroupId(@_);
X die "Unknown group $_[0]" unless defined($group);
X $_maxart = -1;
X open(DATA, '<' . &DbFileName($group, 'd'));
}
X
sub CloseDataFile {
X close(DATA);
}
X
# ReadDataEntry - read infomation about an article.
#
# ReadDataEntry($pos) - read the data entry at position $pos in
# the current data file.
#
# Group data file must have been opened with OpenDataFile.
#
# Returns the following variables:
#
# $dh_number - article number
# $dh_date - date article was posted
# $dh_hpos - absolute offset for first byte of header
# $dh_lpos - absolute offset for last byte of article
# $dh_fpos - relative offset for first byte of article text
# $dh_lines - number of lines
# $dh_replies - ???
# $dh_xposts - number of other groups article has ben cross posted to
# $dh_subjlen - length of subject
# $dh_sendlen - length of sender
# $dh_subject - subject
# $dh_sender - sender
# $dh_re - TRUE if this is a 'Re:' ???
# $dh_type - 0 = normal, 1 = digest, 2 = sub-digest
X
sub ReadDataEntry {
X local($buf);
X
X $dh_offset = tell(DATA);
X
X # Read the data header.
X
X return 0 if (read(DATA, $buf, $HEADER_SIZE) != $HEADER_SIZE);
X
X ($dh_number, $dh_date, $dh_hpos, $dh_lpos, $dh_fpos, $dh_lines,
X $dh_replies, $dh_xposts, $dh_subjlen, $dh_sendlen)
X = unpack($HEADER_FMT,$buf);
X
X undef @dh_groups;
X if ($dh_xposts > 0) {
X if (read(DATA, $buf, $dh_xposts*2) != $dh_xposts*2) {
X die "Bad read on cross posts";
X }
X @dh_groups = unpack("s*", $buf);
X }
X undef $dh_sender;
X if ($dh_sendlen > 0) {
X if (read(DATA, $dh_sender, $dh_sendlen) != $dh_sendlen) {
X die "Bad read on sender";
X }
X }
X undef $dh_subject;
X
X if ($dh_subjlen > 0) {
X if (read(DATA, $dh_subject, $dh_subjlen) != $dh_subjlen) {
X die "Bad read on subject";
X }
X }
X $dh_re = $dh_replies & 128;
X $dh_replies &= 127;
X
X if ($dh_number > 0) {
X $dh_type = 0;
X } elsif ($dh_number == 0) {
X $dh_type = 2;
X $dh_number = $current_digest;
X } elsif ($dh_number < 0) {
X $dh_type = 1;
X $current_digest = $dh_number = -$dh_number;
X }
X
X $_maxart = $dh_number if ($dh_number > $_maxart);
X return 1;
}
X
#
# ReadGroup - Read a group entry from the MASTER file.
#
X
sub ReadGroup {
X local($gid) = @_;
X local($buf);
X local($bytes) = $GROUP_FIELDS*4*$gid;
X
X open(MASTER, "<$DB_DIRECTORY/MASTER") || die" Cannot open MASTER: $!";
X seek(MASTER, $MASTER_FIELDS*4+$DB_LOCK_MESSAGE,0);
X if (read(MASTER, $buf, $bytes) != $bytes) {
X die "Bad MASTER read: $!";
X }
X ($gh_first_article, $gh_last_article, $gh_index_eof, $gh_data_eof,
X $gh_name_length, $gh_master_flag, $gh_first_a_article,
X $gh_last_a_article, $gh_creation_time)
X = unpack("N$GROUP_FIELDS", $buf);
X close(MASTER);
}
X
sub DumpGroup {
X local($group) = @_;
X local($x);
X
X &OpenDataFile($group) || die "Cannot open data file for $group: $!";
X while(&ReadDataEntry) {
X &DumpEntry;
X print "\n";
X }
X &CloseDataFile;
}
X
#
# Print an entry.
#
X
sub DumpEntry {
X print "offset = $dh_offset, article \# = $dh_number\n";
X if ($dh_xposts > 0) {
X print "xpost($dh_xposts): ", join(' ', @dh_groups), "\n";
X }
X print "ts=$dh_date hp=$dh_hpos, fp=+$dh_fpos, lp=$dh_lpos, ref=$dh_replies";
X print "+Re" if $dh_re;
X print ", lines=$dh_lines\n";
X print "Sender($dh_sendlen): $dh_sender\n";
X print "Subj($dh_subjlen): $dh_subject\n";
}
X
1;
SHAR_EOF
chmod 0664 nn.pl ||
echo 'restore of nn.pl failed'
Wc_c="`wc -c < 'nn.pl'`"
test 10603 -eq "$Wc_c" ||
echo 'nn.pl: original size 10603, current size' "$Wc_c"
fi
# ============= nnbiff ==============
if test -f 'nnbiff' -a X"$1" != X"-c"; then
echo 'x - skipping nnbiff (File already exists)'
else
echo 'x - extracting nnbiff (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'nnbiff' &&
#!/usr/local/bin/perl -P
X
#include "/home/grad/rantapaa/lib/perl/nn.ph"
X
undef %_labels;
$_all_labels = 1;
while (@ARGV) {
X $arg = shift(@ARGV);
X if ($arg eq '-N') {
X $_checkrc = $_updaterc = 0;
X next;
X }
X $_labels{$arg} = 1;
X $_all_labels = 0;
}
&ReadGroups;
&ReadRcFile if $_checkrc;
X
QUERY(1)
GROUP('news.software.nn')
LOOP
X SUBJECT(/Selecting article after last in/)
X SUBJECT(/nnbiff - report interesting news/)
X SENDER(/Erik E. Rantapaa/)
ENDLOOP
ENDQUERY
X
QUERY(p)
GROUP('comp.lang.perl')
LOOP
X SENDER(/Erik E. Rantapaa/)
X SUBJECT(/nnbiff - report interesting news/)
ENDLOOP
ENDQUERY
X
#
# Report findings.
#
X
&Report;
&WriteRcFile;
SHAR_EOF
chmod 0775 nnbiff ||
echo 'restore of nnbiff failed'
Wc_c="`wc -c < 'nnbiff'`"
test 659 -eq "$Wc_c" ||
echo 'nnbiff: original size 659, current size' "$Wc_c"
fi
exit 0
--
--
Erik Rantapaa rantapaa@math.umn.edu
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