diff mod_audit_register/mod_audit_register.lua @ 5744:b4607c5dfcac

mod_audit_register: Support for deregister and enable/disable events
author Matthew Wild <mwild1@gmail.com>
date Thu, 30 Nov 2023 17:51:27 +0000
parents dde83f6043e6
children
line wrap: on
line diff
--- a/mod_audit_register/mod_audit_register.lua	Wed Nov 29 12:14:49 2023 +0000
+++ b/mod_audit_register/mod_audit_register.lua	Thu Nov 30 17:51:27 2023 +0000
@@ -1,24 +1,53 @@
 module:depends("audit");
 -- luacheck: read globals module.audit
 
+local dt = require "util.datetime";
 local jid = require "util.jid";
 local st = require "util.stanza";
 
-module:hook("user-registered", function(event)
-	local session = event.session;
-	local custom = {};
+local function audit_basic_event(name, custom_handler)
+	module:hook(name, function (event)
+		local custom;
+		if custom_handler then
+			custom = custom_handler(event);
+		end
+		module:audit(jid.join(event.username, module.host), name, {
+			session = event.session;
+			custom = custom;
+		});
+	end);
+end
+
+audit_basic_event("user-registered", function (event)
 	local invite = event.validated_invite or (event.session and event.session.validated_invite);
-	if invite then
-		table.insert(custom, st.stanza(
+	if not invite then return; end
+	return {
+		st.stanza(
 			"invite-used",
 			{
 				xmlns = "xmpp:prosody.im/audit",
 				token = invite.token,
 			}
-		))
+		);
+	};
+end);
+
+audit_basic_event("user-deregistered-pending");
+audit_basic_event("user-deregistered");
+
+audit_basic_event("user-enabled");
+audit_basic_event("user-disabled", function (event)
+	local meta = event.meta;
+	if not meta then return end
+
+	local meta_st = st.stanza("disabled", {
+		xmlns = "xmpp:prosody.im/audit";
+		reason = meta.reason;
+		when = meta.when and dt.datetime(meta.when) or nil;
+	});
+	if meta.comment then
+		meta_st:text_tag("comment", meta.comment);
 	end
-	module:audit(jid.join(event.username, module.host), "user-registered", {
-		session = session,
-		custom = custom,
-	});
+
+	return { meta_st };
 end);