8094904b0634e7ceaa9b5aff4466081609070e53
[asterisk/asterisk.git] / apps / app_queue.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * True call queues with optional send URL on answer
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * These features added by David C. Troy <dave@toad.net>:
11  *    - Per-queue holdtime calculation
12  *    - Estimated holdtime announcement
13  *    - Position announcement
14  *    - Abandoned/completed call counters
15  *    - Failout timer passed as optional app parameter
16  *    - Optional monitoring of calls, started when call is answered
17  *
18  * Patch Version 1.07 2003-12-24 01
19  *
20  * Added servicelevel statistic by Michiel Betel <michiel@betel.nl>
21  *
22  * Fixed ot work with CVS as of 2004-02-25 and released as 1.07a
23  * by Matthew Enger <m.enger@xi.com.au>
24  *
25  * This program is free software, distributed under the terms of
26  * the GNU General Public License
27  */
28
29 #include <asterisk/lock.h>
30 #include <asterisk/file.h>
31 #include <asterisk/logger.h>
32 #include <asterisk/channel.h>
33 #include <asterisk/pbx.h>
34 #include <asterisk/options.h>
35 #include <asterisk/module.h>
36 #include <asterisk/translate.h>
37 #include <asterisk/say.h>
38 #include <asterisk/parking.h>
39 #include <asterisk/musiconhold.h>
40 #include <asterisk/cli.h>
41 #include <asterisk/manager.h> /* JDG */
42 #include <asterisk/config.h>
43 #include <asterisk/monitor.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <sys/time.h>
51 #include <sys/signal.h>
52 #include <netinet/in.h>
53
54 #include "../astconf.h"
55
56 #include <pthread.h>
57
58 #define QUEUE_STRATEGY_RINGALL          0
59 #define QUEUE_STRATEGY_ROUNDROBIN       1
60 #define QUEUE_STRATEGY_LEASTRECENT      2
61 #define QUEUE_STRATEGY_FEWESTCALLS      3
62 #define QUEUE_STRATEGY_RANDOM           4
63
64 static struct strategy {
65         int strategy;
66         char *name;
67 } strategies[] = {
68         { QUEUE_STRATEGY_RINGALL, "ringall" },
69         { QUEUE_STRATEGY_ROUNDROBIN, "roundrobin" },
70         { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
71         { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
72         { QUEUE_STRATEGY_RANDOM, "random" },
73 };
74
75 #define DEFAULT_RETRY           5
76 #define DEFAULT_TIMEOUT         15
77 #define RECHECK                         1               /* Recheck every second to see we we're at the top yet */
78
79 static char *tdesc = "True Call Queueing";
80
81 static char *app = "Queue";
82
83 static char *synopsis = "Queue a call for a call queue";
84
85 static char *descrip =
86 "  Queue(queuename[|options[|URL][|announceoverride][|timeout]]):\n"
87 "Queues an incoming call in a particular call queue as defined in queues.conf.\n"
88 "  This application returns -1 if the originating channel hangs up, or if the\n"
89 "call is bridged and  either of the parties in the bridge terminate the call.\n"
90 "Returns 0 if the queue is full, nonexistant, or has no members.\n"
91 "The option string may contain zero or more of the following characters:\n"
92 "      't' -- allow the called user transfer the calling user\n"
93 "      'T' -- to allow the calling user to transfer the call.\n"
94 "      'd' -- data-quality (modem) call (minimum delay).\n"
95 "      'H' -- allow caller to hang up by hitting *.\n"
96 "      'n' -- no retries on the timeout; will exit this application and go to the next step.\n"
97 "  In addition to transferring the call, a call may be parked and then picked\n"
98 "up by another user.\n"
99 "  The optional URL will be sent to the called party if the channel supports\n"
100 "it.\n"
101 "  The timeout will cause the queue to fail out after a specified number of\n"
102 "seconds, checked between each queues.conf 'timeout' and 'retry' cycle.\n";
103
104 // [PHM 06/26/03]
105 static char *app_aqm = "AddQueueMember" ;
106 static char *app_aqm_synopsis = "Dynamically adds queue members" ;
107 static char *app_aqm_descrip =
108 "   AddQueueMember(queuename[|interface]):\n"
109 "Dynamically adds interface to an existing queue\n"
110 "Returns -1 if there is an error.\n"
111 "Example: AddQueueMember(techsupport|SIP/3000)\n"
112 "";
113
114 static char *app_rqm = "RemoveQueueMember" ;
115 static char *app_rqm_synopsis = "Dynamically removes queue members" ;
116 static char *app_rqm_descrip =
117 "   RemoveQueueMember(queuename[|interface]):\n"
118 "Dynamically removes interface to an existing queue\n"
119 "Returns -1 if there is an error.\n"
120 "Example: RemoveQueueMember(techsupport|SIP/3000)\n"
121 "";
122
123 /* We define a customer "local user" structure because we
124    use it not only for keeping track of what is in use but
125    also for keeping track of who we're dialing. */
126
127 struct localuser {
128         struct ast_channel *chan;
129         char numsubst[256];
130         char tech[40];
131         int stillgoing;
132         int metric;
133         int allowredirect_in;
134         int allowredirect_out;
135         int ringbackonly;
136         int musiconhold;
137         int dataquality;
138         int allowdisconnect;
139         struct member *member;
140         struct localuser *next;
141 };
142
143 LOCAL_USER_DECL;
144
145 struct queue_ent {
146         struct ast_call_queue *parent;  /* What queue is our parent */
147         char moh[80];                           /* Name of musiconhold to be used */
148         char announce[80];              /* Announcement to play for member when call is answered */
149         char context[80];               /* Context when user exits queue */
150         int pos;                                        /* Where we are in the queue */
151         int last_pos_said;              /* Last position we told the user */
152         time_t last_pos;                /* Last time we told the user their position */
153         int opos;                                       /* Where we started in the queue */
154         int handled;                            /* Whether our call was handled */
155         time_t start;                           /* When we started holding */
156         int queuetimeout;               /* How many seconds before timing out of queue */
157         struct ast_channel *chan;       /* Our channel */
158         struct queue_ent *next;         /* The next queue entry */
159 };
160
161 struct member {
162         char tech[80];                          /* Technology */
163         char loc[256];                          /* Location */
164         int penalty;                            /* Are we a last resort? */
165         int calls;
166         int dynamic;                            /* Are we dynamically added? */
167         time_t lastcall;        /* When last successful call was hungup */
168         struct member *next;            /* Next member */
169 };
170
171 struct ast_call_queue {
172         ast_mutex_t     lock;   
173         char name[80];                  /* Name of the queue */
174         char moh[80];                   /* Name of musiconhold to be used */
175         char announce[80];              /* Announcement to play when call is answered */
176         char context[80];               /* Context for this queue */
177         int strategy;                   /* Queueing strategy */
178         int announcefrequency;          /* How often to announce their position */
179         int announceholdtime;           /* When to announce holdtime: 0 = never, -1 = every announcement, 1 = only once */
180         int holdtime;                   /* Current avg holdtime for this queue, based on recursive boxcar filter */
181         int callscompleted;             /* Number of queue calls completed */
182         int callsabandoned;             /* Number of queue calls abandoned */
183         int servicelevel;               /* seconds setting for servicelevel*/
184         int callscompletedinsl;         /* Number of queue calls answererd with servicelevel*/
185         char monfmt[8];                 /* Format to use when recording calls */
186         char sound_next[80];            /* Sound file: "Your call is now first in line" (def. queue-youarenext) */
187         char sound_thereare[80];        /* Sound file: "There are currently" (def. queue-thereare) */
188         char sound_calls[80];           /* Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting)*/
189         char sound_holdtime[80];        /* Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
190         char sound_minutes[80];         /* Sound file: "minutes." (def. queue-minutes) */
191         char sound_thanks[80];          /* Sound file: "Thank you for your patience." (def. queue-thankyou) */
192
193         int count;                              /* How many entries are in the queue */
194         int maxlen;                             /* Max number of entries in queue */
195
196         int dead;                               /* Whether this queue is dead or not */
197         int retry;                              /* Retry calling everyone after this amount of time */
198         int timeout;                    /* How long to wait for an answer */
199         
200         /* Queue strategy things */
201         
202         int rrpos;                              /* Round Robin - position */
203         int wrapped;                    /* Round Robin - wrapped around? */
204
205         struct member *members; /* Member channels to be tried */
206         struct queue_ent *head; /* Start of the actual queue */
207         struct ast_call_queue *next;    /* Next call queue */
208 };
209
210 static struct ast_call_queue *queues = NULL;
211 static ast_mutex_t qlock = AST_MUTEX_INITIALIZER;
212
213 static char *int2strat(int strategy)
214 {
215         int x;
216         for (x=0;x<sizeof(strategies) / sizeof(strategies[0]);x++) {
217                 if (strategy == strategies[x].strategy)
218                         return strategies[x].name;
219         }
220         return "<unknown>";
221 }
222
223 static int strat2int(char *strategy)
224 {
225         int x;
226         for (x=0;x<sizeof(strategies) / sizeof(strategies[0]);x++) {
227                 if (!strcasecmp(strategy, strategies[x].name))
228                         return strategies[x].strategy;
229         }
230         return -1;
231 }
232
233 static int join_queue(char *queuename, struct queue_ent *qe)
234 {
235         struct ast_call_queue *q;
236         struct queue_ent *cur, *prev = NULL;
237         int res = -1;
238         int pos = 0;
239         ast_mutex_lock(&qlock);
240         q = queues;
241         while(q) {
242                 if (!strcasecmp(q->name, queuename)) {
243                         /* This is our one */
244                         ast_mutex_lock(&q->lock);
245                         if (q->members && (!q->maxlen || (q->count < q->maxlen))) {
246                                 /* There's space for us, put us at the end */
247                                 prev = NULL;
248                                 cur = q->head;
249                                 while(cur) {
250                                         cur->pos = ++pos;
251                                         prev = cur;
252                                         cur = cur->next;
253                                 }
254                                 if (prev)
255                                         prev->next = qe;
256                                 else
257                                         q->head = qe;
258                                 /* Fix additional pointers and
259                                   information  */
260                                 qe->next = NULL;
261                                 qe->parent = q;
262                                 qe->pos = ++pos;
263                                 qe->opos = pos;
264                                 strncpy(qe->moh, q->moh, sizeof(qe->moh));
265                                 strncpy(qe->announce, q->announce, sizeof(qe->announce));
266                                 strncpy(qe->context, q->context, sizeof(qe->context));
267                                 q->count++;
268                                 res = 0;
269                                 manager_event(EVENT_FLAG_CALL, "Join", 
270                                                                 "Channel: %s\r\nCallerID: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\n",
271                                                                 qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : "unknown"), q->name, qe->pos, q->count );
272 #if 0
273 ast_log(LOG_NOTICE, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
274 #endif
275                         }
276                         ast_mutex_unlock(&q->lock);
277                         break;
278                 }
279                 q = q->next;
280         }
281         ast_mutex_unlock(&qlock);
282         return res;
283 }
284
285 static void free_members(struct ast_call_queue *q, int all)
286 {
287         /* Free non-dynamic members */
288         struct member *curm, *next, *prev;
289         curm = q->members;
290         prev = NULL;
291         while(curm) {
292                 next = curm->next;
293                 if (all || !curm->dynamic) {
294                         if (prev)
295                                 prev->next = next;
296                         else
297                                 q->members = next;
298                         free(curm);
299                 } else 
300                         prev = curm;
301                 curm = next;
302         }
303 }
304
305 static void destroy_queue(struct ast_call_queue *q)
306 {
307         struct ast_call_queue *cur, *prev = NULL;
308         ast_mutex_lock(&qlock);
309         cur = queues;
310         while(cur) {
311                 if (cur == q) {
312                         if (prev)
313                                 prev->next = cur->next;
314                         else
315                                 queues = cur->next;
316                 } else {
317                         prev = cur;
318                 }
319                 cur = cur->next;
320         }
321         ast_mutex_unlock(&qlock);
322         free_members(q, 1);
323         free(q);
324 }
325
326 static int play_file(struct ast_channel *chan, char *filename)
327 {
328         int res;
329
330         ast_stopstream(chan);
331         res = ast_streamfile(chan, filename, chan->language);
332
333         if (!res)
334                 res = ast_waitstream(chan, "");
335         else
336                 res = 0;
337
338         if (res) {
339                 ast_log(LOG_WARNING, "ast_streamfile failed on %s \n", chan->name);
340                 res = 0;
341         }
342         ast_stopstream(chan);
343
344         return res;
345 }
346
347 static int say_position(struct queue_ent *qe)
348 {
349         int res = 0, avgholdmins;
350         time_t now;
351
352         /* Check to see if this is ludicrous -- if we just announced position, don't do it again*/
353         time(&now);
354         if ( (now - qe->last_pos) < 15 )
355                 return -1;
356
357         /* If either our position has changed, or we are over the freq timer, say position */
358         if ( (qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency) )
359                 return -1;
360
361         ast_moh_stop(qe->chan);
362         /* Say we're next, if we are */
363         if (qe->pos == 1) {
364                 res += play_file(qe->chan, qe->parent->sound_next);
365                 goto posout;
366         } else {
367                 res += play_file(qe->chan, qe->parent->sound_thereare);
368                 res += ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language);
369                 res += play_file(qe->chan, qe->parent->sound_calls);
370         }
371
372         /* Round hold time to nearest minute */
373         avgholdmins = ( (qe->parent->holdtime + 30) - (now - qe->start) ) / 60;
374         ast_verbose(VERBOSE_PREFIX_3 "Hold time for %s is %d minutes\n", qe->parent->name, avgholdmins);
375
376         /* If the hold time is >1 min, if it's enabled, and if it's not
377            supposed to be only once and we have already said it, say it */
378         if (avgholdmins > 1 && (qe->parent->announceholdtime) && (!(qe->parent->announceholdtime==1 && qe->last_pos)) ) {
379                 res += play_file(qe->chan, qe->parent->sound_holdtime);
380                 res += ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language);
381                 res += play_file(qe->chan, qe->parent->sound_minutes);
382         }
383
384         posout:
385         /* Set our last_pos indicators */
386         qe->last_pos = now;
387         qe->last_pos_said = qe->pos;
388
389         ast_verbose(VERBOSE_PREFIX_3 "Told %s in %s their queue position (which was %d)\n", qe->chan->name, qe->parent->name, qe->pos);
390         res += play_file(qe->chan, qe->parent->sound_thanks);
391         ast_moh_start(qe->chan, qe->moh);
392
393         return (res>0);
394 }
395
396 static void record_abandoned(struct queue_ent *qe)
397 {
398         ast_mutex_lock(&qe->parent->lock);
399         qe->parent->callsabandoned++;
400         ast_mutex_unlock(&qe->parent->lock);
401 }
402
403 static void recalc_holdtime(struct queue_ent *qe)
404 {
405         int oldvalue, newvalue;
406
407         /* Calculate holdtime using a recursive boxcar filter */
408         /* Thanks to SRT for this contribution */
409         /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
410
411         newvalue = time(NULL) - qe->start;
412
413         ast_mutex_lock(&qe->parent->lock);
414         if (newvalue <= qe->parent->servicelevel)
415                 qe->parent->callscompletedinsl++;
416         oldvalue = qe->parent->holdtime;
417         qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newvalue) >> 2;
418         ast_mutex_unlock(&qe->parent->lock);
419 }
420
421
422 static void leave_queue(struct queue_ent *qe)
423 {
424         struct ast_call_queue *q;
425         struct queue_ent *cur, *prev = NULL;
426         int pos = 0;
427         q = qe->parent;
428         if (!q)
429                 return;
430         ast_mutex_lock(&q->lock);
431
432         prev = NULL;
433         cur = q->head;
434         while(cur) {
435                 if (cur == qe) {
436                         q->count--;
437
438                         /* Take us out of the queue */
439                         manager_event(EVENT_FLAG_CALL, "Leave",
440                                  "Channel: %s\r\nQueue: %s\r\nCount: %d\r\n",
441                                  qe->chan->name, q->name,  q->count);
442 #if 0
443 ast_log(LOG_NOTICE, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
444 #endif
445                         /* Take us out of the queue */
446                         if (prev)
447                                 prev->next = cur->next;
448                         else
449                                 q->head = cur->next;
450                 } else {
451                         /* Renumber the people after us in the queue based on a new count */
452                         cur->pos = ++pos;
453                         prev = cur;
454                 }
455                 cur = cur->next;
456         }
457         ast_mutex_unlock(&q->lock);
458         if (q->dead && !q->count) {     
459                 /* It's dead and nobody is in it, so kill it */
460                 destroy_queue(q);
461         }
462 }
463
464 static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
465 {
466         /* Hang up a tree of stuff */
467         struct localuser *oo;
468         while(outgoing) {
469                 /* Hangup any existing lines we have open */
470                 if (outgoing->chan && (outgoing->chan != exception))
471                         ast_hangup(outgoing->chan);
472                 oo = outgoing;
473                 outgoing=outgoing->next;
474                 free(oo);
475         }
476 }
477
478 static int ring_entry(struct queue_ent *qe, struct localuser *tmp)
479 {
480         int res;
481         /* Request the peer */
482         tmp->chan = ast_request(tmp->tech, qe->chan->nativeformats, tmp->numsubst);
483         if (!tmp->chan) {                       /* If we can't, just go on to the next call */
484 #if 0
485                 ast_log(LOG_NOTICE, "Unable to create channel of type '%s'\n", cur->tech);
486 #endif                  
487                 if (qe->chan->cdr)
488                         ast_cdr_busy(qe->chan->cdr);
489                 tmp->stillgoing = 0;
490                 return 0;
491         }
492         tmp->chan->appl = "AppQueue";
493         tmp->chan->data = "(Outgoing Line)";
494         tmp->chan->whentohangup = 0;
495         if (tmp->chan->callerid)
496                 free(tmp->chan->callerid);
497         if (tmp->chan->ani)
498                 free(tmp->chan->ani);
499         if (qe->chan->callerid)
500                 tmp->chan->callerid = strdup(qe->chan->callerid);
501         else
502                 tmp->chan->callerid = NULL;
503         if (qe->chan->ani)
504                 tmp->chan->ani = strdup(qe->chan->ani);
505         else
506                 tmp->chan->ani = NULL;
507         /* Presense of ADSI CPE on outgoing channel follows ours */
508         tmp->chan->adsicpe = qe->chan->adsicpe;
509         /* Place the call, but don't wait on the answer */
510         res = ast_call(tmp->chan, tmp->numsubst, 0);
511         if (res) {
512                 /* Again, keep going even if there's an error */
513                 if (option_debug)
514                         ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
515                 else if (option_verbose > 2)
516                         ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", tmp->numsubst);
517                 ast_hangup(tmp->chan);
518                 tmp->chan = NULL;
519                 tmp->stillgoing = 0;
520                 return 0;
521         } else
522                 if (option_verbose > 2)
523                         ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", tmp->numsubst);
524         return 0;
525 }
526
527 static int ring_one(struct queue_ent *qe, struct localuser *outgoing)
528 {
529         struct localuser *cur;
530         struct localuser *best;
531         int bestmetric=0;
532         do {
533                 best = NULL;
534                 cur = outgoing;
535                 while(cur) {
536                         if (cur->stillgoing &&                                                  /* Not already done */
537                                 !cur->chan &&                                                           /* Isn't already going */
538                                 (!best || (cur->metric < bestmetric))) {        /* We haven't found one yet, or it's better */
539                                         bestmetric = cur->metric;
540                                         best = cur;
541                         }
542                         cur = cur->next;
543                 }
544                 if (best) {
545                         if (!qe->parent->strategy) {
546                                 /* Ring everyone who shares this best metric (for ringall) */
547                                 cur = outgoing;
548                                 while(cur) {
549                                         if (cur->stillgoing && !cur->chan && (cur->metric == bestmetric)) {
550                                                 ast_log(LOG_DEBUG, "(Parallel) Trying '%s/%s' with metric %d\n", cur->tech, cur->numsubst, cur->metric);
551                                                 ring_entry(qe, cur);
552                                         }
553                                         cur = cur->next;
554                                 }
555                         } else {
556                                 /* Ring just the best channel */
557                                 ast_log(LOG_DEBUG, "Trying '%s/%s' with metric %d\n", best->tech, best->numsubst, best->metric);
558                                 ring_entry(qe, best);
559                         }
560                 }
561         } while (best && !best->chan);
562         if (!best) {
563                 ast_log(LOG_DEBUG, "Nobody left to try ringing in queue\n");
564                 return 0;
565         }
566         return 1;
567 }
568
569 static int valid_exit(struct queue_ent *qe, char digit)
570 {
571         char tmp[2];
572         if (!strlen(qe->context))
573                 return 0;
574         tmp[0] = digit;
575         tmp[1] = '\0';
576         if (ast_exists_extension(qe->chan, qe->context, tmp, 1, qe->chan->callerid)) {
577                 strncpy(qe->chan->context, qe->context, sizeof(qe->chan->context) - 1);
578                 strncpy(qe->chan->exten, tmp, sizeof(qe->chan->exten) - 1);
579                 qe->chan->priority = 0;
580                 return 1;
581         }
582         return 0;
583 }
584
585 #define MAX 256
586
587 static struct localuser *wait_for_answer(struct queue_ent *qe, struct localuser *outgoing, int *to, int *allowredir_in, int *allowredir_out, int *allowdisconnect, char *digit)
588 {
589         char *queue = qe->parent->name;
590         struct localuser *o;
591         int found;
592         int numlines;
593         int sentringing = 0;
594         int numbusies = 0;
595         int orig = *to;
596         struct ast_frame *f;
597         struct localuser *peer = NULL;
598         struct ast_channel *watchers[MAX];
599         int pos;
600         struct ast_channel *winner;
601         struct ast_channel *in = qe->chan;
602         
603         while(*to && !peer) {
604                 o = outgoing;
605                 found = -1;
606                 pos = 1;
607                 numlines = 0;
608                 watchers[0] = in;
609                 while(o) {
610                         /* Keep track of important channels */
611                         if (o->stillgoing && o->chan) {
612                                 watchers[pos++] = o->chan;
613                                 found = 1;
614                         }
615                         o = o->next;
616                         numlines++;
617                 }
618                 if (found < 0) {
619                         if (numlines == numbusies) {
620                                 ast_log(LOG_DEBUG, "Everyone is busy at this time\n");
621                         } else {
622                                 ast_log(LOG_NOTICE, "No one is answering queue '%s'\n", queue);
623                         }
624                         *to = 0;
625                         return NULL;
626                 }
627                 winner = ast_waitfor_n(watchers, pos, to);
628                 o = outgoing;
629                 while(o) {
630                         if (o->stillgoing && (o->chan) &&  (o->chan->_state == AST_STATE_UP)) {
631                                 if (!peer) {
632                                         if (option_verbose > 2)
633                                                 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
634                                         peer = o;
635                                         *allowredir_in = o->allowredirect_in;
636                                         *allowredir_out = o->allowredirect_out;
637                                         *allowdisconnect = o->allowdisconnect;
638                                 }
639                         } else if (o->chan && (o->chan == winner)) {
640                                 f = ast_read(winner);
641                                 if (f) {
642                                         if (f->frametype == AST_FRAME_CONTROL) {
643                                                 switch(f->subclass) {
644                                             case AST_CONTROL_ANSWER:
645                                                         /* This is our guy if someone answered. */
646                                                         if (!peer) {
647                                                                 if (option_verbose > 2)
648                                                                         ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
649                                                                 peer = o;
650                                                                 *allowredir_in = o->allowredirect_in;
651                                                                 *allowredir_out = o->allowredirect_out;
652                                                                 *allowdisconnect = o->allowdisconnect;
653                                                         }
654                                                         break;
655                                                 case AST_CONTROL_BUSY:
656                                                         if (option_verbose > 2)
657                                                                 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
658                                                         o->stillgoing = 0;
659                                                         if (in->cdr)
660                                                                 ast_cdr_busy(in->cdr);
661                                                         ast_hangup(o->chan);
662                                                         o->chan = NULL;
663                                                         if (qe->parent->strategy)
664                                                                 ring_one(qe, outgoing);
665                                                         numbusies++;
666                                                         break;
667                                                 case AST_CONTROL_CONGESTION:
668                                                         if (option_verbose > 2)
669                                                                 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
670                                                         o->stillgoing = 0;
671                                                         if (in->cdr)
672                                                                 ast_cdr_busy(in->cdr);
673                                                         ast_hangup(o->chan);
674                                                         o->chan = NULL;
675                                                         if (qe->parent->strategy)
676                                                                 ring_one(qe, outgoing);
677                                                         numbusies++;
678                                                         break;
679                                                 case AST_CONTROL_RINGING:
680                                                         if (option_verbose > 2)
681                                                                 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
682                                                         if (!sentringing) {
683 #if 0
684                                                                 ast_indicate(in, AST_CONTROL_RINGING);
685 #endif                                                          
686                                                                 sentringing++;
687                                                         }
688                                                         break;
689                                                 case AST_CONTROL_OFFHOOK:
690                                                         /* Ignore going off hook */
691                                                         break;
692                                                 default:
693                                                         ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
694                                                 }
695                                         }
696                                         ast_frfree(f);
697                                 } else {
698                                         o->stillgoing = 0;
699                                         ast_hangup(o->chan);
700                                         o->chan = NULL;
701                                         if (qe->parent->strategy)
702                                                 ring_one(qe, outgoing);
703                                 }
704                         }
705                         o = o->next;
706                 }
707                 if (winner == in) {
708                         f = ast_read(in);
709 #if 0
710                         if (f && (f->frametype != AST_FRAME_VOICE))
711                                         printf("Frame type: %d, %d\n", f->frametype, f->subclass);
712                         else if (!f || (f->frametype != AST_FRAME_VOICE))
713                                 printf("Hangup received on %s\n", in->name);
714 #endif
715                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
716                                 /* Got hung up */
717                                 *to=-1;
718                                 return NULL;
719                         }
720                         if (f && (f->frametype == AST_FRAME_DTMF) && allowdisconnect && (f->subclass == '*')) {
721                             if (option_verbose > 3)
722                                 ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
723                                 *to=0;
724                                 return NULL;
725                         }
726                         if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass != '*') && valid_exit(qe, f->subclass)) {
727                                 if (option_verbose > 3)
728                                         ast_verbose(VERBOSE_PREFIX_3 "User pressed digit: %c", f->subclass);
729                                 *to=0;
730                                 *digit=f->subclass;
731                                 return NULL;
732                         }
733                 }
734                 if (!*to && (option_verbose > 2))
735                         ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
736         }
737
738         return peer;
739         
740 }
741
742 static int wait_our_turn(struct queue_ent *qe)
743 {
744         struct queue_ent *ch;
745         int res = 0;
746         time_t now;
747
748         /* This is the holding pen for callers 2 through maxlen */
749         for (;;) {
750                 /* Atomically read the parent head -- does not need a lock */
751                 ch = qe->parent->head;
752
753                 /* If we are now at the top of the head, break out */
754                 if (qe == ch)
755                         break;
756
757                 /* If we have timed out, break out */
758                 if ( qe->queuetimeout ) {
759                         time(&now);
760                         if ( (now - qe->start) >= qe->queuetimeout )
761                         break;
762                 }
763
764                 /* Make a position announcement, if enabled */
765                 if (qe->parent->announcefrequency)
766                         say_position(qe);
767
768
769                 /* Wait a second before checking again */
770                 res = ast_waitfordigit(qe->chan, RECHECK * 1000);
771                 if (res)
772                         break;
773         }
774         return res;
775 }
776
777 static int update_queue(struct ast_call_queue *q, struct localuser *user)
778 {
779         struct member *cur;
780         /* Since a reload could have taken place, we have to traverse the list to
781                 be sure it's still valid */
782         ast_mutex_lock(&q->lock);
783         cur = q->members;
784         while(cur) {
785                 if (user->member == cur) {
786                         time(&cur->lastcall);
787                         cur->calls++;
788                         break;
789                 }
790                 cur = cur->next;
791         }
792         q->callscompleted++;
793         ast_mutex_unlock(&q->lock);
794         return 0;
795 }
796
797 static int calc_metric(struct ast_call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct localuser *tmp)
798 {
799         switch (q->strategy) {
800         case QUEUE_STRATEGY_RINGALL:
801                 /* Everyone equal, except for penalty */
802                 tmp->metric = mem->penalty * 1000000;
803                 break;
804         case QUEUE_STRATEGY_ROUNDROBIN:
805                 if (!pos) {
806                         if (!q->wrapped) {
807                                 /* No more channels, start over */
808                                 q->rrpos = 0;
809                         } else {
810                                 /* Prioritize next entry */
811                                 q->rrpos++;
812                         }
813                         q->wrapped = 0;
814                 }
815                 if (pos < q->rrpos) {
816                         tmp->metric = 1000 + pos;
817                 } else {
818                         if (pos > q->rrpos) {
819                                 /* Indicate there is another priority */
820                                 q->wrapped = 1;
821                         }
822                         tmp->metric = pos;
823                 }
824                 tmp->metric += mem->penalty * 1000000;
825                 break;
826         case QUEUE_STRATEGY_RANDOM:
827                 tmp->metric = rand() % 1000;
828                 tmp->metric += mem->penalty * 1000000;
829                 break;
830         case QUEUE_STRATEGY_FEWESTCALLS:
831                 tmp->metric = mem->calls;
832                 tmp->metric += mem->penalty * 1000000;
833                 break;
834         case QUEUE_STRATEGY_LEASTRECENT:
835                 if (!mem->lastcall)
836                         tmp->metric = 0;
837                 else
838                         tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
839                 tmp->metric += mem->penalty * 1000000;
840                 break;
841         default:
842                 ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
843                 break;
844         }
845         return 0;
846 }
847
848 static int try_calling(struct queue_ent *qe, char *options, char *announceoverride, char *url, int *go_on)
849 {
850         struct member *cur;
851         struct localuser *outgoing=NULL, *tmp = NULL;
852         int to;
853         int allowredir_in=0;
854         int allowredir_out=0;
855         int allowdisconnect=0;
856         char restofit[AST_MAX_EXTENSION];
857         char oldexten[AST_MAX_EXTENSION]="";
858         char oldcontext[AST_MAX_EXTENSION]="";
859         char queuename[256]="";
860         char *newnum;
861         struct ast_channel *peer;
862         struct localuser *lpeer;
863         int res = 0, bridge = 0;
864         int zapx = 2;
865         int x=0;
866         char *announce = NULL;
867         char digit = 0;
868         time_t callstart;
869         /* Hold the lock while we setup the outgoing calls */
870         ast_mutex_lock(&qe->parent->lock);
871         strncpy(queuename, qe->parent->name, sizeof(queuename) - 1);
872         cur = qe->parent->members;
873         if (strlen(qe->announce))
874                 announce = qe->announce;
875         if (announceoverride && strlen(announceoverride))
876                 announce = announceoverride;
877         while(cur) {
878                 /* Get a technology/[device:]number pair */
879                 tmp = malloc(sizeof(struct localuser));
880                 if (!tmp) {
881                         ast_mutex_unlock(&qe->parent->lock);
882                         ast_log(LOG_WARNING, "Out of memory\n");
883                         goto out;
884                 }
885                 memset(tmp, 0, sizeof(struct localuser));
886                 tmp->stillgoing = -1;
887                 if (options) {
888                         if (strchr(options, 't'))
889                                 tmp->allowredirect_in = 1;
890                         if (strchr(options, 'T'))
891                                 tmp->allowredirect_out = 1;
892                         if (strchr(options, 'r'))
893                                 tmp->ringbackonly = 1;
894                         if (strchr(options, 'm'))
895                                 tmp->musiconhold = 1;
896                         if (strchr(options, 'd'))
897                                 tmp->dataquality = 1;
898                         if (strchr(options, 'H'))
899                                 tmp->allowdisconnect = 1;
900                         if (strchr(options, 'n'))
901                                 *go_on = 1;
902                 }
903                 if (url) {
904                         ast_log(LOG_DEBUG, "Queue with URL=%s_\n", url);
905                 } else 
906                         ast_log(LOG_DEBUG, "Simple queue (no URL)\n");
907
908                 tmp->member = cur;              /* Never directly dereference!  Could change on reload */
909                 strncpy(tmp->tech, cur->tech, sizeof(tmp->tech)-1);
910                 strncpy(tmp->numsubst, cur->loc, sizeof(tmp->numsubst)-1);
911                 /* If we're dialing by extension, look at the extension to know what to dial */
912                 if ((newnum = strstr(tmp->numsubst, "BYEXTENSION"))) {
913                         strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)-1);
914                         snprintf(newnum, sizeof(tmp->numsubst) - (newnum - tmp->numsubst), "%s%s", qe->chan->exten,restofit);
915                         if (option_debug)
916                                 ast_log(LOG_DEBUG, "Dialing by extension %s\n", tmp->numsubst);
917                 }
918                 /* Special case: If we ring everyone, go ahead and ring them, otherwise
919                    just calculate their metric for the appropriate strategy */
920                 calc_metric(qe->parent, cur, x++, qe, tmp);
921                 /* Put them in the list of outgoing thingies...  We're ready now. 
922                    XXX If we're forcibly removed, these outgoing calls won't get
923                    hung up XXX */
924                 tmp->next = outgoing;
925                 outgoing = tmp;         
926                 /* If this line is up, don't try anybody else */
927                 if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
928                         break;
929
930                 cur = cur->next;
931         }
932         if (qe->parent->timeout)
933                 to = qe->parent->timeout * 1000;
934         else
935                 to = -1;
936         ring_one(qe, outgoing);
937         ast_mutex_unlock(&qe->parent->lock);
938         lpeer = wait_for_answer(qe, outgoing, &to, &allowredir_in, &allowredir_out, &allowdisconnect, &digit);
939         if (lpeer)
940                 peer = lpeer->chan;
941         else
942                 peer = NULL;
943         if (!peer) {
944                 if (to) {
945                         /* Musta gotten hung up */
946                         record_abandoned(qe);
947                         res = -1;
948                 } else {
949                         if (digit && valid_exit(qe, digit))
950                                 res=digit;
951                         else
952                                 /* Nobody answered, next please? */
953                                 res=0;
954                 }
955                 goto out;
956         }
957         if (peer) {
958                 /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
959                    we will always return with -1 so that it is hung up properly after the 
960                    conversation.  */
961                 qe->handled++;
962                 if (!strcmp(qe->chan->type,"Zap")) {
963                         if (tmp->dataquality) zapx = 0;
964                         ast_channel_setoption(qe->chan,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
965                 }                       
966                 if (!strcmp(peer->type,"Zap")) {
967                         if (tmp->dataquality) zapx = 0;
968                         ast_channel_setoption(peer,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
969                 }
970                 /* Update parameters for the queue */
971                 recalc_holdtime(qe);
972                 update_queue(qe->parent, lpeer);
973                 hanguptree(outgoing, peer);
974                 /* Stop music on hold */
975                 ast_moh_stop(qe->chan);
976                 outgoing = NULL;
977                 if (announce) {
978                         int res2;
979                         res2 = ast_autoservice_start(qe->chan);
980                         if (!res2)
981                                 res2 = ast_streamfile(peer, announce, peer->language);
982                         if (!res2)
983                                 res2 = ast_waitstream(peer, "");
984                         res2 |= ast_autoservice_stop(qe->chan);
985                         if (res2) {
986                                 /* Agent must have hung up */
987                                 ast_log(LOG_WARNING, "Agent on %s hungup on the customer.  They're going to be pissed.\n", peer->name);
988                                 ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "AGENTDUMP", "%s", "");
989                                 ast_hangup(peer);
990                                 return -1;
991                         }
992                 }
993                 /* If appropriate, log that we have a destination channel */
994                 if (qe->chan->cdr)
995                         ast_cdr_setdestchan(qe->chan->cdr, peer->name);
996                 /* Make sure channels are compatible */
997                 res = ast_channel_make_compatible(qe->chan, peer);
998                 if (res < 0) {
999                         ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "SYSCOMPAT", "%s", "");
1000                         ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
1001                         ast_hangup(peer);
1002                         return -1;
1003                 }
1004                 /* Begin Monitoring */
1005                 if (qe->parent->monfmt && *qe->parent->monfmt) {
1006                         ast_monitor_start( peer, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1 );
1007                 }
1008                 /* Drop out of the queue at this point, to prepare for next caller */
1009                 leave_queue(qe);                        
1010                 /* JDG: sendurl */
1011                 if( url && strlen(url) && ast_channel_supports_html(peer) ) {
1012                         ast_log(LOG_DEBUG, "app_queue: sendurl=%s.\n", url);
1013                         ast_channel_sendurl( peer, url );
1014                 } /* /JDG */
1015                 ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "CONNECT", "%ld", (long)time(NULL) - qe->start);
1016                 strncpy(oldcontext, qe->chan->context, sizeof(oldcontext) - 1);
1017                 strncpy(oldexten, qe->chan->exten, sizeof(oldexten) - 1);
1018                 time(&callstart);
1019                 bridge = ast_bridge_call(qe->chan, peer, allowredir_in, allowredir_out, allowdisconnect);
1020                 if (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten)) {
1021                         ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "TRANSFER", "%s|%s", qe->chan->exten, qe->chan->context);
1022                 } else if (qe->chan->_softhangup) {
1023                         ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETECALLER", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
1024                 } else {
1025                         ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETEAGENT", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
1026                 }
1027
1028                 if(bridge != AST_PBX_NO_HANGUP_PEER)
1029                         ast_hangup(peer);
1030
1031                 if( bridge == 0 ) res=1; /* JDG: bridge successfull, leave app_queue */
1032                 else res = bridge; /* bridge error, stay in the queue */
1033         }       
1034 out:
1035         hanguptree(outgoing, NULL);
1036         return res;
1037 }
1038
1039 static int wait_a_bit(struct queue_ent *qe)
1040 {
1041         /* Don't need to hold the lock while we setup the outgoing calls */
1042         int retrywait = qe->parent->retry * 1000;
1043         return ast_waitfordigit(qe->chan, retrywait);
1044 }
1045
1046 // [PHM 06/26/03]
1047
1048 static struct member * interface_exists( struct ast_call_queue * q, char * interface )
1049 {
1050         struct member * ret = NULL ;
1051         struct member *mem;
1052         char buf[500] ;
1053
1054         if( q != NULL )
1055         {
1056                 mem = q->members ;
1057
1058                 while( mem != NULL ) {
1059                         sprintf( buf, "%s/%s", mem->tech, mem->loc);
1060
1061                         if( strcmp( buf, interface ) == 0 ) {
1062                                 ret = mem ;
1063                                 break ;
1064                         }
1065                         else
1066                                 mem = mem->next ;
1067                 }
1068         }
1069
1070         return( ret ) ;
1071 }
1072
1073
1074 static struct member * create_queue_node( char * interface )
1075 {
1076         struct member * cur ;
1077         char * tmp ;
1078         
1079         /* Add a new member */
1080
1081         cur = malloc(sizeof(struct member));
1082
1083         if (cur) {
1084                 memset(cur, 0, sizeof(struct member));
1085                 strncpy(cur->tech, interface, sizeof(cur->tech) - 1);
1086                 if ((tmp = strchr(cur->tech, '/')))
1087                         *tmp = '\0';
1088                 if ((tmp = strchr(interface, '/'))) {
1089                         tmp++;
1090                         strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
1091                 } else
1092                         ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
1093         }
1094
1095         return( cur ) ;
1096 }
1097
1098
1099 static int rqm_exec(struct ast_channel *chan, void *data)
1100 {
1101         int res=-1;
1102         struct localuser *u;
1103         char *queuename;
1104         struct member * node ;
1105         struct member * look ;
1106         char info[512];
1107         char tmpchan[256]="";
1108         char *interface=NULL;
1109         struct ast_call_queue *q;
1110         int found=0 ;
1111
1112         if (!data) {
1113                 ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename|optional interface)\n");
1114                 return -1;
1115         }
1116         
1117         LOCAL_USER_ADD(u); // not sure if we need this, but better be safe than sorry ;-)
1118         
1119         /* Parse our arguments XXX Check for failure XXX */
1120         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
1121         queuename = info;
1122         if (queuename) {
1123                 interface = strchr(queuename, '|');
1124                 if (interface) {
1125                         *interface = '\0';
1126                         interface++;
1127                 }
1128                 else {
1129                         strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
1130                         interface = strrchr(tmpchan, '-');
1131                         if (interface)
1132                                 *interface = '\0';
1133                         interface = tmpchan;
1134                 }
1135         }
1136
1137         if( ( q = queues) != NULL )
1138         {
1139                 while( q && ( res != 0 ) && (!found) ) 
1140                 {
1141                         ast_mutex_lock(&q->lock);
1142                         if( strcmp( q->name, queuename) == 0 )
1143                         {
1144                                 // found queue, try to remove  interface
1145                                 found=1 ;
1146
1147                                 if( ( node = interface_exists( q, interface ) ) != NULL )
1148                                 {
1149                                         if( ( look = q->members ) == node )
1150                                         {
1151                                                 // 1st
1152                                                 q->members = node->next;
1153                                         }
1154                                         else
1155                                         {
1156                                                 while( look != NULL )
1157                                                         if( look->next == node )
1158                                                         {
1159                                                                 look->next = node->next ;
1160                                                                 break ;
1161                                                         }
1162                                                         else
1163                                                                 look = look->next ;
1164                                         }
1165
1166                                         free( node ) ;
1167
1168                                         ast_log(LOG_NOTICE, "Removed interface '%s' to queue '%s'\n", 
1169                                                 interface, queuename);
1170                                         res = 0 ;
1171                                 }
1172                                 else
1173                                         ast_log(LOG_WARNING, "Unable to remove interface '%s' from queue '%s': "
1174                                                 "Not there\n", interface, queuename);
1175                         }
1176
1177                         ast_mutex_unlock(&q->lock);
1178                         q = q->next;
1179                 }
1180         }
1181
1182         if( ! found )
1183                 ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", queuename);
1184
1185         LOCAL_USER_REMOVE(u);
1186         return res;
1187 }
1188
1189
1190
1191 static int aqm_exec(struct ast_channel *chan, void *data)
1192 {
1193         int res=-1;
1194         struct localuser *u;
1195         char *queuename;
1196         char info[512];
1197         char tmpchan[512]="";
1198         char *interface=NULL;
1199         struct ast_call_queue *q;
1200         struct member *save;
1201         int found=0 ;
1202
1203         if (!data) {
1204                 ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename|optional interface)\n");
1205                 return -1;
1206         }
1207         
1208         LOCAL_USER_ADD(u); // not sure if we need this, but better be safe than sorry ;-)
1209         
1210         /* Parse our arguments XXX Check for failure XXX */
1211         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
1212         queuename = info;
1213         if (queuename) {
1214                 interface = strchr(queuename, '|');
1215                 if (interface) {
1216                         *interface = '\0';
1217                         interface++;
1218                 }
1219                 else {
1220                         strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
1221                         interface = strrchr(tmpchan, '-');
1222                         if (interface)
1223                                 *interface = '\0';
1224                         interface = tmpchan;
1225                 }
1226         }
1227
1228         if( ( q = queues) != NULL )
1229         {
1230                 while( q && ( res != 0 ) && (!found) ) 
1231                 {
1232                         ast_mutex_lock(&q->lock);
1233                         if( strcmp( q->name, queuename) == 0 )
1234                         {
1235                                 // found queue, try to enable interface
1236                                 found=1 ;
1237
1238                                 if( interface_exists( q, interface ) == NULL )
1239                                 {
1240                                         save = q->members ;
1241                                         q->members = create_queue_node( interface ) ;
1242
1243                                         if( q->members != NULL ) {
1244                                                 q->members->dynamic = 1;
1245                                                 q->members->next = save ;
1246                                         } else
1247                                                 q->members = save ;
1248
1249                                         ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", interface, queuename);
1250                                         res = 0 ;
1251                                 }
1252                                 else
1253                                         ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': "
1254                                                 "Already there\n", interface, queuename);
1255                         }
1256
1257                         ast_mutex_unlock(&q->lock);
1258                         q = q->next;
1259                 }
1260         }
1261
1262         if( ! found )
1263                 ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", queuename);
1264
1265         LOCAL_USER_REMOVE(u);
1266         return res;
1267 }
1268
1269
1270 static int queue_exec(struct ast_channel *chan, void *data)
1271 {
1272         int res=-1;
1273         struct localuser *u;
1274         char *queuename;
1275         char info[512];
1276         char *options = NULL;
1277         char *url = NULL;
1278         char *announceoverride = NULL;
1279         char *queuetimeoutstr = NULL;
1280         /* whether to exit Queue application after the timeout hits */
1281         int go_on = 0;
1282
1283
1284
1285         /* Our queue entry */
1286         struct queue_ent qe;
1287         
1288         if (!data) {
1289                 ast_log(LOG_WARNING, "Queue requires an argument (queuename|optional timeout|optional URL)\n");
1290                 return -1;
1291         }
1292         
1293         LOCAL_USER_ADD(u);
1294
1295         /* Setup our queue entry */
1296         memset(&qe, 0, sizeof(qe));
1297         
1298         /* Parse our arguments XXX Check for failure XXX */
1299         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
1300         queuename = info;
1301         if (queuename) {
1302                 options = strchr(queuename, '|');
1303                 if (options) {
1304                         *options = '\0';
1305                         options++;
1306                         url = strchr(options, '|');
1307                         if (url) {
1308                                 *url = '\0';
1309                                 url++;
1310                                 announceoverride = strchr(url, '|');
1311                                 if (announceoverride) {
1312                                         *announceoverride = '\0';
1313                                         announceoverride++;
1314                                         queuetimeoutstr = strchr(announceoverride, '|');
1315                                         if (queuetimeoutstr) {
1316                                                 *queuetimeoutstr = '\0';
1317                                                 queuetimeoutstr++;
1318                                                 qe.queuetimeout = atoi(queuetimeoutstr);
1319                                         } else {
1320                                                 qe.queuetimeout = 0;
1321                                 }
1322                         }
1323                 }
1324         }
1325         }
1326
1327         printf("queue: %s, options: %s, url: %s, announce: %s, timeout: %d\n",
1328                 queuename, options, url, announceoverride, qe.queuetimeout);
1329
1330
1331         qe.chan = chan;
1332         qe.start = time(NULL);
1333         qe.last_pos_said = 0;
1334         qe.last_pos = 0;
1335         if (!join_queue(queuename, &qe)) {
1336                 ast_queue_log(queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", url ? url : "", chan->callerid ? chan->callerid : "");
1337                 /* Start music on hold */
1338                 ast_moh_start(chan, qe.moh);
1339                 for (;;) {
1340                         /* This is the wait loop for callers 2 through maxlen */
1341
1342                         res = wait_our_turn(&qe);
1343                         /* If they hungup, return immediately */
1344                         if (res < 0) {
1345                                 /* Record this abandoned call */
1346                                 record_abandoned(&qe);
1347                                 ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
1348                                 if (option_verbose > 2) {
1349                                         ast_verbose(VERBOSE_PREFIX_3 "User disconnected while waiting their turn\n");
1350                                         res = -1;
1351                                 }
1352                                 break;
1353                         }
1354                         if (!res) 
1355                                 break;
1356                         if (valid_exit(&qe, res)) {
1357                                 ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
1358                                 break;
1359                         }
1360                 }
1361                 if (!res) {
1362                         for (;;) {
1363                                 /* This is the wait loop for the head caller*/
1364                                 /* To exit, they may get their call answered; */
1365                                 /* they may dial a digit from the queue context; */
1366                                 /* or, they may may timeout. */
1367
1368                                 /* Leave if we have exceeded our queuetimeout */
1369                                 if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
1370                                         res = 0;
1371                                         break;
1372                                 }
1373
1374                                 /* Make a position announcement, if enabled */
1375                                 if (qe.parent->announcefrequency)
1376                                         say_position(&qe);
1377
1378                                 /* Try calling all queue members for 'timeout' seconds */
1379                                 res = try_calling(&qe, options, announceoverride, url, &go_on);
1380                                 if (res) {
1381                                         if (res < 0) {
1382                                                 if (!qe.handled)
1383                                                         ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
1384                                         } else if (res > 0)
1385                                                 ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
1386                                         break;
1387                                 }
1388
1389                                 /* Leave if we have exceeded our queuetimeout */
1390                                 if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
1391                                         res = 0;
1392                                         break;
1393                                 }
1394
1395                                 /* OK, we didn't get anybody; wait for 'retry' seconds; may get a digit to exit with */
1396                                 res = wait_a_bit(&qe);
1397                                 if (res < 0) {
1398                                         ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
1399                                         if (option_verbose > 2) {
1400                                                 ast_verbose(VERBOSE_PREFIX_3 "User disconnected when they almost made it\n");
1401                                                 res = -1;
1402                                         }
1403                                         break;
1404                                 }
1405                                 if (res && valid_exit(&qe, res)) {
1406                                         ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
1407                                         break;
1408                                 }
1409                                 /* exit after 'timeout' cycle if 'n' option enabled */
1410                                 if (go_on) {
1411                                         ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
1412                                         res = 0;
1413                                         break;
1414                                 }
1415
1416                         }
1417                 }
1418                 /* Don't allow return code > 0 */
1419                 if (res > 0 && res != AST_PBX_KEEPALIVE) {
1420                         res = 0;        
1421                         ast_moh_stop(chan);
1422                         ast_stopstream(chan);
1423                 }
1424                 leave_queue(&qe);
1425         } else {
1426                 ast_log(LOG_WARNING, "Unable to join queue '%s'\n", queuename);
1427                 res =  0;
1428         }
1429         LOCAL_USER_REMOVE(u);
1430         return res;
1431 }
1432
1433 static void reload_queues(void)
1434 {
1435         struct ast_call_queue *q, *ql, *qn;
1436         struct ast_config *cfg;
1437         char *cat, *tmp;
1438         struct ast_variable *var;
1439         struct member *prev, *cur;
1440         int new;
1441         cfg = ast_load("queues.conf");
1442         if (!cfg) {
1443                 ast_log(LOG_NOTICE, "No call queueing config file, so no call queues\n");
1444                 return;
1445         }
1446         ast_mutex_lock(&qlock);
1447         /* Mark all queues as dead for the moment */
1448         q = queues;
1449         while(q) {
1450                 q->dead = 1;
1451                 q = q->next;
1452         }
1453         /* Chug through config file */
1454         cat = ast_category_browse(cfg, NULL);
1455         while(cat) {
1456                 if (strcasecmp(cat, "general")) {
1457                         /* Look for an existing one */
1458                         q = queues;
1459                         while(q) {
1460                                 if (!strcmp(q->name, cat))
1461                                         break;
1462                                 q = q->next;
1463                         }
1464                         if (!q) {
1465                                 /* Make one then */
1466                                 q = malloc(sizeof(struct ast_call_queue));
1467                                 if (q) {
1468                                         /* Initialize it */
1469                                         memset(q, 0, sizeof(struct ast_call_queue));
1470                                         ast_mutex_init(&q->lock);
1471                                         strncpy(q->name, cat, sizeof(q->name));
1472                                         new = 1;
1473                                 } else new = 0;
1474                         } else
1475                                         new = 0;
1476                         if (q) {
1477                                 if (!new) 
1478                                         ast_mutex_lock(&q->lock);
1479                                 /* Re-initialize the queue */
1480                                 q->dead = 0;
1481                                 q->retry = 0;
1482                                 q->timeout = -1;
1483                                 q->maxlen = 0;
1484                                 q->announcefrequency = 0;
1485                                 q->announceholdtime = 0;
1486                                 q->holdtime = 0;
1487                                 q->callscompleted = 0;
1488                                 q->callsabandoned = 0;
1489                                 q->callscompletedinsl = 0;
1490                                 q->servicelevel = 0;
1491                                 free_members(q, 0);
1492                                 strcpy(q->moh, "");
1493                                 strcpy(q->announce, "");
1494                                 strcpy(q->context, "");
1495                                 strcpy(q->monfmt, "");
1496                                 strcpy(q->sound_next, "queue-youarenext");
1497                                 strcpy(q->sound_thereare, "queue-thereare");
1498                                 strcpy(q->sound_calls, "queue-callswaiting");
1499                                 strcpy(q->sound_holdtime, "queue-holdtime");
1500                                 strcpy(q->sound_minutes, "queue-minutes");
1501                                 strcpy(q->sound_thanks, "queue-thankyou");
1502                                 prev = q->members;
1503                                 if (prev) {
1504                                         /* find the end of any dynamic members */
1505                                         while(prev->next)
1506                                                 prev = prev->next;
1507                                 }
1508                                 var = ast_variable_browse(cfg, cat);
1509                                 while(var) {
1510                                         if (!strcasecmp(var->name, "member")) {
1511                                                 /* Add a new member */
1512                                                 cur = malloc(sizeof(struct member));
1513                                                 if (cur) {
1514                                                         memset(cur, 0, sizeof(struct member));
1515                                                         strncpy(cur->tech, var->value, sizeof(cur->tech) - 1);
1516                                                         if ((tmp = strchr(cur->tech, ','))) {
1517                                                                 *tmp = '\0';
1518                                                                 tmp++;
1519                                                                 cur->penalty = atoi(tmp);
1520                                                                 if (cur->penalty < 0)
1521                                                                         cur->penalty = 0;
1522                                                         }
1523                                                         if ((tmp = strchr(cur->tech, '/')))
1524                                                                 *tmp = '\0';
1525                                                         if ((tmp = strchr(var->value, '/'))) {
1526                                                                 tmp++;
1527                                                                 strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
1528                                                                 if ((tmp = strchr(cur->loc, ',')))
1529                                                                         *tmp = '\0';
1530                                                         } else
1531                                                                 ast_log(LOG_WARNING, "No location at line %d of queue.conf\n", var->lineno);
1532                                                         if (prev)
1533                                                                 prev->next = cur;
1534                                                         else
1535                                                                 q->members = cur;
1536                                                         prev = cur;
1537                                                 }
1538                                         } else if (!strcasecmp(var->name, "music")) {
1539                                                 strncpy(q->moh, var->value, sizeof(q->moh) - 1);
1540                                         } else if (!strcasecmp(var->name, "announce")) {
1541                                                 strncpy(q->announce, var->value, sizeof(q->announce) - 1);
1542                                         } else if (!strcasecmp(var->name, "context")) {
1543                                                 strncpy(q->context, var->value, sizeof(q->context) - 1);
1544                                         } else if (!strcasecmp(var->name, "timeout")) {
1545                                                 q->timeout = atoi(var->value);
1546                                         } else if (!strcasecmp(var->name, "monitor-format")) {
1547                                                 strncpy(q->monfmt, var->value, sizeof(q->monfmt) - 1);
1548                                         } else if (!strcasecmp(var->name, "queue-youarenext")) {
1549                                                 strncpy(q->sound_next, var->value, sizeof(q->sound_next) - 1);
1550                                         } else if (!strcasecmp(var->name, "queue-thereare")) {
1551                                                 strncpy(q->sound_thereare, var->value, sizeof(q->sound_thereare) - 1);
1552                                         } else if (!strcasecmp(var->name, "queue-callswaiting")) {
1553                                                 strncpy(q->sound_calls, var->value, sizeof(q->sound_calls) - 1);
1554                                         } else if (!strcasecmp(var->name, "queue-holdtime")) {
1555                                                 strncpy(q->sound_holdtime, var->value, sizeof(q->sound_holdtime) - 1);
1556                                         } else if (!strcasecmp(var->name, "queue-minutes")) {
1557                                                 strncpy(q->sound_minutes, var->value, sizeof(q->sound_minutes) - 1);
1558                                         } else if (!strcasecmp(var->name, "queue-thankyou")) {
1559                                                 strncpy(q->sound_thanks, var->value, sizeof(q->sound_thanks) - 1);
1560                                         } else if (!strcasecmp(var->name, "announce-frequency")) {
1561                                                 q->announcefrequency = atoi(var->value);
1562                                         } else if (!strcasecmp(var->name, "announce-holdtime")) {
1563                                                 q->announceholdtime = (!strcasecmp(var->value,"once")) ? 1 : ast_true(var->value);
1564                                         } else if (!strcasecmp(var->name, "retry")) {
1565                                                 q->retry = atoi(var->value);
1566                                         } else if (!strcasecmp(var->name, "maxlen")) {
1567                                                 q->maxlen = atoi(var->value);
1568                                         } else if (!strcasecmp(var->name, "servicelevel")) {
1569                                                 q->servicelevel= atoi(var->value);
1570                                         } else if (!strcasecmp(var->name, "strategy")) {
1571                                                 q->strategy = strat2int(var->value);
1572                                                 if (q->strategy < 0) {
1573                                                         ast_log(LOG_WARNING, "'%s' isn't a valid strategy, using ringall instead\n", var->value);
1574                                                         q->strategy = 0;
1575                                                 }
1576                                         } else {
1577                                                 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queue.conf\n", cat, var->name, var->lineno);
1578                                         }
1579                                         var = var->next;
1580                                 }
1581                                 if (q->retry < 1)
1582                                         q->retry = DEFAULT_RETRY;
1583                                 if (q->timeout < 0)
1584                                         q->timeout = DEFAULT_TIMEOUT;
1585                                 if (q->maxlen < 0)
1586                                         q->maxlen = 0;
1587                                 if (!new) 
1588                                         ast_mutex_unlock(&q->lock);
1589                                 if (new) {
1590                                         q->next = queues;
1591                                         queues = q;
1592                                 }
1593                         }
1594                 }
1595                 cat = ast_category_browse(cfg, cat);
1596         }
1597         ast_destroy(cfg);
1598         q = queues;
1599         ql = NULL;
1600         while(q) {
1601                 qn = q->next;
1602                 if (q->dead) {
1603                         if (ql)
1604                                 ql->next = q->next;
1605                         else
1606                                 queues = q->next;
1607                         if (!q->count) {
1608                                 free(q);
1609                         } else
1610                                 ast_log(LOG_WARNING, "XXX Leaking a litttle memory :( XXX\n");
1611                 } else
1612                         ql = q;
1613                 q = qn;
1614         }
1615         ast_mutex_unlock(&qlock);
1616 }
1617
1618 static int __queues_show(int fd, int argc, char **argv, int queue_show)
1619 {
1620         struct ast_call_queue *q;
1621         struct queue_ent *qe;
1622         struct member *mem;
1623         int pos;
1624         time_t now;
1625         char max[80];
1626         char calls[80];
1627         float sl = 0;
1628
1629         time(&now);
1630         if ((!queue_show && argc != 2) || (queue_show && argc != 3))
1631                 return RESULT_SHOWUSAGE;
1632         ast_mutex_lock(&qlock);
1633         q = queues;
1634         if (!q) {       
1635                 ast_mutex_unlock(&qlock);
1636                 if (queue_show)
1637                         ast_cli(fd, "No such queue: %s.\n",argv[2]);
1638                 else
1639                         ast_cli(fd, "No queues.\n");
1640                 return RESULT_SUCCESS;
1641         }
1642         while(q) {
1643                 ast_mutex_lock(&q->lock);
1644                 if (queue_show) {
1645                         if (strcasecmp(q->name, argv[2]) != 0) {
1646                                 ast_mutex_unlock(&q->lock);
1647                                 q = q->next;
1648                                 if (!q) {
1649                                         ast_cli(fd, "No such queue: %s.\n",argv[2]);
1650                                         break;
1651                                 }
1652                                 continue;
1653                         }
1654                 }
1655                 if (q->maxlen)
1656                         snprintf(max, sizeof(max), "%d", q->maxlen);
1657                 else
1658                         strcpy(max, "unlimited");
1659                 sl = 0;
1660                 if(q->callscompleted > 0)
1661                         sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
1662                 ast_cli(fd, "%-12.12s has %d calls (max %s) in '%s' strategy (%ds holdtime), C:%d, A:%d, SL:%2.1f%% within %ds\n",
1663                         q->name, q->count, max, int2strat(q->strategy), q->holdtime, q->callscompleted, q->callsabandoned,sl,q->servicelevel);
1664                 if (q->members) {
1665                         ast_cli(fd, "   Members: \n");
1666                         for (mem = q->members; mem; mem = mem->next) {
1667                                 if (mem->penalty)
1668                                         snprintf(max, sizeof(max) - 20, " with penalty %d", mem->penalty);
1669                                 else
1670                                         strcpy(max, "");
1671                                 if (mem->dynamic)
1672                                         strcat(max, " (dynamic)");
1673                                 if (mem->calls) {
1674                                         snprintf(calls, sizeof(calls), " has taken %d calls (last was %ld secs ago)",
1675                                                         mem->calls, (long)(time(NULL) - mem->lastcall));
1676                                 } else
1677                                         strcpy(calls, " has taken no calls yet");
1678                                 ast_cli(fd, "      %s/%s%s%s\n", mem->tech, mem->loc, max, calls);
1679                         }
1680                 } else
1681                         ast_cli(fd, "   No Members\n");
1682                 if (q->head) {
1683                         pos = 1;
1684                         ast_cli(fd, "   Callers: \n");
1685                         for (qe = q->head; qe; qe = qe->next) 
1686                                 ast_cli(fd, "      %d. %s (wait: %ld:%2.2ld)\n", pos++, qe->chan->name,
1687                                                                 (long)(now - qe->start) / 60, (long)(now - qe->start) % 60);
1688                 } else
1689                         ast_cli(fd, "   No Callers\n");
1690                 ast_cli(fd, "\n");
1691                 ast_mutex_unlock(&q->lock);
1692                 q = q->next;
1693                 if (queue_show)
1694                         break;
1695         }
1696         ast_mutex_unlock(&qlock);
1697         return RESULT_SUCCESS;
1698 }
1699
1700 static int queues_show(int fd, int argc, char **argv)
1701 {
1702         return __queues_show(fd, argc, argv, 0);
1703 }
1704
1705 static int queue_show(int fd, int argc, char **argv)
1706 {
1707         return __queues_show(fd, argc, argv, 1);
1708 }
1709
1710 static char *complete_queue(char *line, char *word, int pos, int state)
1711 {
1712         struct ast_call_queue *q;
1713         int which=0;
1714         
1715         ast_mutex_lock(&qlock);
1716         q = queues;
1717         while(q) {
1718                 if (!strncasecmp(word, q->name, strlen(word))) {
1719                         if (++which > state)
1720                                 break;
1721                 }
1722                 q = q->next;
1723         }
1724         ast_mutex_unlock(&qlock);
1725         return q ? strdup(q->name) : NULL;
1726 }
1727
1728 /* JDG: callback to display queues status in manager */
1729 static int manager_queues_show( struct mansession *s, struct message *m )
1730 {
1731         char *a[] = { "show", "queues" };
1732         return queues_show( s->fd, 2, a );
1733 } /* /JDG */
1734
1735
1736 /* Dump queue status */
1737 static int manager_queues_status( struct mansession *s, struct message *m )
1738 {
1739         time_t now;
1740         int pos;
1741         char *id = astman_get_header(m,"ActionID");
1742         char idText[256] = "";
1743         struct ast_call_queue *q;
1744         struct queue_ent *qe;
1745         float sl = 0;
1746         struct member *mem;
1747         astman_send_ack(s, m, "Queue status will follow");
1748         time(&now);
1749         ast_mutex_lock(&qlock);
1750         q = queues;
1751         if (id && &id) {
1752                 snprintf(idText,256,"ActionID: %s\r\n",id);
1753         }
1754         while(q) {
1755                 ast_mutex_lock(&q->lock);
1756
1757                 /* List queue properties */
1758                 if(q->callscompleted > 0)
1759                         sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
1760                 ast_cli(s->fd, "Event: QueueParams\r\n"
1761                                         "Queue: %s\r\n"
1762                                         "Max: %d\r\n"
1763                                         "Calls: %d\r\n"
1764                                         "Holdtime: %d\r\n"
1765                                         "Completed: %d\r\n"
1766                                         "Abandoned: %d\r\n"
1767                                         "ServiceLevel: %d\r\n"
1768                                         "ServicelevelPerf: %2.1f\r\n"
1769                                         "%s"
1770                                         "\r\n",
1771                                                 q->name, q->maxlen, q->count, q->holdtime, q->callscompleted,
1772                                                 q->callsabandoned, q->servicelevel, sl, idText);
1773
1774                 /* List Queue Members */
1775                 for (mem = q->members; mem; mem = mem->next) 
1776                         ast_cli(s->fd, "Event: QueueMember\r\n"
1777                                 "Queue: %s\r\n"
1778                                 "Location: %s/%s\r\n"
1779                                 "Membership: %s\r\n"
1780                                 "Penalty: %d\r\n"
1781                                 "CallsTaken: %d\r\n"
1782                                 "LastCall: %ld\r\n"
1783                                 "%s"
1784                                 "\r\n",
1785                                         q->name, mem->tech, mem->loc, mem->dynamic ? "dynamic" : "static",
1786                                         mem->penalty, mem->calls, mem->lastcall, idText);
1787
1788                 /* List Queue Entries */
1789
1790                 pos = 1;
1791                 for (qe = q->head; qe; qe = qe->next) 
1792                         ast_cli(s->fd, "Event: QueueEntry\r\n"
1793                                 "Queue: %s\r\n"
1794                                 "Position: %d\r\n"
1795                                 "Channel: %s\r\n"
1796                                 "CallerID: %s\r\n"
1797                                 "Wait: %ld\r\n"
1798                                 "%s"
1799                                 "\r\n", 
1800                                         q->name, pos++, qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : ""), (long)(now - qe->start), idText);
1801                 ast_mutex_unlock(&q->lock);
1802                 q = q->next;
1803         }
1804         ast_mutex_unlock(&qlock);
1805         return RESULT_SUCCESS;
1806 }
1807
1808 static char show_queues_usage[] = 
1809 "Usage: show queues\n"
1810 "       Provides summary information on call queues.\n";
1811
1812 static struct ast_cli_entry cli_show_queues = {
1813         { "show", "queues", NULL }, queues_show, 
1814         "Show status of queues", show_queues_usage, NULL };
1815
1816 static char show_queue_usage[] = 
1817 "Usage: show queue\n"
1818 "       Provides summary information on a specified queue.\n";
1819
1820 static struct ast_cli_entry cli_show_queue = {
1821         { "show", "queue", NULL }, queue_show, 
1822         "Show status of a specified queue", show_queue_usage, complete_queue };
1823
1824 int unload_module(void)
1825 {
1826         STANDARD_HANGUP_LOCALUSERS;
1827         ast_cli_unregister(&cli_show_queue);
1828         ast_cli_unregister(&cli_show_queues);
1829         ast_manager_unregister( "Queues" );
1830         ast_manager_unregister( "QueueStatus" );
1831         ast_unregister_application(app_aqm);
1832         ast_unregister_application(app_rqm);
1833         return ast_unregister_application(app);
1834 }
1835
1836 int load_module(void)
1837 {
1838         int res;
1839         res = ast_register_application(app, queue_exec, synopsis, descrip);
1840         if (!res) {
1841                 ast_cli_register(&cli_show_queue);
1842                 ast_cli_register(&cli_show_queues);
1843                 ast_manager_register( "Queues", 0, manager_queues_show, "Queues" );
1844                 ast_manager_register( "QueueStatus", 0, manager_queues_status, "Queue Status" );
1845
1846                 // [PHM 06/26/03]
1847                 ast_register_application(app_aqm, aqm_exec, app_aqm_synopsis, app_aqm_descrip) ;
1848                 ast_register_application(app_rqm, rqm_exec, app_rqm_synopsis, app_rqm_descrip) ;
1849         }
1850         reload_queues();
1851         return res;
1852 }
1853
1854
1855 int reload(void)
1856 {
1857         reload_queues();
1858         return 0;
1859 }
1860
1861 char *description(void)
1862 {
1863         return tdesc;
1864 }
1865
1866 int usecount(void)
1867 {
1868         int res;
1869         STANDARD_USECOUNT(res);
1870         return res;
1871 }
1872
1873 char *key()
1874 {
1875         return ASTERISK_GPL_KEY;
1876 }