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 /* Don't pass answer along */
244 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
245 if (!p->acknowledged) {
251 if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
252 /* * terminates call */
257 ast_pthread_mutex_unlock(&p->lock);
261 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
263 struct agent_pvt *p = ast->pvt->pvt;
265 ast_pthread_mutex_lock(&p->lock);
267 res = ast_write(p->chan, f);
271 ast_pthread_mutex_unlock(&p->lock);
275 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
277 struct agent_pvt *p = newchan->pvt->pvt;
278 ast_pthread_mutex_lock(&p->lock);
279 if (p->owner != oldchan) {
280 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
281 ast_pthread_mutex_unlock(&p->lock);
285 ast_pthread_mutex_unlock(&p->lock);
289 static int agent_indicate(struct ast_channel *ast, int condition)
291 struct agent_pvt *p = ast->pvt->pvt;
293 ast_pthread_mutex_lock(&p->lock);
295 res = ast_indicate(p->chan, condition);
298 ast_pthread_mutex_unlock(&p->lock);
302 static int agent_digit(struct ast_channel *ast, char digit)
304 struct agent_pvt *p = ast->pvt->pvt;
306 ast_pthread_mutex_lock(&p->lock);
308 res = p->chan->pvt->send_digit(p->chan, digit);
311 ast_pthread_mutex_unlock(&p->lock);
315 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
317 struct agent_pvt *p = ast->pvt->pvt;
319 ast_pthread_mutex_lock(&p->lock);
322 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
323 ast_setstate(ast, AST_STATE_DIALING);
326 ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
329 ast_pthread_mutex_unlock(&p->lock);
331 } else if (strlen(p->loginchan)) {
333 /* Call on this agent */
334 if (option_verbose > 2)
335 ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
336 res = ast_call(p->chan, p->loginchan, 0);
338 ast_pthread_mutex_unlock(&p->lock);
341 ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
342 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
343 res = ast_streamfile(p->chan, "beep", p->chan->language);
344 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
346 res = ast_waitstream(p->chan, "");
347 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
350 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
351 ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
353 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
360 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
361 ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
363 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
367 /* Call is immediately up */
368 ast_setstate(ast, AST_STATE_UP);
371 ast_pthread_mutex_unlock(&p->lock);
375 static int agent_hangup(struct ast_channel *ast)
377 struct agent_pvt *p = ast->pvt->pvt;
379 ast_pthread_mutex_lock(&p->lock);
381 ast->pvt->pvt = NULL;
382 p->app_sleep_cond = 1;
383 if (p->start && (ast->_state != AST_STATE_UP))
384 howlong = time(NULL) - p->start;
387 /* If they're dead, go ahead and hang up on the agent now */
388 if (strlen(p->loginchan)) {
390 /* Recognize the hangup and pass it along immediately */
394 ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
395 if (howlong && p->autologoff && (howlong > p->autologoff)) {
396 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
397 strcpy(p->loginchan, "");
399 } else if (p->dead) {
400 ast_pthread_mutex_lock(&p->chan->lock);
401 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
402 ast_pthread_mutex_unlock(&p->chan->lock);
404 ast_pthread_mutex_lock(&p->chan->lock);
405 ast_moh_start(p->chan, p->moh);
406 ast_pthread_mutex_unlock(&p->chan->lock);
410 ast_pthread_mutex_unlock(&p->lock);
411 /* Release ownership of the agent to other threads (presumably running the login app). */
412 ast_pthread_mutex_unlock(&p->app_lock);
413 } else if (p->dead) {
414 /* Go ahead and lose it */
415 ast_pthread_mutex_unlock(&p->lock);
416 /* Release ownership of the agent to other threads (presumably running the login app). */
417 ast_pthread_mutex_unlock(&p->app_lock);
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);
424 ast_pthread_mutex_unlock(&p->lock);
425 /* Release ownership of the agent to other threads (presumably running the login app). */
426 ast_pthread_mutex_unlock(&p->app_lock);
429 ast_pthread_mutex_lock(&agentlock);
431 ast_pthread_mutex_unlock(&agentlock);
433 if (p->abouttograb) {
434 /* Let the "about to grab" thread know this isn't valid anymore, and let it
437 } else if (p->dead) {
439 } else if (p->chan) {
440 /* Not dead -- check availability now */
441 ast_pthread_mutex_lock(&p->lock);
442 /* check_availability(p, 1); */
443 /* Store last disconnect time */
444 gettimeofday(&p->lastdisc, NULL);
445 ast_pthread_mutex_unlock(&p->lock);
450 static int agent_cont_sleep( void *data )
456 p = (struct agent_pvt *)data;
458 ast_pthread_mutex_lock(&p->lock);
459 res = p->app_sleep_cond;
460 if (p->lastdisc.tv_sec) {
461 gettimeofday(&tv, NULL);
462 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
463 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
466 ast_pthread_mutex_unlock(&p->lock);
469 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
474 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
476 struct ast_channel *tmp;
477 struct ast_frame null_frame = { AST_FRAME_NULL };
480 ast_log(LOG_WARNING, "No channel? :(\n");
484 tmp = ast_channel_alloc(0);
487 tmp->nativeformats = p->chan->nativeformats;
488 tmp->writeformat = p->chan->writeformat;
489 tmp->pvt->rawwriteformat = p->chan->writeformat;
490 tmp->readformat = p->chan->readformat;
491 tmp->pvt->rawreadformat = p->chan->readformat;
492 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
493 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
494 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
496 tmp->nativeformats = AST_FORMAT_SLINEAR;
497 tmp->writeformat = AST_FORMAT_SLINEAR;
498 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
499 tmp->readformat = AST_FORMAT_SLINEAR;
500 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
503 snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
505 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
507 ast_setstate(tmp, state);
509 tmp->pvt->send_digit = agent_digit;
510 tmp->pvt->call = agent_call;
511 tmp->pvt->hangup = agent_hangup;
512 tmp->pvt->answer = agent_answer;
513 tmp->pvt->read = agent_read;
514 tmp->pvt->write = agent_write;
515 tmp->pvt->exception = agent_read;
516 tmp->pvt->indicate = agent_indicate;
517 tmp->pvt->fixup = agent_fixup;
519 ast_pthread_mutex_lock(&usecnt_lock);
521 ast_pthread_mutex_unlock(&usecnt_lock);
522 ast_update_use_count();
524 /* Wake up and wait for other applications (by definition the login app)
525 * to release this channel). Takes ownership of the agent channel
526 * to this thread only.
527 * For signalling the other thread, ast_queue_frame is used until we
528 * can safely use signals for this purpose. The pselect() needs to be
529 * implemented in the kernel for this.
531 p->app_sleep_cond = 0;
532 if( pthread_mutex_trylock(&p->app_lock) )
535 ast_queue_frame(p->chan, &null_frame, 1);
536 ast_pthread_mutex_unlock(&p->lock); /* For other thread to read the condition. */
537 ast_pthread_mutex_lock(&p->app_lock);
538 ast_pthread_mutex_lock(&p->lock);
542 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
544 tmp->pvt->pvt = NULL;
545 p->app_sleep_cond = 1;
546 ast_channel_free( tmp );
550 p->owning_app = pthread_self();
551 /* After the above step, there should not be any blockers. */
553 if (p->chan->blocking) {
554 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
557 ast_moh_stop(p->chan);
560 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
565 static int read_agent_config(void)
567 struct ast_config *cfg;
568 struct ast_variable *v;
569 struct agent_pvt *p, *pl, *pn;
573 cfg = ast_load(config);
575 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
578 ast_pthread_mutex_lock(&agentlock);
584 strcpy(moh, "default");
585 v = ast_variable_browse(cfg, "agents");
587 /* Create the interface list */
588 if (!strcasecmp(v->name, "agent")) {
589 add_agent(v->value, 0);
590 } else if (!strcasecmp(v->name, "group")) {
591 group = ast_get_group(v->value);
592 } else if (!strcasecmp(v->name, "autologoff")) {
593 autologoff = atoi(v->value);
596 } else if (!strcasecmp(v->name, "wrapuptime")) {
597 wrapuptime = atoi(v->value);
600 } else if (!strcasecmp(v->name, "musiconhold")) {
601 strncpy(moh, v->value, sizeof(moh) - 1);
615 /* Destroy if appropriate */
620 /* Cause them to hang up */
621 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
628 ast_pthread_mutex_unlock(&agentlock);
633 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
635 struct ast_channel *chan=NULL, *parent=NULL;
638 ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
640 ast_pthread_mutex_lock(&agentlock);
643 if (p == newlyavailable) {
647 ast_pthread_mutex_lock(&p->lock);
648 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
649 ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
650 /* We found a pending call, time to merge */
651 chan = agent_new(newlyavailable, AST_STATE_DOWN);
654 ast_pthread_mutex_unlock(&p->lock);
657 ast_pthread_mutex_unlock(&p->lock);
661 ast_pthread_mutex_unlock(&agentlock);
662 if (parent && chan) {
663 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
664 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
665 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
667 res = ast_waitstream(newlyavailable->chan, "");
668 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
671 /* Note -- parent may have disappeared */
672 if (p->abouttograb) {
673 ast_setstate(parent, AST_STATE_UP);
674 ast_setstate(chan, AST_STATE_UP);
675 /* Go ahead and mark the channel as a zombie so that masquerade will
676 destroy it for us, and we need not call ast_hangup */
677 ast_pthread_mutex_lock(&parent->lock);
679 ast_channel_masquerade(parent, chan);
680 ast_pthread_mutex_unlock(&parent->lock);
683 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
684 agent_cleanup(newlyavailable);
687 ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
688 agent_cleanup(newlyavailable);
694 static struct ast_channel *agent_request(char *type, int format, void *data)
697 struct ast_channel *chan = NULL;
699 unsigned int groupmatch;
702 if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
703 groupmatch = (1 << groupmatch);
704 } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
705 groupmatch = (1 << groupmatch);
710 ast_pthread_mutex_lock(&agentlock);
713 ast_pthread_mutex_lock(&p->lock);
714 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
715 !p->lastdisc.tv_sec) {
716 /* Agent must be registered, but not have any active call, and not be in a waiting state */
717 if (!p->owner && p->chan) {
719 chan = agent_new(p, AST_STATE_DOWN);
720 } else if (!p->owner && strlen(p->loginchan)) {
721 /* Adjustable agent */
722 p->chan = ast_request("Local", format, p->loginchan);
724 chan = agent_new(p, AST_STATE_DOWN);
727 ast_pthread_mutex_unlock(&p->lock);
731 ast_pthread_mutex_unlock(&p->lock);
734 if (!chan && waitforagent) {
735 /* No agent available -- but we're requesting to wait for one.
736 Allocate a place holder */
737 ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
738 p = add_agent(data, 1);
739 p->group = groupmatch;
740 chan = agent_new(p, AST_STATE_DOWN);
742 ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
745 ast_pthread_mutex_unlock(&agentlock);
749 static int powerof(unsigned int v)
753 if (v & (1 << x)) return x;
758 static int agents_show(int fd, int argc, char **argv)
767 return RESULT_SHOWUSAGE;
768 ast_pthread_mutex_lock(&agentlock);
771 ast_pthread_mutex_lock(&p->lock);
774 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
776 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
779 snprintf(username, sizeof(username), "(%s) ", p->name);
781 strcpy(username, "");
783 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
784 if (p->owner && p->owner->bridge) {
785 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
787 strcpy(talkingto, " is idle");
789 } else if (strlen(p->loginchan)) {
790 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
791 strcpy(talkingto, "");
793 strcat(location, " (Confirmed)");
795 strcpy(location, "not logged in");
796 strcpy(talkingto, "");
799 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
800 ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
801 username, location, talkingto, moh);
803 ast_pthread_mutex_unlock(&p->lock);
806 ast_pthread_mutex_unlock(&agentlock);
807 return RESULT_SUCCESS;
810 static char show_agents_usage[] =
811 "Usage: show agents\n"
812 " Provides summary information on agents.\n";
814 static struct ast_cli_entry cli_show_agents = {
815 { "show", "agents", NULL }, agents_show,
816 "Show status of agents", show_agents_usage, NULL };
821 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
828 char user[AST_MAX_AGENT];
829 char pass[AST_MAX_AGENT];
830 char xpass[AST_MAX_AGENT] = "";
833 char *opt_user = NULL;
834 char *options = NULL;
835 int play_announcement;
836 char *filename = "agent-loginok";
840 /* Parse the arguments XXX Check for failure XXX */
841 strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
844 options = strchr(opt_user, '|');
851 if (chan->_state != AST_STATE_UP)
852 res = ast_answer(chan);
854 if( opt_user && strlen(opt_user))
855 strncpy( user, opt_user, AST_MAX_AGENT );
857 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
859 while (!res && (tries < 3)) {
860 /* Check for password */
861 ast_pthread_mutex_lock(&agentlock);
864 if (!strcmp(p->agent, user) && !p->pending)
865 strncpy(xpass, p->password, sizeof(xpass) - 1);
868 ast_pthread_mutex_unlock(&agentlock);
871 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
875 errmsg = "agent-incorrect";
878 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
881 /* Check again for accuracy */
882 ast_pthread_mutex_lock(&agentlock);
885 ast_pthread_mutex_lock(&p->lock);
886 if (!strcmp(p->agent, user) &&
887 !strcmp(p->password, pass) && !p->pending) {
890 char tmpchan[256] = "";
891 /* Retrieve login chan */
892 res = ast_app_getdata(chan, "agent-newlocation", tmpchan, sizeof(tmpchan) - 1, 0);
894 strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
895 if (!strlen(p->loginchan))
896 filename = "agent-loggedoff";
900 strcpy(p->loginchan, "");
903 play_announcement = 1;
905 if( strchr( options, 's' ) )
906 play_announcement = 0;
907 if( !res && play_announcement )
908 res = ast_streamfile(chan, filename, chan->language);
910 ast_waitstream(chan, "");
912 res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
914 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
917 ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
919 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
921 /* Check once more just in case */
924 if (callbackmode && !res) {
925 /* Just say goodbye and be done with it */
927 res = ast_safe_sleep(chan, 500);
928 res = ast_streamfile(chan, "vm-goodbye", chan->language);
930 res = ast_waitstream(chan, "");
932 res = ast_safe_sleep(chan, 1000);
933 ast_pthread_mutex_unlock(&p->lock);
934 ast_pthread_mutex_unlock(&agentlock);
936 /* check if the moh class was changed with setmusiconhold */
937 if (*(chan->musicclass))
938 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
939 ast_moh_start(chan, p->moh);
940 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
943 p->agent, chan->name);
944 if (option_verbose > 2)
945 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %d/%d)\n", p->agent,
946 chan->readformat, chan->writeformat);
947 /* Login this channel and wait for it to
951 check_availability(p, 0);
952 ast_pthread_mutex_unlock(&p->lock);
953 ast_pthread_mutex_unlock(&agentlock);
955 ast_pthread_mutex_lock(&p->lock);
958 ast_pthread_mutex_unlock(&p->lock);
959 /* Yield here so other interested threads can kick in. */
964 ast_pthread_mutex_lock(&p->lock);
965 if (p->lastdisc.tv_sec) {
966 gettimeofday(&tv, NULL);
967 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
968 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
969 ast_log(LOG_DEBUG, "Wrapup time expired!\n");
970 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
971 check_availability(p, 1);
974 ast_pthread_mutex_unlock(&p->lock);
975 /* Synchronize channel ownership between call to agent and itself. */
976 pthread_mutex_lock( &p->app_lock );
977 ast_pthread_mutex_lock(&p->lock);
978 p->owning_app = pthread_self();
979 ast_pthread_mutex_unlock(&p->lock);
980 res = ast_safe_sleep_conditional( chan, 1000,
981 agent_cont_sleep, p );
982 pthread_mutex_unlock( &p->app_lock );
985 ast_pthread_mutex_lock(&p->lock);
987 ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
988 /* Log us off if appropriate */
992 ast_pthread_mutex_unlock(&p->lock);
993 if (option_verbose > 2)
994 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
995 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
998 /* If there is no owner, go ahead and kill it now */
999 if (p->dead && !p->owner)
1003 ast_pthread_mutex_unlock(&p->lock);
1008 ast_pthread_mutex_unlock(&p->lock);
1009 errmsg = "agent-alreadyon";
1014 ast_pthread_mutex_unlock(&p->lock);
1018 ast_pthread_mutex_unlock(&agentlock);
1021 res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1024 LOCAL_USER_REMOVE(u);
1029 static int login_exec(struct ast_channel *chan, void *data)
1031 return __login_exec(chan, data, 0);
1034 static int callback_exec(struct ast_channel *chan, void *data)
1036 return __login_exec(chan, data, 1);
1041 /* Make sure we can register our sip channel type */
1042 if (ast_channel_register(type, tdesc, capability, agent_request)) {
1043 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1046 ast_register_application(app, login_exec, synopsis, descrip);
1047 ast_register_application(app2, callback_exec, synopsis2, descrip2);
1048 ast_cli_register(&cli_show_agents);
1049 /* Read in the config */
1050 read_agent_config();
1056 read_agent_config();
1062 struct agent_pvt *p;
1063 /* First, take us out of the channel loop */
1064 ast_cli_unregister(&cli_show_agents);
1065 ast_unregister_application(app);
1066 ast_unregister_application(app2);
1067 ast_channel_unregister(type);
1068 if (!ast_pthread_mutex_lock(&agentlock)) {
1069 /* Hangup all interfaces if they have an owner */
1073 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1077 ast_pthread_mutex_unlock(&agentlock);
1079 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1088 ast_pthread_mutex_lock(&usecnt_lock);
1090 ast_pthread_mutex_unlock(&usecnt_lock);
1096 return ASTERISK_GPL_KEY;