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";
14 local user_exists = require "core.usermanager".user_exists;
15 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
16 local pairs, ipairs = pairs, ipairs;
19 local calculate_hash = require "util.caps".calculate_hash;
23 local recipients = {};
26 module.save = function()
27 return { data = data, recipients = recipients, hash_map = hash_map };
29 module.restore = function(state)
30 data = state.data or {};
31 recipients = state.recipients or {};
32 hash_map = state.hash_map or {};
35 module:add_identity("pubsub", "pep", "Prosody");
36 module:add_feature("http://jabber.org/protocol/pubsub#publish");
38 local function subscription_presence(user_bare, recipient)
39 local recipient_bare = jid_bare(recipient);
40 if (recipient_bare == user_bare) then return true end
41 local username, host = jid_split(user_bare);
42 return is_contact_subscribed(username, host, recipient_bare);
45 local function publish(session, node, id, item)
46 item.attr.xmlns = nil;
47 local disable = #item.tags ~= 1 or #item.tags[1] == 0;
48 if #item.tags == 0 then item.name = "retract"; end
49 local bare = session.username..'@'..session.host;
50 local stanza = st.message({from=bare, type='headline'})
51 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
52 :tag('items', {node=node})
57 -- store for the future
58 local user_data = data[bare];
61 user_data[node] = nil;
62 if not next(user_data) then data[bare] = nil; end
65 if not user_data then user_data = {}; data[bare] = user_data; end
66 user_data[node] = {id or "1", item};
70 for recipient, notify in pairs(recipients[bare] or NULL) do
72 stanza.attr.to = recipient;
73 core_post_stanza(session, stanza);
77 local function publish_all(user, recipient, session)
79 local notify = recipients[user] and recipients[user][recipient];
81 for node in pairs(notify) do
83 local id, item = unpack(d[node]);
84 session.send(st.message({from=user, to=recipient, type='headline'})
85 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
86 :tag('items', {node=node})
95 local function get_caps_hash_from_presence(stanza, current)
96 local t = stanza.attr.type;
98 for _, child in pairs(stanza.tags) do
99 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
100 local attr = child.attr;
101 if attr.hash then -- new caps
102 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
104 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
106 return; -- bad caps format
109 elseif t == "unavailable" or t == "error" then
112 return current; -- no caps, could mean caps optimization, so return current
115 module:hook("presence/bare", function(event)
116 -- inbound presence to bare JID recieved
117 local origin, stanza = event.origin, event.stanza;
118 local user = stanza.attr.to or (origin.username..'@'..origin.host);
119 local t = stanza.attr.type;
120 local self = not stanza.attr.to;
122 if not t then -- available presence
123 if self or subscription_presence(user, stanza.attr.from) then
124 local recipient = stanza.attr.from;
125 local current = recipients[user] and recipients[user][recipient];
126 local hash = get_caps_hash_from_presence(stanza, current);
127 if current == hash or (current and current == hash_map[hash]) then return; end
129 if recipients[user] then recipients[user][recipient] = nil; end
131 recipients[user] = recipients[user] or {};
132 if hash_map[hash] then
133 recipients[user][recipient] = hash_map[hash];
134 publish_all(user, recipient, origin);
136 recipients[user][recipient] = hash;
137 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
138 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
139 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
141 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
142 :query("http://jabber.org/protocol/disco#info")
148 elseif t == "unavailable" then
149 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
150 elseif not self and t == "unsubscribe" then
151 local from = jid_bare(stanza.attr.from);
152 local subscriptions = recipients[user];
153 if subscriptions then
154 for subscriber in pairs(subscriptions) do
155 if jid_bare(subscriber) == from then
156 recipients[user][subscriber] = nil;
163 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
164 local session, stanza = event.origin, event.stanza;
165 local payload = stanza.tags[1];
167 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
168 payload = payload.tags[1];
169 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
170 local node = payload.attr.node;
171 payload = payload.tags[1];
172 if payload and payload.name == "item" then -- <item>
173 local id = payload.attr.id;
174 session.send(st.reply(stanza));
175 publish(session, node, id, st.clone(payload));
179 elseif stanza.attr.type == 'get' then
180 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
181 if subscription_presence(user, stanza.attr.from) then
182 local user_data = data[user];
183 local node, requested_id;
184 payload = payload.tags[1];
185 if payload and payload.name == 'items' then
186 node = payload.attr.node;
187 local item = payload.tags[1];
188 if item and item.name == "item" then
189 requested_id = item.attr.id;
192 if node and user_data and user_data[node] then -- Send the last item
193 local id, item = unpack(user_data[node]);
194 if not requested_id or id == requested_id then
195 local stanza = st.reply(stanza)
196 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
197 :tag('items', {node=node})
201 session.send(stanza);
203 else -- requested item doesn't exist
204 local stanza = st.reply(stanza)
205 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
206 :tag('items', {node=node})
208 session.send(stanza);
211 elseif node then -- node doesn't exist
212 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
214 else --invalid request
215 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
218 else --no presence subscription
219 session.send(st.error_reply(stanza, 'auth', 'not-authorized')
220 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
226 module:hook("iq-result/bare/disco", function(event)
227 local session, stanza = event.origin, event.stanza;
228 if stanza.attr.type == "result" then
229 local disco = stanza.tags[1];
230 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
231 -- Process disco response
232 local self = not stanza.attr.to;
233 local user = stanza.attr.to or (session.username..'@'..session.host);
234 local contact = stanza.attr.from;
235 local current = recipients[user] and recipients[user][contact];
236 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
238 if not string.find(current, "#") then
239 ver = calculate_hash(disco.tags); -- calculate hash
242 for _, feature in pairs(disco.tags) do
243 if feature.name == "feature" and feature.attr.var then
244 local nfeature = feature.attr.var:match("^(.*)%+notify$");
245 if nfeature then notify[nfeature] = true; end
248 hash_map[ver] = notify; -- update hash map
250 for jid, item in pairs(session.roster) do -- for all interested contacts
251 if item.subscription == "both" or item.subscription == "from" then
252 if not recipients[jid] then recipients[jid] = {}; end
253 recipients[jid][contact] = notify;
254 publish_all(jid, contact, session);
258 recipients[user][contact] = notify; -- set recipient's data to calculated data
259 -- send messages to recipient
260 publish_all(user, contact, session);
265 module:hook("account-disco-info", function(event)
266 local stanza = event.stanza;
267 stanza:tag('identity', {category='pubsub', type='pep'}):up();
268 stanza:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
271 module:hook("account-disco-items", function(event)
272 local stanza = event.stanza;
273 local bare = stanza.attr.to;
274 local user_data = data[bare];
277 for node, _ in pairs(user_data) do
278 stanza:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes