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";
14 local my_name = arg[0];
15 if my_name:match("[/\\]") then
16 package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
17 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
21 local serialize = require "util.serialization".serialize;
22 local st = require "util.stanza";
23 local parse_xml = require "util.xml".parse;
24 package.loaded["util.logger"] = {init = function() return function() end; end}
25 local dm = require "util.datamanager"
26 dm.set_data_path("data");
28 function parseFile(filename)
34 local function read(expected)
37 ch = last; last = nil;
40 if ch == "\n" then line = line + 1; end
42 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil").." on line "..line); end
45 local function pushback(ch)
46 if last then error(); end
50 if not last then last = read(); end
67 local function unescape(s)
68 return escapes[s] or error("Unknown escape sequence: "..s);
70 local function readString()
76 s = s..unescape(read()..read());
86 local function readNonString()
89 if peek() == "," or peek() == ")" then
97 local function readItem()
101 return readNonString();
104 local function readTuple()
107 while peek() ~= ")" do
108 table.insert(items, readItem());
109 if peek() == ")" then break; end
115 local function readTuples()
116 if peek() ~= "(" then read("("); end
119 table.insert(tuples, readTuple());
120 if peek() == "," then read() end
121 if peek() == ";" then break; end
125 local function readTableName()
127 while peek() ~= "`" do tname = tname..read(); end
130 local function readInsert()
131 if peek() == nil then return nil; end
132 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
135 else -- match failed, skip line
136 while peek() and read() ~= "\n" do end
140 local tname = readTableName();
141 read("`"); read(" ") -- expect this
142 if peek() == "(" then -- skip column list
143 repeat until read() == ")";
146 for ch in ("VALUES "):gmatch(".") do read(ch); end -- expect this
147 local tuples = readTuples();
148 read(";"); read("\n");
149 return tname, tuples;
152 local function readFile(filename)
153 file = io.open(filename);
154 if not file then error("File not found: "..filename); os.exit(0); end
157 local tname, tuples = readInsert();
160 local t_name = t[tname];
162 table.insert(t_name, tuples[i]);
167 elseif peek() == nil then
174 return readFile(filename);
179 local arg, host = ...;
180 local help = "/? -? ? /h -h /help -help --help";
181 if not(arg and host) or help:find(arg, 1, true) then
182 print([[ejabberd SQL DB dump importer for Prosody
184 Usage: ejabberdsql2prosody.lua filename.txt hostname
186 The file can be generated using mysqldump:
187 mysqldump db_name > filename.txt]]);
191 ["last"] = {"username", "seconds", "state"};
192 ["privacy_default_list"] = {"username", "name"};
193 ["privacy_list"] = {"username", "name", "id"};
194 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
195 ["private_storage"] = {"username", "namespace", "data"};
196 ["rostergroups"] = {"username", "jid", "grp"};
197 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
198 ["spool"] = {"username", "xml", "seq"};
199 ["users"] = {"username", "password"};
200 ["vcard"] = {"username", "vcard"};
201 --["vcard_search"] = {};
204 local t = parseFile(arg);
205 for name, data in pairs(t) do
208 if #data > 0 and #data[1] ~= #m then
209 print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
220 --print(serialize(t));
222 for i, row in ipairs(t["users"] or NULL) do
223 local node, password = row.username, row.password;
224 local ret, err = dm.store(node, host, "accounts", {password = password});
225 print("["..(err or "success").."] accounts: "..node.."@"..host);
228 function roster(node, host, jid, item)
229 local roster = dm.load(node, host, "roster") or {};
231 local ret, err = dm.store(node, host, "roster", roster);
232 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
234 function roster_pending(node, host, jid)
235 local roster = dm.load(node, host, "roster") or {};
236 roster.pending = roster.pending or {};
237 roster.pending[jid] = true;
238 local ret, err = dm.store(node, host, "roster", roster);
239 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
241 function roster_group(node, host, jid, group)
242 local roster = dm.load(node, host, "roster") or {};
243 local item = roster[jid];
244 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
245 item.groups[group] = true;
246 local ret, err = dm.store(node, host, "roster", roster);
247 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
249 function private_storage(node, host, xmlns, stanza)
250 local private = dm.load(node, host, "private") or {};
251 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
252 local ret, err = dm.store(node, host, "private", private);
253 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
255 function offline_msg(node, host, t, stanza)
256 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
257 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
258 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
259 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
261 for i, row in ipairs(t["rosterusers"] or NULL) do
262 local node, contact = row.username, row.jid;
263 local name = row.nick;
264 if name == "" then name = nil; end
265 local subscription = row.subscription;
266 if subscription == "N" then
267 subscription = "none"
268 elseif subscription == "B" then
269 subscription = "both"
270 elseif subscription == "F" then
271 subscription = "from"
272 elseif subscription == "T" then
274 else error("Unknown subscription type: "..subscription) end;
278 elseif ask == "O" then
280 elseif ask == "I" then
281 roster_pending(node, host, contact);
283 elseif ask == "B" then
284 roster_pending(node, host, contact);
286 else error("Unknown ask type: "..ask); end
287 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
288 roster(node, host, contact, item);
290 for i, row in ipairs(t["rostergroups"] or NULL) do
291 roster_group(row.username, host, row.jid, row.grp);
293 for i, row in ipairs(t["vcard"] or NULL) do
294 local stanza, err = parse_xml(row.vcard);
296 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(stanza));
297 print("["..(err or "success").."] vCard: "..row.username.."@"..host);
299 print("[error] vCard XML parse failed: "..row.username.."@"..host);
302 for i, row in ipairs(t["private_storage"] or NULL) do
303 local stanza, err = parse_xml(row.data);
305 private_storage(row.username, host, row.namespace, stanza);
307 print("[error] Private XML parse failed: "..row.username.."@"..host);
310 table.sort(t["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case
311 local time_offset = os.difftime(os.time(os.date("!*t")), os.time(os.date("*t"))) -- to deal with timezones
312 local date_parse = function(s)
313 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)");
314 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec-time_offset});
316 for i, row in ipairs(t["spool"] or NULL) do
317 local stanza, err = parse_xml(row.xml);
319 local last_child = stanza.tags[#stanza.tags];
320 if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end
321 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
322 stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil;
323 local t = date_parse(last_child.attr.stamp);
324 offline_msg(row.username, host, t, stanza);
326 print("[error] Offline message XML parsing failed: "..row.username.."@"..host);