comparison mod_s2soutinjection/mod_s2soutinjection.lua @ 1089:4057f176be7b

mod_s2soutinjection: Initial commit, variant of mod_srvinjection using 0.9+ APIs
author Kim Alvefur <zash@zash.se>
date Fri, 28 Jun 2013 03:20:57 +0200
parents
children 864fefec1c07
comparison
equal deleted inserted replaced
1088:6f8e7f65f704 1089:4057f176be7b
1 local st = require"util.stanza";
2 local new_ip = require"util.ip".new_ip;
3 local new_outgoing = require"core.s2smanager".new_outgoing;
4 local bounce_sendq = module:depends"s2s".route_to_new_session.bounce_sendq;
5 local s2sout = module:depends"s2s".route_to_new_session.s2sout;
6
7 local injected = module:get_option("s2s_connect_overrides");
8
9 local function isip(addr)
10 return not not (addr and addr:match("^%d+%.%d+%.%d+%.%d+$") or addr:match("^[%x:]*:[%x:]-:[%x:]*$"));
11 end
12
13 module:hook("route/remote", function(event)
14 local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
15 local inject = injected and injected[to_host];
16 if not inject then return end
17 log("debug", "opening a new outgoing connection for this stanza");
18 local host_session = new_outgoing(from_host, to_host);
19
20 -- Store in buffer
21 host_session.bounce_sendq = bounce_sendq;
22 host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
23 log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name));
24
25 local ip_hosts, srv_hosts = {}, {};
26 host_session.srv_hosts = srv_hosts;
27 host_session.srv_choice = 0;
28
29 if type(inject) == "string" then inject = { inject } end
30
31 for _, item in ipairs(inject) do
32 local host, port = item[1] or item, tonumber(item[2]) or 5269;
33 if isip(host) then
34 ip_hosts[#ip_hosts+1] = { ip = new_ip(host), port = port }
35 else
36 srv_hosts[#srv_hosts+1] = { target = host, port = port }
37 end
38 end
39 if #ip_hosts > 0 then
40 host_session.ip_hosts = ip_hosts;
41 host_session.ip_choice = 0; -- Incremented by try_next_ip
42 s2sout.try_next_ip(host_session);
43 return true;
44 end
45
46 return s2sout.try_connect(host_session, host_session.srv_hosts[1].target, host_session.srv_hosts[1].port);
47 end, -2);
48