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 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
133 local tuples = readTuples();
134 read(";"); read("\n");
135 return tname, tuples;
138 local function readFile(filename)
139 file = io.open(filename);
140 if not file then error("File not found: "..filename); os.exit(0); end
143 local tname, tuples = readInsert();
146 local t_name = t[tname];
148 table.insert(t_name, tuples[i]);
153 elseif peek() == nil then
160 return readFile(filename);
166 local parse_xml = (function()
167 local entity_map = setmetatable({
173 }, {__index = function(_, s)
174 if s:sub(1,1) == "#" then
175 if s:sub(2,2) == "x" then
176 return string.char(tonumber(s:sub(3), 16));
178 return string.char(tonumber(s:sub(2)));
183 local function xml_unescape(str)
184 return (str:gsub("&(.-);", entity_map));
186 local function parse_tag(s)
187 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
189 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
193 local stanza = st.stanza("root");
194 local regexp = "<([^>]*)>([^<]*)";
195 for elem, text in xml:gmatch(regexp) do
196 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
197 elseif elem:sub(1,1) == "/" then -- end tag
199 stanza:up(); -- TODO check for start-end tag name match
200 elseif elem:sub(-1,-1) == "/" then -- empty tag
201 elem = elem:sub(1,-2);
202 local name,attr = parse_tag(elem);
203 stanza:tag(name, attr):up();
205 local name,attr = parse_tag(elem);
206 stanza:tag(name, attr);
208 if #text ~= 0 then -- text
209 stanza:text(xml_unescape(text));
212 return stanza.tags[1];
217 local arg, host = ...;
218 local help = "/? -? ? /h -h /help -help --help";
219 if not(arg and host) or help:find(arg, 1, true) then
220 print([[ejabberd SQL DB dump importer for Prosody
222 Usage: ejabberdsql2prosody.lua filename.txt hostname
224 The file can be generated using mysqldump:
225 mysqldump db_name > filename.txt]]);
229 ["last"] = {"username", "seconds", "state"};
230 ["privacy_default_list"] = {"username", "name"};
231 ["privacy_list"] = {"username", "name", "id"};
232 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
233 ["private_storage"] = {"username", "namespace", "data"};
234 ["rostergroups"] = {"username", "jid", "grp"};
235 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
236 ["spool"] = {"username", "xml", "seq"};
237 ["users"] = {"username", "password"};
238 ["vcard"] = {"username", "vcard"};
239 --["vcard_search"] = {};
242 local t = parseFile(arg);
243 for name, data in pairs(t) do
246 if #data > 0 and #data[1] ~= #m then
247 print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
258 --print(serialize(t));
260 for i, row in ipairs(t["users"] or NULL) do
261 local node, password = row.username, row.password;
262 local ret, err = dm.store(node, host, "accounts", {password = password});
263 print("["..(err or "success").."] accounts: "..node.."@"..host);
266 function roster(node, host, jid, item)
267 local roster = dm.load(node, host, "roster") or {};
269 local ret, err = dm.store(node, host, "roster", roster);
270 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
272 function roster_pending(node, host, jid)
273 local roster = dm.load(node, host, "roster") or {};
274 roster.pending = roster.pending or {};
275 roster.pending[jid] = true;
276 local ret, err = dm.store(node, host, "roster", roster);
277 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
279 function roster_group(node, host, jid, group)
280 local roster = dm.load(node, host, "roster") or {};
281 local item = roster[jid];
282 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
283 item.groups[group] = true;
284 local ret, err = dm.store(node, host, "roster", roster);
285 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
287 function private_storage(node, host, xmlns, stanza)
288 local private = dm.load(node, host, "private") or {};
289 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
290 local ret, err = dm.store(node, host, "private", private);
291 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
293 function offline_msg(node, host, t, stanza)
294 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
295 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
296 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
297 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
299 for i, row in ipairs(t["rosterusers"] or NULL) do
300 local node, contact = row.username, row.jid;
301 local name = row.nick;
302 if name == "" then name = nil; end
303 local subscription = row.subscription;
304 if subscription == "N" then
305 subscription = "none"
306 elseif subscription == "B" then
307 subscription = "both"
308 elseif subscription == "F" then
309 subscription = "from"
310 elseif subscription == "T" then
312 else error("Unknown subscription type: "..subscription) end;
316 elseif ask == "O" then
318 elseif ask == "I" then
319 roster_pending(node, host, contact);
321 elseif ask == "B" then
322 roster_pending(node, host, contact);
324 else error("Unknown ask type: "..ask); end
325 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
326 roster(node, host, contact, item);
328 for i, row in ipairs(t["rostergroups"] or NULL) do
329 roster_group(row.username, host, row.jid, row.grp);
331 for i, row in ipairs(t["vcard"] or NULL) do
332 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard)));
333 print("["..(err or "success").."] vCard: "..row.username.."@"..host);
335 for i, row in ipairs(t["private_storage"] or NULL) do
336 private_storage(row.username, host, row.namespace, parse_xml(row.data));
338 table.sort(t["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case
339 local time_offset = os.difftime(os.time(os.date("!*t")), os.time(os.date("*t"))) -- to deal with timezones
340 local date_parse = function(s)
341 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)");
342 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec-time_offset});
344 for i, row in ipairs(t["spool"] or NULL) do
345 local stanza = parse_xml(row.xml);
346 local last_child = stanza.tags[#stanza.tags];
347 if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end
348 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
349 stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil;
350 local t = date_parse(last_child.attr.stamp);
351 offline_msg(row.username, host, t, stanza);