Mercurial > prosody-hg
annotate net/xmppserver_listener.lua @ 330:d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 18 Nov 2008 14:42:45 +0000 |
| parents | 20745f8f4cf1 |
| children | 830fd67f9378 |
| rev | line source |
|---|---|
| 148 | 1 |
| 2 local logger = require "logger"; | |
| 3 local lxp = require "lxp" | |
| 4 local init_xmlhandlers = require "core.xmlhandlers" | |
| 5 local sm_new_session = require "core.sessionmanager".new_session; | |
| 6 local s2s_new_incoming = require "core.s2smanager".new_incoming; | |
| 7 local s2s_streamopened = require "core.s2smanager".streamopened; | |
|
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
8 local s2s_destroy_session = require "core.s2smanager".destroy_session; |
| 148 | 9 |
| 10 local connlisteners_register = require "net.connlisteners".register; | |
| 11 | |
| 12 local t_insert = table.insert; | |
| 13 local t_concat = table.concat; | |
| 14 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 | |
| 15 local m_random = math.random; | |
| 16 local format = string.format; | |
| 17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); | |
| 18 local st = stanza; | |
| 19 | |
| 20 local sessions = {}; | |
| 21 local xmppserver = { default_port = 5269 }; | |
| 22 | |
| 23 -- These are session methods -- | |
| 24 | |
| 25 local function session_reset_stream(session) | |
| 26 -- Reset stream | |
| 27 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|"); | |
| 28 session.parser = parser; | |
| 29 | |
| 30 session.notopen = true; | |
| 31 | |
| 32 function session.data(conn, data) | |
| 33 parser:parse(data); | |
| 34 end | |
| 35 return true; | |
| 36 end | |
| 37 | |
|
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
|
38 |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
39 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
40 local function session_disconnect(session, reason) |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
41 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
|
42 if session.conn 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
|
43 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
|
44 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
|
45 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
46 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza)); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
57 session.send(stanza); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
58 elseif reason.name then -- a stanza |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
59 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(reason)); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
60 session.send(reason); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 session.send("</stream:stream>"); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
65 session.conn.close(); |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
66 xmppserver.disconnect(session.conn, "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
|
67 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
|
68 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
|
69 |
|
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
70 |
| 148 | 71 -- End of session methods -- |
| 72 | |
| 73 function xmppserver.listener(conn, data) | |
| 74 local session = sessions[conn]; | |
| 75 if not session then | |
| 76 session = s2s_new_incoming(conn); | |
| 77 sessions[conn] = session; | |
| 78 | |
| 79 -- Logging functions -- | |
| 80 | |
| 81 local mainlog, log = log; | |
| 82 do | |
| 83 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$"); | |
| 84 log = logger.init(conn_name); | |
| 85 end | |
| 86 local print = function (...) log("info", t_concatall({...}, "\t")); end | |
| 87 session.log = log; | |
| 88 | |
| 89 print("Incoming s2s connection"); | |
| 90 | |
| 91 session.reset_stream = session_reset_stream; | |
|
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
|
92 session.disconnect = session_disconnect; |
| 148 | 93 |
| 94 session_reset_stream(session); -- Initialise, ready for use | |
| 95 | |
| 96 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
| 97 -- this will avoid the useless indirection we have atm | |
| 98 -- (I'm on a mission, no time to fix now) | |
|
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
|
99 |
|
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
|
100 -- Debug version -- |
|
232
20745f8f4cf1
Actually show error and position when we show a traceback :)
Matthew Wild <mwild1@gmail.com>
parents:
226
diff
changeset
|
101 local function handleerr(err) print("Traceback:", err, debug.traceback()); end |
|
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
|
102 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
|
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
|
103 |
|
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
|
104 -- session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
| 148 | 105 |
| 106 end | |
| 107 if data then | |
| 108 session.data(conn, data); | |
| 109 end | |
| 110 end | |
| 111 | |
| 112 function xmppserver.disconnect(conn) | |
|
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
113 local session = sessions[conn]; |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
114 if session then |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
115 s2s_destroy_session(session); |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
116 sessions[conn] = nil; |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
117 session = nil; |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
118 collectgarbage("collect"); |
|
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
119 end |
| 148 | 120 end |
| 121 | |
| 122 function xmppserver.register_outgoing(conn, session) | |
| 123 session.direction = "outgoing"; | |
| 124 sessions[conn] = session; | |
| 125 | |
| 126 session.reset_stream = session_reset_stream; | |
| 127 session_reset_stream(session); -- Initialise, ready for use | |
| 128 | |
| 129 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
| 130 -- this will avoid the useless indirection we have atm | |
| 131 -- (I'm on a mission, no time to fix now) | |
| 132 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end | |
| 133 end | |
| 134 | |
| 135 connlisteners_register("xmppserver", xmppserver); | |
| 136 | |
| 137 | |
| 138 -- We need to perform some initialisation when a connection is created | |
| 139 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) | |
| 140 | |
| 141 -- ...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
|
142 -- ...and record all sessions associated with connections |
