tools/ejabberd2prosody: Disable generating a config, as the format it generates is...
[prosody.git] / plugins / mod_pep.lua
index 0d42ac0f3e99d26956437433ecb3ebeb4833968b..bdb742e3a458f22f2b856616b406ea3e99e840af 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)
@@ -63,7 +62,7 @@ local function publish(session, node, id, item)
                end
        else
                if not user_data then user_data = {}; data[bare] = user_data; end
-               user_data[node] = {id or "1", item};
+               user_data[node] = {id, item};
        end
 
        -- broadcast
@@ -124,7 +123,7 @@ module:hook("presence/bare", function(event)
                        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
@@ -134,9 +133,11 @@ module:hook("presence/bare", function(event)
                                        publish_all(user, recipient, origin);
                                else
                                        recipients[user][recipient] = hash;
-                                       if self or origin.type ~= "c2s" then
+                                       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 and Asterisk 1.8 can't deal with missing from attribute
                                                origin.send(
-                                                       st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
+                                                       st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
                                                                :query("http://jabber.org/protocol/disco#info")
                                                );
                                        end
@@ -145,6 +146,16 @@ module:hook("presence/bare", function(event)
                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);
 
@@ -158,7 +169,8 @@ module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
                        local node = payload.attr.node;
                        payload = payload.tags[1];
                        if payload and payload.name == "item" then -- <item>
-                               local id = payload.attr.id;
+                               local id = payload.attr.id or "1";
+                               payload.attr.id = id;
                                session.send(st.reply(stanza));
                                publish(session, node, id, st.clone(payload));
                                return true;