lib/News/Active.pm

filter: Statementstmt 19.8% 20 / 101 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 25.0% 5 / 20 Podpod 100% 15 / 15 totaltotal 17.1% 40 / 233 CC 44 SCAR 36.8
CC
44
Cov
11%
write
110.0
read
72.0
new
56.0
CRAP 39.6
« script/newsrecurse.pl ↑ Summary lib/News/GroupInfo.pm »
1 1 $VERSION = "0.13";
2 package News::Active;
3 1 our $VERSION = "0.13";
4  
5 # -*- Perl -*- Tue May 25 14:35:18 CDT 2004
6 ###############################################################################
7 # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2003-2004,
8 # Tim Skirvin. Redistribution terms are below.
9 ###############################################################################
10  
11 =head1 NAME
12  
13 News::Active - keep track of news active file information
14  
15 =head1 SYNOPSIS
16  
17 use News::Active;
18 my $active = News::Active->new( '/home/tskirvin/kiboze/active' );
19 $active->subscribe("humanities.philosophy.objectivism");
20 $active->add_article("humanities.philosophy.objectivism");
21 $active->write;
22  
23 See below for more information.
24  
25 =head1 DESCRIPTION
26  
27 News::Active is used to keep track of active newsgroup information in an
28 external file. It contains many C<News::Active::Entry> objects, one for
29 each newsgroup we are subscribed to. It should be a fairly simple module
30 to use and understand, as it is only a subsection of C<News::Archive>.
31  
32 =head1 USAGE
33  
34 =head2 Variables
35  
36 =over 4
37  
38 =item $News::Active::DEBUG - default value for C<debug()> in new objects.
39  
40 =item $News::Active::READONLY - default value for C<readonly()> in new objects.
41  
42 =back
43  
44 =cut
45  
46 ###############################################################################
47 ### main() ####################################################################
48 ###############################################################################
49  
50 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use News::Active::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Net::NNTP::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use vars qw( $DEBUG $READONLY );
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 $DEBUG = 0;
57 1 $READONLY = 0;
58  
59 =head2 Basic Functions
60  
61 The following functions give us access to the object class
62  
63 =over 4
64  
65 =item new ( FILE [, HASH] )
66  
67 Creates and returns a new News::Active object. C<FILE> is the filename
68 (later accessed with C<filename()>) that we will load old information from
69 and save to when we close the object. It will be created if it doesn't
70 already exist, and read (with C<read()>) from if it does.
71  
72 If C<HASH> is offered, its possible values:
73  
74 debug Print debugging information when using this
75 object. Defaults to $DEBUG.
76 readonly Don't write anything back out with this object
77 when we're done with it. Defaults to $READONLY.
78  
79 Returns undef on failure, or the object on success.
80  
81 =cut
82  
83 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
84 0 my ($proto, $file, %hash) = @_;
85 0 ▶ return undef unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
86 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
87 my $self = {
88 Groups => { },
89 FileName => $file,
90 Debug => $hash{'debug'} || $DEBUG || 0,
91 0 ▶ ReadOnly => $hash{'readonly'} || $READONLY || 0,
Condition0%0/10
$hash{'debug'} || $News::Active::DEBUG || 0
l!l
00
$hash{"debug"} or $News::Active::DEBUG
l!l&&r!l&&!r
000
$hash{'readonly'} || $News::Active::READONLY || 0
l!l
00
$hash{"readonly"} or $News::Active::READONLY
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash{'debug'} || $News::Active::DEBUG || 0
$hash{"debug"} $News::Active::DEBUG
MC/DC0%0/2
$hash{'readonly'} || $News::Active::READONLY || 0
$hash{"readonly"} $News::Active::READONLY
Truth table0%0/3
A or B
A= $hash{"debug"}B= $News::Active::DEBUG
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $hash{"readonly"}B= $News::Active::READONLY
ABresult
1X1
011
000
92 Changed => 0,
93 };
94 0 bless $self, $class;
95 0 $self->read($file);
96 0 $self;
97 }
98  
99 =item groups ()
100  
101 Returns a hash reference containing all subscribed newsgroups; the keys
102 are the group names, and the values are B<News::Active::Entry> objects.
103  
104 =item filename ()
105  
106 Returns the filename used for loading and saving our News::Active
107 information.
108  
109 =item debug ()
110  
111 Returns true if we want to print debugging information, false otherwise.
112  
113 =item readonly ()
114  
115 Returns true if we shouldn't write out the information later, false
116 otherwise.
117  
118 =cut
119  
120 0 ▶sub groups { shift->{Groups} || {} }
Subroutine groups: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"Groups"} or {} 0 0 0
121 0 ▶sub filename { shift->{FileName} || undef }
Subroutine filename: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"FileName"} or undef 0 0 0
122 0 ▶sub debug { shift->{Debug} || 0 }
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"Debug"} or 0 0 0 0
123 0 ▶sub readonly { shift->{ReadOnly} || 0 }
Subroutine readonly: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"ReadOnly"} or 0 0 0 0
124  
125 =item entry ( GROUP )
126  
127 Returns the News::Active::Entry object for C<GROUP>, or undef if none exists.
128  
129 =cut
130  
131 0 ▶sub entry {
Subroutine entry: not called CC=2 SCAR=17.9
Pod documented
132 0 my ($self, $group) = @_;
133 0 ▶ $self->groups->{$group} || undef
Branch: 1 branch
BranchTrueFalseTotal
$self->groups->{$group} or undef 0 0 0
134 }
135  
136 =item entries ( PATTERN )
137  
138 Returns an array of News::Active::Entry objects whose newsgroup names
139 match the given pattern C<PATTERN> (using C<Net::NNTP::Function::wildmat()>.
140  
141 =cut
142  
143 0 ▶sub entries {
Subroutine entries: not called CC=4 SCAR=30.0
Pod documented
144 0 my ($self, $pattern) = @_;
145 0 my @return;
146 0 foreach (sort keys %{$self->groups}) {
147 0 ▶ next unless wildmat($pattern || "*", $_);
Branch: 1 branch
BranchTrueFalseTotal
unless &wildmat(scalar($pattern || "*"), $_) 0 0 0
Condition0%0/2
$pattern || '*'
l!l
00
MC/DC0%0/1
$pattern || '*'
$pattern
Truth table0%0/2
A
A= $pattern
Aresult
11
00
148 0 push @return, $self->entry($_);
149 }
150 0 @return;
151 }
152  
153 =back
154  
155 =head2 Newsgroup Functions
156  
157 The following functions implement the functions that we actually want to
158 use this module for, ie adding groups and articles to the active file.
159  
160 =over 4
161  
162 =item subscribe ( GROUP )
163  
164 Adds a News::Active::Entry entry for the given C<GROUP>, thus adding it to
165 our subscription list. Returns 1 on success, undef otherwise.
166  
167 =cut
168  
169 0 ▶sub subscribe {
Subroutine subscribe: not called CC=4 SCAR=30.0
Pod documented
170 0 my ($self, $group) = @_;
171 0 ▶ return undef unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 0 0 0
172 0 ▶ return 1 if $self->subscribed($group);
Branch: 1 branch
BranchTrueFalseTotal
if $self->subscribed($group) 0 0 0
173 0 ▶ print "Subscribing to $group\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
174 0 $self->{Changed}++;
175 0 $self->groups->{$group} = new News::Active::Entry($group);
176 # warn "G2: $group ", $self->groups->{$group}, "\n";
177 # foreach (keys %{$self->groups}) { warn "G3: $_\n"; }
178 0 1;
179 }
180  
181 =item unsubscribe ( GROUP )
182  
183 Unsubscribe from C<GROUP> by making sure there is no News::Active::Entry
184 entry for that groupname. Returns 1 on success or if we were already
185 unsubscribed, undef otherwise.
186  
187 =cut
188  
189 0 ▶sub unsubscribe {
Subroutine unsubscribe: not called CC=4 SCAR=30.0
Pod documented
190 0 my ($self, $group) = @_;
191 0 ▶ return undef unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 0 0 0
192 0 ▶ return 1 unless $self->groups->{$group};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->groups->{$group} 0 0 0
193 0 ▶ print "Unsubscribing from $group\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
194 0 delete $self->groups->{$group};
195 0 $self->{Changed}++;
196 0 1;
197 }
198  
199 =item subscribed ( GROUP )
200  
201 Returns 1 if we are subscribed to C<GROUP>, 0 otherwise.
202  
203 =cut
204  
205 0 ▶sub subscribed {
Subroutine subscribed: not called CC=3 SCAR=24.8
Pod documented
206 0 my ($self, $group) = @_;
207 0 ▶ return 0 unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 0 0 0
208 0 ▶ $self->groups->{$group} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->groups->{$group} ? : 0 0 0
209 }
210  
211 =item add_article ( GROUP [, ARGS] )
212  
213 Invokes C<News::Active::add_article()> on the entry for C<GROUP>.
214  
215 =cut
216  
217 0 ▶sub add_article {
Subroutine add_article: not called CC=2 SCAR=17.9
Pod documented
218 0 my ($self, $group, @args) = @_;
219 0 ▶ my $entry = $self->entry($group) || return undef;
Condition0%0/2
$self->entry($group) || (return undef)
l!l
00
MC/DC0%0/1
$self->entry($group) || (return undef)
$self->entry($group)
Truth table0%0/2
A
A= $self->entry($group)
Aresult
11
00
220 0 $self->{Changed}++;
221 0 $entry->add_article(@args);
222 }
223  
224 =back
225  
226 =head2 Input/Output Functions
227  
228 The following functions are used for reading, displaying, and saving
229 information from News::Active and News::Active::Entry.
230  
231 =over 4
232  
233 =item read ( [FILE] )
234  
235 Reads News::Active::Entry information from C<FILE> (or the value of
236 C<file()>), populating the News::Active object. This file contains lines
237 that each contain the output from a single C<News::Active::Entry::output()>
238 call. Returns 1 on success, undef otherwise.
239  
240 =cut
241  
242 0 ▶sub read {
Subroutine read: not called CC=8 SCAR=42.8
Pod documented
243 0 my ($self, $file) = @_;
244 0 ▶ $file ||= $self->filename;
Condition0%0/3
$file ||= $self->filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$file ||= $self->filename
$file $self->filename
Truth table0%0/3
A ||= B
A= $fileB= $self->filename
ABresult
1X1
011
000
245 0 ▶ return undef unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
246 0 $$self{Groups} = {};
247 0 ▶ print "Reading from $file\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
248 0 ▶ open(FILE, $file) or (warn "Couldn't read from $file: $!\n" && return undef);
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or warn "Couldn't read from ${file}: $!\n" && (return undef) 0 0 0
Condition0%0/2
"Couldn't read from ${file}: $!\n" && (return undef)
!ll
00
MC/DC0%0/1
"Couldn't read from ${file}: $!\n" && (return undef)
"Couldn't read from ${file}: $!\n"
Truth table0%0/2
A
A= "Couldn't read from ${file}: $!\n"
Aresult
00
11
249 0 foreach (<FILE>) {
250 0 chomp;
251 0 ▶ my $entry = new News::Active::Entry($_) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = "News::Active::Entry"->new($_) or next 0 0 0
252 0 $self->groups->{$entry->name} = $entry;
253 }
254 0 close FILE;
255 0 $self->{Changed}++;
256 0 1;
257 }
258  
259 =item printable ()
260  
261 Returns an array (or arrayref, depending on invocation) containing the
262 value of C<News::Active::Entry::print()> on each entry within the
263 News::Active object. These are then suitable for printing.
264  
265 =cut
266  
267 0 ▶sub printable {
Subroutine printable: not called CC=3 SCAR=24.8
Pod documented
268 0 my ($self) = @_;
269 0 my @return;
270 0 foreach (sort keys %{$self->groups}) {
271 0 push @return, $self->entry($_)->print;
272 }
273 0 ▶ wantarray ? @return : join("\n", @return);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
274 }
275  
276 =item output ()
277  
278 Returns an array (or arrayref, depending on invocation) containing the
279 value of C<News::Active::Entry::output()> on each entry within the
280 News::Active object. These are then suitable for saving to a database and
281 later reloading.
282  
283 =cut
284  
285 0 ▶sub output {
Subroutine output: not called CC=3 SCAR=24.8
Pod documented
286 0 my ($self) = @_;
287 0 my @return;
288 0 foreach (sort keys %{$self->groups}) {
289 0 push @return, $self->entry($_)->output;
290 }
291 0 ▶ wantarray ? @return : join("\n", @return);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
292 }
293  
294 =item write ( [FILE] )
295  
296 Using the information from output(), writes out to C<FILE> (or the value
297 of C<file()>). Returns 1 on success, undef otherwise. If the readonly
298 flag is set, we don't actually write anything back out.
299  
300 Note that this function is called when the object is destroyed as well.
301  
302 =cut
303  
304 0 ▶sub write {
Subroutine write: not called CC=10 SCAR=47.0
Pod documented
305 0 my ($self, $file) = @_;
306 0 ▶ if ( !$self->{Changed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Changed"}) 0 0 0
307 0 ▶ warn "Nothing changed, not writing\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
308 0 return 1;
309 }
310 0 ▶ if ( $self->readonly ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->readonly) 0 0 0
311 0 ▶ warn "Not writing output, readonly!\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
312 0 return 1;
313 }
314 0 ▶ $file ||= $self->filename;
Condition0%0/3
$file ||= $self->filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$file ||= $self->filename
$file $self->filename
Truth table0%0/3
A ||= B
A= $fileB= $self->filename
ABresult
1X1
011
000
315 0 ▶ return undef unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
316 0 ▶ print "Writing to $file\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
317 0 ▶ open(FILE, ">$file")
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$file" or warn "Couldn't write to ${file}: $!\n" && (return undef) 0 0 0
Condition0%0/2
"Couldn't write to ${file}: $!\n" && (return undef)
!ll
00
MC/DC0%0/1
"Couldn't write to ${file}: $!\n" && (return undef)
"Couldn't write to ${file}: $!\n"
Truth table0%0/2
A
A= "Couldn't write to ${file}: $!\n"
Aresult
00
11
318 or (warn "Couldn't write to $file: $!\n" && return undef);
319 0 print FILE join("\n", $self->output);
320 0 close FILE;
321 0 $self->{Changed} = 0;
322 0 1;
323 }
324  
325 =back
326  
327 =cut
328  
329 ###############################################################################
330 ### Internal Functions ########################################################
331 ###############################################################################
332  
333 ### DESTROY
334 # Item destructor. Make sure the file is written back out.
335 # sub DESTROY { shift->write }
336  
337 1 1;
338  
339 =head1 NOTES
340  
341 This and C<News::Active> are fairly similar, but are meant to take care of
342 different types of information. The News::Active file is meant to be
343 modified regularly, every time a new article is added; in INN terms this
344 is the equivalent of the active file. The information in News::GroupInfo
345 is meant to only be modified occasionally, when something major changes;
346 in INN terms this is the equivalent of the newsgroups and active.times
347 files.
348  
349 =head1 TODO
350  
351 File locking would be nice.
352  
353 =head1 REQUIREMENTS
354  
355 C<Net::NNTP::Functions>
356  
357 =head1 SEE ALSO
358  
359 B<News::Archive>, B<News::GroupInfo>
360  
361 =head1 AUTHOR
362  
363 Tim Skirvin <tskirvin@killfile.org>
364  
365 =head1 HOMEPAGE
366  
367 B<http://www.killfile.org/~tskirvin/software/news-archive/>
368  
369 =head1 LICENSE
370  
371 This code may be redistributed under the same terms as Perl itself.
372  
373 =head1 COPYRIGHT
374  
375 Copyright 2003-2004, Tim Skirvin.
376  
377 =cut
378  
379 ###############################################################################
380 ### Version History ###########################################################
381 ###############################################################################
382 # v0.10 Wed Apr 28 10:12:40 CDT 2004
383 ### First documented version; it's been working since last year, though.
384 # v0.11 Wed Apr 28 11:06:26 CDT 2004
385 ### Added the matching stuff from Net::NNTP::Functions.
386 # v0.12 Tue May 25 11:21:03 CDT 2004
387 ### Added read-only stuff.
388 # v0.13 Tue May 25 14:34:25 CDT 2004
389 ### Doesn't automatically write on close anymore.
« script/newsrecurse.pl ↑ Summary lib/News/GroupInfo.pm »