mod_mix: Make the linter happy

This commit is contained in:
PapaTutuWawa 2021-02-21 18:38:21 +01:00
parent dc9d388ec3
commit b0aca926fe
3 changed files with 44 additions and 44 deletions

View File

@ -1,5 +1,5 @@
-- Helper functions for mod_mix
function find(array, f)
local function find(array, f)
-- Searches for an element for which f returns true. The first element
-- and its index are returned. If none are found, then -1, nil is returned.
--
@ -11,9 +11,9 @@ function find(array, f)
return -1, nil;
end
function find_str(array, str)
local function find_str(array, str)
-- Returns the index of str in array. -1 if array does not contain str
local i, v = find(array, function(v) return v == str end);
local i, _ = find(array, function(v) return v == str end);
return i;
end

View File

@ -71,17 +71,17 @@ function Channel:debug_print()
for _, p in pairs(self.participants) do
module:log("debug", "=> %s (%s)", p.jid, p.nick);
end
module:log("debug", "Contacts:");
for _, c in pairs(self.contacts) do
module:log("debug", "=> %s", c);
end
if self.subscriptions then
module:log("debug", "Subscriptions:");
for user, subs in pairs(self.subscriptions) do
module:log("debug", "[%s]", user);
for _, sub in pairs(self.subscriptions[user]) do
for _, sub in pairs(subs) do
module:log("debug", "=> %s", sub);
end
end

View File

@ -28,9 +28,9 @@ local mix_anon_xmlns = "urn:xmpp:mix:anon:0";
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";
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";
--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";
local mam_xmlns = "urn:xmpp:mam:2";
@ -88,13 +88,13 @@ module:add_feature(mix_core_xmlns);
local channels = {};
function get_channel(jid)
local function get_channel(channel_jid)
-- Return the channel object from the channels array for which the
-- JID matches. If none is found, returns -1, nil
return helpers.find(channels, function(c) return c.jid == jid; end);
return helpers.find(channels, function(c) return c.jid == channel_jid; end);
end
function save_channels()
local function save_channels()
module:log("debug", "Saving channel list...");
local channel_list = {};
for _, channel in pairs(channels) do
@ -115,7 +115,7 @@ function Channel:save_state()
module:log("debug", "Saving state done.", self.jid);
end
function publish_participant(service, channel, spid, participant)
local function publish_participant(service, channel, spid, participant)
-- Publish a new participant on the service
-- NOTE: This function has be to called *after* the new participant
-- has been added to the channel.participants attay
@ -133,11 +133,11 @@ end
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
local channel = Channel:from(persistent_channel_data:get(channel));
for _, channel_data in pairs(channel_list) do
local channel = Channel:from(persistent_channel_data:get(channel_data));
table.insert(channels, channel);
module:log("debug", "MIX channel %s loaded", channel.jid);
end
@ -147,7 +147,7 @@ function module.load()
module:log("info", "Loading MIX channels done.");
end
function channel_not_found(stanza)
local function channel_not_found(stanza)
-- Wrapper for returning a "Channel-not-found" error stanza
return st.error_reply(stanza,
"cancel",
@ -163,7 +163,7 @@ module:hook("host-disco-items", function(event)
if not channel.adhoc then
reply:tag("item", { jid = channel.jid }):up();
end
end
end
end);
module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
@ -198,14 +198,14 @@ module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(eve
return true;
end);
function can_create_channels(user)
local function can_create_channels(user)
-- Returns true when the jid is allowed to create MIX channels. False otherwise.
-- NOTE: Taken from plugins/muc/mod_muc.lua
local host_suffix = host:gsub("^[^%.]+%.", "");
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
@ -229,7 +229,7 @@ end
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" })
@ -281,14 +281,15 @@ module:hook("iq-set/bare/"..mam_xmlns..":query", function(event)
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
-- TODO: Error handling
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"));
@ -311,9 +312,9 @@ module:hook("iq-set/bare/"..mam_xmlns..":query", function(event)
end
return true;
end
for id, item, when in data do
for message_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("result", { xmlns = mam_xmlns, queryid = query_id, id = message_id })
:tag("forwarded", { xmlns = "urn:xmpp:forward:0" })
:tag("delay", { xmlns = "urn:xmpp:delay", stamp = datetime.datetime(when) }):up();
msg:add_child(item);
@ -324,7 +325,7 @@ 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)
@ -343,7 +344,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":leave", function(event)
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);
local _, channel = get_channel(stanza.attr.to);
if not channel then
origin.send(channel_not_found(stanza));
return true;
@ -384,12 +385,12 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":join", function(event)
local origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from);
local i, channel = get_channel(stanza.attr.to);
local _, channel = get_channel(stanza.attr.to);
if not channel then
origin.send(channel_not_found(stanza));
return true;
end
-- Prevent the user from joining multiple times
local j, _ = channel:find_participant(from);
if j ~= -1 then
@ -404,13 +405,13 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":join", function(event)
local join = stanza:get_child("join", mix_core_xmlns);
local nick_tag = join:get_child("nick");
local nick;
if not join:get_child("nick") then
if not nick_tag then
nick = jid.node(from);
else
nick = join:get_child("nick"):get_text();
nick = nick_tag:get_text();
end
module:log("debug", "User joining as nick %s", nick);
local nodes = {};
local has_subscribed_once = false;
local first_error = nil;
@ -492,7 +493,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":setnick", function(event)
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);
local _, channel = get_channel(stanza.attr.to);
if not channel then
origin.send(channel_not_found(stanza));
return true;
@ -504,7 +505,7 @@ module:hook("iq-set/bare/"..mix_core_xmlns..":setnick", function(event)
module:log("debug", "%s is not a participant in %s", from, channel.jid);
return true;
end
-- 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");
@ -537,12 +538,12 @@ function Channel:publish_info(srv)
:tag("value"):text(self.name):up()
:tag("field", { var = "Contact" });
for _, contact in pairs(self.contacts) do
info:add_child(st.stanza("value"):text(contact));
info:add_child(st.stanza("value"):text(contact));
end
srv:publish(mix_node_info, true, timestamp, info);
end
function create_channel(node, creator, adhoc)
local function create_channel(node, creator, adhoc)
local channel = Channel:new(string.format("%s@%s", node, host),
default_channel_name,
default_channel_description,
@ -553,7 +554,6 @@ function create_channel(node, creator, adhoc)
adhoc);
-- Create the PEP nodes
local srv = pep.get_pep_service(node);
local timestamp = datetime.datetime(time.now());
-- MIX-CORE
for _, psnode in pairs({ mix_node_info, mix_node_participants, mix_node_messages }) do
srv:create(psnode, true, {
@ -562,7 +562,7 @@ function create_channel(node, creator, adhoc)
});
end
channel:publish_info(srv);
--[[
-- MIX-ADMIN
local admin_nodes = { mix_node_banned, mix_node_config };
@ -621,7 +621,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":create", function(event)
break;
end
end
create_channel(node, from, true);
end
module:log("debug", "Channel %s created with %s as owner", node, from);
@ -637,8 +637,8 @@ module:hook("iq-set/host/"..mix_core_xmlns..":destroy", function(event)
local origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from);
local destroy = stanza:get_child("create", mix_core_xmlns);
local node = destory.attr.channel;
local destroy = stanza:get_child("destroy", mix_core_xmlns);
local node = destroy.attr.channel;
local node_jid = string.format("%s@%s", node, host);
local i, channel = get_channel(node_jid);
if not channel then
@ -657,7 +657,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":destroy", function(event)
srv:delete(pep_node, true);
end
table.remove(channels, i);
module:log("debug", "Channel %s destroyed", node);
origin.send(st.reply(stanza));
@ -674,7 +674,7 @@ module:hook("message/bare", function(event)
end
local from = jid.bare(stanza.attr.from);
local i, channel = get_channel(stanza.attr.to);
local _, channel = get_channel(stanza.attr.to);
if not channel then
origin.send(channel_not_found(stanza));
return true;
@ -690,7 +690,7 @@ module:hook("message/bare", function(event)
msg:add_child(st.stanza("mix", { xmlns = mix_core_xmlns })
:tag("nick"):text(participant.nick):up()
:tag("jid"):text(participant.jid):up());
-- Put the message into the archive
local mam_id = uuid.generate();
msg.attr.id = mam_id;