3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
12 package.path = package.path ..";../?.lua";
14 local my_name = arg[0];
15 if my_name:match("[/\\]") then
16 package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
17 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
20 local erlparse = require "erlparse";
24 package.loaded["util.logger"] = {init = function() return function() end; end}
25 local serialize = require "util.serialization".serialize;
26 local st = require "util.stanza";
27 local dm = require "util.datamanager"
28 dm.set_data_path("data");
30 function build_stanza(tuple, stanza)
31 assert(type(tuple) == "table", "XML node is of unexpected type: "..type(tuple));
32 if tuple[1] == "xmlelement" then
33 assert(type(tuple[2]) == "string", "element name has type: "..type(tuple[2]));
34 assert(type(tuple[3]) == "table", "element attribute array has type: "..type(tuple[3]));
35 assert(type(tuple[4]) == "table", "element children array has type: "..type(tuple[4]));
36 local name = tuple[2];
38 for _, a in ipairs(tuple[3]) do
39 if type(a[1]) == "string" and type(a[2]) == "string" then attr[a[1]] = a[2]; end
42 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
43 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
44 if up then stanza:up(); else return stanza end
45 elseif tuple[1] == "xmlcdata" then
46 assert(type(tuple[2]) == "string", "XML CDATA has unexpected type: "..type(tuple[2]));
47 stanza:text(tuple[2]);
49 error("unknown element type: "..serialize(tuple));
52 function build_time(tuple)
53 local Megaseconds,Seconds,Microseconds = unpack(tuple);
54 return Megaseconds * 1000000 + Seconds;
57 function vcard(node, host, stanza)
58 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
59 print("["..(err or "success").."] vCard: "..node.."@"..host);
61 function password(node, host, password)
62 local ret, err = dm.store(node, host, "accounts", {password = password});
63 print("["..(err or "success").."] accounts: "..node.."@"..host);
65 function roster(node, host, jid, item)
66 local roster = dm.load(node, host, "roster") or {};
68 local ret, err = dm.store(node, host, "roster", roster);
69 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
71 function roster_pending(node, host, jid)
72 local roster = dm.load(node, host, "roster") or {};
73 roster.pending = roster.pending or {};
74 roster.pending[jid] = true;
75 local ret, err = dm.store(node, host, "roster", roster);
76 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
78 function private_storage(node, host, xmlns, stanza)
79 local private = dm.load(node, host, "private") or {};
80 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
81 local ret, err = dm.store(node, host, "private", private);
82 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
84 function offline_msg(node, host, t, stanza)
85 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
86 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
87 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
88 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
90 function privacy(node, host, default, lists)
91 local privacy = { lists = {} };
93 if default then privacy.default = default; end
94 for _, inlist in ipairs(lists) do
95 local name, items = inlist[1], inlist[2];
96 local list = { name = name; items = {}; };
98 for _, item in pairs(items) do
100 if item[1] ~= "listitem" then print("[error] privacy: unhandled item: "..tostring(item[1])); break; end
101 local _type, value = item[2], item[3];
102 if _type == "jid" then
103 if type(value) ~= "table" then print("[error] privacy: jid value is not valid: "..tostring(value)); break; end
104 local _node, _host, _resource = value[1], value[2], value[3];
105 if (type(_node) == "table") then _node = nil; end
106 if (type(_host) == "table") then _host = nil; end
107 if (type(_resource) == "table") then _resource = nil; end
108 value = (_node and _node.."@".._host or _host)..(_resource and "/".._resource or "");
109 elseif _type == "none" then
112 elseif _type == "group" then
113 if type(value) ~= "string" then print("[error] privacy: group value is not string: "..tostring(value)); break; end
114 elseif _type == "subscription" then
115 if value~="both" and value~="from" and value~="to" and value~="none" then
116 print("[error] privacy: subscription value is invalid: "..tostring(value)); break;
118 else print("[error] privacy: invalid item type: "..tostring(_type)); break; end
119 local action = item[4];
120 if action ~= "allow" and action ~= "deny" then print("[error] privacy: unhandled action: "..tostring(action)); break; end
121 local order = item[5];
122 if type(order) ~= "number" or order<0 then print("[error] privacy: order is not numeric: "..tostring(order)); break; end
123 if orders[order] then print("[error] privacy: duplicate order value: "..tostring(order)); break; end
124 orders[order] = true;
125 local match_all = item[6];
126 local match_iq = item[7];
127 local match_message = item[8];
128 local match_presence_in = item[9];
129 local match_presence_out = item[10];
130 list.items[#list.items+1] = {
135 message = match_message == "true";
136 iq = match_iq == "true";
137 ["presence-in"] = match_presence_in == "true";
138 ["presence-out"] = match_presence_out == "true";
142 table.sort(list.items, function(a, b) return a.order < b.order; end);
143 if privacy.lists[list.name] then print("[warn] duplicate privacy list: "..tostring(list.name)); end
144 privacy.lists[list.name] = list;
147 if default and not privacy.lists[default] then
148 if default == "none" then privacy.default = nil;
149 else print("[warn] default privacy list doesn't exist: "..tostring(default)); end
151 local ret, err = dm.store(node, host, "privacy", privacy);
152 print("["..(err or "success").."] privacy: " ..node.."@"..host.." - "..count.." list(s)");
157 passwd = function(tuple)
158 password(tuple[2][1], tuple[2][2], tuple[3]);
160 vcard = function(tuple)
161 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
163 roster = function(tuple)
164 local node = tuple[3][1]; local host = tuple[3][2];
165 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];
166 local name = tuple[5]; local subscription = tuple[6];
167 local ask = tuple[7]; local groups = tuple[8];
168 if type(name) ~= type("") then name = nil; end
169 if ask == "none" then
171 elseif ask == "out" then
173 elseif ask == "in" then
174 roster_pending(node, host, contact);
176 elseif ask == "both" then
177 roster_pending(node, host, contact);
179 else error("Unknown ask type: "..ask); end
180 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
181 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
182 for _, g in ipairs(groups) do
183 if type(g) == "string" then
184 item.groups[g] = true;
187 roster(node, host, contact, item);
189 private_storage = function(tuple)
190 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
192 offline_msg = function(tuple)
193 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
195 privacy = function(tuple)
196 privacy(tuple[2][1], tuple[2][2], tuple[3], tuple[4]);
198 config = function(tuple)
199 if tuple[2] == "hosts" then
200 local output = io.output(); io.output("prosody.cfg.lua");
201 io.write("-- Configuration imported from ejabberd --\n");
204 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
205 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
206 "roster"; -- Allow users to have a roster. Recommended ;)
207 "register"; -- Allow users to register on this server using a client
208 "tls"; -- Add support for secure TLS on c2s/s2s connections
209 "vcard"; -- Allow users to set vCards
210 "private"; -- Private XML storage (for room bookmarks, etc.)
211 "version"; -- Replies to server version requests
212 "dialback"; -- s2s dialback support
220 for _, h in ipairs(tuple[3]) do
221 io.write("Host \"" .. h .. "\"\n");
224 print("prosody.cfg.lua created");
230 local help = "/? -? ? /h -h /help -help --help";
231 if not arg or help:find(arg, 1, true) then
232 print([[ejabberd db dump importer for Prosody
234 Usage: ]]..my_name..[[ filename.txt
236 The file can be generated from ejabberd using:
237 sudo ejabberdctl dump filename.txt
239 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
244 for item in erlparse.parseFile(arg) do
246 local name = item[1];
247 t[name] = (t[name] or 0) + 1;
248 --print(count, serialize(item));
249 if filters[name] then filters[name](item); end
251 --print(serialize(t));