/* * Created by PyramIDE. * User: Totoro * Date: 13/07/2013 * Time: 02:07 * */ read system/string.anubis read tools/basis.anubis read tools/connections.anubis read network/dns.anubis transmit xlib/asterisk/ami_types.anubis transmit xlib/asterisk/ami_commands.anubis transmit xlib/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 = resolve_address(name), if a is success(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) .