Software /
code /
prosody
Comparison
core/sessionmanager.lua @ 6779:6236668da30a
core.*: Remove use of module() function
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 21 Feb 2015 10:42:19 +0100 |
parent | 6677:1089f45c5e67 |
child | 6842:eed846384178 |
comparison
equal
deleted
inserted
replaced
6778:4009ae66e0f0 | 6779:6236668da30a |
---|---|
22 local uuid_generate = require "util.uuid".generate; | 22 local uuid_generate = require "util.uuid".generate; |
23 | 23 |
24 local initialize_filters = require "util.filters".initialize; | 24 local initialize_filters = require "util.filters".initialize; |
25 local gettime = require "socket".gettime; | 25 local gettime = require "socket".gettime; |
26 | 26 |
27 module "sessionmanager" | 27 local _ENV = nil; |
28 | 28 |
29 function new_session(conn) | 29 local function new_session(conn) |
30 local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() }; | 30 local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() }; |
31 local filter = initialize_filters(session); | 31 local filter = initialize_filters(session); |
32 local w = conn.write; | 32 local w = conn.write; |
33 session.send = function (t) | 33 session.send = function (t) |
34 if t.name then | 34 if t.name then |
55 session.log("debug", "Attempt to close already-closed session"); | 55 session.log("debug", "Attempt to close already-closed session"); |
56 end; | 56 end; |
57 filter = function (type, data) return data; end; --luacheck: ignore 212/type | 57 filter = function (type, data) return data; end; --luacheck: ignore 212/type |
58 }; resting_session.__index = resting_session; | 58 }; resting_session.__index = resting_session; |
59 | 59 |
60 function retire_session(session) | 60 local function retire_session(session) |
61 local log = session.log or log; --luacheck: ignore 431/log | 61 local log = session.log or log; --luacheck: ignore 431/log |
62 for k in pairs(session) do | 62 for k in pairs(session) do |
63 if k ~= "log" and k ~= "id" then | 63 if k ~= "log" and k ~= "id" then |
64 session[k] = nil; | 64 session[k] = nil; |
65 end | 65 end |
69 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end | 69 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end |
70 session.thread = { run = function (_, data) return session.data(data) end }; | 70 session.thread = { run = function (_, data) return session.data(data) end }; |
71 return setmetatable(session, resting_session); | 71 return setmetatable(session, resting_session); |
72 end | 72 end |
73 | 73 |
74 function destroy_session(session, err) | 74 local function destroy_session(session, err) |
75 (session.log or log)("debug", "Destroying session for %s (%s@%s)%s", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)", err and (": "..err) or ""); | 75 (session.log or log)("debug", "Destroying session for %s (%s@%s)%s", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)", err and (": "..err) or ""); |
76 if session.destroyed then return; end | 76 if session.destroyed then return; end |
77 | 77 |
78 -- Remove session/resource from user's session list | 78 -- Remove session/resource from user's session list |
79 if session.full_jid then | 79 if session.full_jid then |
97 end | 97 end |
98 | 98 |
99 retire_session(session); | 99 retire_session(session); |
100 end | 100 end |
101 | 101 |
102 function make_authenticated(session, username) | 102 local function make_authenticated(session, username) |
103 username = nodeprep(username); | 103 username = nodeprep(username); |
104 if not username or #username == 0 then return nil, "Invalid username"; end | 104 if not username or #username == 0 then return nil, "Invalid username"; end |
105 session.username = username; | 105 session.username = username; |
106 if session.type == "c2s_unauthed" then | 106 if session.type == "c2s_unauthed" then |
107 session.type = "c2s"; | 107 session.type = "c2s"; |
110 return true; | 110 return true; |
111 end | 111 end |
112 | 112 |
113 -- returns true, nil on success | 113 -- returns true, nil on success |
114 -- returns nil, err_type, err, err_message on failure | 114 -- returns nil, err_type, err, err_message on failure |
115 function bind_resource(session, resource) | 115 local function bind_resource(session, resource) |
116 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end | 116 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end |
117 if session.resource then return nil, "cancel", "not-allowed", "Cannot bind multiple resources on a single connection"; end | 117 if session.resource then return nil, "cancel", "not-allowed", "Cannot bind multiple resources on a single connection"; end |
118 -- We don't support binding multiple resources | 118 -- We don't support binding multiple resources |
119 | 119 |
120 local event_payload = { session = session, resource = resource }; | 120 local event_payload = { session = session, resource = resource }; |
191 hosts[session.host].events.fire_event("resource-bind", {session=session}); | 191 hosts[session.host].events.fire_event("resource-bind", {session=session}); |
192 | 192 |
193 return true; | 193 return true; |
194 end | 194 end |
195 | 195 |
196 function send_to_available_resources(username, host, stanza) | 196 local function send_to_available_resources(username, host, stanza) |
197 local jid = username.."@"..host; | 197 local jid = username.."@"..host; |
198 local count = 0; | 198 local count = 0; |
199 local user = bare_sessions[jid]; | 199 local user = bare_sessions[jid]; |
200 if user then | 200 if user then |
201 for _, session in pairs(user.sessions) do | 201 for _, session in pairs(user.sessions) do |
206 end | 206 end |
207 end | 207 end |
208 return count; | 208 return count; |
209 end | 209 end |
210 | 210 |
211 function send_to_interested_resources(username, host, stanza) | 211 local function send_to_interested_resources(username, host, stanza) |
212 local jid = username.."@"..host; | 212 local jid = username.."@"..host; |
213 local count = 0; | 213 local count = 0; |
214 local user = bare_sessions[jid]; | 214 local user = bare_sessions[jid]; |
215 if user then | 215 if user then |
216 for _, session in pairs(user.sessions) do | 216 for _, session in pairs(user.sessions) do |
221 end | 221 end |
222 end | 222 end |
223 return count; | 223 return count; |
224 end | 224 end |
225 | 225 |
226 return _M; | 226 return { |
227 new_session = new_session; | |
228 retire_session = retire_session; | |
229 destroy_session = destroy_session; | |
230 make_authenticated = make_authenticated; | |
231 bind_resource = bind_resource; | |
232 send_to_available_resources = send_to_available_resources; | |
233 send_to_interested_resources = send_to_interested_resources; | |
234 }; |