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);
142 local help = "/? -? ? /h -h /help -help --help";
143 if not arg or help:find(arg, 1, true) then
144 print([[ejabberd SQL db dump importer for Prosody
146 Usage: ejabberdsql2prosody.lua filename.txt
148 The file can be generated from ejabberd using:
149 sudo ./bin/ejabberdctl dump filename.txt
151 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
155 ["last"] = {"username", "seconds", "state"};
156 ["privacy_default_list"] = {"username", "name"};
157 ["privacy_list"] = {"username", "name", "id"};
158 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
159 ["private_storage"] = {"username", "namespace", "data"};
160 ["rostergroups"] = {"username", "jid", "grp"};
161 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
162 ["spool"] = {"username", "xml", "seq"};
163 ["users"] = {"username", "password"};
164 ["vcard"] = {"username", "vcard"};
165 --["vcard_search"] = {};
168 local t = parseFile(arg);
169 for name, data in pairs(t) do
182 local host = "ayena.de";
184 for i, row in ipairs(t["users"] or NULL) do
185 local node, password = row.username, row.password;
186 local ret, err = dm.store(node, host, "accounts", {password = password});
187 --print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
189 for i, row in ipairs(t["private_storage"] or NULL) do
190 --local node, password = row.username, row.password;
191 --local ret, err = dm.store(node, host, "accounts", {password = password});
192 --print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);