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