896f3e78b764842d04dfaee7c410d806f7c6ad38
[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, 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})
58                                 :add_child(item)
59                         :up()
60                 :up();
61
62         -- store for the future
63         local user_data = data[bare];
64         if disable then
65                 if user_data then
66                         user_data[node] = nil;
67                         if not next(user_data) then data[bare] = nil; end
68                 end
69         else
70                 if not user_data then user_data = {}; data[bare] = user_data; end
71                 user_data[node] = {id, item};
72         end
73
74         -- broadcast
75         for recipient, notify in pairs(recipients[bare] or NULL) do
76                 if notify[node] then
77                         stanza.attr.to = recipient;
78                         core_post_stanza(session, stanza);
79                 end
80         end
81 end);
82
83 local function publish_all(user, recipient, session)
84         local d = data[user];
85         local notify = recipients[user] and recipients[user][recipient];
86         if d and notify then
87                 for node in pairs(notify) do
88                         if d[node] then
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})
93                                                         :add_child(item)
94                                                 :up()
95                                         :up());
96                         end
97                 end
98         end
99 end
100
101 local function get_caps_hash_from_presence(stanza, current)
102         local t = stanza.attr.type;
103         if not t then
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
109                                 else -- legacy caps
110                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
111                                 end
112                                 return; -- bad caps format
113                         end
114                 end
115         elseif t == "unavailable" or t == "error" then
116                 return;
117         end
118         return current; -- no caps, could mean caps optimization, so return current
119 end
120
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;
127
128         -- Only cache subscriptions if user is online
129         if not bare_sessions[user] then return; end
130
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
137                         if not hash then
138                                 if recipients[user] then recipients[user][recipient] = nil; end
139                         else
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);
144                                 else
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
149                                                 origin.send(
150                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
151                                                                 :query("http://jabber.org/protocol/disco#info")
152                                                 );
153                                         end
154                                 end
155                         end
156                 end
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;
166                                 end
167                         end
168                 end
169         end
170 end, 10);
171
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];
175
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);
187                                 });
188                                 return true;
189                         end
190                 end
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;
202                                 end
203                         end
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})
210                                                                 :add_child(item)
211                                                         :up()
212                                                 :up();
213                                         session.send(stanza);
214                                         return true;
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})
219                                                 :up();
220                                         session.send(stanza);
221                                         return true;
222                                 end
223                         elseif node then -- node doesn't exist
224                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
225                                 return true;
226                         else --invalid request
227                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
228                                 return true;
229                         end
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'}));
233                         return true;
234                 end
235         end
236 end);
237
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
249                         local ver = current;
250                         if not string.find(current, "#") then
251                                 ver = calculate_hash(disco.tags); -- calculate hash
252                         end
253                         local notify = {};
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
258                                 end
259                         end
260                         hash_map[ver] = notify; -- update hash map
261                         if self then
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);
267                                         end
268                                 end
269                         end
270                         recipients[user][contact] = notify; -- set recipient's data to calculated data
271                         -- send messages to recipient
272                         publish_all(user, contact, session);
273                 end
274         end
275 end);
276
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();
281 end);
282
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];
287
288         if user_data then
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
291                 end
292         end
293 end);
294
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;
300         end
301 end);