Autocommit.
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2
3 require "erlparse";
4 require "serialize";
5
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);
12
13 local _mkdir = {}
14 function mkdir(path)
15         if os.getenv("WINDIR") then
16                 -- I'm afraid it's true :(
17                 path = path:gsub("/", "\\");
18         end
19         --print("mkdir",path);
20         local x = io.popen("mkdir "..path.." 2>&1"):read("*a");
21 end
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)
24         ext = ext or "dat";
25         if username then
26                 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
27         elseif host then
28                 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
29         else
30                 return format("%s/%s.%s", data_path, datastore, ext);
31         end
32 end
33 function mkdirs(host)
34         if not _mkdir[host] then
35                 local host_dir = string.format("%s/%s", data_path, encode(host));
36                 mkdir(host_dir);
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");
42                 _mkdir[host] = true;
43         end
44 end
45 mkdir(data_path);
46
47 function build_stanza(tuple, stanza)
48         if tuple[1] == "xmlelement" then
49                 local name = tuple[2];
50                 local attr = {};
51                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
52                 local up;
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]);
58         else
59                 error("unknown element type: "..serialize.serialize(tuple));
60         end
61 end
62 function build_time(tuple)
63         local Megaseconds,Seconds,Microseconds = unpack(tuple);
64         return Megaseconds * 1000000 + Seconds;
65 end
66
67 function vcard(node, host, stanza)
68         mkdirs(host);
69         local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
70         print("["..(err or "success").."] vCard: "..node.."@"..host);
71 end
72 function password(node, host, password)
73         mkdirs(host);
74         local ret, err = dm.store(node, host, "accounts", {password = password});
75         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
76 end
77 function roster(node, host, jid, item)
78         mkdirs(host);
79         local roster = dm.load(node, host, "roster") or {};
80         roster[jid] = item;
81         local ret, err = dm.store(node, host, "roster", roster);
82         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
83 end
84 function private_storage(node, host, xmlns, stanza)
85         mkdirs(host);
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);
90 end
91 function offline_msg(node, host, t, stanza)
92         mkdirs(host);
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));
97 end
98
99
100 local filters = {
101         passwd = function(tuple)
102                 password(tuple[2][1], tuple[2][2], tuple[3]);
103         end;
104         vcard = function(tuple)
105                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
106         end;
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);
118         end;
119         private_storage = function(tuple)
120                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
121         end;
122         offline_msg = function(tuple)
123                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
124         end;
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");
129                         io.write([[Host "*"
130         modules_enabled = {
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
140                 "uptime";
141                 "disco";
142                 "time";
143                 "ping";
144                 --"selftests";
145         };
146 ]]);
147                         for _, h in ipairs(tuple[3]) do
148                                 io.write("Host \"" .. h .. "\"\n");
149                         end
150                         io.output(output);
151                         print("prosody.cfg.lua created");
152                 end
153         end;
154 };
155
156 local arg = ...;
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
160
161   Usage: ejabberd2prosody.lua filename.txt
162
163 The file can be generated from ejabberd using:
164   sudo ./bin/ejabberdctl dump filename.txt
165
166 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
167         os.exit(1);
168 end
169 local count = 0;
170 local t = {};
171 for item in erlparse.parseFile(arg) do
172         count = count + 1;
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
177 end
178 --print(serialize.serialize(t));