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