Merge "astobj2: Create function to copy weak proxied objects from container."
[asterisk/asterisk.git] / apps / app_followme.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
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  * \ingroup applications
27  */
28
29 /*! \li \ref app_followme.c uses the configuration file \ref followme.conf
30  * \addtogroup configuration_file Configuration Files
31  */
32
33 /*!
34  * \page followme.conf followme.conf
35  * \verbinclude followme.conf.sample
36  */
37
38 /*** MODULEINFO
39         <support_level>core</support_level>
40  ***/
41
42 #include "asterisk.h"
43
44 #include <signal.h>
45
46 #include "asterisk/paths.h"     /* use ast_config_AST_SPOOL_DIR */
47 #include "asterisk/lock.h"
48 #include "asterisk/file.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/module.h"
52 #include "asterisk/translate.h"
53 #include "asterisk/say.h"
54 #include "asterisk/features.h"
55 #include "asterisk/musiconhold.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/manager.h"
58 #include "asterisk/config.h"
59 #include "asterisk/utils.h"
60 #include "asterisk/causes.h"
61 #include "asterisk/astdb.h"
62 #include "asterisk/dsp.h"
63 #include "asterisk/app.h"
64 #include "asterisk/stasis_channels.h"
65 #include "asterisk/max_forwards.h"
66
67 #define REC_FORMAT "sln"
68
69 /*** DOCUMENTATION
70         <application name="FollowMe" language="en_US">
71                 <synopsis>
72                         Find-Me/Follow-Me application.
73                 </synopsis>
74                 <syntax>
75                         <parameter name="followmeid" required="true" />
76                         <parameter name="options">
77                                 <optionlist>
78                                         <option name="a">
79                                                 <para>Record the caller's name so it can be announced to the
80                                                 callee on each step.</para>
81                                         </option>
82                                         <option name="B" argsep="^">
83                                                 <para>Before initiating the outgoing call(s), Gosub to the specified
84                                                 location using the current channel.</para>
85                                                 <argument name="context" required="false" />
86                                                 <argument name="exten" required="false" />
87                                                 <argument name="priority" required="true" hasparams="optional" argsep="^">
88                                                         <argument name="arg1" multiple="true" required="true" />
89                                                         <argument name="argN" />
90                                                 </argument>
91                                         </option>
92                                         <option name="b" argsep="^">
93                                                 <para>Before initiating an outgoing call, Gosub to the specified
94                                                 location using the newly created channel.  The Gosub will be
95                                                 executed for each destination channel.</para>
96                                                 <argument name="context" required="false" />
97                                                 <argument name="exten" required="false" />
98                                                 <argument name="priority" required="true" hasparams="optional" argsep="^">
99                                                         <argument name="arg1" multiple="true" required="true" />
100                                                         <argument name="argN" />
101                                                 </argument>
102                                         </option>
103                                         <option name="d">
104                                                 <para>Disable the 'Please hold while we try to connect your call' announcement.</para>
105                                         </option>
106                                         <option name="I">
107                                                 <para>Asterisk will ignore any connected line update requests
108                                                 it may receive on this dial attempt.</para>
109                                         </option>
110                                         <option name="l">
111                                                 <para>Disable local call optimization so that applications with
112                                                 audio hooks between the local bridge don't get dropped when the
113                                                 calls get joined directly.</para>
114                                         </option>
115                                         <option name="N">
116                                                 <para>Don't answer the incoming call until we're ready to
117                                                 connect the caller or give up.</para>
118                                                 <note>
119                                                         <para>This option is ignored if the call is already answered.</para>
120                                                 </note>
121                                                 <note>
122                                                         <para>If the call is not already answered, the 'a' and 's'
123                                                         options are ignored while the 'd' option is implicitly enabled.</para>
124                                                 </note>
125                                         </option>
126                                         <option name="n">
127                                                 <para>Playback the unreachable status message if we've run out
128                                                 of steps or the callee has elected not to be reachable.</para>
129                                         </option>
130                                         <option name="s">
131                                                 <para>Playback the incoming status message prior to starting
132                                                 the follow-me step(s)</para>
133                                         </option>
134                                 </optionlist>
135                         </parameter>
136                 </syntax>
137                 <description>
138                         <para>This application performs Find-Me/Follow-Me functionality for the caller
139                         as defined in the profile matching the <replaceable>followmeid</replaceable> parameter in
140                         <filename>followme.conf</filename>. If the specified <replaceable>followmeid</replaceable>
141                         profile doesn't exist in <filename>followme.conf</filename>, execution will be returned
142                         to the dialplan and call execution will continue at the next priority.</para>
143                         <para>Returns -1 on hangup.</para>
144                 </description>
145         </application>
146  ***/
147
148 static char *app = "FollowMe";
149
150 /*! Maximum accept/decline DTMF string plus terminator. */
151 #define MAX_YN_STRING           20
152
153 /*! \brief Number structure */
154 struct number {
155         char number[512];       /*!< Phone Number(s) and/or Extension(s) */
156         long timeout;           /*!< Dial Timeout, if used. */
157         int order;              /*!< The order to dial in */
158         AST_LIST_ENTRY(number) entry; /*!< Next Number record */
159 };
160
161 /*! \brief Data structure for followme scripts */
162 struct call_followme {
163         ast_mutex_t lock;
164         char name[AST_MAX_EXTENSION];   /*!< Name - FollowMeID */
165         char moh[MAX_MUSICCLASS];       /*!< Music On Hold Class to be used */
166         char context[AST_MAX_CONTEXT];  /*!< Context to dial from */
167         unsigned int active;            /*!< Profile is active (1), or disabled (0). */
168         int realtime;           /*!< Cached from realtime */
169         /*! Allow callees to accept/reject the forwarded call */
170         unsigned int enable_callee_prompt:1;
171         char takecall[MAX_YN_STRING];   /*!< Digit mapping to take a call */
172         char nextindp[MAX_YN_STRING];   /*!< Digit mapping to decline a call */
173         char callfromprompt[PATH_MAX];  /*!< Sound prompt name and path */
174         char norecordingprompt[PATH_MAX];       /*!< Sound prompt name and path */
175         char optionsprompt[PATH_MAX];   /*!< Sound prompt name and path */
176         char plsholdprompt[PATH_MAX];   /*!< Sound prompt name and path */
177         char statusprompt[PATH_MAX];    /*!< Sound prompt name and path */
178         char sorryprompt[PATH_MAX];     /*!< Sound prompt name and path */
179         char connprompt[PATH_MAX];      /*!< Sound prompt name and path */
180
181         AST_LIST_HEAD_NOLOCK(numbers, number) numbers;     /*!< Head of the list of follow-me numbers */
182         AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
183         AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
184         AST_LIST_ENTRY(call_followme) entry;           /*!< Next Follow-Me record */
185 };
186
187 struct fm_args {
188         char *mohclass;
189         AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
190         /*! Gosub app arguments for outgoing calls.  NULL if not supplied. */
191         const char *predial_callee;
192         /*! Accumulated connected line information from inbound call. */
193         struct ast_party_connected_line connected_in;
194         /*! Accumulated connected line information from outbound call. */
195         struct ast_party_connected_line connected_out;
196         /*! TRUE if connected line information from inbound call changed. */
197         unsigned int pending_in_connected_update:1;
198         /*! TRUE if connected line information from outbound call is available. */
199         unsigned int pending_out_connected_update:1;
200         /*! TRUE if caller has a pending hold request for the winning call. */
201         unsigned int pending_hold:1;
202         /*! TRUE if callees will be prompted to answer */
203         unsigned int enable_callee_prompt:1;
204         /*! Music On Hold Class suggested by caller hold for winning call. */
205         char suggested_moh[MAX_MUSICCLASS];
206         char context[AST_MAX_CONTEXT];
207         char namerecloc[PATH_MAX];
208         char takecall[MAX_YN_STRING];   /*!< Digit mapping to take a call */
209         char nextindp[MAX_YN_STRING];   /*!< Digit mapping to decline a call */
210         char callfromprompt[PATH_MAX];  /*!< Sound prompt name and path */
211         char norecordingprompt[PATH_MAX];       /*!< Sound prompt name and path */
212         char optionsprompt[PATH_MAX];   /*!< Sound prompt name and path */
213         char plsholdprompt[PATH_MAX];   /*!< Sound prompt name and path */
214         char statusprompt[PATH_MAX];    /*!< Sound prompt name and path */
215         char sorryprompt[PATH_MAX];     /*!< Sound prompt name and path */
216         char connprompt[PATH_MAX];      /*!< Sound prompt name and path */
217         struct ast_flags followmeflags;
218 };
219
220 struct findme_user {
221         struct ast_channel *ochan;
222         /*! Accumulated connected line information from outgoing call. */
223         struct ast_party_connected_line connected;
224         long digts;
225         int ynidx;
226         int state;
227         char dialarg[256];
228         /*! Collected digits to accept/decline the call. */
229         char yn[MAX_YN_STRING];
230         /*! TRUE if the outgoing call is answered. */
231         unsigned int answered:1;
232         /*! TRUE if connected line information is available. */
233         unsigned int pending_connected_update:1;
234         AST_LIST_ENTRY(findme_user) entry;
235 };
236
237 enum {
238         FOLLOWMEFLAG_STATUSMSG = (1 << 0),
239         FOLLOWMEFLAG_RECORDNAME = (1 << 1),
240         FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2),
241         FOLLOWMEFLAG_DISABLEHOLDPROMPT = (1 << 3),
242         FOLLOWMEFLAG_NOANSWER = (1 << 4),
243         FOLLOWMEFLAG_DISABLEOPTIMIZATION = (1 << 5),
244         FOLLOWMEFLAG_IGNORE_CONNECTEDLINE = (1 << 6),
245         FOLLOWMEFLAG_PREDIAL_CALLER = (1 << 7),
246         FOLLOWMEFLAG_PREDIAL_CALLEE = (1 << 8),
247 };
248
249 enum {
250         FOLLOWMEFLAG_ARG_PREDIAL_CALLER,
251         FOLLOWMEFLAG_ARG_PREDIAL_CALLEE,
252
253         /* note: this entry _MUST_ be the last one in the enum */
254         FOLLOWMEFLAG_ARG_ARRAY_SIZE
255 };
256
257 AST_APP_OPTIONS(followme_opts, {
258         AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME),
259         AST_APP_OPTION_ARG('B', FOLLOWMEFLAG_PREDIAL_CALLER, FOLLOWMEFLAG_ARG_PREDIAL_CALLER),
260         AST_APP_OPTION_ARG('b', FOLLOWMEFLAG_PREDIAL_CALLEE, FOLLOWMEFLAG_ARG_PREDIAL_CALLEE),
261         AST_APP_OPTION('d', FOLLOWMEFLAG_DISABLEHOLDPROMPT),
262         AST_APP_OPTION('I', FOLLOWMEFLAG_IGNORE_CONNECTEDLINE),
263         AST_APP_OPTION('l', FOLLOWMEFLAG_DISABLEOPTIMIZATION),
264         AST_APP_OPTION('N', FOLLOWMEFLAG_NOANSWER),
265         AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG),
266         AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG),
267 });
268
269 static const char *featuredigittostr;
270 static int featuredigittimeout = 5000;          /*!< Feature Digit Timeout */
271 static const char *defaultmoh = "default";      /*!< Default Music-On-Hold Class */
272
273 static char takecall[MAX_YN_STRING] = "1";
274 static char nextindp[MAX_YN_STRING] = "2";
275 static int enable_callee_prompt = 1;
276 static char callfromprompt[PATH_MAX] = "followme/call-from";
277 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
278 static char optionsprompt[PATH_MAX] = "followme/options";
279 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
280 static char statusprompt[PATH_MAX] = "followme/status";
281 static char sorryprompt[PATH_MAX] = "followme/sorry";
282 static char connprompt[PATH_MAX] = "";
283
284
285 static AST_RWLIST_HEAD_STATIC(followmes, call_followme);
286 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
287
288 static void free_numbers(struct call_followme *f)
289 {
290         /* Free numbers attached to the profile */
291         struct number *prev;
292
293         while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
294                 /* Free the number */
295                 ast_free(prev);
296         AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
297
298         while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
299                 /* Free the blacklisted number */
300                 ast_free(prev);
301         AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
302
303         while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
304                 /* Free the whitelisted number */
305                 ast_free(prev);
306         AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
307 }
308
309
310 /*! \brief Allocate and initialize followme profile */
311 static struct call_followme *alloc_profile(const char *fmname)
312 {
313         struct call_followme *f;
314
315         if (!(f = ast_calloc(1, sizeof(*f))))
316                 return NULL;
317
318         ast_mutex_init(&f->lock);
319         ast_copy_string(f->name, fmname, sizeof(f->name));
320         AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
321         AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
322         AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
323         return f;
324 }
325
326 static void init_profile(struct call_followme *f, int activate)
327 {
328         f->enable_callee_prompt = enable_callee_prompt;
329         f->context[0] = '\0';
330         ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
331         ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
332         ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
333         ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
334         ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
335         ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
336         ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
337         ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
338         ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
339         ast_copy_string(f->connprompt, connprompt, sizeof(f->connprompt));
340         if (activate) {
341                 f->active = 1;
342         }
343 }
344
345
346
347 /*! \brief Set parameter in profile from configuration file */
348 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
349 {
350
351         if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
352                 ast_copy_string(f->moh, val, sizeof(f->moh));
353         else if (!strcasecmp(param, "context"))
354                 ast_copy_string(f->context, val, sizeof(f->context));
355         else if (!strcasecmp(param, "enable_callee_prompt"))
356                 f->enable_callee_prompt = ast_true(val);
357         else if (!strcasecmp(param, "takecall"))
358                 ast_copy_string(f->takecall, val, sizeof(f->takecall));
359         else if (!strcasecmp(param, "declinecall"))
360                 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
361         else if (!strcasecmp(param, "call-from-prompt") || !strcasecmp(param, "call_from_prompt"))
362                 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
363         else if (!strcasecmp(param, "followme-norecording-prompt") || !strcasecmp(param, "norecording_prompt"))
364                 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
365         else if (!strcasecmp(param, "followme-options-prompt") || !strcasecmp(param, "options_prompt"))
366                 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
367         else if (!strcasecmp(param, "followme-pls-hold-prompt") || !strcasecmp(param, "pls_hold_prompt"))
368                 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
369         else if (!strcasecmp(param, "followme-status-prompt") || !strcasecmp(param, "status_prompt"))
370                 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
371         else if (!strcasecmp(param, "followme-sorry-prompt") || !strcasecmp(param, "sorry_prompt"))
372                 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
373         else if (!strcasecmp(param, "followme-connecting-prompt") || !strcasecmp(param, "connecting_prompt")) {
374                 ast_copy_string(f->connprompt, val, sizeof(f->connprompt));
375         } else if (failunknown) {
376                 if (linenum >= 0)
377                         ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
378                 else
379                         ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
380         }
381 }
382
383 /*! \brief Add a new number */
384 static struct number *create_followme_number(const char *number, int timeout, int numorder)
385 {
386         struct number *cur;
387         char *buf = ast_strdupa(number);
388         char *tmp;
389
390         if (!(cur = ast_calloc(1, sizeof(*cur))))
391                 return NULL;
392
393         cur->timeout = timeout;
394         if ((tmp = strchr(buf, ',')))
395                 *tmp = '\0';
396         ast_copy_string(cur->number, buf, sizeof(cur->number));
397         cur->order = numorder;
398         ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
399
400         return cur;
401 }
402
403 /*! \brief Reload followme application module */
404 static int reload_followme(int reload)
405 {
406         struct call_followme *f;
407         struct ast_config *cfg;
408         char *cat = NULL, *tmp;
409         struct ast_variable *var;
410         struct number *cur, *nm;
411         char *numberstr;
412         int timeout;
413         int numorder;
414         const char* enable_callee_prompt_str;
415         const char *takecallstr;
416         const char *declinecallstr;
417         const char *tmpstr;
418         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
419
420         if (!(cfg = ast_config_load("followme.conf", config_flags))) {
421                 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
422                 return 0;
423         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
424                 return 0;
425         } else if (cfg == CONFIG_STATUS_FILEINVALID) {
426                 ast_log(LOG_ERROR, "Config file followme.conf is in an invalid format.  Aborting.\n");
427                 return 0;
428         }
429
430         AST_RWLIST_WRLOCK(&followmes);
431
432         /* Reset Global Var Values */
433         featuredigittimeout = 5000;
434
435         /* Mark all profiles as inactive for the moment */
436         AST_RWLIST_TRAVERSE(&followmes, f, entry) {
437                 f->active = 0;
438         }
439
440         featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
441
442         if (!ast_strlen_zero(featuredigittostr)) {
443                 if (!sscanf(featuredigittostr, "%30d", &featuredigittimeout))
444                         featuredigittimeout = 5000;
445         }
446
447         if ((enable_callee_prompt_str = ast_variable_retrieve(cfg, "general",
448                                         "enable_callee_prompt")) &&
449                         !ast_strlen_zero(enable_callee_prompt_str)) {
450                 enable_callee_prompt = ast_true(enable_callee_prompt_str);
451         }
452
453         if ((takecallstr = ast_variable_retrieve(cfg, "general", "takecall")) && !ast_strlen_zero(takecallstr)) {
454                 ast_copy_string(takecall, takecallstr, sizeof(takecall));
455         }
456
457         if ((declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall")) && !ast_strlen_zero(declinecallstr)) {
458                 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
459         }
460
461         if ((tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt")) && !ast_strlen_zero(tmpstr)) {
462                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
463         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "call_from_prompt")) && !ast_strlen_zero(tmpstr)) {
464                 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
465         }
466
467         if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt")) && !ast_strlen_zero(tmpstr)) {
468                 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
469         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording_prompt")) && !ast_strlen_zero(tmpstr)) {
470                 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
471         }
472
473
474         if ((tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt")) && !ast_strlen_zero(tmpstr)) {
475                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
476         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "options_prompt")) && !ast_strlen_zero(tmpstr)) {
477                 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
478         }
479
480         if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt")) && !ast_strlen_zero(tmpstr)) {
481                 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
482         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls_hold_prompt")) && !ast_strlen_zero(tmpstr)) {
483                 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
484         }
485
486         if ((tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt")) && !ast_strlen_zero(tmpstr)) {
487                 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
488         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "status_prompt")) && !ast_strlen_zero(tmpstr)) {
489                 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
490         }
491
492         if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt")) && !ast_strlen_zero(tmpstr)) {
493                 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
494         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry_prompt")) && !ast_strlen_zero(tmpstr)) {
495                 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
496         }
497
498         if ((tmpstr = ast_variable_retrieve(cfg, "general", "connecting-prompt")) && !ast_strlen_zero(tmpstr)) {
499                 ast_copy_string(connprompt, tmpstr, sizeof(connprompt));
500         } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "connecting_prompt")) && !ast_strlen_zero(tmpstr)) {
501                 ast_copy_string(connprompt, tmpstr, sizeof(connprompt));
502         }
503
504
505         /* Chug through config file */
506         while ((cat = ast_category_browse(cfg, cat))) {
507                 int new = 0;
508
509                 if (!strcasecmp(cat, "general"))
510                         continue;
511
512                 /* Look for an existing one */
513                 AST_LIST_TRAVERSE(&followmes, f, entry) {
514                         if (!strcasecmp(f->name, cat))
515                                 break;
516                 }
517
518                 ast_debug(1, "New profile %s.\n", cat);
519
520                 if (!f) {
521                         /* Make one then */
522                         f = alloc_profile(cat);
523                         new = 1;
524                 }
525
526                 /* Totally fail if we fail to find/create an entry */
527                 if (!f)
528                         continue;
529
530                 if (!new)
531                         ast_mutex_lock(&f->lock);
532                 /* Re-initialize the profile */
533                 init_profile(f, 1);
534                 free_numbers(f);
535                 var = ast_variable_browse(cfg, cat);
536                 while (var) {
537                         if (!strcasecmp(var->name, "number")) {
538                                 int idx = 0;
539
540                                 /* Add a new number */
541                                 numberstr = ast_strdupa(var->value);
542                                 if ((tmp = strchr(numberstr, ','))) {
543                                         *tmp++ = '\0';
544                                         timeout = atoi(tmp);
545                                         if (timeout < 0) {
546                                                 timeout = 25;
547                                         }
548                                         if ((tmp = strchr(tmp, ','))) {
549                                                 *tmp++ = '\0';
550                                                 numorder = atoi(tmp);
551                                                 if (numorder < 0)
552                                                         numorder = 0;
553                                         } else
554                                                 numorder = 0;
555                                 } else {
556                                         timeout = 25;
557                                         numorder = 0;
558                                 }
559
560                                 if (!numorder) {
561                                         idx = 1;
562                                         AST_LIST_TRAVERSE(&f->numbers, nm, entry)
563                                                 idx++;
564                                         numorder = idx;
565                                 }
566                                 cur = create_followme_number(numberstr, timeout, numorder);
567                                 if (cur) {
568                                         AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
569                                 }
570                         } else {
571                                 profile_set_param(f, var->name, var->value, var->lineno, 1);
572                                 ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
573                         }
574                         var = var->next;
575                 } /* End while(var) loop */
576
577                 if (!new)
578                         ast_mutex_unlock(&f->lock);
579                 else
580                         AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
581         }
582
583         ast_config_destroy(cfg);
584
585         AST_RWLIST_UNLOCK(&followmes);
586
587         return 1;
588 }
589
590 static void publish_dial_end_event(struct ast_channel *in, struct findme_user_listptr *findme_user_list, struct ast_channel *exception, const char *status)
591 {
592         struct findme_user *tmpuser;
593
594         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
595                 if (tmpuser->ochan && tmpuser->ochan != exception) {
596                         ast_channel_publish_dial(in, tmpuser->ochan, NULL, status);
597                 }
598         }
599 }
600
601 static void clear_caller(struct findme_user *tmpuser)
602 {
603         struct ast_channel *outbound;
604
605         if (!tmpuser->ochan) {
606                 /* Call already cleared. */
607                 return;
608         }
609
610         outbound = tmpuser->ochan;
611         ast_hangup(outbound);
612         tmpuser->ochan = NULL;
613 }
614
615 static void clear_unanswered_calls(struct findme_user_listptr *findme_user_list)
616 {
617         struct findme_user *tmpuser;
618
619         AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
620                 if (!tmpuser->answered) {
621                         clear_caller(tmpuser);
622                 }
623         }
624 }
625
626 static void destroy_calling_node(struct findme_user *node)
627 {
628         clear_caller(node);
629         ast_party_connected_line_free(&node->connected);
630         ast_free(node);
631 }
632
633 static void destroy_calling_tree(struct findme_user_listptr *findme_user_list)
634 {
635         struct findme_user *fmuser;
636
637         while ((fmuser = AST_LIST_REMOVE_HEAD(findme_user_list, entry))) {
638                 destroy_calling_node(fmuser);
639         }
640 }
641
642 static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, struct fm_args *tpargs)
643 {
644         struct ast_party_connected_line connected;
645         struct ast_channel *watchers[256];
646         int pos;
647         struct ast_channel *winner;
648         struct ast_frame *f;
649         struct findme_user *tmpuser;
650         int to = 0;
651         int livechannels;
652         int tmpto;
653         long totalwait = 0, wtd = 0, towas = 0;
654         char *callfromname;
655         char *pressbuttonname;
656
657         /* ------------ wait_for_winner_channel start --------------- */
658
659         callfromname = ast_strdupa(tpargs->callfromprompt);
660         pressbuttonname = ast_strdupa(tpargs->optionsprompt);
661
662         totalwait = nm->timeout * 1000;
663
664         for (;;) {
665                 to = 1000;
666                 pos = 1;
667                 livechannels = 0;
668                 watchers[0] = caller;
669
670                 winner = NULL;
671                 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
672                         if (!tmpuser->ochan) {
673                                 continue;
674                         }
675                         if (tmpuser->state == 3) {
676                                 tmpuser->digts += (towas - wtd);
677                         }
678                         if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
679                                 ast_verb(3, "<%s> We've been waiting for digits longer than we should have.\n",
680                                         ast_channel_name(tmpuser->ochan));
681                                 if (tpargs->enable_callee_prompt) {
682                                         if (!ast_strlen_zero(tpargs->namerecloc)) {
683                                                 tmpuser->state = 1;
684                                                 tmpuser->digts = 0;
685                                                 if (!ast_streamfile(tmpuser->ochan, callfromname, ast_channel_language(tmpuser->ochan))) {
686                                                         ast_sched_runq(ast_channel_sched(tmpuser->ochan));
687                                                 } else {
688                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
689                                                         clear_caller(tmpuser);
690                                                         continue;
691                                                 }
692                                         } else {
693                                                 tmpuser->state = 2;
694                                                 tmpuser->digts = 0;
695                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
696                                                         ast_sched_runq(ast_channel_sched(tmpuser->ochan));
697                                                 else {
698                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
699                                                         clear_caller(tmpuser);
700                                                         continue;
701                                                 }
702                                         }
703                                 } else {
704                                         tmpuser->state = 3;
705                                 }
706                         }
707                         if (ast_channel_stream(tmpuser->ochan)) {
708                                 ast_sched_runq(ast_channel_sched(tmpuser->ochan));
709                                 tmpto = ast_sched_wait(ast_channel_sched(tmpuser->ochan));
710                                 if (tmpto > 0 && tmpto < to)
711                                         to = tmpto;
712                                 else if (tmpto < 0 && !ast_channel_timingfunc(tmpuser->ochan)) {
713                                         ast_stopstream(tmpuser->ochan);
714                                         switch (tmpuser->state) {
715                                         case 1:
716                                                 ast_verb(3, "<%s> Playback of the call-from file appears to be done.\n",
717                                                         ast_channel_name(tmpuser->ochan));
718                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->namerecloc, ast_channel_language(tmpuser->ochan))) {
719                                                         tmpuser->state = 2;
720                                                 } else {
721                                                         ast_log(LOG_NOTICE, "<%s> Unable to playback %s. Maybe the caller didn't record their name?\n",
722                                                                 ast_channel_name(tmpuser->ochan), tpargs->namerecloc);
723                                                         memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
724                                                         tmpuser->ynidx = 0;
725                                                         if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan)))
726                                                                 tmpuser->state = 3;
727                                                         else {
728                                                                 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
729                                                                 clear_caller(tmpuser);
730                                                                 continue;
731                                                         }
732                                                 }
733                                                 break;
734                                         case 2:
735                                                 ast_verb(3, "<%s> Playback of name file appears to be done.\n",
736                                                         ast_channel_name(tmpuser->ochan));
737                                                 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
738                                                 tmpuser->ynidx = 0;
739                                                 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan))) {
740                                                         tmpuser->state = 3;
741                                                 } else {
742                                                         clear_caller(tmpuser);
743                                                         continue;
744                                                 }
745                                                 break;
746                                         case 3:
747                                                 ast_verb(3, "<%s> Playback of the next step file appears to be done.\n",
748                                                         ast_channel_name(tmpuser->ochan));
749                                                 tmpuser->digts = 0;
750                                                 break;
751                                         default:
752                                                 break;
753                                         }
754                                 }
755                         }
756                         watchers[pos++] = tmpuser->ochan;
757                         livechannels++;
758                 }
759                 if (!livechannels) {
760                         ast_verb(3, "No live channels left for this step.\n");
761                         return NULL;
762                 }
763
764                 tmpto = to;
765                 if (to < 0) {
766                         to = 1000;
767                         tmpto = 1000;
768                 }
769                 towas = to;
770                 winner = ast_waitfor_n(watchers, pos, &to);
771                 tmpto -= to;
772                 totalwait -= tmpto;
773                 wtd = to;
774                 if (totalwait <= 0) {
775                         ast_verb(3, "We've hit our timeout for this step. Dropping unanswered calls and starting the next step.\n");
776                         clear_unanswered_calls(findme_user_list);
777                         return NULL;
778                 }
779                 if (winner) {
780                         /* Need to find out which channel this is */
781                         if (winner != caller) {
782                                 /* The winner is an outgoing channel. */
783                                 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
784                                         if (tmpuser->ochan == winner) {
785                                                 break;
786                                         }
787                                 }
788                         } else {
789                                 tmpuser = NULL;
790                         }
791
792                         f = ast_read(winner);
793                         if (f) {
794                                 if (f->frametype == AST_FRAME_CONTROL) {
795                                         switch (f->subclass.integer) {
796                                         case AST_CONTROL_HANGUP:
797                                                 ast_verb(3, "%s received a hangup frame.\n", ast_channel_name(winner));
798                                                 if (f->data.uint32) {
799                                                         ast_channel_hangupcause_set(winner, f->data.uint32);
800                                                 }
801                                                 if (!tmpuser) {
802                                                         ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
803                                                         publish_dial_end_event(caller, findme_user_list, NULL, "CANCEL");
804                                                         ast_frfree(f);
805                                                         return NULL;
806                                                 }
807                                                 clear_caller(tmpuser);
808                                                 break;
809                                         case AST_CONTROL_ANSWER:
810                                                 if (!tmpuser) {
811                                                         /* The caller answered?  We want an outgoing channel to answer. */
812                                                         break;
813                                                 }
814                                                 ast_verb(3, "%s answered %s\n", ast_channel_name(winner), ast_channel_name(caller));
815                                                 ast_channel_publish_dial(caller, winner, NULL, "ANSWER");
816                                                 publish_dial_end_event(caller, findme_user_list, winner, "CANCEL");
817                                                 tmpuser->answered = 1;
818                                                 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
819                                                 ast_channel_hangupcause_set(winner, AST_CAUSE_NORMAL_CLEARING);
820                                                 ast_channel_hangupcause_set(caller, AST_CAUSE_NORMAL_CLEARING);
821                                                 if (tpargs->enable_callee_prompt) {
822                                                         ast_verb(3, "Starting playback of %s\n", callfromname);
823                                                         if (!ast_strlen_zero(tpargs->namerecloc)) {
824                                                                 if (!ast_streamfile(winner, callfromname, ast_channel_language(winner))) {
825                                                                         ast_sched_runq(ast_channel_sched(winner));
826                                                                         tmpuser->state = 1;
827                                                                 } else {
828                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
829                                                                         clear_caller(tmpuser);
830                                                                 }
831                                                         } else {
832                                                                 tmpuser->state = 2;
833                                                                 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
834                                                                         ast_sched_runq(ast_channel_sched(tmpuser->ochan));
835                                                                 else {
836                                                                         ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
837                                                                         clear_caller(tmpuser);
838                                                                 }
839                                                         }
840                                                 } else {
841                                                         ast_verb(3, "Skip playback of caller name / norecording\n");
842                                                         tmpuser->state = 2;
843                                                 }
844                                                 break;
845                                         case AST_CONTROL_BUSY:
846                                                 ast_verb(3, "%s is busy\n", ast_channel_name(winner));
847                                                 if (tmpuser) {
848                                                         /* Outbound call was busy.  Drop it. */
849                                                         ast_channel_publish_dial(caller, winner, NULL, "BUSY");
850                                                         clear_caller(tmpuser);
851                                                 }
852                                                 break;
853                                         case AST_CONTROL_CONGESTION:
854                                                 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(winner));
855                                                 if (tmpuser) {
856                                                         /* Outbound call was congested.  Drop it. */
857                                                         ast_channel_publish_dial(caller, winner, NULL, "CONGESTION");
858                                                         clear_caller(tmpuser);
859                                                 }
860                                                 break;
861                                         case AST_CONTROL_RINGING:
862                                                 ast_verb(3, "%s is ringing\n", ast_channel_name(winner));
863                                                 ast_channel_publish_dial(caller, winner, NULL, "RINGING");
864                                                 break;
865                                         case AST_CONTROL_PROGRESS:
866                                                 ast_verb(3, "%s is making progress\n", ast_channel_name(winner));
867                                                 ast_channel_publish_dial(caller, winner, NULL, "PROGRESS");
868                                                 break;
869                                         case AST_CONTROL_VIDUPDATE:
870                                                 ast_verb(3, "%s requested a video update\n", ast_channel_name(winner));
871                                                 break;
872                                         case AST_CONTROL_SRCUPDATE:
873                                                 ast_verb(3, "%s requested a source update\n", ast_channel_name(winner));
874                                                 break;
875                                         case AST_CONTROL_PROCEEDING:
876                                                 ast_verb(3, "%s is proceeding\n", ast_channel_name(winner));
877                                                 ast_channel_publish_dial(caller, winner, NULL, "PROCEEDING");
878                                                 break;
879                                         case AST_CONTROL_HOLD:
880                                                 ast_verb(3, "%s placed call on hold\n", ast_channel_name(winner));
881                                                 if (!tmpuser) {
882                                                         /* Caller placed outgoing calls on hold. */
883                                                         tpargs->pending_hold = 1;
884                                                         if (f->data.ptr) {
885                                                                 ast_copy_string(tpargs->suggested_moh, f->data.ptr,
886                                                                         sizeof(tpargs->suggested_moh));
887                                                         } else {
888                                                                 tpargs->suggested_moh[0] = '\0';
889                                                         }
890                                                 } else {
891                                                         /*
892                                                          * Outgoing call placed caller on hold.
893                                                          *
894                                                          * Ignore because the outgoing call should not be able to place
895                                                          * the caller on hold until after they are bridged.
896                                                          */
897                                                 }
898                                                 break;
899                                         case AST_CONTROL_UNHOLD:
900                                                 ast_verb(3, "%s removed call from hold\n", ast_channel_name(winner));
901                                                 if (!tmpuser) {
902                                                         /* Caller removed outgoing calls from hold. */
903                                                         tpargs->pending_hold = 0;
904                                                 } else {
905                                                         /*
906                                                          * Outgoing call removed caller from hold.
907                                                          *
908                                                          * Ignore because the outgoing call should not be able to place
909                                                          * the caller on hold until after they are bridged.
910                                                          */
911                                                 }
912                                                 break;
913                                         case AST_CONTROL_OFFHOOK:
914                                         case AST_CONTROL_FLASH:
915                                                 /* Ignore going off hook and flash */
916                                                 break;
917                                         case AST_CONTROL_CONNECTED_LINE:
918                                                 if (ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_IGNORE_CONNECTEDLINE)) {
919                                                         ast_verb(3, "Connected line update from %s prevented.\n",
920                                                                 ast_channel_name(winner));
921                                                         break;
922                                                 }
923                                                 if (!tmpuser) {
924                                                         /*
925                                                          * Hold connected line update from caller until we have a
926                                                          * winner.
927                                                          */
928                                                         ast_verb(3,
929                                                                 "%s connected line has changed. Saving it until we have a winner.\n",
930                                                                 ast_channel_name(winner));
931                                                         ast_party_connected_line_set_init(&connected, &tpargs->connected_in);
932                                                         if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
933                                                                 ast_party_connected_line_set(&tpargs->connected_in,
934                                                                         &connected, NULL);
935                                                                 tpargs->pending_in_connected_update = 1;
936                                                         }
937                                                         ast_party_connected_line_free(&connected);
938                                                 } else {
939                                                         ast_verb(3,
940                                                                 "%s connected line has changed. Saving it until answer.\n",
941                                                                 ast_channel_name(winner));
942                                                         ast_party_connected_line_set_init(&connected, &tmpuser->connected);
943                                                         if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
944                                                                 ast_party_connected_line_set(&tmpuser->connected,
945                                                                         &connected, NULL);
946                                                                 tmpuser->pending_connected_update = 1;
947                                                         }
948                                                         ast_party_connected_line_free(&connected);
949                                                 }
950                                                 break;
951                                         case AST_CONTROL_REDIRECTING:
952                                                 /*
953                                                  * Ignore because we are masking the FollowMe search progress to
954                                                  * the caller.
955                                                  */
956                                                 break;
957                                         case AST_CONTROL_PVT_CAUSE_CODE:
958                                                 ast_indicate_data(caller, f->subclass.integer, f->data.ptr, f->datalen);
959                                                 break;
960                                         case -1:
961                                                 ast_verb(3, "%s stopped sounds\n", ast_channel_name(winner));
962                                                 break;
963                                         default:
964                                                 ast_debug(1, "Dunno what to do with control type %d from %s\n",
965                                                         f->subclass.integer, ast_channel_name(winner));
966                                                 break;
967                                         }
968                                 }
969                                 if (!tpargs->enable_callee_prompt && tmpuser) {
970                                         ast_debug(1, "Taking call with no prompt\n");
971                                         ast_frfree(f);
972                                         return tmpuser->ochan;
973                                 }
974                                 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
975                                         int cmp_len;
976
977                                         if (ast_channel_stream(winner))
978                                                 ast_stopstream(winner);
979                                         tmpuser->digts = 0;
980                                         ast_debug(1, "DTMF received: %c\n", (char) f->subclass.integer);
981                                         if (tmpuser->ynidx < ARRAY_LEN(tmpuser->yn) - 1) {
982                                                 tmpuser->yn[tmpuser->ynidx++] = f->subclass.integer;
983                                         } else {
984                                                 /* Discard oldest digit. */
985                                                 memmove(tmpuser->yn, tmpuser->yn + 1,
986                                                         sizeof(tmpuser->yn) - 2 * sizeof(tmpuser->yn[0]));
987                                                 tmpuser->yn[ARRAY_LEN(tmpuser->yn) - 2] = f->subclass.integer;
988                                         }
989                                         ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
990                                         cmp_len = strlen(tpargs->takecall);
991                                         if (cmp_len <= tmpuser->ynidx
992                                                 && !strcmp(tmpuser->yn + (tmpuser->ynidx - cmp_len), tpargs->takecall)) {
993                                                 ast_debug(1, "Match to take the call!\n");
994                                                 ast_frfree(f);
995                                                 return tmpuser->ochan;
996                                         }
997                                         cmp_len = strlen(tpargs->nextindp);
998                                         if (cmp_len <= tmpuser->ynidx
999                                                 && !strcmp(tmpuser->yn + (tmpuser->ynidx - cmp_len), tpargs->nextindp)) {
1000                                                 ast_debug(1, "Declined to take the call.\n");
1001                                                 clear_caller(tmpuser);
1002                                         }
1003                                 }
1004
1005                                 ast_frfree(f);
1006                         } else {
1007                                 ast_debug(1, "we didn't get a frame. hanging up.\n");
1008                                 if (!tmpuser) {
1009                                         /* Caller hung up. */
1010                                         ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
1011                                         return NULL;
1012                                 }
1013                                 /* Outgoing channel hung up. */
1014                                 ast_channel_publish_dial(caller, winner, NULL, "NOANSWER");
1015                                 clear_caller(tmpuser);
1016                         }
1017                 } else {
1018                         ast_debug(1, "timed out waiting for action\n");
1019                 }
1020         }
1021
1022         /* Unreachable. */
1023 }
1024
1025 /*!
1026  * \internal
1027  * \brief Find an extension willing to take the call.
1028  *
1029  * \param tpargs Active Followme config.
1030  * \param caller Channel initiating the outgoing calls.
1031  *
1032  * \retval winner Winning outgoing call.
1033  * \retval NULL if could not find someone to take the call.
1034  */
1035 static struct ast_channel *findmeexec(struct fm_args *tpargs, struct ast_channel *caller)
1036 {
1037         struct number *nm;
1038         struct ast_channel *winner = NULL;
1039         char num[512];
1040         int dg, idx;
1041         char *rest, *number;
1042         struct findme_user *tmpuser;
1043         struct findme_user *fmuser;
1044         struct findme_user_listptr findme_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
1045         struct findme_user_listptr new_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
1046
1047         for (idx = 1; !ast_check_hangup(caller); ++idx) {
1048                 /* Find next followme numbers to dial. */
1049                 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
1050                         if (nm->order == idx) {
1051                                 break;
1052                         }
1053                 }
1054                 if (!nm) {
1055                         ast_verb(3, "No more steps left.\n");
1056                         break;
1057                 }
1058
1059                 ast_debug(2, "Number(s) %s timeout %ld\n", nm->number, nm->timeout);
1060
1061                 /*
1062                  * Put all active outgoing channels into autoservice.
1063                  *
1064                  * This needs to be done because ast_exists_extension() may put
1065                  * the caller into autoservice.
1066                  */
1067                 AST_LIST_TRAVERSE(&findme_user_list, tmpuser, entry) {
1068                         if (tmpuser->ochan) {
1069                                 ast_autoservice_start(tmpuser->ochan);
1070                         }
1071                 }
1072
1073                 /* Create all new outgoing calls */
1074                 ast_copy_string(num, nm->number, sizeof(num));
1075                 for (number = num; number; number = rest) {
1076                         struct ast_channel *outbound;
1077
1078                         rest = strchr(number, '&');
1079                         if (rest) {
1080                                 *rest++ = 0;
1081                         }
1082
1083                         /* We check if the extension exists, before creating the ast_channel struct */
1084                         if (!ast_exists_extension(caller, tpargs->context, number, 1, S_COR(ast_channel_caller(caller)->id.number.valid, ast_channel_caller(caller)->id.number.str, NULL))) {
1085                                 ast_log(LOG_ERROR, "Extension '%s@%s' doesn't exist\n", number, tpargs->context);
1086                                 continue;
1087                         }
1088
1089                         tmpuser = ast_calloc(1, sizeof(*tmpuser));
1090                         if (!tmpuser) {
1091                                 continue;
1092                         }
1093
1094                         if (ast_strlen_zero(tpargs->context)) {
1095                                 snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s%s",
1096                                         number,
1097                                         ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
1098                                                 ? "/n" : "/m");
1099                         } else {
1100                                 snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s@%s%s",
1101                                         number, tpargs->context,
1102                                         ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
1103                                                 ? "/n" : "/m");
1104                         }
1105
1106                         outbound = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller,
1107                                 tmpuser->dialarg, &dg);
1108                         if (!outbound) {
1109                                 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n",
1110                                         tmpuser->dialarg, ast_cause2str(dg));
1111                                 ast_free(tmpuser);
1112                                 continue;
1113                         }
1114
1115                         ast_channel_lock_both(caller, outbound);
1116                         ast_connected_line_copy_from_caller(ast_channel_connected(outbound), ast_channel_caller(caller));
1117                         ast_channel_inherit_variables(caller, outbound);
1118                         ast_channel_datastore_inherit(caller, outbound);
1119                         ast_max_forwards_decrement(outbound);
1120                         ast_channel_language_set(outbound, ast_channel_language(caller));
1121                         ast_channel_req_accountcodes(outbound, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
1122                         ast_channel_musicclass_set(outbound, ast_channel_musicclass(caller));
1123                         ast_channel_unlock(outbound);
1124                         ast_channel_unlock(caller);
1125
1126                         tmpuser->ochan = outbound;
1127                         tmpuser->state = 0;
1128                         AST_LIST_INSERT_TAIL(&new_user_list, tmpuser, entry);
1129                 }
1130
1131                 /*
1132                  * PREDIAL: Run gosub on all of the new callee channels
1133                  *
1134                  * We run the callee predial before ast_call() in case the user
1135                  * wishes to do something on the newly created channels before
1136                  * the channel does anything important.
1137                  */
1138                 if (tpargs->predial_callee && !AST_LIST_EMPTY(&new_user_list)) {
1139                         /* Put caller into autoservice. */
1140                         ast_autoservice_start(caller);
1141
1142                         /* Run predial on all new outgoing calls. */
1143                         AST_LIST_TRAVERSE(&new_user_list, tmpuser, entry) {
1144                                 ast_pre_call(tmpuser->ochan, tpargs->predial_callee);
1145                         }
1146
1147                         /* Take caller out of autoservice. */
1148                         if (ast_autoservice_stop(caller)) {
1149                                 /*
1150                                  * Caller hungup.
1151                                  *
1152                                  * Destoy all new outgoing calls.
1153                                  */
1154                                 while ((tmpuser = AST_LIST_REMOVE_HEAD(&new_user_list, entry))) {
1155                                         destroy_calling_node(tmpuser);
1156                                 }
1157
1158                                 /* Take all active outgoing channels out of autoservice. */
1159                                 AST_LIST_TRAVERSE(&findme_user_list, tmpuser, entry) {
1160                                         if (tmpuser->ochan) {
1161                                                 ast_autoservice_stop(tmpuser->ochan);
1162                                         }
1163                                 }
1164                                 break;
1165                         }
1166                 }
1167
1168                 /* Start all new outgoing calls */
1169                 AST_LIST_TRAVERSE_SAFE_BEGIN(&new_user_list, tmpuser, entry) {
1170                         ast_verb(3, "calling Local/%s\n", tmpuser->dialarg);
1171                         if (ast_call(tmpuser->ochan, tmpuser->dialarg, 0)) {
1172                                 ast_verb(3, "couldn't reach at this number.\n");
1173                                 AST_LIST_REMOVE_CURRENT(entry);
1174
1175                                 /* Destroy this failed new outgoing call. */
1176                                 destroy_calling_node(tmpuser);
1177                                 continue;
1178                         }
1179
1180                         ast_channel_publish_dial(caller, tmpuser->ochan, tmpuser->dialarg, NULL);
1181                 }
1182                 AST_LIST_TRAVERSE_SAFE_END;
1183
1184                 /* Take all active outgoing channels out of autoservice. */
1185                 AST_LIST_TRAVERSE_SAFE_BEGIN(&findme_user_list, tmpuser, entry) {
1186                         if (tmpuser->ochan && ast_autoservice_stop(tmpuser->ochan)) {
1187                                 /* Existing outgoing call hungup. */
1188                                 AST_LIST_REMOVE_CURRENT(entry);
1189                                 destroy_calling_node(tmpuser);
1190                         }
1191                 }
1192                 AST_LIST_TRAVERSE_SAFE_END;
1193
1194                 if (AST_LIST_EMPTY(&new_user_list)) {
1195                         /* No new channels remain at this order level.  If there were any at all. */
1196                         continue;
1197                 }
1198
1199                 /* Add new outgoing channels to the findme list. */
1200                 AST_LIST_APPEND_LIST(&findme_user_list, &new_user_list, entry);
1201
1202                 winner = wait_for_winner(&findme_user_list, nm, caller, tpargs);
1203                 if (!winner) {
1204                         /* Remove all dead outgoing nodes. */
1205                         AST_LIST_TRAVERSE_SAFE_BEGIN(&findme_user_list, tmpuser, entry) {
1206                                 if (!tmpuser->ochan) {
1207                                         AST_LIST_REMOVE_CURRENT(entry);
1208                                         destroy_calling_node(tmpuser);
1209                                 }
1210                         }
1211                         AST_LIST_TRAVERSE_SAFE_END;
1212                         continue;
1213                 }
1214
1215                 /* Destroy losing calls up to the winner.  The rest will be destroyed later. */
1216                 while ((fmuser = AST_LIST_REMOVE_HEAD(&findme_user_list, entry))) {
1217                         if (fmuser->ochan == winner) {
1218                                 /*
1219                                  * Pass any connected line info up.
1220                                  *
1221                                  * NOTE: This code must be in line with destroy_calling_node().
1222                                  */
1223                                 tpargs->connected_out = fmuser->connected;
1224                                 tpargs->pending_out_connected_update = fmuser->pending_connected_update;
1225                                 ast_free(fmuser);
1226                                 break;
1227                         } else {
1228                                 /* Destroy losing call. */
1229                                 destroy_calling_node(fmuser);
1230                         }
1231                 }
1232                 break;
1233         }
1234         destroy_calling_tree(&findme_user_list);
1235         return winner;
1236 }
1237
1238 static struct call_followme *find_realtime(const char *name)
1239 {
1240         struct ast_variable *var;
1241         struct ast_variable *v;
1242         struct ast_config *cfg;
1243         const char *catg;
1244         struct call_followme *new_follower;
1245         struct ast_str *str;
1246
1247         str = ast_str_create(16);
1248         if (!str) {
1249                 return NULL;
1250         }
1251
1252         var = ast_load_realtime("followme", "name", name, SENTINEL);
1253         if (!var) {
1254                 ast_free(str);
1255                 return NULL;
1256         }
1257
1258         if (!(new_follower = alloc_profile(name))) {
1259                 ast_variables_destroy(var);
1260                 ast_free(str);
1261                 return NULL;
1262         }
1263         init_profile(new_follower, 0);
1264
1265         for (v = var; v; v = v->next) {
1266                 if (!strcasecmp(v->name, "active")) {
1267                         if (ast_false(v->value)) {
1268                                 ast_mutex_destroy(&new_follower->lock);
1269                                 ast_free(new_follower);
1270                                 ast_variables_destroy(var);
1271                                 ast_free(str);
1272                                 return NULL;
1273                         }
1274                 } else {
1275                         profile_set_param(new_follower, v->name, v->value, 0, 0);
1276                 }
1277         }
1278
1279         ast_variables_destroy(var);
1280         new_follower->realtime = 1;
1281
1282         /* Load numbers */
1283         cfg = ast_load_realtime_multientry("followme_numbers", "ordinal LIKE", "%", "name",
1284                 name, SENTINEL);
1285         if (!cfg) {
1286                 ast_mutex_destroy(&new_follower->lock);
1287                 ast_free(new_follower);
1288                 ast_free(str);
1289                 return NULL;
1290         }
1291
1292         for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
1293                 const char *numstr;
1294                 const char *timeoutstr;
1295                 const char *ordstr;
1296                 int timeout;
1297                 struct number *cur;
1298
1299                 if (!(numstr = ast_variable_retrieve(cfg, catg, "phonenumber"))) {
1300                         continue;
1301                 }
1302                 if (!(timeoutstr = ast_variable_retrieve(cfg, catg, "timeout"))
1303                         || sscanf(timeoutstr, "%30d", &timeout) != 1
1304                         || timeout < 1) {
1305                         timeout = 25;
1306                 }
1307                 /* This one has to exist; it was part of the query */
1308                 ordstr = ast_variable_retrieve(cfg, catg, "ordinal");
1309                 ast_str_set(&str, 0, "%s", numstr);
1310                 if ((cur = create_followme_number(ast_str_buffer(str), timeout, atoi(ordstr)))) {
1311                         AST_LIST_INSERT_TAIL(&new_follower->numbers, cur, entry);
1312                 }
1313         }
1314         ast_config_destroy(cfg);
1315
1316         ast_free(str);
1317         return new_follower;
1318 }
1319
1320 static void end_bridge_callback(void *data)
1321 {
1322         char buf[80];
1323         time_t end;
1324         struct ast_channel *chan = data;
1325
1326         time(&end);
1327
1328         ast_channel_lock(chan);
1329         snprintf(buf, sizeof(buf), "%d", ast_channel_get_up_time(chan));
1330         pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
1331         snprintf(buf, sizeof(buf), "%d", ast_channel_get_duration(chan));
1332         pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
1333         ast_channel_unlock(chan);
1334 }
1335
1336 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
1337 {
1338         bconfig->end_bridge_callback_data = originator;
1339 }
1340
1341 static int app_exec(struct ast_channel *chan, const char *data)
1342 {
1343         struct fm_args *targs;
1344         struct ast_bridge_config config;
1345         struct call_followme *f;
1346         struct number *nm, *newnm;
1347         int res = 0;
1348         char *argstr;
1349         struct ast_channel *caller;
1350         struct ast_channel *outbound;
1351         AST_DECLARE_APP_ARGS(args,
1352                 AST_APP_ARG(followmeid);
1353                 AST_APP_ARG(options);
1354         );
1355         char *opt_args[FOLLOWMEFLAG_ARG_ARRAY_SIZE];
1356         int max_forwards;
1357
1358         if (ast_strlen_zero(data)) {
1359                 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1360                 return -1;
1361         }
1362
1363         ast_channel_lock(chan);
1364         max_forwards = ast_max_forwards_get(chan);
1365         ast_channel_unlock(chan);
1366
1367         if (max_forwards <= 0) {
1368                 ast_log(LOG_WARNING, "Unable to execute FollowMe on channel %s. Max forwards exceeded\n",
1369                                 ast_channel_name(chan));
1370                 return -1;
1371         }
1372
1373         argstr = ast_strdupa((char *) data);
1374
1375         AST_STANDARD_APP_ARGS(args, argstr);
1376
1377         if (ast_strlen_zero(args.followmeid)) {
1378                 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1379                 return -1;
1380         }
1381
1382         targs = ast_calloc(1, sizeof(*targs));
1383         if (!targs) {
1384                 return -1;
1385         }
1386
1387         AST_RWLIST_RDLOCK(&followmes);
1388         AST_RWLIST_TRAVERSE(&followmes, f, entry) {
1389                 if (!strcasecmp(f->name, args.followmeid) && (f->active))
1390                         break;
1391         }
1392         AST_RWLIST_UNLOCK(&followmes);
1393
1394         ast_debug(1, "New profile %s.\n", args.followmeid);
1395
1396         if (!f) {
1397                 f = find_realtime(args.followmeid);
1398         }
1399
1400         if (!f) {
1401                 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
1402                 ast_free(targs);
1403                 return 0;
1404         }
1405
1406         /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
1407         if (args.options) {
1408                 ast_app_parse_options(followme_opts, &targs->followmeflags, opt_args, args.options);
1409         }
1410
1411         /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
1412         ast_mutex_lock(&f->lock);
1413         targs->enable_callee_prompt = f->enable_callee_prompt;
1414         targs->mohclass = ast_strdupa(f->moh);
1415         ast_copy_string(targs->context, f->context, sizeof(targs->context));
1416         ast_copy_string(targs->takecall, f->takecall, sizeof(targs->takecall));
1417         ast_copy_string(targs->nextindp, f->nextindp, sizeof(targs->nextindp));
1418         ast_copy_string(targs->callfromprompt, f->callfromprompt, sizeof(targs->callfromprompt));
1419         ast_copy_string(targs->norecordingprompt, f->norecordingprompt, sizeof(targs->norecordingprompt));
1420         ast_copy_string(targs->optionsprompt, f->optionsprompt, sizeof(targs->optionsprompt));
1421         ast_copy_string(targs->plsholdprompt, f->plsholdprompt, sizeof(targs->plsholdprompt));
1422         ast_copy_string(targs->statusprompt, f->statusprompt, sizeof(targs->statusprompt));
1423         ast_copy_string(targs->sorryprompt, f->sorryprompt, sizeof(targs->sorryprompt));
1424         ast_copy_string(targs->connprompt, f->connprompt, sizeof(targs->connprompt));
1425         /* Copy the numbers we're going to use into another list in case the master list should get modified
1426            (and locked) while we're trying to do a follow-me */
1427         AST_LIST_HEAD_INIT_NOLOCK(&targs->cnumbers);
1428         AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
1429                 newnm = create_followme_number(nm->number, nm->timeout, nm->order);
1430                 if (newnm) {
1431                         AST_LIST_INSERT_TAIL(&targs->cnumbers, newnm, entry);
1432                 }
1433         }
1434         ast_mutex_unlock(&f->lock);
1435
1436         /* PREDIAL: Preprocess any callee gosub arguments. */
1437         if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_PREDIAL_CALLEE)
1438                 && !ast_strlen_zero(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE])) {
1439                 ast_replace_subargument_delimiter(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE]);
1440                 targs->predial_callee =
1441                         ast_app_expand_sub_args(chan, opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE]);
1442         }
1443
1444         /* PREDIAL: Run gosub on the caller's channel */
1445         if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_PREDIAL_CALLER)
1446                 && !ast_strlen_zero(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER])) {
1447                 ast_replace_subargument_delimiter(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER]);
1448                 ast_app_exec_sub(NULL, chan, opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER], 0);
1449         }
1450
1451         /* Forget the 'N' option if the call is already up. */
1452         if (ast_channel_state(chan) == AST_STATE_UP) {
1453                 ast_clear_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER);
1454         }
1455
1456         if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1457                 ast_indicate(chan, AST_CONTROL_RINGING);
1458         } else {
1459                 /* Answer the call */
1460                 if (ast_channel_state(chan) != AST_STATE_UP) {
1461                         ast_answer(chan);
1462                 }
1463
1464                 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_STATUSMSG)) {
1465                         ast_stream_and_wait(chan, targs->statusprompt, "");
1466                 }
1467
1468                 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_RECORDNAME)) {
1469                         int duration = 5;
1470
1471                         snprintf(targs->namerecloc, sizeof(targs->namerecloc), "%s/followme.%s",
1472                                 ast_config_AST_SPOOL_DIR, ast_channel_uniqueid(chan));
1473                         if (ast_play_and_record(chan, "vm-rec-name", targs->namerecloc, 5, REC_FORMAT, &duration,
1474                                 NULL, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL) < 0) {
1475                                 goto outrun;
1476                         }
1477                         if (!ast_fileexists(targs->namerecloc, NULL, ast_channel_language(chan))) {
1478                                 targs->namerecloc[0] = '\0';
1479                         }
1480                 }
1481
1482                 if (!ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_DISABLEHOLDPROMPT)) {
1483                         if (ast_streamfile(chan, targs->plsholdprompt, ast_channel_language(chan))) {
1484                                 goto outrun;
1485                         }
1486                         if (ast_waitstream(chan, "") < 0)
1487                                 goto outrun;
1488                 }
1489                 ast_moh_start(chan, targs->mohclass, NULL);
1490         }
1491
1492         ast_channel_lock(chan);
1493         ast_connected_line_copy_from_caller(&targs->connected_in, ast_channel_caller(chan));
1494         ast_channel_unlock(chan);
1495
1496         outbound = findmeexec(targs, chan);
1497         if (!outbound) {
1498                 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1499                         if (ast_channel_state(chan) != AST_STATE_UP) {
1500                                 ast_answer(chan);
1501                         }
1502                 } else {
1503                         ast_moh_stop(chan);
1504                 }
1505
1506                 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG)) {
1507                         ast_stream_and_wait(chan, targs->sorryprompt, "");
1508                 }
1509                 res = 0;
1510         } else {
1511                 caller = chan;
1512
1513                 /* Play "connecting" message to the winner, if configured. */
1514                 if (!ast_strlen_zero(targs->connprompt)) {
1515                         ast_autoservice_start(caller);
1516                         ast_stream_and_wait(outbound, targs->connprompt, "");
1517                         ast_autoservice_stop(caller);
1518                 }
1519
1520                 /* Bridge the two channels. */
1521
1522                 memset(&config, 0, sizeof(config));
1523                 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1524                 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1525                 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1526                 config.end_bridge_callback = end_bridge_callback;
1527                 config.end_bridge_callback_data = chan;
1528                 config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
1529
1530                 /* Update connected line to caller if available. */
1531                 if (targs->pending_out_connected_update) {
1532                         if (ast_channel_connected_line_sub(outbound, caller, &targs->connected_out, 0) &&
1533                                 ast_channel_connected_line_macro(outbound, caller, &targs->connected_out, 1, 0)) {
1534                                 ast_channel_update_connected_line(caller, &targs->connected_out, NULL);
1535                         }
1536                 }
1537
1538                 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1539                         if (ast_channel_state(caller) != AST_STATE_UP) {
1540                                 ast_answer(caller);
1541                         }
1542                 } else {
1543                         ast_moh_stop(caller);
1544                 }
1545
1546                 /* Be sure no generators are left on it */
1547                 ast_deactivate_generator(caller);
1548                 /* Make sure channels are compatible */
1549                 res = ast_channel_make_compatible(caller, outbound);
1550                 if (res < 0) {
1551                         ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", ast_channel_name(caller), ast_channel_name(outbound));
1552                         ast_autoservice_chan_hangup_peer(caller, outbound);
1553                         goto outrun;
1554                 }
1555
1556                 /* Update connected line to winner if changed. */
1557                 if (targs->pending_in_connected_update) {
1558                         if (ast_channel_connected_line_sub(caller, outbound, &targs->connected_in, 0) &&
1559                                 ast_channel_connected_line_macro(caller, outbound, &targs->connected_in, 0, 0)) {
1560                                 ast_channel_update_connected_line(outbound, &targs->connected_in, NULL);
1561                         }
1562                 }
1563
1564                 /* Put winner on hold if caller requested. */
1565                 if (targs->pending_hold) {
1566                         if (ast_strlen_zero(targs->suggested_moh)) {
1567                                 ast_indicate_data(outbound, AST_CONTROL_HOLD, NULL, 0);
1568                         } else {
1569                                 ast_indicate_data(outbound, AST_CONTROL_HOLD,
1570                                         targs->suggested_moh, strlen(targs->suggested_moh) + 1);
1571                         }
1572                 }
1573
1574                 res = ast_bridge_call(caller, outbound, &config);
1575         }
1576
1577 outrun:
1578         while ((nm = AST_LIST_REMOVE_HEAD(&targs->cnumbers, entry))) {
1579                 ast_free(nm);
1580         }
1581         if (!ast_strlen_zero(targs->namerecloc)) {
1582                 int ret;
1583                 char fn[PATH_MAX + sizeof(REC_FORMAT)];
1584
1585                 snprintf(fn, sizeof(fn), "%s.%s", targs->namerecloc,
1586                              REC_FORMAT);
1587                 ret = unlink(fn);
1588                 if (ret != 0) {
1589                         ast_log(LOG_NOTICE, "Failed to delete recorded name file %s: %d (%s)\n",
1590                                         fn, errno, strerror(errno));
1591                 } else {
1592                         ast_debug(2, "deleted recorded prompt %s.\n", fn);
1593                 }
1594         }
1595         ast_free((char *) targs->predial_callee);
1596         ast_party_connected_line_free(&targs->connected_in);
1597         ast_party_connected_line_free(&targs->connected_out);
1598         ast_free(targs);
1599
1600         if (f->realtime) {
1601                 /* Not in list */
1602                 free_numbers(f);
1603                 ast_free(f);
1604         }
1605
1606         return res;
1607 }
1608
1609 static int unload_module(void)
1610 {
1611         struct call_followme *f;
1612
1613         ast_unregister_application(app);
1614
1615         /* Free Memory. Yeah! I'm free! */
1616         AST_RWLIST_WRLOCK(&followmes);
1617         while ((f = AST_RWLIST_REMOVE_HEAD(&followmes, entry))) {
1618                 free_numbers(f);
1619                 ast_free(f);
1620         }
1621
1622         AST_RWLIST_UNLOCK(&followmes);
1623
1624         return 0;
1625 }
1626
1627 /*!
1628  * \brief Load the module
1629  *
1630  * Module loading including tests for configuration or dependencies.
1631  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
1632  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
1633  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
1634  * configuration file or other non-critical problem return
1635  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
1636  */
1637 static int load_module(void)
1638 {
1639         if(!reload_followme(0))
1640                 return AST_MODULE_LOAD_DECLINE;
1641
1642         return ast_register_application_xml(app, app_exec);
1643 }
1644
1645 static int reload(void)
1646 {
1647         reload_followme(1);
1648
1649         return 0;
1650 }
1651
1652 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1653         .support_level = AST_MODULE_SUPPORT_CORE,
1654         .load = load_module,
1655         .unload = unload_module,
1656         .reload = reload,
1657 );