-
Notifications
You must be signed in to change notification settings - Fork 461
/
cf_conference.erl
368 lines (344 loc) · 15.3 KB
/
cf_conference.erl
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
%%%-------------------------------------------------------------------
%%% @author Karl Anderson <karl@2600hz.org>
%%% @copyright (C) 2011, Karl Anderson
%%% @doc
%%%
%%% @end
%%% Created : 16 Mar 2011 by Karl Anderson <karl@2600hz.org>
%%%-------------------------------------------------------------------
-module(cf_conference).
-include("../callflow.hrl").
-export([handle/2]).
-import(cf_call_command, [
answer/1, b_conference/5, b_play/2, hangup/1,
b_play_and_collect_digits/6, wait_for_dtmf/1
]).
-import(cf_conference_command, [
b_members/2
]).
-record(prompts, {
greeting = <<"/system_media/conf-welcome">>
,request_pin = <<"/system_media/conf-pin">>
,incorrect_pin = <<"/system_media/conf-bad-pin">>
,max_pin_tries = <<"shout://translate.google.com/translate_tts?tl=en&q=You+have+reached+the+maximum+number+of+entry+attempts!+Goodbye.">>
,alone_enter = <<"/system_media/conf-alone">>
,single_enter = <<"shout://translate.google.com/translate_tts?tl=en&q=There+is+only+one+other+participant.">>
,multiple_enter = <<"shout://translate.google.com/translate_tts?tl=en&q=There+are+~p+other+participants.">>
,announce_join = <<"tone_stream://%(200,0,500,600,700)">>
,announce_leave = <<"tone_stream://%(500,0,300,200,100,50,25)">>
,muted = <<"/system_media/conf-muted">>
,unmuted = <<"/system_media/conf-unmuted">>
,deaf = <<"shout://translate.google.com/translate_tts?tl=en&q=Silenced.">>
,undeaf = <<"shout://translate.google.com/translate_tts?tl=en&q=Audiable.">>
}).
-record(control, {
mute = <<>>
,unmute = <<>>
,deaf = <<>>
,undeaf = <<>>
,toggle_mute = <<"0">>
,toggle_deaf = <<"*">>
,hangup = <<"#">>
}).
-record(conf, {
id = <<"fe306820-51c4-11e0-b8af-0800200c9a66">>
,members = []
,member = undefined
,member_pins = [<<"1234">>]
,moderator_pins = [<<"9876">>]
,max_pin_tries = 3
,member_join_muted = <<"true">>
,member_join_deaf = <<"false">>
,moderator_join_muted = <<"false">>
,moderator_join_deaf = <<"false">>
,max_members = 0
,require_moderator = <<"false">>
,wait_for_moderator = <<"false">>
,prompts = #prompts{}
,control = #control{}
}).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Entry point for this module, attempts to call an endpoint as defined
%% in the Data payload. Returns continue if fails to connect or
%% stop when successfull.
%% @end
%%--------------------------------------------------------------------
-spec(handle/2 :: (Data :: json_object(), Call :: #cf_call{}) -> tuple(stop | continue)).
handle(Data, #cf_call{cf_pid=CFPid}=Call) ->
Conf = update_members(get_conference_profile(Data, Call#cf_call.account_db), Call),
answer(Call),
_ = play_conference_name(Conf, Call),
case check_pin(Conf, Call, 1) of
member ->
join_conference(Conf, Call, member),
caller_controls(Conf, Call),
announce_leave(Conf, Call),
CFPid ! {continue};
moderator ->
join_conference(Conf, Call, moderator),
caller_controls(Conf, Call),
announce_leave(Conf, Call),
CFPid ! {continue};
stop ->
CFPid ! {stop}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Bridges the caller in to the conference with the intial flags
%% defined by this conference profile
%% @end
%%--------------------------------------------------------------------
-spec(join_conference/3 :: (Conf :: #conf{}, Call :: #cf_call{}, Type :: member|moderator) -> ok).
join_conference(Conf, Call, Type) ->
_ = play_conference_count(Conf, Call),
_ = case Type of
member ->
b_conference(Conf#conf.id, Conf#conf.member_join_muted, Conf#conf.member_join_deaf, <<"false">>, Call);
moderator ->
b_conference(Conf#conf.id, Conf#conf.moderator_join_muted, Conf#conf.moderator_join_deaf, <<"true">>, Call)
end,
announce_join(Conf, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Plays the conference greeting to the caller
%% @end
%%--------------------------------------------------------------------
-spec(play_conference_name/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> tuple(ok, json_object()) | tuple(error, atom())).
play_conference_name(#conf{prompts=Prompts}, Call) ->
b_play(Prompts#prompts.greeting, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Request the conference pin number from the caller, and determines
%% if the pin is for a member or moderator. If the caller fails to
%% provide a valid pin within max_pin_tries the call is hungup
%% @end
%%--------------------------------------------------------------------
-spec(check_pin/3 :: (Conf :: #conf{}, Call :: #cf_call{}, LoopCount :: integer()) -> member | moderator | stop).
check_pin(#conf{prompts=Prompts} = Conf, Call, LoopCount) when LoopCount > Conf#conf.max_pin_tries ->
_ = b_play(Prompts#prompts.max_pin_tries, Call),
stop;
check_pin(#conf{prompts=Prompts} = Conf, Call, LoopCount) ->
case b_play_and_collect_digits(<<"4">>, <<"6">>, Prompts#prompts.request_pin, <<"1">>, <<"5000">>, Call) of
{ok, Pin} ->
case lists:member(Pin, Conf#conf.member_pins) of
true ->
member;
false ->
case lists:member(Pin, Conf#conf.moderator_pins) of
true ->
moderator;
false ->
_ = b_play(Prompts#prompts.incorrect_pin, Call),
check_pin(Conf, Call, LoopCount+1)
end
end;
{error, _} ->
check_pin(Conf, Call, LoopCount+1)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Plays the current conference member count prompt to the caller
%% @end
%%-------------------------------------------------------------------
-spec(play_conference_count/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> tuple(ok, json_object()) | tuple(error, atom())).
play_conference_count(#conf{prompts=Prompts, members=Members}, Call) ->
case length(Members) of
0 ->
b_play(Prompts#prompts.alone_enter, Call);
1 ->
b_play(Prompts#prompts.single_enter, Call);
Count ->
Prompt = io_lib:format(whistle_util:to_list(Prompts#prompts.multiple_enter), [Count]),
b_play(whistle_util:to_binary(Prompt), Call)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Plays the conference announce_join media to the conference
%% @end
%%--------------------------------------------------------------------
-spec(announce_join/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
announce_join(#conf{prompts=Prompts, id=ConfId}, Call) ->
cf_conference_command:play(Prompts#prompts.announce_join, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Plays the conference announce_leave media to the conference
%% @end
%%--------------------------------------------------------------------
-spec(announce_leave/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
announce_leave(#conf{prompts=Prompts, id=ConfId}, Call) ->
cf_conference_command:play(Prompts#prompts.announce_leave, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Fetches the conference parameters from the datastore and loads the
%% conference record
%% @end
%%--------------------------------------------------------------------
-spec(get_conference_profile/2 :: (Data :: json_object(), Db :: binary()) -> #conf{}).
get_conference_profile(Data, Db) ->
Id = wh_json:get_value(<<"id">>, Data),
case couch_mgr:open_doc(Db, Id) of
{ok, JObj} ->
Default=#conf{},
#conf{
id = Id
,member_pins = wh_json:get_value(<<"member_pins">>, JObj, [])
,moderator_pins = wh_json:get_value(<<"moderator_pins">>, JObj, [])
,member_join_muted = wh_json:get_value(<<"member_join_muted">>, JObj, Default#conf.member_join_muted)
,member_join_deaf = wh_json:get_value(<<"member_join_deaf">>, JObj, Default#conf.member_join_deaf)
,moderator_join_muted = wh_json:get_value(<<"moderator_join_muted">>, JObj, Default#conf.moderator_join_muted)
,moderator_join_deaf = wh_json:get_value(<<"moderator_join_deaf">>, JObj, Default#conf.moderator_join_deaf)
,max_members = wh_json:get_value(<<"max_members">>, JObj, Default#conf.max_members)
,require_moderator = wh_json:get_value(<<"require_moderator">>, JObj, Default#conf.require_moderator)
,wait_for_moderator = wh_json:get_value(<<"wait_for_moderator">>, JObj, Default#conf.wait_for_moderator)
};
_ ->
#conf{}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Loops waiting for DTMF events and responds appropraitely to provide
%% an individual member with conference controls
%% @end
%%--------------------------------------------------------------------
-spec(caller_controls/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
caller_controls(#conf{control=Control} = Conf, Call) ->
case wait_for_dtmf(200000) of
{ok, Digit} ->
if
Digit == Control#control.mute ->
mute_caller(Conf, Call);
Digit == Control#control.unmute ->
unmute_caller(Conf, Call);
Digit == Control#control.deaf ->
deaf_caller(Conf, Call);
Digit == Control#control.undeaf ->
undeaf_caller(Conf, Call);
Digit == Control#control.toggle_mute ->
toggle_mute(Conf, Call);
Digit == Control#control.toggle_deaf ->
toggle_deaf(Conf, Call);
Digit == Control#control.hangup ->
cf_call_command:hangup(Call);
true ->
ok
end,
caller_controls(Conf, Call);
{error, timeout} ->
caller_controls(Conf, Call);
{error, channel_hungup} ->
ok
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Mutes or unmutes the conference member belonging to Call,
%% depending on the current 'speak' state
%% @end
%%--------------------------------------------------------------------
-spec(toggle_mute/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
toggle_mute(Conf, Call) ->
C1 = update_members(Conf, Call),
case binary:match(wh_json:get_value(<<"Status">>, C1#conf.member), <<"speak">>) of
nomatch ->
unmute_caller(C1, Call);
_ ->
mute_caller(C1, Call)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Deaf or undeaf the conference member belonging to Call, depending
%% on the current 'hear' state
%% @end
%%--------------------------------------------------------------------
-spec(toggle_deaf/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
toggle_deaf(Conf, Call) ->
C1 = update_members(Conf, Call),
case binary:match(wh_json:get_value(<<"Status">>, C1#conf.member), <<"hear">>) of
nomatch ->
undeaf_caller(C1, Call);
_ ->
deaf_caller(C1, Call)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Mutes the conference member belonging to Call
%% @end
%%--------------------------------------------------------------------
-spec(mute_caller/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
mute_caller(#conf{member=Member, id=ConfId, prompts=Prompts}, Call) ->
MemberId = wh_json:get_value(<<"Member-ID">>, Member),
cf_conference_command:mute(MemberId, ConfId, Call),
cf_conference_command:play(Prompts#prompts.muted, MemberId, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Unmutes the conference member belonging to Call
%% @end
%%--------------------------------------------------------------------
-spec(unmute_caller/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
unmute_caller(#conf{member=Member, id=ConfId, prompts=Prompts}, Call) ->
MemberId = wh_json:get_value(<<"Member-ID">>, Member),
cf_conference_command:unmute(MemberId, ConfId, Call),
cf_conference_command:play(Prompts#prompts.unmuted, MemberId, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Makes the conference member belonging to Call deaf
%% @end
%%--------------------------------------------------------------------
-spec(deaf_caller/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
deaf_caller(#conf{member=Member, id=ConfId, prompts=Prompts}, Call) ->
MemberId = wh_json:get_value(<<"Member-ID">>, Member),
cf_conference_command:deaf(MemberId, ConfId, Call),
cf_conference_command:play(Prompts#prompts.deaf, MemberId, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Makes the conference member belonging to Call undeaf
%% @end
%%--------------------------------------------------------------------
-spec(undeaf_caller/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> ok).
undeaf_caller(#conf{member=Member, id=ConfId, prompts=Prompts}, Call) ->
MemberId = wh_json:get_value(<<"Member-ID">>, Member),
cf_conference_command:undeaf(MemberId, ConfId, Call),
cf_conference_command:play(Prompts#prompts.undeaf, MemberId, ConfId, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Fetches a list of members in the conference and updates the record
%% @end
%%--------------------------------------------------------------------
-spec(update_members/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> #conf{}).
update_members(#conf{id=ConfId} = Conf, Call) ->
C1 = Conf#conf{members = b_members(ConfId, Call)},
find_call_member(C1, Call).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Determines which conference member belongs to Call and updates
%% the record
%% @end
%%-------------------------------------------------------------------
-spec(find_call_member/2 :: (Conf :: #conf{}, Call :: #cf_call{}) -> #conf{}).
find_call_member(Conf, #cf_call{call_id=CallId}) ->
Member = lists:foldr(fun (Member, Acc) ->
case wh_json:get_value(<<"Call-ID">>, Member) of
CallId ->
Member;
_ ->
Acc
end
end, undefined, Conf#conf.members),
Conf#conf{member = Member}.