Changeset

1212:3be23cf5a659

stanza_router: Break off resource selection for messages into a standalone function
author Matthew Wild <mwild1@gmail.com>
date Thu, 28 May 2009 20:39:32 +0100
parents 1211:d60e68855176
children 1213:de66fa750daf
files core/stanza_router.lua
diffstat 1 files changed, 20 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/core/stanza_router.lua	Thu May 28 02:59:47 2009 +0100
+++ b/core/stanza_router.lua	Thu May 28 20:39:32 2009 +0100
@@ -44,6 +44,8 @@
 local print = print;
 local fire_event = require "core.eventmanager2".fire_event;
 
+local select_best_resources;
+
 function core_process_stanza(origin, stanza)
 	(origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
 
@@ -199,21 +201,8 @@
 						-- Groupchat message sent to offline resource
 						origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
 					else
-						local priority = 0;
-						local recipients = {};
-						for _, session in pairs(user.sessions) do -- find resource with greatest priority
-							if session.presence then
-								local p = session.priority;
-								if p > priority then
-									priority = p;
-									recipients = {session};
-								elseif p == priority then
-									t_insert(recipients, session);
-								end
-							end
-						end
 						local count = 0;
-						for _, session in ipairs(recipients) do
+						for _, session in ipairs(select_best_resources(user)) do
 							session.send(stanza);
 							count = count + 1;
 						end
@@ -280,3 +269,20 @@
 	end
 	stanza.attr.to = to; -- reset
 end
+
+function select_best_resources(user)
+	local priority = 0;
+	local recipients = {};
+	for _, session in pairs(user.sessions) do -- find resource with greatest priority
+		if session.presence then
+			local p = session.priority;
+			if p > priority then
+				priority = p;
+				recipients = {session};
+			elseif p == priority then
+				t_insert(recipients, session);
+			end
+		end
+	end
+	return recipients;
+end