Merge branch 'master' into intercept_load_balance

# Conflicts:
#	resources/install/scripts/intercept_group.lua
This commit is contained in:
Alexey Melnichuk
2015-11-10 12:30:45 +03:00
60 changed files with 900 additions and 580 deletions

View File

@@ -38,7 +38,7 @@ local log = require "resources.functions.log".ring_group
require "resources.functions.explode";
require "resources.functions.base64";
require "resources.functions.file_exists";
require "resources.functions.explode";
require "resources.functions.channel_utils"
--get the variables
domain_name = session:getVariable("domain_name");
@@ -439,34 +439,10 @@ local log = require "resources.functions.log".ring_group
extension_uuid = trim(api:executeString(cmd));
--send to user
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;
if (ring_group_skip_active ~= nil) then
if (ring_group_skip_active == "true") then
cmd = "show channels like "..destination_number;
reply = trim(api:executeString(cmd));
--freeswitch.consoleLog("notice", "[ring group] reply "..cmd.." " .. reply .. "\n");
exploded_reply = {};
exploded_reply = explode(",",reply);
if (reply == "0 total.") then
dial_string = dial_string_to_user
else
idle_extension=true;
if (exploded_reply ~= nil) then
for i,v in ipairs(exploded_reply) do
if(v==destination_number.."@"..domain_name) then
idle_extension=false;
end
end
end
if(idle_extension) then
dial_string = dial_string_to_user;
end
end
else
--look inside the reply to check for the correct domain_name
dial_string = dial_string_to_user;
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
end
else
dial_string = dial_string_to_user;

View File

@@ -99,7 +99,7 @@
--send the message waiting event
local event = freeswitch.Event("message_waiting");
if (row["message_count"] == "0") then
if (new_messages == "0") then
event:addHeader("MWI-Messages-Waiting", "no");
else
event:addHeader("MWI-Messages-Waiting", "yes");

View File

@@ -244,6 +244,7 @@
sip_force_expires = row.sip_force_expires;
nibble_account = row.nibble_account;
sip_bypass_media = row.sip_bypass_media;
absolute_codec_string = row.absolute_codec_string;
forward_all_enabled = row.forward_all_enabled;
forward_all_destination = row.forward_all_destination;
forward_busy_enabled = row.forward_busy_enabled;
@@ -445,9 +446,13 @@
if (string.len(nibble_account) > 0) then
table.insert(xml, [[ <variable name="nibble_account" value="]] .. nibble_account .. [["/>]]);
end
if (string.len(absolute_codec_string) > 0) then
table.insert(xml, [[ <variable name="absolute_codec_string" value="]] .. absolute_codec_string .. [["/>]]);
end
if (sip_bypass_media == "bypass-media") then
table.insert(xml, [[ <variable name="bypass_media" value="true"/>]]);
end
if (sip_bypass_media == "bypass-media-after-bridge") then
table.insert(xml, [[ <variable name="bypass_media_after_bridge" value="true"/>]]);
end

View File

@@ -144,14 +144,14 @@
--read root xml language file, parse included xml files
local xml_file_paths = {}
local file_handle = io.open("/usr/local/freeswitch/conf/lang/"..language.."/"..language..".xml", "r");
local file_handle = io.open(phrases_dir.."/"..language.."/"..language..".xml", "r");
if (file_handle ~= nil) then
for file_line in file_handle:lines() do
if (string.find(file_line, 'cmd="include" data="', 0, true) ~= nil) then
pos_beg = string.find(file_line, 'cmd="include" data="', 0, true) + 20;
pos_end = string.find(file_line, '"/>', 0, true) - 1;
xml_file_path = string.sub(file_line, pos_beg, pos_end);
table.insert(xml_file_paths, "/usr/local/freeswitch/conf/lang/"..language.."/"..xml_file_path);
table.insert(xml_file_paths, lang_path.."/"..language.."/"..xml_file_path);
--freeswitch.consoleLog("notice", "file path = "..xml_file_path.."\n");
end
end

View File

@@ -35,6 +35,10 @@
require "resources.functions.database_handle";
dbh = database_handle('system');
local log = require "resources.functions.log".call_flow
local presence_in = require "resources.functions.presence_in"
if (session:ready()) then
--get the variables
domain_name = session:getVariable("domain_name");
@@ -51,11 +55,9 @@ if (session:ready()) then
if (not default_voice) then default_voice = 'callie'; end
--get the extension list
sql = [[SELECT * FROM v_call_flows
where call_flow_uuid = ']]..call_flow_uuid..[[']]
--and call_flow_enabled = 'true'
--freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n");
app_data = "";
sql = "SELECT * FROM v_call_flows where call_flow_uuid = '"..call_flow_uuid.."'"
-- .. "and call_flow_enabled = 'true'"
--log.notice("SQL: %s", sql);
x = 0;
dbh:query(sql, function(row)
@@ -68,17 +70,15 @@ if (session:ready()) then
call_flow_label = row.call_flow_label;
call_flow_anti_label = row.call_flow_anti_label;
if (string.len(call_flow_status) == 0) then
if #call_flow_status == 0 then
call_flow_status = "true";
end
if (call_flow_status == "true") then
app = row.call_flow_app;
data = row.call_flow_data
else
if (call_flow_status == "true") then
app = row.call_flow_app;
data = row.call_flow_data
else
app = row.call_flow_anti_app;
data = row.call_flow_anti_data
end
app = row.call_flow_anti_app;
data = row.call_flow_anti_data
end
end);
@@ -99,70 +99,34 @@ if (session:ready()) then
end
--feature code - toggle the status
if (string.len(call_flow_status) == 0) then
toggle = "false";
else
if (call_flow_status == "true") then
toggle = "false";
else
toggle = "true";
end
end
if (toggle == "true") then
--set the presence to terminated - turn the lamp off:
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("state", "Active (1 waiting)");
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("answer-state", "terminated");
event:fire();
--answer and play a tone
session:answer();
if (string.len(call_flow_label) > 0) then
api = freeswitch.API();
reply = api:executeString("uuid_display "..session:get_uuid().." "..call_flow_label);
end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
--show in the console
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_label..",status=true,uuid="..call_flow_uuid.."\n");
else
--set presence in - turn lamp on
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("status", "Active (1 waiting)");
event:addHeader("rpid", "unknown");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("event_count", "1");
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("Presence-Call-Direction", "outbound")
event:addHeader("answer-state", "confirmed");
event:fire();
--answer and play a tone
session:answer();
if (string.len(call_flow_anti_label) > 0) then
api = freeswitch.API();
reply = api:executeString("uuid_display "..session:get_uuid().." "..call_flow_anti_label);
end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(500,0,300,200,100,50,25)");
--show in the console
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_anti_label..",status=false,uuid="..call_flow_uuid.."\n");
toggle = (call_flow_status == "true") and "false" or "true"
-- turn the lamp
presence_in.turn_lamp( toggle == "false",
call_flow_feature_code.."@"..domain_name,
call_flow_uuid
);
local active_flow_label = (toggle == "true") and call_flow_label or call_flow_anti_label
--answer and play a tone
session:answer();
if #active_flow_label > 0 then
api = freeswitch.API();
reply = api:executeString("uuid_display "..session:get_uuid().." "..active_flow_label);
end
session:execute("sleep", "2000");
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
--show in the console
log.noticef("label=%s,status=%s,uuid=%s", active_flow_label, toggle, call_flow_uuid);
--store in database
dbh:query("UPDATE v_call_flows SET call_flow_status = '"..toggle.."' WHERE call_flow_uuid = '"..call_flow_uuid.."'");
--hangup the call
session:hangup();
else
--app_data
freeswitch.consoleLog("notice", "Call Flow: " .. app .. " " .. data .. "\n");
log.notice("execute " .. app .. " " .. data);
--exucute the application
session:execute(app, data);

View File

@@ -38,9 +38,11 @@
require "resources.functions.file_exists";
require "resources.functions.mkdir";
--connect to the database
require "resources.functions.database_handle";
dbh = database_handle('system');
local log = require "resources.functions.log".call_flow_monitor
local presence_in = require "resources.functions.presence_in"
--make sure the scripts/run dir exists
mkdir(scripts_dir .. "/run");
@@ -61,69 +63,52 @@
--used to stop the lua service
local file = assert(io.open(run_file, "w"));
file:write("remove this file to stop the script");
file:close()
log.notice("Start")
--monitor the call flows status
x = 0
local sql = "select d.domain_name, f.call_flow_uuid, f.call_flow_extension, f.call_flow_feature_code," ..
"f.call_flow_status, f.call_flow_label, f.call_flow_anti_label "..
"from v_call_flows as f, v_domains as d " ..
"where f.domain_uuid = d.domain_uuid " -- and call_flow_enabled = 'true'
while true do
--get the extension list
sql = [[select d.domain_name, f.call_flow_uuid, f.call_flow_extension, f.call_flow_feature_code, f.call_flow_status, f.call_flow_label, f.call_flow_anti_label
from v_call_flows as f, v_domains as d
where f.domain_uuid = d.domain_uuid]]
--and call_flow_enabled = 'true'
-- debug print
if (debug["sql"]) then
freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n");
log.notice("SQL:" .. sql);
end
x = 0;
dbh:query(sql, function(row)
domain_name = row.domain_name;
call_flow_uuid = row.call_flow_uuid;
--call_flow_name = row.call_flow_name;
call_flow_extension = row.call_flow_extension;
call_flow_feature_code = row.call_flow_feature_code;
--call_flow_context = row.call_flow_context;
call_flow_status = row.call_flow_status;
--pin_number = row.call_flow_pin_number;
call_flow_label = row.call_flow_label;
call_flow_anti_label = row.call_flow_anti_label;
if (call_flow_status == "true") then
--set the presence to terminated - turn the lamp off:
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("state", "Active (1 waiting)");
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("answer-state", "terminated");
event:fire();
--show in the console
if (debug["log"]) then
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_label..",status=true,uuid="..call_flow_uuid.."\n");
end
else
--set presence in - turn lamp on
event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("login", call_flow_feature_code.."@"..domain_name);
event:addHeader("from", call_flow_feature_code.."@"..domain_name);
event:addHeader("status", "Active (1 waiting)");
event:addHeader("rpid", "unknown");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("event_count", "1");
event:addHeader("unique-id", call_flow_uuid);
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("answer-state", "confirmed");
event:fire();
--show in the console
if (debug["log"]) then
freeswitch.consoleLog("notice", "Call Flow: label="..call_flow_anti_label..",status=false,uuid="..call_flow_uuid.."\n");
end
end
end);
--connect to the database
local dbh = database_handle('system');
--get the extension list
if dbh:connected() then
dbh:query(sql, function(row)
local domain_name = row.domain_name;
local call_flow_uuid = row.call_flow_uuid;
--local call_flow_name = row.call_flow_name;
--local call_flow_extension = row.call_flow_extension;
local call_flow_feature_code = row.call_flow_feature_code;
--local call_flow_context = row.call_flow_context;
local call_flow_status = row.call_flow_status;
--local pin_number = row.call_flow_pin_number;
local call_flow_label = row.call_flow_label;
local call_flow_anti_label = row.call_flow_anti_label;
-- turn the lamp
presence_in.turn_lamp( call_flow_status == "false",
call_flow_feature_code.."@"..domain_name,
call_flow_uuid
);
if (debug["log"]) then
local label = (call_flow_status == "true") and call_flow_label or call_flow_anti_label
log.noticef("label=%s,status=%s,uuid=%s", label, call_flow_status, call_flow_uuid);
end
end);
end
-- release dbh
dbh:release()
--exit the loop when the file does not exist
if (not file_exists(run_file)) then
@@ -132,4 +117,6 @@
--sleep a moment to prevent using unecessary resources
freeswitch.msleep(sleep*1000);
end
end
log.notice("Stop")

View File

@@ -358,8 +358,7 @@
pos = string.find(ivr_menu_greet_long, ":", 0, true);
if (pos ~= nil and string.sub(ivr_menu_greet_long, 0, pos-1) == 'phrase') then
freeswitch.consoleLog("notice", "[ivr_menu] phrase detected\n");
session:playAndGetDigits(min_digits, ivr_menu_digit_len, 1, ivr_menu_timeout, ivr_menu_confirm_key, ivr_menu_greet_long, "", ".*");
dtmf_digits = session:getVariable("dtmf_digits");
dtmf_digits = session:playAndGetDigits(min_digits, ivr_menu_digit_len, 1, ivr_menu_timeout, ivr_menu_confirm_key, ivr_menu_greet_long, "", ".*");
session:setVariable("slept", "false");
else
dtmf_digits = session:playAndGetDigits(min_digits, ivr_menu_digit_len, 1, ivr_menu_timeout, ivr_menu_confirm_key, ivr_menu_greet_long, "", ".*");

View File

@@ -10,6 +10,13 @@ require "resources.functions.trim";
local api = api or freeswitch.API();
local function send_event(action, key)
local event = freeswitch.Event("MEMCACHE", action);
event:addHeader("API-Command", "memcache");
event:addHeader("API-Command-Argument", action .. " " .. key);
event:fire()
end
local Cache = {}
local function check_error(result)
@@ -57,6 +64,7 @@ function Cache.set(key, value, expire)
end
function Cache.del(key)
send_event('delete', key)
local result, err = check_error(api:execute("memcache", "delete " .. key))
if not result then
if err == 'NOT FOUND' then

View File

@@ -1,3 +1,7 @@
require 'resources.functions.config'
require 'resources.functions.trim'
local Database = require 'resources.functions.database'
local api = api or freeswitch.API()
@@ -18,3 +22,38 @@ function channel_evalute(uuid, cmd)
return result
end
local _switchname
local function switchname()
if _switchname then return _switchname end
local result = api:executeString("switchname")
if result:sub(1, 4) == '-ERR' then return nil, result end
if result == '_undef_' then return false end
_switchname = result
return result
end
function channels_by_number(number, domain)
local hostname = assert(switchname())
local dbh = Database.new('switch')
local full_number = number .. '@' .. (domain or '%')
local sql = ([[select * from channels where hostname='%s' and (
(context = '%s' and (cid_name = '%s' or cid_num = '%s'))
or name like '%s' or presence_id like '%s' or presence_data like '%s'
)
order by created_epoch
]]):format(hostname,
domain, number, number,
full_number, full_number, full_number
)
local rows = assert(dbh:fetch_all(sql))
dbh:release()
return rows
end

View File

@@ -1,4 +1,4 @@
require 'resources.config'
require 'resources.functions.config'
require 'resources.functions.file_exists'
require 'resources.functions.database_handle'

View File

@@ -0,0 +1,23 @@
local function turn_lamp(on, user, uuid)
local event = freeswitch.Event("PRESENCE_IN");
event:addHeader("proto", "sip");
event:addHeader("event_type", "presence");
event:addHeader("alt_event_type", "dialog");
event:addHeader("Presence-Call-Direction", "outbound");
event:addHeader("from", user);
event:addHeader("login", user);
event:addHeader("unique-id", uuid);
event:addHeader("status", "Active (1 waiting)");
if on then
event:addHeader("answer-state", "confirmed");
event:addHeader("rpid", "unknown");
event:addHeader("event_count", "1");
else
event:addHeader("answer-state", "terminated");
end
event:fire();
end
return {
turn_lamp = turn_lamp;
}