From 4716cfc2cbf012a070fec8db4856c1761f8a50ee Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 13 Jul 2010 07:30:46 +0200 Subject: Added VCard support to roster --- src/core/ephraim.erl | 6 +++-- src/core/ephraim_conn.erl | 6 ++++- src/core/ephraim_roster.erl | 66 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/core/ephraim.erl b/src/core/ephraim.erl index b32e1d7..a7a872b 100644 --- a/src/core/ephraim.erl +++ b/src/core/ephraim.erl @@ -121,12 +121,14 @@ loop(State) -> State#state.event ! {receive_event, From, Packet}, loop(State); - {receive_iq, IQ} -> + {receive_iq, From, IQ} -> case IQ of {iq, response, result, _, 'jabber:iq:roster', Payload, _, _, 'jabber:client'} -> State#state.roster ! {roster_iq, Payload}; + {iq, response, result, _, 'vcard-temp', Payload, _, _, 'jabber:client'} -> + State#state.roster ! {vcard_iq, From, Payload}; _ -> - io:format("ephraim: IQ: ~p~n", [IQ]) + io:format("ephraim: IQ from ~p: ~p~n", [From, IQ]) end, loop(State); diff --git a/src/core/ephraim_conn.erl b/src/core/ephraim_conn.erl index 7a6ddae..65bbacc 100644 --- a/src/core/ephraim_conn.erl +++ b/src/core/ephraim_conn.erl @@ -1,6 +1,7 @@ -module(ephraim_conn). -compile([debug_info, export_all]). -include_lib("exmpp/include/exmpp_client.hrl"). +-include_lib("exmpp/include/exmpp_xml.hrl"). -record(conn_state, { session :: any() @@ -21,6 +22,8 @@ init() -> session(State) -> io:format("Logging in...~n"), exmpp_session:login(State#conn_state.session), + io:format("Getting profile...~n"), + exmpp_session:send_packet(State#conn_state.session, exmpp_iq:get('jabber:client', #xmlel{ns='vcard-temp',name='vCard'})), io:format("Getting roster...~n"), exmpp_session:send_packet(State#conn_state.session, exmpp_client_roster:get_roster("foo")), io:format("Setting presence...~n"), @@ -49,8 +52,9 @@ loop(State) -> loop(State); #received_packet{packet_type=iq, raw_packet=Packet} -> + From = exmpp_stanza:get_sender(Packet), IQ = exmpp_iq:xmlel_to_iq(Packet), - ephraim ! {receive_iq, IQ}, + ephraim ! {receive_iq, From, IQ}, loop(State); {send_packet, Packet} -> diff --git a/src/core/ephraim_roster.erl b/src/core/ephraim_roster.erl index 2426761..b596837 100644 --- a/src/core/ephraim_roster.erl +++ b/src/core/ephraim_roster.erl @@ -11,11 +11,12 @@ }). -record(roster_entry, { - name :: binary() | undefined, - subscription :: atom() | undefined, - groups :: set(), - resources :: dict(), - avatar :: #avatar{} | undefined + name :: binary() | undefined, + subscription :: atom() | undefined, + groups = sets:new() :: set(), + resources = dict:new() :: dict(), + vcard = dict:new() :: dict(), + avatar :: #avatar{} | undefined }). -record(resource_entry, { @@ -37,7 +38,7 @@ updateResource(Roster, JID, Priority, Type, Show, Status) -> {ok, Entry} -> Entry; error -> - #roster_entry{resources=dict:new()} + #roster_entry{} end, Resources = case Type of @@ -58,13 +59,28 @@ updateResource(Roster, JID, Priority, Type, Show, Status) -> -spec updateRosterEntry(#roster{}, binary(), binary(), atom(), set()) -> #roster{}. updateRosterEntry(Roster, JID, Name, Subscription, Groups) -> - 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,groups=Groups}, + OldEntry = case dict:find(JID, Roster#roster.entries) of + {ok, Entry} -> + Entry; + error -> + #roster_entry{} + end, + NewEntry = OldEntry#roster_entry{subscription=Subscription,name=Name,groups=Groups}, + + Entries = dict:store(JID, NewEntry, Roster#roster.entries), + uiUpdate(JID, NewEntry), + + Roster#roster{entries=Entries}. + +-spec updateVCard(#roster{}, binary(), dict()) -> #roster{}. +updateVCard(Roster, JID, VCard) -> + OldEntry = case dict:find(JID, Roster#roster.entries) of + {ok, Entry} -> + Entry; + error -> + #roster_entry{} + end, + NewEntry = OldEntry#roster_entry{vcard=VCard}, Entries = dict:store(JID, NewEntry, Roster#roster.entries), uiUpdate(JID, NewEntry), @@ -77,7 +93,7 @@ updateAvatar(Roster, JID, Avatar) -> {ok, Entry} -> Entry; error -> - #roster_entry{resources=dict:new()} + #roster_entry{} end, NewEntry = RosterEntry#roster_entry{avatar=#avatar{data=Avatar}}, Entries = dict:store(JID, NewEntry, Roster#roster.entries), @@ -127,6 +143,20 @@ handleRosterIQs(Roster, [Item = #xmlel{ns='jabber:iq:roster',name=item}|Rest]) - handleRosterIQs(Roster, [_|Rest]) -> handleRosterIQs(Roster, Rest). + +-spec handleVCardIQ(dict(), #xmlel{}) -> dict(). +handleVCardIQ(Dict, Item=#xmlel{name=Key}) -> + Value = exmpp_xml:get_cdata(Item), + dict:store(Key, Value, Dict). + +-spec handleVCardIQs(dict(), [#xmlel{}]) -> dict(). +handleVCardIQs(VCard, []) -> + VCard; + +handleVCardIQs(VCard, [Item|Rest]) -> + VCard2 = handleVCardIQ(VCard, Item), + handleVCardIQs(VCard2, Rest). + -spec loop(#roster{}) -> ok. loop(Roster) -> receive @@ -150,8 +180,12 @@ loop(Roster) -> {roster_iq, Payload} -> Roster2 = handleRosterIQs(Roster, Payload#xmlel.children), - %io:format("ephraim_roster: IQ: ~p~n", [Payload]), - loop(Roster2); + loop(Roster2); + + {vcard_iq, From, Payload} -> + Dict = handleVCardIQs(dict:new(), Payload#xmlel.children), + Roster2 = updateVCard(Roster, From, Dict), + loop(Roster2); Msg -> io:format("ephraim_roster: ~p~n", [Msg]), -- cgit v1.2.3