MUC: A little modification to improve code analysis.
[prosody.git] / plugins / muc / muc.lib.lua
index 0fb5223dbb47636ca90291bfa67c22d760b0ffd2..6dfc6bb9e3d504c5a99d1a14d32174d9d6b3d9d0 100644 (file)
@@ -1,11 +1,14 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- 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.
 --
 
+local select = select;
+local pairs, ipairs = pairs, ipairs;
+
 local datamanager = require "util.datamanager";
 local datetime = require "util.datetime";
 
@@ -59,19 +62,12 @@ local kickable_error_conditions = {
        ["service-unavailable"] = true;
        ["malformed error"] = true;
 };
+
 local function get_error_condition(stanza)
-       for _, tag in ipairs(stanza.tags) do
-               if tag.name == "error" and (not(tag.attr.xmlns) or tag.attr.xmlns == "jabber:client") then
-                       for _, cond in ipairs(tag.tags) do
-                               if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
-                                       return cond.name;
-                               end
-                       end
-                       return "malformed error";
-               end
-       end
-       return "malformed error";
+       local _, condition = stanza:get_error();
+       return condition or "malformed error";
 end
+
 local function is_kickable_error(stanza)
        local cond = get_error_condition(stanza);
        return kickable_error_conditions[cond] and cond;
@@ -89,25 +85,18 @@ local function getTag(stanza, path) return getUsingPath(stanza, path); end
 local function getText(stanza, path) return getUsingPath(stanza, path, true); end
 -----------
 
---[[function get_room_disco_info(room, stanza)
-       return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
-               :tag("identity", {category='conference', type='text', name=room._data["name"]):up()
-               :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
-end
-function get_room_disco_items(room, stanza)
-       return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
-end -- TODO allow non-private rooms]]
-
---
-
 local room_mt = {};
 room_mt.__index = room_mt;
 
 function room_mt:get_default_role(affiliation)
        if affiliation == "owner" or affiliation == "admin" then
                return "moderator";
-       elseif affiliation == "member" or not affiliation then
+       elseif affiliation == "member" then
                return "participant";
+       elseif not affiliation then
+               if not self:is_members_only() then
+                       return self:is_moderated() and "visitor" or "participant";
+               end
        end
 end
 
@@ -140,9 +129,13 @@ function room_mt:broadcast_message(stanza, historic)
                local history = self._data['history'];
                if not history then history = {}; self._data['history'] = history; end
                stanza = st.clone(stanza);
-               stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
+               stanza.attr.to = "";
+               local stamp = datetime.datetime();
+               local chars = #tostring(stanza);
+               stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = stamp}):up(); -- XEP-0203
                stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
-               t_insert(history, st.preserialize(stanza));
+               local entry = { stanza = stanza, stamp = stamp };
+               t_insert(history, entry);
                while #history > history_length do t_remove(history, 1) end
        end
 end
@@ -169,12 +162,46 @@ function room_mt:send_occupant_list(to)
                end
        end
 end
-function room_mt:send_history(to)
+function room_mt:send_history(to, stanza)
        local history = self._data['history']; -- send discussion history
        if history then
-               for _, msg in ipairs(history) do
-                       msg = st.deserialize(msg);
-                       msg.attr.to=to;
+               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
+
+               local seconds = history_tag and tonumber(history_tag.attr.seconds);
+               if seconds then seconds = datetime.datetime(os.time() - math.floor(seconds)); end
+
+               local since = history_tag and history_tag.attr.since;
+               if since and not since:match("^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%dZ$") then since = nil; end -- FIXME timezone support
+               if seconds and (not since or since < seconds) then since = seconds; end
+
+               local n = 0;
+               local charcount = 0;
+               local stanzacount = 0;
+               
+               for i=#history,1,-1 do
+                       local entry = history[i];
+                       if maxchars then
+                               if not entry.chars then
+                                       entry.stanza.attr.to = "";
+                                       entry.chars = #tostring(entry.stanza);
+                               end
+                               charcount = charcount + entry.chars + #to;
+                               if charcount > maxchars then break; end
+                       end
+                       if since and since > entry.stamp then break; end
+                       if n + 1 > maxstanzas then break; end
+                       n = n + 1;
+               end
+               for i=#history-n+1,#history do
+                       local msg = history[i].stanza;
+                       msg.attr.to = to;
                        self:_route_stanza(msg);
                end
        end
@@ -183,12 +210,19 @@ function room_mt:send_history(to)
        end
 end
 
-local function room_get_disco_info(self, stanza)
+function room_mt:get_disco_info(stanza)
        return st.reply(stanza):query("http://jabber.org/protocol/disco#info")
                :tag("identity", {category="conference", type="text"}):up()
-               :tag("feature", {var="http://jabber.org/protocol/muc"});
+               :tag("feature", {var="http://jabber.org/protocol/muc"}):up()
+               :tag("feature", {var=self:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up()
+               :tag("feature", {var=self:is_moderated() and "muc_moderated" or "muc_unmoderated"}):up()
+               :tag("feature", {var=self:is_members_only() and "muc_membersonly" or "muc_open"}):up()
+               :tag("feature", {var=self:is_persistent() and "muc_persistent" or "muc_temporary"}):up()
+               :tag("feature", {var=self:is_hidden() and "muc_hidden" or "muc_public"}):up()
+               :tag("feature", {var=self._data.whois ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up()
+       ;
 end
-local function room_get_disco_items(self, stanza)
+function room_mt:get_disco_items(stanza)
        local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
        for room_jid in pairs(self._occupants) do
                reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
@@ -206,6 +240,67 @@ function room_mt:set_subject(current_nick, subject)
        return true;
 end
 
+local function build_unavailable_presence_from_error(stanza)
+       local type, condition, text = stanza:get_error();
+       local error_message = "Kicked: "..condition:gsub("%-", " ");
+       if text then
+               error_message = error_message..": "..text;
+       end
+       return st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
+               :tag('status'):text(error_message);
+end
+
+function room_mt:set_password(password)
+       if password == "" or type(password) ~= "string" then password = nil; end
+       if self._data.password ~= password then
+               self._data.password = password;
+               if self.save then self:save(true); end
+       end
+end
+function room_mt:get_password()
+       return self._data.password;
+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:is_moderated()
+       return self._data.moderated;
+end
+function room_mt:set_members_only(members_only)
+       members_only = members_only and true or nil;
+       if self._data.members_only ~= members_only then
+               self._data.members_only = members_only;
+               if self.save then self:save(true); end
+       end
+end
+function room_mt:is_members_only()
+       return self._data.members_only;
+end
+function room_mt:set_persistent(persistent)
+       persistent = persistent and true or nil;
+       if self._data.persistent ~= persistent then
+               self._data.persistent = persistent;
+               if self.save then self:save(true); end
+       end
+end
+function room_mt:is_persistent()
+       return self._data.persistent;
+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:is_hidden()
+       return self._data.hidden;
+end
+
 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
        local from, to = stanza.attr.from, stanza.attr.to;
        local room = jid_bare(to);
@@ -219,8 +314,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                if type == "error" then -- error, kick em out!
                        if current_nick then
                                log("debug", "kicking %s from %s", current_nick, room);
-                               self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
-                                       :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
+                               self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza));
                        end
                elseif type == "unavailable" then -- unavailable
                        if current_nick then
@@ -299,7 +393,15 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                        end
                                        is_merge = true;
                                end
-                               if not new_nick then
+                               local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
+                               password = password and password:get_child("password", "http://jabber.org/protocol/muc");
+                               password = password and password[1] ~= "" and password[1];
+                               if self:get_password() and self:get_password() ~= password then
+                                       log("debug", "%s couldn't join due to invalid password: %s", from, to);
+                                       local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
+                                       reply.tags[1].attr.code = "401";
+                                       origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
+                               elseif not new_nick then
                                        log("debug", "%s couldn't join due to nick conflict: %s", from, to);
                                        local reply = st.error_reply(stanza, "cancel", "conflict"):up();
                                        reply.tags[1].attr.code = "409";
@@ -328,7 +430,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                                                :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up()
                                                                :tag("status", {code='110'}));
                                                end
-                                               self:send_history(from);
+                                               self:send_history(from, stanza);
                                        else -- banned
                                                local reply = st.error_reply(stanza, "auth", "forbidden"):up();
                                                reply.tags[1].attr.code = "403";
@@ -367,8 +469,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                origin.send(st.error_reply(stanza, "modify", "bad-request"));
        elseif current_nick and stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
                log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
-               self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
-                       :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
+               self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable
        else -- private stanza
                local o_data = self._occupants[to];
                if o_data then
@@ -389,51 +490,139 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
        end
 end
 
-function room_mt:handle_form(origin, stanza)
-       if self:get_affiliation(stanza.attr.from) ~= "owner" then origin.send(st.error_reply(stanza, "auth", "forbidden")); return; end
-       if stanza.attr.type == "get" then
-               local title = "Configuration for "..self.jid;
-               origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
-                       :tag("x", {xmlns='jabber:x:data', type='form'})
-                               :tag("title"):text(title):up()
-                               :tag("instructions"):text(title):up()
-                               :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up()
-                               :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'})
-                                       :tag("value"):text(self._data.persistent and "1" or "0"):up()
+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")
+               :tag("x", {xmlns='jabber:x:data', type='form'})
+                       :tag("title"):text(title):up()
+                       :tag("instructions"):text(title):up()
+                       :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up()
+                       :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'})
+                               :tag("value"):text(self:is_persistent() and "1" or "0"):up()
+                       :up()
+                       :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'})
+                               :tag("value"):text(self:is_hidden() and "0" or "1"):up()
+                       :up()
+                       :tag("field", {type='list-single', label='Who May Discover Real JIDs?', var='muc#roomconfig_whois'})
+                           :tag("value"):text(self._data.whois or 'moderators'):up()
+                           :tag("option", {label = 'Moderators Only'})
+                               :tag("value"):text('moderators'):up()
                                :up()
-                               :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'})
-                                       :tag("value"):text(self._data.hidden and "0" or "1"):up()
+                           :tag("option", {label = 'Anyone'})
+                               :tag("value"):text('anyone'):up()
                                :up()
-               );
-       elseif stanza.attr.type == "set" then
-               local query = stanza.tags[1];
-               local form;
-               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 and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
-                               fields[field.attr.var] = field.tags[1][1] or "";
-                       end
+                       :up()
+                       :tag("field", {type='text-private', label='Password', var='muc#roomconfig_roomsecret'})
+                               :tag("value"):text(self:get_password() or ""):up()
+                       :up()
+                       :tag("field", {type='boolean', label='Make Room Moderated?', var='muc#roomconfig_moderatedroom'})
+                               :tag("value"):text(self:is_moderated() and "1" or "0"):up()
+                       :up()
+                       :tag("field", {type='boolean', label='Make Room Members-Only?', var='muc#roomconfig_membersonly'})
+                               :tag("value"):text(self:is_members_only() and "1" or "0"):up()
+                       :up()
+       );
+end
+
+local valid_whois = {
+    moderators = true,
+    anyone = true,
+}
+
+function room_mt:process_form(origin, stanza)
+       local query = stanza.tags[1];
+       local form;
+       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 and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
+                       fields[field.attr.var] = field.tags[1][1] or "";
                end
-               if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
+       end
+       if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; 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
-               self._data.persistent = persistent;
-               module:log("debug", "persistent=%s", tostring(persistent));
+       local dirty = false
 
-               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
-               self._data.hidden = not public and true or nil;
+       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));
 
-               if self.save then self:save(true); end
-               origin.send(st.reply(stanza));
+       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'));
+           return;
+       end
+       local whois_changed = self._data.whois ~= whois
+       self._data.whois = whois
+       module:log('debug', 'whois=%s', whois)
+
+       local password = fields['muc#roomconfig_roomsecret'];
+       if password then
+               self:set_password(password);
+       end
+       self:set_moderated(moderated);
+       self:set_members_only(membersonly);
+       self:set_persistent(persistent);
+       self:set_hidden(not public);
+
+       if self.save then self:save(true); end
+       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()
+
+           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
+
+           self:broadcast_message(msg, false)
+       end
+end
+
+function room_mt:destroy(newjid, reason, password)
+       local pr = st.presence({type = "unavailable"})
+               :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+                       :tag("item", { affiliation='none', role='none' }):up()
+                       :tag("destroy", {jid=newjid})
+       if reason then pr:tag("reason"):text(reason):up(); end
+       if password then pr:tag("password"):text(password):up(); end
+       for nick, occupant in pairs(self._occupants) do
+               pr.attr.from = nick;
+               for jid in pairs(occupant.sessions) do
+                       pr.attr.to = jid;
+                       self:_route_stanza(pr);
+                       self._jid_nick[jid] = nil;
+               end
+               self._occupants[nick] = nil;
        end
+       self:set_persistent(false);
 end
 
 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
@@ -441,9 +630,9 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
        local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
        if stanza.name == "iq" then
                if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" then
-                       origin.send(room_get_disco_info(self, stanza));
+                       origin.send(self:get_disco_info(stanza));
                elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" then
-                       origin.send(room_get_disco_items(self, stanza));
+                       origin.send(self:get_disco_items(stanza));
                elseif xmlns == "http://jabber.org/protocol/muc#admin" then
                        local actor = stanza.attr.from;
                        local affiliation = self:get_affiliation(actor);
@@ -463,6 +652,9 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
                                                local occupant = self._occupants[self.jid.."/"..item.attr.nick];
                                                if occupant then item.attr.jid = occupant.jid; end
+                                       elseif not item.attr.nick and item.attr.jid then
+                                               local nick = self._jid_nick[item.attr.jid];
+                                               if nick then item.attr.nick = select(3, jid_split(nick)); end
                                        end
                                        local reason = item.tags[1] and item.tags[1].name == "reason" and #item.tags[1] == 1 and item.tags[1][1];
                                        if item.attr.affiliation and item.attr.jid and not item.attr.role then
@@ -494,9 +686,14 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                                        -- TODO allow admins and owners not in room? Provide read-only access to everyone who can see the participants anyway?
                                                        if _rol == "none" then _rol = nil; end
                                                        local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
-                                                       for nick, occupant in pairs(self._occupants) do
+                                                       for occupant_jid, occupant in pairs(self._occupants) do
                                                                if occupant.role == _rol then
-                                                                       reply:tag("item", {nick = nick, role = _rol or "none", affiliation = occupant.affiliation or "none", jid = occupant.jid}):up();
+                                                                       reply:tag("item", {
+                                                                               nick = select(3, jid_split(occupant_jid)),
+                                                                               role = _rol or "none",
+                                                                               affiliation = occupant.affiliation or "none",
+                                                                               jid = occupant.jid
+                                                                               }):up();
                                                                end
                                                        end
                                                        origin.send(reply);
@@ -511,7 +708,30 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                origin.send(st.error_reply(stanza, "cancel", "bad-request"));
                        end
                elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then
-                       self:handle_form(origin, stanza);
+                       if self:get_affiliation(stanza.attr.from) ~= "owner" then
+                               origin.send(st.error_reply(stanza, "auth", "forbidden"));
+                       elseif stanza.attr.type == "get" then
+                               self:send_form(origin, stanza);
+                       elseif stanza.attr.type == "set" then
+                               local child = stanza.tags[1].tags[1];
+                               if not child then
+                                       origin.send(st.error_reply(stanza, "auth", "bad-request"));
+                               elseif child.name == "destroy" then
+                                       local newjid = child.attr.jid;
+                                       local reason, password;
+                                       for _,tag in ipairs(child.tags) do
+                                               if tag.name == "reason" then
+                                                       reason = #tag.tags == 0 and tag[1];
+                                               elseif tag.name == "password" then
+                                                       password = #tag.tags == 0 and tag[1];
+                                               end
+                                       end
+                                       self:destroy(newjid, reason, password);
+                                       origin.send(st.reply(stanza));
+                               else
+                                       self:process_form(origin, stanza);
+                               end
+                       end
                elseif type == "set" or type == "get" then
                        origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                end
@@ -519,14 +739,22 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                local from, to = stanza.attr.from, stanza.attr.to;
                local room = jid_bare(to);
                local current_nick = self._jid_nick[from];
-               if not current_nick then -- not in room
+               local occupant = self._occupants[current_nick];
+               if not occupant then -- not in room
                        origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
+               elseif occupant.role == "visitor" then
+                       origin.send(st.error_reply(stanza, "cancel", "forbidden"));
                else
                        local from = stanza.attr.from;
                        stanza.attr.from = current_nick;
                        local subject = getText(stanza, {"subject"});
                        if subject then
-                               self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
+                               if occupant.role == "moderator" then
+                                       self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
+                               else
+                                       stanza.attr.from = from;
+                                       origin.send(st.error_reply(stanza, "cancel", "forbidden"));
+                               end
                        else
                                self:broadcast_message(stanza, true);
                        end
@@ -535,8 +763,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
        elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
                local current_nick = self._jid_nick[stanza.attr.from];
                log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
-               self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
-                       :tag('status'):text('Kicked: '..get_error_condition(stanza))); -- send unavailable
+               self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable
        elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
                local to = stanza.attr.to;
                local current_nick = self._jid_nick[stanza.attr.from];
@@ -560,8 +787,11 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
                                                :tag('invite', {from=_from})
                                                        :tag('reason'):text(_reason or ""):up()
-                                               :up()
-                                       :up()
+                                               :up();
+                                               if self:get_password() then
+                                                       invite:tag("password"):text(self:get_password()):up();
+                                               end
+                                       invite:up()
                                        :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this
                                                :text(_reason or "")
                                        :up()
@@ -654,21 +884,37 @@ function room_mt:get_role(nick)
        local session = self._occupants[nick];
        return session and session.role or nil;
 end
-function room_mt:set_role(actor, nick, role, callback, reason)
+function room_mt:can_set_role(actor_jid, occupant_jid, role)
+       local actor = self._occupants[self._jid_nick[actor_jid]];
+       local occupant = self._occupants[occupant_jid];
+       
+       if not occupant or not actor then return nil, "modify", "not-acceptable"; end
+
+       if actor.role == "moderator" then
+               if occupant.affiliation ~= "owner" and occupant.affiliation ~= "admin" then
+                       if actor.affiliation == "owner" or actor.affiliation == "admin" then
+                               return true;
+                       elseif occupant.role ~= "moderator" and role ~= "moderator" then
+                               return true;
+                       end
+               end
+       end
+       return nil, "cancel", "not-allowed";
+end
+function room_mt:set_role(actor, occupant_jid, role, callback, reason)
        if role == "none" then role = nil; end
        if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end
-       if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end
-       local occupant = self._occupants[nick];
-       if not occupant then return nil, "modify", "not-acceptable"; end
-       if occupant.affiliation == "owner" or occupant.affiliation == "admin" then return nil, "cancel", "not-allowed"; end
-       local p = st.presence({from = nick})
+       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"})
-                       :tag("item", {affiliation=occupant.affiliation or "none", nick=nick, role=role or "none"})
+                       :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();
        if not role then -- kick
                p.attr.type = "unavailable";
-               self._occupants[nick] = nil;
+               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
@@ -681,7 +927,7 @@ function room_mt:set_role(actor, nick, role, callback, reason)
                self:_route_stanza(p);
        end
        if callback then callback(); end
-       self:broadcast_except_nick(p, nick);
+       self:broadcast_except_nick(p, occupant_jid);
        return true;
 end
 
@@ -691,13 +937,11 @@ function room_mt:_route_stanza(stanza)
        local from_occupant = self._occupants[stanza.attr.from];
        if stanza.name == "presence" then
                if to_occupant and from_occupant then
-                       if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then
-                               for i=#stanza.tags,1,-1 do
-                                       local tag = stanza.tags[i];
-                                       if tag.name == "x" and tag.attr.xmlns == "http://jabber.org/protocol/muc#user" then
-                                               muc_child = tag;
-                                               break;
-                                       end
+                       if self._data.whois == 'anyone' then
+                           muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
+                       else
+                               if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then
+                                       muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
                                end
                        end
                end
@@ -712,6 +956,9 @@ 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
@@ -730,7 +977,9 @@ function _M.new_room(jid)
                jid = jid;
                _jid_nick = {};
                _occupants = {};
-               _data = {};
+               _data = {
+                   whois = 'moderators',
+               };
                _affiliations = {};
        }, room_mt);
 end