Comparison

tools/ejabberdsql2prosody.lua @ 1631:fa49cbfff0e6

Merge with waqas
author Matthew Wild <mwild1@gmail.com>
date Mon, 03 Aug 2009 17:53:02 +0100
parent 1630:cb3d937dd1da
child 1647:066cd784d97b
comparison
equal deleted inserted replaced
1627:6b2109012688 1631:fa49cbfff0e6
153 153
154 return readFile(filename); 154 return readFile(filename);
155 155
156 ------ 156 ------
157 end 157 end
158
159 -- XML parser
160 local parse_xml = (function()
161 local entity_map = setmetatable({
162 ["amp"] = "&";
163 ["gt"] = ">";
164 ["lt"] = "<";
165 ["apos"] = "'";
166 ["quot"] = "\"";
167 }, {__index = function(_, s)
168 if s:sub(1,1) == "#" then
169 if s:sub(2,2) == "x" then
170 return string.char(tonumber(s:sub(3), 16));
171 else
172 return string.char(tonumber(s:sub(2)));
173 end
174 end
175 end
176 });
177 local function xml_unescape(str)
178 return (str:gsub("&(.-);", entity_map));
179 end
180 local function parse_tag(s)
181 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
182 local attr = {};
183 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
184 return name, attr;
185 end
186 return function(xml)
187 local stanza = st.stanza("root");
188 local regexp = "<([^>]*)>([^<]*)";
189 for elem, text in xml:gmatch(regexp) do
190 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
191 elseif elem:sub(1,1) == "/" then -- end tag
192 elem = elem:sub(2);
193 stanza:up(); -- TODO check for start-end tag name match
194 elseif elem:sub(-1,-1) == "/" then -- empty tag
195 elem = elem:sub(1,-2);
196 local name,attr = parse_tag(elem);
197 stanza:tag(name, attr):up();
198 else -- start tag
199 local name,attr = parse_tag(elem);
200 stanza:tag(name, attr);
201 end
202 if #text ~= 0 then -- text
203 stanza:text(xml_unescape(text));
204 end
205 end
206 return stanza.tags[1];
207 end
208 end)();
209 -- end of XML parser
158 210
159 local arg, host = ...; 211 local arg, host = ...;
160 local help = "/? -? ? /h -h /help -help --help"; 212 local help = "/? -? ? /h -h /help -help --help";
161 if not(arg and host) or help:find(arg, 1, true) then 213 if not(arg and host) or help:find(arg, 1, true) then
162 print([[ejabberd SQL DB dump importer for Prosody 214 print([[ejabberd SQL DB dump importer for Prosody
223 local item = roster[jid]; 275 local item = roster[jid];
224 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end 276 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
225 item.groups[group] = true; 277 item.groups[group] = true;
226 local ret, err = dm.store(node, host, "roster", roster); 278 local ret, err = dm.store(node, host, "roster", roster);
227 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group); 279 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
280 end
281 function private_storage(node, host, xmlns, stanza)
282 local private = dm.load(node, host, "private") or {};
283 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
284 local ret, err = dm.store(node, host, "private", private);
285 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
228 end 286 end
229 for i, row in ipairs(t["rosterusers"] or NULL) do 287 for i, row in ipairs(t["rosterusers"] or NULL) do
230 local node, contact = row.username, row.jid; 288 local node, contact = row.username, row.jid;
231 local name = row.nick; 289 local name = row.nick;
232 if name == "" then name = nil; end 290 if name == "" then name = nil; end
256 roster(node, host, contact, item); 314 roster(node, host, contact, item);
257 end 315 end
258 for i, row in ipairs(t["rostergroups"] or NULL) do 316 for i, row in ipairs(t["rostergroups"] or NULL) do
259 roster_group(row.username, host, row.jid, row.grp); 317 roster_group(row.username, host, row.jid, row.grp);
260 end 318 end
319 for i, row in ipairs(t["vcard"] or NULL) do
320 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard)));
321 print("["..(err or "success").."] vCard: "..row.username.."@"..host);
322 end
323 for i, row in ipairs(t["private_storage"] or NULL) do
324 private_storage(row.username, host, row.namespace, st.preserialize(parse_xml(row.data)));
325 end