3213e9869c96e385f4e640755b556afcc3ae6fdd
[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 function core_process_stanza(origin, stanza)
48         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
49
50         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
51         -- TODO verify validity of stanza (as well as JID validity)
52         if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
53         if stanza.name == "iq" then
54                 if (stanza.attr.type == "set" or stanza.attr.type == "get") and #stanza.tags ~= 1 then
55                         origin.send(st.error_reply(stanza, "modify", "bad-request"));
56                         return;
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                         log("warn", "Received stanza with invalid destination JID: %s", to);
79                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
80                         return;
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                 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
88                 from_node, from_host, from_resource = jid_prepped_split(from);
89                 if not from_host then
90                         log("warn", "Received stanza with invalid source JID: %s", from);
91                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
92                         return;
93                 end
94                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
95                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
96                 stanza.attr.from = from;
97         end
98
99         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
100                 log("warn", "stanza recieved for a non-local server");
101                 return; -- FIXME what should we do here?
102         end]] -- FIXME
103
104         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == "jabber:client" then
105                 if origin.type == "s2sin" and not origin.dummy then
106                         local host_status = origin.hosts[from_host];
107                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
108                                 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from_host, origin.from_host);
109                                 return; -- FIXME what should we do here? does this work with subdomains?
110                         end
111                 end
112                 local event_data = {origin=origin, stanza=stanza};
113                 if fire_event(tostring(host or origin.host).."/"..stanza.name, event_data) then
114                         -- event handled
115                 elseif 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 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 hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
126                         core_handle_stanza(origin, stanza);
127                 else
128                         core_route_stanza(origin, stanza);
129                 end
130         else
131                 core_handle_stanza(origin, stanza);
132         end
133 end
134
135 -- This function handles stanzas which are not routed any further,
136 -- that is, they are handled by this server
137 function core_handle_stanza(origin, stanza)
138         if not modules_handle_stanza(select(2, jid_split(stanza.attr.to)) or origin.host or origin.to_host, origin, stanza) then
139                 log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
140                 if stanza.attr.xmlns == "jabber:client" then
141                         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
142                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
143                         end
144                 else
145                         origin:close("unsupported-stanza-type");
146                 end
147         end
148 end
149
150 function core_route_stanza(origin, stanza)
151         -- Hooks
152         --- ...later
153
154         -- Deliver
155         local to = stanza.attr.to;
156         local node, host, resource = jid_split(to);
157         local to_bare = node and (node.."@"..host) or host; -- bare JID
158         local from = stanza.attr.from;
159         local from_node, from_host, from_resource = jid_split(from);
160         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
161
162         -- Auto-detect origin if not specified
163         origin = origin or hosts[from_host];
164         if not origin then return false; end
165         
166         if hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
167                 return component_handle_stanza(origin, stanza);
168         elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
169                 return component_handle_stanza(origin, stanza);
170         elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
171                 return component_handle_stanza(origin, stanza);
172         end
173
174         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error") then resource = nil; end
175
176         local host_session = hosts[host]
177         if host_session and host_session.type == "local" then
178                 -- Local host
179                 local user = host_session.sessions[node];
180                 if user then
181                         local res = user.sessions[resource];
182                         if not res then
183                                 -- if we get here, resource was not specified or was unavailable
184                                 if stanza.name == "presence" then
185                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
186                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
187                                         elseif not resource then -- sender is available or unavailable or error
188                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
189                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
190                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
191                                                                 session.send(stanza);
192                                                         end
193                                                 end
194                                         end
195                                 elseif stanza.name == "message" then -- select a resource to recieve message
196                                         stanza.attr.to = to_bare;
197                                         if stanza.attr.type == 'headline' then
198                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
199                                                         if session.presence and session.priority >= 0 then
200                                                                 session.send(stanza);
201                                                         end
202                                                 end
203                                         elseif resource and stanza.attr.type == 'groupchat' then
204                                                 -- Groupchat message sent to offline resource
205                                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
206                                         else
207                                                 local priority = 0;
208                                                 local recipients = {};
209                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
210                                                         if session.presence then
211                                                                 local p = session.priority;
212                                                                 if p > priority then
213                                                                         priority = p;
214                                                                         recipients = {session};
215                                                                 elseif p == priority then
216                                                                         t_insert(recipients, session);
217                                                                 end
218                                                         end
219                                                 end
220                                                 local count = 0;
221                                                 for _, session in ipairs(recipients) do
222                                                         session.send(stanza);
223                                                         count = count + 1;
224                                                 end
225                                                 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
226                                                         offlinemanager.store(node, host, stanza);
227                                                         -- TODO deal with storage errors
228                                                 end
229                                         end
230                                 elseif stanza.attr.type == "get" or stanza.attr.type == "set" then
231                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
232                                 end
233                         else
234                                 -- User + resource is online...
235                                 stanza.attr.to = res.full_jid; -- reset at the end of function
236                                 res.send(stanza); -- Yay \o/
237                         end
238                 else
239                         -- user not online
240                         if user_exists(node, host) then
241                                 if stanza.name == "presence" then
242                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
243                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
244                                         else
245                                                 -- TODO send unavailable presence or unsubscribed
246                                         end
247                                 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
248                                         stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
249                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
250                                                 offlinemanager.store(node, host, stanza);
251                                                 -- FIXME don't store messages with only chat state notifications
252                                         elseif stanza.attr.type == "groupchat" then
253                                                 local reply = st.error_reply(stanza, "cancel", "service-unavailable");
254                                                 reply.attr.from = to;
255                                                 origin.send(reply);
256                                         end
257                                         -- TODO allow configuration of offline storage
258                                         -- TODO send error if not storing offline
259                                 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
260                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
261                                 end
262                         else -- user does not exist
263                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
264                                 if stanza.name == "presence" then
265                                         local t = stanza.attr.type;
266                                         if t == "subscribe" or t == "probe" then
267                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
268                                         end
269                                         -- else ignore
270                                 elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
271                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
272                                 end
273                         end
274                 end
275         elseif origin.type == "c2s" then
276                 -- Remote host
277                 local xmlns = stanza.attr.xmlns;
278                 --stanza.attr.xmlns = "jabber:server";
279                 stanza.attr.xmlns = nil;
280                 log("debug", "sending s2s stanza: %s", tostring(stanza));
281                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
282                 stanza.attr.xmlns = xmlns; -- reset
283         elseif origin.type == "component" or origin.type == "local" then
284                 -- Route via s2s for components and modules
285                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
286                 send_s2s(origin.host, host, stanza);
287         else
288                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
289         end
290         stanza.attr.to = to; -- reset
291 end