Changeset

5637:38783b8acbc9

mod_invites_api: add `rename` command.
author Trần H. Trung <xmpp:trần.h.trung@trung.fun>
date Mon, 31 Jul 2023 12:49:04 +0700
parents 5636:7e5701fbae88
children 5638:4379e0e84ee5
files mod_invites_api/mod_invites_api.lua
diffstat 1 files changed, 83 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mod_invites_api/mod_invites_api.lua	Mon Jul 31 11:52:00 2023 +0700
+++ b/mod_invites_api/mod_invites_api.lua	Mon Jul 31 12:49:04 2023 +0700
@@ -133,9 +133,10 @@
 		print("> prosodyctl mod_"..module.name.." delete JID NAME");
 		print("> prosodyctl mod_"..module.name.." delete-id HOST ID");
 		print("");
-		print("Renew:");
+		print("Adjust:");
 		print("");
 		print("> prosodyctl mod_"..module.name.." renew JID NAME");
+		print("> prosodyctl mod_"..module.name.." rename JID NAME NEW_NAME");
 		print("");
 		print("Help:");
 		print("");
@@ -165,12 +166,28 @@
 			print(" Each user's account can only have 1 API key but hosts are unlimited.");
 			print("");
 			print("========================================================================");
+		elseif help_command == "rename" then
+			print("========================================================================");
+			print("");
+			print("Re-name a key:");
+			print("");
+			print("> prosodyctl mod_"..module.name.." rename JID NAME NEW_NAME");
+			print("");
+			print("------------------------------------------------------------------------");
+			print("");
+			print("Usage:");
+			print("");
+			print(" `JID` can only be a valid host.");
+			print("");
+			print(" You need to supply a `NEW_NAME` to replace « `NAME` » the old one.");
+			print("");
+			print("========================================================================");
 		elseif help_command == "renew" then
 			print("========================================================================");
 			print("");
 			print("Re-new a key:");
 			print("");
-			print("> prosodyctl mod_"..module.name.." create JID NAME");
+			print("> prosodyctl mod_"..module.name.." renew JID NAME");
 			print("");
 			print("------------------------------------------------------------------------");
 			print("");
@@ -379,6 +396,68 @@
 			end
 			return;
 		end
+	elseif command == "rename" then
+		local util_token = require "util.id".long();
+		local os_time = os.time();
+		if username then
+			local found;
+			if usermanager.user_exists(username, module.host) then
+				get_value(function (id, info)
+					if username.."@"..module.host == info.jid then
+						print("Found:");
+						print(date, id.."/"..util_token, info.jid);
+						found = true;
+					end
+				end);
+				if not found then
+					print("Error: Could not find the key for "..username.."@"..domain);
+					return 1;
+				end
+				print("Error: not allow to rename user's account.");
+				return 1;
+			else
+				print("Error: "..username.."@"..domain.." does not exists.");
+				return 1;
+			end
+		elseif domain then
+			local arg_name_orig = table.remove(arg, 1);
+			if not arg_name_orig or arg_name_orig == "" then
+				print("Error: key for host needs a `NAME`.");
+				return 1;
+			end
+			local arg_name_new = table.remove(arg, 1);
+			if not arg_name_new or arg_name_new == "" then
+				print("Error: need a `NEW_NAME` to replace "..arg_name_orig);
+				return 1;
+			end
+
+			found = false;
+			get_value(function (id, info)
+				if domain == info.jid and arg_name_orig == info.name then
+					api_key_store:set(nil, id, {
+						id = id;
+						token = info.token;
+						name = arg_name_new;
+						jid = domain;
+						created_at = os_time;
+						allowed_methods = { GET = true, POST = true };
+					});
+					date = datetime.datetime(os_time);
+					print("Re-named:");
+					print(date, id.."/"..info.token, info.jid, arg_name_new);
+					found = true;
+				end
+			end);
+			if not found then
+				date = datetime.datetime(os_time);
+				print("Error: Could not find "..arg_name_orig.." in "..domain);
+				print("");
+				print("To make this API key, run:");
+				print("prosodyctl "..module.name.." create "..domain.." "..arg_name_orig);
+				return 1;
+			end
+			return;
+		end
 	elseif command == "renew" then
 		local util_token = require "util.id".long();
 		local os_time = os.time();
@@ -406,7 +485,7 @@
 					print("");
 					print("To make this API key, run:");
 					print("prosodyctl "..module.name.." create "..username.."@"..domain);
-					return;
+					return 1;
 				end
 				return;
 			else
@@ -442,7 +521,7 @@
 				print("");
 				print("To make this API key, run:");
 				print("prosodyctl "..module.name.." create "..domain.." "..arg_name);
-				return;
+				return 1;
 			end
 			return;
 		end