diff options
author | Ted Trask <ttrask01@yahoo.com> | 2015-04-12 15:41:57 -0400 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2015-04-12 15:41:57 -0400 |
commit | 25724c1c496f06e1f5bb7956583d4a1d85a03722 (patch) | |
tree | 35e728a3ff86f491a0026f4192170710216cd8c5 | |
parent | 59364f1b5c84cf48ea98e9c4107dfa49b5eaac59 (diff) | |
download | acf-freeswitch-vmail-25724c1c496f06e1f5bb7956583d4a1d85a03722.tar.bz2 acf-freeswitch-vmail-25724c1c496f06e1f5bb7956583d4a1d85a03722.tar.xz |
Fix Event Socket access by using luasocket library rather than nc and run_executable
-rw-r--r-- | vmail-model.lua | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/vmail-model.lua b/vmail-model.lua index 2e85b85..9264c3d 100644 --- a/vmail-model.lua +++ b/vmail-model.lua @@ -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() |