net.http.server: Properly handle persistent connections
[prosody.git] / plugins / mod_pep.lua
index b7f20f2b87c4ab6ce4630d15edf061a18725f5b3..a65ee903f578fbdc485fcb402b39c872102c7578 100644 (file)
 local jid_bare = require "util.jid".bare;
 local jid_split = require "util.jid".split;
 local st = require "util.stanza";
-local hosts = hosts;
-local user_exists = require "core.usermanager".user_exists;
 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
-local pairs, ipairs = pairs, ipairs;
+local pairs = pairs;
 local next = next;
 local type = type;
 local calculate_hash = require "util.caps".calculate_hash;
+local core_post_stanza = prosody.core_post_stanza;
 
 local NULL = {};
 local data = {};
@@ -32,7 +31,7 @@ module.restore = function(state)
        hash_map = state.hash_map or {};
 end
 
-module:add_identity("pubsub", "pep", "Prosody");
+module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
 module:add_feature("http://jabber.org/protocol/pubsub#publish");
 
 local function subscription_presence(user_bare, recipient)
@@ -117,13 +116,14 @@ module:hook("presence/bare", function(event)
        local origin, stanza = event.origin, event.stanza;
        local user = stanza.attr.to or (origin.username..'@'..origin.host);
        local t = stanza.attr.type;
+       local self = not stanza.attr.to;
 
        if not t then -- available presence
-               if not stanza.attr.to or subscription_presence(user, stanza.attr.from) then
+               if self or subscription_presence(user, stanza.attr.from) then
                        local recipient = stanza.attr.from;
                        local current = recipients[user] and recipients[user][recipient];
                        local hash = get_caps_hash_from_presence(stanza, current);
-                       if current == hash then return; end
+                       if current == hash or (current and current == hash_map[hash]) then return; end
                        if not hash then
                                if recipients[user] then recipients[user][recipient] = nil; end
                        else
@@ -133,15 +133,29 @@ module:hook("presence/bare", function(event)
                                        publish_all(user, recipient, origin);
                                else
                                        recipients[user][recipient] = hash;
-                                       origin.send(
-                                               st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
-                                                       :query("http://jabber.org/protocol/disco#info")
-                                       );
+                                       local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
+                                       if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
+                                               -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
+                                               origin.send(
+                                                       st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
+                                                               :query("http://jabber.org/protocol/disco#info")
+                                               );
+                                       end
                                end
                        end
                end
        elseif t == "unavailable" then
                if recipients[user] then recipients[user][stanza.attr.from] = nil; end
+       elseif not self and t == "unsubscribe" then
+               local from = jid_bare(stanza.attr.from);
+               local subscriptions = recipients[user];
+               if subscriptions then
+                       for subscriber in pairs(subscriptions) do
+                               if jid_bare(subscriber) == from then
+                                       recipients[user][subscriber] = nil;
+                               end
+                       end
+               end
        end
 end, 10);
 
@@ -214,6 +228,7 @@ module:hook("iq-result/bare/disco", function(event)
                local disco = stanza.tags[1];
                if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
                        -- Process disco response
+                       local self = not stanza.attr.to;
                        local user = stanza.attr.to or (session.username..'@'..session.host);
                        local contact = stanza.attr.from;
                        local current = recipients[user] and recipients[user][contact];
@@ -230,6 +245,15 @@ module:hook("iq-result/bare/disco", function(event)
                                end
                        end
                        hash_map[ver] = notify; -- update hash map
+                       if self then
+                               for jid, item in pairs(session.roster) do -- for all interested contacts
+                                       if item.subscription == "both" or item.subscription == "from" then
+                                               if not recipients[jid] then recipients[jid] = {}; end
+                                               recipients[jid][contact] = notify;
+                                               publish_all(jid, contact, session);
+                                       end
+                               end
+                       end
                        recipients[user][contact] = notify; -- set recipient's data to calculated data
                        -- send messages to recipient
                        publish_all(user, contact, session);