1 local pubsub = require "util.pubsub";
2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local set_new = require "util.set".new;
5 local st = require "util.stanza";
6 local calculate_hash = require "util.caps".calculate_hash;
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
9 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
11 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
13 local lib_pubsub = module:require "pubsub";
14 local handlers = lib_pubsub.handlers;
15 local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
17 local empty_set = set_new();
20 local recipients = {};
23 function module.save()
24 return { services = services };
27 function module.restore(data)
28 services = data.services;
31 local function subscription_presence(user_bare, recipient)
32 local recipient_bare = jid_bare(recipient);
33 if (recipient_bare == user_bare) then return true; end
34 local username, host = jid_split(user_bare);
35 return is_contact_subscribed(username, host, recipient_bare);
38 local function get_broadcaster(name)
39 local function simple_broadcast(kind, node, jids, item)
41 item = st.clone(item);
42 item.attr.xmlns = nil; -- Clear the pubsub namespace
44 local message = st.message({ from = name, type = "headline" })
45 :tag("event", { xmlns = xmlns_pubsub_event })
46 :tag(kind, { node = node })
48 for jid in pairs(jids) do
49 module:log("debug", "Sending notification to %s from %s: %s", jid, name, tostring(item));
50 message.attr.to = jid;
54 return simple_broadcast;
57 function get_pep_service(name)
58 if services[name] then
59 return services[name];
61 services[name] = pubsub.new({
71 get_subscription = false;
72 get_subscriptions = false;
75 subscribe_other = false;
76 unsubscribe_other = false;
77 get_subscription_other = false;
78 get_subscriptions_other = false;
81 be_unsubscribed = true;
83 set_affiliation = false;
93 get_subscription = true;
94 get_subscriptions = true;
97 subscribe_other = false;
98 unsubscribe_other = false;
99 get_subscription_other = false;
100 get_subscriptions_other = false;
102 be_subscribed = true;
103 be_unsubscribed = true;
105 set_affiliation = false;
115 get_subscription = true;
116 get_subscriptions = true;
119 subscribe_other = false;
120 unsubscribe_other = false;
121 get_subscription_other = false;
122 get_subscriptions_other = false;
124 be_subscribed = true;
125 be_unsubscribed = true;
127 set_affiliation = false;
138 get_subscription = true;
139 get_subscriptions = true;
143 subscribe_other = true;
144 unsubscribe_other = true;
145 get_subscription_other = true;
146 get_subscriptions_other = true;
148 be_subscribed = true;
149 be_unsubscribed = true;
151 set_affiliation = true;
155 autocreate_on_publish = true;
156 autocreate_on_subscribe = true;
158 broadcaster = get_broadcaster(name);
159 get_affiliation = function (jid)
160 if jid_bare(jid) == name then
162 elseif subscription_presence(name, jid) then
167 normalize_jid = jid_bare;
169 return services[name];
172 function handle_pubsub_iq(event)
173 local origin, stanza = event.origin, event.stanza;
174 local pubsub = stanza.tags[1];
175 local action = pubsub.tags[1];
177 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
179 local service_name = stanza.attr.to or origin.username.."@"..origin.host
180 local service = get_pep_service(service_name);
181 local handler = handlers[stanza.attr.type.."_"..action.name];
183 handler(origin, stanza, action, service);
188 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
189 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
191 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
192 module:add_feature("http://jabber.org/protocol/pubsub#publish");
194 local function get_caps_hash_from_presence(stanza, current)
195 local t = stanza.attr.type;
197 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
199 local attr = child.attr;
200 if attr.hash then -- new caps
201 if attr.hash == 'sha-1' and attr.node and attr.ver then
202 return attr.ver, attr.node.."#"..attr.ver;
205 if attr.node and attr.ver then
206 return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
210 return; -- no or bad caps
211 elseif t == "unavailable" or t == "error" then
214 return current; -- no caps, could mean caps optimization, so return current
217 local function resend_last_item(jid, node, service)
218 local ok, items = service:get_items(node, jid);
219 if not ok then return; end
220 for i, id in ipairs(items) do
221 service.config.broadcaster("items", node, { [jid] = true }, items[id]);
225 local function update_subscriptions(recipient, service_name, nodes)
226 local service = get_pep_service(service_name);
227 nodes = nodes or empty_set;
229 local service_recipients = recipients[service_name];
230 if not service_recipients then
231 service_recipients = {};
232 recipients[service_name] = service_recipients;
235 local current = service_recipients[recipient];
236 if not current or type(current) ~= "table" then
240 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
244 for node in current - nodes do
245 service:remove_subscription(node, recipient, recipient);
248 for node in nodes - current do
249 service:add_subscription(node, recipient, recipient);
250 resend_last_item(recipient, node, service);
253 if nodes == empty_set or nodes:empty() then
257 service_recipients[recipient] = nodes;
260 module:hook("presence/bare", function(event)
261 -- inbound presence to bare JID recieved
262 local origin, stanza = event.origin, event.stanza;
263 local user = stanza.attr.to or (origin.username..'@'..origin.host);
264 local t = stanza.attr.type;
265 local self = not stanza.attr.to;
266 local service = get_pep_service(user);
268 if not t then -- available presence
269 if self or subscription_presence(user, stanza.attr.from) then
270 local recipient = stanza.attr.from;
271 local current = recipients[user] and recipients[user][recipient];
272 local hash, query_node = get_caps_hash_from_presence(stanza, current);
273 if current == hash or (current and current == hash_map[hash]) then return; end
275 update_subscriptions(recipient, user);
277 recipients[user] = recipients[user] or {};
278 if hash_map[hash] then
279 update_subscriptions(recipient, user, hash_map[hash]);
281 recipients[user][recipient] = hash;
282 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
283 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
284 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
286 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
287 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
293 elseif t == "unavailable" then
294 update_subscriptions(stanza.attr.from, user);
295 elseif not self and t == "unsubscribe" then
296 local from = jid_bare(stanza.attr.from);
297 local subscriptions = recipients[user];
298 if subscriptions then
299 for subscriber in pairs(subscriptions) do
300 if jid_bare(subscriber) == from then
301 update_subscriptions(subscriber, user);
308 module:hook("iq-result/bare/disco", function(event)
309 local origin, stanza = event.origin, event.stanza;
310 local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
315 -- Process disco response
316 local self = not stanza.attr.to;
317 local user = stanza.attr.to or (origin.username..'@'..origin.host);
318 local contact = stanza.attr.from;
319 local current = recipients[user] and recipients[user][contact];
320 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
322 if not string.find(current, "#") then
323 ver = calculate_hash(disco.tags); -- calculate hash
325 local notify = set_new();
326 for _, feature in pairs(disco.tags) do
327 if feature.name == "feature" and feature.attr.var then
328 local nfeature = feature.attr.var:match("^(.*)%+notify$");
329 if nfeature then notify:add(nfeature); end
332 hash_map[ver] = notify; -- update hash map
334 for jid, item in pairs(origin.roster) do -- for all interested contacts
335 if item.subscription == "both" or item.subscription == "from" then
336 if not recipients[jid] then recipients[jid] = {}; end
337 update_subscriptions(contact, jid, notify);
341 update_subscriptions(contact, user, notify);
344 module:hook("account-disco-info-node", function(event)
345 local reply, stanza, origin = event.reply, event.stanza, event.origin;
346 local service_name = stanza.attr.to or origin.username.."@"..origin.host
347 local service = get_pep_service(service_name);
348 local node = event.node;
349 local ok = service:get_items(node, jid_bare(stanza.attr.from) or true);
350 if not ok then return; end
352 reply:tag('identity', {category='pubsub', type='leaf'}):up();
355 module:hook("account-disco-info", function(event)
356 local reply = event.reply;
357 reply:tag('identity', {category='pubsub', type='pep'}):up();
358 reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
361 module:hook("account-disco-items-node", function(event)
362 local reply, stanza, origin = event.reply, event.stanza, event.origin;
363 local node = event.node;
364 local service_name = stanza.attr.to or origin.username.."@"..origin.host
365 local service = get_pep_service(service_name);
366 local ok, ret = service:get_items(node, jid_bare(stanza.attr.from) or true);
367 if not ok then return; end
369 for _, id in ipairs(ret) do
370 reply:tag("item", { jid = service_name, name = id }):up();
374 module:hook("account-disco-items", function(event)
375 local reply, stanza, origin = event.reply, event.stanza, event.origin;
377 local service_name = reply.attr.from or origin.username.."@"..origin.host
378 local service = get_pep_service(service_name);
379 local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
380 if not ok then return; end
382 for node, node_obj in pairs(ret) do
383 reply:tag("item", { jid = service_name, node = node, name = node_obj.config.name }):up();