ami.anubis
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Created by PyramIDE.
* User: Totoro
* Date: 13/07/2013
* Time: 02:07
*
*/
read system/string.anubis
read tools/basis.anubis
read tools/connections.anubis
transmit calexium_lib/asterisk/ami_types.anubis
transmit calexium_lib/asterisk/ami_commands.anubis
transmit calexium_lib/web/CXM_http_get_common.anubis
define Maybe(List(String))
receive
(
Bool print_dump,
Connection conn,
List(String) response
)=
//TODO find the header and content-lenght to get full length of answer
//if read(conn, 16384, 5) is
if read_line(conn, 80, 60) is
{
error then println("readline error");failure,
timeout then println("readline timeout");failure,
eof then println("readline eof");failure,
ok(str) then
if str = crlf then
(if print_dump then
println("=== ASTERISK SERVER answer ==="+crlf +"["+ join("",(reverse(response))) + "]" )
else
unique);
success(reverse(response))
else
receive(print_dump, conn, [str . response])
}
.
public define Maybe(List(String))
ami_receive
(
Bool print_dump,
Connection conn
)=
receive(print_dump, conn, [])
.
define Maybe(String)
_format_action
(
String so_far,
List(Key_value) values
)=
if values is
{
[] then
println("AMI _format_action\n"+so_far+crlf);
success(so_far+crlf),
[h . t] then
if h is key_value(key, value) then
_format_action(so_far+key+": "+value+crlf, t)
}.
/** Format the AMI action
*
*/
define Maybe(String)
format_action
(
AMI_Action action
)=
if action is action(action_type, values_list) then
_format_action( "Action: "+action_type+crlf,
//"ActionID: "+
values_list)
.
public define Maybe(List(String))
send_action
(
Asterisk_client client,
AMI_Action action
)=
if client is asterisk_client(conn, _, auth) then
//format the action into string
if format_action(action) is
{
failure then failure
success(action_str) then
//send the action trough the connection to Asterisk
println("=== ACTION ===");
print(action_str);
println("=== END ACTION ===");
if write(conn, to_byte_array(action_str)) is
{
failure then println("send_action write failure");failure,
success(_) then ami_receive(true, conn)
}
}.
public define Maybe(Asterisk_client)
asterisk_new_client
(
String server_name,
AMI_Auth auth,
Bool subscribe_events
)=
//println("asterisk_new_client "+server_name);
if separate_name_port(server_name, 5038) is (name, server_port) then
//
// resolve server name and call 'https_get' with numeric server address:
//
with a = dns(name),
if a is ok(server_addr) then
println("TCP "+server_port+ " "+server_name);
if (Result(NetworkConnectError,RWStream))connect(server_addr, server_port) is
{
error(e) then failure,
ok(conn) then
//read line of 80 bytes and timeout for 10 seconds
if read_line(tcp(conn), 80, 10) is
{
error then failure,
timeout then failure,
eof then failure,
ok(str) then
println("=== ASTERISK SERVER answer ==="+crlf +"["+ str + "]" );
//to ensure that it is an Asterisk server we test the returned string
if start_with(to_lower(str), "asterisk call manager") then
with client = asterisk_client(tcp(conn), var(0), auth),
if send_action(client, ami_login(auth, subscribe_events)) is
{
failure then failure,
success(_) then success(client)
}
else
failure
}
}
else
failure.
public define Maybe(Asterisk_client)
asterisk_new_client
(
String server_name,
AMI_Auth auth,
)=
asterisk_new_client(server_name, auth, false)
.