ejabberdsql2prosody: Added support for rosters
[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 function unescape(s)
40         if s == "\\'" then return "'"; end
41         if s == "\\n" then return "\n"; end
42         error("Unknown escape sequence: "..s);
43 end
44 local function readString()
45         read("'");
46         local s = "";
47         while true do
48                 local ch = peek();
49                 if ch == "\\" then
50                         s = s..unescape(read()..read());
51                 elseif ch == "'" then
52                         break;
53                 else
54                         s = s..read();
55                 end
56         end
57         read("'");
58         return s;
59 end
60 local function readNonString()
61         local s = "";
62         while true do
63                 if peek() == "," or peek() == ")" then
64                         break;
65                 else
66                         s = s..read();
67                 end
68         end
69         return tonumber(s);
70 end
71 local function readItem()
72         if peek() == "'" then
73                 return readString();
74         else
75                 return readNonString();
76         end
77 end
78 local function readTuple()
79         local items = {}
80         read("(");
81         while peek() ~= ")" do
82                 table.insert(items, readItem());
83                 if peek() == ")" then break; end
84                 read(",");
85         end
86         read(")");
87         return items;
88 end
89 local function readTuples()
90         if peek() ~= "(" then read("("); end
91         local tuples = {};
92         while true do
93                 table.insert(tuples, readTuple());
94                 if peek() == "," then read() end
95                 if peek() == ";" then break; end
96         end
97         return tuples;
98 end
99 local function readTableName()
100         local tname = "";
101         while peek() ~= "`" do tname = tname..read(); end
102         return tname;
103 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
107                 if peek() == ch then
108                         read(); -- found
109                 else -- match failed, skip line
110                         while peek() and read() ~= "\n" do end
111                         return nil;
112                 end
113         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;
119 end
120
121 local function readFile(filename)
122         file = io.open(filename);
123         if not file then error("File not found: "..filename); os.exit(0); end
124         local t = {};
125         while true do
126                 local tname, tuples = readInsert();
127                 if tname then
128                         t[tname] = tuples;
129                 elseif peek() == nil then
130                         break;
131                 end
132         end
133         return t;
134 end
135
136 return readFile(filename);
137
138 ------
139 end
140
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
145
146   Usage: ejabberdsql2prosody.lua filename.txt hostname
147
148 The file can be generated using mysqldump:
149   mysqldump db_name > filename.txt]]);
150         os.exit(1);
151 end
152 local map = {
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"] = {};
164 }
165 local NULL = {};
166 local t = parseFile(arg);
167 for name, data in pairs(t) do
168         local m = map[name];
169         if m then
170                 for i=1,#data do
171                         local row = data[i];
172                         for j=1,#row do
173                                 row[m[j]] = row[j];
174                                 row[j] = nil;
175                         end
176                 end
177         end
178 end
179 --print(serialize(t));
180
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);
185 end
186
187 function roster(node, host, jid, item)
188         local roster = dm.load(node, host, "roster") or {};
189         roster[jid] = item;
190         local ret, err = dm.store(node, host, "roster", roster);
191         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
192 end
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);
199 end
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);
207 end
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
220                 subscription = "to"
221         else error("Unknown subscription type: "..subscription) end;
222         local ask = row.ask;
223         if ask == "N" then
224                 ask = nil;
225         elseif ask == "O" then
226                 ask = "subscribe";
227         elseif ask == "I" then
228                 roster_pending(node, host, contact);
229                 return;
230         else error("Unknown ask type: "..ask); end
231         local item = {name = name, ask = ask, subscription = subscription, groups = {}};
232         roster(node, host, contact, item);
233 end
234 for i, row in ipairs(t["rostergroups"] or NULL) do
235         roster_group(row.username, host, row.jid, row.grp);
236 end