Comparison

tools/ejabberd2prosody.lua @ 487:86e77b6ba579

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