Merge 0.10->trunk
[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 module:hook("pep-publish-item", function (event)
45         local session, node, id, item = event.session, event.node, event.id, event.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})
53                                 :add_child(item)
54                         :up()
55                 :up();
56
57         -- store for the future
58         local user_data = data[bare];
59         if disable then
60                 if user_data then
61                         user_data[node] = nil;
62                         if not next(user_data) then data[bare] = nil; end
63                 end
64         else
65                 if not user_data then user_data = {}; data[bare] = user_data; end
66                 user_data[node] = {id, item};
67         end
68
69         -- broadcast
70         for recipient, notify in pairs(recipients[bare] or NULL) do
71                 if notify[node] then
72                         stanza.attr.to = recipient;
73                         core_post_stanza(session, stanza);
74                 end
75         end
76 end);
77
78 local function publish_all(user, recipient, session)
79         local d = data[user];
80         local notify = recipients[user] and recipients[user][recipient];
81         if d and notify then
82                 for node in pairs(notify) do
83                         if d[node] then
84                                 local id, item = unpack(d[node]);
85                                 session.send(st.message({from=user, to=recipient, type='headline'})
86                                         :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
87                                                 :tag('items', {node=node})
88                                                         :add_child(item)
89                                                 :up()
90                                         :up());
91                         end
92                 end
93         end
94 end
95
96 local function get_caps_hash_from_presence(stanza, current)
97         local t = stanza.attr.type;
98         if not t then
99                 for _, child in pairs(stanza.tags) do
100                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
101                                 local attr = child.attr;
102                                 if attr.hash then -- new caps
103                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
104                                 else -- legacy caps
105                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
106                                 end
107                                 return; -- bad caps format
108                         end
109                 end
110         elseif t == "unavailable" or t == "error" then
111                 return;
112         end
113         return current; -- no caps, could mean caps optimization, so return current
114 end
115
116 module:hook("presence/bare", function(event)
117         -- inbound presence to bare JID recieved
118         local origin, stanza = event.origin, event.stanza;
119         local user = stanza.attr.to or (origin.username..'@'..origin.host);
120         local t = stanza.attr.type;
121         local self = not stanza.attr.to;
122
123         if not t then -- available presence
124                 if self or subscription_presence(user, stanza.attr.from) then
125                         local recipient = stanza.attr.from;
126                         local current = recipients[user] and recipients[user][recipient];
127                         local hash = get_caps_hash_from_presence(stanza, current);
128                         if current == hash or (current and current == hash_map[hash]) then return; end
129                         if not hash then
130                                 if recipients[user] then recipients[user][recipient] = nil; end
131                         else
132                                 recipients[user] = recipients[user] or {};
133                                 if hash_map[hash] then
134                                         recipients[user][recipient] = hash_map[hash];
135                                         publish_all(user, recipient, origin);
136                                 else
137                                         recipients[user][recipient] = hash;
138                                         local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
139                                         if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
140                                                 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
141                                                 origin.send(
142                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
143                                                                 :query("http://jabber.org/protocol/disco#info")
144                                                 );
145                                         end
146                                 end
147                         end
148                 end
149         elseif t == "unavailable" then
150                 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
151         elseif not self and t == "unsubscribe" then
152                 local from = jid_bare(stanza.attr.from);
153                 local subscriptions = recipients[user];
154                 if subscriptions then
155                         for subscriber in pairs(subscriptions) do
156                                 if jid_bare(subscriber) == from then
157                                         recipients[user][subscriber] = nil;
158                                 end
159                         end
160                 end
161         end
162 end, 10);
163
164 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
165         local session, stanza = event.origin, event.stanza;
166         local payload = stanza.tags[1];
167
168         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
169                 payload = payload.tags[1];
170                 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
171                         local node = payload.attr.node;
172                         payload = payload.tags[1];
173                         if payload and payload.name == "item" then -- <item>
174                                 local id = payload.attr.id or "1";
175                                 payload.attr.id = id;
176                                 session.send(st.reply(stanza));
177                                 module:fire_event("pep-publish-item", {
178                                         node = node, actor = session.jid, id = id, session = session, item = st.clone(payload);
179                                 });
180                                 return true;
181                         end
182                 end
183         elseif stanza.attr.type == 'get' then
184                 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
185                 if subscription_presence(user, stanza.attr.from) then
186                         local user_data = data[user];
187                         local node, requested_id;
188                         payload = payload.tags[1];
189                         if payload and payload.name == 'items' then
190                                 node = payload.attr.node;
191                                 local item = payload.tags[1];
192                                 if item and item.name == "item" then
193                                         requested_id = item.attr.id;
194                                 end
195                         end
196                         if node and user_data and user_data[node] then -- Send the last item
197                                 local id, item = unpack(user_data[node]);
198                                 if not requested_id or id == requested_id then
199                                         local stanza = st.reply(stanza)
200                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
201                                                         :tag('items', {node=node})
202                                                                 :add_child(item)
203                                                         :up()
204                                                 :up();
205                                         session.send(stanza);
206                                         return true;
207                                 else -- requested item doesn't exist
208                                         local stanza = st.reply(stanza)
209                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
210                                                         :tag('items', {node=node})
211                                                 :up();
212                                         session.send(stanza);
213                                         return true;
214                                 end
215                         elseif node then -- node doesn't exist
216                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
217                                 return true;
218                         else --invalid request
219                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
220                                 return true;
221                         end
222                 else --no presence subscription
223                         session.send(st.error_reply(stanza, 'auth', 'not-authorized')
224                                 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
225                         return true;
226                 end
227         end
228 end);
229
230 module:hook("iq-result/bare/disco", function(event)
231         local session, stanza = event.origin, event.stanza;
232         if stanza.attr.type == "result" then
233                 local disco = stanza.tags[1];
234                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
235                         -- Process disco response
236                         local self = not stanza.attr.to;
237                         local user = stanza.attr.to or (session.username..'@'..session.host);
238                         local contact = stanza.attr.from;
239                         local current = recipients[user] and recipients[user][contact];
240                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
241                         local ver = current;
242                         if not string.find(current, "#") then
243                                 ver = calculate_hash(disco.tags); -- calculate hash
244                         end
245                         local notify = {};
246                         for _, feature in pairs(disco.tags) do
247                                 if feature.name == "feature" and feature.attr.var then
248                                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
249                                         if nfeature then notify[nfeature] = true; end
250                                 end
251                         end
252                         hash_map[ver] = notify; -- update hash map
253                         if self then
254                                 for jid, item in pairs(session.roster) do -- for all interested contacts
255                                         if item.subscription == "both" or item.subscription == "from" then
256                                                 if not recipients[jid] then recipients[jid] = {}; end
257                                                 recipients[jid][contact] = notify;
258                                                 publish_all(jid, contact, session);
259                                         end
260                                 end
261                         end
262                         recipients[user][contact] = notify; -- set recipient's data to calculated data
263                         -- send messages to recipient
264                         publish_all(user, contact, session);
265                 end
266         end
267 end);
268
269 module:hook("account-disco-info", function(event)
270         local reply = event.reply;
271         reply:tag('identity', {category='pubsub', type='pep'}):up();
272         reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
273 end);
274
275 module:hook("account-disco-items", function(event)
276         local reply = event.reply;
277         local bare = reply.attr.to;
278         local user_data = data[bare];
279
280         if user_data then
281                 for node, _ in pairs(user_data) do
282                         reply:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
283                 end
284         end
285 end);