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