ejabberd2prosody: More intelligent searching for erlparse library
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2 -- Prosody IM
3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 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.." = "..password);
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 ask = nil; elseif ask == "out" then ask = "subscribe" elseif ask == "in" then
95                         roster_pending(node, host, contact);
96                         return;
97                 else error(ask) end
98                 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
99                 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
100                 for _, g in ipairs(groups) do item.groups[g] = true; end
101                 roster(node, host, contact, item);
102         end;
103         private_storage = function(tuple)
104                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
105         end;
106         offline_msg = function(tuple)
107                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
108         end;
109         config = function(tuple)
110                 if tuple[2] == "hosts" then
111                         local output = io.output(); io.output("prosody.cfg.lua");
112                         io.write("-- Configuration imported from ejabberd --\n");
113                         io.write([[Host "*"
114         modules_enabled = {
115                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
116                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
117                 "roster"; -- Allow users to have a roster. Recommended ;)
118                 "register"; -- Allow users to register on this server using a client
119                 "tls"; -- Add support for secure TLS on c2s/s2s connections
120                 "vcard"; -- Allow users to set vCards
121                 "private"; -- Private XML storage (for room bookmarks, etc.)
122                 "version"; -- Replies to server version requests
123                 "dialback"; -- s2s dialback support
124                 "uptime";
125                 "disco";
126                 "time";
127                 "ping";
128                 --"selftests";
129         };
130 ]]);
131                         for _, h in ipairs(tuple[3]) do
132                                 io.write("Host \"" .. h .. "\"\n");
133                         end
134                         io.output(output);
135                         print("prosody.cfg.lua created");
136                 end
137         end;
138 };
139
140 local arg = ...;
141 local help = "/? -? ? /h -h /help -help --help";
142 if not arg or help:find(arg, 1, true) then
143         print([[ejabberd db dump importer for Prosody
144
145   Usage: ejabberd2prosody.lua filename.txt
146
147 The file can be generated from ejabberd using:
148   sudo ./bin/ejabberdctl dump filename.txt
149
150 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
151         os.exit(1);
152 end
153 local count = 0;
154 local t = {};
155 for item in erlparse.parseFile(arg) do
156         count = count + 1;
157         local name = item[1];
158         t[name] = (t[name] or 0) + 1;
159         --print(count, serialize(item));
160         if filters[name] then filters[name](item); end
161 end
162 --print(serialize(t));