end
end, -10);
+function room_mt:handle_first_presence(origin, stanza)
+ local real_jid = stanza.attr.from;
+ local dest_jid = stanza.attr.to;
+ local bare_jid = jid_bare(real_jid);
+ if module:fire_event("muc-room-pre-create", {
+ room = self;
+ origin = origin;
+ stanza = stanza;
+ }) then return true; end
+ local is_first_dest_session = true;
+ local dest_occupant = self:new_occupant(bare_jid, dest_jid);
+
+ -- TODO Handle this case sensibly
+ if not stanza:get_child("x", "http://jabber.org/protocol/muc") then
+ module:log("debug", "Room creation without <x>, possibly desynced");
+ end
+
+ if module:fire_event("muc-occupant-pre-join", {
+ room = self;
+ origin = origin;
+ stanza = stanza;
+ is_first_session = is_first_dest_session;
+ is_new_room = true;
+ occupant = dest_occupant;
+ }) then return true; end
+
+ dest_occupant:set_session(real_jid, stanza);
+ local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
+ dest_x:tag("status", {code = "201"}):up();
+ if self:get_whois() == "anyone" then
+ dest_x:tag("status", {code = "100"}):up();
+ end
+ self:save_occupant(dest_occupant);
+
+ self:publicise_occupant_status(dest_occupant, dest_x);
+
+ module:fire_event("muc-occupant-joined", {
+ room = self;
+ nick = dest_occupant.nick;
+ occupant = dest_occupant;
+ stanza = stanza;
+ origin = origin;
+ });
+ module:fire_event("muc-occupant-session-new", {
+ room = self;
+ nick = dest_occupant.nick;
+ occupant = dest_occupant;
+ stanza = stanza;
+ origin = origin;
+ jid = real_jid;
+ });
+ module:fire_event("muc-room-created", {
+ room = self;
+ creator = dest_occupant;
+ stanza = stanza;
+ origin = origin;
+ });
+ return true;
+end
+
function room_mt:handle_normal_presence(origin, stanza)
local type = stanza.attr.type;
local real_jid = stanza.attr.from;
local bare_jid = jid_bare(real_jid);
- local orig_occupant, dest_occupant;
- local is_new_room = next(self._affiliations) == nil;
- if is_new_room then
- if type == "unavailable" then return true; end -- Unavailable from someone not in the room
- if module:fire_event("muc-room-pre-create", {
- room = self;
- origin = origin;
- stanza = stanza;
- }) then return true; end
- else
- orig_occupant = self:get_occupant_by_real_jid(real_jid);
- if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
- end
+ local orig_occupant = self:get_occupant_by_real_jid(real_jid);
+ if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
local is_first_dest_session;
+ local dest_occupant;
if type == "unavailable" then -- luacheck: ignore 542
-- FIXME Why the empty if branch?
-- dest_occupant = nil
};
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";
if dest_occupant ~= nil then
dest_occupant:set_session(real_jid, stanza);
local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
- if is_new_room then
- dest_x:tag("status", {code = "201"}):up();
- end
if orig_occupant == nil and self:get_whois() == "anyone" then
dest_x:tag("status", {code = "100"}):up();
end
end
event.field, event.value = nil, nil;
- self:save();
+ self:save(true);
origin.send(st.reply(stanza));
if next(event.status_codes) then
else
success, errtype, err = nil, "cancel", "bad-request";
end
- self:save();
+ self:save(true);
if not success then
origin.send(st.error_reply(stanza, errtype, err));
else
end
end
- self:save();
+ self:save(true);
module:fire_event("muc-set-affiliation", {
room = self;
end
function room_mt:freeze(live)
- local frozen = {
+ local frozen, state = {
_jid = self.jid;
_data = self._data;
};
frozen[user] = affiliation;
end
if live then
+ state = {};
for nick, occupant in self:each_occupant() do
- frozen[nick] = {
+ state[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);
+ state[jid] = st.preserialize(presence);
end
end
+ local history = self._history;
+ if history then
+ state._last_message = st.preserialize(history[#history].stanza);
+ state._last_message_at = history[#history].timestamp;
+ end
end
- return frozen;
+ return frozen, state;
end
-function _M.restore_room(frozen)
+function _M.restore_room(frozen, state)
-- COMPAT
if frozen.jid and frozen._affiliations then
local room = _M.new_room(frozen.jid, frozen._data);
local room_jid = frozen._jid;
local room = _M.new_room(room_jid, frozen._data);
+ if state and state._last_message and state._last_message_at then
+ room._history = {
+ { stanza = st.deserialize(state._last_message),
+ timestamp = state._last_message_at, },
+ };
+ end
+
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 host:sub(1,1) ~= "_" and not resource and type(data) == "string" then
+ -- bare jid: affiliation
+ room._affiliations[jid] = data;
+ end
+ end
+ for jid, data in pairs(state or 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
+ if host == room_host and node == room_name and resource and type(data) == "table" then
-- full room jid: bare real jid and role
local bare_jid = data.bare_jid;
local occupant = occupant_lib.new(bare_jid, jid);
end
end
occupant_sessions[bare_jid] = nil;
- else
+ elseif type(data) == "table" and data.name then
-- full user jid: presence
local presence = st.deserialize(data);
local bare_jid = jid_bare(jid);