mod_mix_pam: Replace "state" tracking with promises

This commit is contained in:
PapaTutuWawa 2021-05-28 16:25:35 +02:00
parent 5f973c19d0
commit 3d66be6a9f

View File

@ -70,26 +70,6 @@ function module.load()
end
end
local client_state_tracker = {}; -- [stanza ID] -> resource
local function add_state(id, resource)
client_state_tracker[id] = resource;
module:log("debug", "Adding a resource %s for id %s", resource, id);
end
local function has_state(id)
return client_state_tracker[id] ~= nil;
end
local function pop_state(id)
module:log("debug", "Popping a resource for stanza id %s", id);
if has_state(id) then
local resource = client_state_tracker[id];
client_state_tracker[id] = nil;
return resource;
end
return nil;
end
local function handle_client_join(event)
-- Client requests to join
module:log("debug", "client-join received");
@ -115,9 +95,17 @@ local function handle_client_join(event)
});
join_iq:add_child(join);
add_state(stanza.attr.id, origin);
module:send(join_iq);
module:send_iq(join_iq)
:next(function(resp)
-- Success
handle_mix_join(resp, origin);
end, function(resp)
-- Error
-- TODO
local error_stanza = resp.stanza;
error_stanza.attr.to = origin.full_jid;
module:send(error_stanza);
end);
return true;
end
@ -146,9 +134,16 @@ local function handle_client_leave(event)
});
leave_iq:add_child(leave);
add_state(stanza.attr.id, origin);
module:send(leave_iq);
module:send_iq(leave_iq)
:next(function(resp)
handle_mix_leave(resp, origin);
end, function(resp)
-- Error
-- TODO
local error_stanza = resp.stanza;
error_stanza.attr.to = origin.full_jid;
module:send(error_stanza);
end);
return true;
end
@ -163,20 +158,14 @@ module:hook("iq/self", function(event)
end
end);
local function handle_mix_join(event)
local function handle_mix_join(event, origin)
-- The MIX server responded
module:log("debug", "Received MIX-JOIN result");
local stanza = event.stanza;
local spid = stanza:get_child("join", "urn:xmpp:mix:core:1").attr.id;
local channel_jid = spid.."#"..stanza.attr.from;
local origin = pop_state(stanza.attr.id);
local resource = origin.resource;
if resource == nil then
module:log("error", "Got a MIX join result for a not-requested id %s", stanza.attr.id);
module:log("error", "Maybe the server changed the stanza ID?");
return false;
end
local client_join = st.iq({
type = "result";
@ -200,18 +189,12 @@ local function handle_mix_join(event)
return true;
end
local function handle_mix_leave(event)
local function handle_mix_leave(event, origin)
-- The MIX server responded
module:log("debug", "Received MIX-LEAVE result");
local stanza = event.stanza;
local origin = pop_state(stanza.attr.id);
local resource = origin.resource;
if resource == nil then
module:log("error", "Got a MIX leave result for a not-requested id %s", stanza.attr.id);
module:log("error", "Maybe the MIX server changed the stanza ID?");
return false;
end
local client_leave = st.iq({
type = "result";
@ -232,23 +215,6 @@ local function handle_mix_leave(event)
return true;
end
module:hook("iq/bare", function(event)
-- We handle the MIX results here since IQ stanzas against bare JIDs
-- don't make a lot of sense. We first have to translate them into
-- full JIDs based on our stanza ID -> resource mapping
if event.stanza:get_child("leave", "urn:xmpp:mix:core:1") ~= nil then
return handle_mix_leave(event);
elseif event.stanza:get_child("join", "urn:xmpp:mix:core:1") ~= nil then
return handle_mix_join(event);
elseif has_state(event.stanza.attr.id) then
local tmp = st.clone(event.stanza);
local origin = pop_state(tmp.attr.id);
tmp.attr.to = tmp.attr.to.."/"..origin.resource;
module:send(tmp);
return true;
end
end);
module:hook("roster-get", function(event)
-- NOTE: Currently this requires a patch to make mod_roster emit
-- the roster-get event