mod_storage_*: Don't explicitly set driver name, to ease copying/renaming modules.
[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 hosts = hosts;
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;
17 local next = next;
18 local type = type;
19 local calculate_hash = require "util.caps".calculate_hash;
20 local core_post_stanza = prosody.core_post_stanza;
21
22 local NULL = {};
23 local data = {};
24 local recipients = {};
25 local hash_map = {};
26
27 module.save = function()
28         return { data = data, recipients = recipients, hash_map = hash_map };
29 end
30 module.restore = function(state)
31         data = state.data or {};
32         recipients = state.recipients or {};
33         hash_map = state.hash_map or {};
34 end
35
36 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
37 module:add_feature("http://jabber.org/protocol/pubsub#publish");
38
39 local function subscription_presence(user_bare, recipient)
40         local recipient_bare = jid_bare(recipient);
41         if (recipient_bare == user_bare) then return true end
42         local username, host = jid_split(user_bare);
43         return is_contact_subscribed(username, host, recipient_bare);
44 end
45
46 local function publish(session, node, id, item)
47         item.attr.xmlns = nil;
48         local disable = #item.tags ~= 1 or #item.tags[1] == 0;
49         if #item.tags == 0 then item.name = "retract"; end
50         local bare = session.username..'@'..session.host;
51         local stanza = st.message({from=bare, type='headline'})
52                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
53                         :tag('items', {node=node})
54                                 :add_child(item)
55                         :up()
56                 :up();
57
58         -- store for the future
59         local user_data = data[bare];
60         if disable then
61                 if user_data then
62                         user_data[node] = nil;
63                         if not next(user_data) then data[bare] = nil; end
64                 end
65         else
66                 if not user_data then user_data = {}; data[bare] = user_data; end
67                 user_data[node] = {id or "1", item};
68         end
69
70         -- broadcast
71         for recipient, notify in pairs(recipients[bare] or NULL) do
72                 if notify[node] then
73                         stanza.attr.to = recipient;
74                         core_post_stanza(session, stanza);
75                 end
76         end
77 end
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 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;
175                                 session.send(st.reply(stanza));
176                                 publish(session, node, id, st.clone(payload));
177                                 return true;
178                         end
179                 end
180         elseif stanza.attr.type == 'get' then
181                 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
182                 if subscription_presence(user, stanza.attr.from) then
183                         local user_data = data[user];
184                         local node, requested_id;
185                         payload = payload.tags[1];
186                         if payload and payload.name == 'items' then
187                                 node = payload.attr.node;
188                                 local item = payload.tags[1];
189                                 if item and item.name == "item" then
190                                         requested_id = item.attr.id;
191                                 end
192                         end
193                         if node and user_data and user_data[node] then -- Send the last item
194                                 local id, item = unpack(user_data[node]);
195                                 if not requested_id or id == requested_id then
196                                         local stanza = st.reply(stanza)
197                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
198                                                         :tag('items', {node=node})
199                                                                 :add_child(item)
200                                                         :up()
201                                                 :up();
202                                         session.send(stanza);
203                                         return true;
204                                 else -- requested item doesn't exist
205                                         local stanza = st.reply(stanza)
206                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
207                                                         :tag('items', {node=node})
208                                                 :up();
209                                         session.send(stanza);
210                                         return true;
211                                 end
212                         elseif node then -- node doesn't exist
213                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
214                                 return true;
215                         else --invalid request
216                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
217                                 return true;
218                         end
219                 else --no presence subscription
220                         session.send(st.error_reply(stanza, 'auth', 'not-authorized')
221                                 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
222                         return true;
223                 end
224         end
225 end);
226
227 module:hook("iq-result/bare/disco", function(event)
228         local session, stanza = event.origin, event.stanza;
229         if stanza.attr.type == "result" then
230                 local disco = stanza.tags[1];
231                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
232                         -- Process disco response
233                         local self = not stanza.attr.to;
234                         local user = stanza.attr.to or (session.username..'@'..session.host);
235                         local contact = stanza.attr.from;
236                         local current = recipients[user] and recipients[user][contact];
237                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
238                         local ver = current;
239                         if not string.find(current, "#") then
240                                 ver = calculate_hash(disco.tags); -- calculate hash
241                         end
242                         local notify = {};
243                         for _, feature in pairs(disco.tags) do
244                                 if feature.name == "feature" and feature.attr.var then
245                                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
246                                         if nfeature then notify[nfeature] = true; end
247                                 end
248                         end
249                         hash_map[ver] = notify; -- update hash map
250                         if self then
251                                 for jid, item in pairs(session.roster) do -- for all interested contacts
252                                         if item.subscription == "both" or item.subscription == "from" then
253                                                 if not recipients[jid] then recipients[jid] = {}; end
254                                                 recipients[jid][contact] = notify;
255                                                 publish_all(jid, contact, session);
256                                         end
257                                 end
258                         end
259                         recipients[user][contact] = notify; -- set recipient's data to calculated data
260                         -- send messages to recipient
261                         publish_all(user, contact, session);
262                 end
263         end
264 end);
265
266 module:hook("account-disco-info", function(event)
267         local stanza = event.stanza;
268         stanza:tag('identity', {category='pubsub', type='pep'}):up();
269         stanza:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
270 end);
271
272 module:hook("account-disco-items", function(event)
273         local stanza = event.stanza;
274         local bare = stanza.attr.to;
275         local user_data = data[bare];
276
277         if user_data then
278                 for node, _ in pairs(user_data) do
279                         stanza:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
280                 end
281         end
282 end);