From c7b9dc2432af9619142d7ad33c8deebcf8c27026 Mon Sep 17 00:00:00 2001 From: Alexander PapaTutuWawa Date: Mon, 22 Feb 2021 09:01:31 +0100 Subject: [PATCH] mod_mix_pam: Cleanup of the roster portion --- mod_mix_pam/mod_mix_pam.lua | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/mod_mix_pam/mod_mix_pam.lua b/mod_mix_pam/mod_mix_pam.lua index 01e5af6..0588f09 100644 --- a/mod_mix_pam/mod_mix_pam.lua +++ b/mod_mix_pam/mod_mix_pam.lua @@ -283,6 +283,24 @@ module:hook("resource-unbind", function(event) module:log("debug", "Unbind of not recorded resource %s (%s)", resource, session_jid); end); +local function child_with_attrs(stanza, childtag, xmlns, attrs) + -- Helper function: Like stanza:childtags, but returns only the first + -- child whose attributes are in attrs (and match the values in attrs). + -- Otherwise, nil is returned. + for _, child in ipairs(stanza:childtags(childtag, xmlns)) do + for attr_name, attr_value in pairs(attrs) do + if child.attrs[attr_name] ~= nil and child.attrs[attr_name] == attr_value then + return child; + else + -- We already now that this is not the child we're looking for + break; + end + end + end + + return nil; +end + module:hook("roster-get", function(event) -- NOTE: Currently this requires a patch to make mod_roster emit -- the roster-get event @@ -295,18 +313,14 @@ module:hook("roster-get", function(event) module:log("debug", "Annotated roster request received"); -- User requested the roster with an - local roster = rm_load_roster(jid.bare(stanza.attr.from)); local query = event.reply:get_child("query", "jabber:iq:roster"); - for contact, roster_item in pairs(roster) do - if roster_item["mix_spid"] ~= nil then - for _, item in ipairs(query:childtags("item")) do - if item.attr.jid == contact then - item:tag("channel", { - xmlns = mix_roster_xmlns, - ["participant-id"] = roster_item["mix_spid"], - }); - break; - end + for contact, item in pairs(rm_load_roster(jid.bare(stanza.attr.from))) do + if item["mix_spid"] ~= nil then + local roster_item = child_with_attrs(query, "item", nil, { jid = contact }); + if roster_item then + roster_item:tag("channel", { xmlns = mix_roster_xmlns, ["participant-id"] = item["mix_spid"] }); + else + module:log("warn", "Could not find %s during annotated roster query!"); end end end