--
local select = select;
-local pairs, ipairs = pairs, ipairs;
+local pairs = pairs;
local next = next;
local setmetatable = setmetatable;
local jid_split = require "util.jid".split;
local jid_bare = require "util.jid".bare;
local jid_prep = require "util.jid".prep;
+local jid_join = require "util.jid".join;
local st = require "util.stanza";
local log = require "util.logger".init("mod_muc");
local base64 = require "util.encodings".base64;
if next_occupant_jid == nil then return nil end
return next_occupant_jid, occupant_lib.copy(raw_occupant);
end
- function room_mt:each_occupant(read_only)
+ -- FIXME Explain what 'read_only' is supposed to be
+ function room_mt:each_occupant(read_only) -- luacheck: ignore 212
return next_copied_occupant, self._occupants, nil;
end
end
-- Need to maintain _jid_nick secondary index
local old_occupant = self._occupants[id];
if old_occupant then
- for real_jid in pairs(old_occupant.sessions) do
+ for real_jid in old_occupant:each_session() do
self._jid_nick[real_jid] = nil;
end
end
function room_mt:route_to_occupant(occupant, stanza)
local to = stanza.attr.to;
- for jid, pr in occupant:each_session() do
+ for jid in occupant:each_session() do
stanza.attr.to = jid;
self:route_stanza(stanza);
end
end
-- actor is the attribute table
-local function add_item(x, affiliation, role, jid, nick, actor, reason)
+local function add_item(x, affiliation, role, jid, nick, actor_nick, actor_jid, reason)
x:tag("item", {affiliation = affiliation; role = role; jid = jid; nick = nick;})
- if actor then
- x:tag("actor", actor):up()
+ if actor_nick or actor_jid then
+ x:tag("actor", {nick = actor_nick; jid = actor_jid;}):up()
end
if reason then
x:tag("reason"):text(reason):up()
end
-- actor is (real) jid
-function room_mt:build_item_list(occupant, x, is_anonymous, nick, actor, reason)
+function room_mt:build_item_list(occupant, x, is_anonymous, nick, actor_nick, actor_jid, reason)
local affiliation = self:get_affiliation(occupant.bare_jid) or "none";
local role = occupant.role or "none";
- local actor_attr;
- if actor then
- actor_attr = {nick = select(3,jid_split(self:get_occupant_jid(actor)))};
- end
if is_anonymous then
- add_item(x, affiliation, role, nil, nick, actor_attr, reason);
+ add_item(x, affiliation, role, nil, nick, actor_nick, actor_jid, reason);
else
- if actor_attr then
- actor_attr.jid = actor;
- end
- for real_jid, session in occupant:each_session() do
- add_item(x, affiliation, role, real_jid, nick, actor_attr, reason);
+ for real_jid in occupant:each_session() do
+ add_item(x, affiliation, role, real_jid, nick, actor_nick, actor_jid, reason);
end
end
return x
-- Takes the x element that goes into the stanzas
function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason)
-- Build real jid and (optionally) occupant jid template presences
- local base_presence;
- if occupant.role ~= nil then
+ local base_presence do
-- Try to use main jid's presence
local pr = occupant:get_presence();
- if pr ~= nil then
+ if pr and (pr.attr.type ~= "unavailable" or occupant.role == nil) then
base_presence = st.clone(pr);
+ else -- user is leaving but didn't send a leave presence. make one for them
+ base_presence = st.presence {from = occupant.nick; type = "unavailable";};
end
end
- base_presence = base_presence or st.presence {from = occupant.nick; type = "unavailable";};
-- Fire event (before full_p and anon_p are created)
- module:fire_event("muc-broadcast-presence", {
+ local event = {
room = self; stanza = base_presence; x = base_x;
occupant = occupant; nick = nick; actor = actor;
reason = reason;
- });
+ }
+ module:fire_event("muc-broadcast-presence", event);
+
+ -- Allow muc-broadcast-presence listeners to change things
+ nick = event.nick;
+ actor = event.actor;
+ reason = event.reason;
- local function get_presence(is_anonymous)
- local x = st.clone(base_x);
- self:build_item_list(occupant, x, is_anonymous, nick, actor, reason);
- return st.clone(base_presence):add_child(x), x;
+ local whois = self:get_whois();
+
+ local actor_nick;
+ if actor then
+ actor_nick = select(3, jid_split(self:get_occupant_jid(actor)));
end
- local full_p, full_x = get_presence(false);
+ local full_p, full_x;
+ local function get_full_p()
+ if full_p == nil then
+ full_x = st.clone(base_x);
+ self:build_item_list(occupant, full_x, false, nick, actor_nick, actor, reason);
+ full_p = st.clone(base_presence):add_child(full_x);
+ end
+ return full_p, full_x;
+ end
- -- Create anon_p lazily
local anon_p, anon_x;
local function get_anon_p()
if anon_p == nil then
- anon_p, anon_x = get_presence(true);
+ anon_x = st.clone(base_x);
+ self:build_item_list(occupant, anon_x, true, nick, actor_nick, nil, reason);
+ anon_p = st.clone(base_presence):add_child(anon_x);
end
return anon_p, anon_x;
end
- local whois = self:get_whois();
+ local self_p, self_x;
+ if can_see_real_jids(whois, occupant) then
+ self_p, self_x = get_full_p();
+ else
+ -- Can always see your own full jids
+ -- But not allowed to see actor's
+ self_x = st.clone(base_x);
+ self:build_item_list(occupant, self_x, false, nick, actor_nick, nil, reason);
+ self_p = st.clone(base_presence):add_child(self_x);
+ end
-- General populance
- for nick, n_occupant in self:each_occupant() do
- if nick ~= occupant.nick then
+ for occupant_nick, n_occupant in self:each_occupant() do
+ if occupant_nick ~= occupant.nick then
local pr;
- if can_see_real_jids(whois, n_occupant) or occupant.bare_jid == n_occupant.bare_jid then
- pr = full_p;
+ if can_see_real_jids(whois, n_occupant) then
+ pr = get_full_p();
+ elseif occupant.bare_jid == n_occupant.bare_jid then
+ pr = self_p;
else
pr = get_anon_p();
end
end
-- Presences for occupant itself
- full_x:tag("status", {code = "110";}):up();
+ self_x:tag("status", {code = "110";}):up();
if occupant.role == nil then
-- They get an unavailable
- self:route_to_occupant(occupant, full_p);
+ self:route_to_occupant(occupant, self_p);
else
-- use their own presences as templates
for full_jid, pr in occupant:each_session() do
pr = st.clone(pr);
pr.attr.to = full_jid;
- -- You can always see your own full jids
- pr:add_child(full_x);
+ pr:add_child(self_x);
self:route_stanza(pr);
end
end
local form = dataform.new {
{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" };
};
- module:fire_event("muc-disco#info", {room = self; reply = reply; form = form;});
- reply:add_child(form:form(nil, "result"));
+ local formdata = {};
+ module:fire_event("muc-disco#info", {room = self; reply = reply; form = form, formdata = formdata ;});
+ reply:add_child(form:form(formdata, "result"));
return reply;
end
module:hook("muc-disco#info", function(event)
event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
end);
module:hook("muc-disco#info", function(event)
- local count = iterators.count(event.room:each_occupant());
- table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
+ table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants" });
+ event.formdata["muc#roominfo_occupants"] = tostring(iterators.count(event.room:each_occupant()));
end);
function room_mt:get_disco_items(stanza)
return reply;
end
-function room_mt:handle_kickable(origin, stanza)
+function room_mt:handle_kickable(origin, stanza) -- luacheck: ignore 212
local real_jid = stanza.attr.from;
local occupant = self:get_occupant_by_real_jid(real_jid);
if occupant == nil then return nil; end
local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
:tag("status", {code = "307"})
self:publicise_occupant_status(occupant, x);
+ if occupant.jid == real_jid then -- Was last session
+ module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
+ end
return true;
end
if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
end
local is_first_dest_session;
- if type == "unavailable" then
+ if type == "unavailable" then -- luacheck: ignore 542
+ -- FIXME Why the empty if branch?
-- dest_occupant = nil
elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update
log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid);
if orig_occupant == nil then
event_name = "muc-occupant-pre-join";
event.is_new_room = is_new_room;
+ event.occupant = dest_occupant;
elseif dest_occupant == nil then
event_name = "muc-occupant-pre-leave";
+ event.occupant = orig_occupant;
else
event_name = "muc-occupant-pre-change";
+ event.orig_occupant = orig_occupant;
+ event.dest_occupant = dest_occupant;
end
if module:fire_event(event_name, event) then return true; end
self:publicise_occupant_status(orig_occupant, orig_x, dest_nick);
if is_last_orig_session then
- module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick;});
+ module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick; occupant = orig_occupant;});
end
end
if orig_occupant == nil then
-- Send occupant list to newly joined user
- self:send_occupant_list(real_jid, function(nick, occupant)
+ self:send_occupant_list(real_jid, function(nick, occupant) -- luacheck: ignore 212
-- Don't include self
return occupant:get_presence(real_jid) == nil;
end)
if orig_occupant == nil then
if is_first_dest_session then
- module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; stanza = stanza;});
+ module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; occupant = dest_occupant;});
end
- module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; stanza = stanza; jid = real_jid;});
+ module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;});
end
end
elseif type ~= 'result' then -- bad type
if (type == "error" or type == "result") then
do -- deconstruct_stanza_id
if not occupant then return nil; end
- local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
+ local from_jid, orig_id, to_jid_hash = (base64.decode(id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
local from_occupant_jid = self:get_occupant_jid(from_jid);
if from_occupant_jid == nil then return nil; end
end
end
if session_jid == nil then return nil; end
- stanza.attr.from, stanza.attr.to, stanza.attr.id = from_jid, session_jid, id;
+ stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, orig_id;
end
log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
self:route_stanza(stanza);
if form.attr.type == "cancel" then
origin.send(st.reply(stanza));
elseif form.attr.type == "submit" then
- local fields;
+ local fields, errors, present;
if form.tags[1] == nil then -- Instant room
- fields = {};
+ fields, present = {}, {};
else
- fields = self:get_form_layout(stanza.attr.from):data(form);
+ fields, errors, present = self:get_form_layout(stanza.attr.from):data(form);
if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then
origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration"));
return true;
return true;
end
module:fire_event("muc-config-submitted", event);
+ for submitted_field in pairs(present) do
+ event.field, event.value = submitted_field, fields[submitted_field];
+ module:fire_event("muc-config-submitted/"..submitted_field, event);
+ end
+ event.field, event.value = nil, nil;
if self.save then self:save(true); end
origin.send(st.reply(stanza));
function room_mt:clear(x)
x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
local occupants_updated = {};
- for nick, occupant in self:each_occupant() do
+ for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
occupant.role = nil;
self:save_occupant(occupant);
occupants_updated[occupant] = true;
end
for occupant in pairs(occupants_updated) do
self:publicise_occupant_status(occupant, x);
- module:fire_event("muc-occupant-left", { room = self; nick = occupant.nick; });
+ module:fire_event("muc-occupant-left", { room = self; nick = occupant.nick; occupant = occupant;});
end
end
function room_mt:handle_admin_query_set_command(origin, stanza)
local item = stanza.tags[1].tags[1];
+ if not item then
+ origin.send(st.error_reply(stanza, "cancel", "bad-request"));
+ end
if item.attr.jid then -- Validate provided JID
item.attr.jid = jid_prep(item.attr.jid);
if not item.attr.jid then
else
success, errtype, err = nil, "cancel", "bad-request";
end
- if not success then origin.send(st.error_reply(stanza, errtype, err)); end
- origin.send(st.reply(stanza));
+ if not success then
+ origin.send(st.error_reply(stanza, errtype, err));
+ else
+ origin.send(st.reply(stanza));
+ end
return true;
end
local affiliation = self:get_affiliation(actor);
local item = stanza.tags[1].tags[1];
local _aff = item.attr.affiliation;
+ local _aff_rank = valid_affiliations[_aff or "none"];
local _rol = item.attr.role;
- if _aff and not _rol then
- if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then
+ if _aff and _aff_rank and not _rol then
+ -- You need to be at least an admin, and be requesting info about your affifiliation or lower
+ -- e.g. an admin can't ask for a list of owners
+ local affiliation_rank = valid_affiliations[affiliation or "none"];
+ if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then
local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
- for jid, affiliation in pairs(self._affiliations) do
- if affiliation == _aff then
- reply:tag("item", {affiliation = _aff, jid = jid}):up();
- end
+ for jid in self:each_affiliation(_aff or "none") do
+ reply:tag("item", {affiliation = _aff, jid = jid}):up();
end
- origin.send(reply);
+ origin.send(reply:up());
return true;
else
origin.send(st.error_reply(stanza, "auth", "forbidden"));
return true;
end
- elseif _rol and not _aff then
+ elseif _rol and valid_roles[_rol or "none"] and not _aff then
local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
if valid_roles[role or "none"] >= valid_roles.moderator then
if _rol == "none" then _rol = nil; end
end
function room_mt:handle_groupchat_to_room(origin, stanza)
- -- Prosody has made the decision that messages with <subject/> are exclusively subject changes
- -- e.g. body will be ignored; even if the subject change was not allowed
- if stanza:get_child("subject") then
- return module:fire_event("muc-subject-change", {room = self, origin = origin, stanza = stanza});
- end
local from = stanza.attr.from;
local occupant = self:get_occupant_by_real_jid(from);
- if not occupant then -- not in room
- origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
- return true;
- elseif occupant.role == "visitor" then
- origin.send(st.error_reply(stanza, "auth", "forbidden"));
- return true;
- end
+ if module:fire_event("muc-occupant-groupchat", {
+ room = self; origin = origin; stanza = stanza; from = from; occupant = occupant;
+ }) then return true; end
stanza.attr.from = occupant.nick;
self:broadcast_message(stanza);
stanza.attr.from = from;
return true;
end
+-- Role check
+module:hook("muc-occupant-groupchat", function(event)
+ local role_rank = valid_roles[event.occupant and event.occupant.role or "none"];
+ if role_rank <= valid_roles.none then
+ event.origin.send(st.error_reply(event.stanza, "cancel", "not-acceptable"));
+ return true;
+ elseif role_rank <= valid_roles.visitor then
+ event.origin.send(st.error_reply(event.stanza, "auth", "forbidden"));
+ return true;
+ end
+end, 50);
+
-- hack - some buggy clients send presence updates to the room rather than their nick
function room_mt:handle_presence_to_room(origin, stanza)
local current_nick = self:get_occupant_jid(stanza.attr.from);
-- Need visitor role or higher to invite
module:hook("muc-pre-invite", function(event)
local room, stanza = event.room, event.stanza;
- local _from, _to = stanza.attr.from, stanza.attr.to;
+ local _from = stanza.attr.from;
local inviter = room:get_occupant_by_real_jid(_from);
local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from));
if valid_roles[role or "none"] <= valid_roles.visitor then
:up()
:up();
if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then
- local declinee = decline.attr.to; -- re-fetch, in case event modified it
+ declinee = decline.attr.to; -- re-fetch, in case event modified it
local occupant
if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid
occupant = self:get_occupant_by_nick(declinee);
local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
if x then
local payload = x.tags[1];
- if payload == nil then
+ if payload == nil then --luacheck: ignore 542
-- fallthrough
elseif payload.name == "invite" and payload.attr.to then
return self:handle_mediated_invite(origin, stanza)
end
end
-function room_mt:route_stanza(stanza)
+function room_mt:route_stanza(stanza) -- luacheck: ignore 212
module:send(stanza);
end
return result;
end
+-- Iterates over jid, affiliation pairs
+function room_mt:each_affiliation(with_affiliation)
+ if not with_affiliation then
+ return pairs(self._affiliations);
+ else
+ return function(_affiliations, jid)
+ local affiliation;
+ repeat -- Iterate until we get a match
+ jid, affiliation = next(_affiliations, jid);
+ until jid == nil or affiliation == with_affiliation
+ return jid, affiliation;
+ end, self._affiliations, nil
+ end
+end
+
function room_mt:set_affiliation(actor, jid, affiliation, reason)
if not actor then return nil, "modify", "not-acceptable"; end;
- jid = jid_bare(jid);
+ local node, host, resource = jid_split(jid);
+ if not host then return nil, "modify", "not-acceptable"; end
+ jid = jid_join(node, host); -- Bare
+ local is_host_only = node == nil;
if valid_affiliations[affiliation or "none"] == nil then
return nil, "modify", "not-acceptable";
local target_affiliation = self._affiliations[jid]; -- Raw; don't want to check against host
local is_downgrade = valid_affiliations[target_affiliation or "none"] > valid_affiliations[affiliation or "none"];
- if actor ~= true then
+ if actor == true then
+ actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
+ else
local actor_affiliation = self:get_affiliation(actor);
if actor_affiliation == "owner" then
if jid_bare(actor) == jid then -- self change
-- need at least one owner
local is_last = true;
- for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end
+ for j in self:each_affiliation("owner") do
+ if j ~= jid then is_last = false; break; end
+ end
if is_last then
return nil, "cancel", "conflict";
end
local role = self:get_default_role(affiliation);
local role_rank = valid_roles[role or "none"];
local occupants_updated = {}; -- Filled with old roles
- for nick, occupant in self:each_occupant() do
- if occupant.bare_jid == jid then
+ for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
+ if occupant.bare_jid == jid or (
+ -- Outcast can be by host.
+ is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host
+ ) then
-- need to publcize in all cases; as affiliation in <item/> has changed.
occupants_updated[occupant] = occupant.role;
if occupant.role ~= role and (
local is_semi_anonymous = self:get_whois() == "moderators";
for occupant, old_role in pairs(occupants_updated) do
self:publicise_occupant_status(occupant, x, nil, actor, reason);
- if is_semi_anonymous and
+ if occupant.role == nil then
+ module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
+ elseif is_semi_anonymous and
(old_role == "moderator" and occupant.role ~= "moderator") or
(old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status
-- Send everyone else's presences (as jid visibility has changed)
for real_jid in occupant:each_session() do
- self:send_occupant_list(real_jid, function(occupant_jid, occupant)
+ self:send_occupant_list(real_jid, function(occupant_jid, occupant) --luacheck: ignore 212 433
return occupant.bare_jid ~= jid;
end);
end
end
role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
- if actor ~= true then
+ if actor == true then
+ actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
+ else
-- Can't do anything to other owners or admins
local occupant_affiliation = self:get_affiliation(occupant.bare_jid);
- if occupant_affiliation == "owner" and occupant_affiliation == "admin" then
+ if occupant_affiliation == "owner" or occupant_affiliation == "admin" then
return nil, "cancel", "not-allowed";
end
occupant.role = role;
self:save_occupant(occupant);
self:publicise_occupant_status(occupant, x, nil, actor, reason);
+ if role == nil then
+ module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
+ end
return true;
end
-local affiliation_notify = module:require "muc/affiliation_notify";
-
-local name = module:require "muc/name";
-room_mt.get_name = name.get;
-room_mt.set_name = name.set;
-
-local description = module:require "muc/description";
-room_mt.get_description = description.get;
-room_mt.set_description = description.set;
-
-local hidden = module:require "muc/hidden";
-room_mt.get_hidden = hidden.get;
-room_mt.set_hidden = hidden.set;
-function room_mt:get_public()
- return not self:get_hidden();
-end
-function room_mt:set_public(public)
- return self:set_hidden(not public);
-end
-
-local password = module:require "muc/password";
-room_mt.get_password = password.get;
-room_mt.set_password = password.set;
-
local whois = module:require "muc/whois";
room_mt.get_whois = whois.get;
room_mt.set_whois = whois.set;
-local members_only = module:require "muc/members_only";
-room_mt.get_members_only = members_only.get;
-room_mt.set_members_only = members_only.set;
-
-local moderated = module:require "muc/moderated";
-room_mt.get_moderated = moderated.get;
-room_mt.set_moderated = moderated.set;
-
-local persistent = module:require "muc/persistent";
-room_mt.get_persistent = persistent.get;
-room_mt.set_persistent = persistent.set;
-
-local subject = module:require "muc/subject";
-room_mt.get_changesubject = subject.get_changesubject;
-room_mt.set_changesubject = subject.set_changesubject;
-room_mt.get_subject = subject.get;
-room_mt.set_subject = subject.set;
-room_mt.send_subject = subject.send;
-
-local history = module:require "muc/history";
-room_mt.send_history = history.send;
-room_mt.get_historylength = history.get_length;
-room_mt.set_historylength = history.set_length;
-
local _M = {}; -- module "muc"
-function _M.new_room(jid, config)
+function _M.new_room(jid, config) -- luacheck: ignore 212
+ -- TODO use config?
return setmetatable({
jid = jid;
_jid_nick = {};