Mercurial > prosody-hg
comparison plugins/mod_c2s.lua @ 6005:98b768a41c9d
mod_c2s: Break out stream opening into a separate function
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Fri, 31 Jan 2014 12:01:12 +0100 |
| parents | e327f2d4e09f |
| children | e626ee2fe106 |
comparison
equal
deleted
inserted
replaced
| 6004:09151d26560a | 6005:98b768a41c9d |
|---|---|
| 36 local listener = {}; | 36 local listener = {}; |
| 37 local runner_callbacks = {}; | 37 local runner_callbacks = {}; |
| 38 | 38 |
| 39 --- Stream events handlers | 39 --- Stream events handlers |
| 40 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; | 40 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
| 41 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; | |
| 42 | 41 |
| 43 function stream_callbacks.streamopened(session, attr) | 42 function stream_callbacks.streamopened(session, attr) |
| 44 local send = session.send; | 43 local send = session.send; |
| 45 session.host = nameprep(attr.to); | 44 session.host = nameprep(attr.to); |
| 46 if not session.host then | 45 if not session.host then |
| 56 -- We don't serve this host... | 55 -- We don't serve this host... |
| 57 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)}; | 56 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)}; |
| 58 return; | 57 return; |
| 59 end | 58 end |
| 60 | 59 |
| 61 send("<?xml version='1.0'?>"..st.stanza("stream:stream", { | 60 session:open_stream(); |
| 62 xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams'; | |
| 63 id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }):top_tag()); | |
| 64 | 61 |
| 65 (session.log or log)("debug", "Sent reply <stream:stream> to client"); | 62 (session.log or log)("debug", "Sent reply <stream:stream> to client"); |
| 66 session.notopen = nil; | 63 session.notopen = nil; |
| 67 | 64 |
| 68 -- If session.secure is *false* (not nil) then it means we /were/ encrypting | 65 -- If session.secure is *false* (not nil) then it means we /were/ encrypting |
| 127 --- Session methods | 124 --- Session methods |
| 128 local function session_close(session, reason) | 125 local function session_close(session, reason) |
| 129 local log = session.log or log; | 126 local log = session.log or log; |
| 130 if session.conn then | 127 if session.conn then |
| 131 if session.notopen then | 128 if session.notopen then |
| 132 session.send("<?xml version='1.0'?>"); | 129 session:open_stream(); |
| 133 session.send(st.stanza("stream:stream", default_stream_attr):top_tag()); | |
| 134 end | 130 end |
| 135 if reason then -- nil == no err, initiated by us, false == initiated by client | 131 if reason then -- nil == no err, initiated by us, false == initiated by client |
| 136 local stream_error = st.stanza("stream:error"); | 132 local stream_error = st.stanza("stream:error"); |
| 137 if type(reason) == "string" then -- assume stream error | 133 if type(reason) == "string" then -- assume stream error |
| 138 stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); | 134 stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); |
| 176 conn:close(); | 172 conn:close(); |
| 177 end | 173 end |
| 178 end | 174 end |
| 179 end | 175 end |
| 180 | 176 |
| 177 local function session_open_stream(session) | |
| 178 local attr = { | |
| 179 ["xmlns:stream"] = 'http://etherx.jabber.org/streams', | |
| 180 xmlns = stream_callbacks.default_ns, | |
| 181 version = "1.0", | |
| 182 ["xml:lang"] = 'en', | |
| 183 id = session.streamid or "", | |
| 184 from = session.host | |
| 185 }; | |
| 186 session.send("<?xml version='1.0'?>"); | |
| 187 session.send(st.stanza("stream:stream", attr):top_tag()); | |
| 188 end | |
| 189 | |
| 181 module:hook_global("user-deleted", function(event) | 190 module:hook_global("user-deleted", function(event) |
| 182 local username, host = event.username, event.host; | 191 local username, host = event.username, event.host; |
| 183 local user = hosts[host].sessions[username]; | 192 local user = hosts[host].sessions[username]; |
| 184 if user and user.sessions then | 193 if user and user.sessions then |
| 185 for jid, session in pairs(user.sessions) do | 194 for jid, session in pairs(user.sessions) do |
| 223 | 232 |
| 224 if opt_keepalives then | 233 if opt_keepalives then |
| 225 conn:setoption("keepalive", opt_keepalives); | 234 conn:setoption("keepalive", opt_keepalives); |
| 226 end | 235 end |
| 227 | 236 |
| 237 session.open_stream = session_open_stream; | |
| 228 session.close = session_close; | 238 session.close = session_close; |
| 229 | 239 |
| 230 local stream = new_xmpp_stream(session, stream_callbacks); | 240 local stream = new_xmpp_stream(session, stream_callbacks); |
| 231 session.stream = stream; | 241 session.stream = stream; |
| 232 session.notopen = true; | 242 session.notopen = true; |
