Merge 0.7->trunk
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2 -- Prosody IM
3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- 
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10
11
12 package.path = package.path ..";../?.lua";
13
14 if arg[0]:match("^./") then
15         package.path = package.path .. ";"..arg[0]:gsub("/ejabberd2prosody.lua$", "/?.lua");
16 end
17
18 require "erlparse";
19
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");
25
26 function build_stanza(tuple, stanza)
27         if tuple[1] == "xmlelement" then
28                 local name = tuple[2];
29                 local attr = {};
30                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
31                 local up;
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]);
37         else
38                 error("unknown element type: "..serialize(tuple));
39         end
40 end
41 function build_time(tuple)
42         local Megaseconds,Seconds,Microseconds = unpack(tuple);
43         return Megaseconds * 1000000 + Seconds;
44 end
45
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);
49 end
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);
53 end
54 function roster(node, host, jid, item)
55         local roster = dm.load(node, host, "roster") or {};
56         roster[jid] = item;
57         local ret, err = dm.store(node, host, "roster", roster);
58         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
59 end
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);
66 end
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);
72 end
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));
78 end
79
80
81 local filters = {
82         passwd = function(tuple)
83                 password(tuple[2][1], tuple[2][2], tuple[3]);
84         end;
85         vcard = function(tuple)
86                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
87         end;
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
94                 if ask == "none" then
95                         ask = nil;
96                 elseif ask == "out" then
97                         ask = "subscribe"
98                 elseif ask == "in" then
99                         roster_pending(node, host, contact);
100                         ask = nil;
101                 elseif ask == "both" then
102                         roster_pending(node, host, contact);
103                         ask = "subscribe";
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
108                         if type(g) == "string" then
109                                 item.groups[g] = true;
110                         end
111                 end
112                 roster(node, host, contact, item);
113         end;
114         private_storage = function(tuple)
115                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
116         end;
117         offline_msg = function(tuple)
118                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
119         end;
120         config = function(tuple)
121                 if tuple[2] == "hosts" then
122                         local output = io.output(); io.output("prosody.cfg.lua");
123                         io.write("-- Configuration imported from ejabberd --\n");
124                         io.write([[Host "*"
125         modules_enabled = {
126                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
127                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
128                 "roster"; -- Allow users to have a roster. Recommended ;)
129                 "register"; -- Allow users to register on this server using a client
130                 "tls"; -- Add support for secure TLS on c2s/s2s connections
131                 "vcard"; -- Allow users to set vCards
132                 "private"; -- Private XML storage (for room bookmarks, etc.)
133                 "version"; -- Replies to server version requests
134                 "dialback"; -- s2s dialback support
135                 "uptime";
136                 "disco";
137                 "time";
138                 "ping";
139                 --"selftests";
140         };
141 ]]);
142                         for _, h in ipairs(tuple[3]) do
143                                 io.write("Host \"" .. h .. "\"\n");
144                         end
145                         io.output(output);
146                         print("prosody.cfg.lua created");
147                 end
148         end;
149 };
150
151 local arg = ...;
152 local help = "/? -? ? /h -h /help -help --help";
153 if not arg or help:find(arg, 1, true) then
154         print([[ejabberd db dump importer for Prosody
155
156   Usage: ejabberd2prosody.lua filename.txt
157
158 The file can be generated from ejabberd using:
159   sudo ./bin/ejabberdctl dump filename.txt
160
161 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
162         os.exit(1);
163 end
164 local count = 0;
165 local t = {};
166 for item in erlparse.parseFile(arg) do
167         count = count + 1;
168         local name = item[1];
169         t[name] = (t[name] or 0) + 1;
170         --print(count, serialize(item));
171         if filters[name] then filters[name](item); end
172 end
173 --print(serialize(t));