3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 package.path = package.path ..";../?.lua";
11 local serialize = require "util.serialization".serialize;
12 local st = require "util.stanza";
13 package.loaded["util.logger"] = {init = function() return function() end; end}
14 local dm = require "util.datamanager"
15 dm.set_data_path("data");
17 function parseFile(filename)
22 local function read(expected)
25 ch = last; last = nil;
26 else ch = file:read(1); end
27 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end
30 local function pushback(ch)
31 if last then error(); end
35 if not last then last = read(); end
52 local function unescape(s)
53 return escapes[s] or error("Unknown escape sequence: "..s);
55 local function readString()
61 s = s..unescape(read()..read());
71 local function readNonString()
74 if peek() == "," or peek() == ")" then
82 local function readItem()
86 return readNonString();
89 local function readTuple()
92 while peek() ~= ")" do
93 table.insert(items, readItem());
94 if peek() == ")" then break; end
100 local function readTuples()
101 if peek() ~= "(" then read("("); end
104 table.insert(tuples, readTuple());
105 if peek() == "," then read() end
106 if peek() == ";" then break; end
110 local function readTableName()
112 while peek() ~= "`" do tname = tname..read(); end
115 local function readInsert()
116 if peek() == nil then return nil; end
117 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
120 else -- match failed, skip line
121 while peek() and read() ~= "\n" do end
125 local tname = readTableName();
126 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
127 local tuples = readTuples();
128 read(";"); read("\n");
129 return tname, tuples;
132 local function readFile(filename)
133 file = io.open(filename);
134 if not file then error("File not found: "..filename); os.exit(0); end
137 local tname, tuples = readInsert();
140 local t_name = t[name];
142 table.insert(t_name, tuples[i]);
147 elseif peek() == nil then
154 return readFile(filename);
160 local parse_xml = (function()
161 local entity_map = setmetatable({
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));
172 return string.char(tonumber(s:sub(2)));
177 local function xml_unescape(str)
178 return (str:gsub("&(.-);", entity_map));
180 local function parse_tag(s)
181 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
183 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
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
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();
199 local name,attr = parse_tag(elem);
200 stanza:tag(name, attr);
202 if #text ~= 0 then -- text
203 stanza:text(xml_unescape(text));
206 return stanza.tags[1];
211 local arg, host = ...;
212 local help = "/? -? ? /h -h /help -help --help";
213 if not(arg and host) or help:find(arg, 1, true) then
214 print([[ejabberd SQL DB dump importer for Prosody
216 Usage: ejabberdsql2prosody.lua filename.txt hostname
218 The file can be generated using mysqldump:
219 mysqldump db_name > filename.txt]]);
223 ["last"] = {"username", "seconds", "state"};
224 ["privacy_default_list"] = {"username", "name"};
225 ["privacy_list"] = {"username", "name", "id"};
226 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
227 ["private_storage"] = {"username", "namespace", "data"};
228 ["rostergroups"] = {"username", "jid", "grp"};
229 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
230 ["spool"] = {"username", "xml", "seq"};
231 ["users"] = {"username", "password"};
232 ["vcard"] = {"username", "vcard"};
233 --["vcard_search"] = {};
236 local t = parseFile(arg);
237 for name, data in pairs(t) do
240 if #data > 0 and #data[1] ~= #m then
241 print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
252 --print(serialize(t));
254 for i, row in ipairs(t["users"] or NULL) do
255 local node, password = row.username, row.password;
256 local ret, err = dm.store(node, host, "accounts", {password = password});
257 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
260 function roster(node, host, jid, item)
261 local roster = dm.load(node, host, "roster") or {};
263 local ret, err = dm.store(node, host, "roster", roster);
264 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
266 function roster_pending(node, host, jid)
267 local roster = dm.load(node, host, "roster") or {};
268 roster.pending = roster.pending or {};
269 roster.pending[jid] = true;
270 local ret, err = dm.store(node, host, "roster", roster);
271 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
273 function roster_group(node, host, jid, group)
274 local roster = dm.load(node, host, "roster") or {};
275 local item = roster[jid];
276 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
277 item.groups[group] = true;
278 local ret, err = dm.store(node, host, "roster", roster);
279 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
281 for i, row in ipairs(t["rosterusers"] or NULL) do
282 local node, contact = row.username, row.jid;
283 local name = row.nick;
284 if name == "" then name = nil; end
285 local subscription = row.subscription;
286 if subscription == "N" then
287 subscription = "none"
288 elseif subscription == "B" then
289 subscription = "both"
290 elseif subscription == "F" then
291 subscription = "from"
292 elseif subscription == "T" then
294 else error("Unknown subscription type: "..subscription) end;
298 elseif ask == "O" then
300 elseif ask == "I" then
301 roster_pending(node, host, contact);
303 elseif ask == "B" then
304 roster_pending(node, host, contact);
306 else error("Unknown ask type: "..ask); end
307 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
308 roster(node, host, contact, item);
310 for i, row in ipairs(t["rostergroups"] or NULL) do
311 roster_group(row.username, host, row.jid, row.grp);
313 for i, row in ipairs(t["vcard"] or NULL) do
314 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard)));
315 print("["..(err or "success").."] vCard: "..row.username.."@"..host);