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