2009-06-20
stanza_router: Skip prepping 'from' on c2s origins - #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 node, host, resource = jid_prepped_split(to);
70 if not host then
71 log("warn", "Received stanza with invalid destination JID: %s", to);
72 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
73 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
74 end
75 return;
76 end
77 to_bare = node and (node.."@"..host) or host; -- bare JID
78 if resource then to = to_bare.."/"..resource; else to = to_bare; end
79 stanza.attr.to = to;
80 end
81 if from and not origin.full_jid then
82 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
83 from_node, from_host, from_resource = jid_prepped_split(from);
84 if not from_host then
85 log("warn", "Received stanza with invalid source JID: %s", from);
86 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
87 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
88 end
89 return;
90 end
91 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
92 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
93 stanza.attr.from = from;
94 end
96 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
97 log("warn", "stanza recieved for a non-local server");
98 return; -- FIXME what should we do here?
99 end]] -- FIXME
101 if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == "jabber:client" then
102 if origin.type == "s2sin" and not origin.dummy then
103 local host_status = origin.hosts[from_host];
104 if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
105 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
106 return; -- FIXME what should we do here? does this work with subdomains?
107 end
108 end
109 core_post_stanza(origin, stanza);
110 else
111 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
112 if h then
113 local event;
114 if stanza.attr.xmlns == "jabber:client" then
115 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
116 event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
117 else
118 event = "stanza/"..stanza.name;
119 end
120 else
121 event = "stanza/"..stanza.attr.xmlns..":"..stanza.name;
122 end
123 if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
124 end
125 if host and not hosts[host] then host = nil; end -- workaround for a Pidgin bug which sets 'to' to the SRV result
126 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
127 end
128 end
130 function core_post_stanza(origin, stanza)
131 local to = stanza.attr.to;
132 local node, host, resource = jid_split(to);
133 local to_bare = node and (node.."@"..host) or host; -- bare JID
135 local to_type;
136 if node then
137 if resource then
138 to_type = '/full';
139 else
140 to_type = '/bare';
141 if node == origin.username and host == origin.host then
142 stanza.attr.to = nil;
143 end
144 end
145 else
146 if host then
147 to_type = '/host';
148 else
149 to_type = '/bare';
150 end
151 end
153 local event_data = {origin=origin, stanza=stanza};
154 if origin.full_jid then -- c2s connection
155 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
156 end
157 local h = hosts[to_bare] or hosts[host or origin.host];
158 if h then
159 if h.type == "component" then
160 component_handle_stanza(origin, stanza);
161 return;
162 else
163 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
164 end
165 end
167 if host and fire_event(host.."/"..stanza.name, event_data) then
168 -- event handled
169 elseif stanza.name == "presence" and origin.host and fire_event(origin.host.."/"..stanza.name, event_data) then
170 -- event handled
171 elseif not to then
172 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
173 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
174 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
175 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
176 component_handle_stanza(origin, stanza);
177 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
178 component_handle_stanza(origin, stanza);
179 elseif hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
180 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
181 else
182 core_route_stanza(origin, stanza);
183 end
184 end
186 function core_route_stanza(origin, stanza)
187 -- Hooks
188 --- ...later
190 -- Deliver
191 local to = stanza.attr.to;
192 local node, host, resource = jid_split(to);
193 local to_bare = node and (node.."@"..host) or host; -- bare JID
194 local from = stanza.attr.from;
195 local from_node, from_host, from_resource = jid_split(from);
196 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
198 -- Auto-detect origin if not specified
199 origin = origin or hosts[from_host];
200 if not origin then return false; end
202 if hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
203 return component_handle_stanza(origin, stanza);
204 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
205 return component_handle_stanza(origin, stanza);
206 end
208 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error") then resource = nil; end
210 local host_session = hosts[host]
211 if host_session and host_session.type == "local" then
212 -- Local host
213 local user = host_session.sessions[node];
214 if user then
215 local res = user.sessions[resource];
216 if res then -- resource is online...
217 res.send(stanza); -- Yay \o/
218 else
219 -- if we get here, resource was not specified or was unavailable
220 if stanza.name == "presence" then
221 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
222 -- inbound presence subscriptions and probes, already handled, so should never get here
223 elseif not resource then -- sender is available or unavailable or error
224 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
225 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
226 stanza.attr.to = session.full_jid; -- reset at the end of function
227 session.send(stanza);
228 end
229 end
230 end
231 elseif stanza.name == "message" then -- select a resource to recieve message
232 stanza.attr.to = to_bare;
233 if stanza.attr.type == 'headline' then
234 for _, session in pairs(user.sessions) do -- find resource with greatest priority
235 if session.presence and session.priority >= 0 then
236 session.send(stanza);
237 end
238 end
239 elseif stanza.attr.type == 'groupchat' then
240 -- Groupchat message sent to offline resource
241 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
242 else
243 local count = 0;
244 for _, session in ipairs(select_best_resources(user)) do
245 session.send(stanza);
246 count = count + 1;
247 end
248 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
249 offlinemanager.store(node, host, stanza);
250 -- TODO deal with storage errors
251 end
252 end
253 elseif stanza.attr.type == "get" or stanza.attr.type == "set" then
254 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
255 end
256 end
257 else
258 -- user not online
259 if user_exists(node, host) then
260 if stanza.name == "presence" then
261 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
262 -- inbound presence subscriptions and probes, already handled, so should never get here
263 else
264 -- TODO send unavailable presence or unsubscribed
265 end
266 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
267 stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
268 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
269 offlinemanager.store(node, host, stanza);
270 -- FIXME don't store messages with only chat state notifications
271 elseif stanza.attr.type == "groupchat" then
272 local reply = st.error_reply(stanza, "cancel", "service-unavailable");
273 reply.attr.from = to;
274 origin.send(reply);
275 end
276 -- TODO allow configuration of offline storage
277 -- TODO send error if not storing offline
278 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
279 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
280 end
281 else -- user does not exist
282 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
283 if stanza.name == "presence" then
284 local t = stanza.attr.type;
285 if t == "subscribe" or t == "probe" then
286 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
287 end
288 -- else ignore
289 elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
290 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
291 end
292 end
293 end
294 elseif origin.type == "c2s" then
295 -- Remote host
296 local xmlns = stanza.attr.xmlns;
297 --stanza.attr.xmlns = "jabber:server";
298 stanza.attr.xmlns = nil;
299 log("debug", "sending s2s stanza: %s", tostring(stanza));
300 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
301 stanza.attr.xmlns = xmlns; -- reset
302 elseif origin.type == "component" or origin.type == "local" then
303 -- Route via s2s for components and modules
304 log("debug", "Routing outgoing stanza for %s to %s", from_host, host);
305 send_s2s(from_host, host, stanza);
306 else
307 log("warn", "received stanza from unhandled connection type: %s", origin.type);
308 end
309 stanza.attr.to = to; -- reset
310 end
312 function select_best_resources(user)
313 local priority = 0;
314 local recipients = {};
315 for _, session in pairs(user.sessions) do -- find resource with greatest priority
316 if session.presence then
317 local p = session.priority;
318 if p > priority then
319 priority = p;
320 recipients = {session};
321 elseif p == priority then
322 t_insert(recipients, session);
323 end
324 end
325 end
326 return recipients;
327 end