scansion/objects/client.lua

Sat, 19 Sep 2015 21:45:23 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 19 Sep 2015 21:45:23 +0100
changeset 22
b1fa55d17057
parent 20
cd1fc52f1b26
child 23
af7a51d78b7b
permissions
-rw-r--r--

client: Remove old debug logging

local async = require "scansion.async";
local verse = require "verse".init("client");

local parse_xml = require "scansion.xml".parse;

local stanza_timeout = 5;

local stanzacmp = require "scansion.stanzacmp";

local function fill_vars(script, stanza)
	for k, v in pairs(stanza.attr) do
		local expr = v:match("^%$%{(.+)%}$");
		if expr then
			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);
			stanza.attr[k] = value;
		end
	end
	return stanza;
end

return {
	_validate = function (client)
		assert(client.jid, "No JID specified");
		client.stream = verse.new();
		client.stream:hook("stanza", function (stanza) print("Stanza:", stanza) end);

		-- This one prints all received data
		client.stream:hook("incoming-raw", print, 1000);
		client.stream:hook("outgoing-raw", print, 1000);

	end;

	connects = function (client)
		local wait, done = async.waiter();
		client.stream:hook("ready", function () print"aha" done() end);
		client.stream:connect_client(client.jid, client.password);
		wait();
		client.full_jid = client.stream.jid;
	end;

	sends = function (client, data)
		local stanza =  fill_vars(client.script, parse_xml(table.concat(data)));
		client.stream:send(stanza);
	end;

	receives = function (client, data)
		local expected_stanza = fill_vars(client.script, parse_xml(table.concat(data)));
		local function stanza_handler(received_stanza)
			if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
				print("NOT IT!")
				verse.quit();
			else
				print("YES!")
			end
			expected_stanza = nil;
		end
		client.stream:hook("stanza", stanza_handler, 100);
		verse.add_task(stanza_timeout, function ()
			if not expected_stanza then return; end
			print("TIMEOUT")
		end);
	end;

	disconnects = function (client)
	end;
}

mercurial