Version 0.2.0 from FTP
[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():\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
61 static char moh[80] = "default";
62
63 #define AST_MAX_AGENT   80              /* Agent ID or Password max length */
64
65 static int capability = -1;
66
67 static int usecnt =0;
68 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
69
70 /* Protect the interface list (of sip_pvt's) */
71 static pthread_mutex_t agentlock = AST_MUTEX_INITIALIZER;
72
73 static struct agent_pvt {
74         pthread_mutex_t lock;                           /* Channel private lock */
75         int dead;                                                       /* Poised for destruction? */
76         char moh[80];                                           /* Which music on hold */
77         char agent[AST_MAX_AGENT];                      /* Agent ID */
78         char password[AST_MAX_AGENT];           /* Password for Agent login */
79         char name[AST_MAX_AGENT];
80         struct ast_channel *owner;                      /* Agent */
81         struct ast_channel *chan;                       /* Channel we use */
82         struct agent_pvt *next;                         /* Agent */
83 } *agents = NULL;
84
85 #define CLEANUP(ast, p) do { \
86         int x; \
87         if (p->chan) { \
88                 for (x=0;x<AST_MAX_FDS;x++) \
89                         ast->fds[x] = p->chan->fds[x]; \
90         } \
91 } while(0)
92
93
94 static int add_agent(struct ast_variable *var)
95 {
96         char tmp[256];
97         char *password=NULL, *name=NULL;
98         struct agent_pvt *p;
99         
100         strncpy(tmp, var->value, sizeof(tmp));
101         if ((password = strchr(tmp, ','))) {
102                 *password = '\0';
103                 password++;
104                 while (*password < 33) password++;
105         }
106         if (password && (name = strchr(password, ','))) {
107                 *name = '\0';
108                 name++;
109                 while (*name < 33) name++; 
110         }
111         p = agents;
112         while(p) {
113                 if (!strcmp(p->agent, tmp))
114                         break;
115                 p = p->next;
116         }
117         if (!p) {
118                 p = malloc(sizeof(struct agent_pvt));
119                 if (p) {
120                         memset(p, 0, sizeof(struct agent_pvt));
121                         strncpy(p->agent, tmp, sizeof(p->agent) -1);
122                         p->next = agents;
123                         agents = p;
124                         
125                 }
126         }
127         if (!p)
128                 return -1;
129         strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
130         strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
131         strncpy(p->moh, moh, sizeof(p->moh) - 1);
132         p->dead = 0;
133         return 0;
134 }
135
136 static int agent_answer(struct ast_channel *ast)
137 {
138         ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
139         return -1;
140 }
141
142 static struct ast_frame  *agent_read(struct ast_channel *ast)
143 {
144         struct agent_pvt *p = ast->pvt->pvt;
145         struct ast_frame *f = NULL;
146         ast_pthread_mutex_lock(&p->lock);
147         if (p->chan)
148                 f = ast_read(p->chan);
149         if (!f) {
150                 /* If there's a channel, make it NULL */
151                 if (p->chan)
152                         p->chan = NULL;
153         }
154         if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
155                 /* * terminates call */
156                 ast_frfree(f);
157                 f = NULL;
158         }
159         CLEANUP(ast,p);
160         ast_pthread_mutex_unlock(&p->lock);
161         return f;
162 }
163
164 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
165 {
166         struct agent_pvt *p = ast->pvt->pvt;
167         int res = -1;
168         ast_pthread_mutex_lock(&p->lock);
169         if (p->chan)
170                 res = ast_write(p->chan, f);
171         CLEANUP(ast, p);
172         ast_pthread_mutex_unlock(&p->lock);
173         return res;
174 }
175
176 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
177 {
178         struct agent_pvt *p = newchan->pvt->pvt;
179         ast_pthread_mutex_lock(&p->lock);
180         if (p->owner != oldchan) {
181                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
182                 return -1;
183         }
184         p->owner = newchan;
185         ast_pthread_mutex_unlock(&p->lock);
186         return 0;
187 }
188
189 static int agent_indicate(struct ast_channel *ast, int condition)
190 {
191         struct agent_pvt *p = ast->pvt->pvt;
192         int res = -1;
193         ast_pthread_mutex_lock(&p->lock);
194         if (p->chan)
195                 res = ast_indicate(p->chan, condition);
196         ast_pthread_mutex_unlock(&p->lock);
197         return res;
198 }
199
200 static int agent_digit(struct ast_channel *ast, char digit)
201 {
202         struct agent_pvt *p = ast->pvt->pvt;
203         int res = -1;
204         ast_pthread_mutex_lock(&p->lock);
205         if (p->chan)
206                 res = p->chan->pvt->send_digit(p->chan, digit);
207         ast_pthread_mutex_unlock(&p->lock);
208         return res;
209 }
210
211 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
212 {
213         struct agent_pvt *p = ast->pvt->pvt;
214         int res = -1;
215         ast_pthread_mutex_lock(&p->lock);
216         res = ast_streamfile(p->chan, "beep", p->chan->language);
217         if (!res)
218                 res = ast_waitstream(p->chan, "");
219         if (!res) {
220                 res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
221                 if (res)
222                         ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
223         }
224         if (!res) {
225                 ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
226                 if (res)
227                         ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
228         }
229         /* Call is immediately up */
230         ast_setstate(ast, AST_STATE_UP);
231         ast_pthread_mutex_unlock(&p->lock);
232         return res;
233 }
234
235 static int agent_hangup(struct ast_channel *ast)
236 {
237         struct agent_pvt *p = ast->pvt->pvt;
238         ast_pthread_mutex_lock(&p->lock);
239         p->owner = NULL;
240         ast->pvt->pvt = NULL;
241         ast_pthread_mutex_unlock(&p->lock);
242         if (p->chan) {
243                 /* If they're dead, go ahead and hang up on the agent now */
244                 if (p->dead)
245                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
246                 ast_moh_start(p->chan, p->moh);
247         } else if (p->dead) 
248                 /* Go ahead and lose it */
249                 free(p);
250         return 0;
251 }
252
253 static struct ast_channel *agent_new(struct agent_pvt *p, int state)
254 {
255         struct ast_channel *tmp;
256         if (!p->chan) {
257                 ast_log(LOG_WARNING, "No channel? :(\n");
258                 return NULL;
259         }
260         tmp = ast_channel_alloc(0);
261         if (tmp) {
262                 tmp->nativeformats = p->chan->nativeformats;
263                 snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
264                 tmp->type = type;
265                 ast_setstate(tmp, state);
266                 tmp->writeformat = p->chan->writeformat;
267                 tmp->pvt->rawwriteformat = p->chan->writeformat;
268                 tmp->readformat = p->chan->readformat;
269                 tmp->pvt->rawreadformat = p->chan->readformat;
270                 tmp->pvt->pvt = p;
271                 tmp->pvt->send_digit = agent_digit;
272                 tmp->pvt->call = agent_call;
273                 tmp->pvt->hangup = agent_hangup;
274                 tmp->pvt->answer = agent_answer;
275                 tmp->pvt->read = agent_read;
276                 tmp->pvt->write = agent_write;
277                 tmp->pvt->exception = agent_read;
278                 tmp->pvt->indicate = agent_indicate;
279                 tmp->pvt->fixup = agent_fixup;
280                 strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
281                 p->owner = tmp;
282                 ast_pthread_mutex_lock(&usecnt_lock);
283                 usecnt++;
284                 ast_pthread_mutex_unlock(&usecnt_lock);
285                 ast_update_use_count();
286                 strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
287                 strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
288                 tmp->priority = 1;
289                 /* Wake up any waiting blockers (by definition the login app) */
290                 if (p->chan->blocking) {
291                         pthread_kill(p->chan->blocker, SIGURG);
292                         /* Wait until the blocker releases it */
293                         while(p->chan->blocking)
294                                 usleep(1000);
295                 }
296                 ast_moh_stop(p->chan);
297         } else
298                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
299         return tmp;
300 }
301
302
303 static int read_agent_config(void)
304 {
305         struct ast_config *cfg;
306         struct ast_variable *v;
307         struct agent_pvt *p, *pl, *pn;
308         cfg = ast_load(config);
309         if (!cfg) {
310                 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
311                 return 0;
312         }
313         ast_pthread_mutex_lock(&agentlock);
314         p = agents;
315         while(p) {
316                 p->dead = 1;
317                 p = p->next;
318         }
319         strcpy(moh, "default");
320         v = ast_variable_browse(cfg, "agents");
321         while(v) {
322                 /* Create the interface list */
323                 if (!strcasecmp(v->name, "agent")) {
324                         add_agent(v);
325                 } else if (!strcasecmp(v->name, "musiconhold")) {
326                         strncpy(moh, v->value, sizeof(moh) - 1);
327                 }
328                 v = v->next;
329         }
330         p = agents;
331         pl = NULL;
332         while(p) {
333                 pn = p->next;
334                 if (p->dead) {
335                         /* Unlink */
336                         if (pl)
337                                 pl->next = p->next;
338                         else
339                                 agents = p->next;
340                         /* Destroy if  appropriate */
341                         if (!p->owner) {
342                                 if (!p->chan) {
343                                         free(p);
344                                 } else {
345                                         /* Cause them to hang up */
346                                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
347                                 }
348                         }
349                 } else
350                         pl = p;
351                 p = pn;
352         }
353         ast_pthread_mutex_unlock(&agentlock);
354         ast_destroy(cfg);
355         return 0;
356 }
357
358 static struct ast_channel *agent_request(char *type, int format, void *data)
359 {
360         struct agent_pvt *p;
361         struct ast_channel *chan = NULL;
362         ast_pthread_mutex_lock(&agentlock);
363         p = agents;
364         while(p) {
365                 if (!strcmp(data, p->agent)) {
366                         ast_pthread_mutex_lock(&p->lock);
367                         /* Agent must be registered, but not have any active call */
368                         if (!p->owner && p->chan) {
369                                 chan = agent_new(p, AST_STATE_DOWN);
370                         }
371                         ast_pthread_mutex_unlock(&p->lock);
372                         break;
373                 }
374                 p = p->next;
375         }
376         ast_pthread_mutex_unlock(&agentlock);
377         return chan;
378 }
379
380 static int agents_show(int fd, int argc, char **argv)
381 {
382         struct agent_pvt *p;
383         char username[256];
384         char location[256];
385         char talkingto[256];
386
387         if (argc != 2)
388                 return RESULT_SHOWUSAGE;
389         ast_pthread_mutex_lock(&agentlock);
390         p = agents;
391         while(p) {
392                 ast_pthread_mutex_lock(&p->lock);
393                 if (strlen(p->name))
394                         snprintf(username, sizeof(username), "(%s) ", p->name);
395                 else
396                         strcpy(username, "");
397                 if (p->chan) {
398                         snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
399                         if (p->owner && p->owner->bridge) {
400                                 snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
401                         } else {
402                                 strcpy(talkingto, " is idle");
403                         }
404                 } else {
405                         strcpy(location, "not logged in");
406                         strcpy(talkingto, "");
407                 }
408                 ast_pthread_mutex_unlock(&p->lock);
409                 ast_cli(fd, "%-12.12s %s%s%s\n", p->agent, 
410                                 username, location, talkingto);
411                 p = p->next;
412         }
413         ast_pthread_mutex_unlock(&agentlock);
414         return RESULT_SUCCESS;
415 }
416
417 static char show_agents_usage[] = 
418 "Usage: show agents\n"
419 "       Provides summary information on agents.\n";
420
421 static struct ast_cli_entry cli_show_agents = {
422         { "show", "agents", NULL }, agents_show, 
423         "Show status of agents", show_agents_usage, NULL };
424
425 STANDARD_LOCAL_USER;
426 LOCAL_USER_DECL;
427
428 static int login_exec(struct ast_channel *chan, void *data)
429 {
430         int res=0;
431         int tries = 0;
432         struct agent_pvt *p;
433         struct localuser *u;
434         char user[AST_MAX_AGENT];
435         char pass[AST_MAX_AGENT];
436         char xpass[AST_MAX_AGENT] = "";
437         char *errmsg;
438         LOCAL_USER_ADD(u);
439         if (chan->_state != AST_STATE_UP)
440                 res = ast_answer(chan);
441         if (!res)
442                 res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
443         while (!res && (tries < 3)) {
444                 /* Check for password */
445                 ast_pthread_mutex_lock(&agentlock);
446                 p = agents;
447                 while(p) {
448                         if (!strcmp(p->agent, user))
449                                 strncpy(xpass, p->password, sizeof(xpass) - 1);
450                         p = p->next;
451                 }
452                 ast_pthread_mutex_unlock(&agentlock);
453                 if (!res) {
454                         if (strlen(xpass) || !p)
455                                 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
456                         else
457                                 strcpy(pass, "");
458                 }
459                 errmsg = "agent-incorrect";
460
461 #if 0
462                 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
463 #endif          
464
465                 /* Check again for accuracy */
466                 ast_pthread_mutex_lock(&agentlock);
467                 p = agents;
468                 while(p) {
469                         ast_pthread_mutex_lock(&p->lock);
470                         if (!strcmp(p->agent, user) &&
471                                 !strcmp(p->password, pass)) {
472                                         if (!p->chan) {
473                                                 res = ast_streamfile(chan, "agent-loginok", chan->language);
474                                                 if (!res)
475                                                         ast_waitstream(chan, "");
476                                                 if (!res) {
477                                                         res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
478                                                         if (res)
479                                                                 ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
480                                                 }
481                                                 if (!res) {
482                                                         ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
483                                                         if (res)
484                                                                 ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
485                                                 }
486                                                 /* Check once more just in case */
487                                                 if (p->chan)
488                                                         res = -1;
489                                                 if (!res) {
490                                                         ast_moh_start(chan, p->moh);
491                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogin",
492                                                                 "Agent: %s\r\n"
493                                                                 "Channel: %s\r\n",
494                                                                 p->agent, chan->name);
495                                                         if (option_verbose > 2)
496                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %d/%d)\n", p->agent,
497                                                                                                 chan->readformat, chan->writeformat);
498                                                         /* Login this channel and wait for it to
499                                                            go away */
500                                                         p->chan = chan;
501                                                         ast_pthread_mutex_unlock(&p->lock);
502                                                         ast_pthread_mutex_unlock(&agentlock);
503                                                         while ((res >= 0) && (p->chan == chan)) {
504                                                                 /* True sleep here, since we're being monitored
505                                                                    elsewhere instead */
506                                                                 if (p->owner) 
507                                                                         sleep(1);
508                                                                 else
509                                                                         res = ast_safe_sleep(chan, 1000);
510                                                         }
511                                                         if (res && p->owner) 
512                                                                 ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
513                                                         /* Log us off if appropriate */
514                                                         if (p->chan == chan)
515                                                                 p->chan = NULL;
516                                                         if (option_verbose > 2)
517                                                                 ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
518                                                         manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
519                                                                 "Agent: %s\r\n",
520                                                                 p->agent);
521                                                         /* If there is no owner, go ahead and kill it now */
522                                                         if (p->dead && !p->owner)
523                                                                 free(p);
524                                                 }
525                                                 res = -1;
526                                         } else {
527                                                 ast_pthread_mutex_unlock(&p->lock);
528                                                 errmsg = "agent-alreadyon";
529                                                 p = NULL;
530                                         }
531                                         break;
532                         }
533                         ast_pthread_mutex_unlock(&p->lock);
534                         p = p->next;
535                 }
536                 if (!p)
537                         ast_pthread_mutex_unlock(&agentlock);
538
539                 if (!res)
540                         res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
541         }
542                 
543         LOCAL_USER_REMOVE(u);
544         /* Always hangup */
545         return -1;
546 }
547
548
549 int load_module()
550 {
551         /* Make sure we can register our sip channel type */
552         if (ast_channel_register(type, tdesc, capability, agent_request)) {
553                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
554                 return -1;
555         }
556         ast_register_application(app, login_exec, synopsis, descrip);
557         ast_cli_register(&cli_show_agents);
558         /* Read in the config */
559         read_agent_config();
560         return 0;
561 }
562
563 int reload()
564 {
565         read_agent_config();
566         return 0;
567 }
568
569 int unload_module()
570 {
571         struct agent_pvt *p;
572         /* First, take us out of the channel loop */
573         ast_cli_unregister(&cli_show_agents);
574         ast_unregister_application(app);
575         ast_channel_unregister(type);
576         if (!ast_pthread_mutex_lock(&agentlock)) {
577                 /* Hangup all interfaces if they have an owner */
578                 p = agents;
579                 while(p) {
580                         if (p->owner)
581                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
582                         p = p->next;
583                 }
584                 agents = NULL;
585                 ast_pthread_mutex_unlock(&agentlock);
586         } else {
587                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
588                 return -1;
589         }               
590         return 0;
591 }
592
593 int usecount()
594 {
595         int res;
596         ast_pthread_mutex_lock(&usecnt_lock);
597         res = usecnt;
598         ast_pthread_mutex_unlock(&usecnt_lock);
599         return res;
600 }
601
602 char *key()
603 {
604         return ASTERISK_GPL_KEY;
605 }
606
607 char *description()
608 {
609         return desc;
610 }
611