scansion/objects/client.lua

changeset 171
433a1f36d0d3
parent 164
14500a149b31
child 172
2c17151ed21b
--- a/scansion/objects/client.lua	Thu Mar 23 11:43:25 2023 +0000
+++ b/scansion/objects/client.lua	Thu Mar 23 11:51:31 2023 +0000
@@ -8,31 +8,7 @@
 
 local stanzacmp = require "scansion.stanzacmp";
 
-local function filter_expression(script, s)
-	local expr = s:match("^%$%{(.+)%}$");
-	if not expr then return s end
-	local name, value_name = expr:match("^(.+)'s (.+)$");
-	assert(name, "Unable to parse expression: "..expr);
-	local key = value_name:lower():gsub(" ", "_");
-	assert(script.objects[name], "Unknown object called "..name);
-	local value = script.objects[name][key];
-	assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name);
-	return value;
-end
-
-local function fill_vars(script, stanza)
-	for k, v in pairs(stanza.attr) do
-		stanza.attr[k] = filter_expression(script, v);
-	end
-	for i, child in ipairs(stanza) do
-		if type(child) == "string" then
-			stanza[i] = filter_expression(script, child);
-		elseif type(child) == "table" then
-			fill_vars(script, child);
-		end
-	end
-	return stanza;
-end
+local helpers = require "scansion.helpers";
 
 return {
 	_validate = function (client)
@@ -81,7 +57,7 @@
 	end;
 
 	sends = function (client, data)
-		local stanza =  fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", "  ")))));
+		local stanza =  helpers.fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", "  ")))));
 		local wait, done = async.waiter();
 		local function handle_drained()
 			client.stream:unhook("drained", handle_drained);
@@ -98,7 +74,7 @@
 		local have_received_stanza = false;
 		data = table.concat(data):gsub("\t", "    "):gsub("^%s+", ""):gsub("%s+$", "");
 		if data ~= "nothing" then
-			expected_stanza = fill_vars(client.script, assert(parse_xml(data)));
+			expected_stanza = helpers.fill_vars(client.script, assert(parse_xml(data)));
 		end
 		local function stanza_handler(received_stanza)
 			have_received_stanza = true;

mercurial