Changeset

387:508bf163502b

Import util.rsm from prosody-modules
author Kim Alvefur <zash@zash.se>
date Wed, 19 Aug 2015 16:41:47 +0200
parents 386:7b68c57ceac2
children 388:d963c8a5d89c
files squishy util/rsm.lua
diffstat 2 files changed, 88 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/squishy	Wed Aug 19 14:41:24 2015 +0200
+++ b/squishy	Wed Aug 19 16:41:47 2015 +0200
@@ -6,8 +6,6 @@
 Module "util.sha1"		"util/sha1.lua"
 Module "lib.adhoc"              "libs/adhoc.lib.lua"
 
-AutoFetchURL("http://hg.prosody.im/prosody-modules/raw-file/tip/mod_mam/?");
-Module "util.rsm"       "rsm.lib.lua"
 -- Prosody libraries
 if not GetOption("prosody") then
 	AutoFetchURL "http://hg.prosody.im/0.9/raw-file/tip/?"
@@ -32,6 +30,7 @@
 Module "util.datetime"		"util/datetime.lua"
 Module "util.json"		"util/json.lua"
 Module "util.xml"		"util/xml.lua"
+Module "util.rsm"       "util/rsm.lua"
 
 Module "util.sasl.scram" "util/sasl/scram.lua"
 Module "util.sasl.plain" "util/sasl/plain.lua"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/rsm.lua	Wed Aug 19 16:41:47 2015 +0200
@@ -0,0 +1,87 @@
+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 parsers = element_parsers;
+	local function xs_int(st)
+		return tonumber((st:get_text()));
+	end
+	local function xs_string(st)
+		return st:get_text();
+	end
+
+	parsers.after = xs_string;
+	parsers.before = function(st)
+			local text = st:get_text();
+			return text == "" or text;
+		end;
+	parsers.max = xs_int;
+	parsers.index = xs_int;
+
+	parsers.first = function(st)
+			return { index = tonumber(st.attr.index); st:get_text() };
+		end;
+	parsers.last = xs_string;
+	parsers.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;
+	before = function(st, data)
+		if data == true then
+			st:tag("before"):up();
+		else
+			st:tag("before"):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(set)
+	local rs = {};
+	for tag in set: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("set", { 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);
+	end
+end
+
+return { parse = parse, generate = generate, get = get };