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