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