24eadedc4c2ec2bb112fe154c6de99d7bff15b2a
[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" 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_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
103                         component_handle_stanza(origin, stanza);
104                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
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                         core_route_stanza(origin, stanza);
114                 end
115         else
116                 core_handle_stanza(origin, stanza);
117         end
118 end
119
120 -- This function handles stanzas which are not routed any further,
121 -- that is, they are handled by this server
122 function core_handle_stanza(origin, stanza)
123         -- Handlers
124         if modules_handle_stanza(stanza.attr.to or origin.host, origin, stanza) then return; end
125         if origin.type == "c2s" or origin.type == "s2sin" then
126                 if origin.type == "c2s" then
127                         if stanza.name == "presence" and origin.roster then
128                                 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
129                                         handle_normal_presence(origin, stanza, core_route_stanza);
130                                 else
131                                         log("warn", "Unhandled c2s presence: %s", tostring(stanza));
132                                         if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
133                                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
134                                         end
135                                 end
136                         else
137                                 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
138                                 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
139                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
140                                 end
141                         end
142                 else -- s2s stanzas
143                         log("warn", "Unhandled s2s stanza: %s", tostring(stanza));
144                         if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
145                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
146                         end
147                 end
148         else
149                 log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
150         end
151 end
152
153 function core_route_stanza(origin, stanza)
154         -- Hooks
155         --- ...later
156
157         -- Deliver
158         local to = stanza.attr.to;
159         local node, host, resource = jid_split(to);
160         local to_bare = node and (node.."@"..host) or host; -- bare JID
161         local from = stanza.attr.from;
162         local from_node, from_host, from_resource = jid_split(from);
163         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
164
165         -- Auto-detect origin if not specified
166         origin = origin or hosts[from_host];
167         if not origin then return false; end
168         
169         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
170
171         local host_session = hosts[host]
172         if host_session and host_session.type == "local" then
173                 -- Local host
174                 local user = host_session.sessions[node];
175                 if user then
176                         local res = user.sessions[resource];
177                         if not res then
178                                 -- if we get here, resource was not specified or was unavailable
179                                 if stanza.name == "presence" then
180                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
181                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
182                                         else -- sender is available or unavailable
183                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
184                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
185                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
186                                                                 session.send(stanza);
187                                                         end
188                                                 end
189                                         end
190                                 elseif stanza.name == "message" then -- select a resource to recieve message
191                                         local priority = 0;
192                                         local recipients = {};
193                                         for _, session in pairs(user.sessions) do -- find resource with greatest priority
194                                                 local p = session.priority;
195                                                 if p > priority then
196                                                         priority = p;
197                                                         recipients = {session};
198                                                 elseif p == priority then
199                                                         t_insert(recipients, session);
200                                                 end
201                                         end
202                                         local count = 0;
203                                         for _, session in pairs(recipients) do
204                                                 session.send(stanza);
205                                                 count = count + 1;
206                                         end
207                                         if count == 0 then
208                                                 offlinemanager.store(node, host, stanza);
209                                                 -- TODO deal with storage errors
210                                         end
211                                 else
212                                         -- TODO send IQ error
213                                 end
214                         else
215                                 -- User + resource is online...
216                                 stanza.attr.to = res.full_jid; -- reset at the end of function
217                                 res.send(stanza); -- Yay \o/
218                         end
219                 else
220                         -- user not online
221                         if user_exists(node, host) then
222                                 if stanza.name == "presence" then
223                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
224                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
225                                         else
226                                                 -- TODO send unavailable presence or unsubscribed
227                                         end
228                                 elseif stanza.name == "message" then
229                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
230                                                 offlinemanager.store(node, host, stanza);
231                                                 -- FIXME don't store messages with only chat state notifications
232                                         end
233                                         -- TODO allow configuration of offline storage
234                                         -- TODO send error if not storing offline
235                                 elseif stanza.name == "iq" then
236                                         -- TODO send IQ error
237                                 end
238                         else -- user does not exist
239                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
240                                 if stanza.name == "presence" then
241                                         if stanza.attr.type == "probe" then
242                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
243                                         end
244                                         -- else ignore
245                                 else
246                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
247                                 end
248                         end
249                 end
250         elseif origin.type == "c2s" then
251                 -- Remote host
252                 local xmlns = stanza.attr.xmlns;
253                 --stanza.attr.xmlns = "jabber:server";
254                 stanza.attr.xmlns = nil;
255                 log("debug", "sending s2s stanza: %s", tostring(stanza));
256                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
257                 stanza.attr.xmlns = xmlns; -- reset
258         elseif origin.type == "component" or origin.type == "local" then
259                 -- Route via s2s for components and modules
260                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
261                 send_s2s(origin.host, host, stanza);
262         else
263                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
264         end
265         stanza.attr.to = to; -- reset
266 end