Version 0.1.8 from FTP
[asterisk/asterisk.git] / apps / app_dial.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to dial a channel
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/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/options.h>
19 #include <asterisk/module.h>
20 #include <asterisk/translate.h>
21 #include <asterisk/say.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29 #include <sys/signal.h>
30 #include <netinet/in.h>
31
32 #include <pthread.h>
33
34 static char *tdesc = "Dialing/Parking Application";
35
36 static char *app = "Dial";
37
38 static char *parkedcall = "ParkedCall";
39
40 static char *synopsis = "Place an call and connect to the current channel";
41
42 static char *registrar = "app_dial";
43
44 static char *descrip =
45 "  Dial(Technology/resource[&Technology2/resource2...][|timeout][|transfer]): Requests one or more channels\n"
46 "  and places specified outgoing calls on them.  As soon as a channel answers, the Dial app\n"
47 "  will answer the originating channel (if it needs to be answered) and will bridge a call\n"
48 "  with the channel which first answered.  All other calls placed by the Dial app will be\n"
49 "  hung up.  If a timeout is not specified, the Dial application will wait indefinitely until\n"
50 "  either one of the called channels answers, the user hangs up, or all channels return busy or\n"
51 "  error.  In general, the dialler will return 0 if it was unable to place the call, or the\n"
52 "  timeout expired.  However, if all channels were busy, and there exists an extension with\n"
53 "  priority n+101 (where n is the priority of the dialler instance), then it will be the\n"
54 "  next executed extension (this allows you to setup different behavior on busy from no-answer)\n."
55 "  This application returns -1 if the originating channel hangs up, or if the call is bridged and\n"
56 "  either of the parties in the bridge terminate the call.  The transfer string may contain a\n"
57 "  't' to allow the called user transfer a call or 'T' to allow the calling user to transfer the call.\n"
58 "  In addition to transferring the call, a call may be parked and then picked up by another user.\n";
59
60 /* No more than 45 seconds parked before you do something with them */
61 static int parkingtime = 45000;
62
63 /* Context for which parking is made accessible */
64 static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
65
66 /* Extension you type to park the call */
67 static char parking_ext[AST_MAX_EXTENSION] = "700";
68
69 /* First available extension for parking */
70 static int parking_start = 701;
71
72 /* Last available extension for parking */
73 static int parking_stop = 750;
74
75 /* We define a customer "local user" structure because we
76    use it not only for keeping track of what is in use but
77    also for keeping track of who we're dialing. */
78
79 struct localuser {
80         struct ast_channel *chan;
81         int stillgoing;
82         int allowredirect;
83         struct localuser *next;
84 };
85
86 struct parkeduser {
87         struct ast_channel *chan;
88         struct timeval start;
89         int parkingnum;
90         /* Where to go if our parking time expires */
91         char context[AST_MAX_EXTENSION];
92         char exten[AST_MAX_EXTENSION];
93         int priority;
94         struct parkeduser *next;
95 };
96
97 static struct parkeduser *parkinglot;
98
99 static pthread_mutex_t parking_lock = PTHREAD_MUTEX_INITIALIZER;
100
101 static pthread_t parking_thread;
102
103 LOCAL_USER_DECL;
104
105 static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect);
106
107
108 static int park_call(struct ast_channel *chan, struct ast_channel *peer)
109 {
110         /* We put the user in the parking list, then wake up the parking thread to be sure it looks
111            after these channels too */
112         struct parkeduser *pu, *cur;
113         int x;
114         pu = malloc(sizeof(struct parkeduser));
115         if (pu) {
116                 ast_pthread_mutex_lock(&parking_lock);
117                 for (x=parking_start;x<=parking_stop;x++) {
118                         cur = parkinglot;
119                         while(cur) {
120                                 if (cur->parkingnum == x) 
121                                         break;
122                                 cur = cur->next;
123                         }
124                         if (!cur)
125                                 break;
126                 }
127                 if (x <= parking_stop) {
128                         pu->chan = chan;
129                         gettimeofday(&pu->start, NULL);
130                         pu->parkingnum = x;
131                         /* Remember what had been dialed, so that if the parking
132                            expires, we try to come back to the same place */
133                         strncpy(pu->context, chan->context, sizeof(pu->context));
134                         strncpy(pu->exten, chan->exten, sizeof(pu->exten));
135                         pu->priority = chan->priority;
136                         pu->next = parkinglot;
137                         parkinglot = pu;
138                         ast_pthread_mutex_unlock(&parking_lock);
139                         /* Wake up the (presumably select()ing) thread */
140                         pthread_kill(parking_thread, SIGURG);
141                         if (option_verbose > 1) 
142                                 ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
143                         ast_say_digits(peer, pu->parkingnum, peer->language);
144                         return 0;
145                 } else {
146                         ast_log(LOG_WARNING, "No more parking spaces\n");
147                         free(pu);
148                         ast_pthread_mutex_unlock(&parking_lock);
149                         return -1;
150                 }
151         } else {
152                 ast_log(LOG_WARNING, "Out of memory\n");
153                 return -1;
154         }
155         return 0;
156 }
157
158 static void *do_parking_thread(void *ignore)
159 {
160         int ms, tms, max;
161         struct parkeduser *pu, *pl, *pt = NULL;
162         struct timeval tv;
163         struct ast_frame *f;
164         int x;
165         fd_set rfds, efds;
166         fd_set nrfds, nefds;
167         FD_ZERO(&rfds);
168         FD_ZERO(&efds);
169         for (;;) {
170                 ms = -1;
171                 max = -1;
172                 ast_pthread_mutex_lock(&parking_lock);
173                 pl = NULL;
174                 pu = parkinglot;
175                 gettimeofday(&tv, NULL);
176                 FD_ZERO(&nrfds);
177                 FD_ZERO(&nefds);
178                 while(pu) {
179                         tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
180                         if (tms > parkingtime) {
181                                 /* They've been waiting too long, send them back to where they came.  Theoretically they
182                                    should have their original extensions and such, but we copy to be on the safe side */
183                                 strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten));
184                                 strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context));
185                                 pu->chan->priority = pu->priority;
186                                 /* Start up the PBX, or hang them up */
187                                 if (ast_pbx_start(pu->chan))  {
188                                         ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
189                                         ast_hangup(pu->chan);
190                                 }
191                                 /* And take them out of the parking lot */
192                                 if (pl) 
193                                         pl->next = pu->next;
194                                 else
195                                         parkinglot = pu->next;
196                                 pt = pu;
197                                 pu = pu->next;
198                                 free(pt);
199                         } else {
200                                 for (x=0;x<AST_MAX_FDS;x++) {
201                                         if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
202                                                         if (FD_ISSET(pu->chan->fds[x], &efds))
203                                                                 pu->chan->exception = 1;
204                                                         pu->chan->fdno = x;
205                                                         /* See if they need servicing */
206                                                         f = ast_read(pu->chan);
207                                                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass ==  AST_CONTROL_HANGUP))) {
208                                                         /* There's a problem, hang them up*/
209                                                         if (option_verbose > 1) 
210                                                                 ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
211                                                         ast_hangup(pu->chan);
212                                                         /* And take them out of the parking lot */
213                                                         if (pl) 
214                                                                 pl->next = pu->next;
215                                                         else
216                                                                 parkinglot = pu->next;
217                                                         pt = pu;
218                                                         pu = pu->next;
219                                                         free(pt);
220                                                         break;
221                                                 } else {
222                                                         /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
223                                                         ast_frfree(f);
224                                                         goto std;       /* XXX Ick: jumping into an else statement??? XXX */
225                                                 }
226                                         }
227                                 }
228                                 if (x >= AST_MAX_FDS) {
229                                         /* Keep this one for next one */
230 std:                            FD_SET(pu->chan->fds[x], &nrfds);
231                                         FD_SET(pu->chan->fds[x], &nefds);
232                                         /* Keep track of our longest wait */
233                                         if ((tms < ms) || (ms < 0))
234                                                 ms = tms;
235                                         if (pu->chan->fds[x] > max)
236                                                 max = pu->chan->fds[x];
237                                         pl = pu;
238                                         pu = pu->next;
239                                 }
240                         }
241                 }
242                 ast_pthread_mutex_unlock(&parking_lock);
243                 rfds = nrfds;
244                 efds = nefds;
245                 tv.tv_sec = ms / 1000;
246                 tv.tv_usec = (ms % 1000) * 1000;
247                 /* Wait for something to happen */
248                 select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
249                 pthread_testcancel();
250         }
251         return NULL;    /* Never reached */
252 }
253
254 static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
255 {
256         /* Hang up a tree of stuff */
257         struct localuser *oo;
258         while(outgoing) {
259                 /* Hangup any existing lines we have open */
260                 if (outgoing->chan != exception)
261                         ast_hangup(outgoing->chan);
262                 oo = outgoing;
263                 outgoing=outgoing->next;
264                 free(oo);
265         }
266 }
267
268 #define MAX 256
269
270 static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir)
271 {
272         struct localuser *o;
273         int found;
274         int numlines;
275         int sentringing = 0;
276         int numbusies = 0;
277         int orig = *to;
278         struct ast_frame *f;
279         struct ast_channel *peer = NULL;
280         struct ast_channel *watchers[MAX];
281         int pos;
282         int single;
283         struct ast_channel *winner;
284         
285         single = (outgoing && !outgoing->next);
286         
287         if (single) {
288                 /* If we are calling a single channel, make them compatible for in-band tone purpose */
289                 ast_channel_make_compatible(outgoing->chan, in);
290         }
291         
292         while(*to && !peer) {
293                 o = outgoing;
294                 found = -1;
295                 pos = 1;
296                 numlines = 0;
297                 watchers[0] = in;
298                 while(o) {
299                         /* Keep track of important channels */
300                         if (o->stillgoing) {
301                                 watchers[pos++] = o->chan;
302                                 found = 1;
303                         }
304                         o = o->next;
305                         numlines++;
306                 }
307                 if (found < 0) {
308                         if (numlines == numbusies) {
309                                 if (option_verbose > 2)
310                                         ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy at this time\n");
311                                 /* See if there is a special busy message */
312                                 if (ast_exists_extension(in, in->context, in->exten, in->priority + 101)) 
313                                         in->priority+=100;
314                         } else {
315                                 if (option_verbose > 2)
316                                         ast_verbose( VERBOSE_PREFIX_2 "No one is available to answer at this time\n");
317                         }
318                         *to = 0;
319                         return NULL;
320                 }
321                 winner = ast_waitfor_n(watchers, pos, to);
322                 o = outgoing;
323                 while(o) {
324                         if (o->chan == winner) {
325                                 f = ast_read(winner);
326                                 if (f) {
327                                         if (f->frametype == AST_FRAME_CONTROL) {
328                                                 switch(f->subclass) {
329                                             case AST_CONTROL_ANSWER:
330                                                         /* This is our guy if someone answered. */
331                                                         if (!peer) {
332                                                                 if (option_verbose > 2)
333                                                                         ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
334                                                                 peer = o->chan;
335                                                                 *allowredir = o->allowredirect;
336                                                         }
337                                                         break;
338                                                 case AST_CONTROL_BUSY:
339                                                         if (option_verbose > 2)
340                                                                 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
341                                                         o->stillgoing = 0;
342                                                         numbusies++;
343                                                         break;
344                                                 case AST_CONTROL_RINGING:
345                                                         if (option_verbose > 2)
346                                                                 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
347                                                         if (!sentringing) {
348                                                                 ast_indicate(in, AST_CONTROL_RINGING);
349                                                                 sentringing++;
350                                                         }
351                                                         break;
352                                                 case AST_CONTROL_OFFHOOK:
353                                                         /* Ignore going off hook */
354                                                         break;
355                                                 default:
356                                                         ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
357                                                 }
358                                         } else if (single && (f->frametype == AST_FRAME_VOICE)) {
359                                                 if (ast_write(in, f)) 
360                                                         ast_log(LOG_WARNING, "Unable to forward frame\n");
361                                         }
362                                         ast_frfree(f);
363                                 } else {
364                                         o->stillgoing = 0;
365                                 }
366                         }
367                         o = o->next;
368                 }
369                 if (winner == in) {
370                         f = ast_read(in);
371 #if 0
372                         if (f && (f->frametype != AST_FRAME_VOICE))
373                                         printf("Frame type: %d, %d\n", f->frametype, f->subclass);
374 #endif
375                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass = AST_CONTROL_HANGUP))) {
376                                 /* Got hung up */
377                                 *to=-1;
378                                 return NULL;
379                         }
380                 }
381                 if (!*to && (option_verbose > 2))
382                         ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
383         }
384         return peer;
385         
386 }
387
388 static int bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect)
389 {
390         /* Copy voice back and forth between the two channels.  Give the peer
391            the ability to transfer calls with '#<extension' syntax. */
392         int len;
393         struct ast_frame *f;
394         struct ast_channel *who;
395         char newext[256], *ptr;
396         int res;
397         struct ast_option_header *aoh;
398         /* Answer if need be */
399         if (chan->state != AST_STATE_UP)
400                 if (ast_answer(chan))
401                         return -1;
402         peer->appl = "Bridged Call";
403         peer->data = chan->name;
404         for (;;) {
405                 res = ast_channel_bridge(chan, peer, AST_BRIDGE_DTMF_CHANNEL_1, &f, &who);
406                 if (res < 0) {
407                         ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
408                         return -1;
409                 }
410                 
411                 if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) || 
412                         (f->subclass == AST_CONTROL_CONGESTION)))) {
413                                 res = -1;
414                                 break;
415                 }
416                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
417                         if (who == chan)
418                                 ast_indicate(peer, AST_CONTROL_RINGING);
419                         else
420                                 ast_indicate(chan, AST_CONTROL_RINGING);
421                 }
422                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
423                         aoh = f->data;
424                         /* Forward option Requests */
425                         if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
426                                 if (who == chan)
427                                         ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
428                                 else
429                                         ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
430                         }
431                 }
432                 if ((f->frametype == AST_FRAME_DTMF) && (who == peer) && allowredirect &&
433                      (f->subclass == '#')) {
434                                 memset(newext, 0, sizeof(newext));
435                                 ptr = newext;
436                                 len = ast_pbx_longest_extension(chan->context) + 1;
437                                 if (len < ast_pbx_longest_extension("default") + 1)
438                                         len = ast_pbx_longest_extension("default") + 1;
439                                         /* Transfer */
440                                 if ((res=ast_streamfile(peer, "pbx-transfer", chan->language)))
441                                         break;
442                                 if ((res=ast_waitstream(peer, AST_DIGIT_ANY)) < 0)
443                                         break;
444                                 ast_stopstream(peer);
445                                 if (res > 0) {
446                                         /* If they've typed a digit already, handle it */
447                                         newext[0] = res;
448                                         ptr++;
449                                         len --;
450                                 }
451                                 res = ast_readstring(peer, ptr, len, 3000, 2000, "#");
452                                 if (res)
453                                         break;
454                                 if (!strcmp(newext, parking_ext)) {
455                                         if (!park_call(chan, peer)) {
456                                                 /* We return non-zero, but tell the PBX not to hang the channel when
457                                                    the thread dies -- We have to be careful now though.  We are responsible for 
458                                                    hanging up the channel, else it will never be hung up! */
459                                                 res=AST_PBX_KEEPALIVE;
460                                                 break;
461                                         } else {
462                                                 ast_log(LOG_WARNING, "Unable to park call %s\n", chan->name);
463                                         }
464                                         /* XXX Maybe we should have another message here instead of invalid extension XXX */
465                                 } else if (ast_exists_extension(chan, peer->context, newext, 1)) {
466                                         /* Set the channel's new extension, since it exists, using peer context */
467                                         strncpy(chan->exten, newext, sizeof(chan->exten));
468                                         strncpy(chan->context, peer->context, sizeof(chan->context));
469                                         chan->priority = 0;
470                                         ast_frfree(f);
471                                         if (option_verbose > 2) 
472                                                 ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n", chan->name, chan->exten, chan->context);
473                                         res=0;
474                                         break;
475                                 }
476                                 res = ast_streamfile(peer, "pbx-invalid", chan->language);
477                                 if (res)
478                                         break;
479                                 res = ast_waitstream(peer, AST_DIGIT_ANY);
480                                 ast_stopstream(peer);
481                                 res = 0;
482                         } else {
483 #if 1
484                                 ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
485 #endif
486                         }
487         }
488         return res;
489 }
490
491 static int park_exec(struct ast_channel *chan, void *data)
492 {
493         int res=0;
494         struct localuser *u;
495         struct ast_channel *peer=NULL;
496         struct parkeduser *pu, *pl=NULL;
497         int park;
498         if (!data) {
499                 ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
500                 return -1;
501         }
502         LOCAL_USER_ADD(u);
503         park = atoi((char *)data);
504         ast_pthread_mutex_lock(&parking_lock);
505         pu = parkinglot;
506         while(pu) {
507                 if (pu->parkingnum == park) {
508                         if (pl)
509                                 pl->next = pu->next;
510                         else
511                                 parkinglot = pu->next;
512                         break;
513                 }
514                 pu = pu->next;
515         }
516         ast_pthread_mutex_unlock(&parking_lock);
517         if (pu) {
518                 peer = pu->chan;
519                 free(pu);
520         }
521         if (peer) {
522                 res = ast_channel_make_compatible(chan, peer);
523                 if (res < 0) {
524                         ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
525                         ast_hangup(peer);
526                         return -1;
527                 }
528                 /* This runs sorta backwards, since we give the incoming channel control, as if it
529                    were the person called. */
530                 if (option_verbose > 2) 
531                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
532                 res = bridge_call(peer, chan, 1);
533                 /* Simulate the PBX hanging up */
534                 if (res != AST_PBX_KEEPALIVE)
535                         ast_hangup(peer);
536                 return -1;
537         } else {
538                 /* XXX Play a message XXX */
539                 if (option_verbose > 2) 
540                         ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
541                 res = -1;
542         }
543         LOCAL_USER_REMOVE(u);
544         return res;
545 }
546
547 static int dial_exec(struct ast_channel *chan, void *data)
548 {
549         int res=-1;
550         struct localuser *u;
551         char *info, *peers, *timeout, *tech, *number, *rest, *cur;
552         struct localuser *outgoing=NULL, *tmp;
553         struct ast_channel *peer;
554         int to;
555         int allowredir=0;
556         char numsubst[AST_MAX_EXTENSION];
557         char restofit[AST_MAX_EXTENSION];
558         char *transfer = NULL;
559         char *newnum;
560         
561         if (!data) {
562                 ast_log(LOG_WARNING, "Dial requires an argument (technology1/number1&technology2/number2...|optional timeout)\n");
563                 return -1;
564         }
565         
566         LOCAL_USER_ADD(u);
567         
568         /* Parse our arguments XXX Check for failure XXX */
569         info = malloc(strlen((char *)data) + AST_MAX_EXTENSION);
570         if (!info) {
571                 ast_log(LOG_WARNING, "Out of memory\n");
572                 return -1;
573         }
574         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION);
575         peers = info;
576         if (peers) {
577                 timeout = strchr(info, '|');
578                 if (timeout) {
579                         *timeout = '\0';
580                         timeout++;
581                         transfer = strchr(timeout, '|');
582                         if (transfer) {
583                                 *transfer = '\0';
584                                 transfer++;
585                         }
586                 }
587         } else
588                 timeout = NULL;
589         if (!peers || !strlen(peers)) {
590                 ast_log(LOG_WARNING, "Dial argument takes format (technology1/number1&technology2/number2...|optional timeout)\n");
591                 goto out;
592         }
593         
594         cur = peers;
595         do {
596                 /* Remember where to start next time */
597                 rest = strchr(cur, '&');
598                 if (rest) {
599                         *rest = 0;
600                         rest++;
601                 }
602                 /* Get a technology/[device:]number pair */
603                 tech = cur;
604                 number = strchr(tech, '/');
605                 if (!number) {
606                         ast_log(LOG_WARNING, "Dial argument takes format (technology1/[device:]number1&technology2/[device:]number2...|optional timeout)\n");
607                         goto out;
608                 }
609                 *number = '\0';
610                 number++;
611                 tmp = malloc(sizeof(struct localuser));
612                 if (!tmp) {
613                         ast_log(LOG_WARNING, "Out of memory\n");
614                         goto out;
615                 }
616                 if (transfer && (strchr(transfer, 't')))
617                         tmp->allowredirect = 1;
618                 else
619                         tmp->allowredirect = 0;
620                 strncpy(numsubst, number, sizeof(numsubst));
621                 /* If we're dialing by extension, look at the extension to know what to dial */
622                 if ((newnum = strstr(numsubst, "BYEXTENSION"))) {
623                         strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit));
624                         snprintf(newnum, sizeof(numsubst) - (newnum - numsubst), "%s%s", chan->exten,restofit);
625                         if (option_debug)
626                                 ast_log(LOG_DEBUG, "Dialing by extension %s\n", numsubst);
627                 }
628                 /* Request the peer */
629                 tmp->chan = ast_request(tech, chan->nativeformats, numsubst);
630                 if (!tmp->chan) {
631                         /* If we can't, just go on to the next call */
632                         ast_log(LOG_WARNING, "Unable to create channel of type '%s'\n", tech);
633                         free(tmp);
634                         cur = rest;
635                         continue;
636                 }
637                 tmp->chan->appl = "AppDial";
638                 tmp->chan->data = "(Outgoing Line)";
639                 if (tmp->chan->callerid)
640                         free(tmp->chan->callerid);
641                 if (chan->callerid)
642                         tmp->chan->callerid = strdup(chan->callerid);
643                 else
644                         tmp->chan->callerid = NULL;
645                 /* Place the call, but don't wait on the answer */
646                 res = ast_call(tmp->chan, numsubst, 0);
647                 if (res) {
648                         /* Again, keep going even if there's an error */
649                         if (option_debug)
650                                 ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
651                         else if (option_verbose > 2)
652                                 ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
653                         ast_hangup(tmp->chan);
654                         free(tmp);
655                         cur = rest;
656                         continue;
657                 } else
658                         if (option_verbose > 2)
659                                 ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
660                 /* Put them in the list of outgoing thingies...  We're ready now. 
661                    XXX If we're forcibly removed, these outgoing calls won't get
662                    hung up XXX */
663                 tmp->stillgoing = -1;
664                 tmp->next = outgoing;
665                 outgoing = tmp;
666                 cur = rest;
667         } while(cur);
668         
669         if (timeout && strlen(timeout))
670                 to = atoi(timeout) * 1000;
671         else
672                 to = -1;
673         peer = wait_for_answer(chan, outgoing, &to, &allowredir);
674         if (!peer) {
675                 if (to) 
676                         /* Musta gotten hung up */
677                         res = -1;
678                  else 
679                         /* Nobody answered, next please? */
680                         res=0;
681                 
682                 goto out;
683         }
684         if (peer) {
685                 /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
686                    we will always return with -1 so that it is hung up properly after the 
687                    conversation.  */
688                 hanguptree(outgoing, peer);
689                 outgoing = NULL;
690                 /* Make sure channels are compatible */
691                 res = ast_channel_make_compatible(chan, peer);
692                 if (res < 0) {
693                         ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
694                         ast_hangup(peer);
695                         return -1;
696                 }
697                 res = bridge_call(chan, peer, allowredir);
698                 ast_hangup(peer);
699         }       
700 out:
701         hanguptree(outgoing, NULL);
702         free(info);
703         LOCAL_USER_REMOVE(u);
704         return res;
705 }
706
707 int unload_module(void)
708 {
709         STANDARD_HANGUP_LOCALUSERS;
710         return ast_unregister_application(app);
711 }
712
713 int load_module(void)
714 {
715         int res;
716         int x;
717         struct ast_context *con;
718         char exten[AST_MAX_EXTENSION];
719         con = ast_context_find(parking_con);
720         if (!con) {
721                 con = ast_context_create(parking_con, registrar);
722                 if (!con) {
723                         ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
724                         return -1;
725                 }
726         }
727         for(x=parking_start; x<=parking_stop;x++) {
728                 snprintf(exten, sizeof(exten), "%d", x);
729                 ast_add_extension2(con, 1, exten, 1, parkedcall, strdup(exten), free, registrar);
730         }
731         pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
732         res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
733         if (!res)
734                 res = ast_register_application(app, dial_exec, synopsis, descrip);
735         return res;
736 }
737
738 char *description(void)
739 {
740         return tdesc;
741 }
742
743 int usecount(void)
744 {
745         int res;
746         STANDARD_USECOUNT(res);
747         return res;
748 }
749
750 char *key()
751 {
752         return ASTERISK_GPL_KEY;
753 }