Sat, 05 Sep 2015 23:24:15 +0100
client: Implement send/receive, including new stanzacmp library
6
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | -- This is my attempt at a utility library to compare two XMPP stanzas |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | -- It is not testing for exact equivalency, but through some vague rules that felt right. |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | -- For example, the second stanza passed to stanzas_match() is allowed to have unexpected |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | -- elements and attributes at the top level. Beyond this, they must match exactly, except |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | -- for whitespace differences only. |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | -- |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | -- There are probably bugs, and it can probably be smarter, but I don't want to spend too |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | -- much time on it right now. |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local function trim(s) |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | return (s:gsub("^%s+", ""):gsub("%s+$", "")); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | local function stanzas_strict_match(stanza1, stanza2) |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | if stanza1.name ~= stanza2.name or stanza1.attr.xmlns ~= stanza2.attr.xmlns then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | for k, v in pairs(stanza1.attr) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | if stanza2.attr[k] ~= v then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | for k, v in pairs(stanza2.attr) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | if stanza1.attr[k] ~= v then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | if #stanza1.tags ~= #stanza2.tags then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | local stanza2_pos = 1; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | for _, child in ipairs(stanza1) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | if type(child) == "table" or child:match("%S") then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | local match; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | local child2 = stanza2[stanza2_pos]; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | while child2 and not(type(child2) == "table" or child:match("%S")) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | stanza2_pos = stanza2_pos + 1; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | child2 = stanza2[stanza2_pos]; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | if type(child) ~= type(child2) then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | -- Strict deep match |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | match = stanzas_strict_match(child, child2); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | match = trim(child) == trim(child2); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | if not match then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | return true; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | -- Everything in stanza1 should be present in stanza2 |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | local function stanzas_match(stanza1, stanza2) |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | if stanza1.name ~= stanza2.name or stanza1.attr.xmlns ~= stanza2.attr.xmlns then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | for k, v in pairs(stanza1.attr) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | if stanza2.attr[k] ~= v then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | local stanza2_pos = 1; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | for _, child in ipairs(stanza1) do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | if type(child) == "table" or child:match("%S") then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | local match; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | -- Iterate through remaining nodes in stanza2, looking for a match |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | while stanza2_pos <= #stanza2 do |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | local child2 = stanza2[stanza2_pos]; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | stanza2_pos = stanza2_pos + 1; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | if type(child2) == type(child) then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | -- Strict deep match |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | match = stanzas_strict_match(child, child2); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | match = trim(child) == trim(child2); |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | break; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | if not match then |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | return false; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | return true; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | end |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | return { |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | stanzas_match = stanzas_match; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | stanzas_strict_match = stanzas_strict_match; |
0c94ea0cabec
client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | }; |