Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang...
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2 \r
3 require "erlparse";\r
4 require "serialize";\r
5 \r
6 package.path = package.path ..";../?.lua";\r
7 local st = require "util.stanza";\r
8 package.loaded["util.logger"] = {init = function() return function() end; end}\r
9 local dm = require "util.datamanager"\r
10 local data_path = "data";\r
11 dm.set_data_path(data_path);\r
12 \r
13 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end\r
14 local _mkdir = {}\r
15 function mkdir(path)\r
16         path = path:gsub("/", path_separator);\r
17         --print("mkdir",path);\r
18         local x = io.popen("mkdir "..path.." 2>&1"):read("*a");\r
19 end\r
20 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end\r
21 function getpath(username, host, datastore, ext)\r
22         ext = ext or "dat";\r
23         if username then\r
24                 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);\r
25         elseif host then\r
26                 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);\r
27         else\r
28                 return format("%s/%s.%s", data_path, datastore, ext);\r
29         end\r
30 end\r
31 function mkdirs(host)\r
32         if not _mkdir[host] then\r
33                 local host_dir = string.format("%s/%s", data_path, encode(host));\r
34                 mkdir(host_dir);\r
35                 mkdir(host_dir.."/accounts");\r
36                 mkdir(host_dir.."/vcard");\r
37                 mkdir(host_dir.."/roster");\r
38                 mkdir(host_dir.."/private");\r
39                 mkdir(host_dir.."/offline");\r
40                 _mkdir[host] = true;\r
41         end\r
42 end\r
43 mkdir(data_path);\r
44 \r
45 function build_stanza(tuple, stanza)\r
46         if tuple[1] == "xmlelement" then\r
47                 local name = tuple[2];\r
48                 local attr = {};\r
49                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end\r
50                 local up;\r
51                 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end\r
52                 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end\r
53                 if up then stanza:up(); else return stanza end\r
54         elseif tuple[1] == "xmlcdata" then\r
55                 stanza:text(tuple[2]);\r
56         else\r
57                 error("unknown element type: "..serialize.serialize(tuple));\r
58         end\r
59 end\r
60 function build_time(tuple)\r
61         local Megaseconds,Seconds,Microseconds = unpack(tuple);\r
62         return Megaseconds * 1000000 + Seconds;\r
63 end\r
64 \r
65 function vcard(node, host, stanza)\r
66         mkdirs(host);\r
67         local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));\r
68         print("["..(err or "success").."] vCard: "..node.."@"..host);\r
69 end\r
70 function password(node, host, password)\r
71         mkdirs(host);\r
72         local ret, err = dm.store(node, host, "accounts", {password = password});\r
73         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);\r
74 end\r
75 function roster(node, host, jid, item)\r
76         mkdirs(host);\r
77         local roster = dm.load(node, host, "roster") or {};\r
78         roster[jid] = item;\r
79         local ret, err = dm.store(node, host, "roster", roster);\r
80         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);\r
81 end\r
82 function private_storage(node, host, xmlns, stanza)\r
83         mkdirs(host);\r
84         local private = dm.load(node, host, "private") or {};\r
85         private[xmlns] = st.preserialize(stanza);\r
86         local ret, err = dm.store(node, host, "private", private);\r
87         print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);\r
88 end\r
89 function offline_msg(node, host, t, stanza)\r
90         mkdirs(host);\r
91         stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);\r
92         stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);\r
93         local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));\r
94         print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));\r
95 end\r
96 \r
97 \r
98 local filters = {\r
99         passwd = function(tuple)\r
100                 password(tuple[2][1], tuple[2][2], tuple[3]);\r
101         end;\r
102         vcard = function(tuple)\r
103                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));\r
104         end;\r
105         roster = function(tuple)\r
106                 local node = tuple[3][1]; local host = tuple[3][2];\r
107                 local contact = tuple[4][1].."@"..tuple[4][2];\r
108                 local name = tuple[5]; local subscription = tuple[6];\r
109                 local ask = tuple[7]; local groups = tuple[8];\r
110                 if type(name) ~= type("") then name = nil; end\r
111                 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end\r
112                 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end\r
113                 local item = {name = name, ask = ask, subscription = subscription, groups = {}};\r
114                 for _, g in ipairs(groups) do item.groups[g] = true; end\r
115                 roster(node, host, contact, item);\r
116         end;\r
117         private_storage = function(tuple)\r
118                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));\r
119         end;\r
120         offline_msg = function(tuple)\r
121                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));\r
122         end;\r
123         config = function(tuple)\r
124                 if tuple[2] == "hosts" then\r
125                         local output = io.output(); io.output("prosody.cfg.lua");\r
126                         io.write("-- Configuration imported from ejabberd --\n");\r
127                         io.write([[Host "*"\r
128         modules_enabled = {\r
129                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.\r
130                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.\r
131                 "roster"; -- Allow users to have a roster. Recommended ;)\r
132                 "register"; -- Allow users to register on this server using a client\r
133                 "tls"; -- Add support for secure TLS on c2s/s2s connections\r
134                 "vcard"; -- Allow users to set vCards\r
135                 "private"; -- Private XML storage (for room bookmarks, etc.)\r
136                 "version"; -- Replies to server version requests\r
137                 "dialback"; -- s2s dialback support\r
138                 "uptime";\r
139                 "disco";\r
140                 "time";\r
141                 "ping";\r
142                 --"selftests";\r
143         };\r
144 ]]);\r
145                         for _, h in ipairs(tuple[3]) do\r
146                                 io.write("Host \"" .. h .. "\"\n");\r
147                         end\r
148                         io.output(output);\r
149                         print("prosody.cfg.lua created");\r
150                 end\r
151         end;\r
152 };\r
153 \r
154 local arg = ...;\r
155 local help = "/? -? ? /h -h /help -help --help";\r
156 if not arg or help:find(arg, 1, true) then\r
157         print([[ejabberd db dump importer for Prosody\r
158 \r
159   Usage: ejabberd2prosody.lua filename.txt\r
160 \r
161 The file can be generated from ejabberd using:\r
162   sudo ./bin/ejabberdctl dump filename.txt\r
163 \r
164 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);\r
165         os.exit(1);\r
166 end\r
167 local count = 0;\r
168 local t = {};\r
169 for item in erlparse.parseFile(arg) do\r
170         count = count + 1;\r
171         local name = item[1];\r
172         t[name] = (t[name] or 0) + 1;\r
173         --print(count, serialize.serialize(item));\r
174         if filters[name] then filters[name](item); end\r
175 end\r
176 --print(serialize.serialize(t));\r