| lib/Asterisk/Config.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 16 | 187 | 8.5 |
| branch | 1 | 98 | 1.0 |
| condition | 1 | 24 | 4.1 |
| subroutine | 3 | 18 | 16.6 |
| pod | 0 | 13 | 0.0 |
| total | 21 | 340 | 6.1 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Asterisk::Config; | ||||||
| 2 | |||||||
| 3 | require 5.004; | ||||||
| 4 | |||||||
| 5 | 4 | 4 | 813 | use Asterisk; | |||
| 4 | 5 | ||||||
| 4 | 7308 | ||||||
| 6 | |||||||
| 7 | $VERSION = '0.01'; | ||||||
| 8 | |||||||
| 9 | $MULTISEP = ','; | ||||||
| 10 | |||||||
| 11 | 0 | 0 | 0 | 0 | sub version { $VERSION; } | ||
| 12 | |||||||
| 13 | sub new { | ||||||
| 14 | 1 | 1 | 0 | 668 | my ($class, %args) = @_; | ||
| 15 | 1 | 3 | my $self = {}; | ||||
| 16 | 1 | 1 | $self->{configfile} = undef; | ||||
| 17 | 1 | 2 | $self->{config} = {}; | ||||
| 18 | 1 | 2 | $self->{commit} = 0; | ||||
| 19 | 1 | 2 | $self->{'contextorder'} = (); | ||||
| 20 | |||||||
| 21 | 1 | 1 | $self->{'variables'} = {}; | ||||
| 22 | |||||||
| 23 | 1 | 33 | 8 | bless $self, ref $class || $class; | |||
| 24 | 1 | 2 | return $self; | ||||
| 25 | } | ||||||
| 26 | |||||||
| 27 | 0 | sub DESTROY { } | |||||
| 28 | |||||||
| 29 | sub configfile { | ||||||
| 30 | 1 | 1 | 0 | 1146 | my($self, $configfile) = @_; | ||
| 31 | |||||||
| 32 | 1 | 50 | 5 | if (defined($configfile)) { | |||
| 33 | 1 | 5 | $self->{'configfile'} = $configfile; | ||||
| 34 | } | ||||||
| 35 | |||||||
| 36 | 1 | 6 | return $self->{'configfile'}; | ||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | sub _setvar { | ||||||
| 40 | 0 | 0 | my ($self, $context, $var, $val, $order, $precomment, $postcomment) = @_; | ||||
| 41 | |||||||
| 42 | 0 | 0 | 0 | if (defined($self->{config}{$context}{$var}{val}) && ($self->{'variables'}{$var}{type} =~ /^multi/)) { | |||
| 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 | sub _addcontext { | ||||||
| 54 | 0 | 0 | my ($self, $context, $order) = @_; | ||||
| 55 | |||||||
| 56 | 0 | 0 | if (!defined($order)) { | ||||
| 57 | 0 | $order = ($#{$self->{contextorder}} + 1); | |||||
| 0 | |||||||
| 58 | } | ||||||
| 59 | 0 | $self->{contextorder}[$order] = $context; | |||||
| 60 | } | ||||||
| 61 | |||||||
| 62 | sub _contextorder { | ||||||
| 63 | 0 | 0 | my ($self) = @_; | ||||
| 64 | |||||||
| 65 | 0 | return @{$self->{contextorder}}; | |||||
| 0 | |||||||
| 66 | } | ||||||
| 67 | |||||||
| 68 | sub readconfig { | ||||||
| 69 | 0 | 0 | 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 | 0 | open(CF,"<$configfile") || die "Error loading $configfile: $!\n"; | ||||
| 81 | 0 | while ($line = |
|||||
| 82 | # chop($line); | ||||||
| 83 | |||||||
| 84 | 0 | 0 | if ($line =~ /^;/) { | ||||
| 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 | 0 | if ($line =~ /^\[(\w+)\]$/) { | ||||
| 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 | 0 | print STDERR "Unknown line: $line\n" if ($DEBUG); | ||||
| 110 | } | ||||||
| 111 | } | ||||||
| 112 | 0 | close(CF); | |||||
| 113 | 0 | return %config; | |||||
| 114 | } | ||||||
| 115 | |||||||
| 116 | sub rewriteconfig { | ||||||
| 117 | 0 | 0 | 0 | my ($self) = @_; | |||
| 118 | 0 | my $file = $self->{'configfile'}; | |||||
| 119 | |||||||
| 120 | 0 | my $fh; | |||||
| 121 | 0 | 0 | open($fh, ">$file") || print " OPEN FILE ERROR ($file) $!\n"; |
||||
| 122 | 0 | $self->writeconfig($fh); | |||||
| 123 | 0 | close($fh); | |||||
| 124 | |||||||
| 125 | } | ||||||
| 126 | |||||||
| 127 | |||||||
| 128 | sub writeconfig { | ||||||
| 129 | 0 | 0 | 0 | my ($self, $fh) = @_; | |||
| 130 | |||||||
| 131 | 0 | 0 | if (!$fh) { | ||||
| 132 | 0 | $fh = \*STDERR; | |||||
| 133 | } | ||||||
| 134 | |||||||
| 135 | 0 | foreach $context ($self->_contextorder) { | |||||
| 136 | 0 | 0 | next if (!defined($self->{config}{$context})); | ||||
| 137 | 0 | print $fh "[$context]\n"; | |||||
| 138 | 0 | foreach $key (keys %{$self->{config}{$context}}) { | |||||
| 0 | |||||||
| 139 | 0 | 0 | next if (!$self->{config}{$context}{$key}{val}); | ||||
| 140 | 0 | print $fh $self->{config}{$context}{$key}{precomment}; | |||||
| 141 | 0 | my $val = $self->{config}{$context}{$key}{val}; | |||||
| 142 | 0 | 0 | if ($self->{'variables'}{$key}{type} =~ /^multi/) { | ||||
| 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 | sub setvariable { | ||||||
| 155 | 0 | 0 | 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 | sub variablecheck { | ||||||
| 163 | 0 | 0 | 0 | my ($self, $context, $variable, $value) = @_; | |||
| 164 | |||||||
| 165 | 0 | my $ret = 0; | |||||
| 166 | 0 | my $regex = $self->{'variables'}{$variables}{'regex'}; | |||||
| 167 | |||||||
| 168 | 0 | 0 | if (my $type = $self->{'variables'}{$variable}{'type'}) { | ||||
| 169 | 0 | 0 | if ($type =~ /^multitext$/) { | ||||
| 0 | |||||||
| 0 | |||||||
| 170 | 0 | foreach $multiv (split($MULTISEP, $value)) { | |||||
| 171 | 0 | 0 | if ($multiv =~ /$regex/) { | ||||
| 172 | 0 | $ret = 1; | |||||
| 173 | } | ||||||
| 174 | } | ||||||
| 175 | } elsif ($type =~ /text$/) { | ||||||
| 176 | 0 | 0 | if ($value =~ /$regex/) { | ||||
| 177 | 0 | $ret = 1; | |||||
| 178 | } | ||||||
| 179 | } elsif ($type eq 'one') { | ||||||
| 180 | 0 | foreach $item (@{$self->{'variables'}{$variable}{'values'}}) { | |||||
| 0 | |||||||
| 181 | 0 | 0 | if ($item eq $value) { | ||||
| 182 | 0 | $ret = 1; | |||||
| 183 | } | ||||||
| 184 | } | ||||||
| 185 | } | ||||||
| 186 | } | ||||||
| 187 | |||||||
| 188 | 0 | return $ret; | |||||
| 189 | } | ||||||
| 190 | |||||||
| 191 | sub cgiform { | ||||||
| 192 | 0 | 0 | 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 .= "\n"; | |||||
| 200 | |||||||
| 201 | 0 | 0 | if (!$action) { | ||||
| 202 | 0 | $action = 'list'; | |||||
| 203 | } | ||||||
| 204 | |||||||
| 205 | 0 | 0 | 0 | if (!$context && $action ne 'list') { | |||
| 206 | 0 | $html .= " 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 | 0 | if ($action =~ /(.*)form$/) { | ||||
| 213 | 0 | $html .= " | |||||
| 214 | 0 | $html .= "\n"; | |||||
| 215 | 0 | $html .= "\n"; | |||||
| 216 | 0 | $html .= "\n"; | |||||
| 217 | } | ||||||
| 218 | |||||||
| 219 | 0 | 0 | if ($action eq 'list') { | ||||
| 220 | 0 | foreach $context (@{$self->{'contextorder'}}) { | |||||
| 0 | |||||||
| 221 | 0 | $html .= "Context $context\n"; | |||||
| 222 | } | ||||||
| 223 | } | ||||||
| 224 | |||||||
| 225 | 0 | 0 | 0 | if ($action eq 'deleteform') { | |||
| 0 | 0 | ||||||
| 0 | |||||||
| 226 | 0 | $html .= " Are you sure you want to delete context $context?\n"; |
|||||
| 227 | 0 | $html .= " Confirm\n"; |
|||||
| 228 | } elsif ($action eq 'delete') { | ||||||
| 229 | 0 | 0 | 0 | if ($vars{'doit'} == 1 && $self->deletecontext($context)) { | |||
| 230 | 0 | $html .= " Context $context has been deleted\n"; |
|||||
| 231 | 0 | $self->{'commit'} = 1; | |||||
| 232 | } else { | ||||||
| 233 | 0 | $html .= " Unable to delete context $context\n"; |
|||||
| 234 | } | ||||||
| 235 | } elsif ($action eq 'show' || $action =~ /^modify/ || $action =~ /^add/ ) { | ||||||
| 236 | |||||||
| 237 | 0 | 0 | if ($action eq 'add') { | ||||
| 0 | |||||||
| 238 | 0 | $self->_addcontext($context); | |||||
| 239 | 0 | $self->{'commit'} = 1; | |||||
| 240 | } elsif ($action eq 'show') { | ||||||
| 241 | 0 | $html .= "Add new\n"; | |||||
| 242 | 0 | $html .= "Modify\n"; | |||||
| 243 | 0 | $html .= "Delete\n"; | |||||
| 244 | } | ||||||
| 245 | 0 | foreach $var ( sort keys %{$self->{'variables'}} ) { | |||||
| 0 | |||||||
| 246 | 0 | my $value = ''; | |||||
| 247 | |||||||
| 248 | #the logic here seems backwards, but trust me its right | ||||||
| 249 | 0 | 0 | if (my $regex = $self->{'variables'}{$var}{'contextregex'}) { | ||||
| 250 | 0 | 0 | if ($context !~ /$regex/) { | ||||
| 251 | 0 | next; | |||||
| 252 | } | ||||||
| 253 | } | ||||||
| 254 | 0 | 0 | if (my $regex = $self->{'variables'}{$var}{'negcontextregex'}) { | ||||
| 255 | 0 | 0 | if ($context =~ /$regex/) { | ||||
| 256 | 0 | next; | |||||
| 257 | } | ||||||
| 258 | } | ||||||
| 259 | |||||||
| 260 | 0 | 0 | if ($self->{'config'}{$context}{$var}{'val'}) { | ||||
| 261 | 0 | $value = $self->{'config'}{$context}{$var}{'val'}; | |||||
| 262 | } else { | ||||||
| 263 | 0 | $value = $self->{'variables'}{$var}{'default'}; | |||||
| 264 | } | ||||||
| 265 | |||||||
| 266 | 0 | 0 | 0 | if ($action eq 'show') { | |||
| 0 | |||||||
| 0 | |||||||
| 267 | 0 | $html .= " $var: $value\n"; |
|||||
| 268 | } elsif ($action =~ /(.*)form$/) { | ||||||
| 269 | 0 | my $subaction = $1; | |||||
| 270 | 0 | my $fieldtype = $self->{'variables'}{$var}{'type'}; | |||||
| 271 | 0 | $html .= "\n"; | |||||
| 272 | 0 | 0 | if ($fieldtype =~ /text$/) { | ||||
| 0 | |||||||
| 273 | 0 | $html .= " $var: \n"; |
|||||
| 274 | } elsif ($fieldtype eq 'one') { | ||||||
| 275 | 0 | $html .= " $var: \n"; |
|||||
| 276 | 0 | foreach $item (@{$self->{'variables'}{$var}{'values'}}) { | |||||
| 0 | |||||||
| 277 | 0 | 0 | my $checked = 'checked' if ($item eq $value); | ||||
| 278 | 0 | $html .= " $item\n"; | |||||
| 279 | } | ||||||
| 280 | } | ||||||
| 281 | |||||||
| 282 | } elsif ($action eq 'modify' || $action eq 'add') { | ||||||
| 283 | 0 | 0 | 0 | if ($action eq 'add' || ($vars{"VAR$var"} ne $vars{"OLD$var"})) { | |||
| 284 | 0 | my $newval = $vars{"VAR$var"}; | |||||
| 285 | #need to check for valid value here | ||||||
| 286 | 0 | $html .= "\n"; | |||||
| 287 | 0 | 0 | if ($self->variablecheck($context, $var, $newval)) { | ||||
| 288 | 0 | $html .= " 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 | 0 | if ($action =~ /form$/) { | ||||
| 304 | 0 | $html .= " \n"; |
|||||
| 305 | 0 | $html .= "\n"; | |||||
| 306 | } | ||||||
| 307 | |||||||
| 308 | 0 | 0 | if ($self->{'commit'}) { | ||||
| 309 | 0 | print " Going to try to commit\n"; |
|||||
| 310 | 0 | $self->rewriteconfig(); | |||||
| 311 | } | ||||||
| 312 | 0 | $html .= "\n"; | |||||
| 313 | |||||||
| 314 | 0 | return $html; | |||||
| 315 | } | ||||||
| 316 | |||||||
| 317 | sub htmlheader { | ||||||
| 318 | 0 | 0 | 0 | my ($self, $title) = @_; | |||
| 319 | 0 | 0 | $title = $self->{'description'} if (!defined($title)); | ||||
| 320 | 0 | my $html = " |
|||||
| 321 | 0 | $html .= "\n"; | |||||
| 322 | 0 | return $html; | |||||
| 323 | } | ||||||
| 324 | |||||||
| 325 | sub htmlfooter { | ||||||
| 326 | 0 | 0 | 0 | my ($self) = @_; | |||
| 327 | 0 | my $html = "\n"; | |||||
| 328 | 0 | return $html; | |||||
| 329 | } | ||||||
| 330 | |||||||
| 331 | sub deletecontext { | ||||||
| 332 | 0 | 0 | 0 | my ($self, $context) = @_; | |||
| 333 | |||||||
| 334 | 0 | 0 | if (delete($self->{'config'}{$context})) { | ||||
| 335 | 0 | return 1; | |||||
| 336 | } else { | ||||||
| 337 | 0 | return 0; | |||||
| 338 | } | ||||||
| 339 | } | ||||||
| 340 | |||||||
| 341 | sub helptext { | ||||||
| 342 | 0 | 0 | 0 | my ($self, $helpname) = @_; | |||
| 343 | |||||||
| 344 | } | ||||||
| 345 | |||||||
| 346 | 1; | ||||||
| 347 |