11 local send_s2s = require "core.s2smanager".send_to_host; |
11 local send_s2s = require "core.s2smanager".send_to_host; |
12 local user_exists = require "core.usermanager".user_exists; |
12 local user_exists = require "core.usermanager".user_exists; |
13 |
13 |
14 local rostermanager = require "core.rostermanager"; |
14 local rostermanager = require "core.rostermanager"; |
15 local sessionmanager = require "core.sessionmanager"; |
15 local sessionmanager = require "core.sessionmanager"; |
|
16 local offlinemanager = require "core.offlinemanager"; |
16 |
17 |
17 local s2s_verify_dialback = require "core.s2smanager".verify_dialback; |
18 local s2s_verify_dialback = require "core.s2smanager".verify_dialback; |
18 local s2s_make_authenticated = require "core.s2smanager".make_authenticated; |
19 local s2s_make_authenticated = require "core.s2smanager".make_authenticated; |
19 |
20 |
20 local modules_handle_stanza = require "core.modulemanager".handle_stanza; |
21 local modules_handle_stanza = require "core.modulemanager".handle_stanza; |
29 |
30 |
30 local jid_split = require "util.jid".split; |
31 local jid_split = require "util.jid".split; |
31 local print = print; |
32 local print = print; |
32 |
33 |
33 function core_process_stanza(origin, stanza) |
34 function core_process_stanza(origin, stanza) |
34 log("debug", "Received["..origin.type.."]: "..tostring(st.reply(st.reply(stanza)))) |
35 log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag()) |
35 |
36 |
36 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling. |
37 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling. |
37 -- TODO verify validity of stanza (as well as JID validity) |
38 -- TODO verify validity of stanza (as well as JID validity) |
38 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
39 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
39 if stanza.attr.type == "set" or stanza.attr.type == "get" then |
40 if stanza.attr.type == "set" or stanza.attr.type == "get" then |
147 if item.ask then |
148 if item.ask then |
148 request.attr.to = jid; |
149 request.attr.to = jid; |
149 core_route_stanza(origin, request); |
150 core_route_stanza(origin, request); |
150 end |
151 end |
151 end |
152 end |
|
153 for _, msg in ipairs(offlinemanager.load(node, host) or {}) do |
|
154 origin.send(msg); -- FIXME do we need to modify to/from in any way? |
|
155 end |
|
156 offlinemanager.deleteAll(node, host); |
152 end |
157 end |
153 origin.priority = 0; |
158 origin.priority = 0; |
154 if stanza.attr.type == "unavailable" then |
159 if stanza.attr.type == "unavailable" then |
155 origin.presence = nil; |
160 origin.presence = nil; |
156 else |
161 else |
166 end |
171 end |
167 end |
172 end |
168 end |
173 end |
169 stanza.attr.to = nil; -- reset it |
174 stanza.attr.to = nil; -- reset it |
170 else |
175 else |
171 -- TODO error, bad type |
176 log("warn", "Unhandled c2s presence: %s", tostring(stanza)); |
|
177 if stanza.attr.type ~= "error" then |
|
178 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? |
|
179 end |
|
180 end |
|
181 else |
|
182 log("warn", "Unhandled c2s stanza: %s", tostring(stanza)); |
|
183 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
|
184 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? |
172 end |
185 end |
173 end -- TODO handle other stanzas |
186 end -- TODO handle other stanzas |
174 else |
187 else |
175 log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error |
188 log("warn", "Unhandled origin: %s", origin.type); |
|
189 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
|
190 -- s2s stanzas can get here |
|
191 (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? |
|
192 end |
176 end |
193 end |
177 end |
194 end |
178 |
195 |
179 function send_presence_of_available_resources(user, host, jid, recipient_session) |
196 function send_presence_of_available_resources(user, host, jid, recipient_session) |
180 local h = hosts[host]; |
197 local h = hosts[host]; |
326 recipients = {session}; |
343 recipients = {session}; |
327 elseif p == priority then |
344 elseif p == priority then |
328 t_insert(recipients, session); |
345 t_insert(recipients, session); |
329 end |
346 end |
330 end |
347 end |
|
348 local count = 0; |
331 for _, session in pairs(recipients) do |
349 for _, session in pairs(recipients) do |
332 session.send(stanza); |
350 session.send(stanza); |
|
351 count = count + 1; |
|
352 end |
|
353 if count == 0 then |
|
354 offlinemanager.store(node, host, stanza); |
|
355 -- TODO deal with storage errors |
333 end |
356 end |
334 else |
357 else |
335 -- TODO send IQ error |
358 -- TODO send IQ error |
336 end |
359 end |
337 else |
360 else |
347 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); |
370 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); |
348 else |
371 else |
349 -- TODO send unavailable presence or unsubscribed |
372 -- TODO send unavailable presence or unsubscribed |
350 end |
373 end |
351 elseif stanza.name == "message" then |
374 elseif stanza.name == "message" then |
352 -- TODO send message error, or store offline messages |
375 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then |
|
376 offlinemanager.store(node, host, stanza); |
|
377 -- FIXME don't store messages with only chat state notifications |
|
378 end |
|
379 -- TODO allow configuration of offline storage |
|
380 -- TODO send error if not storing offline |
353 elseif stanza.name == "iq" then |
381 elseif stanza.name == "iq" then |
354 -- TODO send IQ error |
382 -- TODO send IQ error |
355 end |
383 end |
356 else -- user does not exist |
384 else -- user does not exist |
357 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? |
385 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? |