summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conv_handler.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ephraim_conv_handler.erl')
-rw-r--r--src/core/ephraim_conv_handler.erl25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/core/ephraim_conv_handler.erl b/src/core/ephraim_conv_handler.erl
index e525d99..681e53a 100644
--- a/src/core/ephraim_conv_handler.erl
+++ b/src/core/ephraim_conv_handler.erl
@@ -1,20 +1,21 @@
-module(ephraim_conv_handler).
-include_lib("exmpp/include/exmpp_client.hrl").
-behaviour(gen_event).
--export([start/2]).
+-export([start/4]).
-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]).
-record(conv_handler_state, {
supervisor :: pid(),
event_manager :: pid(),
+ connection :: pid(),
+ roster :: pid(),
convs = dict:new() :: dict()
}).
-start(Supervisor, EventManager) ->
- gen_event:add_handler(EventManager, ?MODULE, {Supervisor, EventManager}).
+start(Supervisor, EventManager, Conn, Roster) ->
+ gen_event:add_handler(EventManager, ?MODULE, #conv_handler_state{supervisor=Supervisor,event_manager=EventManager,connection=Conn,roster=Roster}).
-init({Supervisor, EventManager}) ->
- State = #conv_handler_state{supervisor=Supervisor,event_manager=EventManager},
+init(State) ->
{ok, State}.
-spec get_conv(#conv_handler_state{},exmpp_jid:jid()) -> {#conv_handler_state{},pid()|undefined}.
@@ -32,12 +33,24 @@ get_conv(State, JID) ->
{State, Conv};
error ->
{ok, Conv} = supervisor:start_child(State#conv_handler_state.supervisor,
- {Key, {ephraim_conv, start_link, [State#conv_handler_state.event_manager, JID]}, transient, 1000, worker, [ephraim_conv]}),
+ {Key, {ephraim_conv, start_link, [State#conv_handler_state.event_manager,
+ State#conv_handler_state.connection, State#conv_handler_state.roster,
+ JID]},
+ transient, 1000, worker, [ephraim_conv]}),
Dict = dict:store(Key, Conv, State#conv_handler_state.convs),
{State#conv_handler_state{convs=Dict}, Conv}
end
end.
+handle_event({view_request, {conversation,JID,new}}, State) ->
+ {NewState, _} = get_conv(State, JID),
+ {ok, NewState};
+
+handle_event({view_request, {conversation,JID,{send_message,Type,Body}}}, State) ->
+ {NewState, Conv} = get_conv(State, JID),
+ ephraim_conv:send_message(Conv, Type, Body),
+ {ok, NewState};
+
handle_event({received_packet, #received_packet{packet_type=message, raw_packet=Packet}}, State) ->
From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
Type = exmpp_message:get_type(Packet),