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_roster.erl | 66 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 16 deletions(-) (limited to 'src/core/ephraim_roster.erl') 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