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
39 local function unescape(s)
40 if s == "\\'" then return "'"; end
41 if s == "\\n" then return "\n"; end
42 error("Unknown escape sequence: "..s);
44 local function readString()
50 s = s..unescape(read()..read());
60 local function readNonString()
63 if peek() == "," or peek() == ")" then
71 local function readItem()
75 return readNonString();
78 local function readTuple()
81 while peek() ~= ")" do
82 table.insert(items, readItem());
83 if peek() == ")" then break; end
89 local function readTuples()
90 if peek() ~= "(" then read("("); end
93 table.insert(tuples, readTuple());
94 if peek() == "," then read() end
95 if peek() == ";" then break; end
99 local function readTableName()
101 while peek() ~= "`" do tname = tname..read(); end
104 local function readInsert()
105 if peek() == nil then return nil; end
106 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
109 else -- match failed, skip line
110 while peek() and read() ~= "\n" do end
114 local tname = readTableName();
115 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
116 local tuples = readTuples();
117 read(";"); read("\n");
118 return tname, tuples;
121 local function readFile(filename)
122 file = io.open(filename);
123 if not file then error("File not found: "..filename); os.exit(0); end
126 local tname, tuples = readInsert();
129 elseif peek() == nil then
136 return readFile(filename);
141 local arg, host = ...;
142 local help = "/? -? ? /h -h /help -help --help";
143 if not(arg and host) or help:find(arg, 1, true) then
144 print([[ejabberd SQL DB dump importer for Prosody
146 Usage: ejabberdsql2prosody.lua filename.txt hostname
148 The file can be generated using mysqldump:
149 mysqldump db_name > filename.txt]]);
153 ["last"] = {"username", "seconds", "state"};
154 ["privacy_default_list"] = {"username", "name"};
155 ["privacy_list"] = {"username", "name", "id"};
156 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
157 ["private_storage"] = {"username", "namespace", "data"};
158 ["rostergroups"] = {"username", "jid", "grp"};
159 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
160 ["spool"] = {"username", "xml", "seq"};
161 ["users"] = {"username", "password"};
162 ["vcard"] = {"username", "vcard"};
163 --["vcard_search"] = {};
166 local t = parseFile(arg);
167 for name, data in pairs(t) do
179 --print(serialize(t));
181 for i, row in ipairs(t["users"] or NULL) do
182 local node, password = row.username, row.password;
183 local ret, err = dm.store(node, host, "accounts", {password = password});
184 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
187 function roster(node, host, jid, item)
188 local roster = dm.load(node, host, "roster") or {};
190 local ret, err = dm.store(node, host, "roster", roster);
191 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
193 function roster_pending(node, host, jid)
194 local roster = dm.load(node, host, "roster") or {};
195 roster.pending = roster.pending or {};
196 roster.pending[jid] = true;
197 local ret, err = dm.store(node, host, "roster", roster);
198 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
200 function roster_group(node, host, jid, group)
201 local roster = dm.load(node, host, "roster") or {};
202 local item = roster[jid];
203 if not item then print("Warning: No roster item "..jid.." for user "..user..", can't put in group "..group); return; end
204 item.groups[group] = true;
205 local ret, err = dm.store(node, host, "roster", roster);
206 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
208 for i, row in ipairs(t["rosterusers"] or NULL) do
209 local node, contact = row.username, row.jid;
210 local name = row.nick;
211 if name == "" then name = nil; end
212 local subscription = row.subscription;
213 if subscription == "N" then
214 subscription = "none"
215 elseif subscription == "B" then
216 subscription = "both"
217 elseif subscription == "F" then
218 subscription = "from"
219 elseif subscription == "T" then
221 else error("Unknown subscription type: "..subscription) end;
225 elseif ask == "O" then
227 elseif ask == "I" then
228 roster_pending(node, host, contact);
230 else error("Unknown ask type: "..ask); end
231 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
232 roster(node, host, contact, item);
234 for i, row in ipairs(t["rostergroups"] or NULL) do
235 roster_group(row.username, host, row.jid, row.grp);