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