summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conv.erl
blob: 3342bc91f9ccd954246f61dd316048db11d953c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-module(ephraim_conv).
-compile([debug_info, export_all]).

-record(conv_state, {
	  my_jid :: binary(),
	  jid    :: binary()
	 }).

-spec init(binary()) -> ok.
init(JID) ->
    io:format("Starting a conversation with ~p~n", [JID]),
    
    {jid,MyJID} = ephraim_config:get(jid),
    loop(#conv_state{my_jid=list_to_binary(MyJID),jid=JID}),
    io:format("Stopping a conversation with ~p~n", [JID]).

-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.