plugins/muc/muc.lib.lua
changeset 6214 9813c74ce006
parent 6213 95bddf0142f4
child 6215 1dd09dc04945
equal deleted inserted replaced
6213:95bddf0142f4 6214:9813c74ce006
   419 end);
   419 end);
   420 module:hook("muc-disco#info", function(event)
   420 module:hook("muc-disco#info", function(event)
   421 	event.reply:tag("feature", {var = event.room:get_hidden() and "muc_hidden" or "muc_public"}):up();
   421 	event.reply:tag("feature", {var = event.room:get_hidden() and "muc_hidden" or "muc_public"}):up();
   422 end);
   422 end);
   423 module:hook("muc-disco#info", function(event)
   423 module:hook("muc-disco#info", function(event)
   424 	event.reply:tag("feature", {var = event.room:get_whois() ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up();
       
   425 end);
       
   426 module:hook("muc-disco#info", function(event)
       
   427 	local count = 0; for _ in event.room:each_occupant() do count = count + 1; end
   424 	local count = 0; for _ in event.room:each_occupant() do count = count + 1; end
   428 	table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
   425 	table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
   429 end);
   426 end);
   430 
   427 
   431 function room_mt:get_disco_items(stanza)
   428 function room_mt:get_disco_items(stanza)
   539 	length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
   536 	length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
   540 	if length == default_history_length then
   537 	if length == default_history_length then
   541 		length = nil;
   538 		length = nil;
   542 	end
   539 	end
   543 	self._data.history_length = length;
   540 	self._data.history_length = length;
   544 end
       
   545 
       
   546 
       
   547 local valid_whois = { moderators = true, anyone = true };
       
   548 
       
   549 function room_mt:set_whois(whois)
       
   550 	if valid_whois[whois] and self._data.whois ~= whois then
       
   551 		self._data.whois = whois;
       
   552 		if self.save then self:save(true); end
       
   553 	end
       
   554 end
       
   555 
       
   556 function room_mt:get_whois()
       
   557 	return self._data.whois;
       
   558 end
   541 end
   559 
   542 
   560 -- Give the room creator owner affiliation
   543 -- Give the room creator owner affiliation
   561 module:hook("muc-room-pre-create", function(event)
   544 module:hook("muc-room-pre-create", function(event)
   562 	event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
   545 	event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
   871 		label = 'Allow Occupants to Change Subject?',
   854 		label = 'Allow Occupants to Change Subject?',
   872 		value = event.room:get_changesubject()
   855 		value = event.room:get_changesubject()
   873 	});
   856 	});
   874 end);
   857 end);
   875 module:hook("muc-config-form", function(event)
   858 module:hook("muc-config-form", function(event)
   876 	local whois = event.room:get_whois();
       
   877 	table.insert(event.form, {
       
   878 		name = 'muc#roomconfig_whois',
       
   879 		type = 'list-single',
       
   880 		label = 'Who May Discover Real JIDs?',
       
   881 		value = {
       
   882 			{ value = 'moderators', label = 'Moderators Only', default = whois == 'moderators' },
       
   883 			{ value = 'anyone',     label = 'Anyone',          default = whois == 'anyone' }
       
   884 		}
       
   885 	});
       
   886 end);
       
   887 module:hook("muc-config-form", function(event)
       
   888 	table.insert(event.form, {
   859 	table.insert(event.form, {
   889 		name = 'muc#roomconfig_moderatedroom',
   860 		name = 'muc#roomconfig_moderatedroom',
   890 		type = 'boolean',
   861 		type = 'boolean',
   891 		label = 'Make Room Moderated?',
   862 		label = 'Make Room Moderated?',
   892 		value = event.room:get_moderated()
   863 		value = event.room:get_moderated()
   964 module:hook("muc-config-submitted", function(event)
   935 module:hook("muc-config-submitted", function(event)
   965 	event.update_option("changesubject", "muc#roomconfig_changesubject");
   936 	event.update_option("changesubject", "muc#roomconfig_changesubject");
   966 end);
   937 end);
   967 module:hook("muc-config-submitted", function(event)
   938 module:hook("muc-config-submitted", function(event)
   968 	event.update_option("historylength", "muc#roomconfig_historylength");
   939 	event.update_option("historylength", "muc#roomconfig_historylength");
   969 end);
       
   970 module:hook("muc-config-submitted", function(event)
       
   971 	if event.update_option("whois", "muc#roomconfig_whois", valid_whois) then
       
   972 		local code = (event.room:get_whois() == 'moderators') and "173" or "172";
       
   973 		event.status_codes[code] = true;
       
   974 	end
       
   975 end);
   940 end);
   976 
   941 
   977 -- Removes everyone from the room
   942 -- Removes everyone from the room
   978 function room_mt:clear(x)
   943 function room_mt:clear(x)
   979 	x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
   944 	x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
  1216 	stanza:tag("body")
  1181 	stanza:tag("body")
  1217 		:text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
  1182 		:text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
  1218 	:up();
  1183 	:up();
  1219 end);
  1184 end);
  1220 
  1185 
  1221 -- Mask 'from' jid as occupant jid if room is anonymous
  1186 -- When an invite is sent; add an affiliation for the invitee
  1222 module:hook("muc-invite", function(event)
  1187 module:hook("muc-invite", function(event)
  1223 	local room, stanza = event.room, event.stanza;
  1188 	local room, stanza = event.room, event.stanza;
  1224 	if room:get_whois() == "moderators" and room:get_default_role(room:get_affiliation(stanza.attr.to)) ~= "moderator" then
       
  1225 		local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
       
  1226 		local occupant_jid = room:get_occupant_jid(invite.attr.from);
       
  1227 		if occupant_jid ~= nil then -- FIXME: This will expose real jid if inviter is not in room
       
  1228 			invite.attr.from = occupant_jid;
       
  1229 		end
       
  1230 	end
       
  1231 end, 50);
       
  1232 
       
  1233 -- When an invite is sent; add an affiliation for the invitee
       
  1234 module:hook("muc-invite", function(event)
       
  1235 	local room, stanza = event.room, event.stanza
       
  1236 	local invitee = stanza.attr.to
  1189 	local invitee = stanza.attr.to
  1237 	if room:get_members_only() and not room:get_affiliation(invitee) then
  1190 	if room:get_members_only() and not room:get_affiliation(invitee) then
  1238 		local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
  1191 		local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
  1239 		log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
  1192 		log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
  1240 		room:set_affiliation(from, invitee, "member", "Invited by " .. from); -- This might fail; ignore for now
  1193 		room:set_affiliation(from, invitee, "member", "Invited by " .. from); -- This might fail; ignore for now
  1453 
  1406 
  1454 local password = module:require "muc/password";
  1407 local password = module:require "muc/password";
  1455 room_mt.get_password = password.get;
  1408 room_mt.get_password = password.get;
  1456 room_mt.set_password = password.set;
  1409 room_mt.set_password = password.set;
  1457 
  1410 
       
  1411 local whois = module:require "muc/whois";
       
  1412 room_mt.get_whois = whois.get;
       
  1413 room_mt.set_whois = whois.set;
       
  1414 
  1458 local _M = {}; -- module "muc"
  1415 local _M = {}; -- module "muc"
  1459 
  1416 
  1460 function _M.new_room(jid, config)
  1417 function _M.new_room(jid, config)
  1461 	return setmetatable({
  1418 	return setmetatable({
  1462 		jid = jid;
  1419 		jid = jid;
  1463 		_jid_nick = {};
  1420 		_jid_nick = {};
  1464 		_occupants = {};
  1421 		_occupants = {};
  1465 		_data = {
  1422 		_data = {
  1466 		    whois = 'moderators';
       
  1467 		    history_length = math.min((config and config.history_length)
  1423 		    history_length = math.min((config and config.history_length)
  1468 		    	or default_history_length, max_history_length);
  1424 		    	or default_history_length, max_history_length);
  1469 		};
  1425 		};
  1470 		_affiliations = {};
  1426 		_affiliations = {};
  1471 	}, room_mt);
  1427 	}, room_mt);