Sat, 19 Sep 2015 21:44:40 +0100
client: Don't set log handler, let main script initialize verse now
8
42b4e73c0d30
client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
1 | local async = require "scansion.async"; |
4 | 2 | local verse = require "verse".init("client"); |
1
017c5809d537
client: Enable verse log output
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
3 | |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local parse_xml = require "scansion.xml".parse; |
6
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
5 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
6 | local stanza_timeout = 5; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
7 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
8 | local stanzacmp = require "scansion.stanzacmp"; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
9 | |
16
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
10 | local function fill_vars(script, stanza) |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
11 | for k, v in pairs(stanza.attr) do |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
12 | print("attr", k) |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
13 | local expr = v:match("^%$%{(.+)%}$"); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
14 | if expr then |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
15 | local name, value_name = expr:match("^(.-)'s (.+)$"); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
16 | assert(name, "Unable to parse expression: "..expr); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
17 | print("BBB") |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
18 | local key = value_name:lower():gsub(" ", "_"); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
19 | assert(script.objects[name], "Unknown object called "..name); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
20 | local value = script.objects[name][key]; |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
21 | assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name); |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
22 | stanza.attr[k] = value; |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
23 | end |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
24 | end |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
25 | return stanza; |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
26 | end |
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
27 | |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | return { |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | _validate = function (client) |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | assert(client.jid, "No JID specified"); |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | client.stream = verse.new(); |
10 | 32 | client.stream:hook("stanza", function (stanza) print("Stanza:", stanza) end); |
33 | ||
34 | -- This one prints all received data | |
35 | client.stream:hook("incoming-raw", print, 1000); | |
36 | client.stream:hook("outgoing-raw", print, 1000); | |
37 | ||
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | end; |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | connects = function (client) |
8
42b4e73c0d30
client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
41 | local wait, done = async.waiter(); |
42b4e73c0d30
client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
42 | client.stream:hook("ready", function () print"aha" done() end); |
4 | 43 | client.stream:connect_client(client.jid, client.password); |
8
42b4e73c0d30
client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
44 | wait(); |
15
0441673df075
client: Add full JID as property of clients
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
45 | client.full_jid = client.stream.jid; |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | end; |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | sends = function (client, data) |
16
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
49 | local stanza = fill_vars(client.script, parse_xml(table.concat(data))); |
6
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
50 | client.stream:send(stanza); |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | end; |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | receives = function (client, data) |
16
59f176aa3465
client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
54 | local expected_stanza = fill_vars(client.script, parse_xml(table.concat(data))); |
6
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
55 | local function stanza_handler(received_stanza) |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
56 | if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
57 | print("NOT IT!") |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
58 | verse.quit(); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
59 | else |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
60 | print("YES!") |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
61 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
62 | expected_stanza = nil; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
63 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
64 | client.stream:hook("stanza", stanza_handler, 100); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
65 | verse.add_task(stanza_timeout, function () |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
66 | if not expected_stanza then return; end |
17
610917f3ea97
client: Some logging changes
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
67 | print("TIMEOUT") |
6
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
68 | end); |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | end; |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | disconnects = function (client) |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | end; |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | } |