Comparison

plugins/mod_storage_xep0227.lua @ 8351:cc05b6366576

mod_storage_xep0227: Add roster storage (fixes #1023)
author Kim Alvefur <zash@zash.se>
date Thu, 19 Oct 2017 12:08:40 +0200
parent 8350:3ca11d408382
child 8352:6ff50541d2a6
comparison
equal deleted inserted replaced
8350:3ca11d408382 8351:cc05b6366576
162 end 162 end
163 return true; 163 return true;
164 end; 164 end;
165 }; 165 };
166 166
167 handlers.roster = {
168 get = function(self, user)
169 user = getUserElement(getXml(user, self.host));
170 if user then
171 local roster = user:get_child("query", "jabber:iq:roster");
172 if roster then
173 local r = {
174 [false] = {
175 version = roster.attr.version;
176 pending = {};
177 }
178 };
179 for item in roster:childtags("item") do
180 r[item.attr.jid] = {
181 jid = item.attr.jid,
182 subscription = item.attr.subscription,
183 ask = item.attr.ask,
184 name = item.attr.name,
185 groups = {};
186 };
187 for group in item:childtags("group") do
188 r[item.attr.jid].groups[group:get_text()] = true;
189 end
190 for pending in user:childtags("presence", "jabber:client") do
191 r[false].pending[pending.attr.from] = true;
192 end
193 end
194 return r;
195 end
196 end
197 end;
198 set = function(self, user, data)
199 local xml = getXml(user, self.host);
200 local usere = xml and getUserElement(xml);
201 if usere then
202 local roster = usere:get_child("query", 'jabber:iq:roster');
203 if roster then removeStanzaChild(usere, roster); end
204 usere:maptags(function (tag)
205 if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
206 return nil;
207 end
208 return tag;
209 end);
210 if data and next(data) ~= nil then
211 roster = st.stanza("query", {xmlns='jabber:iq:roster'});
212 usere:add_child(roster);
213 for jid, item in pairs(data) do
214 if jid then
215 roster:tag("item", {
216 jid = jid,
217 subscription = item.subscription,
218 ask = item.ask,
219 name = item.name,
220 });
221 for group in pairs(item.groups) do
222 roster:tag("group"):text(group):up();
223 end
224 roster:up(); -- move out from item
225 else
226 roster.attr.version = item.version;
227 for pending_jid in pairs(item.pending) do
228 usere:add_child(st.presence({ from = pending_jid, type = "subscribe" }));
229 end
230 end
231 end
232 end
233 return setXml(user, self.host, xml);
234 end
235 return true;
236 end;
237 };
238
239
167 ----------------------------- 240 -----------------------------
168 local driver = {}; 241 local driver = {};
169 242
170 function driver:open(datastore, typ) 243 function driver:open(datastore, typ)
171 if typ and typ ~= "keyval" then return nil, "unsupported-store"; end 244 if typ and typ ~= "keyval" then return nil, "unsupported-store"; end