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