Diff

mod_mam/rsm.lib.lua @ 701:cc5805f83583

mod_mam: Implement support for Result Set Management in queries.
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jun 2012 03:13:31 +0200
child 702:d94ee0848b27
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_mam/rsm.lib.lua	Fri Jun 08 03:13:31 2012 +0200
@@ -0,0 +1,82 @@
+local stanza = require"util.stanza".stanza;
+local tostring, tonumber = tostring, tonumber;
+local type = type;
+local pairs = pairs;
+
+local xmlns_rsm = 'http://jabber.org/protocol/rsm';
+
+local element_parsers;
+
+do
+	local function xs_int(st)
+		return tonumber(st:get_text());
+	end
+	local function xs_string(st)
+		return st:get_text();
+	end
+
+	element_parsers = {
+		after = xs_string;
+		before = function(st)
+			return st:get_text() or true;
+		end;
+		max = xs_int;
+		index = xs_int;
+
+		first = function(st)
+			return { index = tonumber(st.attr.index); st:get_text() };
+		end;
+		last = xs_string;
+		count = xs_int;
+	}
+end
+
+local element_generators = setmetatable({
+	first = function(st, data)
+		if type(data) == "table" then
+			st:tag("first", { index = data.index }):text(data[1]):up();
+		else
+			st:tag("first"):text(tostring(data)):up();
+		end
+	end;
+}, {
+	__index = function(_, name)
+		return function(st, data)
+			st:tag(name):text(tostring(data)):up();
+		end
+	end;
+});
+
+
+local function parse(stanza)
+	local rs = {};
+	for tag in stanza:childtags() do
+		local name = tag.name;
+		local parser = name and element_parsers[name];
+		if parser then
+			rs[name] = parser(tag);
+		end
+	end
+	return rs;
+end
+
+local function generate(t)
+	local st = stanza("rsm", { xmlns = xmlns_rsm });
+	for k,v in pairs(t) do
+		if element_parsers[k] then
+			element_generators[k](st, v);
+		end
+	end
+	return st;
+end
+
+local function get(st)
+	local set = st:get_child("set", xmlns_rsm);
+	if set and #set.tags > 0 then
+		return parse(set);
+	else
+		module:log("debug", "RSM parse failed, %s", tostring(st));
+	end
+end
+
+return { parse = parse, generate = generate, get = get };