script/kiboze.pl untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 298 CC 3 SCAR 13.0
CC
3
Cov
0%
Exit
12.0
BEGIN
2.0
Usage
2.0
CRAP 3.7
1 #!/usr/local/bin/perl
2 # -*- Perl -*- # Thu Apr 29 10:15:37 CDT 2004
3 ###############################################################################
4 # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2004, Tim Skirvin.
5 # Redistribution terms are below.
6 ###############################################################################
7 our $VERSION = "1.11";
8  
9 ###############################################################################
10 ### User Configuration ########################################################
11 ###############################################################################
12 use vars qw( $LOCALCONF $KIBOZEDIR $DB_TYPE $DEBUG $QUIET $VERBOSE
13 %SERVERS $MAXARTS $LOCALNAME );
14  
15 ## Rather than having everything in this shared configuration, load this
16 ## file to get additional configuration. This file contains additional
17 ## perl.
18  
19 $LOCALCONF = "$ENV{'HOME'}/.kibozerc";
20  
21 ## Where should we store all of our files? This needs to be set or
22 ## nothing will run; also, the directory must already exist.
23  
24 $KIBOZEDIR = "$ENV{'HOME'}/kiboze";
25  
26 ## What kind of database should we use to store history information?
27  
28 $DB_TYPE = "DB_File";
29  
30 ## Where are we getting our news content? By raid^H^H^Heading it from
31 ## other servers, of course! These are the servers and newsrc we will
32 ## be using.
33  
34 %SERVERS = (
35 $ENV{'NNTPSERVER'} => "$KIBOZEDIR/newsrc",
36 );
37  
38 ## How many articles should we invoke in a single run? This shouldn't be
39 ## that high unless you invoke the script very rarely. Set to '-1' to
40 ## ignore this entirely.
41  
42 $MAXARTS = 1000;
43  
44 ## Local "machine" name, which will appear in the Xref and Path headers.
45  
46 $LOCLNAME = "archive";
47  
48 ## Do we want to print debugging information? Use verbose status messages?
49 ## Be entirely silent? Set these things here.
50  
51 $DEBUG = 0;
52 $VERBOSE = 0;
53 $QUIET = 0;
54  
55 ## If the modules are set up in a non-standard place, edit this line
56 ## as appropriate.
57 BEGIN { use lib '/home/tskirvin/dev/news-archive'; }
58  
59 ###############################################################################
60 ### main() ####################################################################
61 ###############################################################################
62  
63 use strict; # Good programming is our friend
64 use warnings;
65  
66 use Getopt::Std; # Command line functions
67 use News::Archive; # News functions
68 use News::Article;
69 use News::Newsrc;
70 use Net::NNTP::Auth;
71 use Net::NNTP;
72  
73 # Error Codes
74 our %ERROR = ( 'SUCCESS' => 0, 'CONFIG' => 1, 'SERVER' => 2 );
75  
76 # Command-line configuration
77 use vars qw( %OPTS );
78 getopts('hvVQdc:s:n:', \%OPTS);
79  
80 # Load local configuration from local configuration file
81 $LOCALCONF = $OPTS{'c'} if $OPTS{'c'};
82 if ( $LOCALCONF && -r $LOCALCONF ) { do $LOCALCONF }
83  
84 &Usage if $OPTS{'h'}; # Print usage information and exit
85 &Version if $OPTS{'v'}; # Print version information and exit
86  
87 # Other command-line parsing; overrides localconf
88 $DEBUG = $OPTS{'d'} if defined $OPTS{'d'};
89 $VERBOSE = $OPTS{'V'} || $DEBUG || $VERBOSE || 0; # Give verbose messages?
90 $QUIET = $OPTS{'Q'} || $QUIET || 0;
91  
92 our $server = $OPTS{'s'} || "";
93 our $newsrc = $OPTS{'n'} || "";
94  
95 # Figure out which servers and newsrcs we want to use.
96 our @SERVERS;
97  
98 # Did we get a specific server on the command-line? Use it.
99 if ($server) {
100 push @SERVERS, $server;
101 if ($newsrc) { $SERVERS{$server} = $newsrc; } # Use the -n for this server
102 elsif (defined($SERVERS{$server})) { } # We've already got one.
103 else { Exit('CONFIG', "Couldn't find newsrc file for $server") }
104  
105 # Use everything in %SERVERS
106 } else {
107 # Limit to just the server with the given newsrc
108 if ($newsrc) {
109 $newsrc = "$ENV{'PWD'}/$newsrc" unless ($newsrc =~ m%^/%);
110  
111 foreach (keys %SERVERS) {
112 push (@SERVERS, $_) if ($newsrc eq $SERVERS{$_});
113 }
114  
115 if ( scalar(@SERVERS) > 1 ) { # Check the matches
116 Exit('CONFIG', "Too many matches on newsrc $newsrc (@SERVERS)");
117 } elsif ( scalar(@SERVERS) == 0 ) {
118 Exit('CONFIG', "No servers found for newsrc $newsrc");
119 }
120  
121 } else { @SERVERS = keys %SERVERS }
122 }
123  
124 Exit('CONFIG', "No servers to scan") unless ( scalar(@SERVERS) >= 1 );
125 Exit('CONFIG', "LOCALNAME not set") unless $LOCALNAME;
126  
127 mkdir($KIBOZEDIR) unless (-d $KIBOZEDIR);
128  
129 # Create the options hash to start a News::Archive object
130 our %OPTHASH;
131 $OPTHASH{ 'basedir' } = $KIBOZEDIR if $KIBOZEDIR;
132 $OPTHASH{ 'db_type' } = $DB_TYPE if $DB_TYPE;
133 $OPTHASH{ 'debug' } = $DEBUG if $DEBUG;
134 $OPTHASH{ 'hostname' } = $LOCALNAME if $LOCALNAME;
135  
136 # Create the News::Archive object
137 my $archive = new News::Archive ( %OPTHASH )
138 or Exit('SERVER', "Couldn't create/load archive item: ",
139 News::Archive->error);
140  
141 my $overcount = 0;
142 my (@articles, %COUNT);
143 foreach $server (@SERVERS) {
144  
145 # Load the newsrc file
146 my $newsrcfile = $SERVERS{$server};
147 print "$server - loading $newsrcfile\n" if $VERBOSE;
148 my $Newsrc = new News::Newsrc;
149 unless ( $Newsrc->load($newsrcfile) ) {
150 warn "Couldn't open $newsrcfile for server $server\n" unless $QUIET;
151 next;
152 }
153  
154 # Connect to the NNTP Server
155 my $NNTP = Net::NNTP->new($server);
156 Exit('SERVER', "Couldn't connect to server $server") unless $NNTP;
157  
158 my ($nntpuser, $nntppass) = Net::NNTP::Auth->nntpauth($server);
159 $NNTP->authinfo($nntpuser, $nntppass) if defined($nntppass);
160  
161 # Make sure we're subscribed to all of the groups we're saving from
162 foreach ($Newsrc->groups) { $archive->subscribe($_) }
163  
164 my $count = 0;
165 foreach my $group ($Newsrc->groups) {
166 my ($articles, $firstnum, $lastnum, $name) = $NNTP->group($group);
167 next unless $name;
168  
169 print "$name: articles $firstnum - $lastnum exist on server\n" if $VERBOSE;
170
171 my $i;
172 foreach ($i = $firstnum; $i <= $lastnum; $i++) {
173 next unless $i > 0;
174 last if ($count >= $MAXARTS);
175 next if $Newsrc->marked($group, $i); # Next if it's marked
176  
177 # Get the article and its information
178 my $article = News::Article->new( $NNTP->article($i) );
179 $Newsrc->mark($group, $i);
180  
181 unless ($article) {
182 warn "No article received ($i from $group on $server)\n" if $DEBUG;
183 next;
184 }
185 my $msgid = $article ? $article->header('message-id') : "";
186 unless ($msgid) { warn "No message-id on article\n" if $DEBUG; next; }
187  
188 if ($archive->article( $msgid )) {
189 print "Article $msgid was already archived, skipping...\n" if $VERBOSE;
190 next;
191 }
192 my $ret = $archive->ihave( $msgid,
193 ( $article->rawheaders, '', @{$article->body} ) );
194 $count++ if $ret;
195 # Would like to get the reason back out here...
196 if ($VERBOSE) { print $ret ? "Accepted article $msgid\n"
197 : "Didn't save article $msgid\n" }
198 }
199 $Newsrc->save;
200 }
201 print "$count articles archived for server $server\n" if $VERBOSE;
202
203 # Close the NNTP Connection
204 $NNTP->quit;
205 $COUNT{$server} += $count if $count;
206 $overcount += $count;
207 }
208  
209 $archive->close;
210  
211 Exit('SUCCESS', "$overcount articles archived from "
212 . ( scalar keys %COUNT ) . " servers");
213  
214 ###############################################################################
215 ### Subroutines ###############################################################
216 ###############################################################################
217  
218 ## Usage ()
219 # Prints out a short help file and exits.
220 sub Usage {
221 my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name
222 Exit('CONFIG', version($VERSION), <<EOM);
223 Archives news articles for later use with News::Archive
224  
225 [...]
226 $prog is part of the News::Archive package, which is used to archive
227 news articles in a reasonably efficient manner. This particular script
228 is meant for use for personal archives, loading articles (or mbox-style
229 archives) on STDIN and loading them into @{[ $KIBOZEDIR || "(unset)" ]}.
230  
231 Configuration information is stored in the below configuration file;
232 please read the manual pages for more information on its configuration.
233  
234 Usage: $prog [-hvVQd] [-c configfile] [-a groupname] < ARTICLE
235 -h Print this usage information and exit.
236 -v Print version information and exit.
237 -c configfile Load this configuration file instead of default.
238 Default: $LOCALCONF
239 -a groupname Add this group to all articles we download, so
240 that they can be accessed uniformly.
241 -V Verbose; print information on every article.
242 Default: @{[ $VERBOSE ? "On" : "Off" ]}
243 -Q Quiet mode; only print that which is absolutely
244 necessary (and even then think about it).
245 Default: @{[ $QUIET ? "On" : "Off" ]}
246 -d Debug; print debugging information (implies -V)
247 Default: @{[ $DEBUG ? "On" : "Off" ]}
248 EOM
249 }
250  
251 ## Version ()
252 # Prints out the version number and exits
253 sub Version { Exit('CONFIG', version($VERSION)) }
254  
255 ## Exit ( CODE, REASON )
256 # Exits the program cleanly with a proper error message
257 sub Exit {
258 my ($code, $reason, @details) = @_;
259 exit $ERROR{$code} unless $reason;
260 # map { $_ = " - $_" } @details;
261 $reason = join("\n", $reason, @details, '');
262 warn $reason if (!$QUIET && $reason);
263 exit $ERROR{$code};
264 }
265  
266 ## version ( [VERSION] )
267 # Returns the program name and version number
268 sub version {
269 my $version = shift || $VERSION || "unknown";
270 my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name
271 "$prog v$VERSION"
272 }
273  
274 ###############################################################################
275 ### Documentation #############################################################
276 ###############################################################################
277  
278 =head1 NAME
279  
280 kiboze.pl - a news archiving 'bot
281  
282 =head1 SYNOPSIS
283  
284 kiboze.pl [-hvd] [B<-V>] [B<-Q>] [B<-n> newsrcfile] [B<-s> newsserver]
285  
286 =head1 DESCRIPTION
287  
288 [...]
289  
290 =over 4
291  
292 =item -h
293  
294 Prints a short help message and exits.
295  
296 =item -v
297  
298 Prints the version number and exits.
299  
300 =back
301  
302 =head2 Recommended Usage
303  
304 After configuration, set up your crontab to run the script periodically.
305 A sample crontab line, which runs every four hours:
306  
307 # Run kiboze.pl
308 0 0,4,8,12,16,20 * * * /home/tskirvin/news/kiboze/kiboze.pl
309  
310 Disk space requirements will vary depending on how many groups you're
311 archiving; I've been archiving a local hierarchy (cmi.*) for about four
312 years now and have saved a few hundred megabytes worth of articles, but it
313 would be a lot worse to get a large hierarchy.
314  
315 =head2 NNTP Authentication
316  
317 NNTP Authentication is taken care of with B<Net::NNTP::Auth>, part of the
318 B<NewsLib> package. More information is there.
319  
320 =head1 NOTES
321  
322 =head1 TODO
323  
324 Make some default newsrc files if one doesn't exist. Locking
325 capabilities, perhaps.
326  
327 Trap ^C and such so that we can close nicely.
328  
329 =head1 REQUIREMENTS
330  
331 B<News::Archive>, B<News::Newsrc>
332  
333 =head1 SEE ALSO
334  
335 B<News::Archive>, B<kibozerc>
336  
337 =head1 AUTHOR
338  
339 Tim Skirvin <tskirvin@killfile.org>
340  
341 =head1 LICENSE
342  
343 This code may be redistributed under the same terms as Perl itself.
344  
345 =head1 HOMEPAGE
346  
347 http://www.killfile.org/~tskirvin/software/kiboze/
348  
349 http://www.killfile.org/~tskirvin/software/news-archive/
350  
351 =head1 COPYRIGHT
352  
353 Copyright 1997-2004, Tim Skirvin <tskirvin@killfile.org>
354  
355 =cut
356  
357 ###############################################################################
358 ### Version History ###########################################################
359 ###############################################################################
360 # v1.0 Thu Nov 16 15:05:08 CST 2000
361 ### Initial release.
362 # v1.0.1 Fri Jan 09 19:20:32 CST 2004
363 ### Minor bugfixes, documentation fixes. Updated to use Makefile.PL system.
364 # v1.10 Mon Apr 26 16:16:03 CDT 2004
365 ### Rewrite to use News::Archive. Working towards v2.0.
366 # v1.11 Thu Apr 29 10:06:48 CDT 2004
367 ### Keeps track of how many servers we've downloaded from.