Article 8981 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:8981
Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!howland.reston.ans.net!darwin.sura.net!mojo.eng.umd.edu!rensin
From: rensin@eng.umd.edu (David Rensin)
Newsgroups: comp.lang.perl
Subject: NNTP reader for Perl (source)
Date: 17 Dec 1993 18:58:20 GMT
Organization: Project GLUE, University of Maryland, College Park
Lines: 188
Distribution: world
Message-ID: <2esvgc$6u8@mojo.eng.umd.edu>
NNTP-Posting-Host: avalon.eng.umd.edu


The following is the source to a news reader I wrote using chat2.pl.. Let
me know what you think.


	-dave :-)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dave Rensin                     College Of Engineering
rensin@Glue.umd.edu   ^^^^^^    University of Maryland
                    @  0 0  @
                        ^ 
                       ()  
                      \__/ 
                      -<>-
                       /\
 
        (will hack Perl code for food) :)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-------------Begin read_group.pl -----------------------


#!/usr/local/bin/perl

#######################################
#     read_group.pl  - by Dave Rensin #
# Syntax: Read_group <news_group>     #
#                                     #
# Variables:                          #
#  $news_server = name of NNTP server #
#  $nntp_port = NNTP port             #
#######################################


require "chat2.pl";
$SIG{'PIPE'} = 'IGNORE';

$prog_name = $0;
($junk,$prog_name) = split(/\//,$prog_name,2) while ($prog_name =~ /\//);
die "Syntax: $prog_name <news group>\n" if ($#ARGV <0);

$| = 1;

$news_server = "mojo.eng.umd.edu";
$nntp_port = 119;

print "\nConnecting to server $news_server...";

$server_handle = &chat'open_port( $news_server, $nntp_port );


#############################
# Wait for a valid connection.
#############################

do  {
	&chat'expect( $server_handle, 30,
		'posting ok\)\.', 'print "\nConnected.";$result_code = 1',
		'TIMEOUT', '$result_code = 2',
		'EOF', '$result_code = 3');
} until $result_code;


############################
# Get group info
############################

&chat'print( $server_handle, "group $ARGV[0]\n");
$result_code = 0;

do {
	&chat'expect( $server_handle, 600,
		'411 No such group', '$result_code = 4',
		'\d+.+', '$output = $&;$result_code = 1',
		'TIMEOUT', '$result_code = 2',
		'EOF', '$result_code = 3');
} until $result_code;

if ($result_code == 4) {

	print "\nNo such group..\n";
	&chat'print( $server_handle, "quit\n");
	exit;
}

($crap,$crap2,$begin,$end,$group) = split(/ /,$output,5);


####################################
# Get all valid headers for a group
####################################

print "\nSucking down headers...\n";

for ($article = $begin;$article < $end + 1;$article ++) {

        $output = " ";
	$left = $end - $article;
        print "  ($article/$end): $left left.\r";
        &chat'print( $server_handle, "xover $article\n");
	$result_code = 0;

	do {
        	&chat'expect( $server_handle, 3,
               	'$article\t(.+)', '$output = $1;$result_code = 1',
               	'TIMEOUT', '$result_code = 2',
               	'EOF', '$result_code = 3');
	} until $result_code;

	if ($result_code == 1) {
		$output =~ /([^\t]*)\t/;
        	$headers[$ctr] = "$1\n";
	}

        $ctr++;
}


$article = " ";


##########################
# While input != NULL
##########################

while ($article) {

        open (out,"|$ENV{'PAGER'}") || die "shit.";
        $ctr =0;

        foreach $header (@headers) {
                $header = "BAD ARTICLE NUMBER\n" if !($header);
                print out $begin+$ctr.") $header";
                $ctr++;
        }
        close out;

        print "\nEnter article number: ";
        $article = <STDIN>;
        chop $article;

        if (!$article) {
                &chat'print( $server_handle, "quit\n");
                exit;
        }

	print "\nAsking for article $article...";

        do {
                &chat'expect( $server_handle, 1,
                '.*\n', '$output = $&;$result_code = 0',
                'TIMEOUT', '$result_code = 2',
                'EOF', '$result_code = 3');
        } until $result_code;

        &chat'print( $server_handle, "body $article\n");
	$result_code =0;

        $output = "";
        $text = "";

        while ($output !~ /^\.\s*$/o) {

                do {
                        &chat'expect( $server_handle, 600,
                        '.*\n', '$output = $&;$result_code = 1',
                        'TIMEOUT', '$result_code = 2',
                        'EOF', '$result_code = 3');
                } until $result_code;

                $text .= "$output" if ($output !~ /222 $article/);
        }

	$text =~ s/\n\.\s*\n//og;

        open (out,"|$ENV{'PAGER'}") || die "shit.";
        print out $text;
        close out;
}

&chat'print( $server_handle, "quit\n");

print "\n";

----------- End read_group.pl ---------------



