Changeset

11286:336cba957c88

MUC: Allow modules to add to the room registration form, and handle the result
author Matthew Wild <mwild1@gmail.com>
date Fri, 15 Jan 2021 18:42:45 +0000
parents 11285:5d61fc3cd194
children 11287:b602dd3c4bbc
files plugins/muc/register.lib.lua
diffstat 1 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/muc/register.lib.lua	Fri Jan 15 14:02:10 2021 +0000
+++ b/plugins/muc/register.lib.lua	Fri Jan 15 18:42:45 2021 +0000
@@ -56,6 +56,20 @@
 	{ name = "muc#register_roomnick", type = "text-single", required = true, label = "Nickname"},
 };
 
+module:handle_items("muc-registration-field", function (event)
+	module:log("debug", "Adding MUC registration form field: %s", event.item.name);
+	table.insert(registration_form, event.item);
+end, function (event)
+	module:log("debug", "Removing MUC registration form field: %s", event.item.name);
+	local removed_field_name = event.item.name;
+	for i, field in ipairs(registration_form) do
+		if field.name == removed_field_name then
+			table.remove(registration_form, i);
+			break;
+		end
+	end
+end);
+
 local function enforce_nick_policy(event)
 	local origin, stanza = event.origin, event.stanza;
 	local room = assert(event.room); -- FIXME
@@ -114,6 +128,8 @@
 	if stanza.attr.type == "get" then
 		reply:query("jabber:iq:register");
 		if registered_nick then
+			-- I find it strange, but XEP-0045 says not to include
+			-- the current registration data (only the registered name)
 			reply:tag("registered"):up();
 			reply:tag("username"):text(registered_nick);
 			origin.send(reply);
@@ -183,6 +199,13 @@
 		-- Checks passed, save the registration
 		if registered_nick ~= desired_nick then
 			local registration_data = { reserved_nickname = desired_nick };
+			module:fire_event("muc-registration-submitted", {
+				room = room;
+				origin = origin;
+				stanza = stanza;
+				submitted_data = reg_data;
+				affiliation_data = registration_data;
+			});
 			local ok, err_type, err_condition = room:set_affiliation(true, user_jid, affiliation or "member", nil, registration_data);
 			if not ok then
 				origin.send(st.error_reply(stanza, err_type, err_condition));