Commit 25724c1c authored by Ted Trask's avatar Ted Trask

Fix Event Socket access by using luasocket library rather than nc and run_executable

parent 59364f1b
......@@ -8,6 +8,7 @@ format = require("acf.format")
validator = require("acf.validator")
db = require("acf.db")
session = require("session")
socket = require("socket")
-- Set variables
local configfile = "/etc/freeswitchvmail.conf"
......@@ -82,37 +83,52 @@ vmaildb.table_creation_scripts = {
-- ################################################################################
-- LOCAL FUNCTIONS
-- Use socket.protect to create a function that will not throw exceptions and cleans itself up
local send_to_event_socket = socket.protect(function(cmd)
-- connect to freeswitch
local conn = socket.try(socket.connect(config.event_socket_ip, config.event_socket_port))
-- create a try function that closes 'conn' on error
local try = socket.newtry(function() conn:close() end)
-- do everything reassured conn will be closed
local out = {}
repeat
out[#out+1] = try(conn:receive())
until out[#out] == ""
for i,c in ipairs(cmd) do
posix.sleep(0)
try(conn:send(c.."\n\n"))
repeat
out[#out+1] = try(conn:receive())
until out[#out] == ""
end
conn:close()
return table.concat(out, "\n") or ""
end)
local function voicemail_inject(user, domain, sound_file, cid_num, cid_name)
local cmd = "auth "..config.event_socket_password.."\n\n"
cmd = cmd.."api voicemail_inject "..user.."@"..domain.." "..sound_file.." "..cid_num.." "..string.gsub(cid_name, " ", "%%20")
cmd = cmd.."\n\nexit\n\n"
return modelfunctions.run_executable({"nc", config.event_socket_ip, config.event_socket_port}, true, cmd)
local cmd = {"auth "..config.event_socket_password, "api voicemail_inject "..user.."@"..domain.." "..sound_file.." "..cid_num.." "..string.gsub(cid_name, " ", "%%20"), "exit"}
return send_to_event_socket(cmd)
end
local function voicemail_callback(extension, sound_file, username)
local cmd = "auth "..config.event_socket_password.."\n\nbgapi "
local c = config.callback_command
c = c:gsub("%$1", extension)
c = c:gsub("%$2", sound_file)
c = c:gsub("%$3", username)
cmd = cmd..c
cmd = cmd.."\n\nexit\n\n"
return modelfunctions.run_executable({"nc", config.event_socket_ip, config.event_socket_port}, true, cmd)
local cmd = {"auth "..config.event_socket_password, "bgapi "..c, "exit"}
return send_to_event_socket(cmd)
end
-- Need to update the voicemail module to turn off MWI
local function voicemail_update(user, domain)
local cmd = "auth "..config.event_socket_password.."\n\n"
cmd = cmd.."api vm_list "..user.."@"..domain
cmd = cmd.."\n\nexit\n\n"
return modelfunctions.run_executable({"nc", config.event_socket_ip, config.event_socket_port}, true, cmd)
local cmd = {"auth "..config.event_socket_password, "api vm_list "..user.."@"..domain, "exit"}
return send_to_event_socket(cmd)
end
local function voicemail_read(user, domain, message)
local cmd = "auth "..config.event_socket_password.."\n\n"
cmd = cmd.."api vm_read "..user.."@"..domain.." read "..message
cmd = cmd.."\n\nexit\n\n"
return modelfunctions.run_executable({"nc", config.event_socket_ip, config.event_socket_port}, true, cmd)
local cmd = {"auth "..config.event_socket_password, "api vm_read "..user.."@"..domain.." read "..message, "exit"}
return send_to_event_socket(cmd)
end
local databasedisconnect = function()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment