Merge 0.9->trunk
[prosody.git] / plugins / muc / mod_muc.lua
index 9f907f172736ac063988951c436588d83a5bc1ee..bc865ec92d4f55c6011f285555333b4e0dbcef35 100644 (file)
@@ -28,13 +28,14 @@ local jid_split = require "util.jid".split;
 local jid_bare = require "util.jid".bare;
 local st = require "util.stanza";
 local uuid_gen = require "util.uuid".generate;
-local datamanager = require "util.datamanager";
 local um_is_admin = require "core.usermanager".is_admin;
-local hosts = hosts;
+local hosts = prosody.hosts;
 
 rooms = {};
 local rooms = rooms;
-local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
+local persistent_rooms_storage = module:open_store("persistent");
+local persistent_rooms = persistent_rooms_storage:get() or {};
+local room_configs = module:open_store("config");
 
 -- Configurable options
 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
@@ -66,15 +67,15 @@ local function room_save(room, forced)
                        _data = room._data;
                        _affiliations = room._affiliations;
                };
-               datamanager.store(node, muc_host, "config", data);
+               room_configs:set(node, data);
                room._data.history = history;
        elseif forced then
-               datamanager.store(node, muc_host, "config", nil);
+               room_configs:set(node, nil);
                if not next(room._occupants) then -- Room empty
                        rooms[room.jid] = nil;
                end
        end
-       if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
+       if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
 end
 
 function create_room(jid)
@@ -82,13 +83,14 @@ function create_room(jid)
        room.route_stanza = room_route_stanza;
        room.save = room_save;
        rooms[jid] = room;
+       module:fire_event("muc-room-created", { room = room });
        return room;
 end
 
 local persistent_errors = false;
 for jid in pairs(persistent_rooms) do
        local node = jid_split(jid);
-       local data = datamanager.load(node, muc_host, "config");
+       local data = room_configs:get(node);
        if data then
                local room = create_room(jid);
                room._data = data._data;
@@ -99,7 +101,7 @@ for jid in pairs(persistent_rooms) do
                persistent_errors = true;
        end
 end
-if persistent_errors then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
+if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
 
 local host_room = muc_new_room(muc_host);
 host_room.route_stanza = room_route_stanza;
@@ -113,7 +115,7 @@ end
 local function get_disco_items(stanza)
        local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
        for jid, room in pairs(rooms) do
-               if not room:is_hidden() then
+               if not room:get_hidden() then
                        reply:tag("item", {jid=jid, name=room:get_name()}):up();
                end
        end
@@ -217,7 +219,8 @@ function shutdown_component()
        if not saved then
                local stanza = st.presence({type = "unavailable"})
                        :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
-                               :tag("item", { affiliation='none', role='none' }):up();
+                               :tag("item", { affiliation='none', role='none' }):up()
+                               :tag("status", { code = "332"}):up();
                for roomjid, room in pairs(rooms) do
                        shutdown_room(room, stanza);
                end