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 if arg[0]:match("^./") then
15 package.path = package.path .. ";"..arg[0]:gsub("/ejabberd2prosody.lua$", "/?.lua");
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");
28 function build_stanza(tuple, stanza)
29 if tuple[1] == "xmlelement" then
30 local name = tuple[2];
32 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
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]);
40 error("unknown element type: "..serialize(tuple));
43 function build_time(tuple)
44 local Megaseconds,Seconds,Microseconds = unpack(tuple);
45 return Megaseconds * 1000000 + Seconds;
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);
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);
56 function roster(node, host, jid, item)
57 local roster = dm.load(node, host, "roster") or {};
59 local ret, err = dm.store(node, host, "roster", roster);
60 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
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);
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);
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));
84 passwd = function(tuple)
85 password(tuple[2][1], tuple[2][2], tuple[3]);
87 vcard = function(tuple)
88 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
90 roster = function(tuple)
91 local node = tuple[3][1]; local host = tuple[3][2];
92 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];
93 local name = tuple[5]; local subscription = tuple[6];
94 local ask = tuple[7]; local groups = tuple[8];
95 if type(name) ~= type("") then name = nil; end
98 elseif ask == "out" then
100 elseif ask == "in" then
101 roster_pending(node, host, contact);
103 elseif ask == "both" then
104 roster_pending(node, host, contact);
106 else error("Unknown ask type: "..ask); end
107 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
108 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
109 for _, g in ipairs(groups) do
110 if type(g) == "string" then
111 item.groups[g] = true;
114 roster(node, host, contact, item);
116 private_storage = function(tuple)
117 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
119 offline_msg = function(tuple)
120 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
122 config = function(tuple)
123 if tuple[2] == "hosts" then
124 local output = io.output(); io.output("prosody.cfg.lua");
125 io.write("-- Configuration imported from ejabberd --\n");
128 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
129 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
130 "roster"; -- Allow users to have a roster. Recommended ;)
131 "register"; -- Allow users to register on this server using a client
132 "tls"; -- Add support for secure TLS on c2s/s2s connections
133 "vcard"; -- Allow users to set vCards
134 "private"; -- Private XML storage (for room bookmarks, etc.)
135 "version"; -- Replies to server version requests
136 "dialback"; -- s2s dialback support
144 for _, h in ipairs(tuple[3]) do
145 io.write("Host \"" .. h .. "\"\n");
148 print("prosody.cfg.lua created");
154 local help = "/? -? ? /h -h /help -help --help";
155 if not arg or help:find(arg, 1, true) then
156 print([[ejabberd db dump importer for Prosody
158 Usage: ejabberd2prosody.lua filename.txt
160 The file can be generated from ejabberd using:
161 sudo ./bin/ejabberdctl dump filename.txt
163 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
168 for item in erlparse.parseFile(arg) do
170 local name = item[1];
171 t[name] = (t[name] or 0) + 1;
172 --print(count, serialize(item));
173 if filters[name] then filters[name](item); end
175 --print(serialize(t));