core/stanza_router.lua

changeset 177
606c433955e7
parent 176
e5cd2a03891d
child 178
8315cf03f304
equal deleted inserted replaced
176:e5cd2a03891d 177:606c433955e7
58 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) 58 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
59 end 59 end
60 60
61 if not to then 61 if not to then
62 core_handle_stanza(origin, stanza); 62 core_handle_stanza(origin, stanza);
63 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
64 local node, host = jid_split(stanza.attr.to);
65 local to_bare = node and (node.."@"..host) or host; -- bare JID
66 local from_node, from_host = jid_split(stanza.attr.from);
67 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
68 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
63 elseif hosts[to] and hosts[to].type == "local" then 69 elseif hosts[to] and hosts[to].type == "local" then
64 core_handle_stanza(origin, stanza); 70 core_handle_stanza(origin, stanza);
65 elseif stanza.name == "iq" and not select(3, jid_split(to)) then 71 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
66 core_handle_stanza(origin, stanza); 72 core_handle_stanza(origin, stanza);
67 elseif origin.type == "c2s" or origin.type == "s2sin" then 73 elseif origin.type == "c2s" or origin.type == "s2sin" then
194 end 200 end
195 end 201 end
196 return count; 202 return count;
197 end 203 end
198 204
199 function handle_outbound_presence_subscriptions(origin, stanza, from_bare, to_bare) 205 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
200 local node, host = jid_split(to_bare); 206 local node, host = jid_split(from_bare);
207 local st_from, st_to = stanza.attr.from, stanza.attr.to;
208 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
201 if stanza.attr.type == "subscribe" then 209 if stanza.attr.type == "subscribe" then
210 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
202 -- 1. route stanza 211 -- 1. route stanza
203 -- 2. roster push (subscription = none, ask = subscribe) 212 -- 2. roster push (subscription = none, ask = subscribe)
204 if rostermanager.set_contact_pending_out(node, host, from_bare) then 213 if rostermanager.set_contact_pending_out(node, host, to_bare) then
205 rostermanager.roster_push(node, host, from_bare); 214 rostermanager.roster_push(node, host, to_bare);
206 end -- else file error 215 end -- else file error
207 core_route_stanza(origin, st.presence({from=from_bare, to=to_bare, type="subscribe"})); 216 core_route_stanza(origin, stanza);
208 elseif stanza.attr.type == "unsubscribe" then 217 elseif stanza.attr.type == "unsubscribe" then
218 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
209 -- 1. route stanza 219 -- 1. route stanza
210 -- 2. roster push (subscription = none or from) 220 -- 2. roster push (subscription = none or from)
211 if rostermanager.unsubscribe(node, host, from_bare) then 221 if rostermanager.unsubscribe(node, host, to_bare) then
212 rostermanager.roster_push(node, host, from_bare); -- FIXME do roster push when roster has in fact not changed? 222 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
213 end -- else file error 223 end -- else file error
214 core_route_stanza(origin, st.presence({from=from_bare, to=to_bare, type="unsubscribe"})); 224 core_route_stanza(origin, stanza);
215 elseif stanza.attr.type == "subscribed" then 225 elseif stanza.attr.type == "subscribed" then
226 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
216 -- 1. route stanza 227 -- 1. route stanza
217 -- 2. roster_push () 228 -- 2. roster_push ()
218 -- 3. send_presence_of_available_resources 229 -- 3. send_presence_of_available_resources
219 if rostermanager.subscribed(node, host, from_bare) then 230 if rostermanager.subscribed(node, host, to_bare) then
220 rostermanager.roster_push(node, host, from_bare); 231 rostermanager.roster_push(node, host, to_bare);
221 core_route_stanza(origin, st.presence({from=from_bare, to=to_bare, type="subscribed"})); 232 core_route_stanza(origin, stanza);
222 send_presence_of_available_resources(user, host, from_bare, origin); 233 send_presence_of_available_resources(node, host, to_bare, origin);
223 end 234 end
224 elseif stanza.attr.type == "unsubscribed" then 235 elseif stanza.attr.type == "unsubscribed" then
236 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
225 -- 1. route stanza 237 -- 1. route stanza
226 -- 2. roster push (subscription = none or to) 238 -- 2. roster push (subscription = none or to)
227 if rostermanager.unsubscribed(node, host, from_bare) then 239 if rostermanager.unsubscribed(node, host, to_bare) then
228 rostermanager.roster_push(node, host, from_bare); 240 rostermanager.roster_push(node, host, to_bare);
229 core_route_stanza(origin, st.presence({from=from_bare, to=to_bare, type="unsubscribed"})); 241 core_route_stanza(origin, stanza);
230 end 242 end
231 end 243 end
244 stanza.attr.from, stanza.attr.to = st_from, st_to;
232 end 245 end
233 246
234 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare) 247 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
235 local node, host = jid_split(to_bare); 248 local node, host = jid_split(to_bare);
249 local st_from, st_to = stanza.attr.from, stanza.attr.to;
250 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
236 if stanza.attr.type == "probe" then 251 if stanza.attr.type == "probe" then
237 if rostermanager.is_contact_subscribed(node, host, from_bare) then 252 if rostermanager.is_contact_subscribed(node, host, from_bare) then
238 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then 253 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
239 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) 254 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
240 end 255 end
241 else 256 else
242 send(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); 257 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
243 end 258 end
244 elseif stanza.attr.type == "subscribe" then 259 elseif stanza.attr.type == "subscribe" then
260 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
245 if rostermanager.is_contact_subscribed(node, host, from_bare) then 261 if rostermanager.is_contact_subscribed(node, host, from_bare) then
246 send(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed 262 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
247 else 263 else
248 if not rostermanager.is_contact_pending(node, host, from_bare) then 264 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
249 if rostermanager.set_contact_pending(node, host, from_bare) then 265 if rostermanager.set_contact_pending_in(node, host, from_bare) then
250 sessionmanager.send_to_available_resources(node, host, st.presence({from=from_bare, type="subscribe"})); 266 sessionmanager.send_to_available_resources(node, host, stanza);
251 end -- TODO else return error, unable to save 267 end -- TODO else return error, unable to save
252 end 268 end
253 end 269 end
254 elseif stanza.attr.type == "unsubscribe" then 270 elseif stanza.attr.type == "unsubscribe" then
271 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
255 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then 272 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
256 rostermanager.roster_push(node, host, from_bare); 273 rostermanager.roster_push(node, host, from_bare);
257 end 274 end
258 elseif stanza.attr.type == "subscribed" then 275 elseif stanza.attr.type == "subscribed" then
276 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
259 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then 277 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
260 rostermanager.roster_push(node, host, from_bare); 278 rostermanager.roster_push(node, host, from_bare);
261 send_presence_of_available_resources(node, host, from_bare, origin);
262 end 279 end
263 elseif stanza.attr.type == "unsubscribed" then 280 elseif stanza.attr.type == "unsubscribed" then
281 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
264 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then 282 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
265 rostermanager.roster_push(node, host, from_bare); 283 rostermanager.roster_push(node, host, from_bare);
266 end 284 end
267 end -- discard any other type 285 end -- discard any other type
286 stanza.attr.from, stanza.attr.to = st_from, st_to;
268 end 287 end
269 288
270 function core_route_stanza(origin, stanza) 289 function core_route_stanza(origin, stanza)
271 -- Hooks 290 -- Hooks
272 --- ...later 291 --- ...later

mercurial