scansion/objects/client.lua

changeset 171
433a1f36d0d3
parent 164
14500a149b31
child 172
2c17151ed21b
equal deleted inserted replaced
170:db73c4c317ce 171:433a1f36d0d3
6 6
7 local default_stanza_timeout = 3; 7 local default_stanza_timeout = 3;
8 8
9 local stanzacmp = require "scansion.stanzacmp"; 9 local stanzacmp = require "scansion.stanzacmp";
10 10
11 local function filter_expression(script, s) 11 local helpers = require "scansion.helpers";
12 local expr = s:match("^%$%{(.+)%}$");
13 if not expr then return s end
14 local name, value_name = expr:match("^(.+)'s (.+)$");
15 assert(name, "Unable to parse expression: "..expr);
16 local key = value_name:lower():gsub(" ", "_");
17 assert(script.objects[name], "Unknown object called "..name);
18 local value = script.objects[name][key];
19 assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name);
20 return value;
21 end
22
23 local function fill_vars(script, stanza)
24 for k, v in pairs(stanza.attr) do
25 stanza.attr[k] = filter_expression(script, v);
26 end
27 for i, child in ipairs(stanza) do
28 if type(child) == "string" then
29 stanza[i] = filter_expression(script, child);
30 elseif type(child) == "table" then
31 fill_vars(script, child);
32 end
33 end
34 return stanza;
35 end
36 12
37 return { 13 return {
38 _validate = function (client) 14 _validate = function (client)
39 assert(client.jid, "No JID specified"); 15 assert(client.jid, "No JID specified");
40 client.stream = verse.new(verse.new_logger(client.name)); 16 client.stream = verse.new(verse.new_logger(client.name));
79 client.full_jid = client.stream.jid; 55 client.full_jid = client.stream.jid;
80 client.host = client.stream.host; 56 client.host = client.stream.host;
81 end; 57 end;
82 58
83 sends = function (client, data) 59 sends = function (client, data)
84 local stanza = fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " "))))); 60 local stanza = helpers.fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " ")))));
85 local wait, done = async.waiter(); 61 local wait, done = async.waiter();
86 local function handle_drained() 62 local function handle_drained()
87 client.stream:unhook("drained", handle_drained); 63 client.stream:unhook("drained", handle_drained);
88 done(); 64 done();
89 end 65 end
96 local wait, done = async.waiter(); 72 local wait, done = async.waiter();
97 local expected_stanza = false; 73 local expected_stanza = false;
98 local have_received_stanza = false; 74 local have_received_stanza = false;
99 data = table.concat(data):gsub("\t", " "):gsub("^%s+", ""):gsub("%s+$", ""); 75 data = table.concat(data):gsub("\t", " "):gsub("^%s+", ""):gsub("%s+$", "");
100 if data ~= "nothing" then 76 if data ~= "nothing" then
101 expected_stanza = fill_vars(client.script, assert(parse_xml(data))); 77 expected_stanza = helpers.fill_vars(client.script, assert(parse_xml(data)));
102 end 78 end
103 local function stanza_handler(received_stanza) 79 local function stanza_handler(received_stanza)
104 have_received_stanza = true; 80 have_received_stanza = true;
105 if not expected_stanza then 81 if not expected_stanza then
106 error(new_error("unexpected-stanza", { 82 error(new_error("unexpected-stanza", {

mercurial