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