00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include "asterisk.h"
00064
00065 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 277489 $")
00066
00067 #include <sys/time.h>
00068 #include <sys/signal.h>
00069 #include <netinet/in.h>
00070 #include <ctype.h>
00071
00072 #include "asterisk/lock.h"
00073 #include "asterisk/file.h"
00074 #include "asterisk/channel.h"
00075 #include "asterisk/pbx.h"
00076 #include "asterisk/app.h"
00077 #include "asterisk/linkedlists.h"
00078 #include "asterisk/module.h"
00079 #include "asterisk/translate.h"
00080 #include "asterisk/say.h"
00081 #include "asterisk/features.h"
00082 #include "asterisk/musiconhold.h"
00083 #include "asterisk/cli.h"
00084 #include "asterisk/manager.h"
00085 #include "asterisk/config.h"
00086 #include "asterisk/monitor.h"
00087 #include "asterisk/utils.h"
00088 #include "asterisk/causes.h"
00089 #include "asterisk/astdb.h"
00090 #include "asterisk/devicestate.h"
00091 #include "asterisk/stringfields.h"
00092 #include "asterisk/event.h"
00093 #include "asterisk/astobj2.h"
00094 #include "asterisk/strings.h"
00095 #include "asterisk/global_datastores.h"
00096 #include "asterisk/taskprocessor.h"
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 enum {
00494 QUEUE_STRATEGY_RINGALL = 0,
00495 QUEUE_STRATEGY_LEASTRECENT,
00496 QUEUE_STRATEGY_FEWESTCALLS,
00497 QUEUE_STRATEGY_RANDOM,
00498 QUEUE_STRATEGY_RRMEMORY,
00499 QUEUE_STRATEGY_LINEAR,
00500 QUEUE_STRATEGY_WRANDOM
00501 };
00502
00503 enum queue_reload_mask {
00504 QUEUE_RELOAD_PARAMETERS = (1 << 0),
00505 QUEUE_RELOAD_MEMBER = (1 << 1),
00506 QUEUE_RELOAD_RULES = (1 << 2),
00507 QUEUE_RESET_STATS = (1 << 3),
00508 };
00509
00510 static const struct strategy {
00511 int strategy;
00512 const char *name;
00513 } strategies[] = {
00514 { QUEUE_STRATEGY_RINGALL, "ringall" },
00515 { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
00516 { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
00517 { QUEUE_STRATEGY_RANDOM, "random" },
00518 { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
00519 { QUEUE_STRATEGY_RRMEMORY, "roundrobin" },
00520 { QUEUE_STRATEGY_LINEAR, "linear" },
00521 { QUEUE_STRATEGY_WRANDOM, "wrandom"},
00522 };
00523
00524 static struct ast_taskprocessor *devicestate_tps;
00525
00526 #define DEFAULT_RETRY 5
00527 #define DEFAULT_TIMEOUT 15
00528 #define RECHECK 1
00529 #define MAX_PERIODIC_ANNOUNCEMENTS 10
00530 #define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15
00531
00532 #define MAX_QUEUE_BUCKETS 53
00533
00534 #define RES_OKAY 0
00535 #define RES_EXISTS (-1)
00536 #define RES_OUTOFMEMORY (-2)
00537 #define RES_NOSUCHQUEUE (-3)
00538 #define RES_NOT_DYNAMIC (-4)
00539
00540 static char *app = "Queue";
00541
00542 static char *app_aqm = "AddQueueMember" ;
00543
00544 static char *app_rqm = "RemoveQueueMember" ;
00545
00546 static char *app_pqm = "PauseQueueMember" ;
00547
00548 static char *app_upqm = "UnpauseQueueMember" ;
00549
00550 static char *app_ql = "QueueLog" ;
00551
00552
00553 static const char *pm_family = "Queue/PersistentMembers";
00554
00555 #define PM_MAX_LEN 8192
00556
00557
00558 static int queue_persistent_members = 0;
00559
00560
00561 static int use_weight = 0;
00562
00563
00564 static int autofill_default = 0;
00565
00566
00567 static int montype_default = 0;
00568
00569
00570 static int shared_lastcall = 0;
00571
00572
00573 static struct ast_event_sub *device_state_sub;
00574
00575
00576 static int update_cdr = 0;
00577
00578 enum queue_result {
00579 QUEUE_UNKNOWN = 0,
00580 QUEUE_TIMEOUT = 1,
00581 QUEUE_JOINEMPTY = 2,
00582 QUEUE_LEAVEEMPTY = 3,
00583 QUEUE_JOINUNAVAIL = 4,
00584 QUEUE_LEAVEUNAVAIL = 5,
00585 QUEUE_FULL = 6,
00586 QUEUE_CONTINUE = 7,
00587 };
00588
00589 const struct {
00590 enum queue_result id;
00591 char *text;
00592 } queue_results[] = {
00593 { QUEUE_UNKNOWN, "UNKNOWN" },
00594 { QUEUE_TIMEOUT, "TIMEOUT" },
00595 { QUEUE_JOINEMPTY,"JOINEMPTY" },
00596 { QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
00597 { QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
00598 { QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
00599 { QUEUE_FULL, "FULL" },
00600 { QUEUE_CONTINUE, "CONTINUE" },
00601 };
00602
00603 enum queue_timeout_priority {
00604 TIMEOUT_PRIORITY_APP,
00605 TIMEOUT_PRIORITY_CONF,
00606 };
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620 struct callattempt {
00621 struct callattempt *q_next;
00622 struct callattempt *call_next;
00623 struct ast_channel *chan;
00624 char interface[256];
00625 int stillgoing;
00626 int metric;
00627 int oldstatus;
00628 time_t lastcall;
00629 struct call_queue *lastqueue;
00630 struct member *member;
00631 };
00632
00633
00634 struct queue_ent {
00635 struct call_queue *parent;
00636 char moh[80];
00637 char announce[80];
00638 char context[AST_MAX_CONTEXT];
00639 char digits[AST_MAX_EXTENSION];
00640 int valid_digits;
00641 int pos;
00642 int prio;
00643 int last_pos_said;
00644 time_t last_periodic_announce_time;
00645 int last_periodic_announce_sound;
00646 time_t last_pos;
00647 int opos;
00648 int handled;
00649 int pending;
00650 int max_penalty;
00651 int min_penalty;
00652 int linpos;
00653 int linwrapped;
00654 time_t start;
00655 time_t expire;
00656 int cancel_answered_elsewhere;
00657 struct ast_channel *chan;
00658 AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules;
00659 struct penalty_rule *pr;
00660 struct queue_ent *next;
00661 };
00662
00663 struct member {
00664 char interface[80];
00665 char state_interface[80];
00666 char membername[80];
00667 int penalty;
00668 int calls;
00669 int dynamic;
00670 int realtime;
00671 int status;
00672 int paused;
00673 time_t lastcall;
00674 struct call_queue *lastqueue;
00675 unsigned int dead:1;
00676 unsigned int delme:1;
00677 char rt_uniqueid[80];
00678 };
00679
00680 enum empty_conditions {
00681 QUEUE_EMPTY_PENALTY = (1 << 0),
00682 QUEUE_EMPTY_PAUSED = (1 << 1),
00683 QUEUE_EMPTY_INUSE = (1 << 2),
00684 QUEUE_EMPTY_RINGING = (1 << 3),
00685 QUEUE_EMPTY_UNAVAILABLE = (1 << 4),
00686 QUEUE_EMPTY_INVALID = (1 << 5),
00687 QUEUE_EMPTY_UNKNOWN = (1 << 6),
00688 QUEUE_EMPTY_WRAPUP = (1 << 7),
00689 };
00690
00691
00692 #define ANNOUNCEHOLDTIME_ALWAYS 1
00693 #define ANNOUNCEHOLDTIME_ONCE 2
00694 #define QUEUE_EVENT_VARIABLES 3
00695
00696 struct penalty_rule {
00697 int time;
00698 int max_value;
00699 int min_value;
00700 int max_relative;
00701 int min_relative;
00702 AST_LIST_ENTRY(penalty_rule) list;
00703 };
00704
00705 #define ANNOUNCEPOSITION_YES 1
00706 #define ANNOUNCEPOSITION_NO 2
00707 #define ANNOUNCEPOSITION_MORE_THAN 3
00708 #define ANNOUNCEPOSITION_LIMIT 4
00709
00710 struct call_queue {
00711
00712 char name[80];
00713
00714 char moh[80];
00715
00716 char announce[80];
00717
00718 char context[AST_MAX_CONTEXT];
00719
00720 char membermacro[AST_MAX_CONTEXT];
00721
00722 char membergosub[AST_MAX_CONTEXT];
00723
00724 char defaultrule[AST_MAX_CONTEXT];
00725
00726 char sound_next[80];
00727
00728 char sound_thereare[80];
00729
00730 char sound_calls[80];
00731
00732 char sound_holdtime[80];
00733
00734 char sound_minutes[80];
00735
00736 char sound_minute[80];
00737
00738 char sound_seconds[80];
00739
00740 char sound_thanks[80];
00741
00742 char sound_callerannounce[80];
00743
00744 char sound_reporthold[80];
00745
00746 char queue_quantity1[80];
00747
00748 char queue_quantity2[80];
00749
00750 struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
00751 unsigned int dead:1;
00752 unsigned int eventwhencalled:2;
00753 unsigned int ringinuse:1;
00754 unsigned int setinterfacevar:1;
00755 unsigned int setqueuevar:1;
00756 unsigned int setqueueentryvar:1;
00757 unsigned int reportholdtime:1;
00758 unsigned int wrapped:1;
00759 unsigned int timeoutrestart:1;
00760 unsigned int announceholdtime:2;
00761 unsigned int announceposition:3;
00762 int strategy:4;
00763 unsigned int maskmemberstatus:1;
00764 unsigned int realtime:1;
00765 unsigned int found:1;
00766 enum empty_conditions joinempty;
00767 enum empty_conditions leavewhenempty;
00768 int announcepositionlimit;
00769 int announcefrequency;
00770 int minannouncefrequency;
00771 int periodicannouncefrequency;
00772 int numperiodicannounce;
00773 int randomperiodicannounce;
00774 int roundingseconds;
00775 int holdtime;
00776 int talktime;
00777 int callscompleted;
00778 int callsabandoned;
00779 int servicelevel;
00780 int callscompletedinsl;
00781 char monfmt[8];
00782 int montype;
00783 int count;
00784 int maxlen;
00785 int wrapuptime;
00786
00787 int retry;
00788 int timeout;
00789 int weight;
00790 int autopause;
00791 int timeoutpriority;
00792
00793
00794 int rrpos;
00795 int memberdelay;
00796 int autofill;
00797
00798 struct ao2_container *members;
00799
00800
00801
00802
00803
00804 int membercount;
00805 struct queue_ent *head;
00806 AST_LIST_ENTRY(call_queue) list;
00807 AST_LIST_HEAD_NOLOCK(, penalty_rule) rules;
00808 };
00809
00810 struct rule_list {
00811 char name[80];
00812 AST_LIST_HEAD_NOLOCK(,penalty_rule) rules;
00813 AST_LIST_ENTRY(rule_list) list;
00814 };
00815
00816 AST_LIST_HEAD_STATIC(rule_lists, rule_list);
00817
00818 static struct ao2_container *queues;
00819
00820 static void update_realtime_members(struct call_queue *q);
00821 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
00822
00823 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00824
00825 static void set_queue_result(struct ast_channel *chan, enum queue_result res)
00826 {
00827 int i;
00828
00829 for (i = 0; i < ARRAY_LEN(queue_results); i++) {
00830 if (queue_results[i].id == res) {
00831 pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
00832 return;
00833 }
00834 }
00835 }
00836
00837 static const char *int2strat(int strategy)
00838 {
00839 int x;
00840
00841 for (x = 0; x < ARRAY_LEN(strategies); x++) {
00842 if (strategy == strategies[x].strategy)
00843 return strategies[x].name;
00844 }
00845
00846 return "<unknown>";
00847 }
00848
00849 static int strat2int(const char *strategy)
00850 {
00851 int x;
00852
00853 for (x = 0; x < ARRAY_LEN(strategies); x++) {
00854 if (!strcasecmp(strategy, strategies[x].name))
00855 return strategies[x].strategy;
00856 }
00857
00858 return -1;
00859 }
00860
00861 static int queue_hash_cb(const void *obj, const int flags)
00862 {
00863 const struct call_queue *q = obj;
00864
00865 return ast_str_case_hash(q->name);
00866 }
00867
00868 static int queue_cmp_cb(void *obj, void *arg, int flags)
00869 {
00870 struct call_queue *q = obj, *q2 = arg;
00871 return !strcasecmp(q->name, q2->name) ? CMP_MATCH | CMP_STOP : 0;
00872 }
00873
00874 #ifdef REF_DEBUG_ONLY_QUEUES
00875 #define queue_ref(a) __ao2_ref_debug(a,1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
00876 #define queue_unref(a) __ao2_ref_debug(a,-1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
00877 #define queue_t_ref(a,b) __ao2_ref_debug(a,1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00878 #define queue_t_unref(a,b) __ao2_ref_debug(a,-1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00879 #define queues_t_link(c,q,tag) __ao2_link_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00880 #define queues_t_unlink(c,q,tag) __ao2_unlink_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00881 #else
00882 #define queue_t_ref(a,b) queue_ref(a)
00883 #define queue_t_unref(a,b) queue_unref(a)
00884 #define queues_t_link(c,q,tag) ao2_t_link(c,q,tag)
00885 #define queues_t_unlink(c,q,tag) ao2_t_unlink(c,q,tag)
00886 static inline struct call_queue *queue_ref(struct call_queue *q)
00887 {
00888 ao2_ref(q, 1);
00889 return q;
00890 }
00891
00892 static inline struct call_queue *queue_unref(struct call_queue *q)
00893 {
00894 ao2_ref(q, -1);
00895 return q;
00896 }
00897 #endif
00898
00899
00900 static void set_queue_variables(struct call_queue *q, struct ast_channel *chan)
00901 {
00902 char interfacevar[256]="";
00903 float sl = 0;
00904
00905 if (q->setqueuevar) {
00906 sl = 0;
00907 if (q->callscompleted > 0)
00908 sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
00909
00910 snprintf(interfacevar, sizeof(interfacevar),
00911 "QUEUENAME=%s,QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
00912 q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned, q->servicelevel, sl);
00913
00914 pbx_builtin_setvar_multiple(chan, interfacevar);
00915 }
00916 }
00917
00918
00919 static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
00920 {
00921 struct queue_ent *cur;
00922
00923 if (!q || !new)
00924 return;
00925 if (prev) {
00926 cur = prev->next;
00927 prev->next = new;
00928 } else {
00929 cur = q->head;
00930 q->head = new;
00931 }
00932 new->next = cur;
00933
00934
00935
00936
00937 queue_ref(q);
00938 new->parent = q;
00939 new->pos = ++(*pos);
00940 new->opos = *pos;
00941 }
00942
00943
00944
00945
00946
00947
00948
00949 static int get_member_status(struct call_queue *q, int max_penalty, int min_penalty, enum empty_conditions conditions)
00950 {
00951 struct member *member;
00952 struct ao2_iterator mem_iter;
00953
00954 ao2_lock(q);
00955 mem_iter = ao2_iterator_init(q->members, 0);
00956 for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
00957 if ((max_penalty && (member->penalty > max_penalty)) || (min_penalty && (member->penalty < min_penalty))) {
00958 if (conditions & QUEUE_EMPTY_PENALTY) {
00959 ast_debug(4, "%s is unavailable because his penalty is not between %d and %d\n", member->membername, min_penalty, max_penalty);
00960 continue;
00961 }
00962 }
00963
00964 switch (member->status) {
00965 case AST_DEVICE_INVALID:
00966 if (conditions & QUEUE_EMPTY_INVALID) {
00967 ast_debug(4, "%s is unavailable because his device state is 'invalid'\n", member->membername);
00968 break;
00969 }
00970 goto default_case;
00971 case AST_DEVICE_UNAVAILABLE:
00972 if (conditions & QUEUE_EMPTY_UNAVAILABLE) {
00973 ast_debug(4, "%s is unavailable because his device state is 'unavailable'\n", member->membername);
00974 break;
00975 }
00976 goto default_case;
00977 case AST_DEVICE_INUSE:
00978 if (conditions & QUEUE_EMPTY_INUSE) {
00979 ast_debug(4, "%s is unavailable because his device state is 'inuse'\n", member->membername);
00980 break;
00981 }
00982 goto default_case;
00983 case AST_DEVICE_RINGING:
00984 if (conditions & QUEUE_EMPTY_RINGING) {
00985 ast_debug(4, "%s is unavailable because his device state is 'ringing'\n", member->membername);
00986 break;
00987 }
00988 case AST_DEVICE_UNKNOWN:
00989 if (conditions & QUEUE_EMPTY_UNKNOWN) {
00990 ast_debug(4, "%s is unavailable because his device state is 'unknown'\n", member->membername);
00991 break;
00992 }
00993
00994 default:
00995 default_case:
00996 if (member->paused && (conditions & QUEUE_EMPTY_PAUSED)) {
00997 ast_debug(4, "%s is unavailable because he is paused'\n", member->membername);
00998 break;
00999 } else if ((conditions & QUEUE_EMPTY_WRAPUP) && member->lastcall && q->wrapuptime && (time(NULL) - q->wrapuptime < member->lastcall)) {
01000 ast_debug(4, "%s is unavailable because it has only been %d seconds since his last call (wrapup time is %d)\n", member->membername, (int) (time(NULL) - member->lastcall), q->wrapuptime);
01001 break;
01002 } else {
01003 ao2_unlock(q);
01004 ao2_ref(member, -1);
01005 ao2_iterator_destroy(&mem_iter);
01006 ast_debug(4, "%s is available.\n", member->membername);
01007 return 0;
01008 }
01009 break;
01010 }
01011 }
01012 ao2_iterator_destroy(&mem_iter);
01013
01014 ao2_unlock(q);
01015 return -1;
01016 }
01017
01018 struct statechange {
01019 AST_LIST_ENTRY(statechange) entry;
01020 int state;
01021 char dev[0];
01022 };
01023
01024
01025
01026
01027
01028
01029 static int update_status(struct call_queue *q, struct member *m, const int status)
01030 {
01031 m->status = status;
01032
01033 if (q->maskmemberstatus)
01034 return 0;
01035
01036 manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
01037 "Queue: %s\r\n"
01038 "Location: %s\r\n"
01039 "MemberName: %s\r\n"
01040 "Membership: %s\r\n"
01041 "Penalty: %d\r\n"
01042 "CallsTaken: %d\r\n"
01043 "LastCall: %d\r\n"
01044 "Status: %d\r\n"
01045 "Paused: %d\r\n",
01046 q->name, m->interface, m->membername, m->dynamic ? "dynamic" : m->realtime ? "realtime" : "static",
01047 m->penalty, m->calls, (int)m->lastcall, m->status, m->paused
01048 );
01049
01050 return 0;
01051 }
01052
01053
01054 static int handle_statechange(void *datap)
01055 {
01056 struct statechange *sc = datap;
01057 struct ao2_iterator miter, qiter;
01058 struct member *m;
01059 struct call_queue *q;
01060 char interface[80], *slash_pos;
01061 int found = 0;
01062
01063 qiter = ao2_iterator_init(queues, 0);
01064 while ((q = ao2_t_iterator_next(&qiter, "Iterate over queues"))) {
01065 ao2_lock(q);
01066
01067 miter = ao2_iterator_init(q->members, 0);
01068 for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
01069 ast_copy_string(interface, m->state_interface, sizeof(interface));
01070
01071 if ((slash_pos = strchr(interface, '/')))
01072 if (!strncasecmp(interface, "Local/", 6) && (slash_pos = strchr(slash_pos + 1, '/')))
01073 *slash_pos = '\0';
01074
01075 if (!strcasecmp(interface, sc->dev)) {
01076 found = 1;
01077 update_status(q, m, sc->state);
01078 ao2_ref(m, -1);
01079 break;
01080 }
01081 }
01082 ao2_iterator_destroy(&miter);
01083
01084 ao2_unlock(q);
01085 queue_t_unref(q, "Done with iterator");
01086 }
01087 ao2_iterator_destroy(&qiter);
01088
01089 if (found)
01090 ast_debug(1, "Device '%s' changed to state '%d' (%s)\n", sc->dev, sc->state, ast_devstate2str(sc->state));
01091 else
01092 ast_debug(3, "Device '%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", sc->dev, sc->state, ast_devstate2str(sc->state));
01093
01094 ast_free(sc);
01095 return 0;
01096 }
01097
01098 static void device_state_cb(const struct ast_event *event, void *unused)
01099 {
01100 enum ast_device_state state;
01101 const char *device;
01102 struct statechange *sc;
01103 size_t datapsize;
01104
01105 state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
01106 device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
01107
01108 if (ast_strlen_zero(device)) {
01109 ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
01110 return;
01111 }
01112 datapsize = sizeof(*sc) + strlen(device) + 1;
01113 if (!(sc = ast_calloc(1, datapsize))) {
01114 ast_log(LOG_ERROR, "failed to calloc a state change struct\n");
01115 return;
01116 }
01117 sc->state = state;
01118 strcpy(sc->dev, device);
01119 if (ast_taskprocessor_push(devicestate_tps, handle_statechange, sc) < 0) {
01120 ast_free(sc);
01121 }
01122 }
01123
01124
01125 static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused, const char *state_interface)
01126 {
01127 struct member *cur;
01128
01129 if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
01130 cur->penalty = penalty;
01131 cur->paused = paused;
01132 ast_copy_string(cur->interface, interface, sizeof(cur->interface));
01133 if (!ast_strlen_zero(state_interface))
01134 ast_copy_string(cur->state_interface, state_interface, sizeof(cur->state_interface));
01135 else
01136 ast_copy_string(cur->state_interface, interface, sizeof(cur->state_interface));
01137 if (!ast_strlen_zero(membername))
01138 ast_copy_string(cur->membername, membername, sizeof(cur->membername));
01139 else
01140 ast_copy_string(cur->membername, interface, sizeof(cur->membername));
01141 if (!strchr(cur->interface, '/'))
01142 ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
01143 cur->status = ast_device_state(cur->state_interface);
01144 }
01145
01146 return cur;
01147 }
01148
01149
01150 static int compress_char(const char c)
01151 {
01152 if (c < 32)
01153 return 0;
01154 else if (c > 96)
01155 return c - 64;
01156 else
01157 return c - 32;
01158 }
01159
01160 static int member_hash_fn(const void *obj, const int flags)
01161 {
01162 const struct member *mem = obj;
01163 const char *chname = strchr(mem->interface, '/');
01164 int ret = 0, i;
01165 if (!chname)
01166 chname = mem->interface;
01167 for (i = 0; i < 5 && chname[i]; i++)
01168 ret += compress_char(chname[i]) << (i * 6);
01169 return ret;
01170 }
01171
01172 static int member_cmp_fn(void *obj1, void *obj2, int flags)
01173 {
01174 struct member *mem1 = obj1, *mem2 = obj2;
01175 return strcasecmp(mem1->interface, mem2->interface) ? 0 : CMP_MATCH | CMP_STOP;
01176 }
01177
01178
01179
01180
01181
01182 static void init_queue(struct call_queue *q)
01183 {
01184 int i;
01185 struct penalty_rule *pr_iter;
01186
01187 q->dead = 0;
01188 q->retry = DEFAULT_RETRY;
01189 q->timeout = DEFAULT_TIMEOUT;
01190 q->maxlen = 0;
01191 q->announcefrequency = 0;
01192 q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
01193 q->announceholdtime = 1;
01194 q->announcepositionlimit = 10;
01195 q->announceposition = ANNOUNCEPOSITION_YES;
01196 q->roundingseconds = 0;
01197 q->servicelevel = 0;
01198 q->ringinuse = 1;
01199 q->setinterfacevar = 0;
01200 q->setqueuevar = 0;
01201 q->setqueueentryvar = 0;
01202 q->autofill = autofill_default;
01203 q->montype = montype_default;
01204 q->monfmt[0] = '\0';
01205 q->reportholdtime = 0;
01206 q->wrapuptime = 0;
01207 q->joinempty = 0;
01208 q->leavewhenempty = 0;
01209 q->memberdelay = 0;
01210 q->maskmemberstatus = 0;
01211 q->eventwhencalled = 0;
01212 q->weight = 0;
01213 q->timeoutrestart = 0;
01214 q->periodicannouncefrequency = 0;
01215 q->randomperiodicannounce = 0;
01216 q->numperiodicannounce = 0;
01217 q->timeoutpriority = TIMEOUT_PRIORITY_APP;
01218 if (!q->members) {
01219 if (q->strategy == QUEUE_STRATEGY_LINEAR)
01220
01221 q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
01222 else
01223 q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
01224 }
01225 q->found = 1;
01226
01227 ast_copy_string(q->sound_next, "queue-youarenext", sizeof(q->sound_next));
01228 ast_copy_string(q->sound_thereare, "queue-thereare", sizeof(q->sound_thereare));
01229 ast_copy_string(q->sound_calls, "queue-callswaiting", sizeof(q->sound_calls));
01230 ast_copy_string(q->sound_holdtime, "queue-holdtime", sizeof(q->sound_holdtime));
01231 ast_copy_string(q->sound_minutes, "queue-minutes", sizeof(q->sound_minutes));
01232 ast_copy_string(q->sound_minute, "queue-minute", sizeof(q->sound_minute));
01233 ast_copy_string(q->sound_seconds, "queue-seconds", sizeof(q->sound_seconds));
01234 ast_copy_string(q->sound_thanks, "queue-thankyou", sizeof(q->sound_thanks));
01235 ast_copy_string(q->sound_reporthold, "queue-reporthold", sizeof(q->sound_reporthold));
01236 ast_copy_string(q->queue_quantity1, "queue-quantity1", sizeof(q->queue_quantity1));
01237 ast_copy_string(q->queue_quantity2, "queue-quantity2", sizeof(q->queue_quantity2));
01238
01239 if (!q->sound_periodicannounce[0]) {
01240 q->sound_periodicannounce[0] = ast_str_create(32);
01241 }
01242
01243 if (q->sound_periodicannounce[0]) {
01244 ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
01245 }
01246
01247 for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
01248 if (q->sound_periodicannounce[i])
01249 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
01250 }
01251
01252 while ((pr_iter = AST_LIST_REMOVE_HEAD(&q->rules,list)))
01253 ast_free(pr_iter);
01254 }
01255
01256 static void clear_queue(struct call_queue *q)
01257 {
01258 q->holdtime = 0;
01259 q->callscompleted = 0;
01260 q->callsabandoned = 0;
01261 q->callscompletedinsl = 0;
01262 q->talktime = 0;
01263
01264 if (q->members) {
01265 struct member *mem;
01266 struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
01267 while ((mem = ao2_iterator_next(&mem_iter))) {
01268 mem->calls = 0;
01269 mem->lastcall = 0;
01270 ao2_ref(mem, -1);
01271 }
01272 ao2_iterator_destroy(&mem_iter);
01273 }
01274 }
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285 static int insert_penaltychange (const char *list_name, const char *content, const int linenum)
01286 {
01287 char *timestr, *maxstr, *minstr, *contentdup;
01288 struct penalty_rule *rule = NULL, *rule_iter;
01289 struct rule_list *rl_iter;
01290 int penaltychangetime, inserted = 0;
01291
01292 if (!(rule = ast_calloc(1, sizeof(*rule)))) {
01293 return -1;
01294 }
01295
01296 contentdup = ast_strdupa(content);
01297
01298 if (!(maxstr = strchr(contentdup, ','))) {
01299 ast_log(LOG_WARNING, "Improperly formatted penaltychange rule at line %d. Ignoring.\n", linenum);
01300 ast_free(rule);
01301 return -1;
01302 }
01303
01304 *maxstr++ = '\0';
01305 timestr = contentdup;
01306
01307 if ((penaltychangetime = atoi(timestr)) < 0) {
01308 ast_log(LOG_WARNING, "Improper time parameter specified for penaltychange rule at line %d. Ignoring.\n", linenum);
01309 ast_free(rule);
01310 return -1;
01311 }
01312
01313 rule->time = penaltychangetime;
01314
01315 if ((minstr = strchr(maxstr,',')))
01316 *minstr++ = '\0';
01317
01318
01319
01320 if (*maxstr == '+' || *maxstr == '-' || *maxstr == '\0') {
01321 rule->max_relative = 1;
01322 }
01323
01324 rule->max_value = atoi(maxstr);
01325
01326 if (!ast_strlen_zero(minstr)) {
01327 if (*minstr == '+' || *minstr == '-')
01328 rule->min_relative = 1;
01329 rule->min_value = atoi(minstr);
01330 } else
01331 rule->min_relative = 1;
01332
01333
01334 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list){
01335 if (strcasecmp(rl_iter->name, list_name))
01336 continue;
01337
01338 AST_LIST_TRAVERSE_SAFE_BEGIN(&rl_iter->rules, rule_iter, list) {
01339 if (rule->time < rule_iter->time) {
01340 AST_LIST_INSERT_BEFORE_CURRENT(rule, list);
01341 inserted = 1;
01342 break;
01343 }
01344 }
01345 AST_LIST_TRAVERSE_SAFE_END;
01346
01347 if (!inserted) {
01348 AST_LIST_INSERT_TAIL(&rl_iter->rules, rule, list);
01349 }
01350 }
01351
01352 return 0;
01353 }
01354
01355 static void parse_empty_options(const char *value, enum empty_conditions *empty, int joinempty)
01356 {
01357 char *value_copy = ast_strdupa(value);
01358 char *option = NULL;
01359 while ((option = strsep(&value_copy, ","))) {
01360 if (!strcasecmp(option, "paused")) {
01361 *empty |= QUEUE_EMPTY_PAUSED;
01362 } else if (!strcasecmp(option, "penalty")) {
01363 *empty |= QUEUE_EMPTY_PENALTY;
01364 } else if (!strcasecmp(option, "inuse")) {
01365 *empty |= QUEUE_EMPTY_INUSE;
01366 } else if (!strcasecmp(option, "ringing")) {
01367 *empty |= QUEUE_EMPTY_RINGING;
01368 } else if (!strcasecmp(option, "invalid")) {
01369 *empty |= QUEUE_EMPTY_INVALID;
01370 } else if (!strcasecmp(option, "wrapup")) {
01371 *empty |= QUEUE_EMPTY_WRAPUP;
01372 } else if (!strcasecmp(option, "unavailable")) {
01373 *empty |= QUEUE_EMPTY_UNAVAILABLE;
01374 } else if (!strcasecmp(option, "unknown")) {
01375 *empty |= QUEUE_EMPTY_UNKNOWN;
01376 } else if (!strcasecmp(option, "loose")) {
01377 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID);
01378 } else if (!strcasecmp(option, "strict")) {
01379 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED | QUEUE_EMPTY_UNAVAILABLE);
01380 } else if ((ast_false(option) && joinempty) || (ast_true(option) && !joinempty)) {
01381 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED);
01382 } else if ((ast_false(option) && !joinempty) || (ast_true(option) && joinempty)) {
01383 *empty = 0;
01384 } else {
01385 ast_log(LOG_WARNING, "Unknown option %s for '%s'\n", option, joinempty ? "joinempty" : "leavewhenempty");
01386 }
01387 }
01388 }
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398 static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
01399 {
01400 if (!strcasecmp(param, "musicclass") ||
01401 !strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
01402 ast_copy_string(q->moh, val, sizeof(q->moh));
01403 } else if (!strcasecmp(param, "announce")) {
01404 ast_copy_string(q->announce, val, sizeof(q->announce));
01405 } else if (!strcasecmp(param, "context")) {
01406 ast_copy_string(q->context, val, sizeof(q->context));
01407 } else if (!strcasecmp(param, "timeout")) {
01408 q->timeout = atoi(val);
01409 if (q->timeout < 0)
01410 q->timeout = DEFAULT_TIMEOUT;
01411 } else if (!strcasecmp(param, "ringinuse")) {
01412 q->ringinuse = ast_true(val);
01413 } else if (!strcasecmp(param, "setinterfacevar")) {
01414 q->setinterfacevar = ast_true(val);
01415 } else if (!strcasecmp(param, "setqueuevar")) {
01416 q->setqueuevar = ast_true(val);
01417 } else if (!strcasecmp(param, "setqueueentryvar")) {
01418 q->setqueueentryvar = ast_true(val);
01419 } else if (!strcasecmp(param, "monitor-format")) {
01420 ast_copy_string(q->monfmt, val, sizeof(q->monfmt));
01421 } else if (!strcasecmp(param, "membermacro")) {
01422 ast_copy_string(q->membermacro, val, sizeof(q->membermacro));
01423 } else if (!strcasecmp(param, "membergosub")) {
01424 ast_copy_string(q->membergosub, val, sizeof(q->membergosub));
01425 } else if (!strcasecmp(param, "queue-youarenext")) {
01426 ast_copy_string(q->sound_next, val, sizeof(q->sound_next));
01427 } else if (!strcasecmp(param, "queue-thereare")) {
01428 ast_copy_string(q->sound_thereare, val, sizeof(q->sound_thereare));
01429 } else if (!strcasecmp(param, "queue-callswaiting")) {
01430 ast_copy_string(q->sound_calls, val, sizeof(q->sound_calls));
01431 } else if (!strcasecmp(param, "queue-quantity1")) {
01432 ast_copy_string(q->queue_quantity1, val, sizeof(q->queue_quantity1));
01433 } else if (!strcasecmp(param, "queue-quantity2")) {
01434 ast_copy_string(q->queue_quantity2, val, sizeof(q->queue_quantity2));
01435 } else if (!strcasecmp(param, "queue-holdtime")) {
01436 ast_copy_string(q->sound_holdtime, val, sizeof(q->sound_holdtime));
01437 } else if (!strcasecmp(param, "queue-minutes")) {
01438 ast_copy_string(q->sound_minutes, val, sizeof(q->sound_minutes));
01439 } else if (!strcasecmp(param, "queue-minute")) {
01440 ast_copy_string(q->sound_minute, val, sizeof(q->sound_minute));
01441 } else if (!strcasecmp(param, "queue-seconds")) {
01442 ast_copy_string(q->sound_seconds, val, sizeof(q->sound_seconds));
01443 } else if (!strcasecmp(param, "queue-thankyou")) {
01444 ast_copy_string(q->sound_thanks, val, sizeof(q->sound_thanks));
01445 } else if (!strcasecmp(param, "queue-callerannounce")) {
01446 ast_copy_string(q->sound_callerannounce, val, sizeof(q->sound_callerannounce));
01447 } else if (!strcasecmp(param, "queue-reporthold")) {
01448 ast_copy_string(q->sound_reporthold, val, sizeof(q->sound_reporthold));
01449 } else if (!strcasecmp(param, "announce-frequency")) {
01450 q->announcefrequency = atoi(val);
01451 } else if (!strcasecmp(param, "min-announce-frequency")) {
01452 q->minannouncefrequency = atoi(val);
01453 ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
01454 } else if (!strcasecmp(param, "announce-round-seconds")) {
01455 q->roundingseconds = atoi(val);
01456
01457 if (!(q->roundingseconds == 0 || q->roundingseconds == 5 || q->roundingseconds == 10
01458 || q->roundingseconds == 15 || q->roundingseconds == 20 || q->roundingseconds == 30)) {
01459 if (linenum >= 0) {
01460 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
01461 "using 0 instead for queue '%s' at line %d of queues.conf\n",
01462 val, param, q->name, linenum);
01463 } else {
01464 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
01465 "using 0 instead for queue '%s'\n", val, param, q->name);
01466 }
01467 q->roundingseconds=0;
01468 }
01469 } else if (!strcasecmp(param, "announce-holdtime")) {
01470 if (!strcasecmp(val, "once"))
01471 q->announceholdtime = ANNOUNCEHOLDTIME_ONCE;
01472 else if (ast_true(val))
01473 q->announceholdtime = ANNOUNCEHOLDTIME_ALWAYS;
01474 else
01475 q->announceholdtime = 0;
01476 } else if (!strcasecmp(param, "announce-position")) {
01477 if (!strcasecmp(val, "limit"))
01478 q->announceposition = ANNOUNCEPOSITION_LIMIT;
01479 else if (!strcasecmp(val, "more"))
01480 q->announceposition = ANNOUNCEPOSITION_MORE_THAN;
01481 else if (ast_true(val))
01482 q->announceposition = ANNOUNCEPOSITION_YES;
01483 else
01484 q->announceposition = ANNOUNCEPOSITION_NO;
01485 } else if (!strcasecmp(param, "announce-position-limit")) {
01486 q->announcepositionlimit = atoi(val);
01487 } else if (!strcasecmp(param, "periodic-announce")) {
01488 if (strchr(val, ',')) {
01489 char *s, *buf = ast_strdupa(val);
01490 unsigned int i = 0;
01491
01492 while ((s = strsep(&buf, ",|"))) {
01493 if (!q->sound_periodicannounce[i])
01494 q->sound_periodicannounce[i] = ast_str_create(16);
01495 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", s);
01496 i++;
01497 if (i == MAX_PERIODIC_ANNOUNCEMENTS)
01498 break;
01499 }
01500 q->numperiodicannounce = i;
01501 } else {
01502 ast_str_set(&q->sound_periodicannounce[0], 0, "%s", val);
01503 q->numperiodicannounce = 1;
01504 }
01505 } else if (!strcasecmp(param, "periodic-announce-frequency")) {
01506 q->periodicannouncefrequency = atoi(val);
01507 } else if (!strcasecmp(param, "random-periodic-announce")) {
01508 q->randomperiodicannounce = ast_true(val);
01509 } else if (!strcasecmp(param, "retry")) {
01510 q->retry = atoi(val);
01511 if (q->retry <= 0)
01512 q->retry = DEFAULT_RETRY;
01513 } else if (!strcasecmp(param, "wrapuptime")) {
01514 q->wrapuptime = atoi(val);
01515 } else if (!strcasecmp(param, "autofill")) {
01516 q->autofill = ast_true(val);
01517 } else if (!strcasecmp(param, "monitor-type")) {
01518 if (!strcasecmp(val, "mixmonitor"))
01519 q->montype = 1;
01520 } else if (!strcasecmp(param, "autopause")) {
01521 q->autopause = ast_true(val);
01522 } else if (!strcasecmp(param, "maxlen")) {
01523 q->maxlen = atoi(val);
01524 if (q->maxlen < 0)
01525 q->maxlen = 0;
01526 } else if (!strcasecmp(param, "servicelevel")) {
01527 q->servicelevel= atoi(val);
01528 } else if (!strcasecmp(param, "strategy")) {
01529 int strategy;
01530
01531
01532 if (failunknown) {
01533 return;
01534 }
01535 strategy = strat2int(val);
01536 if (strategy < 0) {
01537 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
01538 val, q->name);
01539 q->strategy = QUEUE_STRATEGY_RINGALL;
01540 }
01541 if (strategy == q->strategy) {
01542 return;
01543 }
01544 if (strategy == QUEUE_STRATEGY_LINEAR) {
01545 ast_log(LOG_WARNING, "Changing to the linear strategy currently requires asterisk to be restarted.\n");
01546 return;
01547 }
01548 q->strategy = strategy;
01549 } else if (!strcasecmp(param, "joinempty")) {
01550 parse_empty_options(val, &q->joinempty, 1);
01551 } else if (!strcasecmp(param, "leavewhenempty")) {
01552 parse_empty_options(val, &q->leavewhenempty, 0);
01553 } else if (!strcasecmp(param, "eventmemberstatus")) {
01554 q->maskmemberstatus = !ast_true(val);
01555 } else if (!strcasecmp(param, "eventwhencalled")) {
01556 if (!strcasecmp(val, "vars")) {
01557 q->eventwhencalled = QUEUE_EVENT_VARIABLES;
01558 } else {
01559 q->eventwhencalled = ast_true(val) ? 1 : 0;
01560 }
01561 } else if (!strcasecmp(param, "reportholdtime")) {
01562 q->reportholdtime = ast_true(val);
01563 } else if (!strcasecmp(param, "memberdelay")) {
01564 q->memberdelay = atoi(val);
01565 } else if (!strcasecmp(param, "weight")) {
01566 q->weight = atoi(val);
01567 } else if (!strcasecmp(param, "timeoutrestart")) {
01568 q->timeoutrestart = ast_true(val);
01569 } else if (!strcasecmp(param, "defaultrule")) {
01570 ast_copy_string(q->defaultrule, val, sizeof(q->defaultrule));
01571 } else if (!strcasecmp(param, "timeoutpriority")) {
01572 if (!strcasecmp(val, "conf")) {
01573 q->timeoutpriority = TIMEOUT_PRIORITY_CONF;
01574 } else {
01575 q->timeoutpriority = TIMEOUT_PRIORITY_APP;
01576 }
01577 } else if (failunknown) {
01578 if (linenum >= 0) {
01579 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queues.conf\n",
01580 q->name, param, linenum);
01581 } else {
01582 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s\n", q->name, param);
01583 }
01584 }
01585 }
01586
01587
01588
01589
01590
01591
01592
01593 static void rt_handle_member_record(struct call_queue *q, char *interface, const char *rt_uniqueid, const char *membername, const char *penalty_str, const char *paused_str, const char* state_interface)
01594 {
01595 struct member *m;
01596 struct ao2_iterator mem_iter;
01597 int penalty = 0;
01598 int paused = 0;
01599 int found = 0;
01600
01601 if (ast_strlen_zero(rt_uniqueid)) {
01602 ast_log(LOG_WARNING, "Realtime field uniqueid is empty for member %s\n", S_OR(membername, "NULL"));
01603 return;
01604 }
01605
01606 if (penalty_str) {
01607 penalty = atoi(penalty_str);
01608 if (penalty < 0)
01609 penalty = 0;
01610 }
01611
01612 if (paused_str) {
01613 paused = atoi(paused_str);
01614 if (paused < 0)
01615 paused = 0;
01616 }
01617
01618
01619 mem_iter = ao2_iterator_init(q->members, 0);
01620 while ((m = ao2_iterator_next(&mem_iter))) {
01621 if (!strcasecmp(m->rt_uniqueid, rt_uniqueid)) {
01622 m->dead = 0;
01623 ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
01624 if (paused_str)
01625 m->paused = paused;
01626 if (strcasecmp(state_interface, m->state_interface)) {
01627 ast_copy_string(m->state_interface, state_interface, sizeof(m->state_interface));
01628 }
01629 m->penalty = penalty;
01630 found = 1;
01631 ao2_ref(m, -1);
01632 break;
01633 }
01634 ao2_ref(m, -1);
01635 }
01636 ao2_iterator_destroy(&mem_iter);
01637
01638
01639 if (!found) {
01640 if ((m = create_queue_member(interface, membername, penalty, paused, state_interface))) {
01641 m->dead = 0;
01642 m->realtime = 1;
01643 ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
01644 ast_queue_log(q->name, "REALTIME", m->interface, "ADDMEMBER", "%s", "");
01645 ao2_link(q->members, m);
01646 ao2_ref(m, -1);
01647 m = NULL;
01648 q->membercount++;
01649 }
01650 }
01651 }
01652
01653
01654 static void free_members(struct call_queue *q, int all)
01655 {
01656
01657 struct member *cur;
01658 struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
01659
01660 while ((cur = ao2_iterator_next(&mem_iter))) {
01661 if (all || !cur->dynamic) {
01662 ao2_unlink(q->members, cur);
01663 q->membercount--;
01664 }
01665 ao2_ref(cur, -1);
01666 }
01667 ao2_iterator_destroy(&mem_iter);
01668 }
01669
01670
01671 static void destroy_queue(void *obj)
01672 {
01673 struct call_queue *q = obj;
01674 int i;
01675
01676 free_members(q, 1);
01677 for (i = 0; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
01678 if (q->sound_periodicannounce[i])
01679 free(q->sound_periodicannounce[i]);
01680 }
01681 ao2_ref(q->members, -1);
01682 }
01683
01684 static struct call_queue *alloc_queue(const char *queuename)
01685 {
01686 struct call_queue *q;
01687
01688 if ((q = ao2_t_alloc(sizeof(*q), destroy_queue, "Allocate queue"))) {
01689 ast_copy_string(q->name, queuename, sizeof(q->name));
01690 }
01691 return q;
01692 }
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704 static struct call_queue *find_queue_by_name_rt(const char *queuename, struct ast_variable *queue_vars, struct ast_config *member_config)
01705 {
01706 struct ast_variable *v;
01707 struct call_queue *q, tmpq;
01708 struct member *m;
01709 struct ao2_iterator mem_iter;
01710 char *interface = NULL;
01711 const char *tmp_name;
01712 char *tmp;
01713 char tmpbuf[64];
01714
01715 ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
01716
01717
01718 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Check if static queue exists"))) {
01719 ao2_lock(q);
01720 if (!q->realtime) {
01721 if (q->dead) {
01722 ao2_unlock(q);
01723 queue_t_unref(q, "Queue is dead; can't return it");
01724 return NULL;
01725 } else {
01726 ast_log(LOG_WARNING, "Static queue '%s' already exists. Not loading from realtime\n", q->name);
01727 ao2_unlock(q);
01728 return q;
01729 }
01730 }
01731 } else if (!member_config)
01732
01733 return NULL;
01734
01735
01736 if (!queue_vars) {
01737
01738 if (q) {
01739
01740
01741
01742 ast_debug(1, "Queue %s not found in realtime.\n", queuename);
01743
01744 q->dead = 1;
01745
01746 queues_t_unlink(queues, q, "Unused; removing from container");
01747 ao2_unlock(q);
01748 queue_t_unref(q, "Queue is dead; can't return it");
01749 }
01750 return NULL;
01751 }
01752
01753
01754 if (!q) {
01755 struct ast_variable *tmpvar = NULL;
01756 if (!(q = alloc_queue(queuename)))
01757 return NULL;
01758 ao2_lock(q);
01759 clear_queue(q);
01760 q->realtime = 1;
01761 q->membercount = 0;
01762
01763
01764
01765 for (tmpvar = queue_vars; tmpvar; tmpvar = tmpvar->next) {
01766 if (!strcasecmp(tmpvar->name, "strategy")) {
01767 q->strategy = strat2int(tmpvar->value);
01768 if (q->strategy < 0) {
01769 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
01770 tmpvar->value, q->name);
01771 q->strategy = QUEUE_STRATEGY_RINGALL;
01772 }
01773 break;
01774 }
01775 }
01776
01777 if (!tmpvar)
01778 q->strategy = QUEUE_STRATEGY_RINGALL;
01779 queues_t_link(queues, q, "Add queue to container");
01780 }
01781 init_queue(q);
01782
01783 memset(tmpbuf, 0, sizeof(tmpbuf));
01784 for (v = queue_vars; v; v = v->next) {
01785
01786 if ((tmp = strchr(v->name, '_'))) {
01787 ast_copy_string(tmpbuf, v->name, sizeof(tmpbuf));
01788 tmp_name = tmpbuf;
01789 tmp = tmpbuf;
01790 while ((tmp = strchr(tmp, '_')))
01791 *tmp++ = '-';
01792 } else
01793 tmp_name = v->name;
01794
01795 if (!ast_strlen_zero(v->value)) {
01796
01797 queue_set_param(q, tmp_name, v->value, -1, 0);
01798 }
01799 }
01800
01801
01802
01803 mem_iter = ao2_iterator_init(q->members, 0);
01804 while ((m = ao2_iterator_next(&mem_iter))) {
01805 q->membercount++;
01806 if (m->realtime)
01807 m->dead = 1;
01808 ao2_ref(m, -1);
01809 }
01810 ao2_iterator_destroy(&mem_iter);
01811
01812 while ((interface = ast_category_browse(member_config, interface))) {
01813 rt_handle_member_record(q, interface,
01814 ast_variable_retrieve(member_config, interface, "uniqueid"),
01815 S_OR(ast_variable_retrieve(member_config, interface, "membername"),interface),
01816 ast_variable_retrieve(member_config, interface, "penalty"),
01817 ast_variable_retrieve(member_config, interface, "paused"),
01818 S_OR(ast_variable_retrieve(member_config, interface, "state_interface"),interface));
01819 }
01820
01821
01822 mem_iter = ao2_iterator_init(q->members, 0);
01823 while ((m = ao2_iterator_next(&mem_iter))) {
01824 if (m->dead) {
01825 ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
01826 ao2_unlink(q->members, m);
01827 q->membercount--;
01828 }
01829 ao2_ref(m, -1);
01830 }
01831 ao2_iterator_destroy(&mem_iter);
01832
01833 ao2_unlock(q);
01834
01835 return q;
01836 }
01837
01838 static struct call_queue *load_realtime_queue(const char *queuename)
01839 {
01840 struct ast_variable *queue_vars;
01841 struct ast_config *member_config = NULL;
01842 struct call_queue *q = NULL, tmpq;
01843 int prev_weight = 0;
01844
01845
01846 ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
01847 q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Look for queue in memory first");
01848
01849 if (!q || q->realtime) {
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859 queue_vars = ast_load_realtime("queues", "name", queuename, SENTINEL);
01860 if (queue_vars) {
01861 member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", queuename, SENTINEL);
01862 if (!member_config) {
01863 ast_log(LOG_ERROR, "no queue_members defined in your config (extconfig.conf).\n");
01864 ast_variables_destroy(queue_vars);
01865 return NULL;
01866 }
01867 }
01868 if (q) {
01869 prev_weight = q->weight ? 1 : 0;
01870 }
01871
01872 ao2_lock(queues);
01873
01874 q = find_queue_by_name_rt(queuename, queue_vars, member_config);
01875 if (member_config) {
01876 ast_config_destroy(member_config);
01877 }
01878 if (queue_vars) {
01879 ast_variables_destroy(queue_vars);
01880 }
01881
01882 if (q) {
01883 if (!q->weight && prev_weight) {
01884 ast_atomic_fetchadd_int(&use_weight, -1);
01885 }
01886 if (q->weight && !prev_weight) {
01887 ast_atomic_fetchadd_int(&use_weight, +1);
01888 }
01889 }
01890
01891 ao2_unlock(queues);
01892
01893 } else {
01894 update_realtime_members(q);
01895 }
01896 return q;
01897 }
01898
01899 static int update_realtime_member_field(struct member *mem, const char *queue_name, const char *field, const char *value)
01900 {
01901 int ret = -1;
01902
01903 if (ast_strlen_zero(mem->rt_uniqueid))
01904 return ret;
01905
01906 if ((ast_update_realtime("queue_members", "uniqueid", mem->rt_uniqueid, field, value, SENTINEL)) > 0)
01907 ret = 0;
01908
01909 return ret;
01910 }
01911
01912
01913 static void update_realtime_members(struct call_queue *q)
01914 {
01915 struct ast_config *member_config = NULL;
01916 struct member *m;
01917 char *interface = NULL;
01918 struct ao2_iterator mem_iter;
01919
01920 if (!(member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", q->name , SENTINEL))) {
01921
01922 ast_debug(3, "Queue %s has no realtime members defined. No need for update\n", q->name);
01923 return;
01924 }
01925
01926 ao2_lock(queues);
01927 ao2_lock(q);
01928
01929
01930 mem_iter = ao2_iterator_init(q->members, 0);
01931 while ((m = ao2_iterator_next(&mem_iter))) {
01932 if (m->realtime)
01933 m->dead = 1;
01934 ao2_ref(m, -1);
01935 }
01936 ao2_iterator_destroy(&mem_iter);
01937
01938 while ((interface = ast_category_browse(member_config, interface))) {
01939 rt_handle_member_record(q, interface,
01940 ast_variable_retrieve(member_config, interface, "uniqueid"),
01941 S_OR(ast_variable_retrieve(member_config, interface, "membername"), interface),
01942 ast_variable_retrieve(member_config, interface, "penalty"),
01943 ast_variable_retrieve(member_config, interface, "paused"),
01944 S_OR(ast_variable_retrieve(member_config, interface, "state_interface"), interface));
01945 }
01946
01947
01948 mem_iter = ao2_iterator_init(q->members, 0);
01949 while ((m = ao2_iterator_next(&mem_iter))) {
01950 if (m->dead) {
01951 ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
01952 ao2_unlink(q->members, m);
01953 q->membercount--;
01954 }
01955 ao2_ref(m, -1);
01956 }
01957 ao2_iterator_destroy(&mem_iter);
01958 ao2_unlock(q);
01959 ao2_unlock(queues);
01960 ast_config_destroy(member_config);
01961 }
01962
01963 static int join_queue(char *queuename, struct queue_ent *qe, enum queue_result *reason)
01964 {
01965 struct call_queue *q;
01966 struct queue_ent *cur, *prev = NULL;
01967 int res = -1;
01968 int pos = 0;
01969 int inserted = 0;
01970
01971 if (!(q = load_realtime_queue(queuename)))
01972 return res;
01973
01974 ao2_lock(queues);
01975 ao2_lock(q);
01976
01977
01978 if (q->joinempty) {
01979 int status = 0;
01980 if ((status = get_member_status(q, qe->max_penalty, qe->min_penalty, q->joinempty))) {
01981 *reason = QUEUE_JOINEMPTY;
01982 ao2_unlock(q);
01983 ao2_unlock(queues);
01984 return res;
01985 }
01986 }
01987 if (*reason == QUEUE_UNKNOWN && q->maxlen && (q->count >= q->maxlen))
01988 *reason = QUEUE_FULL;
01989 else if (*reason == QUEUE_UNKNOWN) {
01990
01991
01992
01993 inserted = 0;
01994 prev = NULL;
01995 cur = q->head;
01996 while (cur) {
01997
01998
01999
02000 if ((!inserted) && (qe->prio > cur->prio)) {
02001 insert_entry(q, prev, qe, &pos);
02002 inserted = 1;
02003 }
02004 cur->pos = ++pos;
02005 prev = cur;
02006 cur = cur->next;
02007 }
02008
02009 if (!inserted)
02010 insert_entry(q, prev, qe, &pos);
02011 ast_copy_string(qe->moh, q->moh, sizeof(qe->moh));
02012 ast_copy_string(qe->announce, q->announce, sizeof(qe->announce));
02013 ast_copy_string(qe->context, q->context, sizeof(qe->context));
02014 q->count++;
02015 res = 0;
02016 manager_event(EVENT_FLAG_CALL, "Join",
02017 "Channel: %s\r\nCallerIDNum: %s\r\nCallerIDName: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\nUniqueid: %s\r\n",
02018 qe->chan->name,
02019 S_OR(qe->chan->cid.cid_num, "unknown"),
02020 S_OR(qe->chan->cid.cid_name, "unknown"),
02021 q->name, qe->pos, q->count, qe->chan->uniqueid );
02022 ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
02023 }
02024 ao2_unlock(q);
02025 ao2_unlock(queues);
02026
02027 return res;
02028 }
02029
02030 static int play_file(struct ast_channel *chan, const char *filename)
02031 {
02032 int res;
02033
02034 if (ast_strlen_zero(filename)) {
02035 return 0;
02036 }
02037
02038 if (!ast_fileexists(filename, NULL, chan->language)) {
02039 return 0;
02040 }
02041
02042 ast_stopstream(chan);
02043
02044 res = ast_streamfile(chan, filename, chan->language);
02045 if (!res)
02046 res = ast_waitstream(chan, AST_DIGIT_ANY);
02047
02048 ast_stopstream(chan);
02049
02050 return res;
02051 }
02052
02053
02054
02055
02056
02057
02058 static int valid_exit(struct queue_ent *qe, char digit)
02059 {
02060 int digitlen = strlen(qe->digits);
02061
02062
02063 if (digitlen < sizeof(qe->digits) - 2) {
02064 qe->digits[digitlen] = digit;
02065 qe->digits[digitlen + 1] = '\0';
02066 } else {
02067 qe->digits[0] = '\0';
02068 return 0;
02069 }
02070
02071
02072 if (ast_strlen_zero(qe->context))
02073 return 0;
02074
02075
02076 if (!ast_canmatch_extension(qe->chan, qe->context, qe->digits, 1, qe->chan->cid.cid_num)) {
02077 qe->digits[0] = '\0';
02078 return 0;
02079 }
02080
02081
02082 if (!ast_goto_if_exists(qe->chan, qe->context, qe->digits, 1)) {
02083 qe->valid_digits = 1;
02084
02085 return 1;
02086 }
02087
02088 return 0;
02089 }
02090
02091 static int say_position(struct queue_ent *qe, int ringing)
02092 {
02093 int res = 0, avgholdmins, avgholdsecs, announceposition = 0;
02094 int say_thanks = 1;
02095 time_t now;
02096
02097
02098 time(&now);
02099 if ((now - qe->last_pos) < qe->parent->minannouncefrequency)
02100 return 0;
02101
02102
02103 if ((qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency))
02104 return 0;
02105
02106 if (ringing) {
02107 ast_indicate(qe->chan,-1);
02108 } else {
02109 ast_moh_stop(qe->chan);
02110 }
02111
02112 if (qe->parent->announceposition == ANNOUNCEPOSITION_YES ||
02113 qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN ||
02114 (qe->parent->announceposition == ANNOUNCEPOSITION_LIMIT &&
02115 qe->pos <= qe->parent->announcepositionlimit))
02116 announceposition = 1;
02117
02118
02119 if (announceposition == 1) {
02120
02121 if (qe->pos == 1) {
02122 res = play_file(qe->chan, qe->parent->sound_next);
02123 if (res)
02124 goto playout;
02125 else
02126 goto posout;
02127 } else {
02128 if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
02129
02130 res = play_file(qe->chan, qe->parent->queue_quantity1);
02131 if (res)
02132 goto playout;
02133 res = ast_say_number(qe->chan, qe->parent->announcepositionlimit, AST_DIGIT_ANY, qe->chan->language, NULL);
02134 if (res)
02135 goto playout;
02136 } else {
02137
02138 res = play_file(qe->chan, qe->parent->sound_thereare);
02139 if (res)
02140 goto playout;
02141 res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, NULL);
02142 if (res)
02143 goto playout;
02144 }
02145 if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
02146
02147 res = play_file(qe->chan, qe->parent->queue_quantity2);
02148 if (res)
02149 goto playout;
02150 } else {
02151 res = play_file(qe->chan, qe->parent->sound_calls);
02152 if (res)
02153 goto playout;
02154 }
02155 }
02156 }
02157
02158 avgholdmins = abs(((qe->parent->holdtime + 30) - (now - qe->start)) / 60);
02159
02160
02161 if (qe->parent->roundingseconds) {
02162 avgholdsecs = (abs(((qe->parent->holdtime + 30) - (now - qe->start))) - 60 * avgholdmins) / qe->parent->roundingseconds;
02163 avgholdsecs *= qe->parent->roundingseconds;
02164 } else {
02165 avgholdsecs = 0;
02166 }
02167
02168 ast_verb(3, "Hold time for %s is %d minute(s) %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
02169
02170
02171
02172 if ((avgholdmins+avgholdsecs) > 0 && qe->parent->announceholdtime &&
02173 ((qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE && !qe->last_pos) ||
02174 !(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE))) {
02175 res = play_file(qe->chan, qe->parent->sound_holdtime);
02176 if (res)
02177 goto playout;
02178
02179 if (avgholdmins >= 1) {
02180 res = ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language, NULL);
02181 if (res)
02182 goto playout;
02183
02184 if (avgholdmins == 1) {
02185 res = play_file(qe->chan, qe->parent->sound_minute);
02186 if (res)
02187 goto playout;
02188 } else {
02189 res = play_file(qe->chan, qe->parent->sound_minutes);
02190 if (res)
02191 goto playout;
02192 }
02193 }
02194 if (avgholdsecs >= 1) {
02195 res = ast_say_number(qe->chan, avgholdsecs, AST_DIGIT_ANY, qe->chan->language, NULL);
02196 if (res)
02197 goto playout;
02198
02199 res = play_file(qe->chan, qe->parent->sound_seconds);
02200 if (res)
02201 goto playout;
02202 }
02203 } else if (qe->parent->announceholdtime && !qe->parent->announceposition) {
02204 say_thanks = 0;
02205 }
02206
02207 posout:
02208 if (qe->parent->announceposition) {
02209 ast_verb(3, "Told %s in %s their queue position (which was %d)\n",
02210 qe->chan->name, qe->parent->name, qe->pos);
02211 }
02212 if (say_thanks) {
02213 res = play_file(qe->chan, qe->parent->sound_thanks);
02214 }
02215 playout:
02216
02217 if ((res > 0 && !valid_exit(qe, res)))
02218 res = 0;
02219
02220
02221 qe->last_pos = now;
02222 qe->last_pos_said = qe->pos;
02223
02224
02225 if (!res) {
02226 if (ringing) {
02227 ast_indicate(qe->chan, AST_CONTROL_RINGING);
02228 } else {
02229 ast_moh_start(qe->chan, qe->moh, NULL);
02230 }
02231 }
02232 return res;
02233 }
02234
02235 static void recalc_holdtime(struct queue_ent *qe, int newholdtime)
02236 {
02237 int oldvalue;
02238
02239
02240
02241
02242
02243 ao2_lock(qe->parent);
02244 oldvalue = qe->parent->holdtime;
02245 qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newholdtime) >> 2;
02246 ao2_unlock(qe->parent);
02247 }
02248
02249
02250
02251
02252
02253
02254 static void leave_queue(struct queue_ent *qe)
02255 {
02256 struct call_queue *q;
02257 struct queue_ent *current, *prev = NULL;
02258 struct penalty_rule *pr_iter;
02259 int pos = 0;
02260
02261 if (!(q = qe->parent))
02262 return;
02263 queue_t_ref(q, "Copy queue pointer from queue entry");
02264 ao2_lock(q);
02265
02266 prev = NULL;
02267 for (current = q->head; current; current = current->next) {
02268 if (current == qe) {
02269 q->count--;
02270
02271
02272 manager_event(EVENT_FLAG_CALL, "Leave",
02273 "Channel: %s\r\nQueue: %s\r\nCount: %d\r\nUniqueid: %s\r\n",
02274 qe->chan->name, q->name, q->count, qe->chan->uniqueid);
02275 ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
02276
02277 if (prev)
02278 prev->next = current->next;
02279 else
02280 q->head = current->next;
02281
02282 while ((pr_iter = AST_LIST_REMOVE_HEAD(&qe->qe_rules, list)))
02283 ast_free(pr_iter);
02284 } else {
02285
02286 current->pos = ++pos;
02287 prev = current;
02288 }
02289 }
02290 ao2_unlock(q);
02291
02292
02293 if (q->realtime) {
02294 struct ast_variable *var;
02295 if (!(var = ast_load_realtime("queues", "name", q->name, SENTINEL))) {
02296 q->dead = 1;
02297 } else {
02298 ast_variables_destroy(var);
02299 }
02300 }
02301
02302 if (q->dead) {
02303
02304 queues_t_unlink(queues, q, "Queue is now dead; remove it from the container");
02305 }
02306
02307 queue_t_unref(q, "Expire copied reference");
02308 }
02309
02310
02311 static void hangupcalls(struct callattempt *outgoing, struct ast_channel *exception, int cancel_answered_elsewhere)
02312 {
02313 struct callattempt *oo;
02314
02315 while (outgoing) {
02316
02317
02318 if (outgoing->chan && (outgoing->chan != exception)) {
02319 if (exception || cancel_answered_elsewhere)
02320 ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
02321 ast_hangup(outgoing->chan);
02322 }
02323 oo = outgoing;
02324 outgoing = outgoing->q_next;
02325 if (oo->member)
02326 ao2_ref(oo->member, -1);
02327 ast_free(oo);
02328 }
02329 }
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339 static int num_available_members(struct call_queue *q)
02340 {
02341 struct member *mem;
02342 int avl = 0;
02343 struct ao2_iterator mem_iter;
02344
02345 mem_iter = ao2_iterator_init(q->members, 0);
02346 while ((mem = ao2_iterator_next(&mem_iter))) {
02347 switch (mem->status) {
02348 case AST_DEVICE_INUSE:
02349 if (!q->ringinuse)
02350 break;
02351
02352 case AST_DEVICE_NOT_INUSE:
02353 case AST_DEVICE_UNKNOWN:
02354 if (!mem->paused) {
02355 avl++;
02356 }
02357 break;
02358 }
02359 ao2_ref(mem, -1);
02360
02361
02362
02363
02364
02365
02366
02367
02368
02369
02370
02371 if ((!q->autofill || q->strategy == QUEUE_STRATEGY_RINGALL) && avl) {
02372 break;
02373 }
02374 }
02375 ao2_iterator_destroy(&mem_iter);
02376
02377 return avl;
02378 }
02379
02380
02381
02382 static int compare_weight(struct call_queue *rq, struct member *member)
02383 {
02384 struct call_queue *q;
02385 struct member *mem;
02386 int found = 0;
02387 struct ao2_iterator queue_iter;
02388
02389
02390
02391 queue_iter = ao2_iterator_init(queues, 0);
02392 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
02393 if (q == rq) {
02394 queue_t_unref(q, "Done with iterator");
02395 continue;
02396 }
02397 ao2_lock(q);
02398 if (q->count && q->members) {
02399 if ((mem = ao2_find(q->members, member, OBJ_POINTER))) {
02400 ast_debug(1, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
02401 if (q->weight > rq->weight && q->count >= num_available_members(q)) {
02402 ast_debug(1, "Queue '%s' (weight %d, calls %d) is preferred over '%s' (weight %d, calls %d)\n", q->name, q->weight, q->count, rq->name, rq->weight, rq->count);
02403 found = 1;
02404 }
02405 ao2_ref(mem, -1);
02406 }
02407 }
02408 ao2_unlock(q);
02409 queue_t_unref(q, "Done with iterator");
02410 if (found) {
02411 break;
02412 }
02413 }
02414 ao2_iterator_destroy(&queue_iter);
02415 return found;
02416 }
02417
02418
02419 static void do_hang(struct callattempt *o)
02420 {
02421 o->stillgoing = 0;
02422 ast_hangup(o->chan);
02423 o->chan = NULL;
02424 }
02425
02426
02427 static char *vars2manager(struct ast_channel *chan, char *vars, size_t len)
02428 {
02429 struct ast_str *buf = ast_str_thread_get(&ast_str_thread_global_buf, len + 1);
02430 char *tmp;
02431
02432 if (pbx_builtin_serialize_variables(chan, &buf)) {
02433 int i, j;
02434
02435
02436 strcpy(vars, "Variable: ");
02437 tmp = ast_str_buffer(buf);
02438
02439 for (i = 0, j = 10; (i < len - 1) && (j < len - 1); i++, j++) {
02440 vars[j] = tmp[i];
02441
02442 if (tmp[i + 1] == '\0')
02443 break;
02444 if (tmp[i] == '\n') {
02445 vars[j++] = '\r';
02446 vars[j++] = '\n';
02447
02448 ast_copy_string(&(vars[j]), "Variable: ", len - j);
02449 j += 9;
02450 }
02451 }
02452 if (j > len - 3)
02453 j = len - 3;
02454 vars[j++] = '\r';
02455 vars[j++] = '\n';
02456 vars[j] = '\0';
02457 } else {
02458
02459 *vars = '\0';
02460 }
02461 return vars;
02462 }
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475
02476
02477
02478 static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies)
02479 {
02480 int res;
02481 int status;
02482 char tech[256];
02483 char *location;
02484 const char *macrocontext, *macroexten;
02485
02486
02487 if ((tmp->lastqueue && tmp->lastqueue->wrapuptime && (time(NULL) - tmp->lastcall < tmp->lastqueue->wrapuptime)) ||
02488 (!tmp->lastqueue && qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime))) {
02489 ast_debug(1, "Wrapuptime not yet expired on queue %s for %s\n",
02490 (tmp->lastqueue ? tmp->lastqueue->name : qe->parent->name), tmp->interface);
02491 if (qe->chan->cdr)
02492 ast_cdr_busy(qe->chan->cdr);
02493 tmp->stillgoing = 0;
02494 (*busies)++;
02495 return 0;
02496 }
02497
02498 if (!qe->parent->ringinuse && (tmp->member->status != AST_DEVICE_NOT_INUSE) && (tmp->member->status != AST_DEVICE_UNKNOWN)) {
02499 ast_debug(1, "%s in use, can't receive call\n", tmp->interface);
02500 if (qe->chan->cdr)
02501 ast_cdr_busy(qe->chan->cdr);
02502 tmp->stillgoing = 0;
02503 return 0;
02504 }
02505
02506 if (tmp->member->paused) {
02507 ast_debug(1, "%s paused, can't receive call\n", tmp->interface);
02508 if (qe->chan->cdr)
02509 ast_cdr_busy(qe->chan->cdr);
02510 tmp->stillgoing = 0;
02511 return 0;
02512 }
02513 if (use_weight && compare_weight(qe->parent,tmp->member)) {
02514 ast_debug(1, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
02515 if (qe->chan->cdr)
02516 ast_cdr_busy(qe->chan->cdr);
02517 tmp->stillgoing = 0;
02518 (*busies)++;
02519 return 0;
02520 }
02521
02522 ast_copy_string(tech, tmp->interface, sizeof(tech));
02523 if ((location = strchr(tech, '/')))
02524 *location++ = '\0';
02525 else
02526 location = "";
02527
02528
02529 tmp->chan = ast_request(tech, qe->chan->nativeformats, location, &status);
02530 if (!tmp->chan) {
02531 if (qe->chan->cdr)
02532 ast_cdr_busy(qe->chan->cdr);
02533 tmp->stillgoing = 0;
02534
02535 ao2_lock(qe->parent);
02536 update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02537 qe->parent->rrpos++;
02538 qe->linpos++;
02539 ao2_unlock(qe->parent);
02540
02541 (*busies)++;
02542 return 0;
02543 }
02544
02545 if (qe->cancel_answered_elsewhere) {
02546 ast_set_flag(tmp->chan, AST_FLAG_ANSWERED_ELSEWHERE);
02547 }
02548 tmp->chan->appl = "AppQueue";
02549 tmp->chan->data = "(Outgoing Line)";
02550 memset(&tmp->chan->whentohangup, 0, sizeof(tmp->chan->whentohangup));
02551 if (tmp->chan->cid.cid_num)
02552 ast_free(tmp->chan->cid.cid_num);
02553 tmp->chan->cid.cid_num = ast_strdup(qe->chan->cid.cid_num);
02554 if (tmp->chan->cid.cid_name)
02555 ast_free(tmp->chan->cid.cid_name);
02556 tmp->chan->cid.cid_name = ast_strdup(qe->chan->cid.cid_name);
02557 if (tmp->chan->cid.cid_ani)
02558 ast_free(tmp->chan->cid.cid_ani);
02559 tmp->chan->cid.cid_ani = ast_strdup(qe->chan->cid.cid_ani);
02560
02561
02562 ast_channel_inherit_variables(qe->chan, tmp->chan);
02563 ast_channel_datastore_inherit(qe->chan, tmp->chan);
02564
02565
02566 tmp->chan->adsicpe = qe->chan->adsicpe;
02567
02568
02569 ast_channel_lock(qe->chan);
02570 macrocontext = pbx_builtin_getvar_helper(qe->chan, "MACRO_CONTEXT");
02571 ast_string_field_set(tmp->chan, dialcontext, ast_strlen_zero(macrocontext) ? qe->chan->context : macrocontext);
02572 macroexten = pbx_builtin_getvar_helper(qe->chan, "MACRO_EXTEN");
02573 if (!ast_strlen_zero(macroexten))
02574 ast_copy_string(tmp->chan->exten, macroexten, sizeof(tmp->chan->exten));
02575 else
02576 ast_copy_string(tmp->chan->exten, qe->chan->exten, sizeof(tmp->chan->exten));
02577 if (ast_cdr_isset_unanswered()) {
02578
02579
02580 ast_cdr_setdestchan(tmp->chan->cdr, tmp->chan->name);
02581 strcpy(tmp->chan->cdr->clid, qe->chan->cdr->clid);
02582 strcpy(tmp->chan->cdr->channel, qe->chan->cdr->channel);
02583 strcpy(tmp->chan->cdr->src, qe->chan->cdr->src);
02584 strcpy(tmp->chan->cdr->dst, qe->chan->exten);
02585 strcpy(tmp->chan->cdr->dcontext, qe->chan->context);
02586 strcpy(tmp->chan->cdr->lastapp, qe->chan->cdr->lastapp);
02587 strcpy(tmp->chan->cdr->lastdata, qe->chan->cdr->lastdata);
02588 tmp->chan->cdr->amaflags = qe->chan->cdr->amaflags;
02589 strcpy(tmp->chan->cdr->accountcode, qe->chan->cdr->accountcode);
02590 strcpy(tmp->chan->cdr->userfield, qe->chan->cdr->userfield);
02591 }
02592 ast_channel_unlock(qe->chan);
02593
02594
02595 if ((res = ast_call(tmp->chan, location, 0))) {
02596
02597 ast_debug(1, "ast call on peer returned %d\n", res);
02598 ast_verb(3, "Couldn't call %s\n", tmp->interface);
02599 do_hang(tmp);
02600 (*busies)++;
02601 update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02602 return 0;
02603 } else if (qe->parent->eventwhencalled) {
02604 char vars[2048];
02605
02606 manager_event(EVENT_FLAG_AGENT, "AgentCalled",
02607 "Queue: %s\r\n"
02608 "AgentCalled: %s\r\n"
02609 "AgentName: %s\r\n"
02610 "ChannelCalling: %s\r\n"
02611 "DestinationChannel: %s\r\n"
02612 "CallerIDNum: %s\r\n"
02613 "CallerIDName: %s\r\n"
02614 "Context: %s\r\n"
02615 "Extension: %s\r\n"
02616 "Priority: %d\r\n"
02617 "Uniqueid: %s\r\n"
02618 "%s",
02619 qe->parent->name, tmp->interface, tmp->member->membername, qe->chan->name, tmp->chan->name,
02620 tmp->chan->cid.cid_num ? tmp->chan->cid.cid_num : "unknown",
02621 tmp->chan->cid.cid_name ? tmp->chan->cid.cid_name : "unknown",
02622 qe->chan->context, qe->chan->exten, qe->chan->priority, qe->chan->uniqueid,
02623 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
02624 ast_verb(3, "Called %s\n", tmp->interface);
02625 }
02626
02627 update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02628 return 1;
02629 }
02630
02631
02632 static struct callattempt *find_best(struct callattempt *outgoing)
02633 {
02634 struct callattempt *best = NULL, *cur;
02635
02636 for (cur = outgoing; cur; cur = cur->q_next) {
02637 if (cur->stillgoing &&
02638 !cur->chan &&
02639 (!best || cur->metric < best->metric)) {
02640 best = cur;
02641 }
02642 }
02643
02644 return best;
02645 }
02646
02647
02648
02649
02650
02651
02652
02653
02654
02655
02656
02657 static int ring_one(struct queue_ent *qe, struct callattempt *outgoing, int *busies)
02658 {
02659 int ret = 0;
02660
02661 while (ret == 0) {
02662 struct callattempt *best = find_best(outgoing);
02663 if (!best) {
02664 ast_debug(1, "Nobody left to try ringing in queue\n");
02665 break;
02666 }
02667 if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
02668 struct callattempt *cur;
02669
02670 for (cur = outgoing; cur; cur = cur->q_next) {
02671 if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
02672 ast_debug(1, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
02673 ret |= ring_entry(qe, cur, busies);
02674 }
02675 }
02676 } else {
02677
02678 ast_debug(1, "Trying '%s' with metric %d\n", best->interface, best->metric);
02679 ret = ring_entry(qe, best, busies);
02680 }
02681
02682
02683 if (qe->expire && (time(NULL) >= qe->expire)) {
02684 ast_debug(1, "Queue timed out while ringing members.\n");
02685 ret = 0;
02686 break;
02687 }
02688 }
02689
02690 return ret;
02691 }
02692
02693
02694 static int store_next_rr(struct queue_ent *qe, struct callattempt *outgoing)
02695 {
02696 struct callattempt *best = find_best(outgoing);
02697
02698 if (best) {
02699
02700 ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
02701 qe->parent->rrpos = best->metric % 1000;
02702 } else {
02703
02704 if (qe->parent->wrapped) {
02705
02706 qe->parent->rrpos = 0;
02707 } else {
02708
02709 qe->parent->rrpos++;
02710 }
02711 }
02712 qe->parent->wrapped = 0;
02713
02714 return 0;
02715 }
02716
02717
02718 static int store_next_lin(struct queue_ent *qe, struct callattempt *outgoing)
02719 {
02720 struct callattempt *best = find_best(outgoing);
02721
02722 if (best) {
02723
02724 ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
02725 qe->linpos = best->metric % 1000;
02726 } else {
02727
02728 if (qe->linwrapped) {
02729
02730 qe->linpos = 0;
02731 } else {
02732
02733 qe->linpos++;
02734 }
02735 }
02736 qe->linwrapped = 0;
02737
02738 return 0;
02739 }
02740
02741
02742 static int say_periodic_announcement(struct queue_ent *qe, int ringing)
02743 {
02744 int res = 0;
02745 time_t now;
02746
02747
02748 time(&now);
02749
02750
02751 if ((now - qe->last_periodic_announce_time) < qe->parent->periodicannouncefrequency)
02752 return 0;
02753
02754
02755 if (ringing)
02756 ast_indicate(qe->chan,-1);
02757 else
02758 ast_moh_stop(qe->chan);
02759
02760 ast_verb(3, "Playing periodic announcement\n");
02761
02762 if (qe->parent->randomperiodicannounce && qe->parent->numperiodicannounce) {
02763 qe->last_periodic_announce_sound = ((unsigned long) ast_random()) % qe->parent->numperiodicannounce;
02764 } else if (qe->last_periodic_announce_sound >= qe->parent->numperiodicannounce ||
02765 ast_str_strlen(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]) == 0) {
02766 qe->last_periodic_announce_sound = 0;
02767 }
02768
02769
02770 res = play_file(qe->chan, ast_str_buffer(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]));
02771
02772 if (res > 0 && !valid_exit(qe, res))
02773 res = 0;
02774
02775
02776 if (!res) {
02777 if (ringing)
02778 ast_indicate(qe->chan, AST_CONTROL_RINGING);
02779 else
02780 ast_moh_start(qe->chan, qe->moh, NULL);
02781 }
02782
02783
02784 qe->last_periodic_announce_time = now;
02785
02786
02787 if (!qe->parent->randomperiodicannounce) {
02788 qe->last_periodic_announce_sound++;
02789 }
02790
02791 return res;
02792 }
02793
02794
02795 static void record_abandoned(struct queue_ent *qe)
02796 {
02797 ao2_lock(qe->parent);
02798 set_queue_variables(qe->parent, qe->chan);
02799 manager_event(EVENT_FLAG_AGENT, "QueueCallerAbandon",
02800 "Queue: %s\r\n"
02801 "Uniqueid: %s\r\n"
02802 "Position: %d\r\n"
02803 "OriginalPosition: %d\r\n"
02804 "HoldTime: %d\r\n",
02805 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
02806
02807 qe->parent->callsabandoned++;
02808 ao2_unlock(qe->parent);
02809 }
02810
02811
02812 static void rna(int rnatime, struct queue_ent *qe, char *interface, char *membername, int pause)
02813 {
02814 ast_verb(3, "Nobody picked up in %d ms\n", rnatime);
02815 if (qe->parent->eventwhencalled) {
02816 char vars[2048];
02817
02818 manager_event(EVENT_FLAG_AGENT, "AgentRingNoAnswer",
02819 "Queue: %s\r\n"
02820 "Uniqueid: %s\r\n"
02821 "Channel: %s\r\n"
02822 "Member: %s\r\n"
02823 "MemberName: %s\r\n"
02824 "Ringtime: %d\r\n"
02825 "%s",
02826 qe->parent->name,
02827 qe->chan->uniqueid,
02828 qe->chan->name,
02829 interface,
02830 membername,
02831 rnatime,
02832 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
02833 }
02834 ast_queue_log(qe->parent->name, qe->chan->uniqueid, membername, "RINGNOANSWER", "%d", rnatime);
02835 if (qe->parent->autopause && pause) {
02836 if (!set_member_paused(qe->parent->name, interface, "Auto-Pause", 1)) {
02837 ast_verb(3, "Auto-Pausing Queue Member %s in queue %s since they failed to answer.\n", interface, qe->parent->name);
02838 } else {
02839 ast_verb(3, "Failed to pause Queue Member %s in queue %s!\n", interface, qe->parent->name);
02840 }
02841 }
02842 return;
02843 }
02844
02845 #define AST_MAX_WATCHERS 256
02846
02847
02848
02849
02850
02851
02852
02853
02854
02855
02856 static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callattempt *outgoing, int *to, char *digit, int prebusies, int caller_disconnect, int forwardsallowed)
02857 {
02858 const char *queue = qe->parent->name;
02859 struct callattempt *o, *start = NULL, *prev = NULL;
02860 int status;
02861 int numbusies = prebusies;
02862 int numnochan = 0;
02863 int stillgoing = 0;
02864 int orig = *to;
02865 struct ast_frame *f;
02866 struct callattempt *peer = NULL;
02867 struct ast_channel *winner;
02868 struct ast_channel *in = qe->chan;
02869 char on[80] = "";
02870 char membername[80] = "";
02871 long starttime = 0;
02872 long endtime = 0;
02873 #ifdef HAVE_EPOLL
02874 struct callattempt *epollo;
02875 #endif
02876
02877 starttime = (long) time(NULL);
02878 #ifdef HAVE_EPOLL
02879 for (epollo = outgoing; epollo; epollo = epollo->q_next) {
02880 if (epollo->chan)
02881 ast_poll_channel_add(in, epollo->chan);
02882 }
02883 #endif
02884
02885 while (*to && !peer) {
02886 int numlines, retry, pos = 1;
02887 struct ast_channel *watchers[AST_MAX_WATCHERS];
02888 watchers[0] = in;
02889 start = NULL;
02890
02891 for (retry = 0; retry < 2; retry++) {
02892 numlines = 0;
02893 for (o = outgoing; o; o = o->q_next) {
02894 if (o->stillgoing) {
02895 stillgoing = 1;
02896 if (o->chan) {
02897 watchers[pos++] = o->chan;
02898 if (!start)
02899 start = o;
02900 else
02901 prev->call_next = o;
02902 prev = o;
02903 }
02904 }
02905 numlines++;
02906 }
02907 if (pos > 1 || !stillgoing ||
02908 (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) )
02909 break;
02910
02911
02912 ring_one(qe, outgoing, &numbusies);
02913
02914 }
02915 if (pos == 1 ) {
02916 if (numlines == (numbusies + numnochan)) {
02917 ast_debug(1, "Everyone is busy at this time\n");
02918 } else {
02919 ast_debug(3, "No one is answering queue '%s' (%d numlines / %d busies / %d failed channels)\n", queue, numlines, numbusies, numnochan);
02920 }
02921 *to = 0;
02922 return NULL;
02923 }
02924
02925
02926 winner = ast_waitfor_n(watchers, pos, to);
02927
02928
02929 for (o = start; o; o = o->call_next) {
02930 if (o->stillgoing && (o->chan) && (o->chan->_state == AST_STATE_UP)) {
02931 if (!peer) {
02932 ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
02933 peer = o;
02934 }
02935 } else if (o->chan && (o->chan == winner)) {
02936
02937 ast_copy_string(on, o->member->interface, sizeof(on));
02938 ast_copy_string(membername, o->member->membername, sizeof(membername));
02939
02940 if (!ast_strlen_zero(o->chan->call_forward) && !forwardsallowed) {
02941 ast_verb(3, "Forwarding %s to '%s' prevented.\n", in->name, o->chan->call_forward);
02942 numnochan++;
02943 do_hang(o);
02944 winner = NULL;
02945 continue;
02946 } else if (!ast_strlen_zero(o->chan->call_forward)) {
02947 char tmpchan[256];
02948 char *stuff;
02949 char *tech;
02950
02951 ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
02952 if ((stuff = strchr(tmpchan, '/'))) {
02953 *stuff++ = '\0';
02954 tech = tmpchan;
02955 } else {
02956 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
02957 stuff = tmpchan;
02958 tech = "Local";
02959 }
02960
02961 ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
02962
02963 o->chan = ast_request(tech, in->nativeformats, stuff, &status);
02964 if (!o->chan) {
02965 ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s'\n", tech, stuff);
02966 o->stillgoing = 0;
02967 numnochan++;
02968 } else {
02969 ast_channel_inherit_variables(in, o->chan);
02970 ast_channel_datastore_inherit(in, o->chan);
02971 if (o->chan->cid.cid_num)
02972 ast_free(o->chan->cid.cid_num);
02973 o->chan->cid.cid_num = ast_strdup(in->cid.cid_num);
02974
02975 if (o->chan->cid.cid_name)
02976 ast_free(o->chan->cid.cid_name);
02977 o->chan->cid.cid_name = ast_strdup(in->cid.cid_name);
02978
02979 ast_string_field_set(o->chan, accountcode, in->accountcode);
02980 o->chan->cdrflags = in->cdrflags;
02981
02982 if (in->cid.cid_ani) {
02983 if (o->chan->cid.cid_ani)
02984 ast_free(o->chan->cid.cid_ani);
02985 o->chan->cid.cid_ani = ast_strdup(in->cid.cid_ani);
02986 }
02987 if (o->chan->cid.cid_rdnis)
02988 ast_free(o->chan->cid.cid_rdnis);
02989 o->chan->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
02990 if (ast_call(o->chan, tmpchan, 0)) {
02991 ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
02992 do_hang(o);
02993 numnochan++;
02994 }
02995 }
02996
02997 ast_hangup(winner);
02998 continue;
02999 }
03000 f = ast_read(winner);
03001 if (f) {
03002 if (f->frametype == AST_FRAME_CONTROL) {
03003 switch (f->subclass) {
03004 case AST_CONTROL_ANSWER:
03005
03006 if (!peer) {
03007 ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
03008 peer = o;
03009 }
03010 break;
03011 case AST_CONTROL_BUSY:
03012 ast_verb(3, "%s is busy\n", o->chan->name);
03013 if (in->cdr)
03014 ast_cdr_busy(in->cdr);
03015 do_hang(o);
03016 endtime = (long) time(NULL);
03017 endtime -= starttime;
03018 rna(endtime * 1000, qe, on, membername, 0);
03019 if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03020 if (qe->parent->timeoutrestart)
03021 *to = orig;
03022
03023 if (*to > 500) {
03024 ring_one(qe, outgoing, &numbusies);
03025 starttime = (long) time(NULL);
03026 }
03027 }
03028 numbusies++;
03029 break;
03030 case AST_CONTROL_CONGESTION:
03031 ast_verb(3, "%s is circuit-busy\n", o->chan->name);
03032 if (in->cdr)
03033 ast_cdr_busy(in->cdr);
03034 endtime = (long) time(NULL);
03035 endtime -= starttime;
03036 rna(endtime * 1000, qe, on, membername, 0);
03037 do_hang(o);
03038 if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03039 if (qe->parent->timeoutrestart)
03040 *to = orig;
03041 if (*to > 500) {
03042 ring_one(qe, outgoing, &numbusies);
03043 starttime = (long) time(NULL);
03044 }
03045 }
03046 numbusies++;
03047 break;
03048 case AST_CONTROL_RINGING:
03049 ast_verb(3, "%s is ringing\n", o->chan->name);
03050 break;
03051 case AST_CONTROL_OFFHOOK:
03052
03053 break;
03054 default:
03055 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
03056 }
03057 }
03058 ast_frfree(f);
03059 } else {
03060 endtime = (long) time(NULL) - starttime;
03061 rna(endtime * 1000, qe, on, membername, 1);
03062 do_hang(o);
03063 if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03064 if (qe->parent->timeoutrestart)
03065 *to = orig;
03066 if (*to > 500) {
03067 ring_one(qe, outgoing, &numbusies);
03068 starttime = (long) time(NULL);
03069 }
03070 }
03071 }
03072 }
03073 }
03074
03075
03076 if (winner == in) {
03077 f = ast_read(in);
03078 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
03079
03080 *to = -1;
03081 if (f) {
03082 if (f->data.uint32) {
03083 in->hangupcause = f->data.uint32;
03084 }
03085 ast_frfree(f);
03086 }
03087 return NULL;
03088 }
03089 if ((f->frametype == AST_FRAME_DTMF) && caller_disconnect && (f->subclass == '*')) {
03090 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
03091 *to = 0;
03092 ast_frfree(f);
03093 return NULL;
03094 }
03095 if ((f->frametype == AST_FRAME_DTMF) && valid_exit(qe, f->subclass)) {
03096 ast_verb(3, "User pressed digit: %c\n", f->subclass);
03097 *to = 0;
03098 *digit = f->subclass;
03099 ast_frfree(f);
03100 return NULL;
03101 }
03102 ast_frfree(f);
03103 }
03104 if (!*to) {
03105 for (o = start; o; o = o->call_next)
03106 rna(orig, qe, o->interface, o->member->membername, 1);
03107 }
03108 }
03109
03110 #ifdef HAVE_EPOLL
03111 for (epollo = outgoing; epollo; epollo = epollo->q_next) {
03112 if (epollo->chan)
03113 ast_poll_channel_del(in, epollo->chan);
03114 }
03115 #endif
03116
03117 return peer;
03118 }
03119
03120
03121
03122
03123
03124
03125
03126
03127
03128
03129
03130
03131 static int is_our_turn(struct queue_ent *qe)
03132 {
03133 struct queue_ent *ch;
03134 int res;
03135 int avl;
03136 int idx = 0;
03137
03138 ao2_lock(qe->parent);
03139
03140 avl = num_available_members(qe->parent);
03141
03142 ch = qe->parent->head;
03143
03144 ast_debug(1, "There %s %d available %s.\n", avl != 1 ? "are" : "is", avl, avl != 1 ? "members" : "member");
03145
03146 while ((idx < avl) && (ch) && (ch != qe)) {
03147 if (!ch->pending)
03148 idx++;
03149 ch = ch->next;
03150 }
03151
03152 ao2_unlock(qe->parent);
03153
03154
03155
03156
03157 if (ch && idx < avl && (qe->parent->autofill || qe->pos == 1)) {
03158 ast_debug(1, "It's our turn (%s).\n", qe->chan->name);
03159 res = 1;
03160 } else {
03161 ast_debug(1, "It's not our turn (%s).\n", qe->chan->name);
03162 res = 0;
03163 }
03164
03165 return res;
03166 }
03167
03168
03169
03170
03171
03172
03173
03174 static void update_qe_rule(struct queue_ent *qe)
03175 {
03176 int max_penalty = qe->pr->max_relative ? qe->max_penalty + qe->pr->max_value : qe->pr->max_value;
03177 int min_penalty = qe->pr->min_relative ? qe->min_penalty + qe->pr->min_value : qe->pr->min_value;
03178 char max_penalty_str[20], min_penalty_str[20];
03179
03180 if (max_penalty < 0)
03181 max_penalty = 0;
03182 if (min_penalty < 0)
03183 min_penalty = 0;
03184 if (min_penalty > max_penalty)
03185 min_penalty = max_penalty;
03186 snprintf(max_penalty_str, sizeof(max_penalty_str), "%d", max_penalty);
03187 snprintf(min_penalty_str, sizeof(min_penalty_str), "%d", min_penalty);
03188 pbx_builtin_setvar_helper(qe->chan, "QUEUE_MAX_PENALTY", max_penalty_str);
03189 pbx_builtin_setvar_helper(qe->chan, "QUEUE_MIN_PENALTY", min_penalty_str);
03190 qe->max_penalty = max_penalty;
03191 qe->min_penalty = min_penalty;
03192 ast_debug(3, "Setting max penalty to %d and min penalty to %d for caller %s since %d seconds have elapsed\n", qe->max_penalty, qe->min_penalty, qe->chan->name, qe->pr->time);
03193 qe->pr = AST_LIST_NEXT(qe->pr, list);
03194 }
03195
03196
03197
03198
03199
03200
03201
03202
03203
03204
03205
03206 static int wait_our_turn(struct queue_ent *qe, int ringing, enum queue_result *reason)
03207 {
03208 int res = 0;
03209
03210
03211 for (;;) {
03212
03213 if (is_our_turn(qe))
03214 break;
03215
03216
03217 if (qe->expire && (time(NULL) >= qe->expire)) {
03218 *reason = QUEUE_TIMEOUT;
03219 break;
03220 }
03221
03222 if (qe->parent->leavewhenempty) {
03223 int status = 0;
03224
03225 if ((status = get_member_status(qe->parent, qe->max_penalty, qe->min_penalty, qe->parent->leavewhenempty))) {
03226 *reason = QUEUE_LEAVEEMPTY;
03227 ast_queue_log(qe->parent->name, qe->chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
03228 leave_queue(qe);
03229 break;
03230 }
03231 }
03232
03233
03234 if (qe->parent->announcefrequency &&
03235 (res = say_position(qe,ringing)))
03236 break;
03237
03238
03239 if (qe->expire && (time(NULL) >= qe->expire)) {
03240 *reason = QUEUE_TIMEOUT;
03241 break;
03242 }
03243
03244
03245 if (qe->parent->periodicannouncefrequency &&
03246 (res = say_periodic_announcement(qe,ringing)))
03247 break;
03248
03249
03250 while (qe->pr && ((time(NULL) - qe->start) >= qe->pr->time)) {
03251 update_qe_rule(qe);
03252 }
03253
03254
03255 if (qe->expire && (time(NULL) >= qe->expire)) {
03256 *reason = QUEUE_TIMEOUT;
03257 break;
03258 }
03259
03260
03261 if ((res = ast_waitfordigit(qe->chan, RECHECK * 1000))) {
03262 if (res > 0 && !valid_exit(qe, res))
03263 res = 0;
03264 else
03265 break;
03266 }
03267
03268
03269 if (qe->expire && (time(NULL) >= qe->expire)) {
03270 *reason = QUEUE_TIMEOUT;
03271 break;
03272 }
03273 }
03274
03275 return res;
03276 }
03277
03278
03279
03280
03281
03282 static int update_queue(struct call_queue *q, struct member *member, int callcompletedinsl, int newtalktime)
03283 {
03284 int oldtalktime;
03285
03286 struct member *mem;
03287 struct call_queue *qtmp;
03288 struct ao2_iterator queue_iter;
03289
03290 if (shared_lastcall) {
03291 queue_iter = ao2_iterator_init(queues, 0);
03292 while ((qtmp = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
03293 ao2_lock(qtmp);
03294 if ((mem = ao2_find(qtmp->members, member, OBJ_POINTER))) {
03295 time(&mem->lastcall);
03296 mem->calls++;
03297 mem->lastqueue = q;
03298 ao2_ref(mem, -1);
03299 }
03300 ao2_unlock(qtmp);
03301 queue_t_unref(qtmp, "Done with iterator");
03302 }
03303 ao2_iterator_destroy(&queue_iter);
03304 } else {
03305 ao2_lock(q);
03306 time(&member->lastcall);
03307 member->calls++;
03308 member->lastqueue = q;
03309 ao2_unlock(q);
03310 }
03311 ao2_lock(q);
03312 q->callscompleted++;
03313 if (callcompletedinsl)
03314 q->callscompletedinsl++;
03315
03316 oldtalktime = q->talktime;
03317 q->talktime = (((oldtalktime << 2) - oldtalktime) + newtalktime) >> 2;
03318 ao2_unlock(q);
03319 return 0;
03320 }
03321
03322
03323
03324
03325
03326
03327
03328
03329
03330 static int calc_metric(struct call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct callattempt *tmp)
03331 {
03332 if ((qe->max_penalty && (mem->penalty > qe->max_penalty)) || (qe->min_penalty && (mem->penalty < qe->min_penalty)))
03333 return -1;
03334
03335 switch (q->strategy) {
03336 case QUEUE_STRATEGY_RINGALL:
03337
03338 tmp->metric = mem->penalty * 1000000;
03339 break;
03340 case QUEUE_STRATEGY_LINEAR:
03341 if (pos < qe->linpos) {
03342 tmp->metric = 1000 + pos;
03343 } else {
03344 if (pos > qe->linpos)
03345
03346 qe->linwrapped = 1;
03347 tmp->metric = pos;
03348 }
03349 tmp->metric += mem->penalty * 1000000;
03350 break;
03351 case QUEUE_STRATEGY_RRMEMORY:
03352 if (pos < q->rrpos) {
03353 tmp->metric = 1000 + pos;
03354 } else {
03355 if (pos > q->rrpos)
03356
03357 q->wrapped = 1;
03358 tmp->metric = pos;
03359 }
03360 tmp->metric += mem->penalty * 1000000;
03361 break;
03362 case QUEUE_STRATEGY_RANDOM:
03363 tmp->metric = ast_random() % 1000;
03364 tmp->metric += mem->penalty * 1000000;
03365 break;
03366 case QUEUE_STRATEGY_WRANDOM:
03367 tmp->metric = ast_random() % ((1 + mem->penalty) * 1000);
03368 break;
03369 case QUEUE_STRATEGY_FEWESTCALLS:
03370 tmp->metric = mem->calls;
03371 tmp->metric += mem->penalty * 1000000;
03372 break;
03373 case QUEUE_STRATEGY_LEASTRECENT:
03374 if (!mem->lastcall)
03375 tmp->metric = 0;
03376 else
03377 tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
03378 tmp->metric += mem->penalty * 1000000;
03379 break;
03380 default:
03381 ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
03382 break;
03383 }
03384 return 0;
03385 }
03386
03387 enum agent_complete_reason {
03388 CALLER,
03389 AGENT,
03390 TRANSFER
03391 };
03392
03393
03394 static void send_agent_complete(const struct queue_ent *qe, const char *queuename,
03395 const struct ast_channel *peer, const struct member *member, time_t callstart,
03396 char *vars, size_t vars_len, enum agent_complete_reason rsn)
03397 {
03398 const char *reason = NULL;
03399
03400 if (!qe->parent->eventwhencalled)
03401 return;
03402
03403 switch (rsn) {
03404 case CALLER:
03405 reason = "caller";
03406 break;
03407 case AGENT:
03408 reason = "agent";
03409 break;
03410 case TRANSFER:
03411 reason = "transfer";
03412 break;
03413 }
03414
03415 manager_event(EVENT_FLAG_AGENT, "AgentComplete",
03416 "Queue: %s\r\n"
03417 "Uniqueid: %s\r\n"
03418 "Channel: %s\r\n"
03419 "Member: %s\r\n"
03420 "MemberName: %s\r\n"
03421 "HoldTime: %ld\r\n"
03422 "TalkTime: %ld\r\n"
03423 "Reason: %s\r\n"
03424 "%s",
03425 queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
03426 (long)(callstart - qe->start), (long)(time(NULL) - callstart), reason,
03427 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, vars_len) : "");
03428 }
03429
03430 struct queue_transfer_ds {
03431 struct queue_ent *qe;
03432 struct member *member;
03433 time_t starttime;
03434 int callcompletedinsl;
03435 };
03436
03437 static void queue_transfer_destroy(void *data)
03438 {
03439 struct queue_transfer_ds *qtds = data;
03440 ast_free(qtds);
03441 }
03442
03443
03444
03445 static const struct ast_datastore_info queue_transfer_info = {
03446 .type = "queue_transfer",
03447 .chan_fixup = queue_transfer_fixup,
03448 .destroy = queue_transfer_destroy,
03449 };
03450
03451
03452
03453
03454
03455
03456
03457
03458
03459
03460 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
03461 {
03462 struct queue_transfer_ds *qtds = data;
03463 struct queue_ent *qe = qtds->qe;
03464 struct member *member = qtds->member;
03465 time_t callstart = qtds->starttime;
03466 int callcompletedinsl = qtds->callcompletedinsl;
03467 struct ast_datastore *datastore;
03468
03469 ast_queue_log(qe->parent->name, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
03470 new_chan->exten, new_chan->context, (long) (callstart - qe->start),
03471 (long) (time(NULL) - callstart), qe->opos);
03472
03473 update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
03474
03475
03476 if ((datastore = ast_channel_datastore_find(old_chan, &queue_transfer_info, NULL))) {
03477 ast_channel_datastore_remove(old_chan, datastore);
03478 } else {
03479 ast_log(LOG_WARNING, "Can't find the queue_transfer datastore.\n");
03480 }
03481 }
03482
03483
03484
03485
03486
03487
03488
03489
03490
03491 static int attended_transfer_occurred(struct ast_channel *chan)
03492 {
03493 return ast_channel_datastore_find(chan, &queue_transfer_info, NULL) ? 0 : 1;
03494 }
03495
03496
03497
03498 static struct ast_datastore *setup_transfer_datastore(struct queue_ent *qe, struct member *member, time_t starttime, int callcompletedinsl)
03499 {
03500 struct ast_datastore *ds;
03501 struct queue_transfer_ds *qtds = ast_calloc(1, sizeof(*qtds));
03502
03503 if (!qtds) {
03504 ast_log(LOG_WARNING, "Memory allocation error!\n");
03505 return NULL;
03506 }
03507
03508 ast_channel_lock(qe->chan);
03509 if (!(ds = ast_datastore_alloc(&queue_transfer_info, NULL))) {
03510 ast_channel_unlock(qe->chan);
03511 ast_log(LOG_WARNING, "Unable to create transfer datastore. queue_log will not show attended transfer\n");
03512 return NULL;
03513 }
03514
03515 qtds->qe = qe;
03516
03517 qtds->member = member;
03518 qtds->starttime = starttime;
03519 qtds->callcompletedinsl = callcompletedinsl;
03520 ds->data = qtds;
03521 ast_channel_datastore_add(qe->chan, ds);
03522 ast_channel_unlock(qe->chan);
03523 return ds;
03524 }
03525
03526 struct queue_end_bridge {
03527 struct call_queue *q;
03528 struct ast_channel *chan;
03529 };
03530
03531 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
03532 {
03533 struct queue_end_bridge *qeb = bconfig->end_bridge_callback_data;
03534 ao2_ref(qeb, +1);
03535 qeb->chan = originator;
03536 }
03537
03538 static void end_bridge_callback(void *data)
03539 {
03540 struct queue_end_bridge *qeb = data;
03541 struct call_queue *q = qeb->q;
03542 struct ast_channel *chan = qeb->chan;
03543
03544 if (ao2_ref(qeb, -1) == 1) {
03545 ao2_lock(q);
03546 set_queue_variables(q, chan);
03547 ao2_unlock(q);
03548
03549 queue_t_unref(q, "Expire bridge_config reference");
03550 }
03551 }
03552
03553
03554
03555
03556
03557
03558
03559
03560
03561
03562
03563
03564
03565
03566
03567
03568
03569
03570
03571
03572
03573
03574
03575
03576
03577
03578
03579
03580 static int try_calling(struct queue_ent *qe, const char *options, char *announceoverride, const char *url, int *tries, int *noption, const char *agi, const char *macro, const char *gosub, int ringing)
03581 {
03582 struct member *cur;
03583 struct callattempt *outgoing = NULL;
03584 int to, orig;
03585 char oldexten[AST_MAX_EXTENSION]="";
03586 char oldcontext[AST_MAX_CONTEXT]="";
03587 char queuename[256]="";
03588 char interfacevar[256]="";
03589 struct ast_channel *peer;
03590 struct ast_channel *which;
03591 struct callattempt *lpeer;
03592 struct member *member;
03593 struct ast_app *application;
03594 int res = 0, bridge = 0;
03595 int numbusies = 0;
03596 int x=0;
03597 char *announce = NULL;
03598 char digit = 0;
03599 time_t callstart;
03600 time_t now = time(NULL);
03601 struct ast_bridge_config bridge_config;
03602 char nondataquality = 1;
03603 char *agiexec = NULL;
03604 char *macroexec = NULL;
03605 char *gosubexec = NULL;
03606 int ret = 0;
03607 const char *monitorfilename;
03608 const char *monitor_exec;
03609 const char *monitor_options;
03610 char tmpid[256], tmpid2[256];
03611 char meid[1024], meid2[1024];
03612 char mixmonargs[1512];
03613 struct ast_app *mixmonapp = NULL;
03614 char *p;
03615 char vars[2048];
03616 int forwardsallowed = 1;
03617 int callcompletedinsl;
03618 struct ao2_iterator memi;
03619 struct ast_datastore *datastore, *transfer_ds;
03620 struct queue_end_bridge *queue_end_bridge = NULL;
03621 const int need_weight = use_weight;
03622
03623 ast_channel_lock(qe->chan);
03624 datastore = ast_channel_datastore_find(qe->chan, &dialed_interface_info, NULL);
03625 ast_channel_unlock(qe->chan);
03626
03627 memset(&bridge_config, 0, sizeof(bridge_config));
03628 tmpid[0] = 0;
03629 meid[0] = 0;
03630 time(&now);
03631
03632
03633
03634
03635
03636 if (qe->expire && now >= qe->expire) {
03637 res = 0;
03638 goto out;
03639 }
03640
03641 for (; options && *options; options++)
03642 switch (*options) {
03643 case 't':
03644 ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_REDIRECT);
03645 break;
03646 case 'T':
03647 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_REDIRECT);
03648 break;
03649 case 'w':
03650 ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMON);
03651 break;
03652 case 'W':
03653 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMON);
03654 break;
03655 case 'c':
03656 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_NO_H_EXTEN);
03657 break;
03658 case 'd':
03659 nondataquality = 0;
03660 break;
03661 case 'h':
03662 ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_DISCONNECT);
03663 break;
03664 case 'H':
03665 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT);
03666 break;
03667 case 'k':
03668 ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_PARKCALL);
03669 break;
03670 case 'K':
03671 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_PARKCALL);
03672 break;
03673 case 'n':
03674 if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY || qe->parent->strategy == QUEUE_STRATEGY_LINEAR)
03675 (*tries)++;
03676 else
03677 *tries = qe->parent->membercount;
03678 *noption = 1;
03679 break;
03680 case 'i':
03681 forwardsallowed = 0;
03682 break;
03683 case 'x':
03684 ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMIXMON);
03685 break;
03686 case 'X':
03687 ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMIXMON);
03688 break;
03689 case 'C':
03690 qe->cancel_answered_elsewhere = 1;
03691 break;
03692
03693 }
03694
03695
03696
03697
03698 if (ast_test_flag(qe->chan, AST_FLAG_ANSWERED_ELSEWHERE)) {
03699 qe->cancel_answered_elsewhere = 1;
03700 }
03701
03702
03703 if (need_weight)
03704 ao2_lock(queues);
03705 ao2_lock(qe->parent);
03706 ast_debug(1, "%s is trying to call a queue member.\n",
03707 qe->chan->name);
03708 ast_copy_string(queuename, qe->parent->name, sizeof(queuename));
03709 if (!ast_strlen_zero(qe->announce))
03710 announce = qe->announce;
03711 if (!ast_strlen_zero(announceoverride))
03712 announce = announceoverride;
03713
03714 memi = ao2_iterator_init(qe->parent->members, 0);
03715 while ((cur = ao2_iterator_next(&memi))) {
03716 struct callattempt *tmp = ast_calloc(1, sizeof(*tmp));
03717 struct ast_dialed_interface *di;
03718 AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
03719 if (!tmp) {
03720 ao2_ref(cur, -1);
03721 ao2_unlock(qe->parent);
03722 ao2_iterator_destroy(&memi);
03723 if (need_weight)
03724 ao2_unlock(queues);
03725 goto out;
03726 }
03727 if (!datastore) {
03728 if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
03729 ao2_ref(cur, -1);
03730 ao2_unlock(qe->parent);
03731 ao2_iterator_destroy(&memi);
03732 if (need_weight)
03733 ao2_unlock(queues);
03734 free(tmp);
03735 goto out;
03736 }
03737 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
03738 if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
03739 ao2_ref(cur, -1);
03740 ao2_unlock(&qe->parent);
03741 ao2_iterator_destroy(&memi);
03742 if (need_weight)
03743 ao2_unlock(queues);
03744 free(tmp);
03745 goto out;
03746 }
03747 datastore->data = dialed_interfaces;
03748 AST_LIST_HEAD_INIT(dialed_interfaces);
03749
03750 ast_channel_lock(qe->chan);
03751 ast_channel_datastore_add(qe->chan, datastore);
03752 ast_channel_unlock(qe->chan);
03753 } else
03754 dialed_interfaces = datastore->data;
03755
03756 AST_LIST_LOCK(dialed_interfaces);
03757 AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
03758 if (!strcasecmp(cur->interface, di->interface)) {
03759 ast_debug(1, "Skipping dialing interface '%s' since it has already been dialed\n",
03760 di->interface);
03761 break;
03762 }
03763 }
03764 AST_LIST_UNLOCK(dialed_interfaces);
03765
03766 if (di) {
03767 free(tmp);
03768 continue;
03769 }
03770
03771
03772
03773
03774
03775 if (strncasecmp(cur->interface, "Local/", 6)) {
03776 if (!(di = ast_calloc(1, sizeof(*di) + strlen(cur->interface)))) {
03777 ao2_ref(cur, -1);
03778 ao2_unlock(qe->parent);
03779 ao2_iterator_destroy(&memi);
03780 if (need_weight)
03781 ao2_unlock(queues);
03782 free(tmp);
03783 goto out;
03784 }
03785 strcpy(di->interface, cur->interface);
03786
03787 AST_LIST_LOCK(dialed_interfaces);
03788 AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
03789 AST_LIST_UNLOCK(dialed_interfaces);
03790 }
03791
03792 tmp->stillgoing = -1;
03793 tmp->member = cur;
03794 tmp->oldstatus = cur->status;
03795 tmp->lastcall = cur->lastcall;
03796 tmp->lastqueue = cur->lastqueue;
03797 ast_copy_string(tmp->interface, cur->interface, sizeof(tmp->interface));
03798
03799
03800 if (!calc_metric(qe->parent, cur, x++, qe, tmp)) {
03801
03802
03803
03804 tmp->q_next = outgoing;
03805 outgoing = tmp;
03806
03807 if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
03808 break;
03809 } else {
03810 ao2_ref(cur, -1);
03811 ast_free(tmp);
03812 }
03813 }
03814 ao2_iterator_destroy(&memi);
03815
03816 if (qe->parent->timeoutpriority == TIMEOUT_PRIORITY_APP) {
03817
03818 if (qe->expire && (!qe->parent->timeout || (qe->expire - now) <= qe->parent->timeout))
03819 to = (qe->expire - now) * 1000;
03820 else
03821 to = (qe->parent->timeout) ? qe->parent->timeout * 1000 : -1;
03822 } else {
03823
03824 if (qe->expire && qe->expire<=now) {
03825 to = 0;
03826 } else if (qe->parent->timeout) {
03827 to = qe->parent->timeout * 1000;
03828 } else {
03829 to = -1;
03830 }
03831 }
03832 orig = to;
03833 ++qe->pending;
03834 ao2_unlock(qe->parent);
03835 ring_one(qe, outgoing, &numbusies);
03836 if (need_weight)
03837 ao2_unlock(queues);
03838 lpeer = wait_for_answer(qe, outgoing, &to, &digit, numbusies, ast_test_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT), forwardsallowed);
03839
03840
03841
03842
03843
03844
03845 ast_channel_lock(qe->chan);
03846 if (datastore && !ast_channel_datastore_remove(qe->chan, datastore)) {
03847 ast_datastore_free(datastore);
03848 }
03849 ast_channel_unlock(qe->chan);
03850 ao2_lock(qe->parent);
03851 if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY) {
03852 store_next_rr(qe, outgoing);
03853 }
03854 if (qe->parent->strategy == QUEUE_STRATEGY_LINEAR) {
03855 store_next_lin(qe, outgoing);
03856 }
03857 ao2_unlock(qe->parent);
03858 peer = lpeer ? lpeer->chan : NULL;
03859 if (!peer) {
03860 qe->pending = 0;
03861 if (to) {
03862
03863 res = -1;
03864 } else {
03865
03866 res = digit;
03867 }
03868 if (res == -1)
03869 ast_debug(1, "%s: Nobody answered.\n", qe->chan->name);
03870 if (ast_cdr_isset_unanswered()) {
03871
03872
03873 struct callattempt *o;
03874 for (o = outgoing; o; o = o->q_next) {
03875 if (!o->chan) {
03876 continue;
03877 }
03878 if (strcmp(o->chan->cdr->dstchannel, qe->chan->cdr->dstchannel) == 0) {
03879 ast_set_flag(o->chan->cdr, AST_CDR_FLAG_POST_DISABLED);
03880 break;
03881 }
03882 }
03883 }
03884 } else {
03885
03886
03887
03888 if (!strcmp(qe->chan->tech->type, "DAHDI"))
03889 ast_channel_setoption(qe->chan, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
03890 if (!strcmp(peer->tech->type, "DAHDI"))
03891 ast_channel_setoption(peer, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
03892
03893 time(&now);
03894 recalc_holdtime(qe, (now - qe->start));
03895 ao2_lock(qe->parent);
03896 callcompletedinsl = ((now - qe->start) <= qe->parent->servicelevel);
03897 ao2_unlock(qe->parent);
03898 member = lpeer->member;
03899
03900 ao2_ref(member, 1);
03901 hangupcalls(outgoing, peer, qe->cancel_answered_elsewhere);
03902 outgoing = NULL;
03903 if (announce || qe->parent->reportholdtime || qe->parent->memberdelay) {
03904 int res2;
03905
03906 res2 = ast_autoservice_start(qe->chan);
03907 if (!res2) {
03908 if (qe->parent->memberdelay) {
03909 ast_log(LOG_NOTICE, "Delaying member connect for %d seconds\n", qe->parent->memberdelay);
03910 res2 |= ast_safe_sleep(peer, qe->parent->memberdelay * 1000);
03911 }
03912 if (!res2 && announce) {
03913 play_file(peer, announce);
03914 }
03915 if (!res2 && qe->parent->reportholdtime) {
03916 if (!play_file(peer, qe->parent->sound_reporthold)) {
03917 int holdtime, holdtimesecs;
03918
03919 time(&now);
03920 holdtime = abs((now - qe->start) / 60);
03921 holdtimesecs = abs((now - qe->start) % 60);
03922 if (holdtime > 0) {
03923 ast_say_number(peer, holdtime, AST_DIGIT_ANY, peer->language, NULL);
03924 play_file(peer, qe->parent->sound_minutes);
03925 }
03926 if (holdtimesecs > 1) {
03927 ast_say_number(peer, holdtimesecs, AST_DIGIT_ANY, peer->language, NULL);
03928 play_file(peer, qe->parent->sound_seconds);
03929 }
03930 }
03931 }
03932 }
03933 res2 |= ast_autoservice_stop(qe->chan);
03934 if (ast_check_hangup(peer)) {
03935
03936 ast_log(LOG_WARNING, "Agent on %s hungup on the customer.\n", peer->name);
03937 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "AGENTDUMP", "%s", "");
03938 if (qe->parent->eventwhencalled)
03939 manager_event(EVENT_FLAG_AGENT, "AgentDump",
03940 "Queue: %s\r\n"
03941 "Uniqueid: %s\r\n"
03942 "Channel: %s\r\n"
03943 "Member: %s\r\n"
03944 "MemberName: %s\r\n"
03945 "%s",
03946 queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
03947 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
03948 ast_hangup(peer);
03949 ao2_ref(member, -1);
03950 goto out;
03951 } else if (res2) {
03952
03953 ast_log(LOG_NOTICE, "Caller was about to talk to agent on %s but the caller hungup.\n", peer->name);
03954 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "ABANDON", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
03955 record_abandoned(qe);
03956 ast_hangup(peer);
03957 ao2_ref(member, -1);
03958 return -1;
03959 }
03960 }
03961
03962 if (ringing)
03963 ast_indicate(qe->chan,-1);
03964 else
03965 ast_moh_stop(qe->chan);
03966
03967 if (qe->chan->cdr)
03968 ast_cdr_setdestchan(qe->chan->cdr, peer->name);
03969
03970 res = ast_channel_make_compatible(qe->chan, peer);
03971 if (res < 0) {
03972 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "SYSCOMPAT", "%s", "");
03973 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
03974 record_abandoned(qe);
03975 ast_cdr_failed(qe->chan->cdr);
03976 ast_hangup(peer);
03977 ao2_ref(member, -1);
03978 return -1;
03979 }
03980
03981
03982 if (!ast_strlen_zero(qe->parent->sound_callerannounce)) {
03983 if (play_file(qe->chan, qe->parent->sound_callerannounce))
03984 ast_log(LOG_WARNING, "Announcement file '%s' is unavailable, continuing anyway...\n", qe->parent->sound_callerannounce);
03985 }
03986
03987 ao2_lock(qe->parent);
03988
03989
03990 if (qe->parent->setinterfacevar) {
03991 snprintf(interfacevar, sizeof(interfacevar), "MEMBERINTERFACE=%s,MEMBERNAME=%s,MEMBERCALLS=%d,MEMBERLASTCALL=%ld,MEMBERPENALTY=%d,MEMBERDYNAMIC=%d,MEMBERREALTIME=%d",
03992 member->interface, member->membername, member->calls, (long)member->lastcall, member->penalty, member->dynamic, member->realtime);
03993 pbx_builtin_setvar_multiple(qe->chan, interfacevar);
03994 pbx_builtin_setvar_multiple(peer, interfacevar);
03995 }
03996
03997
03998
03999 if (qe->parent->setqueueentryvar) {
04000 snprintf(interfacevar, sizeof(interfacevar), "QEHOLDTIME=%ld,QEORIGINALPOS=%d",
04001 (long) time(NULL) - qe->start, qe->opos);
04002 pbx_builtin_setvar_multiple(qe->chan, interfacevar);
04003 pbx_builtin_setvar_multiple(peer, interfacevar);
04004 }
04005
04006
04007 set_queue_variables(qe->parent, qe->chan);
04008 set_queue_variables(qe->parent, peer);
04009 ao2_unlock(qe->parent);
04010
04011 ast_channel_lock(qe->chan);
04012 if ((monitorfilename = pbx_builtin_getvar_helper(qe->chan, "MONITOR_FILENAME"))) {
04013 monitorfilename = ast_strdupa(monitorfilename);
04014 }
04015 ast_channel_unlock(qe->chan);
04016
04017 if (qe->parent->monfmt && *qe->parent->monfmt) {
04018 if (!qe->parent->montype) {
04019 const char *monexec, *monargs;
04020 ast_debug(1, "Starting Monitor as requested.\n");
04021 ast_channel_lock(qe->chan);
04022 if ((monexec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC")) || (monargs = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC_ARGS"))) {
04023 which = qe->chan;
04024 monexec = monexec ? ast_strdupa(monexec) : NULL;
04025 }
04026 else
04027 which = peer;
04028 ast_channel_unlock(qe->chan);
04029 if (ast_monitor_start) {
04030 if (monitorfilename) {
04031 ast_monitor_start(which, qe->parent->monfmt, monitorfilename, 1, X_REC_IN | X_REC_OUT);
04032 } else if (qe->chan->cdr && ast_monitor_start) {
04033 ast_monitor_start(which, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1, X_REC_IN | X_REC_OUT);
04034 } else if (ast_monitor_start) {
04035
04036 snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
04037 ast_monitor_start(which, qe->parent->monfmt, tmpid, 1, X_REC_IN | X_REC_OUT);
04038 }
04039 }
04040 if (!ast_strlen_zero(monexec) && ast_monitor_setjoinfiles) {
04041 ast_monitor_setjoinfiles(which, 1);
04042 }
04043 } else {
04044 mixmonapp = pbx_findapp("MixMonitor");
04045
04046 if (mixmonapp) {
04047 ast_debug(1, "Starting MixMonitor as requested.\n");
04048 if (!monitorfilename) {
04049 if (qe->chan->cdr)
04050 ast_copy_string(tmpid, qe->chan->cdr->uniqueid, sizeof(tmpid));
04051 else
04052 snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
04053 } else {
04054 const char *m = monitorfilename;
04055 for (p = tmpid2; p < tmpid2 + sizeof(tmpid2) - 1; p++, m++) {
04056 switch (*m) {
04057 case '^':
04058 if (*(m + 1) == '{')
04059 *p = '$';
04060 break;
04061 case ',':
04062 *p++ = '\\';
04063
04064 default:
04065 *p = *m;
04066 }
04067 if (*m == '\0')
04068 break;
04069 }
04070 if (p == tmpid2 + sizeof(tmpid2))
04071 tmpid2[sizeof(tmpid2) - 1] = '\0';
04072
04073 pbx_substitute_variables_helper(qe->chan, tmpid2, tmpid, sizeof(tmpid) - 1);
04074 }
04075
04076 ast_channel_lock(qe->chan);
04077 if ((monitor_exec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC"))) {
04078 monitor_exec = ast_strdupa(monitor_exec);
04079 }
04080 if ((monitor_options = pbx_builtin_getvar_helper(qe->chan, "MONITOR_OPTIONS"))) {
04081 monitor_options = ast_strdupa(monitor_options);
04082 } else {
04083 monitor_options = "";
04084 }
04085 ast_channel_unlock(qe->chan);
04086
04087 if (monitor_exec) {
04088 const char *m = monitor_exec;
04089 for (p = meid2; p < meid2 + sizeof(meid2) - 1; p++, m++) {
04090 switch (*m) {
04091 case '^':
04092 if (*(m + 1) == '{')
04093 *p = '$';
04094 break;
04095 case ',':
04096 *p++ = '\\';
04097
04098 default:
04099 *p = *m;
04100 }
04101 if (*m == '\0')
04102 break;
04103 }
04104 if (p == meid2 + sizeof(meid2))
04105 meid2[sizeof(meid2) - 1] = '\0';
04106
04107 pbx_substitute_variables_helper(qe->chan, meid2, meid, sizeof(meid) - 1);
04108 }
04109
04110 snprintf(tmpid2, sizeof(tmpid2), "%s.%s", tmpid, qe->parent->monfmt);
04111
04112 if (!ast_strlen_zero(monitor_exec))
04113 snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s,%s", tmpid2, monitor_options, monitor_exec);
04114 else
04115 snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s", tmpid2, monitor_options);
04116
04117 ast_debug(1, "Arguments being passed to MixMonitor: %s\n", mixmonargs);
04118
04119 if (qe->chan->cdr)
04120 ast_set_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
04121 ret = pbx_exec(qe->chan, mixmonapp, mixmonargs);
04122 if (qe->chan->cdr)
04123 ast_clear_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
04124
04125 } else {
04126 ast_log(LOG_WARNING, "Asked to run MixMonitor on this call, but cannot find the MixMonitor app!\n");
04127 }
04128 }
04129 }
04130
04131 leave_queue(qe);
04132 if (!ast_strlen_zero(url) && ast_channel_supports_html(peer)) {
04133 ast_debug(1, "app_queue: sendurl=%s.\n", url);
04134 ast_channel_sendurl(peer, url);
04135 }
04136
04137
04138
04139 if (!ast_strlen_zero(macro)) {
04140 macroexec = ast_strdupa(macro);
04141 } else {
04142 if (qe->parent->membermacro)
04143 macroexec = ast_strdupa(qe->parent->membermacro);
04144 }
04145
04146 if (!ast_strlen_zero(macroexec)) {
04147 ast_debug(1, "app_queue: macro=%s.\n", macroexec);
04148
04149 res = ast_autoservice_start(qe->chan);
04150 if (res) {
04151 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
04152 res = -1;
04153 }
04154
04155 application = pbx_findapp("Macro");
04156
04157 if (application) {
04158 res = pbx_exec(peer, application, macroexec);
04159 ast_debug(1, "Macro exited with status %d\n", res);
04160 res = 0;
04161 } else {
04162 ast_log(LOG_ERROR, "Could not find application Macro\n");
04163 res = -1;
04164 }
04165
04166 if (ast_autoservice_stop(qe->chan) < 0) {
04167 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
04168 res = -1;
04169 }
04170 }
04171
04172
04173
04174 if (!ast_strlen_zero(gosub)) {
04175 gosubexec = ast_strdupa(gosub);
04176 } else {
04177 if (qe->parent->membergosub)
04178 gosubexec = ast_strdupa(qe->parent->membergosub);
04179 }
04180
04181 if (!ast_strlen_zero(gosubexec)) {
04182 ast_debug(1, "app_queue: gosub=%s.\n", gosubexec);
04183
04184 res = ast_autoservice_start(qe->chan);
04185 if (res) {
04186 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
04187 res = -1;
04188 }
04189
04190 application = pbx_findapp("Gosub");
04191
04192 if (application) {
04193 char *gosub_args, *gosub_argstart;
04194
04195
04196 ast_copy_string(peer->context, "app_queue_gosub_virtual_context", sizeof(peer->context));
04197 ast_copy_string(peer->exten, "s", sizeof(peer->exten));
04198 peer->priority = 0;
04199
04200 gosub_argstart = strchr(gosubexec, ',');
04201 if (gosub_argstart) {
04202 *gosub_argstart = 0;
04203 if (asprintf(&gosub_args, "%s,s,1(%s)", gosubexec, gosub_argstart + 1) < 0) {
04204 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
04205 gosub_args = NULL;
04206 }
04207 *gosub_argstart = ',';
04208 } else {
04209 if (asprintf(&gosub_args, "%s,s,1", gosubexec) < 0) {
04210 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
04211 gosub_args = NULL;
04212 }
04213 }
04214 if (gosub_args) {
04215 res = pbx_exec(peer, application, gosub_args);
04216 if (!res) {
04217 struct ast_pbx_args args;
04218 memset(&args, 0, sizeof(args));
04219 args.no_hangup_chan = 1;
04220 ast_pbx_run_args(peer, &args);
04221 }
04222 ast_free(gosub_args);
04223 ast_debug(1, "Gosub exited with status %d\n", res);
04224 } else {
04225 ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
04226 }
04227 } else {
04228 ast_log(LOG_ERROR, "Could not find application Gosub\n");
04229 res = -1;
04230 }
04231
04232 if (ast_autoservice_stop(qe->chan) < 0) {
04233 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
04234 res = -1;
04235 }
04236 }
04237
04238 if (!ast_strlen_zero(agi)) {
04239 ast_debug(1, "app_queue: agi=%s.\n", agi);
04240 application = pbx_findapp("agi");
04241 if (application) {
04242 agiexec = ast_strdupa(agi);
04243 ret = pbx_exec(qe->chan, application, agiexec);
04244 } else
04245 ast_log(LOG_WARNING, "Asked to execute an AGI on this channel, but could not find application (agi)!\n");
04246 }
04247 qe->handled++;
04248 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "CONNECT", "%ld|%s|%ld", (long) time(NULL) - qe->start, peer->uniqueid,
04249 (long)(orig - to > 0 ? (orig - to) / 1000 : 0));
04250 if (update_cdr && qe->chan->cdr)
04251 ast_copy_string(qe->chan->cdr->dstchannel, member->membername, sizeof(qe->chan->cdr->dstchannel));
04252 if (qe->parent->eventwhencalled)
04253 manager_event(EVENT_FLAG_AGENT, "AgentConnect",
04254 "Queue: %s\r\n"
04255 "Uniqueid: %s\r\n"
04256 "Channel: %s\r\n"
04257 "Member: %s\r\n"
04258 "MemberName: %s\r\n"
04259 "Holdtime: %ld\r\n"
04260 "BridgedChannel: %s\r\n"
04261 "Ringtime: %ld\r\n"
04262 "%s",
04263 queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
04264 (long) time(NULL) - qe->start, peer->uniqueid, (long)(orig - to > 0 ? (orig - to) / 1000 : 0),
04265 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
04266 ast_copy_string(oldcontext, qe->chan->context, sizeof(oldcontext));
04267 ast_copy_string(oldexten, qe->chan->exten, sizeof(oldexten));
04268
04269 if ((queue_end_bridge = ao2_alloc(sizeof(*queue_end_bridge), NULL))) {
04270 queue_end_bridge->q = qe->parent;
04271 queue_end_bridge->chan = qe->chan;
04272 bridge_config.end_bridge_callback = end_bridge_callback;
04273 bridge_config.end_bridge_callback_data = queue_end_bridge;
04274 bridge_config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
04275
04276
04277
04278
04279 queue_t_ref(qe->parent, "For bridge_config reference");
04280 }
04281
04282 time(&callstart);
04283 transfer_ds = setup_transfer_datastore(qe, member, callstart, callcompletedinsl);
04284 bridge = ast_bridge_call(qe->chan,peer, &bridge_config);
04285
04286
04287
04288
04289
04290 ast_channel_lock(qe->chan);
04291 if (!attended_transfer_occurred(qe->chan)) {
04292 struct ast_datastore *tds;
04293
04294
04295 if (!(qe->chan->_softhangup | peer->_softhangup) && (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten))) {
04296 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
04297 qe->chan->exten, qe->chan->context, (long) (callstart - qe->start),
04298 (long) (time(NULL) - callstart), qe->opos);
04299 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
04300 } else if (ast_check_hangup(qe->chan)) {
04301 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETECALLER", "%ld|%ld|%d",
04302 (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
04303 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), CALLER);
04304 } else {
04305 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETEAGENT", "%ld|%ld|%d",
04306 (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
04307 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), AGENT);
04308 }
04309 if ((tds = ast_channel_datastore_find(qe->chan, &queue_transfer_info, NULL))) {
04310 ast_channel_datastore_remove(qe->chan, tds);
04311 }
04312 update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
04313 } else {
04314
04315 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
04316 }
04317
04318 if (transfer_ds) {
04319 ast_datastore_free(transfer_ds);
04320 }
04321 ast_channel_unlock(qe->chan);
04322 ast_hangup(peer);
04323 res = bridge ? bridge : 1;
04324 ao2_ref(member, -1);
04325 }
04326 out:
04327 hangupcalls(outgoing, NULL, qe->cancel_answered_elsewhere);
04328
04329 return res;
04330 }
04331
04332 static int wait_a_bit(struct queue_ent *qe)
04333 {
04334
04335 int retrywait = qe->parent->retry * 1000;
04336
04337 int res = ast_waitfordigit(qe->chan, retrywait);
04338 if (res > 0 && !valid_exit(qe, res))
04339 res = 0;
04340
04341 return res;
04342 }
04343
04344 static struct member *interface_exists(struct call_queue *q, const char *interface)
04345 {
04346 struct member *mem;
04347 struct ao2_iterator mem_iter;
04348
04349 if (!q)
04350 return NULL;
04351
04352 mem_iter = ao2_iterator_init(q->members, 0);
04353 while ((mem = ao2_iterator_next(&mem_iter))) {
04354 if (!strcasecmp(interface, mem->interface)) {
04355 ao2_iterator_destroy(&mem_iter);
04356 return mem;
04357 }
04358 ao2_ref(mem, -1);
04359 }
04360 ao2_iterator_destroy(&mem_iter);
04361
04362 return NULL;
04363 }
04364
04365
04366
04367
04368
04369
04370 static void dump_queue_members(struct call_queue *pm_queue)
04371 {
04372 struct member *cur_member;
04373 char value[PM_MAX_LEN];
04374 int value_len = 0;
04375 int res;
04376 struct ao2_iterator mem_iter;
04377
04378 memset(value, 0, sizeof(value));
04379
04380 if (!pm_queue)
04381 return;
04382
04383 mem_iter = ao2_iterator_init(pm_queue->members, 0);
04384 while ((cur_member = ao2_iterator_next(&mem_iter))) {
04385 if (!cur_member->dynamic) {
04386 ao2_ref(cur_member, -1);
04387 continue;
04388 }
04389
04390 res = snprintf(value + value_len, sizeof(value) - value_len, "%s%s;%d;%d;%s;%s",
04391 value_len ? "|" : "", cur_member->interface, cur_member->penalty, cur_member->paused, cur_member->membername, cur_member->state_interface);
04392
04393 ao2_ref(cur_member, -1);
04394
04395 if (res != strlen(value + value_len)) {
04396 ast_log(LOG_WARNING, "Could not create persistent member string, out of space\n");
04397 break;
04398 }
04399 value_len += res;
04400 }
04401 ao2_iterator_destroy(&mem_iter);
04402
04403 if (value_len && !cur_member) {
04404 if (ast_db_put(pm_family, pm_queue->name, value))
04405 ast_log(LOG_WARNING, "failed to create persistent dynamic entry!\n");
04406 } else
04407
04408 ast_db_del(pm_family, pm_queue->name);
04409 }
04410
04411
04412
04413
04414
04415
04416
04417 static int remove_from_queue(const char *queuename, const char *interface)
04418 {
04419 struct call_queue *q, tmpq;
04420 struct member *mem, tmpmem;
04421 int res = RES_NOSUCHQUEUE;
04422
04423 ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
04424 ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
04425 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Temporary reference for interface removal"))) {
04426 ao2_lock(queues);
04427 ao2_lock(q);
04428 if ((mem = ao2_find(q->members, &tmpmem, OBJ_POINTER))) {
04429
04430 if (!mem->dynamic) {
04431 ao2_ref(mem, -1);
04432 ao2_unlock(q);
04433 queue_t_unref(q, "Interface wasn't dynamic, expiring temporary reference");
04434 ao2_unlock(queues);
04435 return RES_NOT_DYNAMIC;
04436 }
04437 q->membercount--;
04438 manager_event(EVENT_FLAG_AGENT, "QueueMemberRemoved",
04439 "Queue: %s\r\n"
04440 "Location: %s\r\n"
04441 "MemberName: %s\r\n",
04442 q->name, mem->interface, mem->membername);
04443 ao2_unlink(q->members, mem);
04444 ao2_ref(mem, -1);
04445
04446 if (queue_persistent_members)
04447 dump_queue_members(q);
04448
04449 res = RES_OKAY;
04450 } else {
04451 res = RES_EXISTS;
04452 }
04453 ao2_unlock(q);
04454 ao2_unlock(queues);
04455 queue_t_unref(q, "Expiring temporary reference");
04456 }
04457
04458 return res;
04459 }
04460
04461
04462
04463
04464
04465
04466
04467
04468 static int add_to_queue(const char *queuename, const char *interface, const char *membername, int penalty, int paused, int dump, const char *state_interface)
04469 {
04470 struct call_queue *q;
04471 struct member *new_member, *old_member;
04472 int res = RES_NOSUCHQUEUE;
04473
04474
04475
04476 if (!(q = load_realtime_queue(queuename)))
04477 return res;
04478
04479 ao2_lock(queues);
04480
04481 ao2_lock(q);
04482 if ((old_member = interface_exists(q, interface)) == NULL) {
04483 if ((new_member = create_queue_member(interface, membername, penalty, paused, state_interface))) {
04484 new_member->dynamic = 1;
04485 ao2_link(q->members, new_member);
04486 q->membercount++;
04487 manager_event(EVENT_FLAG_AGENT, "QueueMemberAdded",
04488 "Queue: %s\r\n"
04489 "Location: %s\r\n"
04490 "MemberName: %s\r\n"
04491 "Membership: %s\r\n"
04492 "Penalty: %d\r\n"
04493 "CallsTaken: %d\r\n"
04494 "LastCall: %d\r\n"
04495 "Status: %d\r\n"
04496 "Paused: %d\r\n",
04497 q->name, new_member->interface, new_member->membername,
04498 "dynamic",
04499 new_member->penalty, new_member->calls, (int) new_member->lastcall,
04500 new_member->status, new_member->paused);
04501
04502 ao2_ref(new_member, -1);
04503 new_member = NULL;
04504
04505 if (dump)
04506 dump_queue_members(q);
04507
04508 res = RES_OKAY;
04509 } else {
04510 res = RES_OUTOFMEMORY;
04511 }
04512 } else {
04513 ao2_ref(old_member, -1);
04514 res = RES_EXISTS;
04515 }
04516 ao2_unlock(q);
04517 ao2_unlock(queues);
04518
04519 return res;
04520 }
04521
04522 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused)
04523 {
04524 int found = 0;
04525 struct call_queue *q;
04526 struct member *mem;
04527 struct ao2_iterator queue_iter;
04528 int failed;
04529
04530
04531
04532 if (ast_strlen_zero(queuename))
04533 ast_queue_log("NONE", "NONE", interface, (paused ? "PAUSEALL" : "UNPAUSEALL"), "%s", "");
04534
04535 queue_iter = ao2_iterator_init(queues, 0);
04536 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate over queues"))) {
04537 ao2_lock(q);
04538 if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
04539 if ((mem = interface_exists(q, interface))) {
04540 if (mem->paused == paused) {
04541 ast_debug(1, "%spausing already-%spaused queue member %s:%s\n", (paused ? "" : "un"), (paused ? "" : "un"), q->name, interface);
04542 }
04543
04544 failed = 0;
04545 if (mem->realtime) {
04546 failed = update_realtime_member_field(mem, q->name, "paused", paused ? "1" : "0");
04547 }
04548
04549 if (failed) {
04550 ast_log(LOG_WARNING, "Failed %spausing realtime queue member %s:%s\n", (paused ? "" : "un"), q->name, interface);
04551 ao2_ref(mem, -1);
04552 ao2_unlock(q);
04553 queue_t_unref(q, "Done with iterator");
04554 continue;
04555 }
04556 found++;
04557 mem->paused = paused;
04558
04559 if (queue_persistent_members)
04560 dump_queue_members(q);
04561
04562 ast_queue_log(q->name, "NONE", mem->membername, (paused ? "PAUSE" : "UNPAUSE"), "%s", S_OR(reason, ""));
04563
04564 if (!ast_strlen_zero(reason)) {
04565 manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
04566 "Queue: %s\r\n"
04567 "Location: %s\r\n"
04568 "MemberName: %s\r\n"
04569 "Paused: %d\r\n"
04570 "Reason: %s\r\n",
04571 q->name, mem->interface, mem->membername, paused, reason);
04572 } else {
04573 manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
04574 "Queue: %s\r\n"
04575 "Location: %s\r\n"
04576 "MemberName: %s\r\n"
04577 "Paused: %d\r\n",
04578 q->name, mem->interface, mem->membername, paused);
04579 }
04580 ao2_ref(mem, -1);
04581 }
04582 }
04583
04584 if (!ast_strlen_zero(queuename) && !strcasecmp(queuename, q->name)) {
04585 ao2_unlock(q);
04586 queue_t_unref(q, "Done with iterator");
04587 break;
04588 }
04589
04590 ao2_unlock(q);
04591 queue_t_unref(q, "Done with iterator");
04592 }
04593 ao2_iterator_destroy(&queue_iter);
04594
04595 return found ? RESULT_SUCCESS : RESULT_FAILURE;
04596 }
04597
04598
04599 static int set_member_penalty(char *queuename, char *interface, int penalty)
04600 {
04601 int foundinterface = 0, foundqueue = 0;
04602 struct call_queue *q;
04603 struct member *mem;
04604 struct ao2_iterator queue_iter;
04605
04606 if (penalty < 0) {
04607 ast_log(LOG_ERROR, "Invalid penalty (%d)\n", penalty);
04608 return RESULT_FAILURE;
04609 }
04610
04611 queue_iter = ao2_iterator_init(queues, 0);
04612 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
04613 ao2_lock(q);
04614 if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
04615 foundqueue++;
04616 if ((mem = interface_exists(q, interface))) {
04617 foundinterface++;
04618 mem->penalty = penalty;
04619
04620 ast_queue_log(q->name, "NONE", interface, "PENALTY", "%d", penalty);
04621 manager_event(EVENT_FLAG_AGENT, "QueueMemberPenalty",
04622 "Queue: %s\r\n"
04623 "Location: %s\r\n"
04624 "Penalty: %d\r\n",
04625 q->name, mem->interface, penalty);
04626 ao2_ref(mem, -1);
04627 }
04628 }
04629 ao2_unlock(q);
04630 queue_t_unref(q, "Done with iterator");
04631 }
04632 ao2_iterator_destroy(&queue_iter);
04633
04634 if (foundinterface) {
04635 return RESULT_SUCCESS;
04636 } else if (!foundqueue) {
04637 ast_log (LOG_ERROR, "Invalid queuename\n");
04638 } else {
04639 ast_log (LOG_ERROR, "Invalid interface\n");
04640 }
04641
04642 return RESULT_FAILURE;
04643 }
04644
04645
04646
04647
04648 static int get_member_penalty(char *queuename, char *interface)
04649 {
04650 int foundqueue = 0, penalty;
04651 struct call_queue *q, tmpq;
04652 struct member *mem;
04653
04654 ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
04655 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Search for queue"))) {
04656 foundqueue = 1;
04657 ao2_lock(q);
04658 if ((mem = interface_exists(q, interface))) {
04659 penalty = mem->penalty;
04660 ao2_ref(mem, -1);
04661 ao2_unlock(q);
04662 queue_t_unref(q, "Search complete");
04663 return penalty;
04664 }
04665 ao2_unlock(q);
04666 queue_t_unref(q, "Search complete");
04667 }
04668
04669
04670 if (foundqueue)
04671 ast_log (LOG_ERROR, "Invalid queuename\n");
04672 else
04673 ast_log (LOG_ERROR, "Invalid interface\n");
04674
04675 return RESULT_FAILURE;
04676 }
04677
04678
04679 static void reload_queue_members(void)
04680 {
04681 char *cur_ptr;
04682 const char *queue_name;
04683 char *member;
04684 char *interface;
04685 char *membername = NULL;
04686 char *state_interface;
04687 char *penalty_tok;
04688 int penalty = 0;
04689 char *paused_tok;
04690 int paused = 0;
04691 struct ast_db_entry *db_tree;
04692 struct ast_db_entry *entry;
04693 struct call_queue *cur_queue;
04694 char queue_data[PM_MAX_LEN];
04695
04696 ao2_lock(queues);
04697
04698
04699 db_tree = ast_db_gettree(pm_family, NULL);
04700 for (entry = db_tree; entry; entry = entry->next) {
04701
04702 queue_name = entry->key + strlen(pm_family) + 2;
04703
04704 {
04705 struct call_queue tmpq;
04706 ast_copy_string(tmpq.name, queue_name, sizeof(tmpq.name));
04707 cur_queue = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Reload queue members");
04708 }
04709
04710 if (!cur_queue)
04711 cur_queue = load_realtime_queue(queue_name);
04712
04713 if (!cur_queue) {
04714
04715
04716 ast_log(LOG_WARNING, "Error loading persistent queue: '%s': it does not exist\n", queue_name);
04717 ast_db_del(pm_family, queue_name);
04718 continue;
04719 }
04720
04721 if (ast_db_get(pm_family, queue_name, queue_data, PM_MAX_LEN)) {
04722 queue_t_unref(cur_queue, "Expire reload reference");
04723 continue;
04724 }
04725
04726 cur_ptr = queue_data;
04727 while ((member = strsep(&cur_ptr, ",|"))) {
04728 if (ast_strlen_zero(member))
04729 continue;
04730
04731 interface = strsep(&member, ";");
04732 penalty_tok = strsep(&member, ";");
04733 paused_tok = strsep(&member, ";");
04734 membername = strsep(&member, ";");
04735 state_interface = strsep(&member, ";");
04736
04737 if (!penalty_tok) {
04738 ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (penalty)\n", queue_name);
04739 break;
04740 }
04741 penalty = strtol(penalty_tok, NULL, 10);
04742 if (errno == ERANGE) {
04743 ast_log(LOG_WARNING, "Error converting penalty: %s: Out of range.\n", penalty_tok);
04744 break;
04745 }
04746
04747 if (!paused_tok) {
04748 ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (paused)\n", queue_name);
04749 break;
04750 }
04751 paused = strtol(paused_tok, NULL, 10);
04752 if ((errno == ERANGE) || paused < 0 || paused > 1) {
04753 ast_log(LOG_WARNING, "Error converting paused: %s: Expected 0 or 1.\n", paused_tok);
04754 break;
04755 }
04756
04757 ast_debug(1, "Reload Members: Queue: %s Member: %s Name: %s Penalty: %d Paused: %d\n", queue_name, interface, membername, penalty, paused);
04758
04759 if (add_to_queue(queue_name, interface, membername, penalty, paused, 0, state_interface) == RES_OUTOFMEMORY) {
04760 ast_log(LOG_ERROR, "Out of Memory when reloading persistent queue member\n");
04761 break;
04762 }
04763 }
04764 queue_t_unref(cur_queue, "Expire reload reference");
04765 }
04766
04767 ao2_unlock(queues);
04768 if (db_tree) {
04769 ast_log(LOG_NOTICE, "Queue members successfully reloaded from database.\n");
04770 ast_db_freetree(db_tree);
04771 }
04772 }
04773
04774
04775 static int pqm_exec(struct ast_channel *chan, void *data)
04776 {
04777 char *parse;
04778 AST_DECLARE_APP_ARGS(args,
04779 AST_APP_ARG(queuename);
04780 AST_APP_ARG(interface);
04781 AST_APP_ARG(options);
04782 AST_APP_ARG(reason);
04783 );
04784
04785 if (ast_strlen_zero(data)) {
04786 ast_log(LOG_WARNING, "PauseQueueMember requires an argument ([queuename],interface[,options][,reason])\n");
04787 return -1;
04788 }
04789
04790 parse = ast_strdupa(data);
04791
04792 AST_STANDARD_APP_ARGS(args, parse);
04793
04794 if (ast_strlen_zero(args.interface)) {
04795 ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
04796 return -1;
04797 }
04798
04799 if (set_member_paused(args.queuename, args.interface, args.reason, 1)) {
04800 ast_log(LOG_WARNING, "Attempt to pause interface %s, not found\n", args.interface);
04801 pbx_builtin_setvar_helper(chan, "PQMSTATUS", "NOTFOUND");
04802 return 0;
04803 }
04804
04805 pbx_builtin_setvar_helper(chan, "PQMSTATUS", "PAUSED");
04806
04807 return 0;
04808 }
04809
04810
04811 static int upqm_exec(struct ast_channel *chan, void *data)
04812 {
04813 char *parse;
04814 AST_DECLARE_APP_ARGS(args,
04815 AST_APP_ARG(queuename);
04816 AST_APP_ARG(interface);
04817 AST_APP_ARG(options);
04818 AST_APP_ARG(reason);
04819 );
04820
04821 if (ast_strlen_zero(data)) {
04822 ast_log(LOG_WARNING, "UnpauseQueueMember requires an argument ([queuename],interface[,options[,reason]])\n");
04823 return -1;
04824 }
04825
04826 parse = ast_strdupa(data);
04827
04828 AST_STANDARD_APP_ARGS(args, parse);
04829
04830 if (ast_strlen_zero(args.interface)) {
04831 ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
04832 return -1;
04833 }
04834
04835 if (set_member_paused(args.queuename, args.interface, args.reason, 0)) {
04836 ast_log(LOG_WARNING, "Attempt to unpause interface %s, not found\n", args.interface);
04837 pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "NOTFOUND");
04838 return 0;
04839 }
04840
04841 pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "UNPAUSED");
04842
04843 return 0;
04844 }
04845
04846
04847 static int rqm_exec(struct ast_channel *chan, void *data)
04848 {
04849 int res=-1;
04850 char *parse, *temppos = NULL;
04851 AST_DECLARE_APP_ARGS(args,
04852 AST_APP_ARG(queuename);
04853 AST_APP_ARG(interface);
04854 AST_APP_ARG(options);
04855 );
04856
04857
04858 if (ast_strlen_zero(data)) {
04859 ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename[,interface[,options]])\n");
04860 return -1;
04861 }
04862
04863 parse = ast_strdupa(data);
04864
04865 AST_STANDARD_APP_ARGS(args, parse);
04866
04867 if (ast_strlen_zero(args.interface)) {
04868 args.interface = ast_strdupa(chan->name);
04869 temppos = strrchr(args.interface, '-');
04870 if (temppos)
04871 *temppos = '\0';
04872 }
04873
04874 ast_debug(1, "queue: %s, member: %s\n", args.queuename, args.interface);
04875
04876 switch (remove_from_queue(args.queuename, args.interface)) {
04877 case RES_OKAY:
04878 ast_queue_log(args.queuename, chan->uniqueid, args.interface, "REMOVEMEMBER", "%s", "");
04879 ast_log(LOG_NOTICE, "Removed interface '%s' from queue '%s'\n", args.interface, args.queuename);
04880 pbx_builtin_setvar_helper(chan, "RQMSTATUS", "REMOVED");
04881 res = 0;
04882 break;
04883 case RES_EXISTS:
04884 ast_debug(1, "Unable to remove interface '%s' from queue '%s': Not there\n", args.interface, args.queuename);
04885 pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTINQUEUE");
04886 res = 0;
04887 break;
04888 case RES_NOSUCHQUEUE:
04889 ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", args.queuename);
04890 pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOSUCHQUEUE");
04891 res = 0;
04892 break;
04893 case RES_NOT_DYNAMIC:
04894 ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': '%s' is not a dynamic member\n", args.queuename, args.interface);
04895 pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTDYNAMIC");
04896 res = 0;
04897 break;
04898 }
04899
04900 return res;
04901 }
04902
04903
04904 static int aqm_exec(struct ast_channel *chan, void *data)
04905 {
04906 int res=-1;
04907 char *parse, *temppos = NULL;
04908 AST_DECLARE_APP_ARGS(args,
04909 AST_APP_ARG(queuename);
04910 AST_APP_ARG(interface);
04911 AST_APP_ARG(penalty);
04912 AST_APP_ARG(options);
04913 AST_APP_ARG(membername);
04914 AST_APP_ARG(state_interface);
04915 );
04916 int penalty = 0;
04917
04918 if (ast_strlen_zero(data)) {
04919 ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename[,interface[,penalty[,options[,membername[,stateinterface]]]]])\n");
04920 return -1;
04921 }
04922
04923 parse = ast_strdupa(data);
04924
04925 AST_STANDARD_APP_ARGS(args, parse);
04926
04927 if (ast_strlen_zero(args.interface)) {
04928 args.interface = ast_strdupa(chan->name);
04929 temppos = strrchr(args.interface, '-');
04930 if (temppos)
04931 *temppos = '\0';
04932 }
04933
04934 if (!ast_strlen_zero(args.penalty)) {
04935 if ((sscanf(args.penalty, "%30d", &penalty) != 1) || penalty < 0) {
04936 ast_log(LOG_WARNING, "Penalty '%s' is invalid, must be an integer >= 0\n", args.penalty);
04937 penalty = 0;
04938 }
04939 }
04940
04941 switch (add_to_queue(args.queuename, args.interface, args.membername, penalty, 0, queue_persistent_members, args.state_interface)) {
04942 case RES_OKAY:
04943 ast_queue_log(args.queuename, chan->uniqueid, args.interface, "ADDMEMBER", "%s", "");
04944 ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", args.interface, args.queuename);
04945 pbx_builtin_setvar_helper(chan, "AQMSTATUS", "ADDED");
04946 res = 0;
04947 break;
04948 case RES_EXISTS:
04949 ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", args.interface, args.queuename);
04950 pbx_builtin_setvar_helper(chan, "AQMSTATUS", "MEMBERALREADY");
04951 res = 0;
04952 break;
04953 case RES_NOSUCHQUEUE:
04954 ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", args.queuename);
04955 pbx_builtin_setvar_helper(chan, "AQMSTATUS", "NOSUCHQUEUE");
04956 res = 0;
04957 break;
04958 case RES_OUTOFMEMORY:
04959 ast_log(LOG_ERROR, "Out of memory adding member %s to queue %s\n", args.interface, args.queuename);
04960 break;
04961 }
04962
04963 return res;
04964 }
04965
04966
04967 static int ql_exec(struct ast_channel *chan, void *data)
04968 {
04969 char *parse;
04970
04971 AST_DECLARE_APP_ARGS(args,
04972 AST_APP_ARG(queuename);
04973 AST_APP_ARG(uniqueid);
04974 AST_APP_ARG(membername);
04975 AST_APP_ARG(event);
04976 AST_APP_ARG(params);
04977 );
04978
04979 if (ast_strlen_zero(data)) {
04980 ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo]\n");
04981 return -1;
04982 }
04983
04984 parse = ast_strdupa(data);
04985
04986 AST_STANDARD_APP_ARGS(args, parse);
04987
04988 if (ast_strlen_zero(args.queuename) || ast_strlen_zero(args.uniqueid)
04989 || ast_strlen_zero(args.membername) || ast_strlen_zero(args.event)) {
04990 ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo])\n");
04991 return -1;
04992 }
04993
04994 ast_queue_log(args.queuename, args.uniqueid, args.membername, args.event,
04995 "%s", args.params ? args.params : "");
04996
04997 return 0;
04998 }
04999
05000
05001 static void copy_rules(struct queue_ent *qe, const char *rulename)
05002 {
05003 struct penalty_rule *pr_iter;
05004 struct rule_list *rl_iter;
05005 const char *tmp = ast_strlen_zero(rulename) ? qe->parent->defaultrule : rulename;
05006 AST_LIST_LOCK(&rule_lists);
05007 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
05008 if (!strcasecmp(rl_iter->name, tmp))
05009 break;
05010 }
05011 if (rl_iter) {
05012 AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
05013 struct penalty_rule *new_pr = ast_calloc(1, sizeof(*new_pr));
05014 if (!new_pr) {
05015 ast_log(LOG_ERROR, "Memory allocation error when copying penalty rules! Aborting!\n");
05016 AST_LIST_UNLOCK(&rule_lists);
05017 break;
05018 }
05019 new_pr->time = pr_iter->time;
05020 new_pr->max_value = pr_iter->max_value;
05021 new_pr->min_value = pr_iter->min_value;
05022 new_pr->max_relative = pr_iter->max_relative;
05023 new_pr->min_relative = pr_iter->min_relative;
05024 AST_LIST_INSERT_TAIL(&qe->qe_rules, new_pr, list);
05025 }
05026 }
05027 AST_LIST_UNLOCK(&rule_lists);
05028 }
05029
05030
05031
05032
05033
05034
05035
05036
05037
05038
05039
05040
05041
05042 static int queue_exec(struct ast_channel *chan, void *data)
05043 {
05044 int res=-1;
05045 int ringing=0;
05046 const char *user_priority;
05047 const char *max_penalty_str;
05048 const char *min_penalty_str;
05049 int prio;
05050 int qcontinue = 0;
05051 int max_penalty, min_penalty;
05052 enum queue_result reason = QUEUE_UNKNOWN;
05053
05054 int tries = 0;
05055 int noption = 0;
05056 char *parse;
05057 int makeannouncement = 0;
05058 AST_DECLARE_APP_ARGS(args,
05059 AST_APP_ARG(queuename);
05060 AST_APP_ARG(options);
05061 AST_APP_ARG(url);
05062 AST_APP_ARG(announceoverride);
05063 AST_APP_ARG(queuetimeoutstr);
05064 AST_APP_ARG(agi);
05065 AST_APP_ARG(macro);
05066 AST_APP_ARG(gosub);
05067 AST_APP_ARG(rule);
05068 );
05069
05070 struct queue_ent qe = { 0 };
05071
05072 if (ast_strlen_zero(data)) {
05073 ast_log(LOG_WARNING, "Queue requires an argument: queuename[,options[,URL[,announceoverride[,timeout[,agi[,macro[,gosub[,rule]]]]]]]]\n");
05074 return -1;
05075 }
05076
05077 parse = ast_strdupa(data);
05078 AST_STANDARD_APP_ARGS(args, parse);
05079
05080
05081 qe.start = time(NULL);
05082
05083
05084 if (!ast_strlen_zero(args.queuetimeoutstr))
05085 qe.expire = qe.start + atoi(args.queuetimeoutstr);
05086 else
05087 qe.expire = 0;
05088
05089
05090 ast_channel_lock(chan);
05091 user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
05092 if (user_priority) {
05093 if (sscanf(user_priority, "%30d", &prio) == 1) {
05094 ast_debug(1, "%s: Got priority %d from ${QUEUE_PRIO}.\n", chan->name, prio);
05095 } else {
05096 ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
05097 user_priority, chan->name);
05098 prio = 0;
05099 }
05100 } else {
05101 ast_debug(3, "NO QUEUE_PRIO variable found. Using default.\n");
05102 prio = 0;
05103 }
05104
05105
05106
05107 if ((max_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MAX_PENALTY"))) {
05108 if (sscanf(max_penalty_str, "%30d", &max_penalty) == 1) {
05109 ast_debug(1, "%s: Got max penalty %d from ${QUEUE_MAX_PENALTY}.\n", chan->name, max_penalty);
05110 } else {
05111 ast_log(LOG_WARNING, "${QUEUE_MAX_PENALTY}: Invalid value (%s), channel %s.\n",
05112 max_penalty_str, chan->name);
05113 max_penalty = 0;
05114 }
05115 } else {
05116 max_penalty = 0;
05117 }
05118
05119 if ((min_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MIN_PENALTY"))) {
05120 if (sscanf(min_penalty_str, "%30d", &min_penalty) == 1) {
05121 ast_debug(1, "%s: Got min penalty %d from ${QUEUE_MIN_PENALTY}.\n", chan->name, min_penalty);
05122 } else {
05123 ast_log(LOG_WARNING, "${QUEUE_MIN_PENALTY}: Invalid value (%s), channel %s.\n",
05124 min_penalty_str, chan->name);
05125 min_penalty = 0;
05126 }
05127 } else {
05128 min_penalty = 0;
05129 }
05130 ast_channel_unlock(chan);
05131
05132 if (args.options && (strchr(args.options, 'r')))
05133 ringing = 1;
05134
05135 if (args.options && (strchr(args.options, 'c')))
05136 qcontinue = 1;
05137
05138 ast_debug(1, "queue: %s, options: %s, url: %s, announce: %s, expires: %ld, priority: %d\n",
05139 args.queuename, args.options, args.url, args.announceoverride, (long)qe.expire, prio);
05140
05141 qe.chan = chan;
05142 qe.prio = prio;
05143 qe.max_penalty = max_penalty;
05144 qe.min_penalty = min_penalty;
05145 qe.last_pos_said = 0;
05146 qe.last_pos = 0;
05147 qe.last_periodic_announce_time = time(NULL);
05148 qe.last_periodic_announce_sound = 0;
05149 qe.valid_digits = 0;
05150 if (join_queue(args.queuename, &qe, &reason)) {
05151 ast_log(LOG_WARNING, "Unable to join queue '%s'\n", args.queuename);
05152 set_queue_result(chan, reason);
05153 return 0;
05154 }
05155 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", S_OR(args.url, ""),
05156 S_OR(chan->cid.cid_num, ""));
05157 copy_rules(&qe, args.rule);
05158 qe.pr = AST_LIST_FIRST(&qe.qe_rules);
05159 check_turns:
05160 if (ringing) {
05161 ast_indicate(chan, AST_CONTROL_RINGING);
05162 } else {
05163 ast_moh_start(chan, qe.moh, NULL);
05164 }
05165
05166
05167 res = wait_our_turn(&qe, ringing, &reason);
05168 if (res) {
05169 goto stop;
05170 }
05171
05172 makeannouncement = 0;
05173
05174 for (;;) {
05175
05176
05177
05178
05179
05180
05181 if (qe.expire && (time(NULL) >= qe.expire)) {
05182 record_abandoned(&qe);
05183 reason = QUEUE_TIMEOUT;
05184 res = 0;
05185 ast_queue_log(args.queuename, chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld",
05186 qe.pos, qe.opos, (long) time(NULL) - qe.start);
05187 break;
05188 }
05189
05190 if (makeannouncement) {
05191
05192 if (qe.parent->announcefrequency)
05193 if ((res = say_position(&qe,ringing)))
05194 goto stop;
05195 }
05196 makeannouncement = 1;
05197
05198
05199 if (qe.parent->periodicannouncefrequency)
05200 if ((res = say_periodic_announcement(&qe,ringing)))
05201 goto stop;
05202
05203
05204 if (qe.expire && (time(NULL) >= qe.expire)) {
05205 record_abandoned(&qe);
05206 reason = QUEUE_TIMEOUT;
05207 res = 0;
05208 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
05209 break;
05210 }
05211
05212
05213 while (qe.pr && ((time(NULL) - qe.start) > qe.pr->time)) {
05214 update_qe_rule(&qe);
05215 }
05216
05217
05218 res = try_calling(&qe, args.options, args.announceoverride, args.url, &tries, &noption, args.agi, args.macro, args.gosub, ringing);
05219 if (res) {
05220 goto stop;
05221 }
05222
05223 if (qe.parent->leavewhenempty) {
05224 int status = 0;
05225 if ((status = get_member_status(qe.parent, qe.max_penalty, qe.min_penalty, qe.parent->leavewhenempty))) {
05226 record_abandoned(&qe);
05227 reason = QUEUE_LEAVEEMPTY;
05228 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe.pos, qe.opos, (long)(time(NULL) - qe.start));
05229 res = 0;
05230 break;
05231 }
05232 }
05233
05234
05235 if (noption && tries >= qe.parent->membercount) {
05236 ast_verb(3, "Exiting on time-out cycle\n");
05237 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
05238 record_abandoned(&qe);
05239 reason = QUEUE_TIMEOUT;
05240 res = 0;
05241 break;
05242 }
05243
05244
05245
05246 if (qe.expire && (time(NULL) >= qe.expire)) {
05247 record_abandoned(&qe);
05248 reason = QUEUE_TIMEOUT;
05249 res = 0;
05250 ast_queue_log(qe.parent->name, qe.chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld", qe.pos, qe.opos, (long) time(NULL) - qe.start);
05251 break;
05252 }
05253
05254
05255 update_realtime_members(qe.parent);
05256
05257 res = wait_a_bit(&qe);
05258 if (res)
05259 goto stop;
05260
05261
05262
05263
05264
05265 if (!is_our_turn(&qe)) {
05266 ast_debug(1, "Darn priorities, going back in queue (%s)!\n", qe.chan->name);
05267 goto check_turns;
05268 }
05269 }
05270
05271 stop:
05272 if (res) {
05273 if (res < 0) {
05274 if (!qe.handled) {
05275 record_abandoned(&qe);
05276 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ABANDON",
05277 "%d|%d|%ld", qe.pos, qe.opos,
05278 (long) time(NULL) - qe.start);
05279 res = -1;
05280 } else if (qcontinue) {
05281 reason = QUEUE_CONTINUE;
05282 res = 0;
05283 }
05284 } else if (qe.valid_digits) {
05285 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHKEY",
05286 "%s|%d", qe.digits, qe.pos);
05287 }
05288 }
05289
05290
05291 if (res >= 0) {
05292 res = 0;
05293 if (ringing) {
05294 ast_indicate(chan, -1);
05295 } else {
05296 ast_moh_stop(chan);
05297 }
05298 ast_stopstream(chan);
05299 }
05300
05301 set_queue_variables(qe.parent, qe.chan);
05302
05303 leave_queue(&qe);
05304 if (reason != QUEUE_UNKNOWN)
05305 set_queue_result(chan, reason);
05306
05307 if (qe.parent) {
05308
05309
05310
05311 qe.parent = queue_unref(qe.parent);
05312 }
05313
05314 return res;
05315 }
05316
05317
05318
05319
05320
05321
05322 static int queue_function_var(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05323 {
05324 int res = -1;
05325 struct call_queue *q, tmpq;
05326 char interfacevar[256] = "";
05327 float sl = 0;
05328
05329 if (ast_strlen_zero(data)) {
05330 ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05331 return -1;
05332 }
05333
05334 ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05335 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE() function"))) {
05336 ao2_lock(q);
05337 if (q->setqueuevar) {
05338 sl = 0;
05339 res = 0;
05340
05341 if (q->callscompleted > 0) {
05342 sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
05343 }
05344
05345 snprintf(interfacevar, sizeof(interfacevar),
05346 "QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
05347 q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned, q->servicelevel, sl);
05348
05349 pbx_builtin_setvar_multiple(chan, interfacevar);
05350 }
05351
05352 ao2_unlock(q);
05353 queue_t_unref(q, "Done with QUEUE() function");
05354 } else {
05355 ast_log(LOG_WARNING, "queue %s was not found\n", data);
05356 }
05357
05358 snprintf(buf, len, "%d", res);
05359
05360 return 0;
05361 }
05362
05363
05364
05365
05366
05367
05368 static int queue_function_qac(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05369 {
05370 int count = 0;
05371 struct member *m;
05372 struct ao2_iterator mem_iter;
05373 struct call_queue *q;
05374 char *option;
05375
05376 if (ast_strlen_zero(data)) {
05377 ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05378 return -1;
05379 }
05380
05381 if ((option = strchr(data, ',')))
05382 *option++ = '\0';
05383 else
05384 option = "logged";
05385 if ((q = load_realtime_queue(data))) {
05386 ao2_lock(q);
05387 if (!strcasecmp(option, "logged")) {
05388 mem_iter = ao2_iterator_init(q->members, 0);
05389 while ((m = ao2_iterator_next(&mem_iter))) {
05390
05391 if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
05392 count++;
05393 }
05394 ao2_ref(m, -1);
05395 }
05396 ao2_iterator_destroy(&mem_iter);
05397 } else if (!strcasecmp(option, "free")) {
05398 mem_iter = ao2_iterator_init(q->members, 0);
05399 while ((m = ao2_iterator_next(&mem_iter))) {
05400
05401 if ((m->status == AST_DEVICE_NOT_INUSE) && (!m->paused)) {
05402 count++;
05403 }
05404 ao2_ref(m, -1);
05405 }
05406 ao2_iterator_destroy(&mem_iter);
05407 } else
05408 count = q->membercount;
05409 ao2_unlock(q);
05410 queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER()");
05411 } else
05412 ast_log(LOG_WARNING, "queue %s was not found\n", data);
05413
05414 snprintf(buf, len, "%d", count);
05415
05416 return 0;
05417 }
05418
05419
05420
05421
05422
05423
05424 static int queue_function_qac_dep(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05425 {
05426 int count = 0;
05427 struct member *m;
05428 struct call_queue *q;
05429 struct ao2_iterator mem_iter;
05430 static int depflag = 1;
05431
05432 if (depflag) {
05433 depflag = 0;
05434 ast_log(LOG_NOTICE, "The function QUEUE_MEMBER_COUNT has been deprecated in favor of the QUEUE_MEMBER function and will not be in further releases.\n");
05435 }
05436
05437 if (ast_strlen_zero(data)) {
05438 ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05439 return -1;
05440 }
05441
05442 if ((q = load_realtime_queue(data))) {
05443 ao2_lock(q);
05444 mem_iter = ao2_iterator_init(q->members, 0);
05445 while ((m = ao2_iterator_next(&mem_iter))) {
05446
05447 if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
05448 count++;
05449 }
05450 ao2_ref(m, -1);
05451 }
05452 ao2_iterator_destroy(&mem_iter);
05453 ao2_unlock(q);
05454 queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER_COUNT");
05455 } else
05456 ast_log(LOG_WARNING, "queue %s was not found\n", data);
05457
05458 snprintf(buf, len, "%d", count);
05459
05460 return 0;
05461 }
05462
05463
05464 static int queue_function_queuewaitingcount(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05465 {
05466 int count = 0;
05467 struct call_queue *q, tmpq;
05468 struct ast_variable *var = NULL;
05469
05470 buf[0] = '\0';
05471
05472 if (ast_strlen_zero(data)) {
05473 ast_log(LOG_ERROR, "QUEUE_WAITING_COUNT requires an argument: queuename\n");
05474 return -1;
05475 }
05476
05477 ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05478 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_WAITING_COUNT()"))) {
05479 ao2_lock(q);
05480 count = q->count;
05481 ao2_unlock(q);
05482 queue_t_unref(q, "Done with reference in QUEUE_WAITING_COUNT()");
05483 } else if ((var = ast_load_realtime("queues", "name", data, SENTINEL))) {
05484
05485
05486
05487
05488 count = 0;
05489 ast_variables_destroy(var);
05490 } else
05491 ast_log(LOG_WARNING, "queue %s was not found\n", data);
05492
05493 snprintf(buf, len, "%d", count);
05494
05495 return 0;
05496 }
05497
05498
05499 static int queue_function_queuememberlist(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05500 {
05501 struct call_queue *q, tmpq;
05502 struct member *m;
05503
05504
05505 buf[0] = '\0';
05506
05507 if (ast_strlen_zero(data)) {
05508 ast_log(LOG_ERROR, "QUEUE_MEMBER_LIST requires an argument: queuename\n");
05509 return -1;
05510 }
05511
05512 ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05513 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_MEMBER_LIST()"))) {
05514 int buflen = 0, count = 0;
05515 struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
05516
05517 ao2_lock(q);
05518 while ((m = ao2_iterator_next(&mem_iter))) {
05519
05520 if (count++) {
05521 strncat(buf + buflen, ",", len - buflen - 1);
05522 buflen++;
05523 }
05524 strncat(buf + buflen, m->interface, len - buflen - 1);
05525 buflen += strlen(m->interface);
05526
05527 if (buflen >= len - 2) {
05528 ao2_ref(m, -1);
05529 ast_log(LOG_WARNING, "Truncating list\n");
05530 break;
05531 }
05532 ao2_ref(m, -1);
05533 }
05534 ao2_iterator_destroy(&mem_iter);
05535 ao2_unlock(q);
05536 queue_t_unref(q, "Done with QUEUE_MEMBER_LIST()");
05537 } else
05538 ast_log(LOG_WARNING, "queue %s was not found\n", data);
05539
05540
05541 buf[len - 1] = '\0';
05542
05543 return 0;
05544 }
05545
05546
05547 static int queue_function_memberpenalty_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05548 {
05549 int penalty;
05550 AST_DECLARE_APP_ARGS(args,
05551 AST_APP_ARG(queuename);
05552 AST_APP_ARG(interface);
05553 );
05554
05555 buf[0] = '\0';
05556
05557 if (ast_strlen_zero(data)) {
05558 ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05559 return -1;
05560 }
05561
05562 AST_STANDARD_APP_ARGS(args, data);
05563
05564 if (args.argc < 2) {
05565 ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05566 return -1;
05567 }
05568
05569 penalty = get_member_penalty (args.queuename, args.interface);
05570
05571 if (penalty >= 0)
05572 snprintf (buf, len, "%d", penalty);
05573
05574 return 0;
05575 }
05576
05577
05578 static int queue_function_memberpenalty_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
05579 {
05580 int penalty;
05581 AST_DECLARE_APP_ARGS(args,
05582 AST_APP_ARG(queuename);
05583 AST_APP_ARG(interface);
05584 );
05585
05586 if (ast_strlen_zero(data)) {
05587 ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05588 return -1;
05589 }
05590
05591 AST_STANDARD_APP_ARGS(args, data);
05592
05593 if (args.argc < 2) {
05594 ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05595 return -1;
05596 }
05597
05598 penalty = atoi(value);
05599
05600 if (ast_strlen_zero(args.interface)) {
05601 ast_log (LOG_ERROR, "<interface> parameter can't be null\n");
05602 return -1;
05603 }
05604
05605
05606 if (set_member_penalty(args.queuename, args.interface, penalty)) {
05607 ast_log(LOG_ERROR, "Invalid interface, queue or penalty\n");
05608 return -1;
05609 }
05610
05611 return 0;
05612 }
05613
05614 static struct ast_custom_function queuevar_function = {
05615 .name = "QUEUE_VARIABLES",
05616 .read = queue_function_var,
05617 };
05618
05619 static struct ast_custom_function queuemembercount_function = {
05620 .name = "QUEUE_MEMBER",
05621 .read = queue_function_qac,
05622 };
05623
05624 static struct ast_custom_function queuemembercount_dep = {
05625 .name = "QUEUE_MEMBER_COUNT",
05626 .read = queue_function_qac_dep,
05627 };
05628
05629 static struct ast_custom_function queuewaitingcount_function = {
05630 .name = "QUEUE_WAITING_COUNT",
05631 .read = queue_function_queuewaitingcount,
05632 };
05633
05634 static struct ast_custom_function queuememberlist_function = {
05635 .name = "QUEUE_MEMBER_LIST",
05636 .read = queue_function_queuememberlist,
05637 };
05638
05639 static struct ast_custom_function queuememberpenalty_function = {
05640 .name = "QUEUE_MEMBER_PENALTY",
05641 .read = queue_function_memberpenalty_read,
05642 .write = queue_function_memberpenalty_write,
05643 };
05644
05645
05646
05647
05648
05649
05650
05651 static int reload_queue_rules(int reload)
05652 {
05653 struct ast_config *cfg;
05654 struct rule_list *rl_iter, *new_rl;
05655 struct penalty_rule *pr_iter;
05656 char *rulecat = NULL;
05657 struct ast_variable *rulevar = NULL;
05658 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
05659
05660 if (!(cfg = ast_config_load("queuerules.conf", config_flags))) {
05661 ast_log(LOG_NOTICE, "No queuerules.conf file found, queues will not follow penalty rules\n");
05662 return AST_MODULE_LOAD_SUCCESS;
05663 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
05664 ast_log(LOG_NOTICE, "queuerules.conf has not changed since it was last loaded. Not taking any action.\n");
05665 return AST_MODULE_LOAD_SUCCESS;
05666 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
05667 ast_log(LOG_ERROR, "Config file queuerules.conf is in an invalid format. Aborting.\n");
05668 return AST_MODULE_LOAD_SUCCESS;
05669 }
05670
05671 AST_LIST_LOCK(&rule_lists);
05672 while ((rl_iter = AST_LIST_REMOVE_HEAD(&rule_lists, list))) {
05673 while ((pr_iter = AST_LIST_REMOVE_HEAD(&rl_iter->rules, list)))
05674 ast_free(pr_iter);
05675 ast_free(rl_iter);
05676 }
05677 while ((rulecat = ast_category_browse(cfg, rulecat))) {
05678 if (!(new_rl = ast_calloc(1, sizeof(*new_rl)))) {
05679 AST_LIST_UNLOCK(&rule_lists);
05680 return AST_MODULE_LOAD_FAILURE;
05681 } else {
05682 ast_copy_string(new_rl->name, rulecat, sizeof(new_rl->name));
05683 AST_LIST_INSERT_TAIL(&rule_lists, new_rl, list);
05684 for (rulevar = ast_variable_browse(cfg, rulecat); rulevar; rulevar = rulevar->next)
05685 if(!strcasecmp(rulevar->name, "penaltychange"))
05686 insert_penaltychange(new_rl->name, rulevar->value, rulevar->lineno);
05687 else
05688 ast_log(LOG_WARNING, "Don't know how to handle rule type '%s' on line %d\n", rulevar->name, rulevar->lineno);
05689 }
05690 }
05691 AST_LIST_UNLOCK(&rule_lists);
05692
05693 ast_config_destroy(cfg);
05694
05695 return AST_MODULE_LOAD_SUCCESS;
05696 }
05697
05698
05699 static void queue_set_global_params(struct ast_config *cfg)
05700 {
05701 const char *general_val = NULL;
05702 queue_persistent_members = 0;
05703 if ((general_val = ast_variable_retrieve(cfg, "general", "persistentmembers")))
05704 queue_persistent_members = ast_true(general_val);
05705 autofill_default = 0;
05706 if ((general_val = ast_variable_retrieve(cfg, "general", "autofill")))
05707 autofill_default = ast_true(general_val);
05708 montype_default = 0;
05709 if ((general_val = ast_variable_retrieve(cfg, "general", "monitor-type"))) {
05710 if (!strcasecmp(general_val, "mixmonitor"))
05711 montype_default = 1;
05712 }
05713 update_cdr = 0;
05714 if ((general_val = ast_variable_retrieve(cfg, "general", "updatecdr")))
05715 update_cdr = ast_true(general_val);
05716 shared_lastcall = 0;
05717 if ((general_val = ast_variable_retrieve(cfg, "general", "shared_lastcall")))
05718 shared_lastcall = ast_true(general_val);
05719 }
05720
05721
05722
05723
05724
05725
05726
05727
05728
05729 static void reload_single_member(const char *memberdata, struct call_queue *q)
05730 {
05731 char *membername, *interface, *state_interface, *tmp;
05732 char *parse;
05733 struct member *cur, *newm;
05734 struct member tmpmem;
05735 int penalty;
05736 AST_DECLARE_APP_ARGS(args,
05737 AST_APP_ARG(interface);
05738 AST_APP_ARG(penalty);
05739 AST_APP_ARG(membername);
05740 AST_APP_ARG(state_interface);
05741 );
05742
05743 if (ast_strlen_zero(memberdata)) {
05744 ast_log(LOG_WARNING, "Empty queue member definition. Moving on!\n");
05745 return;
05746 }
05747
05748
05749 parse = ast_strdupa(memberdata);
05750
05751 AST_STANDARD_APP_ARGS(args, parse);
05752
05753 interface = args.interface;
05754 if (!ast_strlen_zero(args.penalty)) {
05755 tmp = args.penalty;
05756 ast_strip(tmp);
05757 penalty = atoi(tmp);
05758 if (penalty < 0) {
05759 penalty = 0;
05760 }
05761 } else {
05762 penalty = 0;
05763 }
05764
05765 if (!ast_strlen_zero(args.membername)) {
05766 membername = args.membername;
05767 ast_strip(membername);
05768 } else {
05769 membername = interface;
05770 }
05771
05772 if (!ast_strlen_zero(args.state_interface)) {
05773 state_interface = args.state_interface;
05774 ast_strip(state_interface);
05775 } else {
05776 state_interface = interface;
05777 }
05778
05779
05780 ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
05781 cur = ao2_find(q->members, &tmpmem, OBJ_POINTER | OBJ_UNLINK);
05782 if ((newm = create_queue_member(interface, membername, penalty, cur ? cur->paused : 0, state_interface))) {
05783 ao2_link(q->members, newm);
05784 ao2_ref(newm, -1);
05785 }
05786 newm = NULL;
05787
05788 if (cur) {
05789 ao2_ref(cur, -1);
05790 } else {
05791 q->membercount++;
05792 }
05793 }
05794
05795 static int mark_member_dead(void *obj, void *arg, int flags)
05796 {
05797 struct member *member = obj;
05798 if (!member->dynamic) {
05799 member->delme = 1;
05800 }
05801 return 0;
05802 }
05803
05804 static int kill_dead_members(void *obj, void *arg, int flags)
05805 {
05806 struct member *member = obj;
05807 struct call_queue *q = arg;
05808
05809 if (!member->delme) {
05810 if (member->dynamic) {
05811
05812
05813
05814 q->membercount++;
05815 }
05816 member->status = ast_device_state(member->state_interface);
05817 return 0;
05818 } else {
05819 q->membercount--;
05820 return CMP_MATCH;
05821 }
05822 }
05823
05824
05825
05826
05827
05828
05829
05830
05831
05832
05833
05834
05835 static void reload_single_queue(struct ast_config *cfg, struct ast_flags *mask, const char *queuename)
05836 {
05837 int new;
05838 struct call_queue *q = NULL;
05839
05840 struct call_queue tmpq;
05841 const char *tmpvar;
05842 const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
05843 const int member_reload = ast_test_flag(mask, QUEUE_RELOAD_MEMBER);
05844 int prev_weight = 0;
05845 struct ast_variable *var;
05846
05847 ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
05848 if (!(q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find queue for reload"))) {
05849 if (queue_reload) {
05850
05851 if (!(q = alloc_queue(queuename))) {
05852 return;
05853 }
05854 } else {
05855
05856
05857
05858 return;
05859 }
05860 new = 1;
05861 } else {
05862 new = 0;
05863 }
05864
05865 if (!new) {
05866 ao2_lock(q);
05867 prev_weight = q->weight ? 1 : 0;
05868 }
05869
05870 if (q->found) {
05871 ast_log(LOG_WARNING, "Queue '%s' already defined! Skipping!\n", queuename);
05872 if (!new) {
05873
05874 ao2_unlock(q);
05875 }
05876 queue_t_unref(q, "We exist! Expiring temporary pointer");
05877 return;
05878 }
05879
05880
05881
05882
05883
05884 if (queue_reload) {
05885 if ((tmpvar = ast_variable_retrieve(cfg, queuename, "strategy"))) {
05886 q->strategy = strat2int(tmpvar);
05887 if (q->strategy < 0) {
05888 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
05889 tmpvar, q->name);
05890 q->strategy = QUEUE_STRATEGY_RINGALL;
05891 }
05892 } else {
05893 q->strategy = QUEUE_STRATEGY_RINGALL;
05894 }
05895 init_queue(q);
05896 }
05897 if (member_reload) {
05898 q->membercount = 0;
05899 ao2_callback(q->members, OBJ_NODATA, mark_member_dead, NULL);
05900 }
05901 for (var = ast_variable_browse(cfg, queuename); var; var = var->next) {
05902 if (member_reload && !strcasecmp(var->name, "member")) {
05903 reload_single_member(var->value, q);
05904 } else if (queue_reload) {
05905 queue_set_param(q, var->name, var->value, var->lineno, 1);
05906 }
05907 }
05908
05909
05910
05911 if (!q->weight && prev_weight) {
05912 ast_atomic_fetchadd_int(&use_weight, -1);
05913 }
05914 else if (q->weight && !prev_weight) {
05915 ast_atomic_fetchadd_int(&use_weight, +1);
05916 }
05917
05918
05919 if (member_reload) {
05920 ao2_callback(q->members, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_members, q);
05921 }
05922
05923 if (new) {
05924 queues_t_link(queues, q, "Add queue to container");
05925 } else {
05926 ao2_unlock(q);
05927 }
05928 queue_t_unref(q, "Expiring creation reference");
05929 }
05930
05931 static int mark_dead_and_unfound(void *obj, void *arg, int flags)
05932 {
05933 struct call_queue *q = obj;
05934 char *queuename = arg;
05935 if (!q->realtime && (ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name))) {
05936 q->dead = 1;
05937 q->found = 0;
05938 }
05939 return 0;
05940 }
05941
05942 static int kill_dead_queues(void *obj, void *arg, int flags)
05943 {
05944 struct call_queue *q = obj;
05945 char *queuename = arg;
05946 if ((ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name)) && q->dead) {
05947 return CMP_MATCH;
05948 } else {
05949 return 0;
05950 }
05951 }
05952
05953
05954
05955
05956
05957
05958
05959
05960
05961
05962
05963
05964
05965 static int reload_queues(int reload, struct ast_flags *mask, const char *queuename)
05966 {
05967 struct ast_config *cfg;
05968 char *cat;
05969 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
05970 const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
05971
05972 if (!(cfg = ast_config_load("queues.conf", config_flags))) {
05973 ast_log(LOG_NOTICE, "No call queueing config file (queues.conf), so no call queues\n");
05974 return -1;
05975 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
05976 return 0;
05977 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
05978 ast_log(LOG_ERROR, "Config file queues.conf is in an invalid format. Aborting.\n");
05979 return -1;
05980 }
05981
05982
05983 ao2_lock(queues);
05984
05985
05986
05987
05988 if (queue_reload) {
05989 ao2_callback(queues, OBJ_NODATA, mark_dead_and_unfound, (char *) queuename);
05990 }
05991
05992
05993 cat = NULL;
05994 while ((cat = ast_category_browse(cfg, cat)) ) {
05995 if (!strcasecmp(cat, "general") && queue_reload) {
05996 queue_set_global_params(cfg);
05997 continue;
05998 }
05999 if (ast_strlen_zero(queuename) || !strcasecmp(cat, queuename))
06000 reload_single_queue(cfg, mask, cat);
06001 }
06002
06003 ast_config_destroy(cfg);
06004
06005 if (queue_reload) {
06006 ao2_callback(queues, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_queues, (char *) queuename);
06007 }
06008 ao2_unlock(queues);
06009 return 0;
06010 }
06011
06012
06013
06014
06015
06016
06017
06018
06019
06020
06021
06022
06023
06024
06025 static int clear_stats(const char *queuename)
06026 {
06027 struct call_queue *q;
06028 struct ao2_iterator queue_iter = ao2_iterator_init(queues, 0);
06029 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06030 ao2_lock(q);
06031 if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename))
06032 clear_queue(q);
06033 ao2_unlock(q);
06034 queue_t_unref(q, "Done with iterator");
06035 }
06036 ao2_iterator_destroy(&queue_iter);
06037 return 0;
06038 }
06039
06040
06041
06042
06043
06044
06045
06046
06047
06048
06049
06050
06051
06052
06053 static int reload_handler(int reload, struct ast_flags *mask, const char *queuename)
06054 {
06055 int res = 0;
06056
06057 if (ast_test_flag(mask, QUEUE_RELOAD_RULES)) {
06058 res |= reload_queue_rules(reload);
06059 }
06060 if (ast_test_flag(mask, QUEUE_RESET_STATS)) {
06061 res |= clear_stats(queuename);
06062 }
06063 if (ast_test_flag(mask, (QUEUE_RELOAD_PARAMETERS | QUEUE_RELOAD_MEMBER))) {
06064 res |= reload_queues(reload, mask, queuename);
06065 }
06066 return res;
06067 }
06068
06069
06070 static void do_print(struct mansession *s, int fd, const char *str)
06071 {
06072 if (s)
06073 astman_append(s, "%s\r\n", str);
06074 else
06075 ast_cli(fd, "%s\n", str);
06076 }
06077
06078
06079
06080
06081
06082
06083
06084 static char *__queues_show(struct mansession *s, int fd, int argc, char **argv)
06085 {
06086 struct call_queue *q;
06087 struct ast_str *out = ast_str_alloca(240);
06088 int found = 0;
06089 time_t now = time(NULL);
06090 struct ao2_iterator queue_iter;
06091 struct ao2_iterator mem_iter;
06092
06093 if (argc != 2 && argc != 3)
06094 return CLI_SHOWUSAGE;
06095
06096 if (argc == 3) {
06097 if ((q = load_realtime_queue(argv[2]))) {
06098 queue_t_unref(q, "Done with temporary pointer");
06099 }
06100 } else if (ast_check_realtime("queues")) {
06101
06102
06103
06104 struct ast_config *cfg = ast_load_realtime_multientry("queues", "name LIKE", "%", SENTINEL);
06105 char *queuename;
06106 if (cfg) {
06107 for (queuename = ast_category_browse(cfg, NULL); !ast_strlen_zero(queuename); queuename = ast_category_browse(cfg, queuename)) {
06108 if ((q = load_realtime_queue(queuename))) {
06109 queue_t_unref(q, "Done with temporary pointer");
06110 }
06111 }
06112 ast_config_destroy(cfg);
06113 }
06114 }
06115
06116 queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
06117 ao2_lock(queues);
06118 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06119 float sl;
06120 struct call_queue *realtime_queue = NULL;
06121
06122 ao2_lock(q);
06123
06124
06125
06126
06127 if (q->realtime && !(realtime_queue = load_realtime_queue(q->name))) {
06128 ao2_unlock(q);
06129 queue_t_unref(q, "Done with iterator");
06130 continue;
06131 } else if (q->realtime) {
06132 queue_t_unref(realtime_queue, "Queue is already in memory");
06133 }
06134 if (argc == 3 && strcasecmp(q->name, argv[2])) {
06135 ao2_unlock(q);
06136 queue_t_unref(q, "Done with iterator");
06137 continue;
06138 }
06139 found = 1;
06140
06141 ast_str_set(&out, 0, "%s has %d calls (max ", q->name, q->count);
06142 if (q->maxlen)
06143 ast_str_append(&out, 0, "%d", q->maxlen);
06144 else
06145 ast_str_append(&out, 0, "unlimited");
06146 sl = 0;
06147 if (q->callscompleted > 0)
06148 sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
06149 ast_str_append(&out, 0, ") in '%s' strategy (%ds holdtime, %ds talktime), W:%d, C:%d, A:%d, SL:%2.1f%% within %ds",
06150 int2strat(q->strategy), q->holdtime, q->talktime, q->weight,
06151 q->callscompleted, q->callsabandoned,sl,q->servicelevel);
06152 do_print(s, fd, ast_str_buffer(out));
06153 if (!ao2_container_count(q->members))
06154 do_print(s, fd, " No Members");
06155 else {
06156 struct member *mem;
06157
06158 do_print(s, fd, " Members: ");
06159 mem_iter = ao2_iterator_init(q->members, 0);
06160 while ((mem = ao2_iterator_next(&mem_iter))) {
06161 ast_str_set(&out, 0, " %s", mem->membername);
06162 if (strcasecmp(mem->membername, mem->interface)) {
06163 ast_str_append(&out, 0, " (%s)", mem->interface);
06164 }
06165 if (mem->penalty)
06166 ast_str_append(&out, 0, " with penalty %d", mem->penalty);
06167 ast_str_append(&out, 0, "%s%s%s (%s)",
06168 mem->dynamic ? " (dynamic)" : "",
06169 mem->realtime ? " (realtime)" : "",
06170 mem->paused ? " (paused)" : "",
06171 ast_devstate2str(mem->status));
06172 if (mem->calls)
06173 ast_str_append(&out, 0, " has taken %d calls (last was %ld secs ago)",
06174 mem->calls, (long) (time(NULL) - mem->lastcall));
06175 else
06176 ast_str_append(&out, 0, " has taken no calls yet");
06177 do_print(s, fd, ast_str_buffer(out));
06178 ao2_ref(mem, -1);
06179 }
06180 ao2_iterator_destroy(&mem_iter);
06181 }
06182 if (!q->head)
06183 do_print(s, fd, " No Callers");
06184 else {
06185 struct queue_ent *qe;
06186 int pos = 1;
06187
06188 do_print(s, fd, " Callers: ");
06189 for (qe = q->head; qe; qe = qe->next) {
06190 ast_str_set(&out, 0, " %d. %s (wait: %ld:%2.2ld, prio: %d)",
06191 pos++, qe->chan->name, (long) (now - qe->start) / 60,
06192 (long) (now - qe->start) % 60, qe->prio);
06193 do_print(s, fd, ast_str_buffer(out));
06194 }
06195 }
06196 do_print(s, fd, "");
06197 ao2_unlock(q);
06198 queue_t_unref(q, "Done with iterator");
06199 }
06200 ao2_iterator_destroy(&queue_iter);
06201 ao2_unlock(queues);
06202 if (!found) {
06203 if (argc == 3)
06204 ast_str_set(&out, 0, "No such queue: %s.", argv[2]);
06205 else
06206 ast_str_set(&out, 0, "No queues.");
06207 do_print(s, fd, ast_str_buffer(out));
06208 }
06209 return CLI_SUCCESS;
06210 }
06211
06212 static char *complete_queue(const char *line, const char *word, int pos, int state)
06213 {
06214 struct call_queue *q;
06215 char *ret = NULL;
06216 int which = 0;
06217 int wordlen = strlen(word);
06218 struct ao2_iterator queue_iter;
06219
06220 queue_iter = ao2_iterator_init(queues, 0);
06221 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06222 if (!strncasecmp(word, q->name, wordlen) && ++which > state) {
06223 ret = ast_strdup(q->name);
06224 queue_t_unref(q, "Done with iterator");
06225 break;
06226 }
06227 queue_t_unref(q, "Done with iterator");
06228 }
06229 ao2_iterator_destroy(&queue_iter);
06230
06231 return ret;
06232 }
06233
06234 static char *complete_queue_show(const char *line, const char *word, int pos, int state)
06235 {
06236 if (pos == 2)
06237 return complete_queue(line, word, pos, state);
06238 return NULL;
06239 }
06240
06241 static char *queue_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06242 {
06243 switch ( cmd ) {
06244 case CLI_INIT:
06245 e->command = "queue show";
06246 e->usage =
06247 "Usage: queue show\n"
06248 " Provides summary information on a specified queue.\n";
06249 return NULL;
06250 case CLI_GENERATE:
06251 return complete_queue_show(a->line, a->word, a->pos, a->n);
06252 }
06253
06254 return __queues_show(NULL, a->fd, a->argc, a->argv);
06255 }
06256
06257
06258
06259
06260 static int manager_queues_show(struct mansession *s, const struct message *m)
06261 {
06262 char *a[] = { "queue", "show" };
06263
06264 __queues_show(s, -1, 2, a);
06265 astman_append(s, "\r\n\r\n");
06266
06267 return RESULT_SUCCESS;
06268 }
06269
06270 static int manager_queue_rule_show(struct mansession *s, const struct message *m)
06271 {
06272 const char *rule = astman_get_header(m, "Rule");
06273 struct rule_list *rl_iter;
06274 struct penalty_rule *pr_iter;
06275
06276 AST_LIST_LOCK(&rule_lists);
06277 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
06278 if (ast_strlen_zero(rule) || !strcasecmp(rule, rl_iter->name)) {
06279 astman_append(s, "RuleList: %s\r\n", rl_iter->name);
06280 AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
06281 astman_append(s, "Rule: %d,%s%d,%s%d\r\n", pr_iter->time, pr_iter->max_relative && pr_iter->max_value >= 0 ? "+" : "", pr_iter->max_value, pr_iter->min_relative && pr_iter->min_value >= 0 ? "+" : "", pr_iter->min_value );
06282 }
06283 if (!ast_strlen_zero(rule))
06284 break;
06285 }
06286 }
06287 AST_LIST_UNLOCK(&rule_lists);
06288
06289 astman_append(s, "\r\n\r\n");
06290
06291 return RESULT_SUCCESS;
06292 }
06293
06294
06295 static int manager_queues_summary(struct mansession *s, const struct message *m)
06296 {
06297 time_t now;
06298 int qmemcount = 0;
06299 int qmemavail = 0;
06300 int qchancount = 0;
06301 int qlongestholdtime = 0;
06302 const char *id = astman_get_header(m, "ActionID");
06303 const char *queuefilter = astman_get_header(m, "Queue");
06304 char idText[256] = "";
06305 struct call_queue *q;
06306 struct queue_ent *qe;
06307 struct member *mem;
06308 struct ao2_iterator queue_iter;
06309 struct ao2_iterator mem_iter;
06310
06311 astman_send_ack(s, m, "Queue summary will follow");
06312 time(&now);
06313 if (!ast_strlen_zero(id))
06314 snprintf(idText, 256, "ActionID: %s\r\n", id);
06315 queue_iter = ao2_iterator_init(queues, 0);
06316 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06317 ao2_lock(q);
06318
06319
06320 if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
06321
06322 qmemcount = 0;
06323 qmemavail = 0;
06324 qchancount = 0;
06325 qlongestholdtime = 0;
06326
06327
06328 mem_iter = ao2_iterator_init(q->members, 0);
06329 while ((mem = ao2_iterator_next(&mem_iter))) {
06330 if ((mem->status != AST_DEVICE_UNAVAILABLE) && (mem->status != AST_DEVICE_INVALID)) {
06331 ++qmemcount;
06332 if (((mem->status == AST_DEVICE_NOT_INUSE) || (mem->status == AST_DEVICE_UNKNOWN)) && !(mem->paused)) {
06333 ++qmemavail;
06334 }
06335 }
06336 ao2_ref(mem, -1);
06337 }
06338 ao2_iterator_destroy(&mem_iter);
06339 for (qe = q->head; qe; qe = qe->next) {
06340 if ((now - qe->start) > qlongestholdtime) {
06341 qlongestholdtime = now - qe->start;
06342 }
06343 ++qchancount;
06344 }
06345 astman_append(s, "Event: QueueSummary\r\n"
06346 "Queue: %s\r\n"
06347 "LoggedIn: %d\r\n"
06348 "Available: %d\r\n"
06349 "Callers: %d\r\n"
06350 "HoldTime: %d\r\n"
06351 "TalkTime: %d\r\n"
06352 "LongestHoldTime: %d\r\n"
06353 "%s"
06354 "\r\n",
06355 q->name, qmemcount, qmemavail, qchancount, q->holdtime, q->talktime, qlongestholdtime, idText);
06356 }
06357 ao2_unlock(q);
06358 queue_t_unref(q, "Done with iterator");
06359 }
06360 ao2_iterator_destroy(&queue_iter);
06361 astman_append(s,
06362 "Event: QueueSummaryComplete\r\n"
06363 "%s"
06364 "\r\n", idText);
06365
06366 return RESULT_SUCCESS;
06367 }
06368
06369
06370 static int manager_queues_status(struct mansession *s, const struct message *m)
06371 {
06372 time_t now;
06373 int pos;
06374 const char *id = astman_get_header(m,"ActionID");
06375 const char *queuefilter = astman_get_header(m,"Queue");
06376 const char *memberfilter = astman_get_header(m,"Member");
06377 char idText[256] = "";
06378 struct call_queue *q;
06379 struct queue_ent *qe;
06380 float sl = 0;
06381 struct member *mem;
06382 struct ao2_iterator queue_iter;
06383 struct ao2_iterator mem_iter;
06384
06385 astman_send_ack(s, m, "Queue status will follow");
06386 time(&now);
06387 if (!ast_strlen_zero(id))
06388 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
06389
06390 queue_iter = ao2_iterator_init(queues, 0);
06391 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06392 ao2_lock(q);
06393
06394
06395 if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
06396 sl = ((q->callscompleted > 0) ? 100 * ((float)q->callscompletedinsl / (float)q->callscompleted) : 0);
06397 astman_append(s, "Event: QueueParams\r\n"
06398 "Queue: %s\r\n"
06399 "Max: %d\r\n"
06400 "Strategy: %s\r\n"
06401 "Calls: %d\r\n"
06402 "Holdtime: %d\r\n"
06403 "TalkTime: %d\r\n"
06404 "Completed: %d\r\n"
06405 "Abandoned: %d\r\n"
06406 "ServiceLevel: %d\r\n"
06407 "ServicelevelPerf: %2.1f\r\n"
06408 "Weight: %d\r\n"
06409 "%s"
06410 "\r\n",
06411 q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted,
06412 q->callsabandoned, q->servicelevel, sl, q->weight, idText);
06413
06414 mem_iter = ao2_iterator_init(q->members, 0);
06415 while ((mem = ao2_iterator_next(&mem_iter))) {
06416 if (ast_strlen_zero(memberfilter) || !strcmp(mem->interface, memberfilter) || !strcmp(mem->membername, memberfilter)) {
06417 astman_append(s, "Event: QueueMember\r\n"
06418 "Queue: %s\r\n"
06419 "Name: %s\r\n"
06420 "Location: %s\r\n"
06421 "Membership: %s\r\n"
06422 "Penalty: %d\r\n"
06423 "CallsTaken: %d\r\n"
06424 "LastCall: %d\r\n"
06425 "Status: %d\r\n"
06426 "Paused: %d\r\n"
06427 "%s"
06428 "\r\n",
06429 q->name, mem->membername, mem->interface, mem->dynamic ? "dynamic" : "static",
06430 mem->penalty, mem->calls, (int)mem->lastcall, mem->status, mem->paused, idText);
06431 }
06432 ao2_ref(mem, -1);
06433 }
06434 ao2_iterator_destroy(&mem_iter);
06435
06436 pos = 1;
06437 for (qe = q->head; qe; qe = qe->next) {
06438 astman_append(s, "Event: QueueEntry\r\n"
06439 "Queue: %s\r\n"
06440 "Position: %d\r\n"
06441 "Channel: %s\r\n"
06442 "Uniqueid: %s\r\n"
06443 "CallerIDNum: %s\r\n"
06444 "CallerIDName: %s\r\n"
06445 "Wait: %ld\r\n"
06446 "%s"
06447 "\r\n",
06448 q->name, pos++, qe->chan->name, qe->chan->uniqueid,
06449 S_OR(qe->chan->cid.cid_num, "unknown"),
06450 S_OR(qe->chan->cid.cid_name, "unknown"),
06451 (long) (now - qe->start), idText);
06452 }
06453 }
06454 ao2_unlock(q);
06455 queue_t_unref(q, "Done with iterator");
06456 }
06457 ao2_iterator_destroy(&queue_iter);
06458
06459 astman_append(s,
06460 "Event: QueueStatusComplete\r\n"
06461 "%s"
06462 "\r\n",idText);
06463
06464 return RESULT_SUCCESS;
06465 }
06466
06467 static int manager_add_queue_member(struct mansession *s, const struct message *m)
06468 {
06469 const char *queuename, *interface, *penalty_s, *paused_s, *membername, *state_interface;
06470 int paused, penalty = 0;
06471
06472 queuename = astman_get_header(m, "Queue");
06473 interface = astman_get_header(m, "Interface");
06474 penalty_s = astman_get_header(m, "Penalty");
06475 paused_s = astman_get_header(m, "Paused");
06476 membername = astman_get_header(m, "MemberName");
06477 state_interface = astman_get_header(m, "StateInterface");
06478
06479 if (ast_strlen_zero(queuename)) {
06480 astman_send_error(s, m, "'Queue' not specified.");
06481 return 0;
06482 }
06483
06484 if (ast_strlen_zero(interface)) {
06485 astman_send_error(s, m, "'Interface' not specified.");
06486 return 0;
06487 }
06488
06489 if (ast_strlen_zero(penalty_s))
06490 penalty = 0;
06491 else if (sscanf(penalty_s, "%30d", &penalty) != 1 || penalty < 0)
06492 penalty = 0;
06493
06494 if (ast_strlen_zero(paused_s))
06495 paused = 0;
06496 else
06497 paused = abs(ast_true(paused_s));
06498
06499 switch (add_to_queue(queuename, interface, membername, penalty, paused, queue_persistent_members, state_interface)) {
06500 case RES_OKAY:
06501 ast_queue_log(queuename, "MANAGER", interface, "ADDMEMBER", "%s", "");
06502 astman_send_ack(s, m, "Added interface to queue");
06503 break;
06504 case RES_EXISTS:
06505 astman_send_error(s, m, "Unable to add interface: Already there");
06506 break;
06507 case RES_NOSUCHQUEUE:
06508 astman_send_error(s, m, "Unable to add interface to queue: No such queue");
06509 break;
06510 case RES_OUTOFMEMORY:
06511 astman_send_error(s, m, "Out of memory");
06512 break;
06513 }
06514
06515 return 0;
06516 }
06517
06518 static int manager_remove_queue_member(struct mansession *s, const struct message *m)
06519 {
06520 const char *queuename, *interface;
06521
06522 queuename = astman_get_header(m, "Queue");
06523 interface = astman_get_header(m, "Interface");
06524
06525 if (ast_strlen_zero(queuename) || ast_strlen_zero(interface)) {
06526 astman_send_error(s, m, "Need 'Queue' and 'Interface' parameters.");
06527 return 0;
06528 }
06529
06530 switch (remove_from_queue(queuename, interface)) {
06531 case RES_OKAY:
06532 ast_queue_log(queuename, "MANAGER", interface, "REMOVEMEMBER", "%s", "");
06533 astman_send_ack(s, m, "Removed interface from queue");
06534 break;
06535 case RES_EXISTS:
06536 astman_send_error(s, m, "Unable to remove interface: Not there");
06537 break;
06538 case RES_NOSUCHQUEUE:
06539 astman_send_error(s, m, "Unable to remove interface from queue: No such queue");
06540 break;
06541 case RES_OUTOFMEMORY:
06542 astman_send_error(s, m, "Out of memory");
06543 break;
06544 case RES_NOT_DYNAMIC:
06545 astman_send_error(s, m, "Member not dynamic");
06546 break;
06547 }
06548
06549 return 0;
06550 }
06551
06552 static int manager_pause_queue_member(struct mansession *s, const struct message *m)
06553 {
06554 const char *queuename, *interface, *paused_s, *reason;
06555 int paused;
06556
06557 interface = astman_get_header(m, "Interface");
06558 paused_s = astman_get_header(m, "Paused");
06559 queuename = astman_get_header(m, "Queue");
06560 reason = astman_get_header(m, "Reason");
06561
06562 if (ast_strlen_zero(interface) || ast_strlen_zero(paused_s)) {
06563 astman_send_error(s, m, "Need 'Interface' and 'Paused' parameters.");
06564 return 0;
06565 }
06566
06567 paused = abs(ast_true(paused_s));
06568
06569 if (set_member_paused(queuename, interface, reason, paused))
06570 astman_send_error(s, m, "Interface not found");
06571 else
06572 astman_send_ack(s, m, paused ? "Interface paused successfully" : "Interface unpaused successfully");
06573 return 0;
06574 }
06575
06576 static int manager_queue_log_custom(struct mansession *s, const struct message *m)
06577 {
06578 const char *queuename, *event, *message, *interface, *uniqueid;
06579
06580 queuename = astman_get_header(m, "Queue");
06581 uniqueid = astman_get_header(m, "UniqueId");
06582 interface = astman_get_header(m, "Interface");
06583 event = astman_get_header(m, "Event");
06584 message = astman_get_header(m, "Message");
06585
06586 if (ast_strlen_zero(queuename) || ast_strlen_zero(event)) {
06587 astman_send_error(s, m, "Need 'Queue' and 'Event' parameters.");
06588 return 0;
06589 }
06590
06591 ast_queue_log(queuename, S_OR(uniqueid, "NONE"), interface, event, "%s", message);
06592 astman_send_ack(s, m, "Event added successfully");
06593
06594 return 0;
06595 }
06596
06597 static int manager_queue_reload(struct mansession *s, const struct message *m)
06598 {
06599 struct ast_flags mask = {0,};
06600 const char *queuename = NULL;
06601 int header_found = 0;
06602
06603 queuename = astman_get_header(m, "Queue");
06604 if (!strcasecmp(S_OR(astman_get_header(m, "Members"), ""), "yes")) {
06605 ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
06606 header_found = 1;
06607 }
06608 if (!strcasecmp(S_OR(astman_get_header(m, "Rules"), ""), "yes")) {
06609 ast_set_flag(&mask, QUEUE_RELOAD_RULES);
06610 header_found = 1;
06611 }
06612 if (!strcasecmp(S_OR(astman_get_header(m, "Parameters"), ""), "yes")) {
06613 ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
06614 header_found = 1;
06615 }
06616
06617 if (!header_found) {
06618 ast_set_flag(&mask, AST_FLAGS_ALL);
06619 }
06620
06621 if (!reload_handler(1, &mask, queuename)) {
06622 astman_send_ack(s, m, "Queue reloaded successfully");
06623 } else {
06624 astman_send_error(s, m, "Error encountered while reloading queue");
06625 }
06626 return 0;
06627 }
06628
06629 static int manager_queue_reset(struct mansession *s, const struct message *m)
06630 {
06631 const char *queuename = NULL;
06632 struct ast_flags mask = {QUEUE_RESET_STATS,};
06633
06634 queuename = astman_get_header(m, "Queue");
06635
06636 if (!reload_handler(1, &mask, queuename)) {
06637 astman_send_ack(s, m, "Queue stats reset successfully");
06638 } else {
06639 astman_send_error(s, m, "Error encountered while resetting queue stats");
06640 }
06641 return 0;
06642 }
06643
06644 static char *complete_queue_add_member(const char *line, const char *word, int pos, int state)
06645 {
06646
06647 switch (pos) {
06648 case 3:
06649 return NULL;
06650 case 4:
06651 return state == 0 ? ast_strdup("to") : NULL;
06652 case 5:
06653 return complete_queue(line, word, pos, state);
06654 case 6:
06655 return state == 0 ? ast_strdup("penalty") : NULL;
06656 case 7:
06657 if (state < 100) {
06658 char *num;
06659 if ((num = ast_malloc(3))) {
06660 sprintf(num, "%d", state);
06661 }
06662 return num;
06663 } else {
06664 return NULL;
06665 }
06666 case 8:
06667 return state == 0 ? ast_strdup("as") : NULL;
06668 case 9:
06669 return NULL;
06670 default:
06671 return NULL;
06672 }
06673 }
06674
06675 static int manager_queue_member_penalty(struct mansession *s, const struct message *m)
06676 {
06677 const char *queuename, *interface, *penalty_s;
06678 int penalty;
06679
06680 interface = astman_get_header(m, "Interface");
06681 penalty_s = astman_get_header(m, "Penalty");
06682
06683 queuename = astman_get_header(m, "Queue");
06684
06685 if (ast_strlen_zero(interface) || ast_strlen_zero(penalty_s)) {
06686 astman_send_error(s, m, "Need 'Interface' and 'Penalty' parameters.");
06687 return 0;
06688 }
06689
06690 penalty = atoi(penalty_s);
06691
06692 if (set_member_penalty((char *)queuename, (char *)interface, penalty))
06693 astman_send_error(s, m, "Invalid interface, queuename or penalty");
06694 else
06695 astman_send_ack(s, m, "Interface penalty set successfully");
06696
06697 return 0;
06698 }
06699
06700 static char *handle_queue_add_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06701 {
06702 char *queuename, *interface, *membername = NULL, *state_interface = NULL;
06703 int penalty;
06704
06705 switch ( cmd ) {
06706 case CLI_INIT:
06707 e->command = "queue add member";
06708 e->usage =
06709 "Usage: queue add member <channel> to <queue> [[[penalty <penalty>] as <membername>] state_interface <interface>]\n"
06710 " Add a channel to a queue with optionally: a penalty, membername and a state_interface\n";
06711 return NULL;
06712 case CLI_GENERATE:
06713 return complete_queue_add_member(a->line, a->word, a->pos, a->n);
06714 }
06715
06716 if ((a->argc != 6) && (a->argc != 8) && (a->argc != 10) && (a->argc != 12)) {
06717 return CLI_SHOWUSAGE;
06718 } else if (strcmp(a->argv[4], "to")) {
06719 return CLI_SHOWUSAGE;
06720 } else if ((a->argc >= 8) && strcmp(a->argv[6], "penalty")) {
06721 return CLI_SHOWUSAGE;
06722 } else if ((a->argc >= 10) && strcmp(a->argv[8], "as")) {
06723 return CLI_SHOWUSAGE;
06724 } else if ((a->argc == 12) && strcmp(a->argv[10], "state_interface")) {
06725 return CLI_SHOWUSAGE;
06726 }
06727
06728 queuename = a->argv[5];
06729 interface = a->argv[3];
06730 if (a->argc >= 8) {
06731 if (sscanf(a->argv[7], "%30d", &penalty) == 1) {
06732 if (penalty < 0) {
06733 ast_cli(a->fd, "Penalty must be >= 0\n");
06734 penalty = 0;
06735 }
06736 } else {
06737 ast_cli(a->fd, "Penalty must be an integer >= 0\n");
06738 penalty = 0;
06739 }
06740 } else {
06741 penalty = 0;
06742 }
06743
06744 if (a->argc >= 10) {
06745 membername = a->argv[9];
06746 }
06747
06748 if (a->argc >= 12) {
06749 state_interface = a->argv[11];
06750 }
06751
06752 switch (add_to_queue(queuename, interface, membername, penalty, 0, queue_persistent_members, state_interface)) {
06753 case RES_OKAY:
06754 ast_queue_log(queuename, "CLI", interface, "ADDMEMBER", "%s", "");
06755 ast_cli(a->fd, "Added interface '%s' to queue '%s'\n", interface, queuename);
06756 return CLI_SUCCESS;
06757 case RES_EXISTS:
06758 ast_cli(a->fd, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
06759 return CLI_FAILURE;
06760 case RES_NOSUCHQUEUE:
06761 ast_cli(a->fd, "Unable to add interface to queue '%s': No such queue\n", queuename);
06762 return CLI_FAILURE;
06763 case RES_OUTOFMEMORY:
06764 ast_cli(a->fd, "Out of memory\n");
06765 return CLI_FAILURE;
06766 case RES_NOT_DYNAMIC:
06767 ast_cli(a->fd, "Member not dynamic\n");
06768 return CLI_FAILURE;
06769 default:
06770 return CLI_FAILURE;
06771 }
06772 }
06773
06774 static char *complete_queue_remove_member(const char *line, const char *word, int pos, int state)
06775 {
06776 int which = 0;
06777 struct call_queue *q;
06778 struct member *m;
06779 struct ao2_iterator queue_iter;
06780 struct ao2_iterator mem_iter;
06781 int wordlen = strlen(word);
06782
06783
06784 if (pos > 5 || pos < 3)
06785 return NULL;
06786 if (pos == 4)
06787 return (state == 0 ? ast_strdup("from") : NULL);
06788
06789 if (pos == 5)
06790 return complete_queue(line, word, pos, state);
06791
06792
06793 queue_iter = ao2_iterator_init(queues, 0);
06794 while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06795 ao2_lock(q);
06796 mem_iter = ao2_iterator_init(q->members, 0);
06797 while ((m = ao2_iterator_next(&mem_iter))) {
06798 if (!strncasecmp(word, m->membername, wordlen) && ++which > state) {
06799 char *tmp;
06800 ao2_unlock(q);
06801 tmp = ast_strdup(m->interface);
06802 ao2_ref(m, -1);
06803 queue_t_unref(q, "Done with iterator, returning interface name");
06804 ao2_iterator_destroy(&mem_iter);
06805 ao2_iterator_destroy(&queue_iter);
06806 return tmp;
06807 }
06808 ao2_ref(m, -1);
06809 }
06810 ao2_iterator_destroy(&mem_iter);
06811 ao2_unlock(q);
06812 queue_t_unref(q, "Done with iterator");
06813 }
06814 ao2_iterator_destroy(&queue_iter);
06815
06816 return NULL;
06817 }
06818
06819 static char *handle_queue_remove_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06820 {
06821 char *queuename, *interface;
06822
06823 switch (cmd) {
06824 case CLI_INIT:
06825 e->command = "queue remove member";
06826 e->usage =
06827 "Usage: queue remove member <channel> from <queue>\n"
06828 " Remove a specific channel from a queue.\n";
06829 return NULL;
06830 case CLI_GENERATE:
06831 return complete_queue_remove_member(a->line, a->word, a->pos, a->n);
06832 }
06833
06834 if (a->argc != 6) {
06835 return CLI_SHOWUSAGE;
06836 } else if (strcmp(a->argv[4], "from")) {
06837 return CLI_SHOWUSAGE;
06838 }
06839
06840 queuename = a->argv[5];
06841 interface = a->argv[3];
06842
06843 switch (remove_from_queue(queuename, interface)) {
06844 case RES_OKAY:
06845 ast_queue_log(queuename, "CLI", interface, "REMOVEMEMBER", "%s", "");
06846 ast_cli(a->fd, "Removed interface '%s' from queue '%s'\n", interface, queuename);
06847 return CLI_SUCCESS;
06848 case RES_EXISTS:
06849 ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
06850 return CLI_FAILURE;
06851 case RES_NOSUCHQUEUE:
06852 ast_cli(a->fd, "Unable to remove interface from queue '%s': No such queue\n", queuename);
06853 return CLI_FAILURE;
06854 case RES_OUTOFMEMORY:
06855 ast_cli(a->fd, "Out of memory\n");
06856 return CLI_FAILURE;
06857 case RES_NOT_DYNAMIC:
06858 ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Member is not dynamic\n", interface, queuename);
06859 return CLI_FAILURE;
06860 default:
06861 return CLI_FAILURE;
06862 }
06863 }
06864
06865 static char *complete_queue_pause_member(const char *line, const char *word, int pos, int state)
06866 {
06867
06868 switch (pos) {
06869 case 3:
06870 return NULL;
06871 case 4:
06872 return state == 0 ? ast_strdup("queue") : NULL;
06873 case 5:
06874 return complete_queue(line, word, pos, state);
06875 case 6:
06876 return state == 0 ? ast_strdup("reason") : NULL;
06877 case 7:
06878 return NULL;
06879 default:
06880 return NULL;
06881 }
06882 }
06883
06884 static char *handle_queue_pause_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06885 {
06886 char *queuename, *interface, *reason;
06887 int paused;
06888
06889 switch (cmd) {
06890 case CLI_INIT:
06891 e->command = "queue {pause|unpause} member";
06892 e->usage =
06893 "Usage: queue {pause|unpause} member <member> [queue <queue> [reason <reason>]]\n"
06894 " Pause or unpause a queue member. Not specifying a particular queue\n"
06895 " will pause or unpause a member across all queues to which the member\n"
06896 " belongs.\n";
06897 return NULL;
06898 case CLI_GENERATE:
06899 return complete_queue_pause_member(a->line, a-> word, a->pos, a->n);
06900 }
06901
06902 if (a->argc < 4 || a->argc == 5 || a->argc == 7 || a->argc > 8) {
06903 return CLI_SHOWUSAGE;
06904 } else if (a->argc >= 5 && strcmp(a->argv[4], "queue")) {
06905 return CLI_SHOWUSAGE;
06906 } else if (a->argc == 8 && strcmp(a->argv[6], "reason")) {
06907 return CLI_SHOWUSAGE;
06908 }
06909
06910
06911 interface = a->argv[3];
06912 queuename = a->argc >= 6 ? a->argv[5] : NULL;
06913 reason = a->argc == 8 ? a->argv[7] : NULL;
06914 paused = !strcasecmp(a->argv[1], "pause");
06915
06916 if (set_member_paused(queuename, interface, reason, paused) == RESULT_SUCCESS) {
06917 ast_cli(a->fd, "%spaused interface '%s'", paused ? "" : "un", interface);
06918 if (!ast_strlen_zero(queuename))
06919 ast_cli(a->fd, " in queue '%s'", queuename);
06920 if (!ast_strlen_zero(reason))
06921 ast_cli(a->fd, " for reason '%s'", reason);
06922 ast_cli(a->fd, "\n");
06923 return CLI_SUCCESS;
06924 } else {
06925 ast_cli(a->fd, "Unable to %spause interface '%s'", paused ? "" : "un", interface);
06926 if (!ast_strlen_zero(queuename))
06927 ast_cli(a->fd, " in queue '%s'", queuename);
06928 if (!ast_strlen_zero(reason))
06929 ast_cli(a->fd, " for reason '%s'", reason);
06930 ast_cli(a->fd, "\n");
06931 return CLI_FAILURE;
06932 }
06933 }
06934
06935 static char *complete_queue_set_member_penalty(const char *line, const char *word, int pos, int state)
06936 {
06937
06938 switch (pos) {
06939 case 4:
06940 if (state == 0) {
06941 return ast_strdup("on");
06942 } else {
06943 return NULL;
06944 }
06945 case 6:
06946 if (state == 0) {
06947 return ast_strdup("in");
06948 } else {
06949 return NULL;
06950 }
06951 case 7:
06952 return complete_queue(line, word, pos, state);
06953 default:
06954 return NULL;
06955 }
06956 }
06957
06958 static char *handle_queue_set_member_penalty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06959 {
06960 char *queuename = NULL, *interface;
06961 int penalty = 0;
06962
06963 switch (cmd) {
06964 case CLI_INIT:
06965 e->command = "queue set penalty";
06966 e->usage =
06967 "Usage: queue set penalty <penalty> on <interface> [in <queue>]\n"
06968 " Set a member's penalty in the queue specified. If no queue is specified\n"
06969 " then that interface's penalty is set in all queues to which that interface is a member\n";
06970 return NULL;
06971 case CLI_GENERATE:
06972 return complete_queue_set_member_penalty(a->line, a->word, a->pos, a->n);
06973 }
06974
06975 if (a->argc != 6 && a->argc != 8) {
06976 return CLI_SHOWUSAGE;
06977 } else if (strcmp(a->argv[4], "on") || (a->argc > 6 && strcmp(a->argv[6], "in"))) {
06978 return CLI_SHOWUSAGE;
06979 }
06980
06981 if (a->argc == 8)
06982 queuename = a->argv[7];
06983 interface = a->argv[5];
06984 penalty = atoi(a->argv[3]);
06985
06986 switch (set_member_penalty(queuename, interface, penalty)) {
06987 case RESULT_SUCCESS:
06988 ast_cli(a->fd, "Set penalty on interface '%s' from queue '%s'\n", interface, queuename);
06989 return CLI_SUCCESS;
06990 case RESULT_FAILURE:
06991 ast_cli(a->fd, "Failed to set penalty on interface '%s' from queue '%s'\n", interface, queuename);
06992 return CLI_FAILURE;
06993 default:
06994 return CLI_FAILURE;
06995 }
06996 }
06997
06998 static char *complete_queue_rule_show(const char *line, const char *word, int pos, int state)
06999 {
07000 int which = 0;
07001 struct rule_list *rl_iter;
07002 int wordlen = strlen(word);
07003 char *ret = NULL;
07004 if (pos != 3) {
07005 return NULL;
07006 }
07007
07008 AST_LIST_LOCK(&rule_lists);
07009 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
07010 if (!strncasecmp(word, rl_iter->name, wordlen) && ++which > state) {
07011 ret = ast_strdup(rl_iter->name);
07012 break;
07013 }
07014 }
07015 AST_LIST_UNLOCK(&rule_lists);
07016
07017 return ret;
07018 }
07019
07020 static char *handle_queue_rule_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07021 {
07022 char *rule;
07023 struct rule_list *rl_iter;
07024 struct penalty_rule *pr_iter;
07025 switch (cmd) {
07026 case CLI_INIT:
07027 e->command = "queue show rules";
07028 e->usage =
07029 "Usage: queue show rules [rulename]\n"
07030 " Show the list of rules associated with rulename. If no\n"
07031 " rulename is specified, list all rules defined in queuerules.conf\n";
07032 return NULL;
07033 case CLI_GENERATE:
07034 return complete_queue_rule_show(a->line, a->word, a->pos, a->n);
07035 }
07036
07037 if (a->argc != 3 && a->argc != 4)
07038 return CLI_SHOWUSAGE;
07039
07040 rule = a->argc == 4 ? a->argv[3] : "";
07041 AST_LIST_LOCK(&rule_lists);
07042 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
07043 if (ast_strlen_zero(rule) || !strcasecmp(rl_iter->name, rule)) {
07044 ast_cli(a->fd, "Rule: %s\n", rl_iter->name);
07045 AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
07046 ast_cli(a->fd, "\tAfter %d seconds, adjust QUEUE_MAX_PENALTY %s %d and adjust QUEUE_MIN_PENALTY %s %d\n", pr_iter->time, pr_iter->max_relative ? "by" : "to", pr_iter->max_value, pr_iter->min_relative ? "by" : "to", pr_iter->min_value);
07047 }
07048 }
07049 }
07050 AST_LIST_UNLOCK(&rule_lists);
07051 return CLI_SUCCESS;
07052 }
07053
07054 static char *handle_queue_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07055 {
07056 struct ast_flags mask = {QUEUE_RESET_STATS,};
07057 int i;
07058
07059 switch (cmd) {
07060 case CLI_INIT:
07061 e->command = "queue reset stats";
07062 e->usage =
07063 "Usage: queue reset stats [<queuenames>]\n"
07064 "\n"
07065 "Issuing this command will reset statistics for\n"
07066 "<queuenames>, or for all queues if no queue is\n"
07067 "specified.\n";
07068 return NULL;
07069 case CLI_GENERATE:
07070 if (a->pos >= 3) {
07071 return complete_queue(a->line, a->word, a->pos, a->n);
07072 } else {
07073 return NULL;
07074 }
07075 }
07076
07077 if (a->argc < 3) {
07078 return CLI_SHOWUSAGE;
07079 }
07080
07081 if (a->argc == 3) {
07082 reload_handler(1, &mask, NULL);
07083 return CLI_SUCCESS;
07084 }
07085
07086 for (i = 3; i < a->argc; ++i) {
07087 reload_handler(1, &mask, a->argv[i]);
07088 }
07089
07090 return CLI_SUCCESS;
07091 }
07092
07093 static char *handle_queue_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07094 {
07095 struct ast_flags mask = {0,};
07096 int i;
07097
07098 switch (cmd) {
07099 case CLI_INIT:
07100 e->command = "queue reload {parameters|members|rules|all}";
07101 e->usage =
07102 "Usage: queue reload {parameters|members|rules|all} [<queuenames>]\n"
07103 "Reload queues. If <queuenames> are specified, only reload information pertaining\n"
07104 "to <queuenames>. One of 'parameters,' 'members,' 'rules,' or 'all' must be\n"
07105 "specified in order to know what information to reload. Below is an explanation\n"
07106 "of each of these qualifiers.\n"
07107 "\n"
07108 "\t'members' - reload queue members from queues.conf\n"
07109 "\t'parameters' - reload all queue options except for queue members\n"
07110 "\t'rules' - reload the queuerules.conf file\n"
07111 "\t'all' - reload queue rules, parameters, and members\n"
07112 "\n"
07113 "Note: the 'rules' qualifier here cannot actually be applied to a specific queue.\n"
07114 "Use of the 'rules' qualifier causes queuerules.conf to be reloaded. Even if only\n"
07115 "one queue is specified when using this command, reloading queue rules may cause\n"
07116 "other queues to be affected\n";
07117 return NULL;
07118 case CLI_GENERATE:
07119 if (a->pos >= 3) {
07120 return complete_queue(a->line, a->word, a->pos, a->n);
07121 } else {
07122 return NULL;
07123 }
07124 }
07125
07126 if (a->argc < 3)
07127 return CLI_SHOWUSAGE;
07128
07129 if (!strcasecmp(a->argv[2], "rules")) {
07130 ast_set_flag(&mask, QUEUE_RELOAD_RULES);
07131 } else if (!strcasecmp(a->argv[2], "members")) {
07132 ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
07133 } else if (!strcasecmp(a->argv[2], "parameters")) {
07134 ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
07135 } else if (!strcasecmp(a->argv[2], "all")) {
07136 ast_set_flag(&mask, AST_FLAGS_ALL);
07137 }
07138
07139 if (a->argc == 3) {
07140 reload_handler(1, &mask, NULL);
07141 return CLI_SUCCESS;
07142 }
07143
07144 for (i = 3; i < a->argc; ++i) {
07145 reload_handler(1, &mask, a->argv[i]);
07146 }
07147
07148 return CLI_SUCCESS;
07149 }
07150
07151 static const char qpm_cmd_usage[] =
07152 "Usage: queue pause member <channel> in <queue> reason <reason>\n";
07153
07154 static const char qum_cmd_usage[] =
07155 "Usage: queue unpause member <channel> in <queue> reason <reason>\n";
07156
07157 static const char qsmp_cmd_usage[] =
07158 "Usage: queue set member penalty <channel> from <queue> <penalty>\n";
07159
07160 static struct ast_cli_entry cli_queue[] = {
07161 AST_CLI_DEFINE(queue_show, "Show status of a specified queue"),
07162 AST_CLI_DEFINE(handle_queue_add_member, "Add a channel to a specified queue"),
07163 AST_CLI_DEFINE(handle_queue_remove_member, "Removes a channel from a specified queue"),
07164 AST_CLI_DEFINE(handle_queue_pause_member, "Pause or unpause a queue member"),
07165 AST_CLI_DEFINE(handle_queue_set_member_penalty, "Set penalty for a channel of a specified queue"),
07166 AST_CLI_DEFINE(handle_queue_rule_show, "Show the rules defined in queuerules.conf"),
07167 AST_CLI_DEFINE(handle_queue_reload, "Reload queues, members, queue rules, or parameters"),
07168 AST_CLI_DEFINE(handle_queue_reset, "Reset statistics for a queue"),
07169 };
07170
07171 static int unload_module(void)
07172 {
07173 int res;
07174 struct ast_context *con;
07175 struct ao2_iterator q_iter;
07176 struct call_queue *q = NULL;
07177
07178 ast_cli_unregister_multiple(cli_queue, ARRAY_LEN(cli_queue));
07179 res = ast_manager_unregister("QueueStatus");
07180 res |= ast_manager_unregister("Queues");
07181 res |= ast_manager_unregister("QueueRule");
07182 res |= ast_manager_unregister("QueueSummary");
07183 res |= ast_manager_unregister("QueueAdd");
07184 res |= ast_manager_unregister("QueueRemove");
07185 res |= ast_manager_unregister("QueuePause");
07186 res |= ast_manager_unregister("QueueLog");
07187 res |= ast_manager_unregister("QueuePenalty");
07188 res |= ast_unregister_application(app_aqm);
07189 res |= ast_unregister_application(app_rqm);
07190 res |= ast_unregister_application(app_pqm);
07191 res |= ast_unregister_application(app_upqm);
07192 res |= ast_unregister_application(app_ql);
07193 res |= ast_unregister_application(app);
07194 res |= ast_custom_function_unregister(&queuevar_function);
07195 res |= ast_custom_function_unregister(&queuemembercount_function);
07196 res |= ast_custom_function_unregister(&queuemembercount_dep);
07197 res |= ast_custom_function_unregister(&queuememberlist_function);
07198 res |= ast_custom_function_unregister(&queuewaitingcount_function);
07199 res |= ast_custom_function_unregister(&queuememberpenalty_function);
07200
07201 if (device_state_sub)
07202 ast_event_unsubscribe(device_state_sub);
07203
07204 if ((con = ast_context_find("app_queue_gosub_virtual_context"))) {
07205 ast_context_remove_extension2(con, "s", 1, NULL, 0);
07206 ast_context_destroy(con, "app_queue");
07207 }
07208
07209 q_iter = ao2_iterator_init(queues, 0);
07210 while ((q = ao2_t_iterator_next(&q_iter, "Iterate through queues"))) {
07211 queues_t_unlink(queues, q, "Remove queue from container due to unload");
07212 queue_t_unref(q, "Done with iterator");
07213 }
07214 ao2_iterator_destroy(&q_iter);
07215 ao2_ref(queues, -1);
07216 devicestate_tps = ast_taskprocessor_unreference(devicestate_tps);
07217 ast_unload_realtime("queue_members");
07218 return res;
07219 }
07220
07221 static int load_module(void)
07222 {
07223 int res;
07224 struct ast_context *con;
07225 struct ast_flags mask = {AST_FLAGS_ALL, };
07226
07227 queues = ao2_container_alloc(MAX_QUEUE_BUCKETS, queue_hash_cb, queue_cmp_cb);
07228
07229 use_weight = 0;
07230
07231 if (reload_handler(0, &mask, NULL))
07232 return AST_MODULE_LOAD_DECLINE;
07233
07234 con = ast_context_find_or_create(NULL, NULL, "app_queue_gosub_virtual_context", "app_queue");
07235 if (!con)
07236 ast_log(LOG_ERROR, "Queue virtual context 'app_queue_gosub_virtual_context' does not exist and unable to create\n");
07237 else
07238 ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_queue");
07239
07240 if (queue_persistent_members)
07241 reload_queue_members();
07242
07243 ast_cli_register_multiple(cli_queue, ARRAY_LEN(cli_queue));
07244 res = ast_register_application_xml(app, queue_exec);
07245 res |= ast_register_application_xml(app_aqm, aqm_exec);
07246 res |= ast_register_application_xml(app_rqm, rqm_exec);
07247 res |= ast_register_application_xml(app_pqm, pqm_exec);
07248 res |= ast_register_application_xml(app_upqm, upqm_exec);
07249 res |= ast_register_application_xml(app_ql, ql_exec);
07250 res |= ast_manager_register("Queues", 0, manager_queues_show, "Queues");
07251 res |= ast_manager_register("QueueStatus", 0, manager_queues_status, "Queue Status");
07252 res |= ast_manager_register("QueueSummary", 0, manager_queues_summary, "Queue Summary");
07253 res |= ast_manager_register("QueueAdd", EVENT_FLAG_AGENT, manager_add_queue_member, "Add interface to queue.");
07254 res |= ast_manager_register("QueueRemove", EVENT_FLAG_AGENT, manager_remove_queue_member, "Remove interface from queue.");
07255 res |= ast_manager_register("QueuePause", EVENT_FLAG_AGENT, manager_pause_queue_member, "Makes a queue member temporarily unavailable");
07256 res |= ast_manager_register("QueueLog", EVENT_FLAG_AGENT, manager_queue_log_custom, "Adds custom entry in queue_log");
07257 res |= ast_manager_register("QueuePenalty", EVENT_FLAG_AGENT, manager_queue_member_penalty, "Set the penalty for a queue member");
07258 res |= ast_manager_register("QueueRule", 0, manager_queue_rule_show, "Queue Rules");
07259 res |= ast_manager_register("QueueReload", 0, manager_queue_reload, "Reload a queue, queues, or any sub-section of a queue or queues");
07260 res |= ast_manager_register("QueueReset", 0, manager_queue_reset, "Reset queue statistics");
07261 res |= ast_custom_function_register(&queuevar_function);
07262 res |= ast_custom_function_register(&queuemembercount_function);
07263 res |= ast_custom_function_register(&queuemembercount_dep);
07264 res |= ast_custom_function_register(&queuememberlist_function);
07265 res |= ast_custom_function_register(&queuewaitingcount_function);
07266 res |= ast_custom_function_register(&queuememberpenalty_function);
07267
07268 if (!(devicestate_tps = ast_taskprocessor_get("app_queue", 0))) {
07269 ast_log(LOG_WARNING, "devicestate taskprocessor reference failed - devicestate notifications will not occur\n");
07270 }
07271
07272 if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL, AST_EVENT_IE_END))) {
07273 res = -1;
07274 }
07275
07276 ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, SENTINEL);
07277
07278 return res ? AST_MODULE_LOAD_DECLINE : 0;
07279 }
07280
07281 static int reload(void)
07282 {
07283 struct ast_flags mask = {AST_FLAGS_ALL,};
07284 ast_unload_realtime("queue_members");
07285 reload_handler(1, &mask, NULL);
07286 return 0;
07287 }
07288
07289 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "True Call Queueing",
07290 .load = load_module,
07291 .unload = unload_module,
07292 .reload = reload,
07293 );
07294