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