summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/configuration.lua
blob: 906d3f834516e09e2f7ba27f7bf1f3fc25c1c5b7 (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
-- Gemeinschaft 5 dynamic freeswitch configuration
-- (c) AMOOMA GmbH 2012
-- 

function nodes(database, local_node_id)
  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:gateway(node_record.name, node_parameters);
    end
  end

  return gateways_xml;
end

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

  if not gateways then
    return '';
  end

  for sofia_gateway, gateway_parameters in pairs(gateways) do
    if tostring(gateway_parameters.profile) == profile_name then
      log:debug('GATEWAY - name: ', sofia_gateway, ', address: ', gateway_parameters.proxy);
      gateways_xml = gateways_xml .. xml:gateway(sofia_gateway, gateway_parameters);
    end
  end

  return gateways_xml;
end

function profile(database, sofia_ini, profile_name, index, domains, node_id)
  local profile_parameters = sofia_ini['profile:' .. profile_name];

  if not profile_parameters then
    log:error('SOFIA_PROFILE ', index,' - name: ', profile_name, ' - no parameters');
    return '';
  end
  -- set local bind address
  if domains[index] then
    profile_parameters['sip-ip'] = domains[index]['host'];
    profile_parameters['rtp-ip'] = domains[index]['host'];
    profile_parameters['force-register-domain'] = domains[index]['host'];
    profile_parameters['force-subscription-domain'] = domains[index]['host'];
    profile_parameters['force-register-db-domain'] = domains[index]['host'];
    log:debug('SOFIA_PROFILE ', index,' - name: ', profile_name, ', domain: ', domains[index]['host'], ',  sip_bind: ', profile_parameters['sip-ip'], ':', profile_parameters['sip-port']);
  else
    log:error('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

  return xml:sofia_profile(profile_name, profile_parameters, gateways_xml);
end

-- generate sofia.conf
function conf_sofia(database)
  local sofia_profile = "gemeinschaft";

  require 'common.configuration_file'
  local sofia_ini = common.configuration_file.get('/opt/freeswitch/scripts/ini/sofia.ini');
  local dialplan_parameters = common.configuration_file.get('/opt/freeswitch/scripts/ini/dialplan.ini', '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 index, profile_name in ipairs(sofia_ini.profiles) do
    sofia_profiles_xml = sofia_profiles_xml .. profile(database, sofia_ini, profile_name, index, domains, local_node_id);
  end

  XML_STRING = xml:document(xml:sofia(sofia_ini.parameters, sofia_profiles_xml))
end

function conf_conference(database)
  XML_STRING = xml:document(xml:conference());

  require 'common.configuration_file'
  local conference_ini = common.configuration_file.get('/opt/freeswitch/scripts/ini/conferences.ini');
  local conference_parameters = conference_ini.parameters;

  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);
        conference_parameters['caller-id-name'] = conference.record.name or '';
        XML_STRING = xml:document(xml:conference(xml:conference_profile(profile_name, conference_parameters)));
      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
end


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

  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);
        XML_STRING = xml:document(xml:directory(xml:gateway_user(sip_gateway, gateway_name, auth_name), domain));
      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);
      if sip_account ~= nil then
        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);
          XML_STRING = xml:document(xml:directory(xml:group_default(xml:user(sip_account.record)), domain));
        else
          log:debug('DIRECTORY_SIP_ACCOUNT - auth_name: ', sip_account.record.auth_name, ', caller_name: ', sip_account.record.caller_name, ', domain: ', domain);
          XML_STRING = xml:document(xml:directory(xml:user(sip_account.record), domain));
        end
      else
        log:debug('DIRECTORY_SIP_ACCOUNT - sip account not found - auth_name: ', auth_name, ', domain: ', domain);
        -- fake sip_account configuration
        sip_account = {
              auth_name             = auth_name,
              id                    = 0,
              uuid                  = '',
              password              = tostring(math.random(0, 65534)),
              voicemail_pin         = '',
              state                 = 'inactive',
              caller_name           = '',
              sip_accountable_type  = 'none',
              sip_accountable_id    = 0,   
        }
        XML_STRING = xml:document(xml:directory(xml:user(sip_account), domain))
      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
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.freeswitch_xml'
xml = configuration.freeswitch_xml.FreeSwitchXml:new();
XML_STRING = xml:document();

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);
  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