view plugins/muc/gc3.lib.lua @ 13955:01f95f3de6fc

usermanager: More consistent method behaviour, errors, and (pre-)events This commit refactors most of the exported usermanager methods substantially to ensure that all of them correctly check for the presence of the host and method they are calling and return consistent errors if either is incorrect or missing. It also unifies the event and logging code, so as a result there are more events being fired than before this commit (it also adds pre- events so that modules can block an action from going ahead). We don't have good test coverage of this API, but some manual testing suggests it seems to be working okay...
author Matthew Wild <mwild1@gmail.com>
date Thu, 25 Sep 2025 16:53:35 +0100
parents 356c8ec22e9a
children
line wrap: on
line source


local jid_bare = require "prosody.util.jid".bare;

local st = require "prosody.util.stanza";

local muc_util = module:require "muc/util";
local valid_affiliations = muc_util.valid_affiliations;

local xmlns_gc3 = "urn:xmpp:gc3:tmp"

--luacheck: ignore 113/get_room_from_jid

function fetch_participant_list(room, stanza, origin)
	local from_jid = stanza.attr.from;
	local from_aff = room:get_affiliation(from_jid);
	local from_rank = valid_affiliations[from_aff or "none"];

	local required_aff_rank = valid_affiliations[room:get_members_only() and "member" or "none"];
	if from_rank < required_aff_rank then
		origin.send(st.error_reply(stanza, "auth", "forbidden"));
		return true;
	end

	local can_see_real_jids = not room:is_anonymous_for(from_jid);

	local reply = st.reply(stanza)
		:tag("participants", { xmlns = xmlns_gc3 });

	for jid, aff in room:each_affiliation() do
		local nick = room:get_registered_nick(jid);
		local visible_jid = can_see_real_jids and jid or nil;
		reply:text_tag("item", nil, {
			affiliation = aff;
			jid = visible_jid;
			nick = nick;
			id = room:get_occupant_id_from_jid(jid);
		});
	end

	origin.send(reply);
	return true;
end

local function room_event_handler(handler, allow_nonexistent)
	return function (event)
		local origin, stanza = event.origin, event.stanza;
		local room_jid = jid_bare(stanza.attr.to);
		local room = get_room_from_jid(room_jid);
		if not room and allow_nonexistent ~= true then
			origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
			return true;
		end
		return handler(room, stanza, origin);
	end;
end

module:hook("iq-get/bare/"..xmlns_gc3..":participants", room_event_handler(fetch_participant_list));