mod_c2s, mod_s2s: Set session.encrypted as session.secure does not allways mean encry...
[prosody.git] / plugins / muc / muc.lib.lua
index 833b1154a56d033c0ec76b8ffa7ce6e50542b6b3..1b76ec94ad391bbed5f3c469c5bf757bbd608b2b 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
@@ -27,28 +27,16 @@ local muc_domain = nil; --module:get_host();
 local default_history_length, max_history_length = 20, math.huge;
 
 ------------
-local function filter_xmlns_from_array(array, filters)
-       local count = 0;
-       for i=#array,1,-1 do
-               local attr = array[i].attr;
-               if filters[attr and attr.xmlns] then
-                       t_remove(array, i);
-                       count = count + 1;
-               end
-       end
-       return count;
-end
-local function filter_xmlns_from_stanza(stanza, filters)
-       if filters then
-               if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
-                       return stanza, filter_xmlns_from_array(stanza, filters);
-               end
+local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
+local function presence_filter(tag)
+       if presence_filters[tag.attr.xmlns] then
+               return nil;
        end
-       return stanza, 0;
+       return tag;
 end
-local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
+
 local function get_filtered_presence(stanza)
-       return filter_xmlns_from_stanza(st.clone(stanza):reset(), presence_filters);
+       return st.clone(stanza):maptags(presence_filter);
 end
 local kickable_error_conditions = {
        ["gone"] = true;
@@ -72,17 +60,6 @@ local function is_kickable_error(stanza)
        local cond = get_error_condition(stanza);
        return kickable_error_conditions[cond] and cond;
 end
-local function getUsingPath(stanza, path, getText)
-       local tag = stanza;
-       for _, name in ipairs(path) do
-               if type(tag) ~= 'table' then return; end
-               tag = tag:child_with_name(name);
-       end
-       if tag and getText then tag = table.concat(tag); end
-       return tag;
-end
-local function getTag(stanza, path) return getUsingPath(stanza, path); end
-local function getText(stanza, path) return getUsingPath(stanza, path, true); end
 -----------
 
 local room_mt = {};
@@ -170,10 +147,10 @@ function room_mt:send_history(to, stanza)
        if history then
                local x_tag = stanza and stanza:get_child("x", "http://jabber.org/protocol/muc");
                local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
-               
+
                local maxchars = history_tag and tonumber(history_tag.attr.maxchars);
                if maxchars then maxchars = math.floor(maxchars); end
-               
+
                local maxstanzas = math.floor(history_tag and tonumber(history_tag.attr.maxstanzas) or #history);
                if not history_tag then maxstanzas = 20; end
 
@@ -186,7 +163,7 @@ function room_mt:send_history(to, stanza)
 
                local n = 0;
                local charcount = 0;
-               
+
                for i=#history,1,-1 do
                        local entry = history[i];
                        if maxchars then
@@ -225,7 +202,7 @@ function room_mt:get_disco_info(stanza)
                :tag("feature", {var=self._data.whois ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up()
                :add_child(dataform.new({
                        { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" },
-                       { name = "muc#roominfo_description", label = "Description"},
+                       { name = "muc#roominfo_description", label = "Description", value = "" },
                        { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) }
                }):form({["muc#roominfo_description"] = self:get_description()}, 'result'))
        ;
@@ -238,7 +215,6 @@ function room_mt:get_disco_items(stanza)
        return reply;
 end
 function room_mt:set_subject(current_nick, subject)
-       -- TODO check nick's authority
        if subject == "" then subject = nil; end
        self._data['subject'] = subject;
        self._data['subject_from'] = current_nick;
@@ -357,12 +333,25 @@ function room_mt:set_historylength(length)
 end
 
 
+local valid_whois = { moderators = true, anyone = true };
+
+function room_mt:set_whois(whois)
+       if valid_whois[whois] and self._data.whois ~= whois then
+               self._data.whois = whois;
+               if self.save then self:save(true); end
+       end
+end
+
+function room_mt:get_whois()
+       return self._data.whois;
+end
+
 local function construct_stanza_id(room, stanza)
        local from_jid, to_nick = stanza.attr.from, stanza.attr.to;
        local from_nick = room._jid_nick[from_jid];
        local occupant = room._occupants[to_nick];
        local to_jid = occupant.jid;
-       
+
        return from_nick, to_jid, base64.encode(to_jid.."\0"..stanza.attr.id.."\0"..md5(from_jid));
 end
 local function deconstruct_stanza_id(room, stanza)
@@ -491,6 +480,12 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                        log("debug", "%s joining as %s", from, to);
                                        if not next(self._affiliations) then -- new room, no owners
                                                self._affiliations[jid_bare(from)] = "owner";
+                                               if self.locked and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
+                                                       self.locked = nil; -- Older groupchat protocol doesn't lock
+                                               end
+                                       elseif self.locked then -- Deny entry
+                                               origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
+                                               return;
                                        end
                                        local affiliation = self:get_affiliation(from);
                                        local role = self:get_default_role(affiliation)
@@ -512,6 +507,9 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                                if self._data.whois == 'anyone' then
                                                        pr:tag("status", {code='100'}):up();
                                                end
+                                               if self.locked then
+                                                       pr:tag("status", {code='201'}):up();
+                                               end
                                                pr.attr.to = from;
                                                self:_route_stanza(pr);
                                                self:send_history(from, stanza);
@@ -581,11 +579,11 @@ end
 
 function room_mt:send_form(origin, stanza)
        origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
-               :add_child(self:get_form_layout():form())
+               :add_child(self:get_form_layout(stanza.attr.from):form())
        );
 end
 
-function room_mt:get_form_layout()
+function room_mt:get_form_layout(actor)
        local form = dataform.new({
                title = "Configuration for "..self.jid,
                instructions = "Complete and submit this form to configure the room.",
@@ -658,11 +656,9 @@ function room_mt:get_form_layout()
                        value = tostring(self:get_historylength())
                }
        });
-       return module:fire_event("muc-config-form", { room = self, form = form }) or form;
+       return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
 end
 
-local valid_whois = { moderators = true, anyone = true };
-
 function room_mt:process_form(origin, stanza)
        local query = stanza.tags[1];
        local form;
@@ -671,7 +667,7 @@ function room_mt:process_form(origin, stanza)
        if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
        if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end
 
-       local fields = self:get_form_layout():data(form);
+       local fields = self:get_form_layout(stanza.attr.from):data(form);
        if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); return; end
 
 
@@ -701,6 +697,10 @@ function room_mt:process_form(origin, stanza)
        handle_option("password", "muc#roomconfig_roomsecret");
 
        if self.save then self:save(true); end
+       if self.locked then
+               module:fire_event("muc-room-unlocked", { room = self });
+               self.locked = nil;
+       end
        origin.send(st.reply(stanza));
 
        if next(changed) then
@@ -708,7 +708,7 @@ function room_mt:process_form(origin, stanza)
                        :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up()
                                :tag('status', {code = '104'}):up();
                if changed.whois then
-                       local code = (whois == 'moderators') and "173" or "172";
+                       local code = (self:get_whois() == 'moderators') and "173" or "172";
                        msg.tags[1]:tag('status', {code = code}):up();
                end
                self:broadcast_message(msg, false)
@@ -846,7 +846,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                        origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                end
        elseif stanza.name == "message" and type == "groupchat" then
-               local from, to = stanza.attr.from, stanza.attr.to;
+               local from = stanza.attr.from;
                local current_nick = self._jid_nick[from];
                local occupant = self._occupants[current_nick];
                if not occupant then -- not in room
@@ -856,11 +856,11 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                else
                        local from = stanza.attr.from;
                        stanza.attr.from = current_nick;
-                       local subject = getText(stanza, {"subject"});
+                       local subject = stanza:get_child_text("subject");
                        if subject then
                                if occupant.role == "moderator" or
                                        ( self._data.changesubject and occupant.role == "participant" ) then -- and participant
-                                       self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
+                                       self:set_subject(current_nick, subject);
                                else
                                        stanza.attr.from = from;
                                        origin.send(st.error_reply(stanza, "auth", "forbidden"));
@@ -1020,7 +1020,7 @@ function room_mt:get_role(nick)
 end
 function room_mt:can_set_role(actor_jid, occupant_jid, role)
        local occupant = self._occupants[occupant_jid];
-       if not occupant or not actor then return nil, "modify", "not-acceptable"; end
+       if not occupant or not actor_jid then return nil, "modify", "not-acceptable"; end
 
        if actor_jid == true then return true; end