module:hook("muc-get-default-role", function(event)
if event.affiliation_rank >= valid_affiliations.admin then
return "moderator";
- elseif event.affiliation_rank >= valid_affiliations.member then
+ elseif event.affiliation_rank >= valid_affiliations.none then
return "participant";
end
end);
-module:hook("muc-get-default-role", function(event)
- if not event.affiliation then
- return event.room:get_moderated() and "visitor" or "participant";
- end
-end, -1);
--- Occupant functions
function room_mt:new_occupant(bare_real_jid, nick)
end
end
+function room_mt:has_occupant()
+ return next(self._occupants, nil) ~= nil
+end
+
function room_mt:get_occupant_by_real_jid(real_jid)
local occupant_jid = self:get_occupant_jid(real_jid);
if occupant_jid == nil then return nil end
self._jid_nick[real_jid] = nil;
end
end
- if occupant.role ~= nil and next(occupant.sessions) then
+
+ local has_live_session = false
+ if occupant.role ~= nil then
for real_jid, presence in occupant:each_session() do
- self._jid_nick[real_jid] = occupant.nick;
+ if presence.attr.type == nil then
+ has_live_session = true
+ self._jid_nick[real_jid] = occupant.nick;
+ end
end
- else
+ if not has_live_session then
+ -- Has no live sessions left; they have left the room.
+ occupant.role = nil
+ end
+ end
+ if not has_live_session then
occupant = nil
end
self._occupants[id] = occupant
function room_mt:route_to_occupant(occupant, stanza)
local to = stanza.attr.to;
for jid, pr in occupant:each_session() do
- if pr.attr.type ~= "unavailable" then
- stanza.attr.to = jid;
- self:route_stanza(stanza);
- end
+ stanza.attr.to = jid;
+ self:route_stanza(stanza);
end
stanza.attr.to = to;
end
return get_base_presence(occupant):add_child(x), x;
end
local full_p, full_x = get_presence(false);
+
+ module:fire_event("muc-broadcast-presence", {room = self; stanza = full_p; x = full_x;});
+
local anon_p, anon_x;
local function get_anon_p()
if anon_p == nil then
for nick, n_occupant in self:each_occupant() do
if nick ~= occupant.nick then
local pr;
- if can_see_real_jids(whois, occupant) or occupant.bare_jid == n_occupant.bare_jid then
+ if can_see_real_jids(whois, n_occupant) or occupant.bare_jid == n_occupant.bare_jid then
pr = full_p;
else
pr = get_anon_p();
function room_mt:send_occupant_list(to, filter)
local to_bare = jid_bare(to);
- local is_anonymous = true;
+ local is_anonymous = false;
local whois = self:get_whois();
if whois ~= "anyone" then
local affiliation = self:get_affiliation(to);
if affiliation ~= "admin" and affiliation ~= "owner" then
local occupant = self:get_occupant_by_real_jid(to);
- if not occupant or can_see_real_jids(whois, occupant) then
- is_anonymous = false;
+ if not (occupant and can_see_real_jids(whois, occupant)) then
+ is_anonymous = true;
end
end
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)
- event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up();
-end);
-module:hook("muc-disco#info", function(event)
- event.reply:tag("feature", {var = event.room:get_hidden() and "muc_hidden" or "muc_public"}):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) });
return reply;
end
-function room_mt:get_subject()
- return self._data['subject'], self._data['subject_from']
-end
-local function create_subject_message(from, subject)
- return st.message({from = from; type = "groupchat"})
- :tag('subject'):text(subject):up();
-end
-function room_mt:send_subject(to)
- local msg = create_subject_message(self:get_subject());
- msg.attr.to = to;
- self:route_stanza(msg);
-end
-function room_mt:set_subject(current_nick, subject)
- if subject == "" then subject = nil; end
- self._data['subject'] = subject;
- self._data['subject_from'] = current_nick;
- if self.save then self:save(); end
- local msg = create_subject_message(current_nick, subject);
- self:broadcast_message(msg);
- return true;
-end
-
function room_mt:handle_kickable(origin, stanza)
local real_jid = stanza.attr.from;
local occupant = self:get_occupant_by_real_jid(real_jid);
return true;
end
-function room_mt:set_moderated(moderated)
- moderated = moderated and true or nil;
- if self._data.moderated ~= moderated then
- self._data.moderated = moderated;
- if self.save then self:save(true); end
- end
-end
-function room_mt:get_moderated()
- return self._data.moderated;
-end
-function room_mt:set_hidden(hidden)
- hidden = hidden and true or nil;
- if self._data.hidden ~= hidden then
- self._data.hidden = hidden;
- if self.save then self:save(true); end
- end
-end
-function room_mt:get_hidden()
- return self._data.hidden;
-end
-function room_mt:get_public()
- return not self:get_hidden();
-end
-function room_mt:set_public(public)
- return self:set_hidden(not public);
-end
-function room_mt:set_changesubject(changesubject)
- changesubject = changesubject and true or nil;
- if self._data.changesubject ~= changesubject then
- self._data.changesubject = changesubject;
- if self.save then self:save(true); end
- end
-end
-function room_mt:get_changesubject()
- return self._data.changesubject;
-end
-
-- Give the room creator owner affiliation
module:hook("muc-room-pre-create", function(event)
event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
end, -1);
-
-- check if user is banned
module:hook("muc-occupant-pre-join", function(event)
local room, stanza = event.room, event.stanza;
end
end, -10);
--- Send occupant list to newly joined user
-module:hook("muc-occupant-joined", function(event)
- local real_jid = event.stanza.attr.from;
- event.room:send_occupant_list(real_jid, function(nick, occupant)
- -- Don't include self
- return occupant:get_presence(real_jid) == nil;
- end);
-end, 80);
-
--- Send subject to joining user
-module:hook("muc-occupant-joined", function(event)
- event.room:send_subject(event.stanza.attr.from);
-end, 20);
-
function room_mt:handle_presence_to_occupant(origin, stanza)
local type = stanza.attr.type;
if type == "error" then -- error, kick em out!
local dest_nick;
if dest_occupant == nil then -- Session is leaving
log("debug", "session %s is leaving occupant %s", real_jid, orig_occupant.nick);
+ if is_last_orig_session then
+ orig_occupant.role = nil;
+ end
orig_occupant:set_session(real_jid, stanza);
else
log("debug", "session %s is changing from occupant %s to %s", real_jid, orig_occupant.nick, dest_occupant.nick);
dest_x:tag("status", {code = "100"}):up();
end
self:save_occupant(dest_occupant);
+
+ if orig_occupant == nil then
+ -- Send occupant list to newly joined user
+ self:send_occupant_list(real_jid, function(nick, occupant)
+ -- Don't include self
+ return occupant:get_presence(real_jid) == nil;
+ end)
+ end
self:publicise_occupant_status(dest_occupant, dest_x);
if orig_occupant ~= nil and orig_occupant ~= dest_occupant and not is_last_orig_session then -- If user is swapping and wasn't last original session
});
return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
end
-module:hook("muc-config-form", function(event)
- table.insert(event.form, {
- name = 'muc#roomconfig_publicroom',
- type = 'boolean',
- label = 'Make Room Publicly Searchable?',
- value = not event.room:get_hidden()
- });
-end);
-module:hook("muc-config-form", function(event)
- table.insert(event.form, {
- name = 'muc#roomconfig_changesubject',
- type = 'boolean',
- label = 'Allow Occupants to Change Subject?',
- value = event.room:get_changesubject()
- });
-end);
-module:hook("muc-config-form", function(event)
- table.insert(event.form, {
- name = 'muc#roomconfig_moderatedroom',
- type = 'boolean',
- label = 'Make Room Moderated?',
- value = event.room:get_moderated()
- });
-end);
function room_mt:process_form(origin, stanza)
local form = stanza.tags[1]:get_child("x", "jabber:x:data");
end
return true;
end
-module:hook("muc-config-submitted", function(event)
- event.update_option("moderated", "muc#roomconfig_moderatedroom");
-end);
-module:hook("muc-config-submitted", function(event)
- event.update_option("public", "muc#roomconfig_publicroom");
-end);
-module:hook("muc-config-submitted", function(event)
- event.update_option("changesubject", "muc#roomconfig_changesubject");
-end);
-- Removes everyone from the room
function room_mt:clear(x)
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
elseif occupant.role == "visitor" then
origin.send(st.error_reply(stanza, "auth", "forbidden"));
return true;
- else
- local from = stanza.attr.from;
- stanza.attr.from = occupant.nick;
- local subject = stanza:get_child_text("subject");
- if subject then
- if occupant.role == "moderator" or
- ( self:get_changesubject() and occupant.role == "participant" ) then -- and participant
- self:set_subject(occupant.nick, subject);
- else
- stanza.attr.from = from;
- origin.send(st.error_reply(stanza, "auth", "forbidden"));
- end
- else
- self:broadcast_message(stanza);
- end
- stanza.attr.from = from;
- return true;
end
+ stanza.attr.from = occupant.nick;
+ self:broadcast_message(stanza);
+ stanza.attr.from = from;
+ return true;
end
-- hack - some buggy clients send presence updates to the room rather than their nick
if not invitee then
origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
return true;
- elseif not module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then
+ elseif module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then
return true;
end
- local invite = st.message({from = self.jid, to = invitee, id = stanza.attr.id})
- :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
+ local invite = muc_util.filter_muc_x(st.clone(stanza));
+ invite.attr.from = self.jid;
+ invite.attr.to = invitee;
+ invite:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
:tag('invite', {from = stanza.attr.from;})
:tag('reason'):text(payload:get_child_text("reason")):up()
:up()
-- Add a plain message for clients which don't support invites
module:hook("muc-invite", function(event)
local room, stanza = event.room, event.stanza;
- local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
- local reason = invite:get_child_text("reason") or "";
- stanza:tag("body")
- :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
- :up();
+ if not stanza:get_child("body") then
+ local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
+ local reason = invite:get_child_text("reason") or "";
+ stanza:tag("body")
+ :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
+ :up();
+ end
end);
function room_mt:handle_mediated_decline(origin, stanza)
if not declinee then
origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
return true;
- elseif not module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then
+ elseif module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then
return true;
end
- local decline = st.message({from = self.jid, to = declinee, id = stanza.attr.id})
- :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+ local decline = muc_util.filter_muc_x(st.clone(stanza));
+ decline.attr.from = self.jid;
+ decline.attr.to = declinee;
+ decline:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
:tag("decline", {from = stanza.attr.from})
:tag("reason"):text(payload:get_child_text("reason")):up()
:up()
-- Add a plain message for clients which don't support declines
module:hook("muc-decline", function(event)
local room, stanza = event.room, event.stanza;
- local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
- local reason = decline:get_child_text("reason") or "";
- stanza:tag("body")
- :text(decline.attr.from.." declined your invite to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
- :up();
+ if not stanza:get_child("body") then
+ local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
+ local reason = decline:get_child_text("reason") or "";
+ stanza:tag("body")
+ :text(decline.attr.from.." declined your invite to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
+ :up();
+ end
end);
function room_mt:handle_message_to_room(origin, stanza)
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;
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;
local _M = {}; -- module "muc"
-_M.set_max_history_length = history.set_max_length;
-
function _M.new_room(jid, config)
return setmetatable({
jid = jid;