summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/gateway.lua
blob: 14c19fb23831a975c96b97ee7a50256b95284ddb (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- 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;
  self.GATEWAY_PREFIX = 'gateway';
  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_id(self, id)
  local sql_query = 'SELECT `a`.*, `c`.`sip_host` `domain` \
    FROM `gateways` `a` \
    LEFT JOIN `gateway_settings` `b` ON `a`.`id` = `b`.`gateway_id` AND `b`.`name` = "inbound_username" \
    LEFT JOIN `sip_registrations` `c` ON `b`.`value` = `c`.`sip_user` \
    WHERE `a`.`id`= ' .. tonumber(id) .. ' LIMIT 1';

  local gateway = nil;
  self.database:query(sql_query, function(entry)
    require 'common.str';
    gateway = Gateway:new(self);
    gateway.record = entry;
    gateway.id = tonumber(entry.id);
    gateway.name = entry.name;
    gateway.technology = entry.technology;
    gateway.outbound = common.str.to_b(entry.outbound);
    gateway.inbound = common.str.to_b(entry.inbound);
  end)

  if gateway then
    gateway.settings = self:config_table_get('gateway_settings', gateway.id);
  end

  return gateway;
end


function Gateway.find_by_name(self, name)
  local gateway_name = name:gsub('([^%a%d%._%+])', '');

  local sql_query = 'SELECT `a`.*, `c`.`sip_host` `domain` \
    FROM `gateways` `a` \
    LEFT JOIN `gateway_settings` `b` ON `a`.`id` = `b`.`gateway_id` AND `b`.`name` = "inbound_username" \
    LEFT JOIN `sip_registrations` `c` ON `b`.`value` = `c`.`sip_user` \
    WHERE `a`.`name`= ' .. self.database:escape(gateway_name, '"') .. ' LIMIT 1';

  local gateway = nil;
  self.database:query(sql_query, function(entry)
    require 'common.str';
    gateway = Gateway:new(self);
    gateway.record = entry;
    gateway.id = tonumber(entry.id);
    gateway.name = entry.name;
    gateway.technology = entry.technology;
    gateway.outbound = common.str.to_b(entry.outbound);
    gateway.inbound = common.str.to_b(entry.inbound);
  end)

  if gateway then
    gateway.settings = self:config_table_get('gateway_settings', gateway.id);
  end

  return gateway;
end


function Gateway.find_by_auth_name(self, name)
  local auth_name = name:gsub('([^%a%d%._%+])', '');

  local sql_query = 'SELECT `c`.*, `a`.`value` `password`, `b`.`value` `username` \
    FROM `gateway_settings` `a` \
    INNER JOIN `gateway_settings` `b` \
    ON (`a`.`gateway_id` = `b`.`gateway_id` AND `a`.`name` = "inbound_password" AND `b`.`name` = "inbound_username" AND `b`.`value` = ' .. self.database:escape(auth_name, '"') .. ') \
    LEFT JOIN `gateways` `c` \
    ON (`a`.`gateway_id` = `c`.`id`) \
    WHERE `c`.`inbound` IS TRUE OR `c`.`outbound` IS TRUE LIMIT 1';

  local gateway = nil;
  self.database:query(sql_query, function(entry)
    require 'common.str';
    gateway = Gateway:new(self);
    gateway.record = entry;
    gateway.id = tonumber(entry.id);
    gateway.name = entry.name;
    gateway.technology = entry.technology;
    gateway.outbound = common.str.to_b(entry.outbound);
    gateway.inbound = common.str.to_b(entry.inbound);
  end)

  if gateway then
    gateway.settings = self:config_table_get('gateway_settings', gateway.id);
  end

  return gateway;
end


function Gateway.call_url(self, destination_number)
  require 'common.str';

  if self.technology == 'sip' then
    if self.settings.inbound_username and self.settings.inbound_password and not common.str.blank(self.record.domain) then
      return 'sofia/' .. (self.settings.profile or 'gemeinschaft') .. '/' .. self.settings.inbound_username .. '%' .. self.record.domain;
    else
      return 'sofia/gateway/' .. self.GATEWAY_PREFIX .. self.id .. '/' .. tostring(destination_number);
    end
    
  elseif self.technology == 'xmpp' then
    local destination_str = tostring(destination_number);
    if self.settings.destination_domain then
      destination_str = destination_str .. '@' .. self.settings.destination_domain;
    end
    return 'dingaling/' .. self.GATEWAY_PREFIX .. self.id .. '/' .. destination_str;
  end

  return '';
end


function Gateway.authenticate(self, caller, technology)
  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';

  if technology then
    sql_query = sql_query .. ' AND `c`.`technology` = "' .. tostring(technology) .. '"';
  end

  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, technology)
  local settings = self:config_table_get('gateway_settings', gateway_id);

  require 'common.str'
  local parameters = {};

  if technology == 'sip' then
    parameters.realm = settings.domain;
    parameters.extension = 'auto_to_user';
    
    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

    parameters['extension-in-contact'] = true;

    if common.str.blank(settings.contact) then
      parameters['extension'] = 'gateway' .. gateway_id;
    else
      parameters['extension'] = settings.contact;
    end
  elseif technology == 'xmpp' then
    parameters.message = 'Gemeinschaft 5 by AMOOMA';
    parameters.dialplan = 'XML';
    parameters.context = 'default';
    parameters['rtp-ip'] = 'auto';
    parameters['auto-login'] = 'true';
    parameters.sasl = 'plain';
    parameters.tls = 'true';
    parameters['use-rtp-timer'] = 'true';
    parameters.vad = 'both';
    parameters.use_jingle = 'true';
    parameters['candidate-acl'] = 'wan.auto';
    parameters.name = self.GATEWAY_PREFIX .. gateway_id;
    parameters.server = settings.server;
    parameters.login = settings.login;
    parameters.password = settings.password;
    parameters.exten = settings.inbound_number or parameters.name;
  end

  for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do 
    parameters[key] = value; 
  end

  return parameters;
end