AST-2009-005
[asterisk/asterisk.git] / apps / app_followme.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * A full-featured Find-Me/Follow-Me Application
5  * 
6  * Copyright (C) 2005-2006, BJ Weschke All Rights Reserved.
7  *
8  * BJ Weschke <bweschke@btwtech.com>
9  *
10  * This code is released by the author with no restrictions on usage.
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  */
19
20 /*! \file
21  *
22  * \brief Find-Me Follow-Me application
23  *
24  * \author BJ Weschke <bweschke@btwtech.com>
25  *
26  * \arg See \ref Config_followme
27  *
28  * \ingroup applications
29  */
30
31 /*** MODULEINFO
32         <depend>chan_local</depend>
33  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <signal.h>
40
41 #include "asterisk/paths.h"     /* use ast_config_AST_SPOOL_DIR */
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/say.h"
49 #include "asterisk/features.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/config.h"
54 #include "asterisk/monitor.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/astdb.h"
58 #include "asterisk/dsp.h"
59 #include "asterisk/app.h"
60
61 /*** DOCUMENTATION
62         <application name="FollowMe" language="en_US">
63                 <synopsis>
64                         Find-Me/Follow-Me application.
65                 </synopsis>
66                 <syntax>
67                         <parameter name="followmeid" required="true" />
68                         <parameter name="options">
69                                 <optionlist>
70                                         <option name="s">
71                                                 <para>Playback the incoming status message prior to starting
72                                                 the follow-me step(s)</para>
73                                         </option>
74                                         <option name="a">
75                                                 <para>Record the caller's name so it can be announced to the
76                                                 callee on each step.</para>
77                                         </option>
78                                         <option name="n">
79                                                 <para>Playback the unreachable status message if we've run out
80                                                 of steps to reach the or the callee has elected not to be reachable.</para>
81                                         </option>
82                                 </optionlist>
83                         </parameter>
84                 </syntax>
85                 <description>
86                         <para>This application performs Find-Me/Follow-Me functionality for the caller
87                         as defined in the profile matching the <replaceable>followmeid</replaceable> parameter in
88                         <filename>followme.conf</filename>. If the specified <replaceable>followmeid</replaceable>
89                         profile doesn't exist in <filename>followme.conf</filename>, execution will be returned
90                         to the dialplan and call execution will continue at the next priority.</para>
91                         <para>Returns -1 on hangup.</para>
92                 </description>
93         </application>
94  ***/
95
96 static char *app = "FollowMe";
97
98 /*! \brief Number structure */
99 struct number {
100         char number[512];       /*!< Phone Number(s) and/or Extension(s) */
101         long timeout;           /*!< Dial Timeout, if used. */
102         int order;              /*!< The order to dial in */
103         AST_LIST_ENTRY(number) entry; /*!< Next Number record */
104 };
105
106 /*! \brief Data structure for followme scripts */
107 struct call_followme {
108         ast_mutex_t lock;
109         char name[AST_MAX_EXTENSION];   /*!< Name - FollowMeID */
110         char moh[AST_MAX_CONTEXT];      /*!< Music On Hold Class to be used */
111         char context[AST_MAX_CONTEXT];  /*!< Context to dial from */
112         unsigned int active;            /*!< Profile is active (1), or disabled (0). */
113         int realtime;           /*!< Cached from realtime */
114         char takecall[20];              /*!< Digit mapping to take a call */
115         char nextindp[20];              /*!< Digit mapping to decline a call */
116         char callfromprompt[PATH_MAX];  /*!< Sound prompt name and path */
117         char norecordingprompt[PATH_MAX];       /*!< Sound prompt name and path */
118         char optionsprompt[PATH_MAX];   /*!< Sound prompt name and path */
119         char plsholdprompt[PATH_MAX];   /*!< Sound prompt name and path */
120         char statusprompt[PATH_MAX];    /*!< Sound prompt name and path */
121         char sorryprompt[PATH_MAX];     /*!< Sound prompt name and path */
122
123         AST_LIST_HEAD_NOLOCK(numbers, number) numbers;     /*!< Head of the list of follow-me numbers */
124         AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
125         AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
126         AST_LIST_ENTRY(call_followme) entry;           /*!< Next Follow-Me record */
127 };
128
129 struct fm_args {
130         struct ast_channel *chan;
131         char *mohclass;
132         AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
133         int status;
134         char context[AST_MAX_CONTEXT];
135         char namerecloc[AST_MAX_CONTEXT];
136         struct ast_channel *outbound;
137         char takecall[20];              /*!< Digit mapping to take a call */
138         char nextindp[20];              /*!< Digit mapping to decline a call */
139         char callfromprompt[PATH_MAX];  /*!< Sound prompt name and path */
140         char norecordingprompt[PATH_MAX];       /*!< Sound prompt name and path */
141         char optionsprompt[PATH_MAX];   /*!< Sound prompt name and path */
142         char plsholdprompt[PATH_MAX];   /*!< Sound prompt name and path */
143         char statusprompt[PATH_MAX];    /*!< Sound prompt name and path */
144         char sorryprompt[PATH_MAX];     /*!< Sound prompt name and path */
145         struct ast_flags followmeflags;
146 };
147
148 struct findme_user {
149         struct ast_channel *ochan;
150         int state;
151         char dialarg[256];
152         char yn[10];
153         int ynidx; 
154         long digts;
155         int cleared;
156         AST_LIST_ENTRY(findme_user) entry;
157 };
158
159 enum {
160         FOLLOWMEFLAG_STATUSMSG = (1 << 0),
161         FOLLOWMEFLAG_RECORDNAME = (1 << 1),
162         FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2)
163 };
164
165 AST_APP_OPTIONS(followme_opts, {
166         AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG ),
167         AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME ),
168         AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG ),
169 });
170
171 static int ynlongest = 0;
172
173 static const char *featuredigittostr;
174 static int featuredigittimeout = 5000;          /*!< Feature Digit Timeout */
175 static const char *defaultmoh = "default";      /*!< Default Music-On-Hold Class */
176
177 static char takecall[20] = "1", nextindp[20] = "2";
178 static char callfromprompt[PATH_MAX] = "followme/call-from";
179 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
180 static char optionsprompt[PATH_MAX] = "followme/options";
181 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
182 static char statusprompt[PATH_MAX] = "followme/status";
183 static char sorryprompt[PATH_MAX] = "followme/sorry";
184
185
186 static AST_RWLIST_HEAD_STATIC(followmes, call_followme);
187 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
188
189 static void free_numbers(struct call_followme *f)
190 {
191         /* Free numbers attached to the profile */
192         struct number *prev;
193
194         while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
195                 /* Free the number */
196                 ast_free(prev);
197         AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
198
199         while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
200                 /* Free the blacklisted number */
201                 ast_free(prev);
202         AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
203
204         while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
205                 /* Free the whitelisted number */
206                 ast_free(prev);
207         AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
208 }
209
210
211 /*! \brief Allocate and initialize followme profile */
212 static struct call_followme *alloc_profile(const char *fmname)
213 {
214         struct call_followme *f;
215
216         if (!(f = ast_calloc(1, sizeof(*f))))
217                 return NULL;
218
219         ast_mutex_init(&f->lock);
220         ast_copy_string(f->name, fmname, sizeof(f->name));
221         f->moh[0] = '\0';
222         f->context[0] = '\0';
223         ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
224         ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
225         ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
226         ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
227         ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
228         ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
229         ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
230         ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
231         AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
232         AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
233         AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
234         return f;
235 }
236
237 static void init_profile(struct call_followme *f)
238 {
239         f->active = 1;
240         ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
241 }
242
243    
244    
245 /*! \brief Set parameter in profile from configuration file */
246 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
247 {
248
249         if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music")) 
250                 ast_copy_string(f->moh, val, sizeof(f->moh));
251         else if (!strcasecmp(param, "context")) 
252                 ast_copy_string(f->context, val, sizeof(f->context));
253         else if (!strcasecmp(param, "takecall"))
254                 ast_copy_string(f->takecall, val, sizeof(f->takecall));
255         else if (!strcasecmp(param, "declinecall"))
256                 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
257         else if (!strcasecmp(param, "call-from-prompt") || !strcasecmp(param, "call_from_prompt"))
258                 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
259         else if (!strcasecmp(param, "followme-norecording-prompt") || !strcasecmp(param, "norecording_prompt")) 
260                 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
261         else if (!strcasecmp(param, "followme-options-prompt") || !strcasecmp(param, "options_prompt")) 
262                 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
263         else if (!strcasecmp(param, "followme-pls-hold-prompt") || !strcasecmp(param, "pls_hold_prompt"))
264                 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
265         else if (!strcasecmp(param, "followme-status-prompt") || !strcasecmp(param, "status_prompt")) 
266                 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
267         else if (!strcasecmp(param, "followme-sorry-prompt") || !strcasecmp(param, "sorry_prompt")) 
268                 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
269         else if (failunknown) {
270                 if (linenum >= 0)
271                         ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
272                 else
273                         ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
274         }
275 }
276
277 /*! \brief Add a new number */
278 static struct number *create_followme_number(const char *number, int timeout, int numorder)
279 {
280         struct number *cur;
281         char *buf = ast_strdupa(number);
282         char *tmp;
283
284         if (!(cur = ast_calloc(1, sizeof(*cur))))
285                 return NULL;
286
287         cur->timeout = timeout;
288         if ((tmp = strchr(buf, ',')))
289                 *tmp = '\0';
290         ast_copy_string(cur->number, buf, sizeof(cur->number));
291         cur->order = numorder;
292         ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
293
294         return cur;
295 }
296
297 /*! \brief Reload followme application module */
298 static int reload_followme(int reload)
299 {
300         struct call_followme *f;
301         struct ast_config *cfg;
302         char *cat = NULL, *tmp;
303         struct ast_variable *var;
304         struct number *cur, *nm;
305         char numberstr[90];
306         int timeout;
307         char *timeoutstr;
308         int numorder;
309         const char *takecallstr;
310         const char *declinecallstr;
311         const char *tmpstr;
312         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
313
314         if (!(cfg = ast_config_load("followme.conf", config_flags))) {
315                 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
316                 return 0;
317         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
318                 return 0;
319         } else if (cfg == CONFIG_STATUS_FILEINVALID) {
320                 ast_log(LOG_ERROR, "Config file followme.conf is in an invalid format.  Aborting.\n");
321                 return 0;
322         }
323
324         AST_RWLIST_WRLOCK(&followmes);
325
326         /* Reset Global Var Values */
327         featuredigittimeout = 5000;
328
329         /* Mark all profiles as inactive for the moment */
330         AST_RWLIST_TRAVERSE(&followmes, f, entry) {
331                 f->active = 0;
332         }
333
334         featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
335
336         if (!ast_strlen_zero(featuredigittostr)) {
337                 if (!sscanf(featuredigittostr, "%30d", &featuredigittimeout))
338                         featuredigittimeout = 5000;
339         }
340
341         if ((takecallstr = ast_variable_retrieve(cfg, "general", "takecall")) && !ast_strlen_zero(takecallstr)) {
342                 ast_copy_string(takecall, takecallstr, sizeof(takecall));
343         }
344
345         if ((declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall")) && !ast_strlen_zero(declinecallstr)) {
346                 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
347         }
348
349         if ((tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt")) && !ast_strlen_zero(tmpstr)) {
350                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
351         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "call_from_prompt")) && !ast_strlen_zero(tmpstr)) {
352                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
353         }
354
355         if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt")) && !ast_strlen_zero(tmpstr)) {
356                 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
357         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording_prompt")) && !ast_strlen_zero(tmpstr)) {
358                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
359         }
360
361
362         if ((tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt")) && !ast_strlen_zero(tmpstr)) {
363                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
364         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "options_prompt")) && !ast_strlen_zero(tmpstr)) {
365                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
366         }
367
368         if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt")) && !ast_strlen_zero(tmpstr)) {
369                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
370         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls_hold_prompt")) && !ast_strlen_zero(tmpstr)) {
371                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
372         }
373
374         if ((tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt")) && !ast_strlen_zero(tmpstr)) {
375                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
376         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "status_prompt")) && !ast_strlen_zero(tmpstr)) {
377                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
378         }
379
380         if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt")) && !ast_strlen_zero(tmpstr)) {
381                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
382         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry_prompt")) && !ast_strlen_zero(tmpstr)) {
383                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
384         }
385
386         /* Chug through config file */
387         while ((cat = ast_category_browse(cfg, cat))) {
388                 int new = 0;
389
390                 if (!strcasecmp(cat, "general"))
391                         continue;
392
393                 /* Look for an existing one */
394                 AST_LIST_TRAVERSE(&followmes, f, entry) {
395                         if (!strcasecmp(f->name, cat))
396                                 break;
397                 }
398
399                 ast_debug(1, "New profile %s.\n", cat);
400
401                 if (!f) {
402                         /* Make one then */
403                         f = alloc_profile(cat);
404                         new = 1;
405                 }
406
407                 /* Totally fail if we fail to find/create an entry */
408                 if (!f)
409                         continue;
410
411                 if (!new)
412                         ast_mutex_lock(&f->lock);
413                 /* Re-initialize the profile */
414                 init_profile(f);
415                 free_numbers(f);
416                 var = ast_variable_browse(cfg, cat);
417                 while (var) {
418                         if (!strcasecmp(var->name, "number")) {
419                                 int idx = 0;
420
421                                 /* Add a new number */
422                                 ast_copy_string(numberstr, var->value, sizeof(numberstr));
423                                 if ((tmp = strchr(numberstr, ','))) {
424                                         *tmp++ = '\0';
425                                         timeoutstr = ast_strdupa(tmp);
426                                         if ((tmp = strchr(timeoutstr, ','))) {
427                                                 *tmp++ = '\0';
428                                                 numorder = atoi(tmp);
429                                                 if (numorder < 0)
430                                                         numorder = 0;
431                                         } else 
432                                                 numorder = 0;
433                                         timeout = atoi(timeoutstr);
434                                         if (timeout < 0) 
435                                                 timeout = 25;
436                                 } else {
437                                         timeout = 25;
438                                         numorder = 0;
439                                 }
440
441                                 if (!numorder) {
442                                         idx = 1;
443                                         AST_LIST_TRAVERSE(&f->numbers, nm, entry) 
444                                                 idx++;
445                                         numorder = idx;
446                                 }
447                                 cur = create_followme_number(numberstr, timeout, numorder);
448                                 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
449                         } else {
450                                 profile_set_param(f, var->name, var->value, var->lineno, 1);
451                                 ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
452                         }
453                         var = var->next;
454                 } /* End while(var) loop */
455
456                 if (!new) 
457                         ast_mutex_unlock(&f->lock);
458                 else
459                         AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
460         }
461
462         ast_config_destroy(cfg);
463
464         AST_RWLIST_UNLOCK(&followmes);
465
466         return 1;
467 }
468
469 static void clear_caller(struct findme_user *tmpuser)
470 {
471         struct ast_channel *outbound;
472
473         if (tmpuser && tmpuser->ochan && tmpuser->state >= 0) {
474                 outbound = tmpuser->ochan;
475                 if (!outbound->cdr) {
476                         outbound->cdr = ast_cdr_alloc();
477                         if (outbound->cdr)
478                                 ast_cdr_init(outbound->cdr, outbound);
479                 }
480                 if (outbound->cdr) {
481                         char tmp[256];
482
483                         snprintf(tmp, sizeof(tmp), "%s/%s", "Local", tmpuser->dialarg);
484                         ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
485                         ast_cdr_update(outbound);
486                         ast_cdr_start(outbound->cdr);
487                         ast_cdr_end(outbound->cdr);
488                         /* If the cause wasn't handled properly */
489                         if (ast_cdr_disposition(outbound->cdr, outbound->hangupcause))
490                                 ast_cdr_failed(outbound->cdr);
491                 } else
492                         ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
493                 ast_hangup(tmpuser->ochan);
494         }
495
496 }
497
498 static void clear_calling_tree(struct findme_user_listptr *findme_user_list) 
499 {
500         struct findme_user *tmpuser;
501
502         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
503                 clear_caller(tmpuser);
504                 tmpuser->cleared = 1;
505         }
506 }
507
508
509
510 static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, char *namerecloc, int *status, struct fm_args *tpargs) 
511 {
512         struct ast_channel *watchers[256];
513         int pos;
514         struct ast_channel *winner;
515         struct ast_frame *f;
516         int ctstatus = 0;
517         int dg;
518         struct findme_user *tmpuser;
519         int to = 0;
520         int livechannels = 0;
521         int tmpto;
522         long totalwait = 0, wtd = 0, towas = 0;
523         char *callfromname;
524         char *pressbuttonname;
525
526         /* ------------ wait_for_winner_channel start --------------- */ 
527
528         callfromname = ast_strdupa(tpargs->callfromprompt);
529         pressbuttonname = ast_strdupa(tpargs->optionsprompt);
530
531         if (AST_LIST_EMPTY(findme_user_list)) {
532                 ast_verb(3, "couldn't reach at this number.\n");
533                 return NULL;
534         }
535
536         if (!caller) {
537                 ast_verb(3, "Original caller hungup. Cleanup.\n");
538                 clear_calling_tree(findme_user_list);
539                 return NULL;
540         }
541
542         totalwait = nm->timeout * 1000;
543
544         while (!ctstatus) {
545                 to = 1000;
546                 pos = 1; 
547                 livechannels = 0;
548                 watchers[0] = caller;
549
550                 dg = 0;
551                 winner = NULL;
552                 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
553                         if (tmpuser->state >= 0 && tmpuser->ochan) {
554                                 if (tmpuser->state == 3) 
555                                         tmpuser->digts += (towas - wtd);
556                                 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
557                                         ast_verb(3, "We've been waiting for digits longer than we should have.\n");
558                                         if (!ast_strlen_zero(namerecloc)) {
559                                                 tmpuser->state = 1;
560                                                 tmpuser->digts = 0;
561                                                 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
562                                                         ast_sched_runq(tmpuser->ochan->sched);
563                                                 } else {
564                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
565                                                         return NULL;
566                                                 }
567                                         } else {
568                                                 tmpuser->state = 2;
569                                                 tmpuser->digts = 0;
570                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
571                                                         ast_sched_runq(tmpuser->ochan->sched);
572                                                 else {
573                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
574                                                         return NULL;
575                                                 }
576                                         }
577                                 }
578                                 if (tmpuser->ochan->stream) {
579                                         ast_sched_runq(tmpuser->ochan->sched);
580                                         tmpto = ast_sched_wait(tmpuser->ochan->sched);
581                                         if (tmpto > 0 && tmpto < to)
582                                                 to = tmpto;
583                                         else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
584                                                 ast_stopstream(tmpuser->ochan);
585                                                 if (tmpuser->state == 1) {
586                                                         ast_verb(3, "Playback of the call-from file appears to be done.\n");
587                                                         if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
588                                                                 tmpuser->state = 2;
589                                                         } else {
590                                                                 ast_log(LOG_NOTICE, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc);
591                                                                 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
592                                                                 tmpuser->ynidx = 0;
593                                                                 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
594                                                                         tmpuser->state = 3;
595                                                                 else {
596                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
597                                                                         return NULL;
598                                                                 } 
599                                                         }
600                                                 } else if (tmpuser->state == 2) {
601                                                         ast_verb(3, "Playback of name file appears to be done.\n");
602                                                         memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
603                                                         tmpuser->ynidx = 0;
604                                                         if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
605                                                                 tmpuser->state = 3;
606                                                         } else {
607                                                                 return NULL;
608                                                         } 
609                                                 } else if (tmpuser->state == 3) {
610                                                         ast_verb(3, "Playback of the next step file appears to be done.\n");
611                                                         tmpuser->digts = 0;
612                                                 }
613                                         }
614                                 }
615                                 watchers[pos++] = tmpuser->ochan;
616                                 livechannels++;
617                         }
618                 }
619
620                 tmpto = to;
621                 if (to < 0) {
622                         to = 1000;
623                         tmpto = 1000;
624                 }
625                 towas = to;
626                 winner = ast_waitfor_n(watchers, pos, &to);
627                 tmpto -= to;
628                 totalwait -= tmpto;
629                 wtd = to;
630                 if (totalwait <= 0) {
631                         ast_verb(3, "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
632                         clear_calling_tree(findme_user_list);
633                         return NULL;
634                 }
635                 if (winner) {
636                         /* Need to find out which channel this is */
637                         dg = 0;
638                         while ((winner != watchers[dg]) && (dg < 256))
639                                 dg++;
640                         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
641                                 if (tmpuser->ochan == winner)
642                                         break;
643                         f = ast_read(winner);
644                         if (f) {
645                                 if (f->frametype == AST_FRAME_CONTROL) {
646                                         switch(f->subclass) {
647                                         case AST_CONTROL_HANGUP:
648                                                 ast_verb(3, "%s received a hangup frame.\n", winner->name);
649                                                 if (f->data.uint32) {
650                                                         winner->hangupcause = f->data.uint32;
651                                                 }
652                                                 if (dg == 0) {
653                                                         ast_verb(3, "The calling channel hungup. Need to drop everyone else.\n");
654                                                         clear_calling_tree(findme_user_list);
655                                                         ctstatus = -1;
656                                                 }
657                                                 break;
658                                         case AST_CONTROL_ANSWER:
659                                                 ast_verb(3, "%s answered %s\n", winner->name, caller->name);
660                                                 /* If call has been answered, then the eventual hangup is likely to be normal hangup */ 
661                                                 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
662                                                 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
663                                                 ast_verb(3, "Starting playback of %s\n", callfromname);
664                                                 if (dg > 0) {
665                                                         if (!ast_strlen_zero(namerecloc)) {
666                                                                 if (!ast_streamfile(winner, callfromname, winner->language)) {
667                                                                         ast_sched_runq(winner->sched);
668                                                                         tmpuser->state = 1;
669                                                                 } else {
670                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
671                                                                         ast_frfree(f);
672                                                                         return NULL;
673                                                                 }
674                                                         } else {
675                                                                 tmpuser->state = 2;
676                                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
677                                                                         ast_sched_runq(tmpuser->ochan->sched);
678                                                                 else {
679                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
680                                                                         ast_frfree(f);
681                                                                         return NULL;
682                                                                 }
683                                                         }
684                                                 }
685                                                 break;
686                                         case AST_CONTROL_BUSY:
687                                                 ast_verb(3, "%s is busy\n", winner->name);
688                                                 break;
689                                         case AST_CONTROL_CONGESTION:
690                                                 ast_verb(3, "%s is circuit-busy\n", winner->name);
691                                                 break;
692                                         case AST_CONTROL_RINGING:
693                                                 ast_verb(3, "%s is ringing\n", winner->name);
694                                                 break;
695                                         case AST_CONTROL_PROGRESS:
696                                                 ast_verb(3, "%s is making progress passing it to %s\n", winner->name, caller->name);
697                                                 break;
698                                         case AST_CONTROL_VIDUPDATE:
699                                                 ast_verb(3, "%s requested a video update, passing it to %s\n", winner->name, caller->name);
700                                                 break;
701                                         case AST_CONTROL_SRCUPDATE:
702                                                 ast_verb(3, "%s requested a source update, passing it to %s\n", winner->name, caller->name);
703                                                 break;
704                                         case AST_CONTROL_PROCEEDING:
705                                                 ast_verb(3, "%s is proceeding passing it to %s\n", winner->name,caller->name);
706                                                 break;
707                                         case AST_CONTROL_HOLD:
708                                                 ast_verb(3, "Call on %s placed on hold\n", winner->name);
709                                                 break;
710                                         case AST_CONTROL_UNHOLD:
711                                                 ast_verb(3, "Call on %s left from hold\n", winner->name);
712                                                 break;
713                                         case AST_CONTROL_OFFHOOK:
714                                         case AST_CONTROL_FLASH:
715                                                 /* Ignore going off hook and flash */
716                                                 break;
717                                         case -1:
718                                                 ast_verb(3, "%s stopped sounds\n", winner->name);
719                                                 break;
720                                         default:
721                                                 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
722                                                 break;
723                                         }
724                                 } 
725                                 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
726                                         if (winner->stream)
727                                                 ast_stopstream(winner);
728                                         tmpuser->digts = 0;
729                                         ast_debug(1, "DTMF received: %c\n",(char) f->subclass);
730                                         tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
731                                         tmpuser->ynidx++;
732                                         ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
733                                         if (tmpuser->ynidx >= ynlongest) {
734                                                 ast_debug(1, "reached longest possible match - doing evals\n");
735                                                 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
736                                                         ast_debug(1, "Match to take the call!\n");
737                                                         ast_frfree(f);
738                                                         return tmpuser->ochan;
739                                                 }
740                                                 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
741                                                         ast_debug(1, "Next in dial plan step requested.\n");
742                                                         *status = 1;
743                                                         ast_frfree(f);
744                                                         return NULL;
745                                                 }
746
747                                         }
748                                 }
749
750                                 ast_frfree(f);
751                         } else {
752                                 if (winner) {
753                                         ast_debug(1, "we didn't get a frame. hanging up. dg is %d\n",dg);                                             
754                                         if (!dg) {
755                                                 clear_calling_tree(findme_user_list);
756                                                 return NULL;
757                                         } else {
758                                                 tmpuser->state = -1;
759                                                 ast_hangup(winner);  
760                                                 livechannels--;
761                                                 ast_debug(1, "live channels left %d\n", livechannels);
762                                                 if (!livechannels) {
763                                                         ast_verb(3, "no live channels left. exiting.\n");
764                                                         return NULL;
765                                                 }
766                                         }
767                                 }
768                         }
769
770                 } else
771                         ast_debug(1, "timed out waiting for action\n");
772         }
773
774         /* --- WAIT FOR WINNER NUMBER END! -----------*/
775         return NULL;
776 }
777
778 static void findmeexec(struct fm_args *tpargs)
779 {
780         struct number *nm;
781         struct ast_channel *outbound;
782         struct ast_channel *caller;
783         struct ast_channel *winner = NULL;
784         char dialarg[512];
785         int dg, idx;
786         char *rest, *number;
787         struct findme_user *tmpuser;
788         struct findme_user *fmuser;
789         struct findme_user *headuser;
790         struct findme_user_listptr *findme_user_list;
791         int status;
792
793         findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
794         AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
795
796         /* We're going to figure out what the longest possible string of digits to collect is */
797         ynlongest = 0;
798         if (strlen(tpargs->takecall) > ynlongest)
799                 ynlongest = strlen(tpargs->takecall);
800         if (strlen(tpargs->nextindp) > ynlongest)
801                 ynlongest = strlen(tpargs->nextindp);
802
803         idx = 1;
804         caller = tpargs->chan;
805         AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
806                 if (nm->order == idx)
807                         break;
808
809         while (nm) {
810
811                 ast_debug(2, "Number %s timeout %ld\n", nm->number,nm->timeout);
812
813                 number = ast_strdupa(nm->number);
814                 ast_debug(3, "examining %s\n", number);
815                 do {
816                         rest = strchr(number, '&');
817                         if (rest) {
818                                 *rest = 0;
819                                 rest++;
820                         }
821
822                         if (!strcmp(tpargs->context, ""))
823                                 snprintf(dialarg, sizeof(dialarg), "%s", number);
824                         else
825                                 snprintf(dialarg, sizeof(dialarg), "%s@%s", number, tpargs->context);
826
827                         tmpuser = ast_calloc(1, sizeof(*tmpuser));
828                         if (!tmpuser) {
829                                 ast_log(LOG_WARNING, "Out of memory!\n");
830                                 ast_free(findme_user_list);
831                                 return;
832                         }
833
834                         outbound = ast_request("Local", ast_best_codec(caller->nativeformats), caller, dialarg, &dg);
835                         if (outbound) {
836                                 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
837                                 ast_channel_inherit_variables(tpargs->chan, outbound);
838                                 ast_verb(3, "calling %s\n", dialarg);
839                                 if (!ast_call(outbound,dialarg,0)) {
840                                         tmpuser->ochan = outbound;
841                                         tmpuser->state = 0;
842                                         tmpuser->cleared = 0;
843                                         ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
844                                         AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
845                                 } else {
846                                         ast_verb(3, "couldn't reach at this number.\n"); 
847                                         if (outbound) {
848                                                 if (!outbound->cdr) 
849                                                         outbound->cdr = ast_cdr_alloc();
850                                                 if (outbound->cdr) {
851                                                         char tmp[256];
852
853                                                         ast_cdr_init(outbound->cdr, outbound);
854                                                         snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
855                                                         ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
856                                                         ast_cdr_update(outbound);
857                                                         ast_cdr_start(outbound->cdr);
858                                                         ast_cdr_end(outbound->cdr);
859                                                         /* If the cause wasn't handled properly */
860                                                         if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
861                                                                 ast_cdr_failed(outbound->cdr);
862                                                 } else {
863                                                         ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
864                                                         ast_hangup(outbound);
865                                                         outbound = NULL;
866                                                 }
867                                         }
868                                 }
869                         } else 
870                                 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
871
872                         number = rest;
873                 } while (number);
874
875                 status = 0;
876                 if (!AST_LIST_EMPTY(findme_user_list))
877                         winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
878
879                 while ((fmuser = AST_LIST_REMOVE_HEAD(findme_user_list, entry))) {
880                         if (!fmuser->cleared && fmuser->ochan != winner)
881                                 clear_caller(fmuser);
882                         ast_free(fmuser);
883                 }
884
885                 fmuser = NULL;
886                 tmpuser = NULL;
887                 headuser = NULL;
888                 if (winner)
889                         break;
890
891                 if (!caller || ast_check_hangup(caller)) {
892                         tpargs->status = 1;
893                         ast_free(findme_user_list);
894                         return;
895                 }
896
897                 idx++;
898                 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
899                         if (nm->order == idx)
900                                 break;
901                 }
902         }
903         ast_free(findme_user_list);
904         if (!winner) 
905                 tpargs->status = 1;
906         else {
907                 tpargs->status = 100;
908                 tpargs->outbound = winner;
909         }
910
911         return;
912 }
913
914 static struct call_followme *find_realtime(const char *name)
915 {
916         struct ast_variable *var = ast_load_realtime("followme", "name", name, SENTINEL), *v;
917         struct ast_config *cfg;
918         const char *catg;
919         struct call_followme *new;
920         struct ast_str *str = ast_str_create(16);
921
922         if (!var) {
923                 return NULL;
924         }
925
926         if (!(new = alloc_profile(name))) {
927                 return NULL;
928         }
929
930         for (v = var; v; v = v->next) {
931                 if (!strcasecmp(v->name, "active")) {
932                         if (ast_false(v->value)) {
933                                 ast_mutex_destroy(&new->lock);
934                                 ast_free(new);
935                                 return NULL;
936                         }
937                 } else {
938                         profile_set_param(new, v->name, v->value, 0, 0);
939                 }
940         }
941
942         ast_variables_destroy(var);
943         new->realtime = 1;
944
945         /* Load numbers */
946         if (!(cfg = ast_load_realtime_multientry("followme_numbers", "ordinal LIKE", "%", "name", name, SENTINEL))) {
947                 ast_mutex_destroy(&new->lock);
948                 ast_free(new);
949                 return NULL;
950         }
951
952         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
953                 const char *numstr, *timeoutstr, *ordstr;
954                 int timeout;
955                 struct number *cur;
956                 if (!(numstr = ast_variable_retrieve(cfg, catg, "phonenumber"))) {
957                         continue;
958                 }
959                 if (!(timeoutstr = ast_variable_retrieve(cfg, catg, "timeout")) || sscanf(timeoutstr, "%30d", &timeout) != 1 || timeout < 1) {
960                         timeout = 25;
961                 }
962                 /* This one has to exist; it was part of the query */
963                 ordstr = ast_variable_retrieve(cfg, catg, "ordinal");
964                 ast_str_set(&str, 0, "%s", numstr);
965                 if ((cur = create_followme_number(ast_str_buffer(str), timeout, atoi(ordstr)))) {
966                         AST_LIST_INSERT_TAIL(&new->numbers, cur, entry);
967                 }
968         }
969         ast_config_destroy(cfg);
970
971         return new;
972 }
973
974 static void end_bridge_callback(void *data)
975 {
976         char buf[80];
977         time_t end;
978         struct ast_channel *chan = data;
979
980         time(&end);
981
982         ast_channel_lock(chan);
983         if (chan->cdr->answer.tv_sec) {
984                 snprintf(buf, sizeof(buf), "%ld", end - chan->cdr->answer.tv_sec);
985                 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
986         }
987
988         if (chan->cdr->start.tv_sec) {
989                 snprintf(buf, sizeof(buf), "%ld", end - chan->cdr->start.tv_sec);
990                 pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
991         }
992         ast_channel_unlock(chan);
993 }
994
995 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
996 {
997         bconfig->end_bridge_callback_data = originator;
998 }
999
1000 static int app_exec(struct ast_channel *chan, const char *data)
1001 {
1002         struct fm_args targs = { 0, };
1003         struct ast_bridge_config config;
1004         struct call_followme *f;
1005         struct number *nm, *newnm;
1006         int res = 0;
1007         char *argstr;
1008         char namerecloc[255];
1009         int duration = 0;
1010         struct ast_channel *caller;
1011         struct ast_channel *outbound;
1012         AST_DECLARE_APP_ARGS(args,
1013                 AST_APP_ARG(followmeid);
1014                 AST_APP_ARG(options);
1015         );
1016
1017         if (ast_strlen_zero(data)) {
1018                 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1019                 return -1;
1020         }
1021
1022         if (!(argstr = ast_strdupa((char *)data))) {
1023                 ast_log(LOG_ERROR, "Out of memory!\n");
1024                 return -1;
1025         }
1026
1027         AST_STANDARD_APP_ARGS(args, argstr);
1028
1029         if (ast_strlen_zero(args.followmeid)) {
1030                 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1031                 return -1;
1032         }
1033
1034         AST_RWLIST_RDLOCK(&followmes);
1035         AST_RWLIST_TRAVERSE(&followmes, f, entry) {
1036                 if (!strcasecmp(f->name, args.followmeid) && (f->active))
1037                         break;
1038         }
1039         AST_RWLIST_UNLOCK(&followmes);
1040
1041         ast_debug(1, "New profile %s.\n", args.followmeid);
1042
1043         if (!f) {
1044                 f = find_realtime(args.followmeid);
1045         }
1046
1047         if (!f) {
1048                 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
1049                 return 0;
1050         }
1051
1052         /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
1053         if (args.options) 
1054                 ast_app_parse_options(followme_opts, &targs.followmeflags, NULL, args.options);
1055
1056         /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
1057         ast_mutex_lock(&f->lock);
1058         targs.mohclass = ast_strdupa(f->moh);
1059         ast_copy_string(targs.context, f->context, sizeof(targs.context));
1060         ast_copy_string(targs.takecall, f->takecall, sizeof(targs.takecall));
1061         ast_copy_string(targs.nextindp, f->nextindp, sizeof(targs.nextindp));
1062         ast_copy_string(targs.callfromprompt, f->callfromprompt, sizeof(targs.callfromprompt));
1063         ast_copy_string(targs.norecordingprompt, f->norecordingprompt, sizeof(targs.norecordingprompt));
1064         ast_copy_string(targs.optionsprompt, f->optionsprompt, sizeof(targs.optionsprompt));
1065         ast_copy_string(targs.plsholdprompt, f->plsholdprompt, sizeof(targs.plsholdprompt));
1066         ast_copy_string(targs.statusprompt, f->statusprompt, sizeof(targs.statusprompt));
1067         ast_copy_string(targs.sorryprompt, f->sorryprompt, sizeof(targs.sorryprompt));
1068         /* Copy the numbers we're going to use into another list in case the master list should get modified 
1069            (and locked) while we're trying to do a follow-me */
1070         AST_LIST_HEAD_INIT_NOLOCK(&targs.cnumbers);
1071         AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
1072                 newnm = create_followme_number(nm->number, nm->timeout, nm->order);
1073                 AST_LIST_INSERT_TAIL(&targs.cnumbers, newnm, entry);
1074         }
1075         ast_mutex_unlock(&f->lock);
1076
1077         /* Answer the call */
1078         if (chan->_state != AST_STATE_UP) {
1079                 ast_answer(chan);
1080         }
1081
1082         if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_STATUSMSG)) 
1083                 ast_stream_and_wait(chan, targs.statusprompt, "");
1084
1085         snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
1086         duration = 5;
1087
1088         if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_RECORDNAME)) 
1089                 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL) < 0)
1090                         goto outrun;
1091
1092         if (!ast_fileexists(namerecloc, NULL, chan->language))
1093                 ast_copy_string(namerecloc, "", sizeof(namerecloc));
1094
1095         if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1096                 goto outrun;
1097         if (ast_waitstream(chan, "") < 0)
1098                 goto outrun;
1099         ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1100
1101         targs.status = 0;
1102         targs.chan = chan;
1103         ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1104
1105         findmeexec(&targs);
1106
1107         while ((nm = AST_LIST_REMOVE_HEAD(&targs.cnumbers, entry)))
1108                 ast_free(nm);
1109
1110         if (!ast_strlen_zero(namerecloc))
1111                 unlink(namerecloc);
1112
1113         if (targs.status != 100) {
1114                 ast_moh_stop(chan);
1115                 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG)) 
1116                         ast_stream_and_wait(chan, targs.sorryprompt, "");
1117                 res = 0;
1118         } else {
1119                 caller = chan;
1120                 outbound = targs.outbound;
1121                 /* Bridge the two channels. */
1122
1123                 memset(&config, 0, sizeof(config));
1124                 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1125                 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1126                 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1127                 config.end_bridge_callback = end_bridge_callback;
1128                 config.end_bridge_callback_data = chan;
1129                 config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
1130
1131                 ast_moh_stop(caller);
1132                 /* Be sure no generators are left on it */
1133                 ast_deactivate_generator(caller);
1134                 /* Make sure channels are compatible */
1135                 res = ast_channel_make_compatible(caller, outbound);
1136                 if (res < 0) {
1137                         ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1138                         ast_hangup(outbound);
1139                         goto outrun;
1140                 }
1141                 res = ast_bridge_call(caller, outbound, &config);
1142                 if (outbound)
1143                         ast_hangup(outbound);
1144         }
1145
1146         outrun:
1147
1148         if (f->realtime) {
1149                 /* Not in list */
1150                 free_numbers(f);
1151                 ast_free(f);
1152         }
1153
1154         return res;
1155 }
1156
1157 static int unload_module(void)
1158 {
1159         struct call_followme *f;
1160
1161         ast_unregister_application(app);
1162
1163         /* Free Memory. Yeah! I'm free! */
1164         AST_RWLIST_WRLOCK(&followmes);
1165         while ((f = AST_RWLIST_REMOVE_HEAD(&followmes, entry))) {
1166                 free_numbers(f);
1167                 ast_free(f);
1168         }
1169
1170         AST_RWLIST_UNLOCK(&followmes);
1171
1172         return 0;
1173 }
1174
1175 static int load_module(void)
1176 {
1177         if(!reload_followme(0))
1178                 return AST_MODULE_LOAD_DECLINE;
1179
1180         return ast_register_application_xml(app, app_exec);
1181 }
1182
1183 static int reload(void)
1184 {
1185         reload_followme(1);
1186
1187         return 0;
1188 }
1189
1190 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1191                 .load = load_module,
1192                 .unload = unload_module,
1193                 .reload = reload,
1194                );