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 */
76 static int capability = -1;
78 static unsigned int group;
79 static int autologoff;
80 static int wrapuptime;
84 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
86 /* Protect the interface list (of sip_pvt's) */
87 static ast_mutex_t agentlock = AST_MUTEX_INITIALIZER;
89 static struct agent_pvt {
90 ast_mutex_t lock; /* Channel private lock */
91 int dead; /* Poised for destruction? */
92 int pending; /* Not a real agent -- just pending a match */
93 int abouttograb; /* About to grab */
94 int autologoff; /* Auto timeout time */
95 int ackcall; /* ackcall */
96 time_t start; /* When call started */
97 struct timeval lastdisc; /* When last disconnected */
98 int wrapuptime; /* Wrapup time in ms */
99 unsigned int group; /* Group memberships */
100 int acknowledged; /* Acknowledged */
101 char moh[80]; /* Which music on hold */
102 char agent[AST_MAX_AGENT]; /* Agent ID */
103 char password[AST_MAX_AGENT]; /* Password for Agent login */
104 char name[AST_MAX_AGENT];
105 ast_mutex_t app_lock; /* Synchronization between owning applications */
106 volatile pthread_t owning_app; /* Owning application thread id */
107 volatile int app_sleep_cond; /* Sleep condition for the login app */
108 struct ast_channel *owner; /* Agent */
110 struct ast_channel *chan; /* Channel we use */
111 struct agent_pvt *next; /* Agent */
114 #define CHECK_FORMATS(ast, p) do { \
116 if (ast->nativeformats != p->chan->nativeformats) { \
117 ast_log(LOG_DEBUG, "Native formats changing from %d to %d\n", ast->nativeformats, p->chan->nativeformats); \
118 /* Native formats changed, reset things */ \
119 ast->nativeformats = p->chan->nativeformats; \
120 ast_log(LOG_DEBUG, "Resetting read to %d and write to %d\n", ast->readformat, ast->writeformat);\
121 ast_set_read_format(ast, ast->readformat); \
122 ast_set_write_format(ast, ast->writeformat); \
124 if (p->chan->readformat != ast->pvt->rawreadformat) \
125 ast_set_read_format(p->chan, ast->pvt->rawreadformat); \
126 if (p->chan->writeformat != ast->pvt->rawwriteformat) \
127 ast_set_write_format(p->chan, ast->pvt->rawwriteformat); \
131 /* Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
132 properly for a timingfd XXX This might need more work if agents were logged in as agents or other
133 totally impractical combinations XXX */
135 #define CLEANUP(ast, p) do { \
138 for (x=0;x<AST_MAX_FDS;x++) {\
139 if (x != AST_MAX_FDS - 2) \
140 ast->fds[x] = p->chan->fds[x]; \
142 ast->fds[AST_MAX_FDS - 3] = p->chan->fds[AST_MAX_FDS - 2]; \
147 static void agent_unlink(struct agent_pvt *agent)
149 struct agent_pvt *p, *prev;
155 prev->next = agent->next;
157 agents = agent->next;
165 static struct agent_pvt *add_agent(char *agent, int pending)
168 char *password=NULL, *name=NULL;
169 struct agent_pvt *p, *prev;
171 strncpy(tmp, agent, sizeof(tmp));
172 if ((password = strchr(tmp, ','))) {
175 while (*password < 33) password++;
177 if (password && (name = strchr(password, ','))) {
180 while (*name < 33) name++;
185 if (!pending && !strcmp(p->agent, tmp))
191 p = malloc(sizeof(struct agent_pvt));
193 memset(p, 0, sizeof(struct agent_pvt));
194 strncpy(p->agent, tmp, sizeof(p->agent) -1);
195 ast_mutex_init( &p->lock );
196 ast_mutex_init( &p->app_lock );
197 p->owning_app = (pthread_t) -1;
198 p->app_sleep_cond = 1;
200 p->pending = pending;
211 strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
212 strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
213 strncpy(p->moh, moh, sizeof(p->moh) - 1);
214 p->ackcall = ackcall;
215 p->autologoff = autologoff;
216 p->wrapuptime = wrapuptime;
224 static int agent_cleanup(struct agent_pvt *p)
226 struct ast_channel *chan = p->owner;
228 chan->pvt->pvt = NULL;
229 p->app_sleep_cond = 1;
230 /* Release ownership of the agent to other threads (presumably running the login app). */
231 ast_mutex_unlock(&p->app_lock);
233 ast_channel_free(chan);
239 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
241 static int agent_answer(struct ast_channel *ast)
243 ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
247 static struct ast_frame *agent_read(struct ast_channel *ast)
249 struct agent_pvt *p = ast->pvt->pvt;
250 struct ast_frame *f = NULL;
251 static struct ast_frame null_frame = { AST_FRAME_NULL, };
252 static struct ast_frame answer_frame = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
253 ast_mutex_lock(&p->lock);
254 CHECK_FORMATS(ast, p);
256 p->chan->exception = ast->exception;
257 if (ast->fdno == AST_MAX_FDS - 3)
258 p->chan->fdno = AST_MAX_FDS - 2;
260 p->chan->fdno = ast->fdno;
261 f = ast_read(p->chan);
265 /* If there's a channel, hang it up (if it's on a callback) make it NULL */
267 /* Note that we don't hangup if it's not a callback because Asterisk will do it
268 for us when the PBX instance that called login finishes */
269 if (strlen(p->loginchan))
275 if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
278 if (option_verbose > 2)
279 ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
280 /* Don't pass answer along */
289 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
290 if (!p->acknowledged) {
291 if (option_verbose > 2)
292 ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
298 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
299 /* * terminates call */
304 ast_mutex_unlock(&p->lock);
308 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
310 struct agent_pvt *p = ast->pvt->pvt;
312 CHECK_FORMATS(ast, p);
313 ast_mutex_lock(&p->lock);
315 if ((f->frametype != AST_FRAME_VOICE) ||
316 (f->subclass == p->chan->writeformat)) {
317 res = ast_write(p->chan, f);
319 ast_log(LOG_DEBUG, "Dropping one incompatible voice frame on '%s' to '%s'\n", ast->name, p->chan->name);
325 ast_mutex_unlock(&p->lock);
329 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
331 struct agent_pvt *p = newchan->pvt->pvt;
332 ast_mutex_lock(&p->lock);
333 if (p->owner != oldchan) {
334 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
335 ast_mutex_unlock(&p->lock);
339 ast_mutex_unlock(&p->lock);
343 static int agent_indicate(struct ast_channel *ast, int condition)
345 struct agent_pvt *p = ast->pvt->pvt;
347 ast_mutex_lock(&p->lock);
349 res = ast_indicate(p->chan, condition);
352 ast_mutex_unlock(&p->lock);
356 static int agent_digit(struct ast_channel *ast, char digit)
358 struct agent_pvt *p = ast->pvt->pvt;
360 ast_mutex_lock(&p->lock);
362 res = p->chan->pvt->send_digit(p->chan, digit);
365 ast_mutex_unlock(&p->lock);
369 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
371 struct agent_pvt *p = ast->pvt->pvt;
373 ast_mutex_lock(&p->lock);
377 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
378 ast_setstate(ast, AST_STATE_DIALING);
381 ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
384 ast_mutex_unlock(&p->lock);
386 } else if (strlen(p->loginchan)) {
388 /* Call on this agent */
389 if (option_verbose > 2)
390 ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
391 if (p->chan->callerid)
392 free(p->chan->callerid);
394 p->chan->callerid = strdup(ast->callerid);
396 p->chan->callerid = NULL;
397 res = ast_call(p->chan, p->loginchan, 0);
399 ast_mutex_unlock(&p->lock);
402 ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
403 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
404 res = ast_streamfile(p->chan, "beep", p->chan->language);
405 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
407 res = ast_waitstream(p->chan, "");
408 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
411 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
412 ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
414 ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
421 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
422 ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
424 ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
428 /* Call is immediately up, or might need ack */
430 ast_setstate(ast, AST_STATE_RINGING);
432 ast_setstate(ast, AST_STATE_UP);
438 ast_mutex_unlock(&p->lock);
442 static int agent_hangup(struct ast_channel *ast)
444 struct agent_pvt *p = ast->pvt->pvt;
446 ast_mutex_lock(&p->lock);
448 ast->pvt->pvt = NULL;
449 p->app_sleep_cond = 1;
451 if (p->start && (ast->_state != AST_STATE_UP))
452 howlong = time(NULL) - p->start;
455 /* If they're dead, go ahead and hang up on the agent now */
456 if (strlen(p->loginchan)) {
458 /* Recognize the hangup and pass it along immediately */
462 ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
463 if (howlong && p->autologoff && (howlong > p->autologoff)) {
464 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
465 strcpy(p->loginchan, "");
467 } else if (p->dead) {
468 ast_mutex_lock(&p->chan->lock);
469 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
470 ast_mutex_unlock(&p->chan->lock);
472 ast_mutex_lock(&p->chan->lock);
473 ast_moh_start(p->chan, p->moh);
474 ast_mutex_unlock(&p->chan->lock);
478 ast_mutex_unlock(&p->lock);
479 /* Release ownership of the agent to other threads (presumably running the login app). */
480 ast_mutex_unlock(&p->app_lock);
481 } else if (p->dead) {
482 /* Go ahead and lose it */
483 ast_mutex_unlock(&p->lock);
484 /* Release ownership of the agent to other threads (presumably running the login app). */
485 ast_mutex_unlock(&p->app_lock);
487 ast_mutex_unlock(&p->lock);
488 /* Release ownership of the agent to other threads (presumably running the login app). */
489 ast_mutex_unlock(&p->app_lock);
492 ast_mutex_unlock(&p->lock);
495 ast_mutex_lock(&agentlock);
497 ast_mutex_unlock(&agentlock);
499 if (p->abouttograb) {
500 /* Let the "about to grab" thread know this isn't valid anymore, and let it
503 } else if (p->dead) {
507 /* Not dead -- check availability now */
508 ast_mutex_lock(&p->lock);
509 /* Store last disconnect time */
510 gettimeofday(&p->lastdisc, NULL);
511 ast_mutex_unlock(&p->lock);
513 /* Release ownership of the agent to other threads (presumably running the login app). */
514 ast_mutex_unlock(&p->app_lock);
519 static int agent_cont_sleep( void *data )
525 p = (struct agent_pvt *)data;
527 ast_mutex_lock(&p->lock);
528 res = p->app_sleep_cond;
529 if (p->lastdisc.tv_sec) {
530 gettimeofday(&tv, NULL);
531 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
532 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
535 ast_mutex_unlock(&p->lock);
538 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
543 static int agent_ack_sleep( void *data )
550 /* Wait a second and look for something */
552 p = (struct agent_pvt *)data;
555 to = ast_waitfor(p->chan, to);
564 f = ast_read(p->chan);
569 if (f->frametype == AST_FRAME_DTMF)
574 ast_mutex_lock(&p->lock);
575 if (!p->app_sleep_cond) {
576 ast_mutex_unlock(&p->lock);
579 } else if (res == '#') {
580 ast_mutex_unlock(&p->lock);
584 ast_mutex_unlock(&p->lock);
592 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
594 struct ast_channel *tmp;
595 struct ast_frame null_frame = { AST_FRAME_NULL };
598 ast_log(LOG_WARNING, "No channel? :(\n");
602 tmp = ast_channel_alloc(0);
605 tmp->nativeformats = p->chan->nativeformats;
606 tmp->writeformat = p->chan->writeformat;
607 tmp->pvt->rawwriteformat = p->chan->writeformat;
608 tmp->readformat = p->chan->readformat;
609 tmp->pvt->rawreadformat = p->chan->readformat;
610 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
611 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
612 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
614 tmp->nativeformats = AST_FORMAT_SLINEAR;
615 tmp->writeformat = AST_FORMAT_SLINEAR;
616 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
617 tmp->readformat = AST_FORMAT_SLINEAR;
618 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
621 snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
623 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
625 ast_setstate(tmp, state);
627 tmp->pvt->send_digit = agent_digit;
628 tmp->pvt->call = agent_call;
629 tmp->pvt->hangup = agent_hangup;
630 tmp->pvt->answer = agent_answer;
631 tmp->pvt->read = agent_read;
632 tmp->pvt->write = agent_write;
633 tmp->pvt->exception = agent_read;
634 tmp->pvt->indicate = agent_indicate;
635 tmp->pvt->fixup = agent_fixup;
637 ast_mutex_lock(&usecnt_lock);
639 ast_mutex_unlock(&usecnt_lock);
640 ast_update_use_count();
642 /* Wake up and wait for other applications (by definition the login app)
643 * to release this channel). Takes ownership of the agent channel
644 * to this thread only.
645 * For signalling the other thread, ast_queue_frame is used until we
646 * can safely use signals for this purpose. The pselect() needs to be
647 * implemented in the kernel for this.
649 p->app_sleep_cond = 0;
650 if( ast_mutex_trylock(&p->app_lock) )
653 ast_queue_frame(p->chan, &null_frame, 1);
654 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
655 ast_mutex_lock(&p->app_lock);
656 ast_mutex_lock(&p->lock);
660 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
662 tmp->pvt->pvt = NULL;
663 p->app_sleep_cond = 1;
664 ast_channel_free( tmp );
665 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
666 ast_mutex_unlock(&p->app_lock);
670 p->owning_app = pthread_self();
671 /* After the above step, there should not be any blockers. */
673 if (p->chan->blocking) {
674 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
677 ast_moh_stop(p->chan);
680 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
685 static int read_agent_config(void)
687 struct ast_config *cfg;
688 struct ast_variable *v;
689 struct agent_pvt *p, *pl, *pn;
694 cfg = ast_load(config);
696 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
699 ast_mutex_lock(&agentlock);
705 strcpy(moh, "default");
706 v = ast_variable_browse(cfg, "agents");
708 /* Create the interface list */
709 if (!strcasecmp(v->name, "agent")) {
710 add_agent(v->value, 0);
711 } else if (!strcasecmp(v->name, "group")) {
712 group = ast_get_group(v->value);
713 } else if (!strcasecmp(v->name, "autologoff")) {
714 autologoff = atoi(v->value);
717 } else if (!strcasecmp(v->name, "ackcall")) {
718 if (!strcasecmp(v->value, "always"))
720 else if (ast_true(v->value))
724 } else if (!strcasecmp(v->name, "wrapuptime")) {
725 wrapuptime = atoi(v->value);
728 } else if (!strcasecmp(v->name, "musiconhold")) {
729 strncpy(moh, v->value, sizeof(moh) - 1);
743 /* Destroy if appropriate */
748 /* Cause them to hang up */
749 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
756 ast_mutex_unlock(&agentlock);
761 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
763 struct ast_channel *chan=NULL, *parent=NULL;
766 ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
768 ast_mutex_lock(&agentlock);
771 if (p == newlyavailable) {
775 ast_mutex_lock(&p->lock);
776 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
777 ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
778 /* We found a pending call, time to merge */
779 chan = agent_new(newlyavailable, AST_STATE_DOWN);
782 ast_mutex_unlock(&p->lock);
785 ast_mutex_unlock(&p->lock);
789 ast_mutex_unlock(&agentlock);
790 if (parent && chan) {
791 if (newlyavailable->ackcall > 1) {
792 /* Don't do beep here */
795 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
796 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
797 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
799 res = ast_waitstream(newlyavailable->chan, "");
800 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
804 /* Note -- parent may have disappeared */
805 if (p->abouttograb) {
806 newlyavailable->acknowledged = 1;
807 ast_setstate(parent, AST_STATE_UP);
808 ast_setstate(chan, AST_STATE_UP);
809 strncpy(parent->context, chan->context, sizeof(parent->context) - 1);
810 /* Go ahead and mark the channel as a zombie so that masquerade will
811 destroy it for us, and we need not call ast_hangup */
812 ast_mutex_lock(&parent->lock);
814 ast_channel_masquerade(parent, chan);
815 ast_mutex_unlock(&parent->lock);
818 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
819 agent_cleanup(newlyavailable);
822 ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
823 agent_cleanup(newlyavailable);
829 static int check_beep(struct agent_pvt *newlyavailable, int needlock)
833 ast_log(LOG_DEBUG, "Checking beep 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 would-be winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
845 ast_mutex_unlock(&p->lock);
848 ast_mutex_unlock(&p->lock);
852 ast_mutex_unlock(&agentlock);
854 ast_mutex_unlock(&newlyavailable->lock);
855 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
856 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
857 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
859 res = ast_waitstream(newlyavailable->chan, "");
860 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
862 ast_mutex_lock(&newlyavailable->lock);
867 static struct ast_channel *agent_request(char *type, int format, void *data)
870 struct ast_channel *chan = NULL;
872 unsigned int groupmatch;
876 if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
877 groupmatch = (1 << groupmatch);
878 } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
879 groupmatch = (1 << groupmatch);
885 /* Check actual logged in agents first */
886 ast_mutex_lock(&agentlock);
889 ast_mutex_lock(&p->lock);
890 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
891 !strlen(p->loginchan)) {
894 if (!p->lastdisc.tv_sec) {
895 /* Agent must be registered, but not have any active call, and not be in a waiting state */
896 if (!p->owner && p->chan) {
898 chan = agent_new(p, AST_STATE_DOWN);
901 ast_mutex_unlock(&p->lock);
906 ast_mutex_unlock(&p->lock);
912 ast_mutex_lock(&p->lock);
913 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
914 if (p->chan || strlen(p->loginchan))
916 if (!p->lastdisc.tv_sec) {
917 /* Agent must be registered, but not have any active call, and not be in a waiting state */
918 if (!p->owner && p->chan) {
919 /* Could still get a fixed agent */
920 chan = agent_new(p, AST_STATE_DOWN);
921 } else if (!p->owner && strlen(p->loginchan)) {
922 /* Adjustable agent */
923 p->chan = ast_request("Local", format, p->loginchan);
925 chan = agent_new(p, AST_STATE_DOWN);
928 ast_mutex_unlock(&p->lock);
933 ast_mutex_unlock(&p->lock);
938 if (!chan && waitforagent) {
939 /* No agent available -- but we're requesting to wait for one.
940 Allocate a place holder */
942 ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
943 p = add_agent(data, 1);
944 p->group = groupmatch;
945 chan = agent_new(p, AST_STATE_DOWN);
947 ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
950 ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s);
952 ast_mutex_unlock(&agentlock);
956 static int powerof(unsigned int v)
960 if (v & (1 << x)) return x;
965 static int agents_show(int fd, int argc, char **argv)
974 return RESULT_SHOWUSAGE;
975 ast_mutex_lock(&agentlock);
978 ast_mutex_lock(&p->lock);
981 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
983 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
986 snprintf(username, sizeof(username), "(%s) ", p->name);
988 strcpy(username, "");
990 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
991 if (p->owner && p->owner->bridge) {
992 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
994 strcpy(talkingto, " is idle");
996 } else if (strlen(p->loginchan)) {
997 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
998 strcpy(talkingto, "");
1000 strcat(location, " (Confirmed)");
1002 strcpy(location, "not logged in");
1003 strcpy(talkingto, "");
1006 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
1007 ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
1008 username, location, talkingto, moh);
1010 ast_mutex_unlock(&p->lock);
1013 ast_mutex_unlock(&agentlock);
1014 return RESULT_SUCCESS;
1017 static char show_agents_usage[] =
1018 "Usage: show agents\n"
1019 " Provides summary information on agents.\n";
1021 static struct ast_cli_entry cli_show_agents = {
1022 { "show", "agents", NULL }, agents_show,
1023 "Show status of agents", show_agents_usage, NULL };
1025 STANDARD_LOCAL_USER;
1028 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
1032 struct agent_pvt *p;
1033 struct localuser *u;
1035 char user[AST_MAX_AGENT];
1036 char pass[AST_MAX_AGENT];
1037 char xpass[AST_MAX_AGENT] = "";
1040 char *opt_user = NULL;
1041 char *options = NULL;
1042 char *context = NULL;
1044 int play_announcement;
1045 char *filename = "agent-loginok";
1049 /* Parse the arguments XXX Check for failure XXX */
1050 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
1053 options = strchr(opt_user, '|');
1058 context = strchr(options, '@');
1064 while(*exten && ((*exten < '0') || (*exten > '9'))) exten++;
1071 if (chan->_state != AST_STATE_UP)
1072 res = ast_answer(chan);
1074 if( opt_user && strlen(opt_user))
1075 strncpy( user, opt_user, AST_MAX_AGENT );
1077 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
1079 while (!res && (tries < 3)) {
1080 /* Check for password */
1081 ast_mutex_lock(&agentlock);
1084 if (!strcmp(p->agent, user) && !p->pending)
1085 strncpy(xpass, p->password, sizeof(xpass) - 1);
1088 ast_mutex_unlock(&agentlock);
1091 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
1095 errmsg = "agent-incorrect";
1098 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
1101 /* Check again for accuracy */
1102 ast_mutex_lock(&agentlock);
1105 ast_mutex_lock(&p->lock);
1106 if (!strcmp(p->agent, user) &&
1107 !strcmp(p->password, pass) && !p->pending) {
1110 char tmpchan[256] = "";
1112 /* Retrieve login chan */
1115 strncpy(tmpchan, exten, sizeof(tmpchan) - 1);
1118 res = ast_app_getdata(chan, "agent-newlocation", tmpchan+pos, sizeof(tmpchan) - 2, 0);
1119 if (!strlen(tmpchan) || ast_exists_extension(chan, context && strlen(context) ? context : "default", tmpchan,
1123 ast_log(LOG_WARNING, "Extension '%s' is not valid for automatic login of agent '%s'\n", exten, p->agent);
1127 res = ast_streamfile(chan, "invalid", chan->language);
1129 res = ast_waitstream(chan, AST_DIGIT_ANY);
1141 if (context && strlen(context) && strlen(tmpchan))
1142 snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
1144 strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
1145 if (!strlen(p->loginchan))
1146 filename = "agent-loggedoff";
1147 p->acknowledged = 0;
1150 strcpy(p->loginchan, "");
1151 p->acknowledged = 0;
1153 play_announcement = 1;
1155 if( strchr( options, 's' ) )
1156 play_announcement = 0;
1157 ast_mutex_unlock(&p->lock);
1158 ast_mutex_unlock(&agentlock);
1159 if( !res && play_announcement )
1160 res = ast_streamfile(chan, filename, chan->language);
1162 ast_waitstream(chan, "");
1163 ast_mutex_lock(&agentlock);
1164 ast_mutex_lock(&p->lock);
1166 res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
1168 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
1171 ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
1173 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
1175 /* Check once more just in case */
1178 if (callbackmode && !res) {
1179 /* Just say goodbye and be done with it */
1180 ast_mutex_unlock(&agentlock);
1182 res = ast_safe_sleep(chan, 500);
1183 res = ast_streamfile(chan, "vm-goodbye", chan->language);
1185 res = ast_waitstream(chan, "");
1187 res = ast_safe_sleep(chan, 1000);
1188 ast_mutex_unlock(&p->lock);
1190 #ifdef HONOR_MUSIC_CLASS
1191 /* check if the moh class was changed with setmusiconhold */
1192 if (*(chan->musicclass))
1193 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
1195 ast_moh_start(chan, p->moh);
1196 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
1199 p->agent, chan->name);
1200 if (option_verbose > 2)
1201 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %s/%s)\n", p->agent,
1202 ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
1203 /* Login this channel and wait for it to
1209 check_availability(p, 0);
1210 ast_mutex_unlock(&p->lock);
1211 ast_mutex_unlock(&agentlock);
1213 ast_mutex_lock(&p->lock);
1214 if (p->chan != chan)
1216 ast_mutex_unlock(&p->lock);
1217 /* Yield here so other interested threads can kick in. */
1222 ast_mutex_lock(&agentlock);
1223 ast_mutex_lock(&p->lock);
1224 if (p->lastdisc.tv_sec) {
1225 gettimeofday(&tv, NULL);
1226 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
1227 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
1228 ast_log(LOG_DEBUG, "Wrapup time expired!\n");
1229 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
1233 check_availability(p, 0);
1236 ast_mutex_unlock(&p->lock);
1237 ast_mutex_unlock(&agentlock);
1238 /* Synchronize channel ownership between call to agent and itself. */
1239 ast_mutex_lock( &p->app_lock );
1240 ast_mutex_lock(&p->lock);
1241 p->owning_app = pthread_self();
1242 ast_mutex_unlock(&p->lock);
1244 res = agent_ack_sleep(p);
1246 res = ast_safe_sleep_conditional( chan, 1000,
1247 agent_cont_sleep, p );
1248 ast_mutex_unlock( &p->app_lock );
1249 if ((p->ackcall > 1) && (res == 1)) {
1250 ast_mutex_lock(&agentlock);
1251 ast_mutex_lock(&p->lock);
1252 check_availability(p, 0);
1253 ast_mutex_unlock(&p->lock);
1254 ast_mutex_unlock(&agentlock);
1259 ast_mutex_lock(&p->lock);
1260 if (res && p->owner)
1261 ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
1262 /* Log us off if appropriate */
1263 if (p->chan == chan)
1265 p->acknowledged = 0;
1266 ast_mutex_unlock(&p->lock);
1267 if (option_verbose > 2)
1268 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
1269 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
1272 /* If there is no owner, go ahead and kill it now */
1273 if (p->dead && !p->owner)
1277 ast_mutex_unlock(&p->lock);
1282 ast_mutex_unlock(&p->lock);
1283 errmsg = "agent-alreadyon";
1288 ast_mutex_unlock(&p->lock);
1292 ast_mutex_unlock(&agentlock);
1295 res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1298 LOCAL_USER_REMOVE(u);
1303 static int login_exec(struct ast_channel *chan, void *data)
1305 return __login_exec(chan, data, 0);
1308 static int callback_exec(struct ast_channel *chan, void *data)
1310 return __login_exec(chan, data, 1);
1315 /* Make sure we can register our sip channel type */
1316 if (ast_channel_register(type, tdesc, capability, agent_request)) {
1317 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1320 ast_register_application(app, login_exec, synopsis, descrip);
1321 ast_register_application(app2, callback_exec, synopsis2, descrip2);
1322 ast_cli_register(&cli_show_agents);
1323 /* Read in the config */
1324 read_agent_config();
1330 read_agent_config();
1336 struct agent_pvt *p;
1337 /* First, take us out of the channel loop */
1338 ast_cli_unregister(&cli_show_agents);
1339 ast_unregister_application(app);
1340 ast_unregister_application(app2);
1341 ast_channel_unregister(type);
1342 if (!ast_mutex_lock(&agentlock)) {
1343 /* Hangup all interfaces if they have an owner */
1347 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1351 ast_mutex_unlock(&agentlock);
1353 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1362 ast_mutex_lock(&usecnt_lock);
1364 ast_mutex_unlock(&usecnt_lock);
1370 return ASTERISK_GPL_KEY;