comparison mod_export_skeletons/skeleton_filter.lua @ 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 mod_export_skeletons/mod_export_skeletons.lua@17fbe82d4bfe
children
comparison
equal deleted inserted replaced
4821:17fbe82d4bfe 4822:b4cc6ee9fc8c
1 #!/usr/bin/env lua
2
3 package.path = package.path:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2");
4 package.cpath = package.cpath:gsub("([^;]*)(?[^;]*)", "%1prosody/%2;%1%2");
5
6 local t_insert = table.insert;
7 local t_sort = table.sort;
8
9 local jid = require "util.jid";
10 local st = require "util.stanza";
11
12 local xs = require "util.xmppstream";
13
14 local function skeleton(s)
15 local o = st.stanza(s.name, { xmlns = s.attr.xmlns });
16
17 local children = {};
18 for _, child in ipairs(s.tags) do t_insert(children, skeleton(child)) end
19 t_sort(children, function(a, b)
20 if a.attr.xmlns == b.attr.xmlns then return a.name < b.name; end
21 return (a.attr.xmlns or "") < (b.attr.xmlns or "");
22 end);
23 for _, child in ipairs(children) do o:add_direct_child(child); end
24 return o;
25 end
26
27 local function classify_jid(s)
28 if not s then return "" end
29 local u, h, r = jid.split(s);
30 if r then
31 return "full"
32 elseif u then
33 return "bare"
34 elseif h then
35 return "host"
36 else
37 return "invalid"
38 end
39 end
40
41 local stream_session = { notopen = true };
42 local stream_callbacks = { stream_ns = "jabber:client"; default_ns = "jabber:client" };
43 function stream_callbacks:handlestanza(item)
44 local clean = skeleton(item);
45
46 -- Normalize top level attributes
47 clean.attr.type = item.attr.type;
48 if clean.attr.type == nil and clean.name == "message" then clean.attr.type = "normal"; end
49 clean.attr.id = string.rep("x", math.floor(math.log(1 + #(item.attr.id or ""), 2)));
50 clean.attr.from = classify_jid(item.attr.from);
51 clean.attr.to = classify_jid(item.attr.to);
52 print(clean);
53 end
54 local stream = xs.new(stream_session, stream_callbacks);
55 assert(stream:feed(st.stanza("stream", { xmlns = "jabber:client" }):top_tag()));
56 stream_session.notopen = nil;
57
58 local data = io.read(4096);
59 while data do
60 stream:feed(data);
61 data = io.read(4096);
62 end
63
64 assert(stream:feed("</stream>"));