#!perl

### Subject: Re: Fuller example of Net::NNTP?
### Date:  Tue, 4 Feb 1997 10:37:58 -0800
### From: "Paul E. Hoffman" <phoffman@imc.org>
### To: Graham Barr <gbarr@ti.com>
### 
### Thanks for your reply. After looking at the examples, I realized that
### you're not doing what I want, which is to store the messages on the local
### hard disk with the same message number as what was on the remote. So, I
### rolled my own program, although I haven't finished it yet (I have a hook
### for expiring, but haven't done it yet).
### 
### You are welcome to use this in the Net:: distribution if you think it is
### useful.
###
### NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
###
### This script is included as-is, I give no guarantee that it will
### work on every system
###

use 5.008001;

use strict;
use warnings;

use Net::NNTP;

my $BaseDir = '/usr/usenet';
chdir($BaseDir) or die "Could not cd to $BaseDir\n";

# Format of grouplist is:
#    groupname<tab>expirationdays
# expirationdays is the number of days to leave the articles around;
#    set it to 0 if you want the articles to stay forever
# If the groupname starts with a #, it is skipped
my $GroupList;
open($GroupList, '<', 'grouplist.txt') or die "Could not open grouplist.txt\n";
my @Groups;
while(<$GroupList>) {
        my $Line = $_; chomp($Line);
        if($Line eq '') { next };  # Skip blank lines
        if(substr($Line, 0, 1) eq '#') { next };  # Skip comments
        push(@Groups, $Line)
}
close $GroupList;

my $NntpPtr = Net::NNTP->new('news.server.com');

foreach my $GroupLine (@Groups) {
        my($GroupName, $GroupExp) = split(/\s/, $GroupLine, 2);
        # Process the expiration first (still to be done...)

        # See if this is a new group
        unless(-e "$BaseDir/$GroupName") {
                unless(mkdir("$BaseDir/$GroupName", 0755))
                        { die "Could not make $BaseDir/$GroupName\n" }
        }
        chdir("$BaseDir/$GroupName") or die "Couldn't chdir to $GroupName\n";
        # Find the last article in the directory
        my @AllInDir = glob('*'); my @RevSortedAllInDir = reverse(sort(@AllInDir));
        my $LenArr = @RevSortedAllInDir;
        my $NumLastInDir;
        if($LenArr > 0) { $NumLastInDir = $RevSortedAllInDir[0] }
        else { $NumLastInDir = 0 }
        my($NumArt, $NumFirst, $NumLast, $XGroupName) =
                $NntpPtr->group($GroupName);

        if($NumLast == $NumLastInDir) { next }  # No new articles
        if($NumLast < $NumLastInDir)
                { die "In $GroupName, the last number was $NumLast, but the " .
                        " last number in the directory was $NumLastInDir\n" }
        # Figure out which article to start from
        my $GetArtNum;
        if($NumLastInDir == 0) { $GetArtNum = $NumFirst }
        else { $GetArtNum = $NumLastInDir + 1 }

        # Now read each of the new articles
        while(1) {  # Loop until "last" is called
                my $ArtRef = $NntpPtr->article($GetArtNum);
                my @ArtArr = @$ArtRef; my $ArtArrLen = @ArtArr;
                if($ArtArrLen > 0 ) {  # Skip article numbers that had 0 len
                        my $Out;
                        open($Out, '>', $GetArtNum) or
                                die "Could not create $GroupName/$GetArtNum\n";
                        print $Out @$ArtRef; close($Out);
                }

                # Check if we're at the end
                if($GetArtNum == $NumLast) { last }
                $GetArtNum += 1;  # Increment the article number to get
        }
}

$NntpPtr->quit;
exit;
