comparison mod_profile/mod_profile.lua @ 1516:48d95ab404c7

mod_profile: Save photo and nickname from PEP to vCard
author Kim Alvefur <zash@zash.se>
date Sat, 27 Sep 2014 19:29:28 +0200
parents f367d7cbfaa6
children 8fbe16c83807
comparison
equal deleted inserted replaced
1515:f367d7cbfaa6 1516:48d95ab404c7
127 module:hook("iq-get/host/vcard-temp:vCard", handle_get); 127 module:hook("iq-get/host/vcard-temp:vCard", handle_get);
128 128
129 module:hook("iq-set/bare/vcard-temp:vCard", handle_set); 129 module:hook("iq-set/bare/vcard-temp:vCard", handle_set);
130 module:hook("iq-set/host/vcard-temp:vCard", handle_set); 130 module:hook("iq-set/host/vcard-temp:vCard", handle_set);
131 131
132 local function on_publish(event)
133 if event.actor == true then return end -- Not from a client
134 local node, item = event.node, event.item;
135 local username = jid_split(event.actor);
136 local data = storage:get(username) or {};
137 if node == "urn:xmpp:avatar:data" then
138 local new_photo = item:get_child_text("data", "urn:xmpp:avatar:data");
139 new_photo = new_photo and { name = "PHOTO"; ENCODING = { "b" }; new_photo } or nil;
140 local _, i = get_item(data, "PHOTO")
141 if new_photo then
142 data[i or #data+1] = new_photo;
143 elseif i then
144 table.remove(data, i);
145 end
146 elseif node == "http://jabber.org/protocol/nick" then
147 local new_nick = item:get_child_text("nick", "http://jabber.org/protocol/nick");
148 new_nick = new_nick and new_nick ~= "" and { name = "NICKNAME"; new_nick } or nil;
149 local _, i = get_item(data, "NICKNAME")
150 if new_nick then
151 data[i or #data+1] = new_nick;
152 elseif i then
153 table.remove(data, i);
154 end
155 else
156 return;
157 end
158 storage:set(username, data);
159 end
160
132 local function pep_service_added(event) 161 local function pep_service_added(event)
133 local item = event.item; 162 local item = event.item;
134 local service, username = item.service, jid_split(item.jid); 163 local service, username = item.service, jid_split(item.jid);
164 service.events.add_handler("item-published", on_publish);
135 local data = storage:get(username); 165 local data = storage:get(username);
136 if data then 166 if data then
137 update_pep(username, data, service); 167 update_pep(username, data, service);
138 end 168 end
139 end 169 end