Comparison

mod_bidi/mod_bidi.lua @ 892:148865199003

mod_bidi: Initial commit of XEP-0288 implementation
author Kim Alvefur <zash@zash.se>
date Fri, 11 Jan 2013 01:08:31 +0100
child 893:602e4c509095
comparison
equal deleted inserted replaced
891:e089160c424b 892:148865199003
1 -- Bidirectional Server-to-Server Connections
2 -- http://xmpp.org/extensions/xep-0288.html
3 -- Copyright (C) 2013 Kim Alvefur
4 --
5 -- This file is MIT/X11 licensed.
6 --
7 local s2smanager = require"core.s2smanager";
8 local add_filter = require "util.filters".add_filter;
9 local st = require "util.stanza";
10 local jid_split = require"util.jid".prepped_split;
11
12 local xmlns_bidi_feature = "urn:xmpp:features:bidi"
13 local xmlns_bidi = "urn:xmpp:bidi";
14 local noop = function () end
15 local core_process_stanza = prosody.core_process_stanza;
16
17 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
18 local function handlestanza(session, stanza)
19 if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
20 stanza.attr.xmlns = nil;
21 end
22 -- stanza = session.filter("stanzas/in", stanza);
23 if stanza then
24 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
25 end
26 end
27
28 local function new_bidi(origin)
29 local bidi_session, remote_host;
30 origin.log("debug", "Creating bidirectional session wrapper");
31 if origin.direction == "incoming" then
32 remote_host = origin.from_host;
33 bidi_session = s2smanager.new_outgoing(origin.to_host, origin.from_host)
34 else -- outgoing
35 remote_host = origin.to_host;
36 bidi_session = s2smanager.new_incoming(origin.conn)
37 bidi_session.to_host = origin.from_host;
38 bidi_session.from_host = origin.to_host;
39 add_filter(origin, "stanzas/in", function(stanza)
40 if stanza.attr.xmlns ~= nil then return stanza end
41 local _, host = jid_split(stanza.attr.from);
42 if host ~= remote_host then return stanza end
43 handlestanza(bidi_session, stanza);
44 end, 1);
45 end
46 origin.bidi_session = bidi_session;
47 bidi_session.sends2s = origin.sends2s;
48 bidi_session.bounce_sendq = noop;
49 bidi_session.notopen = nil;
50 bidi_session.is_bidi = true;
51 bidi_session.bidi_session = false;
52 bidi_session.orig_session = origin;
53 bidi_session.secure = origin.secure;
54 bidi_session.cert_identity_status = origin.cert_identity_status;
55 bidi_session.cert_chain_status = origin.cert_chain_status;
56 bidi_session.close = function(...)
57 return origin.close(...);
58 end
59
60 bidi_session.log("info", "Bidirectional session established");
61 s2smanager.make_authenticated(bidi_session, remote_host);
62 return bidi_session;
63 end
64
65 -- Incoming s2s
66 module:hook("s2s-stream-features", function(event)
67 local origin, features = event.origin, event.features;
68 if not origin.is_bidi and not hosts[module.host].s2sout[origin.from_host] then
69 module:log("debug", "Announcing support for bidirectional streams");
70 features:tag("bidi", { xmlns = xmlns_bidi_feature }):up();
71 end
72 end);
73
74 module:hook("stanza/urn:xmpp:bidi:bidi", function(event)
75 local origin = event.session or event.origin;
76 if not origin.is_bidi and not origin.bidi_session then
77 module:log("debug", "%s requested bidirectional stream", origin.from_host);
78 if hosts[module.host].s2sout[origin.from_host] then
79 local conflicting_session = hosts[module.host].s2sout[origin.from_host]
80 conflicting_session.log("warn", "We already have an outgoing connection to %s, closing it...", origin.from_host);
81 conflicting_session:close{ condition = "conflict", text = "Replaced by bidirectional stream" }
82 s2smanager.destroy_session(conflicting_session);
83 end
84 origin.do_bidi = true;
85 return true;
86 end
87 end);
88
89 -- Outgoing s2s
90 module:hook("stanza/http://etherx.jabber.org/streams:features", function(event)
91 local origin = event.session or event.origin;
92 if not ( origin.bidi_session or origin.is_bidi or origin.do_bidi)
93 and event.stanza:get_child("bidi", xmlns_bidi_feature) then
94 module:log("debug", "%s supports bidirectional streams", origin.to_host);
95 origin.sends2s(st.stanza("bidi", { xmlns = xmlns_bidi }));
96 origin.do_bidi = true;
97 end
98 end, 160);
99
100 function enable_bidi(event)
101 local session = event.session;
102 if session.do_bidi and not ( session.is_bidi or session.bidi_session ) then
103 session.do_bidi = nil;
104 new_bidi(session);
105 end
106 end
107
108 module:hook("s2sin-established", enable_bidi);
109 module:hook("s2sout-established", enable_bidi);
110
111 function disable_bidi(event)
112 local session = event.session;
113 if session.bidi_session then
114 local bidi_session = session.bidi_session;
115 session.bidi_session = nil;
116 session.log("debug", "Tearing down bidirectional stream");
117 s2smanager.destroy_session(bidi_session, event.reason);
118 elseif session.orig_session then
119 local orig_session = session.orig_session;
120 session.orig_session = nil;
121 orig_session.log("debug", "Tearing down bidirectional stream");
122 s2smanager.destroy_session(orig_session, event.reason);
123 end
124 end
125
126 module:hook("s2sin-destroyed", disable_bidi);
127 module:hook("s2sout-destroyed", disable_bidi);
128