# HG changeset patch # User Matthew Wild # Date 1739380529 0 # Node ID 6be7de547a25c19362b6002a2bf6b971e8799bb4 # Parent 3052eae50c9897ce373c9dd9281c62073fcc6efc mod_account_activity: Add shell command to list inactive accounts Including accounts which may never have logged in. diff -r 3052eae50c98 -r 6be7de547a25 plugins/mod_account_activity.lua --- 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";