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 -- 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 -- 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. -- 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; return -1, nil;
end 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 -- 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; return i;
end end

View File

@ -71,17 +71,17 @@ function Channel:debug_print()
for _, p in pairs(self.participants) do for _, p in pairs(self.participants) do
module:log("debug", "=> %s (%s)", p.jid, p.nick); module:log("debug", "=> %s (%s)", p.jid, p.nick);
end end
module:log("debug", "Contacts:"); module:log("debug", "Contacts:");
for _, c in pairs(self.contacts) do for _, c in pairs(self.contacts) do
module:log("debug", "=> %s", c); module:log("debug", "=> %s", c);
end end
if self.subscriptions then if self.subscriptions then
module:log("debug", "Subscriptions:"); module:log("debug", "Subscriptions:");
for user, subs in pairs(self.subscriptions) do for user, subs in pairs(self.subscriptions) do
module:log("debug", "[%s]", user); module:log("debug", "[%s]", user);
for _, sub in pairs(self.subscriptions[user]) do for _, sub in pairs(subs) do
module:log("debug", "=> %s", sub); module:log("debug", "=> %s", sub);
end end
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_messages = "urn:xmpp:mix:nodes:messages";
local mix_node_participants = "urn:xmpp:mix:nodes:participants"; local mix_node_participants = "urn:xmpp:mix:nodes:participants";
local mix_node_info = "urn:xmpp:mix:nodes:info"; local mix_node_info = "urn:xmpp:mix:nodes:info";
local mix_node_allowed = "urn:xmpp:mix:nodes:allowed"; --local mix_node_allowed = "urn:xmpp:mix:nodes:allowed";
local mix_node_banned = "urn:xmpp:mix:nodes:banned"; --local mix_node_banned = "urn:xmpp:mix:nodes:banned";
local mix_node_config = "urn:xmpp:mix:nodes:config"; --local mix_node_config = "urn:xmpp:mix:nodes:config";
local mam_xmlns = "urn:xmpp:mam:2"; local mam_xmlns = "urn:xmpp:mam:2";
@ -88,13 +88,13 @@ module:add_feature(mix_core_xmlns);
local channels = {}; local channels = {};
function get_channel(jid) local function get_channel(channel_jid)
-- Return the channel object from the channels array for which the -- Return the channel object from the channels array for which the
-- JID matches. If none is found, returns -1, nil -- 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 end
function save_channels() local function save_channels()
module:log("debug", "Saving channel list..."); module:log("debug", "Saving channel list...");
local channel_list = {}; local channel_list = {};
for _, channel in pairs(channels) do for _, channel in pairs(channels) do
@ -115,7 +115,7 @@ function Channel:save_state()
module:log("debug", "Saving state done.", self.jid); module:log("debug", "Saving state done.", self.jid);
end end
function publish_participant(service, channel, spid, participant) local function publish_participant(service, channel, spid, participant)
-- Publish a new participant on the service -- Publish a new participant on the service
-- NOTE: This function has be to called *after* the new participant -- NOTE: This function has be to called *after* the new participant
-- has been added to the channel.participants attay -- has been added to the channel.participants attay
@ -133,11 +133,11 @@ end
function module.load() function module.load()
module:log("info", "Loading MIX channels..."); module:log("info", "Loading MIX channels...");
local channel_list = persistent_channels:get("channels"); local channel_list = persistent_channels:get("channels");
if channel_list then if channel_list then
for _, channel in pairs(channel_list) do for _, channel_data in pairs(channel_list) do
local channel = Channel:from(persistent_channel_data:get(channel)); local channel = Channel:from(persistent_channel_data:get(channel_data));
table.insert(channels, channel); table.insert(channels, channel);
module:log("debug", "MIX channel %s loaded", channel.jid); module:log("debug", "MIX channel %s loaded", channel.jid);
end end
@ -147,7 +147,7 @@ function module.load()
module:log("info", "Loading MIX channels done."); module:log("info", "Loading MIX channels done.");
end end
function channel_not_found(stanza) local function channel_not_found(stanza)
-- Wrapper for returning a "Channel-not-found" error stanza -- Wrapper for returning a "Channel-not-found" error stanza
return st.error_reply(stanza, return st.error_reply(stanza,
"cancel", "cancel",
@ -163,7 +163,7 @@ module:hook("host-disco-items", function(event)
if not channel.adhoc then if not channel.adhoc then
reply:tag("item", { jid = channel.jid }):up(); reply:tag("item", { jid = channel.jid }):up();
end end
end end
end); end);
module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event) 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; return true;
end); 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. -- Returns true when the jid is allowed to create MIX channels. False otherwise.
-- NOTE: Taken from plugins/muc/mod_muc.lua -- NOTE: Taken from plugins/muc/mod_muc.lua
local host_suffix = host:gsub("^[^%.]+%.", ""); local host_suffix = host:gsub("^[^%.]+%.", "");
if restrict_channel_creation == "local" then if restrict_channel_creation == "local" then
module:log("debug", "Comparing %s (Sender) to %s (Host)", jid.host(user), host_suffix); module:log("debug", "Comparing %s (Sender) to %s (Host)", jid.host(user), host_suffix);
if jid.host(user) == host_suffix then if jid.host(user) == host_suffix then
return true; return true;
else else
@ -229,7 +229,7 @@ end
module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event) module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event)
module:log("debug", "IQ-GET host disco#info"); module:log("debug", "IQ-GET host disco#info");
local origin, stanza = event.origin, event.stanza; local origin, stanza = event.origin, event.stanza;
local reply = st.reply(stanza) local reply = st.reply(stanza)
:tag("query", { xmlns = "http://jabber.org/protocol/disco#info" }) :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")); origin.send(st.error_reply(stanza, "cancel", "forbidden"));
return true; return true;
end end
local query = stanza:get_child("query", mam_xmlns); local query = stanza:get_child("query", mam_xmlns);
local filter = {}; local filter = {};
local query_id = query.attr.queryid; local query_id = query.attr.queryid;
local x = query:get_child("x", "jabber:x:data"); local x = query:get_child("x", "jabber:x:data");
if x ~= nil then if x ~= nil then
-- TODO: Error handling
local form, err = mam_query_form:data(x); local form, err = mam_query_form:data(x);
-- Validate -- Validate
if (form["start"] and not form["end"]) or (not form["start"] and form["end"]) then 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")); 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 end
return true; return true;
end 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" }) 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("forwarded", { xmlns = "urn:xmpp:forward:0" })
:tag("delay", { xmlns = "urn:xmpp:delay", stamp = datetime.datetime(when) }):up(); :tag("delay", { xmlns = "urn:xmpp:delay", stamp = datetime.datetime(when) }):up();
msg:add_child(item); msg:add_child(item);
@ -324,7 +325,7 @@ end);
module:hook("iq-get/bare/"..mam_xmlns..":query", function(event) module:hook("iq-get/bare/"..mam_xmlns..":query", function(event)
if event.stanza.attr.id ~= "form1" then return; end if event.stanza.attr.id ~= "form1" then return; end
module:log("debug", "Got a MAM query for supported fields"); module:log("debug", "Got a MAM query for supported fields");
local ret = st.reply(event.stanza) 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"); module:log("debug", "MIX leave received");
local origin, stanza = event.origin, event.stanza; local origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from); 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 if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return true; 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 origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from); 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 if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return true; return true;
end end
-- Prevent the user from joining multiple times -- Prevent the user from joining multiple times
local j, _ = channel:find_participant(from); local j, _ = channel:find_participant(from);
if j ~= -1 then 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 join = stanza:get_child("join", mix_core_xmlns);
local nick_tag = join:get_child("nick"); local nick_tag = join:get_child("nick");
local nick; local nick;
if not join:get_child("nick") then if not nick_tag then
nick = jid.node(from); nick = jid.node(from);
else else
nick = join:get_child("nick"):get_text(); nick = nick_tag:get_text();
end end
module:log("debug", "User joining as nick %s", nick); module:log("debug", "User joining as nick %s", nick);
local nodes = {}; local nodes = {};
local has_subscribed_once = false; local has_subscribed_once = false;
local first_error = nil; 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"); module:log("debug", "MIX setnick received");
local origin, stanza = event.origin, event.stanza; local origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from); 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 if not channel then
origin.send(channel_not_found(stanza)); origin.send(channel_not_found(stanza));
return true; 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); module:log("debug", "%s is not a participant in %s", from, channel.jid);
return true; return true;
end end
-- NOTE: Prosody should guarantee us that the setnick stanza exists -- NOTE: Prosody should guarantee us that the setnick stanza exists
local setnick = stanza:get_child("setnick", mix_core_xmlns); local setnick = stanza:get_child("setnick", mix_core_xmlns);
local nick = setnick:get_child("nick"); local nick = setnick:get_child("nick");
@ -537,12 +538,12 @@ function Channel:publish_info(srv)
:tag("value"):text(self.name):up() :tag("value"):text(self.name):up()
:tag("field", { var = "Contact" }); :tag("field", { var = "Contact" });
for _, contact in pairs(self.contacts) do for _, contact in pairs(self.contacts) do
info:add_child(st.stanza("value"):text(contact)); info:add_child(st.stanza("value"):text(contact));
end end
srv:publish(mix_node_info, true, timestamp, info); srv:publish(mix_node_info, true, timestamp, info);
end end
function create_channel(node, creator, adhoc) local function create_channel(node, creator, adhoc)
local channel = Channel:new(string.format("%s@%s", node, host), local channel = Channel:new(string.format("%s@%s", node, host),
default_channel_name, default_channel_name,
default_channel_description, default_channel_description,
@ -553,7 +554,6 @@ function create_channel(node, creator, adhoc)
adhoc); 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());
-- MIX-CORE -- MIX-CORE
for _, psnode in pairs({ mix_node_info, mix_node_participants, mix_node_messages }) do for _, psnode in pairs({ mix_node_info, mix_node_participants, mix_node_messages }) do
srv:create(psnode, true, { srv:create(psnode, true, {
@ -562,7 +562,7 @@ function create_channel(node, creator, adhoc)
}); });
end end
channel:publish_info(srv); channel:publish_info(srv);
--[[ --[[
-- MIX-ADMIN -- MIX-ADMIN
local admin_nodes = { mix_node_banned, mix_node_config }; 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; break;
end end
end end
create_channel(node, from, true); create_channel(node, from, true);
end end
module:log("debug", "Channel %s created with %s as owner", node, from); 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 origin, stanza = event.origin, event.stanza;
local from = jid.bare(stanza.attr.from); local from = jid.bare(stanza.attr.from);
local destroy = stanza:get_child("create", mix_core_xmlns); local destroy = stanza:get_child("destroy", mix_core_xmlns);
local node = destory.attr.channel; local node = destroy.attr.channel;
local node_jid = string.format("%s@%s", node, host); local node_jid = string.format("%s@%s", node, host);
local i, channel = get_channel(node_jid); local i, channel = get_channel(node_jid);
if not channel then if not channel then
@ -657,7 +657,7 @@ module:hook("iq-set/host/"..mix_core_xmlns..":destroy", function(event)
srv:delete(pep_node, true); srv:delete(pep_node, true);
end end
table.remove(channels, i); table.remove(channels, i);
module:log("debug", "Channel %s destroyed", node); module:log("debug", "Channel %s destroyed", node);
origin.send(st.reply(stanza)); origin.send(st.reply(stanza));
@ -674,7 +674,7 @@ module:hook("message/bare", function(event)
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 _, 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 true; return true;
@ -690,7 +690,7 @@ module:hook("message/bare", function(event)
msg:add_child(st.stanza("mix", { xmlns = mix_core_xmlns }) msg:add_child(st.stanza("mix", { xmlns = mix_core_xmlns })
:tag("nick"):text(participant.nick):up() :tag("nick"):text(participant.nick):up()
:tag("jid"):text(participant.jid):up()); :tag("jid"):text(participant.jid):up());
-- Put the message into the archive -- Put the message into the archive
local mam_id = uuid.generate(); local mam_id = uuid.generate();
msg.attr.id = mam_id; msg.attr.id = mam_id;