Diff

plugins/muc/mod_muc.lua @ 1754:67b66eec9777

MUC: Added support for room configuration forms, persistence and hidden rooms.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 07 Sep 2009 20:51:59 +0500
parent 1748:f4c50c75af6f
child 1767:649dd3439809
line wrap: on
line diff
--- a/plugins/muc/mod_muc.lua	Mon Sep 07 20:50:06 2009 +0500
+++ b/plugins/muc/mod_muc.lua	Mon Sep 07 20:51:59 2009 +0500
@@ -21,11 +21,50 @@
 local jid_split = require "util.jid".split;
 local st = require "util.stanza";
 local uuid_gen = require "util.uuid".generate;
+local datamanager = require "util.datamanager";
 
 local rooms = {};
+local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
 local component;
+
+local function room_route_stanza(room, stanza) core_post_stanza(component, stanza); end
+local function room_save(room, forced)
+	local node = jid_split(room.jid);
+	persistent_rooms[room.jid] = room._data.persistent;
+	module:log("debug", "1, %s, %s", room.jid, tostring(room._data.persistent));
+	if room._data.persistent then
+		module:log("debug", "2");
+		local history = room._data.history;
+		room._data.history = nil;
+		local data = {
+			jid = room.jid;
+			_data = room._data;
+			_affiliations = room._affiliations;
+		};
+		datamanager.store(node, muc_host, "config", data);
+		room._data.history = history;
+	elseif forced then
+		module:log("debug", "3");
+		datamanager.store(node, muc_host, "config", nil);
+	end
+	module:log("debug", "4");
+	if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
+end
+
+for jid in pairs(persistent_rooms) do
+	local node = jid_split(jid);
+	local data = datamanager.load(node, muc_host, "config") or {};
+	local room = muc_new_room(jid);
+	room._data = data._data;
+	room._affiliations = data._affiliations;
+	room.route_stanza = room_route_stanza;
+	room.save = room_save;
+	rooms[jid] = room;
+end
+
 local host_room = muc_new_room(muc_host);
-host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
+host_room.route_stanza = room_route_stanza;
+host_room.save = room_save;
 
 local function get_disco_info(stanza)
 	return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
@@ -35,7 +74,9 @@
 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
-		reply:tag("item", {jid=jid, name=jid}):up();
+		if not room._data.hidden then
+			reply:tag("item", {jid=jid, name=jid}):up();
+		end
 	end
 	return reply; -- TODO cache disco reply
 end
@@ -68,7 +109,8 @@
 			local room = rooms[bare];
 			if not room then
 				room = muc_new_room(bare);
-				room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
+				room.route_stanza = room_route_stanza;
+				room.save = room_save;
 				rooms[bare] = room;
 			end
 			room:handle_stanza(origin, stanza);
@@ -95,7 +137,8 @@
 		room._occupants = oldroom._occupants;
 		room._data = oldroom._data;
 		room._affiliations = oldroom._affiliations;
-		room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
+		room.route_stanza = room_route_stanza;
+		room.save = room_save;
 		rooms[jid] = room;
 	end
 	prosody.hosts[module:get_host()].muc = { rooms = rooms };