Merge 0.9->0.10
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2 -- Prosody IM
3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 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
11
12 package.path = package.path ..";../?.lua";
13
14 local my_name = arg[0];
15 if my_name:match("[/\\]") then
16         package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
17         package.path = package.path..";"..my_name:gsub("[^/\\]+$", "?.lua");
18         package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
19 end
20
21 local erlparse = require "erlparse";
22
23 prosody = {};
24
25 package.loaded["util.logger"] = {init = function() return function() end; end}
26 local serialize = require "util.serialization".serialize;
27 local st = require "util.stanza";
28 local dm = require "util.datamanager"
29 dm.set_data_path("data");
30
31 function build_stanza(tuple, stanza)
32         assert(type(tuple) == "table", "XML node is of unexpected type: "..type(tuple));
33         if tuple[1] == "xmlelement" or tuple[1] == "xmlel" then
34                 assert(type(tuple[2]) == "string", "element name has type: "..type(tuple[2]));
35                 assert(type(tuple[3]) == "table", "element attribute array has type: "..type(tuple[3]));
36                 assert(type(tuple[4]) == "table", "element children array has type: "..type(tuple[4]));
37                 local name = tuple[2];
38                 local attr = {};
39                 for _, a in ipairs(tuple[3]) do
40                         if type(a[1]) == "string" and type(a[2]) == "string" then attr[a[1]] = a[2]; end
41                 end
42                 local up;
43                 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
44                 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
45                 if up then stanza:up(); else return stanza end
46         elseif tuple[1] == "xmlcdata" then
47                 if type(tuple[2]) ~= "table" then
48                         assert(type(tuple[2]) == "string", "XML CDATA has unexpected type: "..type(tuple[2]));
49                         stanza:text(tuple[2]);
50                 end -- else it's [], i.e., the null value, used for the empty string
51         else
52                 error("unknown element type: "..serialize(tuple));
53         end
54 end
55 function build_time(tuple)
56         local Megaseconds,Seconds,Microseconds = unpack(tuple);
57         return Megaseconds * 1000000 + Seconds;
58 end
59 function build_jid(tuple, full)
60         local node, jid, resource = tuple[1], tuple[2], tuple[3]
61         if type(node) == "string" and node ~= "" then
62                 jid = tuple[1] .. "@" .. jid;
63         end
64         if full and type(resource) == "string" and resource ~= "" then
65                 jid = jid .. "/" .. resource;
66         end
67         return jid;
68 end
69
70 function vcard(node, host, stanza)
71         local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
72         print("["..(err or "success").."] vCard: "..node.."@"..host);
73 end
74 function password(node, host, password)
75         local ret, err = dm.store(node, host, "accounts", {password = password});
76         print("["..(err or "success").."] accounts: "..node.."@"..host);
77 end
78 function roster(node, host, jid, item)
79         local roster = dm.load(node, host, "roster") or {};
80         roster[jid] = item;
81         local ret, err = dm.store(node, host, "roster", roster);
82         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
83 end
84 function roster_pending(node, host, jid)
85         local roster = dm.load(node, host, "roster") or {};
86         roster.pending = roster.pending or {};
87         roster.pending[jid] = true;
88         local ret, err = dm.store(node, host, "roster", roster);
89         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
90 end
91 function private_storage(node, host, xmlns, stanza)
92         local private = dm.load(node, host, "private") or {};
93         private[stanza.name..":"..xmlns] = st.preserialize(stanza);
94         local ret, err = dm.store(node, host, "private", private);
95         print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
96 end
97 function offline_msg(node, host, t, stanza)
98         stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
99         stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
100         local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
101         print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
102 end
103 function privacy(node, host, default, lists)
104         local privacy = { lists = {} };
105         local count = 0;
106         if default then privacy.default = default; end
107         for _, inlist in ipairs(lists) do
108                 local name, items = inlist[1], inlist[2];
109                 local list = { name = name; items = {}; };
110                 local orders = {};
111                 for _, item in pairs(items) do
112                         repeat
113                                 if item[1] ~= "listitem" then print("[error] privacy: unhandled item: "..tostring(item[1])); break; end
114                                 local _type, value = item[2], item[3];
115                                 if _type == "jid" then
116                                         if type(value) ~= "table" then print("[error] privacy: jid value is not valid: "..tostring(value)); break; end
117                                         local _node, _host, _resource = value[1], value[2], value[3];
118                                         value = build_jid(value, true)
119                                 elseif _type == "none" then
120                                         _type = nil;
121                                         value = nil;
122                                 elseif _type == "group" then
123                                         if type(value) ~= "string" then print("[error] privacy: group value is not string: "..tostring(value)); break; end
124                                 elseif _type == "subscription" then
125                                         if value~="both" and value~="from" and value~="to" and value~="none" then
126                                                 print("[error] privacy: subscription value is invalid: "..tostring(value)); break;
127                                         end
128                                 else print("[error] privacy: invalid item type: "..tostring(_type)); break; end
129                                 local action = item[4];
130                                 if action ~= "allow" and action ~= "deny" then print("[error] privacy: unhandled action: "..tostring(action)); break; end
131                                 local order = item[5];
132                                 if type(order) ~= "number" or order<0 then print("[error] privacy: order is not numeric: "..tostring(order)); break; end
133                                 if orders[order] then print("[error] privacy: duplicate order value: "..tostring(order)); break; end
134                                 orders[order] = true;
135                                 local match_all = item[6];
136                                 local match_iq = item[7];
137                                 local match_message = item[8];
138                                 local match_presence_in = item[9];
139                                 local match_presence_out = item[10];
140                                 list.items[#list.items+1] = {
141                                         type = _type;
142                                         value = value;
143                                         action = action;
144                                         order = order;
145                                         message = match_message == "true";
146                                         iq = match_iq == "true";
147                                         ["presence-in"] = match_presence_in == "true";
148                                         ["presence-out"] = match_presence_out == "true";
149                                 };
150                         until true;
151                 end
152                 table.sort(list.items, function(a, b) return a.order < b.order; end);
153                 if privacy.lists[list.name] then print("[warn] duplicate privacy list: "..tostring(list.name)); end
154                 privacy.lists[list.name] = list;
155                 count = count + 1;
156         end
157         if default and not privacy.lists[default] then
158                 if default == "none" then privacy.default = nil;
159                 else print("[warn] default privacy list doesn't exist: "..tostring(default)); end
160         end
161         local ret, err = dm.store(node, host, "privacy", privacy);
162         print("["..(err or "success").."] privacy: " ..node.."@"..host.." - "..count.." list(s)");
163 end
164 function muc_room(node, host, properties)
165         local store = { jid = node.."@"..host, _data = {}, _affiliations = {} };
166         for _,aff in ipairs(properties.affiliations) do
167                 store._affiliations[build_jid(aff[1])] = aff[2][1] or aff[2];
168         end
169         store._data.subject = properties.subject;
170         if properties.subject_author then
171                 store._data.subject_from = store.jid .. "/" .. properties.subject_author;
172         end
173         store._data.name = properties.title;
174         store._data.description = properties.description;
175         store._data.password = properties.password;
176         store._data.moderated = (properties.moderated == "true") or nil;
177         store._data.members_only = (properties.members_only == "true") or nil;
178         store._data.persistent = (properties.persistent == "true") or nil;
179         store._data.changesubject = (properties.allow_change_subj == "true") or nil;
180         store._data.whois = properties.anonymous == "true" and "moderators" or "anyone";
181         store._data.hidden = (properties.public_list == "false") or nil;
182
183         if not store._data.persistent then
184                 return print("[error] muc_room: skipping non-persistent room: "..node.."@"..host);
185         end
186
187         local ret, err = dm.store(node, host, "config", store);
188         if ret then
189                 ret, err = dm.load(nil, host, "persistent");
190                 if ret or not err then
191                         ret = ret or {};
192                         ret[store.jid] = true;
193                         ret, err = dm.store(nil, host, "persistent", ret);
194                 end
195         end
196         print("["..(err or "success").."] muc_room: " ..node.."@"..host);
197 end
198
199
200 local filters = {
201         passwd = function(tuple)
202                 password(tuple[2][1], tuple[2][2], tuple[3]);
203         end;
204         vcard = function(tuple)
205                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
206         end;
207         roster = function(tuple)
208                 local node = tuple[3][1]; local host = tuple[3][2];
209                 local contact = build_jid(tuple[4]);
210                 local name = tuple[5]; local subscription = tuple[6];
211                 local ask = tuple[7]; local groups = tuple[8];
212                 if type(name) ~= type("") then name = nil; end
213                 if ask == "none" then
214                         ask = nil;
215                 elseif ask == "out" then
216                         ask = "subscribe"
217                 elseif ask == "in" then
218                         roster_pending(node, host, contact);
219                         ask = nil;
220                 elseif ask == "both" then
221                         roster_pending(node, host, contact);
222                         ask = "subscribe";
223                 else error("Unknown ask type: "..ask); end
224                 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
225                 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
226                 for _, g in ipairs(groups) do
227                         if type(g) == "string" then
228                                 item.groups[g] = true;
229                         end
230                 end
231                 roster(node, host, contact, item);
232         end;
233         private_storage = function(tuple)
234                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
235         end;
236         offline_msg = function(tuple)
237                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
238         end;
239         privacy = function(tuple)
240                 privacy(tuple[2][1], tuple[2][2], tuple[3], tuple[4]);
241         end;
242         muc_room = function(tuple)
243                 local properties = {};
244                 for _,pair in ipairs(tuple[3]) do
245                         if not(type(pair[2]) == "table" and #pair[2] == 0) then -- skip nil values
246                                 properties[pair[1]] = pair[2];
247                         end
248                 end
249                 muc_room(tuple[2][1], tuple[2][2], properties);
250         end;
251         --[=[config = function(tuple)
252                 if tuple[2] == "hosts" then
253                         local output = io.output(); io.output("prosody.cfg.lua");
254                         io.write("-- Configuration imported from ejabberd --\n");
255                         io.write([[Host "*"
256         modules_enabled = {
257                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
258                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
259                 "roster"; -- Allow users to have a roster. Recommended ;)
260                 "register"; -- Allow users to register on this server using a client
261                 "tls"; -- Add support for secure TLS on c2s/s2s connections
262                 "vcard"; -- Allow users to set vCards
263                 "private"; -- Private XML storage (for room bookmarks, etc.)
264                 "version"; -- Replies to server version requests
265                 "dialback"; -- s2s dialback support
266                 "uptime";
267                 "disco";
268                 "time";
269                 "ping";
270                 --"selftests";
271         };
272 ]]);
273                         for _, h in ipairs(tuple[3]) do
274                                 io.write("Host \"" .. h .. "\"\n");
275                         end
276                         io.output(output);
277                         print("prosody.cfg.lua created");
278                 end
279         end;]=]
280 };
281
282 local arg = ...;
283 local help = "/? -? ? /h -h /help -help --help";
284 if not arg or help:find(arg, 1, true) then
285         print([[ejabberd db dump importer for Prosody
286
287   Usage: ]]..my_name..[[ filename.txt
288
289 The file can be generated from ejabberd using:
290   sudo ejabberdctl dump filename.txt
291
292 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
293         os.exit(1);
294 end
295 local count = 0;
296 local t = {};
297 for item in erlparse.parseFile(arg) do
298         count = count + 1;
299         local name = item[1];
300         t[name] = (t[name] or 0) + 1;
301         --print(count, serialize(item));
302         if filters[name] then filters[name](item); end
303 end
304 --print(serialize(t));