2021-02-19 16:03:17 +00:00
|
|
|
-- Big TODOlist
|
|
|
|
-- TODO: Channel:is_subscribed could be replaced by get_pep_service(...):get_subscription
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
local host = module:get_host();
|
|
|
|
if module:get_host_type() ~= "component" then
|
2020-11-02 16:07:50 +00:00
|
|
|
error("MIX should be loaded as a component", 0);
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local st = require("util.stanza");
|
|
|
|
local jid = require("util.jid");
|
|
|
|
local uuid = require("util.uuid");
|
|
|
|
local id = require("util.id");
|
|
|
|
local datetime = require("util.datetime");
|
|
|
|
local time = require("util.time");
|
|
|
|
local serialization = require("util.serialization");
|
2020-11-02 16:07:50 +00:00
|
|
|
local dataforms = require("util.dataforms");
|
2020-10-27 17:05:41 +00:00
|
|
|
local pep = module:depends("pep");
|
2020-11-02 16:07:50 +00:00
|
|
|
local helpers = module:require("mix/helpers");
|
|
|
|
|
|
|
|
local mixlib = module:require("mix/mix");
|
|
|
|
Channel = mixlib.Channel;
|
|
|
|
Participant = mixlib.Participant;
|
2020-10-27 17:05:41 +00:00
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
-- XML namespaces
|
|
|
|
local mix_core_xmlns = "urn:xmpp:mix:core:1";
|
2021-02-20 19:33:46 +00:00
|
|
|
local mix_anon_xmlns = "urn:xmpp:mix:anon:0";
|
2021-02-19 16:03:17 +00:00
|
|
|
--local mix_admin_xmlns = "urn:xmpp:mix:admin:0";
|
2020-11-02 16:07:50 +00:00
|
|
|
local mix_node_messages = "urn:xmpp:mix:nodes:messages";
|
|
|
|
local mix_node_participants = "urn:xmpp:mix:nodes:participants";
|
|
|
|
local mix_node_info = "urn:xmpp:mix:nodes:info";
|
2021-02-19 16:03:17 +00:00
|
|
|
local mix_node_allowed = "urn:xmpp:mix:nodes:allowed";
|
|
|
|
local mix_node_banned = "urn:xmpp:mix:nodes:banned";
|
|
|
|
local mix_node_config = "urn:xmpp:mix:nodes:config";
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
local mam_xmlns = "urn:xmpp:mam:2";
|
2020-10-29 16:58:11 +00:00
|
|
|
|
|
|
|
-- Persistent data
|
2020-10-27 17:05:41 +00:00
|
|
|
local persistent_channels = module:open_store("mix_channels", "keyval");
|
|
|
|
local persistent_channel_data = module:open_store("mix_data", "keyval");
|
2020-11-02 16:07:50 +00:00
|
|
|
local message_archive = module:open_store("mix_log", "archive");
|
2020-10-27 17:05:41 +00:00
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
-- Configuration
|
2020-10-27 17:05:41 +00:00
|
|
|
local default_channel_description = module:get_option("default_description", "A MIX channel for chatting");
|
|
|
|
local default_channel_name = module:get_option("default_name", "MIX channel");
|
2020-10-29 17:10:08 +00:00
|
|
|
local restrict_channel_creation = module:get_option("restrict_local_channels", "local");
|
2020-10-27 17:05:41 +00:00
|
|
|
|
2021-02-20 19:33:46 +00:00
|
|
|
-- Dataforms
|
|
|
|
-- MAM
|
2020-11-02 16:07:50 +00:00
|
|
|
local mam_query_form = dataforms.new({
|
|
|
|
{ name = "FORM_TYPE", type = "hidden", value = mam_xmlns },
|
|
|
|
{ name = "with", type = "jid-single" },
|
|
|
|
{ name = "start", type = "text-single" },
|
|
|
|
{ name = "end", type = "text-single" },
|
|
|
|
});
|
2021-02-20 19:33:46 +00:00
|
|
|
-- MIX Anon
|
|
|
|
local mix_anon_form = dataforms.new({
|
|
|
|
{ name = "FORM_TYPE", type = "hidden", value = mix_anon_xmlns },
|
|
|
|
{ name = "JID Visibility", type = "text-single" },
|
|
|
|
{ name = "Private Messages", type = "text-single" },
|
|
|
|
{ name = "Presence", type = "text-single" },
|
|
|
|
{ name = "vCard", type = "text-single" },
|
|
|
|
});
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
|
2021-02-19 16:03:17 +00:00
|
|
|
-- MIX-ADMIN stuff
|
|
|
|
--[[
|
|
|
|
local mix_config_form = dataforms.new({
|
|
|
|
{ name = "FORM_TYPE", type = "hidden", value = mix_admin_xmlns },
|
|
|
|
{ name = "Last Change Made By", type = "jid-single" },
|
|
|
|
{ name = "Owner", type = "jid-multi" },
|
|
|
|
{ name = "Administrator", type = "jid-multi" },
|
|
|
|
{ name = "End of Life", type = "text-single" },
|
|
|
|
{ name = "Nodes Present", type = "list-multi" },
|
|
|
|
{ name = "Message Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Presence Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Participants Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Information Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Allowed Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Banned Node Subscription", type = "list-single" },
|
|
|
|
{ name = "Configuration Node Access", type = "list-single" },
|
|
|
|
});
|
|
|
|
]]--
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
module:depends("disco");
|
2020-10-30 16:45:22 +00:00
|
|
|
module:add_identity("conference", "mix", module:get_option("name", "Prosody MIX service"));
|
2020-10-27 17:05:41 +00:00
|
|
|
module:add_feature("http://jabber.org/protocol/disco#info");
|
2020-10-29 16:58:11 +00:00
|
|
|
module:add_feature(mix_core_xmlns);
|
2020-10-27 17:05:41 +00:00
|
|
|
|
|
|
|
local channels = {};
|
|
|
|
|
|
|
|
function get_channel(jid)
|
2020-10-29 16:58:11 +00:00
|
|
|
-- Return the channel object from the channels array for which the
|
|
|
|
-- JID matches. If none is found, returns -1, nil
|
2020-11-02 16:07:50 +00:00
|
|
|
return helpers.find(channels, function(c) return c.jid == jid; end);
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function save_channels()
|
|
|
|
module:log("debug", "Saving channel list...");
|
|
|
|
local channel_list = {};
|
|
|
|
for _, channel in pairs(channels) do
|
|
|
|
table.insert(channel_list, channel.jid);
|
|
|
|
|
|
|
|
persistent_channel_data:set(channel.jid, channel);
|
|
|
|
end
|
|
|
|
|
|
|
|
persistent_channels:set("channels", channel_list);
|
|
|
|
module:log("debug", "Saving channel list done.");
|
|
|
|
end
|
|
|
|
|
|
|
|
function Channel:save_state()
|
|
|
|
-- Saving the entire state everything one channel seems stupid,
|
|
|
|
-- so we just save the changed channel
|
|
|
|
module:log("debug", "Saving state of channel %s...", self.jid);
|
|
|
|
persistent_channel_data:set(self.jid, self);
|
|
|
|
module:log("debug", "Saving state done.", self.jid);
|
|
|
|
end
|
|
|
|
|
2021-02-19 16:03:17 +00:00
|
|
|
function publish_participant(service, channel, spid, participant)
|
2020-11-02 16:07:50 +00:00
|
|
|
-- Publish a new participant on the service
|
2021-02-19 16:03:17 +00:00
|
|
|
-- NOTE: This function has be to called *after* the new participant
|
|
|
|
-- has been added to the channel.participants attay
|
|
|
|
service:set_node_config("urn:xmpp:mix:nodes:participants",
|
|
|
|
true,
|
|
|
|
{ ["max_items"] = #channel.participants });
|
2020-11-02 16:07:50 +00:00
|
|
|
service:publish("urn:xmpp:mix:nodes:participants",
|
|
|
|
true,
|
|
|
|
spid,
|
|
|
|
st.stanza("item", { id = spid, xmlns = "http://jabber.org/protocol/pubsub" })
|
|
|
|
:tag("participant", { xmlns = mix_core_xmlns })
|
|
|
|
:tag("nick"):text(participant["nick"]):up()
|
|
|
|
:tag("jid"):text(participant["jid"]));
|
|
|
|
end
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
function module.load()
|
|
|
|
module:log("info", "Loading MIX channels...");
|
|
|
|
|
|
|
|
local channel_list = persistent_channels:get("channels");
|
|
|
|
if channel_list then
|
|
|
|
for _, channel in pairs(channel_list) do
|
2020-11-02 16:07:50 +00:00
|
|
|
local channel = Channel:from(persistent_channel_data:get(channel));
|
|
|
|
table.insert(channels, channel);
|
|
|
|
module:log("debug", "MIX channel %s loaded", channel.jid);
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
module:log("debug", "No MIX channels found.");
|
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
module:log("info", "Loading MIX channels done.");
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 16:08:04 +00:00
|
|
|
function channel_not_found(stanza)
|
|
|
|
-- Wrapper for returning a "Channel-not-found" error stanza
|
|
|
|
return st.error_reply(stanza,
|
|
|
|
"cancel",
|
|
|
|
"item-not-found",
|
|
|
|
"The MIX channel was not found");
|
|
|
|
end
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
module:hook("host-disco-items", function(event)
|
|
|
|
module:log("debug", "host-disco-items called");
|
|
|
|
local reply = event.reply;
|
|
|
|
for _, channel in pairs(channels) do
|
|
|
|
-- Adhoc channels are supposed to be invisible
|
|
|
|
if not channel.adhoc then
|
|
|
|
reply:tag("item", { jid = channel.jid }):up();
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end);
|
|
|
|
|
|
|
|
module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
|
|
|
|
module:log("debug", "IQ-GET disco#items");
|
|
|
|
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
2020-11-02 16:07:50 +00:00
|
|
|
if stanza:get_child("query", "http://jabber.org/protocol/disco#items").attr.node ~= "mix" then
|
|
|
|
origin.send(st.error_reply(stanza, "modify", "bad-request"));
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Maybe here we should check if the user has permissions to get infos
|
|
|
|
-- about the channel before saying that it doesn't exist to prevent creating
|
|
|
|
-- an oracle.
|
2020-10-30 16:45:22 +00:00
|
|
|
local _, channel = get_channel(stanza.attr.to);
|
2020-10-27 17:05:41 +00:00
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
|
|
|
|
if not channel:is_participant(jid.bare(stanza.attr.from)) then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "forbidden"));
|
|
|
|
return true;
|
|
|
|
end
|
2020-10-29 16:08:04 +00:00
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = "mix" });
|
2020-11-02 16:07:50 +00:00
|
|
|
for _, node in pairs({mix_node_messages, mix_node_participants, mix_node_info}) do
|
2020-10-27 17:05:41 +00:00
|
|
|
reply:tag("item", { jid = channel.jid, node = node }):up();
|
|
|
|
end
|
|
|
|
|
|
|
|
origin.send(reply);
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
2020-10-29 17:10:08 +00:00
|
|
|
function can_create_channels(user)
|
|
|
|
-- Returns true when the jid is allowed to create MIX channels. False otherwise.
|
2020-11-02 16:07:50 +00:00
|
|
|
-- NOTE: Taken from plugins/muc/mod_muc.lua
|
|
|
|
local host_suffix = host:gsub("^[^%.]+%.", "");
|
|
|
|
|
2020-10-29 17:10:08 +00:00
|
|
|
if restrict_channel_creation == "local" then
|
|
|
|
module:log("debug", "Comparing %s (Sender) to %s (Host)", jid.host(user), host_suffix);
|
|
|
|
|
|
|
|
if jid.host(user) == host_suffix then
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
elseif type(restrict_channel_creation) == "table" then
|
|
|
|
if helpers.find_str(restrict_channel_creation, user) ~= -1 then
|
|
|
|
-- User was specifically listed
|
|
|
|
return true;
|
|
|
|
elseif helpers.find_str(restrict_channel_creation, jid.host(user)) then
|
|
|
|
-- User's host was allowed
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
return false;
|
2020-10-29 17:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Handle also true/"admin" (See mod_muc)
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event)
|
|
|
|
module:log("debug", "IQ-GET host disco#info");
|
|
|
|
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local reply = st.reply(stanza)
|
|
|
|
:tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
|
|
|
|
-- TODO: Name
|
|
|
|
:tag("identity", { category = "conference", type = "mix", name = "" }):up()
|
|
|
|
:tag("feature", { var = "http://jabber.org/protocol/disco#info" }):up()
|
2020-11-02 16:07:50 +00:00
|
|
|
:tag("feature", { var = "http://jabber.org/protocol/disco#items" }):up()
|
2020-10-29 16:58:11 +00:00
|
|
|
:tag("feature", { var = mix_core_xmlns }):up();
|
|
|
|
|
2020-10-29 17:10:08 +00:00
|
|
|
if can_create_channels(stanza.attr.from) then
|
|
|
|
reply:tag("feature", { var = mix_core_xmlns.."#create-channel" }):up();
|
2020-10-29 16:58:11 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
origin.send(reply);
|
|
|
|
return true;
|
2020-10-29 16:58:11 +00:00
|
|
|
end);
|
|
|
|
|
|
|
|
module:hook("iq-get/bare/http://jabber.org/protocol/disco#info:query", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "IQ-GET disco#info");
|
|
|
|
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local _, channel = get_channel(stanza.attr.to);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info" });
|
2020-10-29 16:08:04 +00:00
|
|
|
reply:tag("feature", { var = "http://jabber.org/protocol/disco#info" }):up();
|
2020-10-27 17:05:41 +00:00
|
|
|
reply:tag("identity", { category = "conference", name = channel.name, type = "mix" }):up();
|
2020-10-29 16:08:04 +00:00
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
reply:tag("feature", { var = mix_core_xmlns }):up();
|
2020-11-02 16:07:50 +00:00
|
|
|
reply:tag("feature", { var = "urn:xmpp:mam:2" }):up();
|
2020-10-29 16:58:11 +00:00
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
origin.send(reply);
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
module:hook("iq-set/bare/"..mam_xmlns..":query", function(event)
|
|
|
|
local stanza, origin = event.stanza, event.origin;
|
|
|
|
local channel_jid = stanza.attr.to;
|
|
|
|
local j, channel = get_channel(channel_jid);
|
|
|
|
if j == -1 then
|
|
|
|
-- TODO: Is this correct?
|
|
|
|
origin.send(channel_not_found(stanza));
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if the user is subscribed to the messages node
|
|
|
|
if not channel:is_subscribed(stanza.attr.from, mix_node_messages) then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "forbidden"));
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
local query = stanza:get_child("query", mam_xmlns);
|
|
|
|
local filter = {};
|
|
|
|
local query_id = query.attr.queryid;
|
|
|
|
local x = query:get_child("x", "jabber:x:data");
|
|
|
|
if x ~= nil then
|
|
|
|
local form, err = mam_query_form:data(x);
|
|
|
|
|
|
|
|
-- Validate
|
|
|
|
if (form["start"] and not form["end"]) or (not form["start"] and form["end"]) then
|
|
|
|
origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"));
|
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
|
|
|
|
module:log("debug", "Got a MAM query between %s and %s", form["start"], form["end"]);
|
|
|
|
filter = {
|
|
|
|
start = datetime.parse(form["start"]); ["end"] = datetime.parse(form["end"])
|
|
|
|
};
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
local data, err = message_archive:find(channel_jid, filter);
|
|
|
|
if not data then
|
|
|
|
module:log("debug", "MAM error: %s", err);
|
2020-10-27 17:05:41 +00:00
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
if err == "item-not-found" then
|
|
|
|
origin.send(st.error_reply(stanza, "modify", "item-not-found"));
|
|
|
|
else
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "internal-server-error"));
|
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
for id, item, when in data do
|
|
|
|
local msg = st.stanza("message", { from = channel_jid, to = stanza.attr.from, type = "groupchat" })
|
|
|
|
:tag("result", { xmlns = mam_xmlns, queryid = query_id, id = id })
|
|
|
|
:tag("forwarded", { xmlns = "urn:xmpp:forward:0" })
|
|
|
|
:tag("delay", { xmlns = "urn:xmpp:delay", stamp = datetime.datetime(when) }):up();
|
|
|
|
msg:add_child(item);
|
|
|
|
origin.send(msg);
|
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
|
|
|
module:hook("iq-get/bare/"..mam_xmlns..":query", function(event)
|
|
|
|
if event.stanza.attr.id ~= "form1" then return; end
|
|
|
|
|
|
|
|
module:log("debug", "Got a MAM query for supported fields");
|
|
|
|
|
|
|
|
local ret = st.reply(event.stanza)
|
|
|
|
:tag("query", { xmlns = mam_xmlns })
|
|
|
|
:tag("x", { xmlns = "jabber:x:data", type = "form"})
|
|
|
|
:tag("field", { type = "hidden", var = "FORM_TYPE" })
|
|
|
|
:tag("value"):text(mam_xmlns):up():up()
|
|
|
|
:tag("field", { type = "jid-single", var = "with" }):up()
|
|
|
|
:tag("field", { type = "text-single", var = "start" }):up()
|
|
|
|
:tag("field", { type = "text-single", var = "end" });
|
|
|
|
module:send(ret);
|
|
|
|
return true;
|
|
|
|
end);
|
2020-10-27 17:05:41 +00:00
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-set/bare/"..mix_core_xmlns..":leave", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "MIX leave received");
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local from = jid.bare(stanza.attr.from);
|
|
|
|
local i, channel = get_channel(stanza.attr.to);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
local j, _ = channel:find_participant(from);
|
|
|
|
if j == -1 then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(st.error_reply(stanza,
|
|
|
|
"cancel",
|
|
|
|
"forbidden",
|
|
|
|
"Not a participant"));
|
2020-10-27 17:05:41 +00:00
|
|
|
channel:debug_print();
|
|
|
|
module:log("debug", "%s is not a participant in %s", from, channel.jid);
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove the user as a participant by...
|
|
|
|
-- Unsubscribing
|
|
|
|
local srv = pep.get_pep_service(channel.jid);
|
|
|
|
for _, node in pairs(channel.subscriptions[from]) do
|
2021-02-19 16:03:17 +00:00
|
|
|
srv:set_affiliation(node, true, from, "outcast");
|
2020-10-27 17:05:41 +00:00
|
|
|
srv:remove_subscription(node, true, from);
|
|
|
|
module:log("debug", "Unsubscribed %s from %s on %s", from, node, channel.jid);
|
|
|
|
end
|
2021-02-20 19:33:46 +00:00
|
|
|
channel.subscriptions[from] = nil;
|
2020-11-02 16:07:50 +00:00
|
|
|
-- Retracting the participation
|
2020-10-29 16:58:11 +00:00
|
|
|
srv:retract("urn:xmpp:mix:nodes:participants", true, channel:get_spid(from), true);
|
2020-10-27 17:05:41 +00:00
|
|
|
-- Removing the user
|
2021-02-20 19:33:46 +00:00
|
|
|
table.remove(channel.participants, j);
|
2020-10-27 17:05:41 +00:00
|
|
|
channel:save_state();
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
origin.send(st.reply(stanza):tag("leave", { xmlns = mix_core_xmlns }));
|
2020-10-27 17:05:41 +00:00
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-set/bare/"..mix_core_xmlns..":join", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "MIX join received");
|
|
|
|
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
2020-10-29 16:58:11 +00:00
|
|
|
local from = jid.bare(stanza.attr.from);
|
2020-10-27 17:05:41 +00:00
|
|
|
local i, channel = get_channel(stanza.attr.to);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-10-29 16:58:11 +00:00
|
|
|
|
|
|
|
-- Prevent the user from joining multiple times
|
2020-11-02 16:07:50 +00:00
|
|
|
local j, _ = channel:find_participant(from);
|
|
|
|
if j ~= -1 then
|
2020-10-30 16:45:22 +00:00
|
|
|
module:send(st.error_reply(stanza, "cancel", "bad-request", "User already joined"));
|
|
|
|
return true;
|
2020-10-29 16:58:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local spid = channel:get_spid(from) or uuid.generate(); -- Stable Participant ID
|
2020-10-27 17:05:41 +00:00
|
|
|
local reply = st.reply(stanza)
|
2020-10-29 16:58:11 +00:00
|
|
|
:tag("join", { xmlns = mix_core_xmlns, id = spid });
|
2020-10-27 17:05:41 +00:00
|
|
|
local srv = pep.get_pep_service(jid.node(stanza.attr.to));
|
2020-10-29 16:58:11 +00:00
|
|
|
local join = stanza:get_child("join", mix_core_xmlns);
|
2021-02-19 16:03:17 +00:00
|
|
|
local nick_tag = join:get_child("nick");
|
|
|
|
local nick;
|
|
|
|
if not join:get_child("nick") then
|
|
|
|
nick = jid.node(from);
|
|
|
|
else
|
|
|
|
nick = join:get_child("nick"):get_text();
|
|
|
|
end
|
|
|
|
module:log("debug", "User joining as nick %s", nick);
|
2020-10-27 17:05:41 +00:00
|
|
|
|
|
|
|
local nodes = {};
|
2020-10-29 16:58:11 +00:00
|
|
|
local has_subscribed_once = false;
|
|
|
|
local first_error = nil;
|
2020-10-27 17:05:41 +00:00
|
|
|
for subscribe in join:childtags("subscribe") do
|
|
|
|
module:log("debug", "Subscribing user to node %s", subscribe.attr.node);
|
2021-02-19 16:03:17 +00:00
|
|
|
-- TODO: Once MIX-ADMIN is implemented, we should check here what
|
|
|
|
-- affiliation we set, e.g. if the JID is the owner, then set owner.
|
|
|
|
srv:set_affiliation(subscribe.attr.node, true, from, "member");
|
2020-10-27 17:05:41 +00:00
|
|
|
local ok, err = srv:add_subscription(subscribe.attr.node, true, from);
|
|
|
|
if not ok then
|
|
|
|
module:log("debug", "Error during subscription: %s", err);
|
2020-10-29 16:58:11 +00:00
|
|
|
|
|
|
|
-- MIX-CORE says that the first error should be returned when
|
|
|
|
-- no of the requested nodes could be subscribed to
|
|
|
|
if first_error ~= nil then
|
|
|
|
first_error = err;
|
|
|
|
end
|
2020-10-27 17:05:41 +00:00
|
|
|
else
|
|
|
|
table.insert(nodes, subscribe.attr.node);
|
|
|
|
reply:tag("subscribe", { node = subscribe.attr.node }):up();
|
2020-10-29 16:58:11 +00:00
|
|
|
has_subscribed_once = true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
end
|
2020-10-29 16:58:11 +00:00
|
|
|
|
|
|
|
if not has_subscribed_once then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", first_error));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-29 16:58:11 +00:00
|
|
|
end
|
|
|
|
|
2021-02-20 19:33:46 +00:00
|
|
|
-- TODO: Make the default configurable
|
|
|
|
local jid_visibility = "never"; -- default
|
|
|
|
local allow_pms = "allow";
|
|
|
|
local allow_vcards = "block";
|
|
|
|
local share_presence = "share";
|
|
|
|
local x = join:get_child("x", "jabber:x:data");
|
|
|
|
if x ~= nil then
|
|
|
|
-- TODO: Rethink naming
|
|
|
|
-- TODO: Error handling?
|
|
|
|
local form, err = mix_anon_form:data(x);
|
|
|
|
if form["JID Visibility"] then
|
|
|
|
jid_visibility = form["JID Visibility"];
|
|
|
|
end
|
|
|
|
if form["Private Messages"] then
|
|
|
|
allow_pms = form["Private Messages"];
|
|
|
|
end
|
|
|
|
if form["Presence"] then
|
|
|
|
share_presence = form["Presence"];
|
|
|
|
end
|
|
|
|
if form["vCard"] then
|
|
|
|
allow_vcards = form["vCard"];
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local participant = Participant:new(jid.bare(from), nick, jid_visibility, allow_pms, share_presence, allow_vcards);
|
|
|
|
channel.subscriptions[from] = nodes;
|
|
|
|
table.insert(channel.participants, participant)
|
|
|
|
channel:set_spid(jid.bare(stanza.attr.from), spid);
|
2021-02-19 16:03:17 +00:00
|
|
|
publish_participant(srv, channel, spid, participant);
|
2021-02-20 19:33:46 +00:00
|
|
|
channel:save_state();
|
2020-10-27 17:05:41 +00:00
|
|
|
|
|
|
|
reply:add_child(nick);
|
2021-02-20 19:33:46 +00:00
|
|
|
reply:tag("x", { xmlns = "jabber:x:data", type = "result" })
|
|
|
|
:tag("field", { var = "FORM_TYPE", type = "hidden" })
|
|
|
|
:tag("value"):text(mix_anon_xmlns):up():up()
|
|
|
|
:tag("field", { var = "JID Visibility"})
|
|
|
|
:tag("value"):text(jid_visibility):up():up()
|
|
|
|
:tag("field", { var = "Private Messages"})
|
|
|
|
:tag("value"):text(allow_pms):up():up()
|
|
|
|
:tag("field", { var = "Presence"})
|
|
|
|
:tag("value"):text(share_presence):up():up()
|
|
|
|
:tag("field", { var = "vCard"})
|
|
|
|
:tag("value"):text(allow_vcards):up():up();
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
origin.send(reply);
|
|
|
|
return true
|
|
|
|
end);
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-set/bare/"..mix_core_xmlns..":setnick", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "MIX setnick received");
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local from = jid.bare(stanza.attr.from);
|
|
|
|
local i, channel = get_channel(stanza.attr.to);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
local j, participant = channel:find_participant(from);
|
|
|
|
if j == -1 then
|
2020-10-27 17:05:41 +00:00
|
|
|
channel:debug_print();
|
|
|
|
module:log("debug", "%s is not a participant in %s", from, channel.jid);
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
-- NOTE: Prosody should guarantee us that the setnick stanza exists
|
|
|
|
local setnick = stanza:get_child("setnick", mix_core_xmlns);
|
|
|
|
local nick = setnick:get_child("nick");
|
|
|
|
if nick_stanza == nil then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "bad-request", "Missing <nick>"));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-29 16:58:11 +00:00
|
|
|
end
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
-- Change the nick
|
2021-02-20 19:33:46 +00:00
|
|
|
channel.participants[j].nick = nick:get_text();
|
2020-10-27 17:05:41 +00:00
|
|
|
-- Inform all other members
|
|
|
|
local srv = pep.get_pep_service(channel.jid);
|
2020-11-02 16:07:50 +00:00
|
|
|
--local participant = channel.participants[participant_index];
|
2021-02-19 16:03:17 +00:00
|
|
|
publish_participant(srv, channel, channel:get_spid(participant.jid), participant);
|
2020-10-27 17:05:41 +00:00
|
|
|
|
|
|
|
origin.send(st.reply(stanza)
|
2020-10-29 16:58:11 +00:00
|
|
|
:tag("setnick", { xmlns = mix_core_xmlns })
|
|
|
|
:tag("nick"):text(nick:get_text()));
|
2020-10-27 17:05:41 +00:00
|
|
|
channel:save_state();
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
|
|
|
function Channel:publish_info(srv)
|
2020-11-02 16:07:50 +00:00
|
|
|
local timestamp = datetime.datetime(time.now());
|
|
|
|
local info = st.stanza("item", { id = timestamp, xmlns = "http://jabber.org/protocol/pubsub" })
|
2020-10-27 17:05:41 +00:00
|
|
|
:tag("x", { xmlns = "jabber:x:data", type = "result" })
|
|
|
|
:tag("field", { var = "FORM_TYPE", type = "hidden" })
|
2020-10-29 16:58:11 +00:00
|
|
|
:tag("value"):text(mix_core_xmlns):up():up()
|
2020-10-27 17:05:41 +00:00
|
|
|
:tag("field", { var = "Name" })
|
|
|
|
:tag("value"):text(self.name):up()
|
2020-11-02 16:07:50 +00:00
|
|
|
:tag("field", { var = "Contact" });
|
|
|
|
for _, contact in pairs(self.contacts) do
|
|
|
|
info:add_child(st.stanza("value"):text(contact));
|
|
|
|
end
|
|
|
|
srv:publish(mix_node_info, true, timestamp, info);
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function create_channel(node, creator, adhoc)
|
|
|
|
local channel = Channel:new(string.format("%s@%s", node, host),
|
|
|
|
default_channel_name,
|
|
|
|
default_channel_description,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{ creator },
|
|
|
|
adhoc);
|
|
|
|
-- Create the PEP nodes
|
|
|
|
local srv = pep.get_pep_service(node);
|
|
|
|
local timestamp = datetime.datetime(time.now());
|
2021-02-19 16:03:17 +00:00
|
|
|
-- MIX-CORE
|
2020-11-02 16:07:50 +00:00
|
|
|
for _, psnode in pairs({ mix_node_info, mix_node_participants, mix_node_messages }) do
|
2021-02-19 16:03:17 +00:00
|
|
|
srv:create(psnode, true, {
|
|
|
|
["access_model"] = "whitelist",
|
|
|
|
["persist_items"] = true,
|
|
|
|
});
|
2020-10-30 16:45:22 +00:00
|
|
|
end
|
2020-10-27 17:05:41 +00:00
|
|
|
channel:publish_info(srv);
|
2021-02-19 16:03:17 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
-- MIX-ADMIN
|
|
|
|
local admin_nodes = { mix_node_banned, mix_node_config };
|
|
|
|
if adhoc then
|
|
|
|
table.insert(admin_nodes, mix_node_allowed);
|
|
|
|
end
|
|
|
|
for _, psnode in pairs(admin_nodes) do
|
|
|
|
srv:create(mix_node_allowed, true, { ["access_model"] = "whitelist" });
|
|
|
|
srv:set_affiliation(mix_node_allowed, true, creator, "owner");
|
|
|
|
end
|
|
|
|
if adhoc then
|
|
|
|
-- Allow the creator to join
|
|
|
|
srv:publish(mix_node_allowed,
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
st.stanza("item", { id = creator }));
|
|
|
|
end
|
|
|
|
]]--
|
2020-10-27 17:05:41 +00:00
|
|
|
table.insert(channels, channel);
|
|
|
|
end
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-set/host/"..mix_core_xmlns..":create", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "MIX create received");
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local from = jid.bare(stanza.attr.from);
|
|
|
|
|
2020-10-29 17:10:08 +00:00
|
|
|
-- Check permissions
|
|
|
|
if not can_create_channels(from) then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not authorized to create channels"));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-29 17:10:08 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
local create = stanza:get_child("create", mix_core_xmlns);
|
2020-10-27 17:05:41 +00:00
|
|
|
local node;
|
|
|
|
if create.attr.channel ~= nil then
|
|
|
|
-- Create non-adhoc channel
|
2020-11-02 16:07:50 +00:00
|
|
|
module:log("debug", "Attempting to create channel %s", create.attr.channel);
|
2020-10-27 17:05:41 +00:00
|
|
|
node = create.attr.channel;
|
2020-11-02 16:07:50 +00:00
|
|
|
local i, _ = get_channel(create.attr.channel.."@"..stanza.attr.to);
|
|
|
|
module:log("debug", "Channel index %s", i);
|
|
|
|
if i ~= -1 then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(st.error_reply(stanza,
|
|
|
|
"cancel",
|
|
|
|
"conflict",
|
|
|
|
"Channel already exists"));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
create_channel(create.attr.channel, from, false);
|
|
|
|
else
|
|
|
|
-- Create adhoc channel
|
2020-10-29 16:58:11 +00:00
|
|
|
while (true) do
|
|
|
|
node = id.short();
|
2020-11-02 16:07:50 +00:00
|
|
|
local i, _ = get_channel(string.format("%s@%s", node, host));
|
|
|
|
if i == -1 then
|
2020-10-29 16:58:11 +00:00
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
create_channel(node, from, true);
|
|
|
|
end
|
|
|
|
module:log("debug", "Channel %s created with %s as owner", node, from);
|
|
|
|
|
|
|
|
origin.send(st.reply(stanza)
|
2020-10-29 16:58:11 +00:00
|
|
|
:tag("create", { xmlns = mix_core_xmlns, channel = node }));
|
2020-10-27 17:05:41 +00:00
|
|
|
save_channels();
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
module:hook("iq-set/host/"..mix_core_xmlns..":destroy", function(event)
|
2020-10-27 17:05:41 +00:00
|
|
|
module:log("debug", "MIX destroy received");
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
local from = jid.bare(stanza.attr.from);
|
|
|
|
|
2020-10-29 16:58:11 +00:00
|
|
|
local destroy = stanza:get_child("create", mix_core_xmlns);
|
2020-10-27 17:05:41 +00:00
|
|
|
local node = destory.attr.channel;
|
|
|
|
local node_jid = string.format("%s@%s", node, host);
|
|
|
|
local i, channel = get_channel(node_jid);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
|
|
|
|
-- TODO: Maybe make this configurable
|
|
|
|
if helpers.find_str(channel.contacts, from) == -1 then
|
|
|
|
origin.send(st.error_reply(stanza, "cancel", "forbidden"));
|
|
|
|
return true;
|
|
|
|
end
|
2020-10-27 17:05:41 +00:00
|
|
|
-- Remove all registered nodes
|
|
|
|
local srv = pep.get_pep_service(node);
|
2020-11-02 16:07:50 +00:00
|
|
|
for _, pep_node in pairs({ mix_node_participants, mix_node_info, mix_node_messages }) do
|
2020-10-27 17:05:41 +00:00
|
|
|
srv:delete(pep_node, true);
|
|
|
|
end
|
|
|
|
table.remove(channels, i);
|
|
|
|
|
|
|
|
module:log("debug", "Channel %s destroyed", node);
|
|
|
|
|
|
|
|
origin.send(st.reply(stanza));
|
|
|
|
save_channels();
|
|
|
|
return true;
|
|
|
|
end);
|
|
|
|
|
|
|
|
module:hook("message/bare", function(event)
|
2020-11-02 16:07:50 +00:00
|
|
|
module:log("debug", "MIX message received");
|
2020-10-29 16:08:04 +00:00
|
|
|
local stanza, origin = event.stanza, event.origin;
|
2020-10-27 17:05:41 +00:00
|
|
|
if stanza.attr.type ~= "groupchat" then
|
2020-11-02 16:07:50 +00:00
|
|
|
origin.send(st.error_reply(stanza, "modify", "bad-request", "Non-groupchat message"));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local from = jid.bare(stanza.attr.from);
|
|
|
|
local i, channel = get_channel(stanza.attr.to);
|
|
|
|
if not channel then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(channel_not_found(stanza));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
2020-11-02 16:07:50 +00:00
|
|
|
|
|
|
|
local j, participant = channel:find_participant(from);
|
|
|
|
if j == -1 then
|
2020-10-29 16:08:04 +00:00
|
|
|
origin.send(st.error_reply(stanza, "cancel", "forbidden", "Not a participant"));
|
2020-10-30 16:45:22 +00:00
|
|
|
return true;
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local msg = st.clone(stanza);
|
2020-10-29 16:58:11 +00:00
|
|
|
msg:add_child(st.stanza("mix", { xmlns = mix_core_xmlns })
|
2020-10-27 17:05:41 +00:00
|
|
|
:tag("nick"):text(participant.nick):up()
|
|
|
|
:tag("jid"):text(participant.jid):up());
|
2020-11-02 16:07:50 +00:00
|
|
|
|
|
|
|
-- Put the message into the archive
|
|
|
|
local mam_id = uuid.generate();
|
|
|
|
msg.attr.id = mam_id;
|
|
|
|
-- NOTE: The spec says to do so
|
|
|
|
msg.attr.from = channel.jid;
|
|
|
|
message_archive:append(stanza.attr.to, mam_id, msg, time.now());
|
2020-10-29 16:58:11 +00:00
|
|
|
msg.attr.from = channel.jid.."/"..channel:get_spid(from);
|
2020-11-02 16:07:50 +00:00
|
|
|
|
2021-02-19 16:03:17 +00:00
|
|
|
if module:fire_event("mix-broadcast-message", msg) then
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2020-10-27 17:05:41 +00:00
|
|
|
for _, p in pairs(channel.participants) do
|
2020-11-02 16:07:50 +00:00
|
|
|
-- Only users who subscribed to the messages node should receive
|
|
|
|
-- messages
|
|
|
|
if channel:is_subscribed(p.jid, mix_node_messages) then
|
|
|
|
local tmp = st.clone(msg);
|
|
|
|
tmp.attr.to = p.jid;
|
|
|
|
module:send(tmp);
|
|
|
|
end
|
2020-10-27 17:05:41 +00:00
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end);
|