diff plugins/mod_roster.lua @ 13950:d00409d2329f 13.0

mod_roster: Add command for cleaning out invalid contact JIDs Complements #1903 With the new validation, any existing roster entries with invalid JIDs can't be touched via normal means and thus can't be removed.
author Kim Alvefur <zash@zash.se>
date Thu, 25 Sep 2025 16:51:06 +0200
parents 35819bc9471a
children beb5a667c20d
line wrap: on
line diff
--- a/plugins/mod_roster.lua	Sat Sep 20 17:46:46 2025 +0200
+++ b/plugins/mod_roster.lua	Thu Sep 25 16:51:06 2025 +0200
@@ -313,3 +313,36 @@
 	end;
 });
 
+module:add_item("shell-command", {
+	section = "roster";
+	section_desc = "View and manage user rosters (contact lists)";
+	name = "clean";
+	desc = "Remove invalid JIDs from roster";
+	args = {
+		{ name = "jid", type = "string" };
+	};
+	host_selector = "jid";
+	handler = function(self, jid) -- luacheck: ignore 212/self
+		local function iter(user, host)
+			if user then return pairs({ [user] = true }); end
+			local users = require"prosody.core.usermanager".users;
+			return users(host);
+		end
+
+		local user, host = jid_split(jid);
+		local removed = 0;
+		for user_ in iter(user, host) do
+			local roster = assert(rm_load_roster(user_, host));
+			for contact in pairs(roster) do
+				if type(contact) == "string" then
+					if not jid_prep(contact) or jid_resource(contact) then
+						roster[contact] = nil;
+						removed = removed + 1;
+					end
+				end
+			end
+		end
+		return true, ("%d invalid roster entrie(s) removed"):format(removed);
+	end;
+});
+