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