1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
-- Gemeinschaft 5 module: voicemail class
-- (c) AMOOMA GmbH 2013
--
module(...,package.seeall)
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,
}
-- create voicemail object
function Voicemail.new(self, arg)
arg = arg or {}
object = arg.object or {}
setmetatable(object, self);
self.__index = self;
self.class = 'voicemail';
self.log = arg.log;
self.database = arg.database;
self.record = arg.record;
self.domain = arg.domain;
return object
end
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;
voicemail_account.name = entry.name;
voicemail_account.settings = self:settings_get(entry.id);
end);
return voicemail_account;
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.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
function Voicemail.check_pin(self, pin)
self.caller:answer();
self.caller:sleep(1000);
require 'dialplan.ivr';
local ivr = dialplan.ivr.Ivr:new{ caller = self.caller, log = self.log };
local digits = '';
for i = 1, 3 do
if digits == pin then
self.caller:send_display('PIN: OK');
break
elseif digits ~= "" then
self.caller:send_display('PIN: wrong');
end
self.caller:send_display('Enter PIN');
digits = ivr:read_phrase('voicemail_enter_pass', nil, self.settings.pin_length_min, self.settings.pin_length_max, self.settings.pin_timeout, self.settings.terminator_key);
end
if digits ~= pin then
return false
end
return true;
end
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.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.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
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 self.settings.voicemail_message_count 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
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 index > #messages then
digits, key = self.ivr:ivr_phrase('voicemail_no_messages', message_menu, 0, 0);
return;
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
end
end
function Voicemail.menu_options(self)
self.log:info('VOICEMAIL_OPTIONS_MENU');
self.caller:send_display('Voicemail options');
end
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.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
function Voicemail.leave(self, caller, greeting)
local forwarding_number = caller.forwarding_number;
self.log:info('VOICEMAIL_LEAVE - voicemail_account=', self.record.id, '/', self.record.uuid, '|', self.record.name, ', forwarding_number: ', forwarding_number);
caller:set_callee_id(forwarding_number, common.array.try(caller, 'auth_account.caller_name') or common.array.try(caller, 'auth_account.name'));
caller:answer();
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(forwarding_number) then
caller.session:sayPhrase('voicemail_play_greeting', (tostring(forwarding_number):gsub('[%D]', '')));
end
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));
require 'dialplan.ivr';
local ivr = dialplan.ivr.Ivr:new{ caller = caller, log = self.log };
local duration = ivr:record(
record_file_name,
'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 >= 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.name ..
'@' .. self.domain .. " '" ..
record_file_name .. "' '" ..
caller.caller_id_number .. "' '" ..
caller.caller_id_name .. "' '" ..
caller.uuid .. "'"
);
caller:set_variable('voicemail_message_len', duration);
self:trigger_notification(caller);
else
caller:set_variable('voicemail_message_len');
end
os.remove(record_file_name);
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?voicemail_account_id=' .. tostring(self.id);
require 'common.fapi';
return common.fapi.FApi:new():execute('luarun', command);
end
|