Software /
code /
prosody
Comparison
tools/ejabberdsql2prosody.lua @ 1592:a7c140fc672b
ejabberdsql2prosody: Initial commit
- full parser
- only exports account data at the moment
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 25 Jul 2009 23:19:39 +0500 |
child | 1593:4e44aa858a4c |
comparison
equal
deleted
inserted
replaced
1591:aaa1bcb7af98 | 1592:a7c140fc672b |
---|---|
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 = ...; | |
142 local help = "/? -? ? /h -h /help -help --help"; | |
143 if not arg or help:find(arg, 1, true) then | |
144 print([[ejabberd SQL db dump importer for Prosody | |
145 | |
146 Usage: ejabberdsql2prosody.lua filename.txt | |
147 | |
148 The file can be generated from ejabberd using: | |
149 sudo ./bin/ejabberdctl dump filename.txt | |
150 | |
151 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]); | |
152 os.exit(1); | |
153 end | |
154 local map = { | |
155 ["last"] = {"username", "seconds", "state"}; | |
156 ["privacy_default_list"] = {"username", "name"}; | |
157 ["privacy_list"] = {"username", "name", "id"}; | |
158 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"}; | |
159 ["private_storage"] = {"username", "namespace", "data"}; | |
160 ["rostergroups"] = {"username", "jid", "grp"}; | |
161 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"}; | |
162 ["spool"] = {"username", "xml", "seq"}; | |
163 ["users"] = {"username", "password"}; | |
164 ["vcard"] = {"username", "vcard"}; | |
165 --["vcard_search"] = {}; | |
166 } | |
167 local NULL = {}; | |
168 local t = parseFile(arg); | |
169 for name, data in pairs(t) do | |
170 local m = map[name]; | |
171 if m then | |
172 for i=1,#data do | |
173 local row = data[i]; | |
174 for j=1,#row do | |
175 row[m[j]] = row[j]; | |
176 row[j] = nil; | |
177 end | |
178 end | |
179 end | |
180 end | |
181 | |
182 local host = "ayena.de"; | |
183 | |
184 for i, row in ipairs(t["users"] or NULL) do | |
185 local node, password = row.username, row.password; | |
186 local ret, err = dm.store(node, host, "accounts", {password = password}); | |
187 --print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); | |
188 end | |
189 for i, row in ipairs(t["private_storage"] or NULL) do | |
190 --local node, password = row.username, row.password; | |
191 --local ret, err = dm.store(node, host, "accounts", {password = password}); | |
192 --print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); | |
193 end | |
194 | |
195 | |
196 | |
197 print(serialize(t)); |