6 package.path = package.path ..";../?.lua";
7 local st = require "util.stanza";
8 package.loaded["util.logger"] = {init = function() return function() end; end}
9 local dm = require "util.datamanager"
10 local data_path = "data";
11 dm.set_data_path(data_path);
15 if os.getenv("WINDIR") then
16 -- I'm afraid it's true :(
17 path = path:gsub("/", "\\");
19 --print("mkdir",path);
20 local x = io.popen("mkdir "..path.." 2>&1"):read("*a");
22 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
23 function getpath(username, host, datastore, ext)
26 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
28 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
30 return format("%s/%s.%s", data_path, datastore, ext);
34 if not _mkdir[host] then
35 local host_dir = string.format("%s/%s", data_path, encode(host));
37 mkdir(host_dir.."/accounts");
38 mkdir(host_dir.."/vcard");
39 mkdir(host_dir.."/roster");
40 mkdir(host_dir.."/private");
41 mkdir(host_dir.."/offline");
47 function build_stanza(tuple, stanza)
48 if tuple[1] == "xmlelement" then
49 local name = tuple[2];
51 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
53 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
54 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
55 if up then stanza:up(); else return stanza end
56 elseif tuple[1] == "xmlcdata" then
57 stanza:text(tuple[2]);
59 error("unknown element type: "..serialize.serialize(tuple));
62 function build_time(tuple)
63 local Megaseconds,Seconds,Microseconds = unpack(tuple);
64 return Megaseconds * 1000000 + Seconds;
67 function vcard(node, host, stanza)
69 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
70 print("["..(err or "success").."] vCard: "..node.."@"..host);
72 function password(node, host, password)
74 local ret, err = dm.store(node, host, "accounts", {password = password});
75 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
77 function roster(node, host, jid, item)
79 local roster = dm.load(node, host, "roster") or {};
81 local ret, err = dm.store(node, host, "roster", roster);
82 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
84 function private_storage(node, host, xmlns, stanza)
86 local private = dm.load(node, host, "private") or {};
87 private[xmlns] = st.preserialize(stanza);
88 local ret, err = dm.store(node, host, "private", private);
89 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
91 function offline_msg(node, host, t, stanza)
93 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
94 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
95 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
96 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
101 passwd = function(tuple)
102 password(tuple[2][1], tuple[2][2], tuple[3]);
104 vcard = function(tuple)
105 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
107 roster = function(tuple)
108 local node = tuple[3][1]; local host = tuple[3][2];
109 local contact = tuple[4][1].."@"..tuple[4][2];
110 local name = tuple[5]; local subscription = tuple[6];
111 local ask = tuple[7]; local groups = tuple[8];
112 if type(name) ~= type("") then name = nil; end
113 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end
114 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
115 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
116 for _, g in ipairs(groups) do item.groups[g] = true; end
117 roster(node, host, contact, item);
119 private_storage = function(tuple)
120 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
122 offline_msg = function(tuple)
123 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
125 config = function(tuple)
126 if tuple[2] == "hosts" then
127 local output = io.output(); io.output("prosody.cfg.lua");
128 io.write("-- Configuration imported from ejabberd --\n");
131 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
132 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
133 "roster"; -- Allow users to have a roster. Recommended ;)
134 "register"; -- Allow users to register on this server using a client
135 "tls"; -- Add support for secure TLS on c2s/s2s connections
136 "vcard"; -- Allow users to set vCards
137 "private"; -- Private XML storage (for room bookmarks, etc.)
138 "version"; -- Replies to server version requests
139 "dialback"; -- s2s dialback support
147 for _, h in ipairs(tuple[3]) do
148 io.write("Host \"" .. h .. "\"\n");
151 print("prosody.cfg.lua created");
157 local help = "/? -? ? /h -h /help -help --help";
158 if not arg or help:find(arg, 1, true) then
159 print([[ejabberd db dump importer for Prosody
161 Usage: ejabberd2prosody.lua filename.txt
163 The file can be generated from ejabberd using:
164 sudo ./bin/ejabberdctl dump filename.txt
166 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
171 for item in erlparse.parseFile(arg) do
173 local name = item[1];
174 t[name] = (t[name] or 0) + 1;
175 --print(count, serialize.serialize(item));
176 if filters[name] then filters[name](item); end
178 --print(serialize.serialize(t));