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