Software /
code /
prosody-modules
Comparison
mod_inject_ecaps2/mod_insert_ecaps2.lua @ 2907:776017c92076
mod_inject_ecaps2: New module adding support for XEP-0390 to all local clients supporting XEP-0115
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 07 Mar 2018 19:30:42 +0100 |
child | 2908:5665d14dcc6e |
comparison
equal
deleted
inserted
replaced
2906:d9603b555be2 | 2907:776017c92076 |
---|---|
1 module:depends("cache_c2s_caps"); | |
2 | |
3 local st = require "util.stanza"; | |
4 local hashes = require "util.hashes"; | |
5 local base64 = require "util.encodings".base64; | |
6 local t_insert, t_sort, t_concat = table.insert, table.sort, table.concat; | |
7 | |
8 local algorithms = module:get_option_set("ecaps2_hashes", { "sha-256", "sha-512" }); | |
9 | |
10 -- TODO: Add all of the other hashes supported. | |
11 local algorithm_map = { | |
12 ["sha-256"] = hashes.sha256; | |
13 ["sha-512"] = hashes.sha512; | |
14 }; | |
15 | |
16 -- TODO: move that to util.caps maybe. | |
17 local function calculate_hash(disco_info) | |
18 local identities, features, extensions = {}, {}, {}; | |
19 for _, tag in ipairs(disco_info) do | |
20 if tag.name == "identity" then | |
21 t_insert(identities, ((tag.attr.category or "").."\x1f".. | |
22 (tag.attr.type or "").."\x1f".. | |
23 (tag.attr["xml:lang"] or "").."\x1f".. | |
24 (tag.attr.name or "").."\x1f\x1e")); | |
25 elseif tag.name == "feature" then | |
26 t_insert(features, (tag.attr.var or "").."\x1f"); | |
27 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then | |
28 local form = {}; | |
29 for _, field in ipairs(tag.tags) do | |
30 if field.name == "field" and field.attr.xmlns == "jabber:x:data" and field.attr.var then | |
31 local values = {}; | |
32 for _, value in ipairs(field.tags) do | |
33 if value.name == "value" and value.attr.xmlns == "jabber:x:data" then | |
34 value = #value.tags == 0 and value:get_text(); | |
35 if value then t_insert(values, value.."\x1f"); end | |
36 end | |
37 end | |
38 t_sort(values); | |
39 if #values > 0 then | |
40 t_insert(form, field.attr.var.."\x1f"..t_concat(values, "\x1f").."\x1f\x1e"); | |
41 else | |
42 t_insert(form, field.attr.var.."\x1f\x1e"); | |
43 end | |
44 end | |
45 end | |
46 t_sort(form); | |
47 form = t_concat(form, "\x1d").."\x1d"; | |
48 t_insert(extensions, form); | |
49 else | |
50 return nil, "Unknown element in disco#info"; | |
51 end | |
52 end | |
53 t_sort(identities); | |
54 t_sort(features); | |
55 t_sort(extensions); | |
56 if #identities > 0 then identities = t_concat(identities, "\x1c").."\x1c"; else identities = "\x1c"; end | |
57 if #features > 0 then features = t_concat(features).."\x1c"; else features = "\x1c"; end | |
58 if #extensions > 0 then extensions = t_concat(extensions, "\x1c").."\x1c"; else extensions = "\x1c"; end | |
59 return features..identities..extensions; | |
60 end | |
61 | |
62 local function caps_handler(event) | |
63 local origin = event.origin; | |
64 | |
65 if origin.presence:get_child("c", "urn:xmpp:caps") then | |
66 return; | |
67 end | |
68 | |
69 local disco_info = origin.caps_cache; | |
70 if disco_info == nil then | |
71 return; | |
72 end | |
73 | |
74 local extension_string = calculate_hash(disco_info); | |
75 | |
76 local ecaps2 = st.stanza("c", { xmlns = "urn:xmpp:caps" }); | |
77 for algo in algorithms do | |
78 local func = algorithm_map[algo]; | |
79 if func ~= nil then | |
80 local hash = base64.encode(func(extension_string)); | |
81 ecaps2:tag("hash", { xmlns = "urn:xmpp:hashes:2"; algo = algo }) | |
82 :text(hash) | |
83 :up(); | |
84 end | |
85 end | |
86 | |
87 module:log("debug", "Injected ecaps2 element in presence"); | |
88 origin.presence:add_child(hashes); | |
89 end | |
90 | |
91 module:hook("c2s-capabilities-changed", caps_handler); |