plugins/mod_storage_xep0227.lua
changeset 12086 e87563fefd85
parent 11844 5e9e75c277a2
child 12177 270047afa6af
equal deleted inserted replaced
12085:6cc3135138d7 12086:e87563fefd85
     1 
     1 
     2 local ipairs, pairs = ipairs, pairs;
     2 local ipairs, pairs = ipairs, pairs;
     3 local setmetatable = setmetatable;
     3 local setmetatable = setmetatable;
     4 local tostring = tostring;
     4 local tostring = tostring;
     5 local next, unpack = next, table.unpack or unpack; --luacheck: ignore 113/unpack
     5 local next, unpack = next, table.unpack or unpack; --luacheck: ignore 113/unpack
     6 local t_remove = table.remove;
       
     7 local os_remove = os.remove;
     6 local os_remove = os.remove;
     8 local io_open = io.open;
     7 local io_open = io.open;
     9 local jid_bare = require "util.jid".bare;
     8 local jid_bare = require "util.jid".bare;
    10 local jid_prep = require "util.jid".prep;
     9 local jid_prep = require "util.jid".prep;
    11 
    10 
    63 end
    62 end
    64 local function createOuterXml(user, host)
    63 local function createOuterXml(user, host)
    65 	return st.stanza("server-data", {xmlns='urn:xmpp:pie:0'})
    64 	return st.stanza("server-data", {xmlns='urn:xmpp:pie:0'})
    66 		:tag("host", {jid=host})
    65 		:tag("host", {jid=host})
    67 			:tag("user", {name = user});
    66 			:tag("user", {name = user});
    68 end
       
    69 local function removeFromArray(arr, value)
       
    70 	for i,item in ipairs(arr) do
       
    71 		if item == value then
       
    72 			t_remove(arr, i);
       
    73 			return;
       
    74 		end
       
    75 	end
       
    76 end
       
    77 local function removeStanzaChild(s, child)
       
    78 	removeFromArray(s.tags, child);
       
    79 	removeFromArray(s, child);
       
    80 end
    67 end
    81 
    68 
    82 local function hex_to_base64(s)
    69 local function hex_to_base64(s)
    83 	return base64.encode(hex.from(s));
    70 	return base64.encode(hex.from(s));
    84 end
    71 end
   169 	end;
   156 	end;
   170 	set = function(self, user, data)
   157 	set = function(self, user, data)
   171 		local xml = getXml(user, self.host);
   158 		local xml = getXml(user, self.host);
   172 		local usere = xml and getUserElement(xml);
   159 		local usere = xml and getUserElement(xml);
   173 		if usere then
   160 		if usere then
   174 			local vcard = usere:get_child("vCard", 'vcard-temp');
   161 			usere:remove_children("vCard", "vcard-temp");
   175 			if vcard then
   162 			if not data then
   176 				removeStanzaChild(usere, vcard);
   163 				-- No data to set, old one deleted, success
   177 			elseif not data then
       
   178 				return true;
   164 				return true;
   179 			end
   165 			end
   180 			if data then
   166 			local vcard = st.deserialize(data);
   181 				vcard = st.deserialize(data);
   167 			usere:add_child(vcard);
   182 				usere:add_child(vcard);
       
   183 			end
       
   184 			return setXml(user, self.host, xml);
   168 			return setXml(user, self.host, xml);
   185 		end
   169 		end
   186 		return true;
   170 		return true;
   187 	end;
   171 	end;
   188 };
   172 };
   202 	end;
   186 	end;
   203 	set = function(self, user, data)
   187 	set = function(self, user, data)
   204 		local xml = getXml(user, self.host);
   188 		local xml = getXml(user, self.host);
   205 		local usere = xml and getUserElement(xml);
   189 		local usere = xml and getUserElement(xml);
   206 		if usere then
   190 		if usere then
   207 			local private = usere:get_child("query", 'jabber:iq:private');
   191 			usere:remove_children("query", "jabber:iq:private");
   208 			if private then removeStanzaChild(usere, private); end
       
   209 			if data and next(data) ~= nil then
   192 			if data and next(data) ~= nil then
   210 				private = st.stanza("query", {xmlns='jabber:iq:private'});
   193 				local private = st.stanza("query", {xmlns='jabber:iq:private'});
   211 				for _,tag in pairs(data) do
   194 				for _,tag in pairs(data) do
   212 					private:add_child(st.deserialize(tag));
   195 					private:add_child(st.deserialize(tag));
   213 				end
   196 				end
   214 				usere:add_child(private);
   197 				usere:add_child(private);
   215 			end
   198 			end
   252 	end;
   235 	end;
   253 	set = function(self, user, data)
   236 	set = function(self, user, data)
   254 		local xml = getXml(user, self.host);
   237 		local xml = getXml(user, self.host);
   255 		local usere = xml and getUserElement(xml);
   238 		local usere = xml and getUserElement(xml);
   256 		if usere then
   239 		if usere then
   257 			local roster = usere:get_child("query", 'jabber:iq:roster');
   240 			usere:remove_children("query", "jabber:iq:roster");
   258 			if roster then removeStanzaChild(usere, roster); end
       
   259 			usere:maptags(function (tag)
   241 			usere:maptags(function (tag)
   260 				if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
   242 				if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
   261 					return nil;
   243 					return nil;
   262 				end
   244 				end
   263 				return tag;
   245 				return tag;
   264 			end);
   246 			end);
   265 			if data and next(data) ~= nil then
   247 			if data and next(data) ~= nil then
   266 				roster = st.stanza("query", {xmlns='jabber:iq:roster'});
   248 				local roster = st.stanza("query", {xmlns='jabber:iq:roster'});
   267 				usere:add_child(roster);
   249 				usere:add_child(roster);
   268 				for jid, item in pairs(data) do
   250 				for jid, item in pairs(data) do
   269 					if jid then
   251 					if jid then
   270 						roster:tag("item", {
   252 						roster:tag("item", {
   271 							jid = jid,
   253 							jid = jid,