mod_pep: Document data structures, so I don't have to spend time remembering every...
[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 -- Used as canonical 'empty table'
21 local NULL = {};
22 -- data[user_bare_jid][node] = item_stanza
23 local data = {};
24 --- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true
25 local recipients = {};
26 -- hash_map[hash][subscribed_nodes] = true
27 local hash_map = {};
28
29 module.save = function()
30         return { data = data, recipients = recipients, hash_map = hash_map };
31 end
32 module.restore = function(state)
33         data = state.data or {};
34         recipients = state.recipients or {};
35         hash_map = state.hash_map or {};
36 end
37
38 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
39 module:add_feature("http://jabber.org/protocol/pubsub#publish");
40
41 local function subscription_presence(user_bare, recipient)
42         local recipient_bare = jid_bare(recipient);
43         if (recipient_bare == user_bare) then return true end
44         local username, host = jid_split(user_bare);
45         return is_contact_subscribed(username, host, recipient_bare);
46 end
47
48 local function publish(session, node, id, item)
49         item.attr.xmlns = nil;
50         local disable = #item.tags ~= 1 or #item.tags[1] == 0;
51         if #item.tags == 0 then item.name = "retract"; end
52         local bare = session.username..'@'..session.host;
53         local stanza = st.message({from=bare, type='headline'})
54                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
55                         :tag('items', {node=node})
56                                 :add_child(item)
57                         :up()
58                 :up();
59
60         -- store for the future
61         local user_data = data[bare];
62         if disable then
63                 if user_data then
64                         user_data[node] = nil;
65                         if not next(user_data) then data[bare] = nil; end
66                 end
67         else
68                 if not user_data then user_data = {}; data[bare] = user_data; end
69                 user_data[node] = {id, item};
70         end
71
72         -- broadcast
73         for recipient, notify in pairs(recipients[bare] or NULL) do
74                 if notify[node] then
75                         stanza.attr.to = recipient;
76                         core_post_stanza(session, stanza);
77                 end
78         end
79 end
80 local function publish_all(user, recipient, session)
81         local d = data[user];
82         local notify = recipients[user] and recipients[user][recipient];
83         if d and notify then
84                 for node in pairs(notify) do
85                         if d[node] then
86                                 local id, item = unpack(d[node]);
87                                 session.send(st.message({from=user, to=recipient, type='headline'})
88                                         :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
89                                                 :tag('items', {node=node})
90                                                         :add_child(item)
91                                                 :up()
92                                         :up());
93                         end
94                 end
95         end
96 end
97
98 local function get_caps_hash_from_presence(stanza, current)
99         local t = stanza.attr.type;
100         if not t then
101                 for _, child in pairs(stanza.tags) do
102                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
103                                 local attr = child.attr;
104                                 if attr.hash then -- new caps
105                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
106                                 else -- legacy caps
107                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
108                                 end
109                                 return; -- bad caps format
110                         end
111                 end
112         elseif t == "unavailable" or t == "error" then
113                 return;
114         end
115         return current; -- no caps, could mean caps optimization, so return current
116 end
117
118 module:hook("presence/bare", function(event)
119         -- inbound presence to bare JID recieved
120         local origin, stanza = event.origin, event.stanza;
121         local user = stanza.attr.to or (origin.username..'@'..origin.host);
122         local t = stanza.attr.type;
123         local self = not stanza.attr.to;
124
125         if not t then -- available presence
126                 if self or subscription_presence(user, stanza.attr.from) then
127                         local recipient = stanza.attr.from;
128                         local current = recipients[user] and recipients[user][recipient];
129                         local hash = get_caps_hash_from_presence(stanza, current);
130                         if current == hash or (current and current == hash_map[hash]) then return; end
131                         if not hash then
132                                 if recipients[user] then recipients[user][recipient] = nil; end
133                         else
134                                 recipients[user] = recipients[user] or {};
135                                 if hash_map[hash] then
136                                         recipients[user][recipient] = hash_map[hash];
137                                         publish_all(user, recipient, origin);
138                                 else
139                                         recipients[user][recipient] = hash;
140                                         local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
141                                         if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
142                                                 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
143                                                 origin.send(
144                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
145                                                                 :query("http://jabber.org/protocol/disco#info")
146                                                 );
147                                         end
148                                 end
149                         end
150                 end
151         elseif t == "unavailable" then
152                 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
153         elseif not self and t == "unsubscribe" then
154                 local from = jid_bare(stanza.attr.from);
155                 local subscriptions = recipients[user];
156                 if subscriptions then
157                         for subscriber in pairs(subscriptions) do
158                                 if jid_bare(subscriber) == from then
159                                         recipients[user][subscriber] = nil;
160                                 end
161                         end
162                 end
163         end
164 end, 10);
165
166 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
167         local session, stanza = event.origin, event.stanza;
168         local payload = stanza.tags[1];
169
170         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
171                 payload = payload.tags[1];
172                 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
173                         local node = payload.attr.node;
174                         payload = payload.tags[1];
175                         if payload and payload.name == "item" then -- <item>
176                                 local id = payload.attr.id or "1";
177                                 payload.attr.id = id;
178                                 session.send(st.reply(stanza));
179                                 publish(session, node, id, st.clone(payload));
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 stanza = event.stanza;
271         stanza:tag('identity', {category='pubsub', type='pep'}):up();
272         stanza:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
273 end);
274
275 module:hook("account-disco-items", function(event)
276         local stanza = event.stanza;
277         local bare = stanza.attr.to;
278         local user_data = data[bare];
279
280         if user_data then
281                 for node, _ in pairs(user_data) do
282                         stanza:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
283                 end
284         end
285 end);