summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conv.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ephraim_conv.erl')
-rw-r--r--src/core/ephraim_conv.erl69
1 files changed, 26 insertions, 43 deletions
diff --git a/src/core/ephraim_conv.erl b/src/core/ephraim_conv.erl
index d9431b2..d6ab742 100644
--- a/src/core/ephraim_conv.erl
+++ b/src/core/ephraim_conv.erl
@@ -1,39 +1,53 @@
-module(ephraim_conv).
-behaviour(gen_server).
--export([start_link/2]).
+-export([start_link/4]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
--export([received_message/4]).
+-export([received_message/4, send_message/3]).
-record(conv_state, {
- conv_ref :: reference(),
event_manager :: pid(),
+ connection :: pid(),
+ roster :: pid(),
my_jid :: binary(),
jid :: binary()
}).
--spec start_link(pid(), binary()) -> ok.
-start_link(EventManager, JID) ->
- ConvRef = make_ref(),
- io:format("Starting conversation ~p with ~p~n", [ConvRef, JID]),
-
- gen_event:notify(EventManager, {view_update, {conversation, ConvRef, {new, JID}}}),
+-spec start_link(pid(), pid(), pid(), binary()) -> ok.
+start_link(EventManager, Conn, Roster, JID) ->
+ gen_event:notify(EventManager, {view_update, {conversation, JID, new}}),
{jid,MyJID} = ephraim_config:get(jid),
- gen_server:start_link(?MODULE, #conv_state{conv_ref=ConvRef,event_manager=EventManager,my_jid=list_to_binary(MyJID),jid=JID}, []).
+ gen_server:start_link(?MODULE, #conv_state{event_manager=EventManager,connection=Conn,roster=Roster,my_jid=list_to_binary(MyJID),jid=JID}, []).
init(State) ->
process_flag(trap_exit, true),
+ io:format("Starting conversation ~p with ~p~n", [self(), State#conv_state.jid]),
+
{ok, State}.
received_message(Conv, From, Type, Body) ->
gen_server:cast(Conv, {received_message, From, Type, Body}).
+send_message(Conv, Type, Body) ->
+ gen_server:cast(Conv, {send_message, Type, Body}).
handle_call(_Msg, _From, State) ->
{noreply, State}.
+update_view(State, From, Type, Body) ->
+ Alias = ephraim_roster:get_alias(State#conv_state.roster, From),
+ gen_event:notify(State#conv_state.event_manager, {view_update, {conversation, State#conv_state.jid, {message, Alias, Type, Body}}}).
+
handle_cast({received_message, From, Type, Body}, State) ->
- gen_event:notify(State#conv_state.event_manager, {view_update, {conversation, State#conv_state.conv_ref, {message, From, Type, Body}}}),
+ update_view(State, From, Type, Body),
+ {noreply, State};
+handle_cast({send_message, Type, Body}, State) ->
+ Packet = exmpp_message:normal(Body),
+ Packet2 = exmpp_message:set_type(Packet, Type),
+ Packet3 = exmpp_xml:set_attribute(Packet2, to, State#conv_state.jid),
+ ephraim_conn:send_packet(State#conv_state.connection, Packet3),
+
+ update_view(State, State#conv_state.my_jid, Type, Body),
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
@@ -41,38 +55,7 @@ handle_cast(_Msg, State) ->
handle_info(_Msg, State) ->
{ok, State}.
terminate(_Reason, State) ->
- io:format("Stopping conversation ~p with ~p~n", [State#conv_state.conv_ref, State#conv_state.jid]),
+ io:format("Stopping conversation ~p with ~p~n", [self(), State#conv_state.jid]),
ok.
code_change(_OldVersion, State, _Extra) ->
{ok, State}.
-
-
--spec loop(#conv_state{}) -> ok.
-loop(State) ->
- receive
- stop ->
- ok;
-
- {receive_message, Packet} ->
- Type = exmpp_message:get_type(Packet),
- Body = exmpp_message:get_body(Packet),
- if
- Body =/= undefined ->
- ephraim ! {ui_update, {chat_message, State#conv_state.jid, Type, ephraim:get_alias(State#conv_state.jid), Body}};
- true ->
- io:format("Received strange message from ~p:~n~p~n", [State#conv_state.jid, Packet])
- end,
- loop(State);
-
- {send_message, Type, Message} ->
- Packet = exmpp_message:normal(Message),
- Packet2 = exmpp_message:set_type(Packet, Type),
- Packet3 = exmpp_xml:set_attribute(Packet2, to, State#conv_state.jid),
- ephraim ! {send_packet, Packet3},
- ephraim ! {ui_update, {chat_message, State#conv_state.jid, Type, ephraim:get_alias(State#conv_state.my_jid), Message}},
- loop(State);
-
- Msg ->
- io:format("ephraim_conv (~p): ~p~n", [State#conv_state.jid, Msg]),
- loop(State)
- end.