mod_mix_pam: Remove SPID tracking

This commit is contained in:
PapaTutuWawa 2021-02-21 12:29:33 +01:00
parent c251502799
commit dc9d388ec3

View File

@ -10,13 +10,13 @@ local st = require("util.stanza");
local rm_remove_from_roster = require("core.rostermanager").remove_from_roster;
local rm_add_to_roster = require("core.rostermanager").add_to_roster;
local rm_roster_push = require("core.rostermanager").roster_push;
local rm_load_roster = require("core.rostermanager").load_roster;
-- Persistent storage
local mix_pam = module:open_store("mix_pam", "keyval");
-- Runtime data
local mix_hosts = {};
local mix_spids = {}; -- user -> channel -> spid
local mix_hosts = {}; -- MIX host's JID -> Reference Counter
-- Namespaceing
local mix_pam_xmlns = "urn:xmpp:mix:pam:2";
@ -26,33 +26,6 @@ module:add_feature(mix_pam_xmlns);
-- NOTE: To show that we archive messages
-- module:add_feature(mix_pam_xmlns.."#archive");
local function contains_key(table, key)
-- Returns true if table contains the key key. Returns false
-- otherwise.
return table[key] ~= nil;
end
local function set_mix_spid(user, channel, spid)
if mix_spids[user] ~= nil then
mix_spids[user][channel] = spid;
else
mix_spids[user] = { [channel] = spid };
end
mix_pam:set("spids", mix_spids);
end
local function get_mix_spid(user, channel)
return mix_spids[user][channel];
end;
local function remove_mix_spid(user, channel)
mix_spids[user][channel] = nil;
if mix_spids[user][channel] == {} then
mix_spids[user][channel] = nil;
end
mix_pam:set("spids", mix_spids);
end
local function add_mix_host(host)
if mix_hosts[host] ~= nil then
mix_hosts[host] = mix_hosts[host] + 1;
@ -99,24 +72,6 @@ function module.load()
for host, _ in pairs(mix_hosts) do
module:log("debug", "Known host: %s", host);
end
mix_spids = mix_pam:get("spids");
module:log("info", "Loaded known MIX SPIDs");
if mix_spids == nil then
module:log("info", "No known MIX SPIDs loaded");
mix_spids = {};
end
for user, channel_map in pairs(mix_spids) do
module:log("debug", "-- %s", user);
if channel_map[user] then
for channel, spid in pairs(channel_map[user]) do
module:log("debug", "%s -> %s", channel, spid);
end
else
module:log("warn", "User mapping empty");
end
end
end
local client_state_tracker = {}; -- [stanza ID] -> resource
@ -217,9 +172,6 @@ end);
local function handle_mix_join(event)
-- The MIX server responded
module:log("debug", "Received MIX-JOIN result");
module:log("debug", "Adding %s as a new MIX host", jid.host(event.stanza.attr.from));
add_mix_host(jid.host(event.stanza.attr.from));
mix_pam:set("hosts", mix_hosts);
local stanza = event.stanza;
local spid = stanza:get_child("join", "urn:xmpp:mix:core:1").attr.id;
@ -232,9 +184,6 @@ local function handle_mix_join(event)
return false;
end
-- Keep track of the SPID
set_mix_spid(stanza.attr.to, stanza.attr.from, spid);
local client_join = st.iq({
type = "result";
id = stanza.attr.id;
@ -249,8 +198,10 @@ local function handle_mix_join(event)
rm_add_to_roster(origin, stanza.attr.from, {
subscription = "both",
groups = {},
mix_spid = spid,
});
rm_roster_push(jid.node(stanza.attr.to), module_host, stanza.attr.from);
add_mix_host(jid.bare(stanza.attr.from));
return true;
end
@ -258,8 +209,6 @@ end
local function handle_mix_leave(event)
-- The MIX server responded
module:log("debug", "Received MIX-LEAVE result");
remove_mix_host(jid.host(event.stanza.attr.from));
mix_pam:set("hosts", mix_hosts);
local stanza = event.stanza;
local origin = pop_state(stanza.attr.id);
@ -270,9 +219,6 @@ local function handle_mix_leave(event)
return false;
end
-- Keep track of the SPID
set_mix_spid(stanza.attr.to, jid.bare(stanza.attr.from));
local client_leave = st.iq({
type = "result";
id = stanza.attr.id;
@ -287,6 +233,7 @@ local function handle_mix_leave(event)
-- TODO: Error handling
rm_remove_from_roster(origin, jid.bare(stanza.attr.from));
rm_roster_push(jid.node(stanza.attr.to), module_host, jid.bare(stanza.attr.from));
remove_mix_host(jid.bare(stanza.attr.from));
return true;
end
@ -348,13 +295,19 @@ module:hook("roster-get", function(event)
module:log("debug", "Annotated roster request received");
-- User requested the roster with an <annotate/>
for item in event.reply:get_child("query", "jabber:iq:roster"):children() do
if contains_key(mix_hosts, jid.host(item.attr.jid)) then
local spid = get_mix_spid(jid.bare(stanza.attr.from), item.attr.jid);
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"] = spid,
["participant-id"] = roster_item["mix_spid"],
});
break;
end
end
end
end
end);