X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=plugins%2Fmuc%2Fmuc.lib.lua;h=fea0ba93c97063f73317af439b49c9aaab65439f;hb=f9e3cb3d5580b9a66b122da97c3507b57e374825;hp=69d34e75415e22cb964ec4450ffca10eec5c53ac;hpb=155b53a0ca5b6c7ed484a142bb4e9fed1a2636cd;p=prosody.git diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index 69d34e75..fea0ba93 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -36,6 +36,10 @@ function room_mt:__tostring() 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 @@ -192,7 +196,7 @@ function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason 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";}; @@ -353,6 +357,16 @@ function room_mt:handle_kickable(origin, stanza) -- luacheck: ignore 212 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"); @@ -370,170 +384,249 @@ module:hook("muc-occupant-pre-join", function(event) end end, -10); -function room_mt:handle_presence_to_occupant(origin, stanza) - local type = stanza.attr.type; - if type == "error" then -- error, kick em out! - return self:handle_kickable(origin, stanza) - elseif type == nil or type == "unavailable" then - 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 is_first_dest_session; - 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); - dest_occupant = orig_occupant; - else - local dest_jid = stanza.attr.to; - dest_occupant = self:get_occupant_by_nick(dest_jid); - if dest_occupant == nil then - log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid); - is_first_dest_session = true; - dest_occupant = self:new_occupant(bare_jid, dest_jid); - else - is_first_dest_session = false; - end - end - local is_last_orig_session; - if orig_occupant ~= nil then - -- Is there are least 2 sessions? - local iter, ob, last = orig_occupant:each_session(); - is_last_orig_session = iter(ob, iter(ob, last)) == nil; - end - - local event, event_name = { +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; - is_first_session = is_first_dest_session; - is_last_session = is_last_orig_session; - }; - 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; + }) 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 , 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 = 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 + 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); + dest_occupant = orig_occupant; + else + local dest_jid = stanza.attr.to; + dest_occupant = self:get_occupant_by_nick(dest_jid); + if dest_occupant == nil then + log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid); + is_first_dest_session = true; + dest_occupant = self:new_occupant(bare_jid, dest_jid); 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 - - -- Check for nick conflicts - if dest_occupant ~= nil and not is_first_dest_session and bare_jid ~= jid_bare(dest_occupant.bare_jid) then -- new nick or has different bare real jid - log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick); - local reply = st.error_reply(stanza, "cancel", "conflict"):up(); - reply.tags[1].attr.code = "409"; - origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); - return true; + is_first_dest_session = false; end + end + local is_last_orig_session; + if orig_occupant ~= nil then + -- Is there are least 2 sessions? + local iter, ob, last = orig_occupant:each_session(); + is_last_orig_session = iter(ob, iter(ob, last)) == nil; + end - -- Send presence stanza about original occupant - if orig_occupant ~= nil and orig_occupant ~= dest_occupant then - local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); - 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); - local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"}; - orig_occupant:set_session(real_jid, generated_unavail); - dest_nick = select(3, jid_split(dest_occupant.nick)); - if not is_first_dest_session then -- User is swapping into another pre-existing session - log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick); - -- Show the other session leaving - local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}) - :tag("status"):text("you are joining pre-existing session " .. dest_nick):up(); - add_item(x, self:get_affiliation(bare_jid), "none"); - local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"} - :add_child(x); - self:route_stanza(pr); - end - if is_first_dest_session and is_last_orig_session then -- Normal nick change - log("debug", "no sessions in %s left; publically marking as nick change", orig_occupant.nick); - orig_x:tag("status", {code = "303";}):up(); - else -- The session itself always needs to see a nick change - -- don't want to get our old nick's available presence, - -- so remove our session from there, and manually generate an unavailable - orig_occupant:remove_session(real_jid); - log("debug", "generating nick change for %s", real_jid); - local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); - -- self:build_item_list(orig_occupant, x, false, dest_nick); -- COMPAT: clients get confused if they see other items besides their own - add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick); - x:tag("status", {code = "303";}):up(); - x:tag("status", {code = "110";}):up(); - self:route_stanza(generated_unavail:add_child(x)); - dest_nick = nil; -- set dest_nick to nil; so general populance doesn't see it for whole orig_occupant - end - end - self:save_occupant(orig_occupant); - self:publicise_occupant_status(orig_occupant, orig_x, dest_nick); + -- TODO Handle these cases sensibly + local muc_x = stanza:get_child("x", "http://jabber.org/protocol/muc"); + if orig_occupant == nil and not muc_x then + module:log("debug", "Join without , possibly desynced"); + elseif orig_occupant ~= nil and muc_x then + module:log("debug", "Presence update with , possibly desynced"); + end - if is_last_orig_session then - module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick; occupant = orig_occupant;}); - end - end + local event, event_name = { + room = self; + origin = origin; + stanza = stanza; + is_first_session = is_first_dest_session; + is_last_session = is_last_orig_session; + }; + if orig_occupant == nil then + event_name = "muc-occupant-pre-join"; + 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 + + -- Check for nick conflicts + if dest_occupant ~= nil and not is_first_dest_session and bare_jid ~= jid_bare(dest_occupant.bare_jid) then -- new nick or has different bare real jid + log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick); + local reply = st.error_reply(stanza, "cancel", "conflict"):up(); + reply.tags[1].attr.code = "409"; + origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); + return true; + end - 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(); + -- Send presence stanza about original occupant + if orig_occupant ~= nil and orig_occupant ~= dest_occupant then + local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); + 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 - 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) -- luacheck: ignore 212 - -- Don't include self - return occupant:get_presence(real_jid) == 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); + local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"}; + orig_occupant:set_session(real_jid, generated_unavail); + dest_nick = select(3, jid_split(dest_occupant.nick)); + if not is_first_dest_session then -- User is swapping into another pre-existing session + log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick); + -- Show the other session leaving + local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}) + :tag("status"):text("you are joining pre-existing session " .. dest_nick):up(); + add_item(x, self:get_affiliation(bare_jid), "none"); + local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"} + :add_child(x); + self:route_stanza(pr); 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 - log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick); - -- Show the original nick joining again - local pr = st.clone(orig_occupant:get_presence()); - pr.attr.to = real_jid; + if is_first_dest_session and is_last_orig_session then -- Normal nick change + log("debug", "no sessions in %s left; publically marking as nick change", orig_occupant.nick); + orig_x:tag("status", {code = "303";}):up(); + else -- The session itself always needs to see a nick change + -- don't want to get our old nick's available presence, + -- so remove our session from there, and manually generate an unavailable + orig_occupant:remove_session(real_jid); + log("debug", "generating nick change for %s", real_jid); local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); - self:build_item_list(orig_occupant, x, false); - -- TODO: new status code to inform client this was the multi-session it left? - pr:add_child(x); - self:route_stanza(pr); + -- self:build_item_list(orig_occupant, x, false, dest_nick); -- COMPAT: clients get confused if they see other items besides their own + add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick); + x:tag("status", {code = "303";}):up(); + x:tag("status", {code = "110";}):up(); + self:route_stanza(generated_unavail:add_child(x)); + dest_nick = nil; -- set dest_nick to nil; so general populance doesn't see it for whole orig_occupant end + end + self:save_occupant(orig_occupant); + 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; + 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;}); - end - module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;}); + 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 orig_occupant == nil and self:get_whois() == "anyone" then + 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) -- luacheck: ignore 212 + -- 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 + log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick); + -- Show the original nick joining again + local pr = st.clone(orig_occupant:get_presence()); + pr.attr.to = real_jid; + local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); + self:build_item_list(orig_occupant, x, false); + -- TODO: new status code to inform client this was the multi-session it left? + pr:add_child(x); + self:route_stanza(pr); + 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; + stanza = stanza; + origin = origin; + }); end + 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 + return true; +end + +function room_mt:handle_presence_to_occupant(origin, stanza) + local type = stanza.attr.type; + if type == "error" then -- error, kick em out! + return self:handle_kickable(origin, stanza) + elseif type == nil or type == "unavailable" then + return self:handle_normal_presence(origin, stanza); elseif type ~= 'result' then -- bad type if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? @@ -676,7 +769,7 @@ function room_mt:process_form(origin, stanza) end event.field, event.value = nil, nil; - if self.save then self:save(true); end + self:save(true); origin.send(st.reply(stanza)); if next(event.status_codes) then @@ -759,6 +852,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza) else success, errtype, err = nil, "cancel", "bad-request"; end + self:save(true); if not success then origin.send(st.error_reply(stanza, errtype, err)); else @@ -1125,7 +1219,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, reason) end end - if self.save then self:save(); end + self:save(true); module:fire_event("muc-set-affiliation", { room = self; @@ -1149,7 +1243,7 @@ function room_mt:set_role(actor, occupant_jid, role, reason) 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"; @@ -1199,18 +1293,116 @@ room_mt.set_whois = whois.set; 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, state = { + _jid = self.jid; + _data = self._data; + }; + for user, affiliation in pairs(self._affiliations) do + frozen[user] = affiliation; + end + if live then + state = {}; + for nick, occupant in self:each_occupant() do + state[nick] = { + bare_jid = occupant.bare_jid; + role = occupant.role; + jid = occupant.jid; + } + for jid, presence in occupant:each_session() do + 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, state; +end + +function _M.restore_room(frozen, state) + -- 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); + + if frozen._last_message and frozen._last_message_at then + room._history = { + { stanza = st.deserialize(frozen._last_message), + timestamp = frozen._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 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); + 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; + elseif type(data) == "table" and data.name then + -- 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;