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