2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of Session Initiation Protocol
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
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/lock.h>
26 #include <asterisk/sched.h>
27 #include <asterisk/io.h>
28 #include <asterisk/rtp.h>
29 #include <asterisk/acl.h>
30 #include <asterisk/callerid.h>
31 #include <asterisk/file.h>
32 #include <asterisk/cli.h>
33 #include <asterisk/app.h>
34 #include <asterisk/musiconhold.h>
35 #include <asterisk/manager.h>
36 #include <asterisk/parking.h>
37 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #include <sys/signal.h>
46 static char *desc = "Agent Proxy Channel";
47 static char *type = "Agent";
48 static char *tdesc = "Call Agent Proxy Channel";
49 static char *config = "agents.conf";
51 static char *app = "AgentLogin";
52 static char *app2 = "AgentCallbackLogin";
54 static char *synopsis = "Call agent login";
55 static char *synopsis2 = "Call agent callback login";
57 static char *descrip =
58 " AgentLogin([AgentNo][|options]):\n"
59 "Asks the agent to login to the system. Always returns -1. While\n"
60 "logged in, the agent can receive calls and will hear a 'beep'\n"
61 "when a new call comes in. The agent can dump the call by pressing\n"
63 "The option string may contain zero or more of the following characters:\n"
64 " 's' -- silent login - do not announce the login ok segment\n";
66 static char *descrip2 =
67 " AgentCallbackLogin([AgentNo][|[options][exten]@context]):\n"
68 "Asks the agent to login to the system with callback. Always returns -1.\n"
69 "The agent's callback extension is called (optionally with the specified\n"
72 static char moh[80] = "default";
74 #define AST_MAX_AGENT 80 /* Agent ID or Password max length */
75 #define AST_MAX_BUF 256
77 static int capability = -1;
79 static unsigned int group;
80 static int autologoff;
81 static int wrapuptime;
85 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
87 /* Protect the interface list (of sip_pvt's) */
88 static ast_mutex_t agentlock = AST_MUTEX_INITIALIZER;
90 int recordagentcalls = 0;
91 char recordformat[AST_MAX_BUF];
92 char recordformatext[AST_MAX_BUF];
94 char urlprefix[AST_MAX_BUF];
95 char savecallsin[AST_MAX_BUF];
97 static struct agent_pvt {
98 ast_mutex_t lock; /* Channel private lock */
99 int dead; /* Poised for destruction? */
100 int pending; /* Not a real agent -- just pending a match */
101 int abouttograb; /* About to grab */
102 int autologoff; /* Auto timeout time */
103 int ackcall; /* ackcall */
104 time_t start; /* When call started */
105 struct timeval lastdisc; /* When last disconnected */
106 int wrapuptime; /* Wrapup time in ms */
107 unsigned int group; /* Group memberships */
108 int acknowledged; /* Acknowledged */
109 char moh[80]; /* Which music on hold */
110 char agent[AST_MAX_AGENT]; /* Agent ID */
111 char password[AST_MAX_AGENT]; /* Password for Agent login */
112 char name[AST_MAX_AGENT];
113 ast_mutex_t app_lock; /* Synchronization between owning applications */
114 volatile pthread_t owning_app; /* Owning application thread id */
115 volatile int app_sleep_cond; /* Sleep condition for the login app */
116 struct ast_channel *owner; /* Agent */
118 struct ast_channel *chan; /* Channel we use */
119 struct agent_pvt *next; /* Agent */
122 #define CHECK_FORMATS(ast, p) do { \
124 if (ast->nativeformats != p->chan->nativeformats) { \
125 ast_log(LOG_DEBUG, "Native formats changing from %d to %d\n", ast->nativeformats, p->chan->nativeformats); \
126 /* Native formats changed, reset things */ \
127 ast->nativeformats = p->chan->nativeformats; \
128 ast_log(LOG_DEBUG, "Resetting read to %d and write to %d\n", ast->readformat, ast->writeformat);\
129 ast_set_read_format(ast, ast->readformat); \
130 ast_set_write_format(ast, ast->writeformat); \
132 if (p->chan->readformat != ast->pvt->rawreadformat) \
133 ast_set_read_format(p->chan, ast->pvt->rawreadformat); \
134 if (p->chan->writeformat != ast->pvt->rawwriteformat) \
135 ast_set_write_format(p->chan, ast->pvt->rawwriteformat); \
139 /* Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
140 properly for a timingfd XXX This might need more work if agents were logged in as agents or other
141 totally impractical combinations XXX */
143 #define CLEANUP(ast, p) do { \
146 for (x=0;x<AST_MAX_FDS;x++) {\
147 if (x != AST_MAX_FDS - 2) \
148 ast->fds[x] = p->chan->fds[x]; \
150 ast->fds[AST_MAX_FDS - 3] = p->chan->fds[AST_MAX_FDS - 2]; \
155 static void agent_unlink(struct agent_pvt *agent)
157 struct agent_pvt *p, *prev;
163 prev->next = agent->next;
165 agents = agent->next;
173 static struct agent_pvt *add_agent(char *agent, int pending)
175 char tmp[AST_MAX_BUF];
176 char *password=NULL, *name=NULL;
177 struct agent_pvt *p, *prev;
179 strncpy(tmp, agent, sizeof(tmp));
180 if ((password = strchr(tmp, ','))) {
183 while (*password < 33) password++;
185 if (password && (name = strchr(password, ','))) {
188 while (*name < 33) name++;
193 if (!pending && !strcmp(p->agent, tmp))
199 p = malloc(sizeof(struct agent_pvt));
201 memset(p, 0, sizeof(struct agent_pvt));
202 strncpy(p->agent, tmp, sizeof(p->agent) -1);
203 ast_mutex_init( &p->lock );
204 ast_mutex_init( &p->app_lock );
205 p->owning_app = (pthread_t) -1;
206 p->app_sleep_cond = 1;
208 p->pending = pending;
219 strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
220 strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
221 strncpy(p->moh, moh, sizeof(p->moh) - 1);
222 p->ackcall = ackcall;
223 p->autologoff = autologoff;
224 p->wrapuptime = wrapuptime;
232 static int agent_cleanup(struct agent_pvt *p)
234 struct ast_channel *chan = p->owner;
236 chan->pvt->pvt = NULL;
237 p->app_sleep_cond = 1;
238 /* Release ownership of the agent to other threads (presumably running the login app). */
239 ast_mutex_unlock(&p->app_lock);
241 ast_channel_free(chan);
247 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
249 static int agent_answer(struct ast_channel *ast)
251 ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
255 static int agent_start_monitoring(struct ast_channel *ast, int needlock)
257 struct agent_pvt *p = ast->pvt->pvt;
258 char tmp[AST_MAX_BUF],tmp2[AST_MAX_BUF], *pointer;
259 char filename[AST_MAX_BUF];
264 snprintf(filename, sizeof(filename), "agent-%s-%s",p->agent, ast->uniqueid);
265 snprintf(tmp, sizeof(tmp), "%s%s",savecallsin ? savecallsin : "", filename);
266 if ((pointer = strchr(tmp, '.')))
268 ast_monitor_start(ast, recordformat, tmp, needlock);
269 ast_monitor_setjoinfiles(ast, 1);
270 snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix ? urlprefix : "", filename, recordformatext);
272 ast_verbose("name is %s, link is %s\n",tmp, tmp2);
275 ast->cdr = ast_cdr_alloc();
276 ast_cdr_setuserfield(ast, tmp2);
279 ast_log(LOG_ERROR, "Recording already started on that call.\n");
282 static struct ast_frame *agent_read(struct ast_channel *ast)
284 struct agent_pvt *p = ast->pvt->pvt;
285 struct ast_frame *f = NULL;
286 static struct ast_frame null_frame = { AST_FRAME_NULL, };
287 static struct ast_frame answer_frame = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
288 ast_mutex_lock(&p->lock);
289 CHECK_FORMATS(ast, p);
291 p->chan->exception = ast->exception;
292 if (ast->fdno == AST_MAX_FDS - 3)
293 p->chan->fdno = AST_MAX_FDS - 2;
295 p->chan->fdno = ast->fdno;
296 f = ast_read(p->chan);
300 /* If there's a channel, hang it up (if it's on a callback) make it NULL */
302 /* Note that we don't hangup if it's not a callback because Asterisk will do it
303 for us when the PBX instance that called login finishes */
304 if (strlen(p->loginchan))
310 if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
313 if (option_verbose > 2)
314 ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
315 /* Don't pass answer along */
324 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
325 if (!p->acknowledged) {
326 if (option_verbose > 2)
327 ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
333 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
334 /* * terminates call */
339 ast_mutex_unlock(&p->lock);
340 if (f == &answer_frame)
341 agent_start_monitoring(ast,0);
345 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
347 struct agent_pvt *p = ast->pvt->pvt;
349 CHECK_FORMATS(ast, p);
350 ast_mutex_lock(&p->lock);
352 if ((f->frametype != AST_FRAME_VOICE) ||
353 (f->subclass == p->chan->writeformat)) {
354 res = ast_write(p->chan, f);
356 ast_log(LOG_DEBUG, "Dropping one incompatible voice frame on '%s' to '%s'\n", ast->name, p->chan->name);
362 ast_mutex_unlock(&p->lock);
366 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
368 struct agent_pvt *p = newchan->pvt->pvt;
369 ast_mutex_lock(&p->lock);
370 if (p->owner != oldchan) {
371 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
372 ast_mutex_unlock(&p->lock);
376 ast_mutex_unlock(&p->lock);
380 static int agent_indicate(struct ast_channel *ast, int condition)
382 struct agent_pvt *p = ast->pvt->pvt;
384 ast_mutex_lock(&p->lock);
386 res = ast_indicate(p->chan, condition);
389 ast_mutex_unlock(&p->lock);
393 static int agent_digit(struct ast_channel *ast, char digit)
395 struct agent_pvt *p = ast->pvt->pvt;
397 ast_mutex_lock(&p->lock);
399 res = p->chan->pvt->send_digit(p->chan, digit);
402 ast_mutex_unlock(&p->lock);
406 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
408 struct agent_pvt *p = ast->pvt->pvt;
410 ast_mutex_lock(&p->lock);
414 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
415 ast_setstate(ast, AST_STATE_DIALING);
418 ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
421 ast_mutex_unlock(&p->lock);
423 } else if (strlen(p->loginchan)) {
425 /* Call on this agent */
426 if (option_verbose > 2)
427 ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
428 if (p->chan->callerid)
429 free(p->chan->callerid);
431 p->chan->callerid = strdup(ast->callerid);
433 p->chan->callerid = NULL;
434 res = ast_call(p->chan, p->loginchan, 0);
436 ast_mutex_unlock(&p->lock);
439 ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
440 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
441 res = ast_streamfile(p->chan, "beep", p->chan->language);
442 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
444 res = ast_waitstream(p->chan, "");
445 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
448 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
449 ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
451 ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
458 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
459 ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
461 ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
465 /* Call is immediately up, or might need ack */
467 ast_setstate(ast, AST_STATE_RINGING);
469 ast_setstate(ast, AST_STATE_UP);
470 agent_start_monitoring(ast,0);
476 ast_mutex_unlock(&p->lock);
480 static int agent_hangup(struct ast_channel *ast)
482 struct agent_pvt *p = ast->pvt->pvt;
484 ast_mutex_lock(&p->lock);
486 ast->pvt->pvt = NULL;
487 p->app_sleep_cond = 1;
489 if (p->start && (ast->_state != AST_STATE_UP))
490 howlong = time(NULL) - p->start;
493 /* If they're dead, go ahead and hang up on the agent now */
494 if (strlen(p->loginchan)) {
496 /* Recognize the hangup and pass it along immediately */
500 ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
501 if (howlong && p->autologoff && (howlong > p->autologoff)) {
502 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
503 strcpy(p->loginchan, "");
505 } else if (p->dead) {
506 ast_mutex_lock(&p->chan->lock);
507 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
508 ast_mutex_unlock(&p->chan->lock);
510 ast_mutex_lock(&p->chan->lock);
511 ast_moh_start(p->chan, p->moh);
512 ast_mutex_unlock(&p->chan->lock);
516 ast_mutex_unlock(&p->lock);
517 /* Release ownership of the agent to other threads (presumably running the login app). */
518 ast_mutex_unlock(&p->app_lock);
519 } else if (p->dead) {
520 /* Go ahead and lose it */
521 ast_mutex_unlock(&p->lock);
522 /* Release ownership of the agent to other threads (presumably running the login app). */
523 ast_mutex_unlock(&p->app_lock);
525 ast_mutex_unlock(&p->lock);
526 /* Release ownership of the agent to other threads (presumably running the login app). */
527 ast_mutex_unlock(&p->app_lock);
530 ast_mutex_unlock(&p->lock);
533 ast_mutex_lock(&agentlock);
535 ast_mutex_unlock(&agentlock);
537 if (p->abouttograb) {
538 /* Let the "about to grab" thread know this isn't valid anymore, and let it
541 } else if (p->dead) {
545 /* Not dead -- check availability now */
546 ast_mutex_lock(&p->lock);
547 /* Store last disconnect time */
548 gettimeofday(&p->lastdisc, NULL);
549 ast_mutex_unlock(&p->lock);
551 /* Release ownership of the agent to other threads (presumably running the login app). */
552 ast_mutex_unlock(&p->app_lock);
557 static int agent_cont_sleep( void *data )
563 p = (struct agent_pvt *)data;
565 ast_mutex_lock(&p->lock);
566 res = p->app_sleep_cond;
567 if (p->lastdisc.tv_sec) {
568 gettimeofday(&tv, NULL);
569 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
570 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
573 ast_mutex_unlock(&p->lock);
576 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
581 static int agent_ack_sleep( void *data )
588 /* Wait a second and look for something */
590 p = (struct agent_pvt *)data;
593 to = ast_waitfor(p->chan, to);
602 f = ast_read(p->chan);
607 if (f->frametype == AST_FRAME_DTMF)
612 ast_mutex_lock(&p->lock);
613 if (!p->app_sleep_cond) {
614 ast_mutex_unlock(&p->lock);
617 } else if (res == '#') {
618 ast_mutex_unlock(&p->lock);
622 ast_mutex_unlock(&p->lock);
630 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
632 struct ast_channel *tmp;
633 struct ast_frame null_frame = { AST_FRAME_NULL };
636 ast_log(LOG_WARNING, "No channel? :(\n");
640 tmp = ast_channel_alloc(0);
643 tmp->nativeformats = p->chan->nativeformats;
644 tmp->writeformat = p->chan->writeformat;
645 tmp->pvt->rawwriteformat = p->chan->writeformat;
646 tmp->readformat = p->chan->readformat;
647 tmp->pvt->rawreadformat = p->chan->readformat;
648 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
649 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
650 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
652 tmp->nativeformats = AST_FORMAT_SLINEAR;
653 tmp->writeformat = AST_FORMAT_SLINEAR;
654 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
655 tmp->readformat = AST_FORMAT_SLINEAR;
656 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
659 snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
661 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
663 ast_setstate(tmp, state);
665 tmp->pvt->send_digit = agent_digit;
666 tmp->pvt->call = agent_call;
667 tmp->pvt->hangup = agent_hangup;
668 tmp->pvt->answer = agent_answer;
669 tmp->pvt->read = agent_read;
670 tmp->pvt->write = agent_write;
671 tmp->pvt->exception = agent_read;
672 tmp->pvt->indicate = agent_indicate;
673 tmp->pvt->fixup = agent_fixup;
675 ast_mutex_lock(&usecnt_lock);
677 ast_mutex_unlock(&usecnt_lock);
678 ast_update_use_count();
680 /* Wake up and wait for other applications (by definition the login app)
681 * to release this channel). Takes ownership of the agent channel
682 * to this thread only.
683 * For signalling the other thread, ast_queue_frame is used until we
684 * can safely use signals for this purpose. The pselect() needs to be
685 * implemented in the kernel for this.
687 p->app_sleep_cond = 0;
688 if( ast_mutex_trylock(&p->app_lock) )
691 ast_queue_frame(p->chan, &null_frame, 1);
692 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
693 ast_mutex_lock(&p->app_lock);
694 ast_mutex_lock(&p->lock);
698 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
700 tmp->pvt->pvt = NULL;
701 p->app_sleep_cond = 1;
702 ast_channel_free( tmp );
703 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
704 ast_mutex_unlock(&p->app_lock);
708 p->owning_app = pthread_self();
709 /* After the above step, there should not be any blockers. */
711 if (p->chan->blocking) {
712 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
715 ast_moh_stop(p->chan);
718 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
723 static int read_agent_config(void)
725 struct ast_config *cfg;
726 struct ast_variable *v;
727 struct agent_pvt *p, *pl, *pn;
732 cfg = ast_load(config);
734 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
737 ast_mutex_lock(&agentlock);
743 strcpy(moh, "default");
744 /* set the default recording values */
745 recordagentcalls = 0;
747 strcpy(recordformat, "wav");
748 strcpy(recordformatext, "wav");
749 strcpy(urlprefix, "");
750 strcpy(savecallsin, "");
752 v = ast_variable_browse(cfg, "agents");
754 /* Create the interface list */
755 if (!strcasecmp(v->name, "agent")) {
756 add_agent(v->value, 0);
757 } else if (!strcasecmp(v->name, "group")) {
758 group = ast_get_group(v->value);
759 } else if (!strcasecmp(v->name, "autologoff")) {
760 autologoff = atoi(v->value);
763 } else if (!strcasecmp(v->name, "ackcall")) {
764 if (!strcasecmp(v->value, "always"))
766 else if (ast_true(v->value))
770 } else if (!strcasecmp(v->name, "wrapuptime")) {
771 wrapuptime = atoi(v->value);
774 } else if (!strcasecmp(v->name, "musiconhold")) {
775 strncpy(moh, v->value, sizeof(moh) - 1);
776 } else if (!strcasecmp(v->name, "recordagentcalls")) {
777 recordagentcalls = ast_true(v->value);
778 } else if (!strcasecmp(v->name, "createlink")) {
779 createlink = ast_true(v->value);
780 } else if (!strcasecmp(v->name, "recordformat")) {
781 strncpy(recordformat, v->value, sizeof(recordformat) - 1);
782 if (!strcasecmp(v->value, "wav49"))
783 strcpy(recordformatext, "WAV");
785 strncpy(recordformatext, v->value, sizeof(recordformat) - 1);
786 } else if (!strcasecmp(v->name, "urlprefix")) {
787 strncpy(urlprefix, v->value, sizeof(urlprefix) - 2);
788 if (urlprefix[strlen(urlprefix) - 1] != '/')
789 strcat(urlprefix, "/");
790 } else if (!strcasecmp(v->name, "savecallsin")) {
791 if (v->value[0] == '/')
792 strncpy(savecallsin, v->value, sizeof(savecallsin) - 2);
794 snprintf(savecallsin, sizeof(savecallsin) - 2, "/%s", v->value);
795 if (savecallsin[strlen(savecallsin) - 1] != '/')
796 strcat(savecallsin, "/");
810 /* Destroy if appropriate */
815 /* Cause them to hang up */
816 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
823 ast_mutex_unlock(&agentlock);
828 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
830 struct ast_channel *chan=NULL, *parent=NULL;
833 ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
835 ast_mutex_lock(&agentlock);
838 if (p == newlyavailable) {
842 ast_mutex_lock(&p->lock);
843 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
844 ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
845 /* We found a pending call, time to merge */
846 chan = agent_new(newlyavailable, AST_STATE_DOWN);
849 ast_mutex_unlock(&p->lock);
852 ast_mutex_unlock(&p->lock);
856 ast_mutex_unlock(&agentlock);
857 if (parent && chan) {
858 if (newlyavailable->ackcall > 1) {
859 /* Don't do beep here */
862 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
863 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
864 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
866 res = ast_waitstream(newlyavailable->chan, "");
867 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
871 /* Note -- parent may have disappeared */
872 if (p->abouttograb) {
873 newlyavailable->acknowledged = 1;
874 ast_setstate(parent, AST_STATE_UP);
875 ast_setstate(chan, AST_STATE_UP);
876 strncpy(parent->context, chan->context, sizeof(parent->context) - 1);
877 /* Go ahead and mark the channel as a zombie so that masquerade will
878 destroy it for us, and we need not call ast_hangup */
879 ast_mutex_lock(&parent->lock);
881 ast_channel_masquerade(parent, chan);
882 ast_mutex_unlock(&parent->lock);
885 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
886 agent_cleanup(newlyavailable);
889 ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
890 agent_cleanup(newlyavailable);
896 static int check_beep(struct agent_pvt *newlyavailable, int needlock)
900 ast_log(LOG_DEBUG, "Checking beep availability of '%s'\n", newlyavailable->agent);
902 ast_mutex_lock(&agentlock);
905 if (p == newlyavailable) {
909 ast_mutex_lock(&p->lock);
910 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
911 ast_log(LOG_DEBUG, "Call '%s' looks like a would-be winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
912 ast_mutex_unlock(&p->lock);
915 ast_mutex_unlock(&p->lock);
919 ast_mutex_unlock(&agentlock);
921 ast_mutex_unlock(&newlyavailable->lock);
922 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
923 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
924 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
926 res = ast_waitstream(newlyavailable->chan, "");
927 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
929 ast_mutex_lock(&newlyavailable->lock);
934 static struct ast_channel *agent_request(char *type, int format, void *data)
937 struct ast_channel *chan = NULL;
939 unsigned int groupmatch;
943 if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
944 groupmatch = (1 << groupmatch);
945 } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
946 groupmatch = (1 << groupmatch);
952 /* Check actual logged in agents first */
953 ast_mutex_lock(&agentlock);
956 ast_mutex_lock(&p->lock);
957 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
958 !strlen(p->loginchan)) {
961 if (!p->lastdisc.tv_sec) {
962 /* Agent must be registered, but not have any active call, and not be in a waiting state */
963 if (!p->owner && p->chan) {
965 chan = agent_new(p, AST_STATE_DOWN);
968 ast_mutex_unlock(&p->lock);
973 ast_mutex_unlock(&p->lock);
979 ast_mutex_lock(&p->lock);
980 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
981 if (p->chan || strlen(p->loginchan))
983 if (!p->lastdisc.tv_sec) {
984 /* Agent must be registered, but not have any active call, and not be in a waiting state */
985 if (!p->owner && p->chan) {
986 /* Could still get a fixed agent */
987 chan = agent_new(p, AST_STATE_DOWN);
988 } else if (!p->owner && strlen(p->loginchan)) {
989 /* Adjustable agent */
990 p->chan = ast_request("Local", format, p->loginchan);
992 chan = agent_new(p, AST_STATE_DOWN);
995 ast_mutex_unlock(&p->lock);
1000 ast_mutex_unlock(&p->lock);
1005 if (!chan && waitforagent) {
1006 /* No agent available -- but we're requesting to wait for one.
1007 Allocate a place holder */
1009 ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
1010 p = add_agent(data, 1);
1011 p->group = groupmatch;
1012 chan = agent_new(p, AST_STATE_DOWN);
1014 ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
1017 ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s);
1019 ast_mutex_unlock(&agentlock);
1023 static int powerof(unsigned int v)
1026 for (x=0;x<32;x++) {
1027 if (v & (1 << x)) return x;
1032 static int agents_show(int fd, int argc, char **argv)
1034 struct agent_pvt *p;
1035 char username[AST_MAX_BUF];
1036 char location[AST_MAX_BUF];
1037 char talkingto[AST_MAX_BUF];
1038 char moh[AST_MAX_BUF];
1041 return RESULT_SHOWUSAGE;
1042 ast_mutex_lock(&agentlock);
1045 ast_mutex_lock(&p->lock);
1048 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
1050 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
1052 if (strlen(p->name))
1053 snprintf(username, sizeof(username), "(%s) ", p->name);
1055 strcpy(username, "");
1057 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
1058 if (p->owner && p->owner->bridge) {
1059 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
1061 strcpy(talkingto, " is idle");
1063 } else if (strlen(p->loginchan)) {
1064 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
1065 strcpy(talkingto, "");
1066 if (p->acknowledged)
1067 strcat(location, " (Confirmed)");
1069 strcpy(location, "not logged in");
1070 strcpy(talkingto, "");
1073 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
1074 ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
1075 username, location, talkingto, moh);
1077 ast_mutex_unlock(&p->lock);
1080 ast_mutex_unlock(&agentlock);
1081 return RESULT_SUCCESS;
1084 static char show_agents_usage[] =
1085 "Usage: show agents\n"
1086 " Provides summary information on agents.\n";
1088 static struct ast_cli_entry cli_show_agents = {
1089 { "show", "agents", NULL }, agents_show,
1090 "Show status of agents", show_agents_usage, NULL };
1092 STANDARD_LOCAL_USER;
1095 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
1099 struct agent_pvt *p;
1100 struct localuser *u;
1102 char user[AST_MAX_AGENT];
1103 char pass[AST_MAX_AGENT];
1104 char xpass[AST_MAX_AGENT] = "";
1107 char *opt_user = NULL;
1108 char *options = NULL;
1109 char *context = NULL;
1111 int play_announcement;
1112 char *filename = "agent-loginok";
1116 /* Parse the arguments XXX Check for failure XXX */
1117 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
1120 options = strchr(opt_user, '|');
1125 context = strchr(options, '@');
1131 while(*exten && ((*exten < '0') || (*exten > '9'))) exten++;
1138 if (chan->_state != AST_STATE_UP)
1139 res = ast_answer(chan);
1141 if( opt_user && strlen(opt_user))
1142 strncpy( user, opt_user, AST_MAX_AGENT );
1144 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
1146 while (!res && (tries < 3)) {
1147 /* Check for password */
1148 ast_mutex_lock(&agentlock);
1151 if (!strcmp(p->agent, user) && !p->pending)
1152 strncpy(xpass, p->password, sizeof(xpass) - 1);
1155 ast_mutex_unlock(&agentlock);
1158 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
1162 errmsg = "agent-incorrect";
1165 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
1168 /* Check again for accuracy */
1169 ast_mutex_lock(&agentlock);
1172 ast_mutex_lock(&p->lock);
1173 if (!strcmp(p->agent, user) &&
1174 !strcmp(p->password, pass) && !p->pending) {
1177 char tmpchan[AST_MAX_BUF] = "";
1179 /* Retrieve login chan */
1182 strncpy(tmpchan, exten, sizeof(tmpchan) - 1);
1185 res = ast_app_getdata(chan, "agent-newlocation", tmpchan+pos, sizeof(tmpchan) - 2, 0);
1186 if (!strlen(tmpchan) || ast_exists_extension(chan, context && strlen(context) ? context : "default", tmpchan,
1190 ast_log(LOG_WARNING, "Extension '%s' is not valid for automatic login of agent '%s'\n", exten, p->agent);
1194 res = ast_streamfile(chan, "invalid", chan->language);
1196 res = ast_waitstream(chan, AST_DIGIT_ANY);
1208 if (context && strlen(context) && strlen(tmpchan))
1209 snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
1211 strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
1212 if (!strlen(p->loginchan))
1213 filename = "agent-loggedoff";
1214 p->acknowledged = 0;
1217 strcpy(p->loginchan, "");
1218 p->acknowledged = 0;
1220 play_announcement = 1;
1222 if( strchr( options, 's' ) )
1223 play_announcement = 0;
1224 ast_mutex_unlock(&p->lock);
1225 ast_mutex_unlock(&agentlock);
1226 if( !res && play_announcement )
1227 res = ast_streamfile(chan, filename, chan->language);
1229 ast_waitstream(chan, "");
1230 ast_mutex_lock(&agentlock);
1231 ast_mutex_lock(&p->lock);
1233 res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
1235 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
1238 ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
1240 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
1242 /* Check once more just in case */
1245 if (callbackmode && !res) {
1246 /* Just say goodbye and be done with it */
1247 ast_mutex_unlock(&agentlock);
1249 res = ast_safe_sleep(chan, 500);
1250 res = ast_streamfile(chan, "vm-goodbye", chan->language);
1252 res = ast_waitstream(chan, "");
1254 res = ast_safe_sleep(chan, 1000);
1255 ast_mutex_unlock(&p->lock);
1257 #ifdef HONOR_MUSIC_CLASS
1258 /* check if the moh class was changed with setmusiconhold */
1259 if (*(chan->musicclass))
1260 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
1262 ast_moh_start(chan, p->moh);
1263 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
1266 p->agent, chan->name);
1267 if (option_verbose > 2)
1268 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %s/%s)\n", p->agent,
1269 ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
1270 /* Login this channel and wait for it to
1276 check_availability(p, 0);
1277 ast_mutex_unlock(&p->lock);
1278 ast_mutex_unlock(&agentlock);
1280 ast_mutex_lock(&p->lock);
1281 if (p->chan != chan)
1283 ast_mutex_unlock(&p->lock);
1284 /* Yield here so other interested threads can kick in. */
1289 ast_mutex_lock(&agentlock);
1290 ast_mutex_lock(&p->lock);
1291 if (p->lastdisc.tv_sec) {
1292 gettimeofday(&tv, NULL);
1293 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
1294 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
1295 ast_log(LOG_DEBUG, "Wrapup time expired!\n");
1296 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
1300 check_availability(p, 0);
1303 ast_mutex_unlock(&p->lock);
1304 ast_mutex_unlock(&agentlock);
1305 /* Synchronize channel ownership between call to agent and itself. */
1306 ast_mutex_lock( &p->app_lock );
1307 ast_mutex_lock(&p->lock);
1308 p->owning_app = pthread_self();
1309 ast_mutex_unlock(&p->lock);
1311 res = agent_ack_sleep(p);
1313 res = ast_safe_sleep_conditional( chan, 1000,
1314 agent_cont_sleep, p );
1315 ast_mutex_unlock( &p->app_lock );
1316 if ((p->ackcall > 1) && (res == 1)) {
1317 ast_mutex_lock(&agentlock);
1318 ast_mutex_lock(&p->lock);
1319 check_availability(p, 0);
1320 ast_mutex_unlock(&p->lock);
1321 ast_mutex_unlock(&agentlock);
1326 ast_mutex_lock(&p->lock);
1327 if (res && p->owner)
1328 ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
1329 /* Log us off if appropriate */
1330 if (p->chan == chan)
1332 p->acknowledged = 0;
1333 ast_mutex_unlock(&p->lock);
1334 if (option_verbose > 2)
1335 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
1336 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
1339 /* If there is no owner, go ahead and kill it now */
1340 if (p->dead && !p->owner)
1344 ast_mutex_unlock(&p->lock);
1349 ast_mutex_unlock(&p->lock);
1350 errmsg = "agent-alreadyon";
1355 ast_mutex_unlock(&p->lock);
1359 ast_mutex_unlock(&agentlock);
1362 res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1365 LOCAL_USER_REMOVE(u);
1370 static int login_exec(struct ast_channel *chan, void *data)
1372 return __login_exec(chan, data, 0);
1375 static int callback_exec(struct ast_channel *chan, void *data)
1377 return __login_exec(chan, data, 1);
1382 /* Make sure we can register our sip channel type */
1383 if (ast_channel_register(type, tdesc, capability, agent_request)) {
1384 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1387 ast_register_application(app, login_exec, synopsis, descrip);
1388 ast_register_application(app2, callback_exec, synopsis2, descrip2);
1389 ast_cli_register(&cli_show_agents);
1390 /* Read in the config */
1391 read_agent_config();
1397 read_agent_config();
1403 struct agent_pvt *p;
1404 /* First, take us out of the channel loop */
1405 ast_cli_unregister(&cli_show_agents);
1406 ast_unregister_application(app);
1407 ast_unregister_application(app2);
1408 ast_channel_unregister(type);
1409 if (!ast_mutex_lock(&agentlock)) {
1410 /* Hangup all interfaces if they have an owner */
1414 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1418 ast_mutex_unlock(&agentlock);
1420 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1429 ast_mutex_lock(&usecnt_lock);
1431 ast_mutex_unlock(&usecnt_lock);
1437 return ASTERISK_GPL_KEY;