Guest User

Untitled

a guest
May 7th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 10.95 KB | None | 0 0
  1. %Module
  2. -module(open_rcc_server).
  3.  
  4. %Behaviour
  5. -behaviour(gen_server).
  6.  
  7. %Start Function
  8. -export([start_link/1]).
  9.  
  10. %Gen_Server API
  11. -export([
  12.     init/1,
  13.     handle_call/3,
  14.     handle_cast/2,
  15.     handle_info/2,
  16.     terminate/2,
  17.     code_change/3
  18. ]).
  19.  
  20. -record(state, {} ). %Empty for now.
  21.  
  22. %% mochi
  23. -export([start/1,
  24.          stop/0]).
  25.  
  26. %OpenACD
  27. -include_lib("OpenACD/include/log.hrl").
  28. -include_lib("OpenACD/include/call.hrl").
  29. -include_lib("OpenACD/include/agent.hrl").
  30. -include_lib("OpenACD/include/queue.hrl").
  31. -include_lib("OpenACD/include/web.hrl").
  32.  
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gen_Server Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36.  
  37. start_link(Port) ->
  38.     {foo, '[email protected]'} ! {api, log, "info [open_rcc_server:start_link/0] Starting open_rcc_server..."},
  39.     gen_server:start_link({local, open_rcc_server}, open_rcc_server, [Port], []).
  40.  
  41. init([Port]) ->
  42.     {foo, '[email protected]'} ! {api, log, "info [open_rcc_server:init/1] Starting mochi..."},
  43.     start(Port),
  44.     NewState = #state{},
  45.     {ok, NewState}.
  46.  
  47. handle_call({Resource, Req}, _From, State) ->
  48.     handle_request(Resource, Req),
  49.     {reply, ok, State}.
  50.  
  51. handle_cast(_MSG, State) ->
  52.     {noreply, State}.
  53.  
  54. handle_info(_Msg, State) ->
  55.     NewState = State,
  56.     {noreply, NewState}.
  57.  
  58. terminate(_,_) ->
  59.     ok.
  60.  
  61. code_change(_, _, State) ->
  62.     {ok, State}.
  63.  
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mochi-Web Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  66. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  67.  
  68.  
  69. start(Port) ->
  70.     mochiweb_http:start([{port, Port},
  71.                          {loop, fun(Req) ->
  72.                             Path = Req:get(path),
  73.                             Resource = case string:str(Path, "?") of
  74.                                 0 -> Path;
  75.                                 N -> string:substr(Path, 1, length(Path) - (N + 1))
  76.                             end,
  77.                             handle_request(Resource, Req)
  78.                         end}]).
  79.  
  80. stop() ->
  81.     mochiweb_http:stop().
  82.  
  83. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  84. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% REST API %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  85. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  86.  
  87.  
  88. handle_request("/agents", Req) ->
  89.     Data = cpx:get_agents(),
  90.     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
  91.  
  92. handle_request("/clients", Req) ->
  93.     Data = call_queue_config:get_clients(),
  94.     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
  95.  
  96. handle_request("/queues", Req) ->
  97.     Data = call_queue_config:get_queues(),
  98.     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
  99.  
  100. handle_request("/ringtest/" ++ Agent, Req) ->
  101.     AgentPid = cpx:get_agent(Agent),
  102.     AgentRec = agent:dump_state(AgentPid),
  103.     Callrec = #call{id = "unused", source = self(), callerid= {"Echo test", "0000000"}},
  104.    
  105.     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p~p~p", [AgentPid, AgentRec, Callrec])))}),
  106.  
  107.     Data = freeswitch_media_manager:ring_agent_echo(AgentPid, AgentRec, Callrec, 60000),
  108.     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
  109.     %Data = freeswitch_dialer:start(Node, Number, Exten, Skills, Client, Vars),
  110.  
  111. %freeswitch_media_manager:make_outbound_call("DCF", cpx:get_agent("742"), agent:dump_state(cpx:get_agent("742")#agent.login)
  112.  
  113. handle_request("/init_outbound/" ++ Rest, Req) ->
  114.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
  115.  
  116. handle_request("/dial" ++ _Rest, Req) ->
  117.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
  118.  
  119. handle_request("/spy" ++ _Rest, Req) ->
  120.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
  121.    
  122. handle_request("/hangup" ++ _Rest, Req) ->
  123.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
  124.  
  125. handle_request("/transfer" ++ _Rest, Req) ->
  126.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
  127.  
  128. handle_request("/logout" ++ _Rest, Req) ->
  129.     QueryStringData = Req:parse_qs(),
  130.     UserName = proplists:get_value("username", QueryStringData, ""),
  131.    
  132.     case cpx:get_agent(UserName) of
  133.         none ->
  134.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  135.         Pid ->
  136.             agent:stop(Pid),
  137.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
  138.     end;
  139. %%
  140. %% handle_request("/login" ++ _Rest, Req) ->
  141. %%     QueryStringData = Req:parse_qs(),
  142. %%     UserName = proplists:get_value("username", QueryStringData, ""),
  143. %%     Password = proplists:get_value("password", QueryStringData, ""),
  144. %%     Domain = proplists:get_value("domain", QueryStringData, "config.acd.cdf.patlive.local"),
  145. %%
  146. %%  %TODO Figure out how to get the node name. (Get this un-hard-coded).
  147. %%  Endpointdata = lists:append(lists:append(UserName, "@"), Domain),
  148. %%  Endpoint = pstn,
  149. %%  
  150. %%  case cpx:get_agent(UserName) of
  151. %%      none ->
  152. %%          case agent_auth:auth(UserName, Password) of
  153. %%              {allow, Id, Skills, Security, Profile} ->
  154. %%                  Agent = #agent{
  155. %%                      id = Id,
  156. %%                      login = UserName,
  157. %%                      skills = Skills,
  158. %%                      profile=Profile,
  159. %%                      security_level = Security
  160. %%                  },
  161. %%                  {ok, Pid} = agent_manager:start_agent(Agent),
  162. %%                  agent:set_endpoint(Pid, freeswitch_media, [{type, Endpoint}, {data, Endpointdata}]),
  163. %%                  agent:set_release(Pid, none),
  164. %%                  Agent2 = agent:dump_state(Pid),
  165. %%                  agent_manager:set_avail(UserName, Agent2#agent.all_channels),
  166. %%                  Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
  167. %%          end;
  168. %%      Else ->
  169. %%          Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:append("Agent already logged in. ", io_lib:format("~p", [Else])))})
  170. %%  end;
  171.  
  172. handle_request("/login" ++ _Rest, Req) ->
  173.     QueryStringData = Req:parse_qs(),
  174.     UserName = proplists:get_value("username", QueryStringData, ""),
  175.     Password = proplists:get_value("password", QueryStringData, ""),
  176.     Domain = proplists:get_value("domain", QueryStringData, "config.acd.cdf.patlive.local"),
  177.  
  178.     %TODO Figure out how to get the node name. (Get this un-hard-coded).
  179.     Endpointdata = lists:append(lists:append(UserName, "@"), Domain),
  180.     Endpoint = pstn,
  181.    
  182.     case cpx:get_agent(UserName) of
  183.         none ->
  184.             case agent_auth:auth(UserName, Password) of
  185.                 {allow, Id, Skills, Security, Profile} ->
  186.                     Agent = #agent{
  187.                         id = Id,
  188.                         login = UserName,
  189.                         skills = Skills,
  190.                         profile=Profile,
  191.                         security_level = Security
  192.                     },
  193.                     {ok, Pid} = agent_manager:start_agent(Agent),
  194.                     agent:set_endpoint(Pid, freeswitch_media, [{type, Endpoint}, {data, Endpointdata}]),
  195.                     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
  196.             end;
  197.         Else ->
  198.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:append("Agent already logged in. ", io_lib:format("~p", [Else])))})
  199.     end;
  200.  
  201. handle_request("/set_avail" ++ _Rest, Req) ->
  202.     QueryStringData = Req:parse_qs(),
  203.     UserName = proplists:get_value("username", QueryStringData, ""),
  204.    
  205.     case cpx:get_agent(UserName) of
  206.         none ->
  207.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  208.         Pid ->
  209.             agent:set_release(Pid, none),
  210.             Agent = agent:dump_state(Pid),
  211.             agent_manager:set_avail(UserName, Agent#agent.all_channels),
  212.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
  213.     end;
  214.  
  215. handle_request("/set_released" ++ _Rest, Req) ->
  216.     QueryStringData = Req:parse_qs(),
  217.     UserName = proplists:get_value("username", QueryStringData, ""),
  218.    
  219.     case cpx:get_agent(UserName) of
  220.         none ->
  221.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  222.         Pid ->
  223.             agent:set_release(Pid, default),
  224.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
  225.     end;
  226.  
  227. handle_request("/end_wrapup" ++ _Rest, Req) ->
  228.     QueryStringData = Req:parse_qs(),
  229.     UserName = proplists:get_value("username", QueryStringData, ""),
  230.    
  231.     case cpx:get_agent(UserName) of
  232.         none ->
  233.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  234.         Pid ->
  235.             Agent = agent:dump_state(Pid),
  236.             #agent{used_channels = Channels} = Agent,
  237.             [ChannelID] = dict:fetch_keys(Channels),
  238.  
  239.             case agent_channel:end_wrapup(ChannelID) of
  240.                 ok ->
  241.                     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")});
  242.                 Else ->
  243.                     Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Failure:")})
  244.             end
  245.     end;
  246.  
  247. handle_request("/get_channels" ++ _Rest, Req) ->
  248.     QueryStringData = Req:parse_qs(),
  249.     UserName = proplists:get_value("username", QueryStringData, ""),
  250.    
  251.     case cpx:get_agent(UserName) of
  252.         none ->
  253.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  254.         Pid ->
  255.             Agent = agent:dump_state(Pid),
  256.             #agent{used_channels = Channels} = Agent,
  257.             List =dict:fetch_keys(Channels),
  258.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(io_lib:format("~p",[List]))})
  259.     end;
  260.  
  261. handle_request("/get_voice_channel" ++ _Rest, Req) ->
  262.     QueryStringData = Req:parse_qs(),
  263.     UserName = proplists:get_value("username", QueryStringData, ""),
  264.    
  265.     case cpx:get_agent(UserName) of
  266.         none ->
  267.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
  268.         Pid ->
  269.             Agent = agent:dump_state(Pid),
  270.             #agent{used_channels = Channels} = Agent,
  271.            
  272.             [ChannelID] = dict:fetch_keys(Channels),
  273.             Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(io_lib:format("~p",[ChannelID]))})
  274.     end;
  275.  
  276. handle_request(Path, Req) ->
  277.     Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI">>}).
  278.    
  279. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  280. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DICTIONARY FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  281. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  282.  
  283. fetch_channel(Channel, Channels) when is_binary(Channel) ->
  284.     fetch_channel(binary_to_list(Channel), Channels);
  285.  
  286. fetch_channel(Channel, Chans) ->
  287.     fetch_channel(Channel, Chans);
  288.  
  289. fetch_channel(Channel, Channels) when is_list(Channel) ->
  290.     ?DEBUG("The chan:  ~p, The channels:  ~p", [Channel, Channels]),
  291.     Chans = [C || C <- dict:fetch_keys(Channels), pid_to_list(C) =:= Channel],
  292.     case Chans of
  293.         [] ->   none;
  294.         [Chan] -> {Chan, dict:fetch(Chan, Channels)}
  295.     end;
  296.  
  297. fetch_channel(Channel, Channels) when is_pid(Channel) ->
  298.     case dict:find(Channel, Channels) of
  299.         error -> none;
  300.         {ok, Chan} -> {Channel, Chan}
  301.     end.
Advertisement
Add Comment
Please, Sign In to add comment