Merge with 0.4
[prosody.git] / plugins / mod_muc.lua
index 1f8d04e99586be9f4dd9aaa32dae39f917f7bd87..38f21b4a43db28ef68d86396bb400edd9d95a2db 100644 (file)
@@ -1,4 +1,4 @@
--- Prosody IM v0.3
+-- Prosody IM v0.4
 -- Copyright (C) 2008-2009 Matthew Wild
 -- Copyright (C) 2008-2009 Waqas Hussain
 -- 
@@ -6,6 +6,8 @@
 -- COPYING file in the source package for more information.
 --
 
+local datamanager = require "util.datamanager";
+local datetime = require "util.datetime";
 
 local register_component = require "core.componentmanager".register_component;
 local deregister_component = require "core.componentmanager".deregister_component;
@@ -21,7 +23,7 @@ if module:get_host_type() ~= "component" then
 end
 
 local muc_domain = module:get_host();
-local muc_name = "MUCMUCMUC!!!";
+local muc_name = "Chatrooms";
 local history_length = 20;
 
 -- room_name -> room
@@ -71,6 +73,30 @@ local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabbe
 function get_filtered_presence(stanza)
        return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
 end
+local kickable_error_conditions = {
+       ["gone"] = true;
+       ["internal-server-error"] = true;
+       ["item-not-found"] = true;
+       ["jid-malformed"] = true;
+       ["recipient-unavailable"] = true;
+       ["redirect"] = true;
+       ["remote-server-not-found"] = true;
+       ["remote-server-timeout"] = true;
+       ["service-unavailable"] = true;
+};
+function get_kickable_error(stanza)
+       for _, tag in ipairs(stanza.tags) do
+               if tag.name == "error" and 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 kickable_error_conditions[cond.name] and cond.name;
+                               end
+                       end
+                       return true; -- malformed error message
+               end
+       end
+       return true; -- malformed error message
+end
 function getUsingPath(stanza, path, getText)
        local tag = stanza;
        for _, name in ipairs(path) do
@@ -179,6 +205,34 @@ function broadcast_presence_stanza(room, stanza, code, nick)
                end
        end
 end
+function send_history(room, to)
+       local history = rooms_info:get(room, 'history'); -- send discussion history
+       if history then
+               for _, msg in ipairs(history) do
+                       msg = st.deserialize(msg);
+                       msg.attr.to=to;
+                       core_route_stanza(component, msg);
+               end
+       end
+       if rooms_info:get(room, 'subject') then
+               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;
@@ -206,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;
@@ -233,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')); -- 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
@@ -250,6 +304,7 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                        log("debug", "%s joining as %s", from, to);
                                        local data;
                                        if not rooms:get(room) and not rooms_info:get(room) then -- new room
+                                               rooms_info:set(room, 'name', (jid_split(room)));
                                                data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
                                        end
                                        if not data then -- new occupant
@@ -257,31 +312,10 @@ 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);
-                                       local history = rooms_info:get(room, 'history'); -- send discussion history
-                                       if history then
-                                               for _, msg in ipairs(history) do
-                                                       msg = st.deserialize(msg);
-                                                       msg.attr.to=from;
-                                                       core_route_stanza(component, msg);
-                                               end
-                                       end
-                                       if rooms_info:get(room, 'subject') then
-                                               core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
-                                       end
+                                       send_history(room, from);
                                end
                        end
                elseif type ~= 'result' then -- bad type
@@ -291,7 +325,7 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                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"));
-       elseif stanza.name == "message" and type == "error" then
+       elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
                log("debug", "%s kicked from %s for sending an error message", current_nick, room);
                handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable
        else -- private stanza
@@ -302,7 +336,7 @@ function handle_to_occupant(origin, stanza) -- PM, vCards, etc
                        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);
-               else -- recipient not in room
+               elseif type ~= "error" and type ~= "result" then -- recipient not in room
                        origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
                end
        end
@@ -342,9 +376,19 @@ function handle_to_room(origin, stanza) -- presence changes and groupchat messag
                        stanza.attr.to = current_nick;
                        handle_to_occupant(origin, stanza);
                        stanza.attr.to = to;
-               else
+               elseif type ~= "error" and type ~= "result" then
                        origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                end
+       elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and jid_nick:get(stanza.attr.from, stanza.attr.to)
+               and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1
+               and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then
+               local _from, _to = stanza.attr.from, stanza.attr.to;
+               local _invitee = stanza.tags[1].tags[1].attr.to;
+               stanza.attr.from, stanza.attr.to = _to, _invitee;
+               stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil;
+               core_route_stanza(component, stanza);
+               stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee;
+               stanza.attr.from, stanza.attr.to = _from, _to;
        else
                if type == "error" or type == "result" then return; end
                origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));