-
Notifications
You must be signed in to change notification settings - Fork 50
/
freeswitch_media.erl
1252 lines (1125 loc) · 51.5 KB
/
freeswitch_media.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% The contents of this file are subject to the Common Public Attribution
%% License Version 1.0 (the “License”); you may not use this file except
%% in compliance with the License. You may obtain a copy of the License at
%% http://opensource.org/licenses/cpal_1.0. The License is based on the
%% Mozilla Public License Version 1.1 but Sections 14 and 15 have been
%% added to cover use of software over a computer network and provide for
%% limited attribution for the Original Developer. In addition, Exhibit A
%% has been modified to be consistent with Exhibit B.
%%
%% Software distributed under the License is distributed on an “AS IS”
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is OpenACD.
%%
%% The Initial Developers of the Original Code is
%% Andrew Thompson and Micah Warren.
%%
%% All portions of the code written by the Initial Developers are Copyright
%% (c) 2008-2009 SpiceCSM.
%% All Rights Reserved.
%%
%% Contributor(s):
%%
%% Andrew Thompson <andrew at hijacked dot us>
%% Micah Warren <micahw at lordnull dot com>
%%
%% @doc The gen_media callback module for voice calls through freeswitch.
%% @see freeswitch_media_manager
-module(freeswitch_media).
-author("Micah").
-behaviour(gen_media).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-include_lib("OpenACD/include/log.hrl").
-include_lib("OpenACD/include/queue.hrl").
-include_lib("OpenACD/include/call.hrl").
-include_lib("OpenACD/include/agent.hrl").
-include("cpx_freeswitch_pb.hrl").
%-include_lib("OpenACD/include/gen_media.hrl").
-define(TIMEOUT, 10000).
-define(DEFAULT_PRIORITY, 10).
-define(DEFAULT_VM_PRIORITY_DIFF, 10).
%% API
-export([
start/3,
start_link/3,
get_call/1,
%get_queue/1,
%get_agent/1,
%unqueue/1,
%set_agent/3,
dump_state/1,
'3rd_party_pickup'/1
]).
%% gen_media callbacks
-export([
init/1,
urlpop_getvars/1,
prepare_endpoint/2,
handle_ring/4,
handle_ring_stop/4,
handle_answer/5,
handle_voicemail/3,
handle_spy/3,
handle_announce/3,
%% TODO added for testing only (implemented with focus on real Calls - no other media)
handle_end_call/4,
handle_agent_transfer/4,
handle_queue_transfer/5,
handle_wrapup/5,
handle_call/6,
handle_cast/5,
handle_info/5,
terminate/5,
code_change/4]).
-type(internal_statename() ::
'inivr' | % never been in queue.
'inqueue' | % waiting for an agent to call
'inqueue_ringing' | % got an agent, waiting for the anser
'oncall' | % oncall with an agent.
'oncall_ringing' | % starting an agent transfer
'oncall_hold' | % simple hold
'hold_conference' | % has a conference, but agent (self) not a member
'hold_conference_3rdparty' | % a conference is up, as is a call to a
% 3rd party, but agent is member of neither
'in_conference_3rdparty' | % agent is a member of the conference,
% but there is a 3rd party in limbo.
'3rd_party' | % there is a conference, but agent is talking w/ 3rd party.
'in_conference' | % agent is a member of the conference, no limbo party
'wrapup_conference' | % there is no agent
'blind_transfered'
).
-define(cdr_states, ['oncall', 'oncall_hold', 'hold_conference',
'hold_conference_3rdparty', 'in_conference_3rdparty', '3rd_party',
'in_conference', 'wrapup_conference', 'blind_transfered']).
-record(state, {
statename :: internal_statename(),
uuid :: string(),
cook :: pid() | 'undefined',
queue :: string() | 'undefined',
cnode :: atom(),
dialstring :: string(),
agent :: string() | 'undefined',
agent_pid :: pid() | 'undefined',
ringchannel :: pid() | 'undefined',
ringuuid :: string() | 'undefined',
manager_pid :: 'undefined' | any(),
voicemail = false :: 'false' | string(),
xferchannel :: pid() | 'undefined',
xferuuid :: string() | 'undefined',
in_control = false :: boolean(),
queued = false :: boolean(),
allow_voicemail = false :: boolean(),
vm_priority_diff = ?DEFAULT_VM_PRIORITY_DIFF :: integer(),
ivroption :: string() | 'undefined',
caseid :: string() | 'undefined',
moh = "moh" :: string() | 'none',
record_path :: 'undefined' | string(),
dial_vars :: list(),
hold :: 'undefined' | 'hold',
spawn_oncall_mon :: 'undefined' | {pid(), reference()},
conference_id :: 'undefined' | string(),
'3rd_party_id' :: 'undefined' | string(),
'3rd_party_mon' :: 'undefined' | {pid(), reference()}
}).
-type(state() :: #state{}).
-define(GEN_MEDIA, true).
-include_lib("OpenACD/include/gen_spec.hrl").
%%====================================================================
%% API
%%====================================================================
%% @doc starts the freeswitch media gen_media. `Cnode' is the C node the
%% communicates directly with freeswitch. Dialstring is the data used to
%% connect to an outside line. UUID is the id of the channel on
%% freeswith, and therefore the id of the call in OpenACD.
-spec(start/3 :: (Cnode :: atom(), DialString :: string(), UUID :: string()) -> {'ok', pid()}).
start(Cnode, DialString, UUID) ->
gen_media:start(?MODULE, [Cnode, DialString, UUID]).
%% @doc Starts the freeswitch gen_media linked to the calling process.
%% @see start/3
-spec(start_link/3 :: (Cnode :: atom(), DialString :: string(), UUID :: string()) -> {'ok', pid()}).
start_link(Cnode, DialString, UUID) ->
gen_media:start_link(?MODULE, [Cnode, DialString, UUID]).
%% @doc returns the record of the call freeswitch media `MPid' is in
%% charge of.
-spec(get_call/1 :: (MPid :: pid()) -> #call{}).
get_call(MPid) ->
gen_media:get_call(MPid).
%% @doc A debugging function that returns the state record.
-spec(dump_state/1 :: (Mpid :: pid()) -> #state{}).
dump_state(Mpid) when is_pid(Mpid) ->
gen_media:call(Mpid, dump_state).
%% @hidden
'3rd_party_pickup'(Mpid) ->
Self = self(),
gen_media:cast(Mpid, {'3rd_party_pickup', Self}).
%%====================================================================
%% gen_media callbacks
%%====================================================================
%% @private
init([Cnode, DialString, UUID]) ->
process_flag(trap_exit, true),
Manager = whereis(freeswitch_media_manager),
{DNIS, Client, Priority, CidName, CidNum, SIPFrom, ExportVars} = get_info(Cnode, UUID),
Call = #call{id = UUID, source = self(), client = Client, priority = Priority, callerid={CidName, CidNum}, dnis=DNIS, media_path = inband},
{ok, {#state{statename = inivr, cnode=Cnode, manager_pid = Manager, dialstring = DialString, dial_vars = ["sip_h_X-FromData='"++SIPFrom++"'" | ExportVars], uuid = UUID}, Call, {inivr, [DNIS]}}}.
%% @private
-spec(urlpop_getvars/1 :: (State :: #state{}) -> [{binary(), binary()}]).
urlpop_getvars(#state{ivroption = Ivropt} = _State) ->
[{"itxt", Ivropt}].
%% @private
-spec(handle_announce/3 :: (Announcement :: string(), Callrec :: #call{}, State :: #state{}) -> {'ok', #state{}}).
handle_announce(Announcement, Callrec, State) ->
freeswitch:sendmsg(State#state.cnode, Callrec#call.id,
[{"call-command", "execute"},
{"execute-app-name", "playback"},
{"execute-app-arg", Announcement}]),
{ok, State}.
%%--------------------------------------------------------------------
%% perpare_endpoint
%%--------------------------------------------------------------------
-type(agent_registration_type() :: 'sip' | 'sip_registration' | 'h323' |
'iax2' | 'pstn').
-type(agent_registration_data_opt() :: {'date', string()}).
-type(agent_registration_type_opt() :: {'type', agent_registration_type()}).
-type(persistant_ring_opt() :: 'persistant').
-type(endpoint_opt() :: persistant_ring_opt() |
agent_registration_type_opt() | agent_registration_data_opt()).
-type(endpoint_opts() :: [endpoint_opt()]).
-type(freeswitch_ring_endpoint() :: pid() | {'freeswitch_ring','start',[_]}).
%% @doc Handle an agent being setup with a freeswitch_media endpoint.
%% Options uses defaults of [{type,sip_registration},{data,agent_login()}].
-spec(prepare_endpoint/2 :: (Agent :: #agent{}, Options :: endpoint_opts())
-> {'ok', freeswitch_ring_endpoint()}).
prepare_endpoint(Agent, Options) ->
{Node, Dialstring, Dest} = freeswitch_media_manager:get_ring_data(Agent, Options),
case proplists:get_value(persistant, Options) of
undefined ->
{ok, {freeswitch_ring, start, [Node, freeswitch_ring_transient, [
{destination, Dest},
{dialstring, Dialstring}]]}};
true ->
freeswitch_ring:start(Node, freeswitch_persistant_ring, [
{destination, Dest},
{dialstring, Dialstring},
persistant
])
end.
%%--------------------------------------------------------------------
%% handle_answer
%%--------------------------------------------------------------------
%% @hidden
handle_answer(Apid, StateName, Callrec, GenMediaState, State) when
StateName =:= inqueue_ringing; StateName =:= oncall_ringing ->
{RingUUID, RingPid} = case GenMediaState of
#inqueue_ringing_state{outband_ring_pid = undefined} ->
{"", undefined};
#inqueue_ringing_state{outband_ring_pid = P} ->
{freeswitch_ring:get_uuid(P), P};
#oncall_ringing_state{outband_ring_pid = undefined} ->
{"", undefined};
#oncall_ringing_state{outband_ring_pid = P} ->
{freeswitch_ring:get_uuid(P), P}
end,
case freeswitch:api(State#state.cnode, uuid_bridge, Callrec#call.id ++ " " ++ RingUUID) of
{ok, _} ->
RecPath = case cpx_supervisor:get_archive_path(Callrec) of
none ->
?DEBUG("archiving is not configured for ~p", [Callrec#call.id]),
undefined;
{error, _Reason, Path} ->
?WARNING("Unable to create requested call archiving directory for recording ~p for ~p", [Path, Callrec#call.id]),
undefined;
Path ->
% get_archive_path ensures the directory is writeable by us and
% exists, so this should be safe to do (the call will be hungup
% if creating the recording file fails)
?DEBUG("archiving ~p to ~s.wav", [Callrec#call.id, Path]),
freeswitch:api(State#state.cnode, uuid_setvar, Callrec#call.id ++ " RECORD_APPEND true"),
freeswitch:api(State#state.cnode, uuid_record, Callrec#call.id ++ " start "++Path++".wav"),
Path++".wav"
end,
agent_channel:media_push(Apid, Callrec, {mediaload, Callrec, [{<<"height">>, <<"300px">>}, {<<"title">>, <<"Server Boosts">>}]}),
{ok, State#state{agent_pid = Apid, record_path = RecPath, queued = false, statename = oncall, ringchannel = RingPid, ringuuid = RingUUID}};
{error, Error} ->
?WARNING("Could not do answer: ~p", [Error]),
{error, Error, State}
end.
% TODO added for testing only (implemented with focus on real Calls - no other media)
%% @hidden
-spec(handle_end_call/4 :: (GMState :: atom(), Callrec :: #call{},
GMStateData :: any(), State :: #state{}) -> {'ok', #state{}}).
handle_end_call(_Gmstate, Callrec, _Gmstatedata, State) ->
freeswitch:sendmsg(State#state.cnode, Callrec#call.id,
[{"call-command", "hangup"},
{"hangup-cause", "SUBSCRIBER_ABSENT"}]),
{deferred, State}.
%% @hidden
handle_ring(Apid, RingData, Callrec, State) when is_pid(Apid) ->
?INFO("ring to agent ~p for call ~s", [Apid, Callrec#call.id]),
% TODO - we could avoid this if we had the agent's login,
AgentRec = agent:dump_state(Apid),
handle_ring({Apid, AgentRec}, RingData, Callrec, State);
handle_ring({_Apid, #agent{ring_channel = {undefined, persistant, _}} = Agent}, _RingData, _Callrec, State) ->
?WARNING("Agent (~p) does not have it's persistant channel up yet", [Agent#agent.login]),
{invalid, State};
handle_ring({Apid, #agent{ring_channel = {EndpointPid, persistant, _EndPointType}}} = Agent, _RingData, Callrec, State) ->
%% a persisitant ring does the hard work for us
%% go right to the okay.
?INFO("Ring channel made things happy, I assume", []),
{ok, [{"itxt", State#state.ivroption}], Callrec#call{ring_path = inband, media_path = inband}, State#state{ringchannel = EndpointPid, agent_pid = Apid}};
handle_ring({Apid, #agent{ring_channel = {RPid, transient, _}} = AgentRec}, _RingData, Callrec, State) ->
NewStatename = case State#state.statename of
inqueue -> inqueue_ringing;
oncall -> oncall_ringing
end,
{ok, [{"itxt", State#state.ivroption}], State#state{statename = NewStatename, agent_pid = Apid, ringchannel = RPid}}.
% TODO This needs to be updated when conferencing is fixed.
%% @hidden
handle_ring_stop(_StateName, Callrec, _GenMedia, #state{xferchannel = RingChannel} = State) when is_pid(RingChannel) ->
?DEBUG("hanging up transfer channel for ~p", [Callrec#call.id]),
freeswitch_ring:hangup(RingChannel),
{ok, State#state{xferchannel = undefined, xferuuid = undefined}};
handle_ring_stop(StateName, Callrec, _GenMedia, State) ->
?DEBUG("hanging up ring channel for ~p", [Callrec#call.id]),
case State#state.ringchannel of
undefined ->
ok;
RingChannel ->
% TODO - make sure the call didn't get bridged in the interim?
% the ring channel might have bridged and the message is sitting
% in our mailbox
freeswitch_ring:hangup(RingChannel)
end,
NewStatename = case StateName of
inqueue_ringing -> inqueue;
oncall_ringing -> oncall
end,
{ok, State#state{statename = NewStatename, ringchannel=undefined}}.
%% @hidden
-spec(handle_voicemail/3 :: (Agent :: pid() | 'undefined', Call :: #call{}, State :: #state{}) -> {'ok', #state{}}).
handle_voicemail(Agent, Callrec, State) when is_pid(Agent) ->
{ok, Midstate} = handle_ring_stop(undefined, Callrec, undefined, State),
handle_voicemail(undefined, Callrec, Midstate);
handle_voicemail(undefined, Call, State) ->
UUID = Call#call.id,
freeswitch:bgapi(State#state.cnode, uuid_transfer, UUID ++ " 'playback:IVR/prrec.wav,gentones:%(500\\,0\\,500),sleep:600,record:/tmp/${uuid}.wav' inline"),
{ok, State#state{statename = inivr, voicemail = "/tmp/"++UUID++".wav"}}.
%% @hidden
-spec(handle_spy/3 :: (Agent :: {pid(), #agent{}}, Call :: #call{}, State :: #state{}) -> {'error', 'bad_agent', #state{}} | {'ok', #state{}}).
handle_spy({Agent, AgentRec}, Call, #state{cnode = Fnode, ringchannel = Chan} = State) when is_pid(Chan) ->
agent:blab(Agent, "While spying, you have the following options:\n"++
"* To whisper to the agent; press 1\n"++
"* To whisper to the caller; press 2\n"++
"* To talk to both parties; press 3\n"++
"* To resume spying; press 0"),
Dialstring = freeswitch_media_manager:get_agent_dial_string(AgentRec, []),
freeswitch:bgapi(Fnode, originate, Dialstring ++ " &eavesdrop(" ++ Call#call.id ++ ")"),
{ok, State};
handle_spy(_Agent, _Call, State) ->
{invalid, State}.
%% @hidden
handle_agent_transfer(AgentPid, Timeout, Call, State) ->
AgentRec = agent:dump_state(AgentPid), % TODO - avoid this
?INFO("transfer_agent to ~p for call ~p", [AgentRec#agent.login, Call#call.id]),
% fun that returns another fun when passed the UUID of the new channel
% (what fun!)
F = fun(_UUID) ->
fun(ok, _Reply) ->
% agent picked up?
?INFO("Agent transfer picked up? ~p", [Call#call.id]);
(error, Reply) ->
?WARNING("originate failed for ~p with ~p", [Call#call.id, Reply])
end
end,
case freeswitch_ring:start_link(State#state.cnode, AgentRec, AgentPid, Call, Timeout, F, [single_leg, no_oncall_on_bridge, {dial_vars, State#state.dial_vars}]) of
{ok, Pid} ->
{ok, [{"ivropt", State#state.ivroption}, {"caseid", State#state.caseid}], State#state{statename = oncall_ringing, xferchannel = Pid, xferuuid = freeswitch_ring:get_uuid(Pid)}};
{error, Error} ->
?ERROR("error: ~p", [Error]),
{error, Error, State}
end.
%% @hidden
handle_wrapup(_From, _StateName, #call{media_path = inband} = Call, _GenMediaState, State) ->
?DEBUG("Handling wrapup request", []),
% TODO This could prolly stand to be a bit more elegant.
freeswitch:api(State#state.cnode, uuid_kill, Call#call.id),
{hangup, State};
handle_wrapup(_From, _StateName, _Call, _GenMediaState, State) ->
% This intentionally left blank; media is out of band, so there's
% no direct hangup by the agent
?DEBUG("Not doing wrapup request", []),
{ok, State}.
%% @hidden
handle_queue_transfer(_Queue, _StateName, Call, _GenMediaState, #state{cnode = Fnode} = State) ->
case State#state.record_path of
undefined ->
ok;
Path ->
?DEBUG("stopping recording due to queue transfer for ~p", [Call#call.id]),
freeswitch:api(Fnode, uuid_record, Call#call.id ++ " stop " ++ Path)
end,
freeswitch:api(Fnode, uuid_park, Call#call.id),
% play musique d'attente
% TODO this can generate an annoying warning in FS, but I don't care
% right now
case State#state.moh of
none ->
ok;
_MohMusak ->
freeswitch:sendmsg(Fnode, Call#call.id,
[{"call-command", "execute"},
{"execute-app-name", "playback"},
{"execute-app-arg", "local_stream://" ++ State#state.moh}])
end,
{ok, State#state{statename = inqueue, queued = true, agent_pid = undefined}}.
%%--------------------------------------------------------------------
%% Description: Handling call messages
%%--------------------------------------------------------------------
%% @private
handle_call(get_call, _From, _, Call, _, State) ->
{reply, Call, State};
handle_call(get_agent, _From, _, _Call, _, State) ->
{reply, State#state.agent_pid, State};
handle_call({set_agent, Agent, Apid}, _From, _, _Call, _, State) ->
{reply, ok, State#state{agent = Agent, agent_pid = Apid}};
handle_call(dump_state, _From, _, _Call, _, State) ->
{reply, State, State};
handle_call(Msg, _From, _, Call, _, State) ->
?INFO("unhandled mesage ~p for ~p", [Msg, Call#call.id]),
{reply, ok, State}.
%%--------------------------------------------------------------------
%% Description: Handling cast messages
%%--------------------------------------------------------------------
%% @private
handle_cast(toggle_hold, _Statename, Call, _GenMediaState, #state{statename = Statename} = State)
when Statename == oncall; Statename == oncall_ringing ->
?DEBUG("toggle hold while oncall", []),
#state{cnode = Fnode, moh = Muzak, ringuuid = Ringid} = State,
#call{id = Callid} = Call,
?INFO("Gonna try to set ~s on hold", [Call#call.id]),
freeswitch:api(Fnode, uuid_setvar_multi, Callid ++ " hangup_after_bridge=false;park_after_bridge=true"),
?DEBUG("Ringid: ~p", [Ringid]),
ok = fs_send_execute(Fnode, Ringid, "set", "hangup_after_bridge=false"),
ok = fs_send_execute(Fnode, Ringid, "set", "park_after_bridge=true"),
Res = freeswitch:api(Fnode, uuid_transfer, Ringid ++ " park inline"),
?ERROR("Res of the api: ~p", [Res]),
case Muzak of
none -> ok;
_ ->
?ERROR("5 ~p", [fs_send_execute(Fnode, Callid, "playback", "local_stream://" ++ Muzak)])
end,
{noreply, State#state{statename = oncall_hold}};
%% oncall hold -> next_state
handle_cast(toggle_hold, _Statename, Call, _GenMediaState, #state{statename = oncall_hold} = State) ->
?INFO("Gonna try to pick up the holder dude for ~s", [Call#call.id]),
#state{cnode = Fnode, ringuuid = Ringid} = State,
#call{id = Callid} = Call,
freeswitch:api(Fnode, uuid_bridge, Callid ++ " " ++ Ringid),
freeswitch:api(Fnode, uuid_setvar_multi, Callid ++ " hangup_after_bridge=true;park_after_bridge=false"),
{noreply, State#state{statename = oncall}};
handle_cast({contact_3rd_party, _Args} = Cast, Statename, Call, GenMediaState, #state{statename = oncall_hold, cnode = Fnode} = State) ->
% first step is to move to hold_conference state, which means
% creating the conference.
{ok, ConfId} = freeswitch:api(Fnode, create_uuid),
case freeswitch:api(Fnode, uuid_transfer, Call#call.id ++ " conference:" ++
ConfId ++ " inline") of
{ok, Res} ->
?INFO("Success result creating conferance and transfering call to it: ~p", [Res]),
% okay, solidify the conference state change, and go on.
Newstate = State#state{conference_id = ConfId, statename = hold_conference},
handle_cast(Cast, Statename, Call, GenMediaState, Newstate);
Else ->
?ERROR("Could not create conference: ~p", [Else]),
{noreply, State}
end;
%% hold_conference -> 3rd_party | in_conference
handle_cast({contact_3rd_party, Destination}, _Statename, Call, _GenMediaState, #state{statename = hold_conference, cnode = Fnode} = State) ->
% start a ring chan to 3rd party
% play a ringing sound to agent to be nice
% on any error, we just kill the playback
% otherwise if the 3rd party picks up, we send a message here to move
% to the new state.
#call{client = Client} = Call,
#client{options = ClientOpts} = Client,
{CallerNameOpt, CallerNumberOpt} = case proplists:get_value(<<"caller_id">>, ClientOpts) of
undefined -> {"", ""};
{BinName, BinNumber} when is_binary(BinName), is_binary(BinNumber) ->
{binary_to_list(BinName), binary_to_list(BinNumber)};
CidOut -> CidOut
end,
%Destination = binary_to_list(proplists:get_value("args", Args)),
BaseDs = freeswitch_media_manager:get_default_dial_string(),
RingOps = [CallerNameOpt, CallerNumberOpt, "park_after_bridge=true"],
case originate(Fnode, BaseDs, Destination, RingOps) of
{ok, UUID} ->
{noreply, State#state{'3rd_party_id' = UUID, statename = hold_conference_3rdparty}};
Else ->
?WARNING("Failed to contact 3rd party ~s due to: ~p", [Destination, Else]),
{noreply, State}
end;
handle_cast(retrieve_conference, _Statename, Call, _GenMediaState, #state{
statename = Statename} = State) when Statename =:= 'hold_conference';
Statename =:= 'hold_conference_3rdparty' ->
#state{cnode = Fnode, ringuuid = Ringid, conference_id = Confid} = State,
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " 'conference:" ++ Confid ++ "' inline"),
NewState = case Statename of
'hold_conference' -> 'in_conference';
'hold_conference_3rdparty' -> 'in_conference_3rdparty'
end,
cdr:media_custom(Call, NewState, ?cdr_states, []),
{{mediapush, NewState}, State#state{statename = NewState}};
% 3rd_party -> hold_conference_3rdparty | in_conference | hold_conference
handle_cast(toggle_hold, _Statename, Call, _GenMediaState, #state{statename = '3rd_party'} = State) ->
?INFO("Place 3rd party on hold", []),
#state{cnode = Fnode, moh = Muzak, ringuuid = Ringid, '3rd_party_id' = ThirdPId} = State,
ok = fs_send_execute(Fnode, Ringid, "set", "hangup_after_bridge=false"),
ok = fs_send_execute(Fnode, Ringid, "set", "park_after_bridge=true"),
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " park inline"),
% RETRIEVE_CONFERENCE breaks with this code; just specifying "park"
% works, though.
Helddp = "park",
freeswitch:api(Fnode, uuid_transfer, ThirdPId ++ " " ++ Helddp ++ " inline"),
{noreply, State#state{statename = hold_conference_3rdparty}};
handle_cast(retrieve_conference, Statename, Call, GenMediaState, #state{statename = '3rd_party'} = State) ->
?INFO("Place 3rd party on hold, and go to the conference", []),
{noreply, MidState} = handle_cast(toggle_hold, Statename, Call, GenMediaState, State),
{noreply, MidState#state{statename = in_conference_3rdparty}};
handle_cast(retrieve_3rd_party, _Statename, Call, _GenMediaState, #state{statename = 'in_conference_3rdparty'} = State) ->
?INFO("Taking agent out of conference, briding to 3rdparyt",[]),
#state{cnode = Fnode, ringuuid = Ringid, '3rd_party_id' = Thirdy} = State,
freeswitch:api(Fnode, uuid_bridge, Thirdy ++ " " ++ Ringid),
{noreply, State#state{statename = '3rd_party'}};
handle_cast(hangup_3rd_party, Statename, Call, GenMediaState, #state{statename = '3rd_party'} = State) ->
?INFO("killing 3rd party channel while talking w/ them", []),
{noreply, MidState} = handle_cast(toggle_hold, Statename, Call, GenMediaState, State),
handle_cast(hangup_3rd_party, Statename, Call, GenMediaState, MidState);
handle_cast({merge_3rd_party, _IncludeSelf}, _Statename, Call, _GenMediaState, #state{'3rd_party_id' = undefined} = State) ->
{noreply, State};
handle_cast({merge_3rd_party, IncludeAgent}, _Statename, Call, _GenMediaState, State) ->
#state{cnode = Fnode, ringuuid = Ringid, '3rd_party_id' = Thirdid, conference_id = Confid} = State,
NextState = case IncludeAgent of
true -> 'in_conference';
_ -> 'hold_conference'
end,
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " park inline"),
freeswitch:bgapi(Fnode, uuid_transfer, Thirdid ++ " conference:" ++ Confid ++ " inline"),
{noreply, State#state{statename = NextState}};
% retrieve conference also works here.
handle_cast(retrieve_3rd_party, _Statename, Call, _GenMediaState, #state{statename = hold_conference_3rdparty} = State) ->
#state{cnode= Fnode, ringuuid = Ringid, '3rd_party_id' = Thirdpid} = State,
?INFO("Picking up help 3rd party: ~s", [Thirdpid]),
freeswitch:bgapi(Fnode, uuid_bridge, Thirdpid ++ " " ++ Ringid),
{noreply, State#state{statename = '3rd_party'}};
handle_cast(hangup_3rd_party, _Statename, Call, _GenMediaState, #state{statename = 'hold_conference_3rdparty'} = State) ->
?INFO("hangling up on 3rd party", []),
#state{cnode = Fnode, '3rd_party_id' = Thirdid} = State,
freeswitch:bgapi(Fnode, uuid_kill, Thirdid),
{noreply, State#state{statename = 'hold_conference'}};
% in_conference -> '3rdparty' | 'conference_hold'
handle_cast(toggle_hold, _Statename, Call, _GenMediaState, #state{statename = 'in_conference'} = State) ->
?INFO("Place conference on hold", []),
#state{cnode = Fnode, ringuuid = Ringid} = State,
ok = fs_send_execute(Fnode, Ringid, "set", "hangup_after_bridge=false"),
ok = fs_send_execute(Fnode, Ringid, "set", "park_after_bridge=true"),
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " park inline"),
{{mediapush, hold_conference}, State#state{statename = hold_conference}};
handle_cast({contact_3rd_party, _Targ} = Cast, Statename, Call, GenMediaState, #state{statename = 'in_conference'} = State) ->
?INFO("contact 3rd party, means place conference on hold first", []),
{noreply, MidState} = handle_cast(toggle_hold, Statename, Call, GenMediaState, State),
handle_cast(Cast, Statename, Call, GenMediaState, MidState);
% any state.
handle_cast({audio_level, Target, Level}, _Statename, Call, _GenMediaState, #state{statename = Statename} =
State) when Statename == oncall; Statename == oncall_ringing ->
?INFO("uuid_audio for ~s with direction ~s set to ~p", [Call#call.id, Target, Level]),
ApiStr = Call#call.id ++ " start " ++ binary_to_list(Target) ++ " level "
++ integer_to_list(Level),
freeswitch:bgapi(State#state.cnode, uuid_audio, ApiStr),
{noreply, State};
handle_cast({blind_transfer, Destination}, _Statename, Call, _GenMediaState, #state{statename = oncall} = State) ->
#state{cnode = Fnode} = State,
#call{client = Client, id = UUID} = Call,
#client{options= ClientOpts} = Client,
{CallerNameOpt,CallerNumberOpt} = case proplists:get_value(<<"caller_id">>, ClientOpts) of
undefined -> {"",""};
{BinName,BinNumber} when is_binary(BinName),is_binary(BinNumber) ->
{binary_to_list(BinName),binary_to_list(BinNumber)};
CidOut -> CidOut
end,
BaseDS = freeswitch_media_manager:get_default_dial_string(),
RingOpts = [CallerNameOpt, CallerNumberOpt, "hangup_after_bridge=false"],
Dialstring = freeswitch_media_manager:do_dial_string(BaseDS, Destination, RingOpts),
?DEBUG("Transfering ~s to ~s blindly.", [UUID, Dialstring]),
freeswitch:api(Fnode, uuid_setvar, UUID ++ " park_after_bridge true"),
freeswitch:bgapi(Fnode, uuid_transfer, UUID ++ " 'm:^:bridge:" ++ Dialstring ++ "' inline"),
{wrapup, State#state{statename = 'blind_transfered'}};
handle_cast({play_dtmf, []}, _Statename, _Call, _GenMediaState, State) ->
?DEBUG("Dtmf not played due to no digits", []),
{noreply, State};
handle_cast({play_dtmf, Digits}, Statename, Call, GenMediaState, State) when is_binary(Digits) ->
handle_cast({play_dtmf, binary_to_list(Digits)}, Statename, Call, GenMediaState, State);
handle_cast({play_dtmf, Digits}, _Statename, Call, _GenMediaState, #state{statename = oncall} = State) ->
#state{cnode = Fnode} = State,
#call{id = UUID} = Call,
freeswitch:bgapi(Fnode, uuid_send_dtmf, UUID ++ " " ++ Digits),
{noreply, State};
%% web api's
handle_cast({agent_web_connection, <<"toggle_hold">>, _}, Statename, Call, GenMediaState, State) ->
handle_cast(toggle_hold, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"retrieve_3rd_party">>, _}, Statename, Call, GenMediaState, State) ->
handle_cast(retrieve_3rd_party, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"contact_3rd_party">>, Args}, Statename, Call, GenMediaState, State) ->
Destination = binary_to_list(proplists:get_value("args", Args)),
handle_cast({contact_3rd_party, Destination}, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"retrieve_conference">>, _Args}, Statename, Call, GenMediaState, State) ->
handle_cast(retrieve_conference, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"merge_3rd_party">>, Args}, Statename, Call, GenMediaState, State) ->
IncludeAgent = case proplists:get_value("args", Args) of
<<"true">> ->
true;
_ ->
false
end,
handle_cast({merge_3rd_party, IncludeAgent}, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"hangup_3rd_party">>, _}, Statename, Call, GenMediaState, State) ->
handle_cast(hangup_3rd_party, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"audio_level">>, Arguments}, Statename, Call, GenMediaState, State) ->
[Target, Level] = case proplists:get_value("args", Arguments, [<<"read">>, 0]) of
[<<"write">>, N] -> [write, N];
[_,N] -> [read,N];
_ -> [read,0]
end,
handle_cast({audio_level, Target, Level}, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"blind_transfer">>, Args}, Statename, Call, GenMediaState, State) ->
Dest = proplists:get_value("args", Args),
handle_cast({blind_transfer, Dest}, Statename, Call, GenMediaState, State);
handle_cast({agent_web_connection, <<"play_dtmf">>, Args}, Statename, Call, GMState, State) ->
Digits = case proplists:get_value("args", Args, []) of
X when is_integer(X) -> integer_to_list(X);
X -> X
end,
handle_cast({play_dtmf, Digits}, Statename, Call, GMState, State);
%% tcp api's
handle_cast(Request, Statename, Call, GenMediaState, State) when is_record(Request, mediacommandrequest) ->
FixedRequest = cpx_freeswitch_pb:decode_extensions(Request),
Hint = case cpx_freeswitch_pb:get_extension(FixedRequest, freeswitch_request_hint) of
{ok, O} -> O;
_ -> undefined
end,
case Hint of
'SET_AUDIO_LEVEL' ->
case cpx_freeswitch_pb:get_extension(audio_level_request,FixedRequest) of
#audiolevelrequest{channel = X, value = Y} when X == undefined; Y == undefined ->
{noreply, State};
#audiolevelrequest{channel = Chan, value = Val} ->
Target = case Chan of
'SPEAKER' -> write;
'MIC' -> read
end,
handle_cast({audio_level, Target, Val}, Statename, Call, GenMediaState, State);
_ ->
{noreply, State}
end;
'TOGGLE_HOLD' ->
handle_cast(toggle_hold, Statename, Call, GenMediaState, State);
'CONTACT_3RD_PARTY' ->
case cpx_freeswitch_pb:get_extension(FixedRequest, contact_3rd_party) of
undefined -> handle_cast(contact_3rd_party, Statename, Call, GenMediaState, State);
{ok, #contact3rdpartyrequest{target = Target}} ->
handle_cast({contact_3rd_party, Target}, Statename, Call, GenMediaState, State)
end;
'RETRIEVE_CONFERENCE' -> handle_cast(retrieve_conference, Statename, Call, GenMediaState, State);
'MERGE_3RD_PARTY' ->
case cpx_freeswitch_pb:get_extension(FixedRequest, merge_3rd_party) of
undefined ->
handle_cast({merge_3rd_party, false}, Statename, Call, GenMediaState, State);
{ok, #merge3rdpartyrequest{include_self = AndSelf}} ->
handle_cast({merge_3rd_party, AndSelf}, Statename, Call, GenMediaState, State)
end;
'RETRIEVE_3RD_PARTY' ->
handle_cast(retrieve_3rd_party, Statename, Call, GenMediaState, State);
'HANGUP_3RD_PARTY' ->
handle_cast(hangup_3rd_party, Statename, Call, GenMediaState, State);
'BLIND_TRANSFER' ->
case cpx_freeswitch_pb:get_extension(FixedRequest,blind_transfer) of
{ok, #blindtransferrequest{target = Dest}} ->
handle_cast({blind_transfer, Dest}, Statename, Call, GenMediaState, State);
BTIgnored ->
?DEBUG("blind transfer ignored: ~p", [BTIgnored]),
{noreply, State}
end;
'PLAY_DTMF' ->
case cpx_freeswitch_pb:get_extension(FixedRequest, dtmf_string) of
{ok, []} ->
?DEBUG("dtmf request ignored due to lack of digits", []),
{noreply, State};
{ok, Dtmf} ->
handle_cast({send_dtmf, Dtmf}, Statename, Call, GenMediaState, State);
DtmfIgnored ->
?DEBUG("dtmf request ignored: ~p", [DtmfIgnored]),
{noreply, State}
end;
FullReqIgnored ->
?DEBUG("Request fully ignored: ~p", [FullReqIgnored]),
{noreply, State}
end;
handle_cast({'3rd_party_pickup', ChanPid}, _Statename, Call, _GenMediaState, #state{'3rd_party_mon' = {ChanPid, ChanMon}} = State) ->
#state{cnode = Fnode, '3rd_party_id' = OtherParty, ringuuid = AgentChan} = State,
freeswitch:bgapi(Fnode, uuid_bridge, OtherParty ++ " " ++ AgentChan),
{noreply, State#state{statename = '3rd_party'}};
handle_cast({set_caseid, CaseID}, _Statename, Call, _GenMediaState, State) ->
?INFO("setting caseid for ~p to ~p", [Call#call.id, CaseID]),
{noreply, State#state{caseid = CaseID}};
handle_cast(Msg, _, _Call, _, State) ->
?DEBUG("unhandled cast while in state ~p: ~p", [State#state.statename, Msg]),
{noreply, State}.
%%--------------------------------------------------------------------
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
%% @private
handle_info(check_recovery, _StateName, Call, _Internal, State) ->
case whereis(freeswitch_media_manager) of
Pid when is_pid(Pid) ->
link(Pid),
gen_server:cast(freeswitch_media_manager, {notify, Call#call.id, self()}),
{noreply, State#state{manager_pid = Pid}};
_Else ->
{ok, Tref} = timer:send_after(1000, check_recovery),
{noreply, State#state{manager_pid = Tref}}
end;
handle_info({'EXIT', Pid, Reason}, StateName, Call, _Internal,
#state{ringchannel = Pid} = State) when StateName =:= oncall_ringing;
StateName =:= inqueue_ringing ->
?WARNING("Handling ring channel ~w exit ~p for ~p", [Pid, Reason, Call#call.id]),
{stop_ring, State#state{ringchannel = undefined}};
handle_info({'EXIT', Pid, noconnection}, _StateName, _Call, _Internal,
State) ->
?WARNING("Exit of ~p due to noconnection; this normally indicates a fatal freeswitch failure, so going down too.", [Pid]),
{stop, noconnection, State};
handle_info({'EXIT', Pid, Reason}, _StateName, Call, _Internal, #state{manager_pid = Pid} = State) ->
?WARNING("Handling manager exit from ~w due to ~p for ~p", [Pid, Reason, Call#call.id]),
{ok, Tref} = timer:send_after(1000, check_recovery),
{noreply, State#state{manager_pid = Tref}};
%% TODO make awesome by taking advantage of the state machine-ness.
handle_info({call, {event, [UUID | Rest]}}, _StateName, Call, _Internal, State) when is_list(UUID) ->
SetSess = freeswitch:session_setevent(State#state.cnode, [
'CHANNEL_BRIDGE', 'CHANNEL_PARK', 'CHANNEL_HANGUP',
'CHANNEL_HANGUP_COMPLETE', 'CHANNEL_DESTROY', 'DTMF',
'CHANNEL_ANSWER', 'CUSTOM', 'conference::maintenance']),
?DEBUG("reporting new call ~p (eventage: ~p).", [UUID, SetSess]),
case State#state.uuid of
UUID -> freeswitch_media_manager:notify(UUID, self());
_ -> ok
end,
case_event_name([UUID | Rest], Call, State#state{in_control = true});
handle_info({call_event, {event, [UUID | Rest]}}, _StateName, Call, _Internal, State) when is_list(UUID) ->
case_event_name([ UUID | Rest], Call, State);
handle_info({set_agent, Login, Apid}, _StateName, _Call, _Intenral, State) ->
{noreply, State#state{agent = Login, agent_pid = Apid}};
handle_info({bgok, Reply}, _StateName, Call, _Internal, State) ->
?DEBUG("bgok: ~p for ~p", [Reply, Call#call.id]),
{noreply, State};
handle_info({bgerror, "-ERR NO_ANSWER\n"}, _StateName, Call, _Internal, State) ->
?INFO("Potential ringout. Statecook: ~p for ~p", [State#state.cook, Call#call.id]),
%% the apid is known by gen_media, let it handle if it is not not.
{stop_ring, State};
handle_info({bgerror, "-ERR USER_BUSY\n"}, _StateName, Call, _Internal, State) ->
?NOTICE("Agent rejected the call ~p", [Call#call.id]),
{stop_ring, State};
handle_info({bgerror, Reply}, _StateName, Call, _Internal, State) ->
?WARNING("unhandled bgerror: ~p for ~p", [Reply, Call#call.id]),
{noreply, State};
handle_info(channel_destroy, _StateName, Call, _Internal, #state{in_control = InControl} = State) when not InControl ->
?NOTICE("Hangup in IVR for ~p", [Call#call.id]),
{stop, hangup, State};
% TODO This had a use at some point, but was cuasing ramdom hangups.
% need to find what was sending :(
%handle_info(call_hangup, Call, State) ->
% ?NOTICE("Call hangup info, terminating ~p", [Call#call.id]),
% catch freeswitch_ring:hangup(State#state.ringchannel),
% {stop, normal, State};
handle_info({'DOWN', Ref, process, Pid, Cause}, _Statename, Call,
_Internal, #state{statename = oncall,
spawn_oncall_mon = {Pid, Ref}} = State) ->
?DEBUG("Oncaller pid termination cause: ~p", [Cause]),
cdr:media_custom(Call, oncall, ?cdr_states, []),
{{mediapush, caller_offhold}, State#state{spawn_oncall_mon = undefined}};
handle_info(call_hangup, _StateName, Call, _Internal, State) ->
?NOTICE("Call hangup info, terminating ~p", [Call#call.id]),
catch freeswitch_ring:hangup(State#state.ringchannel),
{stop, normal, State};
handle_info(Info, _StateName, Call, _Internal, State) ->
?INFO("unhandled info ~p for ~p", [Info, Call#call.id]),
?DEBUG("unhandled state: ~p", [State]),
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%%--------------------------------------------------------------------
%% @private
terminate(Reason, _StateName, Call, _Internal, _State) ->
?NOTICE("terminating: ~p ~p", [Reason, Call#call.id]),
ok.
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%%--------------------------------------------------------------------
%% @private
code_change(_OldVsn, _Call, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
%% @private
fs_send_execute(Node, Callid, Name, Arg) ->
freeswitch:sendmsg(Node, Callid, [
{"call-command", "execute"},
{"execute-app-name", "set"},
{"execute-app-arg", Arg}
]).
originate(Node, BaseDialstring, Destination, InOpts) ->
{ok, UUID} = freeswitch:api(Node, create_uuid),
Opts = ["origination_uuid=" ++ UUID | InOpts],
Dialstring = freeswitch_media_manager:do_dial_string(BaseDialstring, Destination, Opts),
case freeswitch:bgapi(Node, originate, Dialstring ++ " &park()") of
{ok, _BgApiId} ->
case originate_gethandle(Node, UUID) of
{error, Err} ->
?WARNING("Cound not originate ~s due to ~p", [UUID, Err]),
{error, Err};
_ ->
{ok, UUID}
end;
Bgerr ->
?WARNING("Could not start originate: ~p", [Bgerr]),
{error, Bgerr}
end.
originate_gethandle(Node, UUID) ->
originate_gethandle(Node, UUID, 0).
originate_gethandle(Node, UUID, Count) ->
case freeswitch:handlecall(Node, UUID) of
{error, badsession} when Count > 10 ->
{error, badsession};
{error, badsession} ->
timer:sleep(100),
originate_gethandle(Node, UUID, Count + 1);
{error, Other} ->
{error, Other};
Else ->
Else
end.
%% @private
case_event_name([UUID | Rawcall], Callrec, State) ->
Ename = case proplists:get_value("Event-Name", Rawcall) of
"CUSTOM" -> {"CUSTOM", proplists:get_value("Event-Subclass", Rawcall)};
Else -> Else
end,
?DEBUG("Event ~p for ~s while in state ~p", [Ename, UUID, State#state.statename]),
case_event_name(Ename, UUID, Rawcall, Callrec, State).
%% @private
case_event_name({"CUSTOM", "conference::maintenance"}, UUID, _Rawcall, Callrec, #state{statename = Statename, '3rd_party_id' = UUID} = State) when
Statename =:= 'in_conference'; Statename =:= 'hold_conference' ->
?INFO("finishing up a 3rd paryt merge",[]),
case Statename of
in_conference ->
#state{cnode = Fnode, conference_id = Confid, ringuuid = Ringid} = State,
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " 'conference:" ++ Confid ++ "' inline");
_ ->
ok
end,
cdr:media_custom(Callrec, Statename, ?cdr_states, []),
{{mediapush, Statename}, State#state{'3rd_party_id' = undefined}};
case_event_name("CHANNEL_ANSWER", UUID, _Rawcall, Callrec, #state{
statename = hold_conference_3rdparty, '3rd_party_id' = UUID} = State) ->
#state{cnode = Fnode, ringuuid = Ruuid} = State,
freeswitch:api(Fnode, uuid_setvar_multi, Ruuid ++ " hangup_after_bridge=false;park_after_bridge=true"),
freeswitch:bgapi(Fnode, uuid_bridge, UUID ++ " " ++ Ruuid),
cdr:media_custom(Callrec, '3rd_party', ?cdr_states, []),
{{mediapush, '3rd_party'}, State#state{statename = '3rd_party'}};
case_event_name("CHANNEL_BRIDGE", UUID, _Rawcall, Callrec, #state{'3rd_party_id' = UUID, statename = '3rd_party'} = State) ->
?DEBUG("Telling agent we're now oncall w/ the 3rd party", []),
cdr:media_custom(Callrec, '3rd_party', ?cdr_states, []),
{{mediapush, '3rd_party'}, State};
case_event_name(EventName, UUID, _Rawcall, _Callrec, #state{statename = blind_transfered} = State) ->
?DEBUG("Blind transfer state doing nothing for event event ~s of uuid ~s", [EventName, UUID]),
{noreply, State};
case_event_name("CHANNEL_BRIDGE", UUID, _Rawcall, Callrec, #state{ringchannel = Rpid, uuid = UUID} = State) when is_pid(Rpid) ->
% TODO fix when this can return an {oncall, State}
RingUUID = freeswitch_ring:get_uuid(State#state.ringchannel),
SpawnOncall = spawn_monitor(fun() ->
Oot = gen_media:oncall(Callrec#call.source),
?DEBUG("Result of oncall: ~p", [Oot])
end),
{noreply, State#state{statename = oncall, spawn_oncall_mon = SpawnOncall, ringuuid = RingUUID}};
case_event_name("CHANNEL_PARK", Thirdy, Rawcall, Callrec, #state{
statename = 'in_conference_3rdparty', '3rd_party_id' = Thirdy} = State) ->
#state{conference_id = Confid, cnode = Fnode, ringuuid = Ringid} = State,
?DEBUG("Most likely in the middle of putting the agent in a conference", []),
freeswitch:api(Fnode, uuid_transfer, Ringid ++ " 'conference:" ++ Confid ++ "' inline"),
cdr:media_custom(Callrec, State#state.statename, ?cdr_states, []),
{{mediapush, 'in_conference_3rdparty'}, State};
case_event_name("CHANNEL_PARK", UUID, Rawcall, Callrec, #state{statename = hold_conference_3rdparty, '3rd_party_id' = UUID} = State) ->
?DEBUG("park of the 3rd party, proll a hold", []),
cdr:media_custom(Callrec, State#state.statename, ?cdr_states, []),
{{mediapush, State#state.statename}, State};
case_event_name("CHANNEL_PARK", UUID, Rawcall, Callrec, #state{
statename = oncall_hold, uuid = UUID} = State) ->
Moh = case proplists:get_value("variable_queue_moh", Rawcall, "moh") of
"silence" ->
none;
MohMusak ->
MohMusak
end,
case Moh of
none ->
freeswitch:sendmsg(State#state.cnode, UUID,
[{"call-command", "execute"},
{"execute-app-name", "playback"},
{"execute-app-arg", "silence"}]);
_MohMusak ->
freeswitch:sendmsg(State#state.cnode, UUID,
[{"call-command", "execute"},
{"execute-app-name", "playback"},
{"execute-app-arg", "local_stream://"++Moh}])
end,
cdr:media_custom(Callrec, 'oncall_hold', ?cdr_states, []),
{{mediapush, caller_hold}, State};
case_event_name("CHANNEL_DESTROY", UUID, Rawcall, Callrec, #state{
'3rd_party_id' = UUID, statename = Statename} = State) ->
{{mediapush, Statename}, State#state{'3rd_party_id' = undefined}};