This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial integration of libnet-1.0703.
[perl5.git] / lib / Net / demos / nntp.mirror
1 #!/usr/bin/perl5
2
3 ### Subject: Re: Fuller example of Net::NNTP?
4 ### Date:  Tue, 4 Feb 1997 10:37:58 -0800
5 ### From: "Paul E. Hoffman" <phoffman@imc.org>
6 ### To: Graham Barr <gbarr@ti.com>
7 ### 
8 ### Thanks for your reply. After looking at the examples, I realized that
9 ### you're not doing what I want, which is to store the messages on the local
10 ### hard disk with the same message number as what was on the remote. So, I
11 ### rolled my own program, although I haven't finished it yet (I have a hook
12 ### for expiring, but haven't done it yet).
13 ### 
14 ### You are welcome to use this in the Net:: distribution if you think it is
15 ### useful.
16 ###
17 ### NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
18 ###
19 ### This script is included as-is, I give no guarantee that it will
20 ### work on every system
21 ###
22
23 use Net::NNTP;
24
25 $BaseDir = '/usr/usenet';
26 chdir($BaseDir) or die "Could not cd to $BaseDir\n";
27
28 # Format of grouplist is:
29 #    groupname<tab>expirationdays
30 # expirationdays is the number of days to leave the articles around;
31 #    set it to 0 if you want the articles to stay forever
32 # If the groupname starts with a #, it is skipped
33 open(GROUPLIST, 'grouplist.txt') or die "Could not open grouplist.txt\n";
34 while(<GROUPLIST>) {
35         $Line = $_; chomp($Line);
36         if($Line eq '') { next };  # Skip blank lines
37         if(substr($Line, 0, 1) eq '#') { next };  # Skip comments
38         push(@Groups, $Line)
39 }
40
41 $NntpPtr = Net::NNTP->new('news.server.com');
42
43 foreach $GroupLine (@Groups) {
44         ($GroupName, $GroupExp) = split(/\s/, $GroupLine, 2);
45         # Process the expiration first (still to be done...)
46
47         # See if this is a new group
48         unless(-e "$BaseDir/$GroupName") {
49                 unless(mkdir("$BaseDir/$GroupName", 0755))
50                         { die "Could not make $BaseDir/$GroupName\n" }
51         }
52         chdir("$BaseDir/$GroupName") or die "Couldn't chdir to $GroupName\n";
53         # Find the last article in the directory
54         @AllInDir = <*>; @RevSortedAllInDir = reverse(sort(@AllInDir));
55         $LenArr = @RevSortedAllInDir;
56         if($LenArr > 0) { $NumLastInDir = $RevSortedAllInDir[0] }
57         else { $NumLastInDir = 0 }
58         ($NumArt, $NumFirst, $NumLast, $XGroupName) =
59 $NntpPtr->group($GroupName);
60
61         if($NumLast == $NumLastInDir) { next }  # No new articles
62         if($NumLast < $NumLastInDir)
63                 { die "In $GroupName, the last number was $NumLast, but the " .
64                         " last number in the directory was $NumLastInDir\n" }
65         # Figure out which article to start from
66         if($NumLastInDir == 0) { $GetArtNum = $NumFirst }
67         else { $GetArtNum = $NumLastInDir + 1 }
68
69         # Now read each of the new articles
70         while(1) {  # Loop until "last" is called
71                 $ArtRef = $NntpPtr->article($GetArtNum);
72                 @ArtArr = @$ArtRef; $ArtArrLen = @ArtArr;
73                 if($ArtArrLen > 0 ) {  # Skip article numbers that had 0 len
74                         open(OUT, ">$GetArtNum") or
75                                 die "Could not create $GroupName/$GetArtNum\n";
76                         print OUT @$ArtRef; close(OUT);
77                 }
78
79                 # Check if we're at the end
80                 if($GetArtNum == $NumLast) { last }
81                 $GetArtNum += 1;  # Increment the article number to get
82         }
83 }
84
85 $NntpPtr->quit;
86 exit;