line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Asterisk::Extension; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.004; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Asterisk::Extension - Stuff to deal with asterisk extension config |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
stuff goes here |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
description |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=over 4 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
537
|
use Asterisk; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1127
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$VERSION = '0.01'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$DEBUG = 1; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
0
|
0
|
0
|
sub version { $VERSION; } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub new { |
30
|
1
|
|
|
1
|
0
|
281
|
my ($class, %args) = @_; |
31
|
1
|
|
|
|
|
2
|
my $self = {}; |
32
|
1
|
|
|
|
|
1
|
$self->{'exten'} = {}; |
33
|
1
|
|
|
|
|
1
|
$self->{'contexts'} = {}; |
34
|
1
|
|
33
|
|
|
5
|
bless $self, ref $class || $class; |
35
|
|
|
|
|
|
|
# while (my ($key,$value) = each %args) { $self->set($key,$value); } |
36
|
1
|
|
|
|
|
2
|
return $self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
0
|
|
|
sub DESTROY { } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub exten { |
42
|
0
|
|
|
0
|
0
|
|
my ($self, $context, $extension, $priority, $value) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
$self->{'exten'}{$context}{$extension}[$priority] = $value; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub getextensionarr { |
48
|
0
|
|
|
0
|
0
|
|
my ($self, $context, $extension) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return @{$self->{'exten'}{$context}{$extension}}; |
|
0
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub getextensionlist { |
54
|
0
|
|
|
0
|
0
|
|
my ($self, $context) = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my @list = (); |
57
|
|
|
|
|
|
|
#whats the best way to sort here |
58
|
0
|
|
|
|
|
|
foreach $ext (sort keys %{$self->{'exten'}{$context}} ) { |
|
0
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
push(@list, $ext); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
return @list; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub setvar { |
67
|
0
|
|
|
0
|
0
|
|
my ($self, $context, $var, $val) = @_; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$self->{'vars'}{$context}{$var} = $val; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub static { |
73
|
0
|
|
|
0
|
0
|
|
my ($self, $static, $context) = @_; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
$self->{'vars'}{$context}{'static'} = $static if defined($static); |
76
|
0
|
|
|
|
|
|
return $self->{'vars'}{$context}{'static'}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub writeprotect { |
80
|
0
|
|
|
0
|
0
|
|
my ($self, $wp, $context) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
$self->{'vars'}{$context}{'writeprotect'} = $wp if defined($wp); |
83
|
0
|
|
|
|
|
|
return $self->{'vars'}{$context}{'writeprotect'}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub matchpattern { |
87
|
0
|
|
|
0
|
0
|
|
my ($self, $dialednum, $extension) = @_; |
88
|
0
|
|
|
|
|
|
my $expr = ''; |
89
|
|
|
|
|
|
|
#N 1-9 |
90
|
|
|
|
|
|
|
#X 0-9 |
91
|
|
|
|
|
|
|
#. any one character |
92
|
|
|
|
|
|
|
#_ or - ignore |
93
|
0
|
|
|
|
|
|
foreach $chr (split(//,$extension)) { |
94
|
0
|
0
|
0
|
|
|
|
if (($chr eq '-')||($chr eq '_')) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
next; |
96
|
|
|
|
|
|
|
} elsif ($chr eq 'N') { |
97
|
0
|
|
|
|
|
|
$expr .= '[1-9]'; |
98
|
|
|
|
|
|
|
} elsif ($chr eq 'X') { |
99
|
0
|
|
|
|
|
|
$expr .= '[0-9]'; |
100
|
|
|
|
|
|
|
} else { |
101
|
0
|
|
|
|
|
|
$expr .= $chr; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
if ($dialednum =~ /^$expr$/) { |
106
|
0
|
|
|
|
|
|
return 1; |
107
|
|
|
|
|
|
|
} else { |
108
|
0
|
|
|
|
|
|
return 0; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub matchextension { |
115
|
0
|
|
|
0
|
0
|
|
my ($self, $context, $dialed) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my %included = (); |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
0
|
|
|
|
return 0 if (!defined($context)||!defined($dialed)); |
120
|
0
|
|
|
|
|
|
my @contextlist = ( $context ); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
foreach $cont (@contextlist) { |
123
|
0
|
|
|
|
|
|
foreach $ext ($self->getextensionlist($cont)) { |
124
|
0
|
0
|
|
|
|
|
if ($self->matchpattern($dialed, $ext)) { |
125
|
0
|
|
|
|
|
|
return ($ext, $cont); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
foreach $inccont (keys %{$self->{'contexts'}{$cont}}) { |
|
0
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
if (!defined($included{$inccont})) { |
132
|
0
|
|
|
|
|
|
push(@contextlist, $inccont); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub context { |
140
|
0
|
|
|
0
|
0
|
|
my ($self, $context, $include) = @_; |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
if (!defined($self->{'contexts'}{$context}) ) { |
143
|
0
|
|
|
|
|
|
$self->{'contexts'}{$context} = {}; |
144
|
0
|
|
|
|
|
|
$self->{'exten'}{$context} = {}; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
if (defined($include)) { |
148
|
0
|
|
|
|
|
|
$self->{'contexts'}{$context}{$include} = 1; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub getcontextarr { |
155
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my @arr = (); |
159
|
0
|
|
|
|
|
|
foreach $context ( keys %{$self->{'contexts'}} ) { |
|
0
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
push(@arr, $context); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
return @arr; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub writeconfig { |
168
|
0
|
|
|
0
|
0
|
|
my ($self, $filename) = @_; |
169
|
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
|
return if (!defined($filename)); |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
open(CFG, ">$filename") || die $!; |
173
|
|
|
|
|
|
|
# my @contextarr = ( 'general' ); |
174
|
0
|
|
|
|
|
|
push(@contextarr, $self->getcontextarr()); |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
foreach $context (@contextarr) { |
177
|
0
|
|
|
|
|
|
print CFG "[$context]\n"; |
178
|
0
|
|
|
|
|
|
foreach $var (keys %{$self->{'vars'}{$context}}) { |
|
0
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
print CFG "$var = " . $self->{'vars'}{$context}{$var} . "\n"; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
foreach $exten ($self->getextensionlist($context)) { |
183
|
0
|
|
|
|
|
|
my @extarr = $self->getextensionarr($context, $exten); |
184
|
0
|
|
|
|
|
|
for ($x=0; $x<=$#extarr; $x++) { |
185
|
0
|
0
|
|
|
|
|
print CFG "exten => $exten,$x,$extarr[$x]\n" if ($extarr[$x]); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub readconfig { |
197
|
0
|
|
|
0
|
0
|
|
my ($self, $filename) = @_; |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
my $context = ''; |
200
|
0
|
|
|
|
|
|
my $line = ''; |
201
|
|
|
|
|
|
|
|
202
|
0
|
0
|
|
|
|
|
$filename = '/etc/asterisk/extensions.conf' if (!defined($filename)); |
203
|
|
|
|
|
|
|
|
204
|
0
|
0
|
|
|
|
|
open(FN, "<$filename") || die $!; |
205
|
0
|
|
|
|
|
|
while ($line = ) { |
206
|
0
|
|
|
|
|
|
chop($line); |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
209
|
0
|
|
|
|
|
|
$line =~ s/;(.*)$//; |
210
|
0
|
|
|
|
|
|
$line =~ s/\s*$//; |
211
|
0
|
0
|
|
|
|
|
if ($line =~ /^;/) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
212
|
0
|
|
|
|
|
|
next; |
213
|
|
|
|
|
|
|
} elsif ($line =~ /^\s*$/) { |
214
|
0
|
|
|
|
|
|
next; |
215
|
|
|
|
|
|
|
} elsif ($line =~ /^static\s*[=>]\s*(.*)$/i) { |
216
|
0
|
|
|
|
|
|
$self->static($1,$context); |
217
|
|
|
|
|
|
|
} elsif ($line =~ /^writeprotect\s*[=>]\s*(.*)$/i) { |
218
|
0
|
|
|
|
|
|
$self->writeprotect($1,$context); |
219
|
|
|
|
|
|
|
} elsif ($line =~ /^\[(\w+)\]$/) { |
220
|
0
|
|
|
|
|
|
$context = $1; |
221
|
0
|
0
|
|
|
|
|
print STDERR "Context: $context\n" if ($DEBUG>3); |
222
|
0
|
|
|
|
|
|
$self->context($context); |
223
|
|
|
|
|
|
|
} elsif ($line =~ /^include\s*[=>]+>\s*(.+)$/) { |
224
|
0
|
|
|
|
|
|
my $include = $1; |
225
|
0
|
0
|
|
|
|
|
print STDERR "Include: $include\n" if ($DEBUG>3); |
226
|
0
|
|
|
|
|
|
$self->context($context, $include); |
227
|
|
|
|
|
|
|
} elsif ($line =~ /^exten\s*[=>]+\s*(.+)/) { |
228
|
0
|
|
|
|
|
|
my $extenstr = $1; |
229
|
0
|
0
|
|
|
|
|
print STDERR "ExtensionString: $extenstr\n" if ($DEBUG>3); |
230
|
0
|
|
|
|
|
|
my @extarr = split(/,/,$extenstr); |
231
|
0
|
|
|
|
|
|
my $exten = shift(@extarr); |
232
|
0
|
|
|
|
|
|
my $pri = shift(@extarr); |
233
|
0
|
|
|
|
|
|
my $addtl = join(',', @extarr); |
234
|
0
|
|
|
|
|
|
$self->exten($context, $exten, $pri, $addtl); |
235
|
|
|
|
|
|
|
} elsif ($line =~ /^(\w+)\s*[=>]+\s*(.*)/) { |
236
|
0
|
|
|
|
|
|
$self->setvar($context, $1, $2); |
237
|
|
|
|
|
|
|
} else { |
238
|
0
|
0
|
|
|
|
|
print STDERR "Unknown line: $line\n" if ($DEBUG); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
close(FN); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1; |