3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
12 package.path = package.path ..";../?.lua";
13 local serialize = require "util.serialization".serialize;
14 local st = require "util.stanza";
15 package.loaded["util.logger"] = {init = function() return function() end; end}
16 local dm = require "util.datamanager"
17 dm.set_data_path("data");
19 function parseFile(filename)
25 local function read(expected)
28 ch = last; last = nil;
31 if ch == "\n" then line = line + 1; end
33 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil").." on line "..line); end
36 local function pushback(ch)
37 if last then error(); end
41 if not last then last = read(); end
58 local function unescape(s)
59 return escapes[s] or error("Unknown escape sequence: "..s);
61 local function readString()
67 s = s..unescape(read()..read());
77 local function readNonString()
80 if peek() == "," or peek() == ")" then
88 local function readItem()
92 return readNonString();
95 local function readTuple()
98 while peek() ~= ")" do
99 table.insert(items, readItem());
100 if peek() == ")" then break; end
106 local function readTuples()
107 if peek() ~= "(" then read("("); end
110 table.insert(tuples, readTuple());
111 if peek() == "," then read() end
112 if peek() == ";" then break; end
116 local function readTableName()
118 while peek() ~= "`" do tname = tname..read(); end
121 local function readInsert()
122 if peek() == nil then return nil; end
123 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
126 else -- match failed, skip line
127 while peek() and read() ~= "\n" do end
131 local tname = readTableName();
132 read("`"); read(" ") -- expect this
133 if peek() == "(" then -- skip column list
134 repeat until read() == ")";
137 for ch in ("VALUES "):gmatch(".") do read(ch); end -- expect this
138 local tuples = readTuples();
139 read(";"); read("\n");
140 return tname, tuples;
143 local function readFile(filename)
144 file = io.open(filename);
145 if not file then error("File not found: "..filename); os.exit(0); end
148 local tname, tuples = readInsert();
151 local t_name = t[tname];
153 table.insert(t_name, tuples[i]);
158 elseif peek() == nil then
165 return readFile(filename);
171 local parse_xml = (function()
172 local entity_map = setmetatable({
178 }, {__index = function(_, s)
179 if s:sub(1,1) == "#" then
180 if s:sub(2,2) == "x" then
181 return string.char(tonumber(s:sub(3), 16));
183 return string.char(tonumber(s:sub(2)));
188 local function xml_unescape(str)
189 return (str:gsub("&(.-);", entity_map));
191 local function parse_tag(s)
192 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
194 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
198 local stanza = st.stanza("root");
199 local regexp = "<([^>]*)>([^<]*)";
200 for elem, text in xml:gmatch(regexp) do
201 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
202 elseif elem:sub(1,1) == "/" then -- end tag
204 stanza:up(); -- TODO check for start-end tag name match
205 elseif elem:sub(-1,-1) == "/" then -- empty tag
206 elem = elem:sub(1,-2);
207 local name,attr = parse_tag(elem);
208 stanza:tag(name, attr):up();
210 local name,attr = parse_tag(elem);
211 stanza:tag(name, attr);
213 if #text ~= 0 then -- text
214 stanza:text(xml_unescape(text));
217 return stanza.tags[1];
222 local arg, host = ...;
223 local help = "/? -? ? /h -h /help -help --help";
224 if not(arg and host) or help:find(arg, 1, true) then
225 print([[ejabberd SQL DB dump importer for Prosody
227 Usage: ejabberdsql2prosody.lua filename.txt hostname
229 The file can be generated using mysqldump:
230 mysqldump db_name > filename.txt]]);
234 ["last"] = {"username", "seconds", "state"};
235 ["privacy_default_list"] = {"username", "name"};
236 ["privacy_list"] = {"username", "name", "id"};
237 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
238 ["private_storage"] = {"username", "namespace", "data"};
239 ["rostergroups"] = {"username", "jid", "grp"};
240 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
241 ["spool"] = {"username", "xml", "seq"};
242 ["users"] = {"username", "password"};
243 ["vcard"] = {"username", "vcard"};
244 --["vcard_search"] = {};
247 local t = parseFile(arg);
248 for name, data in pairs(t) do
251 if #data > 0 and #data[1] ~= #m then
252 print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
263 --print(serialize(t));
265 for i, row in ipairs(t["users"] or NULL) do
266 local node, password = row.username, row.password;
267 local ret, err = dm.store(node, host, "accounts", {password = password});
268 print("["..(err or "success").."] accounts: "..node.."@"..host);
271 function roster(node, host, jid, item)
272 local roster = dm.load(node, host, "roster") or {};
274 local ret, err = dm.store(node, host, "roster", roster);
275 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
277 function roster_pending(node, host, jid)
278 local roster = dm.load(node, host, "roster") or {};
279 roster.pending = roster.pending or {};
280 roster.pending[jid] = true;
281 local ret, err = dm.store(node, host, "roster", roster);
282 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
284 function roster_group(node, host, jid, group)
285 local roster = dm.load(node, host, "roster") or {};
286 local item = roster[jid];
287 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
288 item.groups[group] = true;
289 local ret, err = dm.store(node, host, "roster", roster);
290 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
292 function private_storage(node, host, xmlns, stanza)
293 local private = dm.load(node, host, "private") or {};
294 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
295 local ret, err = dm.store(node, host, "private", private);
296 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
298 function offline_msg(node, host, t, stanza)
299 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
300 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
301 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
302 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
304 for i, row in ipairs(t["rosterusers"] or NULL) do
305 local node, contact = row.username, row.jid;
306 local name = row.nick;
307 if name == "" then name = nil; end
308 local subscription = row.subscription;
309 if subscription == "N" then
310 subscription = "none"
311 elseif subscription == "B" then
312 subscription = "both"
313 elseif subscription == "F" then
314 subscription = "from"
315 elseif subscription == "T" then
317 else error("Unknown subscription type: "..subscription) end;
321 elseif ask == "O" then
323 elseif ask == "I" then
324 roster_pending(node, host, contact);
326 elseif ask == "B" then
327 roster_pending(node, host, contact);
329 else error("Unknown ask type: "..ask); end
330 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
331 roster(node, host, contact, item);
333 for i, row in ipairs(t["rostergroups"] or NULL) do
334 roster_group(row.username, host, row.jid, row.grp);
336 for i, row in ipairs(t["vcard"] or NULL) do
337 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard)));
338 print("["..(err or "success").."] vCard: "..row.username.."@"..host);
340 for i, row in ipairs(t["private_storage"] or NULL) do
341 private_storage(row.username, host, row.namespace, parse_xml(row.data));
343 table.sort(t["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case
344 local time_offset = os.difftime(os.time(os.date("!*t")), os.time(os.date("*t"))) -- to deal with timezones
345 local date_parse = function(s)
346 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)");
347 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec-time_offset});
349 for i, row in ipairs(t["spool"] or NULL) do
350 local stanza = parse_xml(row.xml);
351 local last_child = stanza.tags[#stanza.tags];
352 if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end
353 if last_child.name ~= "x" and last_child.attr.xmlns ~= "jabber:x:delay" then error("Last child of offline message is not a timestamp"); end
354 stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil;
355 local t = date_parse(last_child.attr.stamp);
356 offline_msg(row.username, host, t, stanza);