Make the options for the general and profiles more consistent
[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(char *number, int timeout, int numorder)
279 {
280         struct number *cur;
281         char *tmp;
282
283         if (!(cur = ast_calloc(1, sizeof(*cur))))
284                 return NULL;
285
286         cur->timeout = timeout;
287         if ((tmp = strchr(number, ','))) 
288                 *tmp = '\0';
289         ast_copy_string(cur->number, number, sizeof(cur->number));
290         cur->order = numorder;
291         ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
292
293         return cur;
294 }
295
296 /*! \brief Reload followme application module */
297 static int reload_followme(int reload)
298 {
299         struct call_followme *f;
300         struct ast_config *cfg;
301         char *cat = NULL, *tmp;
302         struct ast_variable *var;
303         struct number *cur, *nm;
304         char numberstr[90];
305         int timeout;
306         char *timeoutstr;
307         int numorder;
308         const char *takecallstr;
309         const char *declinecallstr;
310         const char *tmpstr;
311         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
312
313         if (!(cfg = ast_config_load("followme.conf", config_flags))) {
314                 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
315                 return 0;
316         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
317                 return 0;
318         } else if (cfg == CONFIG_STATUS_FILEINVALID) {
319                 ast_log(LOG_ERROR, "Config file followme.conf is in an invalid format.  Aborting.\n");
320                 return 0;
321         }
322
323         AST_RWLIST_WRLOCK(&followmes);
324
325         /* Reset Global Var Values */
326         featuredigittimeout = 5000;
327
328         /* Mark all profiles as inactive for the moment */
329         AST_RWLIST_TRAVERSE(&followmes, f, entry) {
330                 f->active = 0;
331         }
332
333         featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
334
335         if (!ast_strlen_zero(featuredigittostr)) {
336                 if (!sscanf(featuredigittostr, "%d", &featuredigittimeout))
337                         featuredigittimeout = 5000;
338         }
339
340         if ((takecallstr = ast_variable_retrieve(cfg, "general", "takecall")) && !ast_strlen_zero(takecallstr)) {
341                 ast_copy_string(takecall, takecallstr, sizeof(takecall));
342         }
343
344         if ((declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall")) && !ast_strlen_zero(declinecallstr)) {
345                 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
346         }
347
348         if ((tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt")) && !ast_strlen_zero(tmpstr)) {
349                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
350         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "call_from_prompt")) && !ast_strlen_zero(tmpstr)) {
351                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
352         }
353
354         if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt")) && !ast_strlen_zero(tmpstr)) {
355                 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
356         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording_prompt")) && !ast_strlen_zero(tmpstr)) {
357                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
358         }
359
360
361         if ((tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt")) && !ast_strlen_zero(tmpstr)) {
362                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
363         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "options_prompt")) && !ast_strlen_zero(tmpstr)) {
364                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
365         }
366
367         if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt")) && !ast_strlen_zero(tmpstr)) {
368                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
369         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls_hold_prompt")) && !ast_strlen_zero(tmpstr)) {
370                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
371         }
372
373         if ((tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt")) && !ast_strlen_zero(tmpstr)) {
374                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
375         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "status_prompt")) && !ast_strlen_zero(tmpstr)) {
376                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
377         }
378
379         if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt")) && !ast_strlen_zero(tmpstr)) {
380                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
381         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry_prompt")) && !ast_strlen_zero(tmpstr)) {
382                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
383         }
384
385         /* Chug through config file */
386         while ((cat = ast_category_browse(cfg, cat))) {
387                 int new = 0;
388
389                 if (!strcasecmp(cat, "general"))
390                         continue;
391
392                 /* Look for an existing one */
393                 AST_LIST_TRAVERSE(&followmes, f, entry) {
394                         if (!strcasecmp(f->name, cat))
395                                 break;
396                 }
397
398                 ast_debug(1, "New profile %s.\n", cat);
399
400                 if (!f) {
401                         /* Make one then */
402                         f = alloc_profile(cat);
403                         new = 1;
404                 }
405
406                 /* Totally fail if we fail to find/create an entry */
407                 if (!f)
408                         continue;
409
410                 if (!new)
411                         ast_mutex_lock(&f->lock);
412                 /* Re-initialize the profile */
413                 init_profile(f);
414                 free_numbers(f);
415                 var = ast_variable_browse(cfg, cat);
416                 while (var) {
417                         if (!strcasecmp(var->name, "number")) {
418                                 int idx = 0;
419
420                                 /* Add a new number */
421                                 ast_copy_string(numberstr, var->value, sizeof(numberstr));
422                                 if ((tmp = strchr(numberstr, ','))) {
423                                         *tmp++ = '\0';
424                                         timeoutstr = ast_strdupa(tmp);
425                                         if ((tmp = strchr(timeoutstr, ','))) {
426                                                 *tmp++ = '\0';
427                                                 numorder = atoi(tmp);
428                                                 if (numorder < 0)
429                                                         numorder = 0;
430                                         } else 
431                                                 numorder = 0;
432                                         timeout = atoi(timeoutstr);
433                                         if (timeout < 0) 
434                                                 timeout = 25;
435                                 } else {
436                                         timeout = 25;
437                                         numorder = 0;
438                                 }
439
440                                 if (!numorder) {
441                                         idx = 1;
442                                         AST_LIST_TRAVERSE(&f->numbers, nm, entry) 
443                                                 idx++;
444                                         numorder = idx;
445                                 }
446                                 cur = create_followme_number(numberstr, timeout, numorder);
447                                 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
448                         } else {
449                                 profile_set_param(f, var->name, var->value, var->lineno, 1);
450                                 ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
451                         }
452                         var = var->next;
453                 } /* End while(var) loop */
454
455                 if (!new) 
456                         ast_mutex_unlock(&f->lock);
457                 else
458                         AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
459         }
460
461         ast_config_destroy(cfg);
462
463         AST_RWLIST_UNLOCK(&followmes);
464
465         return 1;
466 }
467
468 static void clear_caller(struct findme_user *tmpuser)
469 {
470         struct ast_channel *outbound;
471
472         if (tmpuser && tmpuser->ochan && tmpuser->state >= 0) {
473                 outbound = tmpuser->ochan;
474                 if (!outbound->cdr) {
475                         outbound->cdr = ast_cdr_alloc();
476                         if (outbound->cdr)
477                                 ast_cdr_init(outbound->cdr, outbound);
478                 }
479                 if (outbound->cdr) {
480                         char tmp[256];
481
482                         snprintf(tmp, sizeof(tmp), "%s/%s", "Local", tmpuser->dialarg);
483                         ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
484                         ast_cdr_update(outbound);
485                         ast_cdr_start(outbound->cdr);
486                         ast_cdr_end(outbound->cdr);
487                         /* If the cause wasn't handled properly */
488                         if (ast_cdr_disposition(outbound->cdr, outbound->hangupcause))
489                                 ast_cdr_failed(outbound->cdr);
490                 } else
491                         ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
492                 ast_hangup(tmpuser->ochan);
493         }
494
495 }
496
497 static void clear_calling_tree(struct findme_user_listptr *findme_user_list) 
498 {
499         struct findme_user *tmpuser;
500
501         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
502                 clear_caller(tmpuser);
503                 tmpuser->cleared = 1;
504         }
505 }
506
507
508
509 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) 
510 {
511         struct ast_channel *watchers[256];
512         int pos;
513         struct ast_channel *winner;
514         struct ast_frame *f;
515         int ctstatus = 0;
516         int dg;
517         struct findme_user *tmpuser;
518         int to = 0;
519         int livechannels = 0;
520         int tmpto;
521         long totalwait = 0, wtd = 0, towas = 0;
522         char *callfromname;
523         char *pressbuttonname;
524
525         /* ------------ wait_for_winner_channel start --------------- */ 
526
527         callfromname = ast_strdupa(tpargs->callfromprompt);
528         pressbuttonname = ast_strdupa(tpargs->optionsprompt);
529
530         if (AST_LIST_EMPTY(findme_user_list)) {
531                 ast_verb(3, "couldn't reach at this number.\n");
532                 return NULL;
533         }
534
535         if (!caller) {
536                 ast_verb(3, "Original caller hungup. Cleanup.\n");
537                 clear_calling_tree(findme_user_list);
538                 return NULL;
539         }
540
541         totalwait = nm->timeout * 1000;
542
543         while (!ctstatus) {
544                 to = 1000;
545                 pos = 1; 
546                 livechannels = 0;
547                 watchers[0] = caller;
548
549                 dg = 0;
550                 winner = NULL;
551                 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
552                         if (tmpuser->state >= 0 && tmpuser->ochan) {
553                                 if (tmpuser->state == 3) 
554                                         tmpuser->digts += (towas - wtd);
555                                 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
556                                         ast_verb(3, "We've been waiting for digits longer than we should have.\n");
557                                         if (!ast_strlen_zero(namerecloc)) {
558                                                 tmpuser->state = 1;
559                                                 tmpuser->digts = 0;
560                                                 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
561                                                         ast_sched_runq(tmpuser->ochan->sched);
562                                                 } else {
563                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
564                                                         return NULL;
565                                                 }
566                                         } else {
567                                                 tmpuser->state = 2;
568                                                 tmpuser->digts = 0;
569                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
570                                                         ast_sched_runq(tmpuser->ochan->sched);
571                                                 else {
572                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
573                                                         return NULL;
574                                                 }
575                                         }
576                                 }
577                                 if (tmpuser->ochan->stream) {
578                                         ast_sched_runq(tmpuser->ochan->sched);
579                                         tmpto = ast_sched_wait(tmpuser->ochan->sched);
580                                         if (tmpto > 0 && tmpto < to)
581                                                 to = tmpto;
582                                         else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
583                                                 ast_stopstream(tmpuser->ochan);
584                                                 if (tmpuser->state == 1) {
585                                                         ast_verb(3, "Playback of the call-from file appears to be done.\n");
586                                                         if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
587                                                                 tmpuser->state = 2;
588                                                         } else {
589                                                                 ast_log(LOG_NOTICE, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc);
590                                                                 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
591                                                                 tmpuser->ynidx = 0;
592                                                                 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
593                                                                         tmpuser->state = 3;
594                                                                 else {
595                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
596                                                                         return NULL;
597                                                                 } 
598                                                         }
599                                                 } else if (tmpuser->state == 2) {
600                                                         ast_verb(3, "Playback of name file appears to be done.\n");
601                                                         memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
602                                                         tmpuser->ynidx = 0;
603                                                         if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
604                                                                 tmpuser->state = 3;
605                                                         } else {
606                                                                 return NULL;
607                                                         } 
608                                                 } else if (tmpuser->state == 3) {
609                                                         ast_verb(3, "Playback of the next step file appears to be done.\n");
610                                                         tmpuser->digts = 0;
611                                                 }
612                                         }
613                                 }
614                                 watchers[pos++] = tmpuser->ochan;
615                                 livechannels++;
616                         }
617                 }
618
619                 tmpto = to;
620                 if (to < 0) {
621                         to = 1000;
622                         tmpto = 1000;
623                 }
624                 towas = to;
625                 winner = ast_waitfor_n(watchers, pos, &to);
626                 tmpto -= to;
627                 totalwait -= tmpto;
628                 wtd = to;
629                 if (totalwait <= 0) {
630                         ast_verb(3, "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
631                         clear_calling_tree(findme_user_list);
632                         return NULL;
633                 }
634                 if (winner) {
635                         /* Need to find out which channel this is */
636                         dg = 0;
637                         while ((winner != watchers[dg]) && (dg < 256))
638                                 dg++;
639                         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
640                                 if (tmpuser->ochan == winner)
641                                         break;
642                         f = ast_read(winner);
643                         if (f) {
644                                 if (f->frametype == AST_FRAME_CONTROL) {
645                                         switch(f->subclass) {
646                                         case AST_CONTROL_HANGUP:
647                                                 ast_verb(3, "%s received a hangup frame.\n", winner->name);
648                                                 if (f->data.uint32) {
649                                                         winner->hangupcause = f->data.uint32;
650                                                 }
651                                                 if (dg == 0) {
652                                                         ast_verb(3, "The calling channel hungup. Need to drop everyone else.\n");
653                                                         clear_calling_tree(findme_user_list);
654                                                         ctstatus = -1;
655                                                 }
656                                                 break;
657                                         case AST_CONTROL_ANSWER:
658                                                 ast_verb(3, "%s answered %s\n", winner->name, caller->name);
659                                                 /* If call has been answered, then the eventual hangup is likely to be normal hangup */ 
660                                                 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
661                                                 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
662                                                 ast_verb(3, "Starting playback of %s\n", callfromname);
663                                                 if (dg > 0) {
664                                                         if (!ast_strlen_zero(namerecloc)) {
665                                                                 if (!ast_streamfile(winner, callfromname, winner->language)) {
666                                                                         ast_sched_runq(winner->sched);
667                                                                         tmpuser->state = 1;
668                                                                 } else {
669                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
670                                                                         ast_frfree(f);
671                                                                         return NULL;
672                                                                 }
673                                                         } else {
674                                                                 tmpuser->state = 2;
675                                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
676                                                                         ast_sched_runq(tmpuser->ochan->sched);
677                                                                 else {
678                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
679                                                                         ast_frfree(f);
680                                                                         return NULL;
681                                                                 }
682                                                         }
683                                                 }
684                                                 break;
685                                         case AST_CONTROL_BUSY:
686                                                 ast_verb(3, "%s is busy\n", winner->name);
687                                                 break;
688                                         case AST_CONTROL_CONGESTION:
689                                                 ast_verb(3, "%s is circuit-busy\n", winner->name);
690                                                 break;
691                                         case AST_CONTROL_RINGING:
692                                                 ast_verb(3, "%s is ringing\n", winner->name);
693                                                 break;
694                                         case AST_CONTROL_PROGRESS:
695                                                 ast_verb(3, "%s is making progress passing it to %s\n", winner->name, caller->name);
696                                                 break;
697                                         case AST_CONTROL_VIDUPDATE:
698                                                 ast_verb(3, "%s requested a video update, passing it to %s\n", winner->name, caller->name);
699                                                 break;
700                                         case AST_CONTROL_SRCUPDATE:
701                                                 ast_verb(3, "%s requested a source update, passing it to %s\n", winner->name, caller->name);
702                                                 break;
703                                         case AST_CONTROL_PROCEEDING:
704                                                 ast_verb(3, "%s is proceeding passing it to %s\n", winner->name,caller->name);
705                                                 break;
706                                         case AST_CONTROL_HOLD:
707                                                 ast_verb(3, "Call on %s placed on hold\n", winner->name);
708                                                 break;
709                                         case AST_CONTROL_UNHOLD:
710                                                 ast_verb(3, "Call on %s left from hold\n", winner->name);
711                                                 break;
712                                         case AST_CONTROL_OFFHOOK:
713                                         case AST_CONTROL_FLASH:
714                                                 /* Ignore going off hook and flash */
715                                                 break;
716                                         case -1:
717                                                 ast_verb(3, "%s stopped sounds\n", winner->name);
718                                                 break;
719                                         default:
720                                                 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
721                                                 break;
722                                         }
723                                 } 
724                                 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
725                                         if (winner->stream)
726                                                 ast_stopstream(winner);
727                                         tmpuser->digts = 0;
728                                         ast_debug(1, "DTMF received: %c\n",(char) f->subclass);
729                                         tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
730                                         tmpuser->ynidx++;
731                                         ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
732                                         if (tmpuser->ynidx >= ynlongest) {
733                                                 ast_debug(1, "reached longest possible match - doing evals\n");
734                                                 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
735                                                         ast_debug(1, "Match to take the call!\n");
736                                                         ast_frfree(f);
737                                                         return tmpuser->ochan;
738                                                 }
739                                                 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
740                                                         ast_debug(1, "Next in dial plan step requested.\n");
741                                                         *status = 1;
742                                                         ast_frfree(f);
743                                                         return NULL;
744                                                 }
745
746                                         }
747                                 }
748
749                                 ast_frfree(f);
750                         } else {
751                                 if (winner) {
752                                         ast_debug(1, "we didn't get a frame. hanging up. dg is %d\n",dg);                                             
753                                         if (!dg) {
754                                                 clear_calling_tree(findme_user_list);
755                                                 return NULL;
756                                         } else {
757                                                 tmpuser->state = -1;
758                                                 ast_hangup(winner);  
759                                                 livechannels--;
760                                                 ast_debug(1, "live channels left %d\n", livechannels);
761                                                 if (!livechannels) {
762                                                         ast_verb(3, "no live channels left. exiting.\n");
763                                                         return NULL;
764                                                 }
765                                         }
766                                 }
767                         }
768
769                 } else
770                         ast_debug(1, "timed out waiting for action\n");
771         }
772
773         /* --- WAIT FOR WINNER NUMBER END! -----------*/
774         return NULL;
775 }
776
777 static void findmeexec(struct fm_args *tpargs)
778 {
779         struct number *nm;
780         struct ast_channel *outbound;
781         struct ast_channel *caller;
782         struct ast_channel *winner = NULL;
783         char dialarg[512];
784         int dg, idx;
785         char *rest, *number;
786         struct findme_user *tmpuser;
787         struct findme_user *fmuser;
788         struct findme_user *headuser;
789         struct findme_user_listptr *findme_user_list;
790         int status;
791
792         findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
793         AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
794
795         /* We're going to figure out what the longest possible string of digits to collect is */
796         ynlongest = 0;
797         if (strlen(tpargs->takecall) > ynlongest)
798                 ynlongest = strlen(tpargs->takecall);
799         if (strlen(tpargs->nextindp) > ynlongest)
800                 ynlongest = strlen(tpargs->nextindp);
801
802         idx = 1;
803         caller = tpargs->chan;
804         AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
805                 if (nm->order == idx)
806                         break;
807
808         while (nm) {
809
810                 ast_debug(2, "Number %s timeout %ld\n", nm->number,nm->timeout);
811
812                 number = ast_strdupa(nm->number);
813                 ast_debug(3, "examining %s\n", number);
814                 do {
815                         rest = strchr(number, '&');
816                         if (rest) {
817                                 *rest = 0;
818                                 rest++;
819                         }
820
821                         if (!strcmp(tpargs->context, ""))
822                                 snprintf(dialarg, sizeof(dialarg), "%s", number);
823                         else
824                                 snprintf(dialarg, sizeof(dialarg), "%s@%s", number, tpargs->context);
825
826                         tmpuser = ast_calloc(1, sizeof(*tmpuser));
827                         if (!tmpuser) {
828                                 ast_log(LOG_WARNING, "Out of memory!\n");
829                                 ast_free(findme_user_list);
830                                 return;
831                         }
832
833                         outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
834                         if (outbound) {
835                                 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
836                                 ast_channel_inherit_variables(tpargs->chan, outbound);
837                                 ast_verb(3, "calling %s\n", dialarg);
838                                 if (!ast_call(outbound,dialarg,0)) {
839                                         tmpuser->ochan = outbound;
840                                         tmpuser->state = 0;
841                                         tmpuser->cleared = 0;
842                                         ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
843                                         AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
844                                 } else {
845                                         ast_verb(3, "couldn't reach at this number.\n"); 
846                                         if (outbound) {
847                                                 if (!outbound->cdr) 
848                                                         outbound->cdr = ast_cdr_alloc();
849                                                 if (outbound->cdr) {
850                                                         char tmp[256];
851
852                                                         ast_cdr_init(outbound->cdr, outbound);
853                                                         snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
854                                                         ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
855                                                         ast_cdr_update(outbound);
856                                                         ast_cdr_start(outbound->cdr);
857                                                         ast_cdr_end(outbound->cdr);
858                                                         /* If the cause wasn't handled properly */
859                                                         if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
860                                                                 ast_cdr_failed(outbound->cdr);
861                                                 } else {
862                                                         ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
863                                                         ast_hangup(outbound);
864                                                         outbound = NULL;
865                                                 }
866                                         }
867                                 }
868                         } else 
869                                 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
870
871                         number = rest;
872                 } while (number);
873
874                 status = 0;
875                 if (!AST_LIST_EMPTY(findme_user_list))
876                         winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
877
878                 while ((fmuser = AST_LIST_REMOVE_HEAD(findme_user_list, entry))) {
879                         if (!fmuser->cleared && fmuser->ochan != winner)
880                                 clear_caller(fmuser);
881                         ast_free(fmuser);
882                 }
883
884                 fmuser = NULL;
885                 tmpuser = NULL;
886                 headuser = NULL;
887                 if (winner)
888                         break;
889
890                 if (!caller) {
891                         tpargs->status = 1;
892                         ast_free(findme_user_list);
893                         return;
894                 }
895
896                 idx++;
897                 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
898                         if (nm->order == idx)
899                                 break;
900                 }
901         }
902         ast_free(findme_user_list);
903         if (!winner) 
904                 tpargs->status = 1;
905         else {
906                 tpargs->status = 100;
907                 tpargs->outbound = winner;
908         }
909
910         return;
911 }
912
913 static struct call_followme *find_realtime(const char *name)
914 {
915         struct ast_variable *var = ast_load_realtime("followme", "name", name, SENTINEL), *v;
916         struct ast_config *cfg;
917         const char *catg;
918         struct call_followme *new;
919         struct ast_str *str = ast_str_create(16);
920
921         if (!var) {
922                 return NULL;
923         }
924
925         if (!(new = alloc_profile(name))) {
926                 return NULL;
927         }
928
929         for (v = var; v; v = v->next) {
930                 if (!strcasecmp(v->name, "active")) {
931                         if (ast_false(v->value)) {
932                                 ast_mutex_destroy(&new->lock);
933                                 ast_free(new);
934                                 return NULL;
935                         }
936                 } else {
937                         profile_set_param(new, v->name, v->value, 0, 0);
938                 }
939         }
940
941         ast_variables_destroy(var);
942         new->realtime = 1;
943
944         /* Load numbers */
945         if (!(cfg = ast_load_realtime_multientry("followme_numbers", "ordinal LIKE", "%", "name", name, SENTINEL))) {
946                 ast_mutex_destroy(&new->lock);
947                 ast_free(new);
948                 return NULL;
949         }
950
951         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
952                 const char *numstr, *timeoutstr, *ordstr;
953                 int timeout;
954                 struct number *cur;
955                 if (!(numstr = ast_variable_retrieve(cfg, catg, "phonenumber"))) {
956                         continue;
957                 }
958                 if (!(timeoutstr = ast_variable_retrieve(cfg, catg, "timeout")) || sscanf(timeoutstr, "%d", &timeout) != 1 || timeout < 1) {
959                         timeout = 25;
960                 }
961                 /* This one has to exist; it was part of the query */
962                 ordstr = ast_variable_retrieve(cfg, catg, "ordinal");
963                 ast_str_make_space(&str, strlen(numstr) + 1);
964                 ast_copy_string(str->str, numstr, str->len);
965                 if ((cur = create_followme_number(str->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, void *data)
1001 {
1002         struct fm_args targs;
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         if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_STATUSMSG)) 
1078                 ast_stream_and_wait(chan, targs.statusprompt, "");
1079
1080         snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
1081         duration = 5;
1082
1083         if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_RECORDNAME)) 
1084                 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL) < 0)
1085                         goto outrun;
1086
1087         if (!ast_fileexists(namerecloc, NULL, chan->language))
1088                 ast_copy_string(namerecloc, "", sizeof(namerecloc));
1089
1090         if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1091                 goto outrun;
1092         if (ast_waitstream(chan, "") < 0)
1093                 goto outrun;
1094         ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1095
1096         targs.status = 0;
1097         targs.chan = chan;
1098         ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1099
1100         findmeexec(&targs);
1101
1102         while ((nm = AST_LIST_REMOVE_HEAD(&targs.cnumbers, entry)))
1103                 ast_free(nm);
1104
1105         if (!ast_strlen_zero(namerecloc))
1106                 unlink(namerecloc);
1107
1108         if (targs.status != 100) {
1109                 ast_moh_stop(chan);
1110                 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG)) 
1111                         ast_stream_and_wait(chan, targs.sorryprompt, "");
1112                 res = 0;
1113         } else {
1114                 caller = chan;
1115                 outbound = targs.outbound;
1116                 /* Bridge the two channels. */
1117
1118                 memset(&config, 0, sizeof(config));
1119                 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1120                 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1121                 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1122                 config.end_bridge_callback = end_bridge_callback;
1123                 config.end_bridge_callback_data = chan;
1124                 config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
1125
1126                 ast_moh_stop(caller);
1127                 /* Be sure no generators are left on it */
1128                 ast_deactivate_generator(caller);
1129                 /* Make sure channels are compatible */
1130                 res = ast_channel_make_compatible(caller, outbound);
1131                 if (res < 0) {
1132                         ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1133                         ast_hangup(outbound);
1134                         goto outrun;
1135                 }
1136                 res = ast_bridge_call(caller, outbound, &config);
1137                 if (outbound)
1138                         ast_hangup(outbound);
1139         }
1140
1141         outrun:
1142
1143         if (f->realtime) {
1144                 /* Not in list */
1145                 free_numbers(f);
1146                 ast_free(f);
1147         }
1148
1149         return res;
1150 }
1151
1152 static int unload_module(void)
1153 {
1154         struct call_followme *f;
1155
1156         ast_unregister_application(app);
1157
1158         /* Free Memory. Yeah! I'm free! */
1159         AST_RWLIST_WRLOCK(&followmes);
1160         while ((f = AST_RWLIST_REMOVE_HEAD(&followmes, entry))) {
1161                 free_numbers(f);
1162                 ast_free(f);
1163         }
1164
1165         AST_RWLIST_UNLOCK(&followmes);
1166
1167         return 0;
1168 }
1169
1170 static int load_module(void)
1171 {
1172         if(!reload_followme(0))
1173                 return AST_MODULE_LOAD_DECLINE;
1174
1175         return ast_register_application_xml(app, app_exec);
1176 }
1177
1178 static int reload(void)
1179 {
1180         reload_followme(1);
1181
1182         return 0;
1183 }
1184
1185 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1186                 .load = load_module,
1187                 .unload = unload_module,
1188                 .reload = reload,
1189                );