mod_storage_sql: Initial commit of new SQL data driver.
[prosody.git] / plugins / muc / muc.lib.lua
index 96ce32a0e9cd2e3001e31bc418efb0590f9198bd..f3e2dd5285385e4482da16b0e0bd2ba5f800fae0 100644 (file)
@@ -449,14 +449,17 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                                self._jid_nick[from] = to;
                                                self:send_occupant_list(from);
                                                pr.attr.from = to;
+                                               pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+                                                       :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
                                                if not is_merge then
-                                                       self:broadcast_presence(pr, from);
-                                               else
-                                                       pr.attr.to = from;
-                                                       self:_route_stanza(pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
-                                                               :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up()
-                                                               :tag("status", {code='110'}));
+                                                       self:broadcast_except_nick(pr, to);
+                                               end
+                                               pr:tag("status", {code='110'});
+                                               if self._data.whois == 'anyone' then
+                                                       pr:tag("status", {code='100'}):up();
                                                end
+                                               pr.attr.to = from;
+                                               self:_route_stanza(pr);
                                                self:send_history(from, stanza);
                                        elseif not affiliation then -- registration required for entering members-only room
                                                local reply = st.error_reply(stanza, "auth", "registration-required"):up();
@@ -522,9 +525,14 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
 end
 
 function room_mt:send_form(origin, stanza)
-       local title = "Configuration for "..self.jid;
        origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
-       :add_child(dataform.new({
+               :add_child(self:get_form_layout():form())
+       );
+end
+
+function room_mt:get_form_layout()
+       local title = "Configuration for "..self.jid;
+       return dataform.new({
                title = title,
                instructions = title,
                {
@@ -583,8 +591,7 @@ function room_mt:send_form(origin, stanza)
                        label = 'Make Room Members-Only?',
                        value = self:is_members_only()
                }
-       }):form())
-       );
+       });
 end
 
 local valid_whois = {
@@ -598,57 +605,41 @@ function room_mt:process_form(origin, stanza)
        for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end
        if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
        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")); return; end
-       local fields = {};
-       for _, field in pairs(form.tags) do
-               if field.name == "field" and field.attr.var then
-                       if field.tags[1] and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
-                               fields[field.attr.var] = field.tags[1][1] or "";
-                       elseif field.attr.type == "boolean" then
-                               fields[field.attr.var] = "false";
-                       end
-               end
-       end
-       if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); 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);
+       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
 
        local dirty = false
 
        local name = fields['muc#roomconfig_roomname'];
-       if name then
+       if name ~= self:get_name() then
                self:set_name(name);
        end
 
        local description = fields['muc#roomconfig_roomdesc'];
-       if description then
+       if description ~= self:get_description() then
                self:set_description(description);
        end
 
        local persistent = fields['muc#roomconfig_persistentroom'];
-       if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true;
-       else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
        dirty = dirty or (self:is_persistent() ~= persistent)
        module:log("debug", "persistent=%s", tostring(persistent));
 
        local moderated = fields['muc#roomconfig_moderatedroom'];
-       if moderated == "0" or moderated == "false" then moderated = nil; elseif moderated == "1" or moderated == "true" then moderated = true;
-       else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
        dirty = dirty or (self:is_moderated() ~= moderated)
        module:log("debug", "moderated=%s", tostring(moderated));
 
        local membersonly = fields['muc#roomconfig_membersonly'];
-       if membersonly == "0" or membersonly == "false" then membersonly = nil; elseif membersonly == "1" or membersonly == "true" then membersonly = true;
-       else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
        dirty = dirty or (self:is_members_only() ~= membersonly)
        module:log("debug", "membersonly=%s", tostring(membersonly));
 
        local public = fields['muc#roomconfig_publicroom'];
-       if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true;
-       else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
        dirty = dirty or (self:is_hidden() ~= (not public and true or nil))
 
        local whois = fields['muc#roomconfig_whois'];
        if not valid_whois[whois] then
-           origin.send(st.error_reply(stanza, 'cancel', 'bad-request'));
+           origin.send(st.error_reply(stanza, 'cancel', 'bad-request', "Invalid value for 'whois'"));
            return;
        end
        local whois_changed = self._data.whois ~= whois
@@ -656,7 +647,7 @@ function room_mt:process_form(origin, stanza)
        module:log('debug', 'whois=%s', whois)
 
        local password = fields['muc#roomconfig_roomsecret'];
-       if password then
+       if self:get_password() ~= password then
                self:set_password(password);
        end
        self:set_moderated(moderated);
@@ -668,18 +659,18 @@ function room_mt:process_form(origin, stanza)
        origin.send(st.reply(stanza));
 
        if dirty or whois_changed then
-           local msg = st.message({type='groupchat', from=self.jid})
-                   :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up()
+               local msg = st.message({type='groupchat', from=self.jid})
+                       :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up()
 
-           if dirty then
-               msg.tags[1]:tag('status', {code = '104'})
-           end
-           if whois_changed then
-               local code = (whois == 'moderators') and 173 or 172
-               msg.tags[1]:tag('status', {code = code})
-           end
+               if dirty then
+                       msg.tags[1]:tag('status', {code = '104'}):up();
+               end
+               if whois_changed then
+                       local code = (whois == 'moderators') and "173" or "172";
+                       msg.tags[1]:tag('status', {code = code}):up();
+               end
 
-           self:broadcast_message(msg, false)
+               self:broadcast_message(msg, false)
        end
 end
 
@@ -875,6 +866,10 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        :tag('body') -- Add a plain message for clients which don't support invites
                                                :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or ""))
                                        :up();
+                               if self:is_members_only() and not self:get_affiliation(_invitee) then
+                                       log("debug", "%s invited %s into members only room %s, granting membership", _from, _invitee, _to);
+                                       self:set_affiliation(_from, _invitee, "member", nil, "Invited by " .. self._jid_nick[_from])
+                               end
                                self:_route_stanza(invite);
                        else
                                origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
@@ -916,15 +911,13 @@ function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
        if jid_bare(actor) == jid then return nil, "cancel", "not-allowed"; end
        self._affiliations[jid] = affiliation;
        local role = self:get_default_role(affiliation);
-       local p = st.presence()
-               :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+       local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
                        :tag("item", {affiliation=affiliation or "none", role=role or "none"})
                                :tag("reason"):text(reason or ""):up()
                        :up();
-       local x = p.tags[1];
-       local item = x.tags[1];
+       local presence_type = nil;
        if not role then -- getting kicked
-               p.attr.type = "unavailable";
+               presence_type = "unavailable";
                if affiliation == "outcast" then
                        x:tag("status", {code="301"}):up(); -- banned
                else
@@ -934,23 +927,28 @@ function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
        local modified_nicks = {};
        for nick, occupant in pairs(self._occupants) do
                if jid_bare(occupant.jid) == jid then
-                       t_insert(modified_nicks, nick);
                        if not role then -- getting kicked
                                self._occupants[nick] = nil;
                        else
                                occupant.affiliation, occupant.role = affiliation, role;
                        end
-                       p.attr.from = nick;
-                       for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
+                       for jid,pres in pairs(occupant.sessions) do -- remove for all sessions of the nick
                                if not role then self._jid_nick[jid] = nil; end
+                               local p = st.clone(pres);
+                               p.attr.from = nick;
+                               p.attr.type = presence_type;
                                p.attr.to = jid;
+                               p:add_child(x);
                                self:_route_stanza(p);
+                               if occupant.jid == jid then
+                                       modified_nicks[nick] = p;
+                               end
                        end
                end
        end
        if self.save then self:save(); end
        if callback then callback(); end
-       for _, nick in ipairs(modified_nicks) do
+       for nick,p in pairs(modified_nicks) do
                p.attr.from = nick;
                self:broadcast_except_nick(p, nick);
        end
@@ -984,27 +982,37 @@ function room_mt:set_role(actor, occupant_jid, role, callback, reason)
        local allowed, err_type, err_condition = self:can_set_role(actor, occupant_jid, role);
        if not allowed then return allowed, err_type, err_condition; end
        local occupant = self._occupants[occupant_jid];
-       local p = st.presence({from = occupant_jid})
-               :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+       local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
                        :tag("item", {affiliation=occupant.affiliation or "none", nick=select(3, jid_split(occupant_jid)), role=role or "none"})
                                :tag("reason"):text(reason or ""):up()
                        :up();
+       local presence_type = nil;
        if not role then -- kick
-               p.attr.type = "unavailable";
+               presence_type = "unavailable";
                self._occupants[occupant_jid] = nil;
                for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
                        self._jid_nick[jid] = nil;
                end
-               p:tag("status", {code = "307"}):up();
+               x:tag("status", {code = "307"}):up();
        else
                occupant.role = role;
        end
-       for jid in pairs(occupant.sessions) do -- send to all sessions of the nick
+       local bp;
+       for jid,pres in pairs(occupant.sessions) do -- send to all sessions of the nick
+               local p = st.clone(pres);
+               p.attr.from = occupant_jid;
+               p.attr.type = presence_type;
                p.attr.to = jid;
+               p:add_child(x);
                self:_route_stanza(p);
+               if occupant.jid == jid then
+                       bp = p;
+               end
        end
        if callback then callback(); end
-       self:broadcast_except_nick(p, occupant_jid);
+       if bp then
+               self:broadcast_except_nick(bp, occupant_jid);
+       end
        return true;
 end
 
@@ -1033,9 +1041,6 @@ function room_mt:_route_stanza(stanza)
                                end
                        end
                end
-               if self._data.whois == 'anyone' then
-                   muc_child:tag('status', { code = '100' });
-               end
        end
        self:route_stanza(stanza);
        if muc_child then