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.path = package.path..";"..my_name:gsub("[^/\\]+$", "?.lua");
18 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
21 local erlparse = require "erlparse";
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");
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];
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
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
52 error("unknown element type: "..serialize(tuple));
55 function build_time(tuple)
56 local Megaseconds,Seconds,Microseconds = unpack(tuple);
57 return Megaseconds * 1000000 + Seconds;
60 function vcard(node, host, stanza)
61 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
62 print("["..(err or "success").."] vCard: "..node.."@"..host);
64 function password(node, host, password)
65 local ret, err = dm.store(node, host, "accounts", {password = password});
66 print("["..(err or "success").."] accounts: "..node.."@"..host);
68 function roster(node, host, jid, item)
69 local roster = dm.load(node, host, "roster") or {};
71 local ret, err = dm.store(node, host, "roster", roster);
72 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
74 function roster_pending(node, host, jid)
75 local roster = dm.load(node, host, "roster") or {};
76 roster.pending = roster.pending or {};
77 roster.pending[jid] = true;
78 local ret, err = dm.store(node, host, "roster", roster);
79 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
81 function private_storage(node, host, xmlns, stanza)
82 local private = dm.load(node, host, "private") or {};
83 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
84 local ret, err = dm.store(node, host, "private", private);
85 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
87 function offline_msg(node, host, t, stanza)
88 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
89 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
90 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
91 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
93 function privacy(node, host, default, lists)
94 local privacy = { lists = {} };
96 if default then privacy.default = default; end
97 for _, inlist in ipairs(lists) do
98 local name, items = inlist[1], inlist[2];
99 local list = { name = name; items = {}; };
101 for _, item in pairs(items) do
103 if item[1] ~= "listitem" then print("[error] privacy: unhandled item: "..tostring(item[1])); break; end
104 local _type, value = item[2], item[3];
105 if _type == "jid" then
106 if type(value) ~= "table" then print("[error] privacy: jid value is not valid: "..tostring(value)); break; end
107 local _node, _host, _resource = value[1], value[2], value[3];
108 if (type(_node) == "table") then _node = nil; end
109 if (type(_host) == "table") then _host = nil; end
110 if (type(_resource) == "table") then _resource = nil; end
111 value = (_node and _node.."@".._host or _host)..(_resource and "/".._resource or "");
112 elseif _type == "none" then
115 elseif _type == "group" then
116 if type(value) ~= "string" then print("[error] privacy: group value is not string: "..tostring(value)); break; end
117 elseif _type == "subscription" then
118 if value~="both" and value~="from" and value~="to" and value~="none" then
119 print("[error] privacy: subscription value is invalid: "..tostring(value)); break;
121 else print("[error] privacy: invalid item type: "..tostring(_type)); break; end
122 local action = item[4];
123 if action ~= "allow" and action ~= "deny" then print("[error] privacy: unhandled action: "..tostring(action)); break; end
124 local order = item[5];
125 if type(order) ~= "number" or order<0 then print("[error] privacy: order is not numeric: "..tostring(order)); break; end
126 if orders[order] then print("[error] privacy: duplicate order value: "..tostring(order)); break; end
127 orders[order] = true;
128 local match_all = item[6];
129 local match_iq = item[7];
130 local match_message = item[8];
131 local match_presence_in = item[9];
132 local match_presence_out = item[10];
133 list.items[#list.items+1] = {
138 message = match_message == "true";
139 iq = match_iq == "true";
140 ["presence-in"] = match_presence_in == "true";
141 ["presence-out"] = match_presence_out == "true";
145 table.sort(list.items, function(a, b) return a.order < b.order; end);
146 if privacy.lists[list.name] then print("[warn] duplicate privacy list: "..tostring(list.name)); end
147 privacy.lists[list.name] = list;
150 if default and not privacy.lists[default] then
151 if default == "none" then privacy.default = nil;
152 else print("[warn] default privacy list doesn't exist: "..tostring(default)); end
154 local ret, err = dm.store(node, host, "privacy", privacy);
155 print("["..(err or "success").."] privacy: " ..node.."@"..host.." - "..count.." list(s)");
157 local function _table_to_jid(t)
158 if type(t[2]) == "string" then
160 if type(t[1]) == "string" then jid = t[1].."@"..jid; end
161 if type(t[3]) == "string" then jid = jid.."/"..t[3]; end
165 function muc_room(node, host, properties)
166 local store = { jid = node.."@"..host, _data = {}, _affiliations = {} };
167 for _,aff in ipairs(properties.affiliations) do
168 store._affiliations[_table_to_jid(aff[1])] = aff[2][1] or aff[2];
170 store._data.subject = properties.subject;
171 if properties.subject_author then
172 store._data.subject_from = store.jid .. "/" .. properties.subject_author;
174 store._data.name = properties.title;
175 store._data.description = properties.description;
176 store._data.password = properties.password;
177 store._data.moderated = (properties.moderated == "true") or nil;
178 store._data.members_only = (properties.members_only == "true") or nil;
179 store._data.persistent = (properties.persistent == "true") or nil;
180 store._data.changesubject = (properties.allow_change_subj == "true") or nil;
181 store._data.whois = properties.anonymous == "true" and "moderators" or "anyone";
182 store._data.hidden = (properties.public_list == "false") or nil;
184 if not store._data.persistent then
185 return print("[error] muc_room: skipping non-persistent room: "..node.."@"..host);
188 local ret, err = dm.store(node, host, "config", store);
190 ret, err = dm.load(nil, host, "persistent");
191 if ret or not err then
193 ret[store.jid] = true;
194 ret, err = dm.store(nil, host, "persistent", ret);
197 print("["..(err or "success").."] muc_room: " ..node.."@"..host);
202 passwd = function(tuple)
203 password(tuple[2][1], tuple[2][2], tuple[3]);
205 vcard = function(tuple)
206 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
208 roster = function(tuple)
209 local node = tuple[3][1]; local host = tuple[3][2];
210 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];
211 local name = tuple[5]; local subscription = tuple[6];
212 local ask = tuple[7]; local groups = tuple[8];
213 if type(name) ~= type("") then name = nil; end
214 if ask == "none" then
216 elseif ask == "out" then
218 elseif ask == "in" then
219 roster_pending(node, host, contact);
221 elseif ask == "both" then
222 roster_pending(node, host, contact);
224 else error("Unknown ask type: "..ask); end
225 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
226 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
227 for _, g in ipairs(groups) do
228 if type(g) == "string" then
229 item.groups[g] = true;
232 roster(node, host, contact, item);
234 private_storage = function(tuple)
235 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
237 offline_msg = function(tuple)
238 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
240 privacy = function(tuple)
241 privacy(tuple[2][1], tuple[2][2], tuple[3], tuple[4]);
243 muc_room = function(tuple)
244 local properties = {};
245 for _,pair in ipairs(tuple[3]) do
246 if not(type(pair[2]) == "table" and #pair[2] == 0) then -- skip nil values
247 properties[pair[1]] = pair[2];
250 muc_room(tuple[2][1], tuple[2][2], properties);
252 --[=[config = function(tuple)
253 if tuple[2] == "hosts" then
254 local output = io.output(); io.output("prosody.cfg.lua");
255 io.write("-- Configuration imported from ejabberd --\n");
258 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
259 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
260 "roster"; -- Allow users to have a roster. Recommended ;)
261 "register"; -- Allow users to register on this server using a client
262 "tls"; -- Add support for secure TLS on c2s/s2s connections
263 "vcard"; -- Allow users to set vCards
264 "private"; -- Private XML storage (for room bookmarks, etc.)
265 "version"; -- Replies to server version requests
266 "dialback"; -- s2s dialback support
274 for _, h in ipairs(tuple[3]) do
275 io.write("Host \"" .. h .. "\"\n");
278 print("prosody.cfg.lua created");
284 local help = "/? -? ? /h -h /help -help --help";
285 if not arg or help:find(arg, 1, true) then
286 print([[ejabberd db dump importer for Prosody
288 Usage: ]]..my_name..[[ filename.txt
290 The file can be generated from ejabberd using:
291 sudo ejabberdctl dump filename.txt
293 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
298 for item in erlparse.parseFile(arg) do
300 local name = item[1];
301 t[name] = (t[name] or 0) + 1;
302 --print(count, serialize(item));
303 if filters[name] then filters[name](item); end
305 --print(serialize(t));