return "MUC room ("..self.jid..")";
end
+function room_mt.save()
+ -- overriden by mod_muc.lua
+end
+
function room_mt:get_occupant_jid(real_jid)
return self._jid_nick[real_jid]
end
local base_presence do
-- Try to use main jid's presence
local pr = occupant:get_presence();
- if pr and (pr.attr.type ~= "unavailable" or occupant.role == nil) then
+ if pr and (pr.attr.type ~= "unavailable" and 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";};
return true;
end
+if not module:get_option_boolean("muc_compat_create", true) then
+ module:hook("muc-room-pre-create", function(event)
+ local origin, stanza = event.origin, event.stanza;
+ if not stanza:get_child("x", "http://jabber.org/protocol/muc") then
+ origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
+ return true;
+ end
+ end, -1);
+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");
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; occupant = orig_occupant;});
+ module:fire_event("muc-occupant-left", {
+ room = self;
+ nick = orig_occupant.nick;
+ occupant = orig_occupant;
+ origin = origin;
+ stanza = stanza;
+ });
end
end
if orig_occupant == nil then
if is_first_dest_session then
- module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; occupant = dest_occupant;});
+ module:fire_event("muc-occupant-joined", {
+ room = self;
+ nick = dest_occupant.nick;
+ occupant = dest_occupant;
+ stanza = stanza;
+ origin = origin;
+ });
end
- module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;});
+ module:fire_event("muc-occupant-session-new", {
+ room = self;
+ nick = dest_occupant.nick;
+ occupant = dest_occupant;
+ stanza = stanza;
+ origin = origin;
+ jid = real_jid;
+ });
end
end
elseif type ~= 'result' then -- bad type
end
event.field, event.value = nil, nil;
- if self.save then self:save(true); end
+ self:save();
origin.send(st.reply(stanza));
if next(event.status_codes) then
else
success, errtype, err = nil, "cancel", "bad-request";
end
+ self:save();
if not success then
origin.send(st.error_reply(stanza, errtype, err));
else
end
end
- if self.save then self:save(); end
+ self:save();
module:fire_event("muc-set-affiliation", {
room = self;
if not actor then return nil, "modify", "not-acceptable"; end
local occupant = self:get_occupant_by_nick(occupant_jid);
- if not occupant then return nil, "modify", "not-acceptable"; end
+ if not occupant then return nil, "modify", "item-not-found"; end
if valid_roles[role or "none"] == nil then
return nil, "modify", "not-acceptable";
local _M = {}; -- module "muc"
-function _M.new_room(jid, config) -- luacheck: ignore 212
- -- TODO use config?
+function _M.new_room(jid, config)
return setmetatable({
jid = jid;
_jid_nick = {};
_occupants = {};
- _data = {
- };
+ _data = config or {};
_affiliations = {};
}, room_mt);
end
+function room_mt:freeze(live)
+ local frozen = {
+ _jid = self.jid;
+ _data = self._data;
+ };
+ for user, affiliation in pairs(self._affiliations) do
+ frozen[user] = affiliation;
+ end
+ if live then
+ for nick, occupant in self:each_occupant() do
+ frozen[nick] = {
+ bare_jid = occupant.bare_jid;
+ role = occupant.role;
+ jid = occupant.jid;
+ }
+ for jid, presence in occupant:each_session() do
+ frozen[jid] = st.preserialize(presence);
+ end
+ end
+ end
+ return frozen;
+end
+
+function _M.restore_room(frozen)
+ -- COMPAT
+ if frozen.jid and frozen._affiliations then
+ local room = _M.new_room(frozen.jid, frozen._data);
+ room._affiliations = frozen._affiliations;
+ return room;
+ end
+
+ local room_jid = frozen._jid;
+ local room = _M.new_room(room_jid, frozen._data);
+
+ local occupants = {};
+ local occupant_sessions = {};
+ local room_name, room_host = jid_split(room_jid);
+ for jid, data in pairs(frozen) do
+ local node, host, resource = jid_split(jid);
+ if node or host:sub(1,1) ~= "_" then
+ if not resource and type(data) == "string" then
+ -- bare jid: affiliation
+ room._affiliations[jid] = data;
+ elseif host == room_host and node == room_name and resource then
+ -- full room jid: bare real jid and role
+ local bare_jid = data.bare_jid;
+ local occupant = occupant_lib.new(bare_jid, jid);
+ occupant.jid = data.jid;
+ occupant.role = data.role;
+ occupants[bare_jid] = occupant;
+ local sessions = occupant_sessions[bare_jid];
+ if sessions then
+ for full_jid, presence in pairs(sessions) do
+ occupant:set_session(full_jid, presence);
+ end
+ end
+ occupant_sessions[bare_jid] = nil;
+ else
+ -- full user jid: presence
+ local presence = st.deserialize(data);
+ local bare_jid = jid_bare(jid);
+ local occupant = occupants[bare_jid];
+ local sessions = occupant_sessions[bare_jid];
+ if occupant then
+ occupant:set_session(jid, presence);
+ elseif sessions then
+ sessions[jid] = presence;
+ else
+ occupant_sessions[bare_jid] = {
+ [jid] = presence;
+ };
+ end
+ end
+ end
+ end
+
+ for _, occupant in pairs(occupants) do
+ room:save_occupant(occupant);
+ end
+
+ return room;
+end
+
_M.room_mt = room_mt;
return _M;