Merge 0.9->0.10
[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
20 local NULL = {};
21 local data = {};
22 local recipients = {};
23 local hash_map = {};
24
25 module.save = function()
26         return { data = data, recipients = recipients, hash_map = hash_map };
27 end
28 module.restore = function(state)
29         data = state.data or {};
30         recipients = state.recipients or {};
31         hash_map = state.hash_map or {};
32 end
33
34 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
35 module:add_feature("http://jabber.org/protocol/pubsub#publish");
36
37 local function subscription_presence(user_bare, recipient)
38         local recipient_bare = jid_bare(recipient);
39         if (recipient_bare == user_bare) then return true end
40         local username, host = jid_split(user_bare);
41         return is_contact_subscribed(username, host, recipient_bare);
42 end
43
44 local function publish(session, node, id, item)
45         item.attr.xmlns = nil;
46         local disable = #item.tags ~= 1 or #item.tags[1] == 0;
47         if #item.tags == 0 then item.name = "retract"; end
48         local bare = session.username..'@'..session.host;
49         local stanza = st.message({from=bare, type='headline'})
50                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
51                         :tag('items', {node=node})
52                                 :add_child(item)
53                         :up()
54                 :up();
55
56         -- store for the future
57         local user_data = data[bare];
58         if disable then
59                 if user_data then
60                         user_data[node] = nil;
61                         if not next(user_data) then data[bare] = nil; end
62                 end
63         else
64                 if not user_data then user_data = {}; data[bare] = user_data; end
65                 user_data[node] = {id, item};
66         end
67
68         -- broadcast
69         for recipient, notify in pairs(recipients[bare] or NULL) do
70                 if notify[node] then
71                         stanza.attr.to = recipient;
72                         core_post_stanza(session, stanza);
73                 end
74         end
75 end
76 local function publish_all(user, recipient, session)
77         local d = data[user];
78         local notify = recipients[user] and recipients[user][recipient];
79         if d and notify then
80                 for node in pairs(notify) do
81                         if d[node] then
82                                 local id, item = unpack(d[node]);
83                                 session.send(st.message({from=user, to=recipient, type='headline'})
84                                         :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
85                                                 :tag('items', {node=node})
86                                                         :add_child(item)
87                                                 :up()
88                                         :up());
89                         end
90                 end
91         end
92 end
93
94 local function get_caps_hash_from_presence(stanza, current)
95         local t = stanza.attr.type;
96         if not t then
97                 for _, child in pairs(stanza.tags) do
98                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
99                                 local attr = child.attr;
100                                 if attr.hash then -- new caps
101                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
102                                 else -- legacy caps
103                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
104                                 end
105                                 return; -- bad caps format
106                         end
107                 end
108         elseif t == "unavailable" or t == "error" then
109                 return;
110         end
111         return current; -- no caps, could mean caps optimization, so return current
112 end
113
114 module:hook("presence/bare", function(event)
115         -- inbound presence to bare JID recieved
116         local origin, stanza = event.origin, event.stanza;
117         local user = stanza.attr.to or (origin.username..'@'..origin.host);
118         local t = stanza.attr.type;
119         local self = not stanza.attr.to;
120
121         if not t then -- available presence
122                 if self or subscription_presence(user, stanza.attr.from) then
123                         local recipient = stanza.attr.from;
124                         local current = recipients[user] and recipients[user][recipient];
125                         local hash = get_caps_hash_from_presence(stanza, current);
126                         if current == hash or (current and current == hash_map[hash]) then return; end
127                         if not hash then
128                                 if recipients[user] then recipients[user][recipient] = nil; end
129                         else
130                                 recipients[user] = recipients[user] or {};
131                                 if hash_map[hash] then
132                                         recipients[user][recipient] = hash_map[hash];
133                                         publish_all(user, recipient, origin);
134                                 else
135                                         recipients[user][recipient] = hash;
136                                         local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
137                                         if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
138                                                 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
139                                                 origin.send(
140                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
141                                                                 :query("http://jabber.org/protocol/disco#info")
142                                                 );
143                                         end
144                                 end
145                         end
146                 end
147         elseif t == "unavailable" then
148                 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
149         elseif not self and t == "unsubscribe" then
150                 local from = jid_bare(stanza.attr.from);
151                 local subscriptions = recipients[user];
152                 if subscriptions then
153                         for subscriber in pairs(subscriptions) do
154                                 if jid_bare(subscriber) == from then
155                                         recipients[user][subscriber] = nil;
156                                 end
157                         end
158                 end
159         end
160 end, 10);
161
162 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
163         local session, stanza = event.origin, event.stanza;
164         local payload = stanza.tags[1];
165
166         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
167                 payload = payload.tags[1];
168                 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
169                         local node = payload.attr.node;
170                         payload = payload.tags[1];
171                         if payload and payload.name == "item" then -- <item>
172                                 local id = payload.attr.id or "1";
173                                 payload.attr.id = id;
174                                 session.send(st.reply(stanza));
175                                 publish(session, node, id, st.clone(payload));
176                                 return true;
177                         end
178                 end
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;
190                                 end
191                         end
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})
198                                                                 :add_child(item)
199                                                         :up()
200                                                 :up();
201                                         session.send(stanza);
202                                         return true;
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})
207                                                 :up();
208                                         session.send(stanza);
209                                         return true;
210                                 end
211                         elseif node then -- node doesn't exist
212                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
213                                 return true;
214                         else --invalid request
215                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
216                                 return true;
217                         end
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'}));
221                         return true;
222                 end
223         end
224 end);
225
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
237                         local ver = current;
238                         if not string.find(current, "#") then
239                                 ver = calculate_hash(disco.tags); -- calculate hash
240                         end
241                         local notify = {};
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
246                                 end
247                         end
248                         hash_map[ver] = notify; -- update hash map
249                         if self then
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);
255                                         end
256                                 end
257                         end
258                         recipients[user][contact] = notify; -- set recipient's data to calculated data
259                         -- send messages to recipient
260                         publish_all(user, contact, session);
261                 end
262         end
263 end);
264
265 module:hook("account-disco-info", function(event)
266         local reply = event.reply;
267         reply:tag('identity', {category='pubsub', type='pep'}):up();
268         reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
269 end);
270
271 module:hook("account-disco-items", function(event)
272         local reply = event.reply;
273         local bare = reply.attr.to;
274         local user_data = data[bare];
275
276         if user_data then
277                 for node, _ in pairs(user_data) do
278                         reply:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
279                 end
280         end
281 end);