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 if s == "\\\\" then return "\\"; end
43 error("Unknown escape sequence: "..s);
45 local function readString()
51 s = s..unescape(read()..read());
61 local function readNonString()
64 if peek() == "," or peek() == ")" then
72 local function readItem()
76 return readNonString();
79 local function readTuple()
82 while peek() ~= ")" do
83 table.insert(items, readItem());
84 if peek() == ")" then break; end
90 local function readTuples()
91 if peek() ~= "(" then read("("); end
94 table.insert(tuples, readTuple());
95 if peek() == "," then read() end
96 if peek() == ";" then break; end
100 local function readTableName()
102 while peek() ~= "`" do tname = tname..read(); end
105 local function readInsert()
106 if peek() == nil then return nil; end
107 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
110 else -- match failed, skip line
111 while peek() and read() ~= "\n" do end
115 local tname = readTableName();
116 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
117 local tuples = readTuples();
118 read(";"); read("\n");
119 return tname, tuples;
122 local function readFile(filename)
123 file = io.open(filename);
124 if not file then error("File not found: "..filename); os.exit(0); end
127 local tname, tuples = readInsert();
130 elseif peek() == nil then
137 return readFile(filename);
142 local arg, host = ...;
143 local help = "/? -? ? /h -h /help -help --help";
144 if not(arg and host) or help:find(arg, 1, true) then
145 print([[ejabberd SQL DB dump importer for Prosody
147 Usage: ejabberdsql2prosody.lua filename.txt hostname
149 The file can be generated using mysqldump:
150 mysqldump db_name > filename.txt]]);
154 ["last"] = {"username", "seconds", "state"};
155 ["privacy_default_list"] = {"username", "name"};
156 ["privacy_list"] = {"username", "name", "id"};
157 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
158 ["private_storage"] = {"username", "namespace", "data"};
159 ["rostergroups"] = {"username", "jid", "grp"};
160 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
161 ["spool"] = {"username", "xml", "seq"};
162 ["users"] = {"username", "password"};
163 ["vcard"] = {"username", "vcard"};
164 --["vcard_search"] = {};
167 local t = parseFile(arg);
168 for name, data in pairs(t) do
180 --print(serialize(t));
182 for i, row in ipairs(t["users"] or NULL) do
183 local node, password = row.username, row.password;
184 local ret, err = dm.store(node, host, "accounts", {password = password});
185 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
188 function roster(node, host, jid, item)
189 local roster = dm.load(node, host, "roster") or {};
191 local ret, err = dm.store(node, host, "roster", roster);
192 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
194 function roster_pending(node, host, jid)
195 local roster = dm.load(node, host, "roster") or {};
196 roster.pending = roster.pending or {};
197 roster.pending[jid] = true;
198 local ret, err = dm.store(node, host, "roster", roster);
199 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
201 function roster_group(node, host, jid, group)
202 local roster = dm.load(node, host, "roster") or {};
203 local item = roster[jid];
204 if not item then print("Warning: No roster item "..jid.." for user "..user..", can't put in group "..group); return; end
205 item.groups[group] = true;
206 local ret, err = dm.store(node, host, "roster", roster);
207 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
209 for i, row in ipairs(t["rosterusers"] or NULL) do
210 local node, contact = row.username, row.jid;
211 local name = row.nick;
212 if name == "" then name = nil; end
213 local subscription = row.subscription;
214 if subscription == "N" then
215 subscription = "none"
216 elseif subscription == "B" then
217 subscription = "both"
218 elseif subscription == "F" then
219 subscription = "from"
220 elseif subscription == "T" then
222 else error("Unknown subscription type: "..subscription) end;
226 elseif ask == "O" then
228 elseif ask == "I" then
229 roster_pending(node, host, contact);
231 else error("Unknown ask type: "..ask); end
232 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
233 roster(node, host, contact, item);
235 for i, row in ipairs(t["rostergroups"] or NULL) do
236 roster_group(row.username, host, row.jid, row.grp);