s2smanager: Include a stub thread on destroyed sessions (thanks Link Mauve)
[prosody.git] / core / s2smanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local hosts = prosody.hosts;
12 local tostring, pairs, setmetatable
13     = tostring, pairs, setmetatable;
14
15 local logger_init = require "util.logger".init;
16
17 local log = logger_init("s2smanager");
18
19 local prosody = _G.prosody;
20 incoming_s2s = {};
21 prosody.incoming_s2s = incoming_s2s;
22 local incoming_s2s = incoming_s2s;
23 local fire_event = prosody.events.fire_event;
24
25 local _ENV = nil;
26
27 local function new_incoming(conn)
28         local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
29         session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$"));
30         incoming_s2s[session] = true;
31         return session;
32 end
33
34 local function new_outgoing(from_host, to_host)
35         local host_session = { to_host = to_host, from_host = from_host, host = from_host,
36                                notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
37         hosts[from_host].s2sout[to_host] = host_session;
38         local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
39         host_session.log = logger_init(conn_name);
40         return host_session;
41 end
42
43 local resting_session = { -- Resting, not dead
44                 destroyed = true;
45                 type = "s2s_destroyed";
46                 open_stream = function (session)
47                         session.log("debug", "Attempt to open stream on resting session");
48                 end;
49                 close = function (session)
50                         session.log("debug", "Attempt to close already-closed session");
51                 end;
52                 filter = function (type, data) return data; end; --luacheck: ignore 212/type
53         }; resting_session.__index = resting_session;
54
55 local function retire_session(session, reason)
56         local log = session.log or log; --luacheck: ignore 431/log
57         for k in pairs(session) do
58                 if k ~= "log" and k ~= "id" and k ~= "conn" then
59                         session[k] = nil;
60                 end
61         end
62
63         session.destruction_reason = reason;
64
65         function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
66         function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
67         session.thread = { run = function (_, data) return session.data(data) end };
68         session.sends2s = session.send;
69         return setmetatable(session, resting_session);
70 end
71
72 local function destroy_session(session, reason)
73         if session.destroyed then return; end
74         (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
75
76         if session.direction == "outgoing" then
77                 hosts[session.from_host].s2sout[session.to_host] = nil;
78                 session:bounce_sendq(reason);
79         elseif session.direction == "incoming" then
80                 incoming_s2s[session] = nil;
81         end
82
83         local event_data = { session = session, reason = reason };
84         if session.type == "s2sout" then
85                 fire_event("s2sout-destroyed", event_data);
86                 if hosts[session.from_host] then
87                         hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
88                 end
89         elseif session.type == "s2sin" then
90                 fire_event("s2sin-destroyed", event_data);
91                 if hosts[session.to_host] then
92                         hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
93                 end
94         end
95
96         retire_session(session, reason); -- Clean session until it is GC'd
97         return true;
98 end
99
100 return {
101         incoming_s2s = incoming_s2s;
102         new_incoming = new_incoming;
103         new_outgoing = new_outgoing;
104         retire_session = retire_session;
105         destroy_session = destroy_session;
106 };