2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local st = require "util.stanza";
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;
10 local load_roster = require "core.rostermanager".load_roster;
13 local recipients = {};
15 module:add_identity("pubsub", "pep");
16 module:add_feature("http://jabber.org/protocol/pubsub#publish");
18 local function publish(session, node, item)
19 local disable = #item.tags ~= 1 or #item.tags[1].tags == 0;
20 local bare = session.username..'@'..session.host;
21 local stanza = st.message({from=bare, type='headline'})
22 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
23 :tag('items', {node=node})
28 -- store for the future
29 local user_data = data[bare];
31 if user_data then user_data[node] = nil; end
32 if not next(user_data) then data[bare] = nil; end
34 if not user_data then user_data = {}; data[bare] = user_data; end
35 user_data[node] = stanza;
38 -- broadcast to resources
39 stanza.attr.to = bare;
40 core_route_stanza(session, stanza);
42 -- broadcast to contacts
43 for jid, item in pairs(session.roster) do
44 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then
46 core_route_stanza(session, stanza);
51 local function get_caps_hash_from_presence(stanza, current)
52 if not stanza.attr.type then
53 for _, child in pairs(stanza.tags) do
54 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
55 local attr = child.attr;
56 if attr.hash then -- new caps
57 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
59 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
61 return; -- bad caps format
64 return current; -- no caps, could mean caps optimization, so return current
68 module:hook("presence/bare", function(event)
69 -- inbound presence to bare JID recieved
70 local origin, stanza = event.origin, event.stanza;
72 local user = stanza.attr.to or (origin.username..'@'..origin.host);
73 local bare = jid_bare(stanza.attr.from);
74 local item = load_roster(jid_split(user))[bare];
75 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
76 local t = stanza.attr.type;
77 local recipient = stanza.attr.from;
78 if t == "unavailable" or t == "error" then
79 if recipients[user] then recipients[user][recipient] = nil; end
81 recipients[user] = recipients[user] or {};
82 if not recipients[user][recipient] then
83 recipients[user][recipient] = true;
84 for node, message in pairs(data[user] or {}) do
85 message.attr.to = stanza.attr.from;
93 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
94 local session, stanza = event.origin, event.stanza;
95 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
96 local payload = stanza.tags[1];
97 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
98 payload = payload.tags[1];
99 if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
100 local node = payload.attr.node;
101 payload = payload.tags[1];
102 if payload then -- <item>
103 publish(session, node, payload);
111 module:hook("iq/bare/disco", function(event)
112 local session, stanza = event.origin, event.stanza;
113 if stanza.attr.type == "result" then
114 local disco = stanza.tags[1];
115 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
116 -- Process disco response
117 -- TODO check if waiting for recipient's response
118 local hash; -- TODO calculate hash
119 -- TODO update hash map
120 -- TODO set recipient's data to calculated data