mod_pep: Use correct field for full JID (thanks Milan*)
[prosody.git] / plugins / mod_pep.lua
index 752cd28c414f019d2fb09701da6711fc61789dc7..46517b9585fc884d2569a34ee9408a21e3f88a45 100644 (file)
@@ -16,10 +16,15 @@ local next = next;
 local type = type;
 local calculate_hash = require "util.caps".calculate_hash;
 local core_post_stanza = prosody.core_post_stanza;
+local bare_sessions = prosody.bare_sessions;
 
+-- Used as canonical 'empty table'
 local NULL = {};
+-- data[user_bare_jid][node] = item_stanza
 local data = {};
+--- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true
 local recipients = {};
+-- hash_map[hash][subscribed_nodes] = true
 local hash_map = {};
 
 module.save = function()
@@ -41,11 +46,11 @@ local function subscription_presence(user_bare, recipient)
        return is_contact_subscribed(username, host, recipient_bare);
 end
 
-local function publish(session, node, id, item)
+module:hook("pep-publish-item", function (event)
+       local session, bare, node, id, item = event.session, event.user, event.node, event.id, event.item;
        item.attr.xmlns = nil;
        local disable = #item.tags ~= 1 or #item.tags[1] == 0;
        if #item.tags == 0 then item.name = "retract"; end
-       local bare = session.username..'@'..session.host;
        local stanza = st.message({from=bare, type='headline'})
                :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
                        :tag('items', {node=node})
@@ -72,7 +77,8 @@ local function publish(session, node, id, item)
                        core_post_stanza(session, stanza);
                end
        end
-end
+end);
+
 local function publish_all(user, recipient, session)
        local d = data[user];
        local notify = recipients[user] and recipients[user][recipient];
@@ -118,6 +124,9 @@ module:hook("presence/bare", function(event)
        local t = stanza.attr.type;
        local self = not stanza.attr.to;
 
+       -- Only cache subscriptions if user is online
+       if not bare_sessions[user] then return; end
+
        if not t then -- available presence
                if self or subscription_presence(user, stanza.attr.from) then
                        local recipient = stanza.attr.from;
@@ -172,7 +181,9 @@ module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
                                local id = payload.attr.id or "1";
                                payload.attr.id = id;
                                session.send(st.reply(stanza));
-                               publish(session, node, id, st.clone(payload));
+                               module:fire_event("pep-publish-item", {
+                                       node = node, user = jid_bare(session.full_jid), actor = session.jid, id = id, session = session, item = st.clone(payload);
+                               });
                                return true;
                        end
                end
@@ -279,3 +290,11 @@ module:hook("account-disco-items", function(event)
                end
        end
 end);
+
+module:hook("resource-unbind", function (event)
+       local user_bare_jid = event.session.username.."@"..event.session.host;
+       if not bare_sessions[user_bare_jid] then -- User went offline
+               -- We don't need this info cached anymore, clear it.
+               recipients[user_bare_jid] = nil;
+       end
+end);