Article View: news.software.nntp
Article #1490Perl version of nntpxfer - a passive news transfer client
From: Christian Seyb
Date: Thu, 04 Jun 1992 05:06
Date: Thu, 04 Jun 1992 05:06
383 lines
13323 bytes
13323 bytes
This is a "passive" news transmission client. It queries other servers
for news with NEWNEWS and gets it via the ARTICLE command. The server has
to be setup to allow anonymous nntp transfers. Because the news is requested
by the nntpxfer client, there is no news maintanance required at the server.
The perl version of nntpxfer creates newsbatches and doesn't call the news
subsystem for every article. It is therefor a lot faster.
Due to the way of handling duplicate articles, this version of nntpxfer only
works for CNEWS.
The mechanism to check if a process is still active (kill -0 $pid) to my
knowledge is only valid for SYSV Unixes.
regards Christian - cs@gold.sub.org
---- 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 06/04/1992 05:03 UTC by root@gold
# Source directory /usr/pdsoft/nntp/xferpl
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 264 -rw-r--r-- Makefile
# 1029 -rw-r--r-- README
# 5643 -rwxr-xr-x nntpxfer.pl
# 1352 -rw-r--r-- chat2.svr4
# 43 -rw-r--r-- nntp.foobar
#
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
#
# Makefile fuer nntpxfer.pl
#
BINDIR = /usr/local/bin
X
install:
X cp nntpxfer.pl $(BINDIR)
X
tar:
X ntar -cvzf nntpxfer.taz Makefile README nntpxfer.pl \
X chat2.svr4 nntp.foobar
X
shar:
X shar -c -onntpxfer Makefile README nntpxfer.pl\
X chat2.svr4 nntp.foobar
SHAR_EOF
chmod 0644 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 264 -eq "$Wc_c" ||
echo 'Makefile: original size 264, current size' "$Wc_c"
fi
# ============= 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' &&
nntpxfer.pl
-----------
X
This is a "passive" news transmission client. It queries other servers
for news with NEWNEWS and gets it via the ARTICLE command. The server has
to be setup to allow anonymous nntp transfers. Because the news is requested
by the nntpxfer client, there is no news maintanance required at the server.
X
The perl version of nntpxfer creates newsbatches and doesn't call the news
subsystem for every article. It is therefor a lot faster.
X
Due to the way of handling duplicate articles, this version of nntpxfer only
works for CNEWS.
X
The mechanism to check if a process is still active (kill -0 $pid) to my
knowledge is only valid for SYSV Unixes.
X
The NNTP protocol is described in RFC977.
X
FILES
-----
X
README - This File ...
chat2.svr4 - Patches to get chat2.pl working with Unix V/386 Rel 4.
nntpxfer.pl - The nntpxfer news transfer client written in Perl.
nntp.foobar - Example $spooldir/nntp.$host file for server foobar.
X
Have fun and send your bug reports/changes to me.
X
Christian - cs@gold.sub.org
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 1029 -eq "$Wc_c" ||
echo 'README: original size 1029, current size' "$Wc_c"
fi
# ============= nntpxfer.pl ==============
if test -f 'nntpxfer.pl' -a X"$1" != X"-c"; then
echo 'x - skipping nntpxfer.pl (File already exists)'
else
echo 'x - extracting nntpxfer.pl (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'nntpxfer.pl' &&
#!/usr/local/bin/perl
#
# usage: nntpxfer.pl host [groups YYMMDD HHMMSS [<dist>]]
#
# nntpxfer command in perl. This version of nntpxfer batches the
# incoming news. This makes nntpxfer a lot faster, because relaynews
# is run less frequently.
#
# The configuration for nntpxfer is in the first line of the file
# $spooldir/nntp.$host with the following format:
#
# newsgroups date time [distributions]
# comp.*,alt.*,!alt.sex.* 920501 010000 world,local
#
# Author: cs@gold.sub.org (Christian Seyb)
#
# Requires chat2.pl
X
$batchsize=300000; # Size of batches in bytes
$timeout=300; # nntp timeouts in seconds
$dbzcmd="/usr/lib/newsbin/dbz -m /usr/lib/news/history";
X # Cnews dbz command
$spooldir='/usr/spool/news'; # New spool directory
$incoming="$spooldir/in.coming"; # Cnews in.coming directory
X
# END OF CONFIGURATION SECTION
X
$locktmp="XFLOCK.$$"; # Lock tempfile
$lock="XFLOCK"; # Lock file
$dbztmp="nndbz.$$"; # For article message ID's
$tmpfile="nnxfer.$$"; # Articles go in there
X
require 'chat2.pl';
X
#
# Get the article for a given message ID from the nntp server.
#
sub get_article {
X local($msgid)=@_;
X local($line,$linetmp,$bytecount);
X &chat'print("ARTICLE $msgid\r\n");
X die "Timeout during response from article\n" unless
X $line=&chat'expect($timeout, "^2.*\r\n",1, "^4.*\r\n",2, "(.*)\r\n",3);
X return ('') if($line == 2); # Article is missing
X die "Protocol error during response from article\n" if ($line == 3);
X $bytecount=0;
X $linetmp='';
X while(1) {
X die "Timeout while receiving article\n" unless
X $line=&chat'expect($timeout ,"(.*)\r\n", '"$1\n"');
X last if $line=~/^\.\n/;
X $line=~s/^\.\./\./;
X $bytecount+=length($line);
X $linetmp.=$line;
X }
X $linetmp="#! rnews $bytecount\n" . $linetmp;
}
X
##########################################################################
# #
# S T A R T O F P R O G R A M #
# #
##########################################################################
X
#
# First do some (limited) checking of command arguments.
#
if(@ARGV != 1 && @ARGV != 4 && @ARGV != 5) {
X $usage=1;
}
$nntphost=shift(@ARGV);
if(@ARGV) {
X $omitupdate=1;
X $newsgroups=shift(@ARGV);
X $date=shift(@ARGV);
X $time=shift(@ARGV);
X if((length($date) != 6) || (length($time) != 6)) {
X $usage=1;
X }
}
if(@ARGV) {
X $distributions=shift(@ARGV);
}
if($usage) {
X print "usage: $0 host [groups YYMMDD HHMMSS [dist]]\n";
X exit(1);
}
$nntpfile="$spooldir/nntp.$nntphost"; # NNTP parameter file
chdir($incoming) || die "Can't chdir to $incoming\n";
#
# Make sure, that only one nntpxfer takes place
#
open(LOCKTMP, ">$locktmp") || die "Can't create $locktmp\n";
print LOCKTMP $$; # Write process id to locktmp
close LOCKTMP;
while(link($locktmp,$lock) == 0) {
X open(LOCK, $lock) || die "Can't open $lock\n";
X $pid=<LOCK>;
X close LOCK;
X # The following works on SYSV
X if(kill(-0, $pid) == 0) {
X unlink $lock;
X next;
X }
X unlink $locktmp;
X exit (1);
}
unlink $locktmp;
#
# Connect to the $nntphost
#
&chat'open_port($nntphost,119) || die "Cannot open nntp to $nntphost: $!";
die "missing 2xx during greeting\n" unless &chat'expect($timeout,"^2.*\r\n",1);
#
# Get current date and time
#
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$newdate=sprintf("%02u%02u%02u", $year,$mon+1,$mday);
$newtime=sprintf("%02u%02u%02u", $hour,$min,$sec);
#
# Open $nntpfile and get newsgroups, timestamp and distributions
# from the 1st line, if they where not part of the command line.
#
if(! defined $newsgroups) {
X open(NNTPFILE, $nntpfile) || die "Can't open $nntpfile\n";
X $_=<NNTPFILE>;
X chop;
X ($newsgroups,$date,$time,$distributions)=split(/\s+/,$_);
X close NNTPFILE;
}
#
# Get the message ID's for all new articles since the last connection
# and junk duplicate message id's.
#
if(length($distributions)) {
X &chat'print("NEWNEWS $newsgroups $date $time GMT <$distributions>\r\n");
} else {
X &chat'print("NEWNEWS $newsgroups $date $time GMT\r\n");
}
die "Expected 2xx during newnews" unless &chat'expect($timeout, "^2.*\r\n",1);
open(NNDBZ, ">$dbztmp") || die "Can't open $dbztmp\n";
while(1) {
X die "Timeout while receiving message ID's\n" unless
X $line=&chat'expect($timeout ,"(.*)\r\n", '"$1\n"');
X last if $line =~ /^\.\n/;
X print NNDBZ $line;
}
close (NNDBZ);
open(DBZCMD, "$dbzcmd $dbztmp |") || die "Can't open pipe to $dbzcmd\n";
while(<DBZCMD>) {
X push(@msgids, $_);
}
close (DBZCMD);
unlink $dbztmp;
#
# Retrieve all articles (they include the '#! rnews count' header) and
# collect them in a tempfile. if the batchsize is exceeded, the tempfile
# gets moved to the news system for further processing by newsrun.
#
foreach $id (@msgids) {
X if ( ! -e $tmpfile) {
X open(TMPFILE,">$tmpfile") || die "Can't open $tmpfile\n";
X }
X $article=&get_article($id);
X if(length($article)) {
X print TMPFILE $article;
X }
X if(($len = -s $tmpfile) > $batchsize) {
X close (TMPFILE);
X $batchfile=time . ".t";
X link($tmpfile,$batchfile) || die "Can't link to $batchfile\n";
X unlink($tmpfile);
X }
}
if(-e $tmpfile) {
X close (TMPFILE);
X $batchfile=time . ".t";
X link($tmpfile,$batchfile) || die "Can't link to $batchfile\n";
X unlink($tmpfile);
}
#
# Rewrite nntpxfer.pl's configuration file with the current date
# and time only if configuration was taken from NNTP parameter file.
#
if( ! defined $omitupdate) {
X open(NNTPFILE, ">$nntpfile") || die "Can't create $nntpfile\n";
X print NNTPFILE "$newsgroups $newdate $newtime $distributions\n";
X close NNTPFILE;
}
unlink $lock;
&chat'close();
Xexit 0;
SHAR_EOF
chmod 0755 nntpxfer.pl ||
echo 'restore of nntpxfer.pl failed'
Wc_c="`wc -c < 'nntpxfer.pl'`"
test 5643 -eq "$Wc_c" ||
echo 'nntpxfer.pl: original size 5643, current size' "$Wc_c"
fi
# ============= chat2.svr4 ==============
if test -f 'chat2.svr4' -a X"$1" != X"-c"; then
echo 'x - skipping chat2.svr4 (File already exists)'
else
echo 'x - extracting chat2.svr4 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'chat2.svr4' &&
*** chat2.pl Fri May 29 18:36:24 1992
--- /usr/local/lib/perl/chat2.pl Fri May 29 18:35:49 1992
***************
*** 30,36 ****
X $serveraddr = $x[4];
X }
X $serverproc = pack($sockaddr, 2, $port, $serveraddr);
! unless (socket(S, 2, 1, 6)) {
X # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
X # but who the heck would change these anyway? (:-)
X ($!) = ($!, close(S)); # close S while saving $!
--- 30,36 ----
X $serveraddr = $x[4];
X }
X $serverproc = pack($sockaddr, 2, $port, $serveraddr);
! unless (socket(S, 2, 2, 6)) {
X # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
X # but who the heck would change these anyway? (:-)
X ($!) = ($!, close(S)); # close S while saving $!
***************
*** 59,65 ****
X local($thisport) = shift || 0;
X local($thisproc_local) = pack($sockaddr, 2, $thisport, $thisaddr);
X local(*NS) = "__" . time;
! unless (socket(NS, 2, 1, 6)) {
X # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
X # but who the heck would change these anyway? (:-)
X ($!) = ($!, close(NS));
--- 59,65 ----
X local($thisport) = shift || 0;
X local($thisproc_local) = pack($sockaddr, 2, $thisport, $thisaddr);
X local(*NS) = "__" . time;
! unless (socket(NS, 2, 2, 6)) {
X # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
X # but who the heck would change these anyway? (:-)
X ($!) = ($!, close(NS));
SHAR_EOF
chmod 0644 chat2.svr4 ||
echo 'restore of chat2.svr4 failed'
Wc_c="`wc -c < 'chat2.svr4'`"
test 1352 -eq "$Wc_c" ||
echo 'chat2.svr4: original size 1352, current size' "$Wc_c"
fi
# ============= nntp.foobar ==============
if test -f 'nntp.foobar' -a X"$1" != X"-c"; then
echo 'x - skipping nntp.foobar (File already exists)'
else
echo 'x - extracting nntp.foobar (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'nntp.foobar' &&
comp.*,alt.*,!alt.binaries.* 920501 000000
SHAR_EOF
chmod 0644 nntp.foobar ||
echo 'restore of nntp.foobar failed'
Wc_c="`wc -c < 'nntp.foobar'`"
test 43 -eq "$Wc_c" ||
echo 'nntp.foobar: original size 43, current size' "$Wc_c"
fi
exit 0
--
Christian Seyb | Internet: cs@gold.sub.org uucp login: nuucp
Fuchsweg 86 | Mailbox: +49-8106-34593 24h 300-19200 PEP/V.32
8011 Baldham | +49-8106-34692 24h 300-19200 HST/FAX
-- AT&T is a modem test command ...
Technical Details
Message-ID:
<1992Jun4.050645.5168@gold.sub.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!sparky!uunet!olivea!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!overload.lbl.gov!lll-winken!iggy.GW.Vitalink.COM!widener!eff!iWarp.intel.com|inews!imutm1.de.intel.com!gold.sub.org!rootShow Full Headers
Complete Article Headers
Message-ID: <1992Jun4.050645.5168@gold.sub.org> Subject: Perl version of nntpxfer - a passive news transfer client Date: Thu, 04 Jun 1992 05:06:45 +0000 From: root@gold.sub.org (Christian Seyb) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!sparky!uunet!olivea!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!overload.lbl.gov!lll-winken!iggy.GW.Vitalink.COM!widener!eff!iWarp.intel.com|inews!imutm1.de.intel.com!gold.sub.org!root Newsgroups: comp.lang.perl,news.software.nntp Reply-To: cs@gold.sub.org Organization: Unix Mailbox Baldham Lines: 382