Mercurial > prosody-modules
comparison mod_inject_ecaps2/mod_inject_ecaps2.lua @ 2909:1d6bec35b752
mod_inject_ecaps2: Rename the Lua file correctly
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 07 Mar 2018 19:40:20 +0100 |
parents | mod_inject_ecaps2/mod_insert_ecaps2.lua@5665d14dcc6e |
children | 415455faccae |
comparison
equal
deleted
inserted
replaced
2908:5665d14dcc6e | 2909:1d6bec35b752 |
---|---|
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 "").."\31".. | |
22 (tag.attr.type or "").."\31".. | |
23 (tag.attr["xml:lang"] or "").."\31".. | |
24 (tag.attr.name or "").."\31\30")); | |
25 elseif tag.name == "feature" then | |
26 t_insert(features, (tag.attr.var or "").."\31"); | |
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.."\31"); end | |
36 end | |
37 end | |
38 t_sort(values); | |
39 if #values > 0 then | |
40 t_insert(form, field.attr.var.."\31"..t_concat(values, "\31").."\31\30"); | |
41 else | |
42 t_insert(form, field.attr.var.."\31\30"); | |
43 end | |
44 end | |
45 end | |
46 t_sort(form); | |
47 form = t_concat(form, "\29").."\29"; | |
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, "\28").."\28"; else identities = "\28"; end | |
57 if #features > 0 then features = t_concat(features).."\28"; else features = "\28"; end | |
58 if #extensions > 0 then extensions = t_concat(extensions, "\28").."\28"; else extensions = "\28"; 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); |