Asterisk/Manager.pm

filter: Statementstmt 18.3% 34 / 185 Branchbran 0.0% 0 / 66 Conditioncond 6.2% 1 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 20.6% 6 / 29 Podpod 0.0% 0 / 23 totaltotal 12.4% 41 / 329 CC 42 SCAR 29.2
CC
42
Cov
13%
read_response
56.0
connect
42.0
sendcommand
30.0
CRAP 18.6
« Asterisk/AGI.pm ↑ Summary Asterisk/Outgoing.pm »
1 package Asterisk::Manager;
2  
3 1 require 5.004;
4  
5 1 ▶use Asterisk;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Asterisk::Manager - Asterisk Manager Interface
15  
16 =head1 SYNOPSIS
17  
18 use Asterisk::Manager;
19  
20 my $astman = new Asterisk::Manager;
21  
22 $astman->user('username');
23  
24 $astman->secret('test');
25  
26 $astman->host('localhost');
27  
28 $astman->connect || die "Could not connect to " . $astman->host . "!\n";
29  
30 $astman->disconnect;
31  
32 =head1 DESCRIPTION
33  
34 This module provides a simple interface to the asterisk manager interface.
35  
36 =cut
37  
38 1 my $EOL = "\r\n";
39 1 my $BLANK = $EOL x 2;
40  
41 1 my $VERSION = '0.01';
42  
43 0 ▶sub version { $VERSION; }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
44  
45 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
46 1 my ($class, %args) = @_;
47  
48 1 my $self = {};
49 1 $self->{_CONNFD} = undef;
50 1 $self->{_PROTOVERS} = undef;
51 1 $self->{_ERRORSTR} = undef;
52 1 $self->{_HOST} = 'localhost';
53 1 $self->{_PORT} = 5038;
54 1 $self->{_USER} = undef;
55 1 $self->{_SECRET} = undef;
56 1 $self->{_EVENTCB} = {};
57 1 $self->{_DEBUG} = 0;
58 1 $self->{_CONNECTED} = 0;
59 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
60 1 return $self;
61 }
62  
63 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
64  
65 0 ▶sub user {
Subroutine user: not called CC=2 SCAR=17.9
Pod undocumented
66 0 my ($self, $user) = @_;
67  
68 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
69 0 $self->{_USER} = $user;
70 }
71  
72 0 return $self->{_USER};
73 }
74  
75 0 ▶sub secret {
Subroutine secret: not called CC=2 SCAR=17.9
Pod undocumented
76 0 my ($self, $secret) = @_;
77  
78 0 ▶ if ($secret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secret) 0 0 0
79 0 $self->{_SECRET} = $secret;
80 }
81  
82 0 return $self->{_SECRET};
83 }
84  
85 0 ▶sub host {
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($self, $host) = @_;
87  
88 0 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 0 0 0
89 0 $self->{_HOST} = $host;
90 }
91  
92 0 return $self->{_HOST};
93 }
94  
95 0 ▶sub port {
Subroutine port: not called CC=2 SCAR=17.9
Pod undocumented
96 0 my ($self, $port) = @_;
97  
98 0 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 0 0 0
99 0 $self->{_PORT} = $port;
100 }
101  
102 0 return $self->{_PORT};
103 }
104  
105 0 ▶sub connected {
Subroutine connected: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my ($self, $connected) = @_;
107  
108 0 ▶ if (defined($connected)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connected) 0 0 0
109 0 $self->{_CONNECTED} = $connected;
110 }
111  
112 0 return $self->{_CONNECTED};
113 }
114  
115 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my ($self, $error) = @_;
117  
118 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
119 0 $self->{_ERRORSTR} = $error;
120 }
121  
122 0 return $self->{_ERRORSTR};
123 }
124  
125 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my ($self, $debug) = @_;
127  
128 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
129 0 $self->{_DEBUG} = $debug;
130 }
131  
132 0 return $self->{_DEBUG};
133 }
134  
135 0 ▶sub connfd {
Subroutine connfd: not called CC=2 SCAR=17.9
Pod undocumented
136 0 my ($self, $connfd) = @_;
137  
138 0 ▶ if ($connfd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connfd) 0 0 0
139 0 $self->{_CONNFD} = $connfd;
140 }
141  
142 0 return $self->{_CONNFD};
143 }
144  
145 0 ▶sub read_response {
Subroutine read_response: not called CC=7 SCAR=40.3
Pod undocumented
146 0 my ($self, $connfd) = @_;
147  
148 0 my @response;
149  
150 0 ▶ if (!$connfd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connfd) 0 0 0
151 0 $connfd = $self->connfd;
152 }
153  
154 0 ▶ while (my $line = <$connfd>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $connfd)) 0 0 0
155 0 ▶ last if ($line eq $EOL);
Branch: 1 branch
BranchTrueFalseTotal
if $line eq $EOL 0 0 0
156  
157 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
158 0 $line =~ s/$EOL//g;
159 0 ▶ push(@response, $line) if $line;
Branch: 1 branch
BranchTrueFalseTotal
if $line 0 0 0
160 } else {
161 0 $response[0] .= $line;
162 }
163  
164 }
165  
166 0 ▶ return wantarray ? @response : $response[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
167 }
168  
169 0 ▶sub connect {
Subroutine connect: not called CC=6 SCAR=37.4
Pod undocumented
170 0 my ($self) = @_;
171  
172 0 my $host = $self->host;
173 0 my $port = $self->port;
174 0 my $user = $self->user;
175 0 my $secret = $self->secret;
176 0 my %resp;
177  
178 0 my $conn = new IO::Socket::INET( Proto => 'tcp',
179 PeerAddr => $host,
180 PeerPort => $port
181 );
182 0 ▶ if (!$conn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn) 0 0 0
183 0 $self->error("Connection refused ($host:$port)\n");
184 0 return undef;
185 }
186  
187 0 $conn->autoflush(1);
188  
189 0 my $input = <$conn>;
190 0 $input =~ s/$EOL//g;
191  
192 0 my ($manager, $version) = split('/', $input);
193  
194 0 ▶ if ($manager !~ /Asterisk Call Manager/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($manager =~ /Asterisk Call Manager/) 0 0 0
195 0 return $self->error("Unknown Protocol\n");
196 }
197  
198 0 $self->{_PROTOVERS} = $version;
199 0 $self->connfd($conn);
200  
201 # check if the remote host supports MD5 Challenge authentication
202 0 my %authresp = $self->sendcommand( Action => 'Challenge',
203 AuthType => 'MD5'
204 );
205  
206 0 ▶ if (($authresp{Response} eq 'Success')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authresp{'Response'} eq 'Success') { } 0 0 0
207 # do md5 login
208 0 my $md5 = new Digest::MD5;
209 0 $md5->add($authresp{Challenge});
210 0 $md5->add($secret);
211 0 my $digest = $md5->hexdigest;
212 0 %resp = $self->sendcommand( Action => 'Login',
213 AuthType => 'MD5',
214 Username => $user,
215 Key => $digest
216 );
217 } else {
218 # do plain text login
219 0 %resp = $self->sendcommand( Action => 'Login',
220 Username => $user,
221 Secret => $secret
222 );
223  
224 }
225  
226 0 ▶ if ( ($resp{Response} ne 'Success') && ($resp{Message} ne 'Authentication accepted') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp{"Response"} ne "Success" and $resp{"Message"} ne "Authentication accepted") 0 0 0
Condition0%0/3
$resp{"Response"} ne "Success" and $resp{"Message"} ne "Authentication accepted"
!ll&&!rl&&r
000
MC/DC0%0/2
$resp{"Response"} ne "Success" and $resp{"Message"} ne "Authentication accepted"
$resp{"Response"} ne "Success" $resp{"Message"} ne "Authentication accepted"
Truth table0%0/3
A and B
A= $resp{"Response"} ne "Success"B= $resp{"Message"} ne "Authentication accepted"
ABresult
0X0
100
111
227 0 $self->error("Authentication failed for user $user\n");
228 0 return undef;
229 }
230  
231 0 $self->connected(1);
232  
233 0 return $conn;
234 }
235  
236 0 ▶sub astman_h2s {
Subroutine astman_h2s: not called CC=2 SCAR=17.9
Pod undocumented
237 0 my ($self, %thash) = @_;
238  
239 0 my $tstring = '';
240  
241 0 foreach my $key (keys %thash) {
242 0 $tstring .= $key . ': ' . $thash{$key} . ${EOL};
243 }
244  
245 0 return $tstring;
246 }
247  
248 0 ▶sub astman_s2h {
Subroutine astman_s2h: not called CC=3 SCAR=24.8
Pod undocumented
249 0 my ($self, $tstring) = @_;
250  
251 0 my %thash;
252  
253 0 foreach my $line (split(/$EOL/, $tstring)) {
254 0 ▶ if ($line =~ /(\w*):\s*(\w*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /(\w*):\s*(\w*)/) 0 0 0
255 0 $thash{$1} = $2;
256 }
257 }
258  
259 0 return %thash;
260 }
261  
262 #$want is how you want the data returned
263 #$want = 0 (default) returns the results in a hash
264 #$want = 1 returns the results in a large string
265 #$want = 2 returns the results in an array
266 0 ▶sub sendcommand {
Subroutine sendcommand: not called CC=5 SCAR=34.0
Pod undocumented
267 0 my ($self, @rest) = @_;
268 0 my (%command, $want);
269  
270 0 ▶ if ((scalar(@rest) % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@rest) % 2 == 1) { } 0 0 0
271 0 $want = pop @rest;
272 } else {
273 0 $want = 0;
274 }
275 0 %command = @rest;
276
277 0 ▶ my $conn = $self->connfd || return;
Condition0%0/2
$self->connfd || (return)
l!l
00
MC/DC0%0/1
$self->connfd || (return)
$self->connfd
Truth table0%0/2
A
A= $self->connfd
Aresult
11
00
278 0 my $cstring = $self->astman_h2s(%command);
279  
280 0 $conn->send("$cstring$EOL");
281  
282 0 ▶ if ($want == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want == 1) 0 0 0
283 0 my $response = $self->read_response($conn);
284 0 return $response;
285 }
286  
287 0 my @resp = $self->read_response($conn);
288  
289 0 ▶ if ($want == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want == 2) { } 0 0 0
290 0 return @resp;
291 } else {
292 0 return map { splitresult($_) } @resp;
293 }
294 }
295  
296 0 ▶sub setcallback {
Subroutine setcallback: not called CC=3 SCAR=24.8
Pod undocumented
297 0 my ($self, $event, $function) = @_;
298  
299 0 ▶ if (defined($function) && ref($function) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function and ref $function eq "CODE") 0 0 0
Condition0%0/3
defined $function and ref $function eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $function and ref $function eq "CODE"
defined $function ref $function eq "CODE"
Truth table0%0/3
A and B
A= defined $functionB= ref $function eq "CODE"
ABresult
0X0
100
111
300 0 $self->{_EVENTCB}{$event} = $function;
301 }
302 }
303  
304 0 ▶sub eventcallback {
Subroutine eventcallback: not called CC=4 SCAR=30.0
Pod undocumented
305 0 my ($self, %resp) = @_;
306  
307 0 my $callback;
308 0 my $event = $resp{Event};
309  
310 0 ▶ return if (!$event);
Branch: 1 branch
BranchTrueFalseTotal
unless $event 0 0 0
311  
312 0 ▶ if (defined($self->{_EVENTCB}{$event})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_EVENTCB'}{$event}) { } 0 0 0
elsif (defined $self->{'_EVENTCB'}{'DEFAULT'}) { } 0 0 0
313 0 $callback = $self->{_EVENTCB}{$event};
314 } elsif (defined($self->{_EVENTCB}{DEFAULT})) {
315 0 $callback = $self->{_EVENTCB}{DEFAULT};
316 } else {
317 0 return;
318 }
319  
320 0 return &{$callback}(%resp);
321 }
322  
323 0 ▶sub eventloop {
Subroutine eventloop: not called CC=1 SCAR=6.9
Pod undocumented
324 0 my ($self) = @_;
325  
326 0 while (1) {
327 0 $self->handleevent;
328 }
329 }
330  
331 0 ▶sub handleevent {
Subroutine handleevent: not called CC=1 SCAR=6.9
Pod undocumented
332 0 my ($self) = @_;
333  
334 0 my %resp = map { splitresult($_); } $self->read_response;
335 0 $self->eventcallback(%resp);
336  
337 0 return %resp;
338 }
339  
340 0 ▶sub action {
Subroutine action: not called CC=5 SCAR=34.0
Pod undocumented
341 0 my ($self, $command, $wanthash) = @_;
342  
343 0 ▶ return if (!$command);
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 0 0
344  
345 0 ▶ my $conn = $self->connfd || return;
Condition0%0/2
$self->connfd || (return)
l!l
00
MC/DC0%0/1
$self->connfd || (return)
$self->connfd
Truth table0%0/2
A
A= $self->connfd
Aresult
11
00
346  
347 0 print $conn "Action: $command" . $BLANK;
348 0 my @resp = $self->read_response($conn);
349  
350 0 ▶ if ($wanthash) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wanthash) { } 0 0 0
elsif (wantarray) { } 0 0 0
351 0 return map { splitresult($_) } @resp;
352 } elsif (wantarray) {
353 0 return @resp;
354 } else {
355 0 return $resp[0];
356 }
357 }
358  
359 0 ▶sub command {
Subroutine command: not called CC=2 SCAR=17.9
Pod undocumented
360 0 my ($self, $command) = @_;
361  
362 0 ▶ return if (!$command);
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 0 0
363  
364 0 return $self->sendcommand('Action' => 'Command',
365 'Command' => $command, 1 );
366 }
367  
368 0 ▶sub disconnect {
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod undocumented
369 0 my ($self) = @_;
370  
371 0 my $conn = $self->connfd;
372  
373 0 my %resp = $self->sendcommand('Action' => 'Logoff');
374  
375  
376 0 ▶ if (defined ($resp{Response}) && $resp{Response} eq 'Goodbye') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resp{"Response"} and $resp{"Response"} eq "Goodbye") 0 0 0
Condition0%0/3
defined $resp{"Response"} and $resp{"Response"} eq "Goodbye"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $resp{"Response"} and $resp{"Response"} eq "Goodbye"
defined $resp{"Response"} $resp{"Response"} eq "Goodbye"
Truth table0%0/3
A and B
A= defined $resp{"Response"}B= $resp{"Response"} eq "Goodbye"
ABresult
0X0
100
111
377 0 $self->{_CONNFD} = undef;
378 0 $self->connected(0);
379 0 return 1;
380 }
381  
382 0 return 0;
383 }
384  
385 0 ▶sub splitresult {
Subroutine splitresult: not called CC=3 SCAR=24.8
Pod undocumented
386 0 my ($res) = @_;
387 0 my ($key, $val) = ('', '');
388  
389 0 $res =~ /^([^:]+):\ {0,1}([^\ ].*)$/;
390 0 ▶ $key = $1 if defined($1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 0 0
391 0 ▶ $val = $2 if defined($2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
392  
393 0 return ($key, $val);
394 }
395  
396 1 1;
« Asterisk/AGI.pm ↑ Summary Asterisk/Outgoing.pm »