blib/lib/Colloquy/Data.pm

filter: Statementstmt 73.0% 95 / 130 Branchbran 41.0% 23 / 56 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 82.3% 14 / 17 Podpod 33.3% 2 / 6 totaltotal 60.7% 138 / 227 CC 37 SCAR 37.7
CC
37
Cov
62%
_get_data
83.1
caps
12.0
_read_file
8.6
CRAP 43.4
↑ Summary
1 ############################################################
2 #
3 # $Id: Data.pm 526 2006-05-29 12:27:43Z nicolaw $
4 # Colloquy::Data - Read Colloquy 1.3 and 1.4 data files
5 #
6 # Copyright 2005,2006 Nicola Worthington
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 ############################################################
21  
22 package Colloquy::Data;
23 # vim:ts=4:sw=4:tw=78
24  
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use constant DEFAULT_DATADIR => '/usr/local/colloquy/data';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 $VERSION = '1.15' || sprintf('%d', q$Revision: 526 $ =~ /(\d+)/g);
35 2 ▶$DEBUG = $ENV{DEBUG} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DEBUG'} ? : 0 2 2
36 2 @ISA = qw(Exporter);
37 2 @EXPORT = ();
38 2 @EXPORT_OK = qw(&lists &users &caps &commify);
39 2 %EXPORT_TAGS = ( all => \@EXPORT_OK );
40  
41 1 ▶sub users {
Subroutine users: called CC=1 SCAR=0.0
Pod documented
42 1 return _get_data(shift);
43 }
44  
45 1 ▶sub lists {
Subroutine lists: called CC=1 SCAR=0.0
Pod documented
46 1 my ($users,$lists) = _get_data(shift);
47 1 return ($lists,$users);
48 }
49  
50 0 ▶sub caps {
Subroutine caps: not called CC=3 SCAR=24.8
Pod undocumented
51 0 (my $c = $_[0]) =~ s/_/ /g;
52 0 my @c = split(/\b/,$c);
53 0 ▶ foreach (@c) { if (/^([a-z])(.*)/) { $_ = uc($1).$2; } }
Branch: 1 branch
BranchTrueFalseTotal
if (/^([a-z])(.*)/) 0 0 0
54 0 return join("",@c);
55 }
56  
57 0 ▶sub commify {
Subroutine commify: not called CC=2 SCAR=17.9
Pod undocumented
58 0 local $_ = shift;
59 0 s/^\s+|\s+$//g;
60 0 ▶ 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
Branch: 1 branch
BranchTrueFalseTotal
s/^([-+]?\d+)(\d{3})/$1,$2/ and do { '???' } 0 0 0
61 0 return $_;
62 }
63  
64 4 ▶sub _munge_user_lua {
Subroutine _munge_user_lua: called CC=1 SCAR=0.0
65 4 local $_ = shift;
66 4 s/'/\\'/g;
67 4 s/"/'/g; #"'
68 4 s/(\s+[a-z0-9]+\s+=)(\s+['{\d+])/$1>$2/gi;
69 4 s/^return //;
70 4 return $_;
71 }
72  
73 6 ▶sub _munge_list_lua {
Subroutine _munge_list_lua: called CC=1 SCAR=0.0
74 6 local $_ = shift;
75 6 s/\s+\['(\S+?)'\]\s+=\s+{/ $1 => {/g;
76 6 s/'/\\'/g;
77 6 s/"/'/g; #"'
78 6 s/(\s+[a-z0-9]+\s+=)(\s+['{\d+])/$1>$2/gi;
79 6 s/(\s+members\s+=>\s+)\{(.+?)\}/$1 [ ( $2 ) ]/sgi;
80 6 s/^return //;
81 6 return $_;
82 }
83  
84 10 ▶sub _read_file {
Subroutine _read_file: called CC=7 SCAR=21.5
85 10 my $file = shift;
86 10 ▶ croak "No such file '$file'\n" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 10 10
87 10 ▶ croak "'$file' is not a plain file type\n" unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 10 10
88 10 ▶ croak "Insufficient permissions to read file '$file'\n" unless -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -r _ 0 10 10
89  
90 10 my $mode = (stat(_))[2];
91 10 my $group_write = ($mode & S_IWGRP) >> 3;
92 10 my $other_write = $mode & S_IWOTH;
93  
94 # Since this module started using Safe to parse the data files,
95 # this code is no longer as important as before. It's now only
96 # a warning.
97 # if ($^W && $group_write) {
98 # cluck "WARNING! $file is group writeable. This is potentially insecure!";
99 # }
100 #if ($other_write) {
101 10 ▶ if ($^W && $other_write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W and $other_write) 0 10 10
Condition33%1/3
$^W and $other_write
!ll&&!rl&&r
1000
MC/DC0%0/2
$^W and $other_write
$^W $other_write
Truth table33%1/3
A and B
A= $^WB= $other_write
ABresult
0X0
100
111
102 #croak "FATAL! $file is world writeable. This insecure file cannot be evaluated!";
103 0 cluck "WARNING! $file is world writeable. This is potentially insecure!";
104 }
105  
106 10 ▶ if (open(FH,"<$file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open FH, "<$file") { } 10 0 10
107 10 local $/ = undef;
108 10 my $data = <FH>;
109 10 close(FH);
110 10 return $data;
111 } else {
112 0 croak "Unable to open file handle FH for file '$file': $!";
113 # return undef;
114 }
115 }
116  
117 2 ▶sub _get_data {
Subroutine _get_data: called CC=26 SCAR=44.2
118 2 ▶ my $datadir = shift || DEFAULT_DATADIR;
Condition50%1/2
shift() || '/usr/local/colloquy/data'
l!l
20
MC/DC0%0/1
shift() || '/usr/local/colloquy/data'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
119 2 ▶ my $users_lua = $datadir.'/users'.(-f $datadir.'/users.lua' ? '.lua' : '');
Branch: 1 branch
BranchTrueFalseTotal
-f $datadir . '/users.lua' ? : 0 2 2
120 2 ▶ my $lists_lua = $datadir.'/lists'.(-f $datadir.'/lists.lua' ? '.lua' : '');
Branch: 1 branch
BranchTrueFalseTotal
-f $datadir . '/lists.lua' ? : 0 2 2
121  
122 2 my $users = {};
123 2 ▶ croak "Insufficient permissions to read $users_lua\n" unless -r $users_lua;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $users_lua 0 2 2
124  
125 2 my $c = new Safe;
126 # Minimum safe opcode set for building data structures lineseq, list and
127 # padany needed for perl 5.8.7
128 2 $c->permit_only(qw(rv2sv sassign aelem aelemfast helem anonlist anonhash
129 pushmark refgen const undef leaveeval lineseq list padany));
130  
131 2 ▶ if (-f $users_lua) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $users_lua) { } 0 2 2
elsif (-d $users_lua) { } 2 0 2
132 0 my $coderef = _munge_user_lua( '$' . _read_file($users_lua) );
133 0 $users = $c->reval($coderef);
134 #eval $coderef;
135  
136 } elsif (-d $users_lua) {
137 2 ▶ if (opendir(DH,$users_lua)) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DH, $users_lua) { } 2 0 2
138 2 for my $user (grep(!/^\./,readdir(DH))) {
139 4 ▶ next unless -f "$users_lua/$user";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$users_lua/$user" 0 4 4
140 4 ▶ unless (-r "$users_lua/$user") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r "$users_lua/$user") 0 4 4
141 0 cluck "Insufficient permissions to read $users_lua/$user";
142 0 next;
143 }
144 4 my $coderef = _munge_user_lua( _read_file("$users_lua/$user") );
145 4 ▶ if (length($coderef) > 9 && $coderef =~ /^\s*(return )?{.+}\s*$/gsi) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig) { } 4 0 4
Condition33%1/3
length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig
!ll&&!rl&&r
004
MC/DC0%0/2
length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig
length $coderef > 9 $coderef =~ /^\s*(return )?{.+}\s*$/sig
Truth table33%1/3
A and B
A= length $coderef > 9B= $coderef =~ /^\s*(return )?{.+}\s*$/sig
ABresult
0X0
100
111
146 # if (length($coderef) > 9 && $coderef =~ /return {.+}/gsi) {
147 4 DUMP('$coderef',$coderef);
148 4 $users->{$user} = $c->reval($coderef);
149 4 DUMP('$users',$users);
150 #eval { $users->{$user} = eval $coderef; }
151 } else {
152 0 cluck "Caught known Colloquy data file corruption for user $user";
153 }
154 }
155 2 closedir(DH);
156 } else {
157 0 croak "Failed to open file handle DH for directory '$users_lua': $!";
158 }
159 }
160  
161 2 my $lists = {};
162 2 ▶ croak "Insufficient permissions to read $lists_lua\n" unless -r $lists_lua;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $lists_lua 0 2 2
163  
164 2 ▶ if (-f $lists_lua) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $lists_lua) { } 0 2 2
elsif (-d $lists_lua) { } 2 0 2
165 0 my $coderef = _munge_list_lua( '$' . _read_file($lists_lua) );
166 0 $lists = $c->reval($coderef);
167 #eval $coderef;
168  
169 } elsif (-d $lists_lua) {
170 2 ▶ if (opendir(DH,$lists_lua)) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DH, $lists_lua) { } 2 0 2
171 2 for my $list (grep(!/^\./,readdir(DH))) {
172 6 ▶ next unless -f "$lists_lua/$list";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$lists_lua/$list" 0 6 6
173 6 ▶ unless (-r "$lists_lua/$list") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r "$lists_lua/$list") 0 6 6
174 0 cluck "Insufficient permissions to read $lists_lua/$list";
175 0 next;
176 }
177 6 my $coderef = _munge_list_lua( _read_file("$lists_lua/$list") );
178 6 ▶ if (length($coderef) > 9 && $coderef =~ /^\s*(return )?{.+}\s*$/gsi) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig) { } 6 0 6
Condition33%1/3
length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig
!ll&&!rl&&r
006
MC/DC0%0/2
length $coderef > 9 and $coderef =~ /^\s*(return )?{.+}\s*$/sig
length $coderef > 9 $coderef =~ /^\s*(return )?{.+}\s*$/sig
Truth table33%1/3
A and B
A= length $coderef > 9B= $coderef =~ /^\s*(return )?{.+}\s*$/sig
ABresult
0X0
100
111
179 # if (length($coderef) > 9 && $coderef =~ /return {.+}/gsi) {
180 6 DUMP('$coderef',$coderef);
181 6 $lists->{$list} = $c->reval($coderef);
182 6 DUMP('$lists',$lists);
183 #$lists->{$list} = eval $coderef;
184 } else {
185 0 cluck "Caught known Colloquy data file corruption for list $list";
186 }
187 }
188 2 closedir(DH);
189 } else {
190 0 croak "Failed to open file handle DH for directory '$lists_lua': $!";
191 }
192 }
193  
194 2 for my $list (keys %{$lists}) {
195 6 for my $member (@{$lists->{$list}->{members}}) {
196 0 ▶ $users->{$member}->{lists} = [] unless exists $users->{$member}->{lists};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $users->{$member}{"lists"} 0 0 0
197 0 ▶ $lists->{$list}->{users} = [] unless exists $lists->{$list}->{users};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lists->{$list}{"users"} 0 0 0
198 0 push @{$users->{$member}->{lists}},$list;
199 0 push @{$lists->{$list}->{users}},$member;
200 }
201 }
202  
203 2 return ($users,$lists);
204 }
205  
206 0 ▶sub TRACE {
Subroutine TRACE: not called CC=2 SCAR=17.9
Pod undocumented
207 0 ▶ return unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Colloquy::Data::DEBUG 0 0 0
208 0 warn(shift());
209 }
210  
211 20 ▶sub DUMP {
Subroutine DUMP: called CC=2 SCAR=11.6
Pod undocumented
212 20 ▶ return unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Colloquy::Data::DEBUG 20 0 20
213 0 eval {
214 0 require Data::Dumper;
215 0 warn(shift().': '.Data::Dumper::Dumper(shift()));
216 }
217 }
218  
219 2 1;
220  
221 =pod
222  
223 =head1 NAME
224  
225 Colloquy::Data - Read Colloquy 1.3 and 1.4 data files
226  
227 =head1 SYNOPSIS
228  
229 use Data::Dumper;
230 use Colloquy::Data qw(:all);
231
232 my $colloquy_datadir = "/home/system/colloquy/data";
233
234 #my ($users_hashref,$lists_hashref) = users($colloquy_datadir);
235 my ($lists_hashref,$users_hashref) = lists($colloquy_datadir);
236
237 print "Users: ".Dumper($users);
238 print "Lists: ".Dumper($lists);
239  
240 =head1 DESCRIPTION
241  
242 This module munges the users.lua and lists.lua (Colloquy 1.3x) files
243 in to executable perl code which is then evaluated. Colloquy 1.4 uses
244 a seperate LUA file for each user and list, which are located in the
245 users and lists directories in the Colloquy data directory. These files
246 are read one by one and evaluated in the same way.
247  
248 This module compiles and execute the Colloquy data files in restricted
249 compartments using the L<Safe> module. Even so, this module should be
250 used with caution if you cannot gaurentee the integrity of the user and
251 list LUA files. The module will issue a warning complaining about world
252 writable permissions if $^W warnings.
253  
254 =head1 EXPORTS
255  
256 =head2 users
257  
258 my ($users_hashref,$lists_hashref) = users($colloquy_datadir);
259  
260 Returns users and lists hash references, in that order.
261  
262 =head2 lists
263  
264 my ($lists_hashref,$users_hashref) = lists($colloquy_datadir);
265  
266 Returns lists and users hash references, in that order.
267  
268 =head1 SEE ALSO
269  
270 L<http://freshmeat.net/projects/colloquy-talker/>, L<Apache2::AuthColloquy>
271  
272 =head1 VERSION
273  
274 $Id: Data.pm 526 2006-05-29 12:27:43Z nicolaw $
275  
276 =head1 AUTHOR
277  
278 Nicola Worthington <nicolaw@cpan.org>
279  
280 L<http://perlgirl.org.uk>
281  
282 =head1 COPYRIGHT
283  
284 Copyright 2005,2006 Nicola Worthington.
285  
286 This software is licensed under The Apache Software License, Version 2.0.
287  
288 L<http://www.apache.org/licenses/LICENSE-2.0>
289  
290 =cut
291  
292 __END__
↑ Summary