61f5bb29b492e4b27b506f22704d11c05d967d32
[asterisk/asterisk.git] / channels / chan_agent.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Implementation of Session Initiation Protocol
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <pthread.h>
16 #include <string.h>
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 <sys/socket.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <netdb.h>
42 #include <arpa/inet.h>
43 #include <sys/signal.h>
44
45 static char *desc = "Agent Proxy Channel";
46 static char *type = "Agent";
47 static char *tdesc = "Call Agent Proxy Channel";
48 static char *config = "agents.conf";
49
50 static char *app = "AgentLogin";
51
52 static char *synopsis = "Call agent login";
53
54 static char *descrip =
55 "  AgentLogin([AgentNo][|options]):\n"
56 "Asks the agent to login to the system.  Always returns -1.  While\n"
57 "logged in, the agent can receive calls and will hear a 'beep'\n"
58 "when a new call comes in.  The agent can dump the call by pressing\n"
59 "the star key.\n"
60 "The option string may contain zero or more of the following characters:\n"
61 "      's' -- silent login - do not announce the login ok segment\n";
62
63 static char moh[80] = "default";
64
65 #define AST_MAX_AGENT   80              /* Agent ID or Password max length */
66
67 static int capability = -1;
68
69 static int usecnt =0;
70 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
71
72 /* Protect the interface list (of sip_pvt's) */
73 static pthread_mutex_t agentlock = AST_MUTEX_INITIALIZER;
74
75 static struct agent_pvt {
76         pthread_mutex_t lock;                           /* Channel private lock */
77         int dead;                                                       /* Poised for destruction? */
78         char moh[80];                                           /* Which music on hold */
79         char agent[AST_MAX_AGENT];                      /* Agent ID */
80         char password[AST_MAX_AGENT];           /* Password for Agent login */
81         char name[AST_MAX_AGENT];
82         pthread_mutex_t app_lock;                       /* Synchronization between owning applications */
83         volatile pthread_t owning_app;          /* Owning application thread id */
84         volatile int app_sleep_cond;            /* Sleep condition for the login app */
85         struct ast_channel *owner;                      /* Agent */
86         struct ast_channel *chan;                       /* Channel we use */
87         struct agent_pvt *next;                         /* Agent */
88 } *agents = NULL;
89
90 #define CLEANUP(ast, p) do { \
91         int x; \
92         if (p->chan) { \
93                 for (x=0;x<AST_MAX_FDS;x++) \
94                         ast->fds[x] = p->chan->fds[x]; \
95         } \
96 } while(0)
97
98
99 static int add_agent(struct ast_variable *var)
100 {
101         char tmp[256];
102         char *password=NULL, *name=NULL;
103         struct agent_pvt *p;
104         
105         strncpy(tmp, var->value, sizeof(tmp));
106         if ((password = strchr(tmp, ','))) {
107                 *password = '\0';
108                 password++;
109                 while (*password < 33) password++;
110         }
111         if (password && (name = strchr(password, ','))) {
112                 *name = '\0';
113                 name++;
114                 while (*name < 33) name++; 
115         }
116         p = agents;
117         while(p) {
118                 if (!strcmp(p->agent, tmp))
119                         break;
120                 p = p->next;
121         }
122         if (!p) {
123                 p = malloc(sizeof(struct agent_pvt));
124                 if (p) {
125                         memset(p, 0, sizeof(struct agent_pvt));
126                         strncpy(p->agent, tmp, sizeof(p->agent) -1);
127                         ast_pthread_mutex_init( &p->lock );
128                         ast_pthread_mutex_init( &p->app_lock );
129                         p->owning_app = -1;
130                         p->app_sleep_cond = 1;
131                         p->next = agents;
132                         agents = p;
133                         
134                 }
135         }
136         if (!p)
137                 return -1;
138         strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
139         strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
140         strncpy(p->moh, moh, sizeof(p->moh) - 1);
141         p->dead = 0;
142         return 0;
143 }
144
145 static int agent_answer(struct ast_channel *ast)
146 {
147         ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
148         return -1;
149 }
150
151 static struct ast_frame  *agent_read(struct ast_channel *ast)
152 {
153         struct agent_pvt *p = ast->pvt->pvt;
154         struct ast_frame *f = NULL;
155         ast_pthread_mutex_lock(&p->lock);
156         if (p->chan)
157                 f = ast_read(p->chan);
158         if (!f) {
159                 /* If there's a channel, make it NULL */
160                 if (p->chan)
161                         p->chan = NULL;
162         }
163         if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
164                 /* * terminates call */
165                 ast_frfree(f);
166                 f = NULL;
167         }
168         CLEANUP(ast,p);
169         ast_pthread_mutex_unlock(&p->lock);
170         return f;
171 }
172
173 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
174 {
175         struct agent_pvt *p = ast->pvt->pvt;
176         int res = -1;
177         ast_pthread_mutex_lock(&p->lock);
178         if (p->chan)
179                 res = ast_write(p->chan, f);
180         CLEANUP(ast, p);
181         ast_pthread_mutex_unlock(&p->lock);
182         return res;
183 }
184
185 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
186 {
187         struct agent_pvt *p = newchan->pvt->pvt;
188         ast_pthread_mutex_lock(&p->lock);
189         if (p->owner != oldchan) {
190                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
191                 ast_pthread_mutex_unlock(&p->lock);
192                 return -1;
193         }
194         p->owner = newchan;
195         ast_pthread_mutex_unlock(&p->lock);
196         return 0;
197 }
198
199 static int agent_indicate(struct ast_channel *ast, int condition)
200 {
201         struct agent_pvt *p = ast->pvt->pvt;
202         int res = -1;
203         ast_pthread_mutex_lock(&p->lock);
204         if (p->chan)
205                 res = ast_indicate(p->chan, condition);
206         ast_pthread_mutex_unlock(&p->lock);
207         return res;
208 }
209
210 static int agent_digit(struct ast_channel *ast, char digit)
211 {
212         struct agent_pvt *p = ast->pvt->pvt;
213         int res = -1;
214         ast_pthread_mutex_lock(&p->lock);
215         if (p->chan)
216                 res = p->chan->pvt->send_digit(p->chan, digit);
217         ast_pthread_mutex_unlock(&p->lock);
218         return res;
219 }
220
221 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
222 {
223         struct agent_pvt *p = ast->pvt->pvt;
224         int res = -1;
225         ast_pthread_mutex_lock(&p->lock);
226         res = ast_streamfile(p->chan, "beep", p->chan->language);
227         if (!res)
228                 res = ast_waitstream(p->chan, "");
229         if (!res) {
230                 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
231                 if (res)
232                         ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
233         }
234         if (!res) {
235                 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
236                 if (res)
237                         ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
238         }
239         /* Call is immediately up */
240         ast_setstate(ast, AST_STATE_UP);
241         CLEANUP(ast,p);
242         ast_pthread_mutex_unlock(&p->lock);
243         return res;
244 }
245
246 static int agent_hangup(struct ast_channel *ast)
247 {
248         struct agent_pvt *p = ast->pvt->pvt;
249         ast_pthread_mutex_lock(&p->lock);
250         p->owner = NULL;
251         ast->pvt->pvt = NULL;
252         p->app_sleep_cond = 1;
253         ast_pthread_mutex_unlock(&p->lock);
254         /* Release ownership of the agent to other threads (presumably running the login app). */
255         ast_pthread_mutex_unlock(&p->app_lock);
256         if (p->chan) {
257                 /* If they're dead, go ahead and hang up on the agent now */
258                 ast_pthread_mutex_lock(&p->chan->lock);
259                 if (p->dead)
260                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
261                 ast_moh_start(p->chan, p->moh);
262                 ast_pthread_mutex_unlock(&p->chan->lock);
263         } else if (p->dead)
264                 /* Go ahead and lose it */
265                 free(p);
266
267         return 0;
268 }
269
270 static int agent_cont_sleep( void *data )
271 {
272         struct agent_pvt *p;
273         int res;
274
275         p = (struct agent_pvt *)data;
276
277         ast_pthread_mutex_lock(&p->lock);
278         res = p->app_sleep_cond;
279         ast_pthread_mutex_unlock(&p->lock);
280         if( !res )
281                 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
282         return res;
283 }
284
285 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
286 {
287         struct ast_channel *tmp;
288         struct ast_frame null_frame = { AST_FRAME_NULL };
289         if (!p->chan) {
290                 ast_log(LOG_WARNING, "No channel? :(\n");
291                 return NULL;
292         }
293         tmp = ast_channel_alloc(0);
294         if (tmp) {
295                 tmp->nativeformats = p->chan->nativeformats;
296                 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
297                 tmp->type = type;
298                 ast_setstate(tmp, state);
299                 tmp->writeformat = p->chan->writeformat;
300                 tmp->pvt->rawwriteformat = p->chan->writeformat;
301                 tmp->readformat = p->chan->readformat;
302                 tmp->pvt->rawreadformat = p->chan->readformat;
303                 tmp->pvt->pvt = p;
304                 tmp->pvt->send_digit = agent_digit;
305                 tmp->pvt->call = agent_call;
306                 tmp->pvt->hangup = agent_hangup;
307                 tmp->pvt->answer = agent_answer;
308                 tmp->pvt->read = agent_read;
309                 tmp->pvt->write = agent_write;
310                 tmp->pvt->exception = agent_read;
311                 tmp->pvt->indicate = agent_indicate;
312                 tmp->pvt->fixup = agent_fixup;
313                 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
314                 p->owner = tmp;
315                 ast_pthread_mutex_lock(&usecnt_lock);
316                 usecnt++;
317                 ast_pthread_mutex_unlock(&usecnt_lock);
318                 ast_update_use_count();
319                 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
320                 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
321                 tmp->priority = 1;
322                 /* Wake up and wait for other applications (by definition the login app)
323                  * to release this channel). Takes ownership of the agent channel
324                  * to this thread only.
325                  * For signalling the other thread, ast_queue_frame is used until we
326                  * can safely use signals for this purpose. The pselect() needs to be
327                  * implemented in the kernel for this.
328                  */
329                 p->app_sleep_cond = 0;
330                 if( pthread_mutex_trylock(&p->app_lock) )
331                 {
332                         ast_queue_frame(p->chan, &null_frame, 1);
333                         ast_pthread_mutex_unlock(&p->lock);     /* For other thread to read the condition. */
334                         ast_pthread_mutex_lock(&p->app_lock);
335                         ast_pthread_mutex_lock(&p->lock);
336                 }
337                 p->owning_app = pthread_self();
338                 /* After the above step, there should not be any blockers. */
339                 if (p->chan->blocking) {
340                         ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
341                         CRASH;
342                 }
343                 ast_moh_stop(p->chan);
344         } else
345                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
346         return tmp;
347 }
348
349
350 static int read_agent_config(void)
351 {
352         struct ast_config *cfg;
353         struct ast_variable *v;
354         struct agent_pvt *p, *pl, *pn;
355         cfg = ast_load(config);
356         if (!cfg) {
357                 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
358                 return 0;
359         }
360         ast_pthread_mutex_lock(&agentlock);
361         p = agents;
362         while(p) {
363                 p->dead = 1;
364                 p = p->next;
365         }
366         strcpy(moh, "default");
367         v = ast_variable_browse(cfg, "agents");
368         while(v) {
369                 /* Create the interface list */
370                 if (!strcasecmp(v->name, "agent")) {
371                         add_agent(v);
372                 } else if (!strcasecmp(v->name, "musiconhold")) {
373                         strncpy(moh, v->value, sizeof(moh) - 1);
374                 }
375                 v = v->next;
376         }
377         p = agents;
378         pl = NULL;
379         while(p) {
380                 pn = p->next;
381                 if (p->dead) {
382                         /* Unlink */
383                         if (pl)
384                                 pl->next = p->next;
385                         else
386                                 agents = p->next;
387                         /* Destroy if  appropriate */
388                         if (!p->owner) {
389                                 if (!p->chan) {
390                                         free(p);
391                                 } else {
392                                         /* Cause them to hang up */
393                                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
394                                 }
395                         }
396                 } else
397                         pl = p;
398                 p = pn;
399         }
400         ast_pthread_mutex_unlock(&agentlock);
401         ast_destroy(cfg);
402         return 0;
403 }
404
405 static struct ast_channel *agent_request(char *type, int format, void *data)
406 {
407         struct agent_pvt *p;
408         struct ast_channel *chan = NULL;
409         ast_pthread_mutex_lock(&agentlock);
410         p = agents;
411         while(p) {
412                 if (!strcmp(data, p->agent)) {
413                         ast_pthread_mutex_lock(&p->lock);
414                         /* Agent must be registered, but not have any active call */
415                         if (!p->owner && p->chan) {
416                                 chan = agent_new(p, AST_STATE_DOWN);
417                         }
418                         ast_pthread_mutex_unlock(&p->lock);
419                         break;
420                 }
421                 p = p->next;
422         }
423         ast_pthread_mutex_unlock(&agentlock);
424         return chan;
425 }
426
427 static int agents_show(int fd, int argc, char **argv)
428 {
429         struct agent_pvt *p;
430         char username[256];
431         char location[256];
432         char talkingto[256];
433
434         if (argc != 2)
435                 return RESULT_SHOWUSAGE;
436         ast_pthread_mutex_lock(&agentlock);
437         p = agents;
438         while(p) {
439                 ast_pthread_mutex_lock(&p->lock);
440                 if (strlen(p->name))
441                         snprintf(username, sizeof(username), "(%s) ", p->name);
442                 else
443                         strcpy(username, "");
444                 if (p->chan) {
445                         snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
446                         if (p->owner && p->owner->bridge) {
447                                 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
448                         } else {
449                                 strcpy(talkingto, " is idle");
450                         }
451                 } else {
452                         strcpy(location, "not logged in");
453                         strcpy(talkingto, "");
454                 }
455                 ast_pthread_mutex_unlock(&p->lock);
456                 ast_cli(fd, "%-12.12s %s%s%s\n", p->agent, 
457                                 username, location, talkingto);
458                 p = p->next;
459         }
460         ast_pthread_mutex_unlock(&agentlock);
461         return RESULT_SUCCESS;
462 }
463
464 static char show_agents_usage[] = 
465 "Usage: show agents\n"
466 "       Provides summary information on agents.\n";
467
468 static struct ast_cli_entry cli_show_agents = {
469         { "show", "agents", NULL }, agents_show, 
470         "Show status of agents", show_agents_usage, NULL };
471
472 STANDARD_LOCAL_USER;
473 LOCAL_USER_DECL;
474
475 static int login_exec(struct ast_channel *chan, void *data)
476 {
477         int res=0;
478         int tries = 0;
479         struct agent_pvt *p;
480         struct localuser *u;
481         char user[AST_MAX_AGENT];
482         char pass[AST_MAX_AGENT];
483         char xpass[AST_MAX_AGENT] = "";
484         char *errmsg;
485         char info[512];
486         char *opt_user = NULL;
487         char *options = NULL;
488         int play_announcement;
489         struct timespec required;
490         struct timespec remaining;
491         int delay;
492         
493         LOCAL_USER_ADD(u);
494
495         /* Parse the arguments XXX Check for failure XXX */
496         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
497         opt_user = info;
498         if( opt_user ) {
499                 options = strchr(opt_user, '|');
500                 if (options) {
501                         *options = '\0';
502                         options++;
503                 }
504         }
505
506         if (chan->_state != AST_STATE_UP)
507                 res = ast_answer(chan);
508         if (!res) {
509                 if( opt_user )
510                         strncpy( user, opt_user, AST_MAX_AGENT );
511                 else
512                         res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
513         }
514         while (!res && (tries < 3)) {
515                 /* Check for password */
516                 ast_pthread_mutex_lock(&agentlock);
517                 p = agents;
518                 while(p) {
519                         if (!strcmp(p->agent, user))
520                                 strncpy(xpass, p->password, sizeof(xpass) - 1);
521                         p = p->next;
522                 }
523                 ast_pthread_mutex_unlock(&agentlock);
524                 if (!res) {
525                         if (strlen(xpass))
526                                 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
527                         else
528                                 strcpy(pass, "");
529                 }
530                 errmsg = "agent-incorrect";
531
532 #if 0
533                 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
534 #endif          
535
536                 /* Check again for accuracy */
537                 ast_pthread_mutex_lock(&agentlock);
538                 p = agents;
539                 while(p) {
540                         ast_pthread_mutex_lock(&p->lock);
541                         if (!strcmp(p->agent, user) &&
542                                 !strcmp(p->password, pass)) {
543                                         if (!p->chan) {
544                                                 play_announcement = 1;
545                                                 if( options )
546                                                         if( strchr( options, 's' ) )
547                                                                 play_announcement = 0;
548                                                 if( play_announcement )
549                                                         res = ast_streamfile(chan, "agent-loginok", chan->language);
550                                                 if (!res)
551                                                         ast_waitstream(chan, "");
552                                                 if (!res) {
553                                                         res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
554                                                         if (res)
555                                                                 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
556                                                 }
557                                                 if (!res) {
558                                                         ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
559                                                         if (res)
560                                                                 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
561                                                 }
562                                                 /* Check once more just in case */
563                                                 if (p->chan)
564                                                         res = -1;
565                                                 if (!res) {
566                                                         ast_moh_start(chan, p->moh);
567                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogin",
568                                                                 "Agent: %s\r\n"
569                                                                 "Channel: %s\r\n",
570                                                                 p->agent, chan->name);
571                                                         if (option_verbose > 2)
572                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %d/%d)\n", p->agent,
573                                                                                                 chan->readformat, chan->writeformat);
574                                                         /* Login this channel and wait for it to
575                                                            go away */
576                                                         p->chan = chan;
577                                                         ast_pthread_mutex_unlock(&p->lock);
578                                                         ast_pthread_mutex_unlock(&agentlock);
579                                                         while (res >= 0) {
580                                                                 /* If we are not the owner, delay here for a while
581                                                                  * so other interested threads can kick in. */
582                                                                 delay = 0;
583                                                                 ast_pthread_mutex_lock(&p->lock);
584                                                                 if (p->chan != chan)
585                                                                         res = -1;
586                                                                 if (p->owner) 
587                                                                         delay = 1;
588                                                                 ast_pthread_mutex_unlock(&p->lock);
589                                                                 if (delay) {
590                                                                         sched_yield();
591                                                                         required.tv_sec = 0;
592                                                                         required.tv_nsec = 20 * 1000 * 1000;
593                                                                         nanosleep( &required, &remaining );
594                                                                 }
595                                                                 if (res)
596                                                                         break;
597
598                                                                 /*      Synchronize channel ownership between call to agent and itself. */
599                                                                 pthread_mutex_lock( &p->app_lock );
600                                                                 ast_pthread_mutex_lock(&p->lock);
601                                                                 p->owning_app = pthread_self();
602                                                                 ast_pthread_mutex_unlock(&p->lock);
603                                                                 res = ast_safe_sleep_conditional( chan, 1000,
604                                                                                                                 agent_cont_sleep, p );
605                                                                 pthread_mutex_unlock( &p->app_lock );
606                                                         }
607                                                         if (res && p->owner) 
608                                                                 ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
609                                                         /* Log us off if appropriate */
610                                                         if (p->chan == chan)
611                                                                 p->chan = NULL;
612                                                         if (option_verbose > 2)
613                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
614                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
615                                                                 "Agent: %s\r\n",
616                                                                 p->agent);
617                                                         /* If there is no owner, go ahead and kill it now */
618                                                         if (p->dead && !p->owner)
619                                                                 free(p);
620                                                 }
621                                                 else {
622                                                         ast_pthread_mutex_unlock(&p->lock);
623                                                         p = NULL;
624                                                 }
625                                                 res = -1;
626                                         } else {
627                                                 ast_pthread_mutex_unlock(&p->lock);
628                                                 errmsg = "agent-alreadyon";
629                                                 p = NULL;
630                                         }
631                                         break;
632                         }
633                         ast_pthread_mutex_unlock(&p->lock);
634                         p = p->next;
635                 }
636                 if (!p)
637                         ast_pthread_mutex_unlock(&agentlock);
638
639                 if (!res)
640                         res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
641         }
642                 
643         LOCAL_USER_REMOVE(u);
644         /* Always hangup */
645         return -1;
646 }
647
648
649 int load_module()
650 {
651         /* Make sure we can register our sip channel type */
652         if (ast_channel_register(type, tdesc, capability, agent_request)) {
653                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
654                 return -1;
655         }
656         ast_register_application(app, login_exec, synopsis, descrip);
657         ast_cli_register(&cli_show_agents);
658         /* Read in the config */
659         read_agent_config();
660         return 0;
661 }
662
663 int reload()
664 {
665         read_agent_config();
666         return 0;
667 }
668
669 int unload_module()
670 {
671         struct agent_pvt *p;
672         /* First, take us out of the channel loop */
673         ast_cli_unregister(&cli_show_agents);
674         ast_unregister_application(app);
675         ast_channel_unregister(type);
676         if (!ast_pthread_mutex_lock(&agentlock)) {
677                 /* Hangup all interfaces if they have an owner */
678                 p = agents;
679                 while(p) {
680                         if (p->owner)
681                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
682                         p = p->next;
683                 }
684                 agents = NULL;
685                 ast_pthread_mutex_unlock(&agentlock);
686         } else {
687                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
688                 return -1;
689         }               
690         return 0;
691 }
692
693 int usecount()
694 {
695         int res;
696         ast_pthread_mutex_lock(&usecnt_lock);
697         res = usecnt;
698         ast_pthread_mutex_unlock(&usecnt_lock);
699         return res;
700 }
701
702 char *key()
703 {
704         return ASTERISK_GPL_KEY;
705 }
706
707 char *description()
708 {
709         return desc;
710 }
711