Bumper commit for the new modulemanager API \o/ Updates all the modules, though some...
[prosody.git] / core / stanza_router.lua
1
2 local log = require "util.logger".init("stanzarouter")
3
4 local st = require "util.stanza";
5 local send_s2s = require "core.s2smanager".send_to_host;
6 local user_exists = require "core.usermanager".user_exists;
7
8 local rostermanager = require "core.rostermanager";
9 local sessionmanager = require "core.sessionmanager";
10 local offlinemanager = require "core.offlinemanager";
11
12 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
13 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
14
15 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
16 local component_handle_stanza = require "core.componentmanager".handle_stanza;
17
18 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
19 local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
20
21 local format = string.format;
22 local tostring = tostring;
23 local t_concat = table.concat;
24 local t_insert = table.insert;
25 local tonumber = tonumber;
26 local s_find = string.find;
27
28 local jid_split = require "util.jid".split;
29 local print = print;
30
31 function core_process_stanza(origin, stanza)
32         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
33
34         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
35         -- TODO verify validity of stanza (as well as JID validity)
36         if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
37                 if stanza.attr.type == "set" or stanza.attr.type == "get" then
38                         error("Invalid IQ");
39                 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
40                         error("Invalid IQ");
41                 end
42         end
43
44         if origin.type == "c2s" and not origin.full_jid
45                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
46                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
47                 error("Client MUST bind resource after auth");
48         end
49
50         -- TODO also, stanzas should be returned to their original state before the function ends
51         if origin.type == "c2s" then
52                 stanza.attr.from = origin.full_jid;
53         end
54         local to = stanza.attr.to;
55         local node, host, resource = jid_split(to);
56         local to_bare = node and (node.."@"..host) or host; -- bare JID
57         local from = stanza.attr.from;
58         local from_node, from_host, from_resource = jid_split(from);
59         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
60
61         if origin.type == "s2sin" then
62                 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
63                         log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
64                         return; -- FIXME what should we do here? does this work with subdomains?
65                 end
66         end
67         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
68                 log("warn", "stanza recieved for a non-local server");
69                 return; -- FIXME what should we do here?
70         end]] -- FIXME
71
72         -- FIXME do stanzas not of jabber:client get handled by components?
73         if origin.type == "s2sin" or origin.type == "c2s" then
74                 if not to then
75                         core_handle_stanza(origin, stanza);
76                 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
77                         core_handle_stanza(origin, stanza);
78                 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
79                         modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
80                 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
81                         component_handle_stanza(origin, stanza);
82                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
83                         component_handle_stanza(origin, stanza);
84                 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
85                         component_handle_stanza(origin, stanza);
86                 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
87                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
88                 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID
89                         core_handle_stanza(origin, stanza);
90                 else
91                         core_route_stanza(origin, stanza);
92                 end
93         else
94                 core_handle_stanza(origin, stanza);
95         end
96 end
97
98 -- This function handles stanzas which are not routed any further,
99 -- that is, they are handled by this server
100 function core_handle_stanza(origin, stanza)
101         -- Handlers
102         if modules_handle_stanza(stanza.attr.to or origin.host, origin, stanza) then return; end
103         if origin.type == "c2s" or origin.type == "c2s_unauthed" then
104                 local session = origin;
105
106                 if stanza.name == "presence" and origin.roster then
107                         if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
108                                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
109                                         local subscription = origin.roster[jid].subscription;
110                                         if subscription == "both" or subscription == "from" then
111                                                 stanza.attr.to = jid;
112                                                 core_route_stanza(origin, stanza);
113                                         end
114                                 end
115                                 local node, host = jid_split(stanza.attr.from);
116                                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
117                                         if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
118                                                 stanza.attr.to = res.full_jid;
119                                                 core_route_stanza(origin, stanza);
120                                         end
121                                 end
122                                 if stanza.attr.type == nil and not origin.presence then -- initial presence
123                                         local probe = st.presence({from = origin.full_jid, type = "probe"});
124                                         for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
125                                                 local subscription = origin.roster[jid].subscription;
126                                                 if subscription == "both" or subscription == "to" then
127                                                         probe.attr.to = jid;
128                                                         core_route_stanza(origin, probe);
129                                                 end
130                                         end
131                                         for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
132                                                 if res ~= origin and res.presence then
133                                                         res.presence.attr.to = origin.full_jid;
134                                                         core_route_stanza(res, res.presence);
135                                                         res.presence.attr.to = nil;
136                                                 end
137                                         end
138                                         if origin.roster.pending then -- resend incoming subscription requests
139                                                 for jid in pairs(origin.roster.pending) do
140                                                         origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
141                                                 end
142                                         end
143                                         local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
144                                         for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
145                                                 if item.ask then
146                                                         request.attr.to = jid;
147                                                         core_route_stanza(origin, request);
148                                                 end
149                                         end
150                                         for _, msg in ipairs(offlinemanager.load(node, host) or {}) do
151                                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?
152                                         end
153                                         offlinemanager.deleteAll(node, host);
154                                 end
155                                 origin.priority = 0;
156                                 if stanza.attr.type == "unavailable" then
157                                         origin.presence = nil;
158                                 else
159                                         origin.presence = stanza;
160                                         local priority = stanza:child_with_name("priority");
161                                         if priority and #priority > 0 then
162                                                 priority = t_concat(priority);
163                                                 if s_find(priority, "^[+-]?[0-9]+$") then
164                                                         priority = tonumber(priority);
165                                                         if priority < -128 then priority = -128 end
166                                                         if priority > 127 then priority = 127 end
167                                                         origin.priority = priority;
168                                                 end
169                                         end
170                                 end
171                                 stanza.attr.to = nil; -- reset it
172                         else
173                                 log("warn", "Unhandled c2s presence: %s", tostring(stanza));
174                                 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
175                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
176                                 end
177                         end
178                 else
179                         log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
180                         if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
181                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
182                         end
183                 end -- TODO handle other stanzas
184         else
185                 log("warn", "Unhandled origin: %s", origin.type);
186                 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
187                         -- s2s stanzas can get here
188                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
189                 end
190         end
191 end
192
193 function core_route_stanza(origin, stanza)
194         -- Hooks
195         --- ...later
196
197         -- Deliver
198         local to = stanza.attr.to;
199         local node, host, resource = jid_split(to);
200         local to_bare = node and (node.."@"..host) or host; -- bare JID
201         local from = stanza.attr.from;
202         local from_node, from_host, from_resource = jid_split(from);
203         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
204
205         -- Auto-detect origin if not specified
206         origin = origin or hosts[from_host];
207         if not origin then return false; end
208         
209         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
210
211         local host_session = hosts[host]
212         if host_session and host_session.type == "local" then
213                 -- Local host
214                 local user = host_session.sessions[node];
215                 if user then
216                         local res = user.sessions[resource];
217                         if not res then
218                                 -- if we get here, resource was not specified or was unavailable
219                                 if stanza.name == "presence" then
220                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
221                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
222                                         else -- sender is available or unavailable
223                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
224                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
225                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
226                                                                 session.send(stanza);
227                                                         end
228                                                 end
229                                         end
230                                 elseif stanza.name == "message" then -- select a resource to recieve message
231                                         local priority = 0;
232                                         local recipients = {};
233                                         for _, session in pairs(user.sessions) do -- find resource with greatest priority
234                                                 local p = session.priority;
235                                                 if p > priority then
236                                                         priority = p;
237                                                         recipients = {session};
238                                                 elseif p == priority then
239                                                         t_insert(recipients, session);
240                                                 end
241                                         end
242                                         local count = 0;
243                                         for _, session in pairs(recipients) do
244                                                 session.send(stanza);
245                                                 count = count + 1;
246                                         end
247                                         if count == 0 then
248                                                 offlinemanager.store(node, host, stanza);
249                                                 -- TODO deal with storage errors
250                                         end
251                                 else
252                                         -- TODO send IQ error
253                                 end
254                         else
255                                 -- User + resource is online...
256                                 stanza.attr.to = res.full_jid; -- reset at the end of function
257                                 res.send(stanza); -- Yay \o/
258                         end
259                 else
260                         -- user not online
261                         if user_exists(node, host) then
262                                 if stanza.name == "presence" then
263                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
264                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
265                                         else
266                                                 -- TODO send unavailable presence or unsubscribed
267                                         end
268                                 elseif stanza.name == "message" then
269                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
270                                                 offlinemanager.store(node, host, stanza);
271                                                 -- FIXME don't store messages with only chat state notifications
272                                         end
273                                         -- TODO allow configuration of offline storage
274                                         -- TODO send error if not storing offline
275                                 elseif stanza.name == "iq" then
276                                         -- TODO send IQ error
277                                 end
278                         else -- user does not exist
279                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
280                                 if stanza.name == "presence" then
281                                         if stanza.attr.type == "probe" then
282                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
283                                         end
284                                         -- else ignore
285                                 else
286                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
287                                 end
288                         end
289                 end
290         elseif origin.type == "c2s" then
291                 -- Remote host
292                 local xmlns = stanza.attr.xmlns;
293                 --stanza.attr.xmlns = "jabber:server";
294                 stanza.attr.xmlns = nil;
295                 log("debug", "sending s2s stanza: %s", tostring(stanza));
296                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
297                 stanza.attr.xmlns = xmlns; -- reset
298         elseif origin.type == "component" or origin.type == "local" then
299                 -- Route via s2s for components and modules
300                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
301                 send_s2s(origin.host, host, stanza);
302         else
303                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
304         end
305         stanza.attr.to = to; -- reset
306 end
307
308 function handle_stanza_toremote(stanza)
309         log("error", "Stanza bound for remote host, but s2s is not implemented");
310 end
311
312