2013-07-24 21:54:17 +02:00
-- Part of FusionPBX
2015-05-05 09:12:44 +02:00
-- Copyright (C) 2010-2015 Mark J Crane <markjcrane@fusionpbx.com>
2013-07-24 21:54:17 +02:00
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- 1. Redistributions of source code must retain the above copyright notice,
2014-07-13 11:24:51 +02:00
-- this list of conditions and the following disclaimer.
2013-07-24 21:54:17 +02:00
--
2014-08-13 12:51:51 +02:00
-- 2. Redistributions in binary form must repoduce the above copyright
2014-07-13 11:24:51 +02:00
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
2013-07-24 21:54:17 +02:00
--
2015-09-27 08:52:47 +02:00
-- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2013-07-24 21:54:17 +02:00
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
2013-12-04 09:14:40 +01:00
--
-- Contributor(s):
-- Mark J Crane <markjcrane@fusionpbx.com>
2014-06-22 20:38:46 +02:00
-- Luis Daniel Lucio Qurioz <dlucio@okay.com.mx>
2013-12-04 09:14:40 +01:00
2015-09-22 17:33:41 +02:00
local log = require " resources.functions.log " . ring_group
2013-07-24 21:54:17 +02:00
--connect to the database
2015-08-11 04:06:33 +02:00
require " resources.functions.database_handle " ;
2013-07-24 21:54:17 +02:00
dbh = database_handle ( ' system ' ) ;
--include functions
2015-08-11 04:06:33 +02:00
require " resources.functions.trim " ;
require " resources.functions.explode " ;
2015-09-15 16:49:37 +02:00
require " resources.functions.base64 " ;
require " resources.functions.file_exists " ;
2015-10-20 14:15:42 +02:00
require " resources.functions.channel_utils "
2013-07-24 21:54:17 +02:00
--get the variables
domain_name = session : getVariable ( " domain_name " ) ;
ring_group_uuid = session : getVariable ( " ring_group_uuid " ) ;
recordings_dir = session : getVariable ( " recordings_dir " ) ;
sounds_dir = session : getVariable ( " sounds_dir " ) ;
--variables that don't require ${} when used in the dialplan conditions
username = session : getVariable ( " username " ) ;
dialplan = session : getVariable ( " dialplan " ) ;
caller_id_name = session : getVariable ( " caller_id_name " ) ;
caller_id_number = session : getVariable ( " caller_id_number " ) ;
network_addr = session : getVariable ( " network_addr " ) ;
ani = session : getVariable ( " ani " ) ;
aniii = session : getVariable ( " aniii " ) ;
rdnis = session : getVariable ( " rdnis " ) ;
destination_number = session : getVariable ( " destination_number " ) ;
source = session : getVariable ( " source " ) ;
uuid = session : getVariable ( " uuid " ) ;
context = session : getVariable ( " context " ) ;
2014-08-14 03:21:55 +02:00
call_direction = session : getVariable ( " call_direction " ) ;
2013-07-24 21:54:17 +02:00
--define additional variables
uuids = " " ;
external = " false " ;
--set the sounds path for the language, dialect and voice
default_language = session : getVariable ( " default_language " ) ;
default_dialect = session : getVariable ( " default_dialect " ) ;
default_voice = session : getVariable ( " default_voice " ) ;
if ( not default_language ) then default_language = ' en ' ; end
if ( not default_dialect ) then default_dialect = ' us ' ; end
if ( not default_voice ) then default_voice = ' callie ' ; end
2014-08-14 03:21:55 +02:00
--get record_ext
2015-08-18 13:38:41 +02:00
record_ext = session : getVariable ( " record_ext " ) ;
if ( not record_ext ) then
2014-08-14 03:21:55 +02:00
record_ext = " wav " ;
end
--set the recording path
2015-08-31 13:47:06 +02:00
recording_archive = recordings_dir .. " / " .. domain_name .. " /archive/ " .. os.date ( " %Y/%b/%d " ) ;
--set the recording file
record_file = recording_archive : gsub ( " \\ " , " / " ) .. " / " .. uuid .. " . " .. record_ext
2014-08-14 03:21:55 +02:00
2013-07-24 21:54:17 +02:00
--prepare the api object
api = freeswitch.API ( ) ;
--define the session hangup
--function on_hangup(s,status)
-- freeswitch.consoleLog("NOTICE","---- on_hangup: "..status.."\n");
-- error();
--end
2013-08-16 22:36:52 +02:00
--get the ring group
ring_group_forward_enabled = " " ;
ring_group_forward_destination = " " ;
sql = " SELECT * FROM v_ring_groups " ;
sql = sql .. " where ring_group_uuid = ' " .. ring_group_uuid .. " ' " ;
2013-08-29 18:57:36 +02:00
status = dbh : query ( sql , function ( row )
domain_uuid = row [ " domain_uuid " ] ;
2015-06-20 05:43:38 +02:00
ring_group_name = row [ " ring_group_name " ] ;
2015-09-27 08:52:47 +02:00
ring_group_extension = row [ " ring_group_extension " ] ;
2013-08-29 18:57:36 +02:00
ring_group_forward_enabled = row [ " ring_group_forward_enabled " ] ;
ring_group_forward_destination = row [ " ring_group_forward_destination " ] ;
2014-02-08 19:42:35 +01:00
ring_group_cid_name_prefix = row [ " ring_group_cid_name_prefix " ] ;
2014-09-06 09:45:17 +02:00
ring_group_cid_number_prefix = row [ " ring_group_cid_number_prefix " ] ;
2015-06-20 06:06:20 +02:00
missed_call_app = row [ " ring_group_missed_call_app " ] ;
missed_call_data = row [ " ring_group_missed_call_data " ] ;
2013-08-16 22:36:52 +02:00
end ) ;
2013-07-24 21:54:17 +02:00
2014-02-08 19:42:35 +01:00
--set the caller id
if ( session : ready ( ) ) then
2015-06-20 06:06:20 +02:00
if ( string.len ( ring_group_cid_name_prefix ) > 0 ) then
session : execute ( " set " , " effective_caller_id_name= " .. ring_group_cid_name_prefix .. " # " .. caller_id_name ) ;
end
if ( string.len ( ring_group_cid_number_prefix ) > 0 ) then
session : execute ( " set " , " effective_caller_id_number= " .. ring_group_cid_number_prefix .. caller_id_number ) ;
end
2014-02-08 19:42:35 +01:00
end
2015-06-20 05:43:38 +02:00
--check the missed calls
function missed ( )
if ( missed_call_app ~= nil and missed_call_data ~= nil ) then
if ( missed_call_app == " email " ) then
2015-09-15 16:49:37 +02:00
--set the sounds path for the language, dialect and voice
default_language = session : getVariable ( " default_language " ) ;
default_dialect = session : getVariable ( " default_dialect " ) ;
default_voice = session : getVariable ( " default_voice " ) ;
if ( not default_language ) then default_language = ' en ' ; end
if ( not default_dialect ) then default_dialect = ' us ' ; end
if ( not default_voice ) then default_voice = ' callie ' ; end
--prepare the files
file_subject = scripts_dir .. " /app/missed_calls/resources/templates/ " .. default_language .. " / " .. default_dialect .. " /email_subject.tpl " ;
file_body = scripts_dir .. " /app/missed_calls/resources/templates/ " .. default_language .. " / " .. default_dialect .. " /email_body.tpl " ;
if ( not file_exists ( file_subject ) ) then
file_subject = scripts_dir .. " /app/missed_calls/resources/templates/en/us/email_subject.tpl " ;
file_body = scripts_dir .. " /app/missed_calls/resources/templates/en/us/email_body.tpl " ;
end
--prepare the headers
headers = ' {"X-FusionPBX-Domain-UUID":" ' .. domain_uuid .. ' ", ' ;
headers = headers .. ' "X-FusionPBX-Domain-Name":" ' .. domain_name .. ' ", ' ;
headers = headers .. ' "X-FusionPBX-Call-UUID":" ' .. uuid .. ' ", ' ;
headers = headers .. ' "X-FusionPBX-Email-Type":"missed"} ' ;
--prepare the subject
local f = io.open ( file_subject , " r " ) ;
local subject = f : read ( " *all " ) ;
f : close ( ) ;
subject = subject : gsub ( " ${caller_id_name} " , caller_id_name ) ;
subject = subject : gsub ( " ${caller_id_number} " , caller_id_number ) ;
subject = subject : gsub ( " ${ring_group_name} " , ring_group_name ) ;
2015-09-27 08:52:47 +02:00
subject = subject : gsub ( " ${ring_group_extension} " , ring_group_extension ) ;
subject = subject : gsub ( " ${sip_to_user} " , ring_group_name ) ;
subject = subject : gsub ( " ${dialed_user} " , ring_group_extension ) ;
2015-09-15 16:49:37 +02:00
subject = trim ( subject ) ;
subject = ' =?utf-8?B? ' .. base64.encode ( subject ) .. ' ?= ' ;
--prepare the body
local f = io.open ( file_body , " r " ) ;
local body = f : read ( " *all " ) ;
f : close ( ) ;
body = body : gsub ( " ${caller_id_name} " , caller_id_name ) ;
body = body : gsub ( " ${caller_id_number} " , caller_id_number ) ;
body = body : gsub ( " ${ring_group_name} " , ring_group_name ) ;
2015-09-27 08:52:47 +02:00
body = body : gsub ( " ${ring_group_extension} " , ring_group_extension ) ;
body = body : gsub ( " ${sip_to_user} " , ring_group_name ) ;
body = body : gsub ( " ${dialed_user} " , ring_group_extension ) ;
2015-09-15 16:49:37 +02:00
body = body : gsub ( " " , " " ) ;
body = body : gsub ( " %s+ " , " " ) ;
body = body : gsub ( " " , " " ) ;
body = body : gsub ( " \n " , " " ) ;
body = body : gsub ( " \n " , " " ) ;
body = body : gsub ( " ' " , " ' " ) ;
body = body : gsub ( [["]] , " " " ) ;
body = trim ( body ) ;
--send the email
cmd = " luarun email.lua " .. missed_call_data .. " " .. missed_call_data .. " " .. headers .. " ' " .. subject .. " ' ' " .. body .. " ' " ;
if ( debug [ " info " ] ) then
freeswitch.consoleLog ( " notice " , " [missed call] cmd: " .. cmd .. " \n " ) ;
end
api = freeswitch.API ( ) ;
result = api : executeString ( cmd ) ;
2015-06-20 05:43:38 +02:00
end
end
end
2013-08-16 22:36:52 +02:00
--process the ring group
if ( ring_group_forward_enabled == " true " and string.len ( ring_group_forward_destination ) > 0 ) then
--forward the ring group
session : execute ( " transfer " , ring_group_forward_destination .. " XML " .. context ) ;
else
2015-09-01 18:33:26 +02:00
--get the strategy of the ring group, if random, we use random() to order the destinations
sql = [ [
SELECT
r.ring_group_strategy
FROM
2015-09-07 11:48:10 +02:00
v_ring_groups as r
2015-09-01 18:33:26 +02:00
WHERE
2015-09-07 11:48:10 +02:00
ring_group_uuid = ' ]]..ring_group_uuid..[[ '
2015-09-01 18:33:26 +02:00
AND r.domain_uuid = ' ]]..domain_uuid..[[ '
AND r.ring_group_enabled = ' true '
] ] ;
2015-09-07 11:48:10 +02:00
2015-09-03 11:53:28 +02:00
2015-09-01 18:33:26 +02:00
assert ( dbh : query ( sql , function ( row )
if ( row.ring_group_strategy == " random " ) then
2015-09-07 11:48:10 +02:00
if ( database [ " type " ] == " mysql " ) then
2015-09-03 11:53:28 +02:00
sql_order = ' rand() '
2015-09-07 12:51:12 +02:00
else
sql_order = ' random() ' --both postgresql and sqlite uses random() instead of rand()
2015-09-03 11:53:28 +02:00
end
2015-09-01 18:33:26 +02:00
else
sql_order = ' d.destination_delay, d.destination_number asc '
end
end ) ) ;
2015-09-03 11:53:28 +02:00
2013-08-16 22:36:52 +02:00
--get the ring group destinations
2015-05-12 07:59:33 +02:00
sql = [ [
2015-05-15 21:14:18 +02:00
SELECT
2015-06-20 06:06:20 +02:00
r.ring_group_strategy , r.ring_group_timeout_app , r.ring_group_distinctive_ring ,
d.destination_number , d.destination_delay , d.destination_timeout , d.destination_prompt ,
r.ring_group_timeout_data , r.ring_group_cid_name_prefix , r.ring_group_cid_number_prefix , r.ring_group_ringback , r.ring_group_skip_active
2015-05-15 21:14:18 +02:00
FROM
2015-06-20 06:06:20 +02:00
v_ring_groups as r , v_ring_group_destinations as d
2015-05-15 21:14:18 +02:00
WHERE
2015-06-20 06:06:20 +02:00
d.ring_group_uuid = r.ring_group_uuid
AND d.ring_group_uuid = ' ]]..ring_group_uuid..[[ '
AND r.domain_uuid = ' ]]..domain_uuid..[[ '
AND r.ring_group_enabled = ' true '
2015-05-15 21:14:18 +02:00
ORDER BY
2015-09-03 11:53:28 +02:00
] ] .. sql_order .. [ [
2015-05-15 21:14:18 +02:00
] ] ;
2015-05-12 07:59:33 +02:00
--freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n");
2013-08-16 22:36:52 +02:00
destinations = { } ;
x = 1 ;
assert ( dbh : query ( sql , function ( row )
if ( row.destination_prompt == " 1 " or row.destination_prompt == " 2 " ) then
prompt = " true " ;
end
2013-12-04 09:14:40 +01:00
local array = explode ( " @ " , row.destination_number ) ;
if ( array [ 2 ] == nil ) then
-- no @
leg_domain_name = domain_name ;
else
leg_domain_name = array [ 2 ] ;
end
cmd = " user_exists id " .. row.destination_number .. " " .. leg_domain_name ;
2013-08-16 22:36:52 +02:00
user_exists = api : executeString ( cmd ) ;
if ( user_exists == " true " ) then
2015-05-17 01:38:07 +02:00
--add user_exists true or false to the row array
row [ ' user_exists ' ] = " true " ;
--handle do_not_disturb
cmd = " user_data " .. row.destination_number .. " @ " .. leg_domain_name .. " var do_not_disturb " ;
if ( api : executeString ( cmd ) ~= " true " ) then
--add the row to the destinations array
destinations [ x ] = row ;
end
2013-08-16 22:36:52 +02:00
else
2015-05-17 01:38:07 +02:00
--set the values
external = " true " ;
row [ ' user_exists ' ] = " false " ;
--add the row to the destinations array
destinations [ x ] = row ;
2013-08-16 22:36:52 +02:00
end
2015-06-07 06:06:39 +02:00
row [ ' domain_name ' ] = leg_domain_name ;
2013-08-16 22:36:52 +02:00
x = x + 1 ;
end ) ) ;
--freeswitch.consoleLog("NOTICE", "[ring_group] external "..external.."\n");
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--get the dialplan data and save it to a table
if ( external ) then
sql = [ [ select * from v_dialplans as d , v_dialplan_details as s
2014-07-13 06:12:04 +02:00
where ( d.domain_uuid = ' ]] .. domain_uuid .. [[ ' or d.domain_uuid is null )
2013-08-16 22:36:52 +02:00
and d.app_uuid = ' 8c914ec3-9fc0-8ab5-4cda-6c9288bdc9a3 '
and d.dialplan_enabled = ' true '
and d.dialplan_uuid = s.dialplan_uuid
order by
d.dialplan_order asc ,
d.dialplan_name asc ,
d.dialplan_uuid asc ,
s.dialplan_detail_group asc ,
CASE s.dialplan_detail_tag
WHEN ' condition ' THEN 1
WHEN ' action ' THEN 2
WHEN ' anti-action ' THEN 3
ELSE 100 END ,
s.dialplan_detail_order asc ] ]
--freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n");
dialplans = { } ;
x = 1 ;
assert ( dbh : query ( sql , function ( row )
dialplans [ x ] = row ;
x = x + 1 ;
end ) ) ;
2013-07-24 21:54:17 +02:00
end
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--process the destinations
x = 0 ;
for key , row in pairs ( destinations ) do
--set the values from the database as variables
user_exists = row.user_exists ;
2014-02-25 05:59:02 +01:00
ring_group_strategy = row.ring_group_strategy ;
2013-08-16 22:36:52 +02:00
ring_group_timeout_app = row.ring_group_timeout_app ;
ring_group_timeout_data = row.ring_group_timeout_data ;
ring_group_cid_name_prefix = row.ring_group_cid_name_prefix ;
2014-09-06 09:45:17 +02:00
ring_group_cid_number_prefix = row.ring_group_cid_number_prefix ;
2015-06-07 06:06:39 +02:00
ring_group_distinctive_ring = row.ring_group_distinctive_ring ;
2013-08-16 22:36:52 +02:00
ring_group_ringback = row.ring_group_ringback ;
2014-08-13 12:51:51 +02:00
ring_group_skip_active = row.ring_group_skip_active ;
2013-08-16 22:36:52 +02:00
destination_number = row.destination_number ;
destination_delay = row.destination_delay ;
destination_timeout = row.destination_timeout ;
destination_prompt = row.destination_prompt ;
2013-12-04 09:14:40 +01:00
domain_name = row.domain_name ;
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--set ringback
if ( ring_group_ringback == " ${uk-ring} " ) then
ring_group_ringback = " tone_stream://%(400,200,400,450);%(400,2200,400,450);loops=-1 " ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
if ( ring_group_ringback == " ${us-ring} " ) then
ring_group_ringback = " tone_stream://%(2000,4000,440.0,480.0);loops=-1 " ;
end
2014-01-17 00:30:56 +01:00
if ( ring_group_ringback == " ${pt-ring} " ) then
ring_group_ringback = " tone_stream://%(1000,5000,400.0,0.0);loops=-1 " ;
end
2013-08-16 22:36:52 +02:00
if ( ring_group_ringback == " ${fr-ring} " ) then
ring_group_ringback = " tone_stream://%(1500,3500,440.0,0.0);loops=-1 " ;
end
if ( ring_group_ringback == " ${rs-ring} " ) then
ring_group_ringback = " tone_stream://%(1000,4000,425.0,0.0);loops=-1 " ;
end
2014-11-03 12:16:10 +01:00
if ( ring_group_ringback == " ${it-ring} " ) then
ring_group_ringback = " tone_stream://%(1000,4000,425.0,0.0);loops=-1 " ;
end
2013-08-16 22:36:52 +02:00
if ( ring_group_ringback == " " ) then
ring_group_ringback = " local_stream://default " ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
session : setVariable ( " ringback " , ring_group_ringback ) ;
session : setVariable ( " transfer_ringback " , ring_group_ringback ) ;
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--setup the delimiter
delimiter = " , " ;
2014-02-25 05:59:02 +01:00
if ( ring_group_strategy == " rollover " ) then
2013-08-16 22:36:52 +02:00
delimiter = " | " ;
end
2014-02-25 05:59:02 +01:00
if ( ring_group_strategy == " sequence " ) then
delimiter = " | " ;
end
2015-09-01 18:33:26 +02:00
if ( ring_group_strategy == " random " ) then
delimiter = " | " ;
end
2014-02-25 05:59:02 +01:00
if ( ring_group_strategy == " simultaneous " ) then
2013-08-16 22:36:52 +02:00
delimiter = " , " ;
end
2014-02-25 05:59:02 +01:00
if ( ring_group_strategy == " enterprise " ) then
2013-08-16 22:36:52 +02:00
delimiter = " :_: " ;
end
2013-09-10 01:57:10 +02:00
2015-07-03 17:51:02 +02:00
--leg delay settings
if ( ring_group_strategy == " enterprise " ) then
delay_name = " originate_delay_start " ;
destination_delay = destination_delay * 1000 ;
else
delay_name = " leg_delay_start " ;
end
2013-08-16 22:36:52 +02:00
--create a new uuid and add it to the uuid list
new_uuid = api : executeString ( " create_uuid " ) ;
if ( string.len ( uuids ) == 0 ) then
uuids = new_uuid ;
else
uuids = uuids .. " , " .. new_uuid ;
end
session : execute ( " set " , " uuids= " .. uuids ) ;
2014-07-13 05:25:05 +02:00
2015-06-07 06:06:39 +02:00
--export the ringback
if ( ring_group_distinctive_ring ~= nil ) then
2015-06-16 01:26:02 +02:00
ring_group_distinctive_ring = ring_group_distinctive_ring : gsub ( " ${local_ip_v4} " , session : getVariable ( " local_ip_v4 " ) ) ;
ring_group_distinctive_ring = ring_group_distinctive_ring : gsub ( " ${domain_name} " , session : getVariable ( " domain_name " ) ) ;
2015-06-07 06:10:41 +02:00
session : execute ( " export " , " sip_h_Alert-Info= " .. ring_group_distinctive_ring ) ;
2015-06-07 06:06:39 +02:00
end
2014-08-14 05:06:00 +02:00
--set confirm
if ( ring_group_strategy == " simultaneous "
or ring_group_strategy == " sequence "
or ring_group_strategy == " rollover " ) then
session : execute ( " set " , " group_confirm_key=exec " ) ;
2015-08-11 04:06:33 +02:00
session : execute ( " set " , " group_confirm_file=lua " .. scripts_dir : gsub ( ' \\ ' , ' / ' ) .. " /confirm.lua " ) ;
2014-08-14 05:06:00 +02:00
end
2014-07-13 05:25:05 +02:00
--determine confirm prompt
if ( destination_prompt == nil ) then
2014-08-14 05:06:00 +02:00
group_confirm = " confirm=false, " ;
2014-07-13 05:25:05 +02:00
elseif ( destination_prompt == " 1 " ) then
2015-08-11 04:06:33 +02:00
group_confirm = " group_confirm_key=exec,group_confirm_file=lua " .. scripts_dir : gsub ( ' \\ ' , ' / ' ) .. " /confirm.lua,confirm=true, " ;
2014-07-13 05:25:05 +02:00
elseif ( destination_prompt == " 2 " ) then
2015-08-11 04:06:33 +02:00
group_confirm = " group_confirm_key=exec,group_confirm_file=lua " .. scripts_dir : gsub ( ' \\ ' , ' / ' ) .. " /confirm.lua,confirm=true, " ;
2013-09-11 04:37:57 +02:00
else
2014-08-14 05:06:00 +02:00
group_confirm = " confirm=false, " ;
2013-09-11 04:37:57 +02:00
end
2013-09-10 01:57:10 +02:00
2014-08-14 03:21:55 +02:00
--get user_record value and determine whether to record the session
cmd = " user_data " .. destination_number .. " @ " .. domain_name .. " var user_record " ;
user_record = trim ( api : executeString ( cmd ) ) ;
--set the record_session variable
2015-08-31 13:47:06 +02:00
record_session = false ;
2014-08-14 03:21:55 +02:00
if ( user_record == " all " ) then
record_session = true ;
end
if ( user_record == " inbound " and call_direction == " inbound " ) then
record_session = true ;
end
if ( user_record == " outbound " and call_direction == " outbound " ) then
record_session = true ;
end
if ( user_record == " local " and call_direction == " local " ) then
record_session = true ;
end
--record the session
if ( record_session ) then
2015-08-31 13:47:06 +02:00
record_session = " ,api_on_answer='uuid_record " .. uuid .. " start " .. record_file .. " ' " ;
else
record_session = " "
2014-08-14 03:21:55 +02:00
end
2015-08-31 13:47:06 +02:00
row.record_session = record_session
2014-08-14 03:21:55 +02:00
2013-08-16 22:36:52 +02:00
--process according to user_exists, sip_uri, external number
if ( user_exists == " true " ) then
2014-08-03 14:06:49 +02:00
--get the extension_uuid
2014-08-13 12:51:51 +02:00
cmd = " user_data " .. destination_number .. " @ " .. domain_name .. " var extension_uuid " ;
extension_uuid = trim ( api : executeString ( cmd ) ) ;
2013-08-16 22:36:52 +02:00
--send to user
2015-08-31 13:47:06 +02:00
local dial_string_to_user = " [sip_invite_domain= " .. domain_name .. " , " .. group_confirm .. " leg_timeout= " .. destination_timeout .. " , " .. delay_name .. " = " .. destination_delay .. " ,dialed_extension= " .. row.destination_number .. " ,extension_uuid= " .. extension_uuid .. row.record_session .. " ]user/ " .. row.destination_number .. " @ " .. domain_name ;
2015-10-20 14:15:42 +02:00
if ( ring_group_skip_active == " true " ) then
local channels = channels_by_number ( destination_number , domain_name )
if ( not channels ) or # channels == 0 then
dial_string = dial_string_to_user
2014-08-13 12:51:51 +02:00
end
else
2015-08-31 13:47:06 +02:00
dial_string = dial_string_to_user ;
2014-08-13 12:51:51 +02:00
end
2013-08-16 22:36:52 +02:00
elseif ( tonumber ( destination_number ) == nil ) then
--sip uri
2015-07-03 17:51:02 +02:00
dial_string = " [sip_invite_domain= " .. domain_name .. " , " .. group_confirm .. " leg_timeout= " .. destination_timeout .. " , " .. delay_name .. " = " .. destination_delay .. " ] " .. row.destination_number ;
2013-08-16 22:36:52 +02:00
else
--external number
y = 0 ;
previous_dialplan_uuid = ' ' ;
for k , r in pairs ( dialplans ) do
if ( y > 0 ) then
if ( previous_dialplan_uuid ~= r.dialplan_uuid ) then
regex_match = false ;
bridge_match = false ;
square = square .. " ] " ;
y = 0 ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
end
if ( r.dialplan_detail_tag == " condition " ) then
if ( r.dialplan_detail_type == " destination_number " ) then
if ( api : execute ( " regex " , " m:~ " .. destination_number .. " ~ " .. r.dialplan_detail_data ) == " true " ) then
--get the regex result
2014-02-04 01:49:16 +01:00
destination_result = trim ( api : execute ( " regex " , " m:~ " .. destination_number .. " ~ " .. r.dialplan_detail_data .. " ~$1 " ) ) ;
2013-08-16 22:36:52 +02:00
--set match equal to true
regex_match = true
end
end
end
if ( r.dialplan_detail_tag == " action " ) then
if ( regex_match ) then
--replace $1
dialplan_detail_data = r.dialplan_detail_data : gsub ( " $1 " , destination_result ) ;
--if the session is set then process the actions
if ( y == 0 ) then
2015-07-03 17:51:02 +02:00
square = " [domain_name= " .. domain_name .. " ,domain_uuid= " .. domain_uuid .. " ,sip_invite_domain= " .. domain_name .. " , " .. group_confirm .. " leg_timeout= " .. destination_timeout .. " , " .. delay_name .. " = " .. destination_delay .. " ,ignore_early_media=true, " ;
2013-08-16 22:36:52 +02:00
end
if ( r.dialplan_detail_type == " set " ) then
--session:execute("eval", dialplan_detail_data);
if ( dialplan_detail_data == " sip_h_X-accountcode=${accountcode} " ) then
if ( session ) then
accountcode = session : getVariable ( " accountcode " ) ;
if ( accountcode ) then
square = square .. " sip_h_X-accountcode= " .. accountcode .. " , " ;
end
end
elseif ( dialplan_detail_data == " effective_caller_id_name=${outbound_caller_id_name} " ) then
elseif ( dialplan_detail_data == " effective_caller_id_number=${outbound_caller_id_number} " ) then
else
square = square .. dialplan_detail_data .. " , " ;
end
elseif ( r.dialplan_detail_type == " bridge " ) then
if ( bridge_match ) then
2015-11-10 02:42:29 +01:00
dial_string = dial_string .. delimiter .. square .. " ] " .. dialplan_detail_data ;
2014-07-13 05:25:05 +02:00
square = " [ " ;
2013-08-16 22:36:52 +02:00
else
dial_string = square .. " ] " .. dialplan_detail_data ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
bridge_match = true ;
2015-08-11 21:42:46 +02:00
break ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
--increment the value
y = y + 1 ;
2013-07-24 21:54:17 +02:00
end
2013-08-16 22:36:52 +02:00
end
previous_dialplan_uuid = r.dialplan_uuid ;
2013-07-24 21:54:17 +02:00
end
end
2013-09-10 01:57:10 +02:00
2014-08-30 20:22:06 +02:00
--add a delimiter between destinations
2014-07-13 05:25:05 +02:00
if ( dial_string ~= nil ) then
2014-08-13 12:51:51 +02:00
--freeswitch.consoleLog("notice", "[ring group] dial_string: " .. dial_string .. "\n");
2014-07-13 05:25:05 +02:00
if ( x == 0 ) then
2014-08-30 20:22:06 +02:00
if ( ring_group_strategy == " enterprise " ) then
app_data = dial_string ;
else
app_data = " {ignore_early_media=true} " .. dial_string ;
end
2014-07-13 05:25:05 +02:00
else
2014-08-13 12:51:51 +02:00
if ( app_data == nil ) then
2014-08-30 20:22:06 +02:00
if ( ring_group_strategy == " enterprise " ) then
app_data = dial_string ;
else
app_data = " {ignore_early_media=true} " .. dial_string ;
end
2014-08-13 12:51:51 +02:00
else
app_data = app_data .. delimiter .. dial_string ;
end
2014-07-13 05:25:05 +02:00
end
2013-07-24 21:54:17 +02:00
end
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--increment the value of x
x = x + 1 ;
2013-07-24 21:54:17 +02:00
end
2013-09-10 01:57:10 +02:00
2013-08-16 22:36:52 +02:00
--session execute
if ( session : ready ( ) ) then
--set the variables
session : execute ( " set " , " hangup_after_bridge=true " ) ;
session : execute ( " set " , " continue_on_fail=true " ) ;
2013-09-10 01:57:10 +02:00
2015-08-19 08:06:22 +02:00
local bind_target = ' both '
-- if session:getVariable("sip_authorized") ~= "true" then
-- bind_target = 'peer'
-- end
2015-08-18 13:38:41 +02:00
--set bind digit action
local bindings = {
" local,*1,exec:execute_extension,dx XML " .. context ,
" local,*2,exec:record_session, " .. record_file ,
" local,*3,exec:execute_extension,cf XML " .. context ,
" local,*4,exec:execute_extension,att_xfer XML " .. context ,
}
for _ , str in ipairs ( bindings ) do
session : execute ( " bind_digit_action " , str .. " , " .. bind_target )
end
session : execute ( " digit_action_set_realm " , " local " )
2013-09-10 01:57:10 +02:00
2014-07-13 05:25:05 +02:00
--if the user is busy rollover to the next destination
if ( ring_group_strategy == " rollover " ) then
2015-08-14 23:27:48 +02:00
x = 0 ;
2014-07-13 05:25:05 +02:00
for key , row in pairs ( destinations ) do
--set the values from the database as variables
user_exists = row.user_exists ;
destination_number = row.destination_number ;
domain_name = row.domain_name ;
2014-08-03 12:35:01 +02:00
--get the extension_uuid
if ( user_exists == " true " ) then
2014-08-03 14:06:49 +02:00
cmd = " user_data " .. destination_number .. " @ " .. domain_name .. " var extension_uuid " ;
2014-08-03 12:35:01 +02:00
extension_uuid = trim ( api : executeString ( cmd ) ) ;
end
2014-07-13 05:25:05 +02:00
--set the timeout
session : execute ( " set " , " call_timeout= " .. row.destination_timeout ) ;
2015-08-14 23:27:48 +02:00
--if the timeout was reached go to the timeout action
if ( x > 0 ) then
if ( session : getVariable ( " originate_disposition " ) == " ALLOTTED_TIMEOUT "
or session : getVariable ( " originate_disposition " ) == " NO_ANSWER "
or session : getVariable ( " originate_disposition " ) == " NO_USER_RESPONSE " ) then
break ;
2014-02-25 05:59:02 +01:00
end
2014-07-13 05:25:05 +02:00
end
2015-08-14 23:27:48 +02:00
--send the call to the destination
if ( user_exists == " true " ) then
2015-08-31 13:47:06 +02:00
dial_string = " [ " .. group_confirm .. " sip_invite_domain= " .. domain_name .. " ,dialed_extension= " .. destination_number .. " ,extension_uuid= " .. extension_uuid .. " ,domain_name= " .. domain_name .. " ,domain_uuid= " .. domain_uuid .. row.record_session .. " ]user/ " .. destination_number .. " @ " .. domain_name ;
2015-08-14 23:27:48 +02:00
session : execute ( " bridge " , dial_string ) ;
elseif ( tonumber ( destination_number ) == nil ) then
--sip uri
dial_string = " [ " .. group_confirm .. " sip_invite_domain= " .. domain_name .. " ,domain_name= " .. domain_name .. " ,domain_uuid= " .. domain_uuid .. " ] " .. destination_number ;
session : execute ( " bridge " , dial_string ) ;
else
dial_string = " [ " .. group_confirm .. " sip_invite_domain= " .. domain_name .. " ,domain_name= " .. domain_name .. " ,domain_uuid= " .. domain_uuid .. " ]loopback/ " .. destination_number ;
session : execute ( " bridge " , dial_string ) ;
end
--increment the value of x
x = x + 1 ;
2014-02-25 05:59:02 +01:00
end
2014-07-13 05:25:05 +02:00
end
2014-02-25 05:59:02 +01:00
2014-08-30 20:22:06 +02:00
--execute the bridge
2014-07-13 05:25:05 +02:00
if ( app_data ~= nil ) then
2014-08-30 20:22:06 +02:00
if ( ring_group_strategy == " enterprise " ) then
app_data = app_data : gsub ( " %[ " , " { " ) ;
app_data = app_data : gsub ( " %] " , " } " ) ;
end
freeswitch.consoleLog ( " NOTICE " , " [ring group] app_data: " .. app_data .. " \n " ) ;
2015-10-06 09:05:15 +02:00
-- log.noticef("bridge begin: originate_disposition:%s answered:%s ready:%s bridged:%s", session:getVariable("originate_disposition"), session:answered() and "true" or "false", session:ready() and "true" or "false", session:bridged() and "true" or "false")
2014-07-13 05:25:05 +02:00
session : execute ( " bridge " , app_data ) ;
2015-09-22 17:33:41 +02:00
-- log.noticef("bridge done: originate_disposition:%s answered:%s ready:%s bridged:%s", session:getVariable("originate_disposition"), session:answered() and "true" or "false", session:ready() and "true" or "false", session:bridged() and "true" or "false")
2014-08-30 20:22:06 +02:00
end
--timeout destination
if ( app_data ~= nil ) then
2015-10-06 09:05:15 +02:00
if session : ready ( ) and (
session : getVariable ( " originate_disposition " ) == " ALLOTTED_TIMEOUT "
2014-07-13 05:25:05 +02:00
or session : getVariable ( " originate_disposition " ) == " NO_ANSWER "
or session : getVariable ( " originate_disposition " ) == " NO_USER_RESPONSE "
or session : getVariable ( " originate_disposition " ) == " USER_NOT_REGISTERED "
or session : getVariable ( " originate_disposition " ) == " NORMAL_TEMPORARY_FAILURE "
or session : getVariable ( " originate_disposition " ) == " NO_ROUTE_DESTINATION "
or session : getVariable ( " originate_disposition " ) == " USER_BUSY "
2015-10-06 09:05:15 +02:00
or session : getVariable ( " originate_disposition " ) == " failure "
) then
--send missed call notification
missed ( ) ;
--execute the time out action
session : execute ( ring_group_timeout_app , ring_group_timeout_data ) ;
2013-08-16 22:36:52 +02:00
end
2014-07-13 05:25:05 +02:00
else
if ( ring_group_timeout_app ~= nil ) then
2015-06-20 05:43:38 +02:00
--send missed call notification
missed ( ) ;
--execute the time out action
session : execute ( ring_group_timeout_app , ring_group_timeout_data ) ;
2014-07-13 05:25:05 +02:00
else
sql = " SELECT ring_group_timeout_app, ring_group_timeout_data FROM v_ring_groups " ;
sql = sql .. " where ring_group_uuid = ' " .. ring_group_uuid .. " ' " ;
2014-08-13 12:51:51 +02:00
--freeswitch.consoleLog("notice", "[ring group] SQL:" .. sql .. "\n");
2014-07-13 05:25:05 +02:00
dbh : query ( sql , function ( row )
2015-06-20 05:43:38 +02:00
--send missed call notification
missed ( ) ;
--execute the time out action
session : execute ( row.ring_group_timeout_app , row.ring_group_timeout_data ) ;
2014-07-13 05:25:05 +02:00
end ) ;
end
end
end
end
2013-07-24 21:54:17 +02:00
--actions
--ACTIONS = {}
--table.insert(ACTIONS, {"set", "hangup_after_bridge=true"});
--table.insert(ACTIONS, {"set", "continue_on_fail=true"});
--table.insert(ACTIONS, {"bridge", app_data});
2015-05-05 08:35:43 +02:00
--table.insert(ACTIONS, {ring_group_timeout_app, ring_group_timeout_data});