30
|
1 |
|
|
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) |
|
5 |
|
|
6 |
require "core.servermanager" |
|
7 |
|
|
8 |
local log = require "util.logger".init("stanzarouter") |
|
9 |
|
|
10 |
require "util.jid" |
|
11 |
local jid_split = jid.split; |
|
12 |
|
|
13 |
function core_process_stanza(origin, stanza) |
66
|
14 |
log("debug", "Received: "..tostring(stanza)) |
30
|
15 |
local to = stanza.attr.to; |
|
16 |
|
|
17 |
if not to or (hosts[to] and hosts[to].type == "local") then |
|
18 |
core_handle_stanza(origin, stanza); |
|
19 |
elseif origin.type == "c2s" then |
|
20 |
core_route_stanza(origin, stanza); |
|
21 |
end |
|
22 |
|
|
23 |
end |
|
24 |
|
|
25 |
function core_handle_stanza(origin, stanza) |
|
26 |
-- Handlers |
|
27 |
if origin.type == "c2s" or origin.type == "c2s_unauthed" then |
|
28 |
local session = origin; |
|
29 |
stanza.attr.from = session.full_jid; |
|
30 |
|
|
31 |
log("debug", "Routing stanza"); |
|
32 |
-- Stanza has no to attribute |
|
33 |
--local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
|
34 |
--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 |
|
|
36 |
-- Stanza is to this server, or a user on this server |
|
37 |
log("debug", "Routing stanza to local"); |
|
38 |
handle_stanza(session, stanza); |
|
39 |
end |
|
40 |
end |
|
41 |
|
|
42 |
function core_route_stanza(origin, stanza) |
|
43 |
-- Hooks |
|
44 |
-- Deliver |
|
45 |
end |
|
46 |
|
|
47 |
function handle_stanza_nodest(stanza) |
|
48 |
if stanza.name == "iq" then |
|
49 |
handle_stanza_iq_no_to(session, stanza); |
|
50 |
elseif stanza.name == "presence" then |
|
51 |
-- Broadcast to this user's contacts |
|
52 |
handle_stanza_presence_broadcast(session, stanza); |
|
53 |
-- also, if it is initial presence, send out presence probes |
|
54 |
if not session.last_presence then |
|
55 |
handle_stanza_presence_probe_broadcast(session, stanza); |
|
56 |
end |
|
57 |
session.last_presence = stanza; |
|
58 |
elseif stanza.name == "message" then |
|
59 |
-- Treat as if message was sent to bare JID of the sender |
|
60 |
handle_stanza_to_local_user(stanza); |
|
61 |
end |
|
62 |
end |
|
63 |
|
|
64 |
function handle_stanza_tolocal(stanza) |
|
65 |
local node, host, resource = jid.split(stanza.attr.to); |
|
66 |
if host and hosts[host] and hosts[host].type == "local" then |
|
67 |
-- Is a local host, handle internally |
|
68 |
if node then |
|
69 |
-- Is a local user, send to their session |
|
70 |
log("debug", "Routing stanza to %s@%s", node, host); |
|
71 |
if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what? |
|
72 |
handle_stanza_to_local_user(stanza); |
|
73 |
else |
|
74 |
-- Is sent to this server, let's handle it... |
|
75 |
log("debug", "Routing stanza to %s", host); |
|
76 |
handle_stanza_to_server(stanza, session); |
|
77 |
end |
|
78 |
end |
|
79 |
end |
|
80 |
|
|
81 |
function handle_stanza_toremote(stanza) |
|
82 |
log("error", "Stanza bound for remote host, but s2s is not implemented"); |
|
83 |
end |
|
84 |
|
|
85 |
|
|
86 |
--[[ |
|
87 |
local function route_c2s_stanza(session, stanza) |
|
88 |
stanza.attr.from = session.full_jid; |
|
89 |
if not stanza.attr.to and session.username then |
|
90 |
-- Has no 'to' attribute, handle internally |
|
91 |
if stanza.name == "iq" then |
|
92 |
handle_stanza_iq_no_to(session, stanza); |
|
93 |
elseif stanza.name == "presence" then |
|
94 |
-- Broadcast to this user's contacts |
|
95 |
handle_stanza_presence_broadcast(session, stanza); |
|
96 |
-- also, if it is initial presence, send out presence probes |
|
97 |
if not session.last_presence then |
|
98 |
handle_stanza_presence_probe_broadcast(session, stanza); |
|
99 |
end |
|
100 |
session.last_presence = stanza; |
|
101 |
elseif stanza.name == "message" then |
|
102 |
-- Treat as if message was sent to bare JID of the sender |
|
103 |
handle_stanza_to_local_user(stanza); |
|
104 |
end |
|
105 |
end |
|
106 |
local node, host, resource = jid.split(stanza.attr.to); |
|
107 |
if host and hosts[host] and hosts[host].type == "local" then |
|
108 |
-- Is a local host, handle internally |
|
109 |
if node then |
|
110 |
-- Is a local user, send to their session |
|
111 |
if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what? |
|
112 |
handle_stanza_to_local_user(stanza); |
|
113 |
else |
|
114 |
-- Is sent to this server, let's handle it... |
|
115 |
handle_stanza_to_server(stanza, session); |
|
116 |
end |
|
117 |
else |
|
118 |
-- Is not for us or a local user, route accordingly |
|
119 |
route_s2s_stanza(stanza); |
|
120 |
end |
|
121 |
end |
|
122 |
|
|
123 |
function handle_stanza_no_to(session, stanza) |
|
124 |
if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end |
|
125 |
local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns); |
|
126 |
if stanza.attr.type == "get" or stanza.attr.type == "set" then |
|
127 |
if iq_handlers[xmlns] then |
|
128 |
if iq_handlers[xmlns](stanza) then return; end; -- If handler returns true, it handled it |
|
129 |
end |
|
130 |
-- Oh, handler didn't handle it. Need to send service-unavailable now. |
|
131 |
log("warn", "Unhandled namespace: "..xmlns); |
|
132 |
session:send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id)); |
|
133 |
return; -- All done! |
|
134 |
end |
|
135 |
end |
|
136 |
|
|
137 |
function handle_stanza_to_local_user(stanza) |
|
138 |
if stanza.name == "message" then |
|
139 |
handle_stanza_message_to_local_user(stanza); |
|
140 |
elseif stanza.name == "presence" then |
|
141 |
handle_stanza_presence_to_local_user(stanza); |
|
142 |
elseif stanza.name == "iq" then |
|
143 |
handle_stanza_iq_to_local_user(stanza); |
|
144 |
end |
|
145 |
end |
|
146 |
|
|
147 |
function handle_stanza_message_to_local_user(stanza) |
|
148 |
local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; |
|
149 |
local destuser = hosts[host].sessions[node]; |
|
150 |
if destuser then |
|
151 |
if resource and destuser[resource] then |
|
152 |
destuser[resource]:send(stanza); |
|
153 |
else |
|
154 |
-- Bare JID, or resource offline |
|
155 |
local best_session; |
|
156 |
for resource, session in pairs(destuser.sessions) do |
|
157 |
if not best_session then best_session = session; |
|
158 |
elseif session.priority >= best_session.priority and session.priority >= 0 then |
|
159 |
best_session = session; |
|
160 |
end |
|
161 |
end |
|
162 |
if not best_session then |
|
163 |
offlinemessage.new(node, host, stanza); |
|
164 |
else |
|
165 |
print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); |
|
166 |
destuser[best_session]:send(stanza); |
|
167 |
end |
|
168 |
end |
|
169 |
else |
|
170 |
-- User is offline |
|
171 |
offlinemessage.new(node, host, stanza); |
|
172 |
end |
|
173 |
end |
|
174 |
|
|
175 |
function handle_stanza_presence_to_local_user(stanza) |
|
176 |
local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; |
|
177 |
local destuser = hosts[host].sessions[node]; |
|
178 |
if destuser then |
|
179 |
if resource then |
|
180 |
if destuser[resource] then |
|
181 |
destuser[resource]:send(stanza); |
|
182 |
else |
|
183 |
return; |
|
184 |
end |
|
185 |
else |
|
186 |
-- Broadcast to all user's resources |
|
187 |
for resource, session in pairs(destuser.sessions) do |
|
188 |
session:send(stanza); |
|
189 |
end |
|
190 |
end |
|
191 |
end |
|
192 |
end |
|
193 |
|
|
194 |
function handle_stanza_iq_to_local_user(stanza) |
|
195 |
|
|
196 |
end |
|
197 |
|
|
198 |
function foo() |
|
199 |
local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; |
|
200 |
local destuser = hosts[host].sessions[node]; |
|
201 |
if destuser and destuser.sessions then |
|
202 |
-- User online |
|
203 |
if resource and destuser.sessions[resource] then |
|
204 |
stanza.to:send(stanza); |
|
205 |
else |
|
206 |
--User is online, but specified resource isn't (or no resource specified) |
|
207 |
local best_session; |
|
208 |
for resource, session in pairs(destuser.sessions) do |
|
209 |
if not best_session then best_session = session; |
|
210 |
elseif session.priority >= best_session.priority and session.priority >= 0 then |
|
211 |
best_session = session; |
|
212 |
end |
|
213 |
end |
|
214 |
if not best_session then |
|
215 |
offlinemessage.new(node, host, stanza); |
|
216 |
else |
|
217 |
print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); |
|
218 |
resource = best_session.resource; |
|
219 |
end |
|
220 |
end |
|
221 |
if destuser.sessions[resource] == session then |
|
222 |
log("warn", "core", "Attempt to send stanza to self, dropping..."); |
|
223 |
else |
|
224 |
print("...sending...", tostring(stanza)); |
|
225 |
--destuser.sessions[resource].conn.write(tostring(data)); |
|
226 |
print(" to conn ", destuser.sessions[resource].conn); |
|
227 |
destuser.sessions[resource].conn.write(tostring(stanza)); |
|
228 |
print("...sent") |
|
229 |
end |
|
230 |
elseif stanza.name == "message" then |
|
231 |
print(" ...will be stored offline"); |
|
232 |
offlinemessage.new(node, host, stanza); |
|
233 |
elseif stanza.name == "iq" then |
|
234 |
print(" ...is an iq"); |
|
235 |
stanza.from:send(st.reply(stanza) |
|
236 |
:tag("error", { type = "cancel" }) |
|
237 |
:tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); |
|
238 |
end |
|
239 |
end |
|
240 |
|
|
241 |
-- Broadcast a presence stanza to all of a user's contacts |
|
242 |
function handle_stanza_presence_broadcast(session, stanza) |
|
243 |
if session.roster then |
|
244 |
local initial_presence = not session.last_presence; |
|
245 |
session.last_presence = stanza; |
|
246 |
|
|
247 |
-- Broadcast presence and probes |
|
248 |
local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type }); |
|
249 |
|
|
250 |
for child in stanza:childtags() do |
|
251 |
broadcast:add_child(child); |
|
252 |
end |
|
253 |
for contact_jid in pairs(session.roster) do |
|
254 |
broadcast.attr.to = contact_jid; |
|
255 |
send_to(contact_jid, broadcast); |
|
256 |
if initial_presence then |
|
257 |
local node, host = jid.split(contact_jid); |
|
258 |
if hosts[host] and hosts[host].type == "local" then |
|
259 |
local contact = hosts[host].sessions[node] |
|
260 |
if contact then |
|
261 |
local pres = st.presence { to = session.full_jid }; |
|
262 |
for resource, contact_session in pairs(contact.sessions) do |
|
263 |
if contact_session.last_presence then |
|
264 |
pres.tags = contact_session.last_presence.tags; |
|
265 |
pres.attr.from = contact_session.full_jid; |
|
266 |
send(pres); |
|
267 |
end |
|
268 |
end |
|
269 |
end |
|
270 |
--FIXME: Do we send unavailable if they are offline? |
|
271 |
else |
|
272 |
probe.attr.to = contact; |
|
273 |
send_to(contact, probe); |
|
274 |
end |
|
275 |
end |
|
276 |
end |
|
277 |
|
|
278 |
-- Probe for our contacts' presence |
|
279 |
end |
|
280 |
end |
|
281 |
|
|
282 |
-- Broadcast presence probes to all of a user's contacts |
|
283 |
function handle_stanza_presence_probe_broadcast(session, stanza) |
|
284 |
end |
|
285 |
|
|
286 |
-- |
|
287 |
function handle_stanza_to_server(stanza) |
|
288 |
end |
|
289 |
|
|
290 |
function handle_stanza_iq_no_to(session, stanza) |
|
291 |
end |
|
292 |
]] |