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;
18 local recipients = {};
21 function module.save()
22 return { services = services };
25 function module.restore(data)
26 services = data.services;
29 local function subscription_presence(user_bare, recipient)
30 local recipient_bare = jid_bare(recipient);
31 if (recipient_bare == user_bare) then return true; end
32 local username, host = jid_split(user_bare);
33 return is_contact_subscribed(username, host, recipient_bare);
36 local function get_broadcaster(name)
37 local function simple_broadcast(kind, node, jids, item)
39 item = st.clone(item);
40 item.attr.xmlns = nil; -- Clear the pubsub namespace
42 local message = st.message({ from = name, type = "headline" })
43 :tag("event", { xmlns = xmlns_pubsub_event })
44 :tag(kind, { node = node })
46 for jid in pairs(jids) do
47 module:log("debug", "Sending notification to %s from %s: %s", jid, name, tostring(item));
48 message.attr.to = jid;
52 return simple_broadcast;
55 function get_pep_service(name)
56 if services[name] then
57 return services[name];
59 services[name] = pubsub.new({
69 get_subscription = false;
70 get_subscriptions = false;
73 subscribe_other = false;
74 unsubscribe_other = false;
75 get_subscription_other = false;
76 get_subscriptions_other = false;
79 be_unsubscribed = true;
81 set_affiliation = false;
91 get_subscription = true;
92 get_subscriptions = true;
95 subscribe_other = false;
96 unsubscribe_other = false;
97 get_subscription_other = false;
98 get_subscriptions_other = false;
100 be_subscribed = true;
101 be_unsubscribed = true;
103 set_affiliation = false;
113 get_subscription = true;
114 get_subscriptions = true;
117 subscribe_other = false;
118 unsubscribe_other = false;
119 get_subscription_other = false;
120 get_subscriptions_other = false;
122 be_subscribed = true;
123 be_unsubscribed = true;
125 set_affiliation = false;
136 get_subscription = true;
137 get_subscriptions = true;
141 subscribe_other = true;
142 unsubscribe_other = true;
143 get_subscription_other = true;
144 get_subscriptions_other = true;
146 be_subscribed = true;
147 be_unsubscribed = true;
149 set_affiliation = true;
153 autocreate_on_publish = true;
154 autocreate_on_subscribe = true;
156 broadcaster = get_broadcaster(name);
157 get_affiliation = function (jid)
158 if jid_bare(jid) == name then
160 elseif subscription_presence(name, jid) then
165 normalize_jid = jid_bare;
167 return services[name];
170 function handle_pubsub_iq(event)
171 local origin, stanza = event.origin, event.stanza;
172 local pubsub = stanza.tags[1];
173 local action = pubsub.tags[1];
175 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
177 local service_name = stanza.attr.to or origin.username.."@"..origin.host
178 local service = get_pep_service(service_name);
179 local handler = handlers[stanza.attr.type.."_"..action.name];
181 handler(origin, stanza, action, service);
186 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
187 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
189 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
190 module:add_feature("http://jabber.org/protocol/pubsub#publish");
192 local function get_caps_hash_from_presence(stanza, current)
193 local t = stanza.attr.type;
195 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
197 local attr = child.attr;
198 if attr.hash then -- new caps
199 if attr.hash == 'sha-1' and attr.node and attr.ver then
200 return attr.ver, attr.node.."#"..attr.ver;
203 if attr.node and attr.ver then
204 return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
208 return; -- no or bad caps
209 elseif t == "unavailable" or t == "error" then
212 return current; -- no caps, could mean caps optimization, so return current
215 local function resend_last_item(jid, node, service)
216 local ok, items = service:get_items(node, jid);
217 if not ok then return; end
218 for i, id in ipairs(items) do
219 service.config.broadcaster("items", node, { [jid] = true }, items[id]);
223 local function update_subscriptions(recipient, service_name, nodes)
224 local service = get_pep_service(service_name);
226 recipients[service_name] = recipients[service_name] or {};
227 nodes = nodes or set_new();
228 local old = recipients[service_name][recipient];
230 if old and type(old) == table then
231 for node in pairs((old - nodes):items()) do
232 service:remove_subscription(node, recipient, recipient);
236 for node in nodes:items() do
237 service:add_subscription(node, recipient, recipient);
238 resend_last_item(recipient, node, service);
240 recipients[service_name][recipient] = nodes;
243 module:hook("presence/bare", function(event)
244 -- inbound presence to bare JID recieved
245 local origin, stanza = event.origin, event.stanza;
246 local user = stanza.attr.to or (origin.username..'@'..origin.host);
247 local t = stanza.attr.type;
248 local self = not stanza.attr.to;
249 local service = get_pep_service(user);
251 if not t then -- available presence
252 if self or subscription_presence(user, stanza.attr.from) then
253 local recipient = stanza.attr.from;
254 local current = recipients[user] and recipients[user][recipient];
255 local hash, query_node = get_caps_hash_from_presence(stanza, current);
256 if current == hash or (current and current == hash_map[hash]) then return; end
258 update_subscriptions(recipient, user);
260 recipients[user] = recipients[user] or {};
261 if hash_map[hash] then
262 update_subscriptions(recipient, user, hash_map[hash]);
264 recipients[user][recipient] = hash;
265 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
266 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
267 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
269 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
270 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
276 elseif t == "unavailable" then
277 update_subscriptions(stanza.attr.from, user);
278 elseif not self and t == "unsubscribe" then
279 local from = jid_bare(stanza.attr.from);
280 local subscriptions = recipients[user];
281 if subscriptions then
282 for subscriber in pairs(subscriptions) do
283 if jid_bare(subscriber) == from then
284 update_subscriptions(subscriber, user);
291 module:hook("iq-result/bare/disco", function(event)
292 local origin, stanza = event.origin, event.stanza;
293 local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
298 -- Process disco response
299 local self = not stanza.attr.to;
300 local user = stanza.attr.to or (origin.username..'@'..origin.host);
301 local contact = stanza.attr.from;
302 local current = recipients[user] and recipients[user][contact];
303 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
305 if not string.find(current, "#") then
306 ver = calculate_hash(disco.tags); -- calculate hash
308 local notify = set_new();
309 for _, feature in pairs(disco.tags) do
310 if feature.name == "feature" and feature.attr.var then
311 local nfeature = feature.attr.var:match("^(.*)%+notify$");
312 if nfeature then notify:add(nfeature); end
315 hash_map[ver] = notify; -- update hash map
317 for jid, item in pairs(origin.roster) do -- for all interested contacts
318 if item.subscription == "both" or item.subscription == "from" then
319 if not recipients[jid] then recipients[jid] = {}; end
320 update_subscriptions(contact, jid, notify);
324 update_subscriptions(contact, user, notify);
327 module:hook("account-disco-info-node", function(event)
328 local reply, stanza, origin = event.reply, event.stanza, event.origin;
329 local service_name = stanza.attr.to or origin.username.."@"..origin.host
330 local service = get_pep_service(service_name);
331 local node = event.node;
332 local ok = service:get_items(node, jid_bare(stanza.attr.from) or true);
333 if not ok then return; end
335 reply:tag('identity', {category='pubsub', type='leaf'}):up();
338 module:hook("account-disco-info", function(event)
339 local reply = event.reply;
340 reply:tag('identity', {category='pubsub', type='pep'}):up();
341 reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
344 module:hook("account-disco-items-node", function(event)
345 local reply, stanza, origin = event.reply, event.stanza, event.origin;
346 local node = event.node;
347 local service_name = stanza.attr.to or origin.username.."@"..origin.host
348 local service = get_pep_service(service_name);
349 local ok, ret = service:get_items(node, jid_bare(stanza.attr.from) or true);
350 if not ok then return; end
352 for _, id in ipairs(ret) do
353 reply:tag("item", { jid = service_name, name = id }):up();
357 module:hook("account-disco-items", function(event)
358 local reply, stanza, origin = event.reply, event.stanza, event.origin;
360 local service_name = reply.attr.from or origin.username.."@"..origin.host
361 local service = get_pep_service(service_name);
362 local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
363 if not ok then return; end
365 for node, node_obj in pairs(ret) do
366 reply:tag("item", { jid = service_name, node = node, name = node_obj.config.name }):up();