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