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 #define CLEANUP(ast, p) do { \
134 for (x=0;x<AST_MAX_FDS;x++) \
135 ast->fds[x] = p->chan->fds[x]; \
140 static void agent_unlink(struct agent_pvt *agent)
142 struct agent_pvt *p, *prev;
148 prev->next = agent->next;
150 agents = agent->next;
158 static struct agent_pvt *add_agent(char *agent, int pending)
161 char *password=NULL, *name=NULL;
162 struct agent_pvt *p, *prev;
164 strncpy(tmp, agent, sizeof(tmp));
165 if ((password = strchr(tmp, ','))) {
168 while (*password < 33) password++;
170 if (password && (name = strchr(password, ','))) {
173 while (*name < 33) name++;
178 if (!pending && !strcmp(p->agent, tmp))
184 p = malloc(sizeof(struct agent_pvt));
186 memset(p, 0, sizeof(struct agent_pvt));
187 strncpy(p->agent, tmp, sizeof(p->agent) -1);
188 ast_mutex_init( &p->lock );
189 ast_mutex_init( &p->app_lock );
191 p->app_sleep_cond = 1;
193 p->pending = pending;
204 strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
205 strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
206 strncpy(p->moh, moh, sizeof(p->moh) - 1);
207 p->ackcall = ackcall;
208 p->autologoff = autologoff;
209 p->wrapuptime = wrapuptime;
217 static int agent_cleanup(struct agent_pvt *p)
219 struct ast_channel *chan = p->owner;
221 chan->pvt->pvt = NULL;
222 p->app_sleep_cond = 1;
223 /* Release ownership of the agent to other threads (presumably running the login app). */
224 ast_mutex_unlock(&p->app_lock);
226 ast_channel_free(chan);
232 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
234 static int agent_answer(struct ast_channel *ast)
236 ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
240 static struct ast_frame *agent_read(struct ast_channel *ast)
242 struct agent_pvt *p = ast->pvt->pvt;
243 struct ast_frame *f = NULL;
244 static struct ast_frame null_frame = { AST_FRAME_NULL, };
245 static struct ast_frame answer_frame = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
246 ast_mutex_lock(&p->lock);
247 CHECK_FORMATS(ast, p);
249 p->chan->exception = ast->exception;
250 p->chan->fdno = ast->fdno;
251 f = ast_read(p->chan);
255 /* If there's a channel, hang it up (if it's on a callback) make it NULL */
257 if (strlen(p->loginchan))
263 if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
266 if (option_verbose > 2)
267 ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
268 /* Don't pass answer along */
277 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
278 if (!p->acknowledged) {
279 if (option_verbose > 2)
280 ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
286 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
287 /* * terminates call */
292 ast_mutex_unlock(&p->lock);
296 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
298 struct agent_pvt *p = ast->pvt->pvt;
300 CHECK_FORMATS(ast, p);
301 ast_mutex_lock(&p->lock);
303 if ((f->frametype != AST_FRAME_VOICE) ||
304 (f->subclass == p->chan->writeformat)) {
305 res = ast_write(p->chan, f);
307 ast_log(LOG_DEBUG, "Dropping one incompatible voice frame on '%s' to '%s'\n", ast->name, p->chan->name);
313 ast_mutex_unlock(&p->lock);
317 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
319 struct agent_pvt *p = newchan->pvt->pvt;
320 ast_mutex_lock(&p->lock);
321 if (p->owner != oldchan) {
322 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
323 ast_mutex_unlock(&p->lock);
327 ast_mutex_unlock(&p->lock);
331 static int agent_indicate(struct ast_channel *ast, int condition)
333 struct agent_pvt *p = ast->pvt->pvt;
335 ast_mutex_lock(&p->lock);
337 res = ast_indicate(p->chan, condition);
340 ast_mutex_unlock(&p->lock);
344 static int agent_digit(struct ast_channel *ast, char digit)
346 struct agent_pvt *p = ast->pvt->pvt;
348 ast_mutex_lock(&p->lock);
350 res = p->chan->pvt->send_digit(p->chan, digit);
353 ast_mutex_unlock(&p->lock);
357 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
359 struct agent_pvt *p = ast->pvt->pvt;
361 ast_mutex_lock(&p->lock);
364 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
365 ast_setstate(ast, AST_STATE_DIALING);
368 ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
371 ast_mutex_unlock(&p->lock);
373 } else if (strlen(p->loginchan)) {
375 /* Call on this agent */
376 if (option_verbose > 2)
377 ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
378 if (p->chan->callerid)
379 free(p->chan->callerid);
381 p->chan->callerid = strdup(ast->callerid);
383 p->chan->callerid = NULL;
384 res = ast_call(p->chan, p->loginchan, 0);
386 ast_mutex_unlock(&p->lock);
389 ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
390 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
391 res = ast_streamfile(p->chan, "beep", p->chan->language);
392 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
394 res = ast_waitstream(p->chan, "");
395 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
398 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
399 ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
401 ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
408 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
409 ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
411 ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
415 /* Call is immediately up */
416 ast_setstate(ast, AST_STATE_UP);
419 ast_mutex_unlock(&p->lock);
423 static int agent_hangup(struct ast_channel *ast)
425 struct agent_pvt *p = ast->pvt->pvt;
427 ast_mutex_lock(&p->lock);
429 ast->pvt->pvt = NULL;
430 p->app_sleep_cond = 1;
431 if (p->start && (ast->_state != AST_STATE_UP))
432 howlong = time(NULL) - p->start;
435 /* If they're dead, go ahead and hang up on the agent now */
436 if (strlen(p->loginchan)) {
439 /* Recognize the hangup and pass it along immediately */
443 ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
444 if (howlong && p->autologoff && (howlong > p->autologoff)) {
445 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
446 strcpy(p->loginchan, "");
448 } else if (p->dead) {
449 ast_mutex_lock(&p->chan->lock);
450 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
451 ast_mutex_unlock(&p->chan->lock);
453 ast_mutex_lock(&p->chan->lock);
454 ast_moh_start(p->chan, p->moh);
455 ast_mutex_unlock(&p->chan->lock);
459 ast_mutex_unlock(&p->lock);
460 /* Release ownership of the agent to other threads (presumably running the login app). */
461 ast_mutex_unlock(&p->app_lock);
462 } else if (p->dead) {
463 /* Go ahead and lose it */
464 ast_mutex_unlock(&p->lock);
465 /* Release ownership of the agent to other threads (presumably running the login app). */
466 ast_mutex_unlock(&p->app_lock);
468 ast_mutex_unlock(&p->lock);
469 /* Release ownership of the agent to other threads (presumably running the login app). */
470 ast_mutex_unlock(&p->app_lock);
473 ast_mutex_unlock(&p->lock);
474 /* Release ownership of the agent to other threads (presumably running the login app). */
475 ast_mutex_unlock(&p->app_lock);
478 ast_mutex_lock(&agentlock);
480 ast_mutex_unlock(&agentlock);
482 if (p->abouttograb) {
483 /* Let the "about to grab" thread know this isn't valid anymore, and let it
486 } else if (p->dead) {
488 } else if (p->chan) {
489 /* Not dead -- check availability now */
490 ast_mutex_lock(&p->lock);
491 /* check_availability(p, 1); */
492 /* Store last disconnect time */
493 gettimeofday(&p->lastdisc, NULL);
494 ast_mutex_unlock(&p->lock);
499 static int agent_cont_sleep( void *data )
505 p = (struct agent_pvt *)data;
507 ast_mutex_lock(&p->lock);
508 res = p->app_sleep_cond;
509 if (p->lastdisc.tv_sec) {
510 gettimeofday(&tv, NULL);
511 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
512 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
515 ast_mutex_unlock(&p->lock);
518 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
523 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
525 struct ast_channel *tmp;
526 struct ast_frame null_frame = { AST_FRAME_NULL };
529 ast_log(LOG_WARNING, "No channel? :(\n");
533 tmp = ast_channel_alloc(0);
536 tmp->nativeformats = p->chan->nativeformats;
537 tmp->writeformat = p->chan->writeformat;
538 tmp->pvt->rawwriteformat = p->chan->writeformat;
539 tmp->readformat = p->chan->readformat;
540 tmp->pvt->rawreadformat = p->chan->readformat;
541 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
542 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
543 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
545 tmp->nativeformats = AST_FORMAT_SLINEAR;
546 tmp->writeformat = AST_FORMAT_SLINEAR;
547 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
548 tmp->readformat = AST_FORMAT_SLINEAR;
549 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
552 snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
554 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
556 ast_setstate(tmp, state);
558 tmp->pvt->send_digit = agent_digit;
559 tmp->pvt->call = agent_call;
560 tmp->pvt->hangup = agent_hangup;
561 tmp->pvt->answer = agent_answer;
562 tmp->pvt->read = agent_read;
563 tmp->pvt->write = agent_write;
564 tmp->pvt->exception = agent_read;
565 tmp->pvt->indicate = agent_indicate;
566 tmp->pvt->fixup = agent_fixup;
568 ast_mutex_lock(&usecnt_lock);
570 ast_mutex_unlock(&usecnt_lock);
571 ast_update_use_count();
573 /* Wake up and wait for other applications (by definition the login app)
574 * to release this channel). Takes ownership of the agent channel
575 * to this thread only.
576 * For signalling the other thread, ast_queue_frame is used until we
577 * can safely use signals for this purpose. The pselect() needs to be
578 * implemented in the kernel for this.
580 p->app_sleep_cond = 0;
581 if( ast_mutex_trylock(&p->app_lock) )
584 ast_queue_frame(p->chan, &null_frame, 1);
585 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
586 ast_mutex_lock(&p->app_lock);
587 ast_mutex_lock(&p->lock);
591 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
593 tmp->pvt->pvt = NULL;
594 p->app_sleep_cond = 1;
595 ast_channel_free( tmp );
596 ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
597 ast_mutex_unlock(&p->app_lock);
601 p->owning_app = pthread_self();
602 /* After the above step, there should not be any blockers. */
604 if (p->chan->blocking) {
605 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
608 ast_moh_stop(p->chan);
611 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
616 static int read_agent_config(void)
618 struct ast_config *cfg;
619 struct ast_variable *v;
620 struct agent_pvt *p, *pl, *pn;
625 cfg = ast_load(config);
627 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
630 ast_mutex_lock(&agentlock);
636 strcpy(moh, "default");
637 v = ast_variable_browse(cfg, "agents");
639 /* Create the interface list */
640 if (!strcasecmp(v->name, "agent")) {
641 add_agent(v->value, 0);
642 } else if (!strcasecmp(v->name, "group")) {
643 group = ast_get_group(v->value);
644 } else if (!strcasecmp(v->name, "autologoff")) {
645 autologoff = atoi(v->value);
648 } else if (!strcasecmp(v->name, "ackcall")) {
649 ackcall = ast_true(v->value);
650 } else if (!strcasecmp(v->name, "wrapuptime")) {
651 wrapuptime = atoi(v->value);
654 } else if (!strcasecmp(v->name, "musiconhold")) {
655 strncpy(moh, v->value, sizeof(moh) - 1);
669 /* Destroy if appropriate */
674 /* Cause them to hang up */
675 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
682 ast_mutex_unlock(&agentlock);
687 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
689 struct ast_channel *chan=NULL, *parent=NULL;
692 ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
694 ast_mutex_lock(&agentlock);
697 if (p == newlyavailable) {
701 ast_mutex_lock(&p->lock);
702 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
703 ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
704 /* We found a pending call, time to merge */
705 chan = agent_new(newlyavailable, AST_STATE_DOWN);
708 ast_mutex_unlock(&p->lock);
711 ast_mutex_unlock(&p->lock);
715 ast_mutex_unlock(&agentlock);
716 if (parent && chan) {
717 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
718 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
719 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
721 res = ast_waitstream(newlyavailable->chan, "");
722 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
725 /* Note -- parent may have disappeared */
726 if (p->abouttograb) {
727 ast_setstate(parent, AST_STATE_UP);
728 ast_setstate(chan, AST_STATE_UP);
729 /* Go ahead and mark the channel as a zombie so that masquerade will
730 destroy it for us, and we need not call ast_hangup */
731 ast_mutex_lock(&parent->lock);
733 ast_channel_masquerade(parent, chan);
734 ast_mutex_unlock(&parent->lock);
737 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
738 agent_cleanup(newlyavailable);
741 ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
742 agent_cleanup(newlyavailable);
748 static struct ast_channel *agent_request(char *type, int format, void *data)
751 struct ast_channel *chan = NULL;
753 unsigned int groupmatch;
757 if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
758 groupmatch = (1 << groupmatch);
759 } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
760 groupmatch = (1 << groupmatch);
766 /* Check actual logged in agents first */
767 ast_mutex_lock(&agentlock);
770 ast_mutex_lock(&p->lock);
771 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
772 !strlen(p->loginchan)) {
775 if (!p->lastdisc.tv_sec) {
776 /* Agent must be registered, but not have any active call, and not be in a waiting state */
777 if (!p->owner && p->chan) {
779 chan = agent_new(p, AST_STATE_DOWN);
782 ast_mutex_unlock(&p->lock);
787 ast_mutex_unlock(&p->lock);
793 ast_mutex_lock(&p->lock);
794 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
795 if (p->chan || strlen(p->loginchan))
797 if (!p->lastdisc.tv_sec) {
798 /* Agent must be registered, but not have any active call, and not be in a waiting state */
799 if (!p->owner && p->chan) {
800 /* Could still get a fixed agent */
801 chan = agent_new(p, AST_STATE_DOWN);
802 } else if (!p->owner && strlen(p->loginchan)) {
803 /* Adjustable agent */
804 p->chan = ast_request("Local", format, p->loginchan);
806 chan = agent_new(p, AST_STATE_DOWN);
809 ast_mutex_unlock(&p->lock);
814 ast_mutex_unlock(&p->lock);
819 if (!chan && waitforagent) {
820 /* No agent available -- but we're requesting to wait for one.
821 Allocate a place holder */
823 ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
824 p = add_agent(data, 1);
825 p->group = groupmatch;
826 chan = agent_new(p, AST_STATE_DOWN);
828 ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
831 ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n");
833 ast_mutex_unlock(&agentlock);
837 static int powerof(unsigned int v)
841 if (v & (1 << x)) return x;
846 static int agents_show(int fd, int argc, char **argv)
855 return RESULT_SHOWUSAGE;
856 ast_mutex_lock(&agentlock);
859 ast_mutex_lock(&p->lock);
862 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
864 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
867 snprintf(username, sizeof(username), "(%s) ", p->name);
869 strcpy(username, "");
871 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
872 if (p->owner && p->owner->bridge) {
873 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
875 strcpy(talkingto, " is idle");
877 } else if (strlen(p->loginchan)) {
878 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
879 strcpy(talkingto, "");
881 strcat(location, " (Confirmed)");
883 strcpy(location, "not logged in");
884 strcpy(talkingto, "");
887 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
888 ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
889 username, location, talkingto, moh);
891 ast_mutex_unlock(&p->lock);
894 ast_mutex_unlock(&agentlock);
895 return RESULT_SUCCESS;
898 static char show_agents_usage[] =
899 "Usage: show agents\n"
900 " Provides summary information on agents.\n";
902 static struct ast_cli_entry cli_show_agents = {
903 { "show", "agents", NULL }, agents_show,
904 "Show status of agents", show_agents_usage, NULL };
909 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
916 char user[AST_MAX_AGENT];
917 char pass[AST_MAX_AGENT];
918 char xpass[AST_MAX_AGENT] = "";
921 char *opt_user = NULL;
922 char *options = NULL;
923 char *context = NULL;
925 int play_announcement;
926 char *filename = "agent-loginok";
930 /* Parse the arguments XXX Check for failure XXX */
931 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
934 options = strchr(opt_user, '|');
939 context = strchr(options, '@');
945 while(*exten && ((*exten < '0') || (*exten > '9'))) exten++;
952 if (chan->_state != AST_STATE_UP)
953 res = ast_answer(chan);
955 if( opt_user && strlen(opt_user))
956 strncpy( user, opt_user, AST_MAX_AGENT );
958 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
960 while (!res && (tries < 3)) {
961 /* Check for password */
962 ast_mutex_lock(&agentlock);
965 if (!strcmp(p->agent, user) && !p->pending)
966 strncpy(xpass, p->password, sizeof(xpass) - 1);
969 ast_mutex_unlock(&agentlock);
972 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
976 errmsg = "agent-incorrect";
979 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
982 /* Check again for accuracy */
983 ast_mutex_lock(&agentlock);
986 ast_mutex_lock(&p->lock);
987 if (!strcmp(p->agent, user) &&
988 !strcmp(p->password, pass) && !p->pending) {
991 char tmpchan[256] = "";
993 /* Retrieve login chan */
996 strncpy(tmpchan, exten, sizeof(tmpchan) - 1);
999 res = ast_app_getdata(chan, "agent-newlocation", tmpchan+pos, sizeof(tmpchan) - 2, 0);
1000 if (!strlen(tmpchan) || ast_exists_extension(chan, context && strlen(context) ? context : "default", tmpchan,
1004 ast_log(LOG_WARNING, "Extension '%s' is not valid for automatic login of agent '%s'\n", exten, p->agent);
1008 res = ast_streamfile(chan, "invalid", chan->language);
1010 res = ast_waitstream(chan, AST_DIGIT_ANY);
1022 if (context && strlen(context) && strlen(tmpchan))
1023 snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
1025 strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
1026 if (!strlen(p->loginchan))
1027 filename = "agent-loggedoff";
1028 p->acknowledged = 0;
1031 strcpy(p->loginchan, "");
1032 p->acknowledged = 0;
1034 play_announcement = 1;
1036 if( strchr( options, 's' ) )
1037 play_announcement = 0;
1038 if( !res && play_announcement )
1039 res = ast_streamfile(chan, filename, chan->language);
1041 ast_waitstream(chan, "");
1043 res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
1045 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
1048 ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
1050 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
1052 /* Check once more just in case */
1055 if (callbackmode && !res) {
1056 /* Just say goodbye and be done with it */
1058 res = ast_safe_sleep(chan, 500);
1059 res = ast_streamfile(chan, "vm-goodbye", chan->language);
1061 res = ast_waitstream(chan, "");
1063 res = ast_safe_sleep(chan, 1000);
1064 ast_mutex_unlock(&p->lock);
1065 ast_mutex_unlock(&agentlock);
1067 #ifdef HONOR_MUSIC_CLASS
1068 /* check if the moh class was changed with setmusiconhold */
1069 if (*(chan->musicclass))
1070 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
1072 ast_moh_start(chan, p->moh);
1073 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
1076 p->agent, chan->name);
1077 if (option_verbose > 2)
1078 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %s/%s)\n", p->agent,
1079 ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
1080 /* Login this channel and wait for it to
1083 p->acknowledged = 1;
1084 check_availability(p, 0);
1085 ast_mutex_unlock(&p->lock);
1086 ast_mutex_unlock(&agentlock);
1088 ast_mutex_lock(&p->lock);
1089 if (p->chan != chan)
1091 ast_mutex_unlock(&p->lock);
1092 /* Yield here so other interested threads can kick in. */
1097 ast_mutex_lock(&p->lock);
1098 if (p->lastdisc.tv_sec) {
1099 gettimeofday(&tv, NULL);
1100 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
1101 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
1102 ast_log(LOG_DEBUG, "Wrapup time expired!\n");
1103 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
1104 check_availability(p, 1);
1107 ast_mutex_unlock(&p->lock);
1108 /* Synchronize channel ownership between call to agent and itself. */
1109 ast_mutex_lock( &p->app_lock );
1110 ast_mutex_lock(&p->lock);
1111 p->owning_app = pthread_self();
1112 ast_mutex_unlock(&p->lock);
1113 res = ast_safe_sleep_conditional( chan, 1000,
1114 agent_cont_sleep, p );
1115 ast_mutex_unlock( &p->app_lock );
1118 ast_mutex_lock(&p->lock);
1119 if (res && p->owner)
1120 ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
1121 /* Log us off if appropriate */
1122 if (p->chan == chan)
1124 p->acknowledged = 0;
1125 ast_mutex_unlock(&p->lock);
1126 if (option_verbose > 2)
1127 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
1128 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
1131 /* If there is no owner, go ahead and kill it now */
1132 if (p->dead && !p->owner)
1136 ast_mutex_unlock(&p->lock);
1141 ast_mutex_unlock(&p->lock);
1142 errmsg = "agent-alreadyon";
1147 ast_mutex_unlock(&p->lock);
1151 ast_mutex_unlock(&agentlock);
1154 res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1157 LOCAL_USER_REMOVE(u);
1162 static int login_exec(struct ast_channel *chan, void *data)
1164 return __login_exec(chan, data, 0);
1167 static int callback_exec(struct ast_channel *chan, void *data)
1169 return __login_exec(chan, data, 1);
1174 /* Make sure we can register our sip channel type */
1175 if (ast_channel_register(type, tdesc, capability, agent_request)) {
1176 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1179 ast_register_application(app, login_exec, synopsis, descrip);
1180 ast_register_application(app2, callback_exec, synopsis2, descrip2);
1181 ast_cli_register(&cli_show_agents);
1182 /* Read in the config */
1183 read_agent_config();
1189 read_agent_config();
1195 struct agent_pvt *p;
1196 /* First, take us out of the channel loop */
1197 ast_cli_unregister(&cli_show_agents);
1198 ast_unregister_application(app);
1199 ast_unregister_application(app2);
1200 ast_channel_unregister(type);
1201 if (!ast_mutex_lock(&agentlock)) {
1202 /* Hangup all interfaces if they have an owner */
1206 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1210 ast_mutex_unlock(&agentlock);
1212 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1221 ast_mutex_lock(&usecnt_lock);
1223 ast_mutex_unlock(&usecnt_lock);
1229 return ASTERISK_GPL_KEY;