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 elseif peek() == nil then
147 return readFile(filename);
152 local arg, host = ...;
153 local help = "/? -? ? /h -h /help -help --help";
154 if not(arg and host) or help:find(arg, 1, true) then
155 print([[ejabberd SQL DB dump importer for Prosody
157 Usage: ejabberdsql2prosody.lua filename.txt hostname
159 The file can be generated using mysqldump:
160 mysqldump db_name > filename.txt]]);
164 ["last"] = {"username", "seconds", "state"};
165 ["privacy_default_list"] = {"username", "name"};
166 ["privacy_list"] = {"username", "name", "id"};
167 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
168 ["private_storage"] = {"username", "namespace", "data"};
169 ["rostergroups"] = {"username", "jid", "grp"};
170 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
171 ["spool"] = {"username", "xml", "seq"};
172 ["users"] = {"username", "password"};
173 ["vcard"] = {"username", "vcard"};
174 --["vcard_search"] = {};
177 local t = parseFile(arg);
178 for name, data in pairs(t) do
190 --print(serialize(t));
192 for i, row in ipairs(t["users"] or NULL) do
193 local node, password = row.username, row.password;
194 local ret, err = dm.store(node, host, "accounts", {password = password});
195 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
198 function roster(node, host, jid, item)
199 local roster = dm.load(node, host, "roster") or {};
201 local ret, err = dm.store(node, host, "roster", roster);
202 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
204 function roster_pending(node, host, jid)
205 local roster = dm.load(node, host, "roster") or {};
206 roster.pending = roster.pending or {};
207 roster.pending[jid] = true;
208 local ret, err = dm.store(node, host, "roster", roster);
209 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
211 function roster_group(node, host, jid, group)
212 local roster = dm.load(node, host, "roster") or {};
213 local item = roster[jid];
214 if not item then print("Warning: No roster item "..jid.." for user "..user..", can't put in group "..group); return; end
215 item.groups[group] = true;
216 local ret, err = dm.store(node, host, "roster", roster);
217 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
219 for i, row in ipairs(t["rosterusers"] or NULL) do
220 local node, contact = row.username, row.jid;
221 local name = row.nick;
222 if name == "" then name = nil; end
223 local subscription = row.subscription;
224 if subscription == "N" then
225 subscription = "none"
226 elseif subscription == "B" then
227 subscription = "both"
228 elseif subscription == "F" then
229 subscription = "from"
230 elseif subscription == "T" then
232 else error("Unknown subscription type: "..subscription) end;
236 elseif ask == "O" then
238 elseif ask == "I" then
239 roster_pending(node, host, contact);
241 else error("Unknown ask type: "..ask); end
242 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
243 roster(node, host, contact, item);
245 for i, row in ipairs(t["rostergroups"] or NULL) do
246 roster_group(row.username, host, row.jid, row.grp);