summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/configuration.lua
blob: 3ef1a9c70444aa987f162e23ebc97186ef0f6a9b (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
-- Gemeinschaft 5 dynamic freeswitch configuration
-- (c) AMOOMA GmbH 2012
-- 

function nodes(database, local_node_id)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  local gateways_xml = '';

  require 'common.node'
  for node_id, node_record in pairs(common.node.Node:new{log=log, database=database}:all()) do
    if node_id ~= local_node_id then
      local node_parameters = {}
      node_parameters['username'] = node_record.name;
      node_parameters['password'] = 'gemeinschaft';
      node_parameters['proxy'] = node_record.ip_address;
      node_parameters['register'] = 'false';
      log:debug('NODE_GATEWAY ', node_record.id, ' - name: ', node_record.name, ', address: ', node_record.ip_address);
      gateways_xml = gateways_xml .. xml:element{
        'gateway',
        name = node_record.name,
        xml:from_hash('param', node_parameters, 'name', 'value'),
      };
    end
  end

  return gateways_xml;
end

function gateways(profile_name)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  require 'common.configuration_file'
  local gateways_xml = '';
  local gateways  = common.configuration_file.get('/opt/freeswitch/scripts/ini/gateways.ini', false);

  if not gateways then
    return '';
  end

  for gateway_name, gateway_parameters in pairs(gateways) do
    if tostring(gateway_parameters.profile) == profile_name then
      log:debug('GATEWAY - name: ', gateway_name, ', address: ', gateway_parameters.proxy);
      gateways_xml = gateways_xml .. xml:element{
        'gateway',
        name = gateway_name,
        xml:from_hash('param', gateway_parameters, 'name', 'value'),
      };
    end
  end

  return gateways_xml;
end

function profile(database, sofia_ini, profile_name, index, domains, node_id)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  local parameters = sofia_ini['profile:' .. profile_name];

  if not parameters then
    log:error('SOFIA_PROFILE ', index,' - name: ', profile_name, ' - no parameters');
    return '';
  end

  if tostring(parameters['odbc-dsn']) == 'default' then
    parameters['odbc-dsn'] = 'gemeinschaft:' .. tostring(database.user_name) .. ':' .. tostring(database.password);
  end

  -- set local bind address
  if domains[index] then
    parameters['sip-ip'] = domains[index]['host'];
    parameters['rtp-ip'] = domains[index]['host'];
    parameters['force-register-domain'] = domains[index]['host'];
    parameters['force-subscription-domain'] = domains[index]['host'];
    parameters['force-register-db-domain'] = domains[index]['host'];
    log:debug('SOFIA_PROFILE ', index,' - name: ', profile_name, ', domain: ', domains[index]['host'], ',  sip_bind: ', parameters['sip-ip'], ':', parameters['sip-port']);
  else
    log:debug('SOFIA_PROFILE ', index,' - name: ', profile_name, ' - no domains');
  end

  local gateways_xml = gateways(profile_name);

  if index == 1 then
    gateways_xml = gateways_xml .. nodes(database, node_id);
  end

  local profile_xml = xml:element{
    'profile',
    name = profile_name,
    xml:element{
      'gateways',
      gateways_xml,
    },
    xml:element{
      'domains',
      xml:element{
        'domain',
        name = 'all',
        alias = 'true',
        parse = 'false',
      },
    },
    xml:element{
      'settings',
      xml:from_hash('param', parameters, 'name', 'value'),
    },
  };

  return profile_xml;
end


-- generate sofia.conf
function conf_sofia(database)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  require 'common.configuration_table'
  local sofia_profile = "gemeinschaft";

  local sofia_ini = common.configuration_table.get(database, 'sofia');
  local dialplan_parameters = common.configuration_table.get(database, 'dialplan', 'parameters');

  local local_node_id = tonumber(dialplan_parameters.node_id) or 1;
  
  require 'configuration.sip'
  local domains = configuration.sip.Sip:new{ log = log, database = database}:domains();

  sofia_profiles_xml = '';
  for profile_name, index in pairs(sofia_ini.profiles) do
    if tonumber(index) and tonumber(index) > 0  then
      sofia_profiles_xml = sofia_profiles_xml .. profile(database, sofia_ini, profile_name, tonumber(index), domains, local_node_id);
    end
  end

  XML_STRING = xml:element{
    'document', 
    ['type'] = 'freeswitch/xml',
    xml:element{
      'section',
      name = 'configuration',
      description = 'Gemeinschaft 5 FreeSWITCH configuration',
      xml:element{
        'configuration',
        name = 'sofia.conf',
        description = 'Sofia configuration',
        xml:element{
          'global_settings',
          xml:from_hash('param', sofia_ini.parameters, 'name', 'value'),
        },
        xml:element{
          'profiles',
          sofia_profiles_xml,
        },
      },
    },
  };

end

function conf_conference(database)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  require 'common.configuration_table'
  local config = common.configuration_table.get(database, 'conferences');
  local profiles = nil;

  local event_name = params:getHeader("Event-Name")
  if event_name == 'COMMAND' then
    local conf_name    = params:getHeader('conf_name');
    local profile_name = params:getHeader('profile_name');

    if conf_name then
      require 'common.conference'
      conference = common.conference.Conference:new{log=log, database=database}:find_by_id(conf_name);
      if conference then
        log:debug('CONFIG_CONFERENCE ', conf_name, ' name: ', conference.record.name, ', profile: ', profile_name);
        config.parameters['caller-id-name'] = conference.record.name or '';
        profiles = xml:element{
          'profiles',
          xml:element{
            'profile',
            name = profile_name,
            xml:from_hash('param', config.parameters, 'name', 'value'),
          },
        };
      else
        log:error('CONFIG_CONFERENCE ', conf_name, ' - conference not found');
      end
    else
      log:notice('CONFIG_CONFERENCE - no conference name');
    end
  else
    log:debug('CONFIG_CONFERENCE ', conf_name, ' - event: ', event_name);
  end

  XML_STRING = xml:element{
    'document', 
    ['type'] = 'freeswitch/xml',
    xml:element{
      'section',
      name = 'configuration',
      description = 'Gemeinschaft 5 FreeSWITCH configuration',
      xml:element{
        'configuration',
        name = 'conference.conf',
        description = 'Conference configuration',
        xml:element{
          'caller-controls',
          xml:element{
            'group',
            name = 'speaker',
            xml:from_hash('control', config.controls_speaker, 'action', 'digits'),
          },
          xml:element{
            'group',
            name = 'moderator',
            xml:from_hash('control', config.controls_moderator, 'action', 'digits'),
          },
        },
        profiles,
      },
    },
  };
end

function conf_voicemail(database)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  require 'common.configuration_table';
  local parameters = common.configuration_table.get(database, 'voicemail', 'parameters');

  if tostring(parameters['odbc-dsn']) == 'default' then
    parameters['odbc-dsn'] = 'gemeinschaft:' .. tostring(database.user_name) .. ':' .. tostring(database.password);
  end

  XML_STRING = xml:element{
    'document', 
    ['type'] = 'freeswitch/xml',
    xml:element{
      'section',
      name = 'configuration',
      description = 'Gemeinschaft 5 FreeSWITCH configuration',
      xml:element{
        'configuration',
        name = 'voicemail.conf',
        description = 'Voicemail configuration',
        xml:element{
          'profiles',
          xml:element{
            'profile',
            name = 'default',
            xml:from_hash('param', parameters, 'name', 'value'),
          },
        },
      },
    },
  };
end

function conf_post_switch(database)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  require 'common.configuration_table';
  local parameters = common.configuration_table.get(database, 'post_load_switch', 'settings');

  XML_STRING = xml:element{
    'document', 
    ['type'] = 'freeswitch/xml',
    xml:element{
      'section',
      name = 'configuration',
      description = 'Gemeinschaft 5 FreeSWITCH configuration',
      xml:element{
        'configuration',
        name = 'post_load_switch.conf',
        description = 'Switch configuration',
        xml:element{
          'settings',
          xml:from_hash('param', parameters, 'name', 'value'),
        },
      },
    },
  };
end


function directory_sip_account(database)
  require 'configuration.simple_xml'
  local xml = configuration.simple_xml.SimpleXml:new();

  local key       = params:getHeader('key');
  local auth_name = params:getHeader('user');
  local domain    = params:getHeader('domain');
  local purpose   = params:getHeader('purpose');

  local user_xml = nil;

  if auth_name and  auth_name ~= '' then
    -- sip account or gateway
    if string.len(auth_name) > 3 and auth_name:sub(1, 3) == 'gw+' then
      local gateway_name = auth_name:sub(4);
      domain = domain or freeswitch.API():execute('global_getvar', 'domain');
      require 'configuration.sip'
      log:notice('DATABASE: ', database);
      local sip_gateway = configuration.sip.Sip:new{ log = log, database = database}:find_gateway_by_name(gateway_name);
      if sip_gateway ~= nil and next(sip_gateway) ~= nil then
        log:debug('DIRECTORY_GATEWAY - name: ', gateway_name, ', auth_name: ', auth_name);

        local user_variables = {
          user_context = "default",
          gs_from_gateway = "true",
          gs_gateway_name = gateway_name,
          gs_gateway_id   = sip_gateway.id,
        }

        user_xml = xml:element{
          'user',
          id = auth_name,
          xml:element{
            'params',
            xml:element{
              'param',
              password = sip_gateway.password,
            }
          },
          xml:element{
            'variables',
            xml:from_hash('variable', user_variables, 'name', 'value'),
          },
        };
      else
        log:debug('DIRECTORY_GATEWAY - gateway not found - name: ', gateway_name, ', auth_name: ', auth_name);
      end
    else
      require 'common.sip_account'
      local sip_account = common.sip_account.SipAccount:new{ log = log, database = database}:find_by_auth_name(auth_name, domain);

      require 'common.configuration_table'
      local user_parameters = common.configuration_table.get(database, 'sip_accounts', 'parameters');
            
      if sip_account ~= nil then
        user_parameters['password'] = sip_account.record.password;
        user_parameters['vm-password'] = sip_account.record.voicemail_pin;

        local user_variables = {
          user_context           = "default",
          gs_from_gateway        = "false",
          gs_account_id          = sip_account.record.id,
          gs_account_uuid        = sip_account.record.uuid,
          gs_account_type        = "SipAccount",
          gs_account_state       = sip_account.record.state,
          gs_account_caller_name = sip_account.record.caller_name,
          gs_account_owner_type  = sip_account.record.sip_accountable_type,
          gs_account_owner_id    = sip_account.record.sip_accountable_id    
        }

        if tostring(purpose) == 'publish-vm' then
          log:debug('DIRECTORY_SIP_ACCOUNT - purpose: VoiceMail, auth_name: ', sip_account.record.auth_name, ', caller_name: ', sip_account.record.caller_name, ', domain: ', domain);
          user_xml = xml:element{
            'groups',
            xml:element{
              'group',
              name = 'default',
              xml:element{
                'users',
                xml:element{  
                  'user',
                  id = sip_account.record.auth_name,
                  xml:element{
                    'params',
                    xml:from_hash('param', user_parameters, 'name', 'value'),
                  },
                  xml:element{
                    'variables',
                    xml:from_hash('variable', user_variables, 'name', 'value'),
                  },
                },
              },
            },
          };
        else
          log:debug('DIRECTORY_SIP_ACCOUNT - auth_name: ', sip_account.record.auth_name, ', caller_name: ', sip_account.record.caller_name, ', domain: ', domain);
          
          user_xml = xml:element{
            'user',
            id = sip_account.record.auth_name,
            xml:element{
              'params',
              xml:from_hash('param', user_parameters, 'name', 'value'),
            },
            xml:element{
              'variables',
              xml:from_hash('variable', user_variables, 'name', 'value'),
            },
          };
        end
      else
        log:debug('DIRECTORY_SIP_ACCOUNT - sip account not found - auth_name: ', auth_name, ', domain: ', domain);
        -- fake sip_account configuration
        user_parameters['password'] = tostring(math.random(0, 65534));
        user_parameters['vm-password'] = '';

        user_xml = xml:element{
          'user',
          id = auth_name,
          xml:element{
            'params',
            xml:from_hash('param', user_parameters, 'name', 'value'),
          },
        };
      end
    end
  elseif tostring(XML_REQUEST.key_name) == 'name' and tostring(XML_REQUEST.key_value) ~= '' then
    log:debug('DOMAIN_DIRECTORY - domain: ', XML_REQUEST.key_value);
    XML_STRING = xml:document(xml:directory(nil, XML_REQUEST.key_value));
  end

  XML_STRING = xml:element{
    'document', 
    ['type'] = 'freeswitch/xml',
    xml:element{
      'section',
      name = 'directory',
      xml:element{
        'domain',
        name = domain,
        xml:element{
          'params',
          xml:element{
            'param',
            name = 'dial-string',
            value = '${sofia_contact(${dialed_user}@${dialed_domain})}',
          },
        },
        user_xml,
      },
    },
  };  
end


local log_identifier = XML_REQUEST.key_value or 'CONFIG';

-- set logger
require 'common.log'
log = common.log.Log:new();
log.prefix = '#C# [' .. log_identifier .. '] ';

-- return a valid xml document
require 'configuration.simple_xml'
local xml = configuration.simple_xml.SimpleXml:new();
XML_STRING = xml:element{
  'document', 
  ['type'] = 'freeswitch/xml',
};

local database = nil;

-- log:debug('CONFIG_REQUEST section: ', XML_REQUEST.section, ', tag: ', XML_REQUEST.tag_name, ', key: ', XML_REQUEST.key_value);

if XML_REQUEST.section == 'configuration' and XML_REQUEST.tag_name == 'configuration' then
  -- database connection
  require 'common.database'
  database = common.database.Database:new{ log = log }:connect();
  if database:connected() == false then
    log:error('CONFIG_REQUEST - cannot connect to Gemeinschaft database');
    return false;
  end

  if XML_REQUEST.key_value == 'sofia.conf' then
    conf_sofia(database);
  elseif XML_REQUEST.key_value == "conference.conf" then
    conf_conference(database);
  elseif XML_REQUEST.key_value == "voicemail.conf" then
    conf_voicemail(database);
  elseif XML_REQUEST.key_value == "post_load_switch.conf" then
    conf_post_switch(database);
  end
elseif XML_REQUEST.section == 'directory' and XML_REQUEST.tag_name == '' then
  log:debug('SIP_ACCOUNT_DIRECTORY - initialization phase');
elseif XML_REQUEST.section == 'directory' and XML_REQUEST.tag_name == 'domain' then
  if params == nil then
    log:error('SIP_ACCOUNT_DIRECTORY - no parameters');
    return false;
  end
 
  require 'common.database'
  database = common.database.Database:new{ log = log }:connect();
  if not database:connected() then
    log:error('CONFIG_REQUEST - cannot connect to Gemeinschaft database');
    return false;
  end
  directory_sip_account(database);
else
  log:error('CONFIG_REQUEST - no configuration handler, section: ', XML_REQUEST.section, ', tag: ', XML_REQUEST.tag_name);
end

-- ensure database handler is released on exit
if database then
  database:release();
end