mod_tls: Let hosts without an 'ssl' option inherit it from their parent hosts.
[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
21 local NULL = {};
22 local data = {};
23 local recipients = {};
24 local hash_map = {};
25
26 module.save = function()
27         return { data = data, recipients = recipients, hash_map = hash_map };
28 end
29 module.restore = function(state)
30         data = state.data or {};
31         recipients = state.recipients or {};
32         hash_map = state.hash_map or {};
33 end
34
35 module:add_identity("pubsub", "pep", "Prosody");
36 module:add_feature("http://jabber.org/protocol/pubsub#publish");
37
38 local function subscription_presence(user_bare, recipient)
39         local recipient_bare = jid_bare(recipient);
40         if (recipient_bare == user_bare) then return true end
41         local username, host = jid_split(user_bare);
42         return is_contact_subscribed(username, host, recipient_bare);
43 end
44
45 local function publish(session, node, id, 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 or "1", 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 local function publish_all(user, recipient, session)
78         local d = data[user];
79         local notify = recipients[user] and recipients[user][recipient];
80         if d and notify then
81                 for node in pairs(notify) do
82                         if d[node] then
83                                 local id, item = unpack(d[node]);
84                                 session.send(st.message({from=user, to=recipient, type='headline'})
85                                         :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
86                                                 :tag('items', {node=node})
87                                                         :add_child(item)
88                                                 :up()
89                                         :up());
90                         end
91                 end
92         end
93 end
94
95 local function get_caps_hash_from_presence(stanza, current)
96         local t = stanza.attr.type;
97         if not t then
98                 for _, child in pairs(stanza.tags) do
99                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
100                                 local attr = child.attr;
101                                 if attr.hash then -- new caps
102                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
103                                 else -- legacy caps
104                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
105                                 end
106                                 return; -- bad caps format
107                         end
108                 end
109         elseif t == "unavailable" or t == "error" then
110                 return;
111         end
112         return current; -- no caps, could mean caps optimization, so return current
113 end
114
115 module:hook("presence/bare", function(event)
116         -- inbound presence to bare JID recieved
117         local origin, stanza = event.origin, event.stanza;
118         local user = stanza.attr.to or (origin.username..'@'..origin.host);
119         local t = stanza.attr.type;
120
121         if not t then -- available presence
122                 if not stanza.attr.to 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 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                                         origin.send(
137                                                 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
138                                                         :query("http://jabber.org/protocol/disco#info")
139                                         );
140                                 end
141                         end
142                 end
143         elseif t == "unavailable" then
144                 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
145         end
146 end, 10);
147
148 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
149         local session, stanza = event.origin, event.stanza;
150         local payload = stanza.tags[1];
151
152         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
153                 payload = payload.tags[1];
154                 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
155                         local node = payload.attr.node;
156                         payload = payload.tags[1];
157                         if payload and payload.name == "item" then -- <item>
158                                 local id = payload.attr.id;
159                                 session.send(st.reply(stanza));
160                                 publish(session, node, id, st.clone(payload));
161                                 return true;
162                         end
163                 end
164         elseif stanza.attr.type == 'get' then
165                 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
166                 if subscription_presence(user, stanza.attr.from) then
167                         local user_data = data[user];
168                         local node, requested_id;
169                         payload = payload.tags[1];
170                         if payload and payload.name == 'items' then
171                                 node = payload.attr.node;
172                                 local item = payload.tags[1];
173                                 if item and item.name == "item" then
174                                         requested_id = item.attr.id;
175                                 end
176                         end
177                         if node and user_data and user_data[node] then -- Send the last item
178                                 local id, item = unpack(user_data[node]);
179                                 if not requested_id or id == requested_id then
180                                         local stanza = st.reply(stanza)
181                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
182                                                         :tag('items', {node=node})
183                                                                 :add_child(item)
184                                                         :up()
185                                                 :up();
186                                         session.send(stanza);
187                                         return true;
188                                 else -- requested item doesn't exist
189                                         local stanza = st.reply(stanza)
190                                                 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
191                                                         :tag('items', {node=node})
192                                                 :up();
193                                         session.send(stanza);
194                                         return true;
195                                 end
196                         elseif node then -- node doesn't exist
197                                 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
198                                 return true;
199                         else --invalid request
200                                 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
201                                 return true;
202                         end
203                 else --no presence subscription
204                         session.send(st.error_reply(stanza, 'auth', 'not-authorized')
205                                 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
206                         return true;
207                 end
208         end
209 end);
210
211 module:hook("iq/bare/disco", function(event)
212         local session, stanza = event.origin, event.stanza;
213         if stanza.attr.type == "result" then
214                 local disco = stanza.tags[1];
215                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
216                         -- Process disco response
217                         local user = stanza.attr.to or (session.username..'@'..session.host);
218                         local contact = stanza.attr.from;
219                         local current = recipients[user] and recipients[user][contact];
220                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
221                         local ver = current;
222                         if not string.find(current, "#") then
223                                 ver = calculate_hash(disco.tags); -- calculate hash
224                         end
225                         local notify = {};
226                         for _, feature in pairs(disco.tags) do
227                                 if feature.name == "feature" and feature.attr.var then
228                                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
229                                         if nfeature then notify[nfeature] = true; end
230                                 end
231                         end
232                         hash_map[ver] = notify; -- update hash map
233                         recipients[user][contact] = notify; -- set recipient's data to calculated data
234                         -- send messages to recipient
235                         publish_all(user, contact, session);
236                 end
237         end
238 end);
239
240 module:hook("account-disco-info", function(event)
241         local stanza = event.stanza;
242         stanza:tag('identity', {category='pubsub', type='pep'}):up();
243         stanza:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
244 end);
245
246 module:hook("account-disco-items", function(event)
247         local stanza = event.stanza;
248         local bare = stanza.attr.to;
249         local user_data = data[bare];
250
251         if user_data then
252                 for node, _ in pairs(user_data) do
253                         stanza:tag('item', {jid=bare, node=node}):up(); -- TODO we need to handle queries to these nodes
254                 end
255         end
256 end);