annotate mod_bidi/mod_bidi.lua @ 959:6ef334596276

mod_firewall/actions: Add REPLY
author Matthew Wild <mwild1@gmail.com>
date Fri, 05 Apr 2013 18:06:32 +0100
parents 4e235e565693
children c714ed7de4ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Bidirectional Server-to-Server Connections
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- http://xmpp.org/extensions/xep-0288.html
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (C) 2013 Kim Alvefur
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 --
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- This file is MIT/X11 licensed.
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 --
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local s2smanager = require"core.s2smanager";
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local add_filter = require "util.filters".add_filter;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local st = require "util.stanza";
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local jid_split = require"util.jid".prepped_split;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local xmlns_bidi_feature = "urn:xmpp:features:bidi"
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local xmlns_bidi = "urn:xmpp:bidi";
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local noop = function () end
899
4cd018025a26 mod_bidi: Work with 0.8
Kim Alvefur <zash@zash.se>
parents: 898
diff changeset
15 local core_process_stanza = prosody.core_process_stanza or core_process_stanza;
898
4aa473216432 mod_bidi: Missing import of debug.trackeback()
Kim Alvefur <zash@zash.se>
parents: 897
diff changeset
16 local traceback = debug.traceback;
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local function handlestanza(session, stanza)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 stanza.attr.xmlns = nil;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 -- stanza = session.filter("stanzas/in", stanza);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if stanza then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local function new_bidi(origin)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local bidi_session, remote_host;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 origin.log("debug", "Creating bidirectional session wrapper");
893
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
32 if origin.direction == "incoming" then -- then we create an "outgoing" bidirectional session
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
33 local conflicting_session = hosts[origin.to_host].s2sout[origin.from_host]
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
34 if conflicting_session then
894
d066987e00b7 mod_bidi: Lower severity of notice about outgoing stream being replaced by bidi
Kim Alvefur <zash@zash.se>
parents: 893
diff changeset
35 conflicting_session.log("info", "We already have an outgoing connection to %s, closing it...", origin.from_host);
893
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
36 conflicting_session:close{ condition = "conflict", text = "Replaced by bidirectional stream" }
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
37 s2smanager.destroy_session(conflicting_session);
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
38 end
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 remote_host = origin.from_host;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 bidi_session = s2smanager.new_outgoing(origin.to_host, origin.from_host)
893
602e4c509095 mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
Kim Alvefur <zash@zash.se>
parents: 892
diff changeset
41 else -- outgoing -- then we create an "incoming" bidirectional session
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 remote_host = origin.to_host;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 bidi_session = s2smanager.new_incoming(origin.conn)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 bidi_session.to_host = origin.from_host;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 bidi_session.from_host = origin.to_host;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 add_filter(origin, "stanzas/in", function(stanza)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 if stanza.attr.xmlns ~= nil then return stanza end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local _, host = jid_split(stanza.attr.from);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if host ~= remote_host then return stanza end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 handlestanza(bidi_session, stanza);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end, 1);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 origin.bidi_session = bidi_session;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 bidi_session.sends2s = origin.sends2s;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 bidi_session.bounce_sendq = noop;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 bidi_session.notopen = nil;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 bidi_session.is_bidi = true;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 bidi_session.bidi_session = false;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 bidi_session.orig_session = origin;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 bidi_session.secure = origin.secure;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 bidi_session.cert_identity_status = origin.cert_identity_status;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 bidi_session.cert_chain_status = origin.cert_chain_status;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 bidi_session.close = function(...)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 return origin.close(...);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 bidi_session.log("info", "Bidirectional session established");
932
4e235e565693 mod_bidi, mod_dwd, mod_s2s_idle_timeout: Update for recent 0.9 changes (612467e263af)
Matthew Wild <mwild1@gmail.com>
parents: 899
diff changeset
68 module:fire_event("s2s-authenticated", { session = bidi_session, host = remote_host });
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return bidi_session;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 -- Incoming s2s
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 module:hook("s2s-stream-features", function(event)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 local origin, features = event.origin, event.features;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 if not origin.is_bidi and not hosts[module.host].s2sout[origin.from_host] then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 module:log("debug", "Announcing support for bidirectional streams");
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 features:tag("bidi", { xmlns = xmlns_bidi_feature }):up();
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 module:hook("stanza/urn:xmpp:bidi:bidi", function(event)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local origin = event.session or event.origin;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if not origin.is_bidi and not origin.bidi_session then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 module:log("debug", "%s requested bidirectional stream", origin.from_host);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 origin.do_bidi = true;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 return true;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 -- Outgoing s2s
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 module:hook("stanza/http://etherx.jabber.org/streams:features", function(event)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 local origin = event.session or event.origin;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if not ( origin.bidi_session or origin.is_bidi or origin.do_bidi)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 and event.stanza:get_child("bidi", xmlns_bidi_feature) then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 module:log("debug", "%s supports bidirectional streams", origin.to_host);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 origin.sends2s(st.stanza("bidi", { xmlns = xmlns_bidi }));
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 origin.do_bidi = true;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end, 160);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 function enable_bidi(event)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 local session = event.session;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 if session.do_bidi and not ( session.is_bidi or session.bidi_session ) then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 session.do_bidi = nil;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 new_bidi(session);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 module:hook("s2sin-established", enable_bidi);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 module:hook("s2sout-established", enable_bidi);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 function disable_bidi(event)
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 local session = event.session;
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 if session.bidi_session then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local bidi_session = session.bidi_session;
897
844b6ef5d4c4 mod_bidi: Disassociate bidi and original session when they are destroyed
Kim Alvefur <zash@zash.se>
parents: 894
diff changeset
116 session.bidi_session, bidi_session.orig_session = nil, nil;
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 session.log("debug", "Tearing down bidirectional stream");
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 s2smanager.destroy_session(bidi_session, event.reason);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 elseif session.orig_session then
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 local orig_session = session.orig_session;
897
844b6ef5d4c4 mod_bidi: Disassociate bidi and original session when they are destroyed
Kim Alvefur <zash@zash.se>
parents: 894
diff changeset
121 orig_session.bidi_session, session.orig_session = nil, nil;
892
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 orig_session.log("debug", "Tearing down bidirectional stream");
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 s2smanager.destroy_session(orig_session, event.reason);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 module:hook("s2sin-destroyed", disable_bidi);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 module:hook("s2sout-destroyed", disable_bidi);
148865199003 mod_bidi: Initial commit of XEP-0288 implementation
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129