2 local log = require "util.logger".init("stanzarouter")
4 local st = require "util.stanza";
5 local send_s2s = require "core.s2smanager".send_to_host;
6 local user_exists = require "core.usermanager".user_exists;
8 local rostermanager = require "core.rostermanager";
9 local sessionmanager = require "core.sessionmanager";
10 local offlinemanager = require "core.offlinemanager";
12 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
13 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
15 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
16 local component_handle_stanza = require "core.componentmanager".handle_stanza;
18 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
19 local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
21 local format = string.format;
22 local tostring = tostring;
23 local t_concat = table.concat;
24 local t_insert = table.insert;
25 local tonumber = tonumber;
26 local s_find = string.find;
28 local jid_split = require "util.jid".split;
31 function core_process_stanza(origin, stanza)
32 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
34 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
35 -- TODO verify validity of stanza (as well as JID validity)
36 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
37 if stanza.attr.type == "set" or stanza.attr.type == "get" then
39 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
44 if origin.type == "c2s" and not origin.full_jid
45 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
46 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
47 error("Client MUST bind resource after auth");
50 -- TODO also, stanzas should be returned to their original state before the function ends
51 if origin.type == "c2s" then
52 stanza.attr.from = origin.full_jid;
54 local to = stanza.attr.to;
55 local node, host, resource = jid_split(to);
56 local to_bare = node and (node.."@"..host) or host; -- bare JID
57 local from = stanza.attr.from;
58 local from_node, from_host, from_resource = jid_split(from);
59 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
61 if origin.type == "s2sin" then
62 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
63 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
64 return; -- FIXME what should we do here? does this work with subdomains?
67 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
68 log("warn", "stanza recieved for a non-local server");
69 return; -- FIXME what should we do here?
72 -- FIXME do stanzas not of jabber:client get handled by components?
73 if origin.type == "s2sin" or origin.type == "c2s" then
75 core_handle_stanza(origin, stanza);
76 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
77 core_handle_stanza(origin, stanza);
78 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
79 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
80 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
81 component_handle_stanza(origin, stanza);
82 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
83 component_handle_stanza(origin, stanza);
84 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
85 component_handle_stanza(origin, stanza);
86 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
87 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
88 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID
89 core_handle_stanza(origin, stanza);
91 core_route_stanza(origin, stanza);
94 core_handle_stanza(origin, stanza);
98 -- This function handles stanzas which are not routed any further,
99 -- that is, they are handled by this server
100 function core_handle_stanza(origin, stanza)
102 if modules_handle_stanza(stanza.attr.to or origin.host, origin, stanza) then return; end
103 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
104 local session = origin;
106 if stanza.name == "presence" and origin.roster then
107 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
108 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
109 local subscription = origin.roster[jid].subscription;
110 if subscription == "both" or subscription == "from" then
111 stanza.attr.to = jid;
112 core_route_stanza(origin, stanza);
115 local node, host = jid_split(stanza.attr.from);
116 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
117 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
118 stanza.attr.to = res.full_jid;
119 core_route_stanza(origin, stanza);
122 if stanza.attr.type == nil and not origin.presence then -- initial presence
123 local probe = st.presence({from = origin.full_jid, type = "probe"});
124 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
125 local subscription = origin.roster[jid].subscription;
126 if subscription == "both" or subscription == "to" then
128 core_route_stanza(origin, probe);
131 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
132 if res ~= origin and res.presence then
133 res.presence.attr.to = origin.full_jid;
134 core_route_stanza(res, res.presence);
135 res.presence.attr.to = nil;
138 if origin.roster.pending then -- resend incoming subscription requests
139 for jid in pairs(origin.roster.pending) do
140 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
143 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
144 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
146 request.attr.to = jid;
147 core_route_stanza(origin, request);
150 for _, msg in ipairs(offlinemanager.load(node, host) or {}) do
151 origin.send(msg); -- FIXME do we need to modify to/from in any way?
153 offlinemanager.deleteAll(node, host);
156 if stanza.attr.type == "unavailable" then
157 origin.presence = nil;
159 origin.presence = stanza;
160 local priority = stanza:child_with_name("priority");
161 if priority and #priority > 0 then
162 priority = t_concat(priority);
163 if s_find(priority, "^[+-]?[0-9]+$") then
164 priority = tonumber(priority);
165 if priority < -128 then priority = -128 end
166 if priority > 127 then priority = 127 end
167 origin.priority = priority;
171 stanza.attr.to = nil; -- reset it
173 log("warn", "Unhandled c2s presence: %s", tostring(stanza));
174 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
175 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
179 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
180 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
181 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
183 end -- TODO handle other stanzas
185 log("warn", "Unhandled origin: %s", origin.type);
186 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
187 -- s2s stanzas can get here
188 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
193 function core_route_stanza(origin, stanza)
198 local to = stanza.attr.to;
199 local node, host, resource = jid_split(to);
200 local to_bare = node and (node.."@"..host) or host; -- bare JID
201 local from = stanza.attr.from;
202 local from_node, from_host, from_resource = jid_split(from);
203 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
205 -- Auto-detect origin if not specified
206 origin = origin or hosts[from_host];
207 if not origin then return false; end
209 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
211 local host_session = hosts[host]
212 if host_session and host_session.type == "local" then
214 local user = host_session.sessions[node];
216 local res = user.sessions[resource];
218 -- if we get here, resource was not specified or was unavailable
219 if stanza.name == "presence" then
220 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
221 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
222 else -- sender is available or unavailable
223 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
224 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
225 stanza.attr.to = session.full_jid; -- reset at the end of function
226 session.send(stanza);
230 elseif stanza.name == "message" then -- select a resource to recieve message
232 local recipients = {};
233 for _, session in pairs(user.sessions) do -- find resource with greatest priority
234 local p = session.priority;
237 recipients = {session};
238 elseif p == priority then
239 t_insert(recipients, session);
243 for _, session in pairs(recipients) do
244 session.send(stanza);
248 offlinemanager.store(node, host, stanza);
249 -- TODO deal with storage errors
252 -- TODO send IQ error
255 -- User + resource is online...
256 stanza.attr.to = res.full_jid; -- reset at the end of function
257 res.send(stanza); -- Yay \o/
261 if user_exists(node, host) then
262 if stanza.name == "presence" then
263 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
264 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
266 -- TODO send unavailable presence or unsubscribed
268 elseif stanza.name == "message" then
269 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
270 offlinemanager.store(node, host, stanza);
271 -- FIXME don't store messages with only chat state notifications
273 -- TODO allow configuration of offline storage
274 -- TODO send error if not storing offline
275 elseif stanza.name == "iq" then
276 -- TODO send IQ error
278 else -- user does not exist
279 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
280 if stanza.name == "presence" then
281 if stanza.attr.type == "probe" then
282 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
286 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
290 elseif origin.type == "c2s" then
292 local xmlns = stanza.attr.xmlns;
293 --stanza.attr.xmlns = "jabber:server";
294 stanza.attr.xmlns = nil;
295 log("debug", "sending s2s stanza: %s", tostring(stanza));
296 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
297 stanza.attr.xmlns = xmlns; -- reset
298 elseif origin.type == "component" or origin.type == "local" then
299 -- Route via s2s for components and modules
300 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
301 send_s2s(origin.host, host, stanza);
303 log("warn", "received stanza from unhandled connection type: %s", origin.type);
305 stanza.attr.to = to; -- reset
308 function handle_stanza_toremote(stanza)
309 log("error", "Stanza bound for remote host, but s2s is not implemented");