1-- Part of FusionPBX 2-- Copyright (C) 2013-2015 Mark J Crane <markjcrane@fusionpbx.com> 3-- All rights reserved. 4-- 5-- Redistribution and use in source and binary forms, with or without 6-- modification, are permitted provided that the following conditions are met: 7-- 8-- 1. Redistributions of source code must retain the above copyright notice, 9-- this list of conditions and the following disclaimer. 10-- 11-- 2. Redistributions in binary form must reproduce the above copyright 12-- notice, this list of conditions and the following disclaimer in the 13-- documentation and/or other materials provided with the distribution. 14-- 15-- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 16-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 18-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 19-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24-- POSSIBILITY OF SUCH DAMAGE. 25 26--record message menu 27 function record_menu(type, tmp_file, greeting_id, menu) 28 if (session:ready()) then 29 --clear the dtmf digits variable 30 dtmf_digits = ''; 31 --flush dtmf digits from the input buffer 32 session:flushDigits(); 33 --to listen to the recording press 1 34 if (session:ready()) then 35 if (string.len(dtmf_digits) == 0) then 36 dtmf_digits = macro(session, "to_listen_to_recording", 1, 100, ''); 37 end 38 end 39 --to save the recording press 2 40 if (session:ready()) then 41 if (string.len(dtmf_digits) == 0) then 42 dtmf_digits = macro(session, "to_save_recording", 1, 100, ''); 43 end 44 end 45 --to re-record press 3 46 if (session:ready()) then 47 if (string.len(dtmf_digits) == 0) then 48 dtmf_digits = macro(session, "to_rerecord", 1, 3000, ''); 49 end 50 end 51 --process the dtmf 52 if (session:ready()) then 53 if (dtmf_digits == "1") then 54 --listen to the recording 55 session:streamFile(tmp_file); 56 --session:streamFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext); 57 --record menu (1=listen, 2=save, 3=re-record) 58 record_menu(type, tmp_file, greeting_id, menu); 59 elseif (dtmf_digits == "2") then 60 --save the message 61 dtmf_digits = ''; 62 macro(session, "message_saved", 1, 100, ''); 63 if (type == "message") then 64 --goodbye 65 macro(session, "goodbye", 1, 100, ''); 66 --hangup the call 67 session:hangup(); 68 end 69 if (type == "greeting") then 70 --remove old greeting file, and rename tmp file 71 local real_file = string.gsub(tmp_file, ".tmp", ""); 72 if (file_exists(real_file)) then 73 os.remove(real_file); 74 end 75 if (file_exists(tmp_file)) then 76 os.rename(tmp_file, real_file); 77 end 78 if (storage_type == "base64") then 79 --delete the greeting (retain local for better responsiveness) 80 --os.remove(real_file); 81 end 82 83 --if base64, encode file 84 if (storage_type == "base64") then 85 --include the file io 86 local file = require "resources.functions.file" 87 88 --read file content as base64 string 89 greeting_base64 = assert(file.read_base64(real_file)); 90 end 91 92 --delete the previous recording 93 local sql = "delete from v_voicemail_greetings "; 94 sql = sql .. "where domain_uuid = :domain_uuid "; 95 sql = sql .. "and voicemail_id = :voicemail_id "; 96 sql = sql .. "and greeting_id = :greeting_id "; 97 local params = {domain_uuid = domain_uuid, 98 voicemail_id = voicemail_id, greeting_id = greeting_id}; 99 --freeswitch.consoleLog("notice", "[SQL] DELETING: " .. greeting_id .. "\n"); 100 dbh:query(sql, params); 101 102 --get a new uuid 103 voicemail_greeting_uuid = api:execute("create_uuid"); 104 105 --save the message to the voicemail messages 106 local array = {} 107 table.insert(array, "INSERT INTO v_voicemail_greetings "); 108 table.insert(array, "("); 109 table.insert(array, "voicemail_greeting_uuid, "); 110 table.insert(array, "domain_uuid, "); 111 table.insert(array, "voicemail_id, "); 112 table.insert(array, "greeting_id, "); 113 if (storage_type == "base64") then 114 table.insert(array, "greeting_base64, "); 115 end 116 table.insert(array, "greeting_name, "); 117 table.insert(array, "greeting_filename "); 118 table.insert(array, ") "); 119 table.insert(array, "VALUES "); 120 table.insert(array, "( "); 121 table.insert(array, ":greeting_uuid, "); 122 table.insert(array, ":domain_uuid, "); 123 table.insert(array, ":voicemail_id, "); 124 table.insert(array, ":greeting_id, "); 125 if (storage_type == "base64") then 126 table.insert(array, ":greeting_base64, "); 127 end 128 table.insert(array, ":greeting_name, "); 129 table.insert(array, ":greeting_filename "); 130 table.insert(array, ") "); 131 sql = table.concat(array, "\n"); 132 params = { 133 greeting_uuid = voicemail_greeting_uuid; 134 domain_uuid = domain_uuid; 135 voicemail_id = voicemail_id; 136 greeting_id = greeting_id; 137 greeting_base64 = greeting_base64; 138 greeting_name = "Greeting "..greeting_id; 139 greeting_filename = "greeting_"..greeting_id..".wav" 140 }; 141 --freeswitch.consoleLog("notice", "[SQL] INSERTING: " .. greeting_id .. "\n"); 142 if (debug["sql"]) then 143 freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); 144 end 145 if (storage_type == "base64") then 146 local dbh = Database.new('system', 'base64'); 147 dbh:query(sql, params); 148 dbh:release(); 149 else 150 dbh:query(sql, params); 151 end 152 153 --use the new greeting 154 sql = {} 155 table.insert(sql, "update v_voicemails "); 156 table.insert(sql, "set greeting_id = :greeting_id "); 157 table.insert(sql, "where domain_uuid = :domain_uuid "); 158 table.insert(sql, "and voicemail_id = :voicemail_id "); 159 sql = table.concat(sql, "\n"); 160 params = {domain_uuid = domain_uuid, greeting_id = greeting_id, 161 voicemail_id = voicemail_id}; 162 dbh:query(sql, params); 163 164 if (menu == "advanced") then 165 advanced(); 166 end 167 if (menu == "tutorial") then 168 tutorial("finish") 169 end 170 end 171 if (type == "name") then 172 if (menu == "advanced") then 173 advanced(); 174 end 175 if (menu == "tutorial") then 176 tutorial("change_password") 177 end 178 179 end 180 elseif (dtmf_digits == "3") then 181 --re-record the message 182 timeouts = 0; 183 dtmf_digits = ''; 184 if (type == "message") then 185 record_message(); 186 end 187 if (type == "greeting") then 188 --remove temporary greeting file, if any 189 if (file_exists(tmp_file)) then 190 os.remove(tmp_file); 191 end 192 record_greeting(greeting_id, menu); 193 end 194 if (type == "name") then 195 record_name(menu); 196 end 197 elseif (dtmf_digits == "*") then 198 if (type == "greeting") then 199 --remove temporary greeting file, if any 200 if (file_exists(tmp_file)) then 201 os.remove(tmp_file); 202 end 203 end 204 --hangup 205 if (session:ready()) then 206 dtmf_digits = ''; 207 macro(session, "goodbye", 1, 100, ''); 208 session:hangup(); 209 end 210 else 211 if (session:ready()) then 212 timeouts = timeouts + 1; 213 if (timeouts < max_timeouts) then 214 record_menu(type, tmp_file, greeting_id, menu); 215 else 216 if (type == "message") then 217 dtmf_digits = ''; 218 macro(session, "message_saved", 1, 100, ''); 219 macro(session, "goodbye", 1, 1000, ''); 220 session:hangup(); 221 end 222 if (type == "greeting") then 223 --remove temporary greeting file, if any 224 if (file_exists(tmp_file)) then 225 os.remove(tmp_file); 226 end 227 if (menu == "advanced") then 228 advanced(); 229 end 230 if (menu == "tutorial") then 231 tutorial("finish") 232 end 233 end 234 if (type == "name") then 235 if (menu == "advanced") then 236 advanced(); 237 end 238 if (menu == "tutorial") then 239 tutorial("change_password") 240 end 241 end 242 end 243 end 244 end 245 end 246 end 247 end 248