2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local jid_bare = require "util.jid".bare;
11 local jid_split = require "util.jid".split;
12 local st = require "util.stanza";
13 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
17 local calculate_hash = require "util.caps".calculate_hash;
18 local core_post_stanza = prosody.core_post_stanza;
19 local bare_sessions = prosody.bare_sessions;
21 -- Used as canonical 'empty table'
23 -- data[user_bare_jid][node] = item_stanza
25 --- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true
26 local recipients = {};
27 -- hash_map[hash][subscribed_nodes] = true
30 module.save = function()
31 return { data = data, recipients = recipients, hash_map = hash_map };
33 module.restore = function(state)
34 data = state.data or {};
35 recipients = state.recipients or {};
36 hash_map = state.hash_map or {};
39 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
40 module:add_feature("http://jabber.org/protocol/pubsub#publish");
42 local function subscription_presence(user_bare, recipient)
43 local recipient_bare = jid_bare(recipient);
44 if (recipient_bare == user_bare) then return true end
45 local username, host = jid_split(user_bare);
46 return is_contact_subscribed(username, host, recipient_bare);
49 module:hook("pep-publish-item", function (event)
50 local session, node, id, item = event.session, event.node, event.id, event.item;
51 item.attr.xmlns = nil;
52 local disable = #item.tags ~= 1 or #item.tags[1] == 0;
53 if #item.tags == 0 then item.name = "retract"; end
54 local bare = session.username..'@'..session.host;
55 local stanza = st.message({from=bare, type='headline'})
56 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
57 :tag('items', {node=node})
62 -- store for the future
63 local user_data = data[bare];
66 user_data[node] = nil;
67 if not next(user_data) then data[bare] = nil; end
70 if not user_data then user_data = {}; data[bare] = user_data; end
71 user_data[node] = {id, item};
75 for recipient, notify in pairs(recipients[bare] or NULL) do
77 stanza.attr.to = recipient;
78 core_post_stanza(session, stanza);
83 local function publish_all(user, recipient, session)
85 local notify = recipients[user] and recipients[user][recipient];
87 for node in pairs(notify) do
89 local id, item = unpack(d[node]);
90 session.send(st.message({from=user, to=recipient, type='headline'})
91 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
92 :tag('items', {node=node})
101 local function get_caps_hash_from_presence(stanza, current)
102 local t = stanza.attr.type;
104 for _, child in pairs(stanza.tags) do
105 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
106 local attr = child.attr;
107 if attr.hash then -- new caps
108 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
110 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
112 return; -- bad caps format
115 elseif t == "unavailable" or t == "error" then
118 return current; -- no caps, could mean caps optimization, so return current
121 module:hook("presence/bare", function(event)
122 -- inbound presence to bare JID recieved
123 local origin, stanza = event.origin, event.stanza;
124 local user = stanza.attr.to or (origin.username..'@'..origin.host);
125 local t = stanza.attr.type;
126 local self = not stanza.attr.to;
128 -- Only cache subscriptions if user is online
129 if not bare_sessions[user] then return; end
131 if not t then -- available presence
132 if self or subscription_presence(user, stanza.attr.from) then
133 local recipient = stanza.attr.from;
134 local current = recipients[user] and recipients[user][recipient];
135 local hash = get_caps_hash_from_presence(stanza, current);
136 if current == hash or (current and current == hash_map[hash]) then return; end
138 if recipients[user] then recipients[user][recipient] = nil; end
140 recipients[user] = recipients[user] or {};
141 if hash_map[hash] then
142 recipients[user][recipient] = hash_map[hash];
143 publish_all(user, recipient, origin);
145 recipients[user][recipient] = hash;
146 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
147 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
148 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
150 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
151 :query("http://jabber.org/protocol/disco#info")
157 elseif t == "unavailable" then
158 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
159 elseif not self and t == "unsubscribe" then
160 local from = jid_bare(stanza.attr.from);
161 local subscriptions = recipients[user];
162 if subscriptions then
163 for subscriber in pairs(subscriptions) do
164 if jid_bare(subscriber) == from then
165 recipients[user][subscriber] = nil;
172 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
173 local session, stanza = event.origin, event.stanza;
174 local payload = stanza.tags[1];
176 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
177 payload = payload.tags[1];
178 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
179 local node = payload.attr.node;
180 payload = payload.tags[1];
181 if payload and payload.name == "item" then -- <item>
182 local id = payload.attr.id or "1";
183 payload.attr.id = id;
184 session.send(st.reply(stanza));
185 module:fire_event("pep-publish-item", {
186 node = node, actor = session.jid, id = id, session = session, item = st.clone(payload);
191 elseif stanza.attr.type == 'get' then
192 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
193 if subscription_presence(user, stanza.attr.from) then
194 local user_data = data[user];
195 local node, requested_id;
196 payload = payload.tags[1];
197 if payload and payload.name == 'items' then
198 node = payload.attr.node;
199 local item = payload.tags[1];
200 if item and item.name == "item" then
201 requested_id = item.attr.id;
204 if node and user_data and user_data[node] then -- Send the last item
205 local id, item = unpack(user_data[node]);
206 if not requested_id or id == requested_id then
207 local stanza = st.reply(stanza)
208 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
209 :tag('items', {node=node})
213 session.send(stanza);
215 else -- requested item doesn't exist
216 local stanza = st.reply(stanza)
217 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
218 :tag('items', {node=node})
220 session.send(stanza);
223 elseif node then -- node doesn't exist
224 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
226 else --invalid request
227 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
230 else --no presence subscription
231 session.send(st.error_reply(stanza, 'auth', 'not-authorized')
232 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
238 module:hook("iq-result/bare/disco", function(event)
239 local session, stanza = event.origin, event.stanza;
240 if stanza.attr.type == "result" then
241 local disco = stanza.tags[1];
242 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
243 -- Process disco response
244 local self = not stanza.attr.to;
245 local user = stanza.attr.to or (session.username..'@'..session.host);
246 local contact = stanza.attr.from;
247 local current = recipients[user] and recipients[user][contact];
248 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
250 if not string.find(current, "#") then
251 ver = calculate_hash(disco.tags); -- calculate hash
254 for _, feature in pairs(disco.tags) do
255 if feature.name == "feature" and feature.attr.var then
256 local nfeature = feature.attr.var:match("^(.*)%+notify$");
257 if nfeature then notify[nfeature] = true; end
260 hash_map[ver] = notify; -- update hash map
262 for jid, item in pairs(session.roster) do -- for all interested contacts
263 if item.subscription == "both" or item.subscription == "from" then
264 if not recipients[jid] then recipients[jid] = {}; end
265 recipients[jid][contact] = notify;
266 publish_all(jid, contact, session);
270 recipients[user][contact] = notify; -- set recipient's data to calculated data
271 -- send messages to recipient
272 publish_all(user, contact, session);
277 module:hook("account-disco-info", function(event)
278 local reply = event.reply;
279 reply:tag('identity', {category='pubsub', type='pep'}):up();
280 reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
283 module:hook("account-disco-items", function(event)
284 local reply = event.reply;
285 local bare = reply.attr.to;
286 local user_data = data[bare];
289 for node, _ in pairs(user_data) do
290 reply:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
295 module:hook("resource-unbind", function (event)
296 local user_bare_jid = event.session.username.."@"..event.session.host;
297 if not bare_sessions[user_bare_jid] then -- User went offline
298 -- We don't need this info cached anymore, clear it.
299 recipients[user_bare_jid] = nil;