Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Module
- -module(open_rcc_server).
- %Behaviour
- -behaviour(gen_server).
- %Start Function
- -export([start_link/1]).
- %Gen_Server API
- -export([
- init/1,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- terminate/2,
- code_change/3
- ]).
- -record(state, {} ). %Empty for now.
- %% mochi
- -export([start/1,
- stop/0]).
- %OpenACD
- -include_lib("OpenACD/include/log.hrl").
- -include_lib("OpenACD/include/call.hrl").
- -include_lib("OpenACD/include/agent.hrl").
- -include_lib("OpenACD/include/queue.hrl").
- -include_lib("OpenACD/include/web.hrl").
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gen_Server Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- start_link(Port) ->
- {foo, '[email protected]'} ! {api, log, "info [open_rcc_server:start_link/0] Starting open_rcc_server..."},
- gen_server:start_link({local, open_rcc_server}, open_rcc_server, [Port], []).
- init([Port]) ->
- start(Port),
- NewState = #state{},
- {ok, NewState}.
- handle_call({Resource, Req}, _From, State) ->
- handle_request(Resource, Req),
- {reply, ok, State}.
- handle_cast(_MSG, State) ->
- {noreply, State}.
- handle_info(_Msg, State) ->
- NewState = State,
- {noreply, NewState}.
- terminate(_,_) ->
- ok.
- code_change(_, _, State) ->
- {ok, State}.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mochi-Web Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- start(Port) ->
- mochiweb_http:start([{port, Port},
- {loop, fun(Req) ->
- Path = Req:get(path),
- Resource = case string:str(Path, "?") of
- 0 -> Path;
- N -> string:substr(Path, 1, length(Path) - (N + 1))
- end,
- handle_request(Resource, Req)
- end}]).
- stop() ->
- mochiweb_http:stop().
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% REST API %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- handle_request("/agents", Req) ->
- Data = cpx:get_agents(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/clients", Req) ->
- Data = call_queue_config:get_clients(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/queues", Req) ->
- Data = call_queue_config:get_queues(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/ringtest/" ++ Agent, Req) ->
- AgentPid = cpx:get_agent(Agent),
- AgentRec = agent:dump_state(AgentPid),
- Callrec = #call{id = "unused", source = self(), callerid= {"Echo test", "0000000"}},
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p~p~p", [AgentPid, AgentRec, Callrec])))}),
- Data = freeswitch_media_manager:ring_agent_echo(AgentPid, AgentRec, Callrec, 60000),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- %Data = freeswitch_dialer:start(Node, Number, Exten, Skills, Client, Vars),
- %freeswitch_media_manager:make_outbound_call("DCF", cpx:get_agent("742"), agent:dump_state(cpx:get_agent("742")#agent.login)
- handle_request("/init_outbound/" ++ Rest, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
- handle_request("/dial" ++ _Rest, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
- handle_request("/spy" ++ _Rest, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
- handle_request("/hangup" ++ _Rest, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
- handle_request("/transfer" ++ _Rest, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI.">>});
- handle_request("/logout" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- agent:stop(Pid),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
- end;
- %%
- %% handle_request("/login" ++ _Rest, Req) ->
- %% QueryStringData = Req:parse_qs(),
- %% UserName = proplists:get_value("username", QueryStringData, ""),
- %% Password = proplists:get_value("password", QueryStringData, ""),
- %% Domain = proplists:get_value("domain", QueryStringData, "config.acd.cdf.patlive.local"),
- %%
- %% %TODO Figure out how to get the node name. (Get this un-hard-coded).
- %% Endpointdata = lists:append(lists:append(UserName, "@"), Domain),
- %% Endpoint = pstn,
- %%
- %% case cpx:get_agent(UserName) of
- %% none ->
- %% case agent_auth:auth(UserName, Password) of
- %% {allow, Id, Skills, Security, Profile} ->
- %% Agent = #agent{
- %% id = Id,
- %% login = UserName,
- %% skills = Skills,
- %% profile=Profile,
- %% security_level = Security
- %% },
- %% {ok, Pid} = agent_manager:start_agent(Agent),
- %% agent:set_endpoint(Pid, freeswitch_media, [{type, Endpoint}, {data, Endpointdata}]),
- %% agent:set_release(Pid, none),
- %% Agent2 = agent:dump_state(Pid),
- %% agent_manager:set_avail(UserName, Agent2#agent.all_channels),
- %% Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
- %% end;
- %% Else ->
- %% Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:append("Agent already logged in. ", io_lib:format("~p", [Else])))})
- %% end;
- handle_request("/login" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- Password = proplists:get_value("password", QueryStringData, ""),
- Domain = proplists:get_value("domain", QueryStringData, "config.acd.cdf.patlive.local"),
- %TODO Figure out how to get the node name. (Get this un-hard-coded).
- Endpointdata = lists:append(lists:append(UserName, "@"), Domain),
- Endpoint = pstn,
- case cpx:get_agent(UserName) of
- none ->
- case agent_auth:auth(UserName, Password) of
- {allow, Id, Skills, Security, Profile} ->
- Agent = #agent{
- id = Id,
- login = UserName,
- skills = Skills,
- profile=Profile,
- security_level = Security
- },
- {ok, Pid} = agent_manager:start_agent(Agent),
- agent:set_endpoint(Pid, freeswitch_media, [{type, Endpoint}, {data, Endpointdata}]),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
- end;
- Else ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:append("Agent already logged in. ", io_lib:format("~p", [Else])))})
- end;
- handle_request("/set_avail" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- agent:set_release(Pid, none),
- Agent = agent:dump_state(Pid),
- agent_manager:set_avail(UserName, Agent#agent.all_channels),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
- end;
- handle_request("/set_released" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- agent:set_release(Pid, default),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")})
- end;
- handle_request("/end_wrapup" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- Agent = agent:dump_state(Pid),
- #agent{used_channels = Channels} = Agent,
- [ChannelID] = dict:fetch_keys(Channels),
- case agent_channel:end_wrapup(ChannelID) of
- ok ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Success.")});
- Else ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Failure:")})
- end
- end;
- handle_request("/get_channels" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- Agent = agent:dump_state(Pid),
- #agent{used_channels = Channels} = Agent,
- List =dict:fetch_keys(Channels),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(io_lib:format("~p",[List]))})
- end;
- handle_request("/get_voice_channel" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- UserName = proplists:get_value("username", QueryStringData, ""),
- case cpx:get_agent(UserName) of
- none ->
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary("Agent not logged in.")});
- Pid ->
- Agent = agent:dump_state(Pid),
- #agent{used_channels = Channels} = Agent,
- [ChannelID] = dict:fetch_keys(Channels),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(io_lib:format("~p",[ChannelID]))})
- end;
- handle_request(Path, Req) ->
- Req:respond({200, [{"Content-Type", "text/html"}], <<"NYI">>}).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DICTIONARY FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- fetch_channel(Channel, Channels) when is_binary(Channel) ->
- fetch_channel(binary_to_list(Channel), Channels);
- fetch_channel(Channel, Chans) ->
- fetch_channel(Channel, Chans);
- fetch_channel(Channel, Channels) when is_list(Channel) ->
- ?DEBUG("The chan: ~p, The channels: ~p", [Channel, Channels]),
- Chans = [C || C <- dict:fetch_keys(Channels), pid_to_list(C) =:= Channel],
- case Chans of
- [] -> none;
- [Chan] -> {Chan, dict:fetch(Chan, Channels)}
- end;
- fetch_channel(Channel, Channels) when is_pid(Channel) ->
- case dict:find(Channel, Channels) of
- error -> none;
- {ok, Chan} -> {Channel, Chan}
- end.
Advertisement
Add Comment
Please, Sign In to add comment