You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
5.0 KiB
155 lines
5.0 KiB
--
|
|
-- Copyright 2013 SipWise Team <development@sipwise.com>
|
|
--
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
-- it under the terms of the GNU General Public License as published by
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
-- (at your option) any later version.
|
|
--
|
|
-- This package is distributed in the hope that it will be useful,
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
-- GNU General Public License for more details.
|
|
--
|
|
-- You should have received a copy of the GNU General Public License
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
-- .
|
|
-- On Debian systems, the complete text of the GNU General
|
|
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
|
|
--
|
|
local utils = require 'ngcp.utils'
|
|
local utable = utils.table
|
|
local NGCPPrefs = require 'ngcp.pref'
|
|
local NGCPDomainPrefs = require 'ngcp.dp'
|
|
local NGCPPeerPrefs = require 'ngcp.pp'
|
|
local NGCPUserPrefs = require 'ngcp.up'
|
|
local NGCPProfilePrefs = require 'ngcp.pprof'
|
|
local NGCPContractPrefs = require 'ngcp.cp'
|
|
local NGCPXAvp = require 'ngcp.xavp'
|
|
|
|
-- class NGCPRealPrefs
|
|
local NGCPRealPrefs = {
|
|
__class__ = 'NGCPRealPrefs'
|
|
}
|
|
local NGCPRealPrefs_MT = { __index = NGCPRealPrefs }
|
|
|
|
NGCPRealPrefs_MT.__tostring = function ()
|
|
local xavp = NGCPXAvp:new('caller','real_prefs')
|
|
local output = string.format("caller_real_prefs:%s\n", tostring(xavp))
|
|
xavp = NGCPXAvp:new('callee','real_prefs')
|
|
output = output .. string.format("callee_real_prefs:%s\n", tostring(xavp))
|
|
return output
|
|
end
|
|
|
|
function NGCPRealPrefs:new()
|
|
local t = {}
|
|
-- creates xavp real
|
|
NGCPPrefs.init("real_prefs")
|
|
return setmetatable( t, NGCPRealPrefs_MT )
|
|
end
|
|
|
|
function NGCPRealPrefs:caller_contract_load(keys)
|
|
return NGCPRealPrefs:_contract_load("caller", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:callee_contract_load(keys)
|
|
return NGCPRealPrefs:_contract_load("callee", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:caller_peer_load(keys)
|
|
return NGCPRealPrefs:_peer_load("caller", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:callee_peer_load(keys)
|
|
return NGCPRealPrefs:_peer_load("callee", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:caller_usr_load(keys)
|
|
return NGCPRealPrefs:_usr_load("caller", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:callee_usr_load(keys)
|
|
return NGCPRealPrefs:_usr_load("callee", keys)
|
|
end
|
|
|
|
function NGCPRealPrefs:_contract_load(level, keys)
|
|
local xavp = {
|
|
contract = NGCPContractPrefs:xavp(level),
|
|
}
|
|
local contract_keys = {}
|
|
local values = sr.xavp.get(xavp.contract.name, 0, 0)
|
|
for _,v in pairs(keys) do
|
|
local value = values[v]
|
|
if value then
|
|
utable.add(contract_keys, v)
|
|
end
|
|
end
|
|
return contract_keys
|
|
end
|
|
|
|
function NGCPRealPrefs:_peer_load(level, keys)
|
|
local xavp = {
|
|
peer = NGCPPeerPrefs:xavp(level),
|
|
}
|
|
local peer_keys = {}
|
|
local values = sr.xavp.get(xavp.peer.name, 0, 0)
|
|
for _,v in pairs(keys) do
|
|
local value = values[v]
|
|
if value then
|
|
utable.add(peer_keys, v)
|
|
end
|
|
end
|
|
return peer_keys
|
|
end
|
|
|
|
function NGCPRealPrefs:_usr_load(level, keys)
|
|
local xavp = {
|
|
real = NGCPRealPrefs:xavp(level),
|
|
dom = NGCPDomainPrefs:xavp(level),
|
|
prof = NGCPProfilePrefs:xavp(level),
|
|
usr = NGCPUserPrefs:xavp(level)
|
|
}
|
|
local real_values = {}
|
|
local dom_values = sr.xavp.get(xavp.dom.name, 0, 0)
|
|
local prof_values = sr.xavp.get(xavp.prof.name, 0, 0)
|
|
local usr_values = sr.xavp.get(xavp.usr.name, 0, 0)
|
|
for _,v in pairs(keys) do
|
|
local value = usr_values[v]
|
|
if not value then
|
|
value = prof_values[v]
|
|
if not value then
|
|
value = dom_values[v]
|
|
end
|
|
end
|
|
if value then
|
|
real_values[v] = value
|
|
else
|
|
sr.log("err", string.format("key:%s not in user, profile or domain", v))
|
|
end
|
|
end
|
|
local real_keys = {}
|
|
for k,v in pairs(real_values) do
|
|
table.insert(real_keys, k)
|
|
xavp.real(k, v)
|
|
end
|
|
return real_keys
|
|
end
|
|
|
|
function NGCPRealPrefs:xavp(level, l)
|
|
if level ~= 'caller' and level ~= 'callee' then
|
|
error(string.format("unknown level:%s. It has to be [caller|callee]", tostring(level)))
|
|
end
|
|
return NGCPXAvp:new(level,'real_prefs', l)
|
|
end
|
|
|
|
function NGCPRealPrefs:clean(vtype)
|
|
if not vtype then
|
|
NGCPRealPrefs:xavp('callee'):clean()
|
|
NGCPRealPrefs:xavp('caller'):clean()
|
|
else
|
|
NGCPRealPrefs:xavp(vtype):clean()
|
|
end
|
|
end
|
|
-- class
|
|
return NGCPRealPrefs
|