41 |
41 |
42 local jid_split = require "util.jid".split; |
42 local jid_split = require "util.jid".split; |
43 local jid_prepped_split = require "util.jid".prepped_split; |
43 local jid_prepped_split = require "util.jid".prepped_split; |
44 local print = print; |
44 local print = print; |
45 local fire_event = require "core.eventmanager2".fire_event; |
45 local fire_event = require "core.eventmanager2".fire_event; |
|
46 |
|
47 local select_best_resources; |
46 |
48 |
47 function core_process_stanza(origin, stanza) |
49 function core_process_stanza(origin, stanza) |
48 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag()) |
50 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag()) |
49 |
51 |
50 -- Currently we guarantee every stanza to have an xmlns, should we keep this rule? |
52 -- Currently we guarantee every stanza to have an xmlns, should we keep this rule? |
197 end |
199 end |
198 elseif resource and stanza.attr.type == 'groupchat' then |
200 elseif resource and stanza.attr.type == 'groupchat' then |
199 -- Groupchat message sent to offline resource |
201 -- Groupchat message sent to offline resource |
200 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
202 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
201 else |
203 else |
202 local priority = 0; |
|
203 local recipients = {}; |
|
204 for _, session in pairs(user.sessions) do -- find resource with greatest priority |
|
205 if session.presence then |
|
206 local p = session.priority; |
|
207 if p > priority then |
|
208 priority = p; |
|
209 recipients = {session}; |
|
210 elseif p == priority then |
|
211 t_insert(recipients, session); |
|
212 end |
|
213 end |
|
214 end |
|
215 local count = 0; |
204 local count = 0; |
216 for _, session in ipairs(recipients) do |
205 for _, session in ipairs(select_best_resources(user)) do |
217 session.send(stanza); |
206 session.send(stanza); |
218 count = count + 1; |
207 count = count + 1; |
219 end |
208 end |
220 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then |
209 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then |
221 offlinemanager.store(node, host, stanza); |
210 offlinemanager.store(node, host, stanza); |
278 else |
267 else |
279 log("warn", "received stanza from unhandled connection type: %s", origin.type); |
268 log("warn", "received stanza from unhandled connection type: %s", origin.type); |
280 end |
269 end |
281 stanza.attr.to = to; -- reset |
270 stanza.attr.to = to; -- reset |
282 end |
271 end |
|
272 |
|
273 function select_best_resources(user) |
|
274 local priority = 0; |
|
275 local recipients = {}; |
|
276 for _, session in pairs(user.sessions) do -- find resource with greatest priority |
|
277 if session.presence then |
|
278 local p = session.priority; |
|
279 if p > priority then |
|
280 priority = p; |
|
281 recipients = {session}; |
|
282 elseif p == priority then |
|
283 t_insert(recipients, session); |
|
284 end |
|
285 end |
|
286 end |
|
287 return recipients; |
|
288 end |