Changeset

4822:b4cc6ee9fc8c

mod_export_skeletons: Add a standalone filter script Full XML goes in. Skeletons come out.
author Kim Alvefur <zash@zash.se>
date Fri, 10 Dec 2021 16:36:56 +0100
parents 4821:17fbe82d4bfe
children 4823:d3e926bf0c8a
files mod_export_skeletons/skeleton_filter.lua
diffstat 1 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_export_skeletons/skeleton_filter.lua	Fri Dec 10 16:36:56 2021 +0100
@@ -0,0 +1,64 @@
+#!/usr/bin/env lua
+
+package.path = package.path:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2");
+package.cpath = package.cpath:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2");
+
+local t_insert = table.insert;
+local t_sort = table.sort;
+
+local jid = require "util.jid";
+local st = require "util.stanza";
+
+local xs = require "util.xmppstream";
+
+local function skeleton(s)
+	local o = st.stanza(s.name, { xmlns = s.attr.xmlns });
+
+	local children = {};
+	for _, child in ipairs(s.tags) do t_insert(children, skeleton(child)) end
+	t_sort(children, function(a, b)
+		if a.attr.xmlns == b.attr.xmlns then return a.name < b.name; end
+		return (a.attr.xmlns or "") < (b.attr.xmlns or "");
+	end);
+	for _, child in ipairs(children) do o:add_direct_child(child); end
+	return o;
+end
+
+local function classify_jid(s)
+	if not s then return "" end
+	local u, h, r = jid.split(s);
+	if r then
+		return "full"
+	elseif u then
+		return "bare"
+	elseif h then
+		return "host"
+	else
+		return "invalid"
+	end
+end
+
+local stream_session = { notopen = true };
+local stream_callbacks = { stream_ns = "jabber:client"; default_ns = "jabber:client" };
+function stream_callbacks:handlestanza(item)
+	local clean = skeleton(item);
+
+	-- Normalize top level attributes
+	clean.attr.type = item.attr.type;
+	if clean.attr.type == nil and clean.name == "message" then clean.attr.type = "normal"; end
+	clean.attr.id = string.rep("x", math.floor(math.log(1 + #(item.attr.id or ""), 2)));
+	clean.attr.from = classify_jid(item.attr.from);
+	clean.attr.to = classify_jid(item.attr.to);
+	print(clean);
+end
+local stream = xs.new(stream_session, stream_callbacks);
+assert(stream:feed(st.stanza("stream", { xmlns = "jabber:client" }):top_tag()));
+stream_session.notopen = nil;
+
+local data = io.read(4096);
+while data do
+	stream:feed(data);
+	data = io.read(4096);
+end
+
+assert(stream:feed("</stream>"));