3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 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");
20 local serialize = require "util.serialization".serialize;
21 local st = require "util.stanza";
22 package.loaded["util.logger"] = {init = function() return function() end; end}
23 local dm = require "util.datamanager"
24 dm.set_data_path("data");
26 function build_stanza(tuple, stanza)
27 if tuple[1] == "xmlelement" then
28 local name = tuple[2];
30 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
32 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
33 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
34 if up then stanza:up(); else return stanza end
35 elseif tuple[1] == "xmlcdata" then
36 stanza:text(tuple[2]);
38 error("unknown element type: "..serialize(tuple));
41 function build_time(tuple)
42 local Megaseconds,Seconds,Microseconds = unpack(tuple);
43 return Megaseconds * 1000000 + Seconds;
46 function vcard(node, host, stanza)
47 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
48 print("["..(err or "success").."] vCard: "..node.."@"..host);
50 function password(node, host, password)
51 local ret, err = dm.store(node, host, "accounts", {password = password});
52 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
54 function roster(node, host, jid, item)
55 local roster = dm.load(node, host, "roster") or {};
57 local ret, err = dm.store(node, host, "roster", roster);
58 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
60 function roster_pending(node, host, jid)
61 local roster = dm.load(node, host, "roster") or {};
62 roster.pending = roster.pending or {};
63 roster.pending[jid] = true;
64 local ret, err = dm.store(node, host, "roster", roster);
65 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
67 function private_storage(node, host, xmlns, stanza)
68 local private = dm.load(node, host, "private") or {};
69 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
70 local ret, err = dm.store(node, host, "private", private);
71 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
73 function offline_msg(node, host, t, stanza)
74 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
75 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
76 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
77 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
82 passwd = function(tuple)
83 password(tuple[2][1], tuple[2][2], tuple[3]);
85 vcard = function(tuple)
86 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
88 roster = function(tuple)
89 local node = tuple[3][1]; local host = tuple[3][2];
90 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];
91 local name = tuple[5]; local subscription = tuple[6];
92 local ask = tuple[7]; local groups = tuple[8];
93 if type(name) ~= type("") then name = nil; end
96 elseif ask == "out" then
98 elseif ask == "in" then
99 roster_pending(node, host, contact);
101 elseif ask == "both" then
102 roster_pending(node, host, contact);
104 else error("Unknown ask type: "..ask); end
105 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
106 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
107 for _, g in ipairs(groups) do item.groups[g] = true; end
108 roster(node, host, contact, item);
110 private_storage = function(tuple)
111 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
113 offline_msg = function(tuple)
114 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
116 config = function(tuple)
117 if tuple[2] == "hosts" then
118 local output = io.output(); io.output("prosody.cfg.lua");
119 io.write("-- Configuration imported from ejabberd --\n");
122 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
123 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
124 "roster"; -- Allow users to have a roster. Recommended ;)
125 "register"; -- Allow users to register on this server using a client
126 "tls"; -- Add support for secure TLS on c2s/s2s connections
127 "vcard"; -- Allow users to set vCards
128 "private"; -- Private XML storage (for room bookmarks, etc.)
129 "version"; -- Replies to server version requests
130 "dialback"; -- s2s dialback support
138 for _, h in ipairs(tuple[3]) do
139 io.write("Host \"" .. h .. "\"\n");
142 print("prosody.cfg.lua created");
148 local help = "/? -? ? /h -h /help -help --help";
149 if not arg or help:find(arg, 1, true) then
150 print([[ejabberd db dump importer for Prosody
152 Usage: ejabberd2prosody.lua filename.txt
154 The file can be generated from ejabberd using:
155 sudo ./bin/ejabberdctl dump filename.txt
157 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
162 for item in erlparse.parseFile(arg) do
164 local name = item[1];
165 t[name] = (t[name] or 0) + 1;
166 --print(count, serialize(item));
167 if filters[name] then filters[name](item); end
169 --print(serialize(t));