2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/options.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/say.h>
23 #include <asterisk/parking.h>
24 #include <asterisk/musiconhold.h>
25 #include <asterisk/cli.h>
26 #include <asterisk/config.h>
34 #include <sys/signal.h>
35 #include <netinet/in.h>
39 #define DEFAULT_RETRY 5
40 #define DEFAULT_TIMEOUT 15
41 #define RECHECK 1 /* Recheck every second to see we we're at the top yet */
43 static char *tdesc = "True Call Queueing";
45 static char *app = "Queue";
47 static char *synopsis = "Queue a call for a call queue";
49 static char *descrip =
50 " Queue(queuename[|timeout[|options]]):\n"
51 "Queues an incoming call in a particular call queue as defined in queues.conf.\n"
52 " This application returns -1 if the originating channel hangs up, or if the\n"
53 "call is bridged and either of the parties in the bridge terminate the call.\n"
54 "Returns 0 if the queue is full, nonexistant, or has no members.\n"
55 "The option string may contain zero or more of the following characters:\n"
56 " 't' -- allow the called user transfer the calling user\n"
57 " 'T' -- to allow the calling user to transfer the call.\n"
58 " 'd' -- data-quality (modem) call (minimum delay).\n"
59 " 'H' -- allow caller to hang up by hitting *.\n"
60 " In addition to transferring the call, a call may be parked and then picked\n"
61 "up by another user.\n";
63 /* We define a customer "local user" structure because we
64 use it not only for keeping track of what is in use but
65 also for keeping track of who we're dialing. */
68 struct ast_channel *chan;
75 struct localuser *next;
81 struct ast_call_queue *parent; /* What queue is our parent */
82 char moh[80]; /* Name of musiconhold to be used */
83 char announce[80]; /* Announcement to play */
84 char context[80]; /* Announcement to play */
85 int pos; /* Where we are in the queue */
86 time_t start; /* When we started holding */
87 struct ast_channel *chan; /* Our channel */
88 struct queue_ent *next; /* The next queue entry */
92 char tech[80]; /* Technology */
93 char loc[256]; /* Location */
94 struct member *next; /* Next member */
97 struct ast_call_queue {
99 char name[80]; /* Name of the queue */
100 char moh[80]; /* Name of musiconhold to be used */
101 char announce[80]; /* Announcement to play */
102 char context[80]; /* Announcement to play */
103 int announcetimeout; /* How often to announce their position */
104 int count; /* How many entries are in the queue */
105 int maxlen; /* Max number of entries in queue */
107 int dead; /* Whether this queue is dead or not */
108 int retry; /* Retry calling everyone after this amount of time */
109 int timeout; /* How long to wait for an answer */
111 struct member *members; /* Member channels to be tried */
112 struct queue_ent *head; /* Start of the actual queue */
113 struct ast_call_queue *next; /* Next call queue */
116 static struct ast_call_queue *queues = NULL;
117 static pthread_mutex_t qlock = AST_MUTEX_INITIALIZER;
120 static int join_queue(char *queuename, struct queue_ent *qe)
122 struct ast_call_queue *q;
123 struct queue_ent *cur, *prev = NULL;
126 ast_pthread_mutex_lock(&qlock);
129 if (!strcasecmp(q->name, queuename)) {
130 /* This is our one */
131 ast_pthread_mutex_lock(&q->lock);
132 if (q->members && (!q->maxlen || (q->count < q->maxlen))) {
133 /* There's space for us, put us at the end */
145 /* Fix additional pointers and
150 strncpy(qe->moh, q->moh, sizeof(qe->moh));
151 strncpy(qe->announce, q->announce, sizeof(qe->announce));
152 strncpy(qe->context, q->context, sizeof(qe->context));
156 ast_pthread_mutex_unlock(&q->lock);
161 ast_pthread_mutex_unlock(&qlock);
165 static void free_members(struct ast_call_queue *q)
167 struct member *curm, *next;
177 static void destroy_queue(struct ast_call_queue *q)
179 struct ast_call_queue *cur, *prev = NULL;
180 ast_pthread_mutex_lock(&qlock);
185 prev->next = cur->next;
193 ast_pthread_mutex_unlock(&qlock);
198 static void leave_queue(struct queue_ent *qe)
200 struct ast_call_queue *q;
201 struct queue_ent *cur, *prev = NULL;
206 ast_pthread_mutex_lock(&q->lock);
207 /* Take us out of the queue */
213 /* Take us out of the queue */
215 prev->next = cur->next;
224 ast_pthread_mutex_unlock(&q->lock);
225 if (q->dead && !q->count) {
226 /* It's dead and nobody is in it, so kill it */
231 static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
233 /* Hang up a tree of stuff */
234 struct localuser *oo;
236 /* Hangup any existing lines we have open */
237 if (outgoing->chan != exception)
238 ast_hangup(outgoing->chan);
240 outgoing=outgoing->next;
247 static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir, int *allowdisconnect, char *queue)
256 struct ast_channel *peer = NULL;
257 struct ast_channel *watchers[MAX];
259 struct ast_channel *winner;
261 while(*to && !peer) {
268 /* Keep track of important channels */
270 watchers[pos++] = o->chan;
277 if (numlines == numbusies) {
278 ast_log(LOG_DEBUG, "Everyone is busy at this time\n");
280 ast_log(LOG_NOTICE, "No one is answered queue %s\n", queue);
285 winner = ast_waitfor_n(watchers, pos, to);
288 if (o->stillgoing && (o->chan->_state == AST_STATE_UP)) {
290 if (option_verbose > 2)
291 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
293 *allowredir = o->allowredirect;
294 *allowdisconnect = o->allowdisconnect;
296 } else if (o->chan == winner) {
297 f = ast_read(winner);
299 if (f->frametype == AST_FRAME_CONTROL) {
300 switch(f->subclass) {
301 case AST_CONTROL_ANSWER:
302 /* This is our guy if someone answered. */
304 if (option_verbose > 2)
305 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
307 *allowredir = o->allowredirect;
308 *allowdisconnect = o->allowdisconnect;
311 case AST_CONTROL_BUSY:
312 if (option_verbose > 2)
313 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
316 ast_cdr_busy(in->cdr);
319 case AST_CONTROL_CONGESTION:
320 if (option_verbose > 2)
321 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
324 ast_cdr_busy(in->cdr);
327 case AST_CONTROL_RINGING:
328 if (option_verbose > 2)
329 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
332 ast_indicate(in, AST_CONTROL_RINGING);
337 case AST_CONTROL_OFFHOOK:
338 /* Ignore going off hook */
341 ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
354 if (f && (f->frametype != AST_FRAME_VOICE))
355 printf("Frame type: %d, %d\n", f->frametype, f->subclass);
356 else if (!f || (f->frametype != AST_FRAME_VOICE))
357 printf("Hangup received on %s\n", in->name);
359 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
364 if (f && (f->frametype == AST_FRAME_DTMF) && allowdisconnect &&
365 (f->subclass == '*')) {
366 if (option_verbose > 3)
367 ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
372 if (!*to && (option_verbose > 2))
373 ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
380 static int wait_our_turn(struct queue_ent *qe)
382 struct queue_ent *ch;
385 /* Atomically read the parent head */
386 pthread_mutex_lock(&qe->parent->lock);
387 ch = qe->parent->head;
388 pthread_mutex_unlock(&qe->parent->lock);
389 /* If we are now at the top of the head, break out */
390 if (qe->parent->head == qe)
392 /* Wait a second before checking again */
393 res = ast_waitfordigit(qe->chan, RECHECK * 1000);
400 static int try_calling(struct queue_ent *qe, char *options)
403 struct localuser *outgoing=NULL, *tmp = NULL;
406 int allowdisconnect=0;
407 char numsubst[AST_MAX_EXTENSION];
408 char restofit[AST_MAX_EXTENSION];
410 struct ast_channel *peer;
412 /* Hold the lock while we setup the outgoing calls */
413 ast_pthread_mutex_lock(&qe->parent->lock);
414 cur = qe->parent->members;
416 /* Get a technology/[device:]number pair */
417 tmp = malloc(sizeof(struct localuser));
419 ast_log(LOG_WARNING, "Out of memory\n");
422 memset(tmp, 0, sizeof(struct localuser));
424 if (strchr(options, 't'))
425 tmp->allowredirect = 1;
426 if (strchr(options, 'r'))
427 tmp->ringbackonly = 1;
428 if (strchr(options, 'm'))
429 tmp->musiconhold = 1;
430 if (strchr(options, 'd'))
431 tmp->dataquality = 1;
432 if (strchr(options, 'H'))
433 tmp->allowdisconnect = 1;
436 strncpy(numsubst, cur->loc, sizeof(numsubst)-1);
437 /* If we're dialing by extension, look at the extension to know what to dial */
438 if ((newnum = strstr(numsubst, "BYEXTENSION"))) {
439 strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)-1);
440 snprintf(newnum, sizeof(numsubst) - (newnum - numsubst), "%s%s", qe->chan->exten,restofit);
442 ast_log(LOG_DEBUG, "Dialing by extension %s\n", numsubst);
444 /* Request the peer */
445 tmp->chan = ast_request(cur->tech, qe->chan->nativeformats, numsubst);
447 /* If we can't, just go on to the next call */
449 ast_log(LOG_NOTICE, "Unable to create channel of type '%s'\n", cur->tech);
452 ast_cdr_busy(qe->chan->cdr);
458 /* Don't honor call forwarding on a queue! */
459 if (strlen(tmp->chan->call_forward)) {
460 if (option_verbose > 2)
461 ast_verbose(VERBOSE_PREFIX_3 "Forwarding call to '%s@%s'\n", tmp->chan->call_forward, tmp->chan->context);
462 /* Setup parameters */
463 strncpy(chan->exten, tmp->chan->call_forward, sizeof(chan->exten));
464 strncpy(chan->context, tmp->chan->context, sizeof(chan->context));
467 ast_hangup(tmp->chan);
473 tmp->chan->appl = "AppQueue";
474 tmp->chan->data = "(Outgoing Line)";
475 tmp->chan->whentohangup = 0;
476 if (tmp->chan->callerid)
477 free(tmp->chan->callerid);
479 free(tmp->chan->ani);
480 if (qe->chan->callerid)
481 tmp->chan->callerid = strdup(qe->chan->callerid);
483 tmp->chan->callerid = NULL;
485 tmp->chan->ani = strdup(qe->chan->ani);
487 tmp->chan->ani = NULL;
488 /* Presense of ADSI CPE on outgoing channel follows ours */
489 tmp->chan->adsicpe = qe->chan->adsicpe;
490 /* Place the call, but don't wait on the answer */
491 res = ast_call(tmp->chan, numsubst, 0);
493 /* Again, keep going even if there's an error */
495 ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
496 else if (option_verbose > 2)
497 ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
498 ast_hangup(tmp->chan);
503 if (option_verbose > 2)
504 ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
505 /* Put them in the list of outgoing thingies... We're ready now.
506 XXX If we're forcibly removed, these outgoing calls won't get
508 tmp->stillgoing = -1;
509 tmp->next = outgoing;
511 /* If this line is up, don't try anybody else */
512 if (outgoing->chan->_state == AST_STATE_UP)
517 if (qe->parent->timeout)
518 to = qe->parent->timeout * 1000;
521 ast_pthread_mutex_unlock(&qe->parent->lock);
523 peer = wait_for_answer(qe->chan, outgoing, &to, &allowredir, &allowdisconnect, qe->parent->name);
526 /* Musta gotten hung up */
529 /* Nobody answered, next please? */
535 /* Ah ha! Someone answered within the desired timeframe. Of course after this
536 we will always return with -1 so that it is hung up properly after the
538 hanguptree(outgoing, peer);
539 /* Stop music on hold */
540 ast_moh_stop(qe->chan);
542 if (strlen(qe->announce)) {
544 res2 = ast_streamfile(peer, qe->announce, peer->language);
546 res2 = ast_waitstream(peer, "");
550 /* Agent must have hung up */
551 ast_log(LOG_WARNING, "Agent on %s hungup on the customer. They're going to be pissed.\n", peer->name);
556 /* If appropriate, log that we have a destination channel */
558 ast_cdr_setdestchan(qe->chan->cdr, peer->name);
559 /* Make sure channels are compatible */
560 res = ast_channel_make_compatible(qe->chan, peer);
562 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
566 if (!strcmp(qe->chan->type,"Zap")) {
568 if (tmp->dataquality) x = 0;
569 ast_channel_setoption(qe->chan,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
571 if (!strcmp(peer->type,"Zap")) {
573 if (tmp->dataquality) x = 0;
574 ast_channel_setoption(peer,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
576 /* Drop out of the queue at this point, to prepare for next caller */
578 res = ast_bridge_call(qe->chan, peer, allowredir, allowdisconnect);
582 hanguptree(outgoing, NULL);
586 static int wait_a_bit(struct queue_ent *qe)
589 /* Hold the lock while we setup the outgoing calls */
590 ast_pthread_mutex_lock(&qe->parent->lock);
591 retrywait = qe->parent->retry * 1000;
592 ast_pthread_mutex_unlock(&qe->parent->lock);
593 return ast_waitfordigit(qe->chan, retrywait);
596 static int valid_exit(struct queue_ent *qe, char digit)
599 if (!strlen(qe->context))
603 if (ast_exists_extension(qe->chan, qe->context, tmp, 1, qe->chan->callerid)) {
604 strncpy(qe->chan->context, qe->context, sizeof(qe->chan->context) - 1);
605 strncpy(qe->chan->exten, tmp, sizeof(qe->chan->exten) - 1);
606 qe->chan->priority = 0;
612 static int queue_exec(struct ast_channel *chan, void *data)
618 char *options = NULL;
620 /* Our queue entry */
624 ast_log(LOG_WARNING, "Queue requires an argument (queuename|optional timeout)\n");
630 /* Parse our arguments XXX Check for failure XXX */
631 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
634 options = strchr(queuename, '|');
641 /* Setup our queue entry */
642 memset(&qe, 0, sizeof(qe));
644 qe.start = time(NULL);
645 if (!join_queue(queuename, &qe)) {
646 /* Start music on hold */
647 ast_moh_start(chan, qe.moh);
649 res = wait_our_turn(&qe);
650 /* If they hungup, return immediately */
652 if (option_verbose > 2) {
653 ast_verbose(VERBOSE_PREFIX_3 "User disconnected while waiting their turn\n");
660 if (valid_exit(&qe, res))
665 res = try_calling(&qe, options);
668 res = wait_a_bit(&qe);
670 if (option_verbose > 2) {
671 ast_verbose(VERBOSE_PREFIX_3 "User disconnected when they almost made it\n");
676 if (res && valid_exit(&qe, res))
680 /* Don't allow return code > 0 */
686 ast_log(LOG_WARNING, "Unable to join queue '%s'\n", queuename);
689 LOCAL_USER_REMOVE(u);
693 static void reload_queues(void)
695 struct ast_call_queue *q, *ql, *qn;
696 struct ast_config *cfg;
698 struct ast_variable *var;
699 struct member *prev, *cur;
701 cfg = ast_load("queues.conf");
703 ast_log(LOG_NOTICE, "No call queueing config file, so no call queues\n");
706 ast_pthread_mutex_lock(&qlock);
707 /* Mark all queues as dead for the moment */
713 /* Chug through config file */
714 cat = ast_category_browse(cfg, NULL);
716 if (strcasecmp(cat, "general")) {
717 /* Look for an existing one */
720 if (!strcmp(q->name, cat))
726 q = malloc(sizeof(struct ast_call_queue));
729 memset(q, 0, sizeof(struct ast_call_queue));
730 strncpy(q->name, cat, sizeof(q->name));
737 ast_pthread_mutex_lock(&q->lock);
738 /* Re-initialize the queue */
745 strcpy(q->announce, "");
746 strcpy(q->context, "");
748 var = ast_variable_browse(cfg, cat);
750 if (!strcasecmp(var->name, "member")) {
751 /* Add a new member */
752 cur = malloc(sizeof(struct member));
754 memset(cur, 0, sizeof(struct member));
755 strncpy(cur->tech, var->value, sizeof(cur->tech) - 1);
756 if ((tmp = strchr(cur->tech, '/')))
758 if ((tmp = strchr(var->value, '/'))) {
760 strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
762 ast_log(LOG_WARNING, "No location at line %d of queue.conf\n", var->lineno);
769 } else if (!strcasecmp(var->name, "music")) {
770 strncpy(q->moh, var->value, sizeof(q->moh) - 1);
771 } else if (!strcasecmp(var->name, "announce")) {
772 strncpy(q->announce, var->value, sizeof(q->announce) - 1);
773 } else if (!strcasecmp(var->name, "context")) {
774 strncpy(q->context, var->value, sizeof(q->context) - 1);
775 } else if (!strcasecmp(var->name, "timeout")) {
776 q->timeout = atoi(var->value);
777 } else if (!strcasecmp(var->name, "retry")) {
778 q->retry = atoi(var->value);
779 } else if (!strcasecmp(var->name, "maxlen")) {
780 q->maxlen = atoi(var->value);
782 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queue.conf\n", cat, var->name, var->lineno);
787 q->retry = DEFAULT_RETRY;
789 q->timeout = DEFAULT_TIMEOUT;
793 ast_pthread_mutex_unlock(&q->lock);
800 cat = ast_category_browse(cfg, cat);
814 ast_log(LOG_WARNING, "XXX Leaking a litttle memory :( XXX\n");
819 ast_pthread_mutex_unlock(&qlock);
822 static int queues_show(int fd, int argc, char **argv)
824 struct ast_call_queue *q;
825 struct queue_ent *qe;
833 return RESULT_SHOWUSAGE;
836 ast_cli(fd, "No queues.\n");
837 return RESULT_SUCCESS;
840 ast_pthread_mutex_lock(&q->lock);
842 snprintf(max, sizeof(max), "%d", q->maxlen);
844 strcpy(max, "unlimited");
845 ast_cli(fd, "%-12.12s has %d calls (max %s)\n", q->name, q->count, max);
847 ast_cli(fd, " Members: \n");
848 for (mem = q->members; mem; mem = mem->next)
849 ast_cli(fd, " %s/%s\n", mem->tech, mem->loc);
851 ast_cli(fd, " No Members\n");
854 ast_cli(fd, " Callers: \n");
855 for (qe = q->head; qe; qe = qe->next)
856 ast_cli(fd, " %d. %s (wait: %d:%02.2d)\n", pos++, qe->chan->name,
857 (now - qe->start) / 60, (now - qe->start) % 60);
859 ast_cli(fd, " No Callers\n");
861 ast_pthread_mutex_unlock(&q->lock);
864 return RESULT_SUCCESS;
867 static char show_queues_usage[] =
868 "Usage: show queues\n"
869 " Provides summary information on call queues.\n";
871 static struct ast_cli_entry cli_show_queues = {
872 { "show", "queues", NULL }, queues_show,
873 "Show status of queues", show_queues_usage, NULL };
877 int unload_module(void)
879 STANDARD_HANGUP_LOCALUSERS;
880 ast_cli_unregister(&cli_show_queues);
881 return ast_unregister_application(app);
884 int load_module(void)
887 res = ast_register_application(app, queue_exec, synopsis, descrip);
889 ast_cli_register(&cli_show_queues);
900 char *description(void)
908 STANDARD_USECOUNT(res);
914 return ASTERISK_GPL_KEY;