3 -- Copyright (C) 2008 Matthew Wild
4 -- Copyright (C) 2008 Waqas Hussain
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.
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.
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.
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
33 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
\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
40 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
\r
41 function getpath(username, host, datastore, ext)
\r
44 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
\r
46 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
\r
48 return format("%s/%s.%s", data_path, datastore, ext);
\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
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
65 function build_stanza(tuple, stanza)
\r
66 if tuple[1] == "xmlelement" then
\r
67 local name = tuple[2];
\r
69 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
\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
77 error("unknown element type: "..serialize(tuple));
\r
80 function build_time(tuple)
\r
81 local Megaseconds,Seconds,Microseconds = unpack(tuple);
\r
82 return Megaseconds * 1000000 + Seconds;
\r
85 function vcard(node, host, stanza)
\r
87 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
\r
88 print("["..(err or "success").."] vCard: "..node.."@"..host);
\r
90 function password(node, host, password)
\r
92 local ret, err = dm.store(node, host, "accounts", {password = password});
\r
93 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
\r
95 function roster(node, host, jid, item)
\r
97 local roster = dm.load(node, host, "roster") or {};
\r
99 local ret, err = dm.store(node, host, "roster", roster);
\r
100 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
\r
102 function private_storage(node, host, xmlns, stanza)
\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
109 function offline_msg(node, host, t, stanza)
\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
119 passwd = function(tuple)
\r
120 password(tuple[2][1], tuple[2][2], tuple[3]);
\r
122 vcard = function(tuple)
\r
123 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
\r
125 roster = function(tuple)
\r
126 local node = tuple[3][1]; local host = tuple[3][2];
\r
127 local contact = 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
137 private_storage = function(tuple)
\r
138 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
\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
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
165 for _, h in ipairs(tuple[3]) do
\r
166 io.write("Host \"" .. h .. "\"\n");
\r
169 print("prosody.cfg.lua created");
\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
179 Usage: ejabberd2prosody.lua filename.txt
\r
181 The file can be generated from ejabberd using:
\r
182 sudo ./bin/ejabberdctl dump filename.txt
\r
184 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
\r
189 for item in erlparse.parseFile(arg) do
\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
196 --print(serialize(t));
\r