bbbb6385033c88604520972807d7f4dfa3b20df7
[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 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.attr.xmlns == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
55         if stanza.name == "iq" then
56                 if (stanza.attr.type == "set" or stanza.attr.type == "get") and #stanza.tags ~= 1 then
57                         origin.send(st.error_reply(stanza, "modify", "bad-request"));
58                         return;
59                 end
60         end
61
62         if origin.type == "c2s" and not origin.full_jid
63                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
64                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
65                 error("Client MUST bind resource after auth");
66         end
67
68         -- TODO also, stanzas should be returned to their original state before the function ends
69         if origin.type == "c2s" then
70                 stanza.attr.from = origin.full_jid;
71         end
72         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
73         local from = stanza.attr.from;
74         local node, host, resource;
75         local from_node, from_host, from_resource;
76         local to_bare, from_bare;
77         if to then
78                 node, host, resource = jid_prepped_split(to);
79                 if not host then
80                         error("Invalid to JID");
81                 end
82                 to_bare = node and (node.."@"..host) or host; -- bare JID
83                 if resource then to = to_bare.."/"..resource; else to = to_bare; end
84                 stanza.attr.to = to;
85         end
86         if from then
87                 from_node, from_host, from_resource = jid_prepped_split(from);
88                 if not from_host then
89                         error("Invalid from JID");
90                 end
91                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
92                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
93                 stanza.attr.from = from;
94         end
95
96         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
97                 log("warn", "stanza recieved for a non-local server");
98                 return; -- FIXME what should we do here?
99         end]] -- FIXME
100
101         -- FIXME do stanzas not of jabber:client get handled by components?
102         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and (not xmlns or xmlns == "jabber:server" or xmlns == "jabber:client") then                  
103                 if origin.type == "s2sin" and not origin.dummy then
104                         local host_status = origin.hosts[from_host];
105                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
106                                 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from_host, origin.from_host);
107                                 return; -- FIXME what should we do here? does this work with subdomains?
108                         end
109                 end
110                 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
111                         origin.directed = origin.directed or {};
112                         origin.directed[to] = true;
113                         --t_insert(origin.directed, to); -- FIXME does it make more sense to add to_bare rather than to?
114                 end
115                 if not to then
116                         core_handle_stanza(origin, stanza);
117                 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
118                         core_handle_stanza(origin, stanza);
119                 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
120                         modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
121                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
122                         component_handle_stanza(origin, stanza);
123                 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
124                         component_handle_stanza(origin, stanza);
125                 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
126                         component_handle_stanza(origin, stanza);
127                 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
128                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
129                 elseif hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
130                         core_handle_stanza(origin, stanza);
131                 else
132                         core_route_stanza(origin, stanza);
133                 end
134         else
135                 core_handle_stanza(origin, stanza);
136         end
137 end
138
139 -- This function handles stanzas which are not routed any further,
140 -- that is, they are handled by this server
141 function core_handle_stanza(origin, stanza)
142         -- Handlers
143         if modules_handle_stanza(select(2, jid_split(stanza.attr.to)) or origin.host, origin, stanza) then return; end
144         if origin.type == "c2s" or origin.type == "s2sin" then
145                 if origin.type == "c2s" then
146                         if stanza.name == "presence" and origin.roster then
147                                 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
148                                         handle_normal_presence(origin, stanza, core_route_stanza);
149                                 else
150                                         log("warn", "Unhandled c2s presence: %s", tostring(stanza));
151                                         checked_error_reply(origin, stanza);
152                                 end
153                         else
154                                 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
155                                 checked_error_reply(origin, stanza);
156                         end
157                 else -- s2s stanzas
158                         log("warn", "Unhandled s2s stanza: %s", tostring(stanza));
159                         checked_error_reply(origin, stanza);
160                 end
161         else
162                 log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
163                 checked_error_reply(origin, stanza);
164         end
165 end
166
167 function core_route_stanza(origin, stanza)
168         -- Hooks
169         --- ...later
170
171         -- Deliver
172         local to = stanza.attr.to;
173         local node, host, resource = jid_split(to);
174         local to_bare = node and (node.."@"..host) or host; -- bare JID
175         local from = stanza.attr.from;
176         local from_node, from_host, from_resource = jid_split(from);
177         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
178
179         -- Auto-detect origin if not specified
180         origin = origin or hosts[from_host];
181         if not origin then return false; end
182         
183         if hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
184                 return component_handle_stanza(origin, stanza);
185         elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
186                 return component_handle_stanza(origin, stanza);
187         elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
188                 return component_handle_stanza(origin, stanza);
189         end
190
191         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
192
193         local host_session = hosts[host]
194         if host_session and host_session.type == "local" then
195                 -- Local host
196                 local user = host_session.sessions[node];
197                 if user then
198                         local res = user.sessions[resource];
199                         if not res then
200                                 -- if we get here, resource was not specified or was unavailable
201                                 if stanza.name == "presence" then
202                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
203                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
204                                         else -- sender is available or unavailable
205                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
206                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
207                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
208                                                                 session.send(stanza);
209                                                         end
210                                                 end
211                                         end
212                                 elseif stanza.name == "message" then -- select a resource to recieve message
213                                         stanza.attr.to = to_bare;
214                                         if stanza.attr.type == 'headline' then
215                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
216                                                         if session.presence and session.priority >= 0 then
217                                                                 session.send(stanza);
218                                                         end
219                                                 end
220                                         elseif resource and stanza.attr.type == 'groupchat' then
221                                                 -- Groupchat message sent to offline resource
222                                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
223                                         else
224                                                 local priority = 0;
225                                                 local recipients = {};
226                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
227                                                         if session.presence then
228                                                                 local p = session.priority;
229                                                                 if p > priority then
230                                                                         priority = p;
231                                                                         recipients = {session};
232                                                                 elseif p == priority then
233                                                                         t_insert(recipients, session);
234                                                                 end
235                                                         end
236                                                 end
237                                                 local count = 0;
238                                                 for _, session in ipairs(recipients) do
239                                                         session.send(stanza);
240                                                         count = count + 1;
241                                                 end
242                                                 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
243                                                         offlinemanager.store(node, host, stanza);
244                                                         -- TODO deal with storage errors
245                                                 end
246                                         end
247                                 else
248                                         -- TODO send IQ error
249                                 end
250                         else
251                                 -- User + resource is online...
252                                 stanza.attr.to = res.full_jid; -- reset at the end of function
253                                 res.send(stanza); -- Yay \o/
254                         end
255                 else
256                         -- user not online
257                         if user_exists(node, host) then
258                                 if stanza.name == "presence" then
259                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
260                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
261                                         else
262                                                 -- TODO send unavailable presence or unsubscribed
263                                         end
264                                 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
265                                         stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
266                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
267                                                 offlinemanager.store(node, host, stanza);
268                                                 -- FIXME don't store messages with only chat state notifications
269                                         elseif stanza.attr.type == "groupchat" then
270                                                 local reply = st.error_reply(stanza, "cancel", "service-unavailable");
271                                                 reply.attr.from = to;
272                                                 origin.send(reply);
273                                         end
274                                         -- TODO allow configuration of offline storage
275                                         -- TODO send error if not storing offline
276                                 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
277                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
278                                 end
279                         else -- user does not exist
280                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
281                                 if stanza.name == "presence" then
282                                         local t = stanza.attr.type;
283                                         if t == "subscribe" or t == "probe" then
284                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
285                                         end
286                                         -- else ignore
287                                 elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
288                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
289                                 end
290                         end
291                 end
292         elseif origin.type == "c2s" then
293                 -- Remote host
294                 local xmlns = stanza.attr.xmlns;
295                 --stanza.attr.xmlns = "jabber:server";
296                 stanza.attr.xmlns = nil;
297                 log("debug", "sending s2s stanza: %s", tostring(stanza));
298                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
299                 stanza.attr.xmlns = xmlns; -- reset
300         elseif origin.type == "component" or origin.type == "local" then
301                 -- Route via s2s for components and modules
302                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
303                 send_s2s(origin.host, host, stanza);
304         else
305                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
306         end
307         stanza.attr.to = to; -- reset
308 end