diff options
Diffstat (limited to 'misc/freeswitch/scripts/common/gateway.lua')
-rw-r--r-- | misc/freeswitch/scripts/common/gateway.lua | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 09e8c4b..8ea6b04 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -274,3 +274,97 @@ function Gateway.parameters_build(self, gateway_id, technology) return parameters; end + + +function Gateway.headers_get(self, header_type, gateway_id) + gateway_id = gateway_id or self.id; + + local sql_query = 'SELECT * FROM `gateway_headers` WHERE `gateway_id` = ' .. tonumber(gateway_id) .. ' AND `header_type` = ' .. self.database:escape(header_type, '"'); + + local headers = {}; + self.database:query(sql_query, function(entry) + table.insert(headers, entry); + end) + + return headers; +end + + +function Gateway.constraint_match(self, constraints_str, variable_sets) + if common.str.blank(constraints_str) then + entry_match = true; + else + local constraints = common.str.strip_to_a(constraints_str, ',') + for constraint_index=1, #constraints do + local variable_name, pattern = common.str.partition(constraints[constraint_index], '!=') + local invert = variable_name ~= nil; + if not variable_name then + variable_name, pattern = common.str.partition(constraints[constraint_index], '=') + end + + if not common.str.blank(variable_name) and not common.str.blank(pattern) then + local search_string = common.array.expand_variable(variable_name, variable_sets); + if search_string ~= nil then + local success, result = pcall(string.find, tostring(search_string), pattern); + + entry_match = common.str.to_b(result); + + if invert then + entry_match = not entry_match; + end + + if entry_match == false then + break; + end + end + end + end + end + + return entry_match; +end + + +function Gateway.origination_variables(self, header_type, origination_variables, ...) + local dtmf = tostring(self.settings.dtmf_type):lower(); + if dtmf == 'inband' then + table.insert(origination_variables, "dtmf_type=none"); + elseif dtmf == 'none' or dtmf == 'info' then + table.insert(origination_variables, "dtmf_type=" .. dtmf); + else + table.insert(origination_variables, "dtmf_type=rfc2833"); + end + + local header_to_variable = { + default = { + default = 'sip_h_', + from = 'sip_full_from', + to = 'sip_full_to', + invite = 'sip_req_uri', + }, + invite = { + default = 'sip_h_', + from = 'sip_invite_full_from', + to = 'sip_invite_full_to', + invite = 'sip_invite_req_uri', + route = 'sip_invite_route_uri', + ['Record-Route'] = 'sip_invite_record_route', + } + } + + local variable_sets = {...}; + local headers = self:headers_get('default'); + for index, header in ipairs(self:headers_get(header_type)) do + table.insert(headers, header); + end + + for index, header in ipairs(headers) do + local search_string = common.array.expand_variable(header.constraint_source, variable_sets); + if common.str.blank(header.constraint_value) or self:constraint_match(header.constraint_value, variable_sets) then + if header_to_variable[header.header_type] then + local origination_variable = header_to_variable[header.header_type][header.name:lower()] or header_to_variable[header.header_type].default .. header.name; + table.insert(origination_variables, origination_variable .. "='" .. common.array.expand_variables(header.value, unpack(variable_sets)) .. "'"); + end + end + end +end |