ami.anubis 4.34 KB
/*
 * 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 asterisk/ami_types.anubis
read asterisk/ami_commands.anubis
read web/CXM_http_get_common.anubis


  
 public define Result(
 
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
      
        success(println("=== ASTERISK SERVER answer ==="+crlf +"["+ ami_response + "]" ))
        //println("=== XML_RPC Anubis interpretation ===");

       /* if typed_response is
        {
          failure         then println("Interpretation error"),
          success(result) then 
            if result is
            {
              ok(ok_res)    then println(format_xml_rpc_parameters(ok_res,1)),
              fault(fault)  then println(format_xml_rpc_fault(fault,1))
            }
           
        } */
        
      else success(unique)
  }
  .
  
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
  )=
  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.20:5038", auth("admin","admin")) is
  {
    failure             then println(" asterisk new client failure"),
    success(ami_client) then unique
      //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))
  }.