1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
-- Gemeinschaft 5 module: gateway class
-- (c) AMOOMA GmbH 2013
--
module(...,package.seeall)
Gateway = {}
-- Create Gateway object
function Gateway.new(self, arg)
arg = arg or {}
object = arg.object or {}
setmetatable(object, self);
self.__index = self;
self.class = 'gateway';
self.log = arg.log;
self.database = arg.database;
self.record = arg.record;
return object;
end
function Gateway.list(self, technology)
technology = technology or 'sip';
local sql_query = 'SELECT * FROM `gateways` WHERE (`outbound` IS TRUE OR `inbound` IS TRUE) AND `technology` = "' .. technology .. '"';
local gateways = {};
self.database:query(sql_query, function(entry)
table.insert(gateways, entry);
end)
return gateways;
end
function Gateway.find_by_sql(self, where)
local sql_query = 'SELECT * FROM `gateways` WHERE ' .. where .. ' LIMIT 1';
local gateway = nil;
self.database:query(sql_query, function(entry)
gateway = Gateway:new(self);
gateway.record = entry;
gateway.id = tonumber(entry.id);
gateway.uuid = entry.uuid;
end)
return gateway;
end
-- find gateway by id
function Gateway.find_by_id(self, id)
local sql_query = '`id`= ' .. tonumber(id);
return self:find_by_sql(sql_query);
end
-- find gateway name
function Gateway.find_by_name(self, name)
local sql_query = '`name`= "' .. name .. '"';
return self:find_by_sql(sql_query);
end
function Gateway.authenticate(self, technology, caller)
local sql_query = 'SELECT `c`.`name`, `c`.`id`, `a`.`value` `auth_source`, `b`.`value` `auth_pattern` \
FROM `gateway_settings` `a` \
INNER JOIN `gateway_settings` `b` \
ON (`a`.`gateway_id` = `b`.`gateway_id` AND `a`.`name` = "auth_source" AND `b`.`name` = "auth_pattern" ) \
LEFT JOIN `gateways` `c` \
ON (`a`.`gateway_id` = `c`.`id`) \
WHERE `c`.`inbound` IS TRUE AND `c`.`technology` = "' .. tostring(technology) .. '"';
local gateway = false;
self.database:query(sql_query, function(entry)
if caller:to_s(entry.auth_source):match(entry.auth_pattern) then
gateway = entry;
return;
end
end)
return gateway;
end
function Gateway.profile_get(self, gateway_id)
local sql_query = 'SELECT `value` FROM `gateway_settings` WHERE `gateway_id` = ' .. tonumber(gateway_id) .. ' AND `name` = "profile" LIMIT 1';
return self.database:query_return_value(sql_query);
end
function Gateway.config_table_get(self, config_table, gateway_id)
require 'common.str'
local sql_query = 'SELECT * FROM `'.. config_table ..'` WHERE `gateway_id` = ' .. tonumber(gateway_id);
local settings = {};
self.database:query(sql_query, function(entry)
local p_class_type = common.str.strip(entry.class_type):lower();
local p_name = common.str.strip(entry.name):lower();
if p_class_type == 'boolean' then
settings[p_name] = common.str.to_b(entry.value);
elseif p_class_type == 'integer' then
settings[p_name] = common.str.to_i(entry.value);
else
settings[p_name] = tostring(entry.value);
end
end)
return settings
end
function Gateway.parameters_build(self, gateway_id)
local settings = self:config_table_get('gateway_settings', gateway_id);
local parameters = {
realm = settings.domain,
extension = 'auto_to_user',
};
require 'common.str'
if common.str.blank(settings.username) then
parameters.username = 'gateway' .. gateway_id;
parameters.register = false;
else
parameters.username = settings.username;
parameters.register = true;
end
if not common.str.blank(settings.register) then
parameters.register = common.str.to_b(settings.register);
end
if common.str.blank(settings.password) then
parameters.password = 'gateway' .. gateway_id;
else
parameters.password = settings.password;
end
for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do
parameters[key] = value;
end
return parameters;
end
|