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