Version 0.1.12 from FTP
[asterisk/asterisk.git] / res / res_parking.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Routines implementing call parking
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 <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/options.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/say.h>
23 #include <asterisk/channel_pvt.h>
24 #include <asterisk/parking.h>
25 #include <asterisk/musiconhold.h>
26 #include <asterisk/config.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/time.h>
34 #include <sys/signal.h>
35 #include <netinet/in.h>
36
37 #include <pthread.h>
38
39 static char *parkedcall = "ParkedCall";
40
41 /* No more than 45 seconds parked before you do something with them */
42 static int parkingtime = 45000;
43
44 /* Context for which parking is made accessible */
45 static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
46
47 /* Extension you type to park the call */
48 static char parking_ext[AST_MAX_EXTENSION] = "700";
49
50 /* First available extension for parking */
51 static int parking_start = 701;
52
53 /* Last available extension for parking */
54 static int parking_stop = 750;
55
56 /* Registrar for operations */
57 static char *registrar = "res_parking";
58
59 static char *synopsis = "Answer a parked call";
60
61 static char *descrip = "ParkedCall(exten):"
62 "Used to connect to a parked call.  This Application is always\n"
63 "registered internally and does not need to be explicitly added\n"
64 "into the dialplan, although you should include the 'parkedcalls'\n"
65 "context.\n";
66
67 struct parkeduser {
68         struct ast_channel *chan;
69         struct timeval start;
70         int parkingnum;
71         /* Where to go if our parking time expires */
72         char context[AST_MAX_EXTENSION];
73         char exten[AST_MAX_EXTENSION];
74         int priority;
75         struct parkeduser *next;
76 };
77
78 static struct parkeduser *parkinglot;
79
80 static pthread_mutex_t parking_lock = AST_MUTEX_INITIALIZER;
81
82 static pthread_t parking_thread;
83
84 STANDARD_LOCAL_USER;
85
86 LOCAL_USER_DECL;
87
88 char *ast_parking_ext(void)
89 {
90         return parking_ext;
91 }
92
93 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer)
94 {
95         /* We put the user in the parking list, then wake up the parking thread to be sure it looks
96            after these channels too */
97         struct parkeduser *pu, *cur;
98         int x;
99         pu = malloc(sizeof(struct parkeduser));
100         if (pu) {
101                 ast_pthread_mutex_lock(&parking_lock);
102                 for (x=parking_start;x<=parking_stop;x++) {
103                         cur = parkinglot;
104                         while(cur) {
105                                 if (cur->parkingnum == x) 
106                                         break;
107                                 cur = cur->next;
108                         }
109                         if (!cur)
110                                 break;
111                 }
112                 if (x <= parking_stop) {
113                         pu->chan = chan;
114                         ast_moh_start(pu->chan, NULL);
115                         gettimeofday(&pu->start, NULL);
116                         pu->parkingnum = x;
117                         /* Remember what had been dialed, so that if the parking
118                            expires, we try to come back to the same place */
119                         strncpy(pu->context, chan->context, sizeof(pu->context)-1);
120                         strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
121                         pu->priority = chan->priority;
122                         pu->next = parkinglot;
123                         parkinglot = pu;
124                         ast_pthread_mutex_unlock(&parking_lock);
125                         /* Wake up the (presumably select()ing) thread */
126                         pthread_kill(parking_thread, SIGURG);
127                         if (option_verbose > 1) 
128                                 ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
129                         ast_say_digits(peer, pu->parkingnum, "", peer->language);
130                         /* Start music on hold */
131                         return 0;
132                 } else {
133                         ast_log(LOG_WARNING, "No more parking spaces\n");
134                         free(pu);
135                         ast_pthread_mutex_unlock(&parking_lock);
136                         return -1;
137                 }
138         } else {
139                 ast_log(LOG_WARNING, "Out of memory\n");
140                 return -1;
141         }
142         return 0;
143 }
144
145 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer)
146 {
147         struct ast_channel *chan;
148         struct ast_frame *f;
149         /* Make a new, fake channel that we'll use to masquerade in the real one */
150         chan = ast_channel_alloc(0);
151         if (chan) {
152                 /* Let us keep track of the channel name */
153                 snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
154                 /* Make formats okay */
155                 chan->readformat = rchan->readformat;
156                 chan->writeformat = rchan->writeformat;
157                 ast_channel_masquerade(chan, rchan);
158                 /* Setup the extensions and such */
159                 strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
160                 strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
161                 chan->priority = rchan->priority;
162                 /* Make the masq execute */
163                 f = ast_read(chan);
164                 if (f)
165                         ast_frfree(f);
166                 ast_park_call(chan, peer);
167         } else {
168                 ast_log(LOG_WARNING, "Unable to create parked channel\n");
169                 return -1;
170         }
171         return 0;
172 }
173
174 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect)
175 {
176         /* Copy voice back and forth between the two channels.  Give the peer
177            the ability to transfer calls with '#<extension' syntax. */
178         int len;
179         struct ast_frame *f;
180         struct ast_channel *who;
181         char newext[256], *ptr;
182         int res;
183         struct ast_option_header *aoh;
184         /* Answer if need be */
185         if (chan->state != AST_STATE_UP) {
186                 if (ast_answer(chan))
187                         return -1;
188         }
189         peer->appl = "Bridged Call";
190         peer->data = chan->name;
191         for (;;) {
192                 res = ast_channel_bridge(chan, peer, allowredirect ? AST_BRIDGE_DTMF_CHANNEL_1 : 0, &f, &who);
193                 if (res < 0) {
194                         ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
195                         return -1;
196                 }
197                 
198                 if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) || 
199                         (f->subclass == AST_CONTROL_CONGESTION)))) {
200                                 res = -1;
201                                 break;
202                 }
203                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
204                         if (who == chan)
205                                 ast_indicate(peer, AST_CONTROL_RINGING);
206                         else
207                                 ast_indicate(chan, AST_CONTROL_RINGING);
208                 }
209                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
210                         aoh = f->data;
211                         /* Forward option Requests */
212                         if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
213                                 if (who == chan)
214                                         ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
215                                 else
216                                         ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
217                         }
218                 }
219                 if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect &&
220                      (f->subclass == '#')) {
221                                 memset(newext, 0, sizeof(newext));
222                                 ptr = newext;
223                                         /* Transfer */
224                                 if ((res=ast_streamfile(peer, "pbx-transfer", chan->language)))
225                                         break;
226                                 if ((res=ast_waitstream(peer, AST_DIGIT_ANY)) < 0)
227                                         break;
228                                 ast_stopstream(peer);
229                                 if (res > 0) {
230                                         /* If they've typed a digit already, handle it */
231                                         newext[0] = res;
232                                         ptr++;
233                                         len --;
234                                 }
235                                 res = 0;
236                                 while(strlen(newext) < sizeof(newext - 1)) {
237                                         res = ast_waitfordigit(peer, 3000);
238                                         if (res < 1)
239                                                 break;
240                                         *(ptr++) = res;
241                                         if (!ast_canmatch_extension(peer, peer->context, newext, 1, peer->callerid) ||
242                                                 ast_exists_extension(peer, peer->context, newext, 1, peer->callerid)) {
243                                                 res = 0;
244                                                 break;
245                                         }
246                                 }
247                                 if (res)
248                                         break;
249                                 if (!strcmp(newext, ast_parking_ext())) {
250                                         if (!ast_park_call(chan, peer)) {
251                                                 /* We return non-zero, but tell the PBX not to hang the channel when
252                                                    the thread dies -- We have to be careful now though.  We are responsible for 
253                                                    hanging up the channel, else it will never be hung up! */
254                                                 res=AST_PBX_KEEPALIVE;
255                                                 break;
256                                         } else {
257                                                 ast_log(LOG_WARNING, "Unable to park call %s\n", chan->name);
258                                         }
259                                         /* XXX Maybe we should have another message here instead of invalid extension XXX */
260                                 } else if (ast_exists_extension(chan, peer->context, newext, 1, peer->callerid)) {
261                                         /* Set the channel's new extension, since it exists, using peer context */
262                                         strncpy(chan->exten, newext, sizeof(chan->exten)-1);
263                                         strncpy(chan->context, peer->context, sizeof(chan->context)-1);
264                                         chan->priority = 0;
265                                         ast_frfree(f);
266                                         if (option_verbose > 2) 
267                                                 ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n", chan->name, chan->exten, chan->context);
268                                         res=0;
269                                         break;
270                                 } else {
271                                         if (option_verbose > 2) 
272                                                 ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context %s\n", newext, peer->context);
273                                 }
274                                 res = ast_streamfile(peer, "pbx-invalid", chan->language);
275                                 if (res)
276                                         break;
277                                 res = ast_waitstream(peer, AST_DIGIT_ANY);
278                                 ast_stopstream(peer);
279                                 res = 0;
280                         } else {
281             if (f && (f->frametype == AST_FRAME_DTMF)) {
282                   if (who == peer)
283                         ast_write(chan, f);
284                   else
285                         ast_write(peer, f);
286             }            
287 #if 1
288                                 ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
289 #endif
290                         }
291          if (f)
292                ast_frfree(f);
293         }
294         return res;
295 }
296
297 static void *do_parking_thread(void *ignore)
298 {
299         int ms, tms, max;
300         struct parkeduser *pu, *pl, *pt = NULL;
301         struct timeval tv;
302         struct ast_frame *f;
303         int x;
304         fd_set rfds, efds;
305         fd_set nrfds, nefds;
306         FD_ZERO(&rfds);
307         FD_ZERO(&efds);
308         for (;;) {
309                 ms = -1;
310                 max = -1;
311                 ast_pthread_mutex_lock(&parking_lock);
312                 pl = NULL;
313                 pu = parkinglot;
314                 gettimeofday(&tv, NULL);
315                 FD_ZERO(&nrfds);
316                 FD_ZERO(&nefds);
317                 while(pu) {
318                         tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
319                         if (tms > parkingtime) {
320                                 /* They've been waiting too long, send them back to where they came.  Theoretically they
321                                    should have their original extensions and such, but we copy to be on the safe side */
322                                 strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
323                                 strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
324                                 pu->chan->priority = pu->priority;
325                                 /* Stop music on hold */
326                                 ast_moh_stop(pu->chan);
327                                 /* Start up the PBX, or hang them up */
328                                 if (ast_pbx_start(pu->chan))  {
329                                         ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
330                                         ast_hangup(pu->chan);
331                                 }
332                                 /* And take them out of the parking lot */
333                                 if (pl) 
334                                         pl->next = pu->next;
335                                 else
336                                         parkinglot = pu->next;
337                                 pt = pu;
338                                 pu = pu->next;
339                                 free(pt);
340                         } else {
341                                 for (x=0;x<AST_MAX_FDS;x++) {
342                                         if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
343                                                         if (FD_ISSET(pu->chan->fds[x], &efds))
344                                                                 pu->chan->exception = 1;
345                                                         pu->chan->fdno = x;
346                                                         /* See if they need servicing */
347                                                         f = ast_read(pu->chan);
348                                                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass ==  AST_CONTROL_HANGUP))) {
349                                                         /* There's a problem, hang them up*/
350                                                         if (option_verbose > 1) 
351                                                                 ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
352                                                         ast_hangup(pu->chan);
353                                                         /* And take them out of the parking lot */
354                                                         if (pl) 
355                                                                 pl->next = pu->next;
356                                                         else
357                                                                 parkinglot = pu->next;
358                                                         pt = pu;
359                                                         pu = pu->next;
360                                                         free(pt);
361                                                         break;
362                                                 } else {
363                                                         /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
364                                                         ast_frfree(f);
365                                                         goto std;       /* XXX Ick: jumping into an else statement??? XXX */
366                                                 }
367                                         }
368                                 }
369                                 if (x >= AST_MAX_FDS) {
370 std:                                    for (x=0;x<AST_MAX_FDS;x++) {
371                                                 /* Keep this one for next one */
372                                                 if (pu->chan->fds[x] > -1) {
373                                                         FD_SET(pu->chan->fds[x], &nrfds);
374                                                         FD_SET(pu->chan->fds[x], &nefds);
375                                                         if (pu->chan->fds[x] > max)
376                                                                 max = pu->chan->fds[x];
377                                                 }
378                                         }
379                                         /* Keep track of our longest wait */
380                                         if ((tms < ms) || (ms < 0))
381                                                 ms = tms;
382                                         pl = pu;
383                                         pu = pu->next;
384                                 }
385                         }
386                 }
387                 ast_pthread_mutex_unlock(&parking_lock);
388                 rfds = nrfds;
389                 efds = nefds;
390                 tv.tv_sec = ms / 1000;
391                 tv.tv_usec = (ms % 1000) * 1000;
392                 /* Wait for something to happen */
393                 select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
394                 pthread_testcancel();
395         }
396         return NULL;    /* Never reached */
397 }
398
399 static int park_exec(struct ast_channel *chan, void *data)
400 {
401         int res=0;
402         struct localuser *u;
403         struct ast_channel *peer=NULL;
404         struct parkeduser *pu, *pl=NULL;
405         int park;
406         if (!data) {
407                 ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
408                 return -1;
409         }
410         LOCAL_USER_ADD(u);
411         park = atoi((char *)data);
412         ast_pthread_mutex_lock(&parking_lock);
413         pu = parkinglot;
414         while(pu) {
415                 if (pu->parkingnum == park) {
416                         if (pl)
417                                 pl->next = pu->next;
418                         else
419                                 parkinglot = pu->next;
420                         break;
421                 }
422                 pu = pu->next;
423         }
424         ast_pthread_mutex_unlock(&parking_lock);
425         if (pu) {
426                 peer = pu->chan;
427                 free(pu);
428         }
429         if (peer) {
430                 ast_moh_stop(peer);
431                 res = ast_channel_make_compatible(chan, peer);
432                 if (res < 0) {
433                         ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
434                         ast_hangup(peer);
435                         return -1;
436                 }
437                 /* This runs sorta backwards, since we give the incoming channel control, as if it
438                    were the person called. */
439                 if (option_verbose > 2) 
440                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
441                 res = ast_bridge_call(peer, chan, 1);
442                 /* Simulate the PBX hanging up */
443                 if (res != AST_PBX_KEEPALIVE)
444                         ast_hangup(peer);
445                 return -1;
446         } else {
447                 /* XXX Play a message XXX */
448                 if (option_verbose > 2) 
449                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
450                 res = -1;
451         }
452         LOCAL_USER_REMOVE(u);
453         return res;
454 }
455
456 int load_module(void)
457 {
458         int res;
459         int x;
460         int start, end;
461         struct ast_context *con;
462         char exten[AST_MAX_EXTENSION];
463         struct ast_config *cfg;
464         struct ast_variable *var;
465         cfg = ast_load("parking.conf");
466         if (cfg) {
467                 var = ast_variable_browse(cfg, "general");
468                 while(var) {
469                         if (!strcasecmp(var->name, "parkext")) {
470                                 strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
471                         } else if (!strcasecmp(var->name, "context")) {
472                                 strncpy(parking_con, var->value, sizeof(parking_con) - 1);
473                         } else if (!strcasecmp(var->name, "parkpos")) {
474                                 if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
475                                         ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of parking.conf\n", var->lineno);
476                                 } else {
477                                         parking_start = start;
478                                         parking_stop = end;
479                                 }
480                         }
481                         var = var->next;
482                 }
483                 ast_destroy(cfg);
484         }
485         con = ast_context_find(parking_con);
486         if (!con) {
487                 con = ast_context_create(parking_con, registrar);
488                 if (!con) {
489                         ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
490                         return -1;
491                 }
492         }
493         for(x=parking_start; x<=parking_stop;x++) {
494                 snprintf(exten, sizeof(exten), "%d", x);
495                 ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), free, registrar);
496         }
497         pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
498         res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
499         return res;
500 }
501
502 int unload_module(void)
503 {
504         STANDARD_HANGUP_LOCALUSERS;
505         return ast_unregister_application(parkedcall);
506 }
507
508 char *description(void)
509 {
510         return "Call Parking Resource";
511 }
512
513 int usecount(void)
514 {
515         /* Never allow parking to be unloaded because it will
516            unresolve needed symbols in the dialer */
517 #if 0
518         int res;
519         STANDARD_USECOUNT(res);
520         return res;
521 #else
522         return 1;
523 #endif
524 }
525
526 char *key()
527 {
528         return ASTERISK_GPL_KEY;
529 }