ami.anubis
4.05 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
/*
* 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
define Maybe(One)
receive
(
Bool print_dump,
Connection conn
)=
//TODO find the header and content-lenght to get full length of answer
if read(conn, 16384, 5) is
{
error then println("Read error");failure,
timeout then println("Read timeout");failure,
ok(ba) then
with ami_response = to_string(ba),
// typed_response = xml_rpc_get_response(xml_response),
if print_dump then
println("=== ASTERISK SERVER answer ==="+crlf +"["+ ami_response + "]" );
receive(print_dump, conn)
else
receive(print_dump, conn)
}
.
define Maybe(String)
_format_action
(
String so_far,
List(Key_value) values
)=
if values is
{
[] then 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(One)
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 failure,
success(_) then receive(true, conn)
}
}.
public define Maybe(Asterisk_client)
asterisk_new_client
(
String server_name,
AMI_Auth auth
)=
//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
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)) is
{
failure then failure,
success(_) then success(client)
}
else
failure
}
}
else
failure.
global define One
ami_test
(
List(String) args
)=
if asterisk_new_client("192.168.3.118:5038", auth("anubix","anubix")) is
{
failure then println(" asterisk new client failure"),
success(ami_client) then
forget(send_action(ami_client, ami_originate_call("1000", "1001")))
//forget(xml_rpc_client_execute(true, rpc_client, "/Settings", "list_domains", empty_param))
//forget(xml_rpc_client_execute(true, rpc_client, "/Settings", "delete_domain", parameters([parameter[string("amisdefontainebleau.org")]])))
//forget(xml_rpc_client_execute(true, rpc_client, "/Settings", "create_domain", empty_param))
}.