Annotate

net/xmppserver_listener.lua @ 3874:1488fd9d3fa5

net.xmppserver_listener: Removed unused variables and imports.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 15 Dec 2010 03:53:44 +0500
parent 3873:9bc569083226
child 3755:0865524c741d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1040
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
9
3873
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
10 local tostring = tostring;
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
11 local type = type;
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
12 local xpcall = xpcall;
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
13 local s_format = string.format;
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
14 local traceback = debug.traceback;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local logger = require "logger";
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 990
diff changeset
17 local log = logger.init("xmppserver_listener");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local lxp = require "lxp"
3874
1488fd9d3fa5 net.xmppserver_listener: Removed unused variables and imports.
Waqas Hussain <waqas20@gmail.com>
parents: 3873
diff changeset
19 local st = require "util.stanza";
1488fd9d3fa5 net.xmppserver_listener: Removed unused variables and imports.
Waqas Hussain <waqas20@gmail.com>
parents: 3873
diff changeset
20 local connlisteners_register = require "net.connlisteners".register;
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
21 local new_xmpp_stream = require "util.xmppstream".new;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local s2s_new_incoming = require "core.s2smanager".new_incoming;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local s2s_streamopened = require "core.s2smanager".streamopened;
342
52f75260a22d Incorrect function set as callback
Matthew Wild <mwild1@gmail.com>
parents: 333
diff changeset
24 local s2s_streamclosed = require "core.s2smanager".streamclosed;
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
25 local s2s_destroy_session = require "core.s2smanager".destroy_session;
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
26 local s2s_attempt_connect = require "core.s2smanager".attempt_connection;
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2465
diff changeset
27 local stream_callbacks = { default_ns = "jabber:server",
900
686e3e4a7e15 net.xmppserver_listener: Set default namespace to jabber:server
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
28 streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza = core_process_stanza };
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
29
2471
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
30 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
31
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
32 function stream_callbacks.error(session, error, data)
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
33 if error == "no-stream" then
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
34 session:close("invalid-namespace");
2471
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
35 elseif error == "parse-error" then
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
36 session.log("debug", "Server-to-server XML parse error: %s", tostring(error));
3634
0d6b57edb7bd net.xmpp{client,server,component}_listener: s/xml-not-well-formed/not-well-formed/ as per latest bis drafts.
Waqas Hussain <waqas20@gmail.com>
parents: 3345
diff changeset
37 session:close("not-well-formed");
2471
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
38 elseif error == "stream-error" then
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
39 local condition, text = "undefined-condition";
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
40 for child in data:children() do
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
41 if child.attr.xmlns == xmlns_xmpp_streams then
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
42 if child.name ~= "text" then
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
43 condition = child.name;
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
44 else
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
45 text = child:get_text();
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
46 end
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
47 if condition ~= "undefined-condition" and text then
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
48 break;
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
49 end
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
50 end
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
51 end
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
52 text = condition .. (text and (" ("..text..")") or "");
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
53 session.log("info", "Session closed by remote with error: %s", text);
2aeb55a51f47 net.xmppserver_listener: Handle stream-error errors from xmlhandlers, and close session in response
Matthew Wild <mwild1@gmail.com>
parents: 2470
diff changeset
54 session:close(nil, text);
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
55 end
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
56 end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
57
3873
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
58 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
59 function stream_callbacks.handlestanza(session, stanza)
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
60 if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
61 stanza.attr.xmlns = nil;
2950
0250fba6be72 xmppserver_listener: Compatibility fix for older Prosodies with the s2s xmlns bug
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
62 end
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
63 stanza = session.filter("stanzas/in", stanza);
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
64 if stanza then
3345
8520cd88b84c xmpp{client,server}_listener: Convert some calls to tail calls for efficiency
Matthew Wild <mwild1@gmail.com>
parents: 3333
diff changeset
65 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
66 end
585
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
67 end
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
68
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local sessions = {};
451
e9f269e5204e No more reading 1 byte at a time from sockets
Matthew Wild <mwild1@gmail.com>
parents: 434
diff changeset
70 local xmppserver = { default_port = 5269, default_mode = "*a" };
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 -- These are session methods --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
74 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2465
diff changeset
75 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
2470
2c3f05c01d7c net.xmppserver_listener: Extend session:close() with a remote_reason parameter
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
76 local function session_close(session, reason, remote_reason)
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
77 local log = session.log or log;
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
78 if session.conn then
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
79 if session.notopen then
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
80 session.sends2s("<?xml version='1.0'?>");
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
81 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
82 end
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
83 if reason then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
84 if type(reason) == "string" then -- assume stream error
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
85 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason);
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
86 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
87 elseif type(reason) == "table" then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
88 if reason.condition then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
89 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
90 if reason.text then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
91 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
92 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
93 if reason.extra then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
94 stanza:add_child(reason.extra);
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
95 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
96 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza));
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
97 session.sends2s(stanza);
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
98 elseif reason.name then -- a stanza
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
99 log("info", "Disconnecting %s->%s[%s], <stream:error> is: %s", session.from_host or "(unknown host)", session.to_host or "(unknown host)", session.type, tostring(reason));
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
100 session.sends2s(reason);
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
101 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
102 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
103 end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
104 session.sends2s("</stream:stream>");
2256
482bc84c15ea xmppserver_listener: Update for new server API, fixes traceback when closing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
105 if session.notopen or not session.conn:close() then
482bc84c15ea xmppserver_listener: Update for new server API, fixes traceback when closing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
106 session.conn:close(true); -- Force FIXME: timer?
1951
632039101699 xmppserver_listener: More forcefully close s2s connections (fixes fd leak)
Matthew Wild <mwild1@gmail.com>
parents: 1617
diff changeset
107 end
2256
482bc84c15ea xmppserver_listener: Update for new server API, fixes traceback when closing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
108 session.conn:close();
2470
2c3f05c01d7c net.xmppserver_listener: Extend session:close() with a remote_reason parameter
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
109 xmppserver.ondisconnect(session.conn, remote_reason or (reason and (reason.text or reason.condition)) or reason or "stream closed");
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
110 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
111 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
112
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
113
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 -- End of session methods --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
116 local function initialize_session(session)
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
117 local stream = new_xmpp_stream(session, stream_callbacks);
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
118 session.stream = stream;
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
119
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
120 session.notopen = true;
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
121
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
122 function session.reset_stream()
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
123 session.notopen = true;
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
124 session.stream:reset();
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
125 end
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
126
3147
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
127 local filter = session.filter;
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
128 function session.data(data)
3147
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
129 data = filter("bytes/in", data);
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
130 if data then
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
131 local ok, err = stream:feed(data);
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
132 if ok then return; end
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
133 (session.log or log)("warn", "Received invalid XML: %s", data);
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
134 (session.log or log)("warn", "Problem was: %s", err);
3634
0d6b57edb7bd net.xmpp{client,server,component}_listener: s/xml-not-well-formed/not-well-formed/ as per latest bis drafts.
Waqas Hussain <waqas20@gmail.com>
parents: 3345
diff changeset
135 session:close("not-well-formed");
3147
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
136 end
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
137 end
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
138
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
139 session.close = session_close;
3147
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
140 local handlestanza = stream_callbacks.handlestanza;
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
141 function session.dispatch_stanza(session, stanza)
3332
c941d1191709 xmpp{client,server}_listener: Put stanzas/in filtering code in the correct place to make it actually work :)
Matthew Wild <mwild1@gmail.com>
parents: 3147
diff changeset
142 return handlestanza(session, stanza);
3147
bc8c31399520 xmppserver_listener: Add filters for incoming bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 3141
diff changeset
143 end
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
144 end
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
145
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
146 function xmppserver.onconnect(conn)
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
147 if not sessions[conn] then -- May be an existing outgoing session
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
148 local session = s2s_new_incoming(conn);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 sessions[conn] = session;
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
150
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 -- Logging functions --
585
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
152 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$");
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
153 session.log = logger.init(conn_name);
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
154
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
155 session.log("info", "Incoming s2s connection");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
157 initialize_session(session);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
159 end
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
160
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
161 function xmppserver.onincoming(conn, data)
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
162 local session = sessions[conn];
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
163 if session then
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
164 session.data(data);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
2560
56063f825199 net.xmppserver_listener: status -> onstatus for consistency
Matthew Wild <mwild1@gmail.com>
parents: 2471
diff changeset
168 function xmppserver.onstatus(conn, status)
1880
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
169 if status == "ssl-handshake-complete" then
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
170 local session = sessions[conn];
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
171 if session and session.direction == "outgoing" then
3873
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
172 local to_host, from_host = session.to_host, session.from_host;
1902
a7b06e2539c8 xmppserver_listener: Lower log-level of debug message to, er, 'debug'
Matthew Wild <mwild1@gmail.com>
parents: 1880
diff changeset
173 session.log("debug", "Sending stream header...");
3873
9bc569083226 net.xmppserver_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3634
diff changeset
174 session.sends2s(s_format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host));
1880
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
175 end
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
176 end
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
177 end
d5dc9e06d917 xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents: 1879
diff changeset
178
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
179 function xmppserver.ondisconnect(conn, err)
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
180 local session = sessions[conn];
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
181 if session then
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
182 if err and err ~= "closed" and session.srv_hosts then
2779
87b66be6d514 xmppserver_listener: Make log messages during SRV retries clearer
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
183 (session.log or log)("debug", "s2s connection attempt failed: %s", err);
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
184 if s2s_attempt_connect(session, err) then
2779
87b66be6d514 xmppserver_listener: Make log messages during SRV retries clearer
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
185 (session.log or log)("debug", "...so we're going to try another target");
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
186 return; -- Session lives for now
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
187 end
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
188 end
2745
5cedad1bcb28 net.xmppserver_listener: Clarify log message (for nil/false)
Matthew Wild <mwild1@gmail.com>
parents: 2560
diff changeset
189 (session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "closed"));
2782
71e6c13f6e08 xmppserver_listener: When a connection fails, pass the reason to destroy_session
Matthew Wild <mwild1@gmail.com>
parents: 2779
diff changeset
190 s2s_destroy_session(session, err);
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
191 sessions[conn] = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
192 session = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
193 end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 function xmppserver.register_outgoing(conn, session)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 session.direction = "outgoing";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 sessions[conn] = session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
3141
50318ac90394 xmppserver_listener: Port to util.xmppstream \o/
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
200 initialize_session(session);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 connlisteners_register("xmppserver", xmppserver);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 -- We need to perform some initialisation when a connection is created
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 -- We also need to perform that same initialisation at other points (SASL, TLS, ...)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 -- ...and we need to handle data
226
ba4711c4e8d2 Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
210 -- ...and record all sessions associated with connections