- if origin.type == "c2s" or origin.type == "c2s_unauthed" then
- local session = origin;
-
- if stanza.name == "presence" and origin.roster then
- if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
- for jid in pairs(origin.roster) do -- broadcast to all interested contacts
- local subscription = origin.roster[jid].subscription;
- if subscription == "both" or subscription == "from" then
- stanza.attr.to = jid;
- core_route_stanza(origin, stanza);
- end
- end
- local node, host = jid_split(stanza.attr.from);
- for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
- if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
- stanza.attr.to = res.full_jid;
- core_route_stanza(origin, stanza);
- end
- end
- if not origin.presence then -- presence probes on initial presence
- local probe = st.presence({from = origin.full_jid, type = "probe"});
- for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
- local subscription = origin.roster[jid].subscription;
- if subscription == "both" or subscription == "to" then
- probe.attr.to = jid;
- core_route_stanza(origin, probe);
- end
- end
- for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all resources
- if res ~= origin and stanza.attr.type ~= "unavailable" and res.presence then -- FIXME does unavailable qualify as initial presence?
- res.presence.attr.to = origin.full_jid;
- core_route_stanza(res, res.presence);
- res.presence.attr.to = nil;
- end
- end
- -- TODO resend subscription requests
- end
- origin.presence = stanza;
- stanza.attr.to = nil; -- reset it
- else
- -- TODO error, bad type
- end
- else
- log("debug", "Routing stanza to local");
- handle_stanza(session, stanza);
- end
- elseif origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
- if stanza.attr.xmlns == "jabber:server:dialback" then
- if stanza.name == "verify" then
- -- We are being asked to verify the key, to ensure it was generated by us
- log("debug", "verifying dialback key...");
- local attr = stanza.attr;
- print(tostring(attr.to), tostring(attr.from))
- print(tostring(origin.to_host), tostring(origin.from_host))
- -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
- --if attr.from ~= origin.to_host then error("invalid-from"); end
- local type = "invalid";
- if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
- type = "valid"
- end
- origin.send(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
- elseif stanza.name == "result" and origin.type == "s2sin_unauthed" then
- -- he wants to be identified through dialback
- -- We need to check the key with the Authoritative server
- local attr = stanza.attr;
- origin.from_host = attr.from;
- origin.to_host = attr.to;
- origin.dialback_key = stanza[1];
- log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
- send_s2s(attr.to, attr.from, format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", attr.to, attr.from, origin.streamid, stanza[1]));
- hosts[attr.from].dialback_verifying = origin;
- end
- end
- elseif origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
- if stanza.attr.xmlns == "jabber:server:dialback" then
- if stanza.name == "result" then
- if stanza.attr.type == "valid" then
- s2s_make_authenticated(origin);