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