core/stanza_router.lua

2009-06-20

author
Waqas Hussain <waqas20@gmail.com>
date
Sat Jun 20 18:18:38 2009 +0500
changeset 1370
3a467e6885f0
parent 1369
633e032a3b4b
child 1406
83c6fb3d9e73
permissions
-rw-r--r--

stanza_router: Skip prepping 'to' in many common cases - #optimization

     1 -- Prosody IM v0.4
     2 -- Copyright (C) 2008-2009 Matthew Wild
     3 -- Copyright (C) 2008-2009 Waqas Hussain
     4 -- 
     5 -- This project is MIT/X11 licensed. Please see the
     6 -- COPYING file in the source package for more information.
     7 --
    11 local log = require "util.logger".init("stanzarouter")
    13 local hosts = _G.hosts;
    15 local st = require "util.stanza";
    16 local send_s2s = require "core.s2smanager".send_to_host;
    17 local user_exists = require "core.usermanager".user_exists;
    19 local rostermanager = require "core.rostermanager";
    20 local offlinemanager = require "core.offlinemanager";
    22 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
    23 local component_handle_stanza = require "core.componentmanager".handle_stanza;
    25 local tostring = tostring;
    26 local t_insert = table.insert;
    27 local pairs = pairs;
    28 local ipairs = ipairs;
    30 local jid_split = require "util.jid".split;
    31 local jid_prepped_split = require "util.jid".prepped_split;
    32 local fire_event = prosody.events.fire_event;
    34 local select_best_resources;
    36 function core_process_stanza(origin, stanza)
    37 	(origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
    39 	-- Currently we guarantee every stanza to have an xmlns, should we keep this rule?
    40 	if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end
    42 	-- TODO verify validity of stanza (as well as JID validity)
    43 	if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
    44 	if stanza.name == "iq" then
    45 		if (stanza.attr.type == "set" or stanza.attr.type == "get") and #stanza.tags ~= 1 then
    46 			origin.send(st.error_reply(stanza, "modify", "bad-request"));
    47 			return;
    48 		end
    49 	end
    51 	if origin.type == "c2s" then
    52 		if not origin.full_jid
    53 			and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind"
    54 					and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
    55 			-- authenticated client isn't bound and current stanza is not a bind request
    56 			origin.send(st.error_reply(stanza, "auth", "not-authorized")); -- FIXME maybe allow stanzas to account or server
    57 			return;
    58 		end
    60 		-- TODO also, stanzas should be returned to their original state before the function ends
    61 		stanza.attr.from = origin.full_jid;
    62 	end
    63 	local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
    64 	local from = stanza.attr.from;
    65 	local node, host, resource;
    66 	local from_node, from_host, from_resource;
    67 	local to_bare, from_bare;
    68 	if to then
    69 		if full_sessions[to] or bare_sessions[to] or hosts[to] then
    70 			node, host = jid_split(to); -- TODO only the host is needed, optimize
    71 		else
    72 			node, host, resource = jid_prepped_split(to);
    73 			if not host then
    74 				log("warn", "Received stanza with invalid destination JID: %s", to);
    75 				if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
    76 					origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
    77 				end
    78 				return;
    79 			end
    80 			to_bare = node and (node.."@"..host) or host; -- bare JID
    81 			if resource then to = to_bare.."/"..resource; else to = to_bare; end
    82 			stanza.attr.to = to;
    83 		end
    84 	end
    85 	if from and not origin.full_jid then
    86 		-- We only stamp the 'from' on c2s stanzas, so we still need to check validity
    87 		from_node, from_host, from_resource = jid_prepped_split(from);
    88 		if not from_host then
    89 			log("warn", "Received stanza with invalid source JID: %s", from);
    90 			if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
    91 				origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
    92 			end
    93 			return;
    94 		end
    95 		from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
    96 		if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
    97 		stanza.attr.from = from;
    98 	end
   100 	--[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
   101 		log("warn", "stanza recieved for a non-local server");
   102 		return; -- FIXME what should we do here?
   103 	end]] -- FIXME
   105 	if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == "jabber:client" then
   106 		if origin.type == "s2sin" and not origin.dummy then
   107 			local host_status = origin.hosts[from_host];
   108 			if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
   109 				log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
   110 				return; -- FIXME what should we do here? does this work with subdomains?
   111 			end
   112 		end
   113 		core_post_stanza(origin, stanza);
   114 	else
   115 		local h = hosts[stanza.attr.to or origin.host or origin.to_host];
   116 		if h then
   117 			local event;
   118 			if stanza.attr.xmlns == "jabber:client" then
   119 				if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
   120 					event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
   121 				else
   122 					event = "stanza/"..stanza.name;
   123 				end
   124 			else
   125 				event = "stanza/"..stanza.attr.xmlns..":"..stanza.name;
   126 			end
   127 			if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
   128 		end
   129 		if host and not hosts[host] then host = nil; end -- workaround for a Pidgin bug which sets 'to' to the SRV result
   130 		modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
   131 	end
   132 end
   134 function core_post_stanza(origin, stanza)
   135 	local to = stanza.attr.to;
   136 	local node, host, resource = jid_split(to);
   137 	local to_bare = node and (node.."@"..host) or host; -- bare JID
   139 	local to_type;
   140 	if node then
   141 		if resource then
   142 			to_type = '/full';
   143 		else
   144 			to_type = '/bare';
   145 			if node == origin.username and host == origin.host then
   146 				stanza.attr.to = nil;
   147 			end
   148 		end
   149 	else
   150 		if host then
   151 			to_type = '/host';
   152 		else
   153 			to_type = '/bare';
   154 		end
   155 	end
   157 	local event_data = {origin=origin, stanza=stanza};
   158 	if origin.full_jid then -- c2s connection
   159 		if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
   160 	end
   161 	local h = hosts[to_bare] or hosts[host or origin.host];
   162 	if h then
   163 		if h.type == "component" then
   164 			component_handle_stanza(origin, stanza);
   165 			return;
   166 		else
   167 			if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
   168 		end
   169 	end
   171 	if host and fire_event(host.."/"..stanza.name, event_data) then
   172 		-- event handled
   173 	elseif stanza.name == "presence" and origin.host and fire_event(origin.host.."/"..stanza.name, event_data) then
   174 		-- event handled
   175 	elseif not to then
   176 		modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
   177 	elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
   178 		modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
   179 	elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
   180 		component_handle_stanza(origin, stanza);
   181 	elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
   182 		component_handle_stanza(origin, stanza);
   183 	elseif hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
   184 		modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
   185 	else
   186 		core_route_stanza(origin, stanza);
   187 	end
   188 end
   190 function core_route_stanza(origin, stanza)
   191 	-- Hooks
   192 	--- ...later
   194 	-- Deliver
   195 	local to = stanza.attr.to;
   196 	local node, host, resource = jid_split(to);
   197 	local to_bare = node and (node.."@"..host) or host; -- bare JID
   198 	local from = stanza.attr.from;
   199 	local from_node, from_host, from_resource = jid_split(from);
   200 	local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
   202 	-- Auto-detect origin if not specified
   203 	origin = origin or hosts[from_host];
   204 	if not origin then return false; end
   206 	if hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
   207 		return component_handle_stanza(origin, stanza);
   208 	elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
   209 		return component_handle_stanza(origin, stanza);
   210 	end
   212 	if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error") then resource = nil; end
   214 	local host_session = hosts[host]
   215 	if host_session and host_session.type == "local" then
   216 		-- Local host
   217 		local user = host_session.sessions[node];
   218 		if user then
   219 			local res = user.sessions[resource];
   220 			if res then -- resource is online...
   221 				res.send(stanza); -- Yay \o/
   222 			else
   223 				-- if we get here, resource was not specified or was unavailable
   224 				if stanza.name == "presence" then
   225 					if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
   226 						-- inbound presence subscriptions and probes, already handled, so should never get here
   227 					elseif not resource then -- sender is available or unavailable or error
   228 						for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
   229 							if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
   230 								stanza.attr.to = session.full_jid; -- reset at the end of function
   231 								session.send(stanza);
   232 							end
   233 						end
   234 					end
   235 				elseif stanza.name == "message" then -- select a resource to recieve message
   236 					stanza.attr.to = to_bare;
   237 					if stanza.attr.type == 'headline' then
   238 						for _, session in pairs(user.sessions) do -- find resource with greatest priority
   239 							if session.presence and session.priority >= 0 then
   240 								session.send(stanza);
   241 							end
   242 						end
   243 					elseif stanza.attr.type == 'groupchat' then
   244 						-- Groupchat message sent to offline resource
   245 						origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   246 					else
   247 						local count = 0;
   248 						for _, session in ipairs(select_best_resources(user)) do
   249 							session.send(stanza);
   250 							count = count + 1;
   251 						end
   252 						if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
   253 							offlinemanager.store(node, host, stanza);
   254 							-- TODO deal with storage errors
   255 						end
   256 					end
   257 				elseif stanza.attr.type == "get" or stanza.attr.type == "set" then
   258 					origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   259 				end
   260 			end
   261 		else
   262 			-- user not online
   263 			if user_exists(node, host) then
   264 				if stanza.name == "presence" then
   265 					if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
   266 						-- inbound presence subscriptions and probes, already handled, so should never get here
   267 					else
   268 						-- TODO send unavailable presence or unsubscribed
   269 					end
   270 				elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
   271 					stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
   272 					if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
   273 						offlinemanager.store(node, host, stanza);
   274 						-- FIXME don't store messages with only chat state notifications
   275 					elseif stanza.attr.type == "groupchat" then
   276 						local reply = st.error_reply(stanza, "cancel", "service-unavailable");
   277 						reply.attr.from = to;
   278 						origin.send(reply);
   279 					end
   280 					-- TODO allow configuration of offline storage
   281 					-- TODO send error if not storing offline
   282 				elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
   283 					origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   284 				end
   285 			else -- user does not exist
   286 				-- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
   287 				if stanza.name == "presence" then
   288 					local t = stanza.attr.type;
   289 					if t == "subscribe" or t == "probe" then
   290 						origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
   291 					end
   292 					-- else ignore
   293 				elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
   294 					origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   295 				end
   296 			end
   297 		end
   298 	elseif origin.type == "c2s" then
   299 		-- Remote host
   300 		local xmlns = stanza.attr.xmlns;
   301 		--stanza.attr.xmlns = "jabber:server";
   302 		stanza.attr.xmlns = nil;
   303 		log("debug", "sending s2s stanza: %s", tostring(stanza));
   304 		send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
   305 		stanza.attr.xmlns = xmlns; -- reset
   306 	elseif origin.type == "component" or origin.type == "local" then
   307 		-- Route via s2s for components and modules
   308 		log("debug", "Routing outgoing stanza for %s to %s", from_host, host);
   309 		send_s2s(from_host, host, stanza);
   310 	else
   311 		log("warn", "received stanza from unhandled connection type: %s", origin.type);
   312 	end
   313 	stanza.attr.to = to; -- reset
   314 end
   316 function select_best_resources(user)
   317 	local priority = 0;
   318 	local recipients = {};
   319 	for _, session in pairs(user.sessions) do -- find resource with greatest priority
   320 		if session.presence then
   321 			local p = session.priority;
   322 			if p > priority then
   323 				priority = p;
   324 				recipients = {session};
   325 			elseif p == priority then
   326 				t_insert(recipients, session);
   327 			end
   328 		end
   329 	end
   330 	return recipients;
   331 end

mercurial