lib/News/GroupInfo.pm

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