| 1 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Telephony::Asterisk::AMI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Copyright 2015 Christopher J. Madsen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # Author: Christopher J. Madsen <perl@cjmweb.net> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Created: 31 Oct 2015 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # GNU General Public License or the Artistic License for more details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Simple Asterisk Manager Interface client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use IO::Socket::IP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # This file is part of Telephony-Asterisk-AMI 0.005 (December 19, 2015) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | my $EOL = "\r\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | #===================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | my $args = (@_ == 1) ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | Debug_FH => ($args->{Debug_FH} || ($args->{Debug} ? *STDERR : undef)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Event_Callback => $args->{Event_Callback}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Host => $args->{Host} || 'localhost', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | Port => $args->{Port} || 5038, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | ActionID => $args->{ActionID} || 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
$args->{'Debug_FH'} || ($args->{'Debug'} ? *STDERR : undef)
$args->{'Host'} || 'localhost'
$args->{'Port'} || 5038
$args->{'ActionID'} || 1
MC/DC0%0/2
$args->{'Debug_FH'} || ($args->{'Debug'} ? *STDERR : undef)
$args->{'Debug_FH'}
($args->{'Debug'} ? *STDERR : undef)
MC/DC0%0/1
$args->{'Host'} || 'localhost'
$args->{'Host'}
MC/DC0%0/1
$args->{'Port'} || 5038
$args->{'Port'}
MC/DC0%0/1
$args->{'ActionID'} || 1
$args->{'ActionID'}
Truth table33%1/3
A || B
A= $args->{'Debug_FH'}B= ($args->{'Debug'} ? *STDERR : undef)
Truth table50%1/2
A
A= $args->{'Host'}
Truth table50%1/2
A
A= $args->{'Port'}
Truth table50%1/2
A
A= $args->{'ActionID'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | for my $key (qw(Username Secret)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | defined( $self->{$key} = $args->{$key} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | or Carp::croak("Required parameter '$key' not defined"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } # end new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | sub connect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect: called CC=6 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Open a socket to Asterisk. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # IO::Socket::IP->new reports error in $@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | $self->{socket} = IO::Socket::IP->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | Type => IO::Socket::IP::SOCK_STREAM(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | PeerHost => $self->{Host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | PeerService => $self->{Port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | unless ($self->{socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $self->{error} = "Connection failed: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # Verify that we've connected to Asterisk Call Manager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my $id = readline($self->{socket}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | unless (defined $id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $self->{error} = "Connection closed without input: $!"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | chomp $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | print { $self->{Debug_FH} } "<< $id\n" if $self->{Debug_FH}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | if ($id =~ m!^Asterisk Call Manager/(.+)!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $self->{protocol} = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $self->{error} = "Unknown Protocol"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # Automatically log in using Username/Secret | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | my $response = $self->action({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | Action => 'Login', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | Username => $self->{Username}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | Secret => $self->{Secret}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # If login failed, set error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | unless ($response->{Response} eq 'Success') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->{error} = "Login failed: $response->{Message}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # Login successful | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } # end connect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | sub disconnect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $response = $self->action({Action => 'Logoff'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # If logoff failed, set error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | unless ($response->{Response} eq 'Goodbye') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $self->{error} = "Logoff failed: $response->{Message}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | unless ($self->{socket}->close) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $self->{error} = "Closing socket failed: $!"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | undef $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # Logoff successful | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } # end disconnect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | sub action { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
action: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # Send the request to Asterisk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | my $id = $self->send_action(@_) or return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Response => 'Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | Message => $self->{error}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # Read responses until we get the response to this action | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 7 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 9 | my $response = $self->read_response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # If this is the response to the action we just sent, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # or there was an error, return it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 9 | ▶ | if (($response->{ActionID} eq $id) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$response->{"ActionID"} eq $id or $response->{"Response"} eq "Error"
MC/DC50%1/2
$response->{"ActionID"} eq $id or $response->{"Response"} eq "Error"
$response->{"ActionID"} eq $id
$response->{"Response"} eq "Error"
Truth table66%2/3
A or B
A= $response->{"ActionID"} eq $idB= $response->{"Response"} eq "Error"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | ($response->{Response} eq 'Error')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # If there is an event callback, send it this event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | ▶ | if ($self->{Event_Callback}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $self->{Event_Callback}->($response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } # end infinite loop waiting for response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } # end action | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | sub send_action { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_action: called CC=9 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 7 | ▶ | my $act = (@_ == 1) ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 7 | ▶ | Carp::croak("Required parameter 'Action' not defined") unless $act->{Action}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Check that the connection is open | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 7 | ▶ | unless ($self->{socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $self->{error} = "Not connected to Asterisk!"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # Assemble the message to send to Asterisk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 7 | my $id = $self->{ActionID}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 7 | my $message = "ActionID: $id$EOL"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 7 | for my $key (sort keys %$act) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 14 | ▶ | if (ref $act->{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 1 | $message .= "$key: $_$EOL" for @{ $act->{$key} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 13 | $message .= "$key: $act->{$key}$EOL"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 7 | $message .= $EOL; # Message ends with blank line | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # If debugging, print out the message before sending it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 7 | ▶ | if ($self->{Debug_FH}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $debug = $message; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $debug =~ s/\r//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $debug =~ s/^/>> /mg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | print { $self->{Debug_FH} } $debug; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # Send the request to Asterisk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 7 | ▶ | unless (print { $self->{socket} } $message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $self->{error} = "Writing to socket failed: $!"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 7 | $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } # end send_action | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | sub read_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_response: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # Check that the connection is open | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 9 | my $socket = $self->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 9 | ▶ | unless ($socket) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | Response => 'Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | Message => $self->{error} = "Not connected to Asterisk!", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Read a response terminated by a blank line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 9 | local $/ = $EOL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 9 | my $debug_fh = $self->{Debug_FH}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 9 | my ($line, %response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 9 | undef $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 9 | ▶ | while ($line = <$socket>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 60 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 60 | ▶ | print $debug_fh "<< $line\n" if $debug_fh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 60 | ▶ | return \%response unless length $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # Remove the key from the "Key: Value" line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # If the line is not in that format, ignore it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 51 | ▶ | $line =~ s/^([^:]+): // or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 51 | ▶ | if (not exists $response{$1}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # First occurrence of this key, save as string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 45 | $response{$1} = $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | } elsif (ref $response{$1}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Third or more occurrence of this key, append to arrayref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | push @{ $response{$1} }, $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # Second occurrence of this key, convert to arrayref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 4 | $response{$1} = [ $response{$1}, $line ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } # end while reading from $socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # There was a communication failure; return an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | Response => 'Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | Message => $self->{error} = "Reading from socket failed: $!", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } # end read_response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 7 | ▶ | sub error { shift->{error} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | #===================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # Package Return Value: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||