Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

NNTP: Most recent articles in a group

by tomhukins (Curate)
on Mar 08, 2001 at 16:23 UTC ( #62951=sourcecode: print w/ replies, xml ) Need Help??

Category: Networking Code
Author/Contact Info Tom Hukins <tom@eborcom.com>
Description:

Prints out the subject and sender of the most recent articles in a newsgroup.

To configure the script's behaviour, set the NUMBER_OF_ARTICLES, GROUP_NAME and SERVER_NAME constants at the top of the script.

I've tried to make this script as resource-friendly as possible for both the client and server. If there's a better way, let me know!

#!/usr/bin/perl -Tw

use strict;
use Net::NNTP ();

use constant NUMBER_OF_ARTICLES    => 10;
use constant GROUP_NAME        => 'comp.lang.perl.moderated';
use constant SERVER_NAME    => 'my.nntp.server';
use constant NNTP_DEBUG        => 0;

my $nntp = Net::NNTP->new(SERVER_NAME, 'Debug' => NNTP_DEBUG) or die;
my($article_count, $first_article, $last_article) = $nntp->group(GROUP
+_NAME) or die;

# Which XOVER fields contain Subject: and From:?
my $count = 0;
my %xover_fmt = map( ($_, $count++), @{ $nntp->overview_fmt or die} );
die unless exists $xover_fmt{'Subject:'};
my $subject_offset = $xover_fmt{'Subject:'};
my $from_offset = $xover_fmt{'From:'};

my(@xover, $start_article);
RETRIEVE: while ($#xover+1 < NUMBER_OF_ARTICLES and $last_article >= $
+first_article) {
    # How many articles do we need?  Stop retrieving if we have enough
    my $articles_required = NUMBER_OF_ARTICLES - ($#xover+1) or last R
+ETRIEVE;

    # Fetch overview information for the articles
    $start_article = $last_article - ($articles_required-1);
    $start_article = $start_article > $first_article ? $start_article 
+: $first_article;
    my $xover_query = $start_article == $last_article ?
    $start_article :
    [$start_article, $last_article];
    my $xover_ref = $nntp->xover($xover_query) or die;

    # Store headers for the articles we've retrieved
    foreach (sort {$b <=> $a} keys %$xover_ref) {
        push @xover, $xover_ref->{$_};
    }
} continue {
    # Move the pointer forward to fetch previous articles
    $last_article = $start_article - 1;
}

# Disconnect from the NNTP server
$nntp->quit;

print join("\n", map ($_->[$subject_offset].' from '.$_->[$from_offset
+], @xover)),"\n";

Comment on NNTP: Most recent articles in a group
Download Code
Re: NNTP: Most recent articles in a group
by merlyn (Sage) on Mar 08, 2001 at 21:10 UTC

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://62951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-07-05 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls