Diff

mod_lastlog2/mod_lastlog2.lua @ 6208:e20901443eae draft

Merge
author Trần H. Trung <xmpp:trần.h.trung@trung.fun>
date Mon, 17 Mar 2025 23:42:11 +0700
parent 5900:c5df6d53f17f
child 6211:750d64c47ec6
line wrap: on
line diff
--- a/mod_lastlog2/mod_lastlog2.lua	Wed Feb 26 19:36:35 2025 +0700
+++ b/mod_lastlog2/mod_lastlog2.lua	Mon Mar 17 23:42:11 2025 +0700
@@ -47,7 +47,7 @@
 	end);
 end
 
-do
+if module.host ~= "*" then
 	local user_sessions = prosody.hosts[module.host].sessions;
 	local kv_store = module:open_store();
 	function get_last_active(username) --luacheck: ignore 131/get_last_active
@@ -67,6 +67,31 @@
 	end
 end
 
+module:add_item("shell-command", {
+	section = "lastlog";
+	section_desc = "View and manage user activity data";
+	name = "show";
+	desc = "View recorded user activity for user";
+	args = { { name = "jid"; type = "string" } };
+	host_selector = "jid";
+	handler = function(self, userjid)
+		local kv_store = module:open_store();
+		local username = jid.prepped_split(userjid);
+		local lastlog, err = kv_store:get(username);
+		if err then return false, err; end
+		if not lastlog then return true, "No record found"; end
+		local print = self.session.print;
+		for event, data in pairs(lastlog) do
+			print(("Last %s: %s"):format(event,
+				data.timestamp and os.date("%Y-%m-%d %H:%M:%S", data.timestamp) or "<unknown>"));
+			if data.ip then
+				print("IP address: "..data.ip);
+			end
+		end
+		return true, "Record shown"
+	end;
+});
+
 function module.command(arg)
 	if not arg[1] or arg[1] == "--help" then
 		require"util.prosodyctl".show_usage([[mod_lastlog2 <user@host>]], [[Show when user last logged in or out]]);