| 1 | package Asterisk::Astman; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 1 | require 5.004; | |||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||
| 7 | Asterisk::Astman - Interface to the astman port | ||||||||||||||||||||||||||
| 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 | ▶ | use Asterisk; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Net::Telnet; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||
| 25 | 1 | $VERSION = '0.01'; | |||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||
| 27 | 1 | $DEBUG = 5; | |||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub version { $VERSION; } | ||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub new { | ||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||
| 32 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||
| 33 | 1 | my $self = {}; | |||||||||||||||||||||||||
| 34 | 1 | $self->{'configfile'} = undef; | |||||||||||||||||||||||||
| 35 | 1 | $self->{'PORT'} = undef; | |||||||||||||||||||||||||
| 36 | 1 | $self->{'USER'} = undef; | |||||||||||||||||||||||||
| 37 | 1 | $self->{'SECRET'} = undef; | |||||||||||||||||||||||||
| 38 | 1 | $self->{'vars'} = {}; | |||||||||||||||||||||||||
| 39 | 1 | $self->{'telnet'} = new Net::Telnet; | |||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | bless $self, ref $class || $class; | ||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||
| 42 | # while (my ($key,$value) = each %args) { $self->set($key,$value); } | ||||||||||||||||||||||||||
| 43 | 1 | return $self; | |||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub DESTROY { } | ||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub port { | ||||||||||||||||||||||||
|
Subroutine
port: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 49 | 0 | my ($self, $port) = @_; | |||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (defined($port)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 52 | 0 | $self->{'PORT'} = $port; | |||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||
| 54 | 0 | ▶ | $self->{'PORT'} = 5038 if (!defined($self->{'PORT'})); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||
| 57 | 0 | return $self->{'PORT'}; | |||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub host { | ||||||||||||||||||||||||
|
Subroutine
host: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 61 | 0 | my ($self, $host) = @_; | |||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if (defined($host)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 64 | 0 | $self->{'HOST'} = $host; | |||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self->{'HOST'} = 'localhost' if (!defined($self->{'HOST'})); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||
| 69 | 0 | return $self->{'HOST'}; | |||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub user { | ||||||||||||||||||||||||
|
Subroutine
user: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 73 | 0 | my ($self, $user) = @_; | |||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (defined($user)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 76 | 0 | $self->{'USER'} = $user; | |||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||
| 79 | 0 | return $self->{'USER'}; | |||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub secret { | ||||||||||||||||||||||||
|
Subroutine
secret: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 83 | 0 | my ($self, $secret) = @_; | |||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (defined($secret)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 86 | 0 | $self->{'SECRET'} = $secret; | |||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||
| 89 | 0 | return $self->{'SECRET'}; | |||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub connect { | ||||||||||||||||||||||||
|
Subroutine
connect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||
| 95 | 0 | my $res; | |||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||
| 97 | 0 | my $telnet = $self->{'telnet'}; | |||||||||||||||||||||||||
| 98 | 0 | my $host = $self->host(); | |||||||||||||||||||||||||
| 99 | 0 | $telnet->port( $self->port() ); | |||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||
| 102 | 0 | $res = $telnet->open($host); | |||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||
| 104 | 0 | $telnet->errmode('return'); | |||||||||||||||||||||||||
| 105 | 0 | my $header = $telnet->getline(); | |||||||||||||||||||||||||
| 106 | 0 | print STDERR "HEADER $header\n"; | |||||||||||||||||||||||||
| 107 | 0 | $telnet->input_record_separator( "\n\n" ); | |||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||
| 110 | 0 | return $res; | |||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 114 | 0 | my ($self) = @_; | |||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||
| 116 | 0 | my $username = $self->user(); | |||||||||||||||||||||||||
| 117 | 0 | my $secret = $self->secret(); | |||||||||||||||||||||||||
| 118 | 0 | my $telnet = $self->{'telnet'}; | |||||||||||||||||||||||||
| 119 | 0 | my $command = "Action: Login\n"; | |||||||||||||||||||||||||
| 120 | 0 | $command .= "Username: $username\n"; | |||||||||||||||||||||||||
| 121 | 0 | $command .= "Secret: $secret\n"; | |||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||
| 123 | 0 | print $self->execute($command); | |||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 129 | 0 | my ($self, $string) = @_; | |||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||
| 131 | 0 | my $result = ''; | |||||||||||||||||||||||||
| 132 | 0 | my $telnet = $self->{'telnet'}; | |||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($telnet->print($string)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 134 | 0 | my @results = $telnet->getline(); | |||||||||||||||||||||||||
| 135 | 0 | $result = arrtostr(@results); | |||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||
| 139 | 0 | return $result; | |||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub defaultevent { | ||||||||||||||||||||||||
|
Subroutine
defaultevent: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 143 | 0 | my ($self, $string) = @_; | |||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | print STDERR "MYEvent: $string\n" if ($DEBUG>2); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub setevent { | ||||||||||||||||||||||||
|
Subroutine
setevent: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 149 | 0 | my ($self, $callback) = @_; | |||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (defined($callback)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 152 | 0 | $self->{'callback'} = $callback; | |||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||
| 154 | 0 | ▶ | $self->{'callback'} = \$self->defaultevent if (!defined($self->{'callback'})); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||
| 157 | 0 | return $self->{'callback'}; | |||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub managerloop { | ||||||||||||||||||||||||
|
Subroutine
managerloop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 161 | 0 | my ($self) = @_; | |||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||
| 163 | 0 | my $telnet = $self->{'telnet'}; | |||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | while (my $result = $telnet->getline()) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 166 | # my $result = arrtostr(@results); | ||||||||||||||||||||||||||
| 167 | # print STDERR $result if ($DEBUG); | ||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($result =~ /Event/) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 169 | # print \$self->{'callback'}; | ||||||||||||||||||||||||||
| 170 | 0 | eval "&$self->{'callback'};"; | |||||||||||||||||||||||||
| 171 | # eval ($self->{'callback'}('$result'); ); | ||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub arrtostr { | ||||||||||||||||||||||||
|
Subroutine
arrtostr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 180 | 0 | my (@input) = @_; | |||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||
| 182 | 0 | my $output = ''; | |||||||||||||||||||||||||
| 183 | 0 | foreach (@input) { | |||||||||||||||||||||||||
| 184 | 0 | $output .= $_; | |||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||
| 186 | 0 | return $output; | |||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub configfile { | ||||||||||||||||||||||||
|
Subroutine
configfile: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 192 | 0 | my ($self, $configfile) = @_; | |||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | if (defined($configfile)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 195 | 0 | $self->{'configfile'} = $configfile; | |||||||||||||||||||||||||
| 196 | } else { | ||||||||||||||||||||||||||
| 197 | 0 | ▶ | $self->{'configfile'} = '/etc/asterisk/manager.conf' if (!defined($self->{'configfile'})); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||
| 200 | 0 | return $self->{'configfile'}; | |||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub setvar { | ||||||||||||||||||||||||
|
Subroutine
setvar: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 204 | 0 | my ($self, $context, $var, $val) = @_; | |||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||
| 206 | 0 | $self->{'vars'}{$context}{$var} = $val; | |||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub readconfig { | ||||||||||||||||||||||||
|
Subroutine
readconfig: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||
| 210 | 0 | my ($self) = @_; | |||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||
| 212 | 0 | my $context = ''; | |||||||||||||||||||||||||
| 213 | 0 | my $line = ''; | |||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||
| 215 | 0 | my $configfile = $self->configfile(); | |||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | open(CF, "<$configfile") || die "Error loading $configfile: $!\n"; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 218 | 0 | ▶ | while ($line = <CF>) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 219 | 0 | chop($line); | |||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||
| 221 | 0 | $line =~ s/;.*$//; | |||||||||||||||||||||||||
| 222 | 0 | $line =~ s/\s*$//; | |||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||
| 224 | 0 | ▶ | if ($line =~ /^;/) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 225 | 0 | next; | |||||||||||||||||||||||||
| 226 | } elsif ($line =~ /^\s*$/) { | ||||||||||||||||||||||||||
| 227 | 0 | next; | |||||||||||||||||||||||||
| 228 | } elsif ($line =~ /^\[(\w+)\]$/) { | ||||||||||||||||||||||||||
| 229 | 0 | $context = $1; | |||||||||||||||||||||||||
| 230 | 0 | ▶ | print STDERR "Context: $context\n" if ($DEBUG>3); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 231 | } elsif ($line =~ /^port\s*[=>]+\s*(\d+)/) { | ||||||||||||||||||||||||||
| 232 | 0 | $self->port($1); | |||||||||||||||||||||||||
| 233 | } elsif ($line =~ /^(\w+)\s*[=>]+\s*(.*)/) { | ||||||||||||||||||||||||||
| 234 | 0 | $self->setvar($context, $1, $2); | |||||||||||||||||||||||||
| 235 | } else { | ||||||||||||||||||||||||||
| 236 | 0 | ▶ | print STDERR "Unknown line: $line\n" if ($DEBUG); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||
| 240 | 0 | close(CF); | |||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||
| 242 | 0 | return 1; | |||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||
| 245 | 1 | 1; | |||||||||||||||||||||||||