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