Merge with trunk
[prosody.git] / plugins / mod_muc.lua
index c07102c9b7b5debf301d2c2df548ee08865c1175..a7b8db5fae46778d4d8dfba3d76e36cea5397a9a 100644 (file)
@@ -205,7 +205,7 @@ function broadcast_presence_stanza(room, stanza, code, nick)
                end
        end
 end
-function broadcast_history(room, to)
+function send_history(room, to)
        local history = rooms_info:get(room, 'history'); -- send discussion history
        if history then
                for _, msg in ipairs(history) do
@@ -218,6 +218,21 @@ function broadcast_history(room, to)
                core_route_stanza(component, st.message({type='groupchat', from=room, to=to}):tag("subject"):text(rooms_info:get(room, 'subject')));
        end
 end
+function send_occupant_list(room, to)
+       local r = rooms:get(room);
+       if r then
+               local current_nick = jid_nick:get(to, room);
+               for occupant, o_data in pairs(r) do
+                       if occupant ~= current_nick then
+                               local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
+                               pres.attr.to, pres.attr.from = to, occupant;
+                               pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+                                       :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
+                               core_route_stanza(component, pres);
+                       end
+               end
+       end
+end
 
 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
        local from, to = stanza.attr.from, stanza.attr.to;
@@ -245,7 +260,7 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                        end
                elseif not type then -- available
                        if current_nick then
-                               if #pr == #stanza or current_nick ~= to then
+                               --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
                                        if current_nick == to then -- simple presence
                                                log("debug", "%s broadcasted presence", current_nick);
                                                rooms:get(room, current_nick).sessions[from] = pr;
@@ -272,11 +287,11 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                                        end
                                                end
                                        end
-                               else -- possible rejoin
-                                       log("debug", "%s had connection replaced", current_nick);
-                                       handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection'):up()); -- send unavailable
-                                       handle_to_occupant(origin, stanza); -- resend available
-                               end
+                               --else -- possible rejoin
+                               --      log("debug", "%s had connection replaced", current_nick);
+                               --      handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection'):up()); -- send unavailable
+                               --      handle_to_occupant(origin, stanza); -- resend available
+                               --end
                        else -- enter room
                                local new_nick = to;
                                if rooms:get(room, to) then
@@ -297,27 +312,16 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                        end
                                        rooms:set(room, to, data);
                                        jid_nick:set(from, room, to);
-                                       local r = rooms:get(room);
-                                       if r then
-                                               for occupant, o_data in pairs(r) do
-                                                       if occupant ~= to then
-                                                               local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
-                                                               pres.attr.to, pres.attr.from = from, occupant;
-                                                               pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
-                                                                       :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
-                                                               core_route_stanza(component, pres);
-                                                       end
-                                               end
-                                       end
+                                       send_occupant_list(room, from);
                                        pr.attr.from = to;
                                        broadcast_presence_stanza(room, pr);
-                                       broadcast_history(room, from);
+                                       send_history(room, from);
                                end
                        end
                elseif type ~= 'result' then -- bad type
                        origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
                end
-       elseif not current_nick and type ~= "error" then -- not in room
+       elseif not current_nick and type ~= "error" and type ~= "result" then -- not in room
                origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
        elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
                origin.send(st.error_reply(stanza, "modify", "bad-request"));
@@ -329,7 +333,7 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                if o_data then
                        log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
                        local jid = o_data.jid;
-                       if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
+                       -- TODO if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
                        stanza.attr.to, stanza.attr.from = jid, current_nick;
                        core_route_stanza(component, stanza);
                elseif type ~= "error" and type ~= "result" then -- recipient not in room