Changeset

13668:6be7de547a25 13.0

mod_account_activity: Add shell command to list inactive accounts Including accounts which may never have logged in.
author Matthew Wild <mwild1@gmail.com>
date Wed, 12 Feb 2025 17:15:29 +0000
parents 13667:3052eae50c98
children 13669:4ac30e46a4fb
files plugins/mod_account_activity.lua
diffstat 1 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_account_activity.lua	Wed Feb 12 17:15:00 2025 +0000
+++ b/plugins/mod_account_activity.lua	Wed Feb 12 17:15:29 2025 +0000
@@ -47,6 +47,49 @@
 });
 
 module:add_item("shell-command", {
+	section = "user";
+	section_desc = "View user activity data";
+	name = "list_inactive";
+	desc = "List inactive user accounts";
+	args = {
+		{ name = "host"; type = "string" };
+		{ name = "duration"; type = "string" };
+	};
+	host_selector = "host";
+	handler = function(self, host, duration) --luacheck: ignore 212/self
+		local um = require "prosody.core.usermanager";
+		local duration_sec = require "prosody.util.human.io".parse_duration(duration);
+		if not duration_sec then
+			return false, ("Invalid duration %q - try something like \"30d\""):format(duration);
+		end
+
+		local now = os.time();
+		local n_inactive, n_unknown = 0, 0;
+
+		for username in um.users(host) do
+			local last_active = store:get_key(username, "timestamp");
+			if not last_active then
+				local created_at = um.get_account_info(username, host).created;
+				if created_at and (now - created_at) > duration_sec then
+					self.session.print(username, "");
+					n_inactive = n_inactive + 1;
+				elseif not created_at then
+					n_unknown = n_unknown + 1;
+				end
+			elseif (now - last_active) > duration_sec then
+				self.session.print(username, os.date("%Y-%m-%dT%T", last_active));
+				n_inactive = n_inactive + 1;
+			end
+		end
+
+		if n_unknown > 0 then
+			return true, ("%d accounts inactive since %s (%d unknown)"):format(n_inactive, os.date("%Y-%m-%dT%T", now - duration_sec), n_unknown);
+		end
+		return true, ("%d accounts inactive since %s"):format(n_inactive, os.date("%Y-%m-%dT%T", now - duration_sec));
+	end;
+});
+
+module:add_item("shell-command", {
 	section = "migrate";
 	section_desc = "Perform data migrations";
 	name = "account_activity_lastlog2";