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")
11 local jid_split = jid.split;
13 function core_process_stanza(origin, stanza)
14 local to = stanza.attr.to;
16 if not to or (hosts[to] and hosts[to].type == "local") then
17 core_handle_stanza(origin, stanza);
18 elseif origin.type == "c2s" then
19 core_route_stanza(origin, stanza);
24 function core_handle_stanza(origin, stanza)
26 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
27 local session = origin;
28 stanza.attr.from = session.full_jid;
30 log("debug", "Routing stanza");
31 -- Stanza has no to attribute
32 --local to_node, to_host, to_resource = jid_split(stanza.attr.to);
33 --if not to_host then error("Invalid destination JID: "..string.format("{ %q, %q, %q } == %q", to_node or "", to_host or "", to_resource or "", stanza.attr.to or "nil")); end
35 -- Stanza is to this server, or a user on this server
36 log("debug", "Routing stanza to local");
37 handle_stanza(session, stanza);
41 function core_route_stanza(origin, stanza)
46 function handle_stanza_nodest(stanza)
47 if stanza.name == "iq" then
48 handle_stanza_iq_no_to(session, stanza);
49 elseif stanza.name == "presence" then
50 -- Broadcast to this user's contacts
51 handle_stanza_presence_broadcast(session, stanza);
52 -- also, if it is initial presence, send out presence probes
53 if not session.last_presence then
54 handle_stanza_presence_probe_broadcast(session, stanza);
56 session.last_presence = stanza;
57 elseif stanza.name == "message" then
58 -- Treat as if message was sent to bare JID of the sender
59 handle_stanza_to_local_user(stanza);
63 function handle_stanza_tolocal(stanza)
64 local node, host, resource = jid.split(stanza.attr.to);
65 if host and hosts[host] and hosts[host].type == "local" then
66 -- Is a local host, handle internally
68 -- Is a local user, send to their session
69 log("debug", "Routing stanza to %s@%s", node, host);
70 if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what?
71 handle_stanza_to_local_user(stanza);
73 -- Is sent to this server, let's handle it...
74 log("debug", "Routing stanza to %s", host);
75 handle_stanza_to_server(stanza, session);
80 function handle_stanza_toremote(stanza)
81 log("error", "Stanza bound for remote host, but s2s is not implemented");
86 local function route_c2s_stanza(session, stanza)
87 stanza.attr.from = session.full_jid;
88 if not stanza.attr.to and session.username then
89 -- Has no 'to' attribute, handle internally
90 if stanza.name == "iq" then
91 handle_stanza_iq_no_to(session, stanza);
92 elseif stanza.name == "presence" then
93 -- Broadcast to this user's contacts
94 handle_stanza_presence_broadcast(session, stanza);
95 -- also, if it is initial presence, send out presence probes
96 if not session.last_presence then
97 handle_stanza_presence_probe_broadcast(session, stanza);
99 session.last_presence = stanza;
100 elseif stanza.name == "message" then
101 -- Treat as if message was sent to bare JID of the sender
102 handle_stanza_to_local_user(stanza);
105 local node, host, resource = jid.split(stanza.attr.to);
106 if host and hosts[host] and hosts[host].type == "local" then
107 -- Is a local host, handle internally
109 -- Is a local user, send to their session
110 if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what?
111 handle_stanza_to_local_user(stanza);
113 -- Is sent to this server, let's handle it...
114 handle_stanza_to_server(stanza, session);
117 -- Is not for us or a local user, route accordingly
118 route_s2s_stanza(stanza);
122 function handle_stanza_no_to(session, stanza)
123 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
124 local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns);
125 if stanza.attr.type == "get" or stanza.attr.type == "set" then
126 if iq_handlers[xmlns] then
127 if iq_handlers[xmlns](stanza) then return; end; -- If handler returns true, it handled it
129 -- Oh, handler didn't handle it. Need to send service-unavailable now.
130 log("warn", "Unhandled namespace: "..xmlns);
131 session:send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
136 function handle_stanza_to_local_user(stanza)
137 if stanza.name == "message" then
138 handle_stanza_message_to_local_user(stanza);
139 elseif stanza.name == "presence" then
140 handle_stanza_presence_to_local_user(stanza);
141 elseif stanza.name == "iq" then
142 handle_stanza_iq_to_local_user(stanza);
146 function handle_stanza_message_to_local_user(stanza)
147 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource;
148 local destuser = hosts[host].sessions[node];
150 if resource and destuser[resource] then
151 destuser[resource]:send(stanza);
153 -- Bare JID, or resource offline
155 for resource, session in pairs(destuser.sessions) do
156 if not best_session then best_session = session;
157 elseif session.priority >= best_session.priority and session.priority >= 0 then
158 best_session = session;
161 if not best_session then
162 offlinemessage.new(node, host, stanza);
164 print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'");
165 destuser[best_session]:send(stanza);
170 offlinemessage.new(node, host, stanza);
174 function handle_stanza_presence_to_local_user(stanza)
175 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource;
176 local destuser = hosts[host].sessions[node];
179 if destuser[resource] then
180 destuser[resource]:send(stanza);
185 -- Broadcast to all user's resources
186 for resource, session in pairs(destuser.sessions) do
187 session:send(stanza);
193 function handle_stanza_iq_to_local_user(stanza)
198 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource;
199 local destuser = hosts[host].sessions[node];
200 if destuser and destuser.sessions then
202 if resource and destuser.sessions[resource] then
203 stanza.to:send(stanza);
205 --User is online, but specified resource isn't (or no resource specified)
207 for resource, session in pairs(destuser.sessions) do
208 if not best_session then best_session = session;
209 elseif session.priority >= best_session.priority and session.priority >= 0 then
210 best_session = session;
213 if not best_session then
214 offlinemessage.new(node, host, stanza);
216 print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'");
217 resource = best_session.resource;
220 if destuser.sessions[resource] == session then
221 log("warn", "core", "Attempt to send stanza to self, dropping...");
223 print("...sending...", tostring(stanza));
224 --destuser.sessions[resource].conn.write(tostring(data));
225 print(" to conn ", destuser.sessions[resource].conn);
226 destuser.sessions[resource].conn.write(tostring(stanza));
229 elseif stanza.name == "message" then
230 print(" ...will be stored offline");
231 offlinemessage.new(node, host, stanza);
232 elseif stanza.name == "iq" then
233 print(" ...is an iq");
234 stanza.from:send(st.reply(stanza)
235 :tag("error", { type = "cancel" })
236 :tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" }));
240 -- Broadcast a presence stanza to all of a user's contacts
241 function handle_stanza_presence_broadcast(session, stanza)
242 if session.roster then
243 local initial_presence = not session.last_presence;
244 session.last_presence = stanza;
246 -- Broadcast presence and probes
247 local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type });
249 for child in stanza:childtags() do
250 broadcast:add_child(child);
252 for contact_jid in pairs(session.roster) do
253 broadcast.attr.to = contact_jid;
254 send_to(contact_jid, broadcast);
255 if initial_presence then
256 local node, host = jid.split(contact_jid);
257 if hosts[host] and hosts[host].type == "local" then
258 local contact = hosts[host].sessions[node]
260 local pres = st.presence { to = session.full_jid };
261 for resource, contact_session in pairs(contact.sessions) do
262 if contact_session.last_presence then
263 pres.tags = contact_session.last_presence.tags;
264 pres.attr.from = contact_session.full_jid;
269 --FIXME: Do we send unavailable if they are offline?
271 probe.attr.to = contact;
272 send_to(contact, probe);
277 -- Probe for our contacts' presence
281 -- Broadcast presence probes to all of a user's contacts
282 function handle_stanza_presence_probe_broadcast(session, stanza)
286 function handle_stanza_to_server(stanza)
289 function handle_stanza_iq_no_to(session, stanza)