Annotate

mod_cache_c2s_caps/mod_cache_c2s_caps.lua @ 2899:0273d7583373

mod_cache_c2s_caps: New module caching capabilities from local clients
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 07 Mar 2018 17:11:12 +0100
child 2901:0fb44258d2cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2899
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 local st_iq = require "util.stanza".iq;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 local jid_split = require "util.jid".split;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 local uuid_gen = require "util.uuid".generate;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 local calculate_hash = require "util.caps".calculate_hash;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 -- Map of jid..node, to avoid querying the same client multiple times for the same value.
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 local in_flight_iqs = {}
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 -- Some clients (*ahem* poezio…) don’t include the @node in their result iq.
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 local iq_node_map = {}
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 local function iq_result_handler(event)
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 local origin, stanza = event.origin, event.stanza;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 local from = stanza.attr.from;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 local id = stanza.attr.id;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 local query = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 local node_string = query.attr.node;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 local node_query = iq_node_map[from..id];
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 if node_string == nil then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 node_string = node_query;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 query.attr.node = node_query;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 iq_node_map[from..id] = nil;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 in_flight_iqs[from..node_string] = nil;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 if node_string ~= node_query then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 module:log("debug", "Wrong node for our disco#info query, expected %s, received %s", node_string, node_query);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 local node, ver = node_query:match("([^#]+)#([^#]+)");
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 local hash = calculate_hash(query)
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 if ver ~= hash then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 module:log("debug", "Wrong hash for disco#info: %s ~= %s", ver, hash);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 origin.caps_cache = query;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 module:log("info", "Stored caps %s", ver);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 module:fire_event("c2s-capabilities-changed", { origin = origin });
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 return true;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 local function iq_error_handler(event)
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 local origin = event.origin;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 origin.caps_cache = nil;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 module:fire_event("c2s-capabilities-changed", { origin = origin });
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 local function presence_stanza_handler(event)
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 local origin, stanza = event.origin, event.stanza;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 local from = stanza.attr.from;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 if stanza.attr.to ~= nil then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 local caps = stanza:get_child("c", "http://jabber.org/protocol/caps");
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 if caps == nil then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 module:log("debug", "Presence without caps received, skipping");
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 local hash = caps.attr.hash;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 local node = caps.attr.node;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 local ver = caps.attr.ver;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 if not hash or not node or not ver then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 if hash ~= "sha-1" then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 module:log("warn", "Non-SHA-1 caps received: %s", hash);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 local node_query = node.."#"..ver;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 if (origin.caps_cache and origin.caps_cache.attr.node == node_query) or in_flight_iqs[from..node_query] ~= nil then
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 module:log("debug", "Already requested these caps, skipping");
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 return;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 module:log("debug", "Received presence with SHA-1 caps %s, querying disco#info", node_query);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 local id = uuid_gen();
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 iq_node_map[from..id] = node_query
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 local iq = st_iq({ type = "get", from = module.host, to = from, id = id })
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node_query });
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 module:hook("iq-result/host/"..id, iq_result_handler);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 module:hook("iq-error/host/"..id, iq_error_handler);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 module:send(iq);
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 in_flight_iqs[from..node_query] = true;
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 end
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 -- Handle only non-directed presences for now.
0273d7583373 mod_cache_c2s_caps: New module caching capabilities from local clients
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 module:hook("pre-presence/bare", presence_stanza_handler);