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