🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #4873

Re: Off-line Mail Readers

#4873
From: Ayman I. Kayssi
Date: Fri, 09 Jul 1993 20:29
327 lines
10109 bytes
#!/usr/bin/perl
##
## digestify v0.41
##
## Ayman Kayssi --  ayman@eecs.umich.edu
##
## Use at your own risk!
##
## Let me know if you find any bugs or add new features...
##
## The documentation is really horrible, so...
#
$0 =~ s|.*/||;
require 'getopts.pl';
&Getopts('a:b:d:e:f:s:m:h');
if($opt_h){
    print "Usage: $0 [-a max_num_of_articles] [-b max_art_size] [-d working_directory] [-e send_mail] [-f newsrc_file] [-m email_address] [-s nntp_server]
       $0 -h to get help
$0 reads a newsrc file (\$newrc variable or the argument to -f) 
in the working directory (\$wdir variable or the argument to -d)
and connects to an NNTP server (\$nntpserv variable or the argument to -s).
It gets from the server any articles numbered higher than the
number listed in the newsrc file. The format of this file is:
newsgroup last_read_article [email address]
>>example:
comp.lsi.cad 2200
comp.lsi 2310 joe@cs.univ.edu
comp.arch 35150
The articles are retrieved and placed in a file called newsgroup.dgst,
e.g. 'comp.lsi.dgst', 'comp.lsi.cad.dgst' in the working directory. A
maximum of \$max_art (or the argument to -a) is retrieved, and
any article whose size is larger than \$max_size (or the argument to -b)
is discarded. The article numbers in the newsrc file are then updated.
If an argument is given to the -m switch, $0 will send the
digests to the address specified, which overrides any email address specified
in the newsrc file. $0 uses either \$sendmail (set by default to 
/usr/lib/sendmail) or whatever the argument of the -e switch is. 
The mailed digests are removed from the working directory.
";
    exit;
}
#############################################################################
### Customize
#
# where to send the digests...
#
$origmailto = $opt_m;
#
# name of the newsrc file
#
$newsrc = $opt_f || 'newsrc';
#
# directory where the newsrc and the digests are kept
#
$wdir = $opt_d || '/z/ayman/Digests';
#
# NNTP server name
#
$nntpserv = $opt_s || 'zip.eecs.umich.edu';
#
# How do we send mail?
#
$sendmail = $opt_e || '/usr/lib/sendmail';
#
# maximum number of articles to retrieve in case there are more than this
# number of articles on the server - the articles will be the *last* $max_art
#
$max_art = $opt_a || 50;
#
# maximum article size above which the article will be discarded
#
$max_size = $opt_b || 20000;
#
#############################################################################
##
## go to the directory where all the stuff is installed
##
chdir $wdir || die "Can't change dir to $wdir: $!\n";
$newsrc =~ s|.*/||;
$newnewsrc = $newsrc.'.new';
open(NEWSRC,$newsrc) || die "Can't find $newsrc in $wdir: $!\n";
open(NEWNEWSRC,"> $newnewsrc") || die "Can't open $newnewsrc: $!\n";
##
## check that $sendmail exists and is executable
##
if($origmailto){
    die "Can't execute $sendmail\n" unless -x $sendmail;
}
##
## connect to the server and absorb the first line
##
&open_port($nntpserv,119) || die "unable to connect to $nntpserv..$!\n";
<SOCK>;
##
## main loop
##
while(<NEWSRC>){
    ++$nl;
    chop;
    ($newsgroup,$article,$altmailto) = split;
##
## make sure that the article number is an integer
##
    unless ($article =~ /^\d+$/){
        warn "Error on line $nl in $newsrc: $article should be an integer\n";
        warn "Removing line $nl from $newsrc\n";
	next;
    } 
##
## if there is an alternative mailing address for this newsgroup, override it
## with $origmailto
##
    $mailto = $origmailto || $altmailto;
##
## increment the article counter - this is the first article we don't have
##
    ++$article;
##
## get the last article that the server has
##
    $serverlast = &nntplast($newsgroup);
##
## make sure that the newsgroup is valid
##
    if ($serverlast == -1){
        warn "Bogus newsgroup $newsgroup on line $nl in $newsrc\n";
        warn "Removing line $nl from $newsrc\n";
        next;
    }
##
## digest name is newsgroup.dgst
##
    $digest = $newsgroup.'.dgst';
    rename($digest,$digest.'.old') if -e $digest;
    open(DIGEST,"> $digest") || die "Can't open $digest in $wdir: $!\n";
##
## If there are more than $max_art articles, get only the last $max_art
##
    if(($serverlast - $article + 1 ) > $max_art){
	$article = $serverlast - $max_art + 1;
	print DIGEST "Note: Only $max_art articles (the maximum allowed) ";
	print DIGEST "were included in this digest.\n";
	print DIGEST "-----\n";
    }
##
## get all the articles from the server
##
    &nntpget($newsgroup,$article,$serverlast);
    for($afile=$article;$afile<=$serverlast;$afile++){
##
## if the size of the article is greater than $max_size, delete the file
## without including in the digest
##
	if ((-s $afile) > $max_size){
	    unlink $afile;
	    print DIGEST "Article $afile was not included ";
	    print DIGEST "due to its large size.\n";
            print DIGEST "--------- End of Article ";
            print DIGEST "$afile in $newsgroup ---------\014\n";
	    next;
        }
##
## acceptable size: include the file in the digest after splitting into header
## and body
##
	if(open(ARTICLE,$afile)){
	    $/ = "";
##
## get the header of the article
##
	    $header = <ARTICLE>;
##
## some header processing can go here - I do none
##
	    print DIGEST $header;
##
## get the body of the article
##
	    undef($/);
	    $body = <ARTICLE>;
##
## print the body and separate messages by CNTRL-L and newline
##
	    print DIGEST "\n$body";
	    print DIGEST "--------- End of Article ";
	    print DIGEST "$afile in $newsgroup ---------\014\n";
	    close(ARTICLE);
##
## remove the article file
##
	    unlink $afile;
        }
    }
##
## update the new newsrc file
##
    $update = "$newsgroup $serverlast";
    $update .= " $altmailto" if $altmailto;
    print NEWNEWSRC "$update\n";
    close(DIGEST);
##
## send the digest
##
    if(($mailto) && (-s $digest)){
	open(DIGEST,$digest) || die "Hmm..can't open $digest for mailing:$!\n";
	open(SM,"| $sendmail $mailto") || die "Can't pipe into $sendmail:$!\n";
	print SM "To: $mailto\n";
	print SM "Subject: Digest of $newsgroup\n\n";
	print SM <DIGEST>;
	print SM ".\n";
	close(SM);
	close(DIGEST);
##
## remove the new and old digests since the digest is being mailed
##
	unlink($digest);
	unlink("$digest.old") if -e "$digest.old";
    }
    $/ = "\n";
}
close(NEWSRC);
close(NEWNEWSRC);
rename($newnewsrc,$newsrc);
print SOCK "quit\r\n";
<SOCK>;
close(SOCK);
##
## function nntplast
## arguments: $newsgroup -- name of a news group
## returns  : $last_available_article -- the number of the last available 
##                                       article on the server
##            -1 if the newsgroup is not valid
##
sub nntplast{
    local ($group) = @_;
    print SOCK "group $group\r\n";
    $_=<SOCK>;
    /^411 / && return (-1);
    /^211 / || warn "strange return value($_)\n";
    ($num,$start,$end)=/^211 (\d+) (\d+) (\d+)/;
    return $end;
}
##
## function nntpget
## arguments: $newsgroup -- name of a news group
##            $start -- number of the first article in a range to fetch
##            $end -- number of the last article in a range to fetch
## retuns: -- creates files with filenames corresponding to article numbers
##
sub nntpget{
    local ($group,$start,$end) = @_;
    for($num=$start;$num<=$end;$num++){
	print SOCK "HEAD $num\r\n";
        $_=<SOCK>;
        unless (/^221 /){ 
	    warn "head cmd problems ($num):$_";
	}
        else{
	    open(OUT,"> $num") || die "unable to open $num:$!\n";
	    for (;;){
		$_=<SOCK>;
		last if /^\.\r\n$/;
		chop;
		chop;
		print OUT "$_\n";
	    }
	}
        print OUT "\n";
        print SOCK "BODY $num\r\n";
        $_=<SOCK>;
        unless (/^222 /){ 
	    warn "body cmd problems ($num):$_";
	}
        else{
	    for (;;){
		$_=<SOCK>;
		last if /^\.\r\n$/;
		chop;
		chop;
		print OUT "$_\n";
	    }
	    print OUT "\n";
	}
        close(OUT);
    }
}
##
## function open_port -- Who wrote it?
## arguments: $server -- a tcp/ip server
##            $port -- a port number
## returns  : 1 or undef
##
sub open_port{
    local($server, $port) = @_;

    $sockaddr = 'S n a4 x8';
    chop($thishost = `hostname`); $thisaddr = (gethostbyname($thishost))[4];
    $thisproc = pack($sockaddr, 2, 0, $thisaddr);
    
    local($serveraddr,$serverproc);
    if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/){ 
	$serveraddr = pack('C4', $1, $2, $3, $4); 
    }
    else{
	local(@x) = gethostbyname($server);
	return undef unless @x;
	$serveraddr = $x[4];
    }
    $serverproc = pack($sockaddr, 2, $port, $serveraddr);
    unless (socket(SOCK, 2, 1, 6)){
	# XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
	# but who the heck would change these anyway? (:-)
	($!) = ($!, close(SOCK)); # close S while saving $!
	return undef;
    }
    unless (bind(SOCK, $thisproc)){
	($!) = ($!, close(SOCK)); # close S while saving $!
	return undef;
    }
    unless (connect(SOCK, $serverproc)){
	($!) = ($!, close(SOCK)); # close S while saving $!
	return undef;
    }
    select((select(SOCK), $| = 1)[0]);
    1;
}
Technical Details
Message-ID: <21kkesINNd8g@srvr1.engin.umich.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmd.de!xlink.net!sol.ctr.columbia.edu!spool.mu.edu!caen!razi.eecs.umich.edu!ayman
References: <C9qq2z.23y@discus.technion.ac.il> <Jul.8.20.39.15.1993.6265@andromeda.rutgers.edu>

Show Full Headers
Complete Article Headers
Message-ID: <21kkesINNd8g@srvr1.engin.umich.edu>
Subject: Re: Off-line Mail Readers
Date: Fri, 09 Jul 1993 20:29:16 +0000
From: ayman@razi.eecs.umich.edu (Ayman I. Kayssi)
References: <C9qq2z.23y@discus.technion.ac.il> <Jul.8.20.39.15.1993.6265@andromeda.rutgers.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmd.de!xlink.net!sol.ctr.columbia.edu!spool.mu.edu!caen!razi.eecs.umich.edu!ayman
Newsgroups: news.software.readers
Organization: University of Michigan Engineering, Ann Arbor
Lines: 327
Distribution: world
NNTP-Posting-Host: razi.eecs.umich.edu
X-Newsreader: TIN [version 1.2 PL0]