mod_muc_auto_reserve_nicks: Automatically reserve nicknames of MUC occupants
authorMatthew Wild <mwild1@gmail.com>
Tue, 16 Nov 2021 15:08:09 +0000
changeset 4776 85d4ab318d66
parent 4775 e227af629736
child 4777 eb63890ae8fc
mod_muc_auto_reserve_nicks: Automatically reserve nicknames of MUC occupants
mod_muc_auto_reserve_nicks/README.md
mod_muc_auto_reserve_nicks/mod_muc_auto_reserve_nicks.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_auto_reserve_nicks/README.md	Tue Nov 16 15:08:09 2021 +0000
@@ -0,0 +1,25 @@
+---
+labels:
+- 'Stage-Alpha'
+summary: 'Automatically reserve nicknames of MUC users'
+...
+
+Introduction
+============
+
+This module automatically reserves the nickname of a user when they first join
+a MUC. That's all.
+
+Details
+=======
+
+The module doesn't currently update the registration if the user changes their
+nick. That could cause flip-flopping if the user has two clients in regular
+use with different nicks configured.
+
+Compatibility
+=============
+
+Requires Prosody trunk (0.12) for the API introduced in commit
+[0e7dedd8b18d](https://hg.prosody.im/trunk/rev/0e7dedd8b18d) and
+[e0b58717f0c5](https://hg.prosody.im/trunk/rev/e0b58717f0c5).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_auto_reserve_nicks/mod_muc_auto_reserve_nicks.lua	Tue Nov 16 15:08:09 2021 +0000
@@ -0,0 +1,19 @@
+local jid = require "util.jid";
+local set = require "util.set";
+
+local active_affiliations = set.new({ "member", "admin", "owner" });
+
+module:hook("muc-occupant-joined", function (event)
+	local room, occupant = event.room, event.occupant;
+	local user_jid = occupant.bare_jid;
+	local user_affiliation = room:get_affiliation(user_jid);
+	if not active_affiliations:contains(user_affiliation) then
+		return;
+	end
+	local aff_data = event.room:get_affiliation_data(user_jid);
+	if not aff_data then
+		local reserved_nick = jid.resource(occupant.nick);
+		module:log("debug", "Automatically reserving nickname '%s' for <%s>", reserved_nick, user_jid);
+		room:set_affiliation_data(user_jid, "reserved_nickname", reserved_nick);
+	end
+end);