2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local hosts = prosody.hosts;
12 local tostring, pairs, getmetatable, newproxy, setmetatable
13 = tostring, pairs, getmetatable, newproxy, setmetatable;
15 local logger_init = require "util.logger".init;
17 local log = logger_init("s2smanager");
19 local prosody = _G.prosody;
21 prosody.incoming_s2s = incoming_s2s;
22 local incoming_s2s = incoming_s2s;
23 local fire_event = prosody.events.fire_event;
27 local open_sessions = 0;
29 function new_incoming(conn)
30 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
32 session.trace = newproxy(true);
33 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
35 open_sessions = open_sessions + 1;
36 session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$"));
37 incoming_s2s[session] = true;
41 function new_outgoing(from_host, to_host, connect)
42 local host_session = { to_host = to_host, from_host = from_host, host = from_host,
43 notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
44 hosts[from_host].s2sout[to_host] = host_session;
45 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
46 host_session.log = logger_init(conn_name);
50 local resting_session = { -- Resting, not dead
52 type = "s2s_destroyed";
53 open_stream = function (session)
54 session.log("debug", "Attempt to open stream on resting session");
56 close = function (session)
57 session.log("debug", "Attempt to close already-closed session");
59 filter = function (type, data) return data; end;
60 }; resting_session.__index = resting_session;
62 function retire_session(session, reason)
63 local log = session.log or log;
64 for k in pairs(session) do
65 if k ~= "trace" and k ~= "log" and k ~= "id" and k ~= "conn" then
70 session.destruction_reason = reason;
72 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
73 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
74 return setmetatable(session, resting_session);
77 function destroy_session(session, reason)
78 if session.destroyed then return; end
79 (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
81 if session.direction == "outgoing" then
82 hosts[session.from_host].s2sout[session.to_host] = nil;
83 session:bounce_sendq(reason);
84 elseif session.direction == "incoming" then
85 incoming_s2s[session] = nil;
88 local event_data = { session = session, reason = reason };
89 if session.type == "s2sout" then
90 fire_event("s2sout-destroyed", event_data);
91 if hosts[session.from_host] then
92 hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
94 elseif session.type == "s2sin" then
95 fire_event("s2sin-destroyed", event_data);
96 if hosts[session.to_host] then
97 hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
101 retire_session(session, reason); -- Clean session until it is GC'd