Merge with 0.5
[prosody.git] / tools / ejabberdsql2prosody.lua
1 #!/usr/bin/env lua
2 -- Prosody IM
3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- 
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
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");
16
17 function parseFile(filename)
18 ------
19
20 local file = nil;
21 local last = nil;
22 local function read(expected)
23         local ch;
24         if last then
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
28         return ch;
29 end
30 local function pushback(ch)
31         if last then error(); end
32         last = ch;
33 end
34 local function peek()
35         if not last then last = read(); end
36         return last;
37 end
38
39 local escapes = {
40         ["\\0"] = "\0";
41         ["\\'"] = "'";
42         ["\\\""] = "\"";
43         ["\\b"] = "\b";
44         ["\\n"] = "\n";
45         ["\\r"] = "\r";
46         ["\\t"] = "\t";
47         ["\\Z"] = "\26";
48         ["\\\\"] = "\\";
49         ["\\%"] = "%";
50         ["\\_"] = "_";
51 }
52 local function unescape(s)
53         return escapes[s] or error("Unknown escape sequence: "..s);
54 end
55 local function readString()
56         read("'");
57         local s = "";
58         while true do
59                 local ch = peek();
60                 if ch == "\\" then
61                         s = s..unescape(read()..read());
62                 elseif ch == "'" then
63                         break;
64                 else
65                         s = s..read();
66                 end
67         end
68         read("'");
69         return s;
70 end
71 local function readNonString()
72         local s = "";
73         while true do
74                 if peek() == "," or peek() == ")" then
75                         break;
76                 else
77                         s = s..read();
78                 end
79         end
80         return tonumber(s);
81 end
82 local function readItem()
83         if peek() == "'" then
84                 return readString();
85         else
86                 return readNonString();
87         end
88 end
89 local function readTuple()
90         local items = {}
91         read("(");
92         while peek() ~= ")" do
93                 table.insert(items, readItem());
94                 if peek() == ")" then break; end
95                 read(",");
96         end
97         read(")");
98         return items;
99 end
100 local function readTuples()
101         if peek() ~= "(" then read("("); end
102         local tuples = {};
103         while true do
104                 table.insert(tuples, readTuple());
105                 if peek() == "," then read() end
106                 if peek() == ";" then break; end
107         end
108         return tuples;
109 end
110 local function readTableName()
111         local tname = "";
112         while peek() ~= "`" do tname = tname..read(); end
113         return tname;
114 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
118                 if peek() == ch then
119                         read(); -- found
120                 else -- match failed, skip line
121                         while peek() and read() ~= "\n" do end
122                         return nil;
123                 end
124         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;
130 end
131
132 local function readFile(filename)
133         file = io.open(filename);
134         if not file then error("File not found: "..filename); os.exit(0); end
135         local t = {};
136         while true do
137                 local tname, tuples = readInsert();
138                 if tname then
139                         if t[name] then
140                                 local t_name = t[name];
141                                 for i=1,#tuples do
142                                         table.insert(t_name, tuples[i]);
143                                 end
144                         else
145                                 t[tname] = tuples;
146                         end
147                 elseif peek() == nil then
148                         break;
149                 end
150         end
151         return t;
152 end
153
154 return readFile(filename);
155
156 ------
157 end
158
159 local arg, host = ...;
160 local help = "/? -? ? /h -h /help -help --help";
161 if not(arg and host) or help:find(arg, 1, true) then
162         print([[ejabberd SQL DB dump importer for Prosody
163
164   Usage: ejabberdsql2prosody.lua filename.txt hostname
165
166 The file can be generated using mysqldump:
167   mysqldump db_name > filename.txt]]);
168         os.exit(1);
169 end
170 local map = {
171         ["last"] = {"username", "seconds", "state"};
172         ["privacy_default_list"] = {"username", "name"};
173         ["privacy_list"] = {"username", "name", "id"};
174         ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
175         ["private_storage"] = {"username", "namespace", "data"};
176         ["rostergroups"] = {"username", "jid", "grp"};
177         ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
178         ["spool"] = {"username", "xml", "seq"};
179         ["users"] = {"username", "password"};
180         ["vcard"] = {"username", "vcard"};
181         --["vcard_search"] = {};
182 }
183 local NULL = {};
184 local t = parseFile(arg);
185 for name, data in pairs(t) do
186         local m = map[name];
187         if m then
188                 if #data > 0 and #data[1] ~= #m then
189                         print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
190                 end
191                 for i=1,#data do
192                         local row = data[i];
193                         for j=1,#m do
194                                 row[m[j]] = row[j];
195                                 row[j] = nil;
196                         end
197                 end
198         end
199 end
200 --print(serialize(t));
201
202 for i, row in ipairs(t["users"] or NULL) do
203         local node, password = row.username, row.password;
204         local ret, err = dm.store(node, host, "accounts", {password = password});
205         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
206 end
207
208 function roster(node, host, jid, item)
209         local roster = dm.load(node, host, "roster") or {};
210         roster[jid] = item;
211         local ret, err = dm.store(node, host, "roster", roster);
212         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
213 end
214 function roster_pending(node, host, jid)
215         local roster = dm.load(node, host, "roster") or {};
216         roster.pending = roster.pending or {};
217         roster.pending[jid] = true;
218         local ret, err = dm.store(node, host, "roster", roster);
219         print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
220 end
221 function roster_group(node, host, jid, group)
222         local roster = dm.load(node, host, "roster") or {};
223         local item = roster[jid];
224         if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
225         item.groups[group] = true;
226         local ret, err = dm.store(node, host, "roster", roster);
227         print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
228 end
229 for i, row in ipairs(t["rosterusers"] or NULL) do
230         local node, contact = row.username, row.jid;
231         local name = row.nick;
232         if name == "" then name = nil; end
233         local subscription = row.subscription;
234         if subscription == "N" then
235                 subscription = "none"
236         elseif subscription == "B" then
237                 subscription = "both"
238         elseif subscription == "F" then
239                 subscription = "from"
240         elseif subscription == "T" then
241                 subscription = "to"
242         else error("Unknown subscription type: "..subscription) end;
243         local ask = row.ask;
244         if ask == "N" then
245                 ask = nil;
246         elseif ask == "O" then
247                 ask = "subscribe";
248         elseif ask == "I" then
249                 roster_pending(node, host, contact);
250                 ask = nil;
251         elseif ask == "B" then
252                 roster_pending(node, host, contact);
253                 ask = "subscribe";
254         else error("Unknown ask type: "..ask); end
255         local item = {name = name, ask = ask, subscription = subscription, groups = {}};
256         roster(node, host, contact, item);
257 end
258 for i, row in ipairs(t["rostergroups"] or NULL) do
259         roster_group(row.username, host, row.jid, row.grp);
260 end