Changeset

12464:f4c59af273a3

mod_admin_shell: Add watch:stanzas() command
author Matthew Wild <mwild1@gmail.com>
date Wed, 23 Mar 2022 13:43:08 +0000
parents 12463:788048158982
children 12465:4a087713cffe
files plugins/mod_admin_shell.lua
diffstat 1 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_admin_shell.lua	Wed Mar 23 13:42:44 2022 +0000
+++ b/plugins/mod_admin_shell.lua	Wed Mar 23 13:43:08 2022 +0000
@@ -1609,6 +1609,40 @@
 	end
 end
 
+local stanza_watchers = module:require("mod_debug_stanzas/watcher");
+function def_env.watch:stanzas(target_spec, filter_spec)
+	local function handler(event_type, stanza, session)
+		if stanza then
+			if event_type == "sent" then
+				self.session.print(("\n<!-- sent to %s -->"):format(session.id));
+			elseif event_type == "received" then
+				self.session.print(("\n<!-- received from %s -->"):format(session.id));
+			else
+				self.session.print(("\n<!-- %s (%s) -->"):format(event_type, session.id));
+			end
+			self.session.print(stanza);
+		elseif session then
+			self.session.print("\n<!-- session "..session.id.." "..event_type.." -->");
+		elseif event_type then
+			self.session.print("\n<!-- "..event_type.." -->");
+		end
+	end
+
+	stanza_watchers.add({
+		target_spec = {
+			jid = target_spec;
+		};
+		filter_spec = filter_spec and {
+			with_jid = filter_spec;
+		};
+	}, handler);
+
+	while self.session.is_connected() do
+		async.sleep(3);
+	end
+
+	stanza_watchers.remove(handler);
+end
 
 def_env.debug = {};
 
@@ -1953,6 +1987,10 @@
 end
 
 
+function module.unload()
+	stanza_watchers.cleanup();
+end
+
 
 -------------