ejabberd db dump importer for Prosody
[prosody.git] / tools / ejabberd2prosody.lua
1 \r
2 require "erlparse";\r
3 require "serialize";\r
4 \r
5 package.path = package.path ..";../?.lua";\r
6 local st = require "util.stanza";\r
7 package.loaded["util.logger"] = {init = function() return function() end; end}\r
8 local dm = require "util.datamanager"\r
9 local data_path = "data";\r
10 dm.set_data_path(data_path);\r
11 \r
12 local _mkdir = {}\r
13 function mkdir(path)\r
14         path = path:gsub("/", "\\");\r
15         --print("mkdir",path);\r
16         local x = io.popen("mkdir "..path.." 2>&1"):read("*a");\r
17 end\r
18 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end\r
19 function getpath(username, host, datastore, ext)\r
20         ext = ext or "dat";\r
21         if username then\r
22                 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);\r
23         elseif host then\r
24                 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);\r
25         else\r
26                 return format("%s/%s.%s", data_path, datastore, ext);\r
27         end\r
28 end\r
29 function mkdirs(host)\r
30         if not _mkdir[host] then\r
31                 local host_dir = string.format("%s/%s", data_path, encode(host));\r
32                 mkdir(host_dir);\r
33                 mkdir(host_dir.."/accounts");\r
34                 mkdir(host_dir.."/vcard");\r
35                 mkdir(host_dir.."/roster");\r
36                 mkdir(host_dir.."/private");\r
37                 mkdir(host_dir.."/offline");\r
38                 _mkdir[host] = true;\r
39         end\r
40 end\r
41 mkdir(data_path);\r
42 \r
43 function build_stanza(tuple, stanza)\r
44         if tuple[1] == "xmlelement" then\r
45                 local name = tuple[2];\r
46                 local attr = {};\r
47                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end\r
48                 local up;\r
49                 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end\r
50                 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end\r
51                 if up then stanza:up(); else return stanza end\r
52         elseif tuple[1] == "xmlcdata" then\r
53                 stanza:text(tuple[2]);\r
54         else\r
55                 error("unknown element type: "..serialize.serialize(tuple));\r
56         end\r
57 end\r
58 function build_time(tuple)\r
59         local Megaseconds,Seconds,Microseconds = unpack(tuple);\r
60         return Megaseconds * 1000000 + Seconds;\r
61 end\r
62 \r
63 function vcard(node, host, stanza)\r
64         mkdirs(host);\r
65         local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));\r
66         print("["..(err or "success").."] vCard: "..node.."@"..host);\r
67 end\r
68 function password(node, host, password)\r
69         mkdirs(host);\r
70         local ret, err = dm.store(node, host, "accounts", {password = password});\r
71         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);\r
72 end\r
73 function roster(node, host, jid, item)\r
74         mkdirs(host);\r
75         local roster = dm.load(node, host, "roster") or {};\r
76         roster[jid] = item;\r
77         local ret, err = dm.store(node, host, "roster", roster);\r
78         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);\r
79 end\r
80 function private_storage(node, host, xmlns, stanza)\r
81         mkdirs(host);\r
82         local private = dm.load(node, host, "private") or {};\r
83         private[xmlns] = st.preserialize(stanza);\r
84         local ret, err = dm.store(node, host, "private", private);\r
85         print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);\r
86 end\r
87 function offline_msg(node, host, t, stanza)\r
88         mkdirs(host);\r
89         stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);\r
90         stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);\r
91         local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));\r
92         print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));\r
93 end\r
94 \r
95 \r
96 local filters = {\r
97         passwd = function(tuple)\r
98                 password(tuple[2][1], tuple[2][2], tuple[3]);\r
99         end;\r
100         vcard = function(tuple)\r
101                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));\r
102         end;\r
103         roster = function(tuple)\r
104                 local node = tuple[3][1]; local host = tuple[3][2];\r
105                 local contact = tuple[4][1].."@"..tuple[4][2];\r
106                 local name = tuple[5]; local subscription = tuple[6];\r
107                 local ask = tuple[7]; local groups = tuple[8];\r
108                 if type(name) ~= type("") then name = nil; end\r
109                 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end\r
110                 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end\r
111                 local item = {name = name, ask = ask, subscription = subscription, groups = {}};\r
112                 for _, g in ipairs(groups) do item.groups[g] = true; end\r
113                 roster(node, host, contact, item);\r
114         end;\r
115         private_storage = function(tuple)\r
116                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));\r
117         end;\r
118         offline_msg = function(tuple)\r
119                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));\r
120         end;\r
121         config = function(tuple)\r
122                 if tuple[2] == "hosts" then\r
123                         local output = io.output(); io.output("prosody.cfg.lua");\r
124                         io.write("-- Configuration imported from ejabberd --\n");\r
125                         io.write([[Host "*"\r
126         modules_enabled = {\r
127                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.\r
128                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.\r
129                 "roster"; -- Allow users to have a roster. Recommended ;)\r
130                 "register"; -- Allow users to register on this server using a client\r
131                 "tls"; -- Add support for secure TLS on c2s/s2s connections\r
132                 "vcard"; -- Allow users to set vCards\r
133                 "private"; -- Private XML storage (for room bookmarks, etc.)\r
134                 "version"; -- Replies to server version requests\r
135                 "dialback"; -- s2s dialback support\r
136                 "uptime";\r
137                 "disco";\r
138                 "time";\r
139                 "ping";\r
140                 --"selftests";\r
141         };\r
142 ]]);\r
143                         for _, h in ipairs(tuple[3]) do\r
144                                 io.write("Host \"" .. h .. "\"\n");\r
145                         end\r
146                         io.output(output);\r
147                         print("prosody.cfg.lua created");\r
148                 end\r
149         end;\r
150 };\r
151 \r
152 local arg = ...;\r
153 local help = "/? -? ? /h -h /help -help --help";\r
154 if not arg or help:find(arg, 1, true) then\r
155         print([[ejabberd db dump importer for Prosody\r
156 \r
157   Usage: ejabberd2prosody.lua filename.txt\r
158 \r
159 The file can be generated from ejabberd using:\r
160   sudo ./bin/ejabberdctl dump filename.txt\r
161 \r
162 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);\r
163         os.exit(1);\r
164 end\r
165 local count = 0;\r
166 local t = {};\r
167 for item in erlparse.parseFile(arg) do\r
168         count = count + 1;\r
169         local name = item[1];\r
170         t[name] = (t[name] or 0) + 1;\r
171         --print(count, serialize.serialize(item));\r
172         if filters[name] then filters[name](item); end\r
173 end\r
174 --print(serialize.serialize(t));\r