ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
[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 require "erlparse";
13
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");
20
21 function build_stanza(tuple, stanza)
22         if tuple[1] == "xmlelement" then
23                 local name = tuple[2];
24                 local attr = {};
25                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
26                 local up;
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]);
32         else
33                 error("unknown element type: "..serialize(tuple));
34         end
35 end
36 function build_time(tuple)
37         local Megaseconds,Seconds,Microseconds = unpack(tuple);
38         return Megaseconds * 1000000 + Seconds;
39 end
40
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);
44 end
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);
48 end
49 function roster(node, host, jid, item)
50         local roster = dm.load(node, host, "roster") or {};
51         roster[jid] = item;
52         local ret, err = dm.store(node, host, "roster", roster);
53         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
54 end
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);
61 end
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);
67 end
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));
73 end
74
75
76 local filters = {
77         passwd = function(tuple)
78                 password(tuple[2][1], tuple[2][2], tuple[3]);
79         end;
80         vcard = function(tuple)
81                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
82         end;
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);
91                         return;
92                 else error(ask) end
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);
97         end;
98         private_storage = function(tuple)
99                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
100         end;
101         offline_msg = function(tuple)
102                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
103         end;
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");
108                         io.write([[Host "*"
109         modules_enabled = {
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
119                 "uptime";
120                 "disco";
121                 "time";
122                 "ping";
123                 --"selftests";
124         };
125 ]]);
126                         for _, h in ipairs(tuple[3]) do
127                                 io.write("Host \"" .. h .. "\"\n");
128                         end
129                         io.output(output);
130                         print("prosody.cfg.lua created");
131                 end
132         end;
133 };
134
135 local arg = ...;
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
139
140   Usage: ejabberd2prosody.lua filename.txt
141
142 The file can be generated from ejabberd using:
143   sudo ./bin/ejabberdctl dump filename.txt
144
145 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
146         os.exit(1);
147 end
148 local count = 0;
149 local t = {};
150 for item in erlparse.parseFile(arg) do
151         count = count + 1;
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
156 end
157 --print(serialize(t));