summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/ephraim.erl45
-rw-r--r--src/core/ephraim_conn.erl25
-rw-r--r--src/core/ephraim_conv.erl6
-rw-r--r--src/core/ephraim_roster.erl109
-rw-r--r--src/core/ephraim_util.erl10
6 files changed, 176 insertions, 21 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 470e686..b0ab82c 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -2,4 +2,6 @@ erl_target(ephraim-core
ephraim.erl
ephraim_conn.erl
ephraim_conv.erl
+ ephraim_roster.erl
+ ephraim_util.erl
)
diff --git a/src/core/ephraim.erl b/src/core/ephraim.erl
index 7be93d2..dc8382d 100644
--- a/src/core/ephraim.erl
+++ b/src/core/ephraim.erl
@@ -2,19 +2,15 @@
-compile([debug_info, export_all]).
-record(state, {
- conn :: pid(),
- convs :: dict(),
- uis :: set()
+ conn :: pid(),
+ roster :: pid(),
+ convs :: dict(),
+ uis :: set()
}).
-spec start() -> ok.
start() ->
- Pid = spawn(?MODULE, init, []),
- case register(ephraim, Pid) of
- true ->
- ok
- end,
- open_port({spawn_executable, "ephraim-gtk"}, [stream, use_stdio, out]).
+ spawn(?MODULE, init, []).
-spec stop() -> ok.
stop() ->
@@ -23,8 +19,12 @@ stop() ->
-spec init() -> ok.
init() ->
+ register(ephraim, self()),
+ open_port({spawn_executable, "ephraim-gtk"}, [stream, use_stdio, out]),
+
Conn = spawn(ephraim_conn, init, []),
- loop(#state{conn=Conn,convs=dict:new(),uis=sets:new()}),
+ Roster = spawn(ephraim_roster, init, []),
+ loop(#state{conn=Conn,roster=Roster,convs=dict:new(),uis=sets:new()}),
init:stop().
-spec get_conv(#state{},exmpp_jid:jid()) -> {#state{},pid()}.
@@ -43,14 +43,18 @@ get_conv(State, JID) ->
loop(State) ->
receive
stop ->
- dict:fold(fun(_,Conv,Msg) -> Conv ! Msg end, stop, State#state.convs),
+ %dict:fold(fun(_,Conv,Msg) -> Conv ! Msg end, stop, State#state.convs),
+ ephraim_util:send_all_values(State#state.convs, stop),
State#state.conn ! stop,
+ State#state.roster ! stop,
ok;
+
{register_ui, Pid} ->
io:format("Registered UI ~p~n", [Pid]),
UIs = State#state.uis,
State2 = State#state{uis=sets:add_element(Pid,UIs)},
loop(State2);
+
{unregister_ui, Pid} ->
io:format("Unregistered UI ~p~n", [Pid]),
UIs = State#state.uis,
@@ -62,10 +66,29 @@ loop(State) ->
ok
end,
loop(State2);
+
+ {ui_update, Msg} ->
+ ephraim_util:send_all(sets:to_list(State#state.uis), Msg),
+ loop(State);
+
+ {roster, Packet} ->
+ State#state.roster ! Packet,
+ loop(State);
+
{receive_message, From, Packet} ->
{State2, Conv} = get_conv(State, From),
Conv ! {receive_message, Packet},
loop(State2);
+
+ {receive_iq, IQ} ->
+ case IQ of
+ {iq, response, result, _, 'jabber:iq:roster', Payload, _, _, 'jabber:client'} ->
+ State#state.roster ! {roster_iq, Payload};
+ _ ->
+ io:format("ephraim: IQ: ~p~n", [IQ])
+ end,
+ loop(State);
+
Msg ->
io:format("ephraim: ~p~n", [Msg]),
loop(State)
diff --git a/src/core/ephraim_conn.erl b/src/core/ephraim_conn.erl
index 18edd6a..c4b163e 100644
--- a/src/core/ephraim_conn.erl
+++ b/src/core/ephraim_conn.erl
@@ -1,10 +1,9 @@
-module(ephraim_conn).
--include_lib("exmpp/include/exmpp_client.hrl").
-compile([debug_info, export_all]).
+-include_lib("exmpp/include/exmpp_client.hrl").
-record(conn_state, {
- session :: any(),
- roster :: any()
+ session :: any()
}).
-spec init() -> ok.
@@ -13,13 +12,20 @@ init() ->
Session = exmpp_session:start(),
JID = exmpp_jid:make("ephraim-test", "jabber.ccc.de", "Bar"),
exmpp_session:auth_basic_digest(Session, JID, "foobarbla"),
+ io:format("Connecting...~n"),
exmpp_session:connect_TCP(Session, "jabber.ccc.de", 5222),
+ io:format("Connected.~n"),
session(#conn_state{session=Session}).
-spec session(#conn_state{}) -> ok.
-session(State) ->
+session(State) ->
+ io:format("Logging in...~n"),
exmpp_session:login(State#conn_state.session),
+ io:format("Setting presence...~n"),
exmpp_session:send_packet(State#conn_state.session, exmpp_presence:set_status(exmpp_presence:available(), "Foo/Test\\Bar")),
+ io:format("Getting roster...~n"),
+ exmpp_session:send_packet(State#conn_state.session, exmpp_client_roster:get_roster("foo")),
+ io:format("Ok.~n"),
loop(State).
-spec loop(#conn_state{}) -> ok.
@@ -29,16 +35,19 @@ loop(State) ->
exmpp_session:stop(State#conn_state.session);
#received_packet{packet_type=presence, raw_packet=Packet} ->
- From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
- Type = atom_to_list(exmpp_presence:get_type(Packet)),
- Status = exmpp_presence:get_status(Packet),
- io:format("~ts is now ~ts: ~ts~n", [From, Type, Status]),
+ ephraim ! {roster, {presence, Packet}},
loop(State);
#received_packet{packet_type=message, raw_packet=Packet} ->
From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
ephraim ! {receive_message, From, Packet},
loop(State);
+
+ #received_packet{packet_type=iq, raw_packet=Packet} ->
+ IQ = exmpp_iq:xmlel_to_iq(Packet),
+ ephraim ! {receive_iq, IQ},
+ loop(State);
+
Msg ->
io:format("ephraim_conn: ~p~n", [Msg]),
loop(State)
diff --git a/src/core/ephraim_conv.erl b/src/core/ephraim_conv.erl
index 00b2bc1..8f53098 100644
--- a/src/core/ephraim_conv.erl
+++ b/src/core/ephraim_conv.erl
@@ -16,9 +16,11 @@ loop(State) ->
receive
stop ->
ok;
- {receive_message, Packet} ->
- io:format("Received packet from ~p: ~p~n", [State#conv_state.jid, Packet]),
+
+ {receive_message, _Packet} ->
+ %io:format("Received packet from ~p: ~p~n", [State#conv_state.jid, Packet]),
loop(State);
+
Msg ->
io:format("ephraim_conv (~p): ~p~n", [State#conv_state.jid, Msg]),
loop(State)
diff --git a/src/core/ephraim_roster.erl b/src/core/ephraim_roster.erl
new file mode 100644
index 0000000..4f8dfef
--- /dev/null
+++ b/src/core/ephraim_roster.erl
@@ -0,0 +1,109 @@
+-module(ephraim_roster).
+-compile([debug_info, export_all]).
+-include_lib("exmpp/include/exmpp.hrl").
+
+-record(roster, {
+ entries :: dict()
+ }).
+
+-record(roster_entry, {
+ name :: binary() | undefined,
+ subscription :: atom() | undefined,
+ resources :: dict()
+ }).
+
+-record(resource_entry, {
+ priority :: integer(),
+ show :: atom(),
+ status :: string()
+ }).
+
+
+-spec init() -> ok.
+init() ->
+ loop(#roster{entries=dict:new()}).
+
+-spec updateResource(#roster{}, binary(), integer(), atom(), atom(), string()) -> #roster{}.
+updateResource(Roster, JID, Priority, Type, Show, Status) ->
+ {Node, Domain, Resource} = exmpp_jid:to_lower(exmpp_jid:parse(JID)),
+ BareJID = list_to_binary([Node, <<"@">>, Domain]),
+
+ RosterEntry = case dict:find(BareJID, Roster#roster.entries) of
+ {ok, Entry} ->
+ Entry;
+ error ->
+ #roster_entry{resources=dict:new()}
+ end,
+
+ Resources = case Type of
+ available ->
+ ResourceEntry = #resource_entry{priority=Priority,show=Show,status=Status},
+ dict:store(Resource, ResourceEntry, RosterEntry#roster_entry.resources);
+ _ ->
+ RosterEntry#roster_entry.resources
+ end,
+ Entries = dict:store(BareJID, RosterEntry#roster_entry{resources=Resources}, Roster#roster.entries),
+ if RosterEntry#roster_entry.subscription =:= undefined ->
+ ok;
+ true ->
+ ephraim ! {ui_update, {roster_update, JID, dict:to_list(Resources)}}
+ end,
+ Roster#roster{entries=Entries}.
+
+-spec updateRosterEntry(#roster{}, binary(), binary(), atom()) -> #roster{}.
+updateRosterEntry(Roster, JID, Name, Subscription) ->
+ Resources = case dict:find(JID, Roster#roster.entries) of
+ {ok, Entry} ->
+ Entry#roster_entry.resources;
+ error ->
+ dict:new()
+ end,
+ NewEntry = #roster_entry{subscription=Subscription,name=Name,resources=Resources},
+ Entries = dict:store(JID, NewEntry, Roster#roster.entries),
+ ephraim ! {ui_update, {roster_update, JID, dict:to_list(Resources)}},
+ Roster#roster{entries=Entries}.
+
+-spec handleRosterIQ(#roster{}, #xmlel{}) -> #roster{}.
+handleRosterIQ(Roster, Item) ->
+ JID = exmpp_xml:get_attribute(Item, jid, undefined),
+ Name = exmpp_xml:get_attribute(Item, name, undefined),
+ Subscription = binary_to_atom(exmpp_xml:get_attribute(Item, subscription, undefined), utf8),
+
+ updateRosterEntry(Roster, JID, Name, Subscription).
+
+-spec handleRosterIQs(#roster{}, [#xmlel{}]) -> #roster{}.
+handleRosterIQs(Roster, []) ->
+ Roster;
+
+handleRosterIQs(Roster, [Item = #xmlel{name=item}|Rest]) ->
+ Roster2 = handleRosterIQ(Roster, Item),
+ handleRosterIQs(Roster2, Rest);
+
+handleRosterIQs(Roster, [_|Rest]) ->
+ handleRosterIQs(Roster, Rest).
+
+-spec loop(#roster{}) -> ok.
+loop(Roster) ->
+ receive
+ stop ->
+ ok;
+
+ {presence, Packet} ->
+ From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
+
+ Priority = exmpp_presence:get_priority(Packet),
+ Type = exmpp_presence:get_type(Packet),
+ Show = exmpp_presence:get_show(Packet),
+ Status = exmpp_presence:get_status(Packet),
+
+ Roster2 = updateResource(Roster, From, Priority, Type, Show, Status),
+ loop(Roster2);
+
+ {roster_iq, Payload} ->
+ Roster2 = handleRosterIQs(Roster, Payload#xmlel.children),
+ loop(Roster2);
+
+ Msg ->
+ io:format("ephraim_roster: ~p~n", [Msg]),
+ loop(Roster)
+ end.
diff --git a/src/core/ephraim_util.erl b/src/core/ephraim_util.erl
new file mode 100644
index 0000000..73cb7fc
--- /dev/null
+++ b/src/core/ephraim_util.erl
@@ -0,0 +1,10 @@
+-module(ephraim_util).
+-compile([debug_info, export_all]).
+
+-spec send_all([term()], term()) -> term().
+send_all(Dest, Message) ->
+ lists:foldr(fun erlang:send/2, Message, Dest).
+
+-spec send_all_values(dict(), term()) -> term().
+send_all_values(Dest, Message) ->
+ dict:fold(fun(_,Value,Msg) -> Value ! Msg end, Message, Dest).