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