Article View: news.software.readers
Article #42886news2html - NNTP headers to HTML
From: Andrew Daviel
Date: Mon, 24 Aug 1998 07:22
Date: Mon, 24 Aug 1998 07:22
193 lines
5870 bytes
5870 bytes
Herewith 2 Perl scripts, "news2html" and "lastnews"
Basically, given a newsgroup, news2html connects to your
NNTP server (from NNTPSERVER in environment) and dumps all
the NNTP headers to an HTML file, giving the user maximal information to
decide whether to download the message body ("me too" messages
and flames are typically short). Threading is left as
an exercise for the student ... :-)
Andrew Daviel
#!/usr/bin/perl
#
# news2html, Andrew Daviel <andrew@vancouver-webpages.com>
#
# This script connects to an NNTP host and creates an HTML page
# from the headers of a newsgroup. The HTML may then be viewed with
# a news-aware browser such as Netscape.
# What's the point ? The output of news2html is richer than the one-line
# listings generated by Netscape's builtin news browser, so one
# can choose to ignore postings based on content length, content type, etc.
# while the subject field is allowed to wrap to its fll length.
# Usage:
# news2html newsgroup [start] [min size]
# the output is written to newsgroup.html
# the last message is saved in a dbm file. By default, news2html
# starts reading at the last message seen. THe companion script
# "lastnews" dumpd the database.
$node = $ENV{'NNTPSERVER'} ;
$blen = 10000 ;
$hmax = 10 ; $addmax = 300 ;
require 5.001;
use Socket;
$tout1 = 10 ; # timeout on initial connection
$tout = 90 ; # timeout on complete transaction
$loop = 600 ;
if ($ARGV[0] =~ /^-/) { $opt = shift(@ARGV) ;}
$|=1 ; # flush buffers
$debug = ($opt eq '-t') ;
$quiet = ($opt eq '-q' || $prstat) ;
$timed_out=0 ; $sec=0 ;
if (!$debug) { $SIG{'ALRM'} = "timed_out" ; alarm($tout1) ; }
$proto = getprotobyname('tcp') ;
if ($timed_out) { exit ; }
unless ($proto) {
print STDERR "ERROR: getprotobyname fail ($!)\n";
&clean ; exit ;
}
$port = 119;
$iaddr = inet_aton ($node) ;
if ($timed_out) { exit ; }
unless ($iaddr) {
print STDERR "ERROR: inet_aton ($!)\n";
&clean ; exit ;
}
$sin = sockaddr_in ($port, $iaddr);
$stat = socket(S, PF_INET, SOCK_STREAM, $proto) ;
if ($timed_out) { exit ; }
unless ($stat) {
print STDERR "ERROR: socket fail ($!): $proto\n" ;
&clean ; exit ;
}
$stat = connect(S, $sin) ;
if ($timed_out) { exit ; }
unless ($stat) {
print STDERR "ERROR: connect fail ($!)\n";
&clean ; exit ;
}
unless ($ARGV[0]) {
print STDERR "Usage: $0 newsgroup [start] [minsize]\n" ;
exit ;
}
$group = $ARGV[0] ;
dbmopen(%LAST,"last",0600) || print STDERR "Can't open DB\n" ;
if (!$debug) { alarm($tout) ; }
recv S,$_,$blen,0 ;
if ($timed_out) { exit ; }
$version = $_ ;
if (!$quiet) { print "*$_*\n"; }
if ($_ != 200) { exit ; }
open (HTML,">>$group.html") ||
open (HTML,">$group.html") || die 'Cannot write newstest.html' ;
print HTML<<EOT ;
<html><head><title>$group</title>
</head><body bgcolor="#ffffff">
<h2>$group</h2>
EOT
send S, "listgroup $group\r\n",0 ;
recv S,$_,$blen,0 ;
if ($timed_out) { exit ; }
if (!$quiet) { print "*$_*\n"; }
tr/\r//d ;
@lines = split(/\n/,$_) ;
$nlines = @lines-1 ;
while ($lines[$nlines] ne '.') {
recv S,$L,$blen,0 ;
if ($timed_out) { exit ; }
#if (!$quiet) { print "*$L*\n"; }
$L =~ tr/\r//d ;
$_ .= $L ;
@lines = split(/\n/,$_) ;
$nlines = @lines-1 ;
}
if ($lines[0] =~ /^211/) { shift(@lines) ; $nlines = @lines-1 ; }
$last = $lines[$nlines-1] ;
$first = $LAST{$group} ;
if ($ARGV[2]) { $first = $ARGV[2] ; }
if ( $first) { print "Starting from $first\n" ; }
foreach $msg (@lines) {
print "$msg\r" ;
if ($msg<=$first) { next ; }
send S, "head $msg\r\n",0 ;
recv S,$_,$blen,0 ;
@head = split(/\n/,$_) ;
$out = '' ; undef($nln) ;
foreach $_ (@head) {
if (/^Message-ID:/i) {
s/</<a href=\"news:/ ; s/>/\">$msg<\/a>/ ; $out .= "$_<br>\n" ;
}
s/&/\&/g ; s/</</g ; s/>/>/g ;
if (/^Subject:/i) { $out .= "$_<br>\n" ; }
if (/^From:/) { s/</</ ; s/>/>/ ; $out .= "$_<br>\n" ; }
if (/^Content-Type:/i) { $out .= "$_<br>\n" ; }
if (/^Date:/i) { $out .= "$_<br>\n" ; }
if (/^Lines:/i) {
$out .= "$_<br>\n" ;
$nln = $_ ;
$nln =~ s/^Lines:// ;
# print HTML "$nln " ;
}
if (/^Organization:/i) { $out .= "$_<br>\n" ; ; }
}
if (!$nln || !$ARGV[1] || $nln>$ARGV[1]) {
print HTML "$out <p>\n" ;
}
}
print "\n" ;
send S, "quit\r\n",0 ;
recv S,$_,$blen,0 ;
if ($timed_out) { exit ; }
if (!$quiet) { print "$_\n"; }
print HTML<<EOT ;
</body></html>
EOT
close(HTML) ;
$LAST{$group} = $last ;
dbmclose(%LAST) ;
&clean ;
exit ;
sub timed_out {
print STDERR "Port 25 timed out to $node\n";
$timed_out=1 ; &clean ;
}
sub clean {
shutdown(S,2) ;
}
#!/usr/bin/perl5
require 5.001;
dbmopen(%LAST,"last",0600) || print STDERR "Can't open DB\n" ;
while (($grp,$last) = each(%LAST)) {
print "$grp: $last\n" ;
}
Technical Details
Message-ID:
<9x8E1.13485$hr3.6315491@news.rdc1.bc.wave.home.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!news2.funet.fi!newsfeed1.funet.fi!newsfeed1.swip.net!swipnet!news.idt.net!news-peer.gip.net!news.gsl.net!gip.net!newshub1.home.com!news.home.com!news.rdc1.bc.wave.home.com.POSTED!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <9x8E1.13485$hr3.6315491@news.rdc1.bc.wave.home.com> Subject: news2html - NNTP headers to HTML Date: Mon, 24 Aug 1998 07:22:45 GMT From: andrew@cr1001800-a.rchmd1.bc.wave.home.com (Andrew Daviel) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!news2.funet.fi!newsfeed1.funet.fi!newsfeed1.swip.net!swipnet!news.idt.net!news-peer.gip.net!news.gsl.net!gip.net!newshub1.home.com!news.home.com!news.rdc1.bc.wave.home.com.POSTED!not-for-mail Newsgroups: news.software.readers,alt.sources Organization: home, Richmond BC Reply-To: andrew@vancouver-webpages.com Distribution: world X-Newsreader: TIN [version 1.2 PL2] Lines: 192 NNTP-Posting-Host: 24.113.41.140 NNTP-Posting-Date: Mon, 24 Aug 1998 00:22:45 PDT