mod_pep: Added some comments
[prosody.git] / plugins / mod_pep.lua
1
2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local st = require "util.stanza";
5 local hosts = hosts;
6 local user_exists = require "core.usermanager".user_exists;
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
8 local pairs, ipairs = pairs, ipairs;
9 local load_roster = require "core.rostermanager".load_roster;
10
11 local data = {};
12 local recipients = {};
13
14 module:add_identity("pubsub", "pep");
15 module:add_feature("http://jabber.org/protocol/pubsub#publish");
16
17 local function publish(session, node, item)
18         local stanza = st.message({from=session.full_jid, type='headline'})
19                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
20                         :tag('items', {node=node})
21                                 :add_child(item)
22                         :up()
23                 :up();
24
25         local bare = session.username..'@'..session.host;
26         -- store for the future
27         local user_data = data[bare];
28         if not user_data then user_data = {}; data[bare] = user_data; end
29         user_data[node] = stanza;
30         
31         -- broadcast to resources
32         stanza.attr.to = bare;
33         core_route_stanza(session, stanza);
34
35         -- broadcast to contacts
36         for jid, item in pairs(session.roster) do
37                 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then
38                         stanza.attr.to = jid;
39                         core_route_stanza(session, stanza);
40                 end
41         end
42 end
43
44 module:hook("presence/bare", function(event)
45         -- inbound presence to bare JID recieved
46         local origin, stanza = event.origin, event.stanza;
47         
48         local user = stanza.attr.to or (origin.username..'@'..origin.host);
49         local bare = jid_bare(stanza.attr.from);
50         local item = load_roster(jid_split(user))[bare];
51         if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
52                 local t = stanza.attr.type;
53                 local recipient = stanza.attr.from;
54                 if t == "unavailable" or t == "error" then
55                         if recipients[user] then recipients[user][recipient] = nil; end
56                 elseif not t then
57                         recipients[user] = recipients[user] or {};
58                         if not recipients[user][recipient] then
59                                 recipients[user][recipient] = true;
60                                 for node, message in pairs(data[user] or {}) do
61                                         message.attr.to = stanza.attr.from;
62                                         origin.send(message);
63                                 end
64                         end
65                 end
66         end
67 end, 10);
68
69 module:add_iq_handler("c2s", "http://jabber.org/protocol/pubsub", function (session, stanza)
70         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
71                 local payload = stanza.tags[1];
72                 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
73                         payload = payload.tags[1];
74                         if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
75                                 local node = payload.attr.node;
76                                 payload = payload.tags[1];
77                                 if payload then -- <item>
78                                         publish(session, node, payload);
79                                         return true;
80                                 end -- TODO else error
81                         end -- TODO else error
82                 end
83         end
84         session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
85 end);
86