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.
14 package.path = package.path ..";../?.lua";
15 local serialize = require "util.serialization".serialize;
16 local st = require "util.stanza";
17 package.loaded["util.logger"] = {init = function() return function() end; end}
18 local dm = require "util.datamanager"
19 dm.set_data_path("data");
21 function build_stanza(tuple, stanza)
22 if tuple[1] == "xmlelement" then
23 local name = tuple[2];
25 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
27 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
28 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
29 if up then stanza:up(); else return stanza end
30 elseif tuple[1] == "xmlcdata" then
31 stanza:text(tuple[2]);
33 error("unknown element type: "..serialize(tuple));
36 function build_time(tuple)
37 local Megaseconds,Seconds,Microseconds = unpack(tuple);
38 return Megaseconds * 1000000 + Seconds;
41 function vcard(node, host, stanza)
42 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
43 print("["..(err or "success").."] vCard: "..node.."@"..host);
45 function password(node, host, password)
46 local ret, err = dm.store(node, host, "accounts", {password = password});
47 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
49 function roster(node, host, jid, item)
50 local roster = dm.load(node, host, "roster") or {};
52 local ret, err = dm.store(node, host, "roster", roster);
53 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
55 function roster_pending(node, host, jid)
56 local roster = dm.load(node, host, "roster") or {};
57 roster.pending = roster.pending or {};
58 roster.pending[jid] = true;
59 local ret, err = dm.store(node, host, "roster", roster);
60 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
62 function private_storage(node, host, xmlns, stanza)
63 local private = dm.load(node, host, "private") or {};
64 private[stanza.name..":"..xmlns] = st.preserialize(stanza);
65 local ret, err = dm.store(node, host, "private", private);
66 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
68 function offline_msg(node, host, t, stanza)
69 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
70 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
71 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
72 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
77 passwd = function(tuple)
78 password(tuple[2][1], tuple[2][2], tuple[3]);
80 vcard = function(tuple)
81 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
83 roster = function(tuple)
84 local node = tuple[3][1]; local host = tuple[3][2];
85 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];
86 local name = tuple[5]; local subscription = tuple[6];
87 local ask = tuple[7]; local groups = tuple[8];
88 if type(name) ~= type("") then name = nil; end
89 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" elseif ask == "in" then
90 roster_pending(node, host, contact);
93 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
94 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
95 for _, g in ipairs(groups) do item.groups[g] = true; end
96 roster(node, host, contact, item);
98 private_storage = function(tuple)
99 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
101 offline_msg = function(tuple)
102 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
104 config = function(tuple)
105 if tuple[2] == "hosts" then
106 local output = io.output(); io.output("prosody.cfg.lua");
107 io.write("-- Configuration imported from ejabberd --\n");
110 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
111 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
112 "roster"; -- Allow users to have a roster. Recommended ;)
113 "register"; -- Allow users to register on this server using a client
114 "tls"; -- Add support for secure TLS on c2s/s2s connections
115 "vcard"; -- Allow users to set vCards
116 "private"; -- Private XML storage (for room bookmarks, etc.)
117 "version"; -- Replies to server version requests
118 "dialback"; -- s2s dialback support
126 for _, h in ipairs(tuple[3]) do
127 io.write("Host \"" .. h .. "\"\n");
130 print("prosody.cfg.lua created");
136 local help = "/? -? ? /h -h /help -help --help";
137 if not arg or help:find(arg, 1, true) then
138 print([[ejabberd db dump importer for Prosody
140 Usage: ejabberd2prosody.lua filename.txt
142 The file can be generated from ejabberd using:
143 sudo ./bin/ejabberdctl dump filename.txt
145 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
150 for item in erlparse.parseFile(arg) do
152 local name = item[1];
153 t[name] = (t[name] or 0) + 1;
154 --print(count, serialize(item));
155 if filters[name] then filters[name](item); end
157 --print(serialize(t));