mod_storage_none: A null-like storage provider that returns all stores as empty,...
[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, getmetatable, newproxy, setmetatable
13     = tostring, pairs, getmetatable, newproxy, 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 module "s2smanager"
26
27 local open_sessions = 0;
28
29 function new_incoming(conn)
30         local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
31         if true then
32                 session.trace = newproxy(true);
33                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
34         end
35         open_sessions = open_sessions + 1;
36         session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$"));
37         incoming_s2s[session] = true;
38         return session;
39 end
40
41 function new_outgoing(from_host, to_host)
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);
47         return host_session;
48 end
49
50 local resting_session = { -- Resting, not dead
51                 destroyed = true;
52                 type = "s2s_destroyed";
53                 open_stream = function (session)
54                         session.log("debug", "Attempt to open stream on resting session");
55                 end;
56                 close = function (session)
57                         session.log("debug", "Attempt to close already-closed session");
58                 end;
59                 filter = function (type, data) return data; end;
60         }; resting_session.__index = resting_session;
61
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
66                         session[k] = nil;
67                 end
68         end
69
70         session.destruction_reason = reason;
71
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);
75 end
76
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 ""));
80         
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;
86         end
87         
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);
93                 end
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);
98                 end
99         end
100         
101         retire_session(session, reason); -- Clean session until it is GC'd
102         return true;
103 end
104
105 return _M;