-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mod_callcenter.c
4372 lines (3773 loc) · 178 KB
/
mod_callcenter.c
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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* 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 FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Marc Olivier Chouinard <mochouinard@moctel.com>
* Emmanuel Schmidbauer <e.schmidbauer@gmail.com>
* Ítalo Rossi <italorossib@gmail.com>
* Fernando Santos <fernando@nextbilling.com.br>
*
* mod_callcenter.c -- Call Center Module
*
*/
#include <switch.h>
#define CALLCENTER_EVENT "callcenter::info"
#define CC_AGENT_TYPE_CALLBACK "Callback"
#define CC_AGENT_TYPE_UUID_STANDBY "uuid-standby"
#define CC_SQLITE_DB_NAME "callcenter"
#define CC_APP_KEY "mod_callcenter"
/* Prototypes */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_callcenter_shutdown);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_callcenter_runtime);
SWITCH_MODULE_LOAD_FUNCTION(mod_callcenter_load);
/* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime)
* Defines a switch_loadable_module_function_table_t and a static const char[] modname
*/
SWITCH_MODULE_DEFINITION(mod_callcenter, mod_callcenter_load, mod_callcenter_shutdown, NULL);
static switch_status_t load_agent(const char *agent_name, switch_event_t *params, switch_xml_t x_agents_cfg);
static switch_status_t load_tiers(switch_bool_t load_all, const char *queue_name, const char *agent_name, switch_event_t *params, switch_xml_t x_tiers_cfg);
static const char *global_cf = "callcenter.conf";
struct cc_status_table {
const char *name;
int status;
};
struct cc_state_table {
const char *name;
int state;
};
typedef enum {
CC_STATUS_SUCCESS,
CC_STATUS_FALSE,
CC_STATUS_AGENT_NOT_FOUND,
CC_STATUS_QUEUE_NOT_FOUND,
CC_STATUS_AGENT_ALREADY_EXIST,
CC_STATUS_AGENT_INVALID_TYPE,
CC_STATUS_AGENT_INVALID_STATUS,
CC_STATUS_AGENT_INVALID_STATE,
CC_STATUS_TIER_ALREADY_EXIST,
CC_STATUS_TIER_NOT_FOUND,
CC_STATUS_TIER_INVALID_STATE,
CC_STATUS_INVALID_KEY
} cc_status_t;
typedef enum {
CC_TIER_STATE_UNKNOWN = 0,
CC_TIER_STATE_NO_ANSWER = 1,
CC_TIER_STATE_READY = 2,
CC_TIER_STATE_OFFERING = 3,
CC_TIER_STATE_ACTIVE_INBOUND = 4,
CC_TIER_STATE_STANDBY = 5
} cc_tier_state_t;
static struct cc_state_table STATE_CHART[] = {
{"Unknown", CC_TIER_STATE_UNKNOWN},
{"No Answer", CC_TIER_STATE_NO_ANSWER},
{"Ready", CC_TIER_STATE_READY},
{"Offering", CC_TIER_STATE_OFFERING},
{"Active Inbound", CC_TIER_STATE_ACTIVE_INBOUND},
{"Standby", CC_TIER_STATE_STANDBY},
{NULL, 0}
};
typedef enum {
CC_AGENT_STATUS_UNKNOWN = 0,
CC_AGENT_STATUS_LOGGED_OUT = 1,
CC_AGENT_STATUS_AVAILABLE = 2,
CC_AGENT_STATUS_AVAILABLE_ON_DEMAND = 3,
CC_AGENT_STATUS_ON_BREAK = 4
} cc_agent_status_t;
static struct cc_status_table AGENT_STATUS_CHART[] = {
{"Unknown", CC_AGENT_STATUS_UNKNOWN},
{"Logged Out", CC_AGENT_STATUS_LOGGED_OUT},
{"Available", CC_AGENT_STATUS_AVAILABLE},
{"Available (On Demand)", CC_AGENT_STATUS_AVAILABLE_ON_DEMAND},
{"On Break", CC_AGENT_STATUS_ON_BREAK},
{NULL, 0}
};
typedef enum {
CC_AGENT_STATE_UNKNOWN = 0,
CC_AGENT_STATE_WAITING = 1,
CC_AGENT_STATE_RECEIVING = 2,
CC_AGENT_STATE_IN_A_QUEUE_CALL = 3,
CC_AGENT_STATE_IDLE = 4,
CC_AGENT_STATE_RESERVED = 5
} cc_agent_state_t;
static struct cc_state_table AGENT_STATE_CHART[] = {
{"Unknown", CC_AGENT_STATE_UNKNOWN},
{"Waiting", CC_AGENT_STATE_WAITING},
{"Receiving", CC_AGENT_STATE_RECEIVING},
{"In a queue call", CC_AGENT_STATE_IN_A_QUEUE_CALL},
{"Idle", CC_AGENT_STATE_IDLE},
{"Reserved", CC_AGENT_STATE_RESERVED},
{NULL, 0}
};
typedef enum {
CC_MEMBER_STATE_UNKNOWN = 0,
CC_MEMBER_STATE_WAITING = 1,
CC_MEMBER_STATE_TRYING = 2,
CC_MEMBER_STATE_ANSWERED = 3,
CC_MEMBER_STATE_ABANDONED = 4
} cc_member_state_t;
static struct cc_state_table MEMBER_STATE_CHART[] = {
{"Unknown", CC_MEMBER_STATE_UNKNOWN},
{"Waiting", CC_MEMBER_STATE_WAITING},
{"Trying", CC_MEMBER_STATE_TRYING},
{"Answered", CC_MEMBER_STATE_ANSWERED},
{"Abandoned", CC_MEMBER_STATE_ABANDONED},
{NULL, 0}
};
struct cc_member_cancel_reason_table {
const char *name;
int reason;
};
typedef enum {
CC_MEMBER_CANCEL_REASON_NONE,
CC_MEMBER_CANCEL_REASON_TIMEOUT,
CC_MEMBER_CANCEL_REASON_NO_AGENT_TIMEOUT,
CC_MEMBER_CANCEL_REASON_BREAK_OUT,
CC_MEMBER_CANCEL_REASON_EXIT_WITH_KEY
} cc_member_cancel_reason_t;
static struct cc_member_cancel_reason_table MEMBER_CANCEL_REASON_CHART[] = {
{"NONE", CC_MEMBER_CANCEL_REASON_NONE},
{"TIMEOUT", CC_MEMBER_CANCEL_REASON_TIMEOUT},
{"NO_AGENT_TIMEOUT", CC_MEMBER_CANCEL_REASON_NO_AGENT_TIMEOUT},
{"BREAK_OUT", CC_MEMBER_CANCEL_REASON_BREAK_OUT},
{"EXIT_WITH_KEY", CC_MEMBER_CANCEL_REASON_EXIT_WITH_KEY},
{NULL, 0}
};
typedef enum {
CC_APP_AGENT_CONNECTING = (1 << 0),
CC_APP_SHOULD_BREAK = (1 << 1)
} cc_app_flag_t;
static char members_sql[] =
"CREATE TABLE members (\n"
" queue VARCHAR(255),\n"
" instance_id VARCHAR(255),\n"
" uuid VARCHAR(255) NOT NULL DEFAULT '',\n"
" session_uuid VARCHAR(255) NOT NULL DEFAULT '',\n"
" cid_number VARCHAR(255),\n"
" cid_name VARCHAR(255),\n"
" system_epoch INTEGER NOT NULL DEFAULT 0,\n"
" joined_epoch INTEGER NOT NULL DEFAULT 0,\n"
" rejoined_epoch INTEGER NOT NULL DEFAULT 0,\n"
" bridge_epoch INTEGER NOT NULL DEFAULT 0,\n"
" abandoned_epoch INTEGER NOT NULL DEFAULT 0,\n"
" base_score INTEGER NOT NULL DEFAULT 0,\n"
" skill_score INTEGER NOT NULL DEFAULT 0,\n"
" serving_agent VARCHAR(255),\n"
" serving_system VARCHAR(255),\n"
" state VARCHAR(255)\n" ");\n";
/* Member State
Waiting
Answered
*/
static char agents_sql[] =
"CREATE TABLE agents (\n"
" name VARCHAR(255),\n"
" instance_id VARCHAR(255),\n"
" uuid VARCHAR(255),\n"
" type VARCHAR(255),\n" /* Callback , Dial in...*/
" contact VARCHAR(1024),\n"
" status VARCHAR(255),\n"
/*User Personal Status
Available
On Break
Logged Out
*/
" state VARCHAR(255),\n"
/* User Personal State
Waiting
Receiving
In a queue call
*/
" max_no_answer INTEGER NOT NULL DEFAULT 0,\n"
" wrap_up_time INTEGER NOT NULL DEFAULT 0,\n"
" reject_delay_time INTEGER NOT NULL DEFAULT 0,\n"
" busy_delay_time INTEGER NOT NULL DEFAULT 0,\n"
" no_answer_delay_time INTEGER NOT NULL DEFAULT 0,\n"
" last_bridge_start INTEGER NOT NULL DEFAULT 0,\n"
" last_bridge_end INTEGER NOT NULL DEFAULT 0,\n"
" last_offered_call INTEGER NOT NULL DEFAULT 0,\n"
" last_status_change INTEGER NOT NULL DEFAULT 0,\n"
" no_answer_count INTEGER NOT NULL DEFAULT 0,\n"
" calls_answered INTEGER NOT NULL DEFAULT 0,\n"
" talk_time INTEGER NOT NULL DEFAULT 0,\n"
" ready_time INTEGER NOT NULL DEFAULT 0,\n"
" external_calls_count INTEGER NOT NULL DEFAULT 0\n"
");\n";
static char tiers_sql[] =
"CREATE TABLE tiers (\n"
" queue VARCHAR(255),\n"
" agent VARCHAR(255),\n"
" state VARCHAR(255),\n"
/*
Agent State:
Ready
Active inbound
Wrap-up inbound
Standby
No Answer
Offering
*/
" level INTEGER NOT NULL DEFAULT 1,\n"
" position INTEGER NOT NULL DEFAULT 1\n" ");\n";
static switch_xml_config_int_options_t config_int_0_86400 = { SWITCH_TRUE, 0, SWITCH_TRUE, 86400 };
/* TODO This is temporary until we either move it to the core, or use it differently in the module */
switch_time_t local_epoch_time_now(switch_time_t *t)
{
switch_time_t now = switch_micro_time_now() / 1000000; /* APR_USEC_PER_SEC */
if (t) {
*t = now;
}
return now;
}
const char * cc_tier_state2str(cc_tier_state_t state)
{
uint8_t x;
const char *str = "Unknown";
for (x = 0; x < (sizeof(STATE_CHART) / sizeof(struct cc_state_table)) - 1; x++) {
if (STATE_CHART[x].state == state) {
str = STATE_CHART[x].name;
break;
}
}
return str;
}
cc_tier_state_t cc_tier_str2state(const char *str)
{
uint8_t x;
cc_tier_state_t state = CC_TIER_STATE_UNKNOWN;
for (x = 0; x < (sizeof(STATE_CHART) / sizeof(struct cc_state_table)) - 1 && STATE_CHART[x].name; x++) {
if (!strcasecmp(STATE_CHART[x].name, str)) {
state = STATE_CHART[x].state;
break;
}
}
return state;
}
const char * cc_member_cancel_reason2str(cc_member_cancel_reason_t reason)
{
uint8_t x;
const char *str = "NONE";
for (x = 0; x < (sizeof(MEMBER_CANCEL_REASON_CHART) / sizeof(struct cc_member_cancel_reason_table)) - 1; x++) {
if (MEMBER_CANCEL_REASON_CHART[x].reason == reason) {
str = MEMBER_CANCEL_REASON_CHART[x].name;
break;
}
}
return str;
}
cc_member_cancel_reason_t cc_member_cancel_str2reason(const char *str)
{
uint8_t x;
cc_member_cancel_reason_t reason = CC_MEMBER_CANCEL_REASON_NONE;
for (x = 0; x < (sizeof(MEMBER_CANCEL_REASON_CHART) / sizeof(struct cc_member_cancel_reason_table)) - 1 && MEMBER_CANCEL_REASON_CHART[x].name; x++) {
if (!strcasecmp(MEMBER_CANCEL_REASON_CHART[x].name, str)) {
reason = MEMBER_CANCEL_REASON_CHART[x].reason;
break;
}
}
return reason;
}
const char * cc_agent_status2str(cc_agent_status_t status)
{
uint8_t x;
const char *str = "Unknown";
for (x = 0; x < (sizeof(AGENT_STATUS_CHART) / sizeof(struct cc_status_table)) - 1; x++) {
if (AGENT_STATUS_CHART[x].status == status) {
str = AGENT_STATUS_CHART[x].name;
break;
}
}
return str;
}
cc_agent_status_t cc_agent_str2status(const char *str)
{
uint8_t x;
cc_agent_status_t status = CC_AGENT_STATUS_UNKNOWN;
for (x = 0; x < (sizeof(AGENT_STATUS_CHART) / sizeof(struct cc_status_table)) - 1 && AGENT_STATUS_CHART[x].name; x++) {
if (!strcasecmp(AGENT_STATUS_CHART[x].name, str)) {
status = AGENT_STATUS_CHART[x].status;
break;
}
}
return status;
}
const char * cc_agent_state2str(cc_agent_state_t state)
{
uint8_t x;
const char *str = "Unknown";
for (x = 0; x < (sizeof(AGENT_STATE_CHART) / sizeof(struct cc_state_table)) - 1; x++) {
if (AGENT_STATE_CHART[x].state == state) {
str = AGENT_STATE_CHART[x].name;
break;
}
}
return str;
}
cc_agent_state_t cc_agent_str2state(const char *str)
{
uint8_t x;
cc_agent_state_t state = CC_AGENT_STATE_UNKNOWN;
for (x = 0; x < (sizeof(AGENT_STATE_CHART) / sizeof(struct cc_state_table)) - 1 && AGENT_STATE_CHART[x].name; x++) {
if (!strcasecmp(AGENT_STATE_CHART[x].name, str)) {
state = AGENT_STATE_CHART[x].state;
break;
}
}
return state;
}
const char * cc_member_state2str(cc_member_state_t state)
{
uint8_t x;
const char *str = "Unknown";
for (x = 0; x < (sizeof(MEMBER_STATE_CHART) / sizeof(struct cc_state_table)) - 1; x++) {
if (MEMBER_STATE_CHART[x].state == state) {
str = MEMBER_STATE_CHART[x].name;
break;
}
}
return str;
}
cc_member_state_t cc_member_str2state(const char *str)
{
uint8_t x;
cc_member_state_t state = CC_MEMBER_STATE_UNKNOWN;
for (x = 0; x < (sizeof(MEMBER_STATE_CHART) / sizeof(struct cc_state_table)) - 1 && MEMBER_STATE_CHART[x].name; x++) {
if (!strcasecmp(MEMBER_STATE_CHART[x].name, str)) {
state = MEMBER_STATE_CHART[x].state;
break;
}
}
return state;
}
typedef enum {
PFLAG_DESTROY = 1 << 0
} cc_flags_t;
static struct {
switch_hash_t *queue_hash;
int debug;
char *odbc_dsn;
char *dbname;
const char *cc_instance_id;
switch_bool_t reserve_agents;
switch_bool_t truncate_tiers;
switch_bool_t truncate_agents;
switch_bool_t global_database_lock;
int32_t threads;
int32_t running;
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
switch_event_node_t *node;
int agent_originate_timeout;
} globals;
#define CC_QUEUE_CONFIGITEM_COUNT 100
struct cc_queue {
char *name;
char *strategy;
char *moh;
char *announce;
uint32_t announce_freq;
char *record_template;
char *time_base_score;
uint32_t ring_progressively_delay;
switch_bool_t tier_rules_apply;
uint32_t tier_rule_wait_second;
switch_bool_t tier_rule_wait_multiply_level;
switch_bool_t tier_rule_no_agent_no_wait;
uint32_t discard_abandoned_after;
switch_bool_t abandoned_resume_allowed;
uint32_t max_wait_time;
uint32_t max_wait_time_with_no_agent;
uint32_t max_wait_time_with_no_agent_time_reached;
char *agent_no_answer_status;
uint32_t calls_answered;
uint32_t calls_abandoned;
switch_mutex_t *mutex;
switch_thread_rwlock_t *rwlock;
switch_memory_pool_t *pool;
uint32_t flags;
switch_time_t last_agent_exist;
switch_time_t last_agent_exist_check;
switch_bool_t skip_agents_with_external_calls;
switch_xml_config_item_t config[CC_QUEUE_CONFIGITEM_COUNT];
switch_xml_config_string_options_t config_str_pool;
};
typedef struct cc_queue cc_queue_t;
static void cc_send_presence(const char *queue_name);
static void free_queue(cc_queue_t *queue)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Destroying Profile %s\n", queue->name);
switch_core_destroy_memory_pool(&queue->pool);
}
static void queue_rwunlock(cc_queue_t *queue)
{
switch_thread_rwlock_unlock(queue->rwlock);
if (switch_test_flag(queue, PFLAG_DESTROY)) {
if (switch_thread_rwlock_trywrlock(queue->rwlock) == SWITCH_STATUS_SUCCESS) {
switch_thread_rwlock_unlock(queue->rwlock);
free_queue(queue);
}
}
}
static void destroy_queue(const char *queue_name)
{
cc_queue_t *queue = NULL;
switch_mutex_lock(globals.mutex);
if ((queue = switch_core_hash_find(globals.queue_hash, queue_name))) {
switch_core_hash_delete(globals.queue_hash, queue_name);
}
switch_mutex_unlock(globals.mutex);
if (!queue) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[%s] Invalid queue\n", queue_name);
return;
}
if (switch_thread_rwlock_trywrlock(queue->rwlock) != SWITCH_STATUS_SUCCESS) {
/* Lock failed, set the destroy flag so it'll be destroyed whenever its not in use anymore */
switch_set_flag(queue, PFLAG_DESTROY);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "[%s] queue is in use, memory will be freed whenever its no longer in use\n",
queue->name);
return;
}
free_queue(queue);
}
switch_cache_db_handle_t *cc_get_db_handle(void)
{
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
dsn = globals.odbc_dsn;
} else {
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
/*!
* \brief Sets the queue's configuration instructions
*/
cc_queue_t *queue_set_config(cc_queue_t *queue)
{
int i = 0;
queue->config_str_pool.pool = queue->pool;
/*
SWITCH _CONFIG_SET_ITEM(item, "key", type, flags,
pointer, default, options, help_syntax, help_description)
*/
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "strategy", SWITCH_CONFIG_STRING, 0, &queue->strategy, "longest-idle-agent", &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "moh-sound", SWITCH_CONFIG_STRING, 0, &queue->moh, NULL, &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "announce-sound", SWITCH_CONFIG_STRING, 0, &queue->announce, NULL, &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "announce-frequency", SWITCH_CONFIG_INT, 0, &queue->announce_freq, 0, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "record-template", SWITCH_CONFIG_STRING, 0, &queue->record_template, NULL, &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "time-base-score", SWITCH_CONFIG_STRING, 0, &queue->time_base_score, "queue", &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "ring-progressively-delay", SWITCH_CONFIG_INT, 0, &queue->ring_progressively_delay, NULL, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "tier-rules-apply", SWITCH_CONFIG_BOOL, 0, &queue->tier_rules_apply, SWITCH_FALSE, NULL, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "tier-rule-wait-second", SWITCH_CONFIG_INT, 0, &queue->tier_rule_wait_second, 0, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "tier-rule-wait-multiply-level", SWITCH_CONFIG_BOOL, 0, &queue->tier_rule_wait_multiply_level, SWITCH_FALSE, NULL, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "tier-rule-no-agent-no-wait", SWITCH_CONFIG_BOOL, 0, &queue->tier_rule_no_agent_no_wait, SWITCH_TRUE, NULL, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "discard-abandoned-after", SWITCH_CONFIG_INT, 0, &queue->discard_abandoned_after, 60, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "abandoned-resume-allowed", SWITCH_CONFIG_BOOL, 0, &queue->abandoned_resume_allowed, SWITCH_FALSE, NULL, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "max-wait-time", SWITCH_CONFIG_INT, 0, &queue->max_wait_time, 0, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "max-wait-time-with-no-agent", SWITCH_CONFIG_INT, 0, &queue->max_wait_time_with_no_agent, 0, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "max-wait-time-with-no-agent-time-reached", SWITCH_CONFIG_INT, 0, &queue->max_wait_time_with_no_agent_time_reached, 5, &config_int_0_86400, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "agent-no-answer-status", SWITCH_CONFIG_STRING, 0, &queue->agent_no_answer_status, cc_agent_status2str(CC_AGENT_STATUS_ON_BREAK), &queue->config_str_pool, NULL, NULL);
SWITCH_CONFIG_SET_ITEM(queue->config[i++], "skip-agents-with-external-calls", SWITCH_CONFIG_BOOL, 0, &queue->skip_agents_with_external_calls, SWITCH_TRUE, NULL, NULL, NULL);
switch_assert(i < CC_QUEUE_CONFIGITEM_COUNT);
return queue;
}
static int cc_execute_sql_affected_rows(char *sql) {
switch_cache_db_handle_t *dbh = NULL;
int res = 0;
if (!(dbh = cc_get_db_handle())) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
return -1;
}
switch_cache_db_execute_sql(dbh, sql, NULL);
res = switch_cache_db_affected_rows(dbh);
switch_cache_db_release_db_handle(&dbh);
return res;
}
char *cc_execute_sql2str(cc_queue_t *queue, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
{
char *ret = NULL;
switch_cache_db_handle_t *dbh = NULL;
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_lock(mutex);
} else {
switch_mutex_lock(globals.mutex);
}
}
if (!(dbh = cc_get_db_handle())) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
ret = switch_cache_db_execute_sql2str(dbh, sql, resbuf, len, NULL);
end:
switch_cache_db_release_db_handle(&dbh);
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_unlock(mutex);
} else {
switch_mutex_unlock(globals.mutex);
}
}
return ret;
}
static switch_status_t cc_execute_sql(cc_queue_t *queue, char *sql, switch_mutex_t *mutex)
{
switch_cache_db_handle_t *dbh = NULL;
switch_status_t status = SWITCH_STATUS_FALSE;
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_lock(mutex);
} else {
switch_mutex_lock(globals.mutex);
}
}
if (!(dbh = cc_get_db_handle())) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
status = switch_cache_db_execute_sql(dbh, sql, NULL);
end:
switch_cache_db_release_db_handle(&dbh);
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_unlock(mutex);
} else {
switch_mutex_unlock(globals.mutex);
}
}
return status;
}
static switch_bool_t cc_execute_sql_callback(cc_queue_t *queue, switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
{
switch_bool_t ret = SWITCH_FALSE;
char *errmsg = NULL;
switch_cache_db_handle_t *dbh = NULL;
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_lock(mutex);
} else {
switch_mutex_lock(globals.mutex);
}
}
if (!(dbh = cc_get_db_handle())) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
if (errmsg) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
free(errmsg);
}
end:
switch_cache_db_release_db_handle(&dbh);
if (globals.global_database_lock) {
if (mutex) {
switch_mutex_unlock(mutex);
} else {
switch_mutex_unlock(globals.mutex);
}
}
return ret;
}
static cc_queue_t *load_queue(const char *queue_name, switch_bool_t request_agents, switch_bool_t request_tiers, switch_xml_t x_queues_cfg)
{
cc_queue_t *queue = NULL;
switch_xml_t x_queues, x_queue, cfg, x_agents, x_agent, x_tiers;
switch_xml_t xml = NULL;
switch_event_t *event = NULL;
switch_event_t *params = NULL;
switch_event_create(¶ms, SWITCH_EVENT_REQUEST_PARAMS);
switch_assert(params);
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "CC-Queue", queue_name);
if (x_queues_cfg) {
x_queues = x_queues_cfg;
} else {
if (!(xml = switch_xml_open_cfg(global_cf, &cfg, params))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf);
goto end;
}
if (!(x_queues = switch_xml_child(cfg, "queues"))) {
goto end;
}
}
if ((x_queue = switch_xml_find_child(x_queues, "queue", "name", queue_name))) {
switch_memory_pool_t *pool;
int count;
if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Pool Failure\n");
goto end;
}
if (!(queue = switch_core_alloc(pool, sizeof(cc_queue_t)))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Alloc Failure\n");
switch_core_destroy_memory_pool(&pool);
goto end;
}
queue->pool = pool;
queue_set_config(queue);
/* Add the params to the event structure */
count = (int)switch_event_import_xml(switch_xml_child(x_queue, "param"), "name", "value", &event);
if (switch_xml_config_parse_event(event, count, SWITCH_FALSE, queue->config) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to process configuration\n");
switch_core_destroy_memory_pool(&pool);
goto end;
}
switch_thread_rwlock_create(&queue->rwlock, pool);
queue->name = switch_core_strdup(pool, queue_name);
queue->last_agent_exist = 0;
queue->last_agent_exist_check = 0;
queue->calls_answered = 0;
queue->calls_abandoned = 0;
if (cc_agent_str2status(queue->agent_no_answer_status) == CC_AGENT_STATUS_UNKNOWN) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Queue %s has invalid agent-no-answer-status, setting to %s", queue->name, cc_agent_status2str(CC_AGENT_STATUS_ON_BREAK));
queue->agent_no_answer_status = switch_core_strdup(pool, cc_agent_status2str(CC_AGENT_STATUS_ON_BREAK));
}
switch_mutex_init(&queue->mutex, SWITCH_MUTEX_NESTED, queue->pool);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added queue %s\n", queue->name);
switch_core_hash_insert(globals.queue_hash, queue->name, queue);
}
/* Importing from XML config Agents */
if (queue && request_agents && (x_agents = switch_xml_child(cfg, "agents"))) {
for (x_agent = switch_xml_child(x_agents, "agent"); x_agent; x_agent = x_agent->next) {
const char *agent = switch_xml_attr(x_agent, "name");
if (agent) {
load_agent(agent, params, x_agents);
}
}
}
/* Importing from XML config Agent Tiers */
if (queue && request_tiers && (x_tiers = switch_xml_child(cfg, "tiers"))) {
load_tiers(SWITCH_TRUE, queue_name, NULL, params, x_tiers);
}
end:
if (xml) {
switch_xml_free(xml);
}
if (event) {
switch_event_destroy(&event);
}
if (params) {
switch_event_destroy(¶ms);
}
return queue;
}
static cc_queue_t *get_queue(const char *queue_name)
{
cc_queue_t *queue = NULL;
switch_mutex_lock(globals.mutex);
if (!(queue = switch_core_hash_find(globals.queue_hash, queue_name))) {
queue = load_queue(queue_name, SWITCH_FALSE, SWITCH_FALSE, NULL);
}
if (queue) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "[%s] rwlock\n", queue->name);
switch_thread_rwlock_rdlock(queue->rwlock);
}
switch_mutex_unlock(globals.mutex);
return queue;
}
struct call_helper {
const char *member_uuid;
const char *member_session_uuid;
const char *queue_name;
const char *queue_strategy;
const char *member_joined_epoch;
const char *member_cid_name;
const char *member_cid_number;
const char *agent_name;
const char *agent_system;
const char *agent_status;
const char *agent_type;
const char *agent_uuid;
const char *originate_string;
const char *record_template;
int no_answer_count;
int max_no_answer;
int reject_delay_time;
int busy_delay_time;
int no_answer_delay_time;
cc_agent_status_t agent_no_answer_status;
switch_memory_pool_t *pool;
};
int cc_queue_count(const char *queue)
{
char *sql;
int count = 0;
char res[256] = "0";
const char *event_name = "Single-Queue";
switch_event_t *event;
if (!switch_strlen_zero(queue)) {
if (queue[0] == '*') {
event_name = "All-Queues";
sql = switch_mprintf("SELECT count(*) FROM members WHERE state = '%q' OR state = '%q'",
cc_member_state2str(CC_MEMBER_STATE_WAITING), cc_member_state2str(CC_MEMBER_STATE_TRYING));
} else {
sql = switch_mprintf("SELECT count(*) FROM members WHERE queue = '%q' AND (state = '%q' OR state = '%q')",
queue, cc_member_state2str(CC_MEMBER_STATE_WAITING), cc_member_state2str(CC_MEMBER_STATE_TRYING));
}
cc_execute_sql2str(NULL, NULL, sql, res, sizeof(res));
switch_safe_free(sql);
count = atoi(res);
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, CALLCENTER_EVENT) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Queue", queue);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Action", "members-count");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Count", res);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Selection", event_name);
switch_event_fire(&event);
}
}
return count;
}
cc_status_t cc_agent_add(const char *agent, const char *type)
{
switch_event_t *event;
cc_status_t result = CC_STATUS_SUCCESS;
char *sql;
if (!strcasecmp(type, CC_AGENT_TYPE_CALLBACK) || !strcasecmp(type, CC_AGENT_TYPE_UUID_STANDBY)) {
char res[256] = "";
/* Check to see if agent already exist */
sql = switch_mprintf("SELECT count(*) FROM agents WHERE name = '%q'", agent);
cc_execute_sql2str(NULL, NULL, sql, res, sizeof(res));
switch_safe_free(sql);
if (atoi(res) != 0) {
result = CC_STATUS_AGENT_ALREADY_EXIST;
goto done;
}
/* Add Agent */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding Agent %s with type %s with default status %s\n",
agent, type, cc_agent_status2str(CC_AGENT_STATUS_LOGGED_OUT));
sql = switch_mprintf("INSERT INTO agents (name, instance_id, type, status, state) VALUES('%q', 'single_box', '%q', '%q', '%q');",
agent, type, cc_agent_status2str(CC_AGENT_STATUS_LOGGED_OUT), cc_agent_state2str(CC_AGENT_STATE_WAITING));
cc_execute_sql(NULL, sql, NULL);
switch_safe_free(sql);
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, CALLCENTER_EVENT) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Agent", agent);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Agent-Type", type);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Action", "agent-add");
switch_event_fire(&event);
}
} else {
result = CC_STATUS_AGENT_INVALID_TYPE;
goto done;
}
done:
return result;
}
cc_status_t cc_agent_del(const char *agent)
{
cc_status_t result = CC_STATUS_SUCCESS;
char *sql;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Deleted Agent %s\n", agent);
sql = switch_mprintf("DELETE FROM agents WHERE name = '%q';"
"DELETE FROM tiers WHERE agent = '%q';",
agent, agent);
cc_execute_sql(NULL, sql, NULL);
switch_safe_free(sql);
return result;
}
cc_status_t cc_agent_get(const char *key, const char *agent, char *ret_result, size_t ret_result_size)
{
cc_status_t result = CC_STATUS_SUCCESS;
char *sql;
switch_event_t *event;
char res[256];
/* Check to see if agent already exists */
sql = switch_mprintf("SELECT count(*) FROM agents WHERE name = '%q'", agent);
cc_execute_sql2str(NULL, NULL, sql, res, sizeof(res));
switch_safe_free(sql);
if (atoi(res) == 0) {
result = CC_STATUS_AGENT_NOT_FOUND;
goto done;
}
if (!strcasecmp(key, "status") || !strcasecmp(key, "state") || !strcasecmp(key, "uuid") ) {
/* Check to see if agent already exists */
sql = switch_mprintf("SELECT %q FROM agents WHERE name = '%q'", key, agent);
cc_execute_sql2str(NULL, NULL, sql, res, sizeof(res));
switch_safe_free(sql);
switch_snprintf(ret_result, ret_result_size, "%s", res);
result = CC_STATUS_SUCCESS;
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, CALLCENTER_EVENT) == SWITCH_STATUS_SUCCESS) {
char tmpname[256];
if (!strcasecmp(key, "uuid")) {
switch_snprintf(tmpname, sizeof(tmpname), "CC-Agent-UUID");
} else {
switch_snprintf(tmpname, sizeof(tmpname), "CC-Agent-%c%s", (char) switch_toupper(key[0]), key+1);
}
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "CC-Agent", agent);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "CC-Action", "agent-%s-get", key);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, tmpname, res);
switch_event_fire(&event);
}
} else {
result = CC_STATUS_INVALID_KEY;
goto done;
}
done:
if (result == CC_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Get Info Agent %s %s = %s\n", agent, key, res);
}
return result;
}
cc_status_t cc_agent_update(const char *key, const char *value, const char *agent)
{
cc_status_t result = CC_STATUS_SUCCESS;
char *sql;