From 85b82fdb3d204411e712a6819e3002d84a68abe2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 12 Feb 2025 17:15:29 +0000 Subject: mod_account_activity: Add shell command to list inactive accounts Including accounts which may never have logged in. --- plugins/mod_account_activity.lua | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/plugins/mod_account_activity.lua b/plugins/mod_account_activity.lua index 829254bd..63a7fb39 100644 --- a/plugins/mod_account_activity.lua +++ b/plugins/mod_account_activity.lua @@ -46,6 +46,49 @@ module:add_item("shell-command", { end; }); +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"; -- cgit v1.2.3