local dialback_secret = "This is very secret!!! Ha!";
+local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.controlezvous.ca" };
+
module "s2smanager"
function connect_host(from_host, to_host)
function send_to_host(from_host, to_host, data)
if hosts[to_host] then
-- Write to connection
- hosts[to_host].send(data);
+ hosts[to_host].sends2s(data);
log("debug", "stanza sent over s2s");
else
log("debug", "opening a new outgoing connection for this stanza");
end
open_sessions = open_sessions + 1;
local w = conn.write;
- session.send = function (t) w(tostring(t)); end
+ session.sends2s = function (t) w(tostring(t)); end
return session;
end
local conn, handler = socket.tcp()
--FIXME: Below parameters (ports/ip) are incorrect (use SRV)
+ to_host = srvmap[to_host] or to_host;
conn:connect(to_host, 5269);
conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx );
host_session.conn = conn;
end
local w = conn.write;
- host_session.send = function (t) w(tostring(t)); end
+ host_session.sends2s = function (t) w(tostring(t)); end
conn.write(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host));
function streamopened(session, attr)
session.log("debug", "s2s stream opened");
- local send = session.send;
+ local send = session.sends2s;
session.version = tonumber(attr.version) or 0;
if session.version >= 1.0 and not (attr.to and attr.from) then
print(session, session.from_host, "incoming s2s stream opened");
send("<?xml version='1.0'?>");
send(format("<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s'>", session.streamid, session.to_host));
- if session.from_host then
- -- Need to perform dialback to check identity
- print("to: "..tostring(attr.to).." from: "..tostring(attr.from));
- print("Need to do dialback here you know!!");
- end
elseif session.direction == "outgoing" then
-- If we are just using the connection for verifying dialback keys, we won't try and auth it
if not session.dialback_verifying then
if not attr.id then error("stream response did not give us a streamid!!!"); end
session.streamid = attr.id;
session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
- session.send(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
+ session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
session.log("info", "sent dialback key on outgoing s2s stream");
else
mark_connected(session);
end
function mark_connected(session)
- local sendq, send = session.sendq, session.send;
+ local sendq, send = session.sendq, session.sends2s;
+
+ local from, to = session.from_host, session.to_host;
+
+ session.log("debug", session.direction.." s2s connection "..session.from_host.."->"..session.to_host.." is now complete");
+
+ local send_to_host = send_to_host;
+ function session.send(data) send_to_host(from, to, data); end
+
if sendq then
- session.log("debug", "sending queued stanzas across new connection");
- for _, data in ipairs(sendq) do
- session.log("debug", "sending: %s", tostring(data));
+ session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host);
+ for i, data in ipairs(sendq) do
send(data);
+ sendq[i] = nil;
+ end
+ session.sendq = nil;
+ end
+end
+
+function destroy_session(session)
+ (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host));
+ if session.direction == "outgoing" then
+ hosts[session.to_host] = nil;
+ end
+ session.conn = nil;
+ session.disconnect = nil;
+ for k in pairs(session) do
+ if k ~= "trace" then
+ session[k] = nil;
end
end
end