Diff

core/stanza_router.lua @ 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
parent 1191:7f965bd37d65
child 1228:853d3d76ba94
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