summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common
diff options
context:
space:
mode:
Diffstat (limited to 'misc/freeswitch/scripts/common')
-rw-r--r--misc/freeswitch/scripts/common/call_forwarding.lua2
-rw-r--r--misc/freeswitch/scripts/common/call_history.lua2
-rw-r--r--misc/freeswitch/scripts/common/conference.lua6
-rw-r--r--misc/freeswitch/scripts/common/configuration_file.lua2
-rw-r--r--misc/freeswitch/scripts/common/configuration_table.lua46
-rw-r--r--misc/freeswitch/scripts/common/database.lua123
-rw-r--r--misc/freeswitch/scripts/common/fapi.lua2
-rw-r--r--misc/freeswitch/scripts/common/gateway.lua168
-rw-r--r--misc/freeswitch/scripts/common/ipcalc.lua2
-rw-r--r--misc/freeswitch/scripts/common/log.lua2
-rw-r--r--misc/freeswitch/scripts/common/phone_number.lua2
-rw-r--r--misc/freeswitch/scripts/common/routing_tables.lua66
-rw-r--r--misc/freeswitch/scripts/common/sip_account.lua2
-rw-r--r--misc/freeswitch/scripts/common/str.lua24
-rw-r--r--misc/freeswitch/scripts/common/sync_log.lua2
15 files changed, 258 insertions, 193 deletions
diff --git a/misc/freeswitch/scripts/common/call_forwarding.lua b/misc/freeswitch/scripts/common/call_forwarding.lua
index 3942d05..400fcde 100644
--- a/misc/freeswitch/scripts/common/call_forwarding.lua
+++ b/misc/freeswitch/scripts/common/call_forwarding.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: call forwarding class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/call_history.lua b/misc/freeswitch/scripts/common/call_history.lua
index 7a9ac07..7e1e22b 100644
--- a/misc/freeswitch/scripts/common/call_history.lua
+++ b/misc/freeswitch/scripts/common/call_history.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: call_history class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/conference.lua b/misc/freeswitch/scripts/common/conference.lua
index d2bf829..ca5fa62 100644
--- a/misc/freeswitch/scripts/common/conference.lua
+++ b/misc/freeswitch/scripts/common/conference.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: conference class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
@@ -103,8 +103,9 @@ function Conference.enter(self, caller, domain)
end;
end
+ require 'common.str'
-- Check if conference is within time frame
- if self.record.start and self.record['end'] then
+ if not common.str.blank(self.record.start) and not common.str.blank(self.record['end']) then
local d = {}
_,_,d.year,d.month,d.day,d.hour,d.min,d.sec=string.find(self.record.start, "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)");
@@ -122,7 +123,6 @@ function Conference.enter(self, caller, domain)
end
end
- require 'common.str'
-- Owner ist always moderator
if (tonumber(self.record.conferenceable_id) == caller.account_owner_id) and (self.record.conferenceable_type == caller.account_owner_type) then
table.insert(flags, 'moderator');
diff --git a/misc/freeswitch/scripts/common/configuration_file.lua b/misc/freeswitch/scripts/common/configuration_file.lua
index 67e1f3b..3b3efbc 100644
--- a/misc/freeswitch/scripts/common/configuration_file.lua
+++ b/misc/freeswitch/scripts/common/configuration_file.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: configuration file
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/configuration_table.lua b/misc/freeswitch/scripts/common/configuration_table.lua
new file mode 100644
index 0000000..85bc014
--- /dev/null
+++ b/misc/freeswitch/scripts/common/configuration_table.lua
@@ -0,0 +1,46 @@
+-- Gemeinschaft 5 module: configuration table
+-- (c) AMOOMA GmbH 2013
+--
+
+module(...,package.seeall)
+
+-- retrieve configuration from database
+function get(database, entity, section)
+ if not database or not entity then
+ return {};
+ end
+
+ require 'common.str'
+
+ local sql_query = 'SELECT * FROM `gs_parameters` WHERE `entity` = "' .. entity .. '"';
+ if section then
+ sql_query = sql_query .. ' AND `section` = "' .. section .. '"';
+ end
+
+ local root = {}
+ local parameter_class = '';
+
+ database:query(sql_query, function(parameters)
+ local p_section = common.str.strip(parameters.section):lower();
+ local p_class_type = common.str.strip(parameters.class_type):lower();
+ local p_name = common.str.strip(parameters.name);
+
+ if not root[p_section] then
+ root[p_section] = {};
+ end
+
+ if p_class_type == 'boolean' then
+ root[p_section][p_name] = common.str.to_b(parameters.value);
+ elseif p_class_type == 'integer' then
+ root[p_section][p_name] = common.str.to_i(parameters.value);
+ else
+ root[p_section][p_name] = tostring(parameters.value);
+ end
+ end)
+
+ if section then
+ return root[section];
+ end
+
+ return root;
+end
diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua
index 3692f84..1f39135 100644
--- a/misc/freeswitch/scripts/common/database.lua
+++ b/misc/freeswitch/scripts/common/database.lua
@@ -1,12 +1,12 @@
-- Gemeinschaft 5 module: database class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2013
--
module(...,package.seeall)
Database = {}
-DATABASE_DRIVER = 'mysql'
+DATABASE_DSN = 'gemeinschaft';
function Database.new(self, arg)
arg = arg or {}
@@ -20,117 +20,49 @@ function Database.new(self, arg)
end
-function Database.connect(self, database_name, user_name, password, host_name)
- local database_driver = nil;
- if not (database_name and user_name and password) then
- require 'common.configuration_file'
- local config = common.configuration_file.get('/opt/freeswitch/scripts/ini/database.ini');
- if config then
- database_driver = config[true].driver
- database_name = config[database_driver].database
- user_name = config[database_driver].user
- password = config[database_driver].password
- host_name = config[database_driver].host
- end
- end
+function Database.connect(self)
+ self.dsn = DATABASE_DSN;
- host_name = host_name or 'localhost';
- database_driver = database_driver or DATABASE_DRIVER;
+ require 'common.configuration_file'
+ local dsn = common.configuration_file.get('/var/lib/freeswitch/.odbc.ini', self.dsn);
- if database_driver == 'mysql' then
- require "luasql.mysql"
- self.env = luasql.mysql();
- elseif database_driver == 'odbc' then
- require "luasql.odbc"
- self.env = luasql.odbc();
- end
+ self.database_name = dsn.DATABASE;
+ self.user_name = dsn.USER;
+ self.password = dsn.PASSWORD;
+ self.host_name = dsn.HOST;
- self.conn = self.env:connect(database_name, user_name, password, host_name);
+ self.conn = freeswitch.Dbh(self.dsn, self.user_name, self.password);
self.conn_id = tostring(self.conn);
- self.database_name = database_name;
- self.user_name = user_name;
- self.password = password;
- self.host_name = host_name;
-
- -- self.log:debug('DATABASE_CONNECT - connection: ', self.conn_id, ', environment: ', self.env);
-
- return self;
-end
-
-
-function Database.reconnect(self)
- self.conn = self.env:connect(self.database_name, self.user_name, self.password, self.host_name);
- self.conn_id = tostring(self.conn);
-
- if self.log then
- self.log:info('DATABASE_RECONNECT - connection: ', self.conn_id, ', environment: ', self.env);
- end
return self;
end
function Database.connected(self)
- return self.conn;
+ return self.conn:connected();
end
function Database.query(self, sql_query, call_function)
- local cursor = self.conn:execute(sql_query);
-
- if cursor == nil and not self.conn:execute('SELECT @@VERSION') then
- if self.log then
- self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query);
- end
- self:reconnect();
-
- if call_function then
- cursor = self.conn:execute(sql_query);
- self.log:notice('DATABASE_QUERY - retry: ', sql_query);
- end
- end
-
- if cursor and call_function then
- repeat
- row = cursor:fetch({}, 'a');
- if row then
- call_function(row);
- end
- until not row;
+ if call_function then
+ return self.conn:query(sql_query, call_function);
+ else
+ return self.conn:query(sql_query);
end
-
- if type(cursor) == 'userdata' then
- cursor:close();
- end
-
- return cursor;
end
function Database.query_return_value(self, sql_query)
- local cursor = self.conn:execute(sql_query);
+ local result = nil;
- if cursor == nil and not self.conn:execute('SELECT @@VERSION') then
- if self.log then
- self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query);
+ self.conn:query(sql_query, function(row)
+ for key, value in pairs(row) do
+ result = value;
+ return result;
end
- self:reconnect();
- cursor = self.conn:execute(sql_query);
- self.log:notice('DATABASE_QUERY - retry: ', sql_query);
- end
-
- if type(cursor) == 'userdata' then
- local row = cursor:fetch({}, 'n');
- cursor:close();
-
- if not row then
- return row;
- else
- return row[1];
- end
- end
+ end)
- return cursor;
+ return result;
end
@@ -139,13 +71,8 @@ function Database.last_insert_id(self)
end
-function Database.release(self, sql_query, call_function)
+function Database.release(self)
if self.conn then
- self.conn:close();
- end
- if self.env then
- self.env:close();
+ self.conn:release();
end
-
- -- self.log:debug('DATABASE_RELEASE - connection: ', self.conn_id, ', status: ', self.env, ', ', self.conn);
end
diff --git a/misc/freeswitch/scripts/common/fapi.lua b/misc/freeswitch/scripts/common/fapi.lua
index 0a05155..5b96633 100644
--- a/misc/freeswitch/scripts/common/fapi.lua
+++ b/misc/freeswitch/scripts/common/fapi.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: FS api class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua
new file mode 100644
index 0000000..e50c763
--- /dev/null
+++ b/misc/freeswitch/scripts/common/gateway.lua
@@ -0,0 +1,168 @@
+-- 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 * FROM `gateways` WHERE `id`= ' .. tonumber(id) .. ' 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.name = entry.name;
+ 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 * FROM `gateways` WHERE `name`= "' .. gateway_name .. '" 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.name = entry.name;
+ end)
+
+ if gateway then
+ gateway.settings = self:config_table_get('gateway_settings', gateway.id);
+ end
+
+ return gateway;
+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
+
+ parameters['extension-in-contact'] = true;
+
+ if common.str.blank(settings.contact) then
+ parameters['extension'] = 'gateway' .. gateway_id;
+ else
+ parameters['extension'] = settings.contact;
+ end
+
+ for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do
+ parameters[key] = value;
+ end
+
+ return parameters;
+end
diff --git a/misc/freeswitch/scripts/common/ipcalc.lua b/misc/freeswitch/scripts/common/ipcalc.lua
index 5c19d20..49cb56c 100644
--- a/misc/freeswitch/scripts/common/ipcalc.lua
+++ b/misc/freeswitch/scripts/common/ipcalc.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: ip calculation functions
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/log.lua b/misc/freeswitch/scripts/common/log.lua
index d0d13dc..5aff2b8 100644
--- a/misc/freeswitch/scripts/common/log.lua
+++ b/misc/freeswitch/scripts/common/log.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: log
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/phone_number.lua b/misc/freeswitch/scripts/common/phone_number.lua
index 4df0d57..6635296 100644
--- a/misc/freeswitch/scripts/common/phone_number.lua
+++ b/misc/freeswitch/scripts/common/phone_number.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: phone number class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/routing_tables.lua b/misc/freeswitch/scripts/common/routing_tables.lua
deleted file mode 100644
index 34d0143..0000000
--- a/misc/freeswitch/scripts/common/routing_tables.lua
+++ /dev/null
@@ -1,66 +0,0 @@
--- Gemeinschaft 5 module: routing table functions
--- (c) AMOOMA GmbH 2012
---
-
-module(...,package.seeall)
-
-function expand_variables(line, variables_list)
- variables_list = variables_list or {};
-
- return (line:gsub('{([%a%d_]+)}', function(captured)
- return variables_list[captured] or '';
- end))
-end
-
-
-function match_route(entry, search_str, variables_list)
- if not entry or not search_str then
- return { error = 'No input values' };
- end
-
- local result = nil;
- local success = nil;
- success, result = pcall(string.find, search_str, entry[1]);
-
- if not success then
- return { error = result, line = line }
- elseif result then
- local route = {
- pattern = entry[1],
- value = search_str:gsub(entry[1], expand_variables(entry[#entry], variables_list)),
- }
-
- for index = 2, #entry-1 do
- local attribute = entry[index]:match('^(.-)%s*=');
- if attribute then
- route[attribute] = entry[index]:match('=%s*(.-)$');
- end
- end
-
- return route;
- end
-
- return {};
-end
-
-
-function match_caller_id(entry, search_str, variables_list)
- if not entry or not search_str then
- return { error = 'No input values' };
- end
- local result = nil;
- local success = nil;
- success, result = pcall(string.find, search_str, entry[1]);
- if not success then
- return { error = result, line = line }
- elseif result then
- return {
- value = search_str:gsub(entry[1], expand_variables(entry[4], variables_list)),
- class = entry[2],
- endpoint = entry[3],
- pattern = entry[1],
- }
- end
-
- return {};
-end
diff --git a/misc/freeswitch/scripts/common/sip_account.lua b/misc/freeswitch/scripts/common/sip_account.lua
index 28a00df..8dd432b 100644
--- a/misc/freeswitch/scripts/common/sip_account.lua
+++ b/misc/freeswitch/scripts/common/sip_account.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: sip account class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
diff --git a/misc/freeswitch/scripts/common/str.lua b/misc/freeswitch/scripts/common/str.lua
index ca6dcd9..32f054e 100644
--- a/misc/freeswitch/scripts/common/str.lua
+++ b/misc/freeswitch/scripts/common/str.lua
@@ -1,27 +1,17 @@
-- Gemeinschaft 5 module: string functions
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
function try(array, arguments)
- local argument = arguments:match('^(.-)%.') or arguments;
- local remaining_arguments = arguments:match('%.(.-)$');
- argument = tonumber(argument) or argument;
+ local result = array;
- if argument and type(array) == 'table' then
- if remaining_arguments then
- if type(array[argument]) == 'table' then
- return try(array[argument], remaining_arguments);
- else
- return nil;
- end
- else
- return array[argument];
- end
- end
-
- return nil;
+ arguments:gsub('([^%.]+)', function(entry)
+ local success, result = pcall(function() result = (result[tonumber(entry) or entry]); end);
+ end);
+
+ return result;
end
-- to number
diff --git a/misc/freeswitch/scripts/common/sync_log.lua b/misc/freeswitch/scripts/common/sync_log.lua
index 05b0dcf..3fdb646 100644
--- a/misc/freeswitch/scripts/common/sync_log.lua
+++ b/misc/freeswitch/scripts/common/sync_log.lua
@@ -1,5 +1,5 @@
-- Gemeinschaft 5 module: sync log class
--- (c) AMOOMA GmbH 2012
+-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)