1a2c479207f97c6fc77192c8904186d240f79e49
[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 <asterisk/parking.h>
37 #include <sys/socket.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <arpa/inet.h>
44 #include <sys/signal.h>
45
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";
50
51 static char *app = "AgentLogin";
52 static char *app2 = "AgentCallbackLogin";
53
54 static char *synopsis = "Call agent login";
55 static char *synopsis2 = "Call agent callback login";
56
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"
62 "the star key.\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";
65
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"
70 "context. \n";
71
72 static char moh[80] = "default";
73
74 #define AST_MAX_AGENT   80              /* Agent ID or Password max length */
75
76 static int capability = -1;
77
78 static unsigned int group;
79 static int autologoff;
80 static int wrapuptime;
81 static int ackcall;
82
83 static int usecnt =0;
84 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
85
86 /* Protect the interface list (of sip_pvt's) */
87 static ast_mutex_t agentlock = AST_MUTEX_INITIALIZER;
88
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 */
109         char loginchan[80];
110         struct ast_channel *chan;                       /* Channel we use */
111         struct agent_pvt *next;                         /* Agent */
112 } *agents = NULL;
113
114 #define CHECK_FORMATS(ast, p) do { \
115         if (p->chan) {\
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); \
123                 } \
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); \
128         } \
129 } while(0)
130
131 #define CLEANUP(ast, p) do { \
132         int x; \
133         if (p->chan) { \
134                 for (x=0;x<AST_MAX_FDS;x++) \
135                         ast->fds[x] = p->chan->fds[x]; \
136         } \
137 } while(0)
138
139
140 static void agent_unlink(struct agent_pvt *agent)
141 {
142         struct agent_pvt *p, *prev;
143         prev = NULL;
144         p = agents;
145         while(p) {
146                 if (p == agent) {
147                         if (prev)
148                                 prev->next = agent->next;
149                         else
150                                 agents = agent->next;
151                         break;
152                 }
153                 prev = p;
154                 p = p->next;
155         }
156 }
157
158 static struct agent_pvt *add_agent(char *agent, int pending)
159 {
160         char tmp[256];
161         char *password=NULL, *name=NULL;
162         struct agent_pvt *p, *prev;
163         
164         strncpy(tmp, agent, sizeof(tmp));
165         if ((password = strchr(tmp, ','))) {
166                 *password = '\0';
167                 password++;
168                 while (*password < 33) password++;
169         }
170         if (password && (name = strchr(password, ','))) {
171                 *name = '\0';
172                 name++;
173                 while (*name < 33) name++; 
174         }
175         prev=NULL;
176         p = agents;
177         while(p) {
178                 if (!pending && !strcmp(p->agent, tmp))
179                         break;
180                 prev = p;
181                 p = p->next;
182         }
183         if (!p) {
184                 p = malloc(sizeof(struct agent_pvt));
185                 if (p) {
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 );
190                         p->owning_app = -1;
191                         p->app_sleep_cond = 1;
192                         p->group = group;
193                         p->pending = pending;
194                         p->next = NULL;
195                         if (prev)
196                                 prev->next = p;
197                         else
198                                 agents = p;
199                         
200                 }
201         }
202         if (!p)
203                 return NULL;
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;
210         if (pending)
211                 p->dead = 1;
212         else
213                 p->dead = 0;
214         return p;
215 }
216
217 static int agent_cleanup(struct agent_pvt *p)
218 {
219         struct ast_channel *chan = p->owner;
220         p->owner = NULL;
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);
225         if (chan)
226                 ast_channel_free(chan);
227         if (p->dead)
228                 free(p);
229         return 0;
230 }
231
232 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
233
234 static int agent_answer(struct ast_channel *ast)
235 {
236         ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
237         return -1;
238 }
239
240 static struct ast_frame  *agent_read(struct ast_channel *ast)
241 {
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);
248         if (p->chan) {
249                 p->chan->exception = ast->exception;
250                 p->chan->fdno = ast->fdno;
251                 f = ast_read(p->chan);
252         } else
253                 f = &null_frame;
254         if (!f) {
255                 /* If there's a channel, hang it up  (if it's on a callback) make it NULL */
256                 if (p->chan) {
257                         if (strlen(p->loginchan))
258                                 ast_hangup(p->chan);
259                         p->chan = NULL;
260                         p->acknowledged = 0;
261                 }
262         }
263         if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
264 /* TC */
265                 if (p->ackcall) {
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 */
269                         ast_frfree(f);
270                         f = &null_frame;
271                 }
272         else {
273                         p->acknowledged = 1;
274                         f = &answer_frame;
275         }
276         }
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);
281                         p->acknowledged = 1;
282                         ast_frfree(f);
283                         f = &answer_frame;
284                 }
285         }
286         if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
287                 /* * terminates call */
288                 ast_frfree(f);
289                 f = NULL;
290         }
291         CLEANUP(ast,p);
292         ast_mutex_unlock(&p->lock);
293         return f;
294 }
295
296 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
297 {
298         struct agent_pvt *p = ast->pvt->pvt;
299         int res = -1;
300         CHECK_FORMATS(ast, p);
301         ast_mutex_lock(&p->lock);
302         if (p->chan) {
303                 if ((f->frametype != AST_FRAME_VOICE) ||
304                         (f->subclass == p->chan->writeformat)) {
305                         res = ast_write(p->chan, f);
306                 } else {
307                         ast_log(LOG_DEBUG, "Dropping one incompatible voice frame on '%s' to '%s'\n", ast->name, p->chan->name);
308                         res = 0;
309                 }
310         } else
311                 res = 0;
312         CLEANUP(ast, p);
313         ast_mutex_unlock(&p->lock);
314         return res;
315 }
316
317 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
318 {
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);
324                 return -1;
325         }
326         p->owner = newchan;
327         ast_mutex_unlock(&p->lock);
328         return 0;
329 }
330
331 static int agent_indicate(struct ast_channel *ast, int condition)
332 {
333         struct agent_pvt *p = ast->pvt->pvt;
334         int res = -1;
335         ast_mutex_lock(&p->lock);
336         if (p->chan)
337                 res = ast_indicate(p->chan, condition);
338         else
339                 res = 0;
340         ast_mutex_unlock(&p->lock);
341         return res;
342 }
343
344 static int agent_digit(struct ast_channel *ast, char digit)
345 {
346         struct agent_pvt *p = ast->pvt->pvt;
347         int res = -1;
348         ast_mutex_lock(&p->lock);
349         if (p->chan)
350                 res = p->chan->pvt->send_digit(p->chan, digit);
351         else
352                 res = 0;
353         ast_mutex_unlock(&p->lock);
354         return res;
355 }
356
357 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
358 {
359         struct agent_pvt *p = ast->pvt->pvt;
360         int res = -1;
361         ast_mutex_lock(&p->lock);
362         if (!p->chan) {
363                 if (p->pending) {
364                         ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
365                         ast_setstate(ast, AST_STATE_DIALING);
366                         res = 0;
367                 } else {
368                         ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call...  what are the odds of that?\n");
369                         res = -1;
370                 }
371                 ast_mutex_unlock(&p->lock);
372                 return res;
373         } else if (strlen(p->loginchan)) {
374                 time(&p->start);
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);
380                 if (ast->callerid)
381                         p->chan->callerid = strdup(ast->callerid);
382                 else
383                         p->chan->callerid = NULL;
384                 res = ast_call(p->chan, p->loginchan, 0);
385                 CLEANUP(ast,p);
386                 ast_mutex_unlock(&p->lock);
387                 return res;
388         }
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);
393         if (!res) {
394                 res = ast_waitstream(p->chan, "");
395                 ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
396         }
397         if (!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);
400                 if (res)
401                         ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
402         } else {
403                 // Agent hung-up
404                 p->chan = NULL;
405         }
406
407         if (!res) {
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);
410                 if (res)
411                         ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
412         }
413         if( !res )
414         {
415                 /* Call is immediately up */
416                 ast_setstate(ast, AST_STATE_UP);
417         }
418         CLEANUP(ast,p);
419         ast_mutex_unlock(&p->lock);
420         return res;
421 }
422
423 static int agent_hangup(struct ast_channel *ast)
424 {
425         struct agent_pvt *p = ast->pvt->pvt;
426         int howlong = 0;
427         ast_mutex_lock(&p->lock);
428         p->owner = NULL;
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;
433         time(&p->start);
434         if (p->chan) {
435                 /* If they're dead, go ahead and hang up on the agent now */
436                 if (strlen(p->loginchan)) {
437                         p->acknowledged = 0;
438                         if (p->chan) {
439                                 /* Recognize the hangup and pass it along immediately */
440                                 ast_hangup(p->chan);
441                                 p->chan = NULL;
442                         }
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, "");
447                         }
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);
452                 } else {
453                         ast_mutex_lock(&p->chan->lock);
454                         ast_moh_start(p->chan, p->moh);
455                         ast_mutex_unlock(&p->chan->lock);
456                 }
457         }
458 #if 0
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);
467         } else {
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);
471         }
472 #endif  
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);
476
477         if (p->pending) {
478                 ast_mutex_lock(&agentlock);
479                 agent_unlink(p);
480                 ast_mutex_unlock(&agentlock);
481         }
482         if (p->abouttograb) {
483                 /* Let the "about to grab" thread know this isn't valid anymore, and let it
484                    kill it later */
485                 p->abouttograb = 0;
486         } else if (p->dead) {
487                 free(p);
488         } else if (p->chan) {
489                 /* Not dead -- check availability now */
490                 ast_mutex_lock(&p->lock);
491                 /* Store last disconnect time */
492                 gettimeofday(&p->lastdisc, NULL);
493                 ast_mutex_unlock(&p->lock);
494         } else if (strlen(p->loginchan)) {
495                 if (!p->wrapuptime)
496                         check_availability(p, 1);
497                 else {
498                         /* XXX Need to add support for wrapuptime on callback agents */
499                 }
500         }
501         return 0;
502 }
503
504 static int agent_cont_sleep( void *data )
505 {
506         struct agent_pvt *p;
507         struct timeval tv;
508         int res;
509
510         p = (struct agent_pvt *)data;
511
512         ast_mutex_lock(&p->lock);
513         res = p->app_sleep_cond;
514         if (p->lastdisc.tv_sec) {
515                 gettimeofday(&tv, NULL);
516                 if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 + 
517                         (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) 
518                         res = 1;
519         }
520         ast_mutex_unlock(&p->lock);
521 #if 0
522         if( !res )
523                 ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
524 #endif          
525         return res;
526 }
527
528 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
529 {
530         struct ast_channel *tmp;
531         struct ast_frame null_frame = { AST_FRAME_NULL };
532 #if 0
533         if (!p->chan) {
534                 ast_log(LOG_WARNING, "No channel? :(\n");
535                 return NULL;
536         }
537 #endif  
538         tmp = ast_channel_alloc(0);
539         if (tmp) {
540                 if (p->chan) {
541                         tmp->nativeformats = p->chan->nativeformats;
542                         tmp->writeformat = p->chan->writeformat;
543                         tmp->pvt->rawwriteformat = p->chan->writeformat;
544                         tmp->readformat = p->chan->readformat;
545                         tmp->pvt->rawreadformat = p->chan->readformat;
546                         strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
547                         strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
548                         strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
549                 } else {
550                         tmp->nativeformats = AST_FORMAT_SLINEAR;
551                         tmp->writeformat = AST_FORMAT_SLINEAR;
552                         tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
553                         tmp->readformat = AST_FORMAT_SLINEAR;
554                         tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
555                 }
556                 if (p->pending)
557                         snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
558                 else
559                         snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
560                 tmp->type = type;
561                 ast_setstate(tmp, state);
562                 tmp->pvt->pvt = p;
563                 tmp->pvt->send_digit = agent_digit;
564                 tmp->pvt->call = agent_call;
565                 tmp->pvt->hangup = agent_hangup;
566                 tmp->pvt->answer = agent_answer;
567                 tmp->pvt->read = agent_read;
568                 tmp->pvt->write = agent_write;
569                 tmp->pvt->exception = agent_read;
570                 tmp->pvt->indicate = agent_indicate;
571                 tmp->pvt->fixup = agent_fixup;
572                 p->owner = tmp;
573                 ast_mutex_lock(&usecnt_lock);
574                 usecnt++;
575                 ast_mutex_unlock(&usecnt_lock);
576                 ast_update_use_count();
577                 tmp->priority = 1;
578                 /* Wake up and wait for other applications (by definition the login app)
579                  * to release this channel). Takes ownership of the agent channel
580                  * to this thread only.
581                  * For signalling the other thread, ast_queue_frame is used until we
582                  * can safely use signals for this purpose. The pselect() needs to be
583                  * implemented in the kernel for this.
584                  */
585                 p->app_sleep_cond = 0;
586                 if( ast_mutex_trylock(&p->app_lock) )
587                 {
588                         if (p->chan) {
589                                 ast_queue_frame(p->chan, &null_frame, 1);
590                                 ast_mutex_unlock(&p->lock);     /* For other thread to read the condition. */
591                                 ast_mutex_lock(&p->app_lock);
592                                 ast_mutex_lock(&p->lock);
593                         }
594                         if( !p->chan )
595                         {
596                                 ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
597                                 p->owner = NULL;
598                                 tmp->pvt->pvt = NULL;
599                                 p->app_sleep_cond = 1;
600                                 ast_channel_free( tmp );
601                                 ast_mutex_unlock(&p->lock);     /* For other thread to read the condition. */
602                                 ast_mutex_unlock(&p->app_lock);
603                                 return NULL;
604                         }
605                 }
606                 p->owning_app = pthread_self();
607                 /* After the above step, there should not be any blockers. */
608                 if (p->chan) {
609                         if (p->chan->blocking) {
610                                 ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
611                                 CRASH;
612                         }
613                         ast_moh_stop(p->chan);
614                 }
615         } else
616                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
617         return tmp;
618 }
619
620
621 static int read_agent_config(void)
622 {
623         struct ast_config *cfg;
624         struct ast_variable *v;
625         struct agent_pvt *p, *pl, *pn;
626         group = 0;
627         autologoff = 0;
628         wrapuptime = 0;
629         ackcall = 1;
630         cfg = ast_load(config);
631         if (!cfg) {
632                 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
633                 return 0;
634         }
635         ast_mutex_lock(&agentlock);
636         p = agents;
637         while(p) {
638                 p->dead = 1;
639                 p = p->next;
640         }
641         strcpy(moh, "default");
642         v = ast_variable_browse(cfg, "agents");
643         while(v) {
644                 /* Create the interface list */
645                 if (!strcasecmp(v->name, "agent")) {
646                         add_agent(v->value, 0);
647                 } else if (!strcasecmp(v->name, "group")) {
648                         group = ast_get_group(v->value);
649                 } else if (!strcasecmp(v->name, "autologoff")) {
650                         autologoff = atoi(v->value);
651                         if (autologoff < 0)
652                                 autologoff = 0;
653                 } else if (!strcasecmp(v->name, "ackcall")) {
654                         ackcall = ast_true(v->value);
655                 } else if (!strcasecmp(v->name, "wrapuptime")) {
656                         wrapuptime = atoi(v->value);
657                         if (wrapuptime < 0)
658                                 wrapuptime = 0;
659                 } else if (!strcasecmp(v->name, "musiconhold")) {
660                         strncpy(moh, v->value, sizeof(moh) - 1);
661                 }
662                 v = v->next;
663         }
664         p = agents;
665         pl = NULL;
666         while(p) {
667                 pn = p->next;
668                 if (p->dead) {
669                         /* Unlink */
670                         if (pl)
671                                 pl->next = p->next;
672                         else
673                                 agents = p->next;
674                         /* Destroy if  appropriate */
675                         if (!p->owner) {
676                                 if (!p->chan) {
677                                         free(p);
678                                 } else {
679                                         /* Cause them to hang up */
680                                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
681                                 }
682                         }
683                 } else
684                         pl = p;
685                 p = pn;
686         }
687         ast_mutex_unlock(&agentlock);
688         ast_destroy(cfg);
689         return 0;
690 }
691
692 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
693 {
694         struct ast_channel *chan=NULL, *parent=NULL;
695         struct agent_pvt *p;
696         int res;
697         ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
698         if (needlock)
699                 ast_mutex_lock(&agentlock);
700         p = agents;
701         while(p) {
702                 if (p == newlyavailable) {
703                         p = p->next;
704                         continue;
705                 }
706                 ast_mutex_lock(&p->lock);
707                 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
708                         ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
709                         /* We found a pending call, time to merge */
710                         if (strlen(newlyavailable->loginchan)) {
711                                 /* Adjustable agent */
712                                 newlyavailable->chan = ast_request("Local", AST_FORMAT_SLINEAR, newlyavailable->loginchan);
713                                 if (newlyavailable->chan) {
714                                         if (ast_call(newlyavailable->chan, newlyavailable->loginchan, 0))
715                                                 ast_log(LOG_WARNING, "Call failed on channel '%s'\n", newlyavailable->chan->name);
716                                         chan = agent_new(p, AST_STATE_DOWN);
717                                 } else {
718                                         ast_log(LOG_WARNING, "I didn't expect to ever get here...\n");
719                                         ast_mutex_unlock(&p->lock);
720                                         p = p->next;
721                                         continue;
722                                 }
723                         } else 
724                                 chan = agent_new(newlyavailable, AST_STATE_DOWN);
725                         parent = p->owner;
726                         p->abouttograb = 1;
727                         ast_mutex_unlock(&p->lock);
728                         break;
729                 }
730                 ast_mutex_unlock(&p->lock);
731                 p = p->next;
732         }
733         if (needlock)
734                 ast_mutex_unlock(&agentlock);
735         if (parent && chan)  {
736                 ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
737                 res = ast_streamfile(newlyavailable->chan, "beep", newlyavailable->chan->language);
738                 ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
739                 if (!res) {
740                         res = ast_waitstream(newlyavailable->chan, "");
741                         ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
742                 }
743                 if (!res) {
744                         /* Note -- parent may have disappeared */
745                         if (p->abouttograb) {
746                                 ast_setstate(parent, AST_STATE_UP);
747                                 ast_setstate(chan, AST_STATE_UP);
748                                 /* Go ahead and mark the channel as a zombie so that masquerade will
749                                    destroy it for us, and we need not call ast_hangup */
750                                 ast_mutex_lock(&parent->lock);
751                                 chan->zombie = 1;
752                                 ast_channel_masquerade(parent, chan);
753                                 ast_mutex_unlock(&parent->lock);
754                                 p->abouttograb = 0;
755                         } else {
756                                 ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
757                                 agent_cleanup(newlyavailable);
758                         }
759                 } else {
760                         ast_log(LOG_DEBUG, "Ugh...  Agent hung up at exactly the wrong time\n");
761                         agent_cleanup(newlyavailable);
762                 }
763         }
764         return 0;
765 }
766
767 static struct ast_channel *agent_request(char *type, int format, void *data)
768 {
769         struct agent_pvt *p;
770         struct ast_channel *chan = NULL;
771         char *s;
772         unsigned int groupmatch;
773         int waitforagent=0;
774         int hasagent = 0;
775         s = data;
776         if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
777                 groupmatch = (1 << groupmatch);
778         } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
779                 groupmatch = (1 << groupmatch);
780                 waitforagent = 1;
781         } else {
782                 groupmatch = 0;
783         }
784
785         /* Check actual logged in agents first */
786         ast_mutex_lock(&agentlock);
787         p = agents;
788         while(p) {
789                 ast_mutex_lock(&p->lock);
790                 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
791                                 !strlen(p->loginchan)) {
792                         if (p->chan)
793                                 hasagent++;
794                         if (!p->lastdisc.tv_sec) {
795                                 /* Agent must be registered, but not have any active call, and not be in a waiting state */
796                                 if (!p->owner && p->chan) {
797                                         /* Fixed agent */
798                                         chan = agent_new(p, AST_STATE_DOWN);
799                                 }
800                                 if (chan) {
801                                         ast_mutex_unlock(&p->lock);
802                                         break;
803                                 }
804                         }
805                 }
806                 ast_mutex_unlock(&p->lock);
807                 p = p->next;
808         }
809         if (!p) {
810                 p = agents;
811                 while(p) {
812                         ast_mutex_lock(&p->lock);
813                         if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
814                                 if (p->chan || strlen(p->loginchan))
815                                         hasagent++;
816                                 if (!p->lastdisc.tv_sec) {
817                                         /* Agent must be registered, but not have any active call, and not be in a waiting state */
818                                         if (!p->owner && p->chan) {
819                                                 /* Could still get a fixed agent */
820                                                 chan = agent_new(p, AST_STATE_DOWN);
821                                         } else if (!p->owner && strlen(p->loginchan)) {
822                                                 /* Adjustable agent */
823                                                 p->chan = ast_request("Local", format, p->loginchan);
824                                                 if (p->chan)
825                                                         chan = agent_new(p, AST_STATE_DOWN);
826                                         }
827                                         if (chan) {
828                                                 ast_mutex_unlock(&p->lock);
829                                                 break;
830                                         }
831                                 }
832                         }
833                         ast_mutex_unlock(&p->lock);
834                         p = p->next;
835                 }
836         }
837
838         if (!chan && waitforagent) {
839                 /* No agent available -- but we're requesting to wait for one.
840                    Allocate a place holder */
841                 if (hasagent) {
842                         ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
843                         p = add_agent(data, 1);
844                         p->group = groupmatch;
845                         chan = agent_new(p, AST_STATE_DOWN);
846                         if (!chan) {
847                                 ast_log(LOG_WARNING, "Weird...  Fix this to drop the unused pending agent\n");
848                         }
849                 } else
850                         ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s);
851         }
852         ast_mutex_unlock(&agentlock);
853         return chan;
854 }
855
856 static int powerof(unsigned int v)
857 {
858         int x;
859         for (x=0;x<32;x++) {
860                 if (v & (1 << x)) return x;
861         }
862         return 0;
863 }
864
865 static int agents_show(int fd, int argc, char **argv)
866 {
867         struct agent_pvt *p;
868         char username[256];
869         char location[256];
870         char talkingto[256];
871         char moh[256];
872
873         if (argc != 2)
874                 return RESULT_SHOWUSAGE;
875         ast_mutex_lock(&agentlock);
876         p = agents;
877         while(p) {
878                 ast_mutex_lock(&p->lock);
879                 if (p->pending) {
880                         if (p->group)
881                                 ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
882                         else
883                                 ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
884                 } else {
885                         if (strlen(p->name))
886                                 snprintf(username, sizeof(username), "(%s) ", p->name);
887                         else
888                                 strcpy(username, "");
889                         if (p->chan) {
890                                 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
891                                 if (p->owner && p->owner->bridge) {
892                                         snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
893                                 } else {
894                                         strcpy(talkingto, " is idle");
895                                 }
896                         } else if (strlen(p->loginchan)) {
897                                 snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
898                                 strcpy(talkingto, "");
899                                 if (p->acknowledged)
900                                         strcat(location, " (Confirmed)");
901                         } else {
902                                 strcpy(location, "not logged in");
903                                 strcpy(talkingto, "");
904                         }
905                         if (strlen(p->moh))
906                                 snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
907                         ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent, 
908                                         username, location, talkingto, moh);
909                 }
910                 ast_mutex_unlock(&p->lock);
911                 p = p->next;
912         }
913         ast_mutex_unlock(&agentlock);
914         return RESULT_SUCCESS;
915 }
916
917 static char show_agents_usage[] = 
918 "Usage: show agents\n"
919 "       Provides summary information on agents.\n";
920
921 static struct ast_cli_entry cli_show_agents = {
922         { "show", "agents", NULL }, agents_show, 
923         "Show status of agents", show_agents_usage, NULL };
924
925 STANDARD_LOCAL_USER;
926 LOCAL_USER_DECL;
927
928 static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
929 {
930         int res=0;
931         int tries = 0;
932         struct agent_pvt *p;
933         struct localuser *u;
934         struct timeval tv;
935         char user[AST_MAX_AGENT];
936         char pass[AST_MAX_AGENT];
937         char xpass[AST_MAX_AGENT] = "";
938         char *errmsg;
939         char info[512];
940         char *opt_user = NULL;
941         char *options = NULL;
942         char *context = NULL;
943         char *exten = NULL;
944         int play_announcement;
945         char *filename = "agent-loginok";
946         
947         LOCAL_USER_ADD(u);
948
949         /* Parse the arguments XXX Check for failure XXX */
950         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
951         opt_user = info;
952         if( opt_user ) {
953                 options = strchr(opt_user, '|');
954                 if (options) {
955                         *options = '\0';
956                         options++;
957                         if (callbackmode) {
958                                 context = strchr(options, '@');
959                                 if (context) {
960                                         *context = '\0';
961                                         context++;
962                                 }
963                                 exten = options;
964                                 while(*exten && ((*exten < '0') || (*exten > '9'))) exten++;
965                                 if (!*exten)
966                                         exten = NULL;
967                         }
968                 }
969         }
970
971         if (chan->_state != AST_STATE_UP)
972                 res = ast_answer(chan);
973         if (!res) {
974                 if( opt_user && strlen(opt_user))
975                         strncpy( user, opt_user, AST_MAX_AGENT );
976                 else
977                         res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
978         }
979         while (!res && (tries < 3)) {
980                 /* Check for password */
981                 ast_mutex_lock(&agentlock);
982                 p = agents;
983                 while(p) {
984                         if (!strcmp(p->agent, user) && !p->pending)
985                                 strncpy(xpass, p->password, sizeof(xpass) - 1);
986                         p = p->next;
987                 }
988                 ast_mutex_unlock(&agentlock);
989                 if (!res) {
990                         if (strlen(xpass))
991                                 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
992                         else
993                                 strcpy(pass, "");
994                 }
995                 errmsg = "agent-incorrect";
996
997 #if 0
998                 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
999 #endif          
1000
1001                 /* Check again for accuracy */
1002                 ast_mutex_lock(&agentlock);
1003                 p = agents;
1004                 while(p) {
1005                         ast_mutex_lock(&p->lock);
1006                         if (!strcmp(p->agent, user) &&
1007                                 !strcmp(p->password, pass) && !p->pending) {
1008                                         if (!p->chan) {
1009                                                 if (callbackmode) {
1010                                                         char tmpchan[256] = "";
1011                                                         int pos = 0;
1012                                                         /* Retrieve login chan */
1013                                                         for (;;) {
1014                                                                 if (exten) {
1015                                                                         strncpy(tmpchan, exten, sizeof(tmpchan) - 1);
1016                                                                         res = 0;
1017                                                                 } else
1018                                                                         res = ast_app_getdata(chan, "agent-newlocation", tmpchan+pos, sizeof(tmpchan) - 2, 0);
1019                                                                 if (!strlen(tmpchan) || ast_exists_extension(chan, context && strlen(context) ? context : "default", tmpchan,
1020                                                                                         1, NULL))
1021                                                                         break;
1022                                                                 if (exten) {
1023                                                                         ast_log(LOG_WARNING, "Extension '%s' is not valid for automatic login of agent '%s'\n", exten, p->agent);
1024                                                                         exten = NULL;
1025                                                                         pos = 0;
1026                                                                 } else {
1027                                                                         res = ast_streamfile(chan, "invalid", chan->language);
1028                                                                         if (!res)
1029                                                                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
1030                                                                         if (res > 0) {
1031                                                                                 tmpchan[0] = res;
1032                                                                                 tmpchan[1] = '\0';
1033                                                                                 pos = 1;
1034                                                                         } else {
1035                                                                                 tmpchan[0] = '\0';
1036                                                                                 pos = 0;
1037                                                                         }
1038                                                                 }
1039                                                         }
1040                                                         if (!res) {
1041                                                                 if (context && strlen(context) && strlen(tmpchan))
1042                                                                         snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
1043                                                                 else
1044                                                                         strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
1045                                                                 if (!strlen(p->loginchan))
1046                                                                         filename = "agent-loggedoff";
1047                                                                 p->acknowledged = 0;
1048                                                         }
1049                                                 } else {
1050                                                         strcpy(p->loginchan, "");
1051                                                         p->acknowledged = 0;
1052                                                 }
1053                                                 play_announcement = 1;
1054                                                 if( options )
1055                                                         if( strchr( options, 's' ) )
1056                                                                 play_announcement = 0;
1057                                                 if( !res && play_announcement )
1058                                                         res = ast_streamfile(chan, filename, chan->language);
1059                                                 if (!res)
1060                                                         ast_waitstream(chan, "");
1061                                                 if (!res) {
1062                                                         res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
1063                                                         if (res)
1064                                                                 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
1065                                                 }
1066                                                 if (!res) {
1067                                                         ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
1068                                                         if (res)
1069                                                                 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
1070                                                 }
1071                                                 /* Check once more just in case */
1072                                                 if (p->chan)
1073                                                         res = -1;
1074                                                 if (callbackmode && !res) {
1075                                                         /* Just say goodbye and be done with it */
1076                                                         if (!res)
1077                                                                 res = ast_safe_sleep(chan, 500);
1078                                                         res = ast_streamfile(chan, "vm-goodbye", chan->language);
1079                                                         if (!res)
1080                                                                 res = ast_waitstream(chan, "");
1081                                                         if (!res)
1082                                                                 res = ast_safe_sleep(chan, 1000);
1083                                                         ast_mutex_unlock(&p->lock);
1084                                                         ast_mutex_unlock(&agentlock);
1085                                                 } else if (!res) {
1086 #ifdef HONOR_MUSIC_CLASS
1087                                                         /* check if the moh class was changed with setmusiconhold */
1088                                                         if (*(chan->musicclass))
1089                                                                 strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
1090 #endif                                                          
1091                                                         ast_moh_start(chan, p->moh);
1092                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogin",
1093                                                                 "Agent: %s\r\n"
1094                                                                 "Channel: %s\r\n",
1095                                                                 p->agent, chan->name);
1096                                                         if (option_verbose > 2)
1097                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %s/%s)\n", p->agent,
1098                                                                                                 ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
1099                                                         /* Login this channel and wait for it to
1100                                                            go away */
1101                                                         p->chan = chan;
1102                                                         p->acknowledged = 1;
1103                                                         check_availability(p, 0);
1104                                                         ast_mutex_unlock(&p->lock);
1105                                                         ast_mutex_unlock(&agentlock);
1106                                                         while (res >= 0) {
1107                                                                 ast_mutex_lock(&p->lock);
1108                                                                 if (p->chan != chan)
1109                                                                         res = -1;
1110                                                                 ast_mutex_unlock(&p->lock);
1111                                                                 /* Yield here so other interested threads can kick in. */
1112                                                                 sched_yield();
1113                                                                 if (res)
1114                                                                         break;
1115
1116                                                                 ast_mutex_lock(&p->lock);
1117                                                                 if (p->lastdisc.tv_sec) {
1118                                                                         gettimeofday(&tv, NULL);
1119                                                                         if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 + 
1120                                                                                 (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
1121                                                                                         ast_log(LOG_DEBUG, "Wrapup time expired!\n");
1122                                                                                 memset(&p->lastdisc, 0, sizeof(p->lastdisc));
1123                                                                                 check_availability(p, 1);
1124                                                                         }
1125                                                                 }
1126                                                                 ast_mutex_unlock(&p->lock);
1127                                                                 /*      Synchronize channel ownership between call to agent and itself. */
1128                                                                 ast_mutex_lock( &p->app_lock );
1129                                                                 ast_mutex_lock(&p->lock);
1130                                                                 p->owning_app = pthread_self();
1131                                                                 ast_mutex_unlock(&p->lock);
1132                                                                 res = ast_safe_sleep_conditional( chan, 1000,
1133                                                                                                                 agent_cont_sleep, p );
1134                                                                 ast_mutex_unlock( &p->app_lock );
1135                                                                 sched_yield();
1136                                                         }
1137                                                         ast_mutex_lock(&p->lock);
1138                                                         if (res && p->owner) 
1139                                                                 ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
1140                                                         /* Log us off if appropriate */
1141                                                         if (p->chan == chan)
1142                                                                 p->chan = NULL;
1143                                                         p->acknowledged = 0;
1144                                                         ast_mutex_unlock(&p->lock);
1145                                                         if (option_verbose > 2)
1146                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
1147                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
1148                                                                 "Agent: %s\r\n",
1149                                                                 p->agent);
1150                                                         /* If there is no owner, go ahead and kill it now */
1151                                                         if (p->dead && !p->owner)
1152                                                                 free(p);
1153                                                 }
1154                                                 else {
1155                                                         ast_mutex_unlock(&p->lock);
1156                                                         p = NULL;
1157                                                 }
1158                                                 res = -1;
1159                                         } else {
1160                                                 ast_mutex_unlock(&p->lock);
1161                                                 errmsg = "agent-alreadyon";
1162                                                 p = NULL;
1163                                         }
1164                                         break;
1165                         }
1166                         ast_mutex_unlock(&p->lock);
1167                         p = p->next;
1168                 }
1169                 if (!p)
1170                         ast_mutex_unlock(&agentlock);
1171
1172                 if (!res)
1173                         res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
1174         }
1175                 
1176         LOCAL_USER_REMOVE(u);
1177         /* Always hangup */
1178         return -1;
1179 }
1180
1181 static int login_exec(struct ast_channel *chan, void *data)
1182 {
1183         return __login_exec(chan, data, 0);
1184 }
1185
1186 static int callback_exec(struct ast_channel *chan, void *data)
1187 {
1188         return __login_exec(chan, data, 1);
1189 }
1190
1191 int load_module()
1192 {
1193         /* Make sure we can register our sip channel type */
1194         if (ast_channel_register(type, tdesc, capability, agent_request)) {
1195                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1196                 return -1;
1197         }
1198         ast_register_application(app, login_exec, synopsis, descrip);
1199         ast_register_application(app2, callback_exec, synopsis2, descrip2);
1200         ast_cli_register(&cli_show_agents);
1201         /* Read in the config */
1202         read_agent_config();
1203         return 0;
1204 }
1205
1206 int reload()
1207 {
1208         read_agent_config();
1209         return 0;
1210 }
1211
1212 int unload_module()
1213 {
1214         struct agent_pvt *p;
1215         /* First, take us out of the channel loop */
1216         ast_cli_unregister(&cli_show_agents);
1217         ast_unregister_application(app);
1218         ast_unregister_application(app2);
1219         ast_channel_unregister(type);
1220         if (!ast_mutex_lock(&agentlock)) {
1221                 /* Hangup all interfaces if they have an owner */
1222                 p = agents;
1223                 while(p) {
1224                         if (p->owner)
1225                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1226                         p = p->next;
1227                 }
1228                 agents = NULL;
1229                 ast_mutex_unlock(&agentlock);
1230         } else {
1231                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1232                 return -1;
1233         }               
1234         return 0;
1235 }
1236
1237 int usecount()
1238 {
1239         int res;
1240         ast_mutex_lock(&usecnt_lock);
1241         res = usecnt;
1242         ast_mutex_unlock(&usecnt_lock);
1243         return res;
1244 }
1245
1246 char *key()
1247 {
1248         return ASTERISK_GPL_KEY;
1249 }
1250
1251 char *description()
1252 {
1253         return desc;
1254 }
1255