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][|@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;
83 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
85 /* Protect the interface list (of sip_pvt's) */
86 static pthread_mutex_t agentlock = AST_MUTEX_INITIALIZER;
88 static struct agent_pvt {
89 pthread_mutex_t lock; /* Channel private lock */
90 int dead; /* Poised for destruction? */
91 int pending; /* Not a real agent -- just pending a match */
92 int abouttograb; /* About to grab */
93 int autologoff; /* Auto timeout time */
94 time_t start; /* When call started */
95 struct timeval lastdisc; /* When last disconnected */
96 int wrapuptime; /* Wrapup time in ms */
97 unsigned int group; /* Group memberships */
98 int acknowledged; /* Acknowledged */
99 char moh[80]; /* Which music on hold */
100 char agent[AST_MAX_AGENT]; /* Agent ID */
101 char password[AST_MAX_AGENT]; /* Password for Agent login */
102 char name[AST_MAX_AGENT];
103 pthread_mutex_t app_lock; /* Synchronization between owning applications */
104 volatile pthread_t owning_app; /* Owning application thread id */
105 volatile int app_sleep_cond; /* Sleep condition for the login app */
106 struct ast_channel *owner; /* Agent */
108 struct ast_channel *chan; /* Channel we use */
109 struct agent_pvt *next; /* Agent */
112 #define CLEANUP(ast, p) do { \
115 for (x=0;x<AST_MAX_FDS;x++) \
116 ast->fds[x] = p->chan->fds[x]; \
121 static void agent_unlink(struct agent_pvt *agent)
123 struct agent_pvt *p, *prev;
129 prev->next = agent->next;
131 agents = agent->next;
139 static struct agent_pvt *add_agent(char *agent, int pending)
142 char *password=NULL, *name=NULL;
143 struct agent_pvt *p, *prev;
145 strncpy(tmp, agent, sizeof(tmp));
146 if ((password = strchr(tmp, ','))) {
149 while (*password < 33) password++;
151 if (password && (name = strchr(password, ','))) {
154 while (*name < 33) name++;
159 if (!pending && !strcmp(p->agent, tmp))
165 p = malloc(sizeof(struct agent_pvt));
167 memset(p, 0, sizeof(struct agent_pvt));
168 strncpy(p->agent, tmp, sizeof(p->agent) -1);
169 ast_pthread_mutex_init( &p->lock );
170 ast_pthread_mutex_init( &p->app_lock );
172 p->app_sleep_cond = 1;
174 p->pending = pending;
185 strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
186 strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
187 strncpy(p->moh, moh, sizeof(p->moh) - 1);
188 p->autologoff = autologoff;
189 p->wrapuptime = wrapuptime;
197 static int agent_cleanup(struct agent_pvt *p)
199 struct ast_channel *chan = p->owner;
201 chan->pvt->pvt = NULL;
202 p->app_sleep_cond = 1;
203 /* Release ownership of the agent to other threads (presumably running the login app). */
204 ast_pthread_mutex_unlock(&p->app_lock);
206 ast_channel_free(chan);
212 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
214 static int agent_answer(struct ast_channel *ast)
216 ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
220 static struct ast_frame *agent_read(struct ast_channel *ast)
222 struct agent_pvt *p = ast->pvt->pvt;
223 struct ast_frame *f = NULL;
224 static struct ast_frame null_frame = { AST_FRAME_NULL, };
225 static struct ast_frame answer_frame = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
226 ast_pthread_mutex_lock(&p->lock);
228 f = ast_read(p->chan);
232 /* If there's a channel, hang it up (if it's on a callback) make it NULL */
234 if (strlen(p->loginchan))
239 if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
240 if (option_verbose > 2)
241 ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
242 /* Don't pass answer along */
246 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
247 if (!p->acknowledged) {
248 if (option_verbose > 2)
249 ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
255 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
256 /* * terminates call */
261 ast_pthread_mutex_unlock(&p->lock);
265 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
267 struct agent_pvt *p = ast->pvt->pvt;
269 ast_pthread_mutex_lock(&p->lock);
271 res = ast_write(p->chan, f);
275 ast_pthread_mutex_unlock(&p->lock);
279 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
281 struct agent_pvt *p = newchan->pvt->pvt;
282 ast_pthread_mutex_lock(&p->lock);
283 if (p->owner != oldchan) {
284 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
285 ast_pthread_mutex_unlock(&p->lock);
289 ast_pthread_mutex_unlock(&p->lock);
293 static int agent_indicate(struct ast_channel *ast, int condition)
295 struct agent_pvt *p = ast->pvt->pvt;
297 ast_pthread_mutex_lock(&p->lock);
299 res = ast_indicate(p->chan, condition);
302 ast_pthread_mutex_unlock(&p->lock);
306 static int agent_digit(struct ast_channel *ast, char digit)
308 struct agent_pvt *p = ast->pvt->pvt;
310 ast_pthread_mutex_lock(&p->lock);
312 res = p->chan->pvt->send_digit(p->chan, digit);
315 ast_pthread_mutex_unlock(&p->lock);
319 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
321 struct agent_pvt *p = ast->pvt->pvt;
323 ast_pthread_mutex_lock(&p->lock);
326 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
327 ast_setstate(ast, AST_STATE_DIALING);
330 ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
333 ast_pthread_mutex_unlock(&p->lock);
335 } else if (strlen(p->loginchan)) {
337 /* Call on this agent */
338 if (option_verbose > 2)
339 ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
340 res = ast_call(p->chan, p->loginchan, 0);
342 ast_pthread_mutex_unlock(&p->lock);
345 ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
346 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
347 res = ast_streamfile(p->chan, "beep", p->chan->language);
348 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
350 res = ast_waitstream(p->chan, "");
351 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
354 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
355 ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
357 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
364 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
365 ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
367 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
371 /* Call is immediately up */
372 ast_setstate(ast, AST_STATE_UP);
375 ast_pthread_mutex_unlock(&p->lock);
379 static int agent_hangup(struct ast_channel *ast)
381 struct agent_pvt *p = ast->pvt->pvt;
383 ast_pthread_mutex_lock(&p->lock);
385 ast->pvt->pvt = NULL;
386 p->app_sleep_cond = 1;
387 if (p->start && (ast->_state != AST_STATE_UP))
388 howlong = time(NULL) - p->start;
391 /* If they're dead, go ahead and hang up on the agent now */
392 if (strlen(p->loginchan)) {
394 /* Recognize the hangup and pass it along immediately */
398 ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
399 if (howlong && p->autologoff && (howlong > p->autologoff)) {
400 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
401 strcpy(p->loginchan, "");
403 } else if (p->dead) {
404 ast_pthread_mutex_lock(&p->chan->lock);
405 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
406 ast_pthread_mutex_unlock(&p->chan->lock);
408 ast_pthread_mutex_lock(&p->chan->lock);
409 ast_moh_start(p->chan, p->moh);
410 ast_pthread_mutex_unlock(&p->chan->lock);
414 ast_pthread_mutex_unlock(&p->lock);
415 /* Release ownership of the agent to other threads (presumably running the login app). */
416 ast_pthread_mutex_unlock(&p->app_lock);
417 } else if (p->dead) {
418 /* Go ahead and lose it */
419 ast_pthread_mutex_unlock(&p->lock);
420 /* Release ownership of the agent to other threads (presumably running the login app). */
421 ast_pthread_mutex_unlock(&p->app_lock);
423 ast_pthread_mutex_unlock(&p->lock);
424 /* Release ownership of the agent to other threads (presumably running the login app). */
425 ast_pthread_mutex_unlock(&p->app_lock);
428 ast_pthread_mutex_unlock(&p->lock);
429 /* Release ownership of the agent to other threads (presumably running the login app). */
430 ast_pthread_mutex_unlock(&p->app_lock);
433 ast_pthread_mutex_lock(&agentlock);
435 ast_pthread_mutex_unlock(&agentlock);
437 if (p->abouttograb) {
438 /* Let the "about to grab" thread know this isn't valid anymore, and let it
441 } else if (p->dead) {
443 } else if (p->chan) {
444 /* Not dead -- check availability now */
445 ast_pthread_mutex_lock(&p->lock);
446 /* check_availability(p, 1); */
447 /* Store last disconnect time */
448 gettimeofday(&p->lastdisc, NULL);
449 ast_pthread_mutex_unlock(&p->lock);
454 static int agent_cont_sleep( void *data )
460 p = (struct agent_pvt *)data;
462 ast_pthread_mutex_lock(&p->lock);
463 res = p->app_sleep_cond;
464 if (p->lastdisc.tv_sec) {
465 gettimeofday(&tv, NULL);
466 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
467 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
470 ast_pthread_mutex_unlock(&p->lock);
473 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
478 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
480 struct ast_channel *tmp;
481 struct ast_frame null_frame = { AST_FRAME_NULL };
484 ast_log(LOG_WARNING, "No channel? :(\n");
488 tmp = ast_channel_alloc(0);
491 tmp->nativeformats = p->chan->nativeformats;
492 tmp->writeformat = p->chan->writeformat;
493 tmp->pvt->rawwriteformat = p->chan->writeformat;
494 tmp->readformat = p->chan->readformat;
495 tmp->pvt->rawreadformat = p->chan->readformat;
496 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
497 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
498 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
500 tmp->nativeformats = AST_FORMAT_SLINEAR;
501 tmp->writeformat = AST_FORMAT_SLINEAR;
502 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
503 tmp->readformat = AST_FORMAT_SLINEAR;
504 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
507 snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
509 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
511 ast_setstate(tmp, state);
513 tmp->pvt->send_digit = agent_digit;
514 tmp->pvt->call = agent_call;
515 tmp->pvt->hangup = agent_hangup;
516 tmp->pvt->answer = agent_answer;
517 tmp->pvt->read = agent_read;
518 tmp->pvt->write = agent_write;
519 tmp->pvt->exception = agent_read;
520 tmp->pvt->indicate = agent_indicate;
521 tmp->pvt->fixup = agent_fixup;
523 ast_pthread_mutex_lock(&usecnt_lock);
525 ast_pthread_mutex_unlock(&usecnt_lock);
526 ast_update_use_count();
528 /* Wake up and wait for other applications (by definition the login app)
529 * to release this channel). Takes ownership of the agent channel
530 * to this thread only.
531 * For signalling the other thread, ast_queue_frame is used until we
532 * can safely use signals for this purpose. The pselect() needs to be
533 * implemented in the kernel for this.
535 p->app_sleep_cond = 0;
536 if( pthread_mutex_trylock(&p->app_lock) )
539 ast_queue_frame(p->chan, &null_frame, 1);
540 ast_pthread_mutex_unlock(&p->lock); /* For other thread to read the condition. */
541 ast_pthread_mutex_lock(&p->app_lock);
542 ast_pthread_mutex_lock(&p->lock);
546 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
548 tmp->pvt->pvt = NULL;
549 p->app_sleep_cond = 1;
550 ast_channel_free( tmp );
554 p->owning_app = pthread_self();
555 /* After the above step, there should not be any blockers. */
557 if (p->chan->blocking) {
558 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
561 ast_moh_stop(p->chan);
564 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
569 static int read_agent_config(void)
571 struct ast_config *cfg;
572 struct ast_variable *v;
573 struct agent_pvt *p, *pl, *pn;
577 cfg = ast_load(config);
579 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
582 ast_pthread_mutex_lock(&agentlock);
588 strcpy(moh, "default");
589 v = ast_variable_browse(cfg, "agents");
591 /* Create the interface list */
592 if (!strcasecmp(v->name, "agent")) {
593 add_agent(v->value, 0);
594 } else if (!strcasecmp(v->name, "group")) {
595 group = ast_get_group(v->value);
596 } else if (!strcasecmp(v->name, "autologoff")) {
597 autologoff = atoi(v->value);
600 } else if (!strcasecmp(v->name, "wrapuptime")) {
601 wrapuptime = atoi(v->value);
604 } else if (!strcasecmp(v->name, "musiconhold")) {
605 strncpy(moh, v->value, sizeof(moh) - 1);
619 /* Destroy if appropriate */
624 /* Cause them to hang up */
625 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
632 ast_pthread_mutex_unlock(&agentlock);
637 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
639 struct ast_channel *chan=NULL, *parent=NULL;
642 ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
644 ast_pthread_mutex_lock(&agentlock);
647 if (p == newlyavailable) {
651 ast_pthread_mutex_lock(&p->lock);
652 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
653 ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
654 /* We found a pending call, time to merge */
655 chan = agent_new(newlyavailable, AST_STATE_DOWN);
658 ast_pthread_mutex_unlock(&p->lock);
661 ast_pthread_mutex_unlock(&p->lock);
665 ast_pthread_mutex_unlock(&agentlock);
666 if (parent && chan) {
667 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
668 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
669 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
671 res = ast_waitstream(newlyavailable->chan, "");
672 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
675 /* Note -- parent may have disappeared */
676 if (p->abouttograb) {
677 ast_setstate(parent, AST_STATE_UP);
678 ast_setstate(chan, AST_STATE_UP);
679 /* Go ahead and mark the channel as a zombie so that masquerade will
680 destroy it for us, and we need not call ast_hangup */
681 ast_pthread_mutex_lock(&parent->lock);
683 ast_channel_masquerade(parent, chan);
684 ast_pthread_mutex_unlock(&parent->lock);
687 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
688 agent_cleanup(newlyavailable);
691 ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
692 agent_cleanup(newlyavailable);
698 static struct ast_channel *agent_request(char *type, int format, void *data)
701 struct ast_channel *chan = NULL;
703 unsigned int groupmatch;
706 if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
707 groupmatch = (1 << groupmatch);
708 } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
709 groupmatch = (1 << groupmatch);
714 ast_pthread_mutex_lock(&agentlock);
717 ast_pthread_mutex_lock(&p->lock);
718 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
719 !p->lastdisc.tv_sec) {
720 /* Agent must be registered, but not have any active call, and not be in a waiting state */
721 if (!p->owner && p->chan) {
723 chan = agent_new(p, AST_STATE_DOWN);
724 } else if (!p->owner && strlen(p->loginchan)) {
725 /* Adjustable agent */
726 p->chan = ast_request("Local", format, p->loginchan);
728 chan = agent_new(p, AST_STATE_DOWN);
731 ast_pthread_mutex_unlock(&p->lock);
735 ast_pthread_mutex_unlock(&p->lock);
738 if (!chan && waitforagent) {
739 /* No agent available -- but we're requesting to wait for one.
740 Allocate a place holder */
741 ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
742 p = add_agent(data, 1);
743 p->group = groupmatch;
744 chan = agent_new(p, AST_STATE_DOWN);
746 ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
749 ast_pthread_mutex_unlock(&agentlock);
753 static int powerof(unsigned int v)
757 if (v & (1 << x)) return x;
762 static int agents_show(int fd, int argc, char **argv)
771 return RESULT_SHOWUSAGE;
772 ast_pthread_mutex_lock(&agentlock);
775 ast_pthread_mutex_lock(&p->lock);
778 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
780 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
783 snprintf(username, sizeof(username), "(%s) ", p->name);
785 strcpy(username, "");
787 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
788 if (p->owner && p->owner->bridge) {
789 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
791 strcpy(talkingto, " is idle");
793 } else if (strlen(p->loginchan)) {
794 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
795 strcpy(talkingto, "");
797 strcat(location, " (Confirmed)");
799 strcpy(location, "not logged in");
800 strcpy(talkingto, "");
803 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
804 ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
805 username, location, talkingto, moh);
807 ast_pthread_mutex_unlock(&p->lock);
810 ast_pthread_mutex_unlock(&agentlock);
811 return RESULT_SUCCESS;
814 static char show_agents_usage[] =
815 "Usage: show agents\n"
816 " Provides summary information on agents.\n";
818 static struct ast_cli_entry cli_show_agents = {
819 { "show", "agents", NULL }, agents_show,
820 "Show status of agents", show_agents_usage, NULL };
825 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
832 char user[AST_MAX_AGENT];
833 char pass[AST_MAX_AGENT];
834 char xpass[AST_MAX_AGENT] = "";
837 char *opt_user = NULL;
838 char *options = NULL;
839 char *context = NULL;
840 int play_announcement;
841 char *filename = "agent-loginok";
845 /* Parse the arguments XXX Check for failure XXX */
846 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
849 options = strchr(opt_user, '|');
854 context = strchr(options, '@');
863 if (chan->_state != AST_STATE_UP)
864 res = ast_answer(chan);
866 if( opt_user && strlen(opt_user))
867 strncpy( user, opt_user, AST_MAX_AGENT );
869 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
871 while (!res && (tries < 3)) {
872 /* Check for password */
873 ast_pthread_mutex_lock(&agentlock);
876 if (!strcmp(p->agent, user) && !p->pending)
877 strncpy(xpass, p->password, sizeof(xpass) - 1);
880 ast_pthread_mutex_unlock(&agentlock);
883 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
887 errmsg = "agent-incorrect";
890 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
893 /* Check again for accuracy */
894 ast_pthread_mutex_lock(&agentlock);
897 ast_pthread_mutex_lock(&p->lock);
898 if (!strcmp(p->agent, user) &&
899 !strcmp(p->password, pass) && !p->pending) {
902 char tmpchan[256] = "";
903 /* Retrieve login chan */
904 res = ast_app_getdata(chan, "agent-newlocation", tmpchan, sizeof(tmpchan) - 1, 0);
906 if (context && strlen(context))
907 snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
909 strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
910 if (!strlen(p->loginchan))
911 filename = "agent-loggedoff";
915 strcpy(p->loginchan, "");
918 play_announcement = 1;
920 if( strchr( options, 's' ) )
921 play_announcement = 0;
922 if( !res && play_announcement )
923 res = ast_streamfile(chan, filename, chan->language);
925 ast_waitstream(chan, "");
927 res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
929 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
932 ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
934 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
936 /* Check once more just in case */
939 if (callbackmode && !res) {
940 /* Just say goodbye and be done with it */
942 res = ast_safe_sleep(chan, 500);
943 res = ast_streamfile(chan, "vm-goodbye", chan->language);
945 res = ast_waitstream(chan, "");
947 res = ast_safe_sleep(chan, 1000);
948 ast_pthread_mutex_unlock(&p->lock);
949 ast_pthread_mutex_unlock(&agentlock);
951 /* check if the moh class was changed with setmusiconhold */
952 if (*(chan->musicclass))
953 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
954 ast_moh_start(chan, p->moh);
955 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
958 p->agent, chan->name);
959 if (option_verbose > 2)
960 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %d/%d)\n", p->agent,
961 chan->readformat, chan->writeformat);
962 /* Login this channel and wait for it to
966 check_availability(p, 0);
967 ast_pthread_mutex_unlock(&p->lock);
968 ast_pthread_mutex_unlock(&agentlock);
970 ast_pthread_mutex_lock(&p->lock);
973 ast_pthread_mutex_unlock(&p->lock);
974 /* Yield here so other interested threads can kick in. */
979 ast_pthread_mutex_lock(&p->lock);
980 if (p->lastdisc.tv_sec) {
981 gettimeofday(&tv, NULL);
982 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
983 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
984 ast_log(LOG_DEBUG, "Wrapup time expired!\n");
985 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
986 check_availability(p, 1);
989 ast_pthread_mutex_unlock(&p->lock);
990 /* Synchronize channel ownership between call to agent and itself. */
991 pthread_mutex_lock( &p->app_lock );
992 ast_pthread_mutex_lock(&p->lock);
993 p->owning_app = pthread_self();
994 ast_pthread_mutex_unlock(&p->lock);
995 res = ast_safe_sleep_conditional( chan, 1000,
996 agent_cont_sleep, p );
997 pthread_mutex_unlock( &p->app_lock );
1000 ast_pthread_mutex_lock(&p->lock);
1001 if (res && p->owner)
1002 ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
1003 /* Log us off if appropriate */
1004 if (p->chan == chan)
1006 p->acknowledged = 0;
1007 ast_pthread_mutex_unlock(&p->lock);
1008 if (option_verbose > 2)
1009 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
1010 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
1013 /* If there is no owner, go ahead and kill it now */
1014 if (p->dead && !p->owner)
1018 ast_pthread_mutex_unlock(&p->lock);
1023 ast_pthread_mutex_unlock(&p->lock);
1024 errmsg = "agent-alreadyon";
1029 ast_pthread_mutex_unlock(&p->lock);
1033 ast_pthread_mutex_unlock(&agentlock);
1036 res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1039 LOCAL_USER_REMOVE(u);
1044 static int login_exec(struct ast_channel *chan, void *data)
1046 return __login_exec(chan, data, 0);
1049 static int callback_exec(struct ast_channel *chan, void *data)
1051 return __login_exec(chan, data, 1);
1056 /* Make sure we can register our sip channel type */
1057 if (ast_channel_register(type, tdesc, capability, agent_request)) {
1058 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1061 ast_register_application(app, login_exec, synopsis, descrip);
1062 ast_register_application(app2, callback_exec, synopsis2, descrip2);
1063 ast_cli_register(&cli_show_agents);
1064 /* Read in the config */
1065 read_agent_config();
1071 read_agent_config();
1077 struct agent_pvt *p;
1078 /* First, take us out of the channel loop */
1079 ast_cli_unregister(&cli_show_agents);
1080 ast_unregister_application(app);
1081 ast_unregister_application(app2);
1082 ast_channel_unregister(type);
1083 if (!ast_pthread_mutex_lock(&agentlock)) {
1084 /* Hangup all interfaces if they have an owner */
1088 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1092 ast_pthread_mutex_unlock(&agentlock);
1094 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1103 ast_pthread_mutex_lock(&usecnt_lock);
1105 ast_pthread_mutex_unlock(&usecnt_lock);
1111 return ASTERISK_GPL_KEY;