ConfigFile/Simple.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 198 CC 14 SCAR 26.3
CC
14
Cov
0%
check
42.0
_nextline
20.0
include_file
12.0
CRAP 13.8
1 # -*- perl -*-
2  
3 # Copyright (c) 2008 by AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2008-Dec-19 10:12 (EST)
6 # Function: read simple config file
7 #
8 # $Id: Simple.pm,v 1.11 2010/10/30 20:27:56 jaw Exp $
9  
10 # file:
11 # keyword value
12 # ...
13  
14 package AC::ConfigFile::Simple;
15 use AC::Misc;
16 use AC::DC::Debug;
17 use Socket;
18 use strict;
19  
20 my $MINSTAT = 15;
21  
22 my %CONFIG = (
23 include => \&include_file,
24 debug => \&parse_debug,
25 allow => \&parse_allow,
26 _default => \&parse_keyvalue,
27 );
28  
29  
30 sub new {
31 my $class = shift;
32 my $file = shift;
33  
34 my $me = bless {
35 _laststat => $^T,
36 _lastconf => $^T,
37 _configfile => $file,
38 _files => [ ],
39 @_,
40 }, $class;
41  
42 $me->_read();
43 return $me;
44 }
45  
46 sub check {
47 my $me = shift;
48  
49 my $now = $^T;
50 return if $now - $me->{_laststat} < $MINSTAT;
51 $me->{_laststat} = $now;
52  
53 my $changed;
54 for my $file ( @{$me->{_files}} ){
55 my $mtime = (stat($file))[9];
56 $changed = 1 if $mtime > $me->{_lastconf};
57 }
58 return unless $changed;
59  
60 verbose("config file changed. reloading");
61 $me->{_lastconf} = $now;
62  
63 eval {
64 $me->_read();
65 verbose("installed new config file");
66 if( my $f = $me->{onreload} ){
67 $f->();
68 }
69 };
70 if(my $e = $@){
71 problem("error reading new config file: $e");
72 return;
73 }
74  
75 return 1;
76 }
77  
78 sub _read {
79 my $me = shift;
80  
81 delete $me->{_pending};
82  
83 $me->_readfile($me->{_configfile});
84  
85 $me->{config} = $me->{_pending};
86 delete $me->{_pending};
87 }
88  
89 sub _readfile {
90 my $me = shift;
91 my $file = shift;
92  
93 my $fd;
94 open($fd, $file) || die "cannot open file '$file': $!";
95 $me->{fd} = $fd;
96  
97 push @{$me->{_files}}, $file;
98  
99 while( defined(my $l = $me->_nextline()) ){
100 my($key, $rest) = split /\s+/, $l, 2;
101 $me->handle_config( $key, $rest ) || die "invalid config '$key'\n";
102 }
103  
104 close $fd;
105 }
106  
107 sub handle_config {
108 my $me = shift;
109 my $key = shift;
110 my $rest = shift;
111  
112 my $fnc = $CONFIG{$key} || $CONFIG{_default};
113 return unless $fnc;
114 $fnc->($me, $key, $rest);
115 return 1;
116 }
117  
118 sub _nextline {
119 my $me = shift;
120  
121 my $line;
122 while(1){
123 my $fd = $me->{fd};
124  
125 my $l = <$fd>;
126 return $line unless defined $l;
127 chomp $l;
128  
129 $l =~ s/\#.*$//;
130 $l =~ s/^\s*//;
131 $l =~ s/\s+$//;
132 next if $l =~ s/^\s*$/; #/;
133 $line .= $l;
134  
135 if( $line =~ /\\$/ ){
136 chop $line;
137 next;
138 }
139 return $line;
140 }
141 }
142  
143 ################################################################
144  
145 sub include_file {
146 my $me = shift;
147 my $key = shift;
148 my $file = shift;
149  
150 $file =~ s/^"(.*)"$/$1/;
151  
152 if( $file !~ m|^/| ){
153 # add path from main config file
154 my($path) = $me->{_configfile} =~ m|(.*)/[^/]+$|;
155 $file = "$path/$file" if $path;
156 }
157  
158 my $fd = $me->{fd};
159 $me->_readfile($file);
160 $me->{fd} = $fd;
161 }
162  
163 sub parse_keyvalue {
164 my $me = shift;
165 my $key = shift;
166 my $value = shift;
167  
168 problem("parameter '$key' redefined") if $me->{_pending}{$key};
169 $me->{_pending}{$key} = $value;
170 }
171  
172 sub parse_keyarray {
173 my $me = shift;
174 my $key = shift;
175 my $value = shift;
176  
177 push @{$me->{_pending}{$key}}, $value;
178 }
179  
180 sub parse_allow {
181 my $me = shift;
182 my $key = shift;
183 my $acl = shift;
184  
185 my($host, $len) = split m|/|, $acl;
186 $host ||= $acl;
187 $len ||= 32;
188  
189 push @{$me->{_pending}{acl}}, [ inet_aton($host), inet_lton($len) ];
190 }
191  
192 sub parse_debug {
193 my $me = shift;
194 my $key = shift;
195 my $value = shift;
196  
197 $me->{_pending}{debug}{$value} = 1;
198 }
199  
200  
201 ################################################################
202  
203 sub config {
204 my $me = shift;
205 return $me->{config};
206 }
207  
208 sub get {
209 my $me = shift;
210 my $k = shift;
211  
212 return $me->{config}{$k};
213 }
214  
215 sub check_acl {
216 my $me = shift;
217 my $ip = shift; # ascii
218  
219 my $ipn = inet_aton($ip);
220 for my $acl ( @{$me->{config}{acl}} ){
221 my($net, $mask) = @$acl;
222 return 1 if ($ipn & $mask) eq $net;
223 }
224  
225 return 0;
226 }
227  
228  
229 1;