2 -- The code in this file should be self-explanatory, though the logic is horrible
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921)
6 require "core.servermanager"
8 local log = require "util.logger".init("stanzarouter")
10 local st = require "util.stanza";
11 local send = require "core.sessionmanager".send_to_session;
12 local send_s2s = require "core.s2smanager".send_to_host;
13 local user_exists = require "core.usermanager".user_exists;
15 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
16 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
17 local format = string.format;
18 local tostring = tostring;
20 local jid_split = require "util.jid".split;
23 function core_process_stanza(origin, stanza)
24 log("debug", "Received: "..tostring(stanza))
25 -- TODO verify validity of stanza (as well as JID validity)
26 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
27 if stanza.attr.type == "set" or stanza.attr.type == "get" then
29 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
34 if origin.type == "c2s" and not origin.full_jid
35 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
36 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
37 error("Client MUST bind resource after auth");
40 local to = stanza.attr.to;
41 -- TODO also, stazas should be returned to their original state before the function ends
42 if origin.type == "c2s" then
43 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
47 core_handle_stanza(origin, stanza);
48 elseif hosts[to] and hosts[to].type == "local" then
49 core_handle_stanza(origin, stanza);
50 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
51 core_handle_stanza(origin, stanza);
52 elseif origin.type == "c2s" then
53 core_route_stanza(origin, stanza);
57 -- This function handles stanzas which are not routed any further,
58 -- that is, they are handled by this server
59 function core_handle_stanza(origin, stanza)
61 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
62 local session = origin;
64 if stanza.name == "presence" and origin.roster then
65 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
66 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
67 local subscription = origin.roster[jid].subscription;
68 if subscription == "both" or subscription == "from" then
70 core_route_stanza(origin, stanza);
73 --[[local node, host = jid_split(stanza.attr.from);
74 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
76 res = user.sessions[k];
80 if not origin.presence then -- presence probes on initial presence
81 local probe = st.presence({from = origin.full_jid, type = "probe"});
82 for jid in pairs(origin.roster) do
83 local subscription = origin.roster[jid].subscription;
84 if subscription == "both" or subscription == "to" then
86 core_route_stanza(origin, probe);
90 origin.presence = stanza;
91 stanza.attr.to = nil; -- reset it
93 -- TODO error, bad type
96 log("debug", "Routing stanza to local");
97 handle_stanza(session, stanza);
99 elseif origin.type == "s2sin_unauthed" then
100 if stanza.name == "verify" and stanza.attr.xmlns == "jabber:server:dialback" then
101 log("debug", "verifying dialback key...");
102 local attr = stanza.attr;
103 print(tostring(attr.to), tostring(attr.from))
104 print(tostring(origin.to_host), tostring(origin.from_host))
105 -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
106 --if attr.from ~= origin.to_host then error("invalid-from"); end
107 local type = "invalid";
108 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
111 origin.send(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
113 elseif origin.type == "s2sout_unauthed" then
114 if stanza.name == "result" and stanza.attr.xmlns == "jabber:server:dialback" then
115 if stanza.attr.type == "valid" then
116 s2s_make_authenticated(origin);
119 error("dialback failed!");
123 log("warn", "Unhandled origin: %s", origin.type);
127 -- TODO: Does this function belong here?
128 function is_authorized_to_see_presence(origin, username, host)
129 local roster = datamanager.load(username, host, "roster") or {};
130 local item = roster[origin.username.."@"..origin.host];
131 return item and (item.subscription == "both" or item.subscription == "from");
134 function core_route_stanza(origin, stanza)
139 local to = stanza.attr.to;
140 local node, host, resource = jid_split(to);
142 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
144 local host_session = hosts[host]
145 if host_session and host_session.type == "local" then
147 local user = host_session.sessions[node];
149 local res = user.sessions[resource];
151 -- if we get here, resource was not specified or was unavailable
152 if stanza.name == "presence" then
153 if stanza.attr.type == "probe" then
154 if is_authorized_to_see_presence(origin, node, host) then
155 for k in pairs(user.sessions) do -- return presence for all resources
156 if user.sessions[k].presence then
157 local pres = user.sessions[k].presence;
158 pres.attr.to = origin.full_jid;
159 pres.attr.from = user.sessions[k].full_jid;
162 pres.attr.from = nil;
166 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
169 for k in pairs(user.sessions) do -- presence broadcast to all user resources
170 if user.sessions[k].full_jid then
171 stanza.attr.to = user.sessions[k].full_jid;
172 send(user.sessions[k], stanza);
176 elseif stanza.name == "message" then -- select a resource to recieve message
177 for k in pairs(user.sessions) do
178 if user.sessions[k].full_jid then
179 res = user.sessions[k];
183 -- TODO find resource with greatest priority
186 -- TODO send IQ error
189 -- User + resource is online...
190 stanza.attr.to = res.full_jid;
191 send(res, stanza); -- Yay \o/
195 if user_exists(node, host) then
196 if stanza.name == "presence" then
197 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
198 -- TODO send last recieved unavailable presence
200 -- TODO send unavailable presence
202 elseif stanza.name == "message" then
203 -- TODO send message error, or store offline messages
204 elseif stanza.name == "iq" then
205 -- TODO send IQ error
207 else -- user does not exist
208 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
209 if stanza.name == "presence" then
210 if stanza.attr.type == "probe" then
211 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
215 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
221 log("debug", "sending s2s stanza: %s", tostring(stanza));
222 stanza.attr.xmlns = "jabber:server";
223 send_s2s(origin.host, host, stanza);
225 stanza.attr.to = to; -- reset
228 function handle_stanza_toremote(stanza)
229 log("error", "Stanza bound for remote host, but s2s is not implemented");