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                         t[tname] = tuples;
140                 elseif peek() == nil then
141                         break;
142                 end
143         end
144         return t;
145 end
146
147 return readFile(filename);
148
149 ------
150 end
151
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
156
157   Usage: ejabberdsql2prosody.lua filename.txt hostname
158
159 The file can be generated using mysqldump:
160   mysqldump db_name > filename.txt]]);
161         os.exit(1);
162 end
163 local map = {
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"] = {};
175 }
176 local NULL = {};
177 local t = parseFile(arg);
178 for name, data in pairs(t) do
179         local m = map[name];
180         if m then
181                 if #data > 0 and #data[1] ~= #m then
182                         print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
183                 end
184                 for i=1,#data do
185                         local row = data[i];
186                         for j=1,#m do
187                                 row[m[j]] = row[j];
188                                 row[j] = nil;
189                         end
190                 end
191         end
192 end
193 --print(serialize(t));
194
195 for i, row in ipairs(t["users"] or NULL) do
196         local node, password = row.username, row.password;
197         local ret, err = dm.store(node, host, "accounts", {password = password});
198         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
199 end
200
201 function roster(node, host, jid, item)
202         local roster = dm.load(node, host, "roster") or {};
203         roster[jid] = item;
204         local ret, err = dm.store(node, host, "roster", roster);
205         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
206 end
207 function roster_pending(node, host, jid)
208         local roster = dm.load(node, host, "roster") or {};
209         roster.pending = roster.pending or {};
210         roster.pending[jid] = true;
211         local ret, err = dm.store(node, host, "roster", roster);
212         print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
213 end
214 function roster_group(node, host, jid, group)
215         local roster = dm.load(node, host, "roster") or {};
216         local item = roster[jid];
217         if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
218         item.groups[group] = true;
219         local ret, err = dm.store(node, host, "roster", roster);
220         print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
221 end
222 for i, row in ipairs(t["rosterusers"] or NULL) do
223         local node, contact = row.username, row.jid;
224         local name = row.nick;
225         if name == "" then name = nil; end
226         local subscription = row.subscription;
227         if subscription == "N" then
228                 subscription = "none"
229         elseif subscription == "B" then
230                 subscription = "both"
231         elseif subscription == "F" then
232                 subscription = "from"
233         elseif subscription == "T" then
234                 subscription = "to"
235         else error("Unknown subscription type: "..subscription) end;
236         local ask = row.ask;
237         if ask == "N" then
238                 ask = nil;
239         elseif ask == "O" then
240                 ask = "subscribe";
241         elseif ask == "I" then
242                 roster_pending(node, host, contact);
243                 ask = nil;
244         elseif ask == "B" then
245                 roster_pending(node, host, contact);
246                 ask = "subscribe";
247         else error("Unknown ask type: "..ask); end
248         local item = {name = name, ask = ask, subscription = subscription, groups = {}};
249         roster(node, host, contact, item);
250 end
251 for i, row in ipairs(t["rostergroups"] or NULL) do
252         roster_group(row.username, host, row.jid, row.grp);
253 end