script/newsrecurse.pl untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 254 CC 14 SCAR 39.3
CC
14
Cov
0%
archive
132.0
Exit
12.0
wanted
6.0
CRAP 50.9
1 #!/usr/local/bin/perl
2 # -*- Perl -*- # Thu Apr 29 13:03:57 CDT 2004
3 ###############################################################################
4 # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2004, Tim Skirvin.
5 # Redistribution terms are below.
6 ###############################################################################
7 our $VERSION = "0.12";
8  
9 ###############################################################################
10 ### User Configuration ########################################################
11 ###############################################################################
12 use vars qw( $LOCALCONF $KIBOZEDIR $DB_TYPE $ARCHIVEGROUP $DEBUG $QUIET
13 $VERBOSE $MAX );
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 = "";
25  
26 ## What kind of database should we use to store history information?
27  
28 $DB_TYPE = "DB_File";
29  
30 ## If we're storing everything to an archive group, you can set it here.
31  
32 $ARCHIVEGROUP = "";
33  
34 ## Do we want to print debugging information? Use verbose status messages?
35 ## Be entirely silent? Set these things here.
36  
37 $DEBUG = 0;
38 $VERBOSE = 0;
39 $QUIET = 0;
40  
41 ## How many articles should we get, maximum? Set to '0' to just get all
42 ## of them.
43  
44 $MAX = 0;
45  
46 ## If the modules are set up in a non-standard place, edit this line
47 ## as appropriate.
48 BEGIN { use lib '/home/tskirvin/dev/news-archive'; }
49  
50 ###############################################################################
51 ### main() ####################################################################
52 ###############################################################################
53  
54 use strict; # Good programming is our friend
55 use warnings;
56  
57 use Getopt::Std; # Command line functions
58 use News::Archive; # News functions
59 use News::Article;
60 use File::Find;
61  
62 use vars qw/*name *dir *prune/;
63 *name = *File::Find::name;
64 *dir = *File::Find::dir;
65 *prune = *File::Find::prune;
66  
67 sub wanted;
68  
69 # Error Codes
70 our %ERROR = ( 'SUCCESS' => 0, 'CONFIG' => 1, 'SERVER' => 2 );
71  
72 # Command-line configuration
73 use vars qw( %OPTS );
74 getopts('hvVQdc:j:a:', \%OPTS);
75  
76 # Load local configuration from local configuration file
77 $LOCALCONF = $OPTS{'c'} if $OPTS{'c'};
78 if ( $LOCALCONF && -r $LOCALCONF ) { do $LOCALCONF }
79  
80 &Usage if $OPTS{'h'}; # Print usage information and exit
81 &Version if $OPTS{'v'}; # Print version information and exit
82  
83 # Other command-line parsing; overrides localconf
84 $DEBUG = $OPTS{'d'} if defined $OPTS{'d'};
85 $VERBOSE = $OPTS{'V'} || $DEBUG || $VERBOSE || 0; # Give verbose messages?
86 $QUIET = $OPTS{'Q'} || $QUIET || 0;
87 $ARCHIVEGROUP = $OPTS{'A'} if defined $OPTS{'A'};
88  
89 # Create the options hash to start a News::Archive object
90 our %OPTHASH;
91 $OPTHASH{ 'basedir' } = $KIBOZEDIR if $KIBOZEDIR;
92 $OPTHASH{ 'db_type' } = $DB_TYPE if $DB_TYPE;
93 $OPTHASH{ 'debug' } = $DEBUG if $DEBUG;
94  
95 # Create the News::Archive object
96 our $archive = new News::Archive ( %OPTHASH )
97 or Exit('SERVER', "Couldn't create/load archive item: ",
98 News::Archive->error);
99  
100 # If we've got $ARCHIVEGROUP set, then make sure we're subscribed to it.
101 $archive->subscribe($ARCHIVEGROUP) if ($ARCHIVEGROUP);
102  
103 my $directory = shift @ARGV || Usage();
104  
105 Exit('CONFIG', "No such directory: $directory") unless (-d $directory);
106  
107 $|++;
108  
109 our $COUNT = 0;
110 File::Find::finddepth({wanted => \&wanted}, $directory);
111 my $groups = $archive->activefile;
112 my $hash = $groups->groups;
113 # foreach (keys %{$hash}) { print "$_: $$hash{$_}\n"; }
114 # foreach (keys %{$groups}) { print "$_: $$groups{$_}\n"; }
115 $archive->close;
116 Exit('SUCCESS', "$COUNT articles archived");
117  
118 sub archive {
119 my ($file) = @_;
120 my $article = new News::Article; $article->read($file);
121 return undef unless $article;
122 my $messageid = $article->header('message-id');
123  
124 # We need at least this many more headers to do anything with it
125 foreach ( qw ( Message-ID from newsgroups subject date ) ) {
126 next if $article->header($_);
127 warn "No header $_\n" if $VERBOSE;
128 return undef;
129 }
130  
131 # If this article has already been processed, skip it
132 if ( $archive->article( $messageid ) ) {
133 warn "Already processed '$messageid'\n" if $VERBOSE;
134 return undef;
135 }
136
137 # Debugging hook; don't worry about this in general.
138 # $article->write(\*STDOUT);
139  
140 # Get the list of groups we're supposed to be saving the article into
141 my @groups = split('\s*,\s*', $article->header('newsgroups') );
142 map { s/\s+//g } @groups;
143  
144 # Make sure we're subscribed to all these groups
145 foreach (@groups) { $archive->subscribe($_) }
146 push @groups, $ARCHIVEGROUP if $ARCHIVEGROUP; # We're subscribed already
147  
148 # Actually save the article.
149 my $ret = $archive->save_article(
150 [ @{$article->rawheaders}, '', @{$article->body} ], @groups );
151 $COUNT++ if $ret;
152 if ($VERBOSE) { warn $ret ? "Accepted article $messageid\n"
153 : "Couldn't save article $messageid\n" }
154 $ret ? 1 : 0;
155 }
156  
157 ###############################################################################
158 ### Subroutines ###############################################################
159 ###############################################################################
160  
161 sub wanted {
162 my ($dev,$ino,$mode,$nlink,$uid,$gid);
163 return "" if ($MAX > 0 && $COUNT >= $MAX);
164 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && ! -d _ && /^[0-9]+$/s
165 && archive($name)
166 && ( print "$name\n" );
167 }
168  
169 ## Usage ()
170 # Prints out a short help file and exits.
171 sub Usage {
172 my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name
173 Exit('CONFIG', version($VERSION), <<EOM);
174 Archives news articles for later use with News::Archive
175  
176 $prog is part of the News::Archive package, which is used to archive
177 news articles in a reasonably efficient manner. This particular script
178 is meant for use for personal archives, loading articles from an mbox-style
179 archie on STDIN and loading them into @{[ $KIBOZEDIR || "(unset)" ]}.
180  
181 Configuration information is stored in the below configuration file;
182 please read the manual pages for more information on its configuration.
183  
184 Usage: $prog [-hvVQd] [-c configfile] [-a groupname] < ARTICLE
185 -h Print this usage information and exit.
186 -v Print version information and exit.
187 -c configfile Load this configuration file instead of default.
188 Default: $LOCALCONF
189 -a groupname Add this group to all articles we download, so
190 that they can be accessed uniformly.
191 Default: @{[ $ARCHIVEGROUP || "Not Set" ]}
192 -V Verbose; print information on every article.
193 Default: @{[ $VERBOSE ? "On" : "Off" ]}
194 -Q Quiet mode; only print that which is absolutely
195 necessary (and even then think about it).
196 Default: @{[ $QUIET ? "On" : "Off" ]}
197 -d Debug; print debugging information (implies -V)
198 Default: @{[ $DEBUG ? "On" : "Off" ]}
199 EOM
200 }
201  
202 ## Version ()
203 # Prints out the version number and exits
204 sub Version { Exit('CONFIG', version($VERSION)) }
205  
206 ## Exit ( CODE, REASON )
207 # Exits the program cleanly with a proper error message
208 sub Exit {
209 my ($code, $reason, @details) = @_;
210 exit $ERROR{$code} unless $reason;
211 # map { $_ = " - $_" } @details;
212 $reason = join("\n", $reason, @details, '');
213 warn $reason if (!$QUIET && $reason);
214 exit $ERROR{$code};
215 }
216  
217 ## version ( [VERSION] )
218 # Returns the program name and version number
219 sub version {
220 my $version = shift || $VERSION || "unknown";
221 my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name
222 "$prog v$VERSION"
223 }
224  
225 ###############################################################################
226 ### Documentation #############################################################
227 ###############################################################################
228  
229 =head1 NAME
230  
231 newsrecurse.pl - Recursively archives news articles with News::Archive
232  
233 =head1 SYNOPSIS
234  
235 newsrecurse.pl [-hvVQd] [-c configfile] [-a groupname] PATH
236  
237 =head1 DESCRIPTION
238  
239 newsrecurse.pl is a part of the News::Archive package, which is used to
240 archive news articles in a reasonably efficient manner. This particular
241 script is meant for use for loading past archives, loading them
242 recursively from all-numeric filenames in the directory structure at PATH
243 and loading them into $KIBOZEDIR.
244  
245 Configuration information is stored in the configuration file
246 (B<kibozerc>). Command-line options are as follows:
247  
248 =over 4
249  
250 =item -h
251  
252 Print this usage information and exit.
253  
254 =item -v
255  
256 Print version information and exit.
257  
258 =item -d
259  
260 Debug; print debugging information (implies -V)
261  
262 =item -c configfile
263  
264 Load this configuration file instead of default (~/.kibozerc).
265  
266 =item -a groupname
267  
268 Add this group to all articles we download, so that they can be accessed
269 uniformly.
270  
271 =item -V
272  
273 Verbose; print information on every article.
274  
275 =item -Q
276  
277 Quiet mode; only print that which is absolutely necessary (and even then
278 think about it).
279  
280 =back
281  
282 =head1 NOTES
283  
284 This was designed to work with the output of old kiboze.pl, which in turn
285 was compatible with the "classic" style INN file layout. If you've got an
286 old news spool sitting around, this is the script you can use to start
287 looking at it.
288  
289 =head1 REQUIREMENTS
290  
291 B<News::Archive>
292  
293 =head1 SEE ALSO
294  
295 B<kibozerc>, B<kiboze.pl>
296  
297 =head1 AUTHOR
298  
299 Tim Skirvin <tskirvin@killfile.org>
300  
301 =head1 LICENSE
302  
303 This code may be redistributed under the same terms as Perl itself.
304  
305 =head1 HOMEPAGE
306  
307 http://www.killfile.org/~tskirvin/software/news-archive/
308  
309 =head1 COPYRIGHT
310  
311 Copyright 2003-2004, Tim Skirvin <tskirvin@killfile.org>
312  
313 =cut
314  
315 ###############################################################################
316 ### Version History ###########################################################
317 ###############################################################################
318 # v0.11 Thu Apr 29 13:03:44 CDT 2004
319 ### First real version.
320 # v0.12 Tue May 25 14:40:26 CDT 2004
321 ### Offered some ways to only get a subset of the articles.