49168e170e66baa1cbe0b0f6327228dd75141d4a
[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 <asterisk/cli.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sys/time.h>
35 #include <sys/signal.h>
36 #include <netinet/in.h>
37
38 #include <pthread.h>
39
40 #define DEFAULT_PARK_TIME 45000
41
42 static char *parkedcall = "ParkedCall";
43
44 /* No more than 45 seconds parked before you do something with them */
45 static int parkingtime = DEFAULT_PARK_TIME;
46
47 /* Context for which parking is made accessible */
48 static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
49
50 /* Extension you type to park the call */
51 static char parking_ext[AST_MAX_EXTENSION] = "700";
52
53 static char pickup_ext[AST_MAX_EXTENSION] = "*8";
54
55 /* First available extension for parking */
56 static int parking_start = 701;
57
58 /* Last available extension for parking */
59 static int parking_stop = 750;
60
61 /* Registrar for operations */
62 static char *registrar = "res_parking";
63
64 static char *synopsis = "Answer a parked call";
65
66 static char *descrip = "ParkedCall(exten):"
67 "Used to connect to a parked call.  This Application is always\n"
68 "registered internally and does not need to be explicitly added\n"
69 "into the dialplan, although you should include the 'parkedcalls'\n"
70 "context.\n";
71
72 struct parkeduser {
73         struct ast_channel *chan;
74         struct timeval start;
75         int parkingnum;
76         /* Where to go if our parking time expires */
77         char context[AST_MAX_EXTENSION];
78         char exten[AST_MAX_EXTENSION];
79         int priority;
80         int parkingtime;
81         struct parkeduser *next;
82 };
83
84 static struct parkeduser *parkinglot;
85
86 static ast_mutex_t parking_lock = AST_MUTEX_INITIALIZER;
87
88 static pthread_t parking_thread;
89
90 STANDARD_LOCAL_USER;
91
92 LOCAL_USER_DECL;
93
94 char *ast_parking_ext(void)
95 {
96         return parking_ext;
97 }
98
99 char *ast_pickup_ext(void)
100 {
101         return pickup_ext;
102 }
103
104 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
105 {
106         /* We put the user in the parking list, then wake up the parking thread to be sure it looks
107            after these channels too */
108         struct parkeduser *pu, *cur;
109         int x;
110         pu = malloc(sizeof(struct parkeduser));
111         if (pu) {
112                 ast_mutex_lock(&parking_lock);
113                 for (x=parking_start;x<=parking_stop;x++) {
114                         cur = parkinglot;
115                         while(cur) {
116                                 if (cur->parkingnum == x) 
117                                         break;
118                                 cur = cur->next;
119                         }
120                         if (!cur)
121                                 break;
122                 }
123                 if (x <= parking_stop) {
124                         chan->appl = "Parked Call";
125                         chan->data = NULL; 
126
127                         pu->chan = chan;
128                         /* Start music on hold */
129                         ast_moh_start(pu->chan, NULL);
130                         gettimeofday(&pu->start, NULL);
131                         pu->parkingnum = x;
132                         if (timeout > 0)
133                                 pu->parkingtime = timeout;
134                         else
135                                 pu->parkingtime = parkingtime;
136                         if (extout)
137                                 *extout = x;
138                         /* Remember what had been dialed, so that if the parking
139                            expires, we try to come back to the same place */
140                         if (strlen(chan->macrocontext))
141                                 strncpy(pu->context, chan->macrocontext, sizeof(pu->context)-1);
142                         else
143                                 strncpy(pu->context, chan->context, sizeof(pu->context)-1);
144                         if (strlen(chan->macroexten))
145                                 strncpy(pu->exten, chan->macroexten, sizeof(pu->exten)-1);
146                         else
147                                 strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
148                         if (chan->macropriority)
149                                 pu->priority = chan->macropriority;
150                         else
151                                 pu->priority = chan->priority;
152                         pu->next = parkinglot;
153                         parkinglot = pu;
154                         ast_mutex_unlock(&parking_lock);
155                         /* Wake up the (presumably select()ing) thread */
156                         pthread_kill(parking_thread, SIGURG);
157                         if (option_verbose > 1) 
158                                 ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
159                         if (peer)
160                                 ast_say_digits(peer, pu->parkingnum, "", peer->language);
161                         return 0;
162                 } else {
163                         ast_log(LOG_WARNING, "No more parking spaces\n");
164                         free(pu);
165                         ast_mutex_unlock(&parking_lock);
166                         return -1;
167                 }
168         } else {
169                 ast_log(LOG_WARNING, "Out of memory\n");
170                 return -1;
171         }
172         return 0;
173 }
174
175 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
176 {
177         struct ast_channel *chan;
178         struct ast_frame *f;
179         /* Make a new, fake channel that we'll use to masquerade in the real one */
180         chan = ast_channel_alloc(0);
181         if (chan) {
182                 /* Let us keep track of the channel name */
183                 snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
184                 /* Make formats okay */
185                 chan->readformat = rchan->readformat;
186                 chan->writeformat = rchan->writeformat;
187                 ast_channel_masquerade(chan, rchan);
188                 /* Setup the extensions and such */
189                 strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
190                 strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
191                 chan->priority = rchan->priority;
192                 /* Make the masq execute */
193                 f = ast_read(chan);
194                 if (f)
195                         ast_frfree(f);
196                 ast_park_call(chan, peer, timeout, extout);
197         } else {
198                 ast_log(LOG_WARNING, "Unable to create parked channel\n");
199                 return -1;
200         }
201         return 0;
202 }
203
204 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect_in, int allowredirect_out, int allowdisconnect)
205 {
206         /* Copy voice back and forth between the two channels.  Give the peer
207            the ability to transfer calls with '#<extension' syntax. */
208         int len;
209         struct ast_frame *f;
210         struct ast_channel *who;
211         char newext[256], *ptr;
212         int res;
213         struct ast_option_header *aoh;
214         struct ast_channel *transferer;
215         struct ast_channel *transferee;
216   char *transferer_real_context;
217
218         /* Answer if need be */
219         if (ast_answer(chan))
220                 return -1;
221         peer->appl = "Bridged Call";
222         peer->data = chan->name;
223         for (;;) {
224                 res = ast_channel_bridge(chan, peer, (allowdisconnect||allowredirect_out ? AST_BRIDGE_DTMF_CHANNEL_0 : 0) + (allowredirect_in ? AST_BRIDGE_DTMF_CHANNEL_1 : 0), &f, &who);
225                 if (res < 0) {
226                         ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
227                         return -1;
228                 }
229                 
230                 if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) || 
231                         (f->subclass == AST_CONTROL_CONGESTION)))) {
232                                 res = -1;
233                                 break;
234                 }
235                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
236                         if (who == chan)
237                                 ast_indicate(peer, AST_CONTROL_RINGING);
238                         else
239                                 ast_indicate(chan, AST_CONTROL_RINGING);
240                 }
241                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == -1)) {
242                         if (who == chan)
243                                 ast_indicate(peer, -1);
244                         else
245                                 ast_indicate(chan, -1);
246                 }
247                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
248                         aoh = f->data;
249                         /* Forward option Requests */
250                         if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
251                                 if (who == chan)
252                                         ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
253                                 else
254                                         ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
255                         }
256                 }
257             if (f && (f->frametype == AST_FRAME_DTMF) && (who == chan) && allowdisconnect &&
258                      (f->subclass == '*')) {
259                 if (option_verbose > 3)
260                         ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
261                         res = -1;
262                         break;
263
264                         }
265
266                 if ((f->frametype == AST_FRAME_DTMF) &&
267                         ((allowredirect_in && who == peer) || (allowredirect_out && who == chan)) &&
268                         (f->subclass == '#')) {
269                                 if(allowredirect_in &&  who == peer) {
270                                         transferer = peer;
271                                         transferee = chan;
272                                 }
273                                 else {
274                                         transferer = chan;
275                                         transferee = peer;
276                                 }
277
278                                 /* Use the non-macro context to transfer the call */
279                                 if(strlen(transferer->macrocontext))
280                                         transferer_real_context=transferer->macrocontext;
281                                 else
282                                         transferer_real_context=transferer->context;
283
284                                 /* Start autoservice on chan while we talk
285                                    to the originator */
286                                 ast_autoservice_start(transferee);
287                                 ast_moh_start(transferee, NULL);
288
289                                 memset(newext, 0, sizeof(newext));
290                                 ptr = newext;
291
292                                         /* Transfer */
293                                 if ((res=ast_streamfile(transferer, "pbx-transfer", transferer->language))) {
294                                         ast_moh_stop(transferee);
295                                         ast_autoservice_stop(transferee);
296                                         break;
297                                 }
298                                 if ((res=ast_waitstream(transferer, AST_DIGIT_ANY)) < 0) {
299                                         ast_moh_stop(transferee);
300                                         ast_autoservice_stop(transferee);
301                                         break;
302                                 }
303                                 ast_stopstream(transferer);
304                                 if (res > 0) {
305                                         /* If they've typed a digit already, handle it */
306                                         newext[0] = res;
307                                         ptr++;
308                                         len --;
309                                 }
310                                 res = 0;
311                                 while(strlen(newext) < sizeof(newext) - 1) {
312                                         res = ast_waitfordigit(transferer, 3000);
313                                         if (res < 1) 
314                                                 break;
315                                         if (res == '#')
316                                                 break;
317                                         *(ptr++) = res;
318                                         if (!ast_matchmore_extension(transferer, transferer_real_context
319                                                                 , newext, 1, transferer->callerid)) {
320                                                 break;
321                                         }
322                                 }
323
324                                 if (res < 0) {
325                                         ast_moh_stop(transferee);
326                                         ast_autoservice_stop(transferee);
327                                         break;
328                                 }
329                                 if (!strcmp(newext, ast_parking_ext())) {
330                                         ast_moh_stop(transferee);
331
332                                         if (ast_autoservice_stop(transferee))
333                                                 res = -1;
334                                         else if (!ast_park_call(transferee, transferer, 0, NULL)) {
335                                                 /* We return non-zero, but tell the PBX not to hang the channel when
336                                                    the thread dies -- We have to be careful now though.  We are responsible for 
337                                                    hanging up the channel, else it will never be hung up! */
338
339                                                 if(transferer==peer)
340                                                         res=AST_PBX_KEEPALIVE;
341                                                 else
342                                                         res=AST_PBX_NO_HANGUP_PEER;
343                                                 break;
344                                         } else {
345                                                 ast_log(LOG_WARNING, "Unable to park call %s\n", transferee->name);
346                                         }
347                                         /* XXX Maybe we should have another message here instead of invalid extension XXX */
348                                 } else if (ast_exists_extension(transferee, transferer_real_context, newext, 1, transferer->callerid)) {
349                                         ast_moh_stop(transferee);
350                                         res=ast_autoservice_stop(transferee);
351                                         if (!transferee->pbx) {
352                                                 /* Doh!  Use our handy async_goto funcitons */
353                                                 if (option_verbose > 2) 
354                                                         ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n"
355                                                                 ,transferee->name, newext, transferer_real_context);
356                                                 if (ast_async_goto(transferee, transferer_real_context, newext, 1, 1))
357                                                         ast_log(LOG_WARNING, "Async goto fialed :(\n");
358                                                 res = -1;
359                                         } else {
360                                                 /* Set the channel's new extension, since it exists, using transferer context */
361                                                 strncpy(transferee->exten, newext, sizeof(transferee->exten)-1);
362                                                 strncpy(transferee->context, transferer_real_context, sizeof(transferee->context)-1);
363                                                 transferee->priority = 0;
364                                                 ast_frfree(f);
365                                         }
366                                         break;
367                                 } else {
368                                         if (option_verbose > 2) 
369                                                 ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context '%s'\n", newext, transferer_real_context);
370                                 }
371                                 res = ast_streamfile(transferer, "pbx-invalid", transferee->language);
372                                 if (res) {
373                                         ast_moh_stop(transferee);
374                                         ast_autoservice_stop(transferee);
375                                         break;
376                                 }
377                                 res = ast_waitstream(transferer, AST_DIGIT_ANY);
378                                 ast_stopstream(transferer);
379                                 ast_moh_stop(transferee);
380                                 res = ast_autoservice_stop(transferee);
381                                 if (res) {
382                                         if (option_verbose > 1)
383                                                 ast_verbose(VERBOSE_PREFIX_2 "Hungup during autoservice stop on '%s'\n", transferee->name);
384                                 }
385                         } else {
386             if (f && (f->frametype == AST_FRAME_DTMF)) {
387                   if (who == peer)
388                         ast_write(chan, f);
389                   else
390                         ast_write(peer, f);
391             }            
392 #if 1
393                                 ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
394 #endif
395                         }
396          if (f)
397                ast_frfree(f);
398         }
399         return res;
400 }
401
402 static void *do_parking_thread(void *ignore)
403 {
404         int ms, tms, max;
405         struct parkeduser *pu, *pl, *pt = NULL;
406         struct timeval tv;
407         struct ast_frame *f;
408         int x;
409         fd_set rfds, efds;
410         fd_set nrfds, nefds;
411         FD_ZERO(&rfds);
412         FD_ZERO(&efds);
413         for (;;) {
414                 ms = -1;
415                 max = -1;
416                 ast_mutex_lock(&parking_lock);
417                 pl = NULL;
418                 pu = parkinglot;
419                 gettimeofday(&tv, NULL);
420                 FD_ZERO(&nrfds);
421                 FD_ZERO(&nefds);
422                 while(pu) {
423                         tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
424                         if (tms > pu->parkingtime) {
425                                 /* They've been waiting too long, send them back to where they came.  Theoretically they
426                                    should have their original extensions and such, but we copy to be on the safe side */
427                                 strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
428                                 strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
429                                 pu->chan->priority = pu->priority;
430                                 /* Stop music on hold */
431                                 ast_moh_stop(pu->chan);
432                                 /* Start up the PBX, or hang them up */
433                                 if (ast_pbx_start(pu->chan))  {
434                                         ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
435                                         ast_hangup(pu->chan);
436                                 }
437                                 /* And take them out of the parking lot */
438                                 if (pl) 
439                                         pl->next = pu->next;
440                                 else
441                                         parkinglot = pu->next;
442                                 pt = pu;
443                                 pu = pu->next;
444                                 free(pt);
445                         } else {
446                                 for (x=0;x<AST_MAX_FDS;x++) {
447                                         if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
448                                                 if (FD_ISSET(pu->chan->fds[x], &efds))
449                                                         pu->chan->exception = 1;
450                                                 pu->chan->fdno = x;
451                                                 /* See if they need servicing */
452                                                 f = ast_read(pu->chan);
453                                                 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass ==  AST_CONTROL_HANGUP))) {
454                                                         /* There's a problem, hang them up*/
455                                                         if (option_verbose > 1) 
456                                                                 ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
457                                                         ast_hangup(pu->chan);
458                                                         /* And take them out of the parking lot */
459                                                         if (pl) 
460                                                                 pl->next = pu->next;
461                                                         else
462                                                                 parkinglot = pu->next;
463                                                         pt = pu;
464                                                         pu = pu->next;
465                                                         free(pt);
466                                                         break;
467                                                 } else {
468                                                         /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
469                                                         ast_frfree(f);
470                                                         goto std;       /* XXX Ick: jumping into an else statement??? XXX */
471                                                 }
472                                         }
473                                 }
474                                 if (x >= AST_MAX_FDS) {
475 std:                                    for (x=0;x<AST_MAX_FDS;x++) {
476                                                 /* Keep this one for next one */
477                                                 if (pu->chan->fds[x] > -1) {
478                                                         FD_SET(pu->chan->fds[x], &nrfds);
479                                                         FD_SET(pu->chan->fds[x], &nefds);
480                                                         if (pu->chan->fds[x] > max)
481                                                                 max = pu->chan->fds[x];
482                                                 }
483                                         }
484                                         /* Keep track of our longest wait */
485                                         if ((tms < ms) || (ms < 0))
486                                                 ms = tms;
487                                         pl = pu;
488                                         pu = pu->next;
489                                 }
490                         }
491                 }
492                 ast_mutex_unlock(&parking_lock);
493                 rfds = nrfds;
494                 efds = nefds;
495                 tv.tv_sec = ms / 1000;
496                 tv.tv_usec = (ms % 1000) * 1000;
497                 /* Wait for something to happen */
498                 ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
499                 pthread_testcancel();
500         }
501         return NULL;    /* Never reached */
502 }
503
504 static int park_exec(struct ast_channel *chan, void *data)
505 {
506         int res=0;
507         struct localuser *u;
508         struct ast_channel *peer=NULL;
509         struct parkeduser *pu, *pl=NULL;
510         int park;
511         int dres;
512         if (!data) {
513                 ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
514                 return -1;
515         }
516         LOCAL_USER_ADD(u);
517         park = atoi((char *)data);
518         ast_mutex_lock(&parking_lock);
519         pu = parkinglot;
520         while(pu) {
521                 if (pu->parkingnum == park) {
522                         if (pl)
523                                 pl->next = pu->next;
524                         else
525                                 parkinglot = pu->next;
526                         break;
527                 }
528                 pl = pu;
529                 pu = pu->next;
530         }
531         ast_mutex_unlock(&parking_lock);
532         if (pu) {
533                 peer = pu->chan;
534                 free(pu);
535         }
536         /* JK02: it helps to answer the channel if not already up */
537         if (chan->_state != AST_STATE_UP) {
538                 ast_answer(chan);
539         }
540
541         if (peer) {
542                 ast_moh_stop(peer);
543                 res = ast_channel_make_compatible(chan, peer);
544                 if (res < 0) {
545                         ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
546                         ast_hangup(peer);
547                         return -1;
548                 }
549                 /* This runs sorta backwards, since we give the incoming channel control, as if it
550                    were the person called. */
551                 if (option_verbose > 2) 
552                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
553                 res = ast_bridge_call(peer, chan, 1, 1, 0);
554                 /* Simulate the PBX hanging up */
555                 if (res != AST_PBX_KEEPALIVE)
556                         ast_hangup(peer);
557                 return -1;
558         } else {
559                 /* XXX Play a message XXX */
560           dres = ast_streamfile(chan, "pbx-invalidpark", chan->language);
561           if (!dres)
562             dres = ast_waitstream(chan, "");
563           else {
564             ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
565             dres = 0;
566           }
567                 if (option_verbose > 2) 
568                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
569                 res = -1;
570         }
571         LOCAL_USER_REMOVE(u);
572         return res;
573 }
574
575 static int handle_parkedcalls(int fd, int argc, char *argv[])
576 {
577         struct parkeduser *cur;
578
579         ast_cli(fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
580                 , "Context", "Extension", "Pri", "Timeout");
581
582         ast_mutex_lock(&parking_lock);
583
584         cur=parkinglot;
585         while(cur) {
586                 ast_cli(fd, "%4d %25s (%-15s %-12s %-4d) %6lds\n"
587                         ,cur->parkingnum, cur->chan->name, cur->context, cur->exten
588                         ,cur->priority, cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL));
589
590                 cur = cur->next;
591         }
592
593         ast_mutex_unlock(&parking_lock);
594
595         return RESULT_SUCCESS;
596 }
597
598 static char showparked_help[] =
599 "Usage: show parkedcalls\n"
600 "       Lists currently parked calls.\n";
601
602 static struct ast_cli_entry showparked =
603 { { "show", "parkedcalls", NULL }, handle_parkedcalls, "Lists parked calls", showparked_help };
604
605
606 int load_module(void)
607 {
608         int res;
609         int x;
610         int start, end;
611         struct ast_context *con;
612         char exten[AST_MAX_EXTENSION];
613         struct ast_config *cfg;
614         struct ast_variable *var;
615
616         ast_cli_register(&showparked);
617
618         cfg = ast_load("parking.conf");
619         if (cfg) {
620                 var = ast_variable_browse(cfg, "general");
621                 while(var) {
622                         if (!strcasecmp(var->name, "parkext")) {
623                                 strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
624                         } else if (!strcasecmp(var->name, "context")) {
625                                 strncpy(parking_con, var->value, sizeof(parking_con) - 1);
626                         } else if (!strcasecmp(var->name, "parkingtime")) {
627                                 if ((sscanf(var->value, "%d", &parkingtime) != 1) || (parkingtime < 1)) {
628                                         ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
629                                         parkingtime = DEFAULT_PARK_TIME;
630                                 } else
631                                         parkingtime = parkingtime * 1000;
632                         } else if (!strcasecmp(var->name, "parkpos")) {
633                                 if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
634                                         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);
635                                 } else {
636                                         parking_start = start;
637                                         parking_stop = end;
638                                 }
639                         }
640                         var = var->next;
641                 }
642                 ast_destroy(cfg);
643         }
644         con = ast_context_find(parking_con);
645         if (!con) {
646                 con = ast_context_create(NULL,parking_con, registrar);
647                 if (!con) {
648                         ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
649                         return -1;
650                 }
651         }
652         for(x=parking_start; x<=parking_stop;x++) {
653                 snprintf(exten, sizeof(exten), "%d", x);
654                 ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), free, registrar);
655         }
656         pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
657         res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
658         return res;
659 }
660
661 int ast_pickup_call(struct ast_channel *chan)
662 {
663         struct ast_channel *cur;
664         int res = -1;
665         cur = ast_channel_walk(NULL);
666         while(cur) {
667                 if (!cur->pbx && 
668                         (cur != chan) &&
669                         (chan->pickupgroup & cur->callgroup) &&
670                         ((cur->_state == AST_STATE_RINGING) ||
671                          (cur->_state == AST_STATE_RING))) {
672                                 break;
673                 }
674                 cur = ast_channel_walk(cur);
675         }
676         if (cur) {
677                 ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
678                 res = ast_answer(chan);
679                 if (res)
680                         ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
681                 res = ast_queue_control(chan, AST_CONTROL_ANSWER, 0);
682                 if (res)
683                         ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
684                 res = ast_channel_masquerade(cur, chan);
685                 if (res)
686                         ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name);           /* Done */
687         } else  {
688                 ast_log(LOG_DEBUG, "No call pickup possible...\n");
689         }
690         return res;
691 }
692
693 unsigned int ast_get_group(char *s)
694 {
695         char *copy;
696         char *piece;
697         char *c=NULL;
698         int start=0, finish=0,x;
699         unsigned int group = 0;
700         copy = ast_strdupa(s);
701         if (!copy) {
702                 ast_log(LOG_ERROR, "Out of memory\n");
703                 return 0;
704         }
705         c = copy;
706         
707         while((piece = strsep(&c, ","))) {
708                 if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
709                         /* Range */
710                 } else if (sscanf(piece, "%d", &start)) {
711                         /* Just one */
712                         finish = start;
713                 } else {
714                         ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'.  Using '0'\n", s,piece);
715                         return 0;
716                 }
717                 for (x=start;x<=finish;x++) {
718                         if ((x > 31) || (x < 0)) {
719                                 ast_log(LOG_WARNING, "Ignoring invalid group %d\n", x);
720                         } else
721                                 group |= (1 << x);
722                 }
723         }
724         return group;
725 }
726
727 int unload_module(void)
728 {
729         STANDARD_HANGUP_LOCALUSERS;
730
731         ast_cli_unregister(&showparked);
732
733         return ast_unregister_application(parkedcall);
734 }
735
736 char *description(void)
737 {
738         return "Call Parking Resource";
739 }
740
741 int usecount(void)
742 {
743         /* Never allow parking to be unloaded because it will
744            unresolve needed symbols in the dialer */
745 #if 0
746         int res;
747         STANDARD_USECOUNT(res);
748         return res;
749 #else
750         return 1;
751 #endif
752 }
753
754 char *key()
755 {
756         return ASTERISK_GPL_KEY;
757 }