tools/ejabberd2prosody: Handle new room member format.
[prosody.git] / tools / ejabberd2prosody.lua
index c11e41d9e602676fba2f935600ad2b71f4b917ba..be1504b270c8ac7b61de062431885b519c0348a5 100755 (executable)
 
 package.path = package.path ..";../?.lua";
 
-if arg[0]:match("[/\\]") then
-       package.path = package.path .. ";"..arg[0]:gsub("[^/\\]*$", "?.lua");
+local my_name = arg[0];
+if my_name:match("[/\\]") then
+       package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
+       package.path = package.path..";"..my_name:gsub("[^/\\]+$", "?.lua");
+       package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
 end
 
 local erlparse = require "erlparse";
@@ -27,7 +30,7 @@ dm.set_data_path("data");
 
 function build_stanza(tuple, stanza)
        assert(type(tuple) == "table", "XML node is of unexpected type: "..type(tuple));
-       if tuple[1] == "xmlelement" then
+       if tuple[1] == "xmlelement" or tuple[1] == "xmlel" then
                assert(type(tuple[2]) == "string", "element name has type: "..type(tuple[2]));
                assert(type(tuple[3]) == "table", "element attribute array has type: "..type(tuple[3]));
                assert(type(tuple[4]) == "table", "element children array has type: "..type(tuple[4]));
@@ -149,6 +152,48 @@ function privacy(node, host, default, lists)
        local ret, err = dm.store(node, host, "privacy", privacy);
        print("["..(err or "success").."] privacy: " ..node.."@"..host.." - "..count.." list(s)");
 end
+local function _table_to_jid(t)
+       if type(t[2]) == "string" then
+               local jid = t[2];
+               if type(t[1]) == "string" then jid = t[1].."@"..jid; end
+               if type(t[3]) == "string" then jid = jid.."/"..t[3]; end
+               return jid;
+       end
+end
+function muc_room(node, host, properties)
+       local store = { jid = node.."@"..host, _data = {}, _affiliations = {} };
+       for _,aff in ipairs(properties.affiliations) do
+               store._affiliations[_table_to_jid(aff[1])] = aff[2][1] or aff[2];
+       end
+       store._data.subject = properties.subject;
+       if properties.subject_author then
+               store._data.subject_from = store.jid .. "/" .. properties.subject_author;
+       end
+       store._data.name = properties.title;
+       store._data.description = properties.description;
+       store._data.password = properties.password;
+       store._data.moderated = (properties.moderated == "true") or nil;
+       store._data.members_only = (properties.members_only == "true") or nil;
+       store._data.persistent = (properties.persistent == "true") or nil;
+       store._data.changesubject = (properties.allow_change_subj == "true") or nil;
+       store._data.whois = properties.anonymous == "true" and "moderators" or "anyone";
+       store._data.hidden = (properties.public_list == "false") or nil;
+
+       if not store._data.persistent then
+               return print("[error] muc_room: skipping non-persistent room: "..node.."@"..host);
+       end
+
+       local ret, err = dm.store(node, host, "config", store);
+       if ret then
+               ret, err = dm.load(nil, host, "persistent");
+               if ret or not err then
+                       ret = ret or {};
+                       ret[store.jid] = true;
+                       ret, err = dm.store(nil, host, "persistent", ret);
+               end
+       end
+       print("["..(err or "success").."] muc_room: " ..node.."@"..host);
+end
 
 
 local filters = {
@@ -193,7 +238,16 @@ local filters = {
        privacy = function(tuple)
                privacy(tuple[2][1], tuple[2][2], tuple[3], tuple[4]);
        end;
-       config = function(tuple)
+       muc_room = function(tuple)
+               local properties = {};
+               for _,pair in ipairs(tuple[3]) do
+                       if not(type(pair[2]) == "table" and #pair[2] == 0) then -- skip nil values
+                               properties[pair[1]] = pair[2];
+                       end
+               end
+               muc_room(tuple[2][1], tuple[2][2], properties);
+       end;
+       --[=[config = function(tuple)
                if tuple[2] == "hosts" then
                        local output = io.output(); io.output("prosody.cfg.lua");
                        io.write("-- Configuration imported from ejabberd --\n");
@@ -221,7 +275,7 @@ local filters = {
                        io.output(output);
                        print("prosody.cfg.lua created");
                end
-       end;
+       end;]=]
 };
 
 local arg = ...;
@@ -229,10 +283,10 @@ local help = "/? -? ? /h -h /help -help --help";
 if not arg or help:find(arg, 1, true) then
        print([[ejabberd db dump importer for Prosody
 
-  Usage: ejabberd2prosody.lua filename.txt
+  Usage: ]]..my_name..[[ filename.txt
 
 The file can be generated from ejabberd using:
-  sudo ./bin/ejabberdctl dump filename.txt
+  sudo ejabberdctl dump filename.txt
 
 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
        os.exit(1);