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