1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
#!/usr/bin/env lua
-- Prosody IM
-- Copyright (C) 2008-2009 Matthew Wild
-- Copyright (C) 2008-2009 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
package.path = package.path ..";../?.lua";
local serialize = require "util.serialization".serialize;
local st = require "util.stanza";
package.loaded["util.logger"] = {init = function() return function() end; end}
local dm = require "util.datamanager"
dm.set_data_path("data");
function parseFile(filename)
------
local file = nil;
local last = nil;
local function read(expected)
local ch;
if last then
ch = last; last = nil;
else ch = file:read(1); end
if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end
return ch;
end
local function pushback(ch)
if last then error(); end
last = ch;
end
local function peek()
if not last then last = read(); end
return last;
end
local escapes = {
["\\0"] = "\0";
["\\'"] = "'";
["\\\""] = "\"";
["\\b"] = "\b";
["\\n"] = "\n";
["\\r"] = "\r";
["\\t"] = "\t";
["\\Z"] = "\26";
["\\\\"] = "\\";
["\\%"] = "%";
["\\_"] = "_";
}
local function unescape(s)
return escapes[s] or error("Unknown escape sequence: "..s);
end
local function readString()
read("'");
local s = "";
while true do
local ch = peek();
if ch == "\\" then
s = s..unescape(read()..read());
elseif ch == "'" then
break;
else
s = s..read();
end
end
read("'");
return s;
end
local function readNonString()
local s = "";
while true do
if peek() == "," or peek() == ")" then
break;
else
s = s..read();
end
end
return tonumber(s);
end
local function readItem()
if peek() == "'" then
return readString();
else
return readNonString();
end
end
local function readTuple()
local items = {}
read("(");
while peek() ~= ")" do
table.insert(items, readItem());
if peek() == ")" then break; end
read(",");
end
read(")");
return items;
end
local function readTuples()
if peek() ~= "(" then read("("); end
local tuples = {};
while true do
table.insert(tuples, readTuple());
if peek() == "," then read() end
if peek() == ";" then break; end
end
return tuples;
end
local function readTableName()
local tname = "";
while peek() ~= "`" do tname = tname..read(); end
return tname;
end
local function readInsert()
if peek() == nil then return nil; end
for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
if peek() == ch then
read(); -- found
else -- match failed, skip line
while peek() and read() ~= "\n" do end
return nil;
end
end
local tname = readTableName();
for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
local tuples = readTuples();
read(";"); read("\n");
return tname, tuples;
end
local function readFile(filename)
file = io.open(filename);
if not file then error("File not found: "..filename); os.exit(0); end
local t = {};
while true do
local tname, tuples = readInsert();
if tname then
if t[tname] then
local t_name = t[tname];
for i=1,#tuples do
table.insert(t_name, tuples[i]);
end
else
t[tname] = tuples;
end
elseif peek() == nil then
break;
end
end
return t;
end
return readFile(filename);
------
end
-- XML parser
local parse_xml = (function()
local entity_map = setmetatable({
["amp"] = "&";
["gt"] = ">";
["lt"] = "<";
["apos"] = "'";
["quot"] = "\"";
}, {__index = function(_, s)
if s:sub(1,1) == "#" then
if s:sub(2,2) == "x" then
return string.char(tonumber(s:sub(3), 16));
else
return string.char(tonumber(s:sub(2)));
end
end
end
});
local function xml_unescape(str)
return (str:gsub("&(.-);", entity_map));
end
local function parse_tag(s)
local name,sattr=(s):gmatch("([^%s]+)(.*)")();
local attr = {};
for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
return name, attr;
end
return function(xml)
local stanza = st.stanza("root");
local regexp = "<([^>]*)>([^<]*)";
for elem, text in xml:gmatch(regexp) do
if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
elseif elem:sub(1,1) == "/" then -- end tag
elem = elem:sub(2);
stanza:up(); -- TODO check for start-end tag name match
elseif elem:sub(-1,-1) == "/" then -- empty tag
elem = elem:sub(1,-2);
local name,attr = parse_tag(elem);
stanza:tag(name, attr):up();
else -- start tag
local name,attr = parse_tag(elem);
stanza:tag(name, attr);
end
if #text ~= 0 then -- text
stanza:text(xml_unescape(text));
end
end
return stanza.tags[1];
end
end)();
-- end of XML parser
local arg, host = ...;
local help = "/? -? ? /h -h /help -help --help";
if not(arg and host) or help:find(arg, 1, true) then
print([[ejabberd SQL DB dump importer for Prosody
Usage: ejabberdsql2prosody.lua filename.txt hostname
The file can be generated using mysqldump:
mysqldump db_name > filename.txt]]);
os.exit(1);
end
local map = {
["last"] = {"username", "seconds", "state"};
["privacy_default_list"] = {"username", "name"};
["privacy_list"] = {"username", "name", "id"};
["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
["private_storage"] = {"username", "namespace", "data"};
["rostergroups"] = {"username", "jid", "grp"};
["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
["spool"] = {"username", "xml", "seq"};
["users"] = {"username", "password"};
["vcard"] = {"username", "vcard"};
--["vcard_search"] = {};
}
local NULL = {};
local t = parseFile(arg);
for name, data in pairs(t) do
local m = map[name];
if m then
if #data > 0 and #data[1] ~= #m then
print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]);
end
for i=1,#data do
local row = data[i];
for j=1,#m do
row[m[j]] = row[j];
row[j] = nil;
end
end
end
end
--print(serialize(t));
for i, row in ipairs(t["users"] or NULL) do
local node, password = row.username, row.password;
local ret, err = dm.store(node, host, "accounts", {password = password});
print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
end
function roster(node, host, jid, item)
local roster = dm.load(node, host, "roster") or {};
roster[jid] = item;
local ret, err = dm.store(node, host, "roster", roster);
print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
end
function roster_pending(node, host, jid)
local roster = dm.load(node, host, "roster") or {};
roster.pending = roster.pending or {};
roster.pending[jid] = true;
local ret, err = dm.store(node, host, "roster", roster);
print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
end
function roster_group(node, host, jid, group)
local roster = dm.load(node, host, "roster") or {};
local item = roster[jid];
if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end
item.groups[group] = true;
local ret, err = dm.store(node, host, "roster", roster);
print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
end
function private_storage(node, host, xmlns, stanza)
local private = dm.load(node, host, "private") or {};
private[stanza.name..":"..xmlns] = st.preserialize(stanza);
local ret, err = dm.store(node, host, "private", private);
print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
end
for i, row in ipairs(t["rosterusers"] or NULL) do
local node, contact = row.username, row.jid;
local name = row.nick;
if name == "" then name = nil; end
local subscription = row.subscription;
if subscription == "N" then
subscription = "none"
elseif subscription == "B" then
subscription = "both"
elseif subscription == "F" then
subscription = "from"
elseif subscription == "T" then
subscription = "to"
else error("Unknown subscription type: "..subscription) end;
local ask = row.ask;
if ask == "N" then
ask = nil;
elseif ask == "O" then
ask = "subscribe";
elseif ask == "I" then
roster_pending(node, host, contact);
ask = nil;
elseif ask == "B" then
roster_pending(node, host, contact);
ask = "subscribe";
else error("Unknown ask type: "..ask); end
local item = {name = name, ask = ask, subscription = subscription, groups = {}};
roster(node, host, contact, item);
end
for i, row in ipairs(t["rostergroups"] or NULL) do
roster_group(row.username, host, row.jid, row.grp);
end
for i, row in ipairs(t["vcard"] or NULL) do
local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard)));
print("["..(err or "success").."] vCard: "..row.username.."@"..host);
end
for i, row in ipairs(t["private_storage"] or NULL) do
private_storage(row.username, host, row.namespace, st.preserialize(parse_xml(row.data)));
end
|