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