plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
[prosody.git] / plugins / muc / muc.lib.lua
index 87f3d69c34a87271787eb08c1ce1c0bad57b03b4..57cc3721ef02b5d53e72fb455b736271054bac07 100644 (file)
@@ -49,15 +49,10 @@ end
 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)
@@ -84,6 +79,10 @@ do
        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
@@ -101,11 +100,21 @@ function room_mt:save_occupant(occupant)
                        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
@@ -114,10 +123,8 @@ end
 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
@@ -200,6 +207,9 @@ function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason
                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
@@ -214,7 +224,7 @@ function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason
        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();
@@ -242,14 +252,14 @@ end
 
 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
@@ -277,12 +287,6 @@ 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) });
@@ -296,28 +300,6 @@ function room_mt:get_disco_items(stanza)
        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);
@@ -336,49 +318,11 @@ function room_mt:handle_kickable(origin, stanza)
        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;
@@ -391,20 +335,6 @@ module:hook("muc-occupant-pre-join", function(event)
        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!
@@ -481,6 +411,9 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
                        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);
@@ -532,6 +465,14 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
                                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
@@ -658,30 +599,6 @@ function room_mt:get_form_layout(actor)
        });
        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");
@@ -723,15 +640,6 @@ function room_mt:process_form(origin, stanza)
        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)
@@ -879,6 +787,11 @@ function room_mt:handle_owner_query_set_to_room(origin, stanza)
 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
@@ -887,24 +800,11 @@ function room_mt:handle_groupchat_to_room(origin, stanza)
        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
@@ -938,11 +838,13 @@ function room_mt:handle_mediated_invite(origin, stanza)
        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()
@@ -966,11 +868,13 @@ end);
 -- 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)
@@ -979,11 +883,13 @@ 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()
@@ -1002,11 +908,13 @@ end
 -- 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)
@@ -1183,6 +1091,16 @@ 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;
@@ -1195,10 +1113,21 @@ 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;
@@ -1206,8 +1135,6 @@ room_mt.set_historylength = history.set_length;
 
 local _M = {}; -- module "muc"
 
-_M.set_max_history_length = history.set_max_length;
-
 function _M.new_room(jid, config)
        return setmetatable({
                jid = jid;