ejabberdsql2prosody: Allow for multiple INSERTs to the same table
[prosody.git] / tools / ejabberdsql2prosody.lua
index b48f2b280d37bfafc4bfbb66afcaff1ce2073e25..5b9752692eea59d272c8257b188ae7474851fb92 100644 (file)
@@ -36,10 +36,21 @@ local function peek()
        return last;
 end
 
+local escapes = {
+       ["\\0"] = "\0";
+       ["\\'"] = "'";
+       ["\\\""] = "\"";
+       ["\\b"] = "\b";
+       ["\\n"] = "\n";
+       ["\\r"] = "\r";
+       ["\\t"] = "\t";
+       ["\\Z"] = "\26";
+       ["\\\\"] = "\\";
+       ["\\%"] = "%";
+       ["\\_"] = "_";
+}
 local function unescape(s)
-       if s == "\\'" then return "'"; end
-       if s == "\\n" then return "\n"; end
-       error("Unknown escape sequence: "..s);
+       return escapes[s] or error("Unknown escape sequence: "..s);
 end
 local function readString()
        read("'");
@@ -125,7 +136,14 @@ local function readFile(filename)
        while true do
                local tname, tuples = readInsert();
                if tname then
-                       t[tname] = tuples;
+                       if t[name] then
+                               local t_name = t[name];
+                               for i=1,#tuples do
+                                       table.insert(t_name, tuples[i]);
+                               end
+                       else
+                               t[tname] = tuples;
+                       end
                elseif peek() == nil then
                        break;
                end
@@ -167,23 +185,76 @@ local t = parseFile(arg);
 for name, data in pairs(t) do
        local m = map[name];
        if m then
+               if #data > 0 and #data[1] ~= #m then
+                       print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
+               end
                for i=1,#data do
                        local row = data[i];
-                       for j=1,#row do
+                       for j=1,#m do
                                row[m[j]] = row[j];
                                row[j] = nil;
                        end
                end
        end
 end
+--print(serialize(t));
 
 for i, row in ipairs(t["users"] or NULL) do
        local node, password = row.username, row.password;
        local ret, err = dm.store(node, host, "accounts", {password = password});
        print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
 end
-for i, row in ipairs(t["private_storage"] or NULL) do
-       --local node, password = row.username, row.password;
-       --local ret, err = dm.store(node, host, "accounts", {password = password});
-       --print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
+
+function roster(node, host, jid, item)
+       local roster = dm.load(node, host, "roster") or {};
+       roster[jid] = item;
+       local ret, err = dm.store(node, host, "roster", roster);
+       print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
+end
+function roster_pending(node, host, jid)
+       local roster = dm.load(node, host, "roster") or {};
+       roster.pending = roster.pending or {};
+       roster.pending[jid] = true;
+       local ret, err = dm.store(node, host, "roster", roster);
+       print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
+end
+function roster_group(node, host, jid, group)
+       local roster = dm.load(node, host, "roster") or {};
+       local item = roster[jid];
+       if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
+       item.groups[group] = true;
+       local ret, err = dm.store(node, host, "roster", roster);
+       print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
+end
+for i, row in ipairs(t["rosterusers"] or NULL) do
+       local node, contact = row.username, row.jid;
+       local name = row.nick;
+       if name == "" then name = nil; end
+       local subscription = row.subscription;
+       if subscription == "N" then
+               subscription = "none"
+       elseif subscription == "B" then
+               subscription = "both"
+       elseif subscription == "F" then
+               subscription = "from"
+       elseif subscription == "T" then
+               subscription = "to"
+       else error("Unknown subscription type: "..subscription) end;
+       local ask = row.ask;
+       if ask == "N" then
+               ask = nil;
+       elseif ask == "O" then
+               ask = "subscribe";
+       elseif ask == "I" then
+               roster_pending(node, host, contact);
+               ask = nil;
+       elseif ask == "B" then
+               roster_pending(node, host, contact);
+               ask = "subscribe";
+       else error("Unknown ask type: "..ask); end
+       local item = {name = name, ask = ask, subscription = subscription, groups = {}};
+       roster(node, host, contact, item);
+end
+for i, row in ipairs(t["rostergroups"] or NULL) do
+       roster_group(row.username, host, row.jid, row.grp);
 end