Software /
code /
prosody
Comparison
core/sessionmanager.lua @ 429:b1d86f89fa27
Added options to limit the number of resources and for handling of resource conflicts
- core.max_resources: defaults to 10
- core.conflict_resolve: can be "random", "increment", "kick_new" and "kick_old" (default)
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 27 Nov 2008 02:48:08 +0500 |
parent | 357:17bcecb06420 |
child | 519:cccd610a0ef9 |
comparison
equal
deleted
inserted
replaced
426:3d8778059e90 | 429:b1d86f89fa27 |
---|---|
11 local modulemanager = require "core.modulemanager"; | 11 local modulemanager = require "core.modulemanager"; |
12 local log = require "util.logger".init("sessionmanager"); | 12 local log = require "util.logger".init("sessionmanager"); |
13 local error = error; | 13 local error = error; |
14 local uuid_generate = require "util.uuid".generate; | 14 local uuid_generate = require "util.uuid".generate; |
15 local rm_load_roster = require "core.rostermanager".load_roster; | 15 local rm_load_roster = require "core.rostermanager".load_roster; |
16 local config_get = require "core.configmanager".get; | |
16 | 17 |
17 local st = require "util.stanza"; | 18 local st = require "util.stanza"; |
18 | 19 |
19 local newproxy = newproxy; | 20 local newproxy = newproxy; |
20 local getmetatable = getmetatable; | 21 local getmetatable = getmetatable; |
86 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing | 87 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing |
87 | 88 |
88 if not hosts[session.host].sessions[session.username] then | 89 if not hosts[session.host].sessions[session.username] then |
89 hosts[session.host].sessions[session.username] = { sessions = {} }; | 90 hosts[session.host].sessions[session.username] = { sessions = {} }; |
90 else | 91 else |
91 if hosts[session.host].sessions[session.username].sessions[resource] then | 92 local sessions = hosts[session.host].sessions[session.username].sessions; |
93 local limit = config_get(session.host, "core", "max_resources") or 10; | |
94 if #sessions >= limit then | |
95 return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed"; | |
96 end | |
97 if sessions[resource] then | |
92 -- Resource conflict | 98 -- Resource conflict |
93 return nil, "cancel", "conflict", "Resource already exists"; -- TODO kick old resource | 99 local policy = config_get(session.host, "core", "conflict_resolve"); |
100 local increment; | |
101 if policy == "random" then | |
102 resource = uuid_generate(); | |
103 increment = true; | |
104 elseif policy == "increment" then | |
105 increment = true; -- TODO ping old resource | |
106 elseif policy == "kick_new" then | |
107 return nil, "cancel", "conflict", "Resource already exists"; | |
108 else -- if policy == "kick_old" then | |
109 hosts[session.host].sessions[session.username].sessions[resource]:close { | |
110 condition = "conflict"; | |
111 text = "Replaced by new connection"; | |
112 }; | |
113 end | |
114 if increment and sessions[resource] then | |
115 local count = 1; | |
116 while sessions[resource.."#"..count] do | |
117 count = count + 1; | |
118 end | |
119 resource = resource.."#"..count; | |
120 end | |
94 end | 121 end |
95 end | 122 end |
96 | 123 |
97 session.resource = resource; | 124 session.resource = resource; |
98 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource; | 125 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource; |