Software /
code /
prosody
Annotate
net/xmppserver_listener.lua @ 2966:e78436234775
loggingmanager: Fix syntax error :)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 07 Apr 2010 21:08:16 +0100 |
parent | 2951:294c359a05f5 |
child | 3141:50318ac90394 |
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 | 5 -- This project is MIT/X11 licensed. Please see the |
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 |
148 | 10 |
11 local logger = require "logger"; | |
1040
4c79b28bce64
xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents:
990
diff
changeset
|
12 local log = logger.init("xmppserver_listener"); |
148 | 13 local lxp = require "lxp" |
14 local init_xmlhandlers = require "core.xmlhandlers" | |
15 local s2s_new_incoming = require "core.s2smanager".new_incoming; | |
16 local s2s_streamopened = require "core.s2smanager".streamopened; | |
342
52f75260a22d
Incorrect function set as callback
Matthew Wild <mwild1@gmail.com>
parents:
333
diff
changeset
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 |
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
|
23 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
|
24 |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 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
|
29 session.log("debug", "Server-to-server XML parse error: %s", tostring(error)); |
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
30 session:close("xml-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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 end |
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
49 end |
331 | 50 |
611
7bb91fcddaf8
Fix blank tracebacks for c2s/s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
604
diff
changeset
|
51 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), debug.traceback()); end |
585
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
52 function stream_callbacks.handlestanza(a, b) |
2950
0250fba6be72
xmppserver_listener: Compatibility fix for older Prosodies with the s2s xmlns bug
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
53 if b.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client |
0250fba6be72
xmppserver_listener: Compatibility fix for older Prosodies with the s2s xmlns bug
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
54 b.attr.xmlns = nil; |
0250fba6be72
xmppserver_listener: Compatibility fix for older Prosodies with the s2s xmlns bug
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
55 end |
585
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
56 xpcall(function () core_process_stanza(a, b) end, handleerr); |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
57 end |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
58 |
148 | 59 local connlisteners_register = require "net.connlisteners".register; |
60 | |
61 local t_insert = table.insert; | |
62 local t_concat = table.concat; | |
63 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end | |
64 local m_random = math.random; | |
65 local format = string.format; | |
1040
4c79b28bce64
xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents:
990
diff
changeset
|
66 local sessionmanager = require "core.sessionmanager"; |
4c79b28bce64
xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents:
990
diff
changeset
|
67 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; |
4c79b28bce64
xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents:
990
diff
changeset
|
68 local st = require "util.stanza"; |
148 | 69 |
70 local sessions = {}; | |
451
e9f269e5204e
No more reading 1 byte at a time from sockets
Matthew Wild <mwild1@gmail.com>
parents:
434
diff
changeset
|
71 local xmppserver = { default_port = 5269, default_mode = "*a" }; |
148 | 72 |
73 -- These are session methods -- | |
74 | |
75 local function session_reset_stream(session) | |
76 -- Reset stream | |
2077
e33658f6052c
Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).
Waqas Hussain <waqas20@gmail.com>
parents:
2052
diff
changeset
|
77 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1"); |
148 | 78 session.parser = parser; |
79 | |
80 session.notopen = true; | |
81 | |
82 function session.data(conn, data) | |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
83 local ok, err = parser:parse(data); |
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
84 if ok then return; end |
2052
161db352822c
xmppserver_listener: Minor logging fix: Prefer the session logger when available to log invalid XML warnings.
Waqas Hussain <waqas20@gmail.com>
parents:
1978
diff
changeset
|
85 (session.log or log)("warn", "Received invalid XML: %s", data); |
161db352822c
xmppserver_listener: Minor logging fix: Prefer the session logger when available to log invalid XML warnings.
Waqas Hussain <waqas20@gmail.com>
parents:
1978
diff
changeset
|
86 (session.log or log)("warn", "Problem was: %s", err); |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
87 session:close("xml-not-well-formed"); |
148 | 88 end |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
89 |
148 | 90 return true; |
91 end | |
92 | |
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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason); |
331 | 105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
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 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
|
113 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
|
114 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
|
115 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza)); |
331 | 116 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
|
117 elseif reason.name then -- a stanza |
331 | 118 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)); |
119 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
|
120 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
|
121 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
|
122 end |
331 | 123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 |
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
132 |
148 | 133 -- End of session methods -- |
134 | |
2129
fcdcdf00787c
*_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents:
2077
diff
changeset
|
135 function xmppserver.onincoming(conn, data) |
148 | 136 local session = sessions[conn]; |
137 if not session then | |
138 session = s2s_new_incoming(conn); | |
139 sessions[conn] = session; | |
140 | |
141 -- Logging functions -- | |
142 | |
585
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
143 |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
144 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
|
145 session.log = logger.init(conn_name); |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
146 |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
147 session.log("info", "Incoming s2s connection"); |
148 | 148 |
149 session.reset_stream = session_reset_stream; | |
333
8d15b073fdbe
session:disconnect() -> session:close() for consistency with other Lua APIs
Matthew Wild <mwild1@gmail.com>
parents:
331
diff
changeset
|
150 session.close = session_close; |
148 | 151 |
152 session_reset_stream(session); -- Initialise, ready for use | |
153 | |
604
b6a31e97d018
stanza_dispatch != dispatch_stanza
Matthew Wild <mwild1@gmail.com>
parents:
598
diff
changeset
|
154 session.dispatch_stanza = stream_callbacks.handlestanza; |
148 | 155 end |
156 if data then | |
157 session.data(conn, data); | |
158 end | |
159 end | |
160 | |
2560
56063f825199
net.xmppserver_listener: status -> onstatus for consistency
Matthew Wild <mwild1@gmail.com>
parents:
2471
diff
changeset
|
161 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
|
162 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
|
163 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
|
164 if session and session.direction == "outgoing" 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
|
165 local format, to_host, from_host = string.format, 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
|
166 session.log("debug", "Sending stream header..."); |
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
|
167 session.sends2s(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)); |
d5dc9e06d917
xmppserver_listener: Add status() function to signal when a connection completes its SSL handshake
Matthew Wild <mwild1@gmail.com>
parents:
1879
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 |
2129
fcdcdf00787c
*_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents:
2077
diff
changeset
|
172 function xmppserver.ondisconnect(conn, err) |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
173 local session = sessions[conn]; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
174 if session then |
434
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
175 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
|
176 (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
|
177 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
|
178 (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
|
179 return; -- Session lives for now |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
180 end |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
181 end |
2745
5cedad1bcb28
net.xmppserver_listener: Clarify log message (for nil/false)
Matthew Wild <mwild1@gmail.com>
parents:
2560
diff
changeset
|
182 (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
|
183 s2s_destroy_session(session, err); |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
184 sessions[conn] = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
185 session = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
186 end |
148 | 187 end |
188 | |
189 function xmppserver.register_outgoing(conn, session) | |
190 session.direction = "outgoing"; | |
191 sessions[conn] = session; | |
192 | |
990
235abebc896e
xmppserver_listener: Add session:close() method to outgoing s2s connections too
Matthew Wild <mwild1@gmail.com>
parents:
974
diff
changeset
|
193 session.reset_stream = session_reset_stream; |
235abebc896e
xmppserver_listener: Add session:close() method to outgoing s2s connections too
Matthew Wild <mwild1@gmail.com>
parents:
974
diff
changeset
|
194 session.close = session_close; |
148 | 195 session_reset_stream(session); -- Initialise, ready for use |
196 | |
585
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
197 --local function handleerr(err) print("Traceback:", err, debug.traceback()); end |
033817e12ddb
Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents:
557
diff
changeset
|
198 --session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
148 | 199 end |
200 | |
201 connlisteners_register("xmppserver", xmppserver); | |
202 | |
203 | |
204 -- We need to perform some initialisation when a connection is created | |
205 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) | |
206 | |
207 -- ...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
|
208 -- ...and record all sessions associated with connections |