This fixes a problem with pattern ranges; and corrects a situation in res_features...
[asterisk/asterisk.git] / res / res_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Routines implementing call features as call pickup, parking and transfer
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <pthread.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <sys/time.h>
38 #include <sys/signal.h>
39 #include <netinet/in.h>
40
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/options.h"
47 #include "asterisk/causes.h"
48 #include "asterisk/module.h"
49 #include "asterisk/translate.h"
50 #include "asterisk/app.h"
51 #include "asterisk/say.h"
52 #include "asterisk/features.h"
53 #include "asterisk/musiconhold.h"
54 #include "asterisk/config.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/manager.h"
57 #include "asterisk/utils.h"
58 #include "asterisk/adsi.h"
59 #include "asterisk/devicestate.h"
60 #include "asterisk/monitor.h"
61
62 #define DEFAULT_PARK_TIME 45000
63 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
64 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 500
65 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
66 #define DEFAULT_ATXFER_DROP_CALL 0
67 #define DEFAULT_ATXFER_LOOP_DELAY 10000
68 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
69
70 #define AST_MAX_WATCHERS 256
71
72 enum {
73         AST_FEATURE_FLAG_NEEDSDTMF = (1 << 0),
74         AST_FEATURE_FLAG_ONPEER =    (1 << 1),
75         AST_FEATURE_FLAG_ONSELF =    (1 << 2),
76         AST_FEATURE_FLAG_BYCALLEE =  (1 << 3),
77         AST_FEATURE_FLAG_BYCALLER =  (1 << 4),
78         AST_FEATURE_FLAG_BYBOTH  =   (3 << 3),
79 };
80
81 struct feature_group_exten {
82         AST_LIST_ENTRY(feature_group_exten) entry;
83         AST_DECLARE_STRING_FIELDS(
84                 AST_STRING_FIELD(exten);
85         );
86         struct ast_call_feature *feature;
87 };
88
89 struct feature_group {
90         AST_LIST_ENTRY(feature_group) entry;
91         AST_DECLARE_STRING_FIELDS(
92                 AST_STRING_FIELD(gname);
93         );
94         AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
95 };
96
97 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
98
99 static char *parkedcall = "ParkedCall";
100
101 static int parkaddhints = 0;                               /*!< Add parking hints automatically */
102 static int parkedcalltransfers = 0;                        /*!< Enable DTMF based transfers on bridge when picking up parked calls */
103 static int parkedcallreparking = 0;                        /*!< Enable DTMF based parking on bridge when picking up parked calls */
104 static int parkingtime = DEFAULT_PARK_TIME;                /*!< No more than 45 seconds parked before you do something with them */
105 static char parking_con[AST_MAX_EXTENSION];                /*!< Context for which parking is made accessible */
106 static char parking_con_dial[AST_MAX_EXTENSION];           /*!< Context for dialback for parking (KLUDGE) */
107 static char parking_ext[AST_MAX_EXTENSION];                /*!< Extension you type to park the call */
108 static char pickup_ext[AST_MAX_EXTENSION];                 /*!< Call pickup extension */
109 static char parkmohclass[MAX_MUSICCLASS];                  /*!< Music class used for parking */
110 static int parking_start;                                  /*!< First available extension for parking */
111 static int parking_stop;                                   /*!< Last available extension for parking */
112
113 static char courtesytone[256];                             /*!< Courtesy tone */
114 static int parkedplay = 0;                                 /*!< Who to play the courtesy tone to */
115 static char xfersound[256];                                /*!< Call transfer sound */
116 static char xferfailsound[256];                            /*!< Call transfer failure sound */
117
118 static int parking_offset;
119 static int parkfindnext;
120
121 static int adsipark;
122
123 static int transferdigittimeout;
124 static int featuredigittimeout;
125 static int comebacktoorigin = 1;
126
127 static int atxfernoanswertimeout;
128 static unsigned int atxferdropcall;
129 static unsigned int atxferloopdelay;
130 static unsigned int atxfercallbackretries;
131
132 static char *registrar = "res_features";                   /*!< Registrar for operations */
133
134 /* module and CLI command definitions */
135 static char *synopsis = "Answer a parked call";
136
137 static char *descrip = "ParkedCall(exten): "
138 "Used to connect to a parked call.  This application is always\n"
139 "registered internally and does not need to be explicitly added\n"
140 "into the dialplan, although you should include the 'parkedcalls'\n"
141 "context.  If no extension is provided, then the first available\n"
142 "parked call will be acquired.\n";
143
144 static char *parkcall = "Park";
145
146 static char *synopsis2 = "Park yourself";
147
148 static char *descrip2 = "Park(): "
149 "Used to park yourself (typically in combination with a supervised\n"
150 "transfer to know the parking space). This application is always\n"
151 "registered internally and does not need to be explicitly added\n"
152 "into the dialplan, although you should include the 'parkedcalls'\n"
153 "context (or the context specified in features.conf).\n\n"
154 "If you set the PARKINGEXTEN variable to an extension in your\n"
155 "parking context, Park() will park the call on that extension, unless\n"
156 "it already exists. In that case, execution will continue at next\n"
157 "priority.\n" ;
158
159 static struct ast_app *monitor_app = NULL;
160 static int monitor_ok = 1;
161
162 struct parkeduser {
163         struct ast_channel *chan;                   /*!< Parking channel */
164         struct timeval start;                       /*!< Time the parking started */
165         int parkingnum;                             /*!< Parking lot */
166         char parkingexten[AST_MAX_EXTENSION];       /*!< If set beforehand, parking extension used for this call */
167         char context[AST_MAX_CONTEXT];              /*!< Where to go if our parking time expires */
168         char exten[AST_MAX_EXTENSION];
169         int priority;
170         int parkingtime;                            /*!< Maximum length in parking lot before return */
171         int notquiteyet;
172         char peername[1024];
173         unsigned char moh_trys;
174         AST_LIST_ENTRY(parkeduser) list;
175 };
176
177 static AST_LIST_HEAD_STATIC(parkinglot, parkeduser);
178
179 static pthread_t parking_thread;
180
181 const char *ast_parking_ext(void)
182 {
183         return parking_ext;
184 }
185
186 const char *ast_pickup_ext(void)
187 {
188         return pickup_ext;
189 }
190
191 struct ast_bridge_thread_obj 
192 {
193         struct ast_bridge_config bconfig;
194         struct ast_channel *chan;
195         struct ast_channel *peer;
196         unsigned int return_to_pbx:1;
197 };
198
199
200
201 /*!
202  * \brief store context, extension and priority 
203  * \param chan, context, ext, pri
204 */
205 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
206 {
207         ast_copy_string(chan->context, context, sizeof(chan->context));
208         ast_copy_string(chan->exten, ext, sizeof(chan->exten));
209         chan->priority = pri;
210 }
211
212 /*!
213  * \brief Check goto on transfer
214  * \param chan
215  *
216  * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
217  * When found make sure the types are compatible. Check if channel is valid
218  * if so start the new channel else hangup the call. 
219 */
220 static void check_goto_on_transfer(struct ast_channel *chan) 
221 {
222         struct ast_channel *xferchan;
223         const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
224         char *x, *goto_on_transfer;
225         struct ast_frame *f;
226
227         if (ast_strlen_zero(val))
228                 return;
229
230         goto_on_transfer = ast_strdupa(val);
231
232         if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, chan->name)))
233                 return;
234
235         for (x = goto_on_transfer; x && *x; x++) {
236                 if (*x == '^')
237                         *x = '|';
238         }
239         /* Make formats okay */
240         xferchan->readformat = chan->readformat;
241         xferchan->writeformat = chan->writeformat;
242         ast_channel_masquerade(xferchan, chan);
243         ast_parseable_goto(xferchan, goto_on_transfer);
244         xferchan->_state = AST_STATE_UP;
245         ast_clear_flag(xferchan, AST_FLAGS_ALL);        
246         xferchan->_softhangup = 0;
247         if ((f = ast_read(xferchan))) {
248                 ast_frfree(f);
249                 f = NULL;
250                 ast_pbx_start(xferchan);
251         } else {
252                 ast_hangup(xferchan);
253         }
254 }
255
256 static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate);
257
258 /*!
259  * \brief bridge the call 
260  * \param data thread bridge.
261  *
262  * Set Last Data for respective channels, reset cdr for channels
263  * bridge call, check if we're going back to dialplan
264  * if not hangup both legs of the call
265 */
266 static void *ast_bridge_call_thread(void *data) 
267 {
268         struct ast_bridge_thread_obj *tobj = data;
269         int res;
270
271         tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
272         tobj->chan->data = tobj->peer->name;
273         tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
274         tobj->peer->data = tobj->chan->name;
275
276         if (tobj->chan->cdr) {
277                 ast_cdr_reset(tobj->chan->cdr, NULL);
278                 ast_cdr_setdestchan(tobj->chan->cdr, tobj->peer->name);
279         }
280         if (tobj->peer->cdr) {
281                 ast_cdr_reset(tobj->peer->cdr, NULL);
282                 ast_cdr_setdestchan(tobj->peer->cdr, tobj->chan->name);
283         }
284
285         ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
286
287         if (tobj->return_to_pbx) {
288                 if (!ast_check_hangup(tobj->peer)) {
289                         ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
290                         res = ast_pbx_start(tobj->peer);
291                         if (res != AST_PBX_SUCCESS)
292                                 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
293                 } else
294                         ast_hangup(tobj->peer);
295                 if (!ast_check_hangup(tobj->chan)) {
296                         ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
297                         res = ast_pbx_start(tobj->chan);
298                         if (res != AST_PBX_SUCCESS)
299                                 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
300                 } else
301                         ast_hangup(tobj->chan);
302         } else {
303                 ast_hangup(tobj->chan);
304                 ast_hangup(tobj->peer);
305         }
306
307         ast_free(tobj);
308
309         return NULL;
310 }
311
312 /*!
313  * \brief create thread for the parked call
314  * \param data
315  *
316  * Create thread and attributes, call ast_bridge_call_thread
317 */
318 static void ast_bridge_call_thread_launch(void *data) 
319 {
320         pthread_t thread;
321         pthread_attr_t attr;
322         struct sched_param sched;
323
324         pthread_attr_init(&attr);
325         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
326         ast_pthread_create(&thread, &attr,ast_bridge_call_thread, data);
327         pthread_attr_destroy(&attr);
328         memset(&sched, 0, sizeof(sched));
329         pthread_setschedparam(thread, SCHED_RR, &sched);
330 }
331
332 /*!
333  * \brief Announce call parking by ADSI
334  * \param chan .
335  * \param parkingexten .
336  * Create message to show for ADSI, display message.
337  * \retval 0 on success.
338  * \retval -1 on failure.
339 */
340 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
341 {
342         int res;
343         int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
344         char tmp[256];
345         char *message[5] = {NULL, NULL, NULL, NULL, NULL};
346
347         snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
348         message[0] = tmp;
349         res = ast_adsi_load_session(chan, NULL, 0, 1);
350         if (res == -1)
351                 return res;
352         return ast_adsi_print(chan, message, justify, 1);
353 }
354
355 /*! \brief Notify metermaids that we've changed an extension */
356 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
357 {
358         ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'", 
359                 exten, context, devstate2str(state));
360
361         ast_devstate_changed(state, "park:%s@%s", exten, context);
362 }
363
364 /*! \brief metermaids callback from devicestate.c */
365 static enum ast_device_state metermaidstate(const char *data)
366 {
367         char *context;
368         char *exten;
369
370         context = ast_strdupa(data);
371
372         exten = strsep(&context, "@");
373         if (!context)
374                 return AST_DEVICE_INVALID;
375         
376         ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
377
378         if (!ast_exists_extension(NULL, context, exten, 1, NULL))
379                 return AST_DEVICE_NOT_INUSE;
380
381         return AST_DEVICE_INUSE;
382 }
383
384 /* Park a call */
385 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
386 {
387         struct parkeduser *pu, *cur;
388         int i, x = -1, parking_range;
389         struct ast_context *con;
390         const char *parkingexten;
391         
392         /* Allocate memory for parking data */
393         if (!(pu = ast_calloc(1, sizeof(*pu)))) 
394                 return -1;
395
396         /* Lock parking lot */
397         AST_LIST_LOCK(&parkinglot);
398         /* Check for channel variable PARKINGEXTEN */
399         parkingexten = pbx_builtin_getvar_helper(chan, "PARKINGEXTEN");
400         if (!ast_strlen_zero(parkingexten)) {
401                 if (ast_exists_extension(NULL, parking_con, parkingexten, 1, NULL)) {
402                         AST_LIST_UNLOCK(&parkinglot);
403                         ast_free(pu);
404                         ast_log(LOG_WARNING, "Requested parking extension already exists: %s@%s\n", parkingexten, parking_con);
405                         return 0;       /* Continue execution if possible */
406                 }
407                 ast_copy_string(pu->parkingexten, parkingexten, sizeof(pu->parkingexten));
408                 x = atoi(parkingexten);
409         } else {
410                 /* Select parking space within range */
411                 parking_range = parking_stop - parking_start+1;
412                 for (i = 0; i < parking_range; i++) {
413                         x = (i + parking_offset) % parking_range + parking_start;
414                         AST_LIST_TRAVERSE(&parkinglot, cur, list) {
415                                 if (cur->parkingnum == x)
416                                         break;
417                         }
418                         if (!cur)
419                                 break;
420                 }
421
422                 if (!(i < parking_range)) {
423                         ast_log(LOG_WARNING, "No more parking spaces\n");
424                         ast_free(pu);
425                         AST_LIST_UNLOCK(&parkinglot);
426                         return -1;
427                 }
428                 /* Set pointer for next parking */
429                 if (parkfindnext) 
430                         parking_offset = x - parking_start + 1;
431         }
432         
433         chan->appl = "Parked Call";
434         chan->data = NULL; 
435
436         pu->chan = chan;
437         
438         /* Put the parked channel on hold if we have two different channels */
439         if (chan != peer) {
440                 ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
441                         S_OR(parkmohclass, NULL),
442                         !ast_strlen_zero(parkmohclass) ? strlen(parkmohclass) + 1 : 0);
443         }
444         
445         pu->start = ast_tvnow();
446         pu->parkingnum = x;
447         pu->parkingtime = (timeout > 0) ? timeout : parkingtime;
448         if (extout)
449                 *extout = x;
450
451         if (peer) 
452                 ast_copy_string(pu->peername, peer->name, sizeof(pu->peername));
453
454         /* Remember what had been dialed, so that if the parking
455            expires, we try to come back to the same place */
456         ast_copy_string(pu->context, S_OR(chan->macrocontext, chan->context), sizeof(pu->context));
457         ast_copy_string(pu->exten, S_OR(chan->macroexten, chan->exten), sizeof(pu->exten));
458         pu->priority = chan->macropriority ? chan->macropriority : chan->priority;
459         AST_LIST_INSERT_TAIL(&parkinglot, pu, list);
460
461         /* If parking a channel directly, don't quiet yet get parking running on it */
462         if (peer == chan) 
463                 pu->notquiteyet = 1;
464         AST_LIST_UNLOCK(&parkinglot);
465         /* Wake up the (presumably select()ing) thread */
466         pthread_kill(parking_thread, SIGURG);
467         ast_verb(2, "Parked %s on %d@%s. Will timeout back to extension [%s] %s, %d in %d seconds\n", pu->chan->name, pu->parkingnum, parking_con, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
468
469         if (pu->parkingnum != -1)
470                 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", x);
471         manager_event(EVENT_FLAG_CALL, "ParkedCall",
472                 "Exten: %s\r\n"
473                 "Channel: %s\r\n"
474                 "From: %s\r\n"
475                 "Timeout: %ld\r\n"
476                 "CallerIDNum: %s\r\n"
477                 "CallerIDName: %s\r\n",
478                 pu->parkingexten, pu->chan->name, peer ? peer->name : "",
479                 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
480                 S_OR(pu->chan->cid.cid_num, "<unknown>"),
481                 S_OR(pu->chan->cid.cid_name, "<unknown>")
482                 );
483
484         if (peer && adsipark && ast_adsi_available(peer)) {
485                 adsi_announce_park(peer, pu->parkingexten);     /* Only supports parking numbers */
486                 ast_adsi_unload_session(peer);
487         }
488
489         con = ast_context_find(parking_con);
490         if (!con) 
491                 con = ast_context_create(NULL, parking_con, registrar);
492         if (!con)       /* Still no context? Bad */
493                 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
494         /* Tell the peer channel the number of the parking space */
495         if (peer && pu->parkingnum != -1) { /* Only say number if it's a number */
496                 /* Make sure we don't start saying digits to the channel being parked */
497                 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
498                 ast_say_digits(peer, pu->parkingnum, "", peer->language);
499                 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
500         }
501         if (con) {
502                 if (!ast_add_extension2(con, 1, pu->parkingexten, 1, NULL, NULL, parkedcall, ast_strdup(pu->parkingexten), ast_free_ptr, registrar))
503                         notify_metermaids(pu->parkingexten, parking_con, AST_DEVICE_INUSE);
504         }
505         if (pu->notquiteyet) {
506                 /* Wake up parking thread if we're really done */
507                 ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
508                         S_OR(parkmohclass, NULL),
509                         !ast_strlen_zero(parkmohclass) ? strlen(parkmohclass) + 1 : 0);
510                 pu->notquiteyet = 0;
511                 pthread_kill(parking_thread, SIGURG);
512         }
513         return 0;
514 }
515
516 /* Park call via masquraded channel */
517 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
518 {
519         struct ast_channel *chan;
520         struct ast_frame *f;
521
522         /* Make a new, fake channel that we'll use to masquerade in the real one */
523         if (!(chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten, rchan->context, rchan->amaflags, "Parked/%s",rchan->name))) {
524                 ast_log(LOG_WARNING, "Unable to create parked channel\n");
525                 return -1;
526         }
527
528         /* Make formats okay */
529         chan->readformat = rchan->readformat;
530         chan->writeformat = rchan->writeformat;
531         ast_channel_masquerade(chan, rchan);
532
533         /* Setup the extensions and such */
534         set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
535
536         /* Make the masq execute */
537         f = ast_read(chan);
538         if (f)
539                 ast_frfree(f);
540
541         ast_park_call(chan, peer, timeout, extout);
542         return 0;
543 }
544
545
546 #define FEATURE_RETURN_HANGUP           -1
547 #define FEATURE_RETURN_SUCCESSBREAK      0
548 #define FEATURE_RETURN_PBX_KEEPALIVE    AST_PBX_KEEPALIVE
549 #define FEATURE_RETURN_NO_HANGUP_PEER   AST_PBX_NO_HANGUP_PEER
550 #define FEATURE_RETURN_PASSDIGITS        21
551 #define FEATURE_RETURN_STOREDIGITS       22
552 #define FEATURE_RETURN_SUCCESS           23
553 #define FEATURE_RETURN_KEEPTRYING    24
554
555 #define FEATURE_SENSE_CHAN      (1 << 0)
556 #define FEATURE_SENSE_PEER      (1 << 1)
557
558 /*! 
559  * \brief set caller and callee according to the direction 
560  * \param caller, callee, peer, chan, sense
561  *
562  * Detect who triggered feature and set callee/caller variables accordingly
563 */
564 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
565         struct ast_channel *peer, struct ast_channel *chan, int sense)
566 {
567         if (sense == FEATURE_SENSE_PEER) {
568                 *caller = peer;
569                 *callee = chan;
570         } else {
571                 *callee = peer;
572                 *caller = chan;
573         }
574 }
575
576 /*! 
577  * \brief support routing for one touch call parking
578  * \param chan channel parking call
579  * \param peer channel to be parked
580  * \param config unsed
581  * \param code unused
582  * \param sense feature options
583  *
584  * \param data
585  * Setup channel, set return exten,priority to 's,1'
586  * answer chan, sleep chan, park call
587 */
588 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
589 {
590         struct ast_channel *parker;
591         struct ast_channel *parkee;
592         int res = 0;
593         struct ast_module_user *u;
594
595         u = ast_module_user_add(chan);
596
597         set_peers(&parker, &parkee, peer, chan, sense);
598         /* Setup the exten/priority to be s/1 since we don't know
599            where this call should return */
600         strcpy(chan->exten, "s");
601         chan->priority = 1;
602         if (chan->_state != AST_STATE_UP)
603                 res = ast_answer(chan);
604         if (!res)
605                 res = ast_safe_sleep(chan, 1000);
606         if (!res)
607                 res = ast_park_call(parkee, parker, 0, NULL);
608
609         ast_module_user_remove(u);
610
611         if (!res) {
612                 if (sense == FEATURE_SENSE_CHAN)
613                         res = AST_PBX_NO_HANGUP_PEER;
614                 else
615                         res = AST_PBX_KEEPALIVE;
616         }
617         return res;
618
619 }
620
621 /*!
622  * \brief Monitor a channel by DTMF
623  * \param chan channel requesting monitor
624  * \param peer channel to be monitored
625  * \param config
626  * \param code
627  * \param sense feature options
628  *
629  * \param data
630  * Check monitor app enabled, setup channels, both caller/callee chans not null
631  * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
632  * \retval FEATURE_RETURN_SUCCESS on success.
633  * \retval -1 on error.
634 */
635 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
636 {
637         char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
638         int x = 0;
639         size_t len;
640         struct ast_channel *caller_chan, *callee_chan;
641
642         if (!monitor_ok) {
643                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
644                 return -1;
645         }
646
647         if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
648                 monitor_ok = 0;
649                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
650                 return -1;
651         }
652
653         set_peers(&caller_chan, &callee_chan, peer, chan, sense);
654
655         if (!ast_strlen_zero(courtesytone)) {
656                 if (ast_autoservice_start(callee_chan))
657                         return -1;
658                 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
659                         ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
660                         ast_autoservice_stop(callee_chan);
661                         return -1;
662                 }
663                 if (ast_autoservice_stop(callee_chan))
664                         return -1;
665         }
666         
667         if (callee_chan->monitor) {
668                 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
669                 ast_monitor_stop(callee_chan, 1);
670                 return FEATURE_RETURN_SUCCESS;
671         }
672
673         if (caller_chan && callee_chan) {
674                 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
675                 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
676                 const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
677
678                 if (!touch_format)
679                         touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
680
681                 if (!touch_monitor)
682                         touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
683         
684                 if (!touch_monitor_prefix)
685                         touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
686         
687                 if (touch_monitor) {
688                         len = strlen(touch_monitor) + 50;
689                         args = alloca(len);
690                         touch_filename = alloca(len);
691                         snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
692                         snprintf(args, len, "%s|%s|m", S_OR(touch_format, "wav"), touch_filename);
693                 } else {
694                         caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
695                         callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
696                         len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
697                         args = alloca(len);
698                         touch_filename = alloca(len);
699                         snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
700                         snprintf(args, len, "%s|%s|m", S_OR(touch_format, "wav"), touch_filename);
701                 }
702
703                 for(x = 0; x < strlen(args); x++) {
704                         if (args[x] == '/')
705                                 args[x] = '-';
706                 }
707                 
708                 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
709
710                 pbx_exec(callee_chan, monitor_app, args);
711                 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
712                 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
713         
714                 return FEATURE_RETURN_SUCCESS;
715         }
716         
717         ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");   
718         return -1;
719 }
720
721 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
722 {
723         ast_verb(4, "User hit '%s' to disconnect call.\n", code);
724         return FEATURE_RETURN_HANGUP;
725 }
726
727 static int finishup(struct ast_channel *chan)
728 {
729         ast_indicate(chan, AST_CONTROL_UNHOLD);
730   
731         return ast_autoservice_stop(chan);
732 }
733
734 /*!
735  * \brief Find the context for the transfer
736  * \param transferer
737  * \param transferee
738  * 
739  * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
740  * \return a context string
741 */
742 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
743 {
744         const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
745         if (ast_strlen_zero(s))
746                 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
747         if (ast_strlen_zero(s)) /* Use the non-macro context to transfer the call XXX ? */
748                 s = transferer->macrocontext;
749         if (ast_strlen_zero(s))
750                 s = transferer->context;
751         return s;  
752 }
753
754 /*!
755  * \brief Blind transfer user to another extension
756  * \param chan channel to be transfered
757  * \param peer channel initiated blind transfer
758  * \param config
759  * \param code
760  * \param data
761  * \param sense  feature options
762  * 
763  * Place chan on hold, check if transferred to parkinglot extension,
764  * otherwise check extension exists and transfer caller.
765  * \retval FEATURE_RETURN_SUCCESS.
766  * \retval -1 on failure.
767 */
768 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
769 {
770         struct ast_channel *transferer;
771         struct ast_channel *transferee;
772         const char *transferer_real_context;
773         char xferto[256];
774         int res;
775
776         set_peers(&transferer, &transferee, peer, chan, sense);
777         transferer_real_context = real_ctx(transferer, transferee);
778         /* Start autoservice on chan while we talk to the originator */
779         ast_autoservice_start(transferee);
780         ast_indicate(transferee, AST_CONTROL_HOLD);
781
782         memset(xferto, 0, sizeof(xferto));
783         
784         /* Transfer */
785         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
786         if (res < 0) {
787                 finishup(transferee);
788                 return -1; /* error ? */
789         }
790         if (res > 0)    /* If they've typed a digit already, handle it */
791                 xferto[0] = (char) res;
792
793         ast_stopstream(transferer);
794         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
795         if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
796                 finishup(transferee);
797                 return res;
798         }
799         if (!strcmp(xferto, ast_parking_ext())) {
800                 res = finishup(transferee);
801                 if (res)
802                         res = -1;
803                 else if (!ast_park_call(transferee, transferer, 0, NULL)) {     /* success */
804                         /* We return non-zero, but tell the PBX not to hang the channel when
805                            the thread dies -- We have to be careful now though.  We are responsible for 
806                            hanging up the channel, else it will never be hung up! */
807
808                         return (transferer == peer) ? AST_PBX_KEEPALIVE : AST_PBX_NO_HANGUP_PEER;
809                 } else {
810                         ast_log(LOG_WARNING, "Unable to park call %s\n", transferee->name);
811                 }
812                 /*! \todo XXX Maybe we should have another message here instead of invalid extension XXX */
813         } else if (ast_exists_extension(transferee, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
814                 pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", transferee->name);
815                 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", peer->name);
816                 res=finishup(transferee);
817                 if (!transferer->cdr) {
818                         transferer->cdr=ast_cdr_alloc();
819                         if (transferer) {
820                                 ast_cdr_init(transferer->cdr, transferer); /* initilize our channel's cdr */
821                                 ast_cdr_start(transferer->cdr);
822                         }
823                 }
824                 if (transferer->cdr) {
825                         ast_cdr_setdestchan(transferer->cdr, transferee->name);
826                         ast_cdr_setapp(transferer->cdr, "BLINDTRANSFER","");
827                 }
828                 if (!transferee->pbx) {
829                         /* Doh!  Use our handy async_goto functions */
830                         if (option_verbose > 2) 
831                                 ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n"
832                                                                 ,transferee->name, xferto, transferer_real_context);
833                         if (ast_async_goto(transferee, transferer_real_context, xferto, 1))
834                                 ast_log(LOG_WARNING, "Async goto failed :-(\n");
835                 } else {
836                         /* Set the channel's new extension, since it exists, using transferer context */
837                         set_c_e_p(transferee, transferer_real_context, xferto, 0);
838                 }
839                 check_goto_on_transfer(transferer);
840                 return res;
841         } else {
842                 ast_verb(3, "Unable to find extension '%s' in context '%s'\n", xferto, transferer_real_context);
843         }
844         if (ast_stream_and_wait(transferer, xferfailsound, AST_DIGIT_ANY) < 0) {
845                 finishup(transferee);
846                 return -1;
847         }
848         ast_stopstream(transferer);
849         res = finishup(transferee);
850         if (res) {
851                 ast_verb(2, "Hungup during autoservice stop on '%s'\n", transferee->name);
852                 return res;
853         }
854         return FEATURE_RETURN_SUCCESS;
855 }
856
857 /*!
858  * \brief make channels compatible
859  * \param c
860  * \param newchan
861  * \retval 0 on success.
862  * \retval -1 on failure.
863 */
864 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
865 {
866         if (ast_channel_make_compatible(c, newchan) < 0) {
867                 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
868                         c->name, newchan->name);
869                 ast_hangup(newchan);
870                 return -1;
871         }
872         return 0;
873 }
874
875 /*!
876  * \brief Attended transfer
877  * \param chan transfered user
878  * \param peer person transfering call
879  * \param config
880  * \param code
881  * \param sense feature options
882  * 
883  * \param data
884  * Get extension to transfer to, if you cannot generate channel (or find extension) 
885  * return to host channel. After called channel answered wait for hangup of transferer,
886  * bridge call between transfer peer (taking them off hold) to attended transfer channel.
887  *
888  * \return -1 on failure
889 */
890 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
891 {
892         struct ast_channel *transferer;
893         struct ast_channel *transferee;
894         const char *transferer_real_context;
895         char xferto[256] = "";
896         int res;
897         int outstate=0;
898         struct ast_channel *newchan;
899         struct ast_channel *xferchan;
900         struct ast_bridge_thread_obj *tobj;
901         struct ast_bridge_config bconfig;
902         struct ast_frame *f;
903         int l;
904
905         ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
906         set_peers(&transferer, &transferee, peer, chan, sense);
907         transferer_real_context = real_ctx(transferer, transferee);
908         /* Start autoservice on chan while we talk to the originator */
909         ast_autoservice_start(transferee);
910         ast_indicate(transferee, AST_CONTROL_HOLD);
911         
912         /* Transfer */
913         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
914         if (res < 0) {
915                 finishup(transferee);
916                 return res;
917         }
918         if (res > 0) /* If they've typed a digit already, handle it */
919                 xferto[0] = (char) res;
920
921         /* this is specific of atxfer */
922         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
923         if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
924                 finishup(transferee);
925                 return res;
926         }
927         if (res == 0) {
928                 ast_log(LOG_WARNING, "Did not read data.\n");
929                 finishup(transferee);
930                 if (ast_stream_and_wait(transferer, "beeperr", ""))
931                         return -1;
932                 return FEATURE_RETURN_SUCCESS;
933         }
934
935         /* valid extension, res == 1 */
936         if (!ast_exists_extension(transferer, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
937                 ast_log(LOG_WARNING, "Extension %s does not exist in context %s\n",xferto,transferer_real_context);
938                 finishup(transferee);
939                 if (ast_stream_and_wait(transferer, "beeperr", ""))
940                         return -1;
941                 return FEATURE_RETURN_SUCCESS;
942         }
943
944         l = strlen(xferto);
945         snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);     /* append context */
946         newchan = ast_feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
947                 xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1);
948
949         if (!ast_check_hangup(transferer)) {
950                 /* Transferer is up - old behaviour */
951                 ast_indicate(transferer, -1);
952                 if (!newchan) {
953                         finishup(transferee);
954                         /* any reason besides user requested cancel and busy triggers the failed sound */
955                         if (outstate != AST_CONTROL_UNHOLD && outstate != AST_CONTROL_BUSY &&
956                                 ast_stream_and_wait(transferer, xferfailsound, ""))
957                                 return -1;
958                         if (ast_stream_and_wait(transferer, xfersound, ""))
959                                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
960                         return FEATURE_RETURN_SUCCESS;
961                 }
962
963                 if (check_compat(transferer, newchan)) {
964                         /* we do mean transferee here, NOT transferer */
965                         finishup(transferee);
966                         return -1;
967                 }
968                 memset(&bconfig,0,sizeof(struct ast_bridge_config));
969                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
970                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
971                 res = ast_bridge_call(transferer, newchan, &bconfig);
972                 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
973                         ast_hangup(newchan);
974                         if (ast_stream_and_wait(transferer, xfersound, ""))
975                                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
976                         finishup(transferee);
977                         transferer->_softhangup = 0;
978                         return FEATURE_RETURN_SUCCESS;
979                 }
980                 if (check_compat(transferee, newchan)) {
981                         finishup(transferee);
982                         return -1;
983                 }
984                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
985
986                 if ((ast_autoservice_stop(transferee) < 0)
987                  || (ast_waitfordigit(transferee, 100) < 0)
988                  || (ast_waitfordigit(newchan, 100) < 0)
989                  || ast_check_hangup(transferee)
990                  || ast_check_hangup(newchan)) {
991                         ast_hangup(newchan);
992                         return -1;
993                 }
994                 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Transfered/%s", transferee->name);
995                 if (!xferchan) {
996                         ast_hangup(newchan);
997                         return -1;
998                 }
999                 /* Make formats okay */
1000                 xferchan->readformat = transferee->readformat;
1001                 xferchan->writeformat = transferee->writeformat;
1002                 ast_channel_masquerade(xferchan, transferee);
1003                 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1004                 xferchan->_state = AST_STATE_UP;
1005                 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1006                 xferchan->_softhangup = 0;
1007                 if ((f = ast_read(xferchan)))
1008                         ast_frfree(f);
1009                 newchan->_state = AST_STATE_UP;
1010                 ast_clear_flag(newchan, AST_FLAGS_ALL);
1011                 newchan->_softhangup = 0;
1012                 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1013                         ast_hangup(xferchan);
1014                         ast_hangup(newchan);
1015                         return -1;
1016                 }
1017                 tobj->chan = xferchan;
1018                 tobj->peer = newchan;
1019                 tobj->bconfig = *config;
1020
1021                 if (ast_stream_and_wait(newchan, xfersound, ""))
1022                         ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1023                 ast_bridge_call_thread_launch(tobj);
1024                 return -1;      /* XXX meaning the channel is bridged ? */
1025         } else if (!ast_check_hangup(transferee)) {
1026                 /* act as blind transfer */
1027                 if (ast_autoservice_stop(transferee) < 0) {
1028                         ast_hangup(newchan);
1029                         return -1;
1030                 }
1031
1032                 if (!newchan) {
1033                         unsigned int tries = 0;
1034                         char *transferer_tech, *transferer_name = ast_strdupa(transferer->name);
1035
1036                         transferer_tech = strsep(&transferer_name, "/");
1037                         transferer_name = strsep(&transferer_name, "-");
1038
1039                         if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
1040                                 ast_log(LOG_WARNING, "Transferer has invalid channel name: '%s'\n", transferer->name);
1041                                 if (ast_stream_and_wait(transferee, "beeperr", ""))
1042                                         return -1;
1043                                 return FEATURE_RETURN_SUCCESS;
1044                         }
1045
1046                         ast_log(LOG_NOTICE, "We're trying to call %s/%s\n", transferer_tech, transferer_name);
1047                         newchan = ast_feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1048                                 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0);
1049                         while (!newchan && !atxferdropcall && tries < atxfercallbackretries) {
1050                                 /* Trying to transfer again */
1051                                 ast_autoservice_start(transferee);
1052                                 ast_indicate(transferee, AST_CONTROL_HOLD);
1053
1054                                 newchan = ast_feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
1055                                         xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1);
1056                                 if (ast_autoservice_stop(transferee) < 0) {
1057                                         if (newchan)
1058                                                 ast_hangup(newchan);
1059                                         return -1;
1060                                 }
1061                                 if (!newchan) {
1062                                         /* Transfer failed, sleeping */
1063                                         ast_debug(1, "Sleeping for %d ms before callback.\n", atxferloopdelay);
1064                                         ast_safe_sleep(transferee, atxferloopdelay);
1065                                         ast_debug(1, "Trying to callback...\n");
1066                                         newchan = ast_feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1067                                                 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0);
1068                                 }
1069                                 tries++;
1070                         }
1071                 }
1072                 if (!newchan)
1073                         return -1;
1074
1075                 /* newchan is up, we should prepare transferee and bridge them */
1076                 if (check_compat(transferee, newchan)) {
1077                         finishup(transferee);
1078                         return -1;
1079                 }
1080                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
1081
1082                 if ((ast_waitfordigit(transferee, 100) < 0)
1083                    || (ast_waitfordigit(newchan, 100) < 0)
1084                    || ast_check_hangup(transferee)
1085                    || ast_check_hangup(newchan)) {
1086                         ast_hangup(newchan);
1087                         return -1;
1088                 }
1089
1090                 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Transfered/%s", transferee->name);
1091                 if (!xferchan) {
1092                         ast_hangup(newchan);
1093                         return -1;
1094                 }
1095                 /* Make formats okay */
1096                 xferchan->readformat = transferee->readformat;
1097                 xferchan->writeformat = transferee->writeformat;
1098                 ast_channel_masquerade(xferchan, transferee);
1099                 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1100                 xferchan->_state = AST_STATE_UP;
1101                 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1102                 xferchan->_softhangup = 0;
1103                 if ((f = ast_read(xferchan)))
1104                         ast_frfree(f);
1105                 newchan->_state = AST_STATE_UP;
1106                 ast_clear_flag(newchan, AST_FLAGS_ALL);
1107                 newchan->_softhangup = 0;
1108                 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1109                         ast_hangup(xferchan);
1110                         ast_hangup(newchan);
1111                         return -1;
1112                 }
1113                 tobj->chan = xferchan;
1114                 tobj->peer = newchan;
1115                 tobj->bconfig = *config;
1116
1117                 if (ast_stream_and_wait(newchan, xfersound, ""))
1118                         ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1119                 ast_bridge_call_thread_launch(tobj);
1120                 return -1;      /* XXX meaning the channel is bridged ? */
1121         } else {
1122                 /* Transferee hung up */
1123                 finishup(transferee);
1124                 return -1;
1125         }
1126 }
1127
1128 /* add atxfer and automon as undefined so you can only use em if you configure them */
1129 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
1130
1131 AST_RWLOCK_DEFINE_STATIC(features_lock);
1132
1133 static struct ast_call_feature builtin_features[] = 
1134 {
1135         { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1136         { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1137         { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1138         { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1139         { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1140 };
1141
1142
1143 static AST_LIST_HEAD_STATIC(feature_list,ast_call_feature);
1144
1145 /*! \brief register new feature into feature_list*/
1146 void ast_register_feature(struct ast_call_feature *feature)
1147 {
1148         if (!feature) {
1149                 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
1150                 return;
1151         }
1152   
1153         AST_LIST_LOCK(&feature_list);
1154         AST_LIST_INSERT_HEAD(&feature_list,feature,feature_entry);
1155         AST_LIST_UNLOCK(&feature_list);
1156
1157         ast_verb(2, "Registered Feature '%s'\n",feature->sname);
1158 }
1159
1160 /*! 
1161  * \brief Add new feature group
1162  * \param fgname feature group name.
1163  *
1164  * Add new feature group to the feature group list insert at head of list.
1165  * \note This function MUST be called while feature_groups is locked.
1166 */
1167 static struct feature_group* register_group(const char *fgname)
1168 {
1169         struct feature_group *fg;
1170
1171         if (!fgname) {
1172                 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
1173                 return NULL;
1174         }
1175
1176         if (!(fg = ast_calloc(1, sizeof(*fg))))
1177                 return NULL;
1178
1179         if (ast_string_field_init(fg, 128)) {
1180                 ast_free(fg);
1181                 return NULL;
1182         }
1183
1184         ast_string_field_set(fg, gname, fgname);
1185
1186         AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
1187
1188         ast_verb(2, "Registered group '%s'\n", fg->gname);
1189
1190         return fg;
1191 }
1192
1193 /*! 
1194  * \brief Add feature to group
1195  * \param fg feature group
1196  * \param exten
1197  * \param feature feature to add.
1198  *
1199  * Check fg and feature specified, add feature to list
1200  * \note This function MUST be called while feature_groups is locked. 
1201 */
1202 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature) 
1203 {
1204         struct feature_group_exten *fge;
1205
1206         if (!(fge = ast_calloc(1, sizeof(*fge))))
1207                 return;
1208
1209         if (ast_string_field_init(fge, 128)) {
1210                 ast_free(fge);
1211                 return;
1212         }
1213
1214         if (!fg) {
1215                 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
1216                 return;
1217         }
1218
1219         if (!feature) {
1220                 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
1221                 return;
1222         }
1223
1224         ast_string_field_set(fge, exten, (ast_strlen_zero(exten) ? feature->exten : exten));
1225
1226         fge->feature = feature;
1227
1228         AST_LIST_INSERT_HEAD(&fg->features, fge, entry);                
1229
1230         ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
1231                                         feature->sname, fg->gname, exten);
1232 }
1233
1234 void ast_unregister_feature(struct ast_call_feature *feature)
1235 {
1236         if (!feature)
1237                 return;
1238
1239         AST_LIST_LOCK(&feature_list);
1240         AST_LIST_REMOVE(&feature_list,feature,feature_entry);
1241         AST_LIST_UNLOCK(&feature_list);
1242         ast_free(feature);
1243 }
1244
1245 /*! \brief Remove all features in the list */
1246 static void ast_unregister_features(void)
1247 {
1248         struct ast_call_feature *feature;
1249
1250         AST_LIST_LOCK(&feature_list);
1251         while ((feature = AST_LIST_REMOVE_HEAD(&feature_list,feature_entry)))
1252                 ast_free(feature);
1253         AST_LIST_UNLOCK(&feature_list);
1254 }
1255
1256 /*! \brief find a call feature by name */
1257 static struct ast_call_feature *find_dynamic_feature(const char *name)
1258 {
1259         struct ast_call_feature *tmp;
1260
1261         AST_LIST_TRAVERSE(&feature_list, tmp, feature_entry) {
1262                 if (!strcasecmp(tmp->sname, name))
1263                         break;
1264         }
1265
1266         return tmp;
1267 }
1268
1269 /*! \brief Remove all feature groups in the list */
1270 static void ast_unregister_groups(void)
1271 {
1272         struct feature_group *fg;
1273         struct feature_group_exten *fge;
1274
1275         AST_RWLIST_WRLOCK(&feature_groups);
1276         while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
1277                 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
1278                         ast_string_field_free_memory(fge);
1279                         ast_free(fge);
1280                 }
1281
1282                 ast_string_field_free_memory(fg);
1283                 ast_free(fg);
1284         }
1285         AST_RWLIST_UNLOCK(&feature_groups);
1286 }
1287
1288 /*! 
1289  * \brief Find a group by name 
1290  * \param name feature name
1291  * \retval feature group on success.
1292  * \retval NULL on failure.
1293 */
1294 static struct feature_group *find_group(const char *name) {
1295         struct feature_group *fg = NULL;
1296
1297         AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
1298                 if (!strcasecmp(fg->gname, name))
1299                         break;
1300         }
1301
1302         return fg;
1303 }
1304
1305 void ast_rdlock_call_features(void)
1306 {
1307         ast_rwlock_rdlock(&features_lock);
1308 }
1309
1310 void ast_unlock_call_features(void)
1311 {
1312         ast_rwlock_unlock(&features_lock);
1313 }
1314
1315 struct ast_call_feature *ast_find_call_feature(const char *name)
1316 {
1317         int x;
1318         for (x = 0; x < FEATURES_COUNT; x++) {
1319                 if (!strcasecmp(name, builtin_features[x].sname))
1320                         return &builtin_features[x];
1321         }
1322         return NULL;
1323 }
1324
1325 /*!
1326  * \brief exec an app by feature 
1327  * \param chan,peer,config,code,sense,data
1328  *
1329  * Find a feature, determine which channel activated
1330  * \retval FEATURE_RETURN_PBX_KEEPALIVE,FEATURE_RETURN_NO_HANGUP_PEER
1331  * \retval -1 error.
1332  * \retval -2 when an application cannot be found.
1333 */
1334 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
1335 {
1336         struct ast_app *app;
1337         struct ast_call_feature *feature = data;
1338         struct ast_channel *work, *idle;
1339         int res;
1340
1341         if (!feature) { /* shouldn't ever happen! */
1342                 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
1343                 return -1; 
1344         }
1345
1346         if (sense == FEATURE_SENSE_CHAN) {
1347                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
1348                         return FEATURE_RETURN_KEEPTRYING;
1349                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
1350                         work = chan;
1351                         idle = peer;
1352                 } else {
1353                         work = peer;
1354                         idle = chan;
1355                 }
1356         } else {
1357                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
1358                         return FEATURE_RETURN_KEEPTRYING;
1359                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
1360                         work = peer;
1361                         idle = chan;
1362                 } else {
1363                         work = chan;
1364                         idle = peer;
1365                 }
1366         }
1367
1368         if (!(app = pbx_findapp(feature->app))) {
1369                 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
1370                 return -2;
1371         }
1372
1373         ast_autoservice_start(idle);
1374         
1375         if (!ast_strlen_zero(feature->moh_class))
1376                 ast_moh_start(idle, feature->moh_class, NULL);
1377
1378         res = pbx_exec(work, app, feature->app_args);
1379
1380         if (!ast_strlen_zero(feature->moh_class))
1381                 ast_moh_stop(idle);
1382
1383         ast_autoservice_stop(idle);
1384
1385         if (res == AST_PBX_KEEPALIVE)
1386                 return FEATURE_RETURN_PBX_KEEPALIVE;
1387         else if (res == AST_PBX_NO_HANGUP_PEER)
1388                 return FEATURE_RETURN_NO_HANGUP_PEER;
1389         else if (res)
1390                 return FEATURE_RETURN_SUCCESSBREAK;
1391         
1392         return FEATURE_RETURN_SUCCESS;  /*! \todo XXX should probably return res */
1393 }
1394
1395 static void unmap_features(void)
1396 {
1397         int x;
1398
1399         ast_rwlock_wrlock(&features_lock);
1400         for (x = 0; x < FEATURES_COUNT; x++)
1401                 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
1402         ast_rwlock_unlock(&features_lock);
1403 }
1404
1405 static int remap_feature(const char *name, const char *value)
1406 {
1407         int x, res = -1;
1408
1409         ast_rwlock_wrlock(&features_lock);
1410         for (x = 0; x < FEATURES_COUNT; x++) {
1411                 if (strcasecmp(builtin_features[x].sname, name))
1412                         continue;
1413
1414                 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
1415                 res = 0;
1416                 break;
1417         }
1418         ast_rwlock_unlock(&features_lock);
1419
1420         return res;
1421 }
1422
1423 /*!
1424  * \brief Check the dynamic features
1425  * \param chan,peer,config,code,sense
1426  *
1427  * Lock features list, browse for code, unlock list
1428  * \retval res on success.
1429  * \retval -1 on failure.
1430 */
1431 static int ast_feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense)
1432 {
1433         int x;
1434         struct ast_flags features;
1435         int res = FEATURE_RETURN_PASSDIGITS;
1436         struct ast_call_feature *feature;
1437         struct feature_group *fg = NULL;
1438         struct feature_group_exten *fge;
1439         const char *dynamic_features=pbx_builtin_getvar_helper(chan,"DYNAMIC_FEATURES");
1440         char *tmp, *tok;
1441
1442         if (sense == FEATURE_SENSE_CHAN)
1443                 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
1444         else
1445                 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
1446         ast_debug(3, "Feature interpret: chan=%s, peer=%s, sense=%d, features=%d\n", chan->name, peer->name, sense, features.flags);
1447
1448         ast_rwlock_rdlock(&features_lock);
1449         for (x = 0; x < FEATURES_COUNT; x++) {
1450                 if ((ast_test_flag(&features, builtin_features[x].feature_mask)) &&
1451                     !ast_strlen_zero(builtin_features[x].exten)) {
1452                         /* Feature is up for consideration */
1453                         if (!strcmp(builtin_features[x].exten, code)) {
1454                                 res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
1455                                 break;
1456                         } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
1457                                 if (res == FEATURE_RETURN_PASSDIGITS)
1458                                         res = FEATURE_RETURN_STOREDIGITS;
1459                         }
1460                 }
1461         }
1462         ast_rwlock_unlock(&features_lock);
1463
1464         if (ast_strlen_zero(dynamic_features))
1465                 return res;
1466
1467         tmp = ast_strdupa(dynamic_features);
1468
1469         while ((tok = strsep(&tmp, "#"))) {
1470                 AST_RWLIST_RDLOCK(&feature_groups);
1471
1472                 fg = find_group(tok);
1473
1474                 if (fg) {
1475                         AST_LIST_TRAVERSE(&fg->features, fge, entry) {
1476                                 if (strcasecmp(fge->exten, code))
1477                                         continue;
1478
1479                                 res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
1480                                 if (res != FEATURE_RETURN_KEEPTRYING) {
1481                                         AST_RWLIST_UNLOCK(&feature_groups);
1482                                         break;
1483                                 }
1484                                 res = FEATURE_RETURN_PASSDIGITS;
1485                         }
1486                         if (fge)
1487                                 break;
1488                 }
1489
1490                 AST_RWLIST_UNLOCK(&feature_groups);
1491                 AST_LIST_LOCK(&feature_list);
1492
1493                 if(!(feature = find_dynamic_feature(tok))) {
1494                         AST_LIST_UNLOCK(&feature_list);
1495                         continue;
1496                 }
1497                         
1498                 /* Feature is up for consideration */
1499                 if (!strcmp(feature->exten, code)) {
1500                         ast_verb(3, " Feature Found: %s exten: %s\n",feature->sname, tok);
1501                         res = feature->operation(chan, peer, config, code, sense, feature);
1502                         if (res != FEATURE_RETURN_KEEPTRYING) {
1503                                 AST_LIST_UNLOCK(&feature_list);
1504                                 break;
1505                         }
1506                         res = FEATURE_RETURN_PASSDIGITS;
1507                 } else if (!strncmp(feature->exten, code, strlen(code)))
1508                         res = FEATURE_RETURN_STOREDIGITS;
1509
1510                 AST_LIST_UNLOCK(&feature_list);
1511         }
1512         
1513         return res;
1514 }
1515
1516 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
1517 {
1518         int x;
1519         
1520         ast_clear_flag(config, AST_FLAGS_ALL);
1521
1522         ast_rwlock_rdlock(&features_lock);
1523         for (x = 0; x < FEATURES_COUNT; x++) {
1524                 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
1525                         continue;
1526
1527                 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
1528                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
1529
1530                 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
1531                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
1532         }
1533         ast_rwlock_unlock(&features_lock);
1534         
1535         if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
1536                 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
1537
1538                 if (dynamic_features) {
1539                         char *tmp = ast_strdupa(dynamic_features);
1540                         char *tok;
1541                         struct ast_call_feature *feature;
1542
1543                         /* while we have a feature */
1544                         while ((tok = strsep(&tmp, "#"))) {
1545                                 AST_LIST_LOCK(&feature_list);
1546                                 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
1547                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
1548                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
1549                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
1550                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
1551                                 }
1552                                 AST_LIST_UNLOCK(&feature_list);
1553                         }
1554                 }
1555         }
1556 }
1557
1558 /*! 
1559  * \brief Get feature and dial
1560  * \param caller,transferee,type,format,data,timeout,outstate,cid_num,cid_name,igncallerstate
1561  *
1562  * Request channel, set channel variables, initiate call,check if they want to disconnect
1563  * go into loop, check if timeout has elapsed, check if person to be transfered hung up,
1564  * check for answer break loop, set cdr return channel.
1565  *
1566  * \todo XXX Check - this is very similar to the code in channel.c 
1567  * \return always a channel
1568 */
1569 static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate)
1570 {
1571         int state = 0;
1572         int cause = 0;
1573         int to;
1574         struct ast_channel *chan;
1575         struct ast_channel *monitor_chans[2];
1576         struct ast_channel *active_channel;
1577         int res = 0, ready = 0;
1578         
1579         if ((chan = ast_request(type, format, data, &cause))) {
1580                 ast_set_callerid(chan, cid_num, cid_name, cid_num);
1581                 ast_channel_inherit_variables(caller, chan);    
1582                 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller->name);
1583                 if (!chan->cdr) {
1584                         chan->cdr=ast_cdr_alloc();
1585                         if (chan->cdr) {
1586                                 ast_cdr_init(chan->cdr, chan); /* initilize our channel's cdr */
1587                                 ast_cdr_start(chan->cdr);
1588                         }
1589                 }
1590                         
1591                 if (!ast_call(chan, data, timeout)) {
1592                         struct timeval started;
1593                         int x, len = 0;
1594                         char *disconnect_code = NULL, *dialed_code = NULL;
1595
1596                         ast_indicate(caller, AST_CONTROL_RINGING);
1597                         /* support dialing of the featuremap disconnect code while performing an attended tranfer */
1598                         ast_rwlock_rdlock(&features_lock);
1599                         for (x = 0; x < FEATURES_COUNT; x++) {
1600                                 if (strcasecmp(builtin_features[x].sname, "disconnect"))
1601                                         continue;
1602
1603                                 disconnect_code = builtin_features[x].exten;
1604                                 len = strlen(disconnect_code) + 1;
1605                                 dialed_code = alloca(len);
1606                                 memset(dialed_code, 0, len);
1607                                 break;
1608                         }
1609                         ast_rwlock_unlock(&features_lock);
1610                         x = 0;
1611                         started = ast_tvnow();
1612                         to = timeout;
1613
1614                         ast_poll_channel_add(caller, chan);
1615
1616                         while (!((transferee && ast_check_hangup(transferee)) && (!igncallerstate && ast_check_hangup(caller))) && timeout && (chan->_state != AST_STATE_UP)) {
1617                                 struct ast_frame *f = NULL;
1618
1619                                 monitor_chans[0] = caller;
1620                                 monitor_chans[1] = chan;
1621                                 active_channel = ast_waitfor_n(monitor_chans, 2, &to);
1622
1623                                 /* see if the timeout has been violated */
1624                                 if(ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
1625                                         state = AST_CONTROL_UNHOLD;
1626                                         ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
1627                                         break; /*doh! timeout*/
1628                                 }
1629
1630                                 if (!active_channel)
1631                                         continue;
1632
1633                                 if (chan && (chan == active_channel)){
1634                                         f = ast_read(chan);
1635                                         if (f == NULL) { /*doh! where'd he go?*/
1636                                                 state = AST_CONTROL_HANGUP;
1637                                                 res = 0;
1638                                                 break;
1639                                         }
1640                                         
1641                                         if (f->frametype == AST_FRAME_CONTROL || f->frametype == AST_FRAME_DTMF || f->frametype == AST_FRAME_TEXT) {
1642                                                 if (f->subclass == AST_CONTROL_RINGING) {
1643                                                         state = f->subclass;
1644                                                         ast_verb(3, "%s is ringing\n", chan->name);
1645                                                         ast_indicate(caller, AST_CONTROL_RINGING);
1646                                                 } else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
1647                                                         state = f->subclass;
1648                                                         ast_verb(3, "%s is busy\n", chan->name);
1649                                                         ast_indicate(caller, AST_CONTROL_BUSY);
1650                                                         ast_frfree(f);
1651                                                         f = NULL;
1652                                                         break;
1653                                                 } else if (f->subclass == AST_CONTROL_ANSWER) {
1654                                                         /* This is what we are hoping for */
1655                                                         state = f->subclass;
1656                                                         ast_frfree(f);
1657                                                         f = NULL;
1658                                                         ready=1;
1659                                                         break;
1660                                                 } else if (f->subclass != -1) {
1661                                                         ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass);
1662                                                 }
1663                                                 /* else who cares */
1664                                         }
1665
1666                                 } else if (caller && (active_channel == caller)) {
1667                                         f = ast_read(caller);
1668                                         if (f == NULL) { /*doh! where'd he go?*/
1669                                                 if (!igncallerstate) {
1670                                                         if (ast_check_hangup(caller) && !ast_check_hangup(chan)) {
1671                                                                 /* make this a blind transfer */
1672                                                                 ready = 1;
1673                                                                 break;
1674                                                         }
1675                                                         state = AST_CONTROL_HANGUP;
1676                                                         res = 0;
1677                                                         break;
1678                                                 }
1679                                         } else {
1680                                         
1681                                                 if (f->frametype == AST_FRAME_DTMF) {
1682                                                         dialed_code[x++] = f->subclass;
1683                                                         dialed_code[x] = '\0';
1684                                                         if (strlen(dialed_code) == len) {
1685                                                                 x = 0;
1686                                                         } else if (x && strncmp(dialed_code, disconnect_code, x)) {
1687                                                                 x = 0;
1688                                                                 dialed_code[x] = '\0';
1689                                                         }
1690                                                         if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
1691                                                                 /* Caller Canceled the call */
1692                                                                 state = AST_CONTROL_UNHOLD;
1693                                                                 ast_frfree(f);
1694                                                                 f = NULL;
1695                                                                 break;
1696                                                         }
1697                                                 }
1698                                         }
1699                                 }
1700                                 if (f)
1701                                         ast_frfree(f);
1702                         } /* end while */
1703
1704                         ast_poll_channel_del(caller, chan);
1705
1706                 } else
1707                         ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
1708         } else {
1709                 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
1710                 switch(cause) {
1711                 case AST_CAUSE_BUSY:
1712                         state = AST_CONTROL_BUSY;
1713                         break;
1714                 case AST_CAUSE_CONGESTION:
1715                         state = AST_CONTROL_CONGESTION;
1716                         break;
1717                 }
1718         }
1719         
1720         ast_indicate(caller, -1);
1721         if (chan && ready) {
1722                 if (chan->_state == AST_STATE_UP) 
1723                         state = AST_CONTROL_ANSWER;
1724                 res = 0;
1725         } else if(chan) {
1726                 res = -1;
1727                 ast_hangup(chan);
1728                 chan = NULL;
1729         } else {
1730                 res = -1;
1731         }
1732         
1733         if (outstate)
1734                 *outstate = state;
1735
1736         if (chan && res <= 0) {
1737                 if (chan->cdr || (chan->cdr = ast_cdr_alloc())) {
1738                         char tmp[256];
1739                         ast_cdr_init(chan->cdr, chan);
1740                         snprintf(tmp, 256, "%s/%s", type, (char *)data);
1741                         ast_cdr_setapp(chan->cdr,"Dial",tmp);
1742                         ast_cdr_update(chan);
1743                         ast_cdr_start(chan->cdr);
1744                         ast_cdr_end(chan->cdr);
1745                         /* If the cause wasn't handled properly */
1746                         if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
1747                                 ast_cdr_failed(chan->cdr);
1748                 } else {
1749                         ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
1750                 }
1751         }
1752         
1753         return chan;
1754 }
1755
1756 /*!
1757  * \brief bridge the call and set CDR
1758  * \param chan,peer,config
1759  * 
1760  * Set start time, check for two channels,check if monitor on
1761  * check for feature activation, create new CDR
1762  * \retval res on success.
1763  * \retval -1 on failure to bridge.
1764 */
1765 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
1766 {
1767         /* Copy voice back and forth between the two channels.  Give the peer
1768            the ability to transfer calls with '#<extension' syntax. */
1769         struct ast_frame *f;
1770         struct ast_channel *who;
1771         char chan_featurecode[FEATURE_MAX_LEN + 1]="";
1772         char peer_featurecode[FEATURE_MAX_LEN + 1]="";
1773         int res;
1774         int diff;
1775         int hasfeatures=0;
1776         int hadfeatures=0;
1777         struct ast_option_header *aoh;
1778         struct ast_bridge_config backup_config;
1779         struct ast_cdr *bridge_cdr;
1780
1781         memset(&backup_config, 0, sizeof(backup_config));
1782
1783         config->start_time = ast_tvnow();
1784
1785         if (chan && peer) {
1786                 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
1787                 pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
1788         } else if (chan)
1789                 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
1790
1791         if (monitor_ok) {
1792                 const char *monitor_exec;
1793                 struct ast_channel *src = NULL;
1794                 if (!monitor_app) { 
1795                         if (!(monitor_app = pbx_findapp("Monitor")))
1796                                 monitor_ok=0;
1797                 }
1798                 if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR"))) 
1799                         src = chan;
1800                 else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
1801                         src = peer;
1802                 if (monitor_app && src) {
1803                         char *tmp = ast_strdupa(monitor_exec);
1804                         pbx_exec(src, monitor_app, tmp);
1805                 }
1806         }
1807         
1808         set_config_flags(chan, peer, config);
1809         config->firstpass = 1;
1810
1811         /* Answer if need be */
1812         if (ast_answer(chan))
1813                 return -1;
1814         peer->appl = "Bridged Call";
1815         peer->data = chan->name;
1816
1817         /* copy the userfield from the B-leg to A-leg if applicable */
1818         if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
1819                 char tmp[256];
1820                 if (!ast_strlen_zero(chan->cdr->userfield)) {
1821                         snprintf(tmp, sizeof(tmp), "%s;%s", chan->cdr->userfield, peer->cdr->userfield);
1822                         ast_cdr_appenduserfield(chan, tmp);
1823                 } else
1824                         ast_cdr_setuserfield(chan, peer->cdr->userfield);
1825                 /* free the peer's cdr without ast_cdr_free complaining */
1826                 ast_free(peer->cdr);
1827                 peer->cdr = NULL;
1828         }
1829
1830         for (;;) {
1831                 struct ast_channel *other;      /* used later */
1832
1833                 res = ast_channel_bridge(chan, peer, config, &f, &who);
1834
1835                 if (config->feature_timer) {
1836                         /* Update time limit for next pass */
1837                         diff = ast_tvdiff_ms(ast_tvnow(), config->start_time);
1838                         config->feature_timer -= diff;
1839                         if (hasfeatures) {
1840                                 /* Running on backup config, meaning a feature might be being
1841                                    activated, but that's no excuse to keep things going 
1842                                    indefinitely! */
1843                                 if (backup_config.feature_timer && ((backup_config.feature_timer -= diff) <= 0)) {
1844                                         ast_debug(1, "Timed out, realtime this time!\n");
1845                                         config->feature_timer = 0;
1846                                         who = chan;
1847                                         if (f)
1848                                                 ast_frfree(f);
1849                                         f = NULL;
1850                                         res = 0;
1851                                 } else if (config->feature_timer <= 0) {
1852                                         /* Not *really* out of time, just out of time for
1853                                            digits to come in for features. */
1854                                         ast_debug(1, "Timed out for feature!\n");
1855                                         if (!ast_strlen_zero(peer_featurecode)) {
1856                                                 ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
1857                                                 memset(peer_featurecode, 0, sizeof(peer_featurecode));
1858                                         }
1859                                         if (!ast_strlen_zero(chan_featurecode)) {
1860                                                 ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
1861                                                 memset(chan_featurecode, 0, sizeof(chan_featurecode));
1862                                         }
1863                                         if (f)
1864                                                 ast_frfree(f);
1865                                         hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
1866                                         if (!hasfeatures) {
1867                                                 /* Restore original (possibly time modified) bridge config */
1868                                                 memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
1869                                                 memset(&backup_config, 0, sizeof(backup_config));
1870                                         }
1871                                         hadfeatures = hasfeatures;
1872                                         /* Continue as we were */
1873                                         continue;
1874                                 } else if (!f) {
1875                                         /* The bridge returned without a frame and there is a feature in progress.
1876                                          * However, we don't think the feature has quite yet timed out, so just
1877                                          * go back into the bridge. */
1878                                         continue;
1879                                 }
1880                         } else {
1881                                 if (config->feature_timer <=0) {
1882                                         /* We ran out of time */
1883                                         config->feature_timer = 0;
1884                                         who = chan;
1885                                         if (f)
1886                                                 ast_frfree(f);
1887                                         f = NULL;
1888                                         res = 0;
1889                                 }
1890                         }
1891                 }
1892                 if (res < 0) {
1893                         if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer))
1894                                 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
1895                         return -1;
1896                 }
1897                 
1898                 if (!f || (f->frametype == AST_FRAME_CONTROL &&
1899                                 (f->subclass == AST_CONTROL_HANGUP || f->subclass == AST_CONTROL_BUSY || 
1900                                         f->subclass == AST_CONTROL_CONGESTION))) {
1901                         res = -1;
1902                         break;
1903                 }
1904                 /* many things should be sent to the 'other' channel */
1905                 other = (who == chan) ? peer : chan;
1906                 if (f->frametype == AST_FRAME_CONTROL) {
1907                         switch (f->subclass) {
1908                         case AST_CONTROL_RINGING:
1909                         case AST_CONTROL_FLASH:
1910                         case -1:
1911                                 ast_indicate(other, f->subclass);
1912                                 break;
1913                         case AST_CONTROL_HOLD:
1914                         case AST_CONTROL_UNHOLD:
1915                                 ast_indicate_data(other, f->subclass, f->data, f->datalen);
1916                                 break;
1917                         case AST_CONTROL_OPTION:
1918                                 aoh = f->data;
1919                                 /* Forward option Requests */
1920                                 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
1921                                         ast_channel_setoption(other, ntohs(aoh->option), aoh->data, 
1922                                                 f->datalen - sizeof(struct ast_option_header), 0);
1923                                 }
1924                                 break;
1925                         }
1926                 } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
1927                         /* eat it */
1928                 } else if (f->frametype == AST_FRAME_DTMF) {
1929                         char *featurecode;
1930                         int sense;
1931
1932                         hadfeatures = hasfeatures;
1933                         /* This cannot overrun because the longest feature is one shorter than our buffer */
1934                         if (who == chan) {
1935                                 sense = FEATURE_SENSE_CHAN;
1936                                 featurecode = chan_featurecode;
1937                         } else  {
1938                                 sense = FEATURE_SENSE_PEER;
1939                                 featurecode = peer_featurecode;
1940                         }
1941                         /*! append the event to featurecode. we rely on the string being zero-filled, and
1942                          * not overflowing it. 
1943                          * \todo XXX how do we guarantee the latter ?
1944                          */
1945                         featurecode[strlen(featurecode)] = f->subclass;
1946                         /* Get rid of the frame before we start doing "stuff" with the channels */
1947                         ast_frfree(f);
1948                         f = NULL;
1949                         config->feature_timer = backup_config.feature_timer;
1950                         res = ast_feature_interpret(chan, peer, config, featurecode, sense);
1951                         switch(res) {
1952                         case FEATURE_RETURN_PASSDIGITS:
1953                                 ast_dtmf_stream(other, who, featurecode, 0, 0);
1954                                 /* Fall through */
1955                         case FEATURE_RETURN_SUCCESS:
1956                                 memset(featurecode, 0, sizeof(chan_featurecode));
1957                                 break;
1958                         }
1959                         if (res >= FEATURE_RETURN_PASSDIGITS) {
1960                                 res = 0;
1961                         } else 
1962                                 break;
1963                         hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
1964                         if (hadfeatures && !hasfeatures) {
1965                                 /* Restore backup */
1966                                 memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
1967                                 memset(&backup_config, 0, sizeof(struct ast_bridge_config));
1968                         } else if (hasfeatures) {
1969                                 if (!hadfeatures) {
1970                                         /* Backup configuration */
1971                                         memcpy(&backup_config, config, sizeof(struct ast_bridge_config));
1972                                         /* Setup temporary config options */
1973                                         config->play_warning = 0;
1974                                         ast_clear_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
1975                                         ast_clear_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
1976                                         config->warning_freq = 0;
1977                                         config->warning_sound = NULL;
1978                                         config->end_sound = NULL;
1979                                         config->start_sound = NULL;
1980                                         config->firstpass = 0;
1981                                 }
1982                                 config->start_time = ast_tvnow();
1983                                 config->feature_timer = featuredigittimeout;
1984                                 ast_debug(1, "Set time limit to %ld\n", config->feature_timer);
1985                         }
1986                 }
1987                 if (f)
1988                         ast_frfree(f);
1989
1990         }
1991         /* arrange the cdrs */
1992         bridge_cdr = ast_cdr_alloc();
1993         if (bridge_cdr) {
1994                 if (chan->cdr && peer->cdr) { /* both of them? merge */
1995                         ast_cdr_init(bridge_cdr,chan); /* seems more logicaller to use the  destination as a base, but, really, it's random */
1996                         ast_cdr_start(bridge_cdr); /* now is the time to start */
1997                         
1998                         /* absorb the channel cdr */
1999                         ast_cdr_merge(bridge_cdr, chan->cdr);
2000                         if (!ast_test_flag(chan->cdr, AST_CDR_FLAG_LOCKED))
2001                                 ast_cdr_discard(chan->cdr); /* if locked cdrs are in chan, they are taken over in the merge */
2002                         
2003                         /* absorb the peer cdr */
2004                         ast_cdr_merge(bridge_cdr, peer->cdr);
2005                         if (!ast_test_flag(peer->cdr, AST_CDR_FLAG_LOCKED))
2006                                 ast_cdr_discard(peer->cdr); /* if locked cdrs are in peer, they are taken over in the merge */
2007                         
2008                         peer->cdr = NULL;
2009                         chan->cdr = bridge_cdr; /* make this available to the rest of the world via the chan while the call is in progress */
2010                 } else if (chan->cdr) {
2011                         /* take the cdr from the channel - literally */
2012                         ast_cdr_init(bridge_cdr,chan);
2013                         /* absorb this data */
2014                         ast_cdr_merge(bridge_cdr, chan->cdr);
2015                         if (!ast_test_flag(chan->cdr, AST_CDR_FLAG_LOCKED))
2016                                 ast_cdr_discard(chan->cdr); /* if locked cdrs are in chan, they are taken over in the merge */
2017                         chan->cdr = bridge_cdr; /* make this available to the rest of the world via the chan while the call is in progress */
2018                 } else if (peer->cdr) {
2019                         /* take the cdr from the peer - literally */
2020                         ast_cdr_init(bridge_cdr,peer);
2021                         /* absorb this data */
2022                         ast_cdr_merge(bridge_cdr, peer->cdr);
2023                         if (!ast_test_flag(peer->cdr, AST_CDR_FLAG_LOCKED))
2024                                 ast_cdr_discard(peer->cdr); /* if locked cdrs are in chan, they are taken over in the merge */
2025                         peer->cdr = NULL;
2026                         peer->cdr = bridge_cdr; /* make this available to the rest of the world via the chan while the call is in progress */
2027                 } else {
2028                         /* make up a new cdr */
2029                         ast_cdr_init(bridge_cdr,chan); /* eh, just pick one of them */
2030                         chan->cdr = bridge_cdr; /*  */
2031                 }
2032                 if (ast_strlen_zero(bridge_cdr->dstchannel)) {
2033                         if (strcmp(bridge_cdr->channel, peer->name) != 0)
2034                                 ast_cdr_setdestchan(bridge_cdr, peer->name);
2035                         else
2036                                 ast_cdr_setdestchan(bridge_cdr, chan->name);
2037                 }
2038         }
2039         return res;
2040 }
2041
2042 /*! \brief Output parking event to manager */
2043 static void post_manager_event(const char *s, struct parkeduser *pu)
2044 {
2045         manager_event(EVENT_FLAG_CALL, s,
2046                 "Exten: %s\r\n"
2047                 "Channel: %s\r\n"
2048                 "CallerIDNum: %s\r\n"
2049                 "CallerIDName: %s\r\n\r\n",
2050                 pu->parkingexten, 
2051                 pu->chan->name,
2052                 S_OR(pu->chan->cid.cid_num, "<unknown>"),
2053                 S_OR(pu->chan->cid.cid_name, "<unknown>")
2054                 );
2055 }
2056
2057 /*! 
2058  * \brief Take care of parked calls and unpark them if needed 
2059  * \param ignore unused var.
2060  * 
2061  * Start inf loop, lock parking lot, check if any parked channels have gone above timeout
2062  * if so, remove channel from parking lot and return it to the extension that parked it.
2063  * Check if parked channel decided to hangup, wait until next FD via select().
2064 */
2065 static void *do_parking_thread(void *ignore)
2066 {
2067         char parkingslot[AST_MAX_EXTENSION];
2068         fd_set rfds, efds;      /* results from previous select, to be preserved across loops. */
2069
2070         FD_ZERO(&rfds);
2071         FD_ZERO(&efds);
2072
2073         for (;;) {
2074                 struct parkeduser *pu;
2075                 int ms = -1;    /* select timeout, uninitialized */
2076                 int max = -1;   /* max fd, none there yet */
2077                 fd_set nrfds, nefds;    /* args for the next select */
2078                 FD_ZERO(&nrfds);
2079                 FD_ZERO(&nefds);
2080
2081                 AST_LIST_LOCK(&parkinglot);
2082                 AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot, pu, list) {
2083                         struct ast_channel *chan = pu->chan;    /* shorthand */
2084                         int tms;        /* timeout for this item */
2085                         int x;          /* fd index in channel */
2086                         struct ast_context *con;
2087
2088                         if (pu->notquiteyet) /* Pretend this one isn't here yet */
2089                                 continue;
2090                         tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
2091                         if (tms > pu->parkingtime) {
2092                                 ast_indicate(chan, AST_CONTROL_UNHOLD);
2093                                 /* Get chan, exten from derived kludge */
2094                                 if (pu->peername[0]) {
2095                                         char *peername = ast_strdupa(pu->peername);
2096                                         char *cp = strrchr(peername, '-');
2097                                         if (cp) 
2098                                                 *cp = 0;
2099                                         con = ast_context_find(parking_con_dial);
2100                                         if (!con) {
2101                                                 con = ast_context_create(NULL, parking_con_dial, registrar);
2102                                                 if (!con)
2103                                                         ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", parking_con_dial);
2104                                         }
2105                                         if (con) {
2106                                                 char returnexten[AST_MAX_EXTENSION];
2107                                                 char peername_flat[AST_MAX_EXTENSION]; /* using something like Zap/52 for an extension name is NOT a good idea */
2108                                                 int i;
2109                                                 ast_copy_string(peername_flat,peername,sizeof(peername_flat));
2110                                                 for(i=0; peername_flat[i] && i < AST_MAX_EXTENSION; i++) {
2111                                                         if (peername_flat[i] == '/') 
2112                                                                 peername_flat[i]= '0';
2113                                                 }
2114                                                 snprintf(returnexten, sizeof(returnexten), "%s,,t", peername);
2115                                                 ast_add_extension2(con, 1, peername_flat, 1, NULL, NULL, "Dial", ast_strdup(returnexten), ast_free_ptr, registrar);
2116                                         }
2117                                         if (comebacktoorigin) { 
2118                                                 set_c_e_p(chan, parking_con_dial, peername, 1);
2119                                         } else {
2120                                                 ast_log(LOG_WARNING, "now going to parkedcallstimeout,s,1 | ps is %d\n",pu->parkingnum);
2121                                                 snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
2122                                                 pbx_builtin_setvar_helper(pu->chan, "PARKINGSLOT", parkingslot);
2123                                                 set_c_e_p(chan, "parkedcallstimeout", peername, 1);
2124                                         }
2125                                 } else {
2126                                         /* They've been waiting too long, send them back to where they came.  Theoretically they
2127                                            should have their original extensions and such, but we copy to be on the safe side */
2128                                         set_c_e_p(chan, pu->context, pu->exten, pu->priority);
2129                                 }
2130
2131                                 post_manager_event("ParkedCallTimeOut", pu);
2132
2133                                 ast_verb(2, "Timeout for %s parked on %d. Returning to %s,%s,%d\n", chan->name, pu->parkingnum, chan->context, chan->exten, chan->priority);
2134                                 /* Start up the PBX, or hang them up */
2135                                 if (ast_pbx_start(chan))  {
2136                                         ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", chan->name);
2137                                         ast_hangup(chan);
2138                                 }
2139                                 /* And take them out of the parking lot */
2140                                 AST_LIST_REMOVE_CURRENT(list);
2141                                 con = ast_context_find(parking_con);
2142                                 if (con) {
2143                                         if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL))
2144                                                 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
2145                                         else
2146                                                 notify_metermaids(pu->parkingexten, parking_con, AST_DEVICE_NOT_INUSE);
2147                                 } else
2148                                         ast_log(LOG_WARNING, "Whoa, no parking context?\n");
2149                                 ast_free(pu);
2150                         } else {        /* still within parking time, process descriptors */
2151                                 for (x = 0; x < AST_MAX_FDS; x++) {
2152                                         struct ast_frame *f;
2153
2154                                         if (chan->fds[x] == -1 || (!FD_ISSET(chan->fds[x], &rfds) && !FD_ISSET(chan->fds[x], &efds)))
2155                                                 continue;       /* nothing on this descriptor */
2156
2157                                         if (FD_ISSET(chan->fds[x], &efds))
2158                                                 ast_set_flag(chan, AST_FLAG_EXCEPTION);
2159                                         else
2160                                                 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
2161                                         chan->fdno = x;
2162
2163                                         /* See if they need servicing */
2164                                         f = ast_read(chan);
2165                                         if (!f || (f->frametype == AST_FRAME_CONTROL && f->subclass ==  AST_CONTROL_HANGUP)) {
2166                                                 if (f)
2167                                                         ast_frfree(f);
2168                                                 post_manager_event("ParkedCallGiveUp", pu);
2169
2170                                                 /* There's a problem, hang them up*/
2171                                                 ast_verb(2, "%s got tired of being parked\n", chan->name);
2172                                                 ast_hangup(chan);
2173                                                 /* And take them out of the parking lot */
2174                                                 AST_LIST_REMOVE_CURRENT(list);
2175                                                 con = ast_context_find(parking_con);
2176                                                 if (con) {
2177                                                         if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL))
2178                                                                 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
2179                                                         else
2180                                                                 notify_metermaids(pu->parkingexten, parking_con, AST_DEVICE_NOT_INUSE);
2181                                                 } else
2182                                                         ast_log(LOG_WARNING, "Whoa, no parking context?\n");
2183                                                 ast_free(pu);
2184                                                 break;
2185                                         } else {
2186                                                 /*! \todo XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
2187                                                 ast_frfree(f);
2188                                                 if (pu->moh_trys < 3 && !chan->generatordata) {
2189                                                         ast_debug(1, "MOH on parked call stopped by outside source.  Restarting.\n");
2190                                                         ast_indicate_data(chan, AST_CONTROL_HOLD, 
2191                                                                 S_OR(parkmohclass, NULL),
2192                                                                 !ast_strlen_zero(parkmohclass) ? strlen(parkmohclass) + 1 : 0);
2193                                                         pu->moh_trys++;
2194                                                 }
2195                                                 goto std;       /*! \todo XXX Ick: jumping into an else statement??? XXX */
2196                                         }
2197
2198                                 } /* end for */
2199                                 if (x >= AST_MAX_FDS) {
2200 std:                                    for (x=0; x<AST_MAX_FDS; x++) { /* mark fds for next round */
2201                                                 if (chan->fds[x] > -1) {
2202                                                         FD_SET(chan->fds[x], &nrfds);
2203                                                         FD_SET(chan->fds[x], &nefds);
2204                                                         if (chan->fds[x] > max)
2205                                                                 max = chan->fds[x];
2206                                                 }
2207                                         }
2208                                         /* Keep track of our shortest wait */
2209                                         if (tms < ms || ms < 0)
2210                                                 ms = tms;
2211                                 }
2212                         }
2213                 } /* end while */
2214                 AST_LIST_TRAVERSE_SAFE_END
2215                 AST_LIST_UNLOCK(&parkinglot);
2216                 rfds = nrfds;
2217                 efds = nefds;
2218                 {
2219                         struct timeval tv = ast_samp2tv(ms, 1000);
2220                         /* Wait for something to happen */
2221                         ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
2222                 }
2223                 pthread_testcancel();
2224         }
2225         return NULL;    /* Never reached */
2226 }
2227
2228 /*! \brief Park a call */
2229 static int park_call_exec(struct ast_channel *chan, void *data)
2230 {
2231         /* Data is unused at the moment but could contain a parking
2232            lot context eventually */
2233         int res = 0;
2234         struct ast_module_user *u;
2235
2236         u = ast_module_user_add(chan);
2237
2238         /* Setup the exten/priority to be s/1 since we don't know
2239            where this call should return */
2240         strcpy(chan->exten, "s");
2241         chan->priority = 1;
2242         /* Answer if call is not up */
2243         if (chan->_state != AST_STATE_UP)
2244                 res = ast_answer(chan);
2245         /* Sleep to allow VoIP streams to settle down */
2246         if (!res)
2247                 res = ast_safe_sleep(chan, 1000);
2248         /* Park the call */
2249         if (!res)
2250                 res = ast_park_call(chan, chan, 0, NULL);
2251
2252         ast_module_user_remove(u);
2253
2254         return !res ? AST_PBX_KEEPALIVE : res;
2255 }
2256
2257 /*! \brief Pickup parked call */
2258 static int park_exec(struct ast_channel *chan, void *data)
2259 {
2260         int res = 0;
2261         struct ast_module_user *u;
2262         struct ast_channel *peer=NULL;
2263         struct parkeduser *pu;
2264         struct ast_context *con;
2265         int park = 0;
2266         struct ast_bridge_config config;
2267
2268         u = ast_module_user_add(chan);
2269
2270         if (data)
2271                 park = atoi((char *)data);
2272
2273         AST_LIST_LOCK(&parkinglot);
2274         AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot, pu, list) {
2275                 if (!data || pu->parkingnum == park) {
2276                         AST_LIST_REMOVE_CURRENT(list);
2277                         break;
2278                 }
2279         }
2280         AST_LIST_TRAVERSE_SAFE_END
2281         AST_LIST_UNLOCK(&parkinglot);
2282
2283         if (pu) {
2284                 peer = pu->chan;
2285                 con = ast_context_find(parking_con);
2286                 if (con) {
2287                         if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL))
2288                                 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
2289                         else
2290                                 notify_metermaids(pu->parkingexten, parking_con, AST_DEVICE_NOT_INUSE);
2291                 } else
2292                         ast_log(LOG_WARNING, "Whoa, no parking context?\n");
2293
2294                 manager_event(EVENT_FLAG_CALL, "UnParkedCall",
2295                         "Exten: %s\r\n"
2296                         "Channel: %s\r\n"
2297                         "From: %s\r\n"
2298                         "CallerIDNum: %s\r\n"
2299                         "CallerIDName: %s\r\n",
2300                         pu->parkingexten, pu->chan->name, chan->name,
2301                         S_OR(pu->chan->cid.cid_num, "<unknown>"),
2302                         S_OR(pu->chan->cid.cid_name, "<unknown>")
2303                         );
2304
2305                 ast_free(pu);
2306         }
2307         /* JK02: it helps to answer the channel if not already up */
2308         if (chan->_state != AST_STATE_UP)
2309                 ast_answer(chan);
2310
2311         if (peer) {
2312                 /* Play a courtesy to the source(s) configured to prefix the bridge connecting */
2313                 
2314                 if (!ast_strlen_zero(courtesytone)) {
2315                         int error = 0;
2316                         ast_indicate(peer, AST_CONTROL_UNHOLD);
2317                         if (parkedplay == 0) {
2318                                 error = ast_stream_and_wait(chan, courtesytone, "");
2319                         } else if (parkedplay == 1) {
2320                                 error = ast_stream_and_wait(peer, courtesytone, "");
2321                         } else if (parkedplay == 2) {
2322                                 if (!ast_streamfile(chan, courtesytone, chan->language) &&
2323                                                 !ast_streamfile(peer, courtesytone, chan->language)) {
2324                                         /*! \todo XXX we would like to wait on both! */
2325                                         res = ast_waitstream(chan, "");
2326                                         if (res >= 0)
2327                                                 res = ast_waitstream(peer, "");
2328                                         if (res < 0)
2329                                                 error = 1;
2330                                 }
2331                         }
2332                         if (error) {
2333                                 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
2334                                 ast_hangup(peer);
2335                                 return -1;
2336                         }
2337                 } else
2338                         ast_indicate(peer, AST_CONTROL_UNHOLD); 
2339
2340                 res = ast_channel_make_compatible(chan, peer);
2341                 if (res < 0) {
2342                         ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
2343                         ast_hangup(peer);
2344                         return -1;
2345                 }
2346                 /* This runs sorta backwards, since we give the incoming channel control, as if it
2347                    were the person called. */
2348                 ast_verb(3, "Channel %s connected to parked call %d\n", chan->name, park);
2349
2350                 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
2351                 ast_cdr_setdestchan(chan->cdr, peer->name);
2352                 memset(&config, 0, sizeof(struct ast_bridge_config));
2353                 if ((parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH))
2354                         ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
2355                 if ((parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH))
2356                         ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
2357                 if ((parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkedcallreparking == AST_FEATURE_FLAG_BYBOTH))
2358                         ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
2359                 if ((parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkedcallreparking == AST_FEATURE_FLAG_BYBOTH))
2360                         ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
2361                 res = ast_bridge_call(chan, peer, &config);
2362
2363                 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
2364                 ast_cdr_setdestchan(chan->cdr, peer->name);
2365
2366                 /* Simulate the PBX hanging up */
2367                 if (res != AST_PBX_NO_HANGUP_PEER)
2368                         ast_hangup(peer);
2369                 return res;
2370         } else {
2371                 /*! \todo XXX Play a message XXX */
2372                 if (ast_stream_and_wait(chan, "pbx-invalidpark", ""))
2373                         ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
2374                 ast_verb(3, "Channel %s tried to talk to nonexistent parked call %d\n", chan->name, park);
2375                 res = -1;
2376         }
2377
2378         ast_module_user_remove(u);
2379
2380         return res;
2381 }
2382
2383 /*!
2384  * \brief CLI command to list configured features
2385  * \param e
2386  * \param cmd
2387  * \param a
2388  *
2389  * \retval CLI_SUCCESS on success.
2390  * \retval NULL when tab completion is used.
2391  */
2392 static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) {
2393         int i;
2394         struct ast_call_feature *feature;
2395         char format[] = "%-25s %-7s %-7s\n";
2396
2397         switch (cmd) {
2398         
2399         case CLI_INIT:
2400                 e->command = "features show";
2401                 e->usage =
2402                         "Usage: features show\n"
2403                         "       Lists configured features\n";
2404                 return NULL;
2405         case CLI_GENERATE:
2406                 return NULL;
2407         }
2408
2409         ast_cli(a->fd, format, "Builtin Feature", "Default", "Current");
2410         ast_cli(a->fd, format, "---------------", "-------", "-------");
2411
2412         ast_cli(a->fd, format, "Pickup", "*8", ast_pickup_ext());          /* default hardcoded above, so we'll hardcode it here */
2413
2414         ast_rwlock_rdlock(&features_lock);
2415         for (i = 0; i < FEATURES_COUNT; i++)
2416                 ast_cli(a->fd, format, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
2417         ast_rwlock_unlock(&features_lock);
2418
2419         ast_cli(a->fd, "\n");
2420         ast_cli(a->fd, format, "Dynamic Feature", "Default", "Current");
2421         ast_cli(a->fd, format, "---------------", "-------", "-------");
2422         if (AST_LIST_EMPTY(&feature_list))
2423                 ast_cli(a->fd, "(none)\n");
2424         else {
2425                 AST_LIST_LOCK(&feature_list);
2426                 AST_LIST_TRAVERSE(&feature_list, feature, feature_entry)
2427                         ast_cli(a->fd, format, feature->sname, "no def", feature->exten);
2428                 AST_LIST_UNLOCK(&feature_list);
2429         }
2430         ast_cli(a->fd, "\nCall parking\n");
2431         ast_cli(a->fd, "------------\n");
2432         ast_cli(a->fd,"%-20s:      %s\n", "Parking extension", parking_ext);
2433         ast_cli(a->fd,"%-20s:      %s\n", "Parking context", parking_con);
2434         ast_cli(a->fd,"%-20s:      %d-%d\n", "Parked call extensions", parking_start, parking_stop);
2435         ast_cli(a->fd,"\n");
2436
2437         return CLI_SUCCESS;
2438 }
2439
2440 static char mandescr_bridge[] =
2441 "Description: Bridge together two channels already in the PBX\n"
2442 "Variables: ( Headers marked with * are required )\n"
2443 "   *Channel1: Channel to Bridge to Channel2\n"
2444 "   *Channel2: Channel to Bridge to Channel1\n"
2445 "        Tone: (Yes|No) Play courtesy tone to Channel 2\n"
2446 "\n";
2447
2448 /*!
2449  * \brief Actual bridge
2450  * \param chan
2451  * \param tmpchan
2452  * 
2453  * Stop hold music, lock both channels, masq channels,
2454  * after bridge return channel to next priority.
2455 */
2456 static void do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
2457 {
2458         ast_moh_stop(chan);
2459         ast_channel_lock(chan);
2460         ast_setstate(tmpchan, chan->_state);
2461         tmpchan->readformat = chan->readformat;
2462         tmpchan->writeformat = chan->writeformat;
2463         ast_channel_masquerade(tmpchan, chan);
2464         ast_channel_lock(tmpchan);
2465         ast_do_masquerade(tmpchan);
2466         /* when returning from bridge, the channel will continue at the next priority */
2467         ast_explicit_goto(tmpchan, chan->context, chan->exten, chan->priority + 1);
2468         ast_channel_unlock(tmpchan);
2469         ast_channel_unlock(chan);
2470 }
2471
2472 /*!
2473  * \brief Bridge channels together
2474  * \param s
2475  * \param m
2476  * 
2477  * Make sure valid channels were specified, 
2478  * send errors if any of the channels could not be found/locked, answer channels if needed,
2479  * create the placeholder channels and grab the other channels 
2480  * make the channels compatible, send error if we fail doing so 
2481  * setup the bridge thread object and start the bridge.
2482  * 
2483  * \retval 0 on success or on incorrect use.
2484  * \retval 1 on failure to bridge channels.
2485 */
2486 static int action_bridge(struct mansession *s, const struct message *m)
2487 {
2488         const char *channela = astman_get_header(m, "Channel1");
2489         const char *channelb = astman_get_header(m, "Channel2");
2490         const char *playtone = astman_get_header(m, "Tone");
2491         struct ast_channel *chana = NULL, *chanb = NULL;
2492         struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
2493         struct ast_bridge_thread_obj *tobj = NULL;
2494
2495         /* make sure valid channels were specified */
2496         if (!ast_strlen_zero(channela) && !ast_strlen_zero(channelb)) {
2497                 chana = ast_get_channel_by_name_prefix_locked(channela, strlen(channela));
2498                 chanb = ast_get_channel_by_name_prefix_locked(channelb, strlen(channelb));
2499                 if (chana)
2500                         ast_channel_unlock(chana);
2501                 if (chanb)
2502                         ast_channel_unlock(chanb);
2503
2504                 /* send errors if any of the channels could not be found/locked */
2505                 if (!chana) {
2506                         char buf[256];
2507                         snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
2508                         astman_send_error(s, m, buf);
2509                         return 0;
2510                 }
2511                 if (!chanb) {
2512                         char buf[256];
2513                         snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
2514                         astman_send_error(s, m, buf);
2515                         return 0;
2516                 }
2517         } else {
2518                 astman_send_error(s, m, "Missing channel parameter in request");
2519                 return 0;
2520         }
2521
2522         /* Answer the channels if needed */
2523         if (chana->_state != AST_STATE_UP)
2524                 ast_answer(chana);
2525         if (chanb->_state != AST_STATE_UP)
2526                 ast_answer(chanb);
2527
2528         /* create the placeholder channels and grab the other channels */
2529         if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
2530                 NULL, NULL, 0, "Bridge/%s", chana->name))) {
2531                 astman_send_error(s, m, "Unable to create temporary channel!");
2532                 return 1;
2533         }
2534
2535         if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
2536                 NULL, NULL, 0, "Bridge/%s", chanb->name))) {
2537                 astman_send_error(s, m, "Unable to create temporary channels!");
2538                 ast_channel_free(tmpchana);
2539                 return 1;
2540         }
2541
2542         do_bridge_masquerade(chana, tmpchana);
2543         do_bridge_masquerade(chanb, tmpchanb);
2544         
2545         /* make the channels compatible, send error if we fail doing so */
2546         if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
2547                 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
2548                 astman_send_error(s, m, "Could not make channels compatible for manager bridge");
2549                 ast_hangup(tmpchana);
2550                 ast_hangup(tmpchanb);
2551                 return 1;
2552         }
2553
2554         /* setup the bridge thread object and start the bridge */
2555         if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
2556                 ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
2557                 astman_send_error(s, m, "Unable to spawn a new bridge thread");
2558                 ast_hangup(tmpchana);
2559                 ast_hangup(tmpchanb);
2560                 return 1;
2561         }
2562
2563         tobj->chan = tmpchana;
2564         tobj->peer = tmpchanb;
2565         tobj->return_to_pbx = 1;
2566         
2567         if (ast_true(playtone)) {
2568                 if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language)) {
2569                         if (ast_waitstream(tmpchanb, "") < 0)
2570                                 ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
2571                 }
2572         }
2573
2574         ast_bridge_call_thread_launch(tobj);
2575
2576         astman_send_ack(s, m, "Launched bridge thread with success");
2577
2578         return 0;
2579 }
2580
2581 /*!
2582  * \brief CLI command to list parked calls
2583  * \param e 
2584  * \param cmd
2585  * \param a
2586  *  
2587  * Check right usage, lock parking lot, display parked calls, unlock parking lot list.
2588  * \retval CLI_SUCCESS on success.
2589  * \retval CLI_SHOWUSAGE on incorrect number of arguments.
2590  * \retval NULL when tab completion is used.
2591 */
2592 static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2593 {
2594         struct parkeduser *cur;
2595         int numparked = 0;
2596
2597         switch (cmd) {
2598         case CLI_INIT:
2599                 e->command = "parkedcalls show";
2600                 e->usage =
2601                         "Usage: parkedcalls show\n"
2602                         "       List currently parked calls\n";
2603                 return NULL;
2604         case CLI_GENERATE:
2605                 return NULL;
2606         }
2607
2608         if (a->argc > e->args)
2609                 return CLI_SHOWUSAGE;
2610
2611         ast_cli(a->fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
2612                 , "Context", "Extension", "Pri", "Timeout");
2613
2614         AST_LIST_LOCK(&parkinglot);
2615         AST_LIST_TRAVERSE(&parkinglot, cur, list) {
2616                 ast_cli(a->fd, "%-10.10s %25s (%-15s %-12s %-4d) %6lds\n"
2617                         ,cur->parkingexten, cur->chan->name, cur->context, cur->exten
2618                         ,cur->priority, cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL));
2619
2620                 numparked++;
2621         }
2622         AST_LIST_UNLOCK(&parkinglot);
2623         ast_cli(a->fd, "%d parked call%s.\n", numparked, ESS(numparked));
2624
2625
2626         return CLI_SUCCESS;
2627 }
2628
2629 static char *handle_parkedcalls_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2630 {
2631         char *res = handle_parkedcalls(e, cmd, a);
2632         if (cmd == CLI_INIT)
2633                 e->command = "show parkedcalls";
2634         return res;
2635 }
2636
2637 static struct ast_cli_entry cli_show_parkedcalls_deprecated = AST_CLI_DEFINE(handle_parkedcalls_deprecated, "List currently parked calls.");
2638
2639 static struct ast_cli_entry cli_features[] = {
2640         AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
2641         AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls", .deprecate_cmd = &cli_show_parkedcalls_deprecated),
2642 };
2643
2644 /*! 
2645  * \brief Dump parking lot status
2646  * \param s
2647  * \param m
2648  * 
2649  * Lock parking lot, iterate list and append parked calls status, unlock parking lot.
2650  * \return Always RESULT_SUCCESS 
2651 */
2652 static int manager_parking_status(struct mansession *s, const struct message *m)
2653 {
2654         struct parkeduser *cur;
2655         const char *id = astman_get_header(m, "ActionID");
2656         char idText[256] = "";
2657
2658         if (!ast_strlen_zero(id))
2659                 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
2660
2661         astman_send_ack(s, m, "Parked calls will follow");
2662
2663         AST_LIST_LOCK(&parkinglot);
2664
2665         AST_LIST_TRAVERSE(&parkinglot, cur, list) {
2666                 astman_append(s, "Event: ParkedCall\r\n"
2667                         "Exten: %d\r\n"
2668                         "Channel: %s\r\n"
2669                         "From: %s\r\n"
2670                         "Timeout: %ld\r\n"
2671                         "CallerIDNum: %s\r\n"
2672                         "CallerIDName: %s\r\n"
2673                         "%s"
2674                         "\r\n",
2675                         cur->parkingnum, cur->chan->name, cur->peername,
2676                         (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - (long) time(NULL),
2677                         S_OR(cur->chan->cid.cid_num, ""),       /* XXX in other places it is <unknown> */
2678                         S_OR(cur->chan->cid.cid_name, ""),
2679                         idText);
2680         }
2681
2682         astman_append(s,
2683                 "Event: ParkedCallsComplete\r\n"
2684                 "%s"
2685                 "\r\n",idText);
2686
2687         AST_LIST_UNLOCK(&parkinglot);
2688
2689         return RESULT_SUCCESS;
2690 }
2691
2692 static char mandescr_park[] =
2693 "Description: Park a channel.\n"
2694 "Variables: (Names marked with * are required)\n"
2695 "       *Channel: Channel name to park\n"
2696 "       *Channel2: Channel to announce park info to (and return to if timeout)\n"
2697 "       Timeout: Number of milliseconds to wait before callback.\n";  
2698
2699 /*!
2700  * \brief Create manager event for parked calls
2701  * \param s
2702  * \param m
2703  *
2704  * Get channels involved in park, create event.
2705  * \return Always 0
2706 */
2707 static int manager_park(struct mansession *s, const struct message *m)
2708 {
2709         const char *channel = astman_get_header(m, "Channel");
2710         const char *channel2 = astman_get_header(m, "Channel2");
2711         const char *timeout = astman_get_header(m, "Timeout");
2712         char buf[BUFSIZ];
2713         int to = 0;
2714         int res = 0;
2715         int parkExt = 0;
2716         struct ast_channel *ch1, *ch2;
2717
2718         if (ast_strlen_zero(channel)) {
2719                 astman_send_error(s, m, "Channel not specified");
2720                 return 0;
2721         }
2722
2723         if (ast_strlen_zero(channel2)) {
2724                 astman_send_error(s, m, "Channel2 not specified");
2725                 return 0;
2726         }
2727
2728         ch1 = ast_get_channel_by_name_locked(channel);
2729         if (!ch1) {
2730                 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
2731                 astman_send_error(s, m, buf);
2732                 return 0;
2733         }
2734
2735         ch2 = ast_get_channel_by_name_locked(channel2);
2736         if (!ch2) {
2737                 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
2738                 astman_send_error(s, m, buf);
2739                 ast_channel_unlock(ch1);
2740                 return 0;
2741         }
2742
2743         if (!ast_strlen_zero(timeout)) {
2744                 sscanf(timeout, "%d", &to);
2745         }
2746
2747         res = ast_masq_park_call(ch1, ch2, to, &parkExt);
2748         if (!res) {
2749                 ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
2750                 astman_send_ack(s, m, "Park successful");
2751         } else {
2752                 astman_send_error(s, m, "Park failure");
2753         }
2754
2755         ast_channel_unlock(ch1);
2756         ast_channel_unlock(ch2);
2757
2758         return 0;
2759 }
2760
2761 /*!
2762  * \brief Pickup a call
2763  * \param chan channel that initiated pickup.
2764  *
2765  * Walk list of channels, checking it is not itself, channel is pbx one,
2766  * check that the callgroup for both channels are the same and the channel is ringing.
2767  * Answer calling channel, flag channel as answered on queue, masq channels together.
2768 */
2769 int ast_pickup_call(struct ast_channel *chan)
2770 {
2771         struct ast_channel *cur = NULL;
2772         int res = -1;
2773
2774         while ((cur = ast_channel_walk_locked(cur)) != NULL) {
2775                 if (!cur->pbx && 
2776                         (cur != chan) &&
2777                         (chan->pickupgroup & cur->callgroup) &&
2778                         ((cur->_state == AST_STATE_RINGING) ||
2779                          (cur->_state == AST_STATE_RING))) {
2780                                 break;
2781                 }
2782                 ast_channel_unlock(cur);
2783         }
2784         if (cur) {
2785                 ast_debug(1, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
2786                 res = ast_answer(chan);
2787                 if (res)
2788                         ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
2789                 res = ast_queue_control(chan, AST_CONTROL_ANSWER);
2790                 if (res)
2791                         ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
2792                 res = ast_channel_masquerade(cur, chan);
2793                 if (res)
2794                         ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name);           /* Done */
2795                 ast_channel_unlock(cur);
2796         } else  {
2797                 ast_debug(1, "No call pickup possible...\n");
2798         }
2799         return res;
2800 }
2801
2802 /*! 
2803  * \brief Add parking hints for all defined parking lots 
2804  * \param context
2805  * \param start starting parkinglot number
2806  * \param stop ending parkinglot number
2807 */
2808 static void park_add_hints(char *context, int start, int stop)
2809 {
2810         int numext;
2811         char device[AST_MAX_EXTENSION];
2812         char exten[10];
2813
2814         for (numext = start; numext <= stop; numext++) {
2815                 snprintf(exten, sizeof(exten), "%d", numext);
2816                 snprintf(device, sizeof(device), "park:%s@%s", exten, context);
2817                 ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
2818         }
2819 }
2820
2821
2822 static int load_config(void) 
2823 {
2824         int start = 0, end = 0;
2825         int res;
2826         int i;
2827         struct ast_context *con = NULL;
2828         struct ast_config *cfg = NULL;
2829         struct ast_variable *var = NULL;
2830         struct feature_group *fg = NULL;
2831         struct ast_flags config_flags = { 0 };
2832         char old_parking_ext[AST_MAX_EXTENSION];
2833         char old_parking_con[AST_MAX_EXTENSION] = "";
2834         char *ctg; 
2835         static const char *categories[] = { 
2836                 /* Categories in features.conf that are not
2837                  * to be parsed as group categories
2838                  */
2839                 "general",
2840                 "featuremap",
2841                 "applicationmap"
2842         };
2843
2844         if (!ast_strlen_zero(parking_con)) {
2845                 strcpy(old_parking_ext, parking_ext);
2846                 strcpy(old_parking_con, parking_con);
2847         } 
2848
2849         /* Reset to defaults */
2850         strcpy(parking_con, "parkedcalls");
2851         strcpy(parking_con_dial, "park-dial");
2852         strcpy(parking_ext, "700");
2853         strcpy(pickup_ext, "*8");
2854         strcpy(parkmohclass, "default");
2855         courtesytone[0] = '\0';
2856         strcpy(xfersound, "beep");
2857         strcpy(xferfailsound, "pbx-invalid");
2858         parking_start = 701;
2859         parking_stop = 750;
2860         parkfindnext = 0;
2861         adsipark = 0;
2862         comebacktoorigin = 1;
2863         parkaddhints = 0;
2864         parkedcalltransfers = 0;
2865         parkedcallreparking = 0;
2866
2867         transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
2868         featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
2869         atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
2870         atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
2871         atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
2872         atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
2873
2874         cfg = ast_config_load("features.conf", config_flags);
2875         if (!cfg) {
2876                 ast_log(LOG_WARNING,"Could not load features.conf\n");
2877                 return AST_MODULE_LOAD_DECLINE;
2878         }
2879         for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
2880                 if (!strcasecmp(var->name, "parkext")) {
2881                         ast_copy_string(parking_ext, var->value, sizeof(parking_ext));
2882                 } else if (!strcasecmp(var->name, "context")) {
2883                         ast_copy_string(parking_con, var->value, sizeof(parking_con));
2884                 } else if (!strcasecmp(var->name, "parkingtime")) {
2885                         if ((sscanf(var->value, "%d", &parkingtime) != 1) || (parkingtime < 1)) {
2886                                 ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
2887                                 parkingtime = DEFAULT_PARK_TIME;
2888                         } else
2889                                 parkingtime = parkingtime * 1000;
2890                 } else if (!strcasecmp(var->name, "parkpos")) {
2891                         if (sscanf(var->value, "%d-%d", &start, &end) != 2) {
2892                                 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);
2893                         } else {
2894                                 parking_start = start;
2895                                 parking_stop = end;
2896                         }
2897                 } else if (!strcasecmp(var->name, "findslot")) {
2898                         parkfindnext = (!strcasecmp(var->value, "next"));
2899                 } else if (!strcasecmp(var->name, "parkinghints")) {
2900                         parkaddhints = ast_true(var->value);
2901                 } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
2902                         if (!strcasecmp(var->value, "both"))
2903                                 parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
2904                         else if (!strcasecmp(var->value, "caller"))
2905                                 parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
2906                         else if (!strcasecmp(var->value, "callee"))
2907                                 parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
2908                 } else if (!strcasecmp(var->name, "parkedcallreparking")) {
2909                         if (!strcasecmp(var->value, "both"))
2910                                 parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
2911                         else if (!strcasecmp(var->value, "caller"))
2912                                 parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
2913                         else if (!strcasecmp(var->value, "callee"))
2914                                 parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
2915                 } else if (!strcasecmp(var->name, "adsipark")) {
2916                         adsipark = ast_true(var->value);
2917                 } else if (!strcasecmp(var->name, "transferdigittimeout")) {
2918                         if ((sscanf(var->value, "%d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
2919                                 ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
2920                                 transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
2921                         } else
2922                                 transferdigittimeout = transferdigittimeout * 1000;
2923                 } else if (!strcasecmp(var->name, "featuredigittimeout")) {
2924                         if ((sscanf(var->value, "%d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
2925                                 ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
2926                                 featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
2927                         }
2928                 } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
2929                         if ((sscanf(var->value, "%d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
2930                                 ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
2931                                 atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
2932                         } else
2933                                 atxfernoanswertimeout = atxfernoanswertimeout * 1000;
2934                 } else if (!strcasecmp(var->name, "atxferloopdelay")) {
2935                         if ((sscanf(var->value, "%u", &atxferloopdelay) != 1)) {
2936                                 ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
2937                                 atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
2938                         } else 
2939                                 atxferloopdelay *= 1000;
2940                 } else if (!strcasecmp(var->name, "atxferdropcall")) {
2941                         atxferdropcall = ast_true(var->value);
2942                 } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
2943                         if ((sscanf(var->value, "%u", &atxferloopdelay) != 1)) {
2944                                 ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
2945                                 atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
2946                         }
2947                 } else if (!strcasecmp(var->name, "courtesytone")) {
2948                         ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
2949                 }  else if (!strcasecmp(var->name, "parkedplay")) {
2950                         if (!strcasecmp(var->value, "both"))
2951                                 parkedplay = 2;
2952                         else if (!strcasecmp(var->value, "parked"))
2953                                 parkedplay = 1;
2954                         else
2955                                 parkedplay = 0;
2956                 } else if (!strcasecmp(var->name, "xfersound")) {
2957                         ast_copy_string(xfersound, var->value, sizeof(xfersound));
2958                 } else if (!strcasecmp(var->name, "xferfailsound")) {
2959                         ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
2960                 } else if (!strcasecmp(var->name, "pickupexten")) {
2961                         ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));