mod_pep: Include the bare user jid in event
[prosody.git] / plugins / mod_pep.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
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;
14 local pairs = pairs;
15 local next = next;
16 local type = type;
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;
20
21 -- Used as canonical 'empty table'
22 local NULL = {};
23 -- data[user_bare_jid][node] = item_stanza
24 local data = {};
25 --- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true
26 local recipients = {};
27 -- hash_map[hash][subscribed_nodes] = true
28 local hash_map = {};
29
30 module.save = function()
31         return { data = data, recipients = recipients, hash_map = hash_map };
32 end
33 module.restore = function(state)
34         data = state.data or {};
35         recipients = state.recipients or {};
36         hash_map = state.hash_map or {};
37 end
38
39 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
40 module:add_feature("http://jabber.org/protocol/pubsub#publish");
41
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);
47 end
48
49 module:hook("pep-publish-item", function (event)
50         local session, bare, node, id, item = event.session, event.user, 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 stanza = st.message({from=bare, type='headline'})
55                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
56                         :tag('items', {node=node})
57                                 :add_child(item)
58                         :up()
59                 :up();
60
61         -- store for the future
62         local user_data = data[bare];
63         if disable then
64                 if user_data then
65                         user_data[node] = nil;
66                         if not next(user_data) then data[bare] = nil; end
67                 end
68         else
69                 if not user_data then user_data = {}; data[bare] = user_data; end
70                 user_data[node] = {id, item};
71         end
72
73         -- broadcast
74         for recipient, notify in pairs(recipients[bare] or NULL) do
75                 if notify[node] then
76                         stanza.attr.to = recipient;
77                         core_post_stanza(session, stanza);
78                 end
79         end
80 end);
81
82 local function publish_all(user, recipient, session)
83         local d = data[user];
84         local notify = recipients[user] and recipients[user][recipient];
85         if d and notify then
86                 for node in pairs(notify) do
87                         if d[node] then
88                                 local id, item = unpack(d[node]);
89                                 session.send(st.message({from=user, to=recipient, type='headline'})
90                                         :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
91                                                 :tag('items', {node=node})
92                                                         :add_child(item)
93                                                 :up()
94                                         :up());
95                         end
96                 end
97         end
98 end
99
100 local function get_caps_hash_from_presence(stanza, current)
101         local t = stanza.attr.type;
102         if not t then
103                 for _, child in pairs(stanza.tags) do
104                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
105                                 local attr = child.attr;
106                                 if attr.hash then -- new caps
107                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
108                                 else -- legacy caps
109                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
110                                 end
111                                 return; -- bad caps format
112                         end
113                 end
114         elseif t == "unavailable" or t == "error" then
115                 return;
116         end
117         return current; -- no caps, could mean caps optimization, so return current
118 end
119
120 module:hook("presence/bare", function(event)
121         -- inbound presence to bare JID recieved
122         local origin, stanza = event.origin, event.stanza;
123         local user = stanza.attr.to or (origin.username..'@'..origin.host);
124         local t = stanza.attr.type;
125         local self = not stanza.attr.to;
126
127         -- Only cache subscriptions if user is online
128         if not bare_sessions[user] then return; end
129
130         if not t then -- available presence
131                 if self or subscription_presence(user, stanza.attr.from) then
132                         local recipient = stanza.attr.from;
133                         local current = recipients[user] and recipients[user][recipient];
134                         local hash = get_caps_hash_from_presence(stanza, current);
135                         if current == hash or (current and current == hash_map[hash]) then return; end
136                         if not hash then
137                                 if recipients[user] then recipients[user][recipient] = nil; end
138                         else
139                                 recipients[user] = recipients[user] or {};
140                                 if hash_map[hash] then
141                                         recipients[user][recipient] = hash_map[hash];
142                                         publish_all(user, recipient, origin);
143                                 else
144                                         recipients[user][recipient] = hash;
145                                         local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
146                                         if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
147                                                 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
148                                                 origin.send(
149                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
150                                                                 :query("http://jabber.org/protocol/disco#info")
151                                                 );
152                                         end
153                                 end
154                         end
155                 end
156         elseif t == "unavailable" then
157                 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
158         elseif not self and t == "unsubscribe" then
159                 local from = jid_bare(stanza.attr.from);
160                 local subscriptions = recipients[user];
161                 if subscriptions then
162                         for subscriber in pairs(subscriptions) do
163                                 if jid_bare(subscriber) == from then
164                                         recipients[user][subscriber] = nil;
165                                 end
166                         end
167                 end
168         end
169 end, 10);
170
171 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
172         local session, stanza = event.origin, event.stanza;
173         local payload = stanza.tags[1];
174
175         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
176                 payload = payload.tags[1];
177                 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
178                         local node = payload.attr.node;
179                         payload = payload.tags[1];
180                         if payload and payload.name == "item" then -- <item>
181                                 local id = payload.attr.id or "1";
182                                 payload.attr.id = id;
183                                 session.send(st.reply(stanza));
184                                 module:fire_event("pep-publish-item", {
185                                         node = node, user = jid_bare(session.jid), actor = session.jid, id = id, session = session, item = st.clone(payload);
186                                 });
187                                 return true;
188                         end
189                 end
190         elseif stanza.attr.type == 'get' then
191                 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
192                 if subscription_presence(user, stanza.attr.from) then
193                         local user_data = data[user];
194                         local node, requested_id;
195                         payload = payload.tags[1];
196                         if payload and payload.name == 'items' then
197                                 node = payload.attr.node;
198                                 local item = payload.tags[1];
199                                 if item and item.name == "item" then
200                                         requested_id = item.attr.id;
201                                 end
202                         end
203                         if node and user_data and user_data[node] then -- Send the last item
204                                 local id, item = unpack(user_data[node]);
205                                 if not requested_id or id == requested_id then
206                                         local stanza = st.reply(stanza)
207                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
208                                                         :tag('items', {node=node})
209                                                                 :add_child(item)
210                                                         :up()
211                                                 :up();
212                                         session.send(stanza);
213                                         return true;
214                                 else -- requested item doesn't exist
215                                         local stanza = st.reply(stanza)
216                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
217                                                         :tag('items', {node=node})
218                                                 :up();
219                                         session.send(stanza);
220                                         return true;
221                                 end
222                         elseif node then -- node doesn't exist
223                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
224                                 return true;
225                         else --invalid request
226                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
227                                 return true;
228                         end
229                 else --no presence subscription
230                         session.send(st.error_reply(stanza, 'auth', 'not-authorized')
231                                 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
232                         return true;
233                 end
234         end
235 end);
236
237 module:hook("iq-result/bare/disco", function(event)
238         local session, stanza = event.origin, event.stanza;
239         if stanza.attr.type == "result" then
240                 local disco = stanza.tags[1];
241                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
242                         -- Process disco response
243                         local self = not stanza.attr.to;
244                         local user = stanza.attr.to or (session.username..'@'..session.host);
245                         local contact = stanza.attr.from;
246                         local current = recipients[user] and recipients[user][contact];
247                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
248                         local ver = current;
249                         if not string.find(current, "#") then
250                                 ver = calculate_hash(disco.tags); -- calculate hash
251                         end
252                         local notify = {};
253                         for _, feature in pairs(disco.tags) do
254                                 if feature.name == "feature" and feature.attr.var then
255                                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
256                                         if nfeature then notify[nfeature] = true; end
257                                 end
258                         end
259                         hash_map[ver] = notify; -- update hash map
260                         if self then
261                                 for jid, item in pairs(session.roster) do -- for all interested contacts
262                                         if item.subscription == "both" or item.subscription == "from" then
263                                                 if not recipients[jid] then recipients[jid] = {}; end
264                                                 recipients[jid][contact] = notify;
265                                                 publish_all(jid, contact, session);
266                                         end
267                                 end
268                         end
269                         recipients[user][contact] = notify; -- set recipient's data to calculated data
270                         -- send messages to recipient
271                         publish_all(user, contact, session);
272                 end
273         end
274 end);
275
276 module:hook("account-disco-info", function(event)
277         local reply = event.reply;
278         reply:tag('identity', {category='pubsub', type='pep'}):up();
279         reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
280 end);
281
282 module:hook("account-disco-items", function(event)
283         local reply = event.reply;
284         local bare = reply.attr.to;
285         local user_data = data[bare];
286
287         if user_data then
288                 for node, _ in pairs(user_data) do
289                         reply:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
290                 end
291         end
292 end);
293
294 module:hook("resource-unbind", function (event)
295         local user_bare_jid = event.session.username.."@"..event.session.host;
296         if not bare_sessions[user_bare_jid] then -- User went offline
297                 -- We don't need this info cached anymore, clear it.
298                 recipients[user_bare_jid] = nil;
299         end
300 end);