Changeset

13674:1ba58459c919 13.0

mod_invites: Add shell commands to list, show and delete pending invitations
author Matthew Wild <mwild1@gmail.com>
date Thu, 13 Feb 2025 13:04:37 +0000
parents 13673:a186f1cfae75
children 13675:40386b43de6f 13676:40c309f8b676
files plugins/mod_invites.lua
diffstat 1 files changed, 140 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_invites.lua	Thu Feb 13 12:21:16 2025 +0000
+++ b/plugins/mod_invites.lua	Thu Feb 13 13:04:37 2025 +0000
@@ -289,6 +289,146 @@
 	end;
 });
 
+module:add_item("shell-command", {
+	section = "invite";
+	section_desc = "Create and manage invitations";
+	name = "show";
+	desc = "Show details of an account invitation token";
+	args = { { name = "host", type = "string" }, { name = "token", type = "string" } };
+	host_selector = "host";
+
+	handler = function (self, host, token) --luacheck: ignore 212/self 212/host
+		local invite, err = get_account_invite_info(token);
+		if not invite then return nil, err; end
+
+		local print = self.session.print;
+
+		if invite.type == "roster" then
+			print("Invitation to register and become a contact of "..invite.jid);
+		elseif invite.type == "register" then
+			local jid_user, jid_host = jid_split(invite.jid);
+			if invite.additional_data and invite.additional_data.allow_reset then
+				print("Password reset for "..invite.additional_data.allow_reset.."@"..jid_host);
+			elseif jid_user then
+				print("Invitation to register on "..jid_host.." with username '"..jid_user.."'");
+			else
+				print("Invitation to register on "..jid_host);
+			end
+		else
+			print("Unknown invitation type");
+		end
+
+		if invite.inviter then
+			print("Creator:", invite.inviter);
+		end
+
+		print("Created:", os.date("%Y-%m-%d %T", invite.created_at));
+		print("Expires:", os.date("%Y-%m-%d %T", invite.expires));
+
+		print("");
+
+		if invite.uri then
+			print("XMPP URI:", invite.uri);
+		end
+
+		if invite.landing_page then
+			print("Web link:", invite.landing_page);
+		end
+
+		if invite.additional_data then
+			print("");
+			if invite.additional_data.roles then
+				if invite.additional_data.roles[1] then
+					print("Role:", invite.additional_data.roles[1]);
+				end
+				if invite.additional_data.roles[2] then
+					print("Secondary roles:", table.concat(invite.additional_data.roles, ", ", 2, #invite.additional_data.roles));
+				end
+			end
+			if invite.additional_data.groups then
+				print("Groups:", table.concat(invite.additional_data.groups, ", "));
+			end
+			if invite.additional_data.note then
+				print("Comment:", invite.additional_data.note);
+			end
+		end
+
+		return true, "Invitation valid";
+	end;
+});
+
+module:add_item("shell-command", {
+	section = "invite";
+	section_desc = "Create and manage invitations";
+	name = "delete";
+	desc = "Delete/revoke an invitation token";
+	args = { { name = "host", type = "string" }, { name = "token", type = "string" } };
+	host_selector = "host";
+
+	handler = function (self, host, token) --luacheck: ignore 212/self 212/host
+		local invite, err = delete_account_invite(token);
+		if not invite then return nil, err; end
+		return true, "Invitation deleted";
+	end;
+});
+
+module:add_item("shell-command", {
+	section = "invite";
+	section_desc = "Create and manage invitations";
+	name = "list";
+	desc = "List pending invitations which allow account registration";
+	args = { { name = "host", type = "string" } };
+	host_selector = "host";
+
+	handler = function (self, host) -- luacheck: ignore 212/host
+		local print_row = human_io.table({
+			{
+				title = "Token";
+				key = "invite";
+				width = 24;
+				mapper = function (invite)
+					return invite.token;
+				end;
+			};
+			{
+				title = "Expires";
+				key = "invite";
+				width = 20;
+				mapper = function (invite)
+					return os.date("%Y-%m-%dT%T", invite.expires);
+				end;
+			};
+			{
+				title = "Description";
+				key = "invite";
+				width = "100%";
+				mapper = function (invite)
+					if invite.type == "roster" then
+						return "Contact with "..invite.jid;
+					elseif invite.type == "register" then
+						local jid_user, jid_host = jid_split(invite.jid);
+						if invite.additional_data and invite.additional_data.allow_reset then
+							return "Password reset for "..invite.additional_data.allow_reset.."@"..jid_host;
+						end
+						if jid_user then
+							return "Register on "..jid_host.." with username "..jid_user;
+						end
+						return "Register on "..jid_host;
+					end
+				end;
+			};
+		}, self.session.width);
+
+		self.session.print(print_row());
+		local count = 0;
+		for _, invite in pending_account_invites() do
+			count = count + 1;
+			self.session.print(print_row({ invite = invite }));
+		end
+		return true, ("%d pending invites"):format(count);
+	end;
+});
+
 local subcommands = {};
 
 --- prosodyctl command