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