mix: Fix smaller issues

This commit is contained in:
PapaTutuWawa 2020-10-30 17:45:22 +01:00
parent 51cecc0494
commit d996bd34b9

View File

@ -26,7 +26,7 @@ local restrict_channel_creation = module:get_option("restrict_local_channels", "
module:depends("disco"); module:depends("disco");
-- module:depends("mam"); TODO: Once message sending works -- module:depends("mam"); TODO: Once message sending works
module:add_identity("conference", "mix", module:get_option("name", "Prosody MIX service"); module:add_identity("conference", "mix", module:get_option("name", "Prosody MIX service"));
module:add_feature("http://jabber.org/protocol/disco#info"); module:add_feature("http://jabber.org/protocol/disco#info");
module:add_feature(mix_core_xmlns); module:add_feature(mix_core_xmlns);
@ -62,6 +62,11 @@ function Channel:get_spid(jid)
return self.spid[jid]; return self.spid[jid];
end end
function Channel:set_spid(jid, spid)
-- Sets the Stable Participant ID for the *BARE* jid
self.spid[jid] = spid;
end
function Channel:debug_print() function Channel:debug_print()
module:log("debug", "Channel %s (%s)", self.jid, self.name); module:log("debug", "Channel %s (%s)", self.jid, self.name);
module:log("debug", "'%s'", self.description); module:log("debug", "'%s'", self.description);
@ -168,10 +173,10 @@ module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(eve
module:log("debug", "IQ-GET disco#items"); module:log("debug", "IQ-GET disco#items");
local origin, stanza = event.origin, event.stanza; local origin, stanza = event.origin, event.stanza;
local _, channel = get_channel(staza.attr.to); local _, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
-- TODO: Maybe check permissions -- TODO: Maybe check permissions
@ -226,7 +231,7 @@ module:hook("iq-get/bare/http://jabber.org/protocol/disco#info:query", function(
local _, channel = get_channel(stanza.attr.to); local _, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info" }); local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info" });
reply:tag("feature", { var = "http://jabber.org/protocol/disco#info" }):up(); reply:tag("feature", { var = "http://jabber.org/protocol/disco#info" }):up();
@ -258,7 +263,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":leave", function(event)
local i, channel = get_channel(stanza.attr.to); local i, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
local participant_index = find_participant(channel.participants, from); local participant_index = find_participant(channel.participants, from);
@ -269,7 +274,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":leave", function(event)
"Not a participant")); "Not a participant"));
channel:debug_print(); channel:debug_print();
module:log("debug", "%s is not a participant in %s", from, channel.jid); module:log("debug", "%s is not a participant in %s", from, channel.jid);
return; return true;
end end
-- Remove the user as a participant by... -- Remove the user as a participant by...
@ -298,14 +303,14 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":join", function(event)
local i, channel = get_channel(stanza.attr.to); local i, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
-- Prevent the user from joining multiple times -- Prevent the user from joining multiple times
local participant_index = find_participant(channel.participants, from); local participant_index = find_participant(channel.participants, from);
if participant_index ~= -1 then if participant_index ~= -1 then
origin.send(st.error_reply(stanza, "cancel", "bad-request", "User already joined")); module:send(st.error_reply(stanza, "cancel", "bad-request", "User already joined"));
return; return true;
end end
local spid = channel:get_spid(from) or uuid.generate(); -- Stable Participant ID local spid = channel:get_spid(from) or uuid.generate(); -- Stable Participant ID
@ -339,13 +344,13 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":join", function(event)
if not has_subscribed_once then if not has_subscribed_once then
origin.send(st.error_reply(stanza, "cancel", first_error)); origin.send(st.error_reply(stanza, "cancel", first_error));
return; return true;
end end
local participant = Participant:new(jid.bare(from), nick:get_text()) local participant = Participant:new(jid.bare(from), nick:get_text())
channels[i].subscriptions[from] = nodes; channels[i].subscriptions[from] = nodes;
table.insert(channels[i].participants, participant) table.insert(channels[i].participants, participant)
channels[i]:get_spid(jid.bare(stanza.attr.from)) = spid; channels[i]:set_spid(jid.bare(stanza.attr.from), spid);
publish_participant(srv, spid, participant); publish_participant(srv, spid, participant);
channels[i]:save_state(); channels[i]:save_state();
@ -361,14 +366,14 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":setnick", function(event)
local i, channel = get_channel(stanza.attr.to); local i, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
local participant_index = find_participant(channel.participants, from); local participant_index = find_participant(channel.participants, from);
if participant_index == -1 then if participant_index == -1 then
channel:debug_print(); channel:debug_print();
module:log("debug", "%s is not a participant in %s", from, channel.jid); module:log("debug", "%s is not a participant in %s", from, channel.jid);
return; return true;
end end
-- NOTE: Prosody should guarantee us that the setnick stanza exists -- NOTE: Prosody should guarantee us that the setnick stanza exists
@ -376,7 +381,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":setnick", function(event)
local nick = setnick:get_child("nick"); local nick = setnick:get_child("nick");
if nick_stanza == nil then if nick_stanza == nil then
origin.send(st.error_reply(stanza, "cancel", "bad-request", "Missing <nick>")); origin.send(st.error_reply(stanza, "cancel", "bad-request", "Missing <nick>"));
return; return true;
end end
-- Change the nick -- Change the nick
@ -417,11 +422,17 @@ function create_channel(node, creator, adhoc)
-- Create the PEP nodes -- Create the PEP nodes
local srv = pep.get_pep_service(node); local srv = pep.get_pep_service(node);
local timestamp = datetime.datetime(time.now()); local timestamp = datetime.datetime(time.now());
srv:create("urn:xmpp:mix:nodes:info", true, { ["access_model"] = "open" }); local access_model = adhoc and "whitelist" or "open";
for _, psnode in pairs({"urn:xmpp:mix:nodes:info", "urn:xmpp:mix:nodes:participants", "urn:xmpp:mix:nodes:messages" }) do
srv:create(psnode, true, { ["access_model"] = access_model });
-- If the channel is an adhoc channel, then we need to make sure that
-- node affiliations are correct
if adhoc then
srv:set_affiliation(psnode, true, creator, "owner");
end
end
channel:publish_info(srv); channel:publish_info(srv);
-- TODO: This seems bad
srv:create("urn:xmpp:mix:nodes:participants", true, { ["access_model"] = "open"});
srv:create("urn:xmpp:mix:nodes:messages", true, { ["access_model"] = "open"});
table.insert(channels, channel); table.insert(channels, channel);
end end
@ -433,7 +444,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":create", function(event)
-- Check permissions -- Check permissions
if not can_create_channels(from) then if not can_create_channels(from) then
origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not authorized to create channels")); origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not authorized to create channels"));
return; return true;
end end
local create = stanza:get_child("create", mix_core_xmlns); local create = stanza:get_child("create", mix_core_xmlns);
@ -447,7 +458,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":create", function(event)
"cancel", "cancel",
"conflict", "conflict",
"Channel already exists")); "Channel already exists"));
return; return true;
end end
create_channel(create.attr.channel, from, false); create_channel(create.attr.channel, from, false);
@ -482,7 +493,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":destroy", function(event)
local i, channel = get_channel(node_jid); local i, channel = get_channel(node_jid);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
-- TODO: Check permissions: can_create_channels and maybe compare to the contact JIDs -- TODO: Check permissions: can_create_channels and maybe compare to the contact JIDs
@ -507,20 +518,20 @@ module:hook("message/bare", function(event)
if stanza.attr.type ~= "groupchat" then if stanza.attr.type ~= "groupchat" then
-- TODO: Is this correct? -- TODO: Is this correct?
origin.send(st.error_reply(stanza, "cancel", "bad-request", "Non-groupchat message")); origin.send(st.error_reply(stanza, "cancel", "bad-request", "Non-groupchat message"));
return; return true;
end end
local from = jid.bare(stanza.attr.from); local from = jid.bare(stanza.attr.from);
local i, channel = get_channel(stanza.attr.to); local i, channel = get_channel(stanza.attr.to);
if not channel then if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return; return true;
end end
local participant_index = find_participant(channel.participants, from); local participant_index = find_participant(channel.participants, from);
if participant_index == -1 then if participant_index == -1 then
origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not a participant")); origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not a participant"));
return; return true;
end end
local participant = channel.participants[participant_index]; local participant = channel.participants[participant_index];
@ -530,12 +541,11 @@ module:hook("message/bare", function(event)
:tag("jid"):text(participant.jid):up()); :tag("jid"):text(participant.jid):up());
msg.attr.from = channel.jid.."/"..channel:get_spid(from); msg.attr.from = channel.jid.."/"..channel:get_spid(from);
for _, p in pairs(channel.participants) do for _, p in pairs(channel.participants) do
if p.jid ~= participant.jid then -- TODO: Add message to the MAM and return its ID
msg.attr.to = p.jid; local tmp = st.clone(msg);
module:send(msg); tmp.attr.to = p.jid;
module:log("debug", msg:pretty_print()); module:send(tmp);
module:log("debug", "Message from %s sent to %s", participant.jid, p.jid); module:log("debug", "Message from %s sent to %s", participant.jid, p.jid);
end end
end
return true; return true;
end); end);