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
|
-- Gemeinschaft 5 module: hunt group class
-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
HuntGroup = {}
local DEFAULT_MEMBER_TIMEOUT = 20;
-- Create HuntGroup object
function HuntGroup.new(self, arg)
arg = arg or {}
object = arg.object or {}
setmetatable(object, self);
self.__index = self;
self.class = 'huntgroup';
self.log = arg.log;
self.database = arg.database;
self.record = arg.record;
return object;
end
function HuntGroup.find_by_id(self, id)
local sql_query = 'SELECT * FROM `hunt_groups` WHERE `id`= '.. tonumber(id) .. ' LIMIT 1';
local hunt_group = nil;
self.database:query(sql_query, function(entry)
hunt_group = HuntGroup:new(self);
hunt_group.record = entry;
hunt_group.id = tonumber(entry.id);
hunt_group.uuid = entry.uuid;
end)
return hunt_group;
end
function HuntGroup.find_by_uuid(self, uuid)
local sql_query = 'SELECT * FROM `hunt_groups` WHERE `id`= "'.. uuid .. '" LIMIT 1';
local hunt_group = nil;
self.database:query(sql_query, function(entry)
hunt_group = HuntGroup:new(self);
hunt_group.record = entry;
hunt_group.id = tonumber(entry.id);
hunt_group.uuid = entry.uuid;
end)
return hunt_group;
end
function HuntGroup.list_active_members(self)
local sql_query = 'SELECT `a`.`number`, `b`.`name` \
FROM `phone_numbers` `a` \
LEFT JOIN `hunt_group_members` `b` ON `a`.`phone_numberable_type` = "huntgroupmember" AND `a`.`phone_numberable_id` = `b`.`id` \
WHERE `a`.`phone_numberable_type` = "huntgroupmember" \
AND `b`.`active` IS TRUE \
AND `b`.`hunt_group_id` = ' .. self.record.id;
local hunt_group_members = {}
self.database:query(sql_query, function(hunt_group_members_entry)
table.insert(hunt_group_members, hunt_group_members_entry);
end)
return hunt_group_members;
end
function HuntGroup.is_member_by_numbers(self, numbers)
local sql_query = 'SELECT `a`.`number`, `b`.`name` \
FROM `phone_numbers` `a` \
LEFT JOIN `hunt_group_members` `b` ON `a`.`phone_numberable_type` = "huntgroupmember" AND `a`.`phone_numberable_id` = `b`.`id` \
WHERE `a`.`phone_numberable_type` = "huntgroupmember" \
AND `b`.`active` IS TRUE \
AND `b`.`hunt_group_id` = ' .. self.record.id .. '\
AND `a`.`number` IN ("' .. table.concat( numbers, '","') .. '") LIMIT 1';
local hunt_group_member = false;
self.database:query(sql_query, function(hunt_group_members_entry)
hunt_group_member = true;
end)
return hunt_group_member;
end
function HuntGroup.run(self, dialplan_object, caller, destination)
local hunt_group_members = self:list_active_members();
if #hunt_group_members == 0 then
return { disposition = 'HUNT_GROUP_EMPTY', code = 480, phrase = 'No active users' }
end
self.log:info('HUNTGROUP ', self.record.id, ' - name: ', self.record.name, ', strategy: ', self.record.strategy,', members: ', #hunt_group_members);
local save_destination = caller.destination;
local destinations = {}
for index, hunt_group_member in ipairs(hunt_group_members) do
local destination = dialplan_object:destination_new{ number = hunt_group_member.number };
if destination.type == 'unknown' then
self.log:notice('HG_DESTINATION - number: ', destination.number, ', hunt_group_member.number: ', hunt_group_member.number);
caller.destination_number = destination.number;
require 'dialplan.router'
local route = dialplan.router.Router:new{ log = self.log, database = self.database, caller = caller, variables = caller }:route_run('outbound', true);
if route then
destination = dialplan_object:destination_new{ ['type'] = route.type, id = route.id, number = route.destination_number }
local ignore_keys = {
id = true,
['type'] = true,
channel_variables = true,
};
for key, value in pairs(route) do
if not ignore_keys[key] then
destination[key] = value;
end
end
table.insert(destinations, destination);
end
else
table.insert(destinations, destination);
end
end
caller.destination = save_destination;
caller.destination_number = save_destination.number;
local forwarding_destination = nil;
if caller.forwarding_service == 'assistant' and caller.auth_account then
forwarding_destination = dialplan_object:destination_new{ type = caller.auth_account.class, id = caller.auth_account.id, number = forwarding_number }
forwarding_destination.alert_info = 'http://amooma.com;info=Ringer0;x-line-id=0';
end
local result = { continue = false };
local start_time = os.time();
if self.record.strategy == 'ring_recursively' then
local member_timeout = tonumber(self.record.seconds_between_jumps) or DEFAULT_MEMBER_TIMEOUT;
local run_queue = true;
while run_queue do
for index, member_destination in ipairs(destinations) do
local recursive_destinations = { member_destination }
if forwarding_destination then
table.insert(recursive_destinations, forwarding_destination);
end
require 'dialplan.sip_call'
result = dialplan.sip_call.SipCall:new{ log = self.log, database = self.database, caller = caller }:fork( recursive_destinations, { callee_id_number = destination.number, timeout = member_timeout });
if result.disposition == 'SUCCESS' then
if result.fork_index then
result.destination = recursive_destinations[result.fork_index];
end
run_queue = false;
break;
elseif os.time() > start_time + dialplan_object.dial_timeout_active then
run_queue = false;
break;
elseif not caller:ready() then
run_queue = false;
break;
end
end
if tostring(result.code) == '486' then
self.log:info('HUNTGROUP ', self.record.id, ' - all members busy');
run_queue = false;
end
end
else
if forwarding_destination then
table.insert(destinations, forwarding_destination);
end
require 'dialplan.sip_call'
result = dialplan.sip_call.SipCall:new{ log = self.log, database = self.database, caller = caller }:fork( destinations,
{
callee_id_number = destination.number,
timeout = dialplan_object.dial_timeout_active,
send_ringing = ( dialplan_object.send_ringing_to_gateways and caller.from_gateway ),
});
if result.fork_index then
result.destination = destinations[result.fork_index];
end
return result;
end
return result;
end
function HuntGroup.list_destination_numbers(self)
require "common.phone_number"
local phone_number_class = common.phone_number.PhoneNumber:new(defaults)
local sql_query = string.format("SELECT * FROM `phone_numbers` WHERE `state`='active' AND `phone_numberable_type` = 'HuntGroupMember' AND `phone_numberable_id` IN ( \
SELECT `id` FROM `hunt_group_members` WHERE `active` IS TRUE AND `hunt_group_id`=%d ) ORDER BY `position` ASC", tonumber(self.record.id));
local phone_numbers = {}
self.database:query(sql_query, function(hunt_group_number_entry)
local number_object = phone_number_class:find_by_number(hunt_group_number_entry.number)
if number_object and number_object.record then
table.insert(phone_numbers, {number = hunt_group_number_entry.number, destination_type = number_object.record.phone_numberable_type, destination_id = number_object.record.phone_numberable_id});
else
table.insert(phone_numbers, {number = hunt_group_number_entry.number});
end
end)
return phone_numbers ;
end
|