Asterisk/Conf.pm

filter: Statementstmt 10.4% 20 / 191 Branchbran 1.0% 1 / 100 Conditioncond 4.1% 1 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 16.6% 3 / 18 Podpod 0.0% 0 / 13 totaltotal 6.9% 25 / 361 CC 67 SCAR 63.2
CC
67
Cov
7%
cgiform
1260.0
variablecheck
110.0
readconfig
90.0
CRAP 555.8
↑ Summary Asterisk/Conf/Zapata.pm »
1 package Asterisk::Conf;
2  
3 4 require 5.004;
4  
5 4 ▶use Asterisk;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 $VERSION = '0.01';
8  
9 4 $MULTISEP = ',';
10  
11 0 ▶sub version { $VERSION; }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
12  
13 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
14 1 my ($class, %args) = @_;
15 1 my $self = {};
16 1 $self->{configfile} = undef;
17 1 $self->{config} = {};
18 1 $self->{commit} = 0;
19 1 $self->{'contextorder'} = ();
20  
21 1 $self->{'variables'} = {};
22  
23 1 ▶ bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
24 1 return $self;
25 }
26  
27 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
28  
29 1 ▶sub configfile {
Subroutine configfile: called CC=2 SCAR=7.0
Pod undocumented
30 1 my($self, $configfile) = @_;
31  
32 1 ▶ if (defined($configfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configfile) 1 0 1
33 1 $self->{'configfile'} = $configfile;
34 }
35  
36 1 return $self->{'configfile'};
37 }
38  
39 0 ▶sub _setvar {
Subroutine _setvar: not called CC=3 SCAR=24.8
40 0 my ($self, $context, $var, $val, $order, $precomment, $postcomment) = @_;
41  
42 0 ▶ if (defined($self->{config}{$context}{$var}{val}) && ($self->{'variables'}{$var}{type} =~ /^multi/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'config'}{$context}{$var}{'val'} and $self->{'variables'}{$var}{'type'} =~ /^multi/) { } 0 0 0
Condition0%0/3
defined $self->{"config"}{$context}{$var}{"val"} and $self->{"variables"}{$var}{"type"} =~ /^multi/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"config"}{$context}{$var}{"val"} and $self->{"variables"}{$var}{"type"} =~ /^multi/
defined $self->{"config"}{$context}{$var}{"val"} $self->{"variables"}{$var}{"type"} =~ /^multi/
Truth table0%0/3
A and B
A= defined $self->{"config"}{$context}{$var}{"val"}B= $self->{"variables"}{$var}{"type"} =~ /^multi/
ABresult
0X0
100
111
43 0 $self->{config}{$context}{$var}{val} .= $MULTISEP . $val;
44 } else {
45 0 $self->{config}{$context}{$var}{val} = $val;
46 0 $self->{config}{$context}{$var}{precomment} = $precomment;
47 0 $self->{config}{$context}{$var}{postcomment} = $postcomment;
48 0 $self->{config}{$context}{$var}{order} = $order;
49 }
50  
51 }
52  
53 0 ▶sub _addcontext {
Subroutine _addcontext: not called CC=2 SCAR=17.9
54 0 my ($self, $context, $order) = @_;
55  
56 0 ▶ if (!defined($order)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $order) 0 0 0
57 0 $order = ($#{$self->{contextorder}} + 1);
58 }
59 0 $self->{contextorder}[$order] = $context;
60 }
61  
62 0 ▶sub _contextorder {
Subroutine _contextorder: not called CC=1 SCAR=6.9
63 0 my ($self) = @_;
64  
65 0 return @{$self->{contextorder}};
66 }
67  
68 0 ▶sub readconfig {
Subroutine readconfig: not called CC=9 SCAR=45.0
Pod undocumented
69 0 my ($self) = @_;
70  
71 0 my $context = '';
72 0 my $line = '';
73 0 my $precomment = '';
74 0 my $postcomment = '';
75  
76 0 my $configfile = $self->configfile();
77 0 my $order = 0;
78 0 my $contextorder =0;
79  
80 0 ▶ open(CF,"<$configfile") || die "Error loading $configfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open CF, "<$configfile" or die "Error loading ${configfile}: $!\n" 0 0 0
81 0 ▶ while ($line = <CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline CF)) 0 0 0
82 # chop($line);
83  
84 0 ▶ if ($line =~ /^;/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^;/) { } 0 0 0
elsif ($line =~ /(;.*)$/) { } 0 0 0
elsif ($line =~ /^\s*$/) { } 0 0 0
85 0 $precomment .= $line;
86 0 next;
87 } elsif ($line =~ /(;.*)$/) {
88 0 $postcomment .= $1;
89 0 $line =~ s/;.*$//;
90 } elsif ($line =~ /^\s*$/) {
91 0 $precomment = '';
92 0 $postcomment = '';
93 0 next;
94 }
95  
96 0 chop($line);
97 0 $line =~ s/\s*$//;
98  
99 0 ▶ if ($line =~ /^\[(\w+)\]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[(\w+)\]$/) { } 0 0 0
elsif ($line =~ /^(\w+)\s*[=>]+\s*(.*)/) { } 0 0 0
100 0 $context = $1;
101 0 $self->_addcontext($context, $contextorder);
102 0 $contextorder++;
103 } elsif ($line =~ /^(\w+)\s*[=>]+\s*(.*)/) {
104 0 $self->_setvar($context, $1, $2, $order, $precomment, $postcomment);
105 0 $precomment = '';
106 0 $postcomment = '';
107 0 $order++;
108 } else {
109 0 ▶ print STDERR "Unknown line: $line\n" if ($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
110 }
111 }
112 0 close(CF);
113 0 return %config;
114 }
115  
116 0 ▶sub rewriteconfig {
Subroutine rewriteconfig: not called CC=2 SCAR=17.9
Pod undocumented
117 0 my ($self) = @_;
118 0 my $file = $self->{'configfile'};
119  
120 0 my $fh;
121 0 ▶ open($fh, ">$file") || print "<br>OPEN FILE ERROR ($file) $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$file" or print "<br>OPEN FILE ERROR ($file) $!\n" 0 0 0
122 0 $self->writeconfig($fh);
123 0 close($fh);
124  
125 }
126  
127  
128 0 ▶sub writeconfig {
Subroutine writeconfig: not called CC=8 SCAR=42.8
Pod undocumented
129 0 my ($self, $fh) = @_;
130  
131 0 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
132 0 $fh = \*STDERR;
133 }
134  
135 0 foreach $context ($self->_contextorder) {
136 0 ▶ next if (!defined($self->{config}{$context}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{$context} 0 0 0
137 0 print $fh "[$context]\n";
138 0 foreach $key (keys %{$self->{config}{$context}}) {
139 0 ▶ next if (!$self->{config}{$context}{$key}{val});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{$context}{$key}{"val"} 0 0 0
140 0 print $fh $self->{config}{$context}{$key}{precomment};
141 0 my $val = $self->{config}{$context}{$key}{val};
142 0 ▶ if ($self->{'variables'}{$key}{type} =~ /^multi/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'variables'}{$key}{'type'} =~ /^multi/) { } 0 0 0
143 0 foreach (split(/$MULTISEP/, $val)) {
144 0 print $fh "$key => $_\n";
145 }
146 } else {
147 0 print $fh "$key => " . $val . "\n";
148 }
149 }
150 0 print $fh "\n";
151 }
152 }
153  
154 0 ▶sub setvariable {
Subroutine setvariable: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my ($self, $context, $var, $val) = @_;
156  
157 0 $self->{config}{$context}{$var}{val} = $val;
158 0 $self->{config}{$context}{$var}{postcomment} = ";Modified by Asterisk::Config::$self->{'name'}\n";
159  
160 }
161  
162 0 ▶sub variablecheck {
Subroutine variablecheck: not called CC=10 SCAR=47.0
Pod undocumented
163 0 my ($self, $context, $variable, $value) = @_;
164  
165 0 my $ret = 0;
166 0 my $regex = $self->{'variables'}{$variables}{'regex'};
167  
168 0 ▶ if (my $type = $self->{'variables'}{$variable}{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $self->{"variables"}{$variable}{"type"}) 0 0 0
169 0 ▶ if ($type =~ /^multitext$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /^multitext$/) { } 0 0 0
elsif ($type =~ /text$/) { } 0 0 0
elsif ($type eq 'one') { } 0 0 0
170 0 foreach $multiv (split($MULTISEP, $value)) {
171 0 ▶ if ($multiv =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiv =~ /$regex/) 0 0 0
172 0 $ret = 1;
173 }
174 }
175 } elsif ($type =~ /text$/) {
176 0 ▶ if ($value =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$regex/) 0 0 0
177 0 $ret = 1;
178 }
179 } elsif ($type eq 'one') {
180 0 foreach $item (@{$self->{'variables'}{$variable}{'values'}}) {
181 0 ▶ if ($item eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq $value) 0 0 0
182 0 $ret = 1;
183 }
184 }
185 }
186 }
187  
188 0 return $ret;
189 }
190  
191 0 ▶sub cgiform {
Subroutine cgiform: not called CC=35 SCAR=71.4
Pod undocumented
192 0 my ($self, $action, $context, %vars) = @_;
193 #valid actions: show, list, add, addform, modify, modifyform, delete, deleteform
194 0 my $html = '';
195  
196 0 my $module = $self->{'name'};
197 0 my $URL = $ENV{'SCRIPT_NAME'};
198  
199 0 $html .= "<!-- cgiform begin -->\n";
200  
201 0 ▶ if (!$action) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action) 0 0 0
202 0 $action = 'list';
203 }
204  
205 0 ▶ if (!$context && $action ne 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $context and $action ne "list") 0 0 0
Condition0%0/3
not $context and $action ne "list"
!ll&&!rl&&r
000
MC/DC0%0/2
not $context and $action ne "list"
not $context $action ne "list"
Truth table0%0/3
not(A) and B
A= not $contextB= $action ne "list"
ABresult
0X0
100
111
206 0 $html .= "<p>Context must be specified\n";
207 0 return $html;
208 }
209  
210  
211 #if this is an addform we need to ask for the contextname
212 0 ▶ if ($action =~ /(.*)form$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /(.*)form$/) 0 0 0
213 0 $html .= "<form method=\"post\">\n";
214 0 $html .= "<input type=\"hidden\" name=\"module\" value=\"$module\">\n";
215 0 $html .= "<input type=\"hidden\" name=\"context\" value=\"$context\">\n";
216 0 $html .= "<input type=\"hidden\" name=\"action\" value=\"$1\">\n";
217 }
218  
219 0 ▶ if ($action eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "list") 0 0 0
220 0 foreach $context (@{$self->{'contextorder'}}) {
221 0 $html .= "<a href=\"$URL?module=$module&action=show&context=$context\">Context $context</a>\n";
222 }
223 }
224  
225 0 ▶ if ($action eq 'deleteform') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'deleteform') { } 0 0 0
elsif ($action eq 'delete') { } 0 0 0
elsif ($action eq 'show' or $action =~ /^modify/ or $action =~ /^add/) { } 0 0 0
226 0 $html .= "<br>Are you sure you want to delete context $context?\n";
227 0 $html .= "<br><a href=\"$URL?module=$module&action=delete&context=$context&doit=1\">Confirm</a>\n";
228 } elsif ($action eq 'delete') {
229 0 ▶ if ($vars{'doit'} == 1 && $self->deletecontext($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{'doit'} == 1 and $self->deletecontext($context)) { } 0 0 0
Condition0%0/3
$vars{"doit"} == 1 and $self->deletecontext($context)
!ll&&!rl&&r
000
MC/DC0%0/2
$vars{"doit"} == 1 and $self->deletecontext($context)
$vars{"doit"} == 1 $self->deletecontext($context)
Truth table0%0/3
A and B
A= $vars{"doit"} == 1B= $self->deletecontext($context)
ABresult
0X0
100
111
230 0 $html .= "<br>Context $context has been deleted\n";
231 0 $self->{'commit'} = 1;
232 } else {
233 0 $html .= "<br>Unable to delete context $context\n";
234 }
235 ▶ } elsif ($action eq 'show' || $action =~ /^modify/ || $action =~ /^add/ ) {
Condition0%0/6
$action eq "show" or $action =~ /^modify/ or $action =~ /^add/
l!l&&r!l&&!r
000
$action eq "show" or $action =~ /^modify/
l!l&&r!l&&!r
000
MC/DC0%0/3
$action eq "show" or $action =~ /^modify/ or $action =~ /^add/
$action eq "show" $action =~ /^modify/ $action =~ /^add/
Truth table0%0/4
A or B or C
A= $action eq "show"B= $action =~ /^modify/C= $action =~ /^add/
ABCresult
1XX1
01X1
0011
0000
236  
237 0 ▶ if ($action eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'add') { } 0 0 0
elsif ($action eq 'show') { } 0 0 0
238 0 $self->_addcontext($context);
239 0 $self->{'commit'} = 1;
240 } elsif ($action eq 'show') {
241 0 $html .= "<a href=\"$URL?module=$module&action=addform\">Add new</a>\n";
242 0 $html .= "<a href=\"$URL?module=$module&action=modifyform&context=$context\">Modify</a>\n";
243 0 $html .= "<a href=\"$URL?module=$module&action=deleteform&context=$context\">Delete</a>\n";
244 }
245 0 foreach $var ( sort keys %{$self->{'variables'}} ) {
246 0 my $value = '';
247  
248 #the logic here seems backwards, but trust me its right
249 0 ▶ if (my $regex = $self->{'variables'}{$var}{'contextregex'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->{"variables"}{$var}{"contextregex"}) 0 0 0
250 0 ▶ if ($context !~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context =~ /$regex/) 0 0 0
251 0 next;
252 }
253 }
254 0 ▶ if (my $regex = $self->{'variables'}{$var}{'negcontextregex'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->{"variables"}{$var}{"negcontextregex"}) 0 0 0
255 0 ▶ if ($context =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context =~ /$regex/) 0 0 0
256 0 next;
257 }
258 }
259  
260 0 ▶ if ($self->{'config'}{$context}{$var}{'val'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{$context}{$var}{'val'}) { } 0 0 0
261 0 $value = $self->{'config'}{$context}{$var}{'val'};
262 } else {
263 0 $value = $self->{'variables'}{$var}{'default'};
264 }
265  
266 0 ▶ if ($action eq 'show') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'show') { } 0 0 0
elsif ($action =~ /(.*)form$/) { } 0 0 0
elsif ($action eq 'modify' or $action eq 'add') { } 0 0 0
267 0 $html .= "<br>$var: $value\n";
268 } elsif ($action =~ /(.*)form$/) {
269 0 my $subaction = $1;
270 0 my $fieldtype = $self->{'variables'}{$var}{'type'};
271 0 $html .= "<input type=\"hidden\" name=\"OLD$var\" value=\"$value\">\n";
272 0 ▶ if ($fieldtype =~ /text$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fieldtype =~ /text$/) { } 0 0 0
elsif ($fieldtype eq 'one') { } 0 0 0
273 0 $html .= "<br>$var: <input type=\"text\" name=\"VAR$var\" value=\"$value\">\n";
274 } elsif ($fieldtype eq 'one') {
275 0 $html .= "<br>$var: \n";
276 0 foreach $item (@{$self->{'variables'}{$var}{'values'}}) {
277 0 ▶ my $checked = 'checked' if ($item eq $value);
Branch: 1 branch
BranchTrueFalseTotal
if $item eq $value 0 0 0
278 0 $html .= "<input type=\"radio\" name=\"VAR$var\" value=\"$item\" $checked> $item\n";
279 }
280 }
281  
282 ▶ } elsif ($action eq 'modify' || $action eq 'add') {
Condition0%0/3
$action eq "modify" or $action eq "add"
l!l&&r!l&&!r
000
MC/DC0%0/2
$action eq "modify" or $action eq "add"
$action eq "modify" $action eq "add"
Truth table0%0/3
A or B
A= $action eq "modify"B= $action eq "add"
ABresult
1X1
011
000
283 0 ▶ if ($action eq 'add' || ($vars{"VAR$var"} ne $vars{"OLD$var"})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "add" or $vars{"VAR$var"} ne $vars{"OLD$var"}) 0 0 0
Condition0%0/3
$action eq "add" or $vars{"VAR$var"} ne $vars{"OLD$var"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$action eq "add" or $vars{"VAR$var"} ne $vars{"OLD$var"}
$action eq "add" $vars{"VAR$var"} ne $vars{"OLD$var"}
Truth table0%0/3
A or B
A= $action eq "add"B= $vars{"VAR$var"} ne $vars{"OLD$var"}
ABresult
1X1
011
000
284 0 my $newval = $vars{"VAR$var"};
285 #need to check for valid value here
286 0 $html .= "<!-- Variable has changed $var = $newval -->\n";
287 0 ▶ if ($self->variablecheck($context, $var, $newval)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->variablecheck($context, $var, $newval)) 0 0 0
288 0 $html .= "<br>SET VARIABLE $context $var=$newval\n";
289 0 $self->setvariable($context, $var, $newval);
290 0 $self->{'commit'} = 1;
291 }
292 }
293  
294 }
295  
296  
297  
298  
299 }
300  
301 }
302  
303 0 ▶ if ($action =~ /form$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /form$/) 0 0 0
304 0 $html .= "<br><input type=\nsubmit\n name=\"submit\">\n";
305 0 $html .= "</form>\n";
306 }
307  
308 0 ▶ if ($self->{'commit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"commit"}) 0 0 0
309 0 print "<br>Going to try to commit\n";
310 0 $self->rewriteconfig();
311 }
312 0 $html .= "<!-- cgiform end -->\n";
313  
314 0 return $html;
315 }
316  
317 0 ▶sub htmlheader {
Subroutine htmlheader: not called CC=2 SCAR=17.9
Pod undocumented
318 0 my ($self, $title) = @_;
319 0 ▶ $title = $self->{'description'} if (!defined($title));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 0 0
320 0 my $html = "<HTML><HEAD><TITLE>$title</TITLE></HEAD>\n";
321 0 $html .= "<BODY>\n";
322 0 return $html;
323 }
324  
325 0 ▶sub htmlfooter {
Subroutine htmlfooter: not called CC=1 SCAR=6.9
Pod undocumented
326 0 my ($self) = @_;
327 0 my $html = "</BODY></HTML>\n";
328 0 return $html;
329 }
330  
331 0 ▶sub deletecontext {
Subroutine deletecontext: not called CC=2 SCAR=17.9
Pod undocumented
332 0 my ($self, $context) = @_;
333  
334 0 ▶ if (delete($self->{'config'}{$context})) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{'config'}{$context}) { } 0 0 0
335 0 return 1;
336 } else {
337 0 return 0;
338 }
339 }
340  
341 0 ▶sub helptext {
Subroutine helptext: not called CC=1 SCAR=6.9
Pod undocumented
342 0 my ($self, $helpname) = @_;
343  
344 }
345  
346 4 1;
347  
↑ Summary Asterisk/Conf/Zapata.pm »