diff options
author | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2013-06-20 19:06:19 +0200 |
---|---|---|
committer | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2013-06-20 19:06:19 +0200 |
commit | eb0e1cc5c26275ff3e5c341404e8bc558f8312b8 (patch) | |
tree | 71f449ccd6f15422717de3ac24f87d5e888ddd79 /misc/freeswitch/scripts/dialplan/voicemail.lua | |
parent | df6e17e48995f25e72509986f30700d778b179b6 (diff) | |
parent | 3b27a5d45b12f6bac65da2a8e17387bfda42a2f1 (diff) |
Merge branch 'develop'
Diffstat (limited to 'misc/freeswitch/scripts/dialplan/voicemail.lua')
-rw-r--r-- | misc/freeswitch/scripts/dialplan/voicemail.lua | 506 |
1 files changed, 436 insertions, 70 deletions
diff --git a/misc/freeswitch/scripts/dialplan/voicemail.lua b/misc/freeswitch/scripts/dialplan/voicemail.lua index caeeb48..3358d2b 100644 --- a/misc/freeswitch/scripts/dialplan/voicemail.lua +++ b/misc/freeswitch/scripts/dialplan/voicemail.lua @@ -1,17 +1,41 @@ -- Gemeinschaft 5 module: voicemail class --- (c) AMOOMA GmbH 2012-2013 +-- (c) AMOOMA GmbH 2013 -- module(...,package.seeall) Voicemail = {} -MESSAGE_LENGTH_MIN = 3; -MESSAGE_LENGTH_MAX = 120; -SILENCE_LENGTH_ABORT = 5; -SILENCE_LEVEL = 500; -BEEP = 'tone_stream://%(1000,0,500)'; -RECORD_FILE_PREFIX = '/var/spool/freeswitch/voicemail_'; +DEFAULT_SETTINGS = { + pin_length_max = 10, + pin_length_min = 2, + pin_timeout = 20, + key_new_messages = '1', + key_saved_messages = '2', + key_config_menu = '0', + key_terminator = '#', + key_previous = '4', + key_next = '6', + key_delete = '7', + key_save = '2', + key_main_menu = '#', + record_length_max = 300, + record_length_min = 4, + records_max = 100, + silence_lenght_abort = 3, + silence_level = 500, + beep = 'tone_stream://%(1000,0,500)', + record_file_prefix = 'voicemail_', + record_file_suffix = '.wav', + record_file_path = '/var/spool/freeswitch/', + record_repeat = 3, + notify = true, + attachment = true, + mark_read = true, + purge = false, + generic_file_path = '/var/opt/gemeinschaft/generic_files/', +} + -- create voicemail object function Voicemail.new(self, arg) @@ -23,93 +47,296 @@ function Voicemail.new(self, arg) self.log = arg.log; self.database = arg.database; self.record = arg.record; + self.domain = arg.domain; return object end --- find voicemail account by sip account id -function Voicemail.find_by_sip_account_id(self, id) - local sql_query = 'SELECT `a`.`id`, `a`.`uuid`, `a`.`auth_name`, `a`.`caller_name`, `b`.`name_path`, `b`.`greeting_path`, `a`.`voicemail_pin`, `b`.`password`, `c`.`host` AS `domain` \ - FROM `sip_accounts` `a` LEFT JOIN `voicemail_prefs` `b` ON `a`.`auth_name` = `b`.`username` \ - JOIN `sip_domains` `c` ON `a`.`sip_domain_id` = `c`.`id` \ - WHERE `a`.`id` = ' .. tonumber(id); +function Voicemail.find_by_sql(self, sql_query) local voicemail_account = nil; self.database:query(sql_query, function(entry) voicemail_account = Voicemail:new(self); voicemail_account.record = entry; voicemail_account.id = tonumber(entry.id); voicemail_account.uuid = entry.uuid; - end) + voicemail_account.name = entry.name; + voicemail_account.settings = self:settings_get(entry.id); + end); return voicemail_account; end --- Find Voicemail account by name -function Voicemail.find_by_name(self, account_name) - id = tonumber(id) or 0; - local sql_query = string.format('SELECT * FROM `voicemail_prefs` WHERE `username`= "%s" LIMIT 1', account_name) - local record = nil - self.database:query(sql_query, function(voicemail_entry) - record = voicemail_entry - end) +function Voicemail.find_by_name(self, name) + local sql_query = 'SELECT * FROM `voicemail_accounts` \ + WHERE `name` = ' .. self.database:escape(name, '"') .. ' LIMIT 1'; + + return self:find_by_sql(sql_query); +end + + +function Voicemail.find_by_id(self, id) + local sql_query = 'SELECT * FROM `voicemail_accounts` \ + WHERE `id` = ' .. self.database:escape(id, '"') .. ' LIMIT 1'; + + return self:find_by_sql(sql_query); +end + + +function Voicemail.find_by_sip_account_id(self, id) + local sql_query = 'SELECT `a`.* FROM `voicemail_accounts` `a` \ + JOIN `sip_accounts` `b` ON `a`.`id` = `b`.`voicemail_account_id` \ + WHERE `b`.`id` = ' .. self.database:escape(id, '"') .. ' LIMIT 1'; + + return self:find_by_sql(sql_query); +end + + +function Voicemail.find_by_number(self, number) + local sql_query = 'SELECT `a`.* FROM `voicemail_accounts` `a` \ + JOIN `sip_accounts` `b` ON `a`.`id` = `b`.`voicemail_account_id` \ + JOIN `phone_numbers` `c` ON `b`.`id` = `c`.`phone_numberable_id` \ + WHERE `c`.`number` = ' .. self.database:escape(number, '"') .. ' \ + AND `c`.`phone_numberable_type` = "SipAccount" LIMIT 1'; + + return self:find_by_sql(sql_query); +end + + +function Voicemail.settings_get(self, id) + require 'common.configuration_table'; + local parameters = common.configuration_table.get(self.database, 'voicemail', 'settings', { settings = DEFAULT_SETTINGS }); + + return common.configuration_table.settings(self.database, 'voicemail_settings', 'voicemail_account_id', id or self.id, parameters) +end + + +function Voicemail.find_message_by_uuid(self, uuid) + local sql_query = 'SELECT * FROM `voicemail_msgs` WHERE `uuid` = ' .. self.database:escape(uuid, '"') .. ' LIMIT 1'; + return self.database:query_return_first(sql_query); +end + + +function Voicemail.messages_get(self, status) + local sql_query = 'SELECT * FROM `voicemail_msgs` WHERE `username` = ' .. self.database:escape(self.name, '"'); + + status = status or 'all'; + + if status == 'read' then + sql_query = sql_query .. ' AND `read_epoch` > 0'; + elseif status == 'unread' then + sql_query = sql_query .. ' AND (`read_epoch` = 0 OR `read_epoch` IS NULL)'; + elseif status == 'new' then + sql_query = sql_query .. ' AND `in_folder` != "save" AND `flags` != "save"'; + elseif status == 'saved' then + sql_query = sql_query .. ' AND (`in_folder` = "save" OR `flags` = "save")'; + end + + return self.database:query_return_all(sql_query); +end - if voicemail_account then - voicemail_account.account_name = account_name; - if record then - voicemail_account.name_path = record.name_path; - voicemail_account.greeting_path = record.greeting_path; - voicemail_account.password = record.password; + +function Voicemail.menu_main(self, caller, authorized) + self.caller = caller; + + require 'dialplan.ivr'; + self.ivr = dialplan.ivr.Ivr:new{ caller = self.caller, log = self.log }; + + if not authorized then + if common.str.blank(self.settings.pin) then + self.log:notice('VOICEMAIL_MAIN_MENU - unaunthorized, no PIN, ', self.class, '=', self.id, '/', self.uuid, '|', self.name); + return { continue = false, code = 500, phrase = 'Unauthorized', no_cdr = true } + end + + self.caller:answer(); + self.caller:sleep(1000); + + if not self.ivr:check_pin('voicemail_enter_pass', 'voicemail_fail_auth', self.settings.pin) then + self.log:notice('VOICEMAIL_MAIN_MENU - wrong PIN, ', self.class, '=', self.id, '/', self.uuid, '|', self.name); + caller.session:sayPhrase('voicemail_goodbye'); + return { continue = false, code = 500, phrase = 'Unauthorized', no_cdr = true } end end - return voicemail_account + local messages_new = self:messages_get('unread'); + local messages_saved = self:messages_get('read'); + + if not caller:answered() then + self.caller:answer(); + self.caller:sleep(1000); + end + + if self.settings.voicemail_hello then + caller.session:sayPhrase('voicemail_hello'); + end + + if #messages_new > 0 then + caller.session:sayPhrase('voicemail_message_count', #messages_new .. ':new'); + end + + while true do + self.log:info('VOICEMAIL_MAIN_MENU - ', self.class, '=', self.id, '/', self.uuid, '|', self.name, ', messages: ', #messages_new, ':', #messages_saved); + self.caller:send_display(#messages_new .. ' new messages'); + + local main_menu = { + { key = self.settings.key_new_messages, method = self.menu_messages, parameters = { self, 'new', messages_new } }, + { key = self.settings.key_saved_messages, method = self.menu_messages, parameters = { self, 'saved', messages_saved } }, + { key = self.settings.key_config_menu, method = self.menu_options, parameters = { self } }, + { key = self.settings.key_terminator, exit = true }, + { key = '', exit = true }, + }; + + local digits, key = self.ivr:ivr_phrase('voicemail_menu', main_menu); + self.log:debug('VOICEMAIL_MAIN_MENU - digit: ', digits); + if key.exit then + break; + end + + key.method(unpack(key.parameters)); + + messages_new = self:messages_get('unread'); + messages_saved = self:messages_get('read'); + end + self.caller:send_display('Goodbye'); + caller.session:sayPhrase('voicemail_goodbye'); end --- Find Voicemail account by number -function Voicemail.find_by_number(self, phone_number) - local sip_account = nil; - require "common.phone_number" - local destination_number_object = common.phone_number.PhoneNumber:new{ log = self.log, database = self.database }:find_by_number(phone_number); - if destination_number_object and destination_number_object.record.phone_numberable_type:lower() == "sipaccount" then - return self:find_by_sip_account_id(destination_number_object.record.phone_numberable_id); +function Voicemail.menu_messages(self, folder, messages) + self.log:info('VOICEMAIL_MESSAGES_MENU - ', folder,' messages: ', #messages); + + local digits = nil; + local key = nil; + + local message_menu = { + { key = self.settings.key_previous, action = 'previous' }, + { key = self.settings.key_delete, action = 'delete' }, + { key = self.settings.key_save, action = 'save' }, + { key = self.settings.key_next, action = 'next' }, + { key = self.settings.key_main_menu, exit = true }, + }; + + if folder == 'saved' then + message_menu = { + { key = self.settings.key_previous, action = 'previous' }, + { key = self.settings.key_delete, action = 'delete' }, + { key = self.settings.key_next, action = 'next' }, + { key = self.settings.key_main_menu, exit = true }, + }; + end + + if #messages == 0 then + digits, key = self.ivr:ivr_phrase('voicemail_no_messages', message_menu, 0, 0); + return; + end + + local index = 1; + while index <= #messages do + local message = messages[index]; + self.caller:send_display(index .. ': ' .. message.cid_name .. ' ' .. message.cid_number); + digits, key = self.ivr:ivr_phrase('voicemail_message_play', message_menu, 0, 0, + index .. ':' .. message.created_epoch .. ':' .. message.file_path + ); + if digits == '' then + if common.str.to_i(message.read_epoch) == 0 then + self:message_mark_read(message); + end + digits, key = self.ivr:ivr_phrase('voicemail_message_menu_' .. folder, message_menu, 15, 0); + end + + if not key or key.exit then + break; + end + + if key.action == 'previous' then + if index > 1 then + index = index - 1; + end + else + index = index + 1; + end + + if key.action == 'delete' and self:message_delete(message) then + self.caller:send_display('Message deleted'); + digits = self.caller.session:sayPhrase('voicemail_ack', 'deleted'); + elseif key.action == 'save' and self:message_save(message) then + self.caller:send_display('Message saved'); + digits = self.caller.session:sayPhrase('voicemail_ack', 'saved'); + end + if index > #messages then + digits = self.ivr:ivr_phrase('voicemail_no_messages', message_menu, 0, 0); + end end +end - return false; + +function Voicemail.message_delete(self, message) + self.log:debug('VOICEMAIL_MESSAGE_DELETE - message: ', message.uuid); + require 'common.fapi'; + return common.fapi.FApi:new{ log = self.log }:execute('vm_delete', message.username .. '@' .. message.domain .. ' ' .. message.uuid); +end + +function Voicemail.message_mark_read(self, message) + self.log:debug('VOICEMAIL_MESSAGE_MARK_READ - message: ', message.uuid); + require 'common.fapi'; + return common.fapi.FApi:new{ log = self.log }:execute('vm_read', message.username .. '@' .. message.domain .. ' read ' .. message.uuid); end -function Voicemail.leave(self, caller, phone_number) - require 'common.str' +function Voicemail.message_save(self, message) + self.log:debug('VOICEMAIL_MESSAGE_SAVE - message: ', message.uuid); + require 'common.fapi'; + return common.fapi.FApi:new{ log = self.log }:execute('vm_fsdb_msg_save', 'default ' .. message.domain .. ' ' .. message.username .. ' ' .. message.uuid); +end + - self.log:info('VOICEMAIL_LEAVE - account=', self.record.id, '/', self.record.uuid, ', auth_name: ', self.record.auth_name, ', caller_name: ', self.record.caller_name); +function Voicemail.leave(self, caller, greeting, number) + self.log:info('VOICEMAIL_LEAVE - voicemail_account=', self.record.id, '/', self.record.uuid, '|', self.record.name, ', forwarding_number: ', number, ', greeting: ', greeting); - caller:set_callee_id(phone_number, self.record.caller_name); + caller:set_callee_id(number, common.array.try(caller, 'auth_account.caller_name') or common.array.try(caller, 'auth_account.name')); caller:answer(); - caller:send_display(common.str.to_s(self.record.caller_name), common.str.to_s(phone_number)); + caller:send_display(common.array.try(caller, 'auth_account.caller_name') or common.array.try(caller, 'auth_account.name')); caller:sleep(1000); - if not common.str.blank(self.record.greeting_path) then - caller.session:sayPhrase('voicemail_play_greeting', 'greeting:' .. tostring(self.record.greeting_path)); - elseif not common.str.blank(self.record.name_path) then - caller.session:sayPhrase('voicemail_play_greeting', 'name:' .. tostring(self.record.name_path)); - elseif not common.str.blank(phone_number) then - caller.session:sayPhrase('voicemail_play_greeting', (tostring(phone_number):gsub('[%D]', ''))); + local greeting_file = nil; + if not common.str.blank(greeting) then + greeting_file = self:greeting_get(greeting, caller.auth_account.owner.class, caller.auth_account.owner.id) + end + + if not common.str.blank(greeting_file) then + self.log:debug('VOICEMAIL_LEAVE greeting_file: ', greeting_file); + caller:playback(greeting_file); + elseif not common.str.blank(number) then + caller.session:sayPhrase('voicemail_play_greeting', (tostring(number):gsub('[%D]', ''))); end - local record_file_name = RECORD_FILE_PREFIX .. caller.uuid .. '.wav'; - caller.session:streamFile(BEEP); + local record_file_name = self.settings.record_file_path ..self.settings.record_file_prefix .. caller.uuid .. self.settings.record_file_suffix; self.log:info('VOICEMAIL_LEAVE - recording to file: ', tostring(record_file_name)); - local result = caller.session:recordFile(record_file_name, MESSAGE_LENGTH_MAX, SILENCE_LEVEL, SILENCE_LENGTH_ABORT); - local duration = caller:to_i('record_seconds'); + + local voicemail_record_message = nil; + if common.str.blank(greeting_file) then + voicemail_record_message = 'voicemail_record_message'; + end + + require 'dialplan.ivr'; + local ivr = dialplan.ivr.Ivr:new{ caller = caller, log = self.log }; + local duration = ivr:record( + record_file_name, + self.settings.beep, + voicemail_record_message, + 'voicemail_message_too_short', + self.settings.record_length_max, + self.settings.record_length_min, + self.settings.record_repeat, + self.settings.silence_level, + self.settings.silence_lenght_abort); - if duration >= MESSAGE_LENGTH_MIN then + if duration >= self.settings.record_length_min then self.log:info('VOICEMAIL_LEAVE - saving recorded message to voicemail, duration: ', duration); require 'common.fapi' common.fapi.FApi:new{ log = self.log, uuid = caller.uuid }:execute('vm_inject', - self.record.auth_name .. - '@' .. self.record.domain .. " '" .. + self.record.name .. + '@' .. self.domain .. " '" .. record_file_name .. "' '" .. caller.caller_id_number .. "' '" .. caller.caller_id_name .. "' '" .. @@ -121,32 +348,171 @@ function Voicemail.leave(self, caller, phone_number) caller:set_variable('voicemail_message_len'); end os.remove(record_file_name); - return true; + caller:send_display('Goodbye'); + caller.session:sayPhrase('voicemail_goodbye'); end function Voicemail.trigger_notification(self, caller) - local command = 'http_request.lua ' .. caller.uuid .. ' http://127.0.0.1/trigger/voicemail?sip_account_id=' .. tostring(self.id); + local command = 'http_request.lua ' .. caller.uuid .. ' http://127.0.0.1/trigger/voicemail?voicemail_account_id=' .. tostring(self.id); - require 'common.fapi' + require 'common.fapi'; return common.fapi.FApi:new():execute('luarun', command); end -function Voicemail.menu(self, caller, authorized) - self.log:info('VOICEMAIL_MENU - account: ', self.record.auth_name); +function Voicemail.message_play(self, caller, uuid) + local message = self:find_message_by_uuid(uuid); - if authorized then - caller:set_variable('voicemail_authorized', true); + if message and message.file_path then + if not caller:answered() then + caller:answer(); + caller:sleep(1000); + end + caller:send_display(message.cid_name .. ' ' .. message.cid_number); + caller:playback(message.file_path); end - caller:set_callee_id(phone_number, self.record.caller_name); - caller:answer(); - caller:send_display(common.str.to_s(self.record.caller_name), common.str.to_s(phone_number)); + return message; +end - caller:sleep(1000); - caller:set_variable('skip_greeting', true); - caller:set_variable('skip_instructions', true); - - caller:execute('voicemail', 'check default ' .. self.record.domain .. ' ' .. self.record.auth_name); + +function Voicemail.greeting_get(self, name, owner_type, owner_id, file_types) + file_types = file_types or {'audio/x-wav'} + local sql_query = 'SELECT * FROM `generic_files` \ + WHERE `name` = ' .. self.database:escape(name, '"') .. ' \ + AND `owner_type` = ' .. self.database:escape(owner_type, '"') .. ' \ + AND `owner_id` = ' .. self.database:escape(owner_id, '"') .. ' \ + AND `file_type` IN ("' .. table.concat(file_types, '","') .. '") LIMIT 1'; + + local greeting = self.database:query_return_first(sql_query); + + if not greeting or common.str.blank(greeting.file) then + return nil; + end + + return self.settings.generic_file_path .. greeting.id .. '/' .. greeting.file; end + + +function Voicemail.menu_options(self) + self.log:info('VOICEMAIL_OPTIONS_MENU'); + self.caller:send_display('Voicemail options'); + + local menu = { + { key = '1', method = self.greeting_record, parameters = { self } }, + { key = '3', method = self.pin_change, parameters = { self } }, + { key = self.settings.key_terminator, exit = true }, + { key = '', exit = true }, + }; + + while true do + local digits, key = self.ivr:ivr_phrase('voicemail_config_menu', menu); + self.log:debug('VOICEMAIL_MAIN_MENU - digit: ', digits); + if key.exit then + break; + end + + key.method(unpack(key.parameters)); + end +end + + +function Voicemail.greeting_record(self) + self.log:info('VOICEMAIL_GREETING_RECORD'); + + local record_file_name = self.settings.record_file_path ..self.settings.record_file_prefix .. self.caller.uuid .. self.settings.record_file_suffix; + + require 'dialplan.ivr'; + local ivr = dialplan.ivr.Ivr:new{ caller = self.caller, log = self.log }; + local duration = ivr:record( + record_file_name, + self.settings.beep, + 'voicemail_record_greeting', + 'voicemail_message_too_short', + self.settings.record_length_max, + 1, + self.settings.record_repeat, + self.settings.silence_level, + self.settings.silence_lenght_abort); + + if duration >= 0 then + self.caller:playback(record_file_name); + end + + if duration >= 1 then + local name = 'greeting_' .. os.time(); + local file_name = name .. self.settings.record_file_suffix + + local generic_file_record = { + name = name, + file = file_name, + file_type = 'audio/x-wav', + category = 'greeting', + owner_id = self.record.voicemail_accountable_id, + owner_type = self.record.voicemail_accountable_type, + updated_at = { 'NOW()', raw = true }, + created_at = { 'NOW()', raw = true }, + }; + + if self.database:insert_or_update('generic_files', generic_file_record) then + local file_id = self.database:last_insert_id(); + + local destination_directory = self.settings.generic_file_path .. file_id; + self.log:info('VOICEMAIL_GREETING_RECORD recorded greeting - id: ', file_id, ', file: ', destination_directory .. '/' .. file_name, ', duration: ', duration); + os.execute('mkdir ' .. destination_directory); + local result, error_string = os.rename(record_file_name, destination_directory .. '/' .. file_name); + if not result then + self.log:error('VOICEMAIL_GREETING_RECORD - ', error_string); + end + end + end +end + + +function Voicemail.pin_change(self) + self.log:info('VOICEMAIL_PIN_CHANGE - lenght: ', self.settings.pin_length_min, '-', self.settings.pin_length_max); + + if not common.str.blank(self.settings.pin) then + if not self.ivr:check_pin('voicemail_enter_pass', 'voicemail_fail_auth', self.settings.pin) then + self.log:notice('VOICEMAIL_PIN_CHANGE - wrong old PIN, ', self.class, '=', self.id, '/', self.uuid, '|', self.name); + return false; + end + end + + local digits = ''; + for i = 1, 3 do + if digits:len() < self.settings.pin_length_min then + self.caller:send_display('PIN too short'); + elseif digits:len() > self.settings.pin_length_max then + self.caller:send_display('PIN too long'); + else + self.caller:send_display('PIN: OK'); + break + end + self.caller:send_display('Enter new PIN'); + digits = self.ivr:read_phrase('voicemail_enter_pass', nil, self.settings.pin_length_max, self.settings.pin_length_min, self.settings.pin_timeout, self.settings.terminator_key); + end + + if digits:len() < self.settings.pin_length_min or digits:len() > self.settings.pin_length_max then + self.caller:send_display('PIN not changed'); + return false; + end + + local sql_query = 'UPDATE `voicemail_settings` \ + SET `value` = ' .. self.database:escape(digits, '"') .. ', `class_type` = "String", `updated_at` = NOW() \ + WHERE `name`="pin" AND `voicemail_account_id` = ' .. self.id; + if not self.settings.pin then + sql_query = 'INSERT INTO `voicemail_settings` \ + (`voicemail_account_id`, `name`, `value`, `class_type`, `description`, `created_at`, `updated_at`) \ + VALUES (' .. self.id .. ', "pin", "' .. digits .. '", "String", "Voicemail PIN", NOW(), NOW())'; + end + + if self.database:query(sql_query) then + self.settings.pin = digits; + self.caller:send_display('PIN changed'); + self.caller.session:sayPhrase('voicemail_change_pass_success'); + return true; + end +end + |